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 $
7 * Author: Benjamin Pracht <bigben at videolan dot org>
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.
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.
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. *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
28 #include <vlc_access.h>
29 #include <vlc_demux.h>
30 #include <vlc_input.h>
35 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <sys/errno.h>
40 #include <asm/types.h> /* for videodev2.h */
42 #include <linux/videodev2.h>
44 /*****************************************************************************
46 *****************************************************************************/
48 static int Open ( vlc_object_t * );
49 static void Close( vlc_object_t * );
51 #define DEV_TEXT N_("Device name")
52 #define DEV_LONGTEXT N_( \
53 "Name of the device to use. " \
54 "If you don't specify anything, /dev/video0 will be used.")
55 #define INPUT_TEXT N_( "Input" )
56 #define INPUT_LONGTEXT N_( \
57 "Input of the card to use (Usually, 0 = tuner, " \
58 "1 = composite, 2 = svideo)." )
62 set_shortname( _("Video4Linux2") );
63 set_description( _("Video4Linux2 input") );
64 set_category( CAT_INPUT );
65 set_subcategory( SUBCAT_INPUT_ACCESS );
67 add_string( "v4l2-dev", "/dev/video0", 0, DEV_TEXT, DEV_LONGTEXT,
69 add_integer( "v4l2-input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
72 add_shortcut( "v4l2" );
73 set_capability( "access_demux", 10 );
74 set_callbacks( Open, Close );
77 /*****************************************************************************
78 * Access: local prototypes
79 *****************************************************************************/
81 static int DemuxMMAP( demux_t * );
82 static int Control( demux_t *, int, va_list );
84 static int ProbeDev( demux_t * );
85 static int OpenVideoDev( demux_t * );
93 struct v4l2_capability dev_cap;
96 struct v4l2_input *p_inputs;
100 /* V4L2 devices cannot have more than 32 audio inputs */
101 struct v4l2_audio p_audios[32];
104 struct v4l2_tuner *p_tuners;
107 struct v4l2_fmtdesc *p_codecs;
110 /*****************************************************************************
111 * Open: opens v4l device
112 *****************************************************************************
114 * url: <video device>::::
116 *****************************************************************************/
117 static int Open( vlc_object_t *p_this )
119 demux_t *p_demux = (demux_t*)p_this;
122 /* Only when selected */
123 if( *p_demux->psz_access == '\0' )
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;
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 ) );
136 p_sys->psz_device = var_CreateGetString( p_demux, "v4l2-dev" );
138 if( ProbeDev( p_demux ) < 0 ) return VLC_EGENERIC;
140 if( OpenVideoDev( p_demux ) < 0 ) return VLC_EGENERIC;
145 /*****************************************************************************
146 * Close: close device, free resources
147 *****************************************************************************/
148 static void Close( vlc_object_t *p_this )
150 demux_t *p_demux = (demux_t *)p_this;
151 demux_sys_t *p_sys = p_demux->p_sys;
153 if( p_sys->i_fd_video >= 0 ) close( p_sys->i_fd_video );
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 );
163 /*****************************************************************************
165 *****************************************************************************/
166 static int Control( demux_t *p_demux, int i_query, va_list args )
171 /*****************************************************************************
173 *****************************************************************************/
174 static int DemuxMMAP( demux_t *p_demux )
181 /*****************************************************************************
182 * OpenVideoDev: open and set up the video device and probe for capabilities
183 *****************************************************************************/
184 int OpenVideoDev( demux_t *p_demux )
187 demux_sys_t *p_sys = p_demux->p_sys;
189 if( ( i_fd = open( p_sys->psz_device, O_RDWR ) ) < 0 )
191 msg_Err( p_demux, "cannot open device (%m)" );
195 p_sys->i_fd_video = i_fd;
196 p_sys->i_selected_input = var_CreateGetInteger( p_demux, "v4l2-input" );
198 if( p_sys->i_selected_input > p_sys->i_input )
200 msg_Warn( p_demux, "invalid input. Using the default one" );
201 p_sys->i_selected_input = 0;
204 if( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
206 msg_Err( p_demux, "cannot set input (%m)" );
210 if( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING )
212 struct v4l2_requestbuffers reqbuf;
214 reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
215 reqbuf.memory = V4L2_MEMORY_MMAP;
218 if( ioctl( i_fd, VIDIOC_REQBUFS, &reqbuf ) < 0 )
220 msg_Err( p_demux, "cannot initiate I/O operation (%m). "
221 "Only MMAP is supported at the moment" );
224 p_demux->pf_demux = DemuxMMAP;
228 msg_Warn( p_demux, "I/O method not supported at the moment" );
235 if( i_fd ) close( i_fd );
236 p_sys->i_fd_video = 0;
241 /*****************************************************************************
242 * ProbeDev: probe for capabilities
243 *****************************************************************************/
244 int ProbeDev( demux_t *p_demux )
249 demux_sys_t *p_sys = p_demux->p_sys;
251 if( ( i_fd = open( p_sys->psz_device, O_RDWR ) ) < 0 )
253 msg_Err( p_demux, "cannot open device (%m)" );
257 /* Get device capabilites */
259 if( ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
261 msg_Err( p_demux, "cannot get capabilities (%m)" );
265 msg_Dbg( p_demux, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
267 p_sys->dev_cap.driver,
268 (p_sys->dev_cap.version >> 16) & 0xFF,
269 (p_sys->dev_cap.version >> 8) & 0xFF,
270 p_sys->dev_cap.version & 0xFF,
271 p_sys->dev_cap.bus_info );
273 msg_Dbg( p_demux, "the device has the capabilities: (%c) Video Capure, "
276 ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE ? 'X':' '),
277 ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO ? 'X':' '),
278 ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER ? 'X':' ') );
280 msg_Dbg( p_demux, "supported I/O methods are: (%c) Read/Write, "
283 ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
284 ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
285 ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
287 /* Now, enumerate all the video inputs. This is useless at the moment
288 since we have no way to present that info to the user except with
291 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
293 while( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_input ) >= 0 )
298 p_sys->p_inputs = malloc( p_sys->i_input * sizeof( struct v4l2_input ) );
299 if( !p_sys->p_inputs ) goto open_failed;
300 memset( p_sys->p_inputs, 0, sizeof( struct v4l2_input ) );
302 for( i_index = 0; i_index < p_sys->i_input; i_index++ )
304 p_sys->p_inputs[i_index].index = i_index;
306 if( ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
309 "cannot get video input characteristics (%m)" );
312 msg_Dbg( p_demux, "video input %i (%s) has type: %s",
314 p_sys->p_inputs[i_index].name,
315 p_sys->p_inputs[i_index].type
316 == V4L2_INPUT_TYPE_TUNER ?
318 "External analog input" );
322 /* initialize the structures for the ioctls */
323 for( i_index = 0; i_index < 32; i_index++ )
325 p_sys->p_audios[i_index].index = i_index;
328 /* Probe audio inputs */
330 if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
332 while( p_sys->i_audio < 32 &&
333 ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
335 if( ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
338 "cannot get video input characteristics (%m)" );
342 msg_Dbg( p_demux, "audio device %i (%s) is %s",
344 p_sys->p_audios[p_sys->i_audio].name,
345 p_sys->p_audios[p_sys->i_audio].capability &
353 if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
355 struct v4l2_tuner tuner;
357 memset( &tuner, 0, sizeof(tuner) );
358 while( ioctl( i_fd, VIDIOC_S_TUNER, &tuner ) >= 0 )
361 tuner.index = p_sys->i_tuner;
364 p_sys->p_tuners = malloc( p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
365 if( !p_sys->p_tuners ) goto open_failed;
366 memset( p_sys->p_tuners, 0, sizeof( struct v4l2_tuner ) );
368 for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
370 p_sys->p_tuners[i_index].index = i_index;
372 if( ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
374 msg_Err( p_demux, "cannot get tuner characteristics (%m)" );
377 msg_Dbg( p_demux, "tuner %i (%s) has type: %s, "
378 "frequency range: %.1f %s -> %.1f %s",
380 p_sys->p_tuners[i_index].name,
381 p_sys->p_tuners[i_index].type
382 == V4L2_TUNER_RADIO ?
383 "Radio" : "Analog TV",
384 p_sys->p_tuners[i_index].rangelow * 62.5,
385 p_sys->p_tuners[i_index].capability &
388 p_sys->p_tuners[i_index].rangehigh * 62.5,
389 p_sys->p_tuners[i_index].capability &
395 /* Probe for available chromas */
396 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
398 struct v4l2_fmtdesc codec;
401 memset( &codec, 0, sizeof(codec) );
402 codec.index = i_index;
403 codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
405 while( ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
408 codec.index = i_index;
411 p_sys->i_codec = i_index;
413 p_sys->p_codecs = malloc( p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
414 memset( p_sys->p_codecs, 0, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
416 for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
418 p_sys->p_codecs[i_index].index = i_index;
419 p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
421 if( ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
423 msg_Err( p_demux, "cannot get codec description (%m)" );
427 msg_Dbg( p_demux, "device supports Codec %s",
428 p_sys->p_codecs[i_index].description );
433 if( i_fd >= 0 ) close( i_fd );
438 if( i_fd >= 0 ) close( i_fd );