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
37 /*****************************************************************************
39 *****************************************************************************/
42 #include <vlc_access.h>
43 #include <vlc_demux.h>
44 #include <vlc_input.h>
50 #include <sys/ioctl.h>
53 #include <linux/videodev2.h>
55 #include <sys/soundcard.h>
58 # define ALSA_PCM_NEW_HW_PARAMS_API
59 # define ALSA_PCM_NEW_SW_PARAMS_API
60 # include <alsa/asoundlib.h>
65 /*****************************************************************************
67 *****************************************************************************/
69 static int DemuxOpen ( vlc_object_t * );
70 static void DemuxClose( vlc_object_t * );
71 static int AccessOpen ( vlc_object_t * );
72 static void AccessClose( vlc_object_t * );
74 #define DEV_TEXT N_("Device name")
75 #define DEV_LONGTEXT N_( \
76 "Name of the device to use. " \
77 "If you don't specify anything, /dev/video0 will be used.")
78 #define STANDARD_TEXT N_( "Standard" )
79 #define STANDARD_LONGTEXT N_( \
80 "Video standard (Default, SECAM, PAL, or NTSC)." )
81 #define CHROMA_TEXT N_("Video input chroma format")
82 #define CHROMA_LONGTEXT N_( \
83 "Force the Video4Linux2 video device to use a specific chroma format " \
84 "(eg. I420 or I422 for raw images, MJPEG for M-JPEG compressed input) " \
85 "(Complete list: GREY, I240, RV16, RV15, RV24, RV32, YUY2, YUYV, UYVY, " \
86 "I41N, I422, I420, I411, I410, MJPG)")
87 #define INPUT_TEXT N_( "Input" )
88 #define INPUT_LONGTEXT N_( \
89 "Input of the card to use (see debug)." )
90 #define AUDIO_INPUT_TEXT N_( "Audio input" )
91 #define AUDIO_INPUT_LONGTEXT N_( \
92 "Audio input of the card to use (see debug)." )
93 #define IOMETHOD_TEXT N_( "IO Method" )
94 #define IOMETHOD_LONGTEXT N_( \
95 "IO Method (READ, MMAP, USERPTR)." )
96 #define WIDTH_TEXT N_( "Width" )
97 #define WIDTH_LONGTEXT N_( \
98 "Force width (-1 for autodetect)." )
99 #define HEIGHT_TEXT N_( "Height" )
100 #define HEIGHT_LONGTEXT N_( \
101 "Force height (-1 for autodetect)." )
102 #define FPS_TEXT N_( "Framerate" )
103 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
104 "(-1 for autodetect)." )
106 #define CTRL_RESET_TEXT N_( "Reset v4l2 controls" )
107 #define CTRL_RESET_LONGTEXT N_( \
108 "Reset controls to defaults provided by the v4l2 driver." )
109 #define BRIGHTNESS_TEXT N_( "Brightness" )
110 #define BRIGHTNESS_LONGTEXT N_( \
111 "Brightness of the video input (if supported by the v4l2 driver)." )
112 #define CONTRAST_TEXT N_( "Contrast" )
113 #define CONTRAST_LONGTEXT N_( \
114 "Contrast of the video input (if supported by the v4l2 driver)." )
115 #define SATURATION_TEXT N_( "Saturation" )
116 #define SATURATION_LONGTEXT N_( \
117 "Saturation of the video input (if supported by the v4l2 driver)." )
118 #define HUE_TEXT N_( "Hue" )
119 #define HUE_LONGTEXT N_( \
120 "Hue of the video input (if supported by the v4l2 driver)." )
121 #define BLACKLEVEL_TEXT N_( "Black level" )
122 #define BLACKLEVEL_LONGTEXT N_( \
123 "Black level of the video input (if supported by the v4l2 driver)." )
124 #define AUTOWHITEBALANCE_TEXT N_( "Auto white balance" )
125 #define AUTOWHITEBALANCE_LONGTEXT N_( \
126 "Automatically set the white balance of the video input " \
127 "(if supported by the v4l2 driver)." )
128 #define DOWHITEBALANCE_TEXT N_( "Do white balance" )
129 #define DOWHITEBALANCE_LONGTEXT N_( \
130 "Trigger a white balancing action, useless if auto white balance is " \
131 "activated (if supported by the v4l2 driver)." )
132 #define REDBALANCE_TEXT N_( "Red balance" )
133 #define REDBALANCE_LONGTEXT N_( \
134 "Red balance of the video input (if supported by the v4l2 driver)." )
135 #define BLUEBALANCE_TEXT N_( "Blue balance" )
136 #define BLUEBALANCE_LONGTEXT N_( \
137 "Blue balance of the video input (if supported by the v4l2 driver)." )
138 #define GAMMA_TEXT N_( "Gamma" )
139 #define GAMMA_LONGTEXT N_( \
140 "Gamma of the video input (if supported by the v4l2 driver)." )
141 #define EXPOSURE_TEXT N_( "Exposure" )
142 #define EXPOSURE_LONGTEXT N_( \
143 "Exposure of the video input (if supported by the v4L2 driver)." )
144 #define AUTOGAIN_TEXT N_( "Auto gain" )
145 #define AUTOGAIN_LONGTEXT N_( \
146 "Automatically set the video input's gain (if supported by the " \
148 #define GAIN_TEXT N_( "Gain" )
149 #define GAIN_LONGTEXT N_( \
150 "Video input's gain (if supported by the v4l2 driver)." )
151 #define HFLIP_TEXT N_( "Horizontal flip" )
152 #define HFLIP_LONGTEXT N_( \
153 "Flip the video horizontally (if supported by the v4l2 driver)." )
154 #define VFLIP_TEXT N_( "Vertical flip" )
155 #define VFLIP_LONGTEXT N_( \
156 "Flip the video vertically (if supported by the v4l2 driver)." )
157 #define HCENTER_TEXT N_( "Horizontal centering" )
158 #define HCENTER_LONGTEXT N_( \
159 "Set the camera's horizontal centering (if supported by the v4l2 driver)." )
160 #define VCENTER_TEXT N_( "Vertical centering" )
161 #define VCENTER_LONGTEXT N_( \
162 "Set the camera's vertical centering (if supported by the v4l2 driver)." )
164 #define ADEV_TEXT N_("Audio device name")
166 #define ADEV_LONGTEXT N_( \
167 "Name of the audio device to use. " \
168 "If you don't specify anything, \"/dev/dsp\" will be used for OSS.")
170 #define ADEV_LONGTEXT N_( \
171 "Name of the audio device to use. " \
172 "If you don't specify anything, \"/dev/dsp\" will be used for OSS, " \
175 #define AUDIO_METHOD_TEXT N_( "Audio method" )
177 #define AUDIO_METHOD_LONGTEXT N_( \
178 "Audio method to use: 0 to disable audio, 1 for OSS." )
180 #define AUDIO_METHOD_LONGTEXT N_( \
181 "Audio method to use: 0 to disable audio, 1 for OSS, 2 for ALSA, " \
182 "3 for ALSA or OSS (ALSA is prefered)." )
184 #define AUDIO_VOLUME_TEXT N_( "Volume" )
185 #define AUDIO_VOLUME_LONGTEXT N_( \
186 "Volume of the audio input (if supported by the v4l2 driver)." )
187 #define AUDIO_BALANCE_TEXT N_( "Balance" )
188 #define AUDIO_BALANCE_LONGTEXT N_( \
189 "Balance of the audio input (if supported by the v4l2 driver)." )
190 #define AUDIO_MUTE_TEXT N_( "Mute" )
191 #define AUDIO_MUTE_LONGTEXT N_( \
192 "Mute audio input (if supported by the v4l2 driver)." )
193 #define AUDIO_BASS_TEXT N_( "Bass" )
194 #define AUDIO_BASS_LONGTEXT N_( \
195 "Bass level of the audio input (if supported by the v4l2 driver)." )
196 #define AUDIO_TREBLE_TEXT N_( "Treble" )
197 #define AUDIO_TREBLE_LONGTEXT N_( \
198 "Treble level of the audio input (if supported by the v4l2 driver)." )
199 #define AUDIO_LOUDNESS_TEXT N_( "Loudness" )
200 #define AUDIO_LOUDNESS_LONGTEXT N_( \
201 "Loudness of the audio input (if supported by the v4l2 driver)." )
203 #define STEREO_TEXT N_( "Stereo" )
204 #define STEREO_LONGTEXT N_( \
205 "Capture the audio stream in stereo." )
206 #define SAMPLERATE_TEXT N_( "Samplerate" )
207 #define SAMPLERATE_LONGTEXT N_( \
208 "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
210 #define CACHING_TEXT N_("Caching value in ms")
211 #define CACHING_LONGTEXT N_( \
212 "Caching value for V4L2 captures. This " \
213 "value should be set in milliseconds." )
214 #define S_CTRLS_TEXT N_("v4l2 driver controls")
215 #define S_CTRLS_LONGTEXT N_( \
216 "Set the v4l2 driver controls to the values specified using a comma " \
217 "separated list optionally encapsulated by curly braces " \
218 "(e.g.: {video_bitrate=6000000,audio_crc=0,stream_type=3} ). " \
219 "To list available controls, increase verbosity (-vvv) " \
220 "or use the v4l2-ctl application." )
222 #define TUNER_TEXT N_("Tuner id")
223 #define TUNER_LONGTEXT N_( \
224 "Tuner id (see debug output)." )
225 #define FREQUENCY_TEXT N_("Frequency")
226 #define FREQUENCY_LONGTEXT N_( \
227 "Tuner frequency in Hz or kHz (see debug output)" )
228 #define TUNER_AUDIO_MODE_TEXT N_("Audio mode")
229 #define TUNER_AUDIO_MODE_LONGTEXT N_( \
230 "Tuner audio mono/stereo and track selection." )
238 static int i_standards_list[] =
239 { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
240 static const char *psz_standards_list_text[] =
241 { N_("Default"), N_("SECAM"), N_("PAL"), N_("NTSC") };
243 static int i_iomethod_list[] =
244 { IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR };
245 static const char *psz_iomethod_list_text[] =
246 { N_("READ"), N_("MMAP"), N_("USERPTR") };
248 static int i_tuner_audio_modes_list[] =
249 { V4L2_TUNER_MODE_MONO, V4L2_TUNER_MODE_STEREO,
250 V4L2_TUNER_MODE_LANG1, V4L2_TUNER_MODE_LANG2,
251 V4L2_TUNER_MODE_SAP, V4L2_TUNER_MODE_LANG1_LANG2 };
252 static const char *psz_tuner_audio_modes_list_text[] =
255 N_( "Primary language (Analog TV tuners only)" ),
256 N_( "Secondary language (Analog TV tuners only)" ),
257 N_( "Second audio program (Analog TV tuners only)" ),
258 N_( "Primary language left, Secondary language right" ) };
263 #define AUDIO_METHOD_OSS 1
264 #define OSS_DEFAULT "/dev/dsp"
265 #define AUDIO_METHOD_ALSA 2
266 #define ALSA_DEFAULT "hw"
267 #define CFG_PREFIX "v4l2-"
270 set_shortname( _("Video4Linux2") );
271 set_description( _("Video4Linux2 input") );
272 set_category( CAT_INPUT );
273 set_subcategory( SUBCAT_INPUT_ACCESS );
275 set_section( N_( "Video input" ), NULL );
276 add_string( CFG_PREFIX "dev", "/dev/video0", 0, DEV_TEXT, DEV_LONGTEXT,
279 add_integer( CFG_PREFIX "standard", 0, NULL, STANDARD_TEXT,
280 STANDARD_LONGTEXT, VLC_FALSE );
282 change_integer_list( i_standards_list, psz_standards_list_text, 0 );
283 add_string( CFG_PREFIX "chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
286 add_integer( CFG_PREFIX "input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
289 add_integer( CFG_PREFIX "audio-input", 0, NULL, AUDIO_INPUT_TEXT,
290 AUDIO_INPUT_LONGTEXT, VLC_TRUE );
292 add_integer( CFG_PREFIX "io", IO_METHOD_MMAP, NULL, IOMETHOD_TEXT,
293 IOMETHOD_LONGTEXT, VLC_TRUE );
295 change_integer_list( i_iomethod_list, psz_iomethod_list_text, 0 );
296 add_integer( CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
297 WIDTH_LONGTEXT, VLC_TRUE );
299 add_integer( CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
300 HEIGHT_LONGTEXT, VLC_TRUE );
302 add_float( CFG_PREFIX "fps", 0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
305 set_section( N_( "Audio input" ), NULL );
306 add_string( CFG_PREFIX "adev", NULL, 0, ADEV_TEXT, ADEV_LONGTEXT,
309 add_integer( CFG_PREFIX "audio-method", AUDIO_METHOD_OSS|AUDIO_METHOD_ALSA,
310 NULL, AUDIO_METHOD_TEXT, AUDIO_METHOD_LONGTEXT, VLC_TRUE );
312 add_bool( CFG_PREFIX "stereo", VLC_TRUE, NULL, STEREO_TEXT, STEREO_LONGTEXT,
315 add_integer( CFG_PREFIX "samplerate", 48000, NULL, SAMPLERATE_TEXT,
316 SAMPLERATE_LONGTEXT, VLC_TRUE );
318 add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
319 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
322 set_section( N_( "Tuner" ), NULL );
323 add_integer( CFG_PREFIX "tuner", 0, NULL, TUNER_TEXT, TUNER_LONGTEXT,
326 add_integer( CFG_PREFIX "tuner-frequency", -1, NULL, FREQUENCY_TEXT,
327 FREQUENCY_LONGTEXT, VLC_TRUE );
329 add_integer( CFG_PREFIX "tuner-audio-mode", -1, NULL, TUNER_AUDIO_MODE_TEXT,
330 TUNER_AUDIO_MODE_LONGTEXT, VLC_TRUE );
332 change_integer_list( i_tuner_audio_modes_list,
333 psz_tuner_audio_modes_list_text, 0 );
335 set_section( N_( "Controls" ),
336 N_( "v4l2 driver controls, if supported by your v4l2 driver." ) );
337 add_bool( CFG_PREFIX "controls-reset", VLC_FALSE, NULL, CTRL_RESET_TEXT,
338 CTRL_RESET_LONGTEXT, VLC_TRUE );
340 add_integer( CFG_PREFIX "brightness", -1, NULL, BRIGHTNESS_TEXT,
341 BRIGHTNESS_LONGTEXT, VLC_TRUE );
343 add_integer( CFG_PREFIX "contrast", -1, NULL, CONTRAST_TEXT,
344 CONTRAST_LONGTEXT, VLC_TRUE );
346 add_integer( CFG_PREFIX "saturation", -1, NULL, SATURATION_TEXT,
347 SATURATION_LONGTEXT, VLC_TRUE );
349 add_integer( CFG_PREFIX "hue", -1, NULL, HUE_TEXT,
350 HUE_LONGTEXT, VLC_TRUE );
352 add_integer( CFG_PREFIX "black-level", -1, NULL, BLACKLEVEL_TEXT,
353 BLACKLEVEL_LONGTEXT, VLC_TRUE );
355 add_integer( CFG_PREFIX "auto-white-balance", -1, NULL,
356 AUTOWHITEBALANCE_TEXT, AUTOWHITEBALANCE_LONGTEXT, VLC_TRUE );
358 add_integer( CFG_PREFIX "do-white-balance", -1, NULL, DOWHITEBALANCE_TEXT,
359 DOWHITEBALANCE_LONGTEXT, VLC_TRUE );
361 add_integer( CFG_PREFIX "red-balance", -1, NULL, REDBALANCE_TEXT,
362 REDBALANCE_LONGTEXT, VLC_TRUE );
364 add_integer( CFG_PREFIX "blue-balance", -1, NULL, BLUEBALANCE_TEXT,
365 BLUEBALANCE_LONGTEXT, VLC_TRUE );
367 add_integer( CFG_PREFIX "gamma", -1, NULL, GAMMA_TEXT,
368 GAMMA_LONGTEXT, VLC_TRUE );
370 add_integer( CFG_PREFIX "exposure", -1, NULL, EXPOSURE_TEXT,
371 EXPOSURE_LONGTEXT, VLC_TRUE );
373 add_integer( CFG_PREFIX "autogain", -1, NULL, AUTOGAIN_TEXT,
374 AUTOGAIN_LONGTEXT, VLC_TRUE );
376 add_integer( CFG_PREFIX "gain", -1, NULL, GAIN_TEXT,
377 GAIN_LONGTEXT, VLC_TRUE );
379 add_integer( CFG_PREFIX "hflip", -1, NULL, HFLIP_TEXT,
380 HFLIP_LONGTEXT, VLC_TRUE );
382 add_integer( CFG_PREFIX "vflip", -1, NULL, VFLIP_TEXT,
383 VFLIP_LONGTEXT, VLC_TRUE );
385 add_integer( CFG_PREFIX "hcenter", -1, NULL, HCENTER_TEXT,
386 HCENTER_LONGTEXT, VLC_TRUE );
388 add_integer( CFG_PREFIX "vcenter", -1, NULL, VCENTER_TEXT,
389 VCENTER_LONGTEXT, VLC_TRUE );
391 add_integer( CFG_PREFIX "audio-volume", -1, NULL, AUDIO_VOLUME_TEXT,
392 AUDIO_VOLUME_LONGTEXT, VLC_TRUE );
394 add_integer( CFG_PREFIX "audio-balance", -1, NULL, AUDIO_BALANCE_TEXT,
395 AUDIO_BALANCE_LONGTEXT, VLC_TRUE );
397 add_bool( CFG_PREFIX "audio-mute", VLC_FALSE, NULL, AUDIO_MUTE_TEXT,
398 AUDIO_MUTE_LONGTEXT, VLC_TRUE );
400 add_integer( CFG_PREFIX "audio-bass", -1, NULL, AUDIO_BASS_TEXT,
401 AUDIO_BASS_LONGTEXT, VLC_TRUE );
403 add_integer( CFG_PREFIX "audio-treble", -1, NULL, AUDIO_TREBLE_TEXT,
404 AUDIO_TREBLE_LONGTEXT, VLC_TRUE );
406 add_integer( CFG_PREFIX "audio-loudness", -1, NULL, AUDIO_LOUDNESS_TEXT,
407 AUDIO_LOUDNESS_LONGTEXT, VLC_TRUE );
409 add_string( CFG_PREFIX "set-ctrls", NULL, NULL, S_CTRLS_TEXT,
410 S_CTRLS_LONGTEXT, VLC_TRUE );
413 add_shortcut( "v4l2" );
414 set_capability( "access_demux", 10 );
415 set_callbacks( DemuxOpen, DemuxClose );
418 add_shortcut( "v4l2c" );
419 set_description( _("Video4Linux2 Compressed A/V") );
420 set_capability( "access2", 0 );
421 /* use these when open as access_demux fails; VLC will use another demux */
422 set_callbacks( AccessOpen, AccessClose );
426 /*****************************************************************************
427 * Access: local prototypes
428 *****************************************************************************/
430 static void CommonClose( vlc_object_t *, demux_sys_t * );
431 static void ParseMRL( demux_sys_t *, char *, vlc_object_t * );
432 static void GetV4L2Params( demux_sys_t *, vlc_object_t * );
433 static void SetAvailControlsByString( vlc_object_t *, demux_sys_t *, int );
435 static int DemuxControl( demux_t *, int, va_list );
436 static int AccessControl( access_t *, int, va_list );
438 static int Demux( demux_t * );
439 static ssize_t AccessRead( access_t *, uint8_t *, size_t );
441 static block_t* GrabVideo( demux_t *p_demux );
442 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t );
443 static block_t* GrabAudio( demux_t *p_demux );
445 static vlc_bool_t IsPixelFormatSupported( demux_t *p_demux,
446 unsigned int i_pixelformat );
449 static char* ResolveALSADeviceName( const char *psz_device );
451 static int OpenVideoDev( vlc_object_t *, demux_sys_t *, vlc_bool_t );
452 static int OpenAudioDev( vlc_object_t *, demux_sys_t *, vlc_bool_t );
453 static vlc_bool_t ProbeVideoDev( vlc_object_t *, demux_sys_t *,
455 static vlc_bool_t ProbeAudioDev( vlc_object_t *, demux_sys_t *,
458 static int ControlList( vlc_object_t *, int , vlc_bool_t, vlc_bool_t );
459 static int Control( vlc_object_t *, int i_fd,
460 const char *psz_name, int i_cid, int i_value );
462 static int DemuxControlCallback( vlc_object_t *p_this, const char *psz_var,
463 vlc_value_t oldval, vlc_value_t newval,
465 static int DemuxControlResetCallback( vlc_object_t *p_this, const char *psz_var,
466 vlc_value_t oldval, vlc_value_t newval,
468 static int AccessControlCallback( vlc_object_t *p_this, const char *psz_var,
469 vlc_value_t oldval, vlc_value_t newval,
471 static int AccessControlResetCallback( vlc_object_t *p_this,
472 const char *psz_var, vlc_value_t oldval,
473 vlc_value_t newval, void *p_data );
479 } v4l2chroma_to_fourcc[] =
482 { V4L2_PIX_FMT_GREY, VLC_FOURCC('G','R','E','Y') },
483 { V4L2_PIX_FMT_HI240, VLC_FOURCC('I','2','4','0') },
484 { V4L2_PIX_FMT_RGB565, VLC_FOURCC('R','V','1','6') },
485 { V4L2_PIX_FMT_RGB555, VLC_FOURCC('R','V','1','5') },
486 { V4L2_PIX_FMT_BGR24, VLC_FOURCC('R','V','2','4') },
487 { V4L2_PIX_FMT_BGR32, VLC_FOURCC('R','V','3','2') },
488 { V4L2_PIX_FMT_YUYV, VLC_FOURCC('Y','U','Y','2') },
489 { V4L2_PIX_FMT_YUYV, VLC_FOURCC('Y','U','Y','V') },
490 { V4L2_PIX_FMT_UYVY, VLC_FOURCC('U','Y','V','Y') },
491 { V4L2_PIX_FMT_Y41P, VLC_FOURCC('I','4','1','N') },
492 { V4L2_PIX_FMT_YUV422P, VLC_FOURCC('I','4','2','2') },
493 { V4L2_PIX_FMT_YVU420, VLC_FOURCC('Y','V','1','2') },
494 { V4L2_PIX_FMT_YUV411P, VLC_FOURCC('I','4','1','1') },
495 { V4L2_PIX_FMT_YUV410, VLC_FOURCC('I','4','1','0') },
497 /* Raw data types, not in V4L2 spec but still in videodev2.h and supported
499 { V4L2_PIX_FMT_YUV420, VLC_FOURCC('I','4','2','0') },
500 /* FIXME { V4L2_PIX_FMT_RGB444, VLC_FOURCC('R','V','3','2') }, */
502 /* Compressed data types */
503 { V4L2_PIX_FMT_MJPEG, VLC_FOURCC('M','J','P','G') },
505 { V4L2_PIX_FMT_JPEG, VLC_FOURCC('J','P','E','G') },
506 { V4L2_PIX_FMT_DV, VLC_FOURCC('?','?','?','?') },
507 { V4L2_PIX_FMT_MPEG, VLC_FOURCC('?','?','?','?') },
513 * List of V4L2 chromas were confident enough to use as fallbacks if the
514 * user hasn't provided a --v4l2-chroma value.
516 static const __u32 p_chroma_fallbacks[] =
517 { V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_YVU420, V4L2_PIX_FMT_YUV422P,
518 V4L2_PIX_FMT_YUYV, V4L2_PIX_FMT_MJPEG };
522 const char *psz_name;
526 { "brightness", V4L2_CID_BRIGHTNESS },
527 { "contrast", V4L2_CID_CONTRAST },
528 { "saturation", V4L2_CID_SATURATION },
529 { "hue", V4L2_CID_HUE },
530 { "audio-volume", V4L2_CID_AUDIO_VOLUME },
531 { "audio-balance", V4L2_CID_AUDIO_BALANCE },
532 { "audio-bass", V4L2_CID_AUDIO_BASS },
533 { "audio-treble", V4L2_CID_AUDIO_TREBLE },
534 { "audio-mute", V4L2_CID_AUDIO_MUTE },
535 { "audio-loudness", V4L2_CID_AUDIO_LOUDNESS },
536 { "black-level", V4L2_CID_BLACK_LEVEL },
537 { "auto-white-balance", V4L2_CID_AUTO_WHITE_BALANCE },
538 { "do-white-balance", V4L2_CID_DO_WHITE_BALANCE },
539 { "red-balance", V4L2_CID_RED_BALANCE },
540 { "blue-balance", V4L2_CID_BLUE_BALANCE },
541 { "gamma", V4L2_CID_GAMMA },
542 { "exposure", V4L2_CID_EXPOSURE },
543 { "autogain", V4L2_CID_AUTOGAIN },
544 { "gain", V4L2_CID_GAIN },
545 { "hflip", V4L2_CID_HFLIP },
546 { "vflip", V4L2_CID_VFLIP },
547 { "hcenter", V4L2_CID_HCENTER },
548 { "vcenter", V4L2_CID_VCENTER },
561 char *psz_device; /* Main device from MRL, can be video or audio */
569 char *psz_requested_chroma;
576 struct v4l2_capability dev_cap;
579 struct v4l2_input *p_inputs;
580 int i_selected_input;
583 struct v4l2_standard *p_standards;
584 v4l2_std_id i_selected_standard_id;
587 /* V4L2 devices cannot have more than 32 audio inputs */
588 struct v4l2_audio p_audios[32];
589 int i_selected_audio_input;
592 struct v4l2_tuner *p_tuners;
595 struct v4l2_fmtdesc *p_codecs;
597 struct buffer_t *p_buffers;
598 unsigned int i_nbuffers;
602 float f_fps; /* <= 0.0 mean to grab at full rate */
603 mtime_t i_video_pts; /* only used when f_fps > 0 */
606 es_out_id_t *p_es_video;
609 unsigned int i_sample_rate;
611 size_t i_audio_max_frame_size;
612 block_t *p_block_audio;
613 es_out_id_t *p_es_audio;
619 snd_pcm_t *p_alsa_pcm;
620 size_t i_alsa_frame_size;
621 int i_alsa_chunk_size;
633 static int FindMainDevice( vlc_object_t *p_this, demux_sys_t *p_sys,
634 int i_flags, vlc_bool_t b_demux,
635 vlc_bool_t b_forced )
637 /* Find main device (video or audio) */
638 if( p_sys->psz_device && *p_sys->psz_device )
640 msg_Dbg( p_this, "main device='%s'", p_sys->psz_device );
642 vlc_bool_t b_maindevice_is_video = VLC_FALSE;
644 /* Try to open as video device */
645 if( i_flags & FIND_VIDEO )
647 msg_Dbg( p_this, "trying device '%s' as video", p_sys->psz_device );
648 if( ProbeVideoDev( p_this, p_sys, p_sys->psz_device ) )
650 msg_Dbg( p_this, "'%s' is a video device", p_sys->psz_device );
651 /* Device was a video device */
652 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
653 p_sys->psz_vdev = p_sys->psz_device;
654 p_sys->psz_device = NULL;
655 p_sys->i_fd_video = OpenVideoDev( p_this, p_sys, b_demux );
656 if( p_sys->i_fd_video < 0 )
658 b_maindevice_is_video = VLC_TRUE;
659 /* If successful we carry on to try the audio if access is forced */
663 /* Try to open as audio device only if main device was not detected as video above */
664 if( i_flags & FIND_AUDIO && !b_maindevice_is_video )
666 msg_Dbg( p_this, "trying device '%s' as audio", p_sys->psz_device );
667 if( ProbeAudioDev( p_this, p_sys, p_sys->psz_device ) )
669 msg_Dbg( p_this, "'%s' is an audio device", p_sys->psz_device );
670 /* Device was an audio device */
671 free( p_sys->psz_adev );
672 p_sys->psz_adev = p_sys->psz_device;
673 p_sys->psz_device = NULL;
674 p_sys->i_fd_audio = OpenAudioDev( p_this, p_sys, b_demux );
675 if( p_sys->i_fd_audio < 0 )
677 /* If successful we carry on to try the video if access is forced */
682 /* If no device opened, only continue if the access was forced */
683 if( b_forced == VLC_FALSE
684 && !( ( i_flags & FIND_VIDEO && p_sys->i_fd_video >= 0 )
685 || ( i_flags & FIND_AUDIO && p_sys->i_fd_audio >= 0 ) ) )
690 /* Find video device */
691 if( i_flags & FIND_VIDEO && p_sys->i_fd_video < 0 )
693 if( !p_sys->psz_vdev || !*p_sys->psz_vdev )
695 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
696 p_sys->psz_vdev = var_CreateGetString( p_this, "v4l2-dev" );
699 msg_Dbg( p_this, "opening '%s' as video", p_sys->psz_vdev );
700 if( p_sys->psz_vdev && *p_sys->psz_vdev
701 && ProbeVideoDev( p_this, p_sys, p_sys->psz_vdev ) )
703 p_sys->i_fd_video = OpenVideoDev( p_this, p_sys, b_demux );
707 /* Find audio device */
708 if( i_flags & FIND_AUDIO && p_sys->i_fd_audio < 0 )
710 if( !p_sys->psz_adev )
712 p_sys->psz_adev = var_CreateGetNonEmptyString( p_this, "v4l2-adev" );
715 msg_Dbg( p_this, "opening '%s' as audio", p_sys->psz_adev );
716 if( ProbeAudioDev( p_this, p_sys, p_sys->psz_adev ) )
718 p_sys->i_fd_audio = OpenAudioDev( p_this, p_sys, b_demux );
722 if( !( ( i_flags & FIND_VIDEO && p_sys->i_fd_video >= 0 )
723 || ( i_flags & FIND_AUDIO && p_sys->i_fd_audio >= 0 ) ) )
730 /*****************************************************************************
731 * DemuxOpen: opens v4l2 device, access_demux callback
732 *****************************************************************************
734 * url: <video device>::::
736 *****************************************************************************/
737 static int DemuxOpen( vlc_object_t *p_this )
739 demux_t *p_demux = (demux_t*)p_this;
742 /* Only when selected */
743 if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
746 p_demux->pf_control = DemuxControl;
747 p_demux->pf_demux = Demux;
748 p_demux->info.i_update = 0;
749 p_demux->info.i_title = 0;
750 p_demux->info.i_seekpoint = 0;
752 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
753 if( p_sys == NULL ) return VLC_ENOMEM;
755 GetV4L2Params(p_sys, (vlc_object_t *) p_demux);
757 ParseMRL( p_sys, p_demux->psz_path, (vlc_object_t *) p_demux );
760 /* Alsa support available? */
761 msg_Dbg( p_demux, "ALSA input support available" );
764 if( FindMainDevice( p_this, p_sys, FIND_VIDEO|FIND_AUDIO,
765 VLC_TRUE, !strncmp( p_demux->psz_access, "v4l2", 4 ) ) != VLC_SUCCESS )
767 DemuxClose( p_this );
774 /*****************************************************************************
775 * GetV4L2Params: fill in p_sys parameters (shared by DemuxOpen and AccessOpen)
776 *****************************************************************************/
777 static void GetV4L2Params( demux_sys_t *p_sys, vlc_object_t *p_obj )
779 p_sys->i_video_pts = -1;
781 p_sys->i_selected_standard_id =
782 i_standards_list[var_CreateGetInteger( p_obj, "v4l2-standard" )];
784 p_sys->i_selected_input = var_CreateGetInteger( p_obj, "v4l2-input" );
785 p_sys->i_selected_audio_input =
786 var_CreateGetInteger( p_obj, "v4l2-audio-input" );
788 p_sys->io = var_CreateGetInteger( p_obj, "v4l2-io" );
790 p_sys->i_width = var_CreateGetInteger( p_obj, "v4l2-width" );
791 p_sys->i_height = var_CreateGetInteger( p_obj, "v4l2-height" );
793 var_CreateGetBool( p_obj, "v4l2-controls-reset" );
795 p_sys->f_fps = var_CreateGetFloat( p_obj, "v4l2-fps" );
796 p_sys->i_sample_rate = var_CreateGetInteger( p_obj, "v4l2-samplerate" );
797 p_sys->psz_requested_chroma = var_CreateGetString( p_obj, "v4l2-chroma" );
799 p_sys->i_audio_method = var_CreateGetInteger( p_obj, "v4l2-audio-method" );
801 p_sys->b_stereo = var_CreateGetBool( p_obj, "v4l2-stereo" );
803 p_sys->i_pts = var_CreateGetInteger( p_obj, "v4l2-caching" );
805 p_sys->i_cur_tuner = var_CreateGetInteger( p_obj, "v4l2-tuner" );
806 p_sys->i_frequency = var_CreateGetInteger( p_obj, "v4l2-tuner-frequency" );
807 p_sys->i_audio_mode = var_CreateGetInteger( p_obj, "v4l2-tuner-audio-mode" );
809 p_sys->psz_set_ctrls = var_CreateGetString( p_obj, "v4l2-set-ctrls" );
811 p_sys->psz_device = p_sys->psz_vdev = p_sys->psz_adev = NULL;
812 p_sys->i_fd_video = -1;
813 p_sys->i_fd_audio = -1;
815 p_sys->p_es_video = p_sys->p_es_audio = 0;
816 p_sys->p_block_audio = 0;
819 /*****************************************************************************
820 * ParseMRL: parse the options contained in the MRL
821 *****************************************************************************/
822 static void ParseMRL( demux_sys_t *p_sys, char *psz_path, vlc_object_t *p_obj )
824 char *psz_dup = strdup( psz_path );
825 char *psz_parser = psz_dup;
827 while( *psz_parser && *psz_parser != ':' )
832 if( *psz_parser == ':' )
837 *psz_parser++ = '\0';
839 if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
843 psz_parser += strlen( "adev=" );
844 if( strchr( psz_parser, ':' ) )
846 i_len = strchr( psz_parser, ':' ) - psz_parser;
850 i_len = strlen( psz_parser );
853 p_sys->psz_adev = strndup( psz_parser, i_len );
854 if( !*p_sys->psz_adev )
856 free( p_sys->psz_adev );
857 p_sys->psz_adev = NULL;
862 else if( !strncmp( psz_parser, "standard=", strlen( "standard=" ) ) )
864 psz_parser += strlen( "standard=" );
865 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
867 p_sys->i_selected_standard_id = V4L2_STD_PAL;
868 psz_parser += strlen( "pal" );
870 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
872 p_sys->i_selected_standard_id = V4L2_STD_NTSC;
873 psz_parser += strlen( "ntsc" );
875 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
877 p_sys->i_selected_standard_id = V4L2_STD_SECAM;
878 psz_parser += strlen( "secam" );
880 else if( !strncmp( psz_parser, "default", strlen( "default" ) ) )
882 p_sys->i_selected_standard_id = V4L2_STD_UNKNOWN;
883 psz_parser += strlen( "default" );
887 p_sys->i_selected_standard_id = i_standards_list[strtol( psz_parser, &psz_parser, 0 )];
890 else if( !strncmp( psz_parser, "chroma=", strlen( "chroma=" ) ) )
894 psz_parser += strlen( "chroma=" );
895 if( strchr( psz_parser, ':' ) )
897 i_len = strchr( psz_parser, ':' ) - psz_parser;
901 i_len = strlen( psz_parser );
904 if( p_sys->psz_requested_chroma ) free( p_sys->psz_requested_chroma );
905 p_sys->psz_requested_chroma = strndup( psz_parser, i_len );
909 else if( !strncmp( psz_parser, "input=", strlen( "input=" ) ) )
911 p_sys->i_selected_input = strtol( psz_parser + strlen( "input=" ),
914 else if( !strncmp( psz_parser, "audio-input=", strlen( "audio-input=" ) ) )
916 p_sys->i_selected_audio_input = strtol( psz_parser + strlen( "audio-input=" ),
919 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
921 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
924 else if( !strncmp( psz_parser, "io=", strlen( "io=" ) ) )
926 psz_parser += strlen( "io=" );
927 if( !strncmp( psz_parser, "read", strlen( "read" ) ) )
929 p_sys->io = IO_METHOD_READ;
930 psz_parser += strlen( "read" );
932 else if( !strncmp( psz_parser, "mmap", strlen( "mmap" ) ) )
934 p_sys->io = IO_METHOD_MMAP;
935 psz_parser += strlen( "mmap" );
937 else if( !strncmp( psz_parser, "userptr", strlen( "userptr" ) ) )
939 p_sys->io = IO_METHOD_USERPTR;
940 psz_parser += strlen( "userptr" );
944 p_sys->io = strtol( psz_parser, &psz_parser, 0 );
947 else if( !strncmp( psz_parser, "width=",
948 strlen( "width=" ) ) )
951 strtol( psz_parser + strlen( "width=" ),
954 else if( !strncmp( psz_parser, "height=",
955 strlen( "height=" ) ) )
958 strtol( psz_parser + strlen( "height=" ),
961 else if( !strncmp( psz_parser, "controls-reset",
962 strlen( "controls-reset" ) ) )
964 var_SetBool( p_obj, "v4l2-controls-reset", VLC_TRUE );
965 psz_parser += strlen( "controls-reset" );
968 else if( !strncmp( psz_parser, "brightness=",
969 strlen( "brightness=" ) ) )
971 var_SetInteger( p_obj, "brightness",
972 strtol( psz_parser + strlen( "brightness=" ),
975 else if( !strncmp( psz_parser, "contrast=",
976 strlen( "contrast=" ) ) )
978 var_SetInteger( p_obj, "contrast",
979 strtol( psz_parser + strlen( "contrast=" ),
982 else if( !strncmp( psz_parser, "saturation=",
983 strlen( "saturation=" ) ) )
985 var_SetInteger( p_obj, "saturation",
986 strtol( psz_parser + strlen( "saturation=" ),
989 else if( !strncmp( psz_parser, "hue=",
992 var_SetInteger( p_obj, "hue",
993 strtol( psz_parser + strlen( "hue=" ),
996 else if( !strncmp( psz_parser, "gamma=",
997 strlen( "gamma=" ) ) )
999 var_SetInteger( p_obj, "gamma",
1000 strtol( psz_parser + strlen( "gamma=" ),
1004 else if( !strncmp( psz_parser, "samplerate=",
1005 strlen( "samplerate=" ) ) )
1007 p_sys->i_sample_rate =
1008 strtol( psz_parser + strlen( "samplerate=" ),
1011 else if( !strncmp( psz_parser, "audio-method", strlen( "audio-method" ) ) )
1013 p_sys->i_audio_method =
1014 strtol( psz_parser + strlen( "audio-method" ),
1017 else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
1019 psz_parser += strlen( "stereo" );
1020 p_sys->b_stereo = VLC_TRUE;
1022 else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
1024 psz_parser += strlen( "mono" );
1025 p_sys->b_stereo = VLC_FALSE;
1027 else if( !strncmp( psz_parser, "caching=", strlen( "caching=" ) ) )
1029 p_sys->i_pts = strtol( psz_parser + strlen( "caching=" ),
1032 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
1034 p_sys->i_cur_tuner = strtol( psz_parser + strlen( "tuner=" ),
1037 else if( !strncmp( psz_parser, "tuner-frequency=", strlen( "tuner-frequency=" ) ) )
1039 p_sys->i_frequency = strtol( psz_parser
1040 + strlen( "tuner-frequency=" ),
1043 else if( !strncmp( psz_parser, "tuner-audio-mode=", strlen( "tuner-audio-mode=" ) ) )
1045 p_sys->i_audio_mode = strtol( psz_parser
1046 + strlen( "tuner-audio-mode=" ),
1049 else if( !strncmp( psz_parser, "set-ctrls=", strlen( "set-ctrls=" )) )
1053 psz_parser += strlen( "set-ctrls=" );
1054 if( strchr( psz_parser, ':' ) )
1056 i_len = strchr( psz_parser, ':' ) - psz_parser;
1060 i_len = strlen( psz_parser );
1063 p_sys->psz_set_ctrls = strndup( psz_parser, i_len );
1065 psz_parser += i_len;
1069 char *psz_unk = strchr( psz_parser, ':' );
1071 psz_unk = strndup( psz_parser, psz_unk - psz_parser );
1073 psz_unk = strdup( psz_parser);
1074 msg_Warn( p_obj, "unknown option %s", psz_unk );
1078 while( *psz_parser && *psz_parser != ':' )
1083 if( *psz_parser == '\0' )
1093 p_sys->psz_device = strdup( psz_dup );
1095 if( psz_dup ) free( psz_dup );
1098 /*****************************************************************************
1099 * Close: close device, free resources
1100 *****************************************************************************/
1101 static void AccessClose( vlc_object_t *p_this )
1103 access_t *p_access = (access_t *)p_this;
1104 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1106 CommonClose( p_this, p_sys );
1109 static void DemuxClose( vlc_object_t *p_this )
1111 struct v4l2_buffer buf;
1112 enum v4l2_buf_type buf_type;
1115 demux_t *p_demux = (demux_t *)p_this;
1116 demux_sys_t *p_sys = p_demux->p_sys;
1118 /* Stop video capture */
1119 if( p_sys->i_fd_video >= 0 )
1123 case IO_METHOD_READ:
1127 case IO_METHOD_MMAP:
1128 case IO_METHOD_USERPTR:
1129 /* Some drivers 'hang' internally if this is not done before streamoff */
1130 for( unsigned int i = 0; i < p_sys->i_nbuffers; i++ )
1132 memset( &buf, 0, sizeof(buf) );
1133 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1134 buf.memory = ( p_sys->io == IO_METHOD_USERPTR ) ?
1135 V4L2_MEMORY_USERPTR : V4L2_MEMORY_MMAP;
1136 ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ); /* ignore result */
1139 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1140 if( ioctl( p_sys->i_fd_video, VIDIOC_STREAMOFF, &buf_type ) < 0 ) {
1141 msg_Err( p_this, "VIDIOC_STREAMOFF failed" );
1148 /* Free Video Buffers */
1149 if( p_sys->p_buffers ) {
1152 case IO_METHOD_READ:
1153 free( p_sys->p_buffers[0].start );
1156 case IO_METHOD_MMAP:
1157 for( i = 0; i < p_sys->i_nbuffers; ++i )
1159 if( munmap( p_sys->p_buffers[i].start, p_sys->p_buffers[i].length ) )
1161 msg_Err( p_this, "munmap failed" );
1166 case IO_METHOD_USERPTR:
1167 for( i = 0; i < p_sys->i_nbuffers; ++i )
1169 free( p_sys->p_buffers[i].orig_userp );
1173 free( p_sys->p_buffers );
1176 CommonClose( p_this, p_sys );
1179 static void CommonClose( vlc_object_t *p_this, demux_sys_t *p_sys )
1182 if( p_sys->i_fd_video >= 0 ) close( p_sys->i_fd_video );
1184 if( p_sys->p_alsa_pcm )
1186 snd_pcm_close( p_sys->p_alsa_pcm );
1187 p_sys->i_fd_audio = -1;
1190 if( p_sys->i_fd_audio >= 0 ) close( p_sys->i_fd_audio );
1192 if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
1193 free( p_sys->psz_device );
1194 free( p_sys->psz_vdev );
1195 free( p_sys->psz_adev );
1196 free( p_sys->p_standards );
1197 free( p_sys->p_inputs );
1198 free( p_sys->p_tuners );
1199 free( p_sys->p_codecs );
1200 free( p_sys->psz_requested_chroma );
1201 free( p_sys->psz_set_ctrls );
1206 /*****************************************************************************
1207 * AccessOpen: opens v4l2 device, access2 callback
1208 *****************************************************************************
1210 * url: <video device>::::
1212 *****************************************************************************/
1213 static int AccessOpen( vlc_object_t * p_this )
1215 access_t *p_access = (access_t*) p_this;
1216 demux_sys_t * p_sys;
1218 /* Only when selected */
1219 if( *p_access->psz_access == '\0' ) return VLC_EGENERIC;
1221 p_access->pf_read = AccessRead;
1222 p_access->pf_block = NULL;
1223 p_access->pf_seek = NULL;
1224 p_access->pf_control = AccessControl;
1225 p_access->info.i_update = 0;
1226 p_access->info.i_size = 0;
1227 p_access->info.i_pos = 0;
1228 p_access->info.b_eof = VLC_FALSE;
1229 p_access->info.i_title = 0;
1230 p_access->info.i_seekpoint = 0;
1232 p_sys = calloc( 1, sizeof( demux_sys_t ) );
1233 p_access->p_sys = (access_sys_t *) p_sys;
1234 if( p_sys == NULL ) return VLC_ENOMEM;
1236 GetV4L2Params( p_sys, (vlc_object_t *) p_access );
1238 ParseMRL( p_sys, p_access->psz_path, (vlc_object_t *) p_access );
1240 if( FindMainDevice( p_this, p_sys, FIND_VIDEO,
1241 VLC_FALSE, !strncmp( p_access->psz_access, "v4l2", 4 ) ) != VLC_SUCCESS )
1243 AccessClose( p_this );
1244 return VLC_EGENERIC;
1250 /*****************************************************************************
1252 *****************************************************************************/
1253 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
1255 demux_sys_t *p_sys = p_demux->p_sys;
1261 /* Special for access_demux */
1262 case DEMUX_CAN_PAUSE:
1263 case DEMUX_CAN_SEEK:
1264 case DEMUX_SET_PAUSE_STATE:
1265 case DEMUX_CAN_CONTROL_PACE:
1266 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1270 case DEMUX_GET_PTS_DELAY:
1271 pi64 = (int64_t*)va_arg( args, int64_t * );
1272 *pi64 = (int64_t)p_sys->i_pts * 1000;
1275 case DEMUX_GET_TIME:
1276 pi64 = (int64_t*)va_arg( args, int64_t * );
1280 /* TODO implement others */
1282 return VLC_EGENERIC;
1285 return VLC_EGENERIC;
1288 /*****************************************************************************
1289 * AccessControl: access2 callback
1290 *****************************************************************************/
1291 static int AccessControl( access_t *p_access, int i_query, va_list args )
1293 vlc_bool_t *pb_bool;
1296 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1301 case ACCESS_CAN_SEEK:
1302 case ACCESS_CAN_FASTSEEK:
1303 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1304 *pb_bool = VLC_FALSE;
1306 case ACCESS_CAN_PAUSE:
1307 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1308 *pb_bool = VLC_FALSE;
1310 case ACCESS_CAN_CONTROL_PACE:
1311 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1312 *pb_bool = VLC_FALSE;
1316 case ACCESS_GET_MTU:
1317 pi_int = (int*)va_arg( args, int * );
1321 case ACCESS_GET_PTS_DELAY:
1322 pi_64 = (int64_t*)va_arg( args, int64_t * );
1323 *pi_64 = (int64_t) p_sys->i_pts * 1000;
1327 case ACCESS_SET_PAUSE_STATE:
1331 case ACCESS_GET_TITLE_INFO:
1332 case ACCESS_SET_TITLE:
1333 case ACCESS_SET_SEEKPOINT:
1334 case ACCESS_SET_PRIVATE_ID_STATE:
1335 case ACCESS_GET_CONTENT_TYPE:
1336 case ACCESS_GET_META:
1337 return VLC_EGENERIC;
1340 msg_Warn( p_access, "Unimplemented query in control(%d).", i_query);
1341 return VLC_EGENERIC;
1347 /*****************************************************************************
1348 * AccessRead: access2 callback
1349 ******************************************************************************/
1350 static ssize_t AccessRead( access_t * p_access, uint8_t * p_buffer, size_t i_len )
1352 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1356 ufd.fd = p_sys->i_fd_video;
1357 ufd.events = POLLIN;
1359 if( p_access->info.b_eof )
1364 if( p_access->b_die )
1369 while( ( i_ret = poll( &ufd, 1, 500 ) ) == 0 );
1373 msg_Err( p_access, "Polling error (%m)." );
1377 i_ret = read( p_sys->i_fd_video, p_buffer, i_len );
1380 p_access->info.b_eof = VLC_TRUE;
1382 else if( i_ret > 0 )
1384 p_access->info.i_pos += i_ret;
1390 /*****************************************************************************
1391 * Demux: Processes the audio or video frame
1392 *****************************************************************************/
1393 static int Demux( demux_t *p_demux )
1395 demux_sys_t *p_sys = p_demux->p_sys;
1396 es_out_id_t *p_es = p_sys->p_es_audio;
1397 block_t *p_block = NULL;
1399 /* Try grabbing audio frames first */
1400 if( p_sys->i_fd_audio < 0 || !( p_block = GrabAudio( p_demux ) ) )
1402 /* Try grabbing video frame */
1403 p_es = p_sys->p_es_video;
1404 if( p_sys->i_fd_video > 0 ) p_block = GrabVideo( p_demux );
1409 /* Sleep so we do not consume all the cpu, 10ms seems
1410 * like a good value (100fps) */
1415 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
1416 es_out_Send( p_demux->out, p_es, p_block );
1421 /*****************************************************************************
1422 * GrabVideo: Grab a video frame
1423 *****************************************************************************/
1424 static block_t* GrabVideo( demux_t *p_demux )
1426 demux_sys_t *p_sys = p_demux->p_sys;
1428 block_t *p_block = NULL;
1429 struct v4l2_buffer buf;
1432 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1434 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1436 /* Did we wait long enough ? (frame rate reduction) */
1437 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1440 /* Grab Video Frame */
1443 case IO_METHOD_READ:
1444 i_ret = read( p_sys->i_fd_video, p_sys->p_buffers[0].start, p_sys->p_buffers[0].length );
1452 /* Could ignore EIO, see spec. */
1455 msg_Err( p_demux, "Failed to read frame" );
1460 p_block = ProcessVideoFrame( p_demux, (uint8_t*)p_sys->p_buffers[0].start, i_ret );
1461 if( !p_block ) return 0;
1465 case IO_METHOD_MMAP:
1466 memset( &buf, 0, sizeof(buf) );
1467 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1468 buf.memory = V4L2_MEMORY_MMAP;
1470 /* Wait for next frame */
1471 if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
1478 /* Could ignore EIO, see spec. */
1481 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1486 if( buf.index >= p_sys->i_nbuffers ) {
1487 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1491 p_block = ProcessVideoFrame( p_demux, p_sys->p_buffers[buf.index].start, buf.bytesused );
1492 if( !p_block ) return 0;
1495 if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
1497 msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
1503 case IO_METHOD_USERPTR:
1504 memset( &buf, 0, sizeof(buf) );
1505 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1506 buf.memory = V4L2_MEMORY_USERPTR;
1508 /* Wait for next frame */
1509 if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
1516 /* Could ignore EIO, see spec. */
1519 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1526 for( i = 0; i < p_sys->i_nbuffers; i++ )
1528 if( buf.m.userptr == (unsigned long)p_sys->p_buffers[i].start &&
1529 buf.length == p_sys->p_buffers[i].length ) break;
1532 if( i >= p_sys->i_nbuffers )
1534 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1538 p_block = ProcessVideoFrame( p_demux, (uint8_t*)buf.m.userptr, buf.bytesused );
1539 if( !p_block ) return 0;
1542 if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
1544 msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
1553 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();
1558 /*****************************************************************************
1559 * ProcessVideoFrame: Helper function to take a buffer and copy it into
1561 *****************************************************************************/
1562 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t i_size )
1566 if( !p_frame ) return 0;
1569 if( !( p_block = block_New( p_demux, i_size ) ) )
1571 msg_Warn( p_demux, "Cannot get new block" );
1576 memcpy( p_block->p_buffer, p_frame, i_size );
1581 /*****************************************************************************
1582 * GrabAudio: Grab an audio frame
1583 *****************************************************************************/
1584 static block_t* GrabAudio( demux_t *p_demux )
1586 demux_sys_t *p_sys = p_demux->p_sys;
1587 struct audio_buf_info buf_info;
1588 int i_read = 0, i_correct;
1591 if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
1592 else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
1596 msg_Warn( p_demux, "cannot get block" );
1600 p_sys->p_block_audio = p_block;
1603 if( p_sys->i_audio_method & AUDIO_METHOD_ALSA )
1606 i_read = snd_pcm_readi( p_sys->p_alsa_pcm, p_block->p_buffer, p_sys->i_alsa_chunk_size );
1616 snd_pcm_prepare( p_sys->p_alsa_pcm );
1620 i_resume = snd_pcm_resume( p_sys->p_alsa_pcm );
1621 if( i_resume < 0 && i_resume != -EAGAIN ) snd_pcm_prepare( p_sys->p_alsa_pcm );
1624 msg_Err( p_demux, "Failed to read alsa frame (%s)", snd_strerror( i_read ) );
1630 /* convert from frames to bytes */
1631 i_read *= p_sys->i_alsa_frame_size;
1636 if( p_sys->i_audio_method & AUDIO_METHOD_OSS )
1639 i_read = read( p_sys->i_fd_audio, p_block->p_buffer,
1640 p_sys->i_audio_max_frame_size );
1643 if( i_read <= 0 ) return 0;
1645 p_block->i_buffer = i_read;
1646 p_sys->p_block_audio = 0;
1648 /* Correct the date because of kernel buffering */
1650 if( p_sys->i_audio_method & AUDIO_METHOD_OSS )
1653 if( ioctl( p_sys->i_fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
1655 i_correct += buf_info.bytes;
1659 else if( p_sys->i_audio_method & AUDIO_METHOD_ALSA )
1663 snd_pcm_sframes_t delay = 0;
1664 if( ( i_err = snd_pcm_delay( p_sys->p_alsa_pcm, &delay ) ) >= 0 )
1666 size_t i_correction_delta = delay * p_sys->i_alsa_frame_size;
1667 /* Test for overrun */
1668 if( i_correction_delta > p_sys->i_audio_max_frame_size )
1670 msg_Warn( p_demux, "ALSA read overrun (%d > %d)",
1671 i_correction_delta, p_sys->i_audio_max_frame_size );
1672 i_correction_delta = p_sys->i_audio_max_frame_size;
1673 snd_pcm_prepare( p_sys->p_alsa_pcm );
1675 i_correct += i_correction_delta;
1679 /* delay failed so reset */
1680 msg_Warn( p_demux, "ALSA snd_pcm_delay failed (%s)", snd_strerror( i_err ) );
1681 snd_pcm_prepare( p_sys->p_alsa_pcm );
1687 p_block->i_pts = p_block->i_dts =
1688 mdate() - I64C(1000000) * (mtime_t)i_correct /
1689 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
1694 /*****************************************************************************
1695 * Helper function to initalise video IO using the Read method
1696 *****************************************************************************/
1697 static int InitRead( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1699 demux_sys_t *p_sys = p_demux->p_sys;
1701 p_sys->p_buffers = calloc( 1, sizeof( *p_sys->p_buffers ) );
1702 if( !p_sys->p_buffers )
1704 msg_Err( p_demux, "Out of memory" );
1708 p_sys->p_buffers[0].length = i_buffer_size;
1709 p_sys->p_buffers[0].start = malloc( i_buffer_size );
1710 if( !p_sys->p_buffers[0].start )
1712 msg_Err( p_demux, "Out of memory" );
1719 return VLC_EGENERIC;
1723 /*****************************************************************************
1724 * Helper function to initalise video IO using the mmap method
1725 *****************************************************************************/
1726 static int InitMmap( demux_t *p_demux, int i_fd )
1728 demux_sys_t *p_sys = p_demux->p_sys;
1729 struct v4l2_requestbuffers req;
1731 memset( &req, 0, sizeof(req) );
1733 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1734 req.memory = V4L2_MEMORY_MMAP;
1736 if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1738 msg_Err( p_demux, "device does not support mmap i/o" );
1744 msg_Err( p_demux, "Insufficient buffer memory" );
1748 p_sys->p_buffers = calloc( req.count, sizeof( *p_sys->p_buffers ) );
1749 if( !p_sys->p_buffers )
1751 msg_Err( p_demux, "Out of memory" );
1755 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < req.count; ++p_sys->i_nbuffers )
1757 struct v4l2_buffer buf;
1759 memset( &buf, 0, sizeof(buf) );
1760 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1761 buf.memory = V4L2_MEMORY_MMAP;
1762 buf.index = p_sys->i_nbuffers;
1764 if( ioctl( i_fd, VIDIOC_QUERYBUF, &buf ) < 0 )
1766 msg_Err( p_demux, "VIDIOC_QUERYBUF" );
1770 p_sys->p_buffers[p_sys->i_nbuffers].length = buf.length;
1771 p_sys->p_buffers[p_sys->i_nbuffers].start =
1772 mmap( NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, buf.m.offset );
1774 if( p_sys->p_buffers[p_sys->i_nbuffers].start == MAP_FAILED )
1776 msg_Err( p_demux, "mmap failed (%m)" );
1784 return VLC_EGENERIC;
1788 /*****************************************************************************
1789 * Helper function to initalise video IO using the userbuf method
1790 *****************************************************************************/
1791 static int InitUserP( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1793 demux_sys_t *p_sys = p_demux->p_sys;
1794 struct v4l2_requestbuffers req;
1795 unsigned int i_page_size;
1797 i_page_size = getpagesize();
1798 i_buffer_size = ( i_buffer_size + i_page_size - 1 ) & ~( i_page_size - 1);
1800 memset( &req, 0, sizeof(req) );
1802 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1803 req.memory = V4L2_MEMORY_USERPTR;
1805 if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1807 msg_Err( p_demux, "device does not support user pointer i/o" );
1811 p_sys->p_buffers = calloc( 4, sizeof( *p_sys->p_buffers ) );
1812 if( !p_sys->p_buffers )
1814 msg_Err( p_demux, "Out of memory" );
1818 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < 4; ++p_sys->i_nbuffers )
1820 p_sys->p_buffers[p_sys->i_nbuffers].length = i_buffer_size;
1821 p_sys->p_buffers[p_sys->i_nbuffers].start =
1822 vlc_memalign( &p_sys->p_buffers[p_sys->i_nbuffers].orig_userp,
1823 /* boundary */ i_page_size, i_buffer_size );
1825 if( !p_sys->p_buffers[p_sys->i_nbuffers].start )
1827 msg_Err( p_demux, "out of memory" );
1835 return VLC_EGENERIC;
1839 /*****************************************************************************
1840 * IsPixelFormatSupported: returns true if the specified V4L2 pixel format is
1841 * in the array of supported formats returned by the driver
1842 *****************************************************************************/
1843 static vlc_bool_t IsPixelFormatSupported( demux_t *p_demux, unsigned int i_pixelformat )
1845 demux_sys_t *p_sys = p_demux->p_sys;
1847 for( int i_index = 0; i_index < p_sys->i_codec; i_index++ )
1849 if( p_sys->p_codecs[i_index].pixelformat == i_pixelformat )
1856 /*****************************************************************************
1857 * OpenVideoDev: open and set up the video device and probe for capabilities
1858 *****************************************************************************/
1859 static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, vlc_bool_t b_demux )
1862 struct v4l2_cropcap cropcap;
1863 struct v4l2_crop crop;
1864 struct v4l2_format fmt;
1866 enum v4l2_buf_type buf_type;
1867 char *psz_device = p_sys->psz_vdev;
1869 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1871 msg_Err( p_obj, "cannot open device (%m)" );
1875 /* Tune the tuner */
1876 if( p_sys->i_frequency >= 0 )
1878 if( p_sys->i_cur_tuner < 0 || p_sys->i_cur_tuner >= p_sys->i_tuner )
1880 msg_Err( p_obj, "invalid tuner %d.", p_sys->i_cur_tuner );
1883 struct v4l2_frequency frequency;
1884 memset( &frequency, 0, sizeof( frequency ) );
1885 frequency.tuner = p_sys->i_cur_tuner;
1886 frequency.type = p_sys->p_tuners[p_sys->i_cur_tuner].type;
1887 frequency.frequency = p_sys->i_frequency / 62.5;
1888 if( ioctl( i_fd, VIDIOC_S_FREQUENCY, &frequency ) < 0 )
1890 msg_Err( p_obj, "cannot set tuner frequency (%m)" );
1893 msg_Dbg( p_obj, "Tuner frequency set" );
1896 /* Set the tuner's audio mode */
1897 if( p_sys->i_audio_mode >= 0 )
1899 if( p_sys->i_cur_tuner < 0 || p_sys->i_cur_tuner >= p_sys->i_tuner )
1901 msg_Err( p_obj, "invalid tuner %d.", p_sys->i_cur_tuner );
1904 struct v4l2_tuner tuner;
1905 memset( &tuner, 0, sizeof( tuner ) );
1906 tuner.index = p_sys->i_cur_tuner;
1907 tuner.audmode = p_sys->i_audio_mode;
1908 if( ioctl( i_fd, VIDIOC_S_TUNER, &tuner ) < 0 )
1910 msg_Err( p_obj, "cannot set tuner audio mode (%m)" );
1913 msg_Dbg( p_obj, "Tuner audio mode set" );
1916 /* Select standard */
1918 if( p_sys->i_selected_standard_id != V4L2_STD_UNKNOWN )
1920 if( ioctl( i_fd, VIDIOC_S_STD, &p_sys->i_selected_standard_id ) < 0 )
1922 msg_Err( p_obj, "cannot set standard (%m)" );
1925 msg_Dbg( p_obj, "Set standard" );
1930 if( p_sys->i_selected_input >= p_sys->i_input )
1932 msg_Warn( p_obj, "invalid input. Using the default one" );
1933 p_sys->i_selected_input = 0;
1936 if( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
1938 msg_Err( p_obj, "cannot set input (%m)" );
1942 /* Set audio input */
1944 if( p_sys->i_audio > 0 )
1946 if( p_sys->i_selected_audio_input < 0
1947 || p_sys->i_selected_audio_input >= p_sys->i_audio )
1949 msg_Warn( p_obj, "invalid audio input. Using the default one" );
1950 p_sys->i_selected_audio_input = 0;
1953 if( ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_selected_audio_input] ) < 0 )
1955 msg_Err( p_obj, "cannot set audio input (%m)" );
1958 msg_Dbg( p_obj, "Audio input set to %d", p_sys->i_selected_audio_input );
1962 /* TODO: Move the resolution stuff up here */
1963 /* if MPEG encoder card, no need to do anything else after this */
1964 ControlList( p_obj, i_fd,
1965 var_GetBool( p_obj, "v4l2-controls-reset" ), b_demux );
1966 SetAvailControlsByString( p_obj, p_sys, i_fd );
1967 if( VLC_FALSE == b_demux)
1972 demux_t *p_demux = (demux_t *) p_obj;
1974 /* Verify device support for the various IO methods */
1977 case IO_METHOD_READ:
1978 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE) )
1980 msg_Err( p_demux, "device does not support read i/o" );
1985 case IO_METHOD_MMAP:
1986 case IO_METHOD_USERPTR:
1987 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING) )
1989 msg_Err( p_demux, "device does not support streaming i/o" );
1995 msg_Err( p_demux, "io method not supported" );
1999 /* Reset Cropping */
2000 memset( &cropcap, 0, sizeof(cropcap) );
2001 cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2002 if( ioctl( i_fd, VIDIOC_CROPCAP, &cropcap ) >= 0 )
2004 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2005 crop.c = cropcap.defrect; /* reset to default */
2006 if( ioctl( i_fd, VIDIOC_S_CROP, &crop ) < 0 )
2011 /* Cropping not supported. */
2014 /* Errors ignored. */
2020 /* Try and find default resolution if not specified */
2021 memset( &fmt, 0, sizeof(fmt) );
2022 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2024 if( p_sys->i_width <= 0 || p_sys->i_height <= 0 )
2026 if( ioctl( i_fd, VIDIOC_G_FMT, &fmt ) < 0 )
2028 msg_Err( p_demux, "Cannot get default width and height." );
2032 p_sys->i_width = fmt.fmt.pix.width;
2033 p_sys->i_height = fmt.fmt.pix.height;
2035 if( fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE )
2037 p_sys->i_height = p_sys->i_height * 2;
2042 msg_Dbg( p_demux, "trying specified size %dx%d", p_sys->i_width, p_sys->i_height );
2045 fmt.fmt.pix.width = p_sys->i_width;
2046 fmt.fmt.pix.height = p_sys->i_height;
2047 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2049 /* Test and set Chroma */
2050 fmt.fmt.pix.pixelformat = 0;
2051 if( p_sys->psz_requested_chroma && strlen( p_sys->psz_requested_chroma ) > 0 )
2053 /* User specified chroma */
2054 if( strlen( p_sys->psz_requested_chroma ) >= 4 )
2056 int i_requested_fourcc = VLC_FOURCC(
2057 p_sys->psz_requested_chroma[0], p_sys->psz_requested_chroma[1],
2058 p_sys->psz_requested_chroma[2], p_sys->psz_requested_chroma[3] );
2059 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
2061 if( v4l2chroma_to_fourcc[i].i_fourcc == i_requested_fourcc )
2063 fmt.fmt.pix.pixelformat = v4l2chroma_to_fourcc[i].i_v4l2;
2068 /* Try and set user chroma */
2069 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ( fmt.fmt.pix.pixelformat && ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 ) )
2071 msg_Warn( p_demux, "Driver is unable to use specified chroma %s. Trying defaults.", p_sys->psz_requested_chroma );
2072 fmt.fmt.pix.pixelformat = 0;
2076 /* If no user specified chroma, find best */
2077 /* This also decides if MPEG encoder card or not */
2078 if( !fmt.fmt.pix.pixelformat )
2081 for( i = 0; i < ARRAY_SIZE( p_chroma_fallbacks ); i++ )
2083 fmt.fmt.pix.pixelformat = p_chroma_fallbacks[i];
2084 if( IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat )
2085 && ioctl( i_fd, VIDIOC_S_FMT, &fmt ) >= 0 )
2088 if( i == ARRAY_SIZE( p_chroma_fallbacks ) )
2090 msg_Warn( p_demux, "Could not select any of the default chromas; attempting to open as MPEG encoder card (access2)" );
2095 /* Reassign width, height and chroma incase driver override */
2096 p_sys->i_width = fmt.fmt.pix.width;
2097 p_sys->i_height = fmt.fmt.pix.height;
2099 /* Look up final fourcc */
2100 p_sys->i_fourcc = 0;
2101 for( int i = 0; v4l2chroma_to_fourcc[i].i_fourcc != 0; i++ )
2103 if( v4l2chroma_to_fourcc[i].i_v4l2 == fmt.fmt.pix.pixelformat )
2105 p_sys->i_fourcc = v4l2chroma_to_fourcc[i].i_fourcc;
2110 /* Buggy driver paranoia */
2111 i_min = fmt.fmt.pix.width * 2;
2112 if( fmt.fmt.pix.bytesperline < i_min )
2113 fmt.fmt.pix.bytesperline = i_min;
2114 i_min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
2115 if( fmt.fmt.pix.sizeimage < i_min )
2116 fmt.fmt.pix.sizeimage = i_min;
2118 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
2119 /* This is new in Linux 2.6.19 */
2120 /* List supported frame rates */
2121 struct v4l2_frmivalenum frmival;
2123 frmival.pixel_format = fmt.fmt.pix.pixelformat;
2124 frmival.width = p_sys->i_width;
2125 frmival.height = p_sys->i_height;
2126 if( ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 )
2129 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
2130 vlc_fourcc_to_char( p_sys->i_fourcc, &psz_fourcc );
2131 msg_Dbg( p_demux, "supported frame intervals for %4s, %dx%d:",
2132 psz_fourcc, frmival.width, frmival.height );
2133 switch( frmival.type )
2135 case V4L2_FRMIVAL_TYPE_DISCRETE:
2138 msg_Dbg( p_demux, " supported frame interval: %d/%d",
2139 frmival.discrete.numerator,
2140 frmival.discrete.denominator );
2142 } while( ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 );
2144 case V4L2_FRMIVAL_TYPE_STEPWISE:
2145 msg_Dbg( p_demux, " supported frame intervals: %d/%d to "
2146 "%d/%d using %d/%d increments",
2147 frmival.stepwise.min.numerator,
2148 frmival.stepwise.min.denominator,
2149 frmival.stepwise.max.numerator,
2150 frmival.stepwise.max.denominator,
2151 frmival.stepwise.step.numerator,
2152 frmival.stepwise.step.denominator );
2154 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
2155 msg_Dbg( p_demux, " supported frame intervals: %d/%d to %d/%d",
2156 frmival.stepwise.min.numerator,
2157 frmival.stepwise.min.denominator,
2158 frmival.stepwise.max.numerator,
2159 frmival.stepwise.max.denominator );
2165 /* Init IO method */
2168 case IO_METHOD_READ:
2169 if( InitRead( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
2172 case IO_METHOD_MMAP:
2173 if( InitMmap( p_demux, i_fd ) != VLC_SUCCESS ) goto open_failed;
2176 case IO_METHOD_USERPTR:
2177 if( InitUserP( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
2184 es_format_Init( &es_fmt, VIDEO_ES, p_sys->i_fourcc );
2185 es_fmt.video.i_width = p_sys->i_width;
2186 es_fmt.video.i_height = p_sys->i_height;
2187 es_fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
2189 /* Setup rgb mask for RGB formats */
2190 if( p_sys->i_fourcc == VLC_FOURCC( 'R','V','2','4' ) )
2192 /* This is in BGR format */
2193 es_fmt.video.i_bmask = 0x00ff0000;
2194 es_fmt.video.i_gmask = 0x0000ff00;
2195 es_fmt.video.i_rmask = 0x000000ff;
2198 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
2199 (char*)&es_fmt.i_codec, es_fmt.video.i_width, es_fmt.video.i_height );
2200 p_sys->p_es_video = es_out_Add( p_demux->out, &es_fmt );
2206 case IO_METHOD_READ:
2210 case IO_METHOD_MMAP:
2211 for (unsigned int i = 0; i < p_sys->i_nbuffers; ++i)
2213 struct v4l2_buffer buf;
2215 memset( &buf, 0, sizeof(buf) );
2216 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2217 buf.memory = V4L2_MEMORY_MMAP;
2220 if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
2222 msg_Err( p_demux, "VIDIOC_QBUF failed" );
2227 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2228 if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
2230 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
2236 case IO_METHOD_USERPTR:
2237 for( unsigned int i = 0; i < p_sys->i_nbuffers; ++i )
2239 struct v4l2_buffer buf;
2241 memset( &buf, 0, sizeof(buf) );
2242 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2243 buf.memory = V4L2_MEMORY_USERPTR;
2245 buf.m.userptr = (unsigned long)p_sys->p_buffers[i].start;
2246 buf.length = p_sys->p_buffers[i].length;
2248 if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
2250 msg_Err( p_demux, "VIDIOC_QBUF failed" );
2255 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2256 if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
2258 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
2266 if( p_sys->f_fps >= 0.1 )
2268 msg_Dbg( p_demux, "User set fps=%f", p_sys->f_fps );
2274 if( i_fd >= 0 ) close( i_fd );
2280 /*****************************************************************************
2281 * ResolveALSADeviceName: Change any . to : in the ALSA device name
2282 *****************************************************************************/
2283 static char *ResolveALSADeviceName( const char *psz_device )
2285 char* psz_alsa_name = strdup( psz_device );
2286 for( unsigned int i = 0; i < strlen( psz_device ); i++ )
2288 if( psz_alsa_name[i] == '.' ) psz_alsa_name[i] = ':';
2290 return psz_alsa_name;
2294 /*****************************************************************************
2295 * OpenAudioDev: open and set up the audio device and probe for capabilities
2296 *****************************************************************************/
2298 static int OpenAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys,
2299 vlc_bool_t b_demux )
2301 char *psz_device = p_sys->psz_adev;
2303 p_sys->p_alsa_pcm = NULL;
2304 char* psz_alsa_device_name = NULL;
2305 snd_pcm_hw_params_t *p_hw_params = NULL;
2306 snd_pcm_uframes_t buffer_size;
2307 snd_pcm_uframes_t chunk_size;
2311 psz_alsa_device_name =
2312 ResolveALSADeviceName( psz_device?: ALSA_DEFAULT );
2314 if( ( i_err = snd_pcm_open( &p_sys->p_alsa_pcm, psz_alsa_device_name,
2315 SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0)
2317 msg_Err( p_this, "Cannot open ALSA audio device %s (%s)",
2318 psz_alsa_device_name, snd_strerror( i_err ) );
2322 if( ( i_err = snd_pcm_nonblock( p_sys->p_alsa_pcm, 1 ) ) < 0)
2324 msg_Err( p_this, "Cannot set ALSA nonblock (%s)",
2325 snd_strerror( i_err ) );
2329 /* Begin setting hardware parameters */
2331 if( ( i_err = snd_pcm_hw_params_malloc( &p_hw_params ) ) < 0 )
2334 "ALSA: cannot allocate hardware parameter structure (%s)",
2335 snd_strerror( i_err ) );
2339 if( ( i_err = snd_pcm_hw_params_any( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
2342 "ALSA: cannot initialize hardware parameter structure (%s)",
2343 snd_strerror( i_err ) );
2347 /* Set Interleaved access */
2348 if( ( i_err = snd_pcm_hw_params_set_access( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 )
2350 msg_Err( p_this, "ALSA: cannot set access type (%s)",
2351 snd_strerror( i_err ) );
2355 /* Set 16 bit little endian */
2356 if( ( i_err = snd_pcm_hw_params_set_format( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_FORMAT_S16_LE ) ) < 0 )
2358 msg_Err( p_this, "ALSA: cannot set sample format (%s)",
2359 snd_strerror( i_err ) );
2363 /* Set sample rate */
2364 #ifdef HAVE_ALSA_NEW_API
2365 i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, &p_sys->i_sample_rate, NULL );
2367 i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, p_sys->i_sample_rate, NULL );
2371 msg_Err( p_this, "ALSA: cannot set sample rate (%s)",
2372 snd_strerror( i_err ) );
2377 unsigned int channels = p_sys->b_stereo ? 2 : 1;
2378 if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
2380 channels = ( channels==1 ) ? 2 : 1;
2381 msg_Warn( p_this, "ALSA: cannot set channel count (%s). "
2382 "Trying with channels=%d",
2383 snd_strerror( i_err ),
2385 if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
2387 msg_Err( p_this, "ALSA: cannot set channel count (%s)",
2388 snd_strerror( i_err ) );
2391 p_sys->b_stereo = ( channels == 2 );
2394 /* Set metrics for buffer calculations later */
2395 unsigned int buffer_time;
2396 if( ( i_err = snd_pcm_hw_params_get_buffer_time_max(p_hw_params, &buffer_time, 0) ) < 0 )
2398 msg_Err( p_this, "ALSA: cannot get buffer time max (%s)",
2399 snd_strerror( i_err ) );
2402 if (buffer_time > 500000) buffer_time = 500000;
2404 /* Set period time */
2405 unsigned int period_time = buffer_time / 4;
2406 #ifdef HAVE_ALSA_NEW_API
2407 i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, &period_time, 0 );
2409 i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, period_time, 0 );
2413 msg_Err( p_this, "ALSA: cannot set period time (%s)",
2414 snd_strerror( i_err ) );
2418 /* Set buffer time */
2419 #ifdef HAVE_ALSA_NEW_API
2420 i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, &buffer_time, 0 );
2422 i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, buffer_time, 0 );
2426 msg_Err( p_this, "ALSA: cannot set buffer time (%s)",
2427 snd_strerror( i_err ) );
2431 /* Apply new hardware parameters */
2432 if( ( i_err = snd_pcm_hw_params( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
2434 msg_Err( p_this, "ALSA: cannot set hw parameters (%s)",
2435 snd_strerror( i_err ) );
2439 /* Get various buffer metrics */
2440 snd_pcm_hw_params_get_period_size( p_hw_params, &chunk_size, 0 );
2441 snd_pcm_hw_params_get_buffer_size( p_hw_params, &buffer_size );
2442 if( chunk_size == buffer_size )
2445 "ALSA: period cannot equal buffer size (%lu == %lu)",
2446 chunk_size, buffer_size);
2450 int bits_per_sample = snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE);
2451 int bits_per_frame = bits_per_sample * channels;
2453 p_sys->i_alsa_chunk_size = chunk_size;
2454 p_sys->i_alsa_frame_size = bits_per_frame / 8;
2455 p_sys->i_audio_max_frame_size = chunk_size * bits_per_frame / 8;
2457 snd_pcm_hw_params_free( p_hw_params );
2461 if( ( i_err = snd_pcm_prepare( p_sys->p_alsa_pcm ) ) < 0 )
2464 "ALSA: cannot prepare audio interface for use (%s)",
2465 snd_strerror( i_err ) );
2469 /* Return a fake handle so other tests work */
2472 free( psz_alsa_device_name );
2474 if( !p_sys->psz_adev )
2475 p_sys->psz_adev = strdup( ALSA_DEFAULT );
2480 if( i_fd >= 0 ) close( i_fd );
2482 if( p_hw_params ) snd_pcm_hw_params_free( p_hw_params );
2483 if( p_sys->p_alsa_pcm ) snd_pcm_close( p_sys->p_alsa_pcm );
2484 free( psz_alsa_device_name );
2491 static int OpenAudioDevOss( vlc_object_t *p_this, demux_sys_t *p_sys,
2492 vlc_bool_t b_demux )
2494 char *psz_device = p_sys->psz_adev;
2498 if( !psz_device ) psz_device = strdup( OSS_DEFAULT ); /* FIXME leak */
2500 if( (i_fd = open( psz_device, O_RDONLY | O_NONBLOCK )) < 0 )
2502 msg_Err( p_this, "cannot open OSS audio device (%m)" );
2506 i_format = AFMT_S16_LE;
2507 if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
2508 || i_format != AFMT_S16_LE )
2511 "cannot set audio format (16b little endian) (%m)" );
2515 if( ioctl( i_fd, SNDCTL_DSP_STEREO,
2516 &p_sys->b_stereo ) < 0 )
2518 msg_Err( p_this, "cannot set audio channels count (%m)" );
2522 if( ioctl( i_fd, SNDCTL_DSP_SPEED,
2523 &p_sys->i_sample_rate ) < 0 )
2525 msg_Err( p_this, "cannot set audio sample rate (%m)" );
2529 p_sys->i_audio_max_frame_size = 6 * 1024;
2531 if( !p_sys->psz_adev )
2532 p_sys->psz_adev = strdup( OSS_DEFAULT );
2537 if( i_fd >= 0 ) close( i_fd );
2542 static int OpenAudioDev( vlc_object_t *p_this, demux_sys_t *p_sys,
2543 vlc_bool_t b_demux )
2549 if( ( p_sys->i_audio_method & AUDIO_METHOD_ALSA ) && i_fd < 0 )
2550 i_fd = OpenAudioDevAlsa( p_this, p_sys, b_demux );
2553 if( ( p_sys->i_audio_method & AUDIO_METHOD_OSS ) && i_fd < 0 )
2554 i_fd = OpenAudioDevOss( p_this, p_sys, b_demux );
2559 psz_device = p_sys->psz_adev;
2561 msg_Dbg( p_this, "opened adev=`%s' %s %dHz",
2562 psz_device, p_sys->b_stereo ? "stereo" : "mono",
2563 p_sys->i_sample_rate );
2566 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
2568 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
2569 fmt.audio.i_rate = p_sys->i_sample_rate;
2570 fmt.audio.i_bitspersample = 16;
2571 fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
2572 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
2574 msg_Dbg( p_this, "new audio es %d channels %dHz",
2575 fmt.audio.i_channels, fmt.audio.i_rate );
2579 demux_t *p_demux = (demux_t *)p_this;
2580 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
2586 /*****************************************************************************
2587 * ProbeVideoDev: probe video for capabilities
2588 *****************************************************************************/
2589 static vlc_bool_t ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
2597 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
2599 msg_Err( p_obj, "cannot open video device (%m)" );
2603 /* Get device capabilites */
2605 if( ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
2607 msg_Err( p_obj, "cannot get video capabilities (%m)" );
2611 msg_Dbg( p_obj, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
2612 p_sys->dev_cap.card,
2613 p_sys->dev_cap.driver,
2614 (p_sys->dev_cap.version >> 16) & 0xFF,
2615 (p_sys->dev_cap.version >> 8) & 0xFF,
2616 p_sys->dev_cap.version & 0xFF,
2617 p_sys->dev_cap.bus_info );
2619 msg_Dbg( p_obj, "the device has the capabilities: (%c) Video Capure, "
2622 ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE ? 'X':' '),
2623 ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO ? 'X':' '),
2624 ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER ? 'X':' ') );
2626 msg_Dbg( p_obj, "supported I/O methods are: (%c) Read/Write, "
2628 "(%c) Asynchronous",
2629 ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
2630 ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
2631 ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
2633 /* Now, enumerate all the video inputs. This is useless at the moment
2634 since we have no way to present that info to the user except with
2637 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2639 struct v4l2_input t_input;
2641 while( ioctl( i_fd, VIDIOC_ENUMINPUT, &t_input ) >= 0 )
2644 t_input.index = p_sys->i_input;
2647 p_sys->p_inputs = calloc( 1, p_sys->i_input * sizeof( struct v4l2_input ) );
2648 if( !p_sys->p_inputs ) goto open_failed;
2650 for( i_index = 0; i_index < p_sys->i_input; i_index++ )
2652 p_sys->p_inputs[i_index].index = i_index;
2654 if( ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
2656 msg_Err( p_obj, "cannot get video input characteristics (%m)" );
2659 msg_Dbg( p_obj, "video input %i (%s) has type: %s %c",
2661 p_sys->p_inputs[i_index].name,
2662 p_sys->p_inputs[i_index].type
2663 == V4L2_INPUT_TYPE_TUNER ?
2665 "External analog input",
2666 i_index == p_sys->i_selected_input ? '*' : ' ' );
2670 /* Probe video standards */
2671 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2673 struct v4l2_standard t_standards;
2674 t_standards.index = 0;
2675 while( ioctl( i_fd, VIDIOC_ENUMSTD, &t_standards ) >=0 )
2677 p_sys->i_standard++;
2678 t_standards.index = p_sys->i_standard;
2681 p_sys->p_standards = calloc( 1, p_sys->i_standard * sizeof( struct v4l2_standard ) );
2682 if( !p_sys->p_standards ) goto open_failed;
2684 for( i_standard = 0; i_standard < p_sys->i_standard; i_standard++ )
2686 p_sys->p_standards[i_standard].index = i_standard;
2688 if( ioctl( i_fd, VIDIOC_ENUMSTD, &p_sys->p_standards[i_standard] ) )
2690 msg_Err( p_obj, "cannot get video input standards (%m)" );
2693 msg_Dbg( p_obj, "video standard %i is: %s %c",
2695 p_sys->p_standards[i_standard].name,
2696 (unsigned)i_standard == p_sys->i_selected_standard_id ? '*' : ' ' );
2700 /* initialize the structures for the ioctls */
2701 for( i_index = 0; i_index < 32; i_index++ )
2703 p_sys->p_audios[i_index].index = i_index;
2706 /* Probe audio inputs */
2707 if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
2709 while( p_sys->i_audio < 32 &&
2710 ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
2712 if( ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
2714 msg_Err( p_obj, "cannot get audio input characteristics (%m)" );
2718 msg_Dbg( p_obj, "audio input %i (%s) is %s %s %c",
2720 p_sys->p_audios[p_sys->i_audio].name,
2721 p_sys->p_audios[p_sys->i_audio].capability &
2722 V4L2_AUDCAP_STEREO ?
2724 p_sys->p_audios[p_sys->i_audio].capability &
2726 "(Automatic Volume Level supported)" : "",
2727 p_sys->i_audio == p_sys->i_selected_audio_input ? '*' : ' ' );
2733 /* List tuner caps */
2734 if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
2736 struct v4l2_tuner tuner;
2737 memset( &tuner, 0, sizeof(tuner) );
2738 while( ioctl( i_fd, VIDIOC_G_TUNER, &tuner ) >= 0 )
2741 memset( &tuner, 0, sizeof(tuner) );
2742 tuner.index = p_sys->i_tuner;
2745 p_sys->p_tuners = calloc( 1, p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
2746 if( !p_sys->p_tuners ) goto open_failed;
2748 for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
2750 p_sys->p_tuners[i_index].index = i_index;
2752 if( ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
2754 msg_Err( p_obj, "cannot get tuner characteristics (%m)" );
2757 msg_Dbg( p_obj, "tuner %i (%s) has type: %s, "
2758 "frequency range: %.1f %s -> %.1f %s",
2760 p_sys->p_tuners[i_index].name,
2761 p_sys->p_tuners[i_index].type
2762 == V4L2_TUNER_RADIO ?
2763 "Radio" : "Analog TV",
2764 p_sys->p_tuners[i_index].rangelow * 62.5,
2765 p_sys->p_tuners[i_index].capability &
2766 V4L2_TUNER_CAP_LOW ?
2768 p_sys->p_tuners[i_index].rangehigh * 62.5,
2769 p_sys->p_tuners[i_index].capability &
2770 V4L2_TUNER_CAP_LOW ?
2773 struct v4l2_frequency frequency;
2774 memset( &frequency, 0, sizeof( frequency ) );
2775 if( ioctl( i_fd, VIDIOC_G_FREQUENCY, &frequency ) < 0 )
2777 msg_Err( p_obj, "cannot get tuner frequency (%m)" );
2780 msg_Dbg( p_obj, "tuner %i (%s) frequency: %.1f %s",
2782 p_sys->p_tuners[i_index].name,
2783 frequency.frequency * 62.5,
2784 p_sys->p_tuners[i_index].capability &
2785 V4L2_TUNER_CAP_LOW ?
2790 /* Probe for available chromas */
2791 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2793 struct v4l2_fmtdesc codec;
2796 memset( &codec, 0, sizeof(codec) );
2797 codec.index = i_index;
2798 codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2800 while( ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
2803 codec.index = i_index;
2806 p_sys->i_codec = i_index;
2808 p_sys->p_codecs = calloc( 1, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
2810 for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
2812 p_sys->p_codecs[i_index].index = i_index;
2813 p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2815 if( ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
2817 msg_Err( p_obj, "cannot get codec description (%m)" );
2821 /* only print if vlc supports the format */
2822 char psz_fourcc_v4l2[5];
2823 memset( &psz_fourcc_v4l2, 0, sizeof( psz_fourcc_v4l2 ) );
2824 vlc_fourcc_to_char( p_sys->p_codecs[i_index].pixelformat,
2826 vlc_bool_t b_codec_supported = VLC_FALSE;
2827 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
2829 if( v4l2chroma_to_fourcc[i].i_v4l2 == p_sys->p_codecs[i_index].pixelformat )
2831 b_codec_supported = VLC_TRUE;
2834 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
2835 vlc_fourcc_to_char( v4l2chroma_to_fourcc[i].i_fourcc,
2837 msg_Dbg( p_obj, "device supports chroma %4s [%s, %s]",
2839 p_sys->p_codecs[i_index].description,
2842 #ifdef VIDIOC_ENUM_FRAMESIZES
2843 /* This is new in Linux 2.6.19 */
2844 /* List valid frame sizes for this format */
2845 struct v4l2_frmsizeenum frmsize;
2847 frmsize.pixel_format = p_sys->p_codecs[i_index].pixelformat;
2848 if( ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) < 0 )
2850 /* Not all devices support this ioctl */
2851 msg_Warn( p_obj, "Unable to query for frame sizes" );
2855 switch( frmsize.type )
2857 case V4L2_FRMSIZE_TYPE_DISCRETE:
2861 " device supports size %dx%d",
2862 frmsize.discrete.width, frmsize.discrete.height );
2864 } while( ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 );
2866 case V4L2_FRMSIZE_TYPE_STEPWISE:
2868 " device supports sizes %dx%d to %dx%d using %dx%d increments",
2869 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2870 frmsize.stepwise.max_width, frmsize.stepwise.max_height,
2871 frmsize.stepwise.step_width, frmsize.stepwise.step_height );
2873 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
2875 " device supports all sizes %dx%d to %dx%d",
2876 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2877 frmsize.stepwise.max_width, frmsize.stepwise.max_height );
2884 if( !b_codec_supported )
2887 "device codec %4s (%s) not supported as access_demux",
2889 p_sys->p_codecs[i_index].description );
2896 if( i_fd >= 0 ) close( i_fd );
2901 if( i_fd >= 0 ) close( i_fd );
2906 /*****************************************************************************
2907 * ProbeAudioDev: probe audio for capabilities
2908 *****************************************************************************/
2910 static vlc_bool_t ProbeAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys,
2914 snd_pcm_t *p_alsa_pcm;
2915 char* psz_alsa_device_name = ResolveALSADeviceName( psz_device ?: ALSA_DEFAULT );
2917 if( ( i_err = snd_pcm_open( &p_alsa_pcm, psz_alsa_device_name, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0 )
2919 msg_Err( p_this, "cannot open device %s for ALSA audio (%s)", psz_alsa_device_name, snd_strerror( i_err ) );
2920 free( psz_alsa_device_name );
2924 snd_pcm_close( p_alsa_pcm );
2925 free( psz_alsa_device_name );
2931 static vlc_bool_t ProbeAudioDevOss( vlc_object_t *p_this, demux_sys_t *p_sys,
2936 if( !psz_device ) psz_device = strdup( OSS_DEFAULT ); /* FIXME leak */
2938 if( ( i_fd = open( psz_device, O_RDONLY | O_NONBLOCK ) ) < 0 )
2940 msg_Err( p_this, "cannot open device %s for OSS audio (%m)", psz_device );
2944 /* this will fail if the device is video */
2945 if( ioctl( i_fd, SNDCTL_DSP_GETCAPS, &i_caps ) < 0 )
2947 msg_Err( p_this, "cannot get audio caps (%m)" );
2951 if( i_fd >= 0 ) close( i_fd );
2956 if( i_fd >= 0 ) close( i_fd );
2960 static vlc_bool_t ProbeAudioDev( vlc_object_t *p_this, demux_sys_t *p_sys,
2964 if( ( p_sys->i_audio_method & AUDIO_METHOD_ALSA )
2965 && ProbeAudioDevAlsa( p_this, p_sys, psz_device ) )
2967 p_sys->i_audio_method = AUDIO_METHOD_ALSA;
2972 if( ( p_sys->i_audio_method & AUDIO_METHOD_OSS )
2973 && ProbeAudioDevOss( p_this, p_sys, psz_device ) )
2975 p_sys->i_audio_method = AUDIO_METHOD_OSS;
2979 p_sys->i_audio_method = 0;
2983 static void name2var( unsigned char *name )
2985 for( ; *name; name++ )
2986 *name = (*name == ' ') ? '_' : tolower( *name );
2989 /*****************************************************************************
2990 * Print a user-class v4l2 control's details, create the relevant variable,
2991 * change the value if needed.
2992 *****************************************************************************/
2993 static void ControlListPrint( vlc_object_t *p_obj, int i_fd,
2994 struct v4l2_queryctrl queryctrl,
2995 vlc_bool_t b_reset, vlc_bool_t b_demux )
2997 struct v4l2_querymenu querymenu;
3004 vlc_value_t val, val2;
3006 if( queryctrl.flags & V4L2_CTRL_FLAG_GRABBED )
3007 msg_Dbg( p_obj, " control is busy" );
3008 if( queryctrl.flags & V4L2_CTRL_FLAG_READ_ONLY )
3009 msg_Dbg( p_obj, " control is read-only" );
3011 for( i = 0; controls[i].psz_name != NULL; i++ )
3012 if( controls[i].i_cid == queryctrl.id ) break;
3014 if( controls[i].psz_name )
3016 psz_name = strdup( controls[i].psz_name );
3017 char psz_cfg_name[40];
3018 sprintf( psz_cfg_name, CFG_PREFIX "%s", psz_name );
3019 i_val = var_CreateGetInteger( p_obj, psz_cfg_name );
3020 var_Destroy( p_obj, psz_cfg_name );
3024 psz_name = strdup( (const char *)queryctrl.name );
3025 name2var( (unsigned char *)psz_name );
3029 switch( queryctrl.type )
3031 case V4L2_CTRL_TYPE_INTEGER:
3032 msg_Dbg( p_obj, " integer control" );
3034 " valid values: %d to %d by steps of %d",
3035 queryctrl.minimum, queryctrl.maximum,
3038 var_Create( p_obj, psz_name,
3039 VLC_VAR_INTEGER | VLC_VAR_HASMIN | VLC_VAR_HASMAX
3040 | VLC_VAR_HASSTEP | VLC_VAR_ISCOMMAND );
3041 val.i_int = queryctrl.minimum;
3042 var_Change( p_obj, psz_name, VLC_VAR_SETMIN, &val, NULL );
3043 val.i_int = queryctrl.maximum;
3044 var_Change( p_obj, psz_name, VLC_VAR_SETMAX, &val, NULL );
3045 val.i_int = queryctrl.step;
3046 var_Change( p_obj, psz_name, VLC_VAR_SETSTEP, &val, NULL );
3048 case V4L2_CTRL_TYPE_BOOLEAN:
3049 msg_Dbg( p_obj, " boolean control" );
3050 var_Create( p_obj, psz_name,
3051 VLC_VAR_BOOL | VLC_VAR_ISCOMMAND );
3053 case V4L2_CTRL_TYPE_MENU:
3054 msg_Dbg( p_obj, " menu control" );
3055 var_Create( p_obj, psz_name,
3056 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE
3057 | VLC_VAR_ISCOMMAND );
3058 memset( &querymenu, 0, sizeof( querymenu ) );
3059 for( i_mid = queryctrl.minimum;
3060 i_mid <= (unsigned)queryctrl.maximum;
3063 querymenu.index = i_mid;
3064 querymenu.id = queryctrl.id;
3065 if( ioctl( i_fd, VIDIOC_QUERYMENU, &querymenu ) >= 0 )
3067 msg_Dbg( p_obj, " %d: %s",
3068 querymenu.index, querymenu.name );
3069 val.i_int = querymenu.index;
3070 val2.psz_string = (char *)querymenu.name;
3071 var_Change( p_obj, psz_name,
3072 VLC_VAR_ADDCHOICE, &val, &val2 );
3076 case V4L2_CTRL_TYPE_BUTTON:
3077 msg_Dbg( p_obj, " button control" );
3078 var_Create( p_obj, psz_name,
3079 VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
3081 case V4L2_CTRL_TYPE_CTRL_CLASS:
3082 msg_Dbg( p_obj, " control class" );
3083 var_Create( p_obj, psz_name, VLC_VAR_VOID );
3086 msg_Dbg( p_obj, " unknown control type (FIXME)" );
3091 switch( queryctrl.type )
3093 case V4L2_CTRL_TYPE_INTEGER:
3094 case V4L2_CTRL_TYPE_BOOLEAN:
3095 case V4L2_CTRL_TYPE_MENU:
3097 struct v4l2_control control;
3098 msg_Dbg( p_obj, " default value: %d",
3099 queryctrl.default_value );
3100 memset( &control, 0, sizeof( control ) );
3101 control.id = queryctrl.id;
3102 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
3104 msg_Dbg( p_obj, " current value: %d", control.value );
3108 i_val = control.value;
3109 if( b_reset && queryctrl.default_value != control.value )
3111 msg_Dbg( p_obj, " reset value to default" );
3112 Control( p_obj, i_fd, psz_name,
3113 queryctrl.id, queryctrl.default_value );
3118 Control( p_obj, i_fd, psz_name,
3119 queryctrl.id, i_val );
3127 val.psz_string = (char *)queryctrl.name;
3128 var_Change( p_obj, psz_name, VLC_VAR_SETTEXT, &val, NULL );
3129 val.i_int = queryctrl.id;
3130 val2.psz_string = (char *)psz_name;
3131 var_Change( p_obj, "allcontrols", VLC_VAR_ADDCHOICE, &val, &val2 );
3132 /* bad things happen changing MPEG mid-stream
3133 * so don't add to Ext Settings GUI */
3134 if( V4L2_CTRL_ID2CLASS( queryctrl.id ) != V4L2_CTRL_CLASS_MPEG )
3135 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
3137 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
3140 var_SetBool( p_obj, psz_name, i_val );
3142 case VLC_VAR_INTEGER:
3143 var_SetInteger( p_obj, psz_name, i_val );
3148 msg_Warn( p_obj, "FIXME: %s %s %d", __FILE__, __func__,
3154 var_AddCallback( p_obj, psz_name,
3155 DemuxControlCallback, (void*)queryctrl.id );
3157 var_AddCallback( p_obj, psz_name,
3158 AccessControlCallback, (void*)queryctrl.id );
3163 /*****************************************************************************
3164 * List all user-class v4l2 controls, set them to the user specified
3165 * value and create the relevant variables to enable runtime changes
3166 *****************************************************************************/
3167 static int ControlList( vlc_object_t *p_obj, int i_fd,
3168 vlc_bool_t b_reset, vlc_bool_t b_demux )
3170 struct v4l2_queryctrl queryctrl;
3173 memset( &queryctrl, 0, sizeof( queryctrl ) );
3175 /* A list of available controls (aka the variable name) will be
3176 * stored as choices in the "allcontrols" variable. We'll thus be able
3177 * to use those to create an appropriate interface
3178 * A list of available controls that can be changed mid-stream will
3179 * be stored in the "controls" variable */
3180 var_Create( p_obj, "controls", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
3181 var_Create( p_obj, "allcontrols", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
3183 var_Create( p_obj, "controls-update", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
3185 /* Add a control to reset all controls to their default values */
3186 vlc_value_t val, val2;
3187 var_Create( p_obj, "controls-reset", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
3188 val.psz_string = _( "Reset controls to default" );
3189 var_Change( p_obj, "controls-reset", VLC_VAR_SETTEXT, &val, NULL );
3191 val2.psz_string = (char *)"controls-reset";
3192 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
3194 var_AddCallback( p_obj, "controls-reset", DemuxControlResetCallback, NULL );
3196 var_AddCallback( p_obj, "controls-reset", AccessControlResetCallback, NULL );
3198 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
3199 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3201 msg_Dbg( p_obj, "Extended control API supported by v4l2 driver" );
3203 /* List extended controls */
3204 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
3205 while( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3207 if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS )
3209 msg_Dbg( p_obj, "%s", queryctrl.name );
3210 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
3213 switch( V4L2_CTRL_ID2CLASS( queryctrl.id ) )
3215 case V4L2_CTRL_CLASS_USER:
3216 msg_Dbg( p_obj, "Available control: %s (%x)",
3217 queryctrl.name, queryctrl.id );
3219 case V4L2_CTRL_CLASS_MPEG:
3220 name2var( queryctrl.name );
3221 msg_Dbg( p_obj, "Available MPEG control: %s (%x)",
3222 queryctrl.name, queryctrl.id );
3225 msg_Dbg( p_obj, "Available private control: %s (%x)",
3226 queryctrl.name, queryctrl.id );
3229 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
3230 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
3235 msg_Dbg( p_obj, "Extended control API not supported by v4l2 driver" );
3237 /* List public controls */
3238 for( i_cid = V4L2_CID_BASE;
3239 i_cid < V4L2_CID_LASTP1;
3242 queryctrl.id = i_cid;
3243 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3245 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3247 msg_Dbg( p_obj, "Available control: %s (%x)",
3248 queryctrl.name, queryctrl.id );
3249 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
3253 /* List private controls */
3254 for( i_cid = V4L2_CID_PRIVATE_BASE;
3258 queryctrl.id = i_cid;
3259 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3261 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3263 msg_Dbg( p_obj, "Available private control: %s (%x)",
3264 queryctrl.name, queryctrl.id );
3265 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
3275 static void SetAvailControlsByString( vlc_object_t *p_obj, demux_sys_t *p_sys,
3278 char *psz_parser = p_sys->psz_set_ctrls;
3279 vlc_value_t val, text, name;
3281 if( psz_parser == NULL )
3284 if( *psz_parser == '{' )
3287 int i_ret = var_Change( p_obj, "allcontrols", VLC_VAR_GETCHOICES,
3291 msg_Err( p_obj, "Oops, can't find 'allcontrols' variable." );
3295 while( *psz_parser && *psz_parser != '}' )
3297 char *psz_delim, *psz_assign;
3299 while( *psz_parser == ',' || *psz_parser == ' ' )
3302 psz_delim = strchr( psz_parser, ',' );
3303 if( psz_delim == NULL )
3304 psz_delim = strchr( psz_parser, '}' );
3305 if( psz_delim == NULL )
3306 psz_delim = psz_parser + strlen( psz_parser );
3308 psz_assign = memchr( psz_parser, '=', psz_delim - psz_parser );
3309 if( psz_assign == NULL )
3311 char *psz_name = strndup( psz_parser, psz_delim - psz_parser );
3312 msg_Err( p_obj, "%s missing '='", psz_name );
3314 psz_parser = psz_delim + 1;
3319 i < val.p_list->i_count ;//&& psz_parser < psz_assign;
3322 const char *psz_var = text.p_list->p_values[i].psz_string;
3323 int i_cid = val.p_list->p_values[i].i_int;
3324 var_Change( p_obj, psz_var, VLC_VAR_GETTEXT, &name, NULL );
3325 const char *psz_name = name.psz_string;
3327 int i_availstrlen = strlen( psz_name );
3328 int i_parsestrlen = psz_assign - psz_parser;
3329 int i_maxstrlen = __MAX( i_availstrlen, i_parsestrlen);
3331 if( !strncasecmp( psz_name, psz_parser, i_maxstrlen ) )
3333 Control( p_obj, i_fd, psz_name, i_cid,
3334 strtol( ++psz_assign, &psz_parser, 0) );
3338 if( psz_parser < psz_assign )
3340 char *psz_name = strndup( psz_parser, psz_assign - psz_parser );
3341 msg_Err( p_obj, "Control %s not available", psz_name );
3343 psz_parser = ( *psz_delim ) ? ( psz_delim + 1 ) : psz_delim;
3348 /*****************************************************************************
3349 * Reset all user-class v4l2 controls to their default value
3350 *****************************************************************************/
3351 static int ControlReset( vlc_object_t *p_obj, int i_fd )
3353 struct v4l2_queryctrl queryctrl;
3355 memset( &queryctrl, 0, sizeof( queryctrl ) );
3357 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
3358 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3360 /* Extended control API supported */
3361 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
3362 while( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3364 if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS
3365 || V4L2_CTRL_ID2CLASS( queryctrl.id ) == V4L2_CTRL_CLASS_MPEG )
3367 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
3370 struct v4l2_control control;
3371 memset( &control, 0, sizeof( control ) );
3372 control.id = queryctrl.id;
3373 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
3374 && queryctrl.default_value != control.value )
3377 for( i = 0; controls[i].psz_name != NULL; i++ )
3378 if( controls[i].i_cid == queryctrl.id ) break;
3379 name2var( queryctrl.name );
3380 Control( p_obj, i_fd,
3381 controls[i].psz_name ? : (const char *)queryctrl.name,
3382 queryctrl.id, queryctrl.default_value );
3384 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
3390 /* public controls */
3391 for( i_cid = V4L2_CID_BASE;
3392 i_cid < V4L2_CID_LASTP1;
3395 queryctrl.id = i_cid;
3396 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3398 struct v4l2_control control;
3399 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3401 memset( &control, 0, sizeof( control ) );
3402 control.id = queryctrl.id;
3403 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
3404 && queryctrl.default_value != control.value )
3407 for( i = 0; controls[i].psz_name != NULL; i++ )
3408 if( controls[i].i_cid == queryctrl.id ) break;
3409 name2var( queryctrl.name );
3410 Control( p_obj, i_fd,
3411 controls[i].psz_name ? : (const char *)queryctrl.name,
3412 queryctrl.id, queryctrl.default_value );
3417 /* private controls */
3418 for( i_cid = V4L2_CID_PRIVATE_BASE;
3422 queryctrl.id = i_cid;
3423 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3425 struct v4l2_control control;
3426 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3428 memset( &control, 0, sizeof( control ) );
3429 control.id = queryctrl.id;
3430 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
3431 && queryctrl.default_value != control.value )
3433 name2var( queryctrl.name );
3434 Control( p_obj, i_fd, (const char *)queryctrl.name,
3435 queryctrl.id, queryctrl.default_value );
3445 /*****************************************************************************
3446 * Issue user-class v4l2 controls
3447 *****************************************************************************/
3448 static int Control( vlc_object_t *p_obj, int i_fd,
3449 const char *psz_name, int i_cid, int i_value )
3451 struct v4l2_queryctrl queryctrl;
3452 struct v4l2_control control;
3453 struct v4l2_ext_control ext_control;
3454 struct v4l2_ext_controls ext_controls;
3459 memset( &queryctrl, 0, sizeof( queryctrl ) );
3461 queryctrl.id = i_cid;
3463 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) < 0
3464 || queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3466 msg_Dbg( p_obj, "%s (%x) control is not supported.", psz_name, i_cid );
3467 return VLC_EGENERIC;
3470 memset( &control, 0, sizeof( control ) );
3471 memset( &ext_control, 0, sizeof( ext_control ) );
3472 memset( &ext_controls, 0, sizeof( ext_controls ) );
3474 ext_control.id = i_cid;
3475 ext_controls.ctrl_class = V4L2_CTRL_ID2CLASS( i_cid );
3476 ext_controls.count = 1;
3477 ext_controls.controls = &ext_control;
3483 ext_control.value = i_value;
3484 if( ioctl( i_fd, VIDIOC_S_EXT_CTRLS, &ext_controls ) < 0 )
3486 control.value = i_value;
3487 if( ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
3489 msg_Err( p_obj, "unable to set %s (%x) to %d (%m)",
3490 psz_name, i_cid, i_value );
3491 return VLC_EGENERIC;
3493 i_ret = ioctl( i_fd, VIDIOC_G_CTRL, &control );
3497 i_ret = ioctl( i_fd, VIDIOC_G_EXT_CTRLS, &ext_controls );
3498 control.value = ext_control.value;
3505 msg_Dbg( p_obj, "video %s: %d", psz_name, control.value );
3506 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
3509 val.b_bool = control.value;
3510 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
3511 var_SetVoid( p_obj, "controls-update" );
3513 case VLC_VAR_INTEGER:
3514 val.i_int = control.value;
3515 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
3516 var_SetVoid( p_obj, "controls-update" );
3523 /*****************************************************************************
3524 * On the fly change settings callback
3525 *****************************************************************************/
3526 static int DemuxControlCallback( vlc_object_t *p_this,
3527 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3530 demux_t *p_demux = (demux_t*)p_this;
3531 demux_sys_t *p_sys = p_demux->p_sys;
3532 int i_cid = (int)p_data;
3534 int i_fd = p_sys->i_fd_video;
3537 return VLC_EGENERIC;
3539 Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
3541 return VLC_EGENERIC;
3544 static int DemuxControlResetCallback( vlc_object_t *p_this,
3545 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3548 demux_t *p_demux = (demux_t*)p_this;
3549 demux_sys_t *p_sys = p_demux->p_sys;
3551 int i_fd = p_sys->i_fd_video;
3554 return VLC_EGENERIC;
3556 ControlReset( p_this, i_fd );
3558 return VLC_EGENERIC;
3561 static int AccessControlCallback( vlc_object_t *p_this,
3562 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3565 access_t *p_access = (access_t *)p_this;
3566 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
3567 int i_cid = (int)p_data;
3569 int i_fd = p_sys->i_fd_video;
3572 return VLC_EGENERIC;
3574 Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
3576 return VLC_EGENERIC;
3579 static int AccessControlResetCallback( vlc_object_t *p_this,
3580 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3583 access_t *p_access = (access_t *)p_this;
3584 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
3586 int i_fd = p_sys->i_fd_video;
3589 return VLC_EGENERIC;
3591 ControlReset( p_this, i_fd );
3593 return VLC_EGENERIC;