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 (Usually, 0 = tuner, " \
90 "1 = composite, 2 = svideo)." )
91 #define IOMETHOD_TEXT N_( "IO Method" )
92 #define IOMETHOD_LONGTEXT N_( \
93 "IO Method (READ, MMAP, USERPTR)." )
94 #define WIDTH_TEXT N_( "Width" )
95 #define WIDTH_LONGTEXT N_( \
96 "Force width (-1 for autodetect)." )
97 #define HEIGHT_TEXT N_( "Height" )
98 #define HEIGHT_LONGTEXT N_( \
99 "Force height (-1 for autodetect)." )
100 #define FPS_TEXT N_( "Framerate" )
101 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
102 "(-1 for autodetect)." )
104 #define CTRL_RESET_TEXT N_( "Reset v4l2 controls" )
105 #define CTRL_RESET_LONGTEXT N_( \
106 "Reset controls to defaults provided by the v4l2 driver." )
107 #define BRIGHTNESS_TEXT N_( "Brightness" )
108 #define BRIGHTNESS_LONGTEXT N_( \
109 "Brightness of the video input (if supported by the v4l2 driver)." )
110 #define CONTRAST_TEXT N_( "Contrast" )
111 #define CONTRAST_LONGTEXT N_( \
112 "Contrast of the video input (if supported by the v4l2 driver)." )
113 #define SATURATION_TEXT N_( "Saturation" )
114 #define SATURATION_LONGTEXT N_( \
115 "Saturation of the video input (if supported by the v4l2 driver)." )
116 #define HUE_TEXT N_( "Hue" )
117 #define HUE_LONGTEXT N_( \
118 "Hue of the video input (if supported by the v4l2 driver)." )
119 #define BLACKLEVEL_TEXT N_( "Black level" )
120 #define BLACKLEVEL_LONGTEXT N_( \
121 "Black level of the video input (if supported by the v4l2 driver)." )
122 #define AUTOWHITEBALANCE_TEXT N_( "Auto white balance" )
123 #define AUTOWHITEBALANCE_LONGTEXT N_( \
124 "Automatically set the white balance of the video input " \
125 "(if supported by the v4l2 driver)." )
126 #define DOWHITEBALANCE_TEXT N_( "Do white balance" )
127 #define DOWHITEBALANCE_LONGTEXT N_( \
128 "Trigger a white balancing action, useless if auto white balance is " \
129 "activated (if supported by the v4l2 driver)." )
130 #define REDBALANCE_TEXT N_( "Red balance" )
131 #define REDBALANCE_LONGTEXT N_( \
132 "Red balance of the video input (if supported by the v4l2 driver)." )
133 #define BLUEBALANCE_TEXT N_( "Blue balance" )
134 #define BLUEBALANCE_LONGTEXT N_( \
135 "Blue balance of the video input (if supported by the v4l2 driver)." )
136 #define GAMMA_TEXT N_( "Gamma" )
137 #define GAMMA_LONGTEXT N_( \
138 "Gamma of the video input (if supported by the v4l2 driver)." )
139 #define EXPOSURE_TEXT N_( "Exposure" )
140 #define EXPOSURE_LONGTEXT N_( \
141 "Exposure of the video input (if supported by the v4L2 driver)." )
142 #define AUTOGAIN_TEXT N_( "Auto gain" )
143 #define AUTOGAIN_LONGTEXT N_( \
144 "Automatically set the video input's gain (if supported by the " \
146 #define GAIN_TEXT N_( "Gain" )
147 #define GAIN_LONGTEXT N_( \
148 "Video input's gain (if supported by the v4l2 driver)." )
149 #define HFLIP_TEXT N_( "Horizontal flip" )
150 #define HFLIP_LONGTEXT N_( \
151 "Flip the video horizontally (if supported by the v4l2 driver)." )
152 #define VFLIP_TEXT N_( "Vertical flip" )
153 #define VFLIP_LONGTEXT N_( \
154 "Flip the video vertically (if supported by the v4l2 driver)." )
155 #define HCENTER_TEXT N_( "Horizontal centering" )
156 #define HCENTER_LONGTEXT N_( \
157 "Set the camera's horizontal centering (if supported by the v4l2 driver)." )
158 #define VCENTER_TEXT N_( "Vertical centering" )
159 #define VCENTER_LONGTEXT N_( \
160 "Set the camera's vertical centering (if supported by the v4l2 driver)." )
162 #define ADEV_TEXT N_("Audio device name")
164 #define ADEV_LONGTEXT N_( \
165 "Name of the audio device to use. " \
166 "If you don't specify anything, \"/dev/dsp\" will be used for OSS.")
168 #define ADEV_LONGTEXT N_( \
169 "Name of the audio device to use. " \
170 "If you don't specify anything, \"/dev/dsp\" will be used for OSS, " \
173 #define AUDIO_METHOD_TEXT N_( "Audio method" )
175 #define AUDIO_METHOD_LONGTEXT N_( \
176 "Audio method to use: 0 to disable audio, 1 for OSS." )
178 #define AUDIO_METHOD_LONGTEXT N_( \
179 "Audio method to use: 0 to disable audio, 1 for OSS, 2 for ALSA, " \
180 "3 for ALSA or OSS (ALSA is prefered)." )
182 #define AUDIO_VOLUME_TEXT N_( "Volume" )
183 #define AUDIO_VOLUME_LONGTEXT N_( \
184 "Volume of the audio input (if supported by the v4l2 driver)." )
185 #define AUDIO_BALANCE_TEXT N_( "Balance" )
186 #define AUDIO_BALANCE_LONGTEXT N_( \
187 "Balance of the audio input (if supported by the v4l2 driver)." )
188 #define AUDIO_MUTE_TEXT N_( "Mute" )
189 #define AUDIO_MUTE_LONGTEXT N_( \
190 "Mute audio input (if supported by the v4l2 driver)." )
191 #define AUDIO_BASS_TEXT N_( "Bass" )
192 #define AUDIO_BASS_LONGTEXT N_( \
193 "Bass level of the audio input (if supported by the v4l2 driver)." )
194 #define AUDIO_TREBLE_TEXT N_( "Treble" )
195 #define AUDIO_TREBLE_LONGTEXT N_( \
196 "Treble level of the audio input (if supported by the v4l2 driver)." )
197 #define AUDIO_LOUDNESS_TEXT N_( "Loudness" )
198 #define AUDIO_LOUDNESS_LONGTEXT N_( \
199 "Loudness of the audio input (if supported by the v4l2 driver)." )
201 #define STEREO_TEXT N_( "Stereo" )
202 #define STEREO_LONGTEXT N_( \
203 "Capture the audio stream in stereo." )
204 #define SAMPLERATE_TEXT N_( "Samplerate" )
205 #define SAMPLERATE_LONGTEXT N_( \
206 "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
208 #define CACHING_TEXT N_("Caching value in ms")
209 #define CACHING_LONGTEXT N_( \
210 "Caching value for V4L2 captures. This " \
211 "value should be set in milliseconds." )
212 #define S_CTRLS_TEXT N_("v4l2 driver controls")
213 #define S_CTRLS_LONGTEXT N_( \
214 "Set the v4l2 driver controls to the values specified using a comma " \
215 "separated list optionally encapsulated by curly braces " \
216 "(e.g.: {video_bitrate=6000000,audio_crc=0,stream_type=3} ). " \
217 "To list available controls, increase verbosity (-vvv) " \
218 "or use the v4l2-ctl application." )
220 #define TUNER_TEXT N_("Tuner id")
221 #define TUNER_LONGTEXT N_( \
222 "Tuner id (see debug output)." )
223 #define FREQUENCY_TEXT N_("Frequency")
224 #define FREQUENCY_LONGTEXT N_( \
225 "Tuner frequency in Hz or kHz (see debug output)" )
226 #define TUNER_AUDIO_MODE_TEXT N_("Audio mode")
227 #define TUNER_AUDIO_MODE_LONGTEXT N_( \
228 "Tuner audio mono/stereo and track selection." )
236 static int i_standards_list[] =
237 { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
238 static const char *psz_standards_list_text[] =
239 { N_("Default"), N_("SECAM"), N_("PAL"), N_("NTSC") };
241 static int i_iomethod_list[] =
242 { IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR };
243 static const char *psz_iomethod_list_text[] =
244 { N_("READ"), N_("MMAP"), N_("USERPTR") };
246 static int i_tuner_audio_modes_list[] =
247 { V4L2_TUNER_MODE_MONO, V4L2_TUNER_MODE_STEREO,
248 V4L2_TUNER_MODE_LANG1, V4L2_TUNER_MODE_LANG2,
249 V4L2_TUNER_MODE_SAP, V4L2_TUNER_MODE_LANG1_LANG2 };
250 static const char *psz_tuner_audio_modes_list_text[] =
253 N_( "Primary language (Analog TV tuners only)" ),
254 N_( "Secondary language (Analog TV tuners only)" ),
255 N_( "Second audio program (Analog TV tuners only)" ),
256 N_( "Primary language left, Secondary language right" ) };
261 #define AUDIO_METHOD_OSS 1
262 #define OSS_DEFAULT "/dev/dsp"
263 #define AUDIO_METHOD_ALSA 2
264 #define ALSA_DEFAULT "hw"
265 #define CFG_PREFIX "v4l2-"
268 set_shortname( _("Video4Linux2") );
269 set_description( _("Video4Linux2 input") );
270 set_category( CAT_INPUT );
271 set_subcategory( SUBCAT_INPUT_ACCESS );
273 set_section( N_( "Video input" ), NULL );
274 add_string( CFG_PREFIX "dev", "/dev/video0", 0, DEV_TEXT, DEV_LONGTEXT,
276 add_integer( CFG_PREFIX "standard", 0, NULL, STANDARD_TEXT,
277 STANDARD_LONGTEXT, VLC_FALSE );
278 change_integer_list( i_standards_list, psz_standards_list_text, 0 );
279 add_string( CFG_PREFIX "chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
281 add_integer( CFG_PREFIX "input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
283 add_integer( CFG_PREFIX "io", IO_METHOD_MMAP, NULL, IOMETHOD_TEXT,
284 IOMETHOD_LONGTEXT, VLC_TRUE );
285 change_integer_list( i_iomethod_list, psz_iomethod_list_text, 0 );
286 add_integer( CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
287 WIDTH_LONGTEXT, VLC_TRUE );
288 add_integer( CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
289 HEIGHT_LONGTEXT, VLC_TRUE );
290 add_float( CFG_PREFIX "fps", 0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
292 set_section( N_( "Audio input" ), NULL );
293 add_string( CFG_PREFIX "adev", NULL, 0, ADEV_TEXT, ADEV_LONGTEXT,
295 add_integer( CFG_PREFIX "audio-method", AUDIO_METHOD_OSS|AUDIO_METHOD_ALSA,
296 NULL, AUDIO_METHOD_TEXT, AUDIO_METHOD_LONGTEXT, VLC_TRUE );
297 add_bool( CFG_PREFIX "stereo", VLC_TRUE, NULL, STEREO_TEXT, STEREO_LONGTEXT,
299 add_integer( CFG_PREFIX "samplerate", 48000, NULL, SAMPLERATE_TEXT,
300 SAMPLERATE_LONGTEXT, VLC_TRUE );
301 add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
302 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
304 set_section( N_( "Tuner" ), NULL );
305 add_integer( CFG_PREFIX "tuner", 0, NULL, TUNER_TEXT, TUNER_LONGTEXT,
307 add_integer( CFG_PREFIX "tuner-frequency", -1, NULL, FREQUENCY_TEXT,
308 FREQUENCY_LONGTEXT, VLC_TRUE );
309 add_integer( CFG_PREFIX "tuner-audio-mode", -1, NULL, TUNER_AUDIO_MODE_TEXT,
310 TUNER_AUDIO_MODE_LONGTEXT, VLC_TRUE );
311 change_integer_list( i_tuner_audio_modes_list,
312 psz_tuner_audio_modes_list_text, 0 );
314 set_section( N_( "Controls" ),
315 N_( "v4l2 driver controls, if supported by your v4l2 driver." ) );
316 add_bool( CFG_PREFIX "controls-reset", VLC_FALSE, NULL, CTRL_RESET_TEXT,
317 CTRL_RESET_LONGTEXT, VLC_TRUE );
318 add_integer( CFG_PREFIX "brightness", -1, NULL, BRIGHTNESS_TEXT,
319 BRIGHTNESS_LONGTEXT, VLC_TRUE );
320 add_integer( CFG_PREFIX "contrast", -1, NULL, CONTRAST_TEXT,
321 CONTRAST_LONGTEXT, VLC_TRUE );
322 add_integer( CFG_PREFIX "saturation", -1, NULL, SATURATION_TEXT,
323 SATURATION_LONGTEXT, VLC_TRUE );
324 add_integer( CFG_PREFIX "hue", -1, NULL, HUE_TEXT,
325 HUE_LONGTEXT, VLC_TRUE );
326 add_integer( CFG_PREFIX "black-level", -1, NULL, BLACKLEVEL_TEXT,
327 BLACKLEVEL_LONGTEXT, VLC_TRUE );
328 add_integer( CFG_PREFIX "auto-white-balance", -1, NULL,
329 AUTOWHITEBALANCE_TEXT, AUTOWHITEBALANCE_LONGTEXT, VLC_TRUE );
330 add_integer( CFG_PREFIX "do-white-balance", -1, NULL, DOWHITEBALANCE_TEXT,
331 DOWHITEBALANCE_LONGTEXT, VLC_TRUE );
332 add_integer( CFG_PREFIX "red-balance", -1, NULL, REDBALANCE_TEXT,
333 REDBALANCE_LONGTEXT, VLC_TRUE );
334 add_integer( CFG_PREFIX "blue-balance", -1, NULL, BLUEBALANCE_TEXT,
335 BLUEBALANCE_LONGTEXT, VLC_TRUE );
336 add_integer( CFG_PREFIX "gamma", -1, NULL, GAMMA_TEXT,
337 GAMMA_LONGTEXT, VLC_TRUE );
338 add_integer( CFG_PREFIX "exposure", -1, NULL, EXPOSURE_TEXT,
339 EXPOSURE_LONGTEXT, VLC_TRUE );
340 add_integer( CFG_PREFIX "autogain", -1, NULL, AUTOGAIN_TEXT,
341 AUTOGAIN_LONGTEXT, VLC_TRUE );
342 add_integer( CFG_PREFIX "gain", -1, NULL, GAIN_TEXT,
343 GAIN_LONGTEXT, VLC_TRUE );
344 add_integer( CFG_PREFIX "hflip", -1, NULL, HFLIP_TEXT,
345 HFLIP_LONGTEXT, VLC_TRUE );
346 add_integer( CFG_PREFIX "vflip", -1, NULL, VFLIP_TEXT,
347 VFLIP_LONGTEXT, VLC_TRUE );
348 add_integer( CFG_PREFIX "hcenter", -1, NULL, HCENTER_TEXT,
349 HCENTER_LONGTEXT, VLC_TRUE );
350 add_integer( CFG_PREFIX "vcenter", -1, NULL, VCENTER_TEXT,
351 VCENTER_LONGTEXT, VLC_TRUE );
352 add_integer( CFG_PREFIX "audio-volume", -1, NULL, AUDIO_VOLUME_TEXT,
353 AUDIO_VOLUME_LONGTEXT, VLC_TRUE );
354 add_integer( CFG_PREFIX "audio-balance", -1, NULL, AUDIO_BALANCE_TEXT,
355 AUDIO_BALANCE_LONGTEXT, VLC_TRUE );
356 add_bool( CFG_PREFIX "audio-mute", VLC_FALSE, NULL, AUDIO_MUTE_TEXT,
357 AUDIO_MUTE_LONGTEXT, VLC_TRUE );
358 add_integer( CFG_PREFIX "audio-bass", -1, NULL, AUDIO_BASS_TEXT,
359 AUDIO_BASS_LONGTEXT, VLC_TRUE );
360 add_integer( CFG_PREFIX "audio-treble", -1, NULL, AUDIO_TREBLE_TEXT,
361 AUDIO_TREBLE_LONGTEXT, VLC_TRUE );
362 add_integer( CFG_PREFIX "audio-loudness", -1, NULL, AUDIO_LOUDNESS_TEXT,
363 AUDIO_LOUDNESS_LONGTEXT, VLC_TRUE );
364 add_string( CFG_PREFIX "set-ctrls", NULL, NULL, S_CTRLS_TEXT,
365 S_CTRLS_LONGTEXT, VLC_TRUE );
367 add_shortcut( "v4l2" );
368 set_capability( "access_demux", 10 );
369 set_callbacks( DemuxOpen, DemuxClose );
372 add_shortcut( "v4l2c" );
373 set_description( _("Video4Linux2 Compressed A/V") );
374 set_capability( "access2", 0 );
375 /* use these when open as access_demux fails; VLC will use another demux */
376 set_callbacks( AccessOpen, AccessClose );
380 /*****************************************************************************
381 * Access: local prototypes
382 *****************************************************************************/
384 static void CommonClose( vlc_object_t *, demux_sys_t * );
385 static void ParseMRL( demux_sys_t *, char *, vlc_object_t * );
386 static void GetV4L2Params( demux_sys_t *, vlc_object_t * );
387 static void SetAvailControlsByString( vlc_object_t *, demux_sys_t *, int );
389 static int DemuxControl( demux_t *, int, va_list );
390 static int AccessControl( access_t *, int, va_list );
392 static int Demux( demux_t * );
393 static ssize_t AccessRead( access_t *, uint8_t *, size_t );
395 static block_t* GrabVideo( demux_t *p_demux );
396 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t );
397 static block_t* GrabAudio( demux_t *p_demux );
399 static vlc_bool_t IsPixelFormatSupported( demux_t *p_demux,
400 unsigned int i_pixelformat );
403 static char* ResolveALSADeviceName( const char *psz_device );
405 static int OpenVideoDev( vlc_object_t *, demux_sys_t *, vlc_bool_t );
406 static int OpenAudioDev( vlc_object_t *, demux_sys_t *, vlc_bool_t );
407 static vlc_bool_t ProbeVideoDev( vlc_object_t *, demux_sys_t *,
409 static vlc_bool_t ProbeAudioDev( vlc_object_t *, demux_sys_t *,
412 static int ControlList( vlc_object_t *, int , vlc_bool_t, vlc_bool_t );
413 static int Control( vlc_object_t *, int i_fd,
414 const char *psz_name, int i_cid, int i_value );
416 static int DemuxControlCallback( vlc_object_t *p_this, const char *psz_var,
417 vlc_value_t oldval, vlc_value_t newval,
419 static int DemuxControlResetCallback( vlc_object_t *p_this, const char *psz_var,
420 vlc_value_t oldval, vlc_value_t newval,
422 static int AccessControlCallback( vlc_object_t *p_this, const char *psz_var,
423 vlc_value_t oldval, vlc_value_t newval,
425 static int AccessControlResetCallback( vlc_object_t *p_this,
426 const char *psz_var, vlc_value_t oldval,
427 vlc_value_t newval, void *p_data );
433 } v4l2chroma_to_fourcc[] =
436 { V4L2_PIX_FMT_GREY, VLC_FOURCC('G','R','E','Y') },
437 { V4L2_PIX_FMT_HI240, VLC_FOURCC('I','2','4','0') },
438 { V4L2_PIX_FMT_RGB565, VLC_FOURCC('R','V','1','6') },
439 { V4L2_PIX_FMT_RGB555, VLC_FOURCC('R','V','1','5') },
440 { V4L2_PIX_FMT_BGR24, VLC_FOURCC('R','V','2','4') },
441 { V4L2_PIX_FMT_BGR32, VLC_FOURCC('R','V','3','2') },
442 { V4L2_PIX_FMT_YUYV, VLC_FOURCC('Y','U','Y','2') },
443 { V4L2_PIX_FMT_YUYV, VLC_FOURCC('Y','U','Y','V') },
444 { V4L2_PIX_FMT_UYVY, VLC_FOURCC('U','Y','V','Y') },
445 { V4L2_PIX_FMT_Y41P, VLC_FOURCC('I','4','1','N') },
446 { V4L2_PIX_FMT_YUV422P, VLC_FOURCC('I','4','2','2') },
447 { V4L2_PIX_FMT_YVU420, VLC_FOURCC('Y','V','1','2') },
448 { V4L2_PIX_FMT_YUV411P, VLC_FOURCC('I','4','1','1') },
449 { V4L2_PIX_FMT_YUV410, VLC_FOURCC('I','4','1','0') },
451 /* Raw data types, not in V4L2 spec but still in videodev2.h and supported
453 { V4L2_PIX_FMT_YUV420, VLC_FOURCC('I','4','2','0') },
454 /* FIXME { V4L2_PIX_FMT_RGB444, VLC_FOURCC('R','V','3','2') }, */
456 /* Compressed data types */
457 { V4L2_PIX_FMT_MJPEG, VLC_FOURCC('M','J','P','G') },
459 { V4L2_PIX_FMT_JPEG, VLC_FOURCC('J','P','E','G') },
460 { V4L2_PIX_FMT_DV, VLC_FOURCC('?','?','?','?') },
461 { V4L2_PIX_FMT_MPEG, VLC_FOURCC('?','?','?','?') },
467 * List of V4L2 chromas were confident enough to use as fallbacks if the
468 * user hasn't provided a --v4l2-chroma value.
470 static const __u32 p_chroma_fallbacks[] =
471 { V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_YVU420, V4L2_PIX_FMT_YUV422P,
472 V4L2_PIX_FMT_YUYV, V4L2_PIX_FMT_MJPEG };
476 const char *psz_name;
480 { "brightness", V4L2_CID_BRIGHTNESS },
481 { "contrast", V4L2_CID_CONTRAST },
482 { "saturation", V4L2_CID_SATURATION },
483 { "hue", V4L2_CID_HUE },
484 { "audio-volume", V4L2_CID_AUDIO_VOLUME },
485 { "audio-balance", V4L2_CID_AUDIO_BALANCE },
486 { "audio-bass", V4L2_CID_AUDIO_BASS },
487 { "audio-treble", V4L2_CID_AUDIO_TREBLE },
488 { "audio-mute", V4L2_CID_AUDIO_MUTE },
489 { "audio-loudness", V4L2_CID_AUDIO_LOUDNESS },
490 { "black-level", V4L2_CID_BLACK_LEVEL },
491 { "auto-white-balance", V4L2_CID_AUTO_WHITE_BALANCE },
492 { "do-white-balance", V4L2_CID_DO_WHITE_BALANCE },
493 { "red-balance", V4L2_CID_RED_BALANCE },
494 { "blue-balance", V4L2_CID_BLUE_BALANCE },
495 { "gamma", V4L2_CID_GAMMA },
496 { "exposure", V4L2_CID_EXPOSURE },
497 { "autogain", V4L2_CID_AUTOGAIN },
498 { "gain", V4L2_CID_GAIN },
499 { "hflip", V4L2_CID_HFLIP },
500 { "vflip", V4L2_CID_VFLIP },
501 { "hcenter", V4L2_CID_HCENTER },
502 { "vcenter", V4L2_CID_VCENTER },
515 char *psz_device; /* Main device from MRL, can be video or audio */
523 char *psz_requested_chroma;
530 struct v4l2_capability dev_cap;
533 struct v4l2_input *p_inputs;
534 int i_selected_input;
537 struct v4l2_standard *p_standards;
538 v4l2_std_id i_selected_standard_id;
541 /* V4L2 devices cannot have more than 32 audio inputs */
542 struct v4l2_audio p_audios[32];
545 struct v4l2_tuner *p_tuners;
548 struct v4l2_fmtdesc *p_codecs;
550 struct buffer_t *p_buffers;
551 unsigned int i_nbuffers;
555 float f_fps; /* <= 0.0 mean to grab at full rate */
556 mtime_t i_video_pts; /* only used when f_fps > 0 */
559 es_out_id_t *p_es_video;
562 unsigned int i_sample_rate;
564 size_t i_audio_max_frame_size;
565 block_t *p_block_audio;
566 es_out_id_t *p_es_audio;
572 snd_pcm_t *p_alsa_pcm;
573 size_t i_alsa_frame_size;
574 int i_alsa_chunk_size;
586 static int FindMainDevice( vlc_object_t *p_this, demux_sys_t *p_sys,
587 int i_flags, vlc_bool_t b_demux,
588 vlc_bool_t b_forced )
590 /* Find main device (video or audio) */
591 if( p_sys->psz_device && *p_sys->psz_device )
593 msg_Dbg( p_this, "main device='%s'", p_sys->psz_device );
595 vlc_bool_t b_maindevice_is_video = VLC_FALSE;
597 /* Try to open as video device */
598 if( i_flags & FIND_VIDEO )
600 msg_Dbg( p_this, "trying device '%s' as video", p_sys->psz_device );
601 if( ProbeVideoDev( p_this, p_sys, p_sys->psz_device ) )
603 msg_Dbg( p_this, "'%s' is a video device", p_sys->psz_device );
604 /* Device was a video device */
605 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
606 p_sys->psz_vdev = p_sys->psz_device;
607 p_sys->psz_device = NULL;
608 p_sys->i_fd_video = OpenVideoDev( p_this, p_sys, b_demux );
609 if( p_sys->i_fd_video < 0 )
611 b_maindevice_is_video = VLC_TRUE;
612 /* If successful we carry on to try the audio if access is forced */
616 /* Try to open as audio device only if main device was not detected as video above */
617 if( i_flags & FIND_AUDIO && !b_maindevice_is_video )
619 msg_Dbg( p_this, "trying device '%s' as audio", p_sys->psz_device );
620 if( ProbeAudioDev( p_this, p_sys, p_sys->psz_device ) )
622 msg_Dbg( p_this, "'%s' is an audio device", p_sys->psz_device );
623 /* Device was an audio device */
624 free( p_sys->psz_adev );
625 p_sys->psz_adev = p_sys->psz_device;
626 p_sys->psz_device = NULL;
627 p_sys->i_fd_audio = OpenAudioDev( p_this, p_sys, b_demux );
628 if( p_sys->i_fd_audio < 0 )
630 /* If successful we carry on to try the video if access is forced */
635 /* If no device opened, only continue if the access was forced */
636 if( b_forced == VLC_FALSE
637 && !( ( i_flags & FIND_VIDEO && p_sys->i_fd_video >= 0 )
638 || ( i_flags & FIND_AUDIO && p_sys->i_fd_audio >= 0 ) ) )
643 /* Find video device */
644 if( i_flags & FIND_VIDEO && p_sys->i_fd_video < 0 )
646 if( !p_sys->psz_vdev || !*p_sys->psz_vdev )
648 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
649 p_sys->psz_vdev = var_CreateGetString( p_this, "v4l2-dev" );
652 msg_Dbg( p_this, "opening '%s' as video", p_sys->psz_vdev );
653 if( p_sys->psz_vdev && *p_sys->psz_vdev
654 && ProbeVideoDev( p_this, p_sys, p_sys->psz_vdev ) )
656 p_sys->i_fd_video = OpenVideoDev( p_this, p_sys, b_demux );
660 /* Find audio device */
661 if( i_flags & FIND_AUDIO && p_sys->i_fd_audio < 0 )
663 if( !p_sys->psz_adev )
665 p_sys->psz_adev = var_CreateGetNonEmptyString( p_this, "v4l2-adev" );
668 msg_Dbg( p_this, "opening '%s' as audio", p_sys->psz_adev );
669 if( ProbeAudioDev( p_this, p_sys, p_sys->psz_adev ) )
671 p_sys->i_fd_audio = OpenAudioDev( p_this, p_sys, b_demux );
675 if( !( ( i_flags & FIND_VIDEO && p_sys->i_fd_video >= 0 )
676 || ( i_flags & FIND_AUDIO && p_sys->i_fd_audio >= 0 ) ) )
683 /*****************************************************************************
684 * DemuxOpen: opens v4l2 device, access_demux callback
685 *****************************************************************************
687 * url: <video device>::::
689 *****************************************************************************/
690 static int DemuxOpen( vlc_object_t *p_this )
692 demux_t *p_demux = (demux_t*)p_this;
695 /* Only when selected */
696 if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
699 p_demux->pf_control = DemuxControl;
700 p_demux->pf_demux = Demux;
701 p_demux->info.i_update = 0;
702 p_demux->info.i_title = 0;
703 p_demux->info.i_seekpoint = 0;
705 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
706 if( p_sys == NULL ) return VLC_ENOMEM;
708 GetV4L2Params(p_sys, (vlc_object_t *) p_demux);
710 ParseMRL( p_sys, p_demux->psz_path, (vlc_object_t *) p_demux );
713 /* Alsa support available? */
714 msg_Dbg( p_demux, "ALSA input support available" );
717 if( FindMainDevice( p_this, p_sys, FIND_VIDEO|FIND_AUDIO,
718 VLC_TRUE, !strncmp( p_demux->psz_access, "v4l2", 4 ) ) != VLC_SUCCESS )
720 DemuxClose( p_this );
727 /*****************************************************************************
728 * GetV4L2Params: fill in p_sys parameters (shared by DemuxOpen and AccessOpen)
729 *****************************************************************************/
730 static void GetV4L2Params( demux_sys_t *p_sys, vlc_object_t *p_obj )
732 p_sys->i_video_pts = -1;
734 p_sys->i_selected_standard_id =
735 i_standards_list[var_CreateGetInteger( p_obj, "v4l2-standard" )];
737 p_sys->i_selected_input = var_CreateGetInteger( p_obj, "v4l2-input" );
739 p_sys->io = var_CreateGetInteger( p_obj, "v4l2-io" );
741 p_sys->i_width = var_CreateGetInteger( p_obj, "v4l2-width" );
742 p_sys->i_height = var_CreateGetInteger( p_obj, "v4l2-height" );
744 var_CreateGetBool( p_obj, "v4l2-controls-reset" );
746 p_sys->f_fps = var_CreateGetFloat( p_obj, "v4l2-fps" );
747 p_sys->i_sample_rate = var_CreateGetInteger( p_obj, "v4l2-samplerate" );
748 p_sys->psz_requested_chroma = var_CreateGetString( p_obj, "v4l2-chroma" );
750 p_sys->i_audio_method = var_CreateGetInteger( p_obj, "v4l2-audio-method" );
752 p_sys->b_stereo = var_CreateGetBool( p_obj, "v4l2-stereo" );
754 p_sys->i_pts = var_CreateGetInteger( p_obj, "v4l2-caching" );
756 p_sys->i_cur_tuner = var_CreateGetInteger( p_obj, "v4l2-tuner" );
757 p_sys->i_frequency = var_CreateGetInteger( p_obj, "v4l2-tuner-frequency" );
758 p_sys->i_audio_mode = var_CreateGetInteger( p_obj, "v4l2-tuner-audio-mode" );
760 p_sys->psz_set_ctrls = var_CreateGetString( p_obj, "v4l2-set-ctrls" );
762 p_sys->psz_device = p_sys->psz_vdev = p_sys->psz_adev = NULL;
763 p_sys->i_fd_video = -1;
764 p_sys->i_fd_audio = -1;
766 p_sys->p_es_video = p_sys->p_es_audio = 0;
767 p_sys->p_block_audio = 0;
770 /*****************************************************************************
771 * ParseMRL: parse the options contained in the MRL
772 *****************************************************************************/
773 static void ParseMRL( demux_sys_t *p_sys, char *psz_path, vlc_object_t *p_obj )
775 char *psz_dup = strdup( psz_path );
776 char *psz_parser = psz_dup;
778 while( *psz_parser && *psz_parser != ':' )
783 if( *psz_parser == ':' )
788 *psz_parser++ = '\0';
790 if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
794 psz_parser += strlen( "adev=" );
795 if( strchr( psz_parser, ':' ) )
797 i_len = strchr( psz_parser, ':' ) - psz_parser;
801 i_len = strlen( psz_parser );
804 p_sys->psz_adev = strndup( psz_parser, i_len );
805 if( !*p_sys->psz_adev )
807 free( p_sys->psz_adev );
808 p_sys->psz_adev = NULL;
813 else if( !strncmp( psz_parser, "standard=", strlen( "standard=" ) ) )
815 psz_parser += strlen( "standard=" );
816 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
818 p_sys->i_selected_standard_id = V4L2_STD_PAL;
819 psz_parser += strlen( "pal" );
821 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
823 p_sys->i_selected_standard_id = V4L2_STD_NTSC;
824 psz_parser += strlen( "ntsc" );
826 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
828 p_sys->i_selected_standard_id = V4L2_STD_SECAM;
829 psz_parser += strlen( "secam" );
831 else if( !strncmp( psz_parser, "default", strlen( "default" ) ) )
833 p_sys->i_selected_standard_id = V4L2_STD_UNKNOWN;
834 psz_parser += strlen( "default" );
838 p_sys->i_selected_standard_id = i_standards_list[strtol( psz_parser, &psz_parser, 0 )];
841 else if( !strncmp( psz_parser, "chroma=", strlen( "chroma=" ) ) )
845 psz_parser += strlen( "chroma=" );
846 if( strchr( psz_parser, ':' ) )
848 i_len = strchr( psz_parser, ':' ) - psz_parser;
852 i_len = strlen( psz_parser );
855 if( p_sys->psz_requested_chroma ) free( p_sys->psz_requested_chroma );
856 p_sys->psz_requested_chroma = strndup( psz_parser, i_len );
860 else if( !strncmp( psz_parser, "input=", strlen( "input=" ) ) )
862 p_sys->i_selected_input = strtol( psz_parser + strlen( "input=" ),
865 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
867 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
870 else if( !strncmp( psz_parser, "io=", strlen( "io=" ) ) )
872 psz_parser += strlen( "io=" );
873 if( !strncmp( psz_parser, "read", strlen( "read" ) ) )
875 p_sys->io = IO_METHOD_READ;
876 psz_parser += strlen( "read" );
878 else if( !strncmp( psz_parser, "mmap", strlen( "mmap" ) ) )
880 p_sys->io = IO_METHOD_MMAP;
881 psz_parser += strlen( "mmap" );
883 else if( !strncmp( psz_parser, "userptr", strlen( "userptr" ) ) )
885 p_sys->io = IO_METHOD_USERPTR;
886 psz_parser += strlen( "userptr" );
890 p_sys->io = strtol( psz_parser, &psz_parser, 0 );
893 else if( !strncmp( psz_parser, "width=",
894 strlen( "width=" ) ) )
897 strtol( psz_parser + strlen( "width=" ),
900 else if( !strncmp( psz_parser, "height=",
901 strlen( "height=" ) ) )
904 strtol( psz_parser + strlen( "height=" ),
907 else if( !strncmp( psz_parser, "controls-reset",
908 strlen( "controls-reset" ) ) )
910 var_SetBool( p_obj, "v4l2-controls-reset", VLC_TRUE );
911 psz_parser += strlen( "controls-reset" );
914 else if( !strncmp( psz_parser, "brightness=",
915 strlen( "brightness=" ) ) )
917 var_SetInteger( p_obj, "brightness",
918 strtol( psz_parser + strlen( "brightness=" ),
921 else if( !strncmp( psz_parser, "contrast=",
922 strlen( "contrast=" ) ) )
924 var_SetInteger( p_obj, "contrast",
925 strtol( psz_parser + strlen( "contrast=" ),
928 else if( !strncmp( psz_parser, "saturation=",
929 strlen( "saturation=" ) ) )
931 var_SetInteger( p_obj, "saturation",
932 strtol( psz_parser + strlen( "saturation=" ),
935 else if( !strncmp( psz_parser, "hue=",
938 var_SetInteger( p_obj, "hue",
939 strtol( psz_parser + strlen( "hue=" ),
942 else if( !strncmp( psz_parser, "gamma=",
943 strlen( "gamma=" ) ) )
945 var_SetInteger( p_obj, "gamma",
946 strtol( psz_parser + strlen( "gamma=" ),
950 else if( !strncmp( psz_parser, "samplerate=",
951 strlen( "samplerate=" ) ) )
953 p_sys->i_sample_rate =
954 strtol( psz_parser + strlen( "samplerate=" ),
957 else if( !strncmp( psz_parser, "audio-method", strlen( "audio-method" ) ) )
959 p_sys->i_audio_method =
960 strtol( psz_parser + strlen( "audio-method" ),
963 else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
965 psz_parser += strlen( "stereo" );
966 p_sys->b_stereo = VLC_TRUE;
968 else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
970 psz_parser += strlen( "mono" );
971 p_sys->b_stereo = VLC_FALSE;
973 else if( !strncmp( psz_parser, "caching=", strlen( "caching=" ) ) )
975 p_sys->i_pts = strtol( psz_parser + strlen( "caching=" ),
978 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
980 p_sys->i_cur_tuner = strtol( psz_parser + strlen( "tuner=" ),
983 else if( !strncmp( psz_parser, "tuner-frequency=", strlen( "tuner-frequency=" ) ) )
985 p_sys->i_frequency = strtol( psz_parser
986 + strlen( "tuner-frequency=" ),
989 else if( !strncmp( psz_parser, "tuner-audio-mode=", strlen( "tuner-audio-mode=" ) ) )
991 p_sys->i_audio_mode = strtol( psz_parser
992 + strlen( "tuner-audio-mode=" ),
995 else if( !strncmp( psz_parser, "set-ctrls=", strlen( "set-ctrls=" )) )
999 psz_parser += strlen( "set-ctrls=" );
1000 if( strchr( psz_parser, ':' ) )
1002 i_len = strchr( psz_parser, ':' ) - psz_parser;
1006 i_len = strlen( psz_parser );
1009 p_sys->psz_set_ctrls = strndup( psz_parser, i_len );
1011 psz_parser += i_len;
1015 char *psz_unk = strchr( psz_parser, ':' );
1017 psz_unk = strndup( psz_parser, psz_unk - psz_parser );
1019 psz_unk = strdup( psz_parser);
1020 msg_Warn( p_obj, "unknown option %s", psz_unk );
1024 while( *psz_parser && *psz_parser != ':' )
1029 if( *psz_parser == '\0' )
1039 p_sys->psz_device = strdup( psz_dup );
1041 if( psz_dup ) free( psz_dup );
1044 /*****************************************************************************
1045 * Close: close device, free resources
1046 *****************************************************************************/
1047 static void AccessClose( vlc_object_t *p_this )
1049 access_t *p_access = (access_t *)p_this;
1050 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1052 CommonClose( p_this, p_sys );
1055 static void DemuxClose( vlc_object_t *p_this )
1057 struct v4l2_buffer buf;
1058 enum v4l2_buf_type buf_type;
1061 demux_t *p_demux = (demux_t *)p_this;
1062 demux_sys_t *p_sys = p_demux->p_sys;
1064 /* Stop video capture */
1065 if( p_sys->i_fd_video >= 0 )
1069 case IO_METHOD_READ:
1073 case IO_METHOD_MMAP:
1074 case IO_METHOD_USERPTR:
1075 /* Some drivers 'hang' internally if this is not done before streamoff */
1076 for( unsigned int i = 0; i < p_sys->i_nbuffers; i++ )
1078 memset( &buf, 0, sizeof(buf) );
1079 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1080 buf.memory = ( p_sys->io == IO_METHOD_USERPTR ) ?
1081 V4L2_MEMORY_USERPTR : V4L2_MEMORY_MMAP;
1082 ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ); /* ignore result */
1085 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1086 if( ioctl( p_sys->i_fd_video, VIDIOC_STREAMOFF, &buf_type ) < 0 ) {
1087 msg_Err( p_this, "VIDIOC_STREAMOFF failed" );
1094 /* Free Video Buffers */
1095 if( p_sys->p_buffers ) {
1098 case IO_METHOD_READ:
1099 free( p_sys->p_buffers[0].start );
1102 case IO_METHOD_MMAP:
1103 for( i = 0; i < p_sys->i_nbuffers; ++i )
1105 if( munmap( p_sys->p_buffers[i].start, p_sys->p_buffers[i].length ) )
1107 msg_Err( p_this, "munmap failed" );
1112 case IO_METHOD_USERPTR:
1113 for( i = 0; i < p_sys->i_nbuffers; ++i )
1115 free( p_sys->p_buffers[i].orig_userp );
1119 free( p_sys->p_buffers );
1122 CommonClose( p_this, p_sys );
1125 static void CommonClose( vlc_object_t *p_this, demux_sys_t *p_sys )
1128 if( p_sys->i_fd_video >= 0 ) close( p_sys->i_fd_video );
1130 if( p_sys->p_alsa_pcm )
1132 snd_pcm_close( p_sys->p_alsa_pcm );
1133 p_sys->i_fd_audio = -1;
1136 if( p_sys->i_fd_audio >= 0 ) close( p_sys->i_fd_audio );
1138 if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
1139 free( p_sys->psz_device );
1140 free( p_sys->psz_vdev );
1141 free( p_sys->psz_adev );
1142 free( p_sys->p_standards );
1143 free( p_sys->p_inputs );
1144 free( p_sys->p_tuners );
1145 free( p_sys->p_codecs );
1146 free( p_sys->psz_requested_chroma );
1147 free( p_sys->psz_set_ctrls );
1152 /*****************************************************************************
1153 * AccessOpen: opens v4l2 device, access2 callback
1154 *****************************************************************************
1156 * url: <video device>::::
1158 *****************************************************************************/
1159 static int AccessOpen( vlc_object_t * p_this )
1161 access_t *p_access = (access_t*) p_this;
1162 demux_sys_t * p_sys;
1164 /* Only when selected */
1165 if( *p_access->psz_access == '\0' ) return VLC_EGENERIC;
1167 p_access->pf_read = AccessRead;
1168 p_access->pf_block = NULL;
1169 p_access->pf_seek = NULL;
1170 p_access->pf_control = AccessControl;
1171 p_access->info.i_update = 0;
1172 p_access->info.i_size = 0;
1173 p_access->info.i_pos = 0;
1174 p_access->info.b_eof = VLC_FALSE;
1175 p_access->info.i_title = 0;
1176 p_access->info.i_seekpoint = 0;
1178 p_sys = calloc( 1, sizeof( demux_sys_t ) );
1179 p_access->p_sys = (access_sys_t *) p_sys;
1180 if( p_sys == NULL ) return VLC_ENOMEM;
1182 GetV4L2Params( p_sys, (vlc_object_t *) p_access );
1184 ParseMRL( p_sys, p_access->psz_path, (vlc_object_t *) p_access );
1186 if( FindMainDevice( p_this, p_sys, FIND_VIDEO,
1187 VLC_FALSE, !strncmp( p_access->psz_access, "v4l2", 4 ) ) != VLC_SUCCESS )
1189 AccessClose( p_this );
1190 return VLC_EGENERIC;
1196 /*****************************************************************************
1198 *****************************************************************************/
1199 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
1201 demux_sys_t *p_sys = p_demux->p_sys;
1207 /* Special for access_demux */
1208 case DEMUX_CAN_PAUSE:
1209 case DEMUX_CAN_SEEK:
1210 case DEMUX_SET_PAUSE_STATE:
1211 case DEMUX_CAN_CONTROL_PACE:
1212 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1216 case DEMUX_GET_PTS_DELAY:
1217 pi64 = (int64_t*)va_arg( args, int64_t * );
1218 *pi64 = (int64_t)p_sys->i_pts * 1000;
1221 case DEMUX_GET_TIME:
1222 pi64 = (int64_t*)va_arg( args, int64_t * );
1226 /* TODO implement others */
1228 return VLC_EGENERIC;
1231 return VLC_EGENERIC;
1234 /*****************************************************************************
1235 * AccessControl: access2 callback
1236 *****************************************************************************/
1237 static int AccessControl( access_t *p_access, int i_query, va_list args )
1239 vlc_bool_t *pb_bool;
1242 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1247 case ACCESS_CAN_SEEK:
1248 case ACCESS_CAN_FASTSEEK:
1249 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1250 *pb_bool = VLC_FALSE;
1252 case ACCESS_CAN_PAUSE:
1253 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1254 *pb_bool = VLC_FALSE;
1256 case ACCESS_CAN_CONTROL_PACE:
1257 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1258 *pb_bool = VLC_FALSE;
1262 case ACCESS_GET_MTU:
1263 pi_int = (int*)va_arg( args, int * );
1267 case ACCESS_GET_PTS_DELAY:
1268 pi_64 = (int64_t*)va_arg( args, int64_t * );
1269 *pi_64 = (int64_t) p_sys->i_pts * 1000;
1273 case ACCESS_SET_PAUSE_STATE:
1277 case ACCESS_GET_TITLE_INFO:
1278 case ACCESS_SET_TITLE:
1279 case ACCESS_SET_SEEKPOINT:
1280 case ACCESS_SET_PRIVATE_ID_STATE:
1281 case ACCESS_GET_CONTENT_TYPE:
1282 case ACCESS_GET_META:
1283 return VLC_EGENERIC;
1286 msg_Warn( p_access, "Unimplemented query in control(%d).", i_query);
1287 return VLC_EGENERIC;
1293 /*****************************************************************************
1294 * AccessRead: access2 callback
1295 ******************************************************************************/
1296 static ssize_t AccessRead( access_t * p_access, uint8_t * p_buffer, size_t i_len )
1298 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1302 ufd.fd = p_sys->i_fd_video;
1303 ufd.events = POLLIN;
1305 if( p_access->info.b_eof )
1310 if( p_access->b_die )
1315 while( ( i_ret = poll( &ufd, 1, 500 ) ) == 0 );
1319 msg_Err( p_access, "Polling error (%m)." );
1323 i_ret = read( p_sys->i_fd_video, p_buffer, i_len );
1326 p_access->info.b_eof = VLC_TRUE;
1328 else if( i_ret > 0 )
1330 p_access->info.i_pos += i_ret;
1336 /*****************************************************************************
1337 * Demux: Processes the audio or video frame
1338 *****************************************************************************/
1339 static int Demux( demux_t *p_demux )
1341 demux_sys_t *p_sys = p_demux->p_sys;
1342 es_out_id_t *p_es = p_sys->p_es_audio;
1343 block_t *p_block = NULL;
1345 /* Try grabbing audio frames first */
1346 if( p_sys->i_fd_audio < 0 || !( p_block = GrabAudio( p_demux ) ) )
1348 /* Try grabbing video frame */
1349 p_es = p_sys->p_es_video;
1350 if( p_sys->i_fd_video > 0 ) p_block = GrabVideo( p_demux );
1355 /* Sleep so we do not consume all the cpu, 10ms seems
1356 * like a good value (100fps) */
1361 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
1362 es_out_Send( p_demux->out, p_es, p_block );
1367 /*****************************************************************************
1368 * GrabVideo: Grab a video frame
1369 *****************************************************************************/
1370 static block_t* GrabVideo( demux_t *p_demux )
1372 demux_sys_t *p_sys = p_demux->p_sys;
1374 block_t *p_block = NULL;
1375 struct v4l2_buffer buf;
1378 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1380 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1382 /* Did we wait long enough ? (frame rate reduction) */
1383 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1386 /* Grab Video Frame */
1389 case IO_METHOD_READ:
1390 i_ret = read( p_sys->i_fd_video, p_sys->p_buffers[0].start, p_sys->p_buffers[0].length );
1398 /* Could ignore EIO, see spec. */
1401 msg_Err( p_demux, "Failed to read frame" );
1406 p_block = ProcessVideoFrame( p_demux, (uint8_t*)p_sys->p_buffers[0].start, i_ret );
1407 if( !p_block ) return 0;
1411 case IO_METHOD_MMAP:
1412 memset( &buf, 0, sizeof(buf) );
1413 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1414 buf.memory = V4L2_MEMORY_MMAP;
1416 /* Wait for next frame */
1417 if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
1424 /* Could ignore EIO, see spec. */
1427 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1432 if( buf.index >= p_sys->i_nbuffers ) {
1433 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1437 p_block = ProcessVideoFrame( p_demux, p_sys->p_buffers[buf.index].start, buf.bytesused );
1438 if( !p_block ) return 0;
1441 if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
1443 msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
1449 case IO_METHOD_USERPTR:
1450 memset( &buf, 0, sizeof(buf) );
1451 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1452 buf.memory = V4L2_MEMORY_USERPTR;
1454 /* Wait for next frame */
1455 if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
1462 /* Could ignore EIO, see spec. */
1465 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1472 for( i = 0; i < p_sys->i_nbuffers; i++ )
1474 if( buf.m.userptr == (unsigned long)p_sys->p_buffers[i].start &&
1475 buf.length == p_sys->p_buffers[i].length ) break;
1478 if( i >= p_sys->i_nbuffers )
1480 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1484 p_block = ProcessVideoFrame( p_demux, (uint8_t*)buf.m.userptr, buf.bytesused );
1485 if( !p_block ) return 0;
1488 if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
1490 msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
1499 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();
1504 /*****************************************************************************
1505 * ProcessVideoFrame: Helper function to take a buffer and copy it into
1507 *****************************************************************************/
1508 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t i_size )
1512 if( !p_frame ) return 0;
1515 if( !( p_block = block_New( p_demux, i_size ) ) )
1517 msg_Warn( p_demux, "Cannot get new block" );
1522 memcpy( p_block->p_buffer, p_frame, i_size );
1527 /*****************************************************************************
1528 * GrabAudio: Grab an audio frame
1529 *****************************************************************************/
1530 static block_t* GrabAudio( demux_t *p_demux )
1532 demux_sys_t *p_sys = p_demux->p_sys;
1533 struct audio_buf_info buf_info;
1534 int i_read = 0, i_correct;
1537 if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
1538 else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
1542 msg_Warn( p_demux, "cannot get block" );
1546 p_sys->p_block_audio = p_block;
1549 if( p_sys->i_audio_method & AUDIO_METHOD_ALSA )
1552 i_read = snd_pcm_readi( p_sys->p_alsa_pcm, p_block->p_buffer, p_sys->i_alsa_chunk_size );
1562 snd_pcm_prepare( p_sys->p_alsa_pcm );
1566 i_resume = snd_pcm_resume( p_sys->p_alsa_pcm );
1567 if( i_resume < 0 && i_resume != -EAGAIN ) snd_pcm_prepare( p_sys->p_alsa_pcm );
1570 msg_Err( p_demux, "Failed to read alsa frame (%s)", snd_strerror( i_read ) );
1576 /* convert from frames to bytes */
1577 i_read *= p_sys->i_alsa_frame_size;
1582 if( p_sys->i_audio_method & AUDIO_METHOD_OSS )
1585 i_read = read( p_sys->i_fd_audio, p_block->p_buffer,
1586 p_sys->i_audio_max_frame_size );
1589 if( i_read <= 0 ) return 0;
1591 p_block->i_buffer = i_read;
1592 p_sys->p_block_audio = 0;
1594 /* Correct the date because of kernel buffering */
1596 if( p_sys->i_audio_method & AUDIO_METHOD_OSS )
1599 if( ioctl( p_sys->i_fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
1601 i_correct += buf_info.bytes;
1605 else if( p_sys->i_audio_method & AUDIO_METHOD_ALSA )
1609 snd_pcm_sframes_t delay = 0;
1610 if( ( i_err = snd_pcm_delay( p_sys->p_alsa_pcm, &delay ) ) >= 0 )
1612 size_t i_correction_delta = delay * p_sys->i_alsa_frame_size;
1613 /* Test for overrun */
1614 if( i_correction_delta > p_sys->i_audio_max_frame_size )
1616 msg_Warn( p_demux, "ALSA read overrun (%d > %d)",
1617 i_correction_delta, p_sys->i_audio_max_frame_size );
1618 i_correction_delta = p_sys->i_audio_max_frame_size;
1619 snd_pcm_prepare( p_sys->p_alsa_pcm );
1621 i_correct += i_correction_delta;
1625 /* delay failed so reset */
1626 msg_Warn( p_demux, "ALSA snd_pcm_delay failed (%s)", snd_strerror( i_err ) );
1627 snd_pcm_prepare( p_sys->p_alsa_pcm );
1633 p_block->i_pts = p_block->i_dts =
1634 mdate() - I64C(1000000) * (mtime_t)i_correct /
1635 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
1640 /*****************************************************************************
1641 * Helper function to initalise video IO using the Read method
1642 *****************************************************************************/
1643 static int InitRead( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1645 demux_sys_t *p_sys = p_demux->p_sys;
1647 p_sys->p_buffers = calloc( 1, sizeof( *p_sys->p_buffers ) );
1648 if( !p_sys->p_buffers )
1650 msg_Err( p_demux, "Out of memory" );
1654 p_sys->p_buffers[0].length = i_buffer_size;
1655 p_sys->p_buffers[0].start = malloc( i_buffer_size );
1656 if( !p_sys->p_buffers[0].start )
1658 msg_Err( p_demux, "Out of memory" );
1665 return VLC_EGENERIC;
1669 /*****************************************************************************
1670 * Helper function to initalise video IO using the mmap method
1671 *****************************************************************************/
1672 static int InitMmap( demux_t *p_demux, int i_fd )
1674 demux_sys_t *p_sys = p_demux->p_sys;
1675 struct v4l2_requestbuffers req;
1677 memset( &req, 0, sizeof(req) );
1679 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1680 req.memory = V4L2_MEMORY_MMAP;
1682 if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1684 msg_Err( p_demux, "device does not support mmap i/o" );
1690 msg_Err( p_demux, "Insufficient buffer memory" );
1694 p_sys->p_buffers = calloc( req.count, sizeof( *p_sys->p_buffers ) );
1695 if( !p_sys->p_buffers )
1697 msg_Err( p_demux, "Out of memory" );
1701 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < req.count; ++p_sys->i_nbuffers )
1703 struct v4l2_buffer buf;
1705 memset( &buf, 0, sizeof(buf) );
1706 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1707 buf.memory = V4L2_MEMORY_MMAP;
1708 buf.index = p_sys->i_nbuffers;
1710 if( ioctl( i_fd, VIDIOC_QUERYBUF, &buf ) < 0 )
1712 msg_Err( p_demux, "VIDIOC_QUERYBUF" );
1716 p_sys->p_buffers[p_sys->i_nbuffers].length = buf.length;
1717 p_sys->p_buffers[p_sys->i_nbuffers].start =
1718 mmap( NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, buf.m.offset );
1720 if( p_sys->p_buffers[p_sys->i_nbuffers].start == MAP_FAILED )
1722 msg_Err( p_demux, "mmap failed (%m)" );
1730 return VLC_EGENERIC;
1734 /*****************************************************************************
1735 * Helper function to initalise video IO using the userbuf method
1736 *****************************************************************************/
1737 static int InitUserP( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1739 demux_sys_t *p_sys = p_demux->p_sys;
1740 struct v4l2_requestbuffers req;
1741 unsigned int i_page_size;
1743 i_page_size = getpagesize();
1744 i_buffer_size = ( i_buffer_size + i_page_size - 1 ) & ~( i_page_size - 1);
1746 memset( &req, 0, sizeof(req) );
1748 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1749 req.memory = V4L2_MEMORY_USERPTR;
1751 if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1753 msg_Err( p_demux, "device does not support user pointer i/o" );
1757 p_sys->p_buffers = calloc( 4, sizeof( *p_sys->p_buffers ) );
1758 if( !p_sys->p_buffers )
1760 msg_Err( p_demux, "Out of memory" );
1764 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < 4; ++p_sys->i_nbuffers )
1766 p_sys->p_buffers[p_sys->i_nbuffers].length = i_buffer_size;
1767 p_sys->p_buffers[p_sys->i_nbuffers].start =
1768 vlc_memalign( &p_sys->p_buffers[p_sys->i_nbuffers].orig_userp,
1769 /* boundary */ i_page_size, i_buffer_size );
1771 if( !p_sys->p_buffers[p_sys->i_nbuffers].start )
1773 msg_Err( p_demux, "out of memory" );
1781 return VLC_EGENERIC;
1785 /*****************************************************************************
1786 * IsPixelFormatSupported: returns true if the specified V4L2 pixel format is
1787 * in the array of supported formats returned by the driver
1788 *****************************************************************************/
1789 static vlc_bool_t IsPixelFormatSupported( demux_t *p_demux, unsigned int i_pixelformat )
1791 demux_sys_t *p_sys = p_demux->p_sys;
1793 for( int i_index = 0; i_index < p_sys->i_codec; i_index++ )
1795 if( p_sys->p_codecs[i_index].pixelformat == i_pixelformat )
1802 /*****************************************************************************
1803 * OpenVideoDev: open and set up the video device and probe for capabilities
1804 *****************************************************************************/
1805 static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, vlc_bool_t b_demux )
1808 struct v4l2_cropcap cropcap;
1809 struct v4l2_crop crop;
1810 struct v4l2_format fmt;
1812 enum v4l2_buf_type buf_type;
1813 char *psz_device = p_sys->psz_vdev;
1815 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1817 msg_Err( p_obj, "cannot open device (%m)" );
1821 /* Tune the tuner */
1822 if( p_sys->i_frequency >= 0 )
1824 if( p_sys->i_cur_tuner < 0 || p_sys->i_cur_tuner >= p_sys->i_tuner )
1826 msg_Err( p_obj, "invalid tuner %d.", p_sys->i_cur_tuner );
1829 struct v4l2_frequency frequency;
1830 memset( &frequency, 0, sizeof( frequency ) );
1831 frequency.tuner = p_sys->i_cur_tuner;
1832 frequency.type = p_sys->p_tuners[p_sys->i_cur_tuner].type;
1833 frequency.frequency = p_sys->i_frequency / 62.5;
1834 if( ioctl( i_fd, VIDIOC_S_FREQUENCY, &frequency ) < 0 )
1836 msg_Err( p_obj, "cannot set tuner frequency (%m)" );
1839 msg_Dbg( p_obj, "Tuner frequency set" );
1842 /* Set the tuner's audio mode */
1843 if( p_sys->i_audio_mode >= 0 )
1845 if( p_sys->i_cur_tuner < 0 || p_sys->i_cur_tuner >= p_sys->i_tuner )
1847 msg_Err( p_obj, "invalid tuner %d.", p_sys->i_cur_tuner );
1850 struct v4l2_tuner tuner;
1851 memset( &tuner, 0, sizeof( tuner ) );
1852 tuner.index = p_sys->i_cur_tuner;
1853 tuner.audmode = p_sys->i_audio_mode;
1854 if( ioctl( i_fd, VIDIOC_S_TUNER, &tuner ) < 0 )
1856 msg_Err( p_obj, "cannot set tuner audio mode (%m)" );
1859 msg_Dbg( p_obj, "Tuner audio mode set" );
1862 /* Select standard */
1864 if( p_sys->i_selected_standard_id != V4L2_STD_UNKNOWN )
1866 if( ioctl( i_fd, VIDIOC_S_STD, &p_sys->i_selected_standard_id ) < 0 )
1868 msg_Err( p_obj, "cannot set standard (%m)" );
1871 msg_Dbg( p_obj, "Set standard" );
1876 if( p_sys->i_selected_input > p_sys->i_input )
1878 msg_Warn( p_obj, "invalid input. Using the default one" );
1879 p_sys->i_selected_input = 0;
1882 if( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
1884 msg_Err( p_obj, "cannot set input (%m)" );
1888 /* TODO: Move the resolution stuff up here */
1889 /* if MPEG encoder card, no need to do anything else after this */
1890 ControlList( p_obj, i_fd,
1891 var_GetBool( p_obj, "v4l2-controls-reset" ), b_demux );
1892 SetAvailControlsByString( p_obj, p_sys, i_fd );
1893 if( VLC_FALSE == b_demux)
1898 demux_t *p_demux = (demux_t *) p_obj;
1900 /* Verify device support for the various IO methods */
1903 case IO_METHOD_READ:
1904 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE) )
1906 msg_Err( p_demux, "device does not support read i/o" );
1911 case IO_METHOD_MMAP:
1912 case IO_METHOD_USERPTR:
1913 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING) )
1915 msg_Err( p_demux, "device does not support streaming i/o" );
1921 msg_Err( p_demux, "io method not supported" );
1925 /* Reset Cropping */
1926 memset( &cropcap, 0, sizeof(cropcap) );
1927 cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1928 if( ioctl( i_fd, VIDIOC_CROPCAP, &cropcap ) >= 0 )
1930 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1931 crop.c = cropcap.defrect; /* reset to default */
1932 if( ioctl( i_fd, VIDIOC_S_CROP, &crop ) < 0 )
1937 /* Cropping not supported. */
1940 /* Errors ignored. */
1946 /* Try and find default resolution if not specified */
1947 memset( &fmt, 0, sizeof(fmt) );
1948 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1950 if( p_sys->i_width <= 0 || p_sys->i_height <= 0 )
1952 if( ioctl( i_fd, VIDIOC_G_FMT, &fmt ) < 0 )
1954 msg_Err( p_demux, "Cannot get default width and height." );
1958 p_sys->i_width = fmt.fmt.pix.width;
1959 p_sys->i_height = fmt.fmt.pix.height;
1961 if( fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE )
1963 p_sys->i_height = p_sys->i_height * 2;
1968 msg_Dbg( p_demux, "trying specified size %dx%d", p_sys->i_width, p_sys->i_height );
1971 fmt.fmt.pix.width = p_sys->i_width;
1972 fmt.fmt.pix.height = p_sys->i_height;
1973 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
1975 /* Test and set Chroma */
1976 fmt.fmt.pix.pixelformat = 0;
1977 if( p_sys->psz_requested_chroma && strlen( p_sys->psz_requested_chroma ) > 0 )
1979 /* User specified chroma */
1980 if( strlen( p_sys->psz_requested_chroma ) >= 4 )
1982 int i_requested_fourcc = VLC_FOURCC(
1983 p_sys->psz_requested_chroma[0], p_sys->psz_requested_chroma[1],
1984 p_sys->psz_requested_chroma[2], p_sys->psz_requested_chroma[3] );
1985 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
1987 if( v4l2chroma_to_fourcc[i].i_fourcc == i_requested_fourcc )
1989 fmt.fmt.pix.pixelformat = v4l2chroma_to_fourcc[i].i_v4l2;
1994 /* Try and set user chroma */
1995 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ( fmt.fmt.pix.pixelformat && ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 ) )
1997 msg_Warn( p_demux, "Driver is unable to use specified chroma %s. Trying defaults.", p_sys->psz_requested_chroma );
1998 fmt.fmt.pix.pixelformat = 0;
2002 /* If no user specified chroma, find best */
2003 /* This also decides if MPEG encoder card or not */
2004 if( !fmt.fmt.pix.pixelformat )
2007 for( i = 0; i < ARRAY_SIZE( p_chroma_fallbacks ); i++ )
2009 fmt.fmt.pix.pixelformat = p_chroma_fallbacks[i];
2010 if( IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat )
2011 && ioctl( i_fd, VIDIOC_S_FMT, &fmt ) >= 0 )
2014 if( i == ARRAY_SIZE( p_chroma_fallbacks ) )
2016 msg_Warn( p_demux, "Could not select any of the default chromas; attempting to open as MPEG encoder card (access2)" );
2021 /* Reassign width, height and chroma incase driver override */
2022 p_sys->i_width = fmt.fmt.pix.width;
2023 p_sys->i_height = fmt.fmt.pix.height;
2025 /* Look up final fourcc */
2026 p_sys->i_fourcc = 0;
2027 for( int i = 0; v4l2chroma_to_fourcc[i].i_fourcc != 0; i++ )
2029 if( v4l2chroma_to_fourcc[i].i_v4l2 == fmt.fmt.pix.pixelformat )
2031 p_sys->i_fourcc = v4l2chroma_to_fourcc[i].i_fourcc;
2036 /* Buggy driver paranoia */
2037 i_min = fmt.fmt.pix.width * 2;
2038 if( fmt.fmt.pix.bytesperline < i_min )
2039 fmt.fmt.pix.bytesperline = i_min;
2040 i_min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
2041 if( fmt.fmt.pix.sizeimage < i_min )
2042 fmt.fmt.pix.sizeimage = i_min;
2044 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
2045 /* This is new in Linux 2.6.19 */
2046 /* List supported frame rates */
2047 struct v4l2_frmivalenum frmival;
2049 frmival.pixel_format = fmt.fmt.pix.pixelformat;
2050 frmival.width = p_sys->i_width;
2051 frmival.height = p_sys->i_height;
2052 if( ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 )
2055 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
2056 vlc_fourcc_to_char( p_sys->i_fourcc, &psz_fourcc );
2057 msg_Dbg( p_demux, "supported frame intervals for %4s, %dx%d:",
2058 psz_fourcc, frmival.width, frmival.height );
2059 switch( frmival.type )
2061 case V4L2_FRMIVAL_TYPE_DISCRETE:
2064 msg_Dbg( p_demux, " supported frame interval: %d/%d",
2065 frmival.discrete.numerator,
2066 frmival.discrete.denominator );
2068 } while( ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 );
2070 case V4L2_FRMIVAL_TYPE_STEPWISE:
2071 msg_Dbg( p_demux, " supported frame intervals: %d/%d to "
2072 "%d/%d using %d/%d increments",
2073 frmival.stepwise.min.numerator,
2074 frmival.stepwise.min.denominator,
2075 frmival.stepwise.max.numerator,
2076 frmival.stepwise.max.denominator,
2077 frmival.stepwise.step.numerator,
2078 frmival.stepwise.step.denominator );
2080 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
2081 msg_Dbg( p_demux, " supported frame intervals: %d/%d to %d/%d",
2082 frmival.stepwise.min.numerator,
2083 frmival.stepwise.min.denominator,
2084 frmival.stepwise.max.numerator,
2085 frmival.stepwise.max.denominator );
2091 /* Init IO method */
2094 case IO_METHOD_READ:
2095 if( InitRead( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
2098 case IO_METHOD_MMAP:
2099 if( InitMmap( p_demux, i_fd ) != VLC_SUCCESS ) goto open_failed;
2102 case IO_METHOD_USERPTR:
2103 if( InitUserP( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
2110 es_format_Init( &es_fmt, VIDEO_ES, p_sys->i_fourcc );
2111 es_fmt.video.i_width = p_sys->i_width;
2112 es_fmt.video.i_height = p_sys->i_height;
2113 es_fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
2115 /* Setup rgb mask for RGB formats */
2116 if( p_sys->i_fourcc == VLC_FOURCC( 'R','V','2','4' ) )
2118 /* This is in BGR format */
2119 es_fmt.video.i_bmask = 0x00ff0000;
2120 es_fmt.video.i_gmask = 0x0000ff00;
2121 es_fmt.video.i_rmask = 0x000000ff;
2124 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
2125 (char*)&es_fmt.i_codec, es_fmt.video.i_width, es_fmt.video.i_height );
2126 p_sys->p_es_video = es_out_Add( p_demux->out, &es_fmt );
2132 case IO_METHOD_READ:
2136 case IO_METHOD_MMAP:
2137 for (unsigned int i = 0; i < p_sys->i_nbuffers; ++i)
2139 struct v4l2_buffer buf;
2141 memset( &buf, 0, sizeof(buf) );
2142 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2143 buf.memory = V4L2_MEMORY_MMAP;
2146 if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
2148 msg_Err( p_demux, "VIDIOC_QBUF failed" );
2153 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2154 if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
2156 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
2162 case IO_METHOD_USERPTR:
2163 for( unsigned int i = 0; i < p_sys->i_nbuffers; ++i )
2165 struct v4l2_buffer buf;
2167 memset( &buf, 0, sizeof(buf) );
2168 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2169 buf.memory = V4L2_MEMORY_USERPTR;
2171 buf.m.userptr = (unsigned long)p_sys->p_buffers[i].start;
2172 buf.length = p_sys->p_buffers[i].length;
2174 if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
2176 msg_Err( p_demux, "VIDIOC_QBUF failed" );
2181 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2182 if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
2184 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
2192 if( p_sys->f_fps >= 0.1 )
2194 msg_Dbg( p_demux, "User set fps=%f", p_sys->f_fps );
2200 if( i_fd >= 0 ) close( i_fd );
2206 /*****************************************************************************
2207 * ResolveALSADeviceName: Change any . to : in the ALSA device name
2208 *****************************************************************************/
2209 static char *ResolveALSADeviceName( const char *psz_device )
2211 char* psz_alsa_name = strdup( psz_device );
2212 for( unsigned int i = 0; i < strlen( psz_device ); i++ )
2214 if( psz_alsa_name[i] == '.' ) psz_alsa_name[i] = ':';
2216 return psz_alsa_name;
2220 /*****************************************************************************
2221 * OpenAudioDev: open and set up the audio device and probe for capabilities
2222 *****************************************************************************/
2224 static int OpenAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys,
2225 vlc_bool_t b_demux )
2227 char *psz_device = p_sys->psz_adev;
2229 p_sys->p_alsa_pcm = NULL;
2230 char* psz_alsa_device_name = NULL;
2231 snd_pcm_hw_params_t *p_hw_params = NULL;
2232 snd_pcm_uframes_t buffer_size;
2233 snd_pcm_uframes_t chunk_size;
2237 psz_alsa_device_name =
2238 ResolveALSADeviceName( psz_device?: ALSA_DEFAULT );
2240 if( ( i_err = snd_pcm_open( &p_sys->p_alsa_pcm, psz_alsa_device_name,
2241 SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0)
2243 msg_Err( p_this, "Cannot open ALSA audio device %s (%s)",
2244 psz_alsa_device_name, snd_strerror( i_err ) );
2248 if( ( i_err = snd_pcm_nonblock( p_sys->p_alsa_pcm, 1 ) ) < 0)
2250 msg_Err( p_this, "Cannot set ALSA nonblock (%s)",
2251 snd_strerror( i_err ) );
2255 /* Begin setting hardware parameters */
2257 if( ( i_err = snd_pcm_hw_params_malloc( &p_hw_params ) ) < 0 )
2260 "ALSA: cannot allocate hardware parameter structure (%s)",
2261 snd_strerror( i_err ) );
2265 if( ( i_err = snd_pcm_hw_params_any( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
2268 "ALSA: cannot initialize hardware parameter structure (%s)",
2269 snd_strerror( i_err ) );
2273 /* Set Interleaved access */
2274 if( ( i_err = snd_pcm_hw_params_set_access( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 )
2276 msg_Err( p_this, "ALSA: cannot set access type (%s)",
2277 snd_strerror( i_err ) );
2281 /* Set 16 bit little endian */
2282 if( ( i_err = snd_pcm_hw_params_set_format( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_FORMAT_S16_LE ) ) < 0 )
2284 msg_Err( p_this, "ALSA: cannot set sample format (%s)",
2285 snd_strerror( i_err ) );
2289 /* Set sample rate */
2290 #ifdef HAVE_ALSA_NEW_API
2291 i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, &p_sys->i_sample_rate, NULL );
2293 i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, p_sys->i_sample_rate, NULL );
2297 msg_Err( p_this, "ALSA: cannot set sample rate (%s)",
2298 snd_strerror( i_err ) );
2303 unsigned int channels = p_sys->b_stereo ? 2 : 1;
2304 if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
2306 channels = ( channels==1 ) ? 2 : 1;
2307 msg_Warn( p_this, "ALSA: cannot set channel count (%s). "
2308 "Trying with channels=%d",
2309 snd_strerror( i_err ),
2311 if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
2313 msg_Err( p_this, "ALSA: cannot set channel count (%s)",
2314 snd_strerror( i_err ) );
2317 p_sys->b_stereo = ( channels == 2 );
2320 /* Set metrics for buffer calculations later */
2321 unsigned int buffer_time;
2322 if( ( i_err = snd_pcm_hw_params_get_buffer_time_max(p_hw_params, &buffer_time, 0) ) < 0 )
2324 msg_Err( p_this, "ALSA: cannot get buffer time max (%s)",
2325 snd_strerror( i_err ) );
2328 if (buffer_time > 500000) buffer_time = 500000;
2330 /* Set period time */
2331 unsigned int period_time = buffer_time / 4;
2332 #ifdef HAVE_ALSA_NEW_API
2333 i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, &period_time, 0 );
2335 i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, period_time, 0 );
2339 msg_Err( p_this, "ALSA: cannot set period time (%s)",
2340 snd_strerror( i_err ) );
2344 /* Set buffer time */
2345 #ifdef HAVE_ALSA_NEW_API
2346 i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, &buffer_time, 0 );
2348 i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, buffer_time, 0 );
2352 msg_Err( p_this, "ALSA: cannot set buffer time (%s)",
2353 snd_strerror( i_err ) );
2357 /* Apply new hardware parameters */
2358 if( ( i_err = snd_pcm_hw_params( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
2360 msg_Err( p_this, "ALSA: cannot set hw parameters (%s)",
2361 snd_strerror( i_err ) );
2365 /* Get various buffer metrics */
2366 snd_pcm_hw_params_get_period_size( p_hw_params, &chunk_size, 0 );
2367 snd_pcm_hw_params_get_buffer_size( p_hw_params, &buffer_size );
2368 if( chunk_size == buffer_size )
2371 "ALSA: period cannot equal buffer size (%lu == %lu)",
2372 chunk_size, buffer_size);
2376 int bits_per_sample = snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE);
2377 int bits_per_frame = bits_per_sample * channels;
2379 p_sys->i_alsa_chunk_size = chunk_size;
2380 p_sys->i_alsa_frame_size = bits_per_frame / 8;
2381 p_sys->i_audio_max_frame_size = chunk_size * bits_per_frame / 8;
2383 snd_pcm_hw_params_free( p_hw_params );
2387 if( ( i_err = snd_pcm_prepare( p_sys->p_alsa_pcm ) ) < 0 )
2390 "ALSA: cannot prepare audio interface for use (%s)",
2391 snd_strerror( i_err ) );
2395 /* Return a fake handle so other tests work */
2398 free( psz_alsa_device_name );
2400 if( !p_sys->psz_adev )
2401 p_sys->psz_adev = strdup( ALSA_DEFAULT );
2406 if( i_fd >= 0 ) close( i_fd );
2408 if( p_hw_params ) snd_pcm_hw_params_free( p_hw_params );
2409 if( p_sys->p_alsa_pcm ) snd_pcm_close( p_sys->p_alsa_pcm );
2410 free( psz_alsa_device_name );
2417 static int OpenAudioDevOss( vlc_object_t *p_this, demux_sys_t *p_sys,
2418 vlc_bool_t b_demux )
2420 char *psz_device = p_sys->psz_adev;
2424 if( !psz_device ) psz_device = strdup( OSS_DEFAULT ); /* FIXME leak */
2426 if( (i_fd = open( psz_device, O_RDONLY | O_NONBLOCK )) < 0 )
2428 msg_Err( p_this, "cannot open OSS audio device (%m)" );
2432 i_format = AFMT_S16_LE;
2433 if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
2434 || i_format != AFMT_S16_LE )
2437 "cannot set audio format (16b little endian) (%m)" );
2441 if( ioctl( i_fd, SNDCTL_DSP_STEREO,
2442 &p_sys->b_stereo ) < 0 )
2444 msg_Err( p_this, "cannot set audio channels count (%m)" );
2448 if( ioctl( i_fd, SNDCTL_DSP_SPEED,
2449 &p_sys->i_sample_rate ) < 0 )
2451 msg_Err( p_this, "cannot set audio sample rate (%m)" );
2455 p_sys->i_audio_max_frame_size = 6 * 1024;
2457 if( !p_sys->psz_adev )
2458 p_sys->psz_adev = strdup( OSS_DEFAULT );
2463 if( i_fd >= 0 ) close( i_fd );
2468 static int OpenAudioDev( vlc_object_t *p_this, demux_sys_t *p_sys,
2469 vlc_bool_t b_demux )
2475 if( ( p_sys->i_audio_method & AUDIO_METHOD_ALSA ) && i_fd < 0 )
2476 i_fd = OpenAudioDevAlsa( p_this, p_sys, b_demux );
2479 if( ( p_sys->i_audio_method & AUDIO_METHOD_OSS ) && i_fd < 0 )
2480 i_fd = OpenAudioDevOss( p_this, p_sys, b_demux );
2485 psz_device = p_sys->psz_adev;
2487 msg_Dbg( p_this, "opened adev=`%s' %s %dHz",
2488 psz_device, p_sys->b_stereo ? "stereo" : "mono",
2489 p_sys->i_sample_rate );
2492 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
2494 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
2495 fmt.audio.i_rate = p_sys->i_sample_rate;
2496 fmt.audio.i_bitspersample = 16;
2497 fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
2498 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
2500 msg_Dbg( p_this, "new audio es %d channels %dHz",
2501 fmt.audio.i_channels, fmt.audio.i_rate );
2505 demux_t *p_demux = (demux_t *)p_this;
2506 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
2512 /*****************************************************************************
2513 * ProbeVideoDev: probe video for capabilities
2514 *****************************************************************************/
2515 static vlc_bool_t ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
2523 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
2525 msg_Err( p_obj, "cannot open video device (%m)" );
2529 /* Get device capabilites */
2531 if( ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
2533 msg_Err( p_obj, "cannot get video capabilities (%m)" );
2537 msg_Dbg( p_obj, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
2538 p_sys->dev_cap.card,
2539 p_sys->dev_cap.driver,
2540 (p_sys->dev_cap.version >> 16) & 0xFF,
2541 (p_sys->dev_cap.version >> 8) & 0xFF,
2542 p_sys->dev_cap.version & 0xFF,
2543 p_sys->dev_cap.bus_info );
2545 msg_Dbg( p_obj, "the device has the capabilities: (%c) Video Capure, "
2548 ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE ? 'X':' '),
2549 ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO ? 'X':' '),
2550 ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER ? 'X':' ') );
2552 msg_Dbg( p_obj, "supported I/O methods are: (%c) Read/Write, "
2554 "(%c) Asynchronous",
2555 ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
2556 ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
2557 ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
2559 /* Now, enumerate all the video inputs. This is useless at the moment
2560 since we have no way to present that info to the user except with
2563 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2565 struct v4l2_input t_input;
2567 while( ioctl( i_fd, VIDIOC_ENUMINPUT, &t_input ) >= 0 )
2570 t_input.index = p_sys->i_input;
2573 p_sys->p_inputs = calloc( 1, p_sys->i_input * sizeof( struct v4l2_input ) );
2574 if( !p_sys->p_inputs ) goto open_failed;
2576 for( i_index = 0; i_index < p_sys->i_input; i_index++ )
2578 p_sys->p_inputs[i_index].index = i_index;
2580 if( ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
2582 msg_Err( p_obj, "cannot get video input characteristics (%m)" );
2585 msg_Dbg( p_obj, "video input %i (%s) has type: %s",
2587 p_sys->p_inputs[i_index].name,
2588 p_sys->p_inputs[i_index].type
2589 == V4L2_INPUT_TYPE_TUNER ?
2591 "External analog input" );
2595 /* Probe video standards */
2596 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2598 struct v4l2_standard t_standards;
2599 t_standards.index = 0;
2600 while( ioctl( i_fd, VIDIOC_ENUMSTD, &t_standards ) >=0 )
2602 p_sys->i_standard++;
2603 t_standards.index = p_sys->i_standard;
2606 p_sys->p_standards = calloc( 1, p_sys->i_standard * sizeof( struct v4l2_standard ) );
2607 if( !p_sys->p_standards ) goto open_failed;
2609 for( i_standard = 0; i_standard < p_sys->i_standard; i_standard++ )
2611 p_sys->p_standards[i_standard].index = i_standard;
2613 if( ioctl( i_fd, VIDIOC_ENUMSTD, &p_sys->p_standards[i_standard] ) )
2615 msg_Err( p_obj, "cannot get video input standards (%m)" );
2618 msg_Dbg( p_obj, "video standard %i is: %s",
2620 p_sys->p_standards[i_standard].name);
2624 /* initialize the structures for the ioctls */
2625 for( i_index = 0; i_index < 32; i_index++ )
2627 p_sys->p_audios[i_index].index = i_index;
2630 /* Probe audio inputs */
2631 if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
2633 while( p_sys->i_audio < 32 &&
2634 ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
2636 if( ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
2638 msg_Err( p_obj, "cannot get audio input characteristics (%m)" );
2642 msg_Dbg( p_obj, "audio device %i (%s) is %s",
2644 p_sys->p_audios[p_sys->i_audio].name,
2645 p_sys->p_audios[p_sys->i_audio].capability &
2646 V4L2_AUDCAP_STEREO ?
2647 "Stereo" : "Mono" );
2653 /* List tuner caps */
2654 if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
2656 struct v4l2_tuner tuner;
2657 memset( &tuner, 0, sizeof(tuner) );
2658 while( ioctl( i_fd, VIDIOC_G_TUNER, &tuner ) >= 0 )
2661 memset( &tuner, 0, sizeof(tuner) );
2662 tuner.index = p_sys->i_tuner;
2665 p_sys->p_tuners = calloc( 1, p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
2666 if( !p_sys->p_tuners ) goto open_failed;
2668 for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
2670 p_sys->p_tuners[i_index].index = i_index;
2672 if( ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
2674 msg_Err( p_obj, "cannot get tuner characteristics (%m)" );
2677 msg_Dbg( p_obj, "tuner %i (%s) has type: %s, "
2678 "frequency range: %.1f %s -> %.1f %s",
2680 p_sys->p_tuners[i_index].name,
2681 p_sys->p_tuners[i_index].type
2682 == V4L2_TUNER_RADIO ?
2683 "Radio" : "Analog TV",
2684 p_sys->p_tuners[i_index].rangelow * 62.5,
2685 p_sys->p_tuners[i_index].capability &
2686 V4L2_TUNER_CAP_LOW ?
2688 p_sys->p_tuners[i_index].rangehigh * 62.5,
2689 p_sys->p_tuners[i_index].capability &
2690 V4L2_TUNER_CAP_LOW ?
2693 struct v4l2_frequency frequency;
2694 memset( &frequency, 0, sizeof( frequency ) );
2695 if( ioctl( i_fd, VIDIOC_G_FREQUENCY, &frequency ) < 0 )
2697 msg_Err( p_obj, "cannot get tuner frequency (%m)" );
2700 msg_Dbg( p_obj, "tuner %i (%s) frequency: %.1f %s",
2702 p_sys->p_tuners[i_index].name,
2703 frequency.frequency * 62.5,
2704 p_sys->p_tuners[i_index].capability &
2705 V4L2_TUNER_CAP_LOW ?
2710 /* Probe for available chromas */
2711 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2713 struct v4l2_fmtdesc codec;
2716 memset( &codec, 0, sizeof(codec) );
2717 codec.index = i_index;
2718 codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2720 while( ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
2723 codec.index = i_index;
2726 p_sys->i_codec = i_index;
2728 p_sys->p_codecs = calloc( 1, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
2730 for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
2732 p_sys->p_codecs[i_index].index = i_index;
2733 p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2735 if( ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
2737 msg_Err( p_obj, "cannot get codec description (%m)" );
2741 /* only print if vlc supports the format */
2742 char psz_fourcc_v4l2[5];
2743 memset( &psz_fourcc_v4l2, 0, sizeof( psz_fourcc_v4l2 ) );
2744 vlc_fourcc_to_char( p_sys->p_codecs[i_index].pixelformat,
2746 vlc_bool_t b_codec_supported = VLC_FALSE;
2747 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
2749 if( v4l2chroma_to_fourcc[i].i_v4l2 == p_sys->p_codecs[i_index].pixelformat )
2751 b_codec_supported = VLC_TRUE;
2754 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
2755 vlc_fourcc_to_char( v4l2chroma_to_fourcc[i].i_fourcc,
2757 msg_Dbg( p_obj, "device supports chroma %4s [%s, %s]",
2759 p_sys->p_codecs[i_index].description,
2762 #ifdef VIDIOC_ENUM_FRAMESIZES
2763 /* This is new in Linux 2.6.19 */
2764 /* List valid frame sizes for this format */
2765 struct v4l2_frmsizeenum frmsize;
2767 frmsize.pixel_format = p_sys->p_codecs[i_index].pixelformat;
2768 if( ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) < 0 )
2770 /* Not all devices support this ioctl */
2771 msg_Warn( p_obj, "Unable to query for frame sizes" );
2775 switch( frmsize.type )
2777 case V4L2_FRMSIZE_TYPE_DISCRETE:
2781 " device supports size %dx%d",
2782 frmsize.discrete.width, frmsize.discrete.height );
2784 } while( ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 );
2786 case V4L2_FRMSIZE_TYPE_STEPWISE:
2788 " device supports sizes %dx%d to %dx%d using %dx%d increments",
2789 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2790 frmsize.stepwise.max_width, frmsize.stepwise.max_height,
2791 frmsize.stepwise.step_width, frmsize.stepwise.step_height );
2793 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
2795 " device supports all sizes %dx%d to %dx%d",
2796 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2797 frmsize.stepwise.max_width, frmsize.stepwise.max_height );
2804 if( !b_codec_supported )
2807 "device codec %4s (%s) not supported as access_demux",
2809 p_sys->p_codecs[i_index].description );
2816 if( i_fd >= 0 ) close( i_fd );
2821 if( i_fd >= 0 ) close( i_fd );
2826 /*****************************************************************************
2827 * ProbeAudioDev: probe audio for capabilities
2828 *****************************************************************************/
2830 static vlc_bool_t ProbeAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys,
2834 snd_pcm_t *p_alsa_pcm;
2835 char* psz_alsa_device_name = ResolveALSADeviceName( psz_device ?: ALSA_DEFAULT );
2837 if( ( i_err = snd_pcm_open( &p_alsa_pcm, psz_alsa_device_name, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0 )
2839 msg_Err( p_this, "cannot open device %s for ALSA audio (%s)", psz_alsa_device_name, snd_strerror( i_err ) );
2840 free( psz_alsa_device_name );
2844 snd_pcm_close( p_alsa_pcm );
2845 free( psz_alsa_device_name );
2851 static vlc_bool_t ProbeAudioDevOss( vlc_object_t *p_this, demux_sys_t *p_sys,
2856 if( !psz_device ) psz_device = strdup( OSS_DEFAULT ); /* FIXME leak */
2858 if( ( i_fd = open( psz_device, O_RDONLY | O_NONBLOCK ) ) < 0 )
2860 msg_Err( p_this, "cannot open device %s for OSS audio (%m)", psz_device );
2864 /* this will fail if the device is video */
2865 if( ioctl( i_fd, SNDCTL_DSP_GETCAPS, &i_caps ) < 0 )
2867 msg_Err( p_this, "cannot get audio caps (%m)" );
2871 if( i_fd >= 0 ) close( i_fd );
2876 if( i_fd >= 0 ) close( i_fd );
2880 static vlc_bool_t ProbeAudioDev( vlc_object_t *p_this, demux_sys_t *p_sys,
2884 if( ( p_sys->i_audio_method & AUDIO_METHOD_ALSA )
2885 && ProbeAudioDevAlsa( p_this, p_sys, psz_device ) )
2887 p_sys->i_audio_method = AUDIO_METHOD_ALSA;
2892 if( ( p_sys->i_audio_method & AUDIO_METHOD_OSS )
2893 && ProbeAudioDevOss( p_this, p_sys, psz_device ) )
2895 p_sys->i_audio_method = AUDIO_METHOD_OSS;
2899 p_sys->i_audio_method = 0;
2903 static void name2var( unsigned char *name )
2905 for( ; *name; name++ )
2906 *name = (*name == ' ') ? '_' : tolower( *name );
2909 /*****************************************************************************
2910 * Print a user-class v4l2 control's details, create the relevant variable,
2911 * change the value if needed.
2912 *****************************************************************************/
2913 static void ControlListPrint( vlc_object_t *p_obj, int i_fd,
2914 struct v4l2_queryctrl queryctrl,
2915 vlc_bool_t b_reset, vlc_bool_t b_demux )
2917 struct v4l2_querymenu querymenu;
2924 vlc_value_t val, val2;
2926 if( queryctrl.flags & V4L2_CTRL_FLAG_GRABBED )
2927 msg_Dbg( p_obj, " control is busy" );
2928 if( queryctrl.flags & V4L2_CTRL_FLAG_READ_ONLY )
2929 msg_Dbg( p_obj, " control is read-only" );
2931 for( i = 0; controls[i].psz_name != NULL; i++ )
2932 if( controls[i].i_cid == queryctrl.id ) break;
2934 if( controls[i].psz_name )
2936 psz_name = strdup( controls[i].psz_name );
2937 char psz_cfg_name[40];
2938 sprintf( psz_cfg_name, CFG_PREFIX "%s", psz_name );
2939 i_val = var_CreateGetInteger( p_obj, psz_cfg_name );
2940 var_Destroy( p_obj, psz_cfg_name );
2944 psz_name = strdup( (const char *)queryctrl.name );
2945 name2var( (unsigned char *)psz_name );
2949 switch( queryctrl.type )
2951 case V4L2_CTRL_TYPE_INTEGER:
2952 msg_Dbg( p_obj, " integer control" );
2954 " valid values: %d to %d by steps of %d",
2955 queryctrl.minimum, queryctrl.maximum,
2958 var_Create( p_obj, psz_name,
2959 VLC_VAR_INTEGER | VLC_VAR_HASMIN | VLC_VAR_HASMAX
2960 | VLC_VAR_HASSTEP | VLC_VAR_ISCOMMAND );
2961 val.i_int = queryctrl.minimum;
2962 var_Change( p_obj, psz_name, VLC_VAR_SETMIN, &val, NULL );
2963 val.i_int = queryctrl.maximum;
2964 var_Change( p_obj, psz_name, VLC_VAR_SETMAX, &val, NULL );
2965 val.i_int = queryctrl.step;
2966 var_Change( p_obj, psz_name, VLC_VAR_SETSTEP, &val, NULL );
2968 case V4L2_CTRL_TYPE_BOOLEAN:
2969 msg_Dbg( p_obj, " boolean control" );
2970 var_Create( p_obj, psz_name,
2971 VLC_VAR_BOOL | VLC_VAR_ISCOMMAND );
2973 case V4L2_CTRL_TYPE_MENU:
2974 msg_Dbg( p_obj, " menu control" );
2975 var_Create( p_obj, psz_name,
2976 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE
2977 | VLC_VAR_ISCOMMAND );
2978 memset( &querymenu, 0, sizeof( querymenu ) );
2979 for( i_mid = queryctrl.minimum;
2980 i_mid <= (unsigned)queryctrl.maximum;
2983 querymenu.index = i_mid;
2984 querymenu.id = queryctrl.id;
2985 if( ioctl( i_fd, VIDIOC_QUERYMENU, &querymenu ) >= 0 )
2987 msg_Dbg( p_obj, " %d: %s",
2988 querymenu.index, querymenu.name );
2989 val.i_int = querymenu.index;
2990 val2.psz_string = (char *)querymenu.name;
2991 var_Change( p_obj, psz_name,
2992 VLC_VAR_ADDCHOICE, &val, &val2 );
2996 case V4L2_CTRL_TYPE_BUTTON:
2997 msg_Dbg( p_obj, " button control" );
2998 var_Create( p_obj, psz_name,
2999 VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
3001 case V4L2_CTRL_TYPE_CTRL_CLASS:
3002 msg_Dbg( p_obj, " control class" );
3003 var_Create( p_obj, psz_name, VLC_VAR_VOID );
3006 msg_Dbg( p_obj, " unknown control type (FIXME)" );
3011 switch( queryctrl.type )
3013 case V4L2_CTRL_TYPE_INTEGER:
3014 case V4L2_CTRL_TYPE_BOOLEAN:
3015 case V4L2_CTRL_TYPE_MENU:
3017 struct v4l2_control control;
3018 msg_Dbg( p_obj, " default value: %d",
3019 queryctrl.default_value );
3020 memset( &control, 0, sizeof( control ) );
3021 control.id = queryctrl.id;
3022 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
3024 msg_Dbg( p_obj, " current value: %d", control.value );
3028 i_val = control.value;
3029 if( b_reset && queryctrl.default_value != control.value )
3031 msg_Dbg( p_obj, " reset value to default" );
3032 Control( p_obj, i_fd, psz_name,
3033 queryctrl.id, queryctrl.default_value );
3038 Control( p_obj, i_fd, psz_name,
3039 queryctrl.id, i_val );
3047 val.psz_string = (char *)queryctrl.name;
3048 var_Change( p_obj, psz_name, VLC_VAR_SETTEXT, &val, NULL );
3049 val.i_int = queryctrl.id;
3050 val2.psz_string = (char *)psz_name;
3051 var_Change( p_obj, "allcontrols", VLC_VAR_ADDCHOICE, &val, &val2 );
3052 /* bad things happen changing MPEG mid-stream
3053 * so don't add to Ext Settings GUI */
3054 if( V4L2_CTRL_ID2CLASS( queryctrl.id ) != V4L2_CTRL_CLASS_MPEG )
3055 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
3057 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
3060 var_SetBool( p_obj, psz_name, i_val );
3062 case VLC_VAR_INTEGER:
3063 var_SetInteger( p_obj, psz_name, i_val );
3068 msg_Warn( p_obj, "FIXME: %s %s %d", __FILE__, __func__,
3074 var_AddCallback( p_obj, psz_name,
3075 DemuxControlCallback, (void*)queryctrl.id );
3077 var_AddCallback( p_obj, psz_name,
3078 AccessControlCallback, (void*)queryctrl.id );
3083 /*****************************************************************************
3084 * List all user-class v4l2 controls, set them to the user specified
3085 * value and create the relevant variables to enable runtime changes
3086 *****************************************************************************/
3087 static int ControlList( vlc_object_t *p_obj, int i_fd,
3088 vlc_bool_t b_reset, vlc_bool_t b_demux )
3090 struct v4l2_queryctrl queryctrl;
3093 memset( &queryctrl, 0, sizeof( queryctrl ) );
3095 /* A list of available controls (aka the variable name) will be
3096 * stored as choices in the "allcontrols" variable. We'll thus be able
3097 * to use those to create an appropriate interface
3098 * A list of available controls that can be changed mid-stream will
3099 * be stored in the "controls" variable */
3100 var_Create( p_obj, "controls", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
3101 var_Create( p_obj, "allcontrols", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
3103 var_Create( p_obj, "controls-update", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
3105 /* Add a control to reset all controls to their default values */
3106 vlc_value_t val, val2;
3107 var_Create( p_obj, "controls-reset", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
3108 val.psz_string = _( "Reset controls to default" );
3109 var_Change( p_obj, "controls-reset", VLC_VAR_SETTEXT, &val, NULL );
3111 val2.psz_string = (char *)"controls-reset";
3112 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
3114 var_AddCallback( p_obj, "controls-reset", DemuxControlResetCallback, NULL );
3116 var_AddCallback( p_obj, "controls-reset", AccessControlResetCallback, NULL );
3118 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
3119 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3121 msg_Dbg( p_obj, "Extended control API supported by v4l2 driver" );
3123 /* List extended controls */
3124 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
3125 while( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3127 if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS )
3129 msg_Dbg( p_obj, "%s", queryctrl.name );
3130 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
3133 switch( V4L2_CTRL_ID2CLASS( queryctrl.id ) )
3135 case V4L2_CTRL_CLASS_USER:
3136 msg_Dbg( p_obj, "Available control: %s (%x)",
3137 queryctrl.name, queryctrl.id );
3139 case V4L2_CTRL_CLASS_MPEG:
3140 name2var( queryctrl.name );
3141 msg_Dbg( p_obj, "Available MPEG control: %s (%x)",
3142 queryctrl.name, queryctrl.id );
3145 msg_Dbg( p_obj, "Available private control: %s (%x)",
3146 queryctrl.name, queryctrl.id );
3149 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
3150 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
3155 msg_Dbg( p_obj, "Extended control API not supported by v4l2 driver" );
3157 /* List public controls */
3158 for( i_cid = V4L2_CID_BASE;
3159 i_cid < V4L2_CID_LASTP1;
3162 queryctrl.id = i_cid;
3163 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3165 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3167 msg_Dbg( p_obj, "Available control: %s (%x)",
3168 queryctrl.name, queryctrl.id );
3169 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
3173 /* List private controls */
3174 for( i_cid = V4L2_CID_PRIVATE_BASE;
3178 queryctrl.id = i_cid;
3179 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3181 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3183 msg_Dbg( p_obj, "Available private control: %s (%x)",
3184 queryctrl.name, queryctrl.id );
3185 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
3195 static void SetAvailControlsByString( vlc_object_t *p_obj, demux_sys_t *p_sys,
3198 char *psz_parser = p_sys->psz_set_ctrls;
3199 vlc_value_t val, text, name;
3201 if( psz_parser == NULL )
3204 if( *psz_parser == '{' )
3207 int i_ret = var_Change( p_obj, "allcontrols", VLC_VAR_GETCHOICES,
3211 msg_Err( p_obj, "Oops, can't find 'allcontrols' variable." );
3215 while( *psz_parser && *psz_parser != '}' )
3217 char *psz_delim, *psz_assign;
3219 while( *psz_parser == ',' || *psz_parser == ' ' )
3222 psz_delim = strchr( psz_parser, ',' );
3223 if( psz_delim == NULL )
3224 psz_delim = strchr( psz_parser, '}' );
3225 if( psz_delim == NULL )
3226 psz_delim = psz_parser + strlen( psz_parser );
3228 psz_assign = memchr( psz_parser, '=', psz_delim - psz_parser );
3229 if( psz_assign == NULL )
3231 char *psz_name = strndup( psz_parser, psz_delim - psz_parser );
3232 msg_Err( p_obj, "%s missing '='", psz_name );
3234 psz_parser = psz_delim + 1;
3239 i < val.p_list->i_count ;//&& psz_parser < psz_assign;
3242 const char *psz_var = text.p_list->p_values[i].psz_string;
3243 int i_cid = val.p_list->p_values[i].i_int;
3244 var_Change( p_obj, psz_var, VLC_VAR_GETTEXT, &name, NULL );
3245 const char *psz_name = name.psz_string;
3247 int i_availstrlen = strlen( psz_name );
3248 int i_parsestrlen = psz_assign - psz_parser;
3249 int i_maxstrlen = __MAX( i_availstrlen, i_parsestrlen);
3251 if( !strncasecmp( psz_name, psz_parser, i_maxstrlen ) )
3253 Control( p_obj, i_fd, psz_name, i_cid,
3254 strtol( ++psz_assign, &psz_parser, 0) );
3258 if( psz_parser < psz_assign )
3260 char *psz_name = strndup( psz_parser, psz_assign - psz_parser );
3261 msg_Err( p_obj, "Control %s not available", psz_name );
3263 psz_parser = ( *psz_delim ) ? ( psz_delim + 1 ) : psz_delim;
3268 /*****************************************************************************
3269 * Reset all user-class v4l2 controls to their default value
3270 *****************************************************************************/
3271 static int ControlReset( vlc_object_t *p_obj, int i_fd )
3273 struct v4l2_queryctrl queryctrl;
3276 memset( &queryctrl, 0, sizeof( queryctrl ) );
3278 /* public controls */
3279 for( i_cid = V4L2_CID_BASE;
3280 i_cid < V4L2_CID_LASTP1;
3283 queryctrl.id = i_cid;
3284 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3286 struct v4l2_control control;
3287 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3289 memset( &control, 0, sizeof( control ) );
3290 control.id = queryctrl.id;
3291 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
3292 && queryctrl.default_value != control.value )
3295 for( i = 0; controls[i].psz_name != NULL; i++ )
3296 if( controls[i].i_cid == queryctrl.id ) break;
3297 Control( p_obj, i_fd,
3298 controls[i].psz_name ? controls[i].psz_name
3299 : (const char *)queryctrl.name,
3300 queryctrl.id, queryctrl.default_value );
3305 /* private controls */
3306 for( i_cid = V4L2_CID_PRIVATE_BASE;
3310 queryctrl.id = i_cid;
3311 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3313 struct v4l2_control control;
3314 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3316 memset( &control, 0, sizeof( control ) );
3317 control.id = queryctrl.id;
3318 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
3319 && queryctrl.default_value != control.value )
3321 Control( p_obj, i_fd, (const char *)queryctrl.name,
3322 queryctrl.id, queryctrl.default_value );
3331 /*****************************************************************************
3332 * Issue user-class v4l2 controls
3333 *****************************************************************************/
3334 static int Control( vlc_object_t *p_obj, int i_fd,
3335 const char *psz_name, int i_cid, int i_value )
3337 struct v4l2_queryctrl queryctrl;
3338 struct v4l2_control control;
3339 struct v4l2_ext_control ext_control;
3340 struct v4l2_ext_controls ext_controls;
3345 memset( &queryctrl, 0, sizeof( queryctrl ) );
3347 queryctrl.id = i_cid;
3349 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) < 0
3350 || queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3352 msg_Dbg( p_obj, "%s (%x) control is not supported.", psz_name, i_cid );
3353 return VLC_EGENERIC;
3356 memset( &control, 0, sizeof( control ) );
3357 memset( &ext_control, 0, sizeof( ext_control ) );
3358 memset( &ext_controls, 0, sizeof( ext_controls ) );
3360 ext_control.id = i_cid;
3361 ext_controls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
3362 ext_controls.count = 1;
3363 ext_controls.controls = &ext_control;
3367 if( V4L2_CTRL_ID2CLASS( i_cid ) == V4L2_CTRL_CLASS_USER )
3369 control.value = i_value;
3370 if( ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
3372 msg_Err( p_obj, "unable to set %s (%x) to %d (%m)",
3373 psz_name, i_cid, i_value );
3374 return VLC_EGENERIC;
3379 ext_control.value = i_value;
3380 if( ioctl( i_fd, VIDIOC_S_EXT_CTRLS, &ext_controls ) < 0 )
3382 msg_Err( p_obj, "unable to set %s (%x) to %d (%m)",
3383 psz_name, i_cid, i_value );
3384 return VLC_EGENERIC;
3391 if( V4L2_CTRL_ID2CLASS( i_cid ) == V4L2_CTRL_CLASS_USER )
3392 i_ret = ioctl( i_fd, VIDIOC_G_CTRL, &control );
3395 i_ret = ioctl( i_fd, VIDIOC_G_EXT_CTRLS, &ext_controls );
3396 control.value = ext_control.value;
3402 msg_Dbg( p_obj, "video %s: %d", psz_name, control.value );
3403 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
3406 val.b_bool = control.value;
3407 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
3408 var_SetVoid( p_obj, "controls-update" );
3410 case VLC_VAR_INTEGER:
3411 val.i_int = control.value;
3412 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
3413 var_SetVoid( p_obj, "controls-update" );
3420 /*****************************************************************************
3421 * On the fly change settings callback
3422 *****************************************************************************/
3423 static int DemuxControlCallback( vlc_object_t *p_this,
3424 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3427 demux_t *p_demux = (demux_t*)p_this;
3428 demux_sys_t *p_sys = p_demux->p_sys;
3429 int i_cid = (int)p_data;
3431 int i_fd = p_sys->i_fd_video;
3434 return VLC_EGENERIC;
3436 Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
3438 return VLC_EGENERIC;
3441 static int DemuxControlResetCallback( vlc_object_t *p_this,
3442 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3445 demux_t *p_demux = (demux_t*)p_this;
3446 demux_sys_t *p_sys = p_demux->p_sys;
3448 int i_fd = p_sys->i_fd_video;
3451 return VLC_EGENERIC;
3453 ControlReset( p_this, i_fd );
3455 return VLC_EGENERIC;
3458 static int AccessControlCallback( vlc_object_t *p_this,
3459 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3462 access_t *p_access = (access_t *)p_this;
3463 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
3464 int i_cid = (int)p_data;
3466 int i_fd = p_sys->i_fd_video;
3469 return VLC_EGENERIC;
3471 Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
3473 return VLC_EGENERIC;
3476 static int AccessControlResetCallback( vlc_object_t *p_this,
3477 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3480 access_t *p_access = (access_t *)p_this;
3481 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
3483 int i_fd = p_sys->i_fd_video;
3486 return VLC_EGENERIC;
3488 ControlReset( p_this, i_fd );
3490 return VLC_EGENERIC;