1 /*****************************************************************************
2 * v4l2.c : Video4Linux2 input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2007 the VideoLAN team
7 * Authors: Benjamin Pracht <bigben at videolan dot org>
8 * Richard Hosking <richard at hovis dot net>
9 * Antoine Cellerier <dionoea at videolan d.t org>
10 * Dennis Lou <dlou99 at yahoo dot com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
28 * Sections based on the reference V4L2 capture example at
29 * http://v4l2spec.bytesex.org/spec/capture-example.html
31 * ALSA support based on parts of
32 * http://www.equalarea.com/paul/alsa-audio.html
33 * and hints taken from alsa-utils (aplay/arecord)
34 * http://www.alsa-project.org
37 /*****************************************************************************
39 *****************************************************************************/
42 #include <vlc_access.h>
43 #include <vlc_demux.h>
44 #include <vlc_input.h>
50 #include <sys/ioctl.h>
53 #include <linux/videodev2.h>
55 #include <sys/soundcard.h>
58 # define ALSA_PCM_NEW_HW_PARAMS_API
59 # define ALSA_PCM_NEW_SW_PARAMS_API
60 # include <alsa/asoundlib.h>
65 /*****************************************************************************
67 *****************************************************************************/
69 static int DemuxOpen ( vlc_object_t * );
70 static void DemuxClose( vlc_object_t * );
71 static int AccessOpen ( vlc_object_t * );
72 static void AccessClose( vlc_object_t * );
74 #define DEV_TEXT N_("Device name")
75 #define DEV_LONGTEXT N_( \
76 "Name of the device to use. " \
77 "If you don't specify anything, /dev/video0 will be used.")
78 #define STANDARD_TEXT N_( "Standard" )
79 #define STANDARD_LONGTEXT N_( \
80 "Video standard (Default, SECAM, PAL, or NTSC)." )
81 #define CHROMA_TEXT N_("Video input chroma format")
82 #define CHROMA_LONGTEXT N_( \
83 "Force the Video4Linux2 video device to use a specific chroma format " \
84 "(eg. I420 or I422 for raw images, MJPEG for M-JPEG compressed input) " \
85 "(Complete list: GREY, I240, RV16, RV15, RV24, RV32, YUY2, YUYV, UYVY, " \
86 "I41N, I422, I420, I411, I410, MJPG)")
87 #define INPUT_TEXT N_( "Input" )
88 #define INPUT_LONGTEXT N_( \
89 "Input of the card to use (see debug)." )
90 #define AUDIO_INPUT_TEXT N_( "Audio input" )
91 #define AUDIO_INPUT_LONGTEXT N_( \
92 "Audio input of the card to use (see debug)." )
93 #define IOMETHOD_TEXT N_( "IO Method" )
94 #define IOMETHOD_LONGTEXT N_( \
95 "IO Method (READ, MMAP, USERPTR)." )
96 #define WIDTH_TEXT N_( "Width" )
97 #define WIDTH_LONGTEXT N_( \
98 "Force width (-1 for autodetect)." )
99 #define HEIGHT_TEXT N_( "Height" )
100 #define HEIGHT_LONGTEXT N_( \
101 "Force height (-1 for autodetect)." )
102 #define FPS_TEXT N_( "Framerate" )
103 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
104 "(-1 for autodetect)." )
106 #define CTRL_RESET_TEXT N_( "Reset v4l2 controls" )
107 #define CTRL_RESET_LONGTEXT N_( \
108 "Reset controls to defaults provided by the v4l2 driver." )
109 #define BRIGHTNESS_TEXT N_( "Brightness" )
110 #define BRIGHTNESS_LONGTEXT N_( \
111 "Brightness of the video input (if supported by the v4l2 driver)." )
112 #define CONTRAST_TEXT N_( "Contrast" )
113 #define CONTRAST_LONGTEXT N_( \
114 "Contrast of the video input (if supported by the v4l2 driver)." )
115 #define SATURATION_TEXT N_( "Saturation" )
116 #define SATURATION_LONGTEXT N_( \
117 "Saturation of the video input (if supported by the v4l2 driver)." )
118 #define HUE_TEXT N_( "Hue" )
119 #define HUE_LONGTEXT N_( \
120 "Hue of the video input (if supported by the v4l2 driver)." )
121 #define BLACKLEVEL_TEXT N_( "Black level" )
122 #define BLACKLEVEL_LONGTEXT N_( \
123 "Black level of the video input (if supported by the v4l2 driver)." )
124 #define AUTOWHITEBALANCE_TEXT N_( "Auto white balance" )
125 #define AUTOWHITEBALANCE_LONGTEXT N_( \
126 "Automatically set the white balance of the video input " \
127 "(if supported by the v4l2 driver)." )
128 #define DOWHITEBALANCE_TEXT N_( "Do white balance" )
129 #define DOWHITEBALANCE_LONGTEXT N_( \
130 "Trigger a white balancing action, useless if auto white balance is " \
131 "activated (if supported by the v4l2 driver)." )
132 #define REDBALANCE_TEXT N_( "Red balance" )
133 #define REDBALANCE_LONGTEXT N_( \
134 "Red balance of the video input (if supported by the v4l2 driver)." )
135 #define BLUEBALANCE_TEXT N_( "Blue balance" )
136 #define BLUEBALANCE_LONGTEXT N_( \
137 "Blue balance of the video input (if supported by the v4l2 driver)." )
138 #define GAMMA_TEXT N_( "Gamma" )
139 #define GAMMA_LONGTEXT N_( \
140 "Gamma of the video input (if supported by the v4l2 driver)." )
141 #define EXPOSURE_TEXT N_( "Exposure" )
142 #define EXPOSURE_LONGTEXT N_( \
143 "Exposure of the video input (if supported by the v4L2 driver)." )
144 #define AUTOGAIN_TEXT N_( "Auto gain" )
145 #define AUTOGAIN_LONGTEXT N_( \
146 "Automatically set the video input's gain (if supported by the " \
148 #define GAIN_TEXT N_( "Gain" )
149 #define GAIN_LONGTEXT N_( \
150 "Video input's gain (if supported by the v4l2 driver)." )
151 #define HFLIP_TEXT N_( "Horizontal flip" )
152 #define HFLIP_LONGTEXT N_( \
153 "Flip the video horizontally (if supported by the v4l2 driver)." )
154 #define VFLIP_TEXT N_( "Vertical flip" )
155 #define VFLIP_LONGTEXT N_( \
156 "Flip the video vertically (if supported by the v4l2 driver)." )
157 #define HCENTER_TEXT N_( "Horizontal centering" )
158 #define HCENTER_LONGTEXT N_( \
159 "Set the camera's horizontal centering (if supported by the v4l2 driver)." )
160 #define VCENTER_TEXT N_( "Vertical centering" )
161 #define VCENTER_LONGTEXT N_( \
162 "Set the camera's vertical centering (if supported by the v4l2 driver)." )
164 #define ADEV_TEXT N_("Audio device name")
166 #define ADEV_LONGTEXT N_( \
167 "Name of the audio device to use. " \
168 "If you don't specify anything, \"/dev/dsp\" will be used for OSS.")
170 #define ADEV_LONGTEXT N_( \
171 "Name of the audio device to use. " \
172 "If you don't specify anything, \"/dev/dsp\" will be used for OSS, " \
175 #define AUDIO_METHOD_TEXT N_( "Audio method" )
177 #define AUDIO_METHOD_LONGTEXT N_( \
178 "Audio method to use: 0 to disable audio, 1 for OSS." )
180 #define AUDIO_METHOD_LONGTEXT N_( \
181 "Audio method to use: 0 to disable audio, 1 for OSS, 2 for ALSA, " \
182 "3 for ALSA or OSS (ALSA is prefered)." )
184 #define AUDIO_VOLUME_TEXT N_( "Volume" )
185 #define AUDIO_VOLUME_LONGTEXT N_( \
186 "Volume of the audio input (if supported by the v4l2 driver)." )
187 #define AUDIO_BALANCE_TEXT N_( "Balance" )
188 #define AUDIO_BALANCE_LONGTEXT N_( \
189 "Balance of the audio input (if supported by the v4l2 driver)." )
190 #define AUDIO_MUTE_TEXT N_( "Mute" )
191 #define AUDIO_MUTE_LONGTEXT N_( \
192 "Mute audio input (if supported by the v4l2 driver)." )
193 #define AUDIO_BASS_TEXT N_( "Bass" )
194 #define AUDIO_BASS_LONGTEXT N_( \
195 "Bass level of the audio input (if supported by the v4l2 driver)." )
196 #define AUDIO_TREBLE_TEXT N_( "Treble" )
197 #define AUDIO_TREBLE_LONGTEXT N_( \
198 "Treble level of the audio input (if supported by the v4l2 driver)." )
199 #define AUDIO_LOUDNESS_TEXT N_( "Loudness" )
200 #define AUDIO_LOUDNESS_LONGTEXT N_( \
201 "Loudness of the audio input (if supported by the v4l2 driver)." )
203 #define STEREO_TEXT N_( "Stereo" )
204 #define STEREO_LONGTEXT N_( \
205 "Capture the audio stream in stereo." )
206 #define SAMPLERATE_TEXT N_( "Samplerate" )
207 #define SAMPLERATE_LONGTEXT N_( \
208 "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
210 #define CACHING_TEXT N_("Caching value in ms")
211 #define CACHING_LONGTEXT N_( \
212 "Caching value for V4L2 captures. This " \
213 "value should be set in milliseconds." )
214 #define S_CTRLS_TEXT N_("v4l2 driver controls")
215 #define S_CTRLS_LONGTEXT N_( \
216 "Set the v4l2 driver controls to the values specified using a comma " \
217 "separated list optionally encapsulated by curly braces " \
218 "(e.g.: {video_bitrate=6000000,audio_crc=0,stream_type=3} ). " \
219 "To list available controls, increase verbosity (-vvv) " \
220 "or use the v4l2-ctl application." )
222 #define TUNER_TEXT N_("Tuner id")
223 #define TUNER_LONGTEXT N_( \
224 "Tuner id (see debug output)." )
225 #define FREQUENCY_TEXT N_("Frequency")
226 #define FREQUENCY_LONGTEXT N_( \
227 "Tuner frequency in Hz or kHz (see debug output)" )
228 #define TUNER_AUDIO_MODE_TEXT N_("Audio mode")
229 #define TUNER_AUDIO_MODE_LONGTEXT N_( \
230 "Tuner audio mono/stereo and track selection." )
238 static int i_standards_list[] =
239 { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
240 static const char *psz_standards_list_text[] =
241 { N_("Default"), N_("SECAM"), N_("PAL"), N_("NTSC") };
243 static int i_iomethod_list[] =
244 { IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR };
245 static const char *psz_iomethod_list_text[] =
246 { N_("READ"), N_("MMAP"), N_("USERPTR") };
248 static int i_tuner_audio_modes_list[] =
249 { V4L2_TUNER_MODE_MONO, V4L2_TUNER_MODE_STEREO,
250 V4L2_TUNER_MODE_LANG1, V4L2_TUNER_MODE_LANG2,
251 V4L2_TUNER_MODE_SAP, V4L2_TUNER_MODE_LANG1_LANG2 };
252 static const char *psz_tuner_audio_modes_list_text[] =
255 N_( "Primary language (Analog TV tuners only)" ),
256 N_( "Secondary language (Analog TV tuners only)" ),
257 N_( "Second audio program (Analog TV tuners only)" ),
258 N_( "Primary language left, Secondary language right" ) };
263 #define AUDIO_METHOD_OSS 1
264 #define OSS_DEFAULT "/dev/dsp"
265 #define AUDIO_METHOD_ALSA 2
266 #define ALSA_DEFAULT "hw"
267 #define CFG_PREFIX "v4l2-"
270 set_shortname( _("Video4Linux2") );
271 set_description( _("Video4Linux2 input") );
272 set_category( CAT_INPUT );
273 set_subcategory( SUBCAT_INPUT_ACCESS );
275 set_section( N_( "Video input" ), NULL );
276 add_string( CFG_PREFIX "dev", "/dev/video0", 0, DEV_TEXT, DEV_LONGTEXT,
278 add_integer( CFG_PREFIX "standard", 0, NULL, STANDARD_TEXT,
279 STANDARD_LONGTEXT, VLC_FALSE );
280 change_integer_list( i_standards_list, psz_standards_list_text, 0 );
281 add_string( CFG_PREFIX "chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
283 add_integer( CFG_PREFIX "input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
285 add_integer( CFG_PREFIX "audio-input", 0, NULL, AUDIO_INPUT_TEXT,
286 AUDIO_INPUT_LONGTEXT, VLC_TRUE );
287 add_integer( CFG_PREFIX "io", IO_METHOD_MMAP, NULL, IOMETHOD_TEXT,
288 IOMETHOD_LONGTEXT, VLC_TRUE );
289 change_integer_list( i_iomethod_list, psz_iomethod_list_text, 0 );
290 add_integer( CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
291 WIDTH_LONGTEXT, VLC_TRUE );
292 add_integer( CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
293 HEIGHT_LONGTEXT, VLC_TRUE );
294 add_float( CFG_PREFIX "fps", 0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
296 set_section( N_( "Audio input" ), NULL );
297 add_string( CFG_PREFIX "adev", NULL, 0, ADEV_TEXT, ADEV_LONGTEXT,
299 add_integer( CFG_PREFIX "audio-method", AUDIO_METHOD_OSS|AUDIO_METHOD_ALSA,
300 NULL, AUDIO_METHOD_TEXT, AUDIO_METHOD_LONGTEXT, VLC_TRUE );
301 add_bool( CFG_PREFIX "stereo", VLC_TRUE, NULL, STEREO_TEXT, STEREO_LONGTEXT,
303 add_integer( CFG_PREFIX "samplerate", 48000, NULL, SAMPLERATE_TEXT,
304 SAMPLERATE_LONGTEXT, VLC_TRUE );
305 add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
306 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
308 set_section( N_( "Tuner" ), NULL );
309 add_integer( CFG_PREFIX "tuner", 0, NULL, TUNER_TEXT, TUNER_LONGTEXT,
311 add_integer( CFG_PREFIX "tuner-frequency", -1, NULL, FREQUENCY_TEXT,
312 FREQUENCY_LONGTEXT, VLC_TRUE );
313 add_integer( CFG_PREFIX "tuner-audio-mode", -1, NULL, TUNER_AUDIO_MODE_TEXT,
314 TUNER_AUDIO_MODE_LONGTEXT, VLC_TRUE );
315 change_integer_list( i_tuner_audio_modes_list,
316 psz_tuner_audio_modes_list_text, 0 );
318 set_section( N_( "Controls" ),
319 N_( "v4l2 driver controls, if supported by your v4l2 driver." ) );
320 add_bool( CFG_PREFIX "controls-reset", VLC_FALSE, NULL, CTRL_RESET_TEXT,
321 CTRL_RESET_LONGTEXT, VLC_TRUE );
322 add_integer( CFG_PREFIX "brightness", -1, NULL, BRIGHTNESS_TEXT,
323 BRIGHTNESS_LONGTEXT, VLC_TRUE );
324 add_integer( CFG_PREFIX "contrast", -1, NULL, CONTRAST_TEXT,
325 CONTRAST_LONGTEXT, VLC_TRUE );
326 add_integer( CFG_PREFIX "saturation", -1, NULL, SATURATION_TEXT,
327 SATURATION_LONGTEXT, VLC_TRUE );
328 add_integer( CFG_PREFIX "hue", -1, NULL, HUE_TEXT,
329 HUE_LONGTEXT, VLC_TRUE );
330 add_integer( CFG_PREFIX "black-level", -1, NULL, BLACKLEVEL_TEXT,
331 BLACKLEVEL_LONGTEXT, VLC_TRUE );
332 add_integer( CFG_PREFIX "auto-white-balance", -1, NULL,
333 AUTOWHITEBALANCE_TEXT, AUTOWHITEBALANCE_LONGTEXT, VLC_TRUE );
334 add_integer( CFG_PREFIX "do-white-balance", -1, NULL, DOWHITEBALANCE_TEXT,
335 DOWHITEBALANCE_LONGTEXT, VLC_TRUE );
336 add_integer( CFG_PREFIX "red-balance", -1, NULL, REDBALANCE_TEXT,
337 REDBALANCE_LONGTEXT, VLC_TRUE );
338 add_integer( CFG_PREFIX "blue-balance", -1, NULL, BLUEBALANCE_TEXT,
339 BLUEBALANCE_LONGTEXT, VLC_TRUE );
340 add_integer( CFG_PREFIX "gamma", -1, NULL, GAMMA_TEXT,
341 GAMMA_LONGTEXT, VLC_TRUE );
342 add_integer( CFG_PREFIX "exposure", -1, NULL, EXPOSURE_TEXT,
343 EXPOSURE_LONGTEXT, VLC_TRUE );
344 add_integer( CFG_PREFIX "autogain", -1, NULL, AUTOGAIN_TEXT,
345 AUTOGAIN_LONGTEXT, VLC_TRUE );
346 add_integer( CFG_PREFIX "gain", -1, NULL, GAIN_TEXT,
347 GAIN_LONGTEXT, VLC_TRUE );
348 add_integer( CFG_PREFIX "hflip", -1, NULL, HFLIP_TEXT,
349 HFLIP_LONGTEXT, VLC_TRUE );
350 add_integer( CFG_PREFIX "vflip", -1, NULL, VFLIP_TEXT,
351 VFLIP_LONGTEXT, VLC_TRUE );
352 add_integer( CFG_PREFIX "hcenter", -1, NULL, HCENTER_TEXT,
353 HCENTER_LONGTEXT, VLC_TRUE );
354 add_integer( CFG_PREFIX "vcenter", -1, NULL, VCENTER_TEXT,
355 VCENTER_LONGTEXT, VLC_TRUE );
356 add_integer( CFG_PREFIX "audio-volume", -1, NULL, AUDIO_VOLUME_TEXT,
357 AUDIO_VOLUME_LONGTEXT, VLC_TRUE );
358 add_integer( CFG_PREFIX "audio-balance", -1, NULL, AUDIO_BALANCE_TEXT,
359 AUDIO_BALANCE_LONGTEXT, VLC_TRUE );
360 add_bool( CFG_PREFIX "audio-mute", VLC_FALSE, NULL, AUDIO_MUTE_TEXT,
361 AUDIO_MUTE_LONGTEXT, VLC_TRUE );
362 add_integer( CFG_PREFIX "audio-bass", -1, NULL, AUDIO_BASS_TEXT,
363 AUDIO_BASS_LONGTEXT, VLC_TRUE );
364 add_integer( CFG_PREFIX "audio-treble", -1, NULL, AUDIO_TREBLE_TEXT,
365 AUDIO_TREBLE_LONGTEXT, VLC_TRUE );
366 add_integer( CFG_PREFIX "audio-loudness", -1, NULL, AUDIO_LOUDNESS_TEXT,
367 AUDIO_LOUDNESS_LONGTEXT, VLC_TRUE );
368 add_string( CFG_PREFIX "set-ctrls", NULL, NULL, S_CTRLS_TEXT,
369 S_CTRLS_LONGTEXT, VLC_TRUE );
371 add_shortcut( "v4l2" );
372 set_capability( "access_demux", 10 );
373 set_callbacks( DemuxOpen, DemuxClose );
376 add_shortcut( "v4l2c" );
377 set_description( _("Video4Linux2 Compressed A/V") );
378 set_capability( "access2", 0 );
379 /* use these when open as access_demux fails; VLC will use another demux */
380 set_callbacks( AccessOpen, AccessClose );
384 /*****************************************************************************
385 * Access: local prototypes
386 *****************************************************************************/
388 static void CommonClose( vlc_object_t *, demux_sys_t * );
389 static void ParseMRL( demux_sys_t *, char *, vlc_object_t * );
390 static void GetV4L2Params( demux_sys_t *, vlc_object_t * );
391 static void SetAvailControlsByString( vlc_object_t *, demux_sys_t *, int );
393 static int DemuxControl( demux_t *, int, va_list );
394 static int AccessControl( access_t *, int, va_list );
396 static int Demux( demux_t * );
397 static ssize_t AccessRead( access_t *, uint8_t *, size_t );
399 static block_t* GrabVideo( demux_t *p_demux );
400 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t );
401 static block_t* GrabAudio( demux_t *p_demux );
403 static vlc_bool_t IsPixelFormatSupported( demux_t *p_demux,
404 unsigned int i_pixelformat );
407 static char* ResolveALSADeviceName( const char *psz_device );
409 static int OpenVideoDev( vlc_object_t *, demux_sys_t *, vlc_bool_t );
410 static int OpenAudioDev( vlc_object_t *, demux_sys_t *, vlc_bool_t );
411 static vlc_bool_t ProbeVideoDev( vlc_object_t *, demux_sys_t *,
413 static vlc_bool_t ProbeAudioDev( vlc_object_t *, demux_sys_t *,
416 static int ControlList( vlc_object_t *, int , vlc_bool_t, vlc_bool_t );
417 static int Control( vlc_object_t *, int i_fd,
418 const char *psz_name, int i_cid, int i_value );
420 static int DemuxControlCallback( vlc_object_t *p_this, const char *psz_var,
421 vlc_value_t oldval, vlc_value_t newval,
423 static int DemuxControlResetCallback( vlc_object_t *p_this, const char *psz_var,
424 vlc_value_t oldval, vlc_value_t newval,
426 static int AccessControlCallback( vlc_object_t *p_this, const char *psz_var,
427 vlc_value_t oldval, vlc_value_t newval,
429 static int AccessControlResetCallback( vlc_object_t *p_this,
430 const char *psz_var, vlc_value_t oldval,
431 vlc_value_t newval, void *p_data );
437 } v4l2chroma_to_fourcc[] =
440 { V4L2_PIX_FMT_GREY, VLC_FOURCC('G','R','E','Y') },
441 { V4L2_PIX_FMT_HI240, VLC_FOURCC('I','2','4','0') },
442 { V4L2_PIX_FMT_RGB565, VLC_FOURCC('R','V','1','6') },
443 { V4L2_PIX_FMT_RGB555, VLC_FOURCC('R','V','1','5') },
444 { V4L2_PIX_FMT_BGR24, VLC_FOURCC('R','V','2','4') },
445 { V4L2_PIX_FMT_BGR32, VLC_FOURCC('R','V','3','2') },
446 { V4L2_PIX_FMT_YUYV, VLC_FOURCC('Y','U','Y','2') },
447 { V4L2_PIX_FMT_YUYV, VLC_FOURCC('Y','U','Y','V') },
448 { V4L2_PIX_FMT_UYVY, VLC_FOURCC('U','Y','V','Y') },
449 { V4L2_PIX_FMT_Y41P, VLC_FOURCC('I','4','1','N') },
450 { V4L2_PIX_FMT_YUV422P, VLC_FOURCC('I','4','2','2') },
451 { V4L2_PIX_FMT_YVU420, VLC_FOURCC('Y','V','1','2') },
452 { V4L2_PIX_FMT_YUV411P, VLC_FOURCC('I','4','1','1') },
453 { V4L2_PIX_FMT_YUV410, VLC_FOURCC('I','4','1','0') },
455 /* Raw data types, not in V4L2 spec but still in videodev2.h and supported
457 { V4L2_PIX_FMT_YUV420, VLC_FOURCC('I','4','2','0') },
458 /* FIXME { V4L2_PIX_FMT_RGB444, VLC_FOURCC('R','V','3','2') }, */
460 /* Compressed data types */
461 { V4L2_PIX_FMT_MJPEG, VLC_FOURCC('M','J','P','G') },
463 { V4L2_PIX_FMT_JPEG, VLC_FOURCC('J','P','E','G') },
464 { V4L2_PIX_FMT_DV, VLC_FOURCC('?','?','?','?') },
465 { V4L2_PIX_FMT_MPEG, VLC_FOURCC('?','?','?','?') },
471 * List of V4L2 chromas were confident enough to use as fallbacks if the
472 * user hasn't provided a --v4l2-chroma value.
474 static const __u32 p_chroma_fallbacks[] =
475 { V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_YVU420, V4L2_PIX_FMT_YUV422P,
476 V4L2_PIX_FMT_YUYV, V4L2_PIX_FMT_MJPEG };
480 const char *psz_name;
484 { "brightness", V4L2_CID_BRIGHTNESS },
485 { "contrast", V4L2_CID_CONTRAST },
486 { "saturation", V4L2_CID_SATURATION },
487 { "hue", V4L2_CID_HUE },
488 { "audio-volume", V4L2_CID_AUDIO_VOLUME },
489 { "audio-balance", V4L2_CID_AUDIO_BALANCE },
490 { "audio-bass", V4L2_CID_AUDIO_BASS },
491 { "audio-treble", V4L2_CID_AUDIO_TREBLE },
492 { "audio-mute", V4L2_CID_AUDIO_MUTE },
493 { "audio-loudness", V4L2_CID_AUDIO_LOUDNESS },
494 { "black-level", V4L2_CID_BLACK_LEVEL },
495 { "auto-white-balance", V4L2_CID_AUTO_WHITE_BALANCE },
496 { "do-white-balance", V4L2_CID_DO_WHITE_BALANCE },
497 { "red-balance", V4L2_CID_RED_BALANCE },
498 { "blue-balance", V4L2_CID_BLUE_BALANCE },
499 { "gamma", V4L2_CID_GAMMA },
500 { "exposure", V4L2_CID_EXPOSURE },
501 { "autogain", V4L2_CID_AUTOGAIN },
502 { "gain", V4L2_CID_GAIN },
503 { "hflip", V4L2_CID_HFLIP },
504 { "vflip", V4L2_CID_VFLIP },
505 { "hcenter", V4L2_CID_HCENTER },
506 { "vcenter", V4L2_CID_VCENTER },
519 char *psz_device; /* Main device from MRL, can be video or audio */
527 char *psz_requested_chroma;
534 struct v4l2_capability dev_cap;
537 struct v4l2_input *p_inputs;
538 int i_selected_input;
541 struct v4l2_standard *p_standards;
542 v4l2_std_id i_selected_standard_id;
545 /* V4L2 devices cannot have more than 32 audio inputs */
546 struct v4l2_audio p_audios[32];
547 int i_selected_audio_input;
550 struct v4l2_tuner *p_tuners;
553 struct v4l2_fmtdesc *p_codecs;
555 struct buffer_t *p_buffers;
556 unsigned int i_nbuffers;
560 float f_fps; /* <= 0.0 mean to grab at full rate */
561 mtime_t i_video_pts; /* only used when f_fps > 0 */
564 es_out_id_t *p_es_video;
567 unsigned int i_sample_rate;
569 size_t i_audio_max_frame_size;
570 block_t *p_block_audio;
571 es_out_id_t *p_es_audio;
577 snd_pcm_t *p_alsa_pcm;
578 size_t i_alsa_frame_size;
579 int i_alsa_chunk_size;
591 static int FindMainDevice( vlc_object_t *p_this, demux_sys_t *p_sys,
592 int i_flags, vlc_bool_t b_demux,
593 vlc_bool_t b_forced )
595 /* Find main device (video or audio) */
596 if( p_sys->psz_device && *p_sys->psz_device )
598 msg_Dbg( p_this, "main device='%s'", p_sys->psz_device );
600 vlc_bool_t b_maindevice_is_video = VLC_FALSE;
602 /* Try to open as video device */
603 if( i_flags & FIND_VIDEO )
605 msg_Dbg( p_this, "trying device '%s' as video", p_sys->psz_device );
606 if( ProbeVideoDev( p_this, p_sys, p_sys->psz_device ) )
608 msg_Dbg( p_this, "'%s' is a video device", p_sys->psz_device );
609 /* Device was a video device */
610 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
611 p_sys->psz_vdev = p_sys->psz_device;
612 p_sys->psz_device = NULL;
613 p_sys->i_fd_video = OpenVideoDev( p_this, p_sys, b_demux );
614 if( p_sys->i_fd_video < 0 )
616 b_maindevice_is_video = VLC_TRUE;
617 /* If successful we carry on to try the audio if access is forced */
621 /* Try to open as audio device only if main device was not detected as video above */
622 if( i_flags & FIND_AUDIO && !b_maindevice_is_video )
624 msg_Dbg( p_this, "trying device '%s' as audio", p_sys->psz_device );
625 if( ProbeAudioDev( p_this, p_sys, p_sys->psz_device ) )
627 msg_Dbg( p_this, "'%s' is an audio device", p_sys->psz_device );
628 /* Device was an audio device */
629 free( p_sys->psz_adev );
630 p_sys->psz_adev = p_sys->psz_device;
631 p_sys->psz_device = NULL;
632 p_sys->i_fd_audio = OpenAudioDev( p_this, p_sys, b_demux );
633 if( p_sys->i_fd_audio < 0 )
635 /* If successful we carry on to try the video if access is forced */
640 /* If no device opened, only continue if the access was forced */
641 if( b_forced == VLC_FALSE
642 && !( ( i_flags & FIND_VIDEO && p_sys->i_fd_video >= 0 )
643 || ( i_flags & FIND_AUDIO && p_sys->i_fd_audio >= 0 ) ) )
648 /* Find video device */
649 if( i_flags & FIND_VIDEO && p_sys->i_fd_video < 0 )
651 if( !p_sys->psz_vdev || !*p_sys->psz_vdev )
653 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
654 p_sys->psz_vdev = var_CreateGetString( p_this, "v4l2-dev" );
657 msg_Dbg( p_this, "opening '%s' as video", p_sys->psz_vdev );
658 if( p_sys->psz_vdev && *p_sys->psz_vdev
659 && ProbeVideoDev( p_this, p_sys, p_sys->psz_vdev ) )
661 p_sys->i_fd_video = OpenVideoDev( p_this, p_sys, b_demux );
665 /* Find audio device */
666 if( i_flags & FIND_AUDIO && p_sys->i_fd_audio < 0 )
668 if( !p_sys->psz_adev )
670 p_sys->psz_adev = var_CreateGetNonEmptyString( p_this, "v4l2-adev" );
673 msg_Dbg( p_this, "opening '%s' as audio", p_sys->psz_adev );
674 if( ProbeAudioDev( p_this, p_sys, p_sys->psz_adev ) )
676 p_sys->i_fd_audio = OpenAudioDev( p_this, p_sys, b_demux );
680 if( !( ( i_flags & FIND_VIDEO && p_sys->i_fd_video >= 0 )
681 || ( i_flags & FIND_AUDIO && p_sys->i_fd_audio >= 0 ) ) )
688 /*****************************************************************************
689 * DemuxOpen: opens v4l2 device, access_demux callback
690 *****************************************************************************
692 * url: <video device>::::
694 *****************************************************************************/
695 static int DemuxOpen( vlc_object_t *p_this )
697 demux_t *p_demux = (demux_t*)p_this;
700 /* Only when selected */
701 if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
704 p_demux->pf_control = DemuxControl;
705 p_demux->pf_demux = Demux;
706 p_demux->info.i_update = 0;
707 p_demux->info.i_title = 0;
708 p_demux->info.i_seekpoint = 0;
710 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
711 if( p_sys == NULL ) return VLC_ENOMEM;
713 GetV4L2Params(p_sys, (vlc_object_t *) p_demux);
715 ParseMRL( p_sys, p_demux->psz_path, (vlc_object_t *) p_demux );
718 /* Alsa support available? */
719 msg_Dbg( p_demux, "ALSA input support available" );
722 if( FindMainDevice( p_this, p_sys, FIND_VIDEO|FIND_AUDIO,
723 VLC_TRUE, !strncmp( p_demux->psz_access, "v4l2", 4 ) ) != VLC_SUCCESS )
725 DemuxClose( p_this );
732 /*****************************************************************************
733 * GetV4L2Params: fill in p_sys parameters (shared by DemuxOpen and AccessOpen)
734 *****************************************************************************/
735 static void GetV4L2Params( demux_sys_t *p_sys, vlc_object_t *p_obj )
737 p_sys->i_video_pts = -1;
739 p_sys->i_selected_standard_id =
740 i_standards_list[var_CreateGetInteger( p_obj, "v4l2-standard" )];
742 p_sys->i_selected_input = var_CreateGetInteger( p_obj, "v4l2-input" );
743 p_sys->i_selected_audio_input =
744 var_CreateGetInteger( p_obj, "v4l2-audio-input" );
746 p_sys->io = var_CreateGetInteger( p_obj, "v4l2-io" );
748 p_sys->i_width = var_CreateGetInteger( p_obj, "v4l2-width" );
749 p_sys->i_height = var_CreateGetInteger( p_obj, "v4l2-height" );
751 var_CreateGetBool( p_obj, "v4l2-controls-reset" );
753 p_sys->f_fps = var_CreateGetFloat( p_obj, "v4l2-fps" );
754 p_sys->i_sample_rate = var_CreateGetInteger( p_obj, "v4l2-samplerate" );
755 p_sys->psz_requested_chroma = var_CreateGetString( p_obj, "v4l2-chroma" );
757 p_sys->i_audio_method = var_CreateGetInteger( p_obj, "v4l2-audio-method" );
759 p_sys->b_stereo = var_CreateGetBool( p_obj, "v4l2-stereo" );
761 p_sys->i_pts = var_CreateGetInteger( p_obj, "v4l2-caching" );
763 p_sys->i_cur_tuner = var_CreateGetInteger( p_obj, "v4l2-tuner" );
764 p_sys->i_frequency = var_CreateGetInteger( p_obj, "v4l2-tuner-frequency" );
765 p_sys->i_audio_mode = var_CreateGetInteger( p_obj, "v4l2-tuner-audio-mode" );
767 p_sys->psz_set_ctrls = var_CreateGetString( p_obj, "v4l2-set-ctrls" );
769 p_sys->psz_device = p_sys->psz_vdev = p_sys->psz_adev = NULL;
770 p_sys->i_fd_video = -1;
771 p_sys->i_fd_audio = -1;
773 p_sys->p_es_video = p_sys->p_es_audio = 0;
774 p_sys->p_block_audio = 0;
777 /*****************************************************************************
778 * ParseMRL: parse the options contained in the MRL
779 *****************************************************************************/
780 static void ParseMRL( demux_sys_t *p_sys, char *psz_path, vlc_object_t *p_obj )
782 char *psz_dup = strdup( psz_path );
783 char *psz_parser = psz_dup;
785 while( *psz_parser && *psz_parser != ':' )
790 if( *psz_parser == ':' )
795 *psz_parser++ = '\0';
797 if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
801 psz_parser += strlen( "adev=" );
802 if( strchr( psz_parser, ':' ) )
804 i_len = strchr( psz_parser, ':' ) - psz_parser;
808 i_len = strlen( psz_parser );
811 p_sys->psz_adev = strndup( psz_parser, i_len );
812 if( !*p_sys->psz_adev )
814 free( p_sys->psz_adev );
815 p_sys->psz_adev = NULL;
820 else if( !strncmp( psz_parser, "standard=", strlen( "standard=" ) ) )
822 psz_parser += strlen( "standard=" );
823 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
825 p_sys->i_selected_standard_id = V4L2_STD_PAL;
826 psz_parser += strlen( "pal" );
828 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
830 p_sys->i_selected_standard_id = V4L2_STD_NTSC;
831 psz_parser += strlen( "ntsc" );
833 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
835 p_sys->i_selected_standard_id = V4L2_STD_SECAM;
836 psz_parser += strlen( "secam" );
838 else if( !strncmp( psz_parser, "default", strlen( "default" ) ) )
840 p_sys->i_selected_standard_id = V4L2_STD_UNKNOWN;
841 psz_parser += strlen( "default" );
845 p_sys->i_selected_standard_id = i_standards_list[strtol( psz_parser, &psz_parser, 0 )];
848 else if( !strncmp( psz_parser, "chroma=", strlen( "chroma=" ) ) )
852 psz_parser += strlen( "chroma=" );
853 if( strchr( psz_parser, ':' ) )
855 i_len = strchr( psz_parser, ':' ) - psz_parser;
859 i_len = strlen( psz_parser );
862 if( p_sys->psz_requested_chroma ) free( p_sys->psz_requested_chroma );
863 p_sys->psz_requested_chroma = strndup( psz_parser, i_len );
867 else if( !strncmp( psz_parser, "input=", strlen( "input=" ) ) )
869 p_sys->i_selected_input = strtol( psz_parser + strlen( "input=" ),
872 else if( !strncmp( psz_parser, "audio-input=", strlen( "audio-input=" ) ) )
874 p_sys->i_selected_audio_input = strtol( psz_parser + strlen( "audio-input=" ),
877 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
879 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
882 else if( !strncmp( psz_parser, "io=", strlen( "io=" ) ) )
884 psz_parser += strlen( "io=" );
885 if( !strncmp( psz_parser, "read", strlen( "read" ) ) )
887 p_sys->io = IO_METHOD_READ;
888 psz_parser += strlen( "read" );
890 else if( !strncmp( psz_parser, "mmap", strlen( "mmap" ) ) )
892 p_sys->io = IO_METHOD_MMAP;
893 psz_parser += strlen( "mmap" );
895 else if( !strncmp( psz_parser, "userptr", strlen( "userptr" ) ) )
897 p_sys->io = IO_METHOD_USERPTR;
898 psz_parser += strlen( "userptr" );
902 p_sys->io = strtol( psz_parser, &psz_parser, 0 );
905 else if( !strncmp( psz_parser, "width=",
906 strlen( "width=" ) ) )
909 strtol( psz_parser + strlen( "width=" ),
912 else if( !strncmp( psz_parser, "height=",
913 strlen( "height=" ) ) )
916 strtol( psz_parser + strlen( "height=" ),
919 else if( !strncmp( psz_parser, "controls-reset",
920 strlen( "controls-reset" ) ) )
922 var_SetBool( p_obj, "v4l2-controls-reset", VLC_TRUE );
923 psz_parser += strlen( "controls-reset" );
926 else if( !strncmp( psz_parser, "brightness=",
927 strlen( "brightness=" ) ) )
929 var_SetInteger( p_obj, "brightness",
930 strtol( psz_parser + strlen( "brightness=" ),
933 else if( !strncmp( psz_parser, "contrast=",
934 strlen( "contrast=" ) ) )
936 var_SetInteger( p_obj, "contrast",
937 strtol( psz_parser + strlen( "contrast=" ),
940 else if( !strncmp( psz_parser, "saturation=",
941 strlen( "saturation=" ) ) )
943 var_SetInteger( p_obj, "saturation",
944 strtol( psz_parser + strlen( "saturation=" ),
947 else if( !strncmp( psz_parser, "hue=",
950 var_SetInteger( p_obj, "hue",
951 strtol( psz_parser + strlen( "hue=" ),
954 else if( !strncmp( psz_parser, "gamma=",
955 strlen( "gamma=" ) ) )
957 var_SetInteger( p_obj, "gamma",
958 strtol( psz_parser + strlen( "gamma=" ),
962 else if( !strncmp( psz_parser, "samplerate=",
963 strlen( "samplerate=" ) ) )
965 p_sys->i_sample_rate =
966 strtol( psz_parser + strlen( "samplerate=" ),
969 else if( !strncmp( psz_parser, "audio-method", strlen( "audio-method" ) ) )
971 p_sys->i_audio_method =
972 strtol( psz_parser + strlen( "audio-method" ),
975 else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
977 psz_parser += strlen( "stereo" );
978 p_sys->b_stereo = VLC_TRUE;
980 else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
982 psz_parser += strlen( "mono" );
983 p_sys->b_stereo = VLC_FALSE;
985 else if( !strncmp( psz_parser, "caching=", strlen( "caching=" ) ) )
987 p_sys->i_pts = strtol( psz_parser + strlen( "caching=" ),
990 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
992 p_sys->i_cur_tuner = strtol( psz_parser + strlen( "tuner=" ),
995 else if( !strncmp( psz_parser, "tuner-frequency=", strlen( "tuner-frequency=" ) ) )
997 p_sys->i_frequency = strtol( psz_parser
998 + strlen( "tuner-frequency=" ),
1001 else if( !strncmp( psz_parser, "tuner-audio-mode=", strlen( "tuner-audio-mode=" ) ) )
1003 p_sys->i_audio_mode = strtol( psz_parser
1004 + strlen( "tuner-audio-mode=" ),
1007 else if( !strncmp( psz_parser, "set-ctrls=", strlen( "set-ctrls=" )) )
1011 psz_parser += strlen( "set-ctrls=" );
1012 if( strchr( psz_parser, ':' ) )
1014 i_len = strchr( psz_parser, ':' ) - psz_parser;
1018 i_len = strlen( psz_parser );
1021 p_sys->psz_set_ctrls = strndup( psz_parser, i_len );
1023 psz_parser += i_len;
1027 char *psz_unk = strchr( psz_parser, ':' );
1029 psz_unk = strndup( psz_parser, psz_unk - psz_parser );
1031 psz_unk = strdup( psz_parser);
1032 msg_Warn( p_obj, "unknown option %s", psz_unk );
1036 while( *psz_parser && *psz_parser != ':' )
1041 if( *psz_parser == '\0' )
1051 p_sys->psz_device = strdup( psz_dup );
1053 if( psz_dup ) free( psz_dup );
1056 /*****************************************************************************
1057 * Close: close device, free resources
1058 *****************************************************************************/
1059 static void AccessClose( vlc_object_t *p_this )
1061 access_t *p_access = (access_t *)p_this;
1062 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1064 CommonClose( p_this, p_sys );
1067 static void DemuxClose( vlc_object_t *p_this )
1069 struct v4l2_buffer buf;
1070 enum v4l2_buf_type buf_type;
1073 demux_t *p_demux = (demux_t *)p_this;
1074 demux_sys_t *p_sys = p_demux->p_sys;
1076 /* Stop video capture */
1077 if( p_sys->i_fd_video >= 0 )
1081 case IO_METHOD_READ:
1085 case IO_METHOD_MMAP:
1086 case IO_METHOD_USERPTR:
1087 /* Some drivers 'hang' internally if this is not done before streamoff */
1088 for( unsigned int i = 0; i < p_sys->i_nbuffers; i++ )
1090 memset( &buf, 0, sizeof(buf) );
1091 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1092 buf.memory = ( p_sys->io == IO_METHOD_USERPTR ) ?
1093 V4L2_MEMORY_USERPTR : V4L2_MEMORY_MMAP;
1094 ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ); /* ignore result */
1097 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1098 if( ioctl( p_sys->i_fd_video, VIDIOC_STREAMOFF, &buf_type ) < 0 ) {
1099 msg_Err( p_this, "VIDIOC_STREAMOFF failed" );
1106 /* Free Video Buffers */
1107 if( p_sys->p_buffers ) {
1110 case IO_METHOD_READ:
1111 free( p_sys->p_buffers[0].start );
1114 case IO_METHOD_MMAP:
1115 for( i = 0; i < p_sys->i_nbuffers; ++i )
1117 if( munmap( p_sys->p_buffers[i].start, p_sys->p_buffers[i].length ) )
1119 msg_Err( p_this, "munmap failed" );
1124 case IO_METHOD_USERPTR:
1125 for( i = 0; i < p_sys->i_nbuffers; ++i )
1127 free( p_sys->p_buffers[i].orig_userp );
1131 free( p_sys->p_buffers );
1134 CommonClose( p_this, p_sys );
1137 static void CommonClose( vlc_object_t *p_this, demux_sys_t *p_sys )
1140 if( p_sys->i_fd_video >= 0 ) close( p_sys->i_fd_video );
1142 if( p_sys->p_alsa_pcm )
1144 snd_pcm_close( p_sys->p_alsa_pcm );
1145 p_sys->i_fd_audio = -1;
1148 if( p_sys->i_fd_audio >= 0 ) close( p_sys->i_fd_audio );
1150 if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
1151 free( p_sys->psz_device );
1152 free( p_sys->psz_vdev );
1153 free( p_sys->psz_adev );
1154 free( p_sys->p_standards );
1155 free( p_sys->p_inputs );
1156 free( p_sys->p_tuners );
1157 free( p_sys->p_codecs );
1158 free( p_sys->psz_requested_chroma );
1159 free( p_sys->psz_set_ctrls );
1164 /*****************************************************************************
1165 * AccessOpen: opens v4l2 device, access2 callback
1166 *****************************************************************************
1168 * url: <video device>::::
1170 *****************************************************************************/
1171 static int AccessOpen( vlc_object_t * p_this )
1173 access_t *p_access = (access_t*) p_this;
1174 demux_sys_t * p_sys;
1176 /* Only when selected */
1177 if( *p_access->psz_access == '\0' ) return VLC_EGENERIC;
1179 p_access->pf_read = AccessRead;
1180 p_access->pf_block = NULL;
1181 p_access->pf_seek = NULL;
1182 p_access->pf_control = AccessControl;
1183 p_access->info.i_update = 0;
1184 p_access->info.i_size = 0;
1185 p_access->info.i_pos = 0;
1186 p_access->info.b_eof = VLC_FALSE;
1187 p_access->info.i_title = 0;
1188 p_access->info.i_seekpoint = 0;
1190 p_sys = calloc( 1, sizeof( demux_sys_t ) );
1191 p_access->p_sys = (access_sys_t *) p_sys;
1192 if( p_sys == NULL ) return VLC_ENOMEM;
1194 GetV4L2Params( p_sys, (vlc_object_t *) p_access );
1196 ParseMRL( p_sys, p_access->psz_path, (vlc_object_t *) p_access );
1198 if( FindMainDevice( p_this, p_sys, FIND_VIDEO,
1199 VLC_FALSE, !strncmp( p_access->psz_access, "v4l2", 4 ) ) != VLC_SUCCESS )
1201 AccessClose( p_this );
1202 return VLC_EGENERIC;
1208 /*****************************************************************************
1210 *****************************************************************************/
1211 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
1213 demux_sys_t *p_sys = p_demux->p_sys;
1219 /* Special for access_demux */
1220 case DEMUX_CAN_PAUSE:
1221 case DEMUX_CAN_SEEK:
1222 case DEMUX_SET_PAUSE_STATE:
1223 case DEMUX_CAN_CONTROL_PACE:
1224 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1228 case DEMUX_GET_PTS_DELAY:
1229 pi64 = (int64_t*)va_arg( args, int64_t * );
1230 *pi64 = (int64_t)p_sys->i_pts * 1000;
1233 case DEMUX_GET_TIME:
1234 pi64 = (int64_t*)va_arg( args, int64_t * );
1238 /* TODO implement others */
1240 return VLC_EGENERIC;
1243 return VLC_EGENERIC;
1246 /*****************************************************************************
1247 * AccessControl: access2 callback
1248 *****************************************************************************/
1249 static int AccessControl( access_t *p_access, int i_query, va_list args )
1251 vlc_bool_t *pb_bool;
1254 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1259 case ACCESS_CAN_SEEK:
1260 case ACCESS_CAN_FASTSEEK:
1261 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1262 *pb_bool = VLC_FALSE;
1264 case ACCESS_CAN_PAUSE:
1265 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1266 *pb_bool = VLC_FALSE;
1268 case ACCESS_CAN_CONTROL_PACE:
1269 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1270 *pb_bool = VLC_FALSE;
1274 case ACCESS_GET_MTU:
1275 pi_int = (int*)va_arg( args, int * );
1279 case ACCESS_GET_PTS_DELAY:
1280 pi_64 = (int64_t*)va_arg( args, int64_t * );
1281 *pi_64 = (int64_t) p_sys->i_pts * 1000;
1285 case ACCESS_SET_PAUSE_STATE:
1289 case ACCESS_GET_TITLE_INFO:
1290 case ACCESS_SET_TITLE:
1291 case ACCESS_SET_SEEKPOINT:
1292 case ACCESS_SET_PRIVATE_ID_STATE:
1293 case ACCESS_GET_CONTENT_TYPE:
1294 case ACCESS_GET_META:
1295 return VLC_EGENERIC;
1298 msg_Warn( p_access, "Unimplemented query in control(%d).", i_query);
1299 return VLC_EGENERIC;
1305 /*****************************************************************************
1306 * AccessRead: access2 callback
1307 ******************************************************************************/
1308 static ssize_t AccessRead( access_t * p_access, uint8_t * p_buffer, size_t i_len )
1310 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1314 ufd.fd = p_sys->i_fd_video;
1315 ufd.events = POLLIN;
1317 if( p_access->info.b_eof )
1322 if( p_access->b_die )
1327 while( ( i_ret = poll( &ufd, 1, 500 ) ) == 0 );
1331 msg_Err( p_access, "Polling error (%m)." );
1335 i_ret = read( p_sys->i_fd_video, p_buffer, i_len );
1338 p_access->info.b_eof = VLC_TRUE;
1340 else if( i_ret > 0 )
1342 p_access->info.i_pos += i_ret;
1348 /*****************************************************************************
1349 * Demux: Processes the audio or video frame
1350 *****************************************************************************/
1351 static int Demux( demux_t *p_demux )
1353 demux_sys_t *p_sys = p_demux->p_sys;
1354 es_out_id_t *p_es = p_sys->p_es_audio;
1355 block_t *p_block = NULL;
1357 /* Try grabbing audio frames first */
1358 if( p_sys->i_fd_audio < 0 || !( p_block = GrabAudio( p_demux ) ) )
1360 /* Try grabbing video frame */
1361 p_es = p_sys->p_es_video;
1362 if( p_sys->i_fd_video > 0 ) p_block = GrabVideo( p_demux );
1367 /* Sleep so we do not consume all the cpu, 10ms seems
1368 * like a good value (100fps) */
1373 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
1374 es_out_Send( p_demux->out, p_es, p_block );
1379 /*****************************************************************************
1380 * GrabVideo: Grab a video frame
1381 *****************************************************************************/
1382 static block_t* GrabVideo( demux_t *p_demux )
1384 demux_sys_t *p_sys = p_demux->p_sys;
1386 block_t *p_block = NULL;
1387 struct v4l2_buffer buf;
1390 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1392 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1394 /* Did we wait long enough ? (frame rate reduction) */
1395 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1398 /* Grab Video Frame */
1401 case IO_METHOD_READ:
1402 i_ret = read( p_sys->i_fd_video, p_sys->p_buffers[0].start, p_sys->p_buffers[0].length );
1410 /* Could ignore EIO, see spec. */
1413 msg_Err( p_demux, "Failed to read frame" );
1418 p_block = ProcessVideoFrame( p_demux, (uint8_t*)p_sys->p_buffers[0].start, i_ret );
1419 if( !p_block ) return 0;
1423 case IO_METHOD_MMAP:
1424 memset( &buf, 0, sizeof(buf) );
1425 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1426 buf.memory = V4L2_MEMORY_MMAP;
1428 /* Wait for next frame */
1429 if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
1436 /* Could ignore EIO, see spec. */
1439 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1444 if( buf.index >= p_sys->i_nbuffers ) {
1445 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1449 p_block = ProcessVideoFrame( p_demux, p_sys->p_buffers[buf.index].start, buf.bytesused );
1450 if( !p_block ) return 0;
1453 if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
1455 msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
1461 case IO_METHOD_USERPTR:
1462 memset( &buf, 0, sizeof(buf) );
1463 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1464 buf.memory = V4L2_MEMORY_USERPTR;
1466 /* Wait for next frame */
1467 if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
1474 /* Could ignore EIO, see spec. */
1477 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1484 for( i = 0; i < p_sys->i_nbuffers; i++ )
1486 if( buf.m.userptr == (unsigned long)p_sys->p_buffers[i].start &&
1487 buf.length == p_sys->p_buffers[i].length ) break;
1490 if( i >= p_sys->i_nbuffers )
1492 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1496 p_block = ProcessVideoFrame( p_demux, (uint8_t*)buf.m.userptr, buf.bytesused );
1497 if( !p_block ) return 0;
1500 if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
1502 msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
1511 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();
1516 /*****************************************************************************
1517 * ProcessVideoFrame: Helper function to take a buffer and copy it into
1519 *****************************************************************************/
1520 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t i_size )
1524 if( !p_frame ) return 0;
1527 if( !( p_block = block_New( p_demux, i_size ) ) )
1529 msg_Warn( p_demux, "Cannot get new block" );
1534 memcpy( p_block->p_buffer, p_frame, i_size );
1539 /*****************************************************************************
1540 * GrabAudio: Grab an audio frame
1541 *****************************************************************************/
1542 static block_t* GrabAudio( demux_t *p_demux )
1544 demux_sys_t *p_sys = p_demux->p_sys;
1545 struct audio_buf_info buf_info;
1546 int i_read = 0, i_correct;
1549 if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
1550 else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
1554 msg_Warn( p_demux, "cannot get block" );
1558 p_sys->p_block_audio = p_block;
1561 if( p_sys->i_audio_method & AUDIO_METHOD_ALSA )
1564 i_read = snd_pcm_readi( p_sys->p_alsa_pcm, p_block->p_buffer, p_sys->i_alsa_chunk_size );
1574 snd_pcm_prepare( p_sys->p_alsa_pcm );
1578 i_resume = snd_pcm_resume( p_sys->p_alsa_pcm );
1579 if( i_resume < 0 && i_resume != -EAGAIN ) snd_pcm_prepare( p_sys->p_alsa_pcm );
1582 msg_Err( p_demux, "Failed to read alsa frame (%s)", snd_strerror( i_read ) );
1588 /* convert from frames to bytes */
1589 i_read *= p_sys->i_alsa_frame_size;
1594 if( p_sys->i_audio_method & AUDIO_METHOD_OSS )
1597 i_read = read( p_sys->i_fd_audio, p_block->p_buffer,
1598 p_sys->i_audio_max_frame_size );
1601 if( i_read <= 0 ) return 0;
1603 p_block->i_buffer = i_read;
1604 p_sys->p_block_audio = 0;
1606 /* Correct the date because of kernel buffering */
1608 if( p_sys->i_audio_method & AUDIO_METHOD_OSS )
1611 if( ioctl( p_sys->i_fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
1613 i_correct += buf_info.bytes;
1617 else if( p_sys->i_audio_method & AUDIO_METHOD_ALSA )
1621 snd_pcm_sframes_t delay = 0;
1622 if( ( i_err = snd_pcm_delay( p_sys->p_alsa_pcm, &delay ) ) >= 0 )
1624 size_t i_correction_delta = delay * p_sys->i_alsa_frame_size;
1625 /* Test for overrun */
1626 if( i_correction_delta > p_sys->i_audio_max_frame_size )
1628 msg_Warn( p_demux, "ALSA read overrun (%d > %d)",
1629 i_correction_delta, p_sys->i_audio_max_frame_size );
1630 i_correction_delta = p_sys->i_audio_max_frame_size;
1631 snd_pcm_prepare( p_sys->p_alsa_pcm );
1633 i_correct += i_correction_delta;
1637 /* delay failed so reset */
1638 msg_Warn( p_demux, "ALSA snd_pcm_delay failed (%s)", snd_strerror( i_err ) );
1639 snd_pcm_prepare( p_sys->p_alsa_pcm );
1645 p_block->i_pts = p_block->i_dts =
1646 mdate() - I64C(1000000) * (mtime_t)i_correct /
1647 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
1652 /*****************************************************************************
1653 * Helper function to initalise video IO using the Read method
1654 *****************************************************************************/
1655 static int InitRead( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1657 demux_sys_t *p_sys = p_demux->p_sys;
1659 p_sys->p_buffers = calloc( 1, sizeof( *p_sys->p_buffers ) );
1660 if( !p_sys->p_buffers )
1662 msg_Err( p_demux, "Out of memory" );
1666 p_sys->p_buffers[0].length = i_buffer_size;
1667 p_sys->p_buffers[0].start = malloc( i_buffer_size );
1668 if( !p_sys->p_buffers[0].start )
1670 msg_Err( p_demux, "Out of memory" );
1677 return VLC_EGENERIC;
1681 /*****************************************************************************
1682 * Helper function to initalise video IO using the mmap method
1683 *****************************************************************************/
1684 static int InitMmap( demux_t *p_demux, int i_fd )
1686 demux_sys_t *p_sys = p_demux->p_sys;
1687 struct v4l2_requestbuffers req;
1689 memset( &req, 0, sizeof(req) );
1691 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1692 req.memory = V4L2_MEMORY_MMAP;
1694 if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1696 msg_Err( p_demux, "device does not support mmap i/o" );
1702 msg_Err( p_demux, "Insufficient buffer memory" );
1706 p_sys->p_buffers = calloc( req.count, sizeof( *p_sys->p_buffers ) );
1707 if( !p_sys->p_buffers )
1709 msg_Err( p_demux, "Out of memory" );
1713 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < req.count; ++p_sys->i_nbuffers )
1715 struct v4l2_buffer buf;
1717 memset( &buf, 0, sizeof(buf) );
1718 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1719 buf.memory = V4L2_MEMORY_MMAP;
1720 buf.index = p_sys->i_nbuffers;
1722 if( ioctl( i_fd, VIDIOC_QUERYBUF, &buf ) < 0 )
1724 msg_Err( p_demux, "VIDIOC_QUERYBUF" );
1728 p_sys->p_buffers[p_sys->i_nbuffers].length = buf.length;
1729 p_sys->p_buffers[p_sys->i_nbuffers].start =
1730 mmap( NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, buf.m.offset );
1732 if( p_sys->p_buffers[p_sys->i_nbuffers].start == MAP_FAILED )
1734 msg_Err( p_demux, "mmap failed (%m)" );
1742 return VLC_EGENERIC;
1746 /*****************************************************************************
1747 * Helper function to initalise video IO using the userbuf method
1748 *****************************************************************************/
1749 static int InitUserP( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1751 demux_sys_t *p_sys = p_demux->p_sys;
1752 struct v4l2_requestbuffers req;
1753 unsigned int i_page_size;
1755 i_page_size = getpagesize();
1756 i_buffer_size = ( i_buffer_size + i_page_size - 1 ) & ~( i_page_size - 1);
1758 memset( &req, 0, sizeof(req) );
1760 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1761 req.memory = V4L2_MEMORY_USERPTR;
1763 if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1765 msg_Err( p_demux, "device does not support user pointer i/o" );
1769 p_sys->p_buffers = calloc( 4, sizeof( *p_sys->p_buffers ) );
1770 if( !p_sys->p_buffers )
1772 msg_Err( p_demux, "Out of memory" );
1776 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < 4; ++p_sys->i_nbuffers )
1778 p_sys->p_buffers[p_sys->i_nbuffers].length = i_buffer_size;
1779 p_sys->p_buffers[p_sys->i_nbuffers].start =
1780 vlc_memalign( &p_sys->p_buffers[p_sys->i_nbuffers].orig_userp,
1781 /* boundary */ i_page_size, i_buffer_size );
1783 if( !p_sys->p_buffers[p_sys->i_nbuffers].start )
1785 msg_Err( p_demux, "out of memory" );
1793 return VLC_EGENERIC;
1797 /*****************************************************************************
1798 * IsPixelFormatSupported: returns true if the specified V4L2 pixel format is
1799 * in the array of supported formats returned by the driver
1800 *****************************************************************************/
1801 static vlc_bool_t IsPixelFormatSupported( demux_t *p_demux, unsigned int i_pixelformat )
1803 demux_sys_t *p_sys = p_demux->p_sys;
1805 for( int i_index = 0; i_index < p_sys->i_codec; i_index++ )
1807 if( p_sys->p_codecs[i_index].pixelformat == i_pixelformat )
1814 /*****************************************************************************
1815 * OpenVideoDev: open and set up the video device and probe for capabilities
1816 *****************************************************************************/
1817 static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, vlc_bool_t b_demux )
1820 struct v4l2_cropcap cropcap;
1821 struct v4l2_crop crop;
1822 struct v4l2_format fmt;
1824 enum v4l2_buf_type buf_type;
1825 char *psz_device = p_sys->psz_vdev;
1827 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1829 msg_Err( p_obj, "cannot open device (%m)" );
1833 /* Tune the tuner */
1834 if( p_sys->i_frequency >= 0 )
1836 if( p_sys->i_cur_tuner < 0 || p_sys->i_cur_tuner >= p_sys->i_tuner )
1838 msg_Err( p_obj, "invalid tuner %d.", p_sys->i_cur_tuner );
1841 struct v4l2_frequency frequency;
1842 memset( &frequency, 0, sizeof( frequency ) );
1843 frequency.tuner = p_sys->i_cur_tuner;
1844 frequency.type = p_sys->p_tuners[p_sys->i_cur_tuner].type;
1845 frequency.frequency = p_sys->i_frequency / 62.5;
1846 if( ioctl( i_fd, VIDIOC_S_FREQUENCY, &frequency ) < 0 )
1848 msg_Err( p_obj, "cannot set tuner frequency (%m)" );
1851 msg_Dbg( p_obj, "Tuner frequency set" );
1854 /* Set the tuner's audio mode */
1855 if( p_sys->i_audio_mode >= 0 )
1857 if( p_sys->i_cur_tuner < 0 || p_sys->i_cur_tuner >= p_sys->i_tuner )
1859 msg_Err( p_obj, "invalid tuner %d.", p_sys->i_cur_tuner );
1862 struct v4l2_tuner tuner;
1863 memset( &tuner, 0, sizeof( tuner ) );
1864 tuner.index = p_sys->i_cur_tuner;
1865 tuner.audmode = p_sys->i_audio_mode;
1866 if( ioctl( i_fd, VIDIOC_S_TUNER, &tuner ) < 0 )
1868 msg_Err( p_obj, "cannot set tuner audio mode (%m)" );
1871 msg_Dbg( p_obj, "Tuner audio mode set" );
1874 /* Select standard */
1876 if( p_sys->i_selected_standard_id != V4L2_STD_UNKNOWN )
1878 if( ioctl( i_fd, VIDIOC_S_STD, &p_sys->i_selected_standard_id ) < 0 )
1880 msg_Err( p_obj, "cannot set standard (%m)" );
1883 msg_Dbg( p_obj, "Set standard" );
1888 if( p_sys->i_selected_input >= p_sys->i_input )
1890 msg_Warn( p_obj, "invalid input. Using the default one" );
1891 p_sys->i_selected_input = 0;
1894 if( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
1896 msg_Err( p_obj, "cannot set input (%m)" );
1900 /* Set audio input */
1902 if( p_sys->i_audio > 0 )
1904 if( p_sys->i_selected_audio_input < 0
1905 || p_sys->i_selected_audio_input >= p_sys->i_audio )
1907 msg_Warn( p_obj, "invalid audio input. Using the default one" );
1908 p_sys->i_selected_audio_input = 0;
1911 if( ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_selected_audio_input] ) < 0 )
1913 msg_Err( p_obj, "cannot set audio input (%m)" );
1916 msg_Dbg( p_obj, "Audio input set to %d", p_sys->i_selected_audio_input );
1920 /* TODO: Move the resolution stuff up here */
1921 /* if MPEG encoder card, no need to do anything else after this */
1922 ControlList( p_obj, i_fd,
1923 var_GetBool( p_obj, "v4l2-controls-reset" ), b_demux );
1924 SetAvailControlsByString( p_obj, p_sys, i_fd );
1925 if( VLC_FALSE == b_demux)
1930 demux_t *p_demux = (demux_t *) p_obj;
1932 /* Verify device support for the various IO methods */
1935 case IO_METHOD_READ:
1936 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE) )
1938 msg_Err( p_demux, "device does not support read i/o" );
1943 case IO_METHOD_MMAP:
1944 case IO_METHOD_USERPTR:
1945 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING) )
1947 msg_Err( p_demux, "device does not support streaming i/o" );
1953 msg_Err( p_demux, "io method not supported" );
1957 /* Reset Cropping */
1958 memset( &cropcap, 0, sizeof(cropcap) );
1959 cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1960 if( ioctl( i_fd, VIDIOC_CROPCAP, &cropcap ) >= 0 )
1962 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1963 crop.c = cropcap.defrect; /* reset to default */
1964 if( ioctl( i_fd, VIDIOC_S_CROP, &crop ) < 0 )
1969 /* Cropping not supported. */
1972 /* Errors ignored. */
1978 /* Try and find default resolution if not specified */
1979 memset( &fmt, 0, sizeof(fmt) );
1980 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1982 if( p_sys->i_width <= 0 || p_sys->i_height <= 0 )
1984 if( ioctl( i_fd, VIDIOC_G_FMT, &fmt ) < 0 )
1986 msg_Err( p_demux, "Cannot get default width and height." );
1990 p_sys->i_width = fmt.fmt.pix.width;
1991 p_sys->i_height = fmt.fmt.pix.height;
1993 if( fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE )
1995 p_sys->i_height = p_sys->i_height * 2;
2000 msg_Dbg( p_demux, "trying specified size %dx%d", p_sys->i_width, p_sys->i_height );
2003 fmt.fmt.pix.width = p_sys->i_width;
2004 fmt.fmt.pix.height = p_sys->i_height;
2005 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
2007 /* Test and set Chroma */
2008 fmt.fmt.pix.pixelformat = 0;
2009 if( p_sys->psz_requested_chroma && strlen( p_sys->psz_requested_chroma ) > 0 )
2011 /* User specified chroma */
2012 if( strlen( p_sys->psz_requested_chroma ) >= 4 )
2014 int i_requested_fourcc = VLC_FOURCC(
2015 p_sys->psz_requested_chroma[0], p_sys->psz_requested_chroma[1],
2016 p_sys->psz_requested_chroma[2], p_sys->psz_requested_chroma[3] );
2017 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
2019 if( v4l2chroma_to_fourcc[i].i_fourcc == i_requested_fourcc )
2021 fmt.fmt.pix.pixelformat = v4l2chroma_to_fourcc[i].i_v4l2;
2026 /* Try and set user chroma */
2027 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ( fmt.fmt.pix.pixelformat && ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 ) )
2029 msg_Warn( p_demux, "Driver is unable to use specified chroma %s. Trying defaults.", p_sys->psz_requested_chroma );
2030 fmt.fmt.pix.pixelformat = 0;
2034 /* If no user specified chroma, find best */
2035 /* This also decides if MPEG encoder card or not */
2036 if( !fmt.fmt.pix.pixelformat )
2039 for( i = 0; i < ARRAY_SIZE( p_chroma_fallbacks ); i++ )
2041 fmt.fmt.pix.pixelformat = p_chroma_fallbacks[i];
2042 if( IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat )
2043 && ioctl( i_fd, VIDIOC_S_FMT, &fmt ) >= 0 )
2046 if( i == ARRAY_SIZE( p_chroma_fallbacks ) )
2048 msg_Warn( p_demux, "Could not select any of the default chromas; attempting to open as MPEG encoder card (access2)" );
2053 /* Reassign width, height and chroma incase driver override */
2054 p_sys->i_width = fmt.fmt.pix.width;
2055 p_sys->i_height = fmt.fmt.pix.height;
2057 /* Look up final fourcc */
2058 p_sys->i_fourcc = 0;
2059 for( int i = 0; v4l2chroma_to_fourcc[i].i_fourcc != 0; i++ )
2061 if( v4l2chroma_to_fourcc[i].i_v4l2 == fmt.fmt.pix.pixelformat )
2063 p_sys->i_fourcc = v4l2chroma_to_fourcc[i].i_fourcc;
2068 /* Buggy driver paranoia */
2069 i_min = fmt.fmt.pix.width * 2;
2070 if( fmt.fmt.pix.bytesperline < i_min )
2071 fmt.fmt.pix.bytesperline = i_min;
2072 i_min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
2073 if( fmt.fmt.pix.sizeimage < i_min )
2074 fmt.fmt.pix.sizeimage = i_min;
2076 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
2077 /* This is new in Linux 2.6.19 */
2078 /* List supported frame rates */
2079 struct v4l2_frmivalenum frmival;
2081 frmival.pixel_format = fmt.fmt.pix.pixelformat;
2082 frmival.width = p_sys->i_width;
2083 frmival.height = p_sys->i_height;
2084 if( ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 )
2087 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
2088 vlc_fourcc_to_char( p_sys->i_fourcc, &psz_fourcc );
2089 msg_Dbg( p_demux, "supported frame intervals for %4s, %dx%d:",
2090 psz_fourcc, frmival.width, frmival.height );
2091 switch( frmival.type )
2093 case V4L2_FRMIVAL_TYPE_DISCRETE:
2096 msg_Dbg( p_demux, " supported frame interval: %d/%d",
2097 frmival.discrete.numerator,
2098 frmival.discrete.denominator );
2100 } while( ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 );
2102 case V4L2_FRMIVAL_TYPE_STEPWISE:
2103 msg_Dbg( p_demux, " supported frame intervals: %d/%d to "
2104 "%d/%d using %d/%d increments",
2105 frmival.stepwise.min.numerator,
2106 frmival.stepwise.min.denominator,
2107 frmival.stepwise.max.numerator,
2108 frmival.stepwise.max.denominator,
2109 frmival.stepwise.step.numerator,
2110 frmival.stepwise.step.denominator );
2112 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
2113 msg_Dbg( p_demux, " supported frame intervals: %d/%d to %d/%d",
2114 frmival.stepwise.min.numerator,
2115 frmival.stepwise.min.denominator,
2116 frmival.stepwise.max.numerator,
2117 frmival.stepwise.max.denominator );
2123 /* Init IO method */
2126 case IO_METHOD_READ:
2127 if( InitRead( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
2130 case IO_METHOD_MMAP:
2131 if( InitMmap( p_demux, i_fd ) != VLC_SUCCESS ) goto open_failed;
2134 case IO_METHOD_USERPTR:
2135 if( InitUserP( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
2142 es_format_Init( &es_fmt, VIDEO_ES, p_sys->i_fourcc );
2143 es_fmt.video.i_width = p_sys->i_width;
2144 es_fmt.video.i_height = p_sys->i_height;
2145 es_fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
2147 /* Setup rgb mask for RGB formats */
2148 if( p_sys->i_fourcc == VLC_FOURCC( 'R','V','2','4' ) )
2150 /* This is in BGR format */
2151 es_fmt.video.i_bmask = 0x00ff0000;
2152 es_fmt.video.i_gmask = 0x0000ff00;
2153 es_fmt.video.i_rmask = 0x000000ff;
2156 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
2157 (char*)&es_fmt.i_codec, es_fmt.video.i_width, es_fmt.video.i_height );
2158 p_sys->p_es_video = es_out_Add( p_demux->out, &es_fmt );
2164 case IO_METHOD_READ:
2168 case IO_METHOD_MMAP:
2169 for (unsigned int i = 0; i < p_sys->i_nbuffers; ++i)
2171 struct v4l2_buffer buf;
2173 memset( &buf, 0, sizeof(buf) );
2174 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2175 buf.memory = V4L2_MEMORY_MMAP;
2178 if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
2180 msg_Err( p_demux, "VIDIOC_QBUF failed" );
2185 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2186 if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
2188 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
2194 case IO_METHOD_USERPTR:
2195 for( unsigned int i = 0; i < p_sys->i_nbuffers; ++i )
2197 struct v4l2_buffer buf;
2199 memset( &buf, 0, sizeof(buf) );
2200 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2201 buf.memory = V4L2_MEMORY_USERPTR;
2203 buf.m.userptr = (unsigned long)p_sys->p_buffers[i].start;
2204 buf.length = p_sys->p_buffers[i].length;
2206 if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
2208 msg_Err( p_demux, "VIDIOC_QBUF failed" );
2213 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2214 if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
2216 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
2224 if( p_sys->f_fps >= 0.1 )
2226 msg_Dbg( p_demux, "User set fps=%f", p_sys->f_fps );
2232 if( i_fd >= 0 ) close( i_fd );
2238 /*****************************************************************************
2239 * ResolveALSADeviceName: Change any . to : in the ALSA device name
2240 *****************************************************************************/
2241 static char *ResolveALSADeviceName( const char *psz_device )
2243 char* psz_alsa_name = strdup( psz_device );
2244 for( unsigned int i = 0; i < strlen( psz_device ); i++ )
2246 if( psz_alsa_name[i] == '.' ) psz_alsa_name[i] = ':';
2248 return psz_alsa_name;
2252 /*****************************************************************************
2253 * OpenAudioDev: open and set up the audio device and probe for capabilities
2254 *****************************************************************************/
2256 static int OpenAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys,
2257 vlc_bool_t b_demux )
2259 char *psz_device = p_sys->psz_adev;
2261 p_sys->p_alsa_pcm = NULL;
2262 char* psz_alsa_device_name = NULL;
2263 snd_pcm_hw_params_t *p_hw_params = NULL;
2264 snd_pcm_uframes_t buffer_size;
2265 snd_pcm_uframes_t chunk_size;
2269 psz_alsa_device_name =
2270 ResolveALSADeviceName( psz_device?: ALSA_DEFAULT );
2272 if( ( i_err = snd_pcm_open( &p_sys->p_alsa_pcm, psz_alsa_device_name,
2273 SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0)
2275 msg_Err( p_this, "Cannot open ALSA audio device %s (%s)",
2276 psz_alsa_device_name, snd_strerror( i_err ) );
2280 if( ( i_err = snd_pcm_nonblock( p_sys->p_alsa_pcm, 1 ) ) < 0)
2282 msg_Err( p_this, "Cannot set ALSA nonblock (%s)",
2283 snd_strerror( i_err ) );
2287 /* Begin setting hardware parameters */
2289 if( ( i_err = snd_pcm_hw_params_malloc( &p_hw_params ) ) < 0 )
2292 "ALSA: cannot allocate hardware parameter structure (%s)",
2293 snd_strerror( i_err ) );
2297 if( ( i_err = snd_pcm_hw_params_any( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
2300 "ALSA: cannot initialize hardware parameter structure (%s)",
2301 snd_strerror( i_err ) );
2305 /* Set Interleaved access */
2306 if( ( i_err = snd_pcm_hw_params_set_access( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 )
2308 msg_Err( p_this, "ALSA: cannot set access type (%s)",
2309 snd_strerror( i_err ) );
2313 /* Set 16 bit little endian */
2314 if( ( i_err = snd_pcm_hw_params_set_format( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_FORMAT_S16_LE ) ) < 0 )
2316 msg_Err( p_this, "ALSA: cannot set sample format (%s)",
2317 snd_strerror( i_err ) );
2321 /* Set sample rate */
2322 #ifdef HAVE_ALSA_NEW_API
2323 i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, &p_sys->i_sample_rate, NULL );
2325 i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, p_sys->i_sample_rate, NULL );
2329 msg_Err( p_this, "ALSA: cannot set sample rate (%s)",
2330 snd_strerror( i_err ) );
2335 unsigned int channels = p_sys->b_stereo ? 2 : 1;
2336 if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
2338 channels = ( channels==1 ) ? 2 : 1;
2339 msg_Warn( p_this, "ALSA: cannot set channel count (%s). "
2340 "Trying with channels=%d",
2341 snd_strerror( i_err ),
2343 if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
2345 msg_Err( p_this, "ALSA: cannot set channel count (%s)",
2346 snd_strerror( i_err ) );
2349 p_sys->b_stereo = ( channels == 2 );
2352 /* Set metrics for buffer calculations later */
2353 unsigned int buffer_time;
2354 if( ( i_err = snd_pcm_hw_params_get_buffer_time_max(p_hw_params, &buffer_time, 0) ) < 0 )
2356 msg_Err( p_this, "ALSA: cannot get buffer time max (%s)",
2357 snd_strerror( i_err ) );
2360 if (buffer_time > 500000) buffer_time = 500000;
2362 /* Set period time */
2363 unsigned int period_time = buffer_time / 4;
2364 #ifdef HAVE_ALSA_NEW_API
2365 i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, &period_time, 0 );
2367 i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, period_time, 0 );
2371 msg_Err( p_this, "ALSA: cannot set period time (%s)",
2372 snd_strerror( i_err ) );
2376 /* Set buffer time */
2377 #ifdef HAVE_ALSA_NEW_API
2378 i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, &buffer_time, 0 );
2380 i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, buffer_time, 0 );
2384 msg_Err( p_this, "ALSA: cannot set buffer time (%s)",
2385 snd_strerror( i_err ) );
2389 /* Apply new hardware parameters */
2390 if( ( i_err = snd_pcm_hw_params( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
2392 msg_Err( p_this, "ALSA: cannot set hw parameters (%s)",
2393 snd_strerror( i_err ) );
2397 /* Get various buffer metrics */
2398 snd_pcm_hw_params_get_period_size( p_hw_params, &chunk_size, 0 );
2399 snd_pcm_hw_params_get_buffer_size( p_hw_params, &buffer_size );
2400 if( chunk_size == buffer_size )
2403 "ALSA: period cannot equal buffer size (%lu == %lu)",
2404 chunk_size, buffer_size);
2408 int bits_per_sample = snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE);
2409 int bits_per_frame = bits_per_sample * channels;
2411 p_sys->i_alsa_chunk_size = chunk_size;
2412 p_sys->i_alsa_frame_size = bits_per_frame / 8;
2413 p_sys->i_audio_max_frame_size = chunk_size * bits_per_frame / 8;
2415 snd_pcm_hw_params_free( p_hw_params );
2419 if( ( i_err = snd_pcm_prepare( p_sys->p_alsa_pcm ) ) < 0 )
2422 "ALSA: cannot prepare audio interface for use (%s)",
2423 snd_strerror( i_err ) );
2427 /* Return a fake handle so other tests work */
2430 free( psz_alsa_device_name );
2432 if( !p_sys->psz_adev )
2433 p_sys->psz_adev = strdup( ALSA_DEFAULT );
2438 if( i_fd >= 0 ) close( i_fd );
2440 if( p_hw_params ) snd_pcm_hw_params_free( p_hw_params );
2441 if( p_sys->p_alsa_pcm ) snd_pcm_close( p_sys->p_alsa_pcm );
2442 free( psz_alsa_device_name );
2449 static int OpenAudioDevOss( vlc_object_t *p_this, demux_sys_t *p_sys,
2450 vlc_bool_t b_demux )
2452 char *psz_device = p_sys->psz_adev;
2456 if( !psz_device ) psz_device = strdup( OSS_DEFAULT ); /* FIXME leak */
2458 if( (i_fd = open( psz_device, O_RDONLY | O_NONBLOCK )) < 0 )
2460 msg_Err( p_this, "cannot open OSS audio device (%m)" );
2464 i_format = AFMT_S16_LE;
2465 if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
2466 || i_format != AFMT_S16_LE )
2469 "cannot set audio format (16b little endian) (%m)" );
2473 if( ioctl( i_fd, SNDCTL_DSP_STEREO,
2474 &p_sys->b_stereo ) < 0 )
2476 msg_Err( p_this, "cannot set audio channels count (%m)" );
2480 if( ioctl( i_fd, SNDCTL_DSP_SPEED,
2481 &p_sys->i_sample_rate ) < 0 )
2483 msg_Err( p_this, "cannot set audio sample rate (%m)" );
2487 p_sys->i_audio_max_frame_size = 6 * 1024;
2489 if( !p_sys->psz_adev )
2490 p_sys->psz_adev = strdup( OSS_DEFAULT );
2495 if( i_fd >= 0 ) close( i_fd );
2500 static int OpenAudioDev( vlc_object_t *p_this, demux_sys_t *p_sys,
2501 vlc_bool_t b_demux )
2507 if( ( p_sys->i_audio_method & AUDIO_METHOD_ALSA ) && i_fd < 0 )
2508 i_fd = OpenAudioDevAlsa( p_this, p_sys, b_demux );
2511 if( ( p_sys->i_audio_method & AUDIO_METHOD_OSS ) && i_fd < 0 )
2512 i_fd = OpenAudioDevOss( p_this, p_sys, b_demux );
2517 psz_device = p_sys->psz_adev;
2519 msg_Dbg( p_this, "opened adev=`%s' %s %dHz",
2520 psz_device, p_sys->b_stereo ? "stereo" : "mono",
2521 p_sys->i_sample_rate );
2524 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
2526 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
2527 fmt.audio.i_rate = p_sys->i_sample_rate;
2528 fmt.audio.i_bitspersample = 16;
2529 fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
2530 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
2532 msg_Dbg( p_this, "new audio es %d channels %dHz",
2533 fmt.audio.i_channels, fmt.audio.i_rate );
2537 demux_t *p_demux = (demux_t *)p_this;
2538 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
2544 /*****************************************************************************
2545 * ProbeVideoDev: probe video for capabilities
2546 *****************************************************************************/
2547 static vlc_bool_t ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
2555 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
2557 msg_Err( p_obj, "cannot open video device (%m)" );
2561 /* Get device capabilites */
2563 if( ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
2565 msg_Err( p_obj, "cannot get video capabilities (%m)" );
2569 msg_Dbg( p_obj, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
2570 p_sys->dev_cap.card,
2571 p_sys->dev_cap.driver,
2572 (p_sys->dev_cap.version >> 16) & 0xFF,
2573 (p_sys->dev_cap.version >> 8) & 0xFF,
2574 p_sys->dev_cap.version & 0xFF,
2575 p_sys->dev_cap.bus_info );
2577 msg_Dbg( p_obj, "the device has the capabilities: (%c) Video Capure, "
2580 ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE ? 'X':' '),
2581 ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO ? 'X':' '),
2582 ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER ? 'X':' ') );
2584 msg_Dbg( p_obj, "supported I/O methods are: (%c) Read/Write, "
2586 "(%c) Asynchronous",
2587 ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
2588 ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
2589 ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
2591 /* Now, enumerate all the video inputs. This is useless at the moment
2592 since we have no way to present that info to the user except with
2595 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2597 struct v4l2_input t_input;
2599 while( ioctl( i_fd, VIDIOC_ENUMINPUT, &t_input ) >= 0 )
2602 t_input.index = p_sys->i_input;
2605 p_sys->p_inputs = calloc( 1, p_sys->i_input * sizeof( struct v4l2_input ) );
2606 if( !p_sys->p_inputs ) goto open_failed;
2608 for( i_index = 0; i_index < p_sys->i_input; i_index++ )
2610 p_sys->p_inputs[i_index].index = i_index;
2612 if( ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
2614 msg_Err( p_obj, "cannot get video input characteristics (%m)" );
2617 msg_Dbg( p_obj, "video input %i (%s) has type: %s %c",
2619 p_sys->p_inputs[i_index].name,
2620 p_sys->p_inputs[i_index].type
2621 == V4L2_INPUT_TYPE_TUNER ?
2623 "External analog input",
2624 i_index == p_sys->i_selected_input ? '*' : ' ' );
2628 /* Probe video standards */
2629 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2631 struct v4l2_standard t_standards;
2632 t_standards.index = 0;
2633 while( ioctl( i_fd, VIDIOC_ENUMSTD, &t_standards ) >=0 )
2635 p_sys->i_standard++;
2636 t_standards.index = p_sys->i_standard;
2639 p_sys->p_standards = calloc( 1, p_sys->i_standard * sizeof( struct v4l2_standard ) );
2640 if( !p_sys->p_standards ) goto open_failed;
2642 for( i_standard = 0; i_standard < p_sys->i_standard; i_standard++ )
2644 p_sys->p_standards[i_standard].index = i_standard;
2646 if( ioctl( i_fd, VIDIOC_ENUMSTD, &p_sys->p_standards[i_standard] ) )
2648 msg_Err( p_obj, "cannot get video input standards (%m)" );
2651 msg_Dbg( p_obj, "video standard %i is: %s %c",
2653 p_sys->p_standards[i_standard].name,
2654 (unsigned)i_standard == p_sys->i_selected_standard_id ? '*' : ' ' );
2658 /* initialize the structures for the ioctls */
2659 for( i_index = 0; i_index < 32; i_index++ )
2661 p_sys->p_audios[i_index].index = i_index;
2664 /* Probe audio inputs */
2665 if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
2667 while( p_sys->i_audio < 32 &&
2668 ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
2670 if( ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
2672 msg_Err( p_obj, "cannot get audio input characteristics (%m)" );
2676 msg_Dbg( p_obj, "audio input %i (%s) is %s %s %c",
2678 p_sys->p_audios[p_sys->i_audio].name,
2679 p_sys->p_audios[p_sys->i_audio].capability &
2680 V4L2_AUDCAP_STEREO ?
2682 p_sys->p_audios[p_sys->i_audio].capability &
2684 "(Automatic Volume Level supported)" : "",
2685 p_sys->i_audio == p_sys->i_selected_audio_input ? '*' : ' ' );
2691 /* List tuner caps */
2692 if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
2694 struct v4l2_tuner tuner;
2695 memset( &tuner, 0, sizeof(tuner) );
2696 while( ioctl( i_fd, VIDIOC_G_TUNER, &tuner ) >= 0 )
2699 memset( &tuner, 0, sizeof(tuner) );
2700 tuner.index = p_sys->i_tuner;
2703 p_sys->p_tuners = calloc( 1, p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
2704 if( !p_sys->p_tuners ) goto open_failed;
2706 for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
2708 p_sys->p_tuners[i_index].index = i_index;
2710 if( ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
2712 msg_Err( p_obj, "cannot get tuner characteristics (%m)" );
2715 msg_Dbg( p_obj, "tuner %i (%s) has type: %s, "
2716 "frequency range: %.1f %s -> %.1f %s",
2718 p_sys->p_tuners[i_index].name,
2719 p_sys->p_tuners[i_index].type
2720 == V4L2_TUNER_RADIO ?
2721 "Radio" : "Analog TV",
2722 p_sys->p_tuners[i_index].rangelow * 62.5,
2723 p_sys->p_tuners[i_index].capability &
2724 V4L2_TUNER_CAP_LOW ?
2726 p_sys->p_tuners[i_index].rangehigh * 62.5,
2727 p_sys->p_tuners[i_index].capability &
2728 V4L2_TUNER_CAP_LOW ?
2731 struct v4l2_frequency frequency;
2732 memset( &frequency, 0, sizeof( frequency ) );
2733 if( ioctl( i_fd, VIDIOC_G_FREQUENCY, &frequency ) < 0 )
2735 msg_Err( p_obj, "cannot get tuner frequency (%m)" );
2738 msg_Dbg( p_obj, "tuner %i (%s) frequency: %.1f %s",
2740 p_sys->p_tuners[i_index].name,
2741 frequency.frequency * 62.5,
2742 p_sys->p_tuners[i_index].capability &
2743 V4L2_TUNER_CAP_LOW ?
2748 /* Probe for available chromas */
2749 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2751 struct v4l2_fmtdesc codec;
2754 memset( &codec, 0, sizeof(codec) );
2755 codec.index = i_index;
2756 codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2758 while( ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
2761 codec.index = i_index;
2764 p_sys->i_codec = i_index;
2766 p_sys->p_codecs = calloc( 1, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
2768 for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
2770 p_sys->p_codecs[i_index].index = i_index;
2771 p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2773 if( ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
2775 msg_Err( p_obj, "cannot get codec description (%m)" );
2779 /* only print if vlc supports the format */
2780 char psz_fourcc_v4l2[5];
2781 memset( &psz_fourcc_v4l2, 0, sizeof( psz_fourcc_v4l2 ) );
2782 vlc_fourcc_to_char( p_sys->p_codecs[i_index].pixelformat,
2784 vlc_bool_t b_codec_supported = VLC_FALSE;
2785 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
2787 if( v4l2chroma_to_fourcc[i].i_v4l2 == p_sys->p_codecs[i_index].pixelformat )
2789 b_codec_supported = VLC_TRUE;
2792 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
2793 vlc_fourcc_to_char( v4l2chroma_to_fourcc[i].i_fourcc,
2795 msg_Dbg( p_obj, "device supports chroma %4s [%s, %s]",
2797 p_sys->p_codecs[i_index].description,
2800 #ifdef VIDIOC_ENUM_FRAMESIZES
2801 /* This is new in Linux 2.6.19 */
2802 /* List valid frame sizes for this format */
2803 struct v4l2_frmsizeenum frmsize;
2805 frmsize.pixel_format = p_sys->p_codecs[i_index].pixelformat;
2806 if( ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) < 0 )
2808 /* Not all devices support this ioctl */
2809 msg_Warn( p_obj, "Unable to query for frame sizes" );
2813 switch( frmsize.type )
2815 case V4L2_FRMSIZE_TYPE_DISCRETE:
2819 " device supports size %dx%d",
2820 frmsize.discrete.width, frmsize.discrete.height );
2822 } while( ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 );
2824 case V4L2_FRMSIZE_TYPE_STEPWISE:
2826 " device supports sizes %dx%d to %dx%d using %dx%d increments",
2827 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2828 frmsize.stepwise.max_width, frmsize.stepwise.max_height,
2829 frmsize.stepwise.step_width, frmsize.stepwise.step_height );
2831 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
2833 " device supports all sizes %dx%d to %dx%d",
2834 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2835 frmsize.stepwise.max_width, frmsize.stepwise.max_height );
2842 if( !b_codec_supported )
2845 "device codec %4s (%s) not supported as access_demux",
2847 p_sys->p_codecs[i_index].description );
2854 if( i_fd >= 0 ) close( i_fd );
2859 if( i_fd >= 0 ) close( i_fd );
2864 /*****************************************************************************
2865 * ProbeAudioDev: probe audio for capabilities
2866 *****************************************************************************/
2868 static vlc_bool_t ProbeAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys,
2872 snd_pcm_t *p_alsa_pcm;
2873 char* psz_alsa_device_name = ResolveALSADeviceName( psz_device ?: ALSA_DEFAULT );
2875 if( ( i_err = snd_pcm_open( &p_alsa_pcm, psz_alsa_device_name, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0 )
2877 msg_Err( p_this, "cannot open device %s for ALSA audio (%s)", psz_alsa_device_name, snd_strerror( i_err ) );
2878 free( psz_alsa_device_name );
2882 snd_pcm_close( p_alsa_pcm );
2883 free( psz_alsa_device_name );
2889 static vlc_bool_t ProbeAudioDevOss( vlc_object_t *p_this, demux_sys_t *p_sys,
2894 if( !psz_device ) psz_device = strdup( OSS_DEFAULT ); /* FIXME leak */
2896 if( ( i_fd = open( psz_device, O_RDONLY | O_NONBLOCK ) ) < 0 )
2898 msg_Err( p_this, "cannot open device %s for OSS audio (%m)", psz_device );
2902 /* this will fail if the device is video */
2903 if( ioctl( i_fd, SNDCTL_DSP_GETCAPS, &i_caps ) < 0 )
2905 msg_Err( p_this, "cannot get audio caps (%m)" );
2909 if( i_fd >= 0 ) close( i_fd );
2914 if( i_fd >= 0 ) close( i_fd );
2918 static vlc_bool_t ProbeAudioDev( vlc_object_t *p_this, demux_sys_t *p_sys,
2922 if( ( p_sys->i_audio_method & AUDIO_METHOD_ALSA )
2923 && ProbeAudioDevAlsa( p_this, p_sys, psz_device ) )
2925 p_sys->i_audio_method = AUDIO_METHOD_ALSA;
2930 if( ( p_sys->i_audio_method & AUDIO_METHOD_OSS )
2931 && ProbeAudioDevOss( p_this, p_sys, psz_device ) )
2933 p_sys->i_audio_method = AUDIO_METHOD_OSS;
2937 p_sys->i_audio_method = 0;
2941 static void name2var( unsigned char *name )
2943 for( ; *name; name++ )
2944 *name = (*name == ' ') ? '_' : tolower( *name );
2947 /*****************************************************************************
2948 * Print a user-class v4l2 control's details, create the relevant variable,
2949 * change the value if needed.
2950 *****************************************************************************/
2951 static void ControlListPrint( vlc_object_t *p_obj, int i_fd,
2952 struct v4l2_queryctrl queryctrl,
2953 vlc_bool_t b_reset, vlc_bool_t b_demux )
2955 struct v4l2_querymenu querymenu;
2962 vlc_value_t val, val2;
2964 if( queryctrl.flags & V4L2_CTRL_FLAG_GRABBED )
2965 msg_Dbg( p_obj, " control is busy" );
2966 if( queryctrl.flags & V4L2_CTRL_FLAG_READ_ONLY )
2967 msg_Dbg( p_obj, " control is read-only" );
2969 for( i = 0; controls[i].psz_name != NULL; i++ )
2970 if( controls[i].i_cid == queryctrl.id ) break;
2972 if( controls[i].psz_name )
2974 psz_name = strdup( controls[i].psz_name );
2975 char psz_cfg_name[40];
2976 sprintf( psz_cfg_name, CFG_PREFIX "%s", psz_name );
2977 i_val = var_CreateGetInteger( p_obj, psz_cfg_name );
2978 var_Destroy( p_obj, psz_cfg_name );
2982 psz_name = strdup( (const char *)queryctrl.name );
2983 name2var( (unsigned char *)psz_name );
2987 switch( queryctrl.type )
2989 case V4L2_CTRL_TYPE_INTEGER:
2990 msg_Dbg( p_obj, " integer control" );
2992 " valid values: %d to %d by steps of %d",
2993 queryctrl.minimum, queryctrl.maximum,
2996 var_Create( p_obj, psz_name,
2997 VLC_VAR_INTEGER | VLC_VAR_HASMIN | VLC_VAR_HASMAX
2998 | VLC_VAR_HASSTEP | VLC_VAR_ISCOMMAND );
2999 val.i_int = queryctrl.minimum;
3000 var_Change( p_obj, psz_name, VLC_VAR_SETMIN, &val, NULL );
3001 val.i_int = queryctrl.maximum;
3002 var_Change( p_obj, psz_name, VLC_VAR_SETMAX, &val, NULL );
3003 val.i_int = queryctrl.step;
3004 var_Change( p_obj, psz_name, VLC_VAR_SETSTEP, &val, NULL );
3006 case V4L2_CTRL_TYPE_BOOLEAN:
3007 msg_Dbg( p_obj, " boolean control" );
3008 var_Create( p_obj, psz_name,
3009 VLC_VAR_BOOL | VLC_VAR_ISCOMMAND );
3011 case V4L2_CTRL_TYPE_MENU:
3012 msg_Dbg( p_obj, " menu control" );
3013 var_Create( p_obj, psz_name,
3014 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE
3015 | VLC_VAR_ISCOMMAND );
3016 memset( &querymenu, 0, sizeof( querymenu ) );
3017 for( i_mid = queryctrl.minimum;
3018 i_mid <= (unsigned)queryctrl.maximum;
3021 querymenu.index = i_mid;
3022 querymenu.id = queryctrl.id;
3023 if( ioctl( i_fd, VIDIOC_QUERYMENU, &querymenu ) >= 0 )
3025 msg_Dbg( p_obj, " %d: %s",
3026 querymenu.index, querymenu.name );
3027 val.i_int = querymenu.index;
3028 val2.psz_string = (char *)querymenu.name;
3029 var_Change( p_obj, psz_name,
3030 VLC_VAR_ADDCHOICE, &val, &val2 );
3034 case V4L2_CTRL_TYPE_BUTTON:
3035 msg_Dbg( p_obj, " button control" );
3036 var_Create( p_obj, psz_name,
3037 VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
3039 case V4L2_CTRL_TYPE_CTRL_CLASS:
3040 msg_Dbg( p_obj, " control class" );
3041 var_Create( p_obj, psz_name, VLC_VAR_VOID );
3044 msg_Dbg( p_obj, " unknown control type (FIXME)" );
3049 switch( queryctrl.type )
3051 case V4L2_CTRL_TYPE_INTEGER:
3052 case V4L2_CTRL_TYPE_BOOLEAN:
3053 case V4L2_CTRL_TYPE_MENU:
3055 struct v4l2_control control;
3056 msg_Dbg( p_obj, " default value: %d",
3057 queryctrl.default_value );
3058 memset( &control, 0, sizeof( control ) );
3059 control.id = queryctrl.id;
3060 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
3062 msg_Dbg( p_obj, " current value: %d", control.value );
3066 i_val = control.value;
3067 if( b_reset && queryctrl.default_value != control.value )
3069 msg_Dbg( p_obj, " reset value to default" );
3070 Control( p_obj, i_fd, psz_name,
3071 queryctrl.id, queryctrl.default_value );
3076 Control( p_obj, i_fd, psz_name,
3077 queryctrl.id, i_val );
3085 val.psz_string = (char *)queryctrl.name;
3086 var_Change( p_obj, psz_name, VLC_VAR_SETTEXT, &val, NULL );
3087 val.i_int = queryctrl.id;
3088 val2.psz_string = (char *)psz_name;
3089 var_Change( p_obj, "allcontrols", VLC_VAR_ADDCHOICE, &val, &val2 );
3090 /* bad things happen changing MPEG mid-stream
3091 * so don't add to Ext Settings GUI */
3092 if( V4L2_CTRL_ID2CLASS( queryctrl.id ) != V4L2_CTRL_CLASS_MPEG )
3093 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
3095 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
3098 var_SetBool( p_obj, psz_name, i_val );
3100 case VLC_VAR_INTEGER:
3101 var_SetInteger( p_obj, psz_name, i_val );
3106 msg_Warn( p_obj, "FIXME: %s %s %d", __FILE__, __func__,
3112 var_AddCallback( p_obj, psz_name,
3113 DemuxControlCallback, (void*)queryctrl.id );
3115 var_AddCallback( p_obj, psz_name,
3116 AccessControlCallback, (void*)queryctrl.id );
3121 /*****************************************************************************
3122 * List all user-class v4l2 controls, set them to the user specified
3123 * value and create the relevant variables to enable runtime changes
3124 *****************************************************************************/
3125 static int ControlList( vlc_object_t *p_obj, int i_fd,
3126 vlc_bool_t b_reset, vlc_bool_t b_demux )
3128 struct v4l2_queryctrl queryctrl;
3131 memset( &queryctrl, 0, sizeof( queryctrl ) );
3133 /* A list of available controls (aka the variable name) will be
3134 * stored as choices in the "allcontrols" variable. We'll thus be able
3135 * to use those to create an appropriate interface
3136 * A list of available controls that can be changed mid-stream will
3137 * be stored in the "controls" variable */
3138 var_Create( p_obj, "controls", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
3139 var_Create( p_obj, "allcontrols", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
3141 var_Create( p_obj, "controls-update", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
3143 /* Add a control to reset all controls to their default values */
3144 vlc_value_t val, val2;
3145 var_Create( p_obj, "controls-reset", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
3146 val.psz_string = _( "Reset controls to default" );
3147 var_Change( p_obj, "controls-reset", VLC_VAR_SETTEXT, &val, NULL );
3149 val2.psz_string = (char *)"controls-reset";
3150 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
3152 var_AddCallback( p_obj, "controls-reset", DemuxControlResetCallback, NULL );
3154 var_AddCallback( p_obj, "controls-reset", AccessControlResetCallback, NULL );
3156 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
3157 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3159 msg_Dbg( p_obj, "Extended control API supported by v4l2 driver" );
3161 /* List extended controls */
3162 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
3163 while( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3165 if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS )
3167 msg_Dbg( p_obj, "%s", queryctrl.name );
3168 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
3171 switch( V4L2_CTRL_ID2CLASS( queryctrl.id ) )
3173 case V4L2_CTRL_CLASS_USER:
3174 msg_Dbg( p_obj, "Available control: %s (%x)",
3175 queryctrl.name, queryctrl.id );
3177 case V4L2_CTRL_CLASS_MPEG:
3178 name2var( queryctrl.name );
3179 msg_Dbg( p_obj, "Available MPEG control: %s (%x)",
3180 queryctrl.name, queryctrl.id );
3183 msg_Dbg( p_obj, "Available private control: %s (%x)",
3184 queryctrl.name, queryctrl.id );
3187 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
3188 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
3193 msg_Dbg( p_obj, "Extended control API not supported by v4l2 driver" );
3195 /* List public controls */
3196 for( i_cid = V4L2_CID_BASE;
3197 i_cid < V4L2_CID_LASTP1;
3200 queryctrl.id = i_cid;
3201 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3203 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3205 msg_Dbg( p_obj, "Available control: %s (%x)",
3206 queryctrl.name, queryctrl.id );
3207 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
3211 /* List private controls */
3212 for( i_cid = V4L2_CID_PRIVATE_BASE;
3216 queryctrl.id = i_cid;
3217 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3219 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3221 msg_Dbg( p_obj, "Available private control: %s (%x)",
3222 queryctrl.name, queryctrl.id );
3223 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
3233 static void SetAvailControlsByString( vlc_object_t *p_obj, demux_sys_t *p_sys,
3236 char *psz_parser = p_sys->psz_set_ctrls;
3237 vlc_value_t val, text, name;
3239 if( psz_parser == NULL )
3242 if( *psz_parser == '{' )
3245 int i_ret = var_Change( p_obj, "allcontrols", VLC_VAR_GETCHOICES,
3249 msg_Err( p_obj, "Oops, can't find 'allcontrols' variable." );
3253 while( *psz_parser && *psz_parser != '}' )
3255 char *psz_delim, *psz_assign;
3257 while( *psz_parser == ',' || *psz_parser == ' ' )
3260 psz_delim = strchr( psz_parser, ',' );
3261 if( psz_delim == NULL )
3262 psz_delim = strchr( psz_parser, '}' );
3263 if( psz_delim == NULL )
3264 psz_delim = psz_parser + strlen( psz_parser );
3266 psz_assign = memchr( psz_parser, '=', psz_delim - psz_parser );
3267 if( psz_assign == NULL )
3269 char *psz_name = strndup( psz_parser, psz_delim - psz_parser );
3270 msg_Err( p_obj, "%s missing '='", psz_name );
3272 psz_parser = psz_delim + 1;
3277 i < val.p_list->i_count ;//&& psz_parser < psz_assign;
3280 const char *psz_var = text.p_list->p_values[i].psz_string;
3281 int i_cid = val.p_list->p_values[i].i_int;
3282 var_Change( p_obj, psz_var, VLC_VAR_GETTEXT, &name, NULL );
3283 const char *psz_name = name.psz_string;
3285 int i_availstrlen = strlen( psz_name );
3286 int i_parsestrlen = psz_assign - psz_parser;
3287 int i_maxstrlen = __MAX( i_availstrlen, i_parsestrlen);
3289 if( !strncasecmp( psz_name, psz_parser, i_maxstrlen ) )
3291 Control( p_obj, i_fd, psz_name, i_cid,
3292 strtol( ++psz_assign, &psz_parser, 0) );
3296 if( psz_parser < psz_assign )
3298 char *psz_name = strndup( psz_parser, psz_assign - psz_parser );
3299 msg_Err( p_obj, "Control %s not available", psz_name );
3301 psz_parser = ( *psz_delim ) ? ( psz_delim + 1 ) : psz_delim;
3306 /*****************************************************************************
3307 * Reset all user-class v4l2 controls to their default value
3308 *****************************************************************************/
3309 static int ControlReset( vlc_object_t *p_obj, int i_fd )
3311 struct v4l2_queryctrl queryctrl;
3313 memset( &queryctrl, 0, sizeof( queryctrl ) );
3315 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
3316 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3318 /* Extended control API supported */
3319 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
3320 while( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3322 if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS
3323 || V4L2_CTRL_ID2CLASS( queryctrl.id ) == V4L2_CTRL_CLASS_MPEG )
3325 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
3328 struct v4l2_control control;
3329 memset( &control, 0, sizeof( control ) );
3330 control.id = queryctrl.id;
3331 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
3332 && queryctrl.default_value != control.value )
3335 for( i = 0; controls[i].psz_name != NULL; i++ )
3336 if( controls[i].i_cid == queryctrl.id ) break;
3337 name2var( queryctrl.name );
3338 Control( p_obj, i_fd,
3339 controls[i].psz_name ? : (const char *)queryctrl.name,
3340 queryctrl.id, queryctrl.default_value );
3342 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
3348 /* public controls */
3349 for( i_cid = V4L2_CID_BASE;
3350 i_cid < V4L2_CID_LASTP1;
3353 queryctrl.id = i_cid;
3354 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3356 struct v4l2_control control;
3357 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3359 memset( &control, 0, sizeof( control ) );
3360 control.id = queryctrl.id;
3361 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
3362 && queryctrl.default_value != control.value )
3365 for( i = 0; controls[i].psz_name != NULL; i++ )
3366 if( controls[i].i_cid == queryctrl.id ) break;
3367 name2var( queryctrl.name );
3368 Control( p_obj, i_fd,
3369 controls[i].psz_name ? : (const char *)queryctrl.name,
3370 queryctrl.id, queryctrl.default_value );
3375 /* private controls */
3376 for( i_cid = V4L2_CID_PRIVATE_BASE;
3380 queryctrl.id = i_cid;
3381 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3383 struct v4l2_control control;
3384 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3386 memset( &control, 0, sizeof( control ) );
3387 control.id = queryctrl.id;
3388 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
3389 && queryctrl.default_value != control.value )
3391 name2var( queryctrl.name );
3392 Control( p_obj, i_fd, (const char *)queryctrl.name,
3393 queryctrl.id, queryctrl.default_value );
3403 /*****************************************************************************
3404 * Issue user-class v4l2 controls
3405 *****************************************************************************/
3406 static int Control( vlc_object_t *p_obj, int i_fd,
3407 const char *psz_name, int i_cid, int i_value )
3409 struct v4l2_queryctrl queryctrl;
3410 struct v4l2_control control;
3411 struct v4l2_ext_control ext_control;
3412 struct v4l2_ext_controls ext_controls;
3417 memset( &queryctrl, 0, sizeof( queryctrl ) );
3419 queryctrl.id = i_cid;
3421 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) < 0
3422 || queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3424 msg_Dbg( p_obj, "%s (%x) control is not supported.", psz_name, i_cid );
3425 return VLC_EGENERIC;
3428 memset( &control, 0, sizeof( control ) );
3429 memset( &ext_control, 0, sizeof( ext_control ) );
3430 memset( &ext_controls, 0, sizeof( ext_controls ) );
3432 ext_control.id = i_cid;
3433 ext_controls.ctrl_class = V4L2_CTRL_ID2CLASS( i_cid );
3434 ext_controls.count = 1;
3435 ext_controls.controls = &ext_control;
3441 ext_control.value = i_value;
3442 if( ioctl( i_fd, VIDIOC_S_EXT_CTRLS, &ext_controls ) < 0 )
3444 control.value = i_value;
3445 if( ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
3447 msg_Err( p_obj, "unable to set %s (%x) to %d (%m)",
3448 psz_name, i_cid, i_value );
3449 return VLC_EGENERIC;
3451 i_ret = ioctl( i_fd, VIDIOC_G_CTRL, &control );
3455 i_ret = ioctl( i_fd, VIDIOC_G_EXT_CTRLS, &ext_controls );
3456 control.value = ext_control.value;
3463 msg_Dbg( p_obj, "video %s: %d", psz_name, control.value );
3464 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
3467 val.b_bool = control.value;
3468 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
3469 var_SetVoid( p_obj, "controls-update" );
3471 case VLC_VAR_INTEGER:
3472 val.i_int = control.value;
3473 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
3474 var_SetVoid( p_obj, "controls-update" );
3481 /*****************************************************************************
3482 * On the fly change settings callback
3483 *****************************************************************************/
3484 static int DemuxControlCallback( vlc_object_t *p_this,
3485 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3488 demux_t *p_demux = (demux_t*)p_this;
3489 demux_sys_t *p_sys = p_demux->p_sys;
3490 int i_cid = (int)p_data;
3492 int i_fd = p_sys->i_fd_video;
3495 return VLC_EGENERIC;
3497 Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
3499 return VLC_EGENERIC;
3502 static int DemuxControlResetCallback( vlc_object_t *p_this,
3503 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3506 demux_t *p_demux = (demux_t*)p_this;
3507 demux_sys_t *p_sys = p_demux->p_sys;
3509 int i_fd = p_sys->i_fd_video;
3512 return VLC_EGENERIC;
3514 ControlReset( p_this, i_fd );
3516 return VLC_EGENERIC;
3519 static int AccessControlCallback( vlc_object_t *p_this,
3520 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3523 access_t *p_access = (access_t *)p_this;
3524 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
3525 int i_cid = (int)p_data;
3527 int i_fd = p_sys->i_fd_video;
3530 return VLC_EGENERIC;
3532 Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
3534 return VLC_EGENERIC;
3537 static int AccessControlResetCallback( vlc_object_t *p_this,
3538 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3541 access_t *p_access = (access_t *)p_this;
3542 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
3544 int i_fd = p_sys->i_fd_video;
3547 return VLC_EGENERIC;
3549 ControlReset( p_this, i_fd );
3551 return VLC_EGENERIC;