1 /*****************************************************************************
2 * v4l2.c : Video4Linux2 input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2007 the VideoLAN team
7 * Authors: Benjamin Pracht <bigben at videolan dot org>
8 * Richard Hosking <richard at hovis dot net>
9 * Antoine Cellerier <dionoea at videolan d.t org>
10 * Dennis Lou <dlou99 at yahoo dot com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
28 * Sections based on the reference V4L2 capture example at
29 * http://v4l2spec.bytesex.org/spec/capture-example.html
31 * ALSA support based on parts of
32 * http://www.equalarea.com/paul/alsa-audio.html
33 * and hints taken from alsa-utils (aplay/arecord)
34 * http://www.alsa-project.org
38 * TODO: Tuner partial implementation.
39 * TODO: Add more MPEG stream params
42 /*****************************************************************************
44 *****************************************************************************/
47 #include <vlc_access.h>
48 #include <vlc_demux.h>
49 #include <vlc_input.h>
54 #include <sys/ioctl.h>
57 #include <linux/videodev2.h>
59 #include <sys/soundcard.h>
62 # define ALSA_PCM_NEW_HW_PARAMS_API
63 # define ALSA_PCM_NEW_SW_PARAMS_API
64 # include <alsa/asoundlib.h>
69 /*****************************************************************************
71 *****************************************************************************/
73 static int DemuxOpen ( vlc_object_t * );
74 static void DemuxClose( vlc_object_t * );
75 static int AccessOpen ( vlc_object_t * );
76 static void AccessClose( vlc_object_t * );
78 #define DEV_TEXT N_("Device name")
79 #define DEV_LONGTEXT N_( \
80 "Name of the device to use. " \
81 "If you don't specify anything, /dev/video0 will be used.")
82 #define STANDARD_TEXT N_( "Standard" )
83 #define STANDARD_LONGTEXT N_( \
84 "Video standard (Default, SECAM, PAL, or NTSC)." )
85 #define CHROMA_TEXT N_("Video input chroma format")
86 #define CHROMA_LONGTEXT N_( \
87 "Force the Video4Linux2 video device to use a specific chroma format " \
88 "(eg. I420 or I422 for raw images, MJPEG for M-JPEG compressed input) " \
89 "(Complete list: GREY, I240, RV16, RV15, RV24, RV32, YUY2, YUYV, UYVY, " \
90 "I41N, I422, I420, I411, I410, MJPG)")
91 #define INPUT_TEXT N_( "Input" )
92 #define INPUT_LONGTEXT N_( \
93 "Input of the card to use (Usually, 0 = tuner, " \
94 "1 = composite, 2 = svideo)." )
95 #define IOMETHOD_TEXT N_( "IO Method" )
96 #define IOMETHOD_LONGTEXT N_( \
97 "IO Method (READ, MMAP, USERPTR)." )
98 #define WIDTH_TEXT N_( "Width" )
99 #define WIDTH_LONGTEXT N_( \
100 "Force width (-1 for autodetect)." )
101 #define HEIGHT_TEXT N_( "Height" )
102 #define HEIGHT_LONGTEXT N_( \
103 "Force height (-1 for autodetect)." )
104 #define FPS_TEXT N_( "Framerate" )
105 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
106 "(-1 for autodetect)." )
108 #define CTRL_RESET_TEXT N_( "Reset v4l2 controls" )
109 #define CTRL_RESET_LONGTEXT N_( \
110 "Reset controls to defaults provided by the v4l2 driver." )
111 #define BRIGHTNESS_TEXT N_( "Brightness" )
112 #define BRIGHTNESS_LONGTEXT N_( \
113 "Brightness of the video input (if supported by v4l2 driver)." )
114 #define CONTRAST_TEXT N_( "Contrast" )
115 #define CONTRAST_LONGTEXT N_( \
116 "Contrast of the video input (if supported by v4l2 driver)." )
117 #define SATURATION_TEXT N_( "Saturation" )
118 #define SATURATION_LONGTEXT N_( \
119 "Saturation of the video input (if supported by v4l2 driver)." )
120 #define HUE_TEXT N_( "Hue" )
121 #define HUE_LONGTEXT N_( \
122 "Hue of the video input (if supported by v4l2 driver)." )
123 #define GAMMA_TEXT N_( "Gamma" )
124 #define GAMMA_LONGTEXT N_( \
125 "Gamma of the video input (if supported by v4l2 driver)." )
127 #define ADEV_TEXT N_("Audio device name")
128 #define ADEV_LONGTEXT N_( \
129 "Name of the audio device to use. " \
130 "If you don't specify anything, \"/dev/dsp\" will be used for OSS, " \
131 "\"default\" for Alsa (if Alsa support is enabled).")
132 #define AUDIO_METHOD_TEXT N_( "Audio method" )
133 #define AUDIO_METHOD_LONGTEXT N_( \
134 "Audio method to use: 1 for OSS, 2 for ALSA, 3 for ALSA or OSS " \
135 "(ALSA is prefered)." )
136 #define STEREO_TEXT N_( "Stereo" )
137 #define STEREO_LONGTEXT N_( \
138 "Capture the audio stream in stereo." )
139 #define SAMPLERATE_TEXT N_( "Samplerate" )
140 #define SAMPLERATE_LONGTEXT N_( \
141 "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
143 #define CACHING_TEXT N_("Caching value in ms")
144 #define CACHING_LONGTEXT N_( \
145 "Caching value for V4L2 captures. This " \
146 "value should be set in milliseconds." )
154 static int i_standards_list[] =
155 { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
156 static const char *psz_standards_list_text[] =
157 { N_("Default"), N_("SECAM"), N_("PAL"), N_("NTSC") };
159 static int i_iomethod_list[] =
160 { IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR };
161 static const char *psz_iomethod_list_text[] =
162 { N_("READ"), N_("MMAP"), N_("USERPTR") };
167 #define AUDIO_METHOD_OSS 1
168 #define OSS_DEFAULT "/dev/dsp"
169 #define AUDIO_METHOD_ALSA 2
170 #define ALSA_DEFAULT "default"
171 #define CFG_PREFIX "v4l2-"
174 set_shortname( _("Video4Linux2") );
175 set_description( _("Video4Linux2 input") );
176 set_category( CAT_INPUT );
177 set_subcategory( SUBCAT_INPUT_ACCESS );
179 set_section( N_( "Video input" ), NULL );
180 add_string( CFG_PREFIX "dev", "/dev/video0", 0, DEV_TEXT, DEV_LONGTEXT,
182 add_integer( CFG_PREFIX "standard", 0, NULL, STANDARD_TEXT,
183 STANDARD_LONGTEXT, VLC_FALSE );
184 change_integer_list( i_standards_list, psz_standards_list_text, 0 );
185 add_string( CFG_PREFIX "chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
187 add_integer( CFG_PREFIX "input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
189 add_integer( CFG_PREFIX "io", IO_METHOD_MMAP, NULL, IOMETHOD_TEXT,
190 IOMETHOD_LONGTEXT, VLC_TRUE );
191 change_integer_list( i_iomethod_list, psz_iomethod_list_text, 0 );
192 add_integer( CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
193 WIDTH_LONGTEXT, VLC_TRUE );
194 add_integer( CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
195 HEIGHT_LONGTEXT, VLC_TRUE );
196 add_float( CFG_PREFIX "fps", 0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
198 set_section( N_( "Audio input" ), NULL );
199 add_string( CFG_PREFIX "adev", NULL, 0, ADEV_TEXT, ADEV_LONGTEXT,
202 add_integer( CFG_PREFIX "audio-method", AUDIO_METHOD_OSS|AUDIO_METHOD_ALSA,
203 NULL, AUDIO_METHOD_TEXT, AUDIO_METHOD_LONGTEXT, VLC_TRUE );
205 add_bool( CFG_PREFIX "stereo", VLC_TRUE, NULL, STEREO_TEXT, STEREO_LONGTEXT,
207 add_integer( CFG_PREFIX "samplerate", 48000, NULL, SAMPLERATE_TEXT,
208 SAMPLERATE_LONGTEXT, VLC_TRUE );
209 add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
210 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
212 set_section( N_( "Controls" ), N_( "v4l2 driver controls" ) );
213 add_bool( CFG_PREFIX "controls-reset", VLC_FALSE, NULL, CTRL_RESET_TEXT,
214 CTRL_RESET_LONGTEXT, VLC_TRUE );
215 add_integer( CFG_PREFIX "brightness", -1, NULL, BRIGHTNESS_TEXT,
216 BRIGHTNESS_LONGTEXT, VLC_TRUE );
217 add_integer( CFG_PREFIX "contrast", -1, NULL, CONTRAST_TEXT,
218 CONTRAST_LONGTEXT, VLC_TRUE );
219 add_integer( CFG_PREFIX "saturation", -1, NULL, SATURATION_TEXT,
220 SATURATION_LONGTEXT, VLC_TRUE );
221 add_integer( CFG_PREFIX "hue", -1, NULL, HUE_TEXT,
222 HUE_LONGTEXT, VLC_TRUE );
223 add_integer( CFG_PREFIX "gamma", -1, NULL, GAMMA_TEXT,
224 GAMMA_LONGTEXT, VLC_TRUE );
227 add_shortcut( "v4l2" );
228 set_capability( "access_demux", 10 );
229 set_callbacks( DemuxOpen, DemuxClose );
232 set_description( _("Video4Linux2 Compressed A/V") );
233 set_capability( "access2", 0 );
234 /* use these when open as access_demux fails; VLC will use another demux */
235 set_callbacks( AccessOpen, AccessClose );
239 /*****************************************************************************
240 * Access: local prototypes
241 *****************************************************************************/
243 static void CommonClose( vlc_object_t *, demux_sys_t * );
244 static void ParseMRL( demux_sys_t *, char *, vlc_object_t * );
245 static void GetV4L2Params( demux_sys_t *, vlc_object_t * );
247 static int DemuxControl( demux_t *, int, va_list );
248 static int AccessControl( access_t *, int, va_list );
250 static int Demux( demux_t * );
251 static ssize_t AccessRead( access_t *, uint8_t *, size_t );
253 static block_t* GrabVideo( demux_t *p_demux );
254 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t );
255 static block_t* GrabAudio( demux_t *p_demux );
257 static vlc_bool_t IsPixelFormatSupported( demux_t *p_demux,
258 unsigned int i_pixelformat );
260 static char* ResolveALSADeviceName( const char *psz_device );
261 static int OpenVideoDev( vlc_object_t *, demux_sys_t *, vlc_bool_t );
262 static int OpenAudioDev( vlc_object_t *, demux_sys_t *, vlc_bool_t );
263 static vlc_bool_t ProbeVideoDev( vlc_object_t *, demux_sys_t *,
265 static vlc_bool_t ProbeAudioDev( vlc_object_t *, demux_sys_t *,
268 static int ControlList( vlc_object_t *, int , vlc_bool_t, vlc_bool_t );
269 static int Control( vlc_object_t *, int i_fd,
270 const char *psz_name, int i_cid, int i_value );
272 static int DemuxControlCallback( vlc_object_t *p_this, const char *psz_var,
273 vlc_value_t oldval, vlc_value_t newval,
275 static int DemuxControlResetCallback( vlc_object_t *p_this, const char *psz_var,
276 vlc_value_t oldval, vlc_value_t newval,
278 static int AccessControlCallback( vlc_object_t *p_this, const char *psz_var,
279 vlc_value_t oldval, vlc_value_t newval,
281 static int AccessControlResetCallback( vlc_object_t *p_this,
282 const char *psz_var, vlc_value_t oldval,
283 vlc_value_t newval, void *p_data );
289 } v4l2chroma_to_fourcc[] =
292 { V4L2_PIX_FMT_GREY, VLC_FOURCC('G','R','E','Y') },
293 { V4L2_PIX_FMT_HI240, VLC_FOURCC('I','2','4','0') },
294 { V4L2_PIX_FMT_RGB565, VLC_FOURCC('R','V','1','6') },
295 { V4L2_PIX_FMT_RGB555, VLC_FOURCC('R','V','1','5') },
296 { V4L2_PIX_FMT_BGR24, VLC_FOURCC('R','V','2','4') },
297 { V4L2_PIX_FMT_BGR32, VLC_FOURCC('R','V','3','2') },
298 { V4L2_PIX_FMT_YUYV, VLC_FOURCC('Y','U','Y','2') },
299 { V4L2_PIX_FMT_YUYV, VLC_FOURCC('Y','U','Y','V') },
300 { V4L2_PIX_FMT_UYVY, VLC_FOURCC('U','Y','V','Y') },
301 { V4L2_PIX_FMT_Y41P, VLC_FOURCC('I','4','1','N') },
302 { V4L2_PIX_FMT_YUV422P, VLC_FOURCC('I','4','2','2') },
303 { V4L2_PIX_FMT_YVU420, VLC_FOURCC('I','4','2','0') },
304 { V4L2_PIX_FMT_YUV411P, VLC_FOURCC('I','4','1','1') },
305 { V4L2_PIX_FMT_YUV410, VLC_FOURCC('I','4','1','0') },
306 /* Compressed data types */
307 { V4L2_PIX_FMT_MJPEG, VLC_FOURCC('M','J','P','G') },
309 { V4L2_PIX_FMT_JPEG, VLC_FOURCC('J','P','E','G') },
310 { V4L2_PIX_FMT_DV, VLC_FOURCC('?','?','?','?') },
311 { V4L2_PIX_FMT_MPEG, VLC_FOURCC('?','?','?','?') },
318 const char *psz_name;
322 { "brightness", V4L2_CID_BRIGHTNESS },
323 { "contrast", V4L2_CID_CONTRAST },
324 { "saturation", V4L2_CID_SATURATION },
325 { "hue", V4L2_CID_HUE },
326 { "audio-volume", V4L2_CID_AUDIO_VOLUME },
327 { "audio-balance", V4L2_CID_AUDIO_BALANCE },
328 { "audio-bass", V4L2_CID_AUDIO_BASS },
329 { "audio-treble", V4L2_CID_AUDIO_TREBLE },
330 { "audio-mute", V4L2_CID_AUDIO_MUTE },
331 { "audio-loudness", V4L2_CID_AUDIO_LOUDNESS },
332 { "black-level", V4L2_CID_BLACK_LEVEL },
333 { "auto-white-balance", V4L2_CID_AUTO_WHITE_BALANCE },
334 { "do-white-balance", V4L2_CID_DO_WHITE_BALANCE },
335 { "red-balance", V4L2_CID_RED_BALANCE },
336 { "blue-balance", V4L2_CID_BLUE_BALANCE },
337 { "gamma", V4L2_CID_GAMMA },
338 { "exposure", V4L2_CID_EXPOSURE },
339 { "autogain", V4L2_CID_AUTOGAIN },
340 { "gain", V4L2_CID_GAIN },
341 { "hflip", V4L2_CID_HFLIP },
342 { "vflip", V4L2_CID_VFLIP },
343 { "hcenter", V4L2_CID_HCENTER },
344 { "vcenter", V4L2_CID_VCENTER },
357 char *psz_device; /* Main device from MRL, can be video or audio */
365 char *psz_requested_chroma;
372 struct v4l2_capability dev_cap;
375 struct v4l2_input *p_inputs;
376 int i_selected_input;
379 struct v4l2_standard *p_standards;
380 v4l2_std_id i_selected_standard_id;
383 /* V4L2 devices cannot have more than 32 audio inputs */
384 struct v4l2_audio p_audios[32];
387 struct v4l2_tuner *p_tuners;
390 struct v4l2_fmtdesc *p_codecs;
392 struct buffer_t *p_buffers;
393 unsigned int i_nbuffers;
397 float f_fps; /* <= 0.0 mean to grab at full rate */
398 mtime_t i_video_pts; /* only used when f_fps > 0 */
401 es_out_id_t *p_es_video;
404 unsigned int i_sample_rate;
406 int i_audio_max_frame_size;
407 block_t *p_block_audio;
408 es_out_id_t *p_es_audio;
414 snd_pcm_t *p_alsa_pcm;
415 int i_alsa_frame_size;
416 int i_alsa_chunk_size;
420 static int FindMainDevice( vlc_object_t *p_this, demux_sys_t *p_sys,
421 int i_flags, vlc_bool_t b_demux,
422 vlc_bool_t b_forced )
424 /* Find main device (video or audio) */
425 if( p_sys->psz_device && *p_sys->psz_device )
427 msg_Dbg( p_this, "main device='%s'", p_sys->psz_device );
429 /* Try to open as video device */
430 if( i_flags & FIND_VIDEO )
432 msg_Dbg( p_this, "trying device '%s' as video", p_sys->psz_device );
433 if( ProbeVideoDev( p_this, p_sys, p_sys->psz_device ) )
435 msg_Dbg( p_this, "'%s' is a video device", p_sys->psz_device );
436 /* Device was a video device */
437 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
438 p_sys->psz_vdev = p_sys->psz_device;
439 p_sys->psz_device = NULL;
440 p_sys->i_fd_video = OpenVideoDev( p_this, p_sys, b_demux );
441 if( p_sys->i_fd_video < 0 )
447 if( i_flags & FIND_AUDIO )
449 /* Try to open as audio device */
450 msg_Dbg( p_this, "trying device '%s' as audio", p_sys->psz_device );
451 if( ProbeAudioDev( p_this, p_sys, p_sys->psz_device ) )
453 msg_Dbg( p_this, "'%s' is an audio device", p_sys->psz_device );
454 /* Device was an audio device */
455 free( p_sys->psz_adev );
456 p_sys->psz_adev = p_sys->psz_device;
457 p_sys->psz_device = NULL;
458 p_sys->i_fd_audio = OpenAudioDev( p_this, p_sys, b_demux );
459 if( p_sys->i_fd_audio < 0 )
466 /* If no device opened, only continue if the access was forced */
467 if( b_forced == VLC_FALSE
468 && !( ( i_flags & FIND_VIDEO && p_sys->i_fd_video >= 0 )
469 || ( i_flags & FIND_AUDIO && p_sys->i_fd_audio >= 0 ) ) )
474 /* Find video device */
475 if( i_flags & FIND_VIDEO && p_sys->i_fd_video < 0 )
477 if( !p_sys->psz_vdev || !*p_sys->psz_vdev )
479 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
480 p_sys->psz_vdev = var_CreateGetString( p_this, "v4l2-dev" );
483 msg_Dbg( p_this, "opening '%s' as video", p_sys->psz_vdev );
484 if( p_sys->psz_vdev && *p_sys->psz_vdev
485 && ProbeVideoDev( p_this, p_sys, p_sys->psz_vdev ) )
487 p_sys->i_fd_video = OpenVideoDev( p_this, p_sys, b_demux );
491 /* Find audio device */
492 if( i_flags & FIND_AUDIO && p_sys->i_fd_audio < 0 )
494 if( !p_sys->psz_adev )
496 p_sys->psz_adev = var_CreateGetNonEmptyString( p_this, "v4l2-adev" );
499 msg_Dbg( p_this, "opening '%s' as audio", p_sys->psz_adev );
500 if( ProbeAudioDev( p_this, p_sys, p_sys->psz_adev ) )
502 p_sys->i_fd_audio = OpenAudioDev( p_this, p_sys, b_demux );
506 if( !( ( i_flags & FIND_VIDEO && p_sys->i_fd_video >= 0 )
507 || ( i_flags & FIND_AUDIO && p_sys->i_fd_audio >= 0 ) ) )
514 /*****************************************************************************
515 * DemuxOpen: opens v4l2 device, access_demux callback
516 *****************************************************************************
518 * url: <video device>::::
520 *****************************************************************************/
521 static int DemuxOpen( vlc_object_t *p_this )
523 demux_t *p_demux = (demux_t*)p_this;
526 /* Only when selected */
527 if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
530 p_demux->pf_control = DemuxControl;
531 p_demux->pf_demux = Demux;
532 p_demux->info.i_update = 0;
533 p_demux->info.i_title = 0;
534 p_demux->info.i_seekpoint = 0;
536 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
537 if( p_sys == NULL ) return VLC_ENOMEM;
539 GetV4L2Params(p_sys, (vlc_object_t *) p_demux);
541 ParseMRL( p_sys, p_demux->psz_path, (vlc_object_t *) p_demux );
544 /* Alsa support available? */
545 msg_Dbg( p_demux, "ALSA input support available" );
548 if( FindMainDevice( p_this, p_sys, FIND_VIDEO|FIND_AUDIO,
549 VLC_TRUE, !strcmp( p_demux->psz_access, "v4l2" ) ) != VLC_SUCCESS )
551 DemuxClose( p_this );
558 /*****************************************************************************
559 * GetV4L2Params: fill in p_sys parameters (shared by DemuxOpen and AccessOpen)
560 *****************************************************************************/
561 static void GetV4L2Params( demux_sys_t *p_sys, vlc_object_t *p_obj )
563 p_sys->i_video_pts = -1;
565 p_sys->i_selected_standard_id =
566 i_standards_list[var_CreateGetInteger( p_obj, "v4l2-standard" )];
568 p_sys->i_selected_input = var_CreateGetInteger( p_obj, "v4l2-input" );
570 p_sys->io = var_CreateGetInteger( p_obj, "v4l2-io" );
572 p_sys->i_width = var_CreateGetInteger( p_obj, "v4l2-width" );
573 p_sys->i_height = var_CreateGetInteger( p_obj, "v4l2-height" );
575 var_CreateGetBool( p_obj, "v4l2-controls-reset" );
577 p_sys->f_fps = var_CreateGetFloat( p_obj, "v4l2-fps" );
578 p_sys->i_sample_rate = var_CreateGetInteger( p_obj, "v4l2-samplerate" );
579 p_sys->psz_requested_chroma = var_CreateGetString( p_obj, "v4l2-chroma" );
582 p_sys->i_audio_method = var_CreateGetInteger( p_obj, "v4l2-audio-method" );
584 p_sys->i_audio_method = AUDIO_METHOD_OSS;
587 p_sys->b_stereo = var_CreateGetBool( p_obj, "v4l2-stereo" );
589 p_sys->i_pts = var_CreateGetInteger( p_obj, "v4l2-caching" );
591 p_sys->psz_device = p_sys->psz_vdev = p_sys->psz_adev = NULL;
592 p_sys->i_fd_video = -1;
593 p_sys->i_fd_audio = -1;
595 p_sys->p_es_video = p_sys->p_es_audio = 0;
596 p_sys->p_block_audio = 0;
599 /*****************************************************************************
600 * ParseMRL: parse the options contained in the MRL
601 *****************************************************************************/
602 static void ParseMRL( demux_sys_t *p_sys, char *psz_path, vlc_object_t *p_obj )
604 char *psz_dup = strdup( psz_path );
605 char *psz_parser = psz_dup;
607 while( *psz_parser && *psz_parser != ':' )
612 if( *psz_parser == ':' )
617 *psz_parser++ = '\0';
619 if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
623 psz_parser += strlen( "adev=" );
624 if( strchr( psz_parser, ':' ) )
626 i_len = strchr( psz_parser, ':' ) - psz_parser;
630 i_len = strlen( psz_parser );
633 p_sys->psz_adev = strndup( psz_parser, i_len );
634 if( !*p_sys->psz_adev )
636 free( p_sys->psz_adev );
637 p_sys->psz_adev = NULL;
642 else if( !strncmp( psz_parser, "standard=", strlen( "standard=" ) ) )
644 psz_parser += strlen( "standard=" );
645 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
647 p_sys->i_selected_standard_id = V4L2_STD_PAL;
648 psz_parser += strlen( "pal" );
650 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
652 p_sys->i_selected_standard_id = V4L2_STD_NTSC;
653 psz_parser += strlen( "ntsc" );
655 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
657 p_sys->i_selected_standard_id = V4L2_STD_SECAM;
658 psz_parser += strlen( "secam" );
660 else if( !strncmp( psz_parser, "default", strlen( "default" ) ) )
662 p_sys->i_selected_standard_id = V4L2_STD_UNKNOWN;
663 psz_parser += strlen( "default" );
667 p_sys->i_selected_standard_id = i_standards_list[strtol( psz_parser, &psz_parser, 0 )];
670 else if( !strncmp( psz_parser, "chroma=", strlen( "chroma=" ) ) )
674 psz_parser += strlen( "chroma=" );
675 if( strchr( psz_parser, ':' ) )
677 i_len = strchr( psz_parser, ':' ) - psz_parser;
681 i_len = strlen( psz_parser );
684 if( p_sys->psz_requested_chroma ) free( p_sys->psz_requested_chroma );
685 p_sys->psz_requested_chroma = strndup( psz_parser, i_len );
689 else if( !strncmp( psz_parser, "input=", strlen( "input=" ) ) )
691 p_sys->i_selected_input = strtol( psz_parser + strlen( "input=" ),
694 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
696 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
699 else if( !strncmp( psz_parser, "io=", strlen( "io=" ) ) )
701 psz_parser += strlen( "io=" );
702 if( !strncmp( psz_parser, "read", strlen( "read" ) ) )
704 p_sys->io = IO_METHOD_READ;
705 psz_parser += strlen( "read" );
707 else if( !strncmp( psz_parser, "mmap", strlen( "mmap" ) ) )
709 p_sys->io = IO_METHOD_MMAP;
710 psz_parser += strlen( "mmap" );
712 else if( !strncmp( psz_parser, "userptr", strlen( "userptr" ) ) )
714 p_sys->io = IO_METHOD_USERPTR;
715 psz_parser += strlen( "userptr" );
719 p_sys->io = strtol( psz_parser, &psz_parser, 0 );
722 else if( !strncmp( psz_parser, "width=",
723 strlen( "width=" ) ) )
726 strtol( psz_parser + strlen( "width=" ),
729 else if( !strncmp( psz_parser, "height=",
730 strlen( "height=" ) ) )
733 strtol( psz_parser + strlen( "height=" ),
736 else if( !strncmp( psz_parser, "controls-reset",
737 strlen( "controls-reset" ) ) )
739 var_SetBool( p_obj, "v4l2-controls-reset", VLC_TRUE );
740 psz_parser += strlen( "controls-reset" );
743 else if( !strncmp( psz_parser, "brightness=",
744 strlen( "brightness=" ) ) )
746 var_SetInteger( p_obj, "brightness",
747 strtol( psz_parser + strlen( "brightness=" ),
750 else if( !strncmp( psz_parser, "contrast=",
751 strlen( "contrast=" ) ) )
753 var_SetInteger( p_obj, "contrast",
754 strtol( psz_parser + strlen( "contrast=" ),
757 else if( !strncmp( psz_parser, "saturation=",
758 strlen( "saturation=" ) ) )
760 var_SetInteger( p_obj, "saturation",
761 strtol( psz_parser + strlen( "saturation=" ),
764 else if( !strncmp( psz_parser, "hue=",
767 var_SetInteger( p_obj, "hue",
768 strtol( psz_parser + strlen( "hue=" ),
771 else if( !strncmp( psz_parser, "gamma=",
772 strlen( "gamma=" ) ) )
774 var_SetInteger( p_obj, "gamma",
775 strtol( psz_parser + strlen( "gamma=" ),
779 else if( !strncmp( psz_parser, "samplerate=",
780 strlen( "samplerate=" ) ) )
782 p_sys->i_sample_rate =
783 strtol( psz_parser + strlen( "samplerate=" ),
787 else if( !strncmp( psz_parser, "audio-method", strlen( "audio-method" ) ) )
789 p_sys->i_audio_method =
790 strtol( psz_parser + strlen( "audio-method" ),
794 else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
796 psz_parser += strlen( "stereo" );
797 p_sys->b_stereo = VLC_TRUE;
799 else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
801 psz_parser += strlen( "mono" );
802 p_sys->b_stereo = VLC_FALSE;
804 else if( !strncmp( psz_parser, "caching=", strlen( "caching=" ) ) )
806 p_sys->i_pts = strtol( psz_parser + strlen( "caching=" ),
811 msg_Warn( p_obj, "unknown option" );
814 while( *psz_parser && *psz_parser != ':' )
819 if( *psz_parser == '\0' )
829 p_sys->psz_device = strdup( psz_dup );
831 if( psz_dup ) free( psz_dup );
834 /*****************************************************************************
835 * Close: close device, free resources
836 *****************************************************************************/
837 static void AccessClose( vlc_object_t *p_this )
839 access_t *p_access = (access_t *)p_this;
840 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
842 CommonClose( p_this, p_sys );
845 static void DemuxClose( vlc_object_t *p_this )
847 struct v4l2_buffer buf;
848 enum v4l2_buf_type buf_type;
851 demux_t *p_demux = (demux_t *)p_this;
852 demux_sys_t *p_sys = p_demux->p_sys;
854 /* Stop video capture */
855 if( p_sys->i_fd_video >= 0 )
864 case IO_METHOD_USERPTR:
865 /* Some drivers 'hang' internally if this is not done before streamoff */
866 for( unsigned int i = 0; i < p_sys->i_nbuffers; i++ )
868 memset( &buf, 0, sizeof(buf) );
869 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
870 buf.memory = ( p_sys->io == IO_METHOD_USERPTR ) ?
871 V4L2_MEMORY_USERPTR : V4L2_MEMORY_MMAP;
872 ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ); /* ignore result */
875 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
876 if( ioctl( p_sys->i_fd_video, VIDIOC_STREAMOFF, &buf_type ) < 0 ) {
877 msg_Err( p_this, "VIDIOC_STREAMOFF failed" );
884 /* Free Video Buffers */
885 if( p_sys->p_buffers ) {
889 free( p_sys->p_buffers[0].start );
893 for( i = 0; i < p_sys->i_nbuffers; ++i )
895 if( munmap( p_sys->p_buffers[i].start, p_sys->p_buffers[i].length ) )
897 msg_Err( p_this, "munmap failed" );
902 case IO_METHOD_USERPTR:
903 for( i = 0; i < p_sys->i_nbuffers; ++i )
905 free( p_sys->p_buffers[i].orig_userp );
909 free( p_sys->p_buffers );
912 CommonClose( p_this, p_sys );
915 static void CommonClose( vlc_object_t *p_this, demux_sys_t *p_sys )
918 if( p_sys->i_fd_video >= 0 ) close( p_sys->i_fd_video );
920 if( p_sys->p_alsa_pcm ) snd_pcm_close( p_sys->p_alsa_pcm );
922 if( p_sys->i_fd_audio >= 0 ) close( p_sys->i_fd_audio );
924 if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
925 free( p_sys->psz_device );
926 free( p_sys->psz_vdev );
927 free( p_sys->psz_adev );
928 free( p_sys->p_standards );
929 free( p_sys->p_inputs );
930 free( p_sys->p_tuners );
931 free( p_sys->p_codecs );
932 free( p_sys->psz_requested_chroma );
937 /*****************************************************************************
938 * AccessOpen: opens v4l2 device, access2 callback
939 *****************************************************************************
941 * url: <video device>::::
943 *****************************************************************************/
944 static int AccessOpen( vlc_object_t * p_this )
946 access_t *p_access = (access_t*) p_this;
949 /* Only when selected */
950 if( *p_access->psz_access == '\0' ) return VLC_EGENERIC;
952 p_access->pf_read = AccessRead;
953 p_access->pf_block = NULL;
954 p_access->pf_seek = NULL;
955 p_access->pf_control = AccessControl;
956 p_access->info.i_update = 0;
957 p_access->info.i_size = 0;
958 p_access->info.i_pos = 0;
959 p_access->info.b_eof = VLC_FALSE;
960 p_access->info.i_title = 0;
961 p_access->info.i_seekpoint = 0;
963 p_sys = calloc( 1, sizeof( demux_sys_t ) );
964 p_access->p_sys = (access_sys_t *) p_sys;
965 if( p_sys == NULL ) return VLC_ENOMEM;
967 GetV4L2Params( p_sys, (vlc_object_t *) p_access );
969 ParseMRL( p_sys, p_access->psz_path, (vlc_object_t *) p_access );
971 if( FindMainDevice( p_this, p_sys, FIND_VIDEO,
972 VLC_FALSE, !strcmp( p_access->psz_access, "v4l2" ) ) != VLC_SUCCESS )
974 AccessClose( p_this );
981 /*****************************************************************************
983 *****************************************************************************/
984 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
986 demux_sys_t *p_sys = p_demux->p_sys;
992 /* Special for access_demux */
993 case DEMUX_CAN_PAUSE:
994 case DEMUX_SET_PAUSE_STATE:
995 case DEMUX_CAN_CONTROL_PACE:
996 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1000 case DEMUX_GET_PTS_DELAY:
1001 pi64 = (int64_t*)va_arg( args, int64_t * );
1002 *pi64 = (int64_t)p_sys->i_pts * 1000;
1005 case DEMUX_GET_TIME:
1006 pi64 = (int64_t*)va_arg( args, int64_t * );
1010 /* TODO implement others */
1012 return VLC_EGENERIC;
1015 return VLC_EGENERIC;
1018 /*****************************************************************************
1019 * AccessControl: access2 callback
1020 *****************************************************************************/
1021 static int AccessControl( access_t *p_access, int i_query, va_list args )
1023 vlc_bool_t *pb_bool;
1026 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1031 case ACCESS_CAN_SEEK:
1032 case ACCESS_CAN_FASTSEEK:
1033 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1034 *pb_bool = VLC_FALSE;
1036 case ACCESS_CAN_PAUSE:
1037 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1038 *pb_bool = VLC_FALSE;
1040 case ACCESS_CAN_CONTROL_PACE:
1041 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1042 *pb_bool = VLC_FALSE;
1046 case ACCESS_GET_MTU:
1047 pi_int = (int*)va_arg( args, int * );
1051 case ACCESS_GET_PTS_DELAY:
1052 pi_64 = (int64_t*)va_arg( args, int64_t * );
1053 *pi_64 = (int64_t) p_sys->i_pts * 1000;
1057 case ACCESS_SET_PAUSE_STATE:
1061 case ACCESS_GET_TITLE_INFO:
1062 case ACCESS_SET_TITLE:
1063 case ACCESS_SET_SEEKPOINT:
1064 case ACCESS_SET_PRIVATE_ID_STATE:
1065 case ACCESS_GET_CONTENT_TYPE:
1066 case ACCESS_GET_META:
1067 return VLC_EGENERIC;
1070 msg_Warn( p_access, "Unimplemented query in control(%d).", i_query);
1071 return VLC_EGENERIC;
1077 /*****************************************************************************
1078 * AccessRead: access2 callback
1079 ******************************************************************************/
1080 static ssize_t AccessRead( access_t * p_access, uint8_t * p_buffer, size_t i_len )
1082 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1086 ufd.fd = p_sys->i_fd_video;
1087 ufd.events = POLLIN;
1089 if( p_access->info.b_eof )
1094 if( p_access->b_die )
1099 while( ( i_ret = poll( &ufd, 1, 500 ) ) == 0 );
1103 msg_Err( p_access, "Polling error (%m)." );
1107 i_ret = read( p_sys->i_fd_video, p_buffer, i_len );
1110 p_access->info.b_eof = VLC_TRUE;
1112 else if( i_ret > 0 )
1114 p_access->info.i_pos += i_ret;
1120 /*****************************************************************************
1121 * Demux: Processes the audio or video frame
1122 *****************************************************************************/
1123 static int Demux( demux_t *p_demux )
1125 demux_sys_t *p_sys = p_demux->p_sys;
1126 es_out_id_t *p_es = p_sys->p_es_audio;
1127 block_t *p_block = NULL;
1129 /* Try grabbing audio frames first */
1130 if( p_sys->i_fd_audio < 0 || !( p_block = GrabAudio( p_demux ) ) )
1132 /* Try grabbing video frame */
1133 p_es = p_sys->p_es_video;
1134 if( p_sys->i_fd_video > 0 ) p_block = GrabVideo( p_demux );
1139 /* Sleep so we do not consume all the cpu, 10ms seems
1140 * like a good value (100fps) */
1145 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
1146 es_out_Send( p_demux->out, p_es, p_block );
1151 /*****************************************************************************
1152 * GrabVideo: Grab a video frame
1153 *****************************************************************************/
1154 static block_t* GrabVideo( demux_t *p_demux )
1156 demux_sys_t *p_sys = p_demux->p_sys;
1158 block_t *p_block = NULL;
1159 struct v4l2_buffer buf;
1162 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1164 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1166 /* Did we wait long enough ? (frame rate reduction) */
1167 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1170 /* Grab Video Frame */
1173 case IO_METHOD_READ:
1174 i_ret = read( p_sys->i_fd_video, p_sys->p_buffers[0].start, p_sys->p_buffers[0].length );
1182 /* Could ignore EIO, see spec. */
1185 msg_Err( p_demux, "Failed to read frame" );
1190 p_block = ProcessVideoFrame( p_demux, (uint8_t*)p_sys->p_buffers[0].start, i_ret );
1191 if( !p_block ) return 0;
1195 case IO_METHOD_MMAP:
1196 memset( &buf, 0, sizeof(buf) );
1197 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1198 buf.memory = V4L2_MEMORY_MMAP;
1200 /* Wait for next frame */
1201 if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
1208 /* Could ignore EIO, see spec. */
1211 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1216 if( buf.index >= p_sys->i_nbuffers ) {
1217 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1221 p_block = ProcessVideoFrame( p_demux, p_sys->p_buffers[buf.index].start, buf.bytesused );
1222 if( !p_block ) return 0;
1225 if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
1227 msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
1233 case IO_METHOD_USERPTR:
1234 memset( &buf, 0, sizeof(buf) );
1235 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1236 buf.memory = V4L2_MEMORY_USERPTR;
1238 /* Wait for next frame */
1239 if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
1246 /* Could ignore EIO, see spec. */
1249 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1256 for( i = 0; i < p_sys->i_nbuffers; i++ )
1258 if( buf.m.userptr == (unsigned long)p_sys->p_buffers[i].start &&
1259 buf.length == p_sys->p_buffers[i].length ) break;
1262 if( i >= p_sys->i_nbuffers )
1264 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1268 p_block = ProcessVideoFrame( p_demux, (uint8_t*)buf.m.userptr, buf.bytesused );
1269 if( !p_block ) return 0;
1272 if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
1274 msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
1283 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();
1288 /*****************************************************************************
1289 * ProcessVideoFrame: Helper function to take a buffer and copy it into
1291 *****************************************************************************/
1292 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t i_size )
1296 if( !p_frame ) return 0;
1299 if( !( p_block = block_New( p_demux, i_size ) ) )
1301 msg_Warn( p_demux, "Cannot get new block" );
1306 memcpy( p_block->p_buffer, p_frame, i_size );
1311 /*****************************************************************************
1312 * GrabAudio: Grab an audio frame
1313 *****************************************************************************/
1314 static block_t* GrabAudio( demux_t *p_demux )
1316 demux_sys_t *p_sys = p_demux->p_sys;
1317 struct audio_buf_info buf_info;
1318 int i_read = 0, i_correct;
1321 if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
1322 else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
1326 msg_Warn( p_demux, "cannot get block" );
1330 p_sys->p_block_audio = p_block;
1333 if( p_sys->i_audio_method & AUDIO_METHOD_ALSA )
1336 i_read = snd_pcm_readi( p_sys->p_alsa_pcm, p_block->p_buffer, p_sys->i_alsa_chunk_size );
1346 snd_pcm_prepare( p_sys->p_alsa_pcm );
1350 i_resume = snd_pcm_resume( p_sys->p_alsa_pcm );
1351 if( i_resume < 0 && i_resume != -EAGAIN ) snd_pcm_prepare( p_sys->p_alsa_pcm );
1354 msg_Err( p_demux, "Failed to read alsa frame (%s)", snd_strerror( i_read ) );
1360 /* convert from frames to bytes */
1361 i_read *= p_sys->i_alsa_frame_size;
1366 if( p_sys->i_audio_method & AUDIO_METHOD_OSS )
1369 i_read = read( p_sys->i_fd_audio, p_block->p_buffer,
1370 p_sys->i_audio_max_frame_size );
1373 if( i_read <= 0 ) return 0;
1375 p_block->i_buffer = i_read;
1376 p_sys->p_block_audio = 0;
1378 /* Correct the date because of kernel buffering */
1380 if( p_sys->i_audio_method & AUDIO_METHOD_OSS )
1383 if( ioctl( p_sys->i_fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
1385 i_correct += buf_info.bytes;
1389 else if( p_sys->i_audio_method & AUDIO_METHOD_ALSA )
1393 snd_pcm_sframes_t delay = 0;
1394 if( ( i_err = snd_pcm_delay( p_sys->p_alsa_pcm, &delay ) ) >= 0 )
1396 int i_correction_delta = delay * p_sys->i_alsa_frame_size;
1397 /* Test for overrun */
1398 if( i_correction_delta>p_sys->i_audio_max_frame_size )
1400 msg_Warn( p_demux, "ALSA read overrun" );
1401 i_correction_delta = p_sys->i_audio_max_frame_size;
1402 snd_pcm_prepare( p_sys->p_alsa_pcm );
1404 i_correct += i_correction_delta;
1408 /* delay failed so reset */
1409 msg_Warn( p_demux, "ALSA snd_pcm_delay failed (%s)", snd_strerror( i_err ) );
1410 snd_pcm_prepare( p_sys->p_alsa_pcm );
1416 p_block->i_pts = p_block->i_dts =
1417 mdate() - I64C(1000000) * (mtime_t)i_correct /
1418 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
1423 /*****************************************************************************
1424 * Helper function to initalise video IO using the Read method
1425 *****************************************************************************/
1426 static int InitRead( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1428 demux_sys_t *p_sys = p_demux->p_sys;
1430 p_sys->p_buffers = calloc( 1, sizeof( *p_sys->p_buffers ) );
1431 if( !p_sys->p_buffers )
1433 msg_Err( p_demux, "Out of memory" );
1437 p_sys->p_buffers[0].length = i_buffer_size;
1438 p_sys->p_buffers[0].start = malloc( i_buffer_size );
1439 if( !p_sys->p_buffers[0].start )
1441 msg_Err( p_demux, "Out of memory" );
1448 return VLC_EGENERIC;
1452 /*****************************************************************************
1453 * Helper function to initalise video IO using the mmap method
1454 *****************************************************************************/
1455 static int InitMmap( demux_t *p_demux, int i_fd )
1457 demux_sys_t *p_sys = p_demux->p_sys;
1458 struct v4l2_requestbuffers req;
1460 memset( &req, 0, sizeof(req) );
1462 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1463 req.memory = V4L2_MEMORY_MMAP;
1465 if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1467 msg_Err( p_demux, "device does not support mmap i/o" );
1473 msg_Err( p_demux, "Insufficient buffer memory" );
1477 p_sys->p_buffers = calloc( req.count, sizeof( *p_sys->p_buffers ) );
1478 if( !p_sys->p_buffers )
1480 msg_Err( p_demux, "Out of memory" );
1484 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < req.count; ++p_sys->i_nbuffers )
1486 struct v4l2_buffer buf;
1488 memset( &buf, 0, sizeof(buf) );
1489 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1490 buf.memory = V4L2_MEMORY_MMAP;
1491 buf.index = p_sys->i_nbuffers;
1493 if( ioctl( i_fd, VIDIOC_QUERYBUF, &buf ) < 0 )
1495 msg_Err( p_demux, "VIDIOC_QUERYBUF" );
1499 p_sys->p_buffers[p_sys->i_nbuffers].length = buf.length;
1500 p_sys->p_buffers[p_sys->i_nbuffers].start =
1501 mmap( NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, buf.m.offset );
1503 if( p_sys->p_buffers[p_sys->i_nbuffers].start == MAP_FAILED )
1505 msg_Err( p_demux, "mmap failed (%m)" );
1513 return VLC_EGENERIC;
1517 /*****************************************************************************
1518 * Helper function to initalise video IO using the userbuf method
1519 *****************************************************************************/
1520 static int InitUserP( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1522 demux_sys_t *p_sys = p_demux->p_sys;
1523 struct v4l2_requestbuffers req;
1524 unsigned int i_page_size;
1526 i_page_size = getpagesize();
1527 i_buffer_size = ( i_buffer_size + i_page_size - 1 ) & ~( i_page_size - 1);
1529 memset( &req, 0, sizeof(req) );
1531 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1532 req.memory = V4L2_MEMORY_USERPTR;
1534 if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1536 msg_Err( p_demux, "device does not support user pointer i/o" );
1540 p_sys->p_buffers = calloc( 4, sizeof( *p_sys->p_buffers ) );
1541 if( !p_sys->p_buffers )
1543 msg_Err( p_demux, "Out of memory" );
1547 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < 4; ++p_sys->i_nbuffers )
1549 p_sys->p_buffers[p_sys->i_nbuffers].length = i_buffer_size;
1550 p_sys->p_buffers[p_sys->i_nbuffers].start =
1551 vlc_memalign( &p_sys->p_buffers[p_sys->i_nbuffers].orig_userp,
1552 /* boundary */ i_page_size, i_buffer_size );
1554 if( !p_sys->p_buffers[p_sys->i_nbuffers].start )
1556 msg_Err( p_demux, "out of memory" );
1564 return VLC_EGENERIC;
1568 /*****************************************************************************
1569 * IsPixelFormatSupported: returns true if the specified V4L2 pixel format is
1570 * in the array of supported formats returned by the driver
1571 *****************************************************************************/
1572 static vlc_bool_t IsPixelFormatSupported( demux_t *p_demux, unsigned int i_pixelformat )
1574 demux_sys_t *p_sys = p_demux->p_sys;
1576 for( int i_index = 0; i_index < p_sys->i_codec; i_index++ )
1578 if( p_sys->p_codecs[i_index].pixelformat == i_pixelformat ) return VLC_TRUE;
1584 /*****************************************************************************
1585 * OpenVideoDev: open and set up the video device and probe for capabilities
1586 *****************************************************************************/
1587 static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, vlc_bool_t b_demux )
1590 struct v4l2_cropcap cropcap;
1591 struct v4l2_crop crop;
1592 struct v4l2_format fmt;
1594 enum v4l2_buf_type buf_type;
1595 char *psz_device = p_sys->psz_vdev;
1597 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1599 msg_Err( p_obj, "cannot open device (%m)" );
1603 /* Select standard */
1605 if( p_sys->i_selected_standard_id != V4L2_STD_UNKNOWN )
1607 if( ioctl( i_fd, VIDIOC_S_STD, &p_sys->i_selected_standard_id ) < 0 )
1609 msg_Err( p_obj, "cannot set standard (%m)" );
1612 msg_Dbg( p_obj, "Set standard" );
1617 if( p_sys->i_selected_input > p_sys->i_input )
1619 msg_Warn( p_obj, "invalid input. Using the default one" );
1620 p_sys->i_selected_input = 0;
1623 if( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
1625 msg_Err( p_obj, "cannot set input (%m)" );
1629 /* TODO: Move the resolution stuff up here */
1630 /* if MPEG encoder card, no need to do anything else after this */
1631 ControlList( p_obj, i_fd,
1632 var_GetBool( p_obj, "v4l2-controls-reset" ), b_demux );
1633 if( VLC_FALSE == b_demux)
1638 demux_t *p_demux = (demux_t *) p_obj;
1640 /* Verify device support for the various IO methods */
1643 case IO_METHOD_READ:
1644 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE) )
1646 msg_Err( p_demux, "device does not support read i/o" );
1651 case IO_METHOD_MMAP:
1652 case IO_METHOD_USERPTR:
1653 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING) )
1655 msg_Err( p_demux, "device does not support streaming i/o" );
1661 msg_Err( p_demux, "io method not supported" );
1665 /* Reset Cropping */
1666 memset( &cropcap, 0, sizeof(cropcap) );
1667 cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1668 if( ioctl( i_fd, VIDIOC_CROPCAP, &cropcap ) >= 0 )
1670 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1671 crop.c = cropcap.defrect; /* reset to default */
1672 if( ioctl( i_fd, VIDIOC_S_CROP, &crop ) < 0 )
1677 /* Cropping not supported. */
1680 /* Errors ignored. */
1686 /* Try and find default resolution if not specified */
1687 memset( &fmt, 0, sizeof(fmt) );
1688 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1690 if( p_sys->i_width <= 0 || p_sys->i_height <= 0 )
1692 if( ioctl( i_fd, VIDIOC_G_FMT, &fmt ) < 0 )
1694 msg_Err( p_demux, "Cannot get default width and height." );
1698 p_sys->i_width = fmt.fmt.pix.width;
1699 p_sys->i_height = fmt.fmt.pix.height;
1701 if( fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE )
1703 p_sys->i_height = p_sys->i_height * 2;
1708 msg_Dbg( p_demux, "trying specified size %dx%d", p_sys->i_width, p_sys->i_height );
1711 fmt.fmt.pix.width = p_sys->i_width;
1712 fmt.fmt.pix.height = p_sys->i_height;
1713 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
1715 /* Test and set Chroma */
1716 fmt.fmt.pix.pixelformat = 0;
1717 if( p_sys->psz_requested_chroma && strlen( p_sys->psz_requested_chroma ) > 0 )
1719 /* User specified chroma */
1720 if( strlen( p_sys->psz_requested_chroma ) >= 4 )
1722 int i_requested_fourcc = VLC_FOURCC(
1723 p_sys->psz_requested_chroma[0], p_sys->psz_requested_chroma[1],
1724 p_sys->psz_requested_chroma[2], p_sys->psz_requested_chroma[3] );
1725 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
1727 if( v4l2chroma_to_fourcc[i].i_fourcc == i_requested_fourcc )
1729 fmt.fmt.pix.pixelformat = v4l2chroma_to_fourcc[i].i_v4l2;
1734 /* Try and set user chroma */
1735 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ( fmt.fmt.pix.pixelformat && ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 ) )
1737 msg_Warn( p_demux, "Driver is unable to use specified chroma %s. Trying defaults.", p_sys->psz_requested_chroma );
1738 fmt.fmt.pix.pixelformat = 0;
1742 /* If no user specified chroma, find best */
1743 /* This also decides if MPEG encoder card or not */
1744 if( !fmt.fmt.pix.pixelformat )
1746 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YVU420;
1747 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
1749 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
1750 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
1752 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1753 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 )
1755 msg_Warn( p_demux, "Could not select any of the default chromas; attempting to open as MPEG encoder card (access2)" );
1762 /* Reassign width, height and chroma incase driver override */
1763 p_sys->i_width = fmt.fmt.pix.width;
1764 p_sys->i_height = fmt.fmt.pix.height;
1766 /* Look up final fourcc */
1767 p_sys->i_fourcc = 0;
1768 for( int i = 0; v4l2chroma_to_fourcc[i].i_fourcc != 0; i++ )
1770 if( v4l2chroma_to_fourcc[i].i_v4l2 == fmt.fmt.pix.pixelformat )
1772 p_sys->i_fourcc = v4l2chroma_to_fourcc[i].i_fourcc;
1777 /* Buggy driver paranoia */
1778 i_min = fmt.fmt.pix.width * 2;
1779 if( fmt.fmt.pix.bytesperline < i_min )
1780 fmt.fmt.pix.bytesperline = i_min;
1781 i_min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
1782 if( fmt.fmt.pix.sizeimage < i_min )
1783 fmt.fmt.pix.sizeimage = i_min;
1785 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1786 /* This is new in Linux 2.6.19 */
1787 /* List supported frame rates */
1788 struct v4l2_frmivalenum frmival;
1790 frmival.pixel_format = fmt.fmt.pix.pixelformat;
1791 frmival.width = p_sys->i_width;
1792 frmival.height = p_sys->i_height;
1793 if( ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 )
1796 memset( &sz_fourcc, 0, sizeof( sz_fourcc ) );
1797 vlc_fourcc_to_char( p_sys->i_fourcc, &sz_fourcc );
1798 msg_Dbg( p_demux, "supported frame intervals for %4s, %dx%d:",
1799 sz_fourcc, frmival.width, frmival.height );
1800 switch( frmival.type )
1802 case V4L2_FRMIVAL_TYPE_DISCRETE:
1805 msg_Dbg( p_demux, " supported frame interval: %d/%d",
1806 frmival.discrete.numerator,
1807 frmival.discrete.denominator );
1809 } while( ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 );
1811 case V4L2_FRMIVAL_TYPE_STEPWISE:
1812 msg_Dbg( p_demux, " supported frame intervals: %d/%d to "
1813 "%d/%d using %d/%d increments",
1814 frmival.stepwise.min.numerator,
1815 frmival.stepwise.min.denominator,
1816 frmival.stepwise.max.numerator,
1817 frmival.stepwise.max.denominator,
1818 frmival.stepwise.step.numerator,
1819 frmival.stepwise.step.denominator );
1821 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
1822 msg_Dbg( p_demux, " supported frame intervals: %d/%d to %d/%d",
1823 frmival.stepwise.min.numerator,
1824 frmival.stepwise.min.denominator,
1825 frmival.stepwise.max.numerator,
1826 frmival.stepwise.max.denominator );
1832 /* Init IO method */
1835 case IO_METHOD_READ:
1836 if( InitRead( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
1839 case IO_METHOD_MMAP:
1840 if( InitMmap( p_demux, i_fd ) != VLC_SUCCESS ) goto open_failed;
1843 case IO_METHOD_USERPTR:
1844 if( InitUserP( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
1851 es_format_Init( &es_fmt, VIDEO_ES, p_sys->i_fourcc );
1852 es_fmt.video.i_width = p_sys->i_width;
1853 es_fmt.video.i_height = p_sys->i_height;
1854 es_fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
1856 /* Setup rgb mask for RGB formats */
1857 if( p_sys->i_fourcc == VLC_FOURCC( 'R','V','2','4' ) )
1859 /* This is in BGR format */
1860 es_fmt.video.i_bmask = 0x00ff0000;
1861 es_fmt.video.i_gmask = 0x0000ff00;
1862 es_fmt.video.i_rmask = 0x000000ff;
1865 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
1866 (char*)&es_fmt.i_codec, es_fmt.video.i_width, es_fmt.video.i_height );
1867 p_sys->p_es_video = es_out_Add( p_demux->out, &es_fmt );
1873 case IO_METHOD_READ:
1877 case IO_METHOD_MMAP:
1878 for (unsigned int i = 0; i < p_sys->i_nbuffers; ++i)
1880 struct v4l2_buffer buf;
1882 memset( &buf, 0, sizeof(buf) );
1883 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1884 buf.memory = V4L2_MEMORY_MMAP;
1887 if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
1889 msg_Err( p_demux, "VIDIOC_QBUF failed" );
1894 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1895 if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
1897 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
1903 case IO_METHOD_USERPTR:
1904 for( unsigned int i = 0; i < p_sys->i_nbuffers; ++i )
1906 struct v4l2_buffer buf;
1908 memset( &buf, 0, sizeof(buf) );
1909 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1910 buf.memory = V4L2_MEMORY_USERPTR;
1912 buf.m.userptr = (unsigned long)p_sys->p_buffers[i].start;
1913 buf.length = p_sys->p_buffers[i].length;
1915 if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
1917 msg_Err( p_demux, "VIDIOC_QBUF failed" );
1922 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1923 if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
1925 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
1933 if( p_sys->f_fps >= 0.1 )
1935 msg_Dbg( p_demux, "User set fps=%f", p_sys->f_fps );
1941 if( i_fd >= 0 ) close( i_fd );
1947 /*****************************************************************************
1948 * ResolveALSADeviceName: Change any . to : in the ALSA device name
1949 *****************************************************************************/
1950 static char *ResolveALSADeviceName( const char *psz_device )
1952 char* psz_alsa_name = strdup( psz_device );
1953 for( unsigned int i = 0; i < strlen( psz_device ); i++ )
1955 if( psz_alsa_name[i] == '.' ) psz_alsa_name[i] = ':';
1957 return psz_alsa_name;
1961 /*****************************************************************************
1962 * OpenAudioDev: open and set up the audio device and probe for capabilities
1963 *****************************************************************************/
1965 static int OpenAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys,
1966 vlc_bool_t b_demux )
1968 char *psz_device = p_sys->psz_adev;
1970 p_sys->p_alsa_pcm = NULL;
1971 char* psz_alsa_device_name = NULL;
1972 snd_pcm_hw_params_t *p_hw_params = NULL;
1973 snd_pcm_uframes_t buffer_size;
1974 snd_pcm_uframes_t chunk_size;
1978 psz_alsa_device_name =
1979 ResolveALSADeviceName( psz_device?: ALSA_DEFAULT );
1981 if( ( i_err = snd_pcm_open( &p_sys->p_alsa_pcm, psz_alsa_device_name,
1982 SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0)
1984 msg_Err( p_this, "Cannot open ALSA audio device %s (%s)",
1985 psz_alsa_device_name, snd_strerror( i_err ) );
1989 if( ( i_err = snd_pcm_nonblock( p_sys->p_alsa_pcm, 1 ) ) < 0)
1991 msg_Err( p_this, "Cannot set ALSA nonblock (%s)",
1992 snd_strerror( i_err ) );
1996 /* Begin setting hardware parameters */
1998 if( ( i_err = snd_pcm_hw_params_malloc( &p_hw_params ) ) < 0 )
2001 "ALSA: cannot allocate hardware parameter structure (%s)",
2002 snd_strerror( i_err ) );
2006 if( ( i_err = snd_pcm_hw_params_any( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
2009 "ALSA: cannot initialize hardware parameter structure (%s)",
2010 snd_strerror( i_err ) );
2014 /* Set Interleaved access */
2015 if( ( i_err = snd_pcm_hw_params_set_access( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 )
2017 msg_Err( p_this, "ALSA: cannot set access type (%s)",
2018 snd_strerror( i_err ) );
2022 /* Set 16 bit little endian */
2023 if( ( i_err = snd_pcm_hw_params_set_format( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_FORMAT_S16_LE ) ) < 0 )
2025 msg_Err( p_this, "ALSA: cannot set sample format (%s)",
2026 snd_strerror( i_err ) );
2030 /* Set sample rate */
2031 #ifdef HAVE_ALSA_NEW_API
2032 i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, &p_sys->i_sample_rate, NULL );
2034 i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, p_sys->i_sample_rate, NULL );
2038 msg_Err( p_this, "ALSA: cannot set sample rate (%s)",
2039 snd_strerror( i_err ) );
2044 unsigned int channels = p_sys->b_stereo ? 2 : 1;
2045 if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
2047 channels = ( channels==1 ) ? 2 : 1;
2048 msg_Warn( p_this, "ALSA: cannot set channel count (%s). "
2049 "Trying with channels=%d",
2050 snd_strerror( i_err ),
2052 if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
2054 msg_Err( p_this, "ALSA: cannot set channel count (%s)",
2055 snd_strerror( i_err ) );
2058 p_sys->b_stereo = ( channels == 2 );
2061 /* Set metrics for buffer calculations later */
2062 unsigned int buffer_time;
2063 if( ( i_err = snd_pcm_hw_params_get_buffer_time_max(p_hw_params, &buffer_time, 0) ) < 0 )
2065 msg_Err( p_this, "ALSA: cannot get buffer time max (%s)",
2066 snd_strerror( i_err ) );
2069 if (buffer_time > 500000) buffer_time = 500000;
2071 /* Set period time */
2072 unsigned int period_time = buffer_time / 4;
2073 #ifdef HAVE_ALSA_NEW_API
2074 i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, &period_time, 0 );
2076 i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, period_time, 0 );
2080 msg_Err( p_this, "ALSA: cannot set period time (%s)",
2081 snd_strerror( i_err ) );
2085 /* Set buffer time */
2086 #ifdef HAVE_ALSA_NEW_API
2087 i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, &buffer_time, 0 );
2089 i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, buffer_time, 0 );
2093 msg_Err( p_this, "ALSA: cannot set buffer time (%s)",
2094 snd_strerror( i_err ) );
2098 /* Apply new hardware parameters */
2099 if( ( i_err = snd_pcm_hw_params( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
2101 msg_Err( p_this, "ALSA: cannot set hw parameters (%s)",
2102 snd_strerror( i_err ) );
2106 /* Get various buffer metrics */
2107 snd_pcm_hw_params_get_period_size( p_hw_params, &chunk_size, 0 );
2108 snd_pcm_hw_params_get_buffer_size( p_hw_params, &buffer_size );
2109 if (chunk_size == buffer_size)
2112 "ALSA: period cannot equal buffer size (%lu == %lu)",
2113 chunk_size, buffer_size);
2117 int bits_per_sample = snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE);
2118 int bits_per_frame = bits_per_sample * channels;
2120 p_sys->i_alsa_chunk_size = chunk_size;
2121 p_sys->i_alsa_frame_size = (bits_per_sample / 8) * channels;
2122 p_sys->i_audio_max_frame_size = chunk_size * bits_per_frame / 8;
2124 snd_pcm_hw_params_free( p_hw_params );
2128 if( ( i_err = snd_pcm_prepare( p_sys->p_alsa_pcm ) ) < 0 )
2131 "ALSA: cannot prepare audio interface for use (%s)",
2132 snd_strerror( i_err ) );
2136 /* Return a fake handle so other tests work */
2139 free( psz_alsa_device_name );
2141 if( !p_sys->psz_adev )
2142 p_sys->psz_adev = strdup( ALSA_DEFAULT );
2147 if( i_fd >= 0 ) close( i_fd );
2149 if( p_hw_params ) snd_pcm_hw_params_free( p_hw_params );
2150 if( p_sys->p_alsa_pcm ) snd_pcm_close( p_sys->p_alsa_pcm );
2151 free( psz_alsa_device_name );
2158 static int OpenAudioDevOss( vlc_object_t *p_this, demux_sys_t *p_sys,
2159 vlc_bool_t b_demux )
2161 char *psz_device = p_sys->psz_adev;
2165 if( !psz_device ) psz_device = strdup( OSS_DEFAULT ); /* FIXME leak */
2167 if( (i_fd = open( psz_device, O_RDONLY | O_NONBLOCK )) < 0 )
2169 msg_Err( p_this, "cannot open OSS audio device (%m)" );
2173 i_format = AFMT_S16_LE;
2174 if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
2175 || i_format != AFMT_S16_LE )
2178 "cannot set audio format (16b little endian) (%m)" );
2182 if( ioctl( i_fd, SNDCTL_DSP_STEREO,
2183 &p_sys->b_stereo ) < 0 )
2185 msg_Err( p_this, "cannot set audio channels count (%m)" );
2189 if( ioctl( i_fd, SNDCTL_DSP_SPEED,
2190 &p_sys->i_sample_rate ) < 0 )
2192 msg_Err( p_this, "cannot set audio sample rate (%m)" );
2196 p_sys->i_audio_max_frame_size = 6 * 1024;
2198 if( !p_sys->psz_adev )
2199 p_sys->psz_adev = strdup( OSS_DEFAULT );
2204 if( i_fd >= 0 ) close( i_fd );
2209 static int OpenAudioDev( vlc_object_t *p_this, demux_sys_t *p_sys,
2210 vlc_bool_t b_demux )
2216 if( ( p_sys->i_audio_method & AUDIO_METHOD_ALSA ) && i_fd < 0 )
2217 i_fd = OpenAudioDevAlsa( p_this, p_sys, b_demux );
2220 if( ( p_sys->i_audio_method & AUDIO_METHOD_OSS ) && i_fd < 0 )
2221 i_fd = OpenAudioDevOss( p_this, p_sys, b_demux );
2226 psz_device = p_sys->psz_adev;
2228 msg_Dbg( p_this, "opened adev=`%s' %s %dHz",
2229 psz_device, p_sys->b_stereo ? "stereo" : "mono",
2230 p_sys->i_sample_rate );
2233 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
2235 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
2236 fmt.audio.i_rate = p_sys->i_sample_rate;
2237 fmt.audio.i_bitspersample = 16;
2238 fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
2239 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
2241 msg_Dbg( p_this, "new audio es %d channels %dHz",
2242 fmt.audio.i_channels, fmt.audio.i_rate );
2246 demux_t *p_demux = (demux_t *)p_this;
2247 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
2253 /*****************************************************************************
2254 * ProbeVideoDev: probe video for capabilities
2255 *****************************************************************************/
2256 static vlc_bool_t ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
2264 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
2266 msg_Err( p_obj, "cannot open video device (%m)" );
2270 /* Get device capabilites */
2272 if( ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
2274 msg_Err( p_obj, "cannot get video capabilities (%m)" );
2278 msg_Dbg( p_obj, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
2279 p_sys->dev_cap.card,
2280 p_sys->dev_cap.driver,
2281 (p_sys->dev_cap.version >> 16) & 0xFF,
2282 (p_sys->dev_cap.version >> 8) & 0xFF,
2283 p_sys->dev_cap.version & 0xFF,
2284 p_sys->dev_cap.bus_info );
2286 msg_Dbg( p_obj, "the device has the capabilities: (%c) Video Capure, "
2289 ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE ? 'X':' '),
2290 ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO ? 'X':' '),
2291 ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER ? 'X':' ') );
2293 msg_Dbg( p_obj, "supported I/O methods are: (%c) Read/Write, "
2295 "(%c) Asynchronous",
2296 ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
2297 ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
2298 ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
2300 /* Now, enumerate all the video inputs. This is useless at the moment
2301 since we have no way to present that info to the user except with
2304 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2306 struct v4l2_input t_input;
2308 while( ioctl( i_fd, VIDIOC_ENUMINPUT, &t_input ) >= 0 )
2311 t_input.index = p_sys->i_input;
2314 p_sys->p_inputs = calloc( 1, p_sys->i_input * sizeof( struct v4l2_input ) );
2315 if( !p_sys->p_inputs ) goto open_failed;
2317 for( i_index = 0; i_index < p_sys->i_input; i_index++ )
2319 p_sys->p_inputs[i_index].index = i_index;
2321 if( ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
2323 msg_Err( p_obj, "cannot get video input characteristics (%m)" );
2326 msg_Dbg( p_obj, "video input %i (%s) has type: %s",
2328 p_sys->p_inputs[i_index].name,
2329 p_sys->p_inputs[i_index].type
2330 == V4L2_INPUT_TYPE_TUNER ?
2332 "External analog input" );
2336 /* Probe video standards */
2337 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2339 struct v4l2_standard t_standards;
2340 t_standards.index = 0;
2341 while( ioctl( i_fd, VIDIOC_ENUMSTD, &t_standards ) >=0 )
2343 p_sys->i_standard++;
2344 t_standards.index = p_sys->i_standard;
2347 p_sys->p_standards = calloc( 1, p_sys->i_standard * sizeof( struct v4l2_standard ) );
2348 if( !p_sys->p_standards ) goto open_failed;
2350 for( i_standard = 0; i_standard < p_sys->i_standard; i_standard++ )
2352 p_sys->p_standards[i_standard].index = i_standard;
2354 if( ioctl( i_fd, VIDIOC_ENUMSTD, &p_sys->p_standards[i_standard] ) )
2356 msg_Err( p_obj, "cannot get video input standards (%m)" );
2359 msg_Dbg( p_obj, "video standard %i is: %s",
2361 p_sys->p_standards[i_standard].name);
2365 /* initialize the structures for the ioctls */
2366 for( i_index = 0; i_index < 32; i_index++ )
2368 p_sys->p_audios[i_index].index = i_index;
2371 /* Probe audio inputs */
2372 if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
2374 while( p_sys->i_audio < 32 &&
2375 ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
2377 if( ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
2379 msg_Err( p_obj, "cannot get audio input characteristics (%m)" );
2383 msg_Dbg( p_obj, "audio device %i (%s) is %s",
2385 p_sys->p_audios[p_sys->i_audio].name,
2386 p_sys->p_audios[p_sys->i_audio].capability &
2387 V4L2_AUDCAP_STEREO ?
2388 "Stereo" : "Mono" );
2394 /* List tuner caps */
2395 if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
2397 struct v4l2_tuner tuner;
2398 memset( &tuner, 0, sizeof(tuner) );
2399 while( ioctl( i_fd, VIDIOC_G_TUNER, &tuner ) >= 0 )
2402 memset( &tuner, 0, sizeof(tuner) );
2403 tuner.index = p_sys->i_tuner;
2406 p_sys->p_tuners = calloc( 1, p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
2407 if( !p_sys->p_tuners ) goto open_failed;
2409 for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
2411 p_sys->p_tuners[i_index].index = i_index;
2413 if( ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
2415 msg_Err( p_obj, "cannot get tuner characteristics (%m)" );
2418 msg_Dbg( p_obj, "tuner %i (%s) has type: %s, "
2419 "frequency range: %.1f %s -> %.1f %s",
2421 p_sys->p_tuners[i_index].name,
2422 p_sys->p_tuners[i_index].type
2423 == V4L2_TUNER_RADIO ?
2424 "Radio" : "Analog TV",
2425 p_sys->p_tuners[i_index].rangelow * 62.5,
2426 p_sys->p_tuners[i_index].capability &
2427 V4L2_TUNER_CAP_LOW ?
2429 p_sys->p_tuners[i_index].rangehigh * 62.5,
2430 p_sys->p_tuners[i_index].capability &
2431 V4L2_TUNER_CAP_LOW ?
2436 /* Probe for available chromas */
2437 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2439 struct v4l2_fmtdesc codec;
2442 memset( &codec, 0, sizeof(codec) );
2443 codec.index = i_index;
2444 codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2446 while( ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
2449 codec.index = i_index;
2452 p_sys->i_codec = i_index;
2454 p_sys->p_codecs = calloc( 1, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
2456 for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
2458 p_sys->p_codecs[i_index].index = i_index;
2459 p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2461 if( ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
2463 msg_Err( p_obj, "cannot get codec description (%m)" );
2467 /* only print if vlc supports the format */
2468 vlc_bool_t b_codec_supported = VLC_FALSE;
2469 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
2471 if( v4l2chroma_to_fourcc[i].i_v4l2 == p_sys->p_codecs[i_index].pixelformat )
2473 b_codec_supported = VLC_TRUE;
2476 memset( &sz_fourcc, 0, sizeof( sz_fourcc ) );
2477 vlc_fourcc_to_char( v4l2chroma_to_fourcc[i].i_fourcc, &sz_fourcc );
2478 msg_Dbg( p_obj, "device supports chroma %4s [%s]",
2480 p_sys->p_codecs[i_index].description );
2482 #ifdef VIDIOC_ENUM_FRAMESIZES
2483 /* This is new in Linux 2.6.19 */
2484 /* List valid frame sizes for this format */
2485 struct v4l2_frmsizeenum frmsize;
2487 frmsize.pixel_format = p_sys->p_codecs[i_index].pixelformat;
2488 if( ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) < 0 )
2490 /* Not all devices support this ioctl */
2491 msg_Warn( p_obj, "Unable to query for frame sizes" );
2495 switch( frmsize.type )
2497 case V4L2_FRMSIZE_TYPE_DISCRETE:
2501 " device supports size %dx%d",
2502 frmsize.discrete.width, frmsize.discrete.height );
2504 } while( ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 );
2506 case V4L2_FRMSIZE_TYPE_STEPWISE:
2508 " device supports sizes %dx%d to %dx%d using %dx%d increments",
2509 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2510 frmsize.stepwise.max_width, frmsize.stepwise.max_height,
2511 frmsize.stepwise.step_width, frmsize.stepwise.step_height );
2513 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
2515 " device supports all sizes %dx%d to %dx%d",
2516 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2517 frmsize.stepwise.max_width, frmsize.stepwise.max_height );
2524 if( !b_codec_supported )
2526 msg_Dbg( p_obj, "device codec %s not supported as access_demux",
2527 p_sys->p_codecs[i_index].description );
2534 if( i_fd >= 0 ) close( i_fd );
2539 if( i_fd >= 0 ) close( i_fd );
2544 /*****************************************************************************
2545 * ProbeAudioDev: probe audio for capabilities
2546 *****************************************************************************/
2548 static vlc_bool_t ProbeAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys,
2552 snd_pcm_t *p_alsa_pcm;
2553 char* psz_alsa_device_name = ResolveALSADeviceName( psz_device ?: ALSA_DEFAULT );
2555 if( ( i_err = snd_pcm_open( &p_alsa_pcm, psz_alsa_device_name, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0 )
2557 msg_Err( p_this, "cannot open device %s for ALSA audio (%s)", psz_alsa_device_name, snd_strerror( i_err ) );
2558 free( psz_alsa_device_name );
2562 snd_pcm_close( p_alsa_pcm );
2563 free( psz_alsa_device_name );
2569 static vlc_bool_t ProbeAudioDevOss( vlc_object_t *p_this, demux_sys_t *p_sys,
2574 if( !psz_device ) psz_device = strdup( OSS_DEFAULT ); /* FIXME leak */
2576 if( ( i_fd = open( psz_device, O_RDONLY | O_NONBLOCK ) ) < 0 )
2578 msg_Err( p_this, "cannot open device %s for OSS audio (%m)", psz_device );
2582 /* this will fail if the device is video */
2583 if( ioctl( i_fd, SNDCTL_DSP_GETCAPS, &i_caps ) < 0 )
2585 msg_Err( p_this, "cannot get audio caps (%m)" );
2589 if( i_fd >= 0 ) close( i_fd );
2594 if( i_fd >= 0 ) close( i_fd );
2598 static vlc_bool_t ProbeAudioDev( vlc_object_t *p_this, demux_sys_t *p_sys,
2602 if( ( p_sys->i_audio_method & AUDIO_METHOD_ALSA )
2603 && ProbeAudioDevAlsa( p_this, p_sys, psz_device ) )
2605 p_sys->i_audio_method = AUDIO_METHOD_ALSA;
2610 if( ( p_sys->i_audio_method & AUDIO_METHOD_OSS )
2611 && ProbeAudioDevOss( p_this, p_sys, psz_device ) )
2613 p_sys->i_audio_method = AUDIO_METHOD_OSS;
2617 p_sys->i_audio_method = 0;
2621 /*****************************************************************************
2622 * Print a user-class v4l2 control's details, create the relevant variable,
2623 * change the value if needed.
2624 *****************************************************************************/
2625 static void ControlListPrint( vlc_object_t *p_obj, int i_fd,
2626 struct v4l2_queryctrl queryctrl,
2627 vlc_bool_t b_reset, vlc_bool_t b_demux )
2629 struct v4l2_querymenu querymenu;
2636 vlc_value_t val, val2;
2638 if( queryctrl.flags & V4L2_CTRL_FLAG_GRABBED )
2639 msg_Dbg( p_obj, " control is busy" );
2640 if( queryctrl.flags & V4L2_CTRL_FLAG_READ_ONLY )
2641 msg_Dbg( p_obj, " control is read-only" );
2643 for( i = 0; controls[i].psz_name != NULL; i++ )
2644 if( controls[i].i_cid == queryctrl.id ) break;
2646 if( controls[i].psz_name )
2648 psz_name = strdup( controls[i].psz_name );
2649 char psz_cfg_name[40];
2650 sprintf( psz_cfg_name, CFG_PREFIX "%s", psz_name );
2651 i_val = var_CreateGetInteger( p_obj, psz_cfg_name );
2652 var_Destroy( p_obj, psz_cfg_name );
2657 psz_name = strdup( (const char *)queryctrl.name );
2658 for( psz_buf = psz_name; *psz_buf; psz_buf++ )
2660 if( *psz_buf == ' ' ) *psz_buf = '-';
2665 switch( queryctrl.type )
2667 case V4L2_CTRL_TYPE_INTEGER:
2668 msg_Dbg( p_obj, " integer control" );
2670 " valid values: %d to %d by steps of %d",
2671 queryctrl.minimum, queryctrl.maximum,
2674 var_Create( p_obj, psz_name,
2675 VLC_VAR_INTEGER | VLC_VAR_HASMIN | VLC_VAR_HASMAX
2676 | VLC_VAR_HASSTEP | VLC_VAR_ISCOMMAND );
2677 val.i_int = queryctrl.minimum;
2678 var_Change( p_obj, psz_name, VLC_VAR_SETMIN, &val, NULL );
2679 val.i_int = queryctrl.maximum;
2680 var_Change( p_obj, psz_name, VLC_VAR_SETMAX, &val, NULL );
2681 val.i_int = queryctrl.step;
2682 var_Change( p_obj, psz_name, VLC_VAR_SETSTEP, &val, NULL );
2684 case V4L2_CTRL_TYPE_BOOLEAN:
2685 msg_Dbg( p_obj, " boolean control" );
2686 var_Create( p_obj, psz_name,
2687 VLC_VAR_BOOL | VLC_VAR_ISCOMMAND );
2689 case V4L2_CTRL_TYPE_MENU:
2690 msg_Dbg( p_obj, " menu control" );
2691 var_Create( p_obj, psz_name,
2692 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE
2693 | VLC_VAR_ISCOMMAND );
2694 memset( &querymenu, 0, sizeof( querymenu ) );
2695 for( i_mid = queryctrl.minimum;
2696 i_mid <= (unsigned)queryctrl.maximum;
2699 querymenu.index = i_mid;
2700 querymenu.id = queryctrl.id;
2701 if( ioctl( i_fd, VIDIOC_QUERYMENU, &querymenu ) >= 0 )
2703 msg_Dbg( p_obj, " %d: %s",
2704 querymenu.index, querymenu.name );
2705 val.i_int = querymenu.index;
2706 val2.psz_string = (char *)querymenu.name;
2707 var_Change( p_obj, psz_name,
2708 VLC_VAR_ADDCHOICE, &val, &val2 );
2712 case V4L2_CTRL_TYPE_BUTTON:
2713 msg_Dbg( p_obj, " button control" );
2714 var_Create( p_obj, psz_name,
2715 VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2718 msg_Dbg( p_obj, " unknown control type (FIXME)" );
2723 switch( queryctrl.type )
2725 case V4L2_CTRL_TYPE_INTEGER:
2726 case V4L2_CTRL_TYPE_BOOLEAN:
2727 case V4L2_CTRL_TYPE_MENU:
2729 struct v4l2_control control;
2730 msg_Dbg( p_obj, " default value: %d",
2731 queryctrl.default_value );
2732 memset( &control, 0, sizeof( control ) );
2733 control.id = queryctrl.id;
2734 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
2736 msg_Dbg( p_obj, " current value: %d", control.value );
2740 i_val = control.value;
2741 if( b_reset && queryctrl.default_value != control.value )
2743 msg_Dbg( p_obj, " reset value to default" );
2744 Control( p_obj, i_fd, psz_name,
2745 queryctrl.id, queryctrl.default_value );
2750 Control( p_obj, i_fd, psz_name,
2751 queryctrl.id, i_val );
2759 val.psz_string = (char *)queryctrl.name;
2760 var_Change( p_obj, psz_name, VLC_VAR_SETTEXT, &val, NULL );
2761 val.i_int = queryctrl.id;
2762 val2.psz_string = (char *)psz_name;
2763 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
2765 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
2768 var_SetBool( p_obj, psz_name, i_val );
2770 case VLC_VAR_INTEGER:
2771 var_SetInteger( p_obj, psz_name, i_val );
2776 msg_Warn( p_obj, "FIXME: %s %s %d", __FILE__, __func__,
2782 var_AddCallback( p_obj, psz_name,
2783 DemuxControlCallback, (void*)queryctrl.id );
2785 var_AddCallback( p_obj, psz_name,
2786 AccessControlCallback, (void*)queryctrl.id );
2791 /*****************************************************************************
2792 * List all user-class v4l2 controls, set them to the user specified
2793 * value and create the relevant variables to enable runtime changes
2794 *****************************************************************************/
2795 static int ControlList( vlc_object_t *p_obj, int i_fd,
2796 vlc_bool_t b_reset, vlc_bool_t b_demux )
2798 struct v4l2_queryctrl queryctrl;
2801 memset( &queryctrl, 0, sizeof( queryctrl ) );
2803 /* A list of available controls (aka the variable name) will be
2804 * stored as choices in the "controls" variable. We'll thus be able
2805 * to use those to create an appropriate interface */
2806 var_Create( p_obj, "controls", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
2808 var_Create( p_obj, "controls-update", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2810 /* Add a control to reset all controls to their default values */
2811 vlc_value_t val, val2;
2812 var_Create( p_obj, "controls-reset", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2813 val.psz_string = _( "Reset controls to default" );
2814 var_Change( p_obj, "controls-reset", VLC_VAR_SETTEXT, &val, NULL );
2816 val2.psz_string = (char *)"controls-reset";
2817 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
2819 var_AddCallback( p_obj, "controls-reset", DemuxControlResetCallback, NULL );
2821 var_AddCallback( p_obj, "controls-reset", AccessControlResetCallback, NULL );
2823 /* List public controls */
2824 for( i_cid = V4L2_CID_BASE;
2825 i_cid < V4L2_CID_LASTP1;
2828 queryctrl.id = i_cid;
2829 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2831 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2833 msg_Dbg( p_obj, "Available control: %s (%x)",
2834 queryctrl.name, queryctrl.id );
2835 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
2839 /* List private controls */
2840 for( i_cid = V4L2_CID_PRIVATE_BASE;
2844 queryctrl.id = i_cid;
2845 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2847 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2849 msg_Dbg( p_obj, "Available private control: %s (%x)",
2850 queryctrl.name, queryctrl.id );
2851 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
2859 /*****************************************************************************
2860 * Reset all user-class v4l2 controls to their default value
2861 *****************************************************************************/
2862 static int ControlReset( vlc_object_t *p_obj, int i_fd )
2864 struct v4l2_queryctrl queryctrl;
2867 memset( &queryctrl, 0, sizeof( queryctrl ) );
2869 /* public controls */
2870 for( i_cid = V4L2_CID_BASE;
2871 i_cid < V4L2_CID_LASTP1;
2874 queryctrl.id = i_cid;
2875 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2877 struct v4l2_control control;
2878 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2880 memset( &control, 0, sizeof( control ) );
2881 control.id = queryctrl.id;
2882 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
2883 && queryctrl.default_value != control.value )
2886 for( i = 0; controls[i].psz_name != NULL; i++ )
2887 if( controls[i].i_cid == queryctrl.id ) break;
2888 Control( p_obj, i_fd,
2889 controls[i].psz_name ? controls[i].psz_name
2890 : (const char *)queryctrl.name,
2891 queryctrl.id, queryctrl.default_value );
2896 /* private controls */
2897 for( i_cid = V4L2_CID_PRIVATE_BASE;
2901 queryctrl.id = i_cid;
2902 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2904 struct v4l2_control control;
2905 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2907 memset( &control, 0, sizeof( control ) );
2908 control.id = queryctrl.id;
2909 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
2910 && queryctrl.default_value != control.value )
2912 Control( p_obj, i_fd, (const char *)queryctrl.name,
2913 queryctrl.id, queryctrl.default_value );
2922 /*****************************************************************************
2923 * Issue user-class v4l2 controls
2924 *****************************************************************************/
2925 static int Control( vlc_object_t *p_obj, int i_fd,
2926 const char *psz_name, int i_cid, int i_value )
2928 struct v4l2_queryctrl queryctrl;
2929 struct v4l2_control control;
2934 memset( &queryctrl, 0, sizeof( queryctrl ) );
2936 queryctrl.id = i_cid;
2938 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) < 0
2939 || queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2941 msg_Dbg( p_obj, "%s (%x) control is not supported.", psz_name,
2943 return VLC_EGENERIC;
2946 memset( &control, 0, sizeof( control ) );
2951 control.value = i_value;
2952 if( ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
2954 msg_Err( p_obj, "unable to set %s to %d (%m)", psz_name,
2956 return VLC_EGENERIC;
2959 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
2962 msg_Dbg( p_obj, "video %s: %d", psz_name, control.value );
2963 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
2966 val.b_bool = control.value;
2967 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
2968 var_SetVoid( p_obj, "controls-update" );
2970 case VLC_VAR_INTEGER:
2971 val.i_int = control.value;
2972 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
2973 var_SetVoid( p_obj, "controls-update" );
2980 /*****************************************************************************
2981 * On the fly change settings callback
2982 *****************************************************************************/
2983 static int DemuxControlCallback( vlc_object_t *p_this,
2984 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
2987 demux_t *p_demux = (demux_t*)p_this;
2988 demux_sys_t *p_sys = p_demux->p_sys;
2989 int i_cid = (int)p_data;
2991 int i_fd = p_sys->i_fd_video;
2994 return VLC_EGENERIC;
2996 Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
2998 return VLC_EGENERIC;
3001 static int DemuxControlResetCallback( vlc_object_t *p_this,
3002 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3005 demux_t *p_demux = (demux_t*)p_this;
3006 demux_sys_t *p_sys = p_demux->p_sys;
3008 int i_fd = p_sys->i_fd_video;
3011 return VLC_EGENERIC;
3013 ControlReset( p_this, i_fd );
3015 return VLC_EGENERIC;
3018 static int AccessControlCallback( vlc_object_t *p_this,
3019 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3022 access_t *p_access = (access_t *)p_this;
3023 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
3024 int i_cid = (int)p_data;
3026 int i_fd = p_sys->i_fd_video;
3029 return VLC_EGENERIC;
3031 Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
3033 return VLC_EGENERIC;
3036 static int AccessControlResetCallback( vlc_object_t *p_this,
3037 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3040 access_t *p_access = (access_t *)p_this;
3041 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
3043 int i_fd = p_sys->i_fd_video;
3046 return VLC_EGENERIC;
3048 ControlReset( p_this, i_fd );
3050 return VLC_EGENERIC;