1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>The DVR device</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="LINUX MEDIA INFRASTRUCTURE API"><link rel="up" href="dvb_examples.html" title="Chapter 16. Examples"><link rel="prev" href="dvb_examples.html" title="Chapter 16. Examples"><link rel="next" href="audio_h.html" title="Appendix F. DVB Audio Header File"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">The DVR device</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="dvb_examples.html">Prev</a> </td><th width="60%" align="center">Chapter 16. Examples</th><td width="20%" align="right"> <a accesskey="n" href="audio_h.html">Next</a></td></tr></table><hr></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="the_dvr_device"></a>The DVR device</h2></div></div></div><p>The following program code shows how to use the DVR device for recording. 2</p><pre class="programlisting"> 3 #include <sys/ioctl.h> 4 #include <stdio.h> 5 #include <stdint.h> 6 #include <sys/types.h> 7 #include <sys/stat.h> 8 #include <fcntl.h> 9 #include <time.h> 10 #include <unistd.h> 11 12 #include <linux/dvb/dmx.h> 13 #include <linux/dvb/video.h> 14 #include <sys/poll.h> 15 #define DVR "/dev/dvb/adapter0/dvr1" 16 #define AUDIO "/dev/dvb/adapter0/audio1" 17 #define VIDEO "/dev/dvb/adapter0/video1" 18 19 #define BUFFY (188⋆20) 20 #define MAX_LENGTH (1024⋆1024⋆5) /⋆ record 5MB ⋆/ 21 22 23 /⋆ switch the demuxes to recording, assuming the transponder is tuned ⋆/ 24 25 /⋆ demux1, demux2: file descriptor of video and audio filters ⋆/ 26 /⋆ vpid, apid: PIDs of video and audio channels ⋆/ 27 28 int switch_to_record(int demux1, int demux2, uint16_t vpid, uint16_t apid) 29 { 30 struct dmx_pes_filter_params pesFilterParams; 31 32 if (demux1 < 0){ 33 if ((demux1=open(DMX, O_RDWR|O_NONBLOCK)) 34 < 0){ 35 perror("DEMUX DEVICE: "); 36 return -1; 37 } 38 } 39 40 if (demux2 < 0){ 41 if ((demux2=open(DMX, O_RDWR|O_NONBLOCK)) 42 < 0){ 43 perror("DEMUX DEVICE: "); 44 return -1; 45 } 46 } 47 48 pesFilterParams.pid = vpid; 49 pesFilterParams.input = DMX_IN_FRONTEND; 50 pesFilterParams.output = DMX_OUT_TS_TAP; 51 pesFilterParams.pes_type = DMX_PES_VIDEO; 52 pesFilterParams.flags = DMX_IMMEDIATE_START; 53 if (ioctl(demux1, DMX_SET_PES_FILTER, &pesFilterParams) < 0){ 54 perror("DEMUX DEVICE"); 55 return -1; 56 } 57 pesFilterParams.pid = apid; 58 pesFilterParams.input = DMX_IN_FRONTEND; 59 pesFilterParams.output = DMX_OUT_TS_TAP; 60 pesFilterParams.pes_type = DMX_PES_AUDIO; 61 pesFilterParams.flags = DMX_IMMEDIATE_START; 62 if (ioctl(demux2, DMX_SET_PES_FILTER, &pesFilterParams) < 0){ 63 perror("DEMUX DEVICE"); 64 return -1; 65 } 66 return 0; 67 } 68 69 /⋆ start recording MAX_LENGTH , assuming the transponder is tuned ⋆/ 70 71 /⋆ demux1, demux2: file descriptor of video and audio filters ⋆/ 72 /⋆ vpid, apid: PIDs of video and audio channels ⋆/ 73 int record_dvr(int demux1, int demux2, uint16_t vpid, uint16_t apid) 74 { 75 int i; 76 int len; 77 int written; 78 uint8_t buf[BUFFY]; 79 uint64_t length; 80 struct pollfd pfd[1]; 81 int dvr, dvr_out; 82 83 /⋆ open dvr device ⋆/ 84 if ((dvr = open(DVR, O_RDONLY|O_NONBLOCK)) < 0){ 85 perror("DVR DEVICE"); 86 return -1; 87 } 88 89 /⋆ switch video and audio demuxes to dvr ⋆/ 90 printf ("Switching dvr on\n"); 91 i = switch_to_record(demux1, demux2, vpid, apid); 92 printf("finished: "); 93 94 printf("Recording %2.0f MB of test file in TS format\n", 95 MAX_LENGTH/(1024.0⋆1024.0)); 96 length = 0; 97 98 /⋆ open output file ⋆/ 99 if ((dvr_out = open(DVR_FILE,O_WRONLY|O_CREAT 100 |O_TRUNC, S_IRUSR|S_IWUSR 101 |S_IRGRP|S_IWGRP|S_IROTH| 102 S_IWOTH)) < 0){ 103 perror("Can't open file for dvr test"); 104 return -1; 105 } 106 107 pfd[0].fd = dvr; 108 pfd[0].events = POLLIN; 109 110 /⋆ poll for dvr data and write to file ⋆/ 111 while (length < MAX_LENGTH ) { 112 if (poll(pfd,1,1)){ 113 if (pfd[0].revents & POLLIN){ 114 len = read(dvr, buf, BUFFY); 115 if (len < 0){ 116 perror("recording"); 117 return -1; 118 } 119 if (len > 0){ 120 written = 0; 121 while (written < len) 122 written += 123 write (dvr_out, 124 buf, len); 125 length += len; 126 printf("written %2.0f MB\r", 127 length/1024./1024.); 128 } 129 } 130 } 131 } 132 return 0; 133 } 134 135</pre></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="dvb_examples.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="dvb_examples.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="audio_h.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 16. Examples </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Appendix F. DVB Audio Header File</td></tr></table></div></body></html> 136