]> git.sesse.net Git - vlc/blob - modules/access/v4l2.c
1f60f2c6d6bed3854c5c89cae47d93107cf0ce68
[vlc] / modules / access / v4l2.c
1 /*****************************************************************************
2  * v4l2.c : Video4Linux2 input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2004 the VideoLAN team
5  * $Id: v4l.c 16084 2006-07-19 09:45:02Z zorglub $
6  *
7  * Author: Benjamin Pracht <bigben at videolan dot org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <<vlc_input.h>>
32 #include <<vlc_vout.h>>
33
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <sys/time.h>
38 #include <sys/ioctl.h>
39 #include <sys/errno.h>
40
41 #include <asm/types.h>          /* for videodev2.h */
42
43 #include <linux/videodev2.h>
44
45 /*****************************************************************************
46  * Module descriptior
47   *****************************************************************************/
48
49 static int  Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
51
52 #define DEV_TEXT N_("Device name")
53 #define DEV_LONGTEXT N_( \
54     "Name of the device to use. " \
55     "If you don't specify anything, /dev/video0 will be used.")
56 #define INPUT_TEXT N_( "Input" )
57 #define INPUT_LONGTEXT N_( \
58     "Input of the card to use (Usually, 0 = tuner, " \
59     "1 = composite, 2 = svideo)." )
60
61
62 vlc_module_begin();
63     set_shortname( _("Video4Linux2") );
64     set_description( _("Video4Linux2 input") );
65     set_category( CAT_INPUT );
66     set_subcategory( SUBCAT_INPUT_ACCESS );
67
68     add_string( "v4l2-dev", "/dev/video0", 0, DEV_TEXT, DEV_LONGTEXT,
69                 VLC_FALSE );
70     add_integer( "v4l2-input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
71                 VLC_TRUE );
72
73     add_shortcut( "v4l2" );
74     set_capability( "access_demux", 10 );
75     set_callbacks( Open, Close );
76 vlc_module_end();
77
78 /*****************************************************************************
79  * Access: local prototypes
80  *****************************************************************************/
81
82 static int DemuxMMAP( demux_t * );
83 static int Control( demux_t *, int, va_list );
84
85 static int ProbeDev( demux_t * );
86 static int OpenVideoDev( demux_t * );
87
88 static block_t *GrabVideo( demux_t * );
89
90 struct demux_sys_t
91 {
92     char *psz_device;
93
94     int i_fd_video;
95
96     struct v4l2_capability dev_cap;
97
98     int i_input;
99     struct v4l2_input *p_inputs;
100     int i_selected_input;
101
102     int i_audio;
103     /* V4L2 devices cannot have more than 32 audio inputs */
104     struct v4l2_audio p_audios[32];
105
106     int i_tuner;
107     struct v4l2_tuner *p_tuners;
108
109     int i_codec;
110     struct v4l2_fmtdesc *p_codecs;
111 };
112
113 /*****************************************************************************
114  * Open: opens v4l device
115  *****************************************************************************
116  *
117  * url: <video device>::::
118  *
119  *****************************************************************************/
120 static int Open( vlc_object_t *p_this )
121 {
122     demux_t     *p_demux = (demux_t*)p_this;
123     demux_sys_t *p_sys;
124     demux_sys_t sys;
125
126     /* Set up p_demux */
127     p_demux->pf_control = Control;
128     p_demux->info.i_update = 0;
129     p_demux->info.i_title = 0;
130     p_demux->info.i_seekpoint = 0;
131
132     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
133     if( p_sys == NULL ) return VLC_ENOMEM;
134     memset( p_sys, 0, sizeof( demux_sys_t ) );
135
136     p_sys->psz_device = var_CreateGetString( p_demux, "v4l2-dev" );
137
138     if( ProbeDev( p_demux ) < 0 ) return VLC_EGENERIC;
139
140     if( OpenVideoDev( p_demux ) < 0 ) return VLC_EGENERIC;
141
142     return VLC_SUCCESS;
143 }
144
145 /*****************************************************************************
146  * Close: close device, free resources
147  *****************************************************************************/
148 static void Close( vlc_object_t *p_this )
149 {
150     demux_t     *p_demux = (demux_t *)p_this;
151     demux_sys_t *p_sys   = p_demux->p_sys;
152
153     if( p_sys->i_fd_video >= 0 ) close( p_sys->i_fd_video );
154
155     if( p_sys->psz_device ) free( p_sys->psz_device );
156     if( p_sys->p_inputs ) free( p_sys->p_inputs );
157     if( p_sys->p_tuners ) free( p_sys->p_tuners );
158     if( p_sys->p_codecs ) free( p_sys->p_codecs );
159
160     free( p_sys );
161 }
162
163 /*****************************************************************************
164  * Control:
165  *****************************************************************************/
166 static int Control( demux_t *p_demux, int i_query, va_list args )
167 {
168 }
169
170 /*****************************************************************************
171  * Demux:
172  *****************************************************************************/
173 static int DemuxMMAP( demux_t *p_demux )
174 {
175 }
176
177
178 /*****************************************************************************
179  * OpenVideoDev: open and set up the video device and probe for capabilities
180  *****************************************************************************/
181 int OpenVideoDev( demux_t *p_demux )
182 {
183     int i_fd;
184     demux_sys_t *p_sys = p_demux->p_sys;
185
186     if( ( i_fd = open( p_sys->psz_device, O_RDWR ) ) < 0 )
187     {
188         msg_Err( p_demux, "cannot open device (%s)", strerror( errno ) );
189         goto open_failed;
190     }
191
192     p_sys->i_fd_video = i_fd;
193
194     if( p_sys->i_selected_input = var_CreateGetInteger( p_demux, "v4l2-input" )
195         > p_sys->i_input )
196     {
197         msg_Warn( p_demux, "invalid input. Using the default one" );
198         p_sys->i_selected_input = 0;
199     }
200
201     if( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
202     {
203        msg_Err( p_demux, "cannot set input (%s)", strerror( errno ) );
204        goto open_failed;
205     }
206
207     if( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING )
208     {
209         struct v4l2_requestbuffers reqbuf;
210
211         reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
212         reqbuf.memory = V4L2_MEMORY_MMAP;
213         reqbuf.count = 0;
214
215         if( ioctl( i_fd, VIDIOC_REQBUFS, &reqbuf ) < 0 )
216         {
217             msg_Err( p_demux, "cannot initiate I/O operation (%s). "
218                     "Only MMAP is supported at the moment", strerror( errno ) );
219             goto open_failed;
220         }
221         p_demux->pf_demux = DemuxMMAP;
222     }
223     else
224     {
225         msg_Warn( p_demux, "I/O method not supported at the moment" );
226         goto open_failed;
227     }
228
229     return VLC_SUCCESS;
230
231 open_failed:
232     if( i_fd ) close( i_fd );
233     p_sys->i_fd_video = 0;
234     return VLC_EGENERIC;
235
236 }
237
238 /*****************************************************************************
239  * ProbeDev: probe for capabilities
240  *****************************************************************************/
241 int ProbeDev( demux_t *p_demux )
242 {
243     int i_index;
244
245     int i_fd;
246     demux_sys_t *p_sys = p_demux->p_sys;
247
248     if( ( i_fd = open( p_sys->psz_device, O_RDWR ) ) < 0 )
249     {
250         msg_Err( p_demux, "cannot open device (%s)", strerror( errno ) );
251         goto open_failed;
252     }
253
254     /* Get device capabilites */
255
256     if( ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
257     {
258         msg_Err( p_demux, "cannot get capabilities (%s)", strerror( errno ) );
259         goto open_failed;
260     }
261
262   msg_Dbg( p_demux, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
263                             p_sys->dev_cap.card,
264                             p_sys->dev_cap.driver,
265                             (p_sys->dev_cap.version >> 16) & 0xFF,
266                             (p_sys->dev_cap.version >> 8) & 0xFF,
267                             p_sys->dev_cap.version & 0xFF,
268                             p_sys->dev_cap.bus_info );
269
270     msg_Dbg( p_demux, "the device has the capabilities: (%c) Video Capure, "
271                                                        "(%c) Audio, "
272                                                        "(%c) Tuner",
273              ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE  ? 'X':' '),
274              ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO  ? 'X':' '),
275              ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER  ? 'X':' ') );
276
277     msg_Dbg( p_demux, "supported I/O methods are: (%c) Read/Write, "
278                                                  "(%c) Streaming, "
279                                                  "(%c) Asynchronous",
280             ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
281             ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
282             ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
283
284     /* Now, enumerate all the video inputs. This is useless at the moment
285        since we have no way to present that info to the user except with
286        debug messages */
287
288     if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
289     {
290         while( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_input ) >= 0 )
291         {
292             p_sys->i_input++;
293         }
294
295         p_sys->p_inputs = malloc( p_sys->i_input * sizeof( struct v4l2_input ) );
296         if( !p_sys->p_inputs ) goto open_failed;
297         memset( p_sys->p_inputs, 0, sizeof( struct v4l2_input ) );
298
299         for( i_index = 0; i_index < p_sys->i_input; i_index++ )
300         {
301             p_sys->p_inputs[i_index].index = i_index;
302
303             if( ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
304             {
305                 msg_Err( p_demux, "cannot get video input characteristics (%s)",
306                                                  strerror( errno ) );
307                 goto open_failed;
308             }
309             msg_Dbg( p_demux, "video input %i (%s) has type: %s",
310                                 i_index,
311                                 p_sys->p_inputs[i_index].name,
312                                 p_sys->p_inputs[i_index].type
313                                         == V4L2_INPUT_TYPE_TUNER ?
314                                         "Tuner adapter" :
315                                         "External analog input" );
316         }
317     }
318
319     /* initialize the structures for the ioctls */
320     for( i_index = 0; i_index < 32; i_index++ )
321     {
322         p_sys->p_audios[i_index].index = i_index;
323     }
324
325     /* Probe audio inputs */
326
327     if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
328     {
329         while( p_sys->i_audio < 32 &&
330                ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
331         {
332             if( ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
333             {
334                 msg_Err( p_demux, "cannot get video input characteristics (%s)",
335                                                  strerror( errno ) );
336                 goto open_failed;
337             }
338
339             msg_Dbg( p_demux, "audio device %i (%s) is %s",
340                                 p_sys->i_audio,
341                                 p_sys->p_audios[p_sys->i_audio].name,
342                                 p_sys->p_audios[p_sys->i_audio].capability &
343                                                     V4L2_AUDCAP_STEREO ?
344                                         "Stereo" : "Mono" );
345
346             p_sys->i_audio++;
347         }
348     }
349
350     if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
351     {
352         struct v4l2_tuner tuner = {};
353
354         while( ioctl( i_fd, VIDIOC_S_TUNER, &tuner ) >= 0 )
355         {
356             p_sys->i_tuner++;
357             tuner.index = p_sys->i_tuner;
358         }
359
360         p_sys->p_tuners = malloc( p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
361         if( !p_sys->p_tuners ) goto open_failed;
362         memset( p_sys->p_tuners, 0, sizeof( struct v4l2_tuner ) );
363
364         for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
365         {
366             p_sys->p_tuners[i_index].index = i_index;
367
368             if( ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
369             {
370                 msg_Err( p_demux, "cannot get tuner characteristics (%s)",
371                                                  strerror( errno ) );
372                 goto open_failed;
373             }
374             msg_Dbg( p_demux, "tuner %i (%s) has type: %s, "
375                               "frequency range: %.1f %s -> %.1f %s",
376                                 i_index,
377                                 p_sys->p_tuners[i_index].name,
378                                 p_sys->p_tuners[i_index].type
379                                         == V4L2_TUNER_RADIO ?
380                                         "Radio" : "Analog TV",
381                                 p_sys->p_tuners[i_index].rangelow * 62.5,
382                                 p_sys->p_tuners[i_index].capability &
383                                         V4L2_TUNER_CAP_LOW ?
384                                         "Hz" : "kHz",
385                                 p_sys->p_tuners[i_index].rangehigh * 62.5,
386                                 p_sys->p_tuners[i_index].capability &
387                                         V4L2_TUNER_CAP_LOW ?
388                                         "Hz" : "kHz" );
389         }
390     }
391
392     /* Probe for available chromas */
393     if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
394     {
395         struct v4l2_fmtdesc codec = {};
396
397         i_index = 0;
398         codec.index = i_index;
399         codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
400
401         while( ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
402         {
403             i_index++;
404             codec.index = i_index;
405         }
406
407         p_sys->i_codec = i_index;
408
409         p_sys->p_codecs = malloc( p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
410         memset( p_sys->p_codecs, 0, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) ); 
411
412         for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
413         {
414             p_sys->p_codecs[i_index].index = i_index;
415             p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
416
417             if( ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
418             {
419                 msg_Err( p_demux, "cannot get codec description (%s)", strerror( errno ) );
420                 goto open_failed;
421             }
422
423             msg_Dbg( p_demux, "device supports Codec %s",
424                                 p_sys->p_codecs[i_index].description );
425         }
426     }
427
428
429     if( i_fd >= 0 ) close( i_fd );
430     return VLC_SUCCESS;
431
432 open_failed:
433     
434     if( i_fd >= 0 ) close( i_fd );
435     return VLC_EGENERIC;
436
437 }