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&#160;16.&#160;Examples"><link rel="prev" href="dvb_examples.html" title="Chapter&#160;16.&#160;Examples"><link rel="next" href="audio_h.html" title="Appendix&#160;F.&#160;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>&#160;</td><th width="60%" align="center">Chapter&#160;16.&#160;Examples</th><td width="20%" align="right">&#160;<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 &lt;sys/ioctl.h&gt;
4 #include &lt;stdio.h&gt;
5 #include &lt;stdint.h&gt;
6 #include &lt;sys/types.h&gt;
7 #include &lt;sys/stat.h&gt;
8 #include &lt;fcntl.h&gt;
9 #include &lt;time.h&gt;
10 #include &lt;unistd.h&gt;
11
12 #include &lt;linux/dvb/dmx.h&gt;
13 #include &lt;linux/dvb/video.h&gt;
14 #include &lt;sys/poll.h&gt;
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&#8902;20)
20 #define MAX_LENGTH (1024&#8902;1024&#8902;5) /&#8902; record 5MB &#8902;/
21
22
23 /&#8902; switch the demuxes to recording, assuming the transponder is tuned &#8902;/
24
25 /&#8902; demux1, demux2: file descriptor of video and audio filters &#8902;/
26 /&#8902; vpid, apid:     PIDs of video and audio channels           &#8902;/
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 &lt; 0){
33		 if ((demux1=open(DMX, O_RDWR|O_NONBLOCK))
34		     &lt; 0){
35			 perror("DEMUX DEVICE: ");
36			 return -1;
37		 }
38	 }
39
40	 if (demux2 &lt; 0){
41		 if ((demux2=open(DMX, O_RDWR|O_NONBLOCK))
42		     &lt; 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, &amp;pesFilterParams) &lt; 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, &amp;pesFilterParams) &lt; 0){
63		 perror("DEMUX DEVICE");
64		 return -1;
65	 }
66	 return 0;
67 }
68
69 /&#8902; start recording MAX_LENGTH , assuming the transponder is tuned &#8902;/
70
71 /&#8902; demux1, demux2: file descriptor of video and audio filters &#8902;/
72 /&#8902; vpid, apid:     PIDs of video and audio channels           &#8902;/
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	 /&#8902; open dvr device &#8902;/
84	 if ((dvr = open(DVR, O_RDONLY|O_NONBLOCK)) &lt; 0){
85			 perror("DVR DEVICE");
86			 return -1;
87	 }
88
89	 /&#8902; switch video and audio demuxes to dvr &#8902;/
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&#8902;1024.0));
96	 length = 0;
97
98	 /&#8902; open output file &#8902;/
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)) &lt; 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	 /&#8902; poll for dvr data and write to file &#8902;/
111	 while (length &lt; MAX_LENGTH ) {
112		 if (poll(pfd,1,1)){
113			 if (pfd[0].revents &amp; POLLIN){
114				 len = read(dvr, buf, BUFFY);
115				 if (len &lt; 0){
116					 perror("recording");
117					 return -1;
118				 }
119				 if (len &gt; 0){
120					 written = 0;
121					 while (written &lt; 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>&#160;</td><td width="20%" align="center"><a accesskey="u" href="dvb_examples.html">Up</a></td><td width="40%" align="right">&#160;<a accesskey="n" href="audio_h.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter&#160;16.&#160;Examples&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;Appendix&#160;F.&#160;DVB Audio Header File</td></tr></table></div></body></html>
136