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 *****************************************************************************/
46 #include <vlc_access.h>
47 #include <vlc_demux.h>
48 #include <vlc_input.h>
54 #include <sys/ioctl.h>
57 #include <linux/videodev2.h>
59 #include <sys/soundcard.h>
62 # define ALSA_PCM_NEW_HW_PARAMS_API
63 # define ALSA_PCM_NEW_SW_PARAMS_API
64 # include <alsa/asoundlib.h>
69 /*****************************************************************************
71 *****************************************************************************/
73 static int DemuxOpen ( vlc_object_t * );
74 static void DemuxClose( vlc_object_t * );
75 static int AccessOpen ( vlc_object_t * );
76 static void AccessClose( vlc_object_t * );
78 #define DEV_TEXT N_("Device name")
79 #define DEV_LONGTEXT N_( \
80 "Name of the device to use. " \
81 "If you don't specify anything, /dev/video0 will be used.")
82 #define STANDARD_TEXT N_( "Standard" )
83 #define STANDARD_LONGTEXT N_( \
84 "Video standard (Default, SECAM, PAL, or NTSC)." )
85 #define CHROMA_TEXT N_("Video input chroma format")
86 #define CHROMA_LONGTEXT N_( \
87 "Force the Video4Linux2 video device to use a specific chroma format " \
88 "(eg. I420 or I422 for raw images, MJPEG for M-JPEG compressed input) " \
89 "(Complete list: GREY, I240, RV16, RV15, RV24, RV32, YUY2, YUYV, UYVY, " \
90 "I41N, I422, I420, I411, I410, MJPG)")
91 #define INPUT_TEXT N_( "Input" )
92 #define INPUT_LONGTEXT N_( \
93 "Input of the card to use (see debug)." )
94 #define AUDIO_INPUT_TEXT N_( "Audio input" )
95 #define AUDIO_INPUT_LONGTEXT N_( \
96 "Audio input of the card to use (see debug)." )
97 #define IOMETHOD_TEXT N_( "IO Method" )
98 #define IOMETHOD_LONGTEXT N_( \
99 "IO Method (READ, MMAP, USERPTR)." )
100 #define WIDTH_TEXT N_( "Width" )
101 #define WIDTH_LONGTEXT N_( \
102 "Force width (-1 for autodetect)." )
103 #define HEIGHT_TEXT N_( "Height" )
104 #define HEIGHT_LONGTEXT N_( \
105 "Force height (-1 for autodetect)." )
106 #define FPS_TEXT N_( "Framerate" )
107 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
108 "(-1 for autodetect)." )
110 #define CTRL_RESET_TEXT N_( "Reset v4l2 controls" )
111 #define CTRL_RESET_LONGTEXT N_( \
112 "Reset controls to defaults provided by the v4l2 driver." )
113 #define BRIGHTNESS_TEXT N_( "Brightness" )
114 #define BRIGHTNESS_LONGTEXT N_( \
115 "Brightness of the video input (if supported by the v4l2 driver)." )
116 #define CONTRAST_TEXT N_( "Contrast" )
117 #define CONTRAST_LONGTEXT N_( \
118 "Contrast of the video input (if supported by the v4l2 driver)." )
119 #define SATURATION_TEXT N_( "Saturation" )
120 #define SATURATION_LONGTEXT N_( \
121 "Saturation of the video input (if supported by the v4l2 driver)." )
122 #define HUE_TEXT N_( "Hue" )
123 #define HUE_LONGTEXT N_( \
124 "Hue of the video input (if supported by the v4l2 driver)." )
125 #define BLACKLEVEL_TEXT N_( "Black level" )
126 #define BLACKLEVEL_LONGTEXT N_( \
127 "Black level of the video input (if supported by the v4l2 driver)." )
128 #define AUTOWHITEBALANCE_TEXT N_( "Auto white balance" )
129 #define AUTOWHITEBALANCE_LONGTEXT N_( \
130 "Automatically set the white balance of the video input " \
131 "(if supported by the v4l2 driver)." )
132 #define DOWHITEBALANCE_TEXT N_( "Do white balance" )
133 #define DOWHITEBALANCE_LONGTEXT N_( \
134 "Trigger a white balancing action, useless if auto white balance is " \
135 "activated (if supported by the v4l2 driver)." )
136 #define REDBALANCE_TEXT N_( "Red balance" )
137 #define REDBALANCE_LONGTEXT N_( \
138 "Red balance of the video input (if supported by the v4l2 driver)." )
139 #define BLUEBALANCE_TEXT N_( "Blue balance" )
140 #define BLUEBALANCE_LONGTEXT N_( \
141 "Blue balance of the video input (if supported by the v4l2 driver)." )
142 #define GAMMA_TEXT N_( "Gamma" )
143 #define GAMMA_LONGTEXT N_( \
144 "Gamma of the video input (if supported by the v4l2 driver)." )
145 #define EXPOSURE_TEXT N_( "Exposure" )
146 #define EXPOSURE_LONGTEXT N_( \
147 "Exposure of the video input (if supported by the v4L2 driver)." )
148 #define AUTOGAIN_TEXT N_( "Auto gain" )
149 #define AUTOGAIN_LONGTEXT N_( \
150 "Automatically set the video input's gain (if supported by the " \
152 #define GAIN_TEXT N_( "Gain" )
153 #define GAIN_LONGTEXT N_( \
154 "Video input's gain (if supported by the v4l2 driver)." )
155 #define HFLIP_TEXT N_( "Horizontal flip" )
156 #define HFLIP_LONGTEXT N_( \
157 "Flip the video horizontally (if supported by the v4l2 driver)." )
158 #define VFLIP_TEXT N_( "Vertical flip" )
159 #define VFLIP_LONGTEXT N_( \
160 "Flip the video vertically (if supported by the v4l2 driver)." )
161 #define HCENTER_TEXT N_( "Horizontal centering" )
162 #define HCENTER_LONGTEXT N_( \
163 "Set the camera's horizontal centering (if supported by the v4l2 driver)." )
164 #define VCENTER_TEXT N_( "Vertical centering" )
165 #define VCENTER_LONGTEXT N_( \
166 "Set the camera's vertical centering (if supported by the v4l2 driver)." )
168 #define ADEV_TEXT N_("Audio device name")
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.")
174 #define ADEV_LONGTEXT N_( \
175 "Name of the audio device to use. " \
176 "If you don't specify anything, \"/dev/dsp\" will be used for OSS, " \
179 #define AUDIO_METHOD_TEXT N_( "Audio method" )
181 #define AUDIO_METHOD_LONGTEXT N_( \
182 "Audio method to use: 0 to disable audio, 1 for OSS." )
184 #define AUDIO_METHOD_LONGTEXT N_( \
185 "Audio method to use: 0 to disable audio, 1 for OSS, 2 for ALSA, " \
186 "3 for ALSA or OSS (ALSA is prefered)." )
188 #define AUDIO_VOLUME_TEXT N_( "Volume" )
189 #define AUDIO_VOLUME_LONGTEXT N_( \
190 "Volume of the audio input (if supported by the v4l2 driver)." )
191 #define AUDIO_BALANCE_TEXT N_( "Balance" )
192 #define AUDIO_BALANCE_LONGTEXT N_( \
193 "Balance of the audio input (if supported by the v4l2 driver)." )
194 #define AUDIO_MUTE_TEXT N_( "Mute" )
195 #define AUDIO_MUTE_LONGTEXT N_( \
196 "Mute audio input (if supported by the v4l2 driver)." )
197 #define AUDIO_BASS_TEXT N_( "Bass" )
198 #define AUDIO_BASS_LONGTEXT N_( \
199 "Bass level of the audio input (if supported by the v4l2 driver)." )
200 #define AUDIO_TREBLE_TEXT N_( "Treble" )
201 #define AUDIO_TREBLE_LONGTEXT N_( \
202 "Treble level of the audio input (if supported by the v4l2 driver)." )
203 #define AUDIO_LOUDNESS_TEXT N_( "Loudness" )
204 #define AUDIO_LOUDNESS_LONGTEXT N_( \
205 "Loudness of the audio input (if supported by the v4l2 driver)." )
207 #define STEREO_TEXT N_( "Stereo" )
208 #define STEREO_LONGTEXT N_( \
209 "Capture the audio stream in stereo." )
210 #define SAMPLERATE_TEXT N_( "Samplerate" )
211 #define SAMPLERATE_LONGTEXT N_( \
212 "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
214 #define CACHING_TEXT N_("Caching value in ms")
215 #define CACHING_LONGTEXT N_( \
216 "Caching value for V4L2 captures. This " \
217 "value should be set in milliseconds." )
218 #define S_CTRLS_TEXT N_("v4l2 driver controls")
219 #define S_CTRLS_LONGTEXT N_( \
220 "Set the v4l2 driver controls to the values specified using a comma " \
221 "separated list optionally encapsulated by curly braces " \
222 "(e.g.: {video_bitrate=6000000,audio_crc=0,stream_type=3} ). " \
223 "To list available controls, increase verbosity (-vvv) " \
224 "or use the v4l2-ctl application." )
226 #define TUNER_TEXT N_("Tuner id")
227 #define TUNER_LONGTEXT N_( \
228 "Tuner id (see debug output)." )
229 #define FREQUENCY_TEXT N_("Frequency")
230 #define FREQUENCY_LONGTEXT N_( \
231 "Tuner frequency in Hz or kHz (see debug output)" )
232 #define TUNER_AUDIO_MODE_TEXT N_("Audio mode")
233 #define TUNER_AUDIO_MODE_LONGTEXT N_( \
234 "Tuner audio mono/stereo and track selection." )
242 static int i_standards_list[] =
243 { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
244 static const char *psz_standards_list_text[] =
245 { N_("Default"), N_("SECAM"), N_("PAL"), N_("NTSC") };
247 static int i_iomethod_list[] =
248 { IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR };
249 static const char *psz_iomethod_list_text[] =
250 { N_("READ"), N_("MMAP"), N_("USERPTR") };
252 static int i_tuner_audio_modes_list[] =
253 { V4L2_TUNER_MODE_MONO, V4L2_TUNER_MODE_STEREO,
254 V4L2_TUNER_MODE_LANG1, V4L2_TUNER_MODE_LANG2,
255 V4L2_TUNER_MODE_SAP, V4L2_TUNER_MODE_LANG1_LANG2 };
256 static const char *psz_tuner_audio_modes_list_text[] =
259 N_( "Primary language (Analog TV tuners only)" ),
260 N_( "Secondary language (Analog TV tuners only)" ),
261 N_( "Second audio program (Analog TV tuners only)" ),
262 N_( "Primary language left, Secondary language right" ) };
267 #define AUDIO_METHOD_OSS 1
268 #define OSS_DEFAULT "/dev/dsp"
269 #define AUDIO_METHOD_ALSA 2
270 #define ALSA_DEFAULT "hw"
271 #define CFG_PREFIX "v4l2-"
274 set_shortname( _("Video4Linux2") );
275 set_description( _("Video4Linux2 input") );
276 set_category( CAT_INPUT );
277 set_subcategory( SUBCAT_INPUT_ACCESS );
279 set_section( N_( "Video input" ), NULL );
280 add_string( CFG_PREFIX "dev", "/dev/video0", 0, DEV_TEXT, DEV_LONGTEXT,
282 add_integer( CFG_PREFIX "standard", 0, NULL, STANDARD_TEXT,
283 STANDARD_LONGTEXT, VLC_FALSE );
284 change_integer_list( i_standards_list, psz_standards_list_text, 0 );
285 add_string( CFG_PREFIX "chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
287 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 );
291 add_integer( CFG_PREFIX "io", IO_METHOD_MMAP, NULL, IOMETHOD_TEXT,
292 IOMETHOD_LONGTEXT, VLC_TRUE );
293 change_integer_list( i_iomethod_list, psz_iomethod_list_text, 0 );
294 add_integer( CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
295 WIDTH_LONGTEXT, VLC_TRUE );
296 add_integer( CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
297 HEIGHT_LONGTEXT, VLC_TRUE );
298 add_float( CFG_PREFIX "fps", 0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
300 set_section( N_( "Audio input" ), NULL );
301 add_string( CFG_PREFIX "adev", NULL, 0, ADEV_TEXT, ADEV_LONGTEXT,
303 add_integer( CFG_PREFIX "audio-method", AUDIO_METHOD_OSS|AUDIO_METHOD_ALSA,
304 NULL, AUDIO_METHOD_TEXT, AUDIO_METHOD_LONGTEXT, VLC_TRUE );
305 add_bool( CFG_PREFIX "stereo", VLC_TRUE, NULL, STEREO_TEXT, STEREO_LONGTEXT,
307 add_integer( CFG_PREFIX "samplerate", 48000, NULL, SAMPLERATE_TEXT,
308 SAMPLERATE_LONGTEXT, VLC_TRUE );
309 add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
310 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
312 set_section( N_( "Tuner" ), NULL );
313 add_integer( CFG_PREFIX "tuner", 0, NULL, TUNER_TEXT, TUNER_LONGTEXT,
315 add_integer( CFG_PREFIX "tuner-frequency", -1, NULL, FREQUENCY_TEXT,
316 FREQUENCY_LONGTEXT, VLC_TRUE );
317 add_integer( CFG_PREFIX "tuner-audio-mode", -1, NULL, TUNER_AUDIO_MODE_TEXT,
318 TUNER_AUDIO_MODE_LONGTEXT, VLC_TRUE );
319 change_integer_list( i_tuner_audio_modes_list,
320 psz_tuner_audio_modes_list_text, 0 );
322 set_section( N_( "Controls" ),
323 N_( "v4l2 driver controls, if supported by your v4l2 driver." ) );
324 add_bool( CFG_PREFIX "controls-reset", VLC_FALSE, NULL, CTRL_RESET_TEXT,
325 CTRL_RESET_LONGTEXT, VLC_TRUE );
326 add_integer( CFG_PREFIX "brightness", -1, NULL, BRIGHTNESS_TEXT,
327 BRIGHTNESS_LONGTEXT, VLC_TRUE );
328 add_integer( CFG_PREFIX "contrast", -1, NULL, CONTRAST_TEXT,
329 CONTRAST_LONGTEXT, VLC_TRUE );
330 add_integer( CFG_PREFIX "saturation", -1, NULL, SATURATION_TEXT,
331 SATURATION_LONGTEXT, VLC_TRUE );
332 add_integer( CFG_PREFIX "hue", -1, NULL, HUE_TEXT,
333 HUE_LONGTEXT, VLC_TRUE );
334 add_integer( CFG_PREFIX "black-level", -1, NULL, BLACKLEVEL_TEXT,
335 BLACKLEVEL_LONGTEXT, VLC_TRUE );
336 add_integer( CFG_PREFIX "auto-white-balance", -1, NULL,
337 AUTOWHITEBALANCE_TEXT, AUTOWHITEBALANCE_LONGTEXT, VLC_TRUE );
338 add_integer( CFG_PREFIX "do-white-balance", -1, NULL, DOWHITEBALANCE_TEXT,
339 DOWHITEBALANCE_LONGTEXT, VLC_TRUE );
340 add_integer( CFG_PREFIX "red-balance", -1, NULL, REDBALANCE_TEXT,
341 REDBALANCE_LONGTEXT, VLC_TRUE );
342 add_integer( CFG_PREFIX "blue-balance", -1, NULL, BLUEBALANCE_TEXT,
343 BLUEBALANCE_LONGTEXT, VLC_TRUE );
344 add_integer( CFG_PREFIX "gamma", -1, NULL, GAMMA_TEXT,
345 GAMMA_LONGTEXT, VLC_TRUE );
346 add_integer( CFG_PREFIX "exposure", -1, NULL, EXPOSURE_TEXT,
347 EXPOSURE_LONGTEXT, VLC_TRUE );
348 add_integer( CFG_PREFIX "autogain", -1, NULL, AUTOGAIN_TEXT,
349 AUTOGAIN_LONGTEXT, VLC_TRUE );
350 add_integer( CFG_PREFIX "gain", -1, NULL, GAIN_TEXT,
351 GAIN_LONGTEXT, VLC_TRUE );
352 add_integer( CFG_PREFIX "hflip", -1, NULL, HFLIP_TEXT,
353 HFLIP_LONGTEXT, VLC_TRUE );
354 add_integer( CFG_PREFIX "vflip", -1, NULL, VFLIP_TEXT,
355 VFLIP_LONGTEXT, VLC_TRUE );
356 add_integer( CFG_PREFIX "hcenter", -1, NULL, HCENTER_TEXT,
357 HCENTER_LONGTEXT, VLC_TRUE );
358 add_integer( CFG_PREFIX "vcenter", -1, NULL, VCENTER_TEXT,
359 VCENTER_LONGTEXT, VLC_TRUE );
360 add_integer( CFG_PREFIX "audio-volume", -1, NULL, AUDIO_VOLUME_TEXT,
361 AUDIO_VOLUME_LONGTEXT, VLC_TRUE );
362 add_integer( CFG_PREFIX "audio-balance", -1, NULL, AUDIO_BALANCE_TEXT,
363 AUDIO_BALANCE_LONGTEXT, VLC_TRUE );
364 add_bool( CFG_PREFIX "audio-mute", VLC_FALSE, NULL, AUDIO_MUTE_TEXT,
365 AUDIO_MUTE_LONGTEXT, VLC_TRUE );
366 add_integer( CFG_PREFIX "audio-bass", -1, NULL, AUDIO_BASS_TEXT,
367 AUDIO_BASS_LONGTEXT, VLC_TRUE );
368 add_integer( CFG_PREFIX "audio-treble", -1, NULL, AUDIO_TREBLE_TEXT,
369 AUDIO_TREBLE_LONGTEXT, VLC_TRUE );
370 add_integer( CFG_PREFIX "audio-loudness", -1, NULL, AUDIO_LOUDNESS_TEXT,
371 AUDIO_LOUDNESS_LONGTEXT, VLC_TRUE );
372 add_string( CFG_PREFIX "set-ctrls", NULL, NULL, S_CTRLS_TEXT,
373 S_CTRLS_LONGTEXT, VLC_TRUE );
375 add_shortcut( "v4l2" );
376 set_capability( "access_demux", 10 );
377 set_callbacks( DemuxOpen, DemuxClose );
380 add_shortcut( "v4l2c" );
381 set_description( _("Video4Linux2 Compressed A/V") );
382 set_capability( "access2", 0 );
383 /* use these when open as access_demux fails; VLC will use another demux */
384 set_callbacks( AccessOpen, AccessClose );
388 /*****************************************************************************
389 * Access: local prototypes
390 *****************************************************************************/
392 static void CommonClose( vlc_object_t *, demux_sys_t * );
393 static void ParseMRL( demux_sys_t *, char *, vlc_object_t * );
394 static void GetV4L2Params( demux_sys_t *, vlc_object_t * );
395 static void SetAvailControlsByString( vlc_object_t *, demux_sys_t *, int );
397 static int DemuxControl( demux_t *, int, va_list );
398 static int AccessControl( access_t *, int, va_list );
400 static int Demux( demux_t * );
401 static ssize_t AccessRead( access_t *, uint8_t *, size_t );
403 static block_t* GrabVideo( demux_t *p_demux );
404 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t );
405 static block_t* GrabAudio( demux_t *p_demux );
407 static vlc_bool_t IsPixelFormatSupported( demux_t *p_demux,
408 unsigned int i_pixelformat );
411 static char* ResolveALSADeviceName( const char *psz_device );
413 static int OpenVideoDev( vlc_object_t *, demux_sys_t *, vlc_bool_t );
414 static int OpenAudioDev( vlc_object_t *, demux_sys_t *, vlc_bool_t );
415 static vlc_bool_t ProbeVideoDev( vlc_object_t *, demux_sys_t *,
417 static vlc_bool_t ProbeAudioDev( vlc_object_t *, demux_sys_t *,
420 static int ControlList( vlc_object_t *, int , vlc_bool_t, vlc_bool_t );
421 static int Control( vlc_object_t *, int i_fd,
422 const char *psz_name, int i_cid, int i_value );
424 static int DemuxControlCallback( vlc_object_t *p_this, const char *psz_var,
425 vlc_value_t oldval, vlc_value_t newval,
427 static int DemuxControlResetCallback( vlc_object_t *p_this, const char *psz_var,
428 vlc_value_t oldval, vlc_value_t newval,
430 static int AccessControlCallback( vlc_object_t *p_this, const char *psz_var,
431 vlc_value_t oldval, vlc_value_t newval,
433 static int AccessControlResetCallback( vlc_object_t *p_this,
434 const char *psz_var, vlc_value_t oldval,
435 vlc_value_t newval, void *p_data );
441 } v4l2chroma_to_fourcc[] =
444 { V4L2_PIX_FMT_GREY, VLC_FOURCC('G','R','E','Y') },
445 { V4L2_PIX_FMT_HI240, VLC_FOURCC('I','2','4','0') },
446 { V4L2_PIX_FMT_RGB565, VLC_FOURCC('R','V','1','6') },
447 { V4L2_PIX_FMT_RGB555, VLC_FOURCC('R','V','1','5') },
448 { V4L2_PIX_FMT_BGR24, VLC_FOURCC('R','V','2','4') },
449 { V4L2_PIX_FMT_BGR32, VLC_FOURCC('R','V','3','2') },
450 { V4L2_PIX_FMT_YUYV, VLC_FOURCC('Y','U','Y','2') },
451 { V4L2_PIX_FMT_YUYV, VLC_FOURCC('Y','U','Y','V') },
452 { V4L2_PIX_FMT_UYVY, VLC_FOURCC('U','Y','V','Y') },
453 { V4L2_PIX_FMT_Y41P, VLC_FOURCC('I','4','1','N') },
454 { V4L2_PIX_FMT_YUV422P, VLC_FOURCC('I','4','2','2') },
455 { V4L2_PIX_FMT_YVU420, VLC_FOURCC('Y','V','1','2') },
456 { V4L2_PIX_FMT_YUV411P, VLC_FOURCC('I','4','1','1') },
457 { V4L2_PIX_FMT_YUV410, VLC_FOURCC('I','4','1','0') },
459 /* Raw data types, not in V4L2 spec but still in videodev2.h and supported
461 { V4L2_PIX_FMT_YUV420, VLC_FOURCC('I','4','2','0') },
462 /* FIXME { V4L2_PIX_FMT_RGB444, VLC_FOURCC('R','V','3','2') }, */
464 /* Compressed data types */
465 { V4L2_PIX_FMT_MJPEG, VLC_FOURCC('M','J','P','G') },
467 { V4L2_PIX_FMT_JPEG, VLC_FOURCC('J','P','E','G') },
468 { V4L2_PIX_FMT_DV, VLC_FOURCC('?','?','?','?') },
469 { V4L2_PIX_FMT_MPEG, VLC_FOURCC('?','?','?','?') },
475 * List of V4L2 chromas were confident enough to use as fallbacks if the
476 * user hasn't provided a --v4l2-chroma value.
478 static const __u32 p_chroma_fallbacks[] =
479 { V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_YVU420, V4L2_PIX_FMT_YUV422P,
480 V4L2_PIX_FMT_YUYV, V4L2_PIX_FMT_MJPEG };
484 const char *psz_name;
488 { "brightness", V4L2_CID_BRIGHTNESS },
489 { "contrast", V4L2_CID_CONTRAST },
490 { "saturation", V4L2_CID_SATURATION },
491 { "hue", V4L2_CID_HUE },
492 { "audio-volume", V4L2_CID_AUDIO_VOLUME },
493 { "audio-balance", V4L2_CID_AUDIO_BALANCE },
494 { "audio-bass", V4L2_CID_AUDIO_BASS },
495 { "audio-treble", V4L2_CID_AUDIO_TREBLE },
496 { "audio-mute", V4L2_CID_AUDIO_MUTE },
497 { "audio-loudness", V4L2_CID_AUDIO_LOUDNESS },
498 { "black-level", V4L2_CID_BLACK_LEVEL },
499 { "auto-white-balance", V4L2_CID_AUTO_WHITE_BALANCE },
500 { "do-white-balance", V4L2_CID_DO_WHITE_BALANCE },
501 { "red-balance", V4L2_CID_RED_BALANCE },
502 { "blue-balance", V4L2_CID_BLUE_BALANCE },
503 { "gamma", V4L2_CID_GAMMA },
504 { "exposure", V4L2_CID_EXPOSURE },
505 { "autogain", V4L2_CID_AUTOGAIN },
506 { "gain", V4L2_CID_GAIN },
507 { "hflip", V4L2_CID_HFLIP },
508 { "vflip", V4L2_CID_VFLIP },
509 { "hcenter", V4L2_CID_HCENTER },
510 { "vcenter", V4L2_CID_VCENTER },
522 char *psz_device; /* Main device from MRL, can be video or audio */
530 char *psz_requested_chroma;
537 struct v4l2_capability dev_cap;
540 struct v4l2_input *p_inputs;
541 int i_selected_input;
544 struct v4l2_standard *p_standards;
545 v4l2_std_id i_selected_standard_id;
548 /* V4L2 devices cannot have more than 32 audio inputs */
549 struct v4l2_audio p_audios[32];
550 int i_selected_audio_input;
553 struct v4l2_tuner *p_tuners;
556 struct v4l2_fmtdesc *p_codecs;
558 struct buffer_t *p_buffers;
559 unsigned int i_nbuffers;
563 float f_fps; /* <= 0.0 mean to grab at full rate */
564 mtime_t i_video_pts; /* only used when f_fps > 0 */
567 es_out_id_t *p_es_video;
570 unsigned int i_sample_rate;
572 size_t i_audio_max_frame_size;
573 block_t *p_block_audio;
574 es_out_id_t *p_es_audio;
580 snd_pcm_t *p_alsa_pcm;
581 size_t i_alsa_frame_size;
582 int i_alsa_chunk_size;
594 static int FindMainDevice( vlc_object_t *p_this, demux_sys_t *p_sys,
595 int i_flags, vlc_bool_t b_demux,
596 vlc_bool_t b_forced )
598 /* Find main device (video or audio) */
599 if( p_sys->psz_device && *p_sys->psz_device )
601 msg_Dbg( p_this, "main device='%s'", p_sys->psz_device );
603 vlc_bool_t b_maindevice_is_video = VLC_FALSE;
605 /* Try to open as video device */
606 if( i_flags & FIND_VIDEO )
608 msg_Dbg( p_this, "trying device '%s' as video", p_sys->psz_device );
609 if( ProbeVideoDev( p_this, p_sys, p_sys->psz_device ) )
611 msg_Dbg( p_this, "'%s' is a video device", p_sys->psz_device );
612 /* Device was a video device */
613 free( p_sys->psz_vdev );
614 p_sys->psz_vdev = p_sys->psz_device;
615 p_sys->psz_device = NULL;
616 p_sys->i_fd_video = OpenVideoDev( p_this, p_sys, b_demux );
617 if( p_sys->i_fd_video < 0 )
619 b_maindevice_is_video = VLC_TRUE;
620 /* If successful we carry on to try the audio if access is forced */
624 /* Try to open as audio device only if main device was not detected as video above */
625 if( i_flags & FIND_AUDIO && !b_maindevice_is_video )
627 msg_Dbg( p_this, "trying device '%s' as audio", p_sys->psz_device );
628 if( ProbeAudioDev( p_this, p_sys, p_sys->psz_device ) )
630 msg_Dbg( p_this, "'%s' is an audio device", p_sys->psz_device );
631 /* Device was an audio device */
632 free( p_sys->psz_adev );
633 p_sys->psz_adev = p_sys->psz_device;
634 p_sys->psz_device = NULL;
635 p_sys->i_fd_audio = OpenAudioDev( p_this, p_sys, b_demux );
636 if( p_sys->i_fd_audio < 0 )
638 /* If successful we carry on to try the video if access is forced */
643 /* If no device opened, only continue if the access was forced */
644 if( b_forced == VLC_FALSE
645 && !( ( i_flags & FIND_VIDEO && p_sys->i_fd_video >= 0 )
646 || ( i_flags & FIND_AUDIO && p_sys->i_fd_audio >= 0 ) ) )
651 /* Find video device */
652 if( i_flags & FIND_VIDEO && p_sys->i_fd_video < 0 )
654 if( !p_sys->psz_vdev || !*p_sys->psz_vdev )
656 free( p_sys->psz_vdev );
657 p_sys->psz_vdev = var_CreateGetString( p_this, "v4l2-dev" );
660 msg_Dbg( p_this, "opening '%s' as video", p_sys->psz_vdev );
661 if( p_sys->psz_vdev && *p_sys->psz_vdev
662 && ProbeVideoDev( p_this, p_sys, p_sys->psz_vdev ) )
664 p_sys->i_fd_video = OpenVideoDev( p_this, p_sys, b_demux );
668 /* Find audio device */
669 if( i_flags & FIND_AUDIO && p_sys->i_fd_audio < 0 )
671 if( !p_sys->psz_adev )
673 p_sys->psz_adev = var_CreateGetNonEmptyString( p_this, "v4l2-adev" );
676 msg_Dbg( p_this, "opening '%s' as audio", p_sys->psz_adev );
677 if( ProbeAudioDev( p_this, p_sys, p_sys->psz_adev ) )
679 p_sys->i_fd_audio = OpenAudioDev( p_this, p_sys, b_demux );
683 if( !( ( i_flags & FIND_VIDEO && p_sys->i_fd_video >= 0 )
684 || ( i_flags & FIND_AUDIO && p_sys->i_fd_audio >= 0 ) ) )
691 /*****************************************************************************
692 * DemuxOpen: opens v4l2 device, access_demux callback
693 *****************************************************************************
695 * url: <video device>::::
697 *****************************************************************************/
698 static int DemuxOpen( vlc_object_t *p_this )
700 demux_t *p_demux = (demux_t*)p_this;
703 /* Only when selected */
704 if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
707 p_demux->pf_control = DemuxControl;
708 p_demux->pf_demux = Demux;
709 p_demux->info.i_update = 0;
710 p_demux->info.i_title = 0;
711 p_demux->info.i_seekpoint = 0;
713 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
714 if( p_sys == NULL ) return VLC_ENOMEM;
716 GetV4L2Params(p_sys, (vlc_object_t *) p_demux);
718 ParseMRL( p_sys, p_demux->psz_path, (vlc_object_t *) p_demux );
721 /* Alsa support available? */
722 msg_Dbg( p_demux, "ALSA input support available" );
725 if( FindMainDevice( p_this, p_sys, FIND_VIDEO|FIND_AUDIO,
726 VLC_TRUE, !strncmp( p_demux->psz_access, "v4l2", 4 ) ) != VLC_SUCCESS )
728 DemuxClose( p_this );
735 /*****************************************************************************
736 * GetV4L2Params: fill in p_sys parameters (shared by DemuxOpen and AccessOpen)
737 *****************************************************************************/
738 static void GetV4L2Params( demux_sys_t *p_sys, vlc_object_t *p_obj )
740 p_sys->i_video_pts = -1;
742 p_sys->i_selected_standard_id =
743 i_standards_list[var_CreateGetInteger( p_obj, "v4l2-standard" )];
745 p_sys->i_selected_input = var_CreateGetInteger( p_obj, "v4l2-input" );
746 p_sys->i_selected_audio_input =
747 var_CreateGetInteger( p_obj, "v4l2-audio-input" );
749 p_sys->io = var_CreateGetInteger( p_obj, "v4l2-io" );
751 p_sys->i_width = var_CreateGetInteger( p_obj, "v4l2-width" );
752 p_sys->i_height = var_CreateGetInteger( p_obj, "v4l2-height" );
754 var_CreateGetBool( p_obj, "v4l2-controls-reset" );
756 p_sys->f_fps = var_CreateGetFloat( p_obj, "v4l2-fps" );
757 p_sys->i_sample_rate = var_CreateGetInteger( p_obj, "v4l2-samplerate" );
758 p_sys->psz_requested_chroma = var_CreateGetString( p_obj, "v4l2-chroma" );
760 p_sys->i_audio_method = var_CreateGetInteger( p_obj, "v4l2-audio-method" );
762 p_sys->b_stereo = var_CreateGetBool( p_obj, "v4l2-stereo" );
764 p_sys->i_pts = var_CreateGetInteger( p_obj, "v4l2-caching" );
766 p_sys->i_cur_tuner = var_CreateGetInteger( p_obj, "v4l2-tuner" );
767 p_sys->i_frequency = var_CreateGetInteger( p_obj, "v4l2-tuner-frequency" );
768 p_sys->i_audio_mode = var_CreateGetInteger( p_obj, "v4l2-tuner-audio-mode" );
770 p_sys->psz_set_ctrls = var_CreateGetString( p_obj, "v4l2-set-ctrls" );
772 p_sys->psz_device = p_sys->psz_vdev = p_sys->psz_adev = NULL;
773 p_sys->i_fd_video = -1;
774 p_sys->i_fd_audio = -1;
776 p_sys->p_es_video = p_sys->p_es_audio = 0;
777 p_sys->p_block_audio = 0;
780 /*****************************************************************************
781 * ParseMRL: parse the options contained in the MRL
782 *****************************************************************************/
783 static void ParseMRL( demux_sys_t *p_sys, char *psz_path, vlc_object_t *p_obj )
785 char *psz_dup = strdup( psz_path );
786 char *psz_parser = psz_dup;
788 while( *psz_parser && *psz_parser != ':' )
793 if( *psz_parser == ':' )
798 *psz_parser++ = '\0';
800 if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
804 psz_parser += strlen( "adev=" );
805 if( strchr( psz_parser, ':' ) )
807 i_len = strchr( psz_parser, ':' ) - psz_parser;
811 i_len = strlen( psz_parser );
814 p_sys->psz_adev = strndup( psz_parser, i_len );
815 if( !*p_sys->psz_adev )
817 free( p_sys->psz_adev );
818 p_sys->psz_adev = NULL;
823 else if( !strncmp( psz_parser, "standard=", strlen( "standard=" ) ) )
825 psz_parser += strlen( "standard=" );
826 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
828 p_sys->i_selected_standard_id = V4L2_STD_PAL;
829 psz_parser += strlen( "pal" );
831 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
833 p_sys->i_selected_standard_id = V4L2_STD_NTSC;
834 psz_parser += strlen( "ntsc" );
836 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
838 p_sys->i_selected_standard_id = V4L2_STD_SECAM;
839 psz_parser += strlen( "secam" );
841 else if( !strncmp( psz_parser, "default", strlen( "default" ) ) )
843 p_sys->i_selected_standard_id = V4L2_STD_UNKNOWN;
844 psz_parser += strlen( "default" );
848 p_sys->i_selected_standard_id = i_standards_list[strtol( psz_parser, &psz_parser, 0 )];
851 else if( !strncmp( psz_parser, "chroma=", strlen( "chroma=" ) ) )
855 psz_parser += strlen( "chroma=" );
856 if( strchr( psz_parser, ':' ) )
858 i_len = strchr( psz_parser, ':' ) - psz_parser;
862 i_len = strlen( psz_parser );
865 free( p_sys->psz_requested_chroma );
866 p_sys->psz_requested_chroma = strndup( psz_parser, i_len );
870 else if( !strncmp( psz_parser, "input=", strlen( "input=" ) ) )
872 p_sys->i_selected_input = strtol( psz_parser + strlen( "input=" ),
875 else if( !strncmp( psz_parser, "audio-input=", strlen( "audio-input=" ) ) )
877 p_sys->i_selected_audio_input = strtol( psz_parser + strlen( "audio-input=" ),
880 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
882 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
885 else if( !strncmp( psz_parser, "io=", strlen( "io=" ) ) )
887 psz_parser += strlen( "io=" );
888 if( !strncmp( psz_parser, "read", strlen( "read" ) ) )
890 p_sys->io = IO_METHOD_READ;
891 psz_parser += strlen( "read" );
893 else if( !strncmp( psz_parser, "mmap", strlen( "mmap" ) ) )
895 p_sys->io = IO_METHOD_MMAP;
896 psz_parser += strlen( "mmap" );
898 else if( !strncmp( psz_parser, "userptr", strlen( "userptr" ) ) )
900 p_sys->io = IO_METHOD_USERPTR;
901 psz_parser += strlen( "userptr" );
905 p_sys->io = strtol( psz_parser, &psz_parser, 0 );
908 else if( !strncmp( psz_parser, "width=",
909 strlen( "width=" ) ) )
912 strtol( psz_parser + strlen( "width=" ),
915 else if( !strncmp( psz_parser, "height=",
916 strlen( "height=" ) ) )
919 strtol( psz_parser + strlen( "height=" ),
922 else if( !strncmp( psz_parser, "controls-reset",
923 strlen( "controls-reset" ) ) )
925 var_SetBool( p_obj, "v4l2-controls-reset", VLC_TRUE );
926 psz_parser += strlen( "controls-reset" );
929 else if( !strncmp( psz_parser, "brightness=",
930 strlen( "brightness=" ) ) )
932 var_SetInteger( p_obj, "brightness",
933 strtol( psz_parser + strlen( "brightness=" ),
936 else if( !strncmp( psz_parser, "contrast=",
937 strlen( "contrast=" ) ) )
939 var_SetInteger( p_obj, "contrast",
940 strtol( psz_parser + strlen( "contrast=" ),
943 else if( !strncmp( psz_parser, "saturation=",
944 strlen( "saturation=" ) ) )
946 var_SetInteger( p_obj, "saturation",
947 strtol( psz_parser + strlen( "saturation=" ),
950 else if( !strncmp( psz_parser, "hue=",
953 var_SetInteger( p_obj, "hue",
954 strtol( psz_parser + strlen( "hue=" ),
957 else if( !strncmp( psz_parser, "gamma=",
958 strlen( "gamma=" ) ) )
960 var_SetInteger( p_obj, "gamma",
961 strtol( psz_parser + strlen( "gamma=" ),
965 else if( !strncmp( psz_parser, "samplerate=",
966 strlen( "samplerate=" ) ) )
968 p_sys->i_sample_rate =
969 strtol( psz_parser + strlen( "samplerate=" ),
972 else if( !strncmp( psz_parser, "audio-method", strlen( "audio-method" ) ) )
974 p_sys->i_audio_method =
975 strtol( psz_parser + strlen( "audio-method" ),
978 else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
980 psz_parser += strlen( "stereo" );
981 p_sys->b_stereo = VLC_TRUE;
983 else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
985 psz_parser += strlen( "mono" );
986 p_sys->b_stereo = VLC_FALSE;
988 else if( !strncmp( psz_parser, "caching=", strlen( "caching=" ) ) )
990 p_sys->i_pts = strtol( psz_parser + strlen( "caching=" ),
993 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
995 p_sys->i_cur_tuner = strtol( psz_parser + strlen( "tuner=" ),
998 else if( !strncmp( psz_parser, "tuner-frequency=", strlen( "tuner-frequency=" ) ) )
1000 p_sys->i_frequency = strtol( psz_parser
1001 + strlen( "tuner-frequency=" ),
1004 else if( !strncmp( psz_parser, "tuner-audio-mode=", strlen( "tuner-audio-mode=" ) ) )
1006 p_sys->i_audio_mode = strtol( psz_parser
1007 + strlen( "tuner-audio-mode=" ),
1010 else if( !strncmp( psz_parser, "set-ctrls=", strlen( "set-ctrls=" )) )
1014 psz_parser += strlen( "set-ctrls=" );
1015 if( strchr( psz_parser, ':' ) )
1017 i_len = strchr( psz_parser, ':' ) - psz_parser;
1021 i_len = strlen( psz_parser );
1024 p_sys->psz_set_ctrls = strndup( psz_parser, i_len );
1026 psz_parser += i_len;
1030 char *psz_unk = strchr( psz_parser, ':' );
1032 psz_unk = strndup( psz_parser, psz_unk - psz_parser );
1034 psz_unk = strdup( psz_parser);
1035 msg_Warn( p_obj, "unknown option %s", psz_unk );
1039 while( *psz_parser && *psz_parser != ':' )
1044 if( *psz_parser == '\0' )
1054 p_sys->psz_device = strdup( psz_dup );
1059 /*****************************************************************************
1060 * Close: close device, free resources
1061 *****************************************************************************/
1062 static void AccessClose( vlc_object_t *p_this )
1064 access_t *p_access = (access_t *)p_this;
1065 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1067 CommonClose( p_this, p_sys );
1070 static void DemuxClose( vlc_object_t *p_this )
1072 struct v4l2_buffer buf;
1073 enum v4l2_buf_type buf_type;
1076 demux_t *p_demux = (demux_t *)p_this;
1077 demux_sys_t *p_sys = p_demux->p_sys;
1079 /* Stop video capture */
1080 if( p_sys->i_fd_video >= 0 )
1084 case IO_METHOD_READ:
1088 case IO_METHOD_MMAP:
1089 case IO_METHOD_USERPTR:
1090 /* Some drivers 'hang' internally if this is not done before streamoff */
1091 for( unsigned int i = 0; i < p_sys->i_nbuffers; i++ )
1093 memset( &buf, 0, sizeof(buf) );
1094 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1095 buf.memory = ( p_sys->io == IO_METHOD_USERPTR ) ?
1096 V4L2_MEMORY_USERPTR : V4L2_MEMORY_MMAP;
1097 ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ); /* ignore result */
1100 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1101 if( ioctl( p_sys->i_fd_video, VIDIOC_STREAMOFF, &buf_type ) < 0 ) {
1102 msg_Err( p_this, "VIDIOC_STREAMOFF failed" );
1109 /* Free Video Buffers */
1110 if( p_sys->p_buffers ) {
1113 case IO_METHOD_READ:
1114 free( p_sys->p_buffers[0].start );
1117 case IO_METHOD_MMAP:
1118 for( i = 0; i < p_sys->i_nbuffers; ++i )
1120 if( munmap( p_sys->p_buffers[i].start, p_sys->p_buffers[i].length ) )
1122 msg_Err( p_this, "munmap failed" );
1127 case IO_METHOD_USERPTR:
1128 for( i = 0; i < p_sys->i_nbuffers; ++i )
1130 free( p_sys->p_buffers[i].start );
1134 free( p_sys->p_buffers );
1137 CommonClose( p_this, p_sys );
1140 static void CommonClose( vlc_object_t *p_this, demux_sys_t *p_sys )
1143 if( p_sys->i_fd_video >= 0 ) close( p_sys->i_fd_video );
1145 if( p_sys->p_alsa_pcm )
1147 snd_pcm_close( p_sys->p_alsa_pcm );
1148 p_sys->i_fd_audio = -1;
1151 if( p_sys->i_fd_audio >= 0 ) close( p_sys->i_fd_audio );
1153 if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
1154 free( p_sys->psz_device );
1155 free( p_sys->psz_vdev );
1156 free( p_sys->psz_adev );
1157 free( p_sys->p_standards );
1158 free( p_sys->p_inputs );
1159 free( p_sys->p_tuners );
1160 free( p_sys->p_codecs );
1161 free( p_sys->psz_requested_chroma );
1162 free( p_sys->psz_set_ctrls );
1167 /*****************************************************************************
1168 * AccessOpen: opens v4l2 device, access2 callback
1169 *****************************************************************************
1171 * url: <video device>::::
1173 *****************************************************************************/
1174 static int AccessOpen( vlc_object_t * p_this )
1176 access_t *p_access = (access_t*) p_this;
1177 demux_sys_t * p_sys;
1179 /* Only when selected */
1180 if( *p_access->psz_access == '\0' ) return VLC_EGENERIC;
1182 p_access->pf_read = AccessRead;
1183 p_access->pf_block = NULL;
1184 p_access->pf_seek = NULL;
1185 p_access->pf_control = AccessControl;
1186 p_access->info.i_update = 0;
1187 p_access->info.i_size = 0;
1188 p_access->info.i_pos = 0;
1189 p_access->info.b_eof = VLC_FALSE;
1190 p_access->info.i_title = 0;
1191 p_access->info.i_seekpoint = 0;
1193 p_sys = calloc( 1, sizeof( demux_sys_t ) );
1194 p_access->p_sys = (access_sys_t *) p_sys;
1195 if( p_sys == NULL ) return VLC_ENOMEM;
1197 GetV4L2Params( p_sys, (vlc_object_t *) p_access );
1199 ParseMRL( p_sys, p_access->psz_path, (vlc_object_t *) p_access );
1201 if( FindMainDevice( p_this, p_sys, FIND_VIDEO,
1202 VLC_FALSE, !strncmp( p_access->psz_access, "v4l2", 4 ) ) != VLC_SUCCESS )
1204 AccessClose( p_this );
1205 return VLC_EGENERIC;
1211 /*****************************************************************************
1213 *****************************************************************************/
1214 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
1216 demux_sys_t *p_sys = p_demux->p_sys;
1222 /* Special for access_demux */
1223 case DEMUX_CAN_PAUSE:
1224 case DEMUX_CAN_SEEK:
1225 case DEMUX_SET_PAUSE_STATE:
1226 case DEMUX_CAN_CONTROL_PACE:
1227 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1231 case DEMUX_GET_PTS_DELAY:
1232 pi64 = (int64_t*)va_arg( args, int64_t * );
1233 *pi64 = (int64_t)p_sys->i_pts * 1000;
1236 case DEMUX_GET_TIME:
1237 pi64 = (int64_t*)va_arg( args, int64_t * );
1241 /* TODO implement others */
1243 return VLC_EGENERIC;
1246 return VLC_EGENERIC;
1249 /*****************************************************************************
1250 * AccessControl: access2 callback
1251 *****************************************************************************/
1252 static int AccessControl( access_t *p_access, int i_query, va_list args )
1254 vlc_bool_t *pb_bool;
1257 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1262 case ACCESS_CAN_SEEK:
1263 case ACCESS_CAN_FASTSEEK:
1264 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1265 *pb_bool = VLC_FALSE;
1267 case ACCESS_CAN_PAUSE:
1268 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1269 *pb_bool = VLC_FALSE;
1271 case ACCESS_CAN_CONTROL_PACE:
1272 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1273 *pb_bool = VLC_FALSE;
1277 case ACCESS_GET_MTU:
1278 pi_int = (int*)va_arg( args, int * );
1282 case ACCESS_GET_PTS_DELAY:
1283 pi_64 = (int64_t*)va_arg( args, int64_t * );
1284 *pi_64 = (int64_t) p_sys->i_pts * 1000;
1288 case ACCESS_SET_PAUSE_STATE:
1292 case ACCESS_GET_TITLE_INFO:
1293 case ACCESS_SET_TITLE:
1294 case ACCESS_SET_SEEKPOINT:
1295 case ACCESS_SET_PRIVATE_ID_STATE:
1296 case ACCESS_GET_CONTENT_TYPE:
1297 case ACCESS_GET_META:
1298 return VLC_EGENERIC;
1301 msg_Warn( p_access, "Unimplemented query in control(%d).", i_query);
1302 return VLC_EGENERIC;
1308 /*****************************************************************************
1309 * AccessRead: access2 callback
1310 ******************************************************************************/
1311 static ssize_t AccessRead( access_t * p_access, uint8_t * p_buffer, size_t i_len )
1313 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1317 ufd.fd = p_sys->i_fd_video;
1318 ufd.events = POLLIN;
1320 if( p_access->info.b_eof )
1325 if( p_access->b_die )
1330 while( ( i_ret = poll( &ufd, 1, 500 ) ) == 0 );
1334 msg_Err( p_access, "Polling error (%m)." );
1338 i_ret = read( p_sys->i_fd_video, p_buffer, i_len );
1341 p_access->info.b_eof = VLC_TRUE;
1343 else if( i_ret > 0 )
1345 p_access->info.i_pos += i_ret;
1351 /*****************************************************************************
1352 * Demux: Processes the audio or video frame
1353 *****************************************************************************/
1354 static int Demux( demux_t *p_demux )
1356 demux_sys_t *p_sys = p_demux->p_sys;
1357 es_out_id_t *p_es = p_sys->p_es_audio;
1358 block_t *p_block = NULL;
1360 /* Try grabbing audio frames first */
1361 if( p_sys->i_fd_audio < 0 || !( p_block = GrabAudio( p_demux ) ) )
1363 /* Try grabbing video frame */
1364 p_es = p_sys->p_es_video;
1365 if( p_sys->i_fd_video > 0 ) p_block = GrabVideo( p_demux );
1370 /* Sleep so we do not consume all the cpu, 10ms seems
1371 * like a good value (100fps) */
1376 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
1377 es_out_Send( p_demux->out, p_es, p_block );
1382 /*****************************************************************************
1383 * GrabVideo: Grab a video frame
1384 *****************************************************************************/
1385 static block_t* GrabVideo( demux_t *p_demux )
1387 demux_sys_t *p_sys = p_demux->p_sys;
1389 block_t *p_block = NULL;
1390 struct v4l2_buffer buf;
1393 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1395 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1397 /* Did we wait long enough ? (frame rate reduction) */
1398 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1401 /* Grab Video Frame */
1404 case IO_METHOD_READ:
1405 i_ret = read( p_sys->i_fd_video, p_sys->p_buffers[0].start, p_sys->p_buffers[0].length );
1413 /* Could ignore EIO, see spec. */
1416 msg_Err( p_demux, "Failed to read frame" );
1421 p_block = ProcessVideoFrame( p_demux, (uint8_t*)p_sys->p_buffers[0].start, i_ret );
1422 if( !p_block ) return 0;
1426 case IO_METHOD_MMAP:
1427 memset( &buf, 0, sizeof(buf) );
1428 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1429 buf.memory = V4L2_MEMORY_MMAP;
1431 /* Wait for next frame */
1432 if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
1439 /* Could ignore EIO, see spec. */
1442 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1447 if( buf.index >= p_sys->i_nbuffers ) {
1448 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1452 p_block = ProcessVideoFrame( p_demux, p_sys->p_buffers[buf.index].start, buf.bytesused );
1453 if( !p_block ) return 0;
1456 if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
1458 msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
1464 case IO_METHOD_USERPTR:
1465 memset( &buf, 0, sizeof(buf) );
1466 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1467 buf.memory = V4L2_MEMORY_USERPTR;
1469 /* Wait for next frame */
1470 if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
1477 /* Could ignore EIO, see spec. */
1480 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1487 for( i = 0; i < p_sys->i_nbuffers; i++ )
1489 if( buf.m.userptr == (unsigned long)p_sys->p_buffers[i].start &&
1490 buf.length == p_sys->p_buffers[i].length ) break;
1493 if( i >= p_sys->i_nbuffers )
1495 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1499 p_block = ProcessVideoFrame( p_demux, (uint8_t*)buf.m.userptr, buf.bytesused );
1500 if( !p_block ) return 0;
1503 if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
1505 msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
1514 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();
1519 /*****************************************************************************
1520 * ProcessVideoFrame: Helper function to take a buffer and copy it into
1522 *****************************************************************************/
1523 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t i_size )
1527 if( !p_frame ) return 0;
1530 if( !( p_block = block_New( p_demux, i_size ) ) )
1532 msg_Warn( p_demux, "Cannot get new block" );
1537 memcpy( p_block->p_buffer, p_frame, i_size );
1542 /*****************************************************************************
1543 * GrabAudio: Grab an audio frame
1544 *****************************************************************************/
1545 static block_t* GrabAudio( demux_t *p_demux )
1547 demux_sys_t *p_sys = p_demux->p_sys;
1548 struct audio_buf_info buf_info;
1549 int i_read = 0, i_correct;
1552 if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
1553 else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
1557 msg_Warn( p_demux, "cannot get block" );
1561 p_sys->p_block_audio = p_block;
1564 if( p_sys->i_audio_method & AUDIO_METHOD_ALSA )
1567 i_read = snd_pcm_readi( p_sys->p_alsa_pcm, p_block->p_buffer, p_sys->i_alsa_chunk_size );
1577 snd_pcm_prepare( p_sys->p_alsa_pcm );
1581 i_resume = snd_pcm_resume( p_sys->p_alsa_pcm );
1582 if( i_resume < 0 && i_resume != -EAGAIN ) snd_pcm_prepare( p_sys->p_alsa_pcm );
1585 msg_Err( p_demux, "Failed to read alsa frame (%s)", snd_strerror( i_read ) );
1591 /* convert from frames to bytes */
1592 i_read *= p_sys->i_alsa_frame_size;
1597 if( p_sys->i_audio_method & AUDIO_METHOD_OSS )
1600 i_read = read( p_sys->i_fd_audio, p_block->p_buffer,
1601 p_sys->i_audio_max_frame_size );
1604 if( i_read <= 0 ) return 0;
1606 p_block->i_buffer = i_read;
1607 p_sys->p_block_audio = 0;
1609 /* Correct the date because of kernel buffering */
1611 if( p_sys->i_audio_method & AUDIO_METHOD_OSS )
1614 if( ioctl( p_sys->i_fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
1616 i_correct += buf_info.bytes;
1620 else if( p_sys->i_audio_method & AUDIO_METHOD_ALSA )
1624 snd_pcm_sframes_t delay = 0;
1625 if( ( i_err = snd_pcm_delay( p_sys->p_alsa_pcm, &delay ) ) >= 0 )
1627 size_t i_correction_delta = delay * p_sys->i_alsa_frame_size;
1628 /* Test for overrun */
1629 if( i_correction_delta > p_sys->i_audio_max_frame_size )
1631 msg_Warn( p_demux, "ALSA read overrun (%d > %d)",
1632 i_correction_delta, p_sys->i_audio_max_frame_size );
1633 i_correction_delta = p_sys->i_audio_max_frame_size;
1634 snd_pcm_prepare( p_sys->p_alsa_pcm );
1636 i_correct += i_correction_delta;
1640 /* delay failed so reset */
1641 msg_Warn( p_demux, "ALSA snd_pcm_delay failed (%s)", snd_strerror( i_err ) );
1642 snd_pcm_prepare( p_sys->p_alsa_pcm );
1648 p_block->i_pts = p_block->i_dts =
1649 mdate() - I64C(1000000) * (mtime_t)i_correct /
1650 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
1655 /*****************************************************************************
1656 * Helper function to initalise video IO using the Read method
1657 *****************************************************************************/
1658 static int InitRead( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1660 demux_sys_t *p_sys = p_demux->p_sys;
1662 p_sys->p_buffers = calloc( 1, sizeof( *p_sys->p_buffers ) );
1663 if( !p_sys->p_buffers )
1665 msg_Err( p_demux, "Out of memory" );
1669 p_sys->p_buffers[0].length = i_buffer_size;
1670 p_sys->p_buffers[0].start = malloc( i_buffer_size );
1671 if( !p_sys->p_buffers[0].start )
1673 msg_Err( p_demux, "Out of memory" );
1680 return VLC_EGENERIC;
1684 /*****************************************************************************
1685 * Helper function to initalise video IO using the mmap method
1686 *****************************************************************************/
1687 static int InitMmap( demux_t *p_demux, int i_fd )
1689 demux_sys_t *p_sys = p_demux->p_sys;
1690 struct v4l2_requestbuffers req;
1692 memset( &req, 0, sizeof(req) );
1694 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1695 req.memory = V4L2_MEMORY_MMAP;
1697 if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1699 msg_Err( p_demux, "device does not support mmap i/o" );
1705 msg_Err( p_demux, "Insufficient buffer memory" );
1709 p_sys->p_buffers = calloc( req.count, sizeof( *p_sys->p_buffers ) );
1710 if( !p_sys->p_buffers )
1712 msg_Err( p_demux, "Out of memory" );
1716 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < req.count; ++p_sys->i_nbuffers )
1718 struct v4l2_buffer buf;
1720 memset( &buf, 0, sizeof(buf) );
1721 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1722 buf.memory = V4L2_MEMORY_MMAP;
1723 buf.index = p_sys->i_nbuffers;
1725 if( ioctl( i_fd, VIDIOC_QUERYBUF, &buf ) < 0 )
1727 msg_Err( p_demux, "VIDIOC_QUERYBUF" );
1731 p_sys->p_buffers[p_sys->i_nbuffers].length = buf.length;
1732 p_sys->p_buffers[p_sys->i_nbuffers].start =
1733 mmap( NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, buf.m.offset );
1735 if( p_sys->p_buffers[p_sys->i_nbuffers].start == MAP_FAILED )
1737 msg_Err( p_demux, "mmap failed (%m)" );
1745 return VLC_EGENERIC;
1749 /*****************************************************************************
1750 * Helper function to initalise video IO using the userbuf method
1751 *****************************************************************************/
1752 static int InitUserP( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1754 demux_sys_t *p_sys = p_demux->p_sys;
1755 struct v4l2_requestbuffers req;
1756 unsigned int i_page_size;
1758 i_page_size = sysconf(_SC_PAGESIZE);
1759 i_buffer_size = ( i_buffer_size + i_page_size - 1 ) & ~( i_page_size - 1);
1761 memset( &req, 0, sizeof(req) );
1763 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1764 req.memory = V4L2_MEMORY_USERPTR;
1766 if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1768 msg_Err( p_demux, "device does not support user pointer i/o" );
1772 p_sys->p_buffers = calloc( 4, sizeof( *p_sys->p_buffers ) );
1773 if( !p_sys->p_buffers )
1775 msg_Err( p_demux, "Out of memory" );
1779 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < 4; ++p_sys->i_nbuffers )
1781 p_sys->p_buffers[p_sys->i_nbuffers].length = i_buffer_size;
1782 posix_memalign( &p_sys->p_buffers[p_sys->i_nbuffers].start,
1783 /* boundary */ i_page_size, i_buffer_size );
1785 if( !p_sys->p_buffers[p_sys->i_nbuffers].start )
1787 msg_Err( p_demux, "out of memory" );
1795 return VLC_EGENERIC;
1799 /*****************************************************************************
1800 * IsPixelFormatSupported: returns true if the specified V4L2 pixel format is
1801 * in the array of supported formats returned by the driver
1802 *****************************************************************************/
1803 static vlc_bool_t IsPixelFormatSupported( demux_t *p_demux, unsigned int i_pixelformat )
1805 demux_sys_t *p_sys = p_demux->p_sys;
1807 for( int i_index = 0; i_index < p_sys->i_codec; i_index++ )
1809 if( p_sys->p_codecs[i_index].pixelformat == i_pixelformat )
1816 /*****************************************************************************
1817 * OpenVideoDev: open and set up the video device and probe for capabilities
1818 *****************************************************************************/
1819 static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, vlc_bool_t b_demux )
1822 struct v4l2_cropcap cropcap;
1823 struct v4l2_crop crop;
1824 struct v4l2_format fmt;
1826 enum v4l2_buf_type buf_type;
1827 char *psz_device = p_sys->psz_vdev;
1829 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1831 msg_Err( p_obj, "cannot open device (%m)" );
1835 /* Tune the tuner */
1836 if( p_sys->i_frequency >= 0 )
1838 if( p_sys->i_cur_tuner < 0 || p_sys->i_cur_tuner >= p_sys->i_tuner )
1840 msg_Err( p_obj, "invalid tuner %d.", p_sys->i_cur_tuner );
1843 struct v4l2_frequency frequency;
1844 memset( &frequency, 0, sizeof( frequency ) );
1845 frequency.tuner = p_sys->i_cur_tuner;
1846 frequency.type = p_sys->p_tuners[p_sys->i_cur_tuner].type;
1847 frequency.frequency = p_sys->i_frequency / 62.5;
1848 if( ioctl( i_fd, VIDIOC_S_FREQUENCY, &frequency ) < 0 )
1850 msg_Err( p_obj, "cannot set tuner frequency (%m)" );
1853 msg_Dbg( p_obj, "Tuner frequency set" );
1856 /* Set the tuner's audio mode */
1857 if( p_sys->i_audio_mode >= 0 )
1859 if( p_sys->i_cur_tuner < 0 || p_sys->i_cur_tuner >= p_sys->i_tuner )
1861 msg_Err( p_obj, "invalid tuner %d.", p_sys->i_cur_tuner );
1864 struct v4l2_tuner tuner;
1865 memset( &tuner, 0, sizeof( tuner ) );
1866 tuner.index = p_sys->i_cur_tuner;
1867 tuner.audmode = p_sys->i_audio_mode;
1868 if( ioctl( i_fd, VIDIOC_S_TUNER, &tuner ) < 0 )
1870 msg_Err( p_obj, "cannot set tuner audio mode (%m)" );
1873 msg_Dbg( p_obj, "Tuner audio mode set" );
1876 /* Select standard */
1878 if( p_sys->i_selected_standard_id != V4L2_STD_UNKNOWN )
1880 if( ioctl( i_fd, VIDIOC_S_STD, &p_sys->i_selected_standard_id ) < 0 )
1882 msg_Err( p_obj, "cannot set standard (%m)" );
1885 msg_Dbg( p_obj, "Set standard" );
1890 if( p_sys->i_selected_input >= p_sys->i_input )
1892 msg_Warn( p_obj, "invalid input. Using the default one" );
1893 p_sys->i_selected_input = 0;
1896 if( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
1898 msg_Err( p_obj, "cannot set input (%m)" );
1902 /* Set audio input */
1904 if( p_sys->i_audio > 0 )
1906 if( p_sys->i_selected_audio_input < 0
1907 || p_sys->i_selected_audio_input >= p_sys->i_audio )
1909 msg_Warn( p_obj, "invalid audio input. Using the default one" );
1910 p_sys->i_selected_audio_input = 0;
1913 if( ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_selected_audio_input] ) < 0 )
1915 msg_Err( p_obj, "cannot set audio input (%m)" );
1918 msg_Dbg( p_obj, "Audio input set to %d", p_sys->i_selected_audio_input );
1922 /* TODO: Move the resolution stuff up here */
1923 /* if MPEG encoder card, no need to do anything else after this */
1924 ControlList( p_obj, i_fd,
1925 var_GetBool( p_obj, "v4l2-controls-reset" ), b_demux );
1926 SetAvailControlsByString( p_obj, p_sys, i_fd );
1927 if( VLC_FALSE == b_demux)
1932 demux_t *p_demux = (demux_t *) p_obj;
1934 /* Verify device support for the various IO methods */
1937 case IO_METHOD_READ:
1938 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE) )
1940 msg_Err( p_demux, "device does not support read i/o" );
1945 case IO_METHOD_MMAP:
1946 case IO_METHOD_USERPTR:
1947 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING) )
1949 msg_Err( p_demux, "device does not support streaming i/o" );
1955 msg_Err( p_demux, "io method not supported" );
1959 /* Reset Cropping */
1960 memset( &cropcap, 0, sizeof(cropcap) );
1961 cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1962 if( ioctl( i_fd, VIDIOC_CROPCAP, &cropcap ) >= 0 )
1964 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1965 crop.c = cropcap.defrect; /* reset to default */
1966 if( ioctl( i_fd, VIDIOC_S_CROP, &crop ) < 0 )
1971 /* Cropping not supported. */
1974 /* Errors ignored. */
1980 /* Try and find default resolution if not specified */
1981 memset( &fmt, 0, sizeof(fmt) );
1982 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1984 if( p_sys->i_width <= 0 || p_sys->i_height <= 0 )
1986 if( ioctl( i_fd, VIDIOC_G_FMT, &fmt ) < 0 )
1988 msg_Err( p_demux, "Cannot get default width and height." );
1992 p_sys->i_width = fmt.fmt.pix.width;
1993 p_sys->i_height = fmt.fmt.pix.height;
1995 if( fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE )
1997 p_sys->i_height = p_sys->i_height * 2;
2002 msg_Dbg( p_demux, "trying specified size %dx%d", p_sys->i_width, p_sys->i_height );
2005 fmt.fmt.pix.width = p_sys->i_width;
2006 fmt.fmt.pix.height = p_sys->i_height;
2007 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2009 /* Test and set Chroma */
2010 fmt.fmt.pix.pixelformat = 0;
2011 if( p_sys->psz_requested_chroma && strlen( p_sys->psz_requested_chroma ) > 0 )
2013 /* User specified chroma */
2014 if( strlen( p_sys->psz_requested_chroma ) >= 4 )
2016 int i_requested_fourcc = VLC_FOURCC(
2017 p_sys->psz_requested_chroma[0], p_sys->psz_requested_chroma[1],
2018 p_sys->psz_requested_chroma[2], p_sys->psz_requested_chroma[3] );
2019 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
2021 if( v4l2chroma_to_fourcc[i].i_fourcc == i_requested_fourcc )
2023 fmt.fmt.pix.pixelformat = v4l2chroma_to_fourcc[i].i_v4l2;
2028 /* Try and set user chroma */
2029 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ( fmt.fmt.pix.pixelformat && ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 ) )
2031 msg_Warn( p_demux, "Driver is unable to use specified chroma %s. Trying defaults.", p_sys->psz_requested_chroma );
2032 fmt.fmt.pix.pixelformat = 0;
2036 /* If no user specified chroma, find best */
2037 /* This also decides if MPEG encoder card or not */
2038 if( !fmt.fmt.pix.pixelformat )
2041 for( i = 0; i < ARRAY_SIZE( p_chroma_fallbacks ); i++ )
2043 fmt.fmt.pix.pixelformat = p_chroma_fallbacks[i];
2044 if( IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat )
2045 && ioctl( i_fd, VIDIOC_S_FMT, &fmt ) >= 0 )
2048 if( i == ARRAY_SIZE( p_chroma_fallbacks ) )
2050 msg_Warn( p_demux, "Could not select any of the default chromas; attempting to open as MPEG encoder card (access2)" );
2055 /* Reassign width, height and chroma incase driver override */
2056 p_sys->i_width = fmt.fmt.pix.width;
2057 p_sys->i_height = fmt.fmt.pix.height;
2059 /* Look up final fourcc */
2060 p_sys->i_fourcc = 0;
2061 for( int i = 0; v4l2chroma_to_fourcc[i].i_fourcc != 0; i++ )
2063 if( v4l2chroma_to_fourcc[i].i_v4l2 == fmt.fmt.pix.pixelformat )
2065 p_sys->i_fourcc = v4l2chroma_to_fourcc[i].i_fourcc;
2070 /* Buggy driver paranoia */
2071 i_min = fmt.fmt.pix.width * 2;
2072 if( fmt.fmt.pix.bytesperline < i_min )
2073 fmt.fmt.pix.bytesperline = i_min;
2074 i_min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
2075 if( fmt.fmt.pix.sizeimage < i_min )
2076 fmt.fmt.pix.sizeimage = i_min;
2078 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
2079 /* This is new in Linux 2.6.19 */
2080 /* List supported frame rates */
2081 struct v4l2_frmivalenum frmival;
2083 frmival.pixel_format = fmt.fmt.pix.pixelformat;
2084 frmival.width = p_sys->i_width;
2085 frmival.height = p_sys->i_height;
2086 if( ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 )
2089 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
2090 vlc_fourcc_to_char( p_sys->i_fourcc, &psz_fourcc );
2091 msg_Dbg( p_demux, "supported frame intervals for %4s, %dx%d:",
2092 psz_fourcc, frmival.width, frmival.height );
2093 switch( frmival.type )
2095 case V4L2_FRMIVAL_TYPE_DISCRETE:
2098 msg_Dbg( p_demux, " supported frame interval: %d/%d",
2099 frmival.discrete.numerator,
2100 frmival.discrete.denominator );
2102 } while( ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 );
2104 case V4L2_FRMIVAL_TYPE_STEPWISE:
2105 msg_Dbg( p_demux, " supported frame intervals: %d/%d to "
2106 "%d/%d using %d/%d increments",
2107 frmival.stepwise.min.numerator,
2108 frmival.stepwise.min.denominator,
2109 frmival.stepwise.max.numerator,
2110 frmival.stepwise.max.denominator,
2111 frmival.stepwise.step.numerator,
2112 frmival.stepwise.step.denominator );
2114 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
2115 msg_Dbg( p_demux, " supported frame intervals: %d/%d to %d/%d",
2116 frmival.stepwise.min.numerator,
2117 frmival.stepwise.min.denominator,
2118 frmival.stepwise.max.numerator,
2119 frmival.stepwise.max.denominator );
2125 /* Init IO method */
2128 case IO_METHOD_READ:
2129 if( InitRead( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
2132 case IO_METHOD_MMAP:
2133 if( InitMmap( p_demux, i_fd ) != VLC_SUCCESS ) goto open_failed;
2136 case IO_METHOD_USERPTR:
2137 if( InitUserP( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
2144 es_format_Init( &es_fmt, VIDEO_ES, p_sys->i_fourcc );
2145 es_fmt.video.i_width = p_sys->i_width;
2146 es_fmt.video.i_height = p_sys->i_height;
2147 es_fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
2149 /* Setup rgb mask for RGB formats */
2150 if( p_sys->i_fourcc == VLC_FOURCC( 'R','V','2','4' ) )
2152 /* This is in BGR format */
2153 es_fmt.video.i_bmask = 0x00ff0000;
2154 es_fmt.video.i_gmask = 0x0000ff00;
2155 es_fmt.video.i_rmask = 0x000000ff;
2158 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
2159 (char*)&es_fmt.i_codec, es_fmt.video.i_width, es_fmt.video.i_height );
2160 p_sys->p_es_video = es_out_Add( p_demux->out, &es_fmt );
2166 case IO_METHOD_READ:
2170 case IO_METHOD_MMAP:
2171 for (unsigned int i = 0; i < p_sys->i_nbuffers; ++i)
2173 struct v4l2_buffer buf;
2175 memset( &buf, 0, sizeof(buf) );
2176 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2177 buf.memory = V4L2_MEMORY_MMAP;
2180 if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
2182 msg_Err( p_demux, "VIDIOC_QBUF failed" );
2187 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2188 if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
2190 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
2196 case IO_METHOD_USERPTR:
2197 for( unsigned int i = 0; i < p_sys->i_nbuffers; ++i )
2199 struct v4l2_buffer buf;
2201 memset( &buf, 0, sizeof(buf) );
2202 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2203 buf.memory = V4L2_MEMORY_USERPTR;
2205 buf.m.userptr = (unsigned long)p_sys->p_buffers[i].start;
2206 buf.length = p_sys->p_buffers[i].length;
2208 if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
2210 msg_Err( p_demux, "VIDIOC_QBUF failed" );
2215 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2216 if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
2218 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
2226 if( p_sys->f_fps >= 0.1 )
2228 msg_Dbg( p_demux, "User set fps=%f", p_sys->f_fps );
2234 if( i_fd >= 0 ) close( i_fd );
2240 /*****************************************************************************
2241 * ResolveALSADeviceName: Change any . to : in the ALSA device name
2242 *****************************************************************************/
2243 static char *ResolveALSADeviceName( const char *psz_device )
2245 char* psz_alsa_name = strdup( psz_device );
2246 for( unsigned int i = 0; i < strlen( psz_device ); i++ )
2248 if( psz_alsa_name[i] == '.' ) psz_alsa_name[i] = ':';
2250 return psz_alsa_name;
2254 /*****************************************************************************
2255 * OpenAudioDev: open and set up the audio device and probe for capabilities
2256 *****************************************************************************/
2258 static int OpenAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys,
2259 vlc_bool_t b_demux )
2261 char *psz_device = p_sys->psz_adev;
2263 p_sys->p_alsa_pcm = NULL;
2264 char* psz_alsa_device_name = NULL;
2265 snd_pcm_hw_params_t *p_hw_params = NULL;
2266 snd_pcm_uframes_t buffer_size;
2267 snd_pcm_uframes_t chunk_size;
2271 psz_alsa_device_name =
2272 ResolveALSADeviceName( psz_device?: ALSA_DEFAULT );
2274 if( ( i_err = snd_pcm_open( &p_sys->p_alsa_pcm, psz_alsa_device_name,
2275 SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0)
2277 msg_Err( p_this, "Cannot open ALSA audio device %s (%s)",
2278 psz_alsa_device_name, snd_strerror( i_err ) );
2282 if( ( i_err = snd_pcm_nonblock( p_sys->p_alsa_pcm, 1 ) ) < 0)
2284 msg_Err( p_this, "Cannot set ALSA nonblock (%s)",
2285 snd_strerror( i_err ) );
2289 /* Begin setting hardware parameters */
2291 if( ( i_err = snd_pcm_hw_params_malloc( &p_hw_params ) ) < 0 )
2294 "ALSA: cannot allocate hardware parameter structure (%s)",
2295 snd_strerror( i_err ) );
2299 if( ( i_err = snd_pcm_hw_params_any( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
2302 "ALSA: cannot initialize hardware parameter structure (%s)",
2303 snd_strerror( i_err ) );
2307 /* Set Interleaved access */
2308 if( ( i_err = snd_pcm_hw_params_set_access( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 )
2310 msg_Err( p_this, "ALSA: cannot set access type (%s)",
2311 snd_strerror( i_err ) );
2315 /* Set 16 bit little endian */
2316 if( ( i_err = snd_pcm_hw_params_set_format( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_FORMAT_S16_LE ) ) < 0 )
2318 msg_Err( p_this, "ALSA: cannot set sample format (%s)",
2319 snd_strerror( i_err ) );
2323 /* Set sample rate */
2324 #ifdef HAVE_ALSA_NEW_API
2325 i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, &p_sys->i_sample_rate, NULL );
2327 i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, p_sys->i_sample_rate, NULL );
2331 msg_Err( p_this, "ALSA: cannot set sample rate (%s)",
2332 snd_strerror( i_err ) );
2337 unsigned int channels = p_sys->b_stereo ? 2 : 1;
2338 if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
2340 channels = ( channels==1 ) ? 2 : 1;
2341 msg_Warn( p_this, "ALSA: cannot set channel count (%s). "
2342 "Trying with channels=%d",
2343 snd_strerror( i_err ),
2345 if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
2347 msg_Err( p_this, "ALSA: cannot set channel count (%s)",
2348 snd_strerror( i_err ) );
2351 p_sys->b_stereo = ( channels == 2 );
2354 /* Set metrics for buffer calculations later */
2355 unsigned int buffer_time;
2356 if( ( i_err = snd_pcm_hw_params_get_buffer_time_max(p_hw_params, &buffer_time, 0) ) < 0 )
2358 msg_Err( p_this, "ALSA: cannot get buffer time max (%s)",
2359 snd_strerror( i_err ) );
2362 if (buffer_time > 500000) buffer_time = 500000;
2364 /* Set period time */
2365 unsigned int period_time = buffer_time / 4;
2366 #ifdef HAVE_ALSA_NEW_API
2367 i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, &period_time, 0 );
2369 i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, period_time, 0 );
2373 msg_Err( p_this, "ALSA: cannot set period time (%s)",
2374 snd_strerror( i_err ) );
2378 /* Set buffer time */
2379 #ifdef HAVE_ALSA_NEW_API
2380 i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, &buffer_time, 0 );
2382 i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, buffer_time, 0 );
2386 msg_Err( p_this, "ALSA: cannot set buffer time (%s)",
2387 snd_strerror( i_err ) );
2391 /* Apply new hardware parameters */
2392 if( ( i_err = snd_pcm_hw_params( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
2394 msg_Err( p_this, "ALSA: cannot set hw parameters (%s)",
2395 snd_strerror( i_err ) );
2399 /* Get various buffer metrics */
2400 snd_pcm_hw_params_get_period_size( p_hw_params, &chunk_size, 0 );
2401 snd_pcm_hw_params_get_buffer_size( p_hw_params, &buffer_size );
2402 if( chunk_size == buffer_size )
2405 "ALSA: period cannot equal buffer size (%lu == %lu)",
2406 chunk_size, buffer_size);
2410 int bits_per_sample = snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE);
2411 int bits_per_frame = bits_per_sample * channels;
2413 p_sys->i_alsa_chunk_size = chunk_size;
2414 p_sys->i_alsa_frame_size = bits_per_frame / 8;
2415 p_sys->i_audio_max_frame_size = chunk_size * bits_per_frame / 8;
2417 snd_pcm_hw_params_free( p_hw_params );
2421 if( ( i_err = snd_pcm_prepare( p_sys->p_alsa_pcm ) ) < 0 )
2424 "ALSA: cannot prepare audio interface for use (%s)",
2425 snd_strerror( i_err ) );
2429 /* Return a fake handle so other tests work */
2432 free( psz_alsa_device_name );
2434 if( !p_sys->psz_adev )
2435 p_sys->psz_adev = strdup( ALSA_DEFAULT );
2440 if( i_fd >= 0 ) close( i_fd );
2442 if( p_hw_params ) snd_pcm_hw_params_free( p_hw_params );
2443 if( p_sys->p_alsa_pcm ) snd_pcm_close( p_sys->p_alsa_pcm );
2444 free( psz_alsa_device_name );
2451 static int OpenAudioDevOss( vlc_object_t *p_this, demux_sys_t *p_sys,
2452 vlc_bool_t b_demux )
2457 char* psz_oss_device_name = strdup( p_sys->psz_adev ? p_sys->psz_adev : OSS_DEFAULT );
2459 if( (i_fd = open( psz_oss_device_name, O_RDONLY | O_NONBLOCK )) < 0 )
2461 msg_Err( p_this, "cannot open OSS audio device (%m)" );
2465 i_format = AFMT_S16_LE;
2466 if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
2467 || i_format != AFMT_S16_LE )
2470 "cannot set audio format (16b little endian) (%m)" );
2474 if( ioctl( i_fd, SNDCTL_DSP_STEREO,
2475 &p_sys->b_stereo ) < 0 )
2477 msg_Err( p_this, "cannot set audio channels count (%m)" );
2481 if( ioctl( i_fd, SNDCTL_DSP_SPEED,
2482 &p_sys->i_sample_rate ) < 0 )
2484 msg_Err( p_this, "cannot set audio sample rate (%m)" );
2488 p_sys->i_audio_max_frame_size = 6 * 1024;
2490 if( !p_sys->psz_adev )
2491 p_sys->psz_adev = strdup( OSS_DEFAULT );
2492 free( psz_oss_device_name );
2497 free( psz_oss_device_name );
2498 if( i_fd >= 0 ) close( i_fd );
2503 static int OpenAudioDev( vlc_object_t *p_this, demux_sys_t *p_sys,
2504 vlc_bool_t b_demux )
2510 if( ( p_sys->i_audio_method & AUDIO_METHOD_ALSA ) && i_fd < 0 )
2511 i_fd = OpenAudioDevAlsa( p_this, p_sys, b_demux );
2514 if( ( p_sys->i_audio_method & AUDIO_METHOD_OSS ) && i_fd < 0 )
2515 i_fd = OpenAudioDevOss( p_this, p_sys, b_demux );
2520 psz_device = p_sys->psz_adev;
2522 msg_Dbg( p_this, "opened adev=`%s' %s %dHz",
2523 psz_device, p_sys->b_stereo ? "stereo" : "mono",
2524 p_sys->i_sample_rate );
2527 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
2529 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
2530 fmt.audio.i_rate = p_sys->i_sample_rate;
2531 fmt.audio.i_bitspersample = 16;
2532 fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
2533 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
2535 msg_Dbg( p_this, "new audio es %d channels %dHz",
2536 fmt.audio.i_channels, fmt.audio.i_rate );
2540 demux_t *p_demux = (demux_t *)p_this;
2541 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
2547 /*****************************************************************************
2548 * ProbeVideoDev: probe video for capabilities
2549 *****************************************************************************/
2550 static vlc_bool_t ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
2558 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
2560 msg_Err( p_obj, "cannot open video device (%m)" );
2564 /* Get device capabilites */
2566 if( ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
2568 msg_Err( p_obj, "cannot get video capabilities (%m)" );
2572 msg_Dbg( p_obj, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
2573 p_sys->dev_cap.card,
2574 p_sys->dev_cap.driver,
2575 (p_sys->dev_cap.version >> 16) & 0xFF,
2576 (p_sys->dev_cap.version >> 8) & 0xFF,
2577 p_sys->dev_cap.version & 0xFF,
2578 p_sys->dev_cap.bus_info );
2580 msg_Dbg( p_obj, "the device has the capabilities: (%c) Video Capure, "
2583 ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE ? 'X':' '),
2584 ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO ? 'X':' '),
2585 ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER ? 'X':' ') );
2587 msg_Dbg( p_obj, "supported I/O methods are: (%c) Read/Write, "
2589 "(%c) Asynchronous",
2590 ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
2591 ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
2592 ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
2594 /* Now, enumerate all the video inputs. This is useless at the moment
2595 since we have no way to present that info to the user except with
2598 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2600 struct v4l2_input t_input;
2602 while( ioctl( i_fd, VIDIOC_ENUMINPUT, &t_input ) >= 0 )
2605 t_input.index = p_sys->i_input;
2608 p_sys->p_inputs = calloc( 1, p_sys->i_input * sizeof( struct v4l2_input ) );
2609 if( !p_sys->p_inputs ) goto open_failed;
2611 for( i_index = 0; i_index < p_sys->i_input; i_index++ )
2613 p_sys->p_inputs[i_index].index = i_index;
2615 if( ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
2617 msg_Err( p_obj, "cannot get video input characteristics (%m)" );
2620 msg_Dbg( p_obj, "video input %i (%s) has type: %s %c",
2622 p_sys->p_inputs[i_index].name,
2623 p_sys->p_inputs[i_index].type
2624 == V4L2_INPUT_TYPE_TUNER ?
2626 "External analog input",
2627 i_index == p_sys->i_selected_input ? '*' : ' ' );
2631 /* Probe video standards */
2632 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2634 struct v4l2_standard t_standards;
2635 t_standards.index = 0;
2636 while( ioctl( i_fd, VIDIOC_ENUMSTD, &t_standards ) >=0 )
2638 p_sys->i_standard++;
2639 t_standards.index = p_sys->i_standard;
2642 p_sys->p_standards = calloc( 1, p_sys->i_standard * sizeof( struct v4l2_standard ) );
2643 if( !p_sys->p_standards ) goto open_failed;
2645 for( i_standard = 0; i_standard < p_sys->i_standard; i_standard++ )
2647 p_sys->p_standards[i_standard].index = i_standard;
2649 if( ioctl( i_fd, VIDIOC_ENUMSTD, &p_sys->p_standards[i_standard] ) )
2651 msg_Err( p_obj, "cannot get video input standards (%m)" );
2654 msg_Dbg( p_obj, "video standard %i is: %s %c",
2656 p_sys->p_standards[i_standard].name,
2657 (unsigned)i_standard == p_sys->i_selected_standard_id ? '*' : ' ' );
2661 /* initialize the structures for the ioctls */
2662 for( i_index = 0; i_index < 32; i_index++ )
2664 p_sys->p_audios[i_index].index = i_index;
2667 /* Probe audio inputs */
2668 if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
2670 while( p_sys->i_audio < 32 &&
2671 ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
2673 if( ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
2675 msg_Err( p_obj, "cannot get audio input characteristics (%m)" );
2679 msg_Dbg( p_obj, "audio input %i (%s) is %s %s %c",
2681 p_sys->p_audios[p_sys->i_audio].name,
2682 p_sys->p_audios[p_sys->i_audio].capability &
2683 V4L2_AUDCAP_STEREO ?
2685 p_sys->p_audios[p_sys->i_audio].capability &
2687 "(Automatic Volume Level supported)" : "",
2688 p_sys->i_audio == p_sys->i_selected_audio_input ? '*' : ' ' );
2694 /* List tuner caps */
2695 if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
2697 struct v4l2_tuner tuner;
2698 memset( &tuner, 0, sizeof(tuner) );
2699 while( ioctl( i_fd, VIDIOC_G_TUNER, &tuner ) >= 0 )
2702 memset( &tuner, 0, sizeof(tuner) );
2703 tuner.index = p_sys->i_tuner;
2706 p_sys->p_tuners = calloc( 1, p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
2707 if( !p_sys->p_tuners ) goto open_failed;
2709 for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
2711 p_sys->p_tuners[i_index].index = i_index;
2713 if( ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
2715 msg_Err( p_obj, "cannot get tuner characteristics (%m)" );
2718 msg_Dbg( p_obj, "tuner %i (%s) has type: %s, "
2719 "frequency range: %.1f %s -> %.1f %s",
2721 p_sys->p_tuners[i_index].name,
2722 p_sys->p_tuners[i_index].type
2723 == V4L2_TUNER_RADIO ?
2724 "Radio" : "Analog TV",
2725 p_sys->p_tuners[i_index].rangelow * 62.5,
2726 p_sys->p_tuners[i_index].capability &
2727 V4L2_TUNER_CAP_LOW ?
2729 p_sys->p_tuners[i_index].rangehigh * 62.5,
2730 p_sys->p_tuners[i_index].capability &
2731 V4L2_TUNER_CAP_LOW ?
2734 struct v4l2_frequency frequency;
2735 memset( &frequency, 0, sizeof( frequency ) );
2736 if( ioctl( i_fd, VIDIOC_G_FREQUENCY, &frequency ) < 0 )
2738 msg_Err( p_obj, "cannot get tuner frequency (%m)" );
2741 msg_Dbg( p_obj, "tuner %i (%s) frequency: %.1f %s",
2743 p_sys->p_tuners[i_index].name,
2744 frequency.frequency * 62.5,
2745 p_sys->p_tuners[i_index].capability &
2746 V4L2_TUNER_CAP_LOW ?
2751 /* Probe for available chromas */
2752 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2754 struct v4l2_fmtdesc codec;
2757 memset( &codec, 0, sizeof(codec) );
2758 codec.index = i_index;
2759 codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2761 while( ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
2764 codec.index = i_index;
2767 p_sys->i_codec = i_index;
2769 p_sys->p_codecs = calloc( 1, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
2771 for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
2773 p_sys->p_codecs[i_index].index = i_index;
2774 p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2776 if( ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
2778 msg_Err( p_obj, "cannot get codec description (%m)" );
2782 /* only print if vlc supports the format */
2783 char psz_fourcc_v4l2[5];
2784 memset( &psz_fourcc_v4l2, 0, sizeof( psz_fourcc_v4l2 ) );
2785 vlc_fourcc_to_char( p_sys->p_codecs[i_index].pixelformat,
2787 vlc_bool_t b_codec_supported = VLC_FALSE;
2788 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
2790 if( v4l2chroma_to_fourcc[i].i_v4l2 == p_sys->p_codecs[i_index].pixelformat )
2792 b_codec_supported = VLC_TRUE;
2795 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
2796 vlc_fourcc_to_char( v4l2chroma_to_fourcc[i].i_fourcc,
2798 msg_Dbg( p_obj, "device supports chroma %4s [%s, %s]",
2800 p_sys->p_codecs[i_index].description,
2803 #ifdef VIDIOC_ENUM_FRAMESIZES
2804 /* This is new in Linux 2.6.19 */
2805 /* List valid frame sizes for this format */
2806 struct v4l2_frmsizeenum frmsize;
2808 frmsize.pixel_format = p_sys->p_codecs[i_index].pixelformat;
2809 if( ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) < 0 )
2811 /* Not all devices support this ioctl */
2812 msg_Warn( p_obj, "Unable to query for frame sizes" );
2816 switch( frmsize.type )
2818 case V4L2_FRMSIZE_TYPE_DISCRETE:
2822 " device supports size %dx%d",
2823 frmsize.discrete.width, frmsize.discrete.height );
2825 } while( ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 );
2827 case V4L2_FRMSIZE_TYPE_STEPWISE:
2829 " device supports sizes %dx%d to %dx%d using %dx%d increments",
2830 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2831 frmsize.stepwise.max_width, frmsize.stepwise.max_height,
2832 frmsize.stepwise.step_width, frmsize.stepwise.step_height );
2834 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
2836 " device supports all sizes %dx%d to %dx%d",
2837 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2838 frmsize.stepwise.max_width, frmsize.stepwise.max_height );
2845 if( !b_codec_supported )
2848 "device codec %4s (%s) not supported as access_demux",
2850 p_sys->p_codecs[i_index].description );
2857 if( i_fd >= 0 ) close( i_fd );
2862 if( i_fd >= 0 ) close( i_fd );
2867 /*****************************************************************************
2868 * ProbeAudioDev: probe audio for capabilities
2869 *****************************************************************************/
2871 static vlc_bool_t ProbeAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys,
2875 snd_pcm_t *p_alsa_pcm;
2876 char* psz_alsa_device_name = ResolveALSADeviceName( psz_device ?: ALSA_DEFAULT );
2878 if( ( i_err = snd_pcm_open( &p_alsa_pcm, psz_alsa_device_name, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0 )
2880 msg_Err( p_this, "cannot open device %s for ALSA audio (%s)", psz_alsa_device_name, snd_strerror( i_err ) );
2881 free( psz_alsa_device_name );
2885 snd_pcm_close( p_alsa_pcm );
2886 free( psz_alsa_device_name );
2892 static vlc_bool_t ProbeAudioDevOss( vlc_object_t *p_this, demux_sys_t *p_sys,
2897 char* psz_oss_device_name = strdup( psz_device ? psz_device : OSS_DEFAULT );
2899 if( ( i_fd = open( psz_oss_device_name, O_RDONLY | O_NONBLOCK ) ) < 0 )
2901 msg_Err( p_this, "cannot open device %s for OSS audio (%m)", psz_oss_device_name );
2905 /* this will fail if the device is video */
2906 if( ioctl( i_fd, SNDCTL_DSP_GETCAPS, &i_caps ) < 0 )
2908 msg_Err( p_this, "cannot get audio caps (%m)" );
2912 free( psz_oss_device_name );
2913 if( i_fd >= 0 ) close( i_fd );
2918 free( psz_oss_device_name );
2919 if( i_fd >= 0 ) close( i_fd );
2923 static vlc_bool_t ProbeAudioDev( vlc_object_t *p_this, demux_sys_t *p_sys,
2927 if( ( p_sys->i_audio_method & AUDIO_METHOD_ALSA )
2928 && ProbeAudioDevAlsa( p_this, p_sys, psz_device ) )
2930 p_sys->i_audio_method = AUDIO_METHOD_ALSA;
2935 if( ( p_sys->i_audio_method & AUDIO_METHOD_OSS )
2936 && ProbeAudioDevOss( p_this, p_sys, psz_device ) )
2938 p_sys->i_audio_method = AUDIO_METHOD_OSS;
2942 p_sys->i_audio_method = 0;
2946 static void name2var( unsigned char *name )
2948 for( ; *name; name++ )
2949 *name = (*name == ' ') ? '_' : tolower( *name );
2952 /*****************************************************************************
2953 * Print a user-class v4l2 control's details, create the relevant variable,
2954 * change the value if needed.
2955 *****************************************************************************/
2956 static void ControlListPrint( vlc_object_t *p_obj, int i_fd,
2957 struct v4l2_queryctrl queryctrl,
2958 vlc_bool_t b_reset, vlc_bool_t b_demux )
2960 struct v4l2_querymenu querymenu;
2967 vlc_value_t val, val2;
2969 if( queryctrl.flags & V4L2_CTRL_FLAG_GRABBED )
2970 msg_Dbg( p_obj, " control is busy" );
2971 if( queryctrl.flags & V4L2_CTRL_FLAG_READ_ONLY )
2972 msg_Dbg( p_obj, " control is read-only" );
2974 for( i = 0; controls[i].psz_name != NULL; i++ )
2975 if( controls[i].i_cid == queryctrl.id ) break;
2977 if( controls[i].psz_name )
2979 psz_name = strdup( controls[i].psz_name );
2980 char psz_cfg_name[40];
2981 sprintf( psz_cfg_name, CFG_PREFIX "%s", psz_name );
2982 i_val = var_CreateGetInteger( p_obj, psz_cfg_name );
2983 var_Destroy( p_obj, psz_cfg_name );
2987 psz_name = strdup( (const char *)queryctrl.name );
2988 name2var( (unsigned char *)psz_name );
2992 switch( queryctrl.type )
2994 case V4L2_CTRL_TYPE_INTEGER:
2995 msg_Dbg( p_obj, " integer control" );
2997 " valid values: %d to %d by steps of %d",
2998 queryctrl.minimum, queryctrl.maximum,
3001 var_Create( p_obj, psz_name,
3002 VLC_VAR_INTEGER | VLC_VAR_HASMIN | VLC_VAR_HASMAX
3003 | VLC_VAR_HASSTEP | VLC_VAR_ISCOMMAND );
3004 val.i_int = queryctrl.minimum;
3005 var_Change( p_obj, psz_name, VLC_VAR_SETMIN, &val, NULL );
3006 val.i_int = queryctrl.maximum;
3007 var_Change( p_obj, psz_name, VLC_VAR_SETMAX, &val, NULL );
3008 val.i_int = queryctrl.step;
3009 var_Change( p_obj, psz_name, VLC_VAR_SETSTEP, &val, NULL );
3011 case V4L2_CTRL_TYPE_BOOLEAN:
3012 msg_Dbg( p_obj, " boolean control" );
3013 var_Create( p_obj, psz_name,
3014 VLC_VAR_BOOL | VLC_VAR_ISCOMMAND );
3016 case V4L2_CTRL_TYPE_MENU:
3017 msg_Dbg( p_obj, " menu control" );
3018 var_Create( p_obj, psz_name,
3019 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE
3020 | VLC_VAR_ISCOMMAND );
3021 memset( &querymenu, 0, sizeof( querymenu ) );
3022 for( i_mid = queryctrl.minimum;
3023 i_mid <= (unsigned)queryctrl.maximum;
3026 querymenu.index = i_mid;
3027 querymenu.id = queryctrl.id;
3028 if( ioctl( i_fd, VIDIOC_QUERYMENU, &querymenu ) >= 0 )
3030 msg_Dbg( p_obj, " %d: %s",
3031 querymenu.index, querymenu.name );
3032 val.i_int = querymenu.index;
3033 val2.psz_string = (char *)querymenu.name;
3034 var_Change( p_obj, psz_name,
3035 VLC_VAR_ADDCHOICE, &val, &val2 );
3039 case V4L2_CTRL_TYPE_BUTTON:
3040 msg_Dbg( p_obj, " button control" );
3041 var_Create( p_obj, psz_name,
3042 VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
3044 case V4L2_CTRL_TYPE_CTRL_CLASS:
3045 msg_Dbg( p_obj, " control class" );
3046 var_Create( p_obj, psz_name, VLC_VAR_VOID );
3049 msg_Dbg( p_obj, " unknown control type (FIXME)" );
3054 switch( queryctrl.type )
3056 case V4L2_CTRL_TYPE_INTEGER:
3057 case V4L2_CTRL_TYPE_BOOLEAN:
3058 case V4L2_CTRL_TYPE_MENU:
3060 struct v4l2_control control;
3061 msg_Dbg( p_obj, " default value: %d",
3062 queryctrl.default_value );
3063 memset( &control, 0, sizeof( control ) );
3064 control.id = queryctrl.id;
3065 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
3067 msg_Dbg( p_obj, " current value: %d", control.value );
3071 i_val = control.value;
3072 if( b_reset && queryctrl.default_value != control.value )
3074 msg_Dbg( p_obj, " reset value to default" );
3075 Control( p_obj, i_fd, psz_name,
3076 queryctrl.id, queryctrl.default_value );
3081 Control( p_obj, i_fd, psz_name,
3082 queryctrl.id, i_val );
3090 val.psz_string = (char *)queryctrl.name;
3091 var_Change( p_obj, psz_name, VLC_VAR_SETTEXT, &val, NULL );
3092 val.i_int = queryctrl.id;
3093 val2.psz_string = (char *)psz_name;
3094 var_Change( p_obj, "allcontrols", VLC_VAR_ADDCHOICE, &val, &val2 );
3095 /* bad things happen changing MPEG mid-stream
3096 * so don't add to Ext Settings GUI */
3097 if( V4L2_CTRL_ID2CLASS( queryctrl.id ) != V4L2_CTRL_CLASS_MPEG )
3098 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
3100 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
3103 var_SetBool( p_obj, psz_name, i_val );
3105 case VLC_VAR_INTEGER:
3106 var_SetInteger( p_obj, psz_name, i_val );
3111 msg_Warn( p_obj, "FIXME: %s %s %d", __FILE__, __func__,
3117 var_AddCallback( p_obj, psz_name,
3118 DemuxControlCallback, (void*)queryctrl.id );
3120 var_AddCallback( p_obj, psz_name,
3121 AccessControlCallback, (void*)queryctrl.id );
3126 /*****************************************************************************
3127 * List all user-class v4l2 controls, set them to the user specified
3128 * value and create the relevant variables to enable runtime changes
3129 *****************************************************************************/
3130 static int ControlList( vlc_object_t *p_obj, int i_fd,
3131 vlc_bool_t b_reset, vlc_bool_t b_demux )
3133 struct v4l2_queryctrl queryctrl;
3136 memset( &queryctrl, 0, sizeof( queryctrl ) );
3138 /* A list of available controls (aka the variable name) will be
3139 * stored as choices in the "allcontrols" variable. We'll thus be able
3140 * to use those to create an appropriate interface
3141 * A list of available controls that can be changed mid-stream will
3142 * be stored in the "controls" variable */
3143 var_Create( p_obj, "controls", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
3144 var_Create( p_obj, "allcontrols", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
3146 var_Create( p_obj, "controls-update", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
3148 /* Add a control to reset all controls to their default values */
3149 vlc_value_t val, val2;
3150 var_Create( p_obj, "controls-reset", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
3151 val.psz_string = _( "Reset controls to default" );
3152 var_Change( p_obj, "controls-reset", VLC_VAR_SETTEXT, &val, NULL );
3154 val2.psz_string = (char *)"controls-reset";
3155 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
3157 var_AddCallback( p_obj, "controls-reset", DemuxControlResetCallback, NULL );
3159 var_AddCallback( p_obj, "controls-reset", AccessControlResetCallback, NULL );
3161 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
3162 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3164 msg_Dbg( p_obj, "Extended control API supported by v4l2 driver" );
3166 /* List extended controls */
3167 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
3168 while( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3170 if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS )
3172 msg_Dbg( p_obj, "%s", queryctrl.name );
3173 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
3176 switch( V4L2_CTRL_ID2CLASS( queryctrl.id ) )
3178 case V4L2_CTRL_CLASS_USER:
3179 msg_Dbg( p_obj, "Available control: %s (%x)",
3180 queryctrl.name, queryctrl.id );
3182 case V4L2_CTRL_CLASS_MPEG:
3183 name2var( queryctrl.name );
3184 msg_Dbg( p_obj, "Available MPEG control: %s (%x)",
3185 queryctrl.name, queryctrl.id );
3188 msg_Dbg( p_obj, "Available private control: %s (%x)",
3189 queryctrl.name, queryctrl.id );
3192 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
3193 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
3198 msg_Dbg( p_obj, "Extended control API not supported by v4l2 driver" );
3200 /* List public controls */
3201 for( i_cid = V4L2_CID_BASE;
3202 i_cid < V4L2_CID_LASTP1;
3205 queryctrl.id = i_cid;
3206 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3208 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3210 msg_Dbg( p_obj, "Available control: %s (%x)",
3211 queryctrl.name, queryctrl.id );
3212 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
3216 /* List private controls */
3217 for( i_cid = V4L2_CID_PRIVATE_BASE;
3221 queryctrl.id = i_cid;
3222 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3224 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3226 msg_Dbg( p_obj, "Available private control: %s (%x)",
3227 queryctrl.name, queryctrl.id );
3228 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
3238 static void SetAvailControlsByString( vlc_object_t *p_obj, demux_sys_t *p_sys,
3241 char *psz_parser = p_sys->psz_set_ctrls;
3242 vlc_value_t val, text, name;
3244 if( psz_parser == NULL )
3247 if( *psz_parser == '{' )
3250 int i_ret = var_Change( p_obj, "allcontrols", VLC_VAR_GETCHOICES,
3254 msg_Err( p_obj, "Oops, can't find 'allcontrols' variable." );
3258 while( *psz_parser && *psz_parser != '}' )
3260 char *psz_delim, *psz_assign;
3262 while( *psz_parser == ',' || *psz_parser == ' ' )
3265 psz_delim = strchr( psz_parser, ',' );
3266 if( psz_delim == NULL )
3267 psz_delim = strchr( psz_parser, '}' );
3268 if( psz_delim == NULL )
3269 psz_delim = psz_parser + strlen( psz_parser );
3271 psz_assign = memchr( psz_parser, '=', psz_delim - psz_parser );
3272 if( psz_assign == NULL )
3274 char *psz_name = strndup( psz_parser, psz_delim - psz_parser );
3275 msg_Err( p_obj, "%s missing '='", psz_name );
3277 psz_parser = psz_delim + 1;
3282 i < val.p_list->i_count ;//&& psz_parser < psz_assign;
3285 const char *psz_var = text.p_list->p_values[i].psz_string;
3286 int i_cid = val.p_list->p_values[i].i_int;
3287 var_Change( p_obj, psz_var, VLC_VAR_GETTEXT, &name, NULL );
3288 const char *psz_name = name.psz_string;
3290 int i_availstrlen = strlen( psz_name );
3291 int i_parsestrlen = psz_assign - psz_parser;
3292 int i_maxstrlen = __MAX( i_availstrlen, i_parsestrlen);
3294 if( !strncasecmp( psz_name, psz_parser, i_maxstrlen ) )
3296 Control( p_obj, i_fd, psz_name, i_cid,
3297 strtol( ++psz_assign, &psz_parser, 0) );
3301 if( psz_parser < psz_assign )
3303 char *psz_name = strndup( psz_parser, psz_assign - psz_parser );
3304 msg_Err( p_obj, "Control %s not available", psz_name );
3306 psz_parser = ( *psz_delim ) ? ( psz_delim + 1 ) : psz_delim;
3311 /*****************************************************************************
3312 * Reset all user-class v4l2 controls to their default value
3313 *****************************************************************************/
3314 static int ControlReset( vlc_object_t *p_obj, int i_fd )
3316 struct v4l2_queryctrl queryctrl;
3318 memset( &queryctrl, 0, sizeof( queryctrl ) );
3320 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
3321 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3323 /* Extended control API supported */
3324 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
3325 while( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3327 if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS
3328 || V4L2_CTRL_ID2CLASS( queryctrl.id ) == V4L2_CTRL_CLASS_MPEG )
3330 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
3333 struct v4l2_control control;
3334 memset( &control, 0, sizeof( control ) );
3335 control.id = queryctrl.id;
3336 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
3337 && queryctrl.default_value != control.value )
3340 for( i = 0; controls[i].psz_name != NULL; i++ )
3341 if( controls[i].i_cid == queryctrl.id ) break;
3342 name2var( queryctrl.name );
3343 Control( p_obj, i_fd,
3344 controls[i].psz_name ? : (const char *)queryctrl.name,
3345 queryctrl.id, queryctrl.default_value );
3347 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
3353 /* public controls */
3354 for( i_cid = V4L2_CID_BASE;
3355 i_cid < V4L2_CID_LASTP1;
3358 queryctrl.id = i_cid;
3359 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3361 struct v4l2_control control;
3362 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3364 memset( &control, 0, sizeof( control ) );
3365 control.id = queryctrl.id;
3366 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
3367 && queryctrl.default_value != control.value )
3370 for( i = 0; controls[i].psz_name != NULL; i++ )
3371 if( controls[i].i_cid == queryctrl.id ) break;
3372 name2var( queryctrl.name );
3373 Control( p_obj, i_fd,
3374 controls[i].psz_name ? : (const char *)queryctrl.name,
3375 queryctrl.id, queryctrl.default_value );
3380 /* private controls */
3381 for( i_cid = V4L2_CID_PRIVATE_BASE;
3385 queryctrl.id = i_cid;
3386 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3388 struct v4l2_control control;
3389 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3391 memset( &control, 0, sizeof( control ) );
3392 control.id = queryctrl.id;
3393 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
3394 && queryctrl.default_value != control.value )
3396 name2var( queryctrl.name );
3397 Control( p_obj, i_fd, (const char *)queryctrl.name,
3398 queryctrl.id, queryctrl.default_value );
3408 /*****************************************************************************
3409 * Issue user-class v4l2 controls
3410 *****************************************************************************/
3411 static int Control( vlc_object_t *p_obj, int i_fd,
3412 const char *psz_name, int i_cid, int i_value )
3414 struct v4l2_queryctrl queryctrl;
3415 struct v4l2_control control;
3416 struct v4l2_ext_control ext_control;
3417 struct v4l2_ext_controls ext_controls;
3422 memset( &queryctrl, 0, sizeof( queryctrl ) );
3424 queryctrl.id = i_cid;
3426 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) < 0
3427 || queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3429 msg_Dbg( p_obj, "%s (%x) control is not supported.", psz_name, i_cid );
3430 return VLC_EGENERIC;
3433 memset( &control, 0, sizeof( control ) );
3434 memset( &ext_control, 0, sizeof( ext_control ) );
3435 memset( &ext_controls, 0, sizeof( ext_controls ) );
3437 ext_control.id = i_cid;
3438 ext_controls.ctrl_class = V4L2_CTRL_ID2CLASS( i_cid );
3439 ext_controls.count = 1;
3440 ext_controls.controls = &ext_control;
3446 ext_control.value = i_value;
3447 if( ioctl( i_fd, VIDIOC_S_EXT_CTRLS, &ext_controls ) < 0 )
3449 control.value = i_value;
3450 if( ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
3452 msg_Err( p_obj, "unable to set %s (%x) to %d (%m)",
3453 psz_name, i_cid, i_value );
3454 return VLC_EGENERIC;
3456 i_ret = ioctl( i_fd, VIDIOC_G_CTRL, &control );
3460 i_ret = ioctl( i_fd, VIDIOC_G_EXT_CTRLS, &ext_controls );
3461 control.value = ext_control.value;
3468 msg_Dbg( p_obj, "video %s: %d", psz_name, control.value );
3469 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
3472 val.b_bool = control.value;
3473 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
3474 var_SetVoid( p_obj, "controls-update" );
3476 case VLC_VAR_INTEGER:
3477 val.i_int = control.value;
3478 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
3479 var_SetVoid( p_obj, "controls-update" );
3486 /*****************************************************************************
3487 * On the fly change settings callback
3488 *****************************************************************************/
3489 static int DemuxControlCallback( vlc_object_t *p_this,
3490 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3493 demux_t *p_demux = (demux_t*)p_this;
3494 demux_sys_t *p_sys = p_demux->p_sys;
3495 int i_cid = (int)p_data;
3497 int i_fd = p_sys->i_fd_video;
3500 return VLC_EGENERIC;
3502 Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
3504 return VLC_EGENERIC;
3507 static int DemuxControlResetCallback( vlc_object_t *p_this,
3508 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3511 demux_t *p_demux = (demux_t*)p_this;
3512 demux_sys_t *p_sys = p_demux->p_sys;
3514 int i_fd = p_sys->i_fd_video;
3517 return VLC_EGENERIC;
3519 ControlReset( p_this, i_fd );
3521 return VLC_EGENERIC;
3524 static int AccessControlCallback( vlc_object_t *p_this,
3525 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3528 access_t *p_access = (access_t *)p_this;
3529 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
3530 int i_cid = (int)p_data;
3532 int i_fd = p_sys->i_fd_video;
3535 return VLC_EGENERIC;
3537 Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
3539 return VLC_EGENERIC;
3542 static int AccessControlResetCallback( vlc_object_t *p_this,
3543 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3546 access_t *p_access = (access_t *)p_this;
3547 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
3549 int i_fd = p_sys->i_fd_video;
3552 return VLC_EGENERIC;
3554 ControlReset( p_this, i_fd );
3556 return VLC_EGENERIC;