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 *****************************************************************************/
31 #include <vlc_access.h>
32 #include <vlc_input.h>
37 #include <sys/types.h>
39 #include <sys/ioctl.h>
40 #include <sys/errno.h>
42 #include <asm/types.h> /* for videodev2.h */
44 #include <linux/videodev2.h>
46 /*****************************************************************************
48 *****************************************************************************/
50 static int Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
53 #define DEV_TEXT N_("Device name")
54 #define DEV_LONGTEXT N_( \
55 "Name of the device to use. " \
56 "If you don't specify anything, /dev/video0 will be used.")
57 #define INPUT_TEXT N_( "Input" )
58 #define INPUT_LONGTEXT N_( \
59 "Input of the card to use (Usually, 0 = tuner, " \
60 "1 = composite, 2 = svideo)." )
64 set_shortname( _("Video4Linux2") );
65 set_description( _("Video4Linux2 input") );
66 set_category( CAT_INPUT );
67 set_subcategory( SUBCAT_INPUT_ACCESS );
69 add_string( "v4l2-dev", "/dev/video0", 0, DEV_TEXT, DEV_LONGTEXT,
71 add_integer( "v4l2-input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
74 add_shortcut( "v4l2" );
75 set_capability( "access_demux", 10 );
76 set_callbacks( Open, Close );
79 /*****************************************************************************
80 * Access: local prototypes
81 *****************************************************************************/
83 static int DemuxMMAP( demux_t * );
84 static int Control( demux_t *, int, va_list );
86 static int ProbeDev( demux_t * );
87 static int OpenVideoDev( demux_t * );
89 static block_t *GrabVideo( demux_t * );
97 struct v4l2_capability dev_cap;
100 struct v4l2_input *p_inputs;
101 int i_selected_input;
104 /* V4L2 devices cannot have more than 32 audio inputs */
105 struct v4l2_audio p_audios[32];
108 struct v4l2_tuner *p_tuners;
111 struct v4l2_fmtdesc *p_codecs;
114 /*****************************************************************************
115 * Open: opens v4l device
116 *****************************************************************************
118 * url: <video device>::::
120 *****************************************************************************/
121 static int Open( vlc_object_t *p_this )
123 demux_t *p_demux = (demux_t*)p_this;
128 p_demux->pf_control = Control;
129 p_demux->info.i_update = 0;
130 p_demux->info.i_title = 0;
131 p_demux->info.i_seekpoint = 0;
133 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
134 if( p_sys == NULL ) return VLC_ENOMEM;
135 memset( p_sys, 0, sizeof( demux_sys_t ) );
137 p_sys->psz_device = var_CreateGetString( p_demux, "v4l2-dev" );
139 if( ProbeDev( p_demux ) < 0 ) return VLC_EGENERIC;
141 if( OpenVideoDev( p_demux ) < 0 ) return VLC_EGENERIC;
146 /*****************************************************************************
147 * Close: close device, free resources
148 *****************************************************************************/
149 static void Close( vlc_object_t *p_this )
151 demux_t *p_demux = (demux_t *)p_this;
152 demux_sys_t *p_sys = p_demux->p_sys;
154 if( p_sys->i_fd_video >= 0 ) close( p_sys->i_fd_video );
156 if( p_sys->psz_device ) free( p_sys->psz_device );
157 if( p_sys->p_inputs ) free( p_sys->p_inputs );
158 if( p_sys->p_tuners ) free( p_sys->p_tuners );
159 if( p_sys->p_codecs ) free( p_sys->p_codecs );
164 /*****************************************************************************
166 *****************************************************************************/
167 static int Control( demux_t *p_demux, int i_query, va_list args )
171 /*****************************************************************************
173 *****************************************************************************/
174 static int DemuxMMAP( demux_t *p_demux )
179 /*****************************************************************************
180 * OpenVideoDev: open and set up the video device and probe for capabilities
181 *****************************************************************************/
182 int OpenVideoDev( demux_t *p_demux )
185 demux_sys_t *p_sys = p_demux->p_sys;
187 if( ( i_fd = open( p_sys->psz_device, O_RDWR ) ) < 0 )
189 msg_Err( p_demux, "cannot open device (%s)", strerror( errno ) );
193 p_sys->i_fd_video = i_fd;
195 if( p_sys->i_selected_input = var_CreateGetInteger( p_demux, "v4l2-input" )
198 msg_Warn( p_demux, "invalid input. Using the default one" );
199 p_sys->i_selected_input = 0;
202 if( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
204 msg_Err( p_demux, "cannot set input (%s)", strerror( errno ) );
208 if( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING )
210 struct v4l2_requestbuffers reqbuf;
212 reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
213 reqbuf.memory = V4L2_MEMORY_MMAP;
216 if( ioctl( i_fd, VIDIOC_REQBUFS, &reqbuf ) < 0 )
218 msg_Err( p_demux, "cannot initiate I/O operation (%s). "
219 "Only MMAP is supported at the moment", strerror( errno ) );
222 p_demux->pf_demux = DemuxMMAP;
226 msg_Warn( p_demux, "I/O method not supported at the moment" );
233 if( i_fd ) close( i_fd );
234 p_sys->i_fd_video = 0;
239 /*****************************************************************************
240 * ProbeDev: probe for capabilities
241 *****************************************************************************/
242 int ProbeDev( demux_t *p_demux )
247 demux_sys_t *p_sys = p_demux->p_sys;
249 if( ( i_fd = open( p_sys->psz_device, O_RDWR ) ) < 0 )
251 msg_Err( p_demux, "cannot open device (%s)", strerror( errno ) );
255 /* Get device capabilites */
257 if( ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
259 msg_Err( p_demux, "cannot get capabilities (%s)", strerror( errno ) );
263 msg_Dbg( p_demux, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
265 p_sys->dev_cap.driver,
266 (p_sys->dev_cap.version >> 16) & 0xFF,
267 (p_sys->dev_cap.version >> 8) & 0xFF,
268 p_sys->dev_cap.version & 0xFF,
269 p_sys->dev_cap.bus_info );
271 msg_Dbg( p_demux, "the device has the capabilities: (%c) Video Capure, "
274 ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE ? 'X':' '),
275 ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO ? 'X':' '),
276 ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER ? 'X':' ') );
278 msg_Dbg( p_demux, "supported I/O methods are: (%c) Read/Write, "
281 ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
282 ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
283 ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
285 /* Now, enumerate all the video inputs. This is useless at the moment
286 since we have no way to present that info to the user except with
289 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
291 while( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_input ) >= 0 )
296 p_sys->p_inputs = malloc( p_sys->i_input * sizeof( struct v4l2_input ) );
297 if( !p_sys->p_inputs ) goto open_failed;
298 memset( p_sys->p_inputs, 0, sizeof( struct v4l2_input ) );
300 for( i_index = 0; i_index < p_sys->i_input; i_index++ )
302 p_sys->p_inputs[i_index].index = i_index;
304 if( ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
306 msg_Err( p_demux, "cannot get video input characteristics (%s)",
310 msg_Dbg( p_demux, "video input %i (%s) has type: %s",
312 p_sys->p_inputs[i_index].name,
313 p_sys->p_inputs[i_index].type
314 == V4L2_INPUT_TYPE_TUNER ?
316 "External analog input" );
320 /* initialize the structures for the ioctls */
321 for( i_index = 0; i_index < 32; i_index++ )
323 p_sys->p_audios[i_index].index = i_index;
326 /* Probe audio inputs */
328 if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
330 while( p_sys->i_audio < 32 &&
331 ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
333 if( ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
335 msg_Err( p_demux, "cannot get video input characteristics (%s)",
340 msg_Dbg( p_demux, "audio device %i (%s) is %s",
342 p_sys->p_audios[p_sys->i_audio].name,
343 p_sys->p_audios[p_sys->i_audio].capability &
351 if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
353 struct v4l2_tuner tuner = {};
355 while( ioctl( i_fd, VIDIOC_S_TUNER, &tuner ) >= 0 )
358 tuner.index = p_sys->i_tuner;
361 p_sys->p_tuners = malloc( p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
362 if( !p_sys->p_tuners ) goto open_failed;
363 memset( p_sys->p_tuners, 0, sizeof( struct v4l2_tuner ) );
365 for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
367 p_sys->p_tuners[i_index].index = i_index;
369 if( ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
371 msg_Err( p_demux, "cannot get tuner characteristics (%s)",
375 msg_Dbg( p_demux, "tuner %i (%s) has type: %s, "
376 "frequency range: %.1f %s -> %.1f %s",
378 p_sys->p_tuners[i_index].name,
379 p_sys->p_tuners[i_index].type
380 == V4L2_TUNER_RADIO ?
381 "Radio" : "Analog TV",
382 p_sys->p_tuners[i_index].rangelow * 62.5,
383 p_sys->p_tuners[i_index].capability &
386 p_sys->p_tuners[i_index].rangehigh * 62.5,
387 p_sys->p_tuners[i_index].capability &
393 /* Probe for available chromas */
394 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
396 struct v4l2_fmtdesc codec = {};
399 codec.index = i_index;
400 codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
402 while( ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
405 codec.index = i_index;
408 p_sys->i_codec = i_index;
410 p_sys->p_codecs = malloc( p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
411 memset( p_sys->p_codecs, 0, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
413 for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
415 p_sys->p_codecs[i_index].index = i_index;
416 p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
418 if( ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
420 msg_Err( p_demux, "cannot get codec description (%s)", strerror( errno ) );
424 msg_Dbg( p_demux, "device supports Codec %s",
425 p_sys->p_codecs[i_index].description );
430 if( i_fd >= 0 ) close( i_fd );
435 if( i_fd >= 0 ) close( i_fd );