1 /*****************************************************************************
2 * v4l2.c : Video4Linux2 input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2007 the VideoLAN team
7 * Authors: Benjamin Pracht <bigben at videolan dot org>
8 * Richard Hosking <richard at hovis dot net>
9 * Antoine Cellerier <dionoea at videolan d.t org>
10 * Dennis Lou <dlou99 at yahoo dot com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
28 * Sections based on the reference V4L2 capture example at
29 * http://v4l2spec.bytesex.org/spec/capture-example.html
31 * ALSA support based on parts of
32 * http://www.equalarea.com/paul/alsa-audio.html
33 * and hints taken from alsa-utils (aplay/arecord)
34 * http://www.alsa-project.org
38 * TODO: Tuner partial implementation.
39 * TODO: Add more MPEG stream params
42 /*****************************************************************************
44 *****************************************************************************/
47 #include <vlc_access.h>
48 #include <vlc_demux.h>
49 #include <vlc_input.h>
54 #include <sys/ioctl.h>
57 #include <linux/videodev2.h>
59 #include <sys/soundcard.h>
62 # define ALSA_PCM_NEW_HW_PARAMS_API
63 # define ALSA_PCM_NEW_SW_PARAMS_API
64 # include <alsa/asoundlib.h>
69 /*****************************************************************************
71 *****************************************************************************/
73 static int DemuxOpen ( vlc_object_t * );
74 static void DemuxClose( vlc_object_t * );
75 static int AccessOpen ( vlc_object_t * );
76 static void AccessClose( vlc_object_t * );
78 #define DEV_TEXT N_("Device name")
79 #define DEV_LONGTEXT N_( \
80 "Name of the device to use. " \
81 "If you don't specify anything, /dev/video0 will be used.")
82 #define STANDARD_TEXT N_( "Standard" )
83 #define STANDARD_LONGTEXT N_( \
84 "Video standard (Default, SECAM, PAL, or NTSC)." )
85 #define CHROMA_TEXT N_("Video input chroma format")
86 #define CHROMA_LONGTEXT N_( \
87 "Force the Video4Linux2 video device to use a specific chroma format " \
88 "(eg. I420 or I422 for raw images, MJPEG for M-JPEG compressed input) " \
89 "(Complete list: GREY, I240, RV16, RV15, RV24, RV32, YUY2, YUYV, UYVY, " \
90 "I41N, I422, I420, I411, I410, MJPG)")
91 #define INPUT_TEXT N_( "Input" )
92 #define INPUT_LONGTEXT N_( \
93 "Input of the card to use (Usually, 0 = tuner, " \
94 "1 = composite, 2 = svideo)." )
95 #define IOMETHOD_TEXT N_( "IO Method" )
96 #define IOMETHOD_LONGTEXT N_( \
97 "IO Method (READ, MMAP, USERPTR)." )
98 #define WIDTH_TEXT N_( "Width" )
99 #define WIDTH_LONGTEXT N_( \
100 "Force width (-1 for autodetect)." )
101 #define HEIGHT_TEXT N_( "Height" )
102 #define HEIGHT_LONGTEXT N_( \
103 "Force height (-1 for autodetect)." )
104 #define FPS_TEXT N_( "Framerate" )
105 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
106 "(-1 for autodetect)." )
108 #define CTRL_RESET_TEXT N_( "Reset v4l2 controls" )
109 #define CTRL_RESET_LONGTEXT N_( \
110 "Reset controls to defaults provided by the v4l2 driver." )
111 #define BRIGHTNESS_TEXT N_( "Brightness" )
112 #define BRIGHTNESS_LONGTEXT N_( \
113 "Brightness of the video input (if supported by the v4l2 driver)." )
114 #define CONTRAST_TEXT N_( "Contrast" )
115 #define CONTRAST_LONGTEXT N_( \
116 "Contrast of the video input (if supported by the v4l2 driver)." )
117 #define SATURATION_TEXT N_( "Saturation" )
118 #define SATURATION_LONGTEXT N_( \
119 "Saturation of the video input (if supported by the v4l2 driver)." )
120 #define HUE_TEXT N_( "Hue" )
121 #define HUE_LONGTEXT N_( \
122 "Hue of the video input (if supported by the v4l2 driver)." )
123 #define BLACKLEVEL_TEXT N_( "Black level" )
124 #define BLACKLEVEL_LONGTEXT N_( \
125 "Black level of the video input (if supported by the v4l2 driver)." )
126 #define AUTOWHITEBALANCE_TEXT N_( "Auto white balance" )
127 #define AUTOWHITEBALANCE_LONGTEXT N_( \
128 "Automatically set the white balance of the video input " \
129 "(if supported by the v4l2 driver)." )
130 #define DOWHITEBALANCE_TEXT N_( "Do white balance" )
131 #define DOWHITEBALANCE_LONGTEXT N_( \
132 "Trigger a white balancing action, useless if auto white balance is " \
133 "activated (if supported by the v4l2 driver)." )
134 #define REDBALANCE_TEXT N_( "Red balance" )
135 #define REDBALANCE_LONGTEXT N_( \
136 "Red balance of the video input (if supported by the v4l2 driver)." )
137 #define BLUEBALANCE_TEXT N_( "Blue balance" )
138 #define BLUEBALANCE_LONGTEXT N_( \
139 "Blue balance of the video input (if supported by the v4l2 driver)." )
140 #define GAMMA_TEXT N_( "Gamma" )
141 #define GAMMA_LONGTEXT N_( \
142 "Gamma of the video input (if supported by the v4l2 driver)." )
143 #define EXPOSURE_TEXT N_( "Exposure" )
144 #define EXPOSURE_LONGTEXT N_( \
145 "Exposure of the video input (if supported by the v4L2 driver)." )
146 #define AUTOGAIN_TEXT N_( "Auto gain" )
147 #define AUTOGAIN_LONGTEXT N_( \
148 "Automatically set the video input's gain (if supported by the " \
150 #define GAIN_TEXT N_( "Gain" )
151 #define GAIN_LONGTEXT N_( \
152 "Video input's gain (if supported by the v4l2 driver)." )
153 #define HFLIP_TEXT N_( "Horizontal flip" )
154 #define HFLIP_LONGTEXT N_( \
155 "Flip the video horizontally (if supported by the v4l2 driver)." )
156 #define VFLIP_TEXT N_( "Vertical flip" )
157 #define VFLIP_LONGTEXT N_( \
158 "Flip the video vertically (if supported by the v4l2 driver)." )
159 #define HCENTER_TEXT N_( "Horizontal centering" )
160 #define HCENTER_LONGTEXT N_( \
161 "Set the camera's horizontal centering (if supported by the v4l2 driver)." )
162 #define VCENTER_TEXT N_( "Vertical centering" )
163 #define VCENTER_LONGTEXT N_( \
164 "Set the camera's vertical centering (if supported by the v4l2 driver)." )
166 #define ADEV_TEXT N_("Audio device name")
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.")
172 #define ADEV_LONGTEXT N_( \
173 "Name of the audio device to use. " \
174 "If you don't specify anything, \"/dev/dsp\" will be used for OSS, " \
177 #define AUDIO_METHOD_TEXT N_( "Audio method" )
179 #define AUDIO_METHOD_LONGTEXT N_( \
180 "Audio method to use: 0 to disable audio, 1 for OSS." )
182 #define AUDIO_METHOD_LONGTEXT N_( \
183 "Audio method to use: 0 to disable audio, 1 for OSS, 2 for ALSA, " \
184 "3 for ALSA or OSS (ALSA is prefered)." )
186 #define STEREO_TEXT N_( "Stereo" )
187 #define STEREO_LONGTEXT N_( \
188 "Capture the audio stream in stereo." )
189 #define SAMPLERATE_TEXT N_( "Samplerate" )
190 #define SAMPLERATE_LONGTEXT N_( \
191 "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
193 #define CACHING_TEXT N_("Caching value in ms")
194 #define CACHING_LONGTEXT N_( \
195 "Caching value for V4L2 captures. This " \
196 "value should be set in milliseconds." )
204 static int i_standards_list[] =
205 { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
206 static const char *psz_standards_list_text[] =
207 { N_("Default"), N_("SECAM"), N_("PAL"), N_("NTSC") };
209 static int i_iomethod_list[] =
210 { IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR };
211 static const char *psz_iomethod_list_text[] =
212 { N_("READ"), N_("MMAP"), N_("USERPTR") };
217 #define AUDIO_METHOD_OSS 1
218 #define OSS_DEFAULT "/dev/dsp"
219 #define AUDIO_METHOD_ALSA 2
220 #define ALSA_DEFAULT "hw"
221 #define CFG_PREFIX "v4l2-"
224 set_shortname( _("Video4Linux2") );
225 set_description( _("Video4Linux2 input") );
226 set_category( CAT_INPUT );
227 set_subcategory( SUBCAT_INPUT_ACCESS );
229 set_section( N_( "Video input" ), NULL );
230 add_string( CFG_PREFIX "dev", "/dev/video0", 0, DEV_TEXT, DEV_LONGTEXT,
232 add_integer( CFG_PREFIX "standard", 0, NULL, STANDARD_TEXT,
233 STANDARD_LONGTEXT, VLC_FALSE );
234 change_integer_list( i_standards_list, psz_standards_list_text, 0 );
235 add_string( CFG_PREFIX "chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
237 add_integer( CFG_PREFIX "input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
239 add_integer( CFG_PREFIX "io", IO_METHOD_MMAP, NULL, IOMETHOD_TEXT,
240 IOMETHOD_LONGTEXT, VLC_TRUE );
241 change_integer_list( i_iomethod_list, psz_iomethod_list_text, 0 );
242 add_integer( CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
243 WIDTH_LONGTEXT, VLC_TRUE );
244 add_integer( CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
245 HEIGHT_LONGTEXT, VLC_TRUE );
246 add_float( CFG_PREFIX "fps", 0, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
248 set_section( N_( "Audio input" ), NULL );
249 add_string( CFG_PREFIX "adev", NULL, 0, ADEV_TEXT, ADEV_LONGTEXT,
251 add_integer( CFG_PREFIX "audio-method", AUDIO_METHOD_OSS|AUDIO_METHOD_ALSA,
252 NULL, AUDIO_METHOD_TEXT, AUDIO_METHOD_LONGTEXT, VLC_TRUE );
253 add_bool( CFG_PREFIX "stereo", VLC_TRUE, NULL, STEREO_TEXT, STEREO_LONGTEXT,
255 add_integer( CFG_PREFIX "samplerate", 48000, NULL, SAMPLERATE_TEXT,
256 SAMPLERATE_LONGTEXT, VLC_TRUE );
257 add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
258 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
260 set_section( N_( "Controls" ),
261 N_( "v4l2 driver controls, if supported by your v4l2 driver." ) );
262 add_bool( CFG_PREFIX "controls-reset", VLC_FALSE, NULL, CTRL_RESET_TEXT,
263 CTRL_RESET_LONGTEXT, VLC_TRUE );
264 add_integer( CFG_PREFIX "brightness", -1, NULL, BRIGHTNESS_TEXT,
265 BRIGHTNESS_LONGTEXT, VLC_TRUE );
266 add_integer( CFG_PREFIX "contrast", -1, NULL, CONTRAST_TEXT,
267 CONTRAST_LONGTEXT, VLC_TRUE );
268 add_integer( CFG_PREFIX "saturation", -1, NULL, SATURATION_TEXT,
269 SATURATION_LONGTEXT, VLC_TRUE );
270 add_integer( CFG_PREFIX "hue", -1, NULL, HUE_TEXT,
271 HUE_LONGTEXT, VLC_TRUE );
272 add_integer( CFG_PREFIX "black-level", -1, NULL, BLACKLEVEL_TEXT,
273 BLACKLEVEL_LONGTEXT, VLC_TRUE );
274 add_integer( CFG_PREFIX "auto-white-balance", -1, NULL,
275 AUTOWHITEBALANCE_TEXT, AUTOWHITEBALANCE_LONGTEXT, VLC_TRUE );
276 add_integer( CFG_PREFIX "do-white-balance", -1, NULL, DOWHITEBALANCE_TEXT,
277 DOWHITEBALANCE_LONGTEXT, VLC_TRUE );
278 add_integer( CFG_PREFIX "red-balance", -1, NULL, REDBALANCE_TEXT,
279 REDBALANCE_LONGTEXT, VLC_TRUE );
280 add_integer( CFG_PREFIX "blue-balance", -1, NULL, BLUEBALANCE_TEXT,
281 BLUEBALANCE_LONGTEXT, VLC_TRUE );
282 add_integer( CFG_PREFIX "gamma", -1, NULL, GAMMA_TEXT,
283 GAMMA_LONGTEXT, VLC_TRUE );
284 add_integer( CFG_PREFIX "exposure", -1, NULL, EXPOSURE_TEXT,
285 EXPOSURE_LONGTEXT, VLC_TRUE );
286 add_integer( CFG_PREFIX "autogain", -1, NULL, AUTOGAIN_TEXT,
287 AUTOGAIN_LONGTEXT, VLC_TRUE );
288 add_integer( CFG_PREFIX "gain", -1, NULL, GAIN_TEXT,
289 GAIN_LONGTEXT, VLC_TRUE );
290 add_integer( CFG_PREFIX "hflip", -1, NULL, HFLIP_TEXT,
291 HFLIP_LONGTEXT, VLC_TRUE );
292 add_integer( CFG_PREFIX "vflip", -1, NULL, VFLIP_TEXT,
293 VFLIP_LONGTEXT, VLC_TRUE );
294 add_integer( CFG_PREFIX "hcenter", -1, NULL, HCENTER_TEXT,
295 HCENTER_LONGTEXT, VLC_TRUE );
296 add_integer( CFG_PREFIX "vcenter", -1, NULL, VCENTER_TEXT,
297 VCENTER_LONGTEXT, VLC_TRUE );
299 add_shortcut( "v4l2" );
300 set_capability( "access_demux", 10 );
301 set_callbacks( DemuxOpen, DemuxClose );
304 set_description( _("Video4Linux2 Compressed A/V") );
305 set_capability( "access2", 0 );
306 /* use these when open as access_demux fails; VLC will use another demux */
307 set_callbacks( AccessOpen, AccessClose );
311 /*****************************************************************************
312 * Access: local prototypes
313 *****************************************************************************/
315 static void CommonClose( vlc_object_t *, demux_sys_t * );
316 static void ParseMRL( demux_sys_t *, char *, vlc_object_t * );
317 static void GetV4L2Params( demux_sys_t *, vlc_object_t * );
319 static int DemuxControl( demux_t *, int, va_list );
320 static int AccessControl( access_t *, int, va_list );
322 static int Demux( demux_t * );
323 static ssize_t AccessRead( access_t *, uint8_t *, size_t );
325 static block_t* GrabVideo( demux_t *p_demux );
326 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t );
327 static block_t* GrabAudio( demux_t *p_demux );
329 static vlc_bool_t IsPixelFormatSupported( demux_t *p_demux,
330 unsigned int i_pixelformat );
333 static char* ResolveALSADeviceName( const char *psz_device );
335 static int OpenVideoDev( vlc_object_t *, demux_sys_t *, vlc_bool_t );
336 static int OpenAudioDev( vlc_object_t *, demux_sys_t *, vlc_bool_t );
337 static vlc_bool_t ProbeVideoDev( vlc_object_t *, demux_sys_t *,
339 static vlc_bool_t ProbeAudioDev( vlc_object_t *, demux_sys_t *,
342 static int ControlList( vlc_object_t *, int , vlc_bool_t, vlc_bool_t );
343 static int Control( vlc_object_t *, int i_fd,
344 const char *psz_name, int i_cid, int i_value );
346 static int DemuxControlCallback( vlc_object_t *p_this, const char *psz_var,
347 vlc_value_t oldval, vlc_value_t newval,
349 static int DemuxControlResetCallback( vlc_object_t *p_this, const char *psz_var,
350 vlc_value_t oldval, vlc_value_t newval,
352 static int AccessControlCallback( vlc_object_t *p_this, const char *psz_var,
353 vlc_value_t oldval, vlc_value_t newval,
355 static int AccessControlResetCallback( vlc_object_t *p_this,
356 const char *psz_var, vlc_value_t oldval,
357 vlc_value_t newval, void *p_data );
363 } v4l2chroma_to_fourcc[] =
366 { V4L2_PIX_FMT_GREY, VLC_FOURCC('G','R','E','Y') },
367 { V4L2_PIX_FMT_HI240, VLC_FOURCC('I','2','4','0') },
368 { V4L2_PIX_FMT_RGB565, VLC_FOURCC('R','V','1','6') },
369 { V4L2_PIX_FMT_RGB555, VLC_FOURCC('R','V','1','5') },
370 { V4L2_PIX_FMT_BGR24, VLC_FOURCC('R','V','2','4') },
371 { V4L2_PIX_FMT_BGR32, VLC_FOURCC('R','V','3','2') },
372 { V4L2_PIX_FMT_YUYV, VLC_FOURCC('Y','U','Y','2') },
373 { V4L2_PIX_FMT_YUYV, VLC_FOURCC('Y','U','Y','V') },
374 { V4L2_PIX_FMT_UYVY, VLC_FOURCC('U','Y','V','Y') },
375 { V4L2_PIX_FMT_Y41P, VLC_FOURCC('I','4','1','N') },
376 { V4L2_PIX_FMT_YUV422P, VLC_FOURCC('I','4','2','2') },
377 { V4L2_PIX_FMT_YVU420, VLC_FOURCC('Y','V','1','2') },
378 { V4L2_PIX_FMT_YUV411P, VLC_FOURCC('I','4','1','1') },
379 { V4L2_PIX_FMT_YUV410, VLC_FOURCC('I','4','1','0') },
381 /* Raw data types, not in V4L2 spec but still in videodev2.h and supported
383 { V4L2_PIX_FMT_YUV420, VLC_FOURCC('I','4','2','0') },
384 { V4L2_PIX_FMT_RGB444, VLC_FOURCC('R','V','3','2') },
386 /* Compressed data types */
387 { V4L2_PIX_FMT_MJPEG, VLC_FOURCC('M','J','P','G') },
389 { V4L2_PIX_FMT_JPEG, VLC_FOURCC('J','P','E','G') },
390 { V4L2_PIX_FMT_DV, VLC_FOURCC('?','?','?','?') },
391 { V4L2_PIX_FMT_MPEG, VLC_FOURCC('?','?','?','?') },
397 * List of V4L2 chromas were confident enough to use as fallbacks if the
398 * user hasn't provided a --v4l2-chroma value.
400 static const __u32 p_chroma_fallbacks[] =
401 { V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_YVU420, V4L2_PIX_FMT_YUV422P,
402 V4L2_PIX_FMT_YUYV, V4L2_PIX_FMT_MJPEG };
406 const char *psz_name;
410 { "brightness", V4L2_CID_BRIGHTNESS },
411 { "contrast", V4L2_CID_CONTRAST },
412 { "saturation", V4L2_CID_SATURATION },
413 { "hue", V4L2_CID_HUE },
414 { "audio-volume", V4L2_CID_AUDIO_VOLUME },
415 { "audio-balance", V4L2_CID_AUDIO_BALANCE },
416 { "audio-bass", V4L2_CID_AUDIO_BASS },
417 { "audio-treble", V4L2_CID_AUDIO_TREBLE },
418 { "audio-mute", V4L2_CID_AUDIO_MUTE },
419 { "audio-loudness", V4L2_CID_AUDIO_LOUDNESS },
420 { "black-level", V4L2_CID_BLACK_LEVEL },
421 { "auto-white-balance", V4L2_CID_AUTO_WHITE_BALANCE },
422 { "do-white-balance", V4L2_CID_DO_WHITE_BALANCE },
423 { "red-balance", V4L2_CID_RED_BALANCE },
424 { "blue-balance", V4L2_CID_BLUE_BALANCE },
425 { "gamma", V4L2_CID_GAMMA },
426 { "exposure", V4L2_CID_EXPOSURE },
427 { "autogain", V4L2_CID_AUTOGAIN },
428 { "gain", V4L2_CID_GAIN },
429 { "hflip", V4L2_CID_HFLIP },
430 { "vflip", V4L2_CID_VFLIP },
431 { "hcenter", V4L2_CID_HCENTER },
432 { "vcenter", V4L2_CID_VCENTER },
445 char *psz_device; /* Main device from MRL, can be video or audio */
453 char *psz_requested_chroma;
460 struct v4l2_capability dev_cap;
463 struct v4l2_input *p_inputs;
464 int i_selected_input;
467 struct v4l2_standard *p_standards;
468 v4l2_std_id i_selected_standard_id;
471 /* V4L2 devices cannot have more than 32 audio inputs */
472 struct v4l2_audio p_audios[32];
475 struct v4l2_tuner *p_tuners;
478 struct v4l2_fmtdesc *p_codecs;
480 struct buffer_t *p_buffers;
481 unsigned int i_nbuffers;
485 float f_fps; /* <= 0.0 mean to grab at full rate */
486 mtime_t i_video_pts; /* only used when f_fps > 0 */
489 es_out_id_t *p_es_video;
492 unsigned int i_sample_rate;
494 size_t i_audio_max_frame_size;
495 block_t *p_block_audio;
496 es_out_id_t *p_es_audio;
502 snd_pcm_t *p_alsa_pcm;
503 size_t i_alsa_frame_size;
504 int i_alsa_chunk_size;
508 static int FindMainDevice( vlc_object_t *p_this, demux_sys_t *p_sys,
509 int i_flags, vlc_bool_t b_demux,
510 vlc_bool_t b_forced )
512 /* Find main device (video or audio) */
513 if( p_sys->psz_device && *p_sys->psz_device )
515 msg_Dbg( p_this, "main device='%s'", p_sys->psz_device );
517 vlc_bool_t b_maindevice_is_video = VLC_FALSE;
519 /* Try to open as video device */
520 if( i_flags & FIND_VIDEO )
522 msg_Dbg( p_this, "trying device '%s' as video", p_sys->psz_device );
523 if( ProbeVideoDev( p_this, p_sys, p_sys->psz_device ) )
525 msg_Dbg( p_this, "'%s' is a video device", p_sys->psz_device );
526 /* Device was a video device */
527 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
528 p_sys->psz_vdev = p_sys->psz_device;
529 p_sys->psz_device = NULL;
530 p_sys->i_fd_video = OpenVideoDev( p_this, p_sys, b_demux );
531 if( p_sys->i_fd_video < 0 )
533 b_maindevice_is_video = VLC_TRUE;
534 /* If successful we carry on to try the audio if access is forced */
538 /* Try to open as audio device only if main device was not detected as video above */
539 if( i_flags & FIND_AUDIO && !b_maindevice_is_video )
541 msg_Dbg( p_this, "trying device '%s' as audio", p_sys->psz_device );
542 if( ProbeAudioDev( p_this, p_sys, p_sys->psz_device ) )
544 msg_Dbg( p_this, "'%s' is an audio device", p_sys->psz_device );
545 /* Device was an audio device */
546 free( p_sys->psz_adev );
547 p_sys->psz_adev = p_sys->psz_device;
548 p_sys->psz_device = NULL;
549 p_sys->i_fd_audio = OpenAudioDev( p_this, p_sys, b_demux );
550 if( p_sys->i_fd_audio < 0 )
552 /* If successful we carry on to try the video if access is forced */
557 /* If no device opened, only continue if the access was forced */
558 if( b_forced == VLC_FALSE
559 && !( ( i_flags & FIND_VIDEO && p_sys->i_fd_video >= 0 )
560 || ( i_flags & FIND_AUDIO && p_sys->i_fd_audio >= 0 ) ) )
565 /* Find video device */
566 if( i_flags & FIND_VIDEO && p_sys->i_fd_video < 0 )
568 if( !p_sys->psz_vdev || !*p_sys->psz_vdev )
570 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
571 p_sys->psz_vdev = var_CreateGetString( p_this, "v4l2-dev" );
574 msg_Dbg( p_this, "opening '%s' as video", p_sys->psz_vdev );
575 if( p_sys->psz_vdev && *p_sys->psz_vdev
576 && ProbeVideoDev( p_this, p_sys, p_sys->psz_vdev ) )
578 p_sys->i_fd_video = OpenVideoDev( p_this, p_sys, b_demux );
582 /* Find audio device */
583 if( i_flags & FIND_AUDIO && p_sys->i_fd_audio < 0 )
585 if( !p_sys->psz_adev )
587 p_sys->psz_adev = var_CreateGetNonEmptyString( p_this, "v4l2-adev" );
590 msg_Dbg( p_this, "opening '%s' as audio", p_sys->psz_adev );
591 if( ProbeAudioDev( p_this, p_sys, p_sys->psz_adev ) )
593 p_sys->i_fd_audio = OpenAudioDev( p_this, p_sys, b_demux );
597 if( !( ( i_flags & FIND_VIDEO && p_sys->i_fd_video >= 0 )
598 || ( i_flags & FIND_AUDIO && p_sys->i_fd_audio >= 0 ) ) )
605 /*****************************************************************************
606 * DemuxOpen: opens v4l2 device, access_demux callback
607 *****************************************************************************
609 * url: <video device>::::
611 *****************************************************************************/
612 static int DemuxOpen( vlc_object_t *p_this )
614 demux_t *p_demux = (demux_t*)p_this;
617 /* Only when selected */
618 if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
621 p_demux->pf_control = DemuxControl;
622 p_demux->pf_demux = Demux;
623 p_demux->info.i_update = 0;
624 p_demux->info.i_title = 0;
625 p_demux->info.i_seekpoint = 0;
627 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
628 if( p_sys == NULL ) return VLC_ENOMEM;
630 GetV4L2Params(p_sys, (vlc_object_t *) p_demux);
632 ParseMRL( p_sys, p_demux->psz_path, (vlc_object_t *) p_demux );
635 /* Alsa support available? */
636 msg_Dbg( p_demux, "ALSA input support available" );
639 if( FindMainDevice( p_this, p_sys, FIND_VIDEO|FIND_AUDIO,
640 VLC_TRUE, !strcmp( p_demux->psz_access, "v4l2" ) ) != VLC_SUCCESS )
642 DemuxClose( p_this );
649 /*****************************************************************************
650 * GetV4L2Params: fill in p_sys parameters (shared by DemuxOpen and AccessOpen)
651 *****************************************************************************/
652 static void GetV4L2Params( demux_sys_t *p_sys, vlc_object_t *p_obj )
654 p_sys->i_video_pts = -1;
656 p_sys->i_selected_standard_id =
657 i_standards_list[var_CreateGetInteger( p_obj, "v4l2-standard" )];
659 p_sys->i_selected_input = var_CreateGetInteger( p_obj, "v4l2-input" );
661 p_sys->io = var_CreateGetInteger( p_obj, "v4l2-io" );
663 p_sys->i_width = var_CreateGetInteger( p_obj, "v4l2-width" );
664 p_sys->i_height = var_CreateGetInteger( p_obj, "v4l2-height" );
666 var_CreateGetBool( p_obj, "v4l2-controls-reset" );
668 p_sys->f_fps = var_CreateGetFloat( p_obj, "v4l2-fps" );
669 p_sys->i_sample_rate = var_CreateGetInteger( p_obj, "v4l2-samplerate" );
670 p_sys->psz_requested_chroma = var_CreateGetString( p_obj, "v4l2-chroma" );
672 p_sys->i_audio_method = var_CreateGetInteger( p_obj, "v4l2-audio-method" );
674 p_sys->b_stereo = var_CreateGetBool( p_obj, "v4l2-stereo" );
676 p_sys->i_pts = var_CreateGetInteger( p_obj, "v4l2-caching" );
678 p_sys->psz_device = p_sys->psz_vdev = p_sys->psz_adev = NULL;
679 p_sys->i_fd_video = -1;
680 p_sys->i_fd_audio = -1;
682 p_sys->p_es_video = p_sys->p_es_audio = 0;
683 p_sys->p_block_audio = 0;
686 /*****************************************************************************
687 * ParseMRL: parse the options contained in the MRL
688 *****************************************************************************/
689 static void ParseMRL( demux_sys_t *p_sys, char *psz_path, vlc_object_t *p_obj )
691 char *psz_dup = strdup( psz_path );
692 char *psz_parser = psz_dup;
694 while( *psz_parser && *psz_parser != ':' )
699 if( *psz_parser == ':' )
704 *psz_parser++ = '\0';
706 if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
710 psz_parser += strlen( "adev=" );
711 if( strchr( psz_parser, ':' ) )
713 i_len = strchr( psz_parser, ':' ) - psz_parser;
717 i_len = strlen( psz_parser );
720 p_sys->psz_adev = strndup( psz_parser, i_len );
721 if( !*p_sys->psz_adev )
723 free( p_sys->psz_adev );
724 p_sys->psz_adev = NULL;
729 else if( !strncmp( psz_parser, "standard=", strlen( "standard=" ) ) )
731 psz_parser += strlen( "standard=" );
732 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
734 p_sys->i_selected_standard_id = V4L2_STD_PAL;
735 psz_parser += strlen( "pal" );
737 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
739 p_sys->i_selected_standard_id = V4L2_STD_NTSC;
740 psz_parser += strlen( "ntsc" );
742 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
744 p_sys->i_selected_standard_id = V4L2_STD_SECAM;
745 psz_parser += strlen( "secam" );
747 else if( !strncmp( psz_parser, "default", strlen( "default" ) ) )
749 p_sys->i_selected_standard_id = V4L2_STD_UNKNOWN;
750 psz_parser += strlen( "default" );
754 p_sys->i_selected_standard_id = i_standards_list[strtol( psz_parser, &psz_parser, 0 )];
757 else if( !strncmp( psz_parser, "chroma=", strlen( "chroma=" ) ) )
761 psz_parser += strlen( "chroma=" );
762 if( strchr( psz_parser, ':' ) )
764 i_len = strchr( psz_parser, ':' ) - psz_parser;
768 i_len = strlen( psz_parser );
771 if( p_sys->psz_requested_chroma ) free( p_sys->psz_requested_chroma );
772 p_sys->psz_requested_chroma = strndup( psz_parser, i_len );
776 else if( !strncmp( psz_parser, "input=", strlen( "input=" ) ) )
778 p_sys->i_selected_input = strtol( psz_parser + strlen( "input=" ),
781 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
783 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
786 else if( !strncmp( psz_parser, "io=", strlen( "io=" ) ) )
788 psz_parser += strlen( "io=" );
789 if( !strncmp( psz_parser, "read", strlen( "read" ) ) )
791 p_sys->io = IO_METHOD_READ;
792 psz_parser += strlen( "read" );
794 else if( !strncmp( psz_parser, "mmap", strlen( "mmap" ) ) )
796 p_sys->io = IO_METHOD_MMAP;
797 psz_parser += strlen( "mmap" );
799 else if( !strncmp( psz_parser, "userptr", strlen( "userptr" ) ) )
801 p_sys->io = IO_METHOD_USERPTR;
802 psz_parser += strlen( "userptr" );
806 p_sys->io = strtol( psz_parser, &psz_parser, 0 );
809 else if( !strncmp( psz_parser, "width=",
810 strlen( "width=" ) ) )
813 strtol( psz_parser + strlen( "width=" ),
816 else if( !strncmp( psz_parser, "height=",
817 strlen( "height=" ) ) )
820 strtol( psz_parser + strlen( "height=" ),
823 else if( !strncmp( psz_parser, "controls-reset",
824 strlen( "controls-reset" ) ) )
826 var_SetBool( p_obj, "v4l2-controls-reset", VLC_TRUE );
827 psz_parser += strlen( "controls-reset" );
830 else if( !strncmp( psz_parser, "brightness=",
831 strlen( "brightness=" ) ) )
833 var_SetInteger( p_obj, "brightness",
834 strtol( psz_parser + strlen( "brightness=" ),
837 else if( !strncmp( psz_parser, "contrast=",
838 strlen( "contrast=" ) ) )
840 var_SetInteger( p_obj, "contrast",
841 strtol( psz_parser + strlen( "contrast=" ),
844 else if( !strncmp( psz_parser, "saturation=",
845 strlen( "saturation=" ) ) )
847 var_SetInteger( p_obj, "saturation",
848 strtol( psz_parser + strlen( "saturation=" ),
851 else if( !strncmp( psz_parser, "hue=",
854 var_SetInteger( p_obj, "hue",
855 strtol( psz_parser + strlen( "hue=" ),
858 else if( !strncmp( psz_parser, "gamma=",
859 strlen( "gamma=" ) ) )
861 var_SetInteger( p_obj, "gamma",
862 strtol( psz_parser + strlen( "gamma=" ),
866 else if( !strncmp( psz_parser, "samplerate=",
867 strlen( "samplerate=" ) ) )
869 p_sys->i_sample_rate =
870 strtol( psz_parser + strlen( "samplerate=" ),
873 else if( !strncmp( psz_parser, "audio-method", strlen( "audio-method" ) ) )
875 p_sys->i_audio_method =
876 strtol( psz_parser + strlen( "audio-method" ),
879 else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
881 psz_parser += strlen( "stereo" );
882 p_sys->b_stereo = VLC_TRUE;
884 else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
886 psz_parser += strlen( "mono" );
887 p_sys->b_stereo = VLC_FALSE;
889 else if( !strncmp( psz_parser, "caching=", strlen( "caching=" ) ) )
891 p_sys->i_pts = strtol( psz_parser + strlen( "caching=" ),
896 msg_Warn( p_obj, "unknown option" );
899 while( *psz_parser && *psz_parser != ':' )
904 if( *psz_parser == '\0' )
914 p_sys->psz_device = strdup( psz_dup );
916 if( psz_dup ) free( psz_dup );
919 /*****************************************************************************
920 * Close: close device, free resources
921 *****************************************************************************/
922 static void AccessClose( vlc_object_t *p_this )
924 access_t *p_access = (access_t *)p_this;
925 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
927 CommonClose( p_this, p_sys );
930 static void DemuxClose( vlc_object_t *p_this )
932 struct v4l2_buffer buf;
933 enum v4l2_buf_type buf_type;
936 demux_t *p_demux = (demux_t *)p_this;
937 demux_sys_t *p_sys = p_demux->p_sys;
939 /* Stop video capture */
940 if( p_sys->i_fd_video >= 0 )
949 case IO_METHOD_USERPTR:
950 /* Some drivers 'hang' internally if this is not done before streamoff */
951 for( unsigned int i = 0; i < p_sys->i_nbuffers; i++ )
953 memset( &buf, 0, sizeof(buf) );
954 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
955 buf.memory = ( p_sys->io == IO_METHOD_USERPTR ) ?
956 V4L2_MEMORY_USERPTR : V4L2_MEMORY_MMAP;
957 ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ); /* ignore result */
960 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
961 if( ioctl( p_sys->i_fd_video, VIDIOC_STREAMOFF, &buf_type ) < 0 ) {
962 msg_Err( p_this, "VIDIOC_STREAMOFF failed" );
969 /* Free Video Buffers */
970 if( p_sys->p_buffers ) {
974 free( p_sys->p_buffers[0].start );
978 for( i = 0; i < p_sys->i_nbuffers; ++i )
980 if( munmap( p_sys->p_buffers[i].start, p_sys->p_buffers[i].length ) )
982 msg_Err( p_this, "munmap failed" );
987 case IO_METHOD_USERPTR:
988 for( i = 0; i < p_sys->i_nbuffers; ++i )
990 free( p_sys->p_buffers[i].orig_userp );
994 free( p_sys->p_buffers );
997 CommonClose( p_this, p_sys );
1000 static void CommonClose( vlc_object_t *p_this, demux_sys_t *p_sys )
1003 if( p_sys->i_fd_video >= 0 ) close( p_sys->i_fd_video );
1005 if( p_sys->p_alsa_pcm )
1007 snd_pcm_close( p_sys->p_alsa_pcm );
1008 p_sys->i_fd_audio = -1;
1011 if( p_sys->i_fd_audio >= 0 ) close( p_sys->i_fd_audio );
1013 if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
1014 free( p_sys->psz_device );
1015 free( p_sys->psz_vdev );
1016 free( p_sys->psz_adev );
1017 free( p_sys->p_standards );
1018 free( p_sys->p_inputs );
1019 free( p_sys->p_tuners );
1020 free( p_sys->p_codecs );
1021 free( p_sys->psz_requested_chroma );
1026 /*****************************************************************************
1027 * AccessOpen: opens v4l2 device, access2 callback
1028 *****************************************************************************
1030 * url: <video device>::::
1032 *****************************************************************************/
1033 static int AccessOpen( vlc_object_t * p_this )
1035 access_t *p_access = (access_t*) p_this;
1036 demux_sys_t * p_sys;
1038 /* Only when selected */
1039 if( *p_access->psz_access == '\0' ) return VLC_EGENERIC;
1041 p_access->pf_read = AccessRead;
1042 p_access->pf_block = NULL;
1043 p_access->pf_seek = NULL;
1044 p_access->pf_control = AccessControl;
1045 p_access->info.i_update = 0;
1046 p_access->info.i_size = 0;
1047 p_access->info.i_pos = 0;
1048 p_access->info.b_eof = VLC_FALSE;
1049 p_access->info.i_title = 0;
1050 p_access->info.i_seekpoint = 0;
1052 p_sys = calloc( 1, sizeof( demux_sys_t ) );
1053 p_access->p_sys = (access_sys_t *) p_sys;
1054 if( p_sys == NULL ) return VLC_ENOMEM;
1056 GetV4L2Params( p_sys, (vlc_object_t *) p_access );
1058 ParseMRL( p_sys, p_access->psz_path, (vlc_object_t *) p_access );
1060 if( FindMainDevice( p_this, p_sys, FIND_VIDEO,
1061 VLC_FALSE, !strcmp( p_access->psz_access, "v4l2" ) ) != VLC_SUCCESS )
1063 AccessClose( p_this );
1064 return VLC_EGENERIC;
1070 /*****************************************************************************
1072 *****************************************************************************/
1073 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
1075 demux_sys_t *p_sys = p_demux->p_sys;
1081 /* Special for access_demux */
1082 case DEMUX_CAN_PAUSE:
1083 case DEMUX_CAN_SEEK:
1084 case DEMUX_SET_PAUSE_STATE:
1085 case DEMUX_CAN_CONTROL_PACE:
1086 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1090 case DEMUX_GET_PTS_DELAY:
1091 pi64 = (int64_t*)va_arg( args, int64_t * );
1092 *pi64 = (int64_t)p_sys->i_pts * 1000;
1095 case DEMUX_GET_TIME:
1096 pi64 = (int64_t*)va_arg( args, int64_t * );
1100 /* TODO implement others */
1102 return VLC_EGENERIC;
1105 return VLC_EGENERIC;
1108 /*****************************************************************************
1109 * AccessControl: access2 callback
1110 *****************************************************************************/
1111 static int AccessControl( access_t *p_access, int i_query, va_list args )
1113 vlc_bool_t *pb_bool;
1116 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1121 case ACCESS_CAN_SEEK:
1122 case ACCESS_CAN_FASTSEEK:
1123 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1124 *pb_bool = VLC_FALSE;
1126 case ACCESS_CAN_PAUSE:
1127 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1128 *pb_bool = VLC_FALSE;
1130 case ACCESS_CAN_CONTROL_PACE:
1131 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1132 *pb_bool = VLC_FALSE;
1136 case ACCESS_GET_MTU:
1137 pi_int = (int*)va_arg( args, int * );
1141 case ACCESS_GET_PTS_DELAY:
1142 pi_64 = (int64_t*)va_arg( args, int64_t * );
1143 *pi_64 = (int64_t) p_sys->i_pts * 1000;
1147 case ACCESS_SET_PAUSE_STATE:
1151 case ACCESS_GET_TITLE_INFO:
1152 case ACCESS_SET_TITLE:
1153 case ACCESS_SET_SEEKPOINT:
1154 case ACCESS_SET_PRIVATE_ID_STATE:
1155 case ACCESS_GET_CONTENT_TYPE:
1156 case ACCESS_GET_META:
1157 return VLC_EGENERIC;
1160 msg_Warn( p_access, "Unimplemented query in control(%d).", i_query);
1161 return VLC_EGENERIC;
1167 /*****************************************************************************
1168 * AccessRead: access2 callback
1169 ******************************************************************************/
1170 static ssize_t AccessRead( access_t * p_access, uint8_t * p_buffer, size_t i_len )
1172 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1176 ufd.fd = p_sys->i_fd_video;
1177 ufd.events = POLLIN;
1179 if( p_access->info.b_eof )
1184 if( p_access->b_die )
1189 while( ( i_ret = poll( &ufd, 1, 500 ) ) == 0 );
1193 msg_Err( p_access, "Polling error (%m)." );
1197 i_ret = read( p_sys->i_fd_video, p_buffer, i_len );
1200 p_access->info.b_eof = VLC_TRUE;
1202 else if( i_ret > 0 )
1204 p_access->info.i_pos += i_ret;
1210 /*****************************************************************************
1211 * Demux: Processes the audio or video frame
1212 *****************************************************************************/
1213 static int Demux( demux_t *p_demux )
1215 demux_sys_t *p_sys = p_demux->p_sys;
1216 es_out_id_t *p_es = p_sys->p_es_audio;
1217 block_t *p_block = NULL;
1219 /* Try grabbing audio frames first */
1220 if( p_sys->i_fd_audio < 0 || !( p_block = GrabAudio( p_demux ) ) )
1222 /* Try grabbing video frame */
1223 p_es = p_sys->p_es_video;
1224 if( p_sys->i_fd_video > 0 ) p_block = GrabVideo( p_demux );
1229 /* Sleep so we do not consume all the cpu, 10ms seems
1230 * like a good value (100fps) */
1235 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
1236 es_out_Send( p_demux->out, p_es, p_block );
1241 /*****************************************************************************
1242 * GrabVideo: Grab a video frame
1243 *****************************************************************************/
1244 static block_t* GrabVideo( demux_t *p_demux )
1246 demux_sys_t *p_sys = p_demux->p_sys;
1248 block_t *p_block = NULL;
1249 struct v4l2_buffer buf;
1252 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1254 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1256 /* Did we wait long enough ? (frame rate reduction) */
1257 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1260 /* Grab Video Frame */
1263 case IO_METHOD_READ:
1264 i_ret = read( p_sys->i_fd_video, p_sys->p_buffers[0].start, p_sys->p_buffers[0].length );
1272 /* Could ignore EIO, see spec. */
1275 msg_Err( p_demux, "Failed to read frame" );
1280 p_block = ProcessVideoFrame( p_demux, (uint8_t*)p_sys->p_buffers[0].start, i_ret );
1281 if( !p_block ) return 0;
1285 case IO_METHOD_MMAP:
1286 memset( &buf, 0, sizeof(buf) );
1287 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1288 buf.memory = V4L2_MEMORY_MMAP;
1290 /* Wait for next frame */
1291 if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
1298 /* Could ignore EIO, see spec. */
1301 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1306 if( buf.index >= p_sys->i_nbuffers ) {
1307 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1311 p_block = ProcessVideoFrame( p_demux, p_sys->p_buffers[buf.index].start, buf.bytesused );
1312 if( !p_block ) return 0;
1315 if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
1317 msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
1323 case IO_METHOD_USERPTR:
1324 memset( &buf, 0, sizeof(buf) );
1325 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1326 buf.memory = V4L2_MEMORY_USERPTR;
1328 /* Wait for next frame */
1329 if (ioctl( p_sys->i_fd_video, VIDIOC_DQBUF, &buf ) < 0 )
1336 /* Could ignore EIO, see spec. */
1339 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1346 for( i = 0; i < p_sys->i_nbuffers; i++ )
1348 if( buf.m.userptr == (unsigned long)p_sys->p_buffers[i].start &&
1349 buf.length == p_sys->p_buffers[i].length ) break;
1352 if( i >= p_sys->i_nbuffers )
1354 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1358 p_block = ProcessVideoFrame( p_demux, (uint8_t*)buf.m.userptr, buf.bytesused );
1359 if( !p_block ) return 0;
1362 if( ioctl( p_sys->i_fd_video, VIDIOC_QBUF, &buf ) < 0 )
1364 msg_Err (p_demux, "Failed to unlock (VIDIOC_QBUF)");
1373 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();
1378 /*****************************************************************************
1379 * ProcessVideoFrame: Helper function to take a buffer and copy it into
1381 *****************************************************************************/
1382 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t i_size )
1386 if( !p_frame ) return 0;
1389 if( !( p_block = block_New( p_demux, i_size ) ) )
1391 msg_Warn( p_demux, "Cannot get new block" );
1396 memcpy( p_block->p_buffer, p_frame, i_size );
1401 /*****************************************************************************
1402 * GrabAudio: Grab an audio frame
1403 *****************************************************************************/
1404 static block_t* GrabAudio( demux_t *p_demux )
1406 demux_sys_t *p_sys = p_demux->p_sys;
1407 struct audio_buf_info buf_info;
1408 int i_read = 0, i_correct;
1411 if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
1412 else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
1416 msg_Warn( p_demux, "cannot get block" );
1420 p_sys->p_block_audio = p_block;
1423 if( p_sys->i_audio_method & AUDIO_METHOD_ALSA )
1426 i_read = snd_pcm_readi( p_sys->p_alsa_pcm, p_block->p_buffer, p_sys->i_alsa_chunk_size );
1436 snd_pcm_prepare( p_sys->p_alsa_pcm );
1440 i_resume = snd_pcm_resume( p_sys->p_alsa_pcm );
1441 if( i_resume < 0 && i_resume != -EAGAIN ) snd_pcm_prepare( p_sys->p_alsa_pcm );
1444 msg_Err( p_demux, "Failed to read alsa frame (%s)", snd_strerror( i_read ) );
1450 /* convert from frames to bytes */
1451 i_read *= p_sys->i_alsa_frame_size;
1456 if( p_sys->i_audio_method & AUDIO_METHOD_OSS )
1459 i_read = read( p_sys->i_fd_audio, p_block->p_buffer,
1460 p_sys->i_audio_max_frame_size );
1463 if( i_read <= 0 ) return 0;
1465 p_block->i_buffer = i_read;
1466 p_sys->p_block_audio = 0;
1468 /* Correct the date because of kernel buffering */
1470 if( p_sys->i_audio_method & AUDIO_METHOD_OSS )
1473 if( ioctl( p_sys->i_fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
1475 i_correct += buf_info.bytes;
1479 else if( p_sys->i_audio_method & AUDIO_METHOD_ALSA )
1483 snd_pcm_sframes_t delay = 0;
1484 if( ( i_err = snd_pcm_delay( p_sys->p_alsa_pcm, &delay ) ) >= 0 )
1486 size_t i_correction_delta = delay * p_sys->i_alsa_frame_size;
1487 /* Test for overrun */
1488 if( i_correction_delta > p_sys->i_audio_max_frame_size )
1490 msg_Warn( p_demux, "ALSA read overrun (%d > %d)",
1491 i_correction_delta, p_sys->i_audio_max_frame_size );
1492 i_correction_delta = p_sys->i_audio_max_frame_size;
1493 snd_pcm_prepare( p_sys->p_alsa_pcm );
1495 i_correct += i_correction_delta;
1499 /* delay failed so reset */
1500 msg_Warn( p_demux, "ALSA snd_pcm_delay failed (%s)", snd_strerror( i_err ) );
1501 snd_pcm_prepare( p_sys->p_alsa_pcm );
1507 p_block->i_pts = p_block->i_dts =
1508 mdate() - I64C(1000000) * (mtime_t)i_correct /
1509 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
1514 /*****************************************************************************
1515 * Helper function to initalise video IO using the Read method
1516 *****************************************************************************/
1517 static int InitRead( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1519 demux_sys_t *p_sys = p_demux->p_sys;
1521 p_sys->p_buffers = calloc( 1, sizeof( *p_sys->p_buffers ) );
1522 if( !p_sys->p_buffers )
1524 msg_Err( p_demux, "Out of memory" );
1528 p_sys->p_buffers[0].length = i_buffer_size;
1529 p_sys->p_buffers[0].start = malloc( i_buffer_size );
1530 if( !p_sys->p_buffers[0].start )
1532 msg_Err( p_demux, "Out of memory" );
1539 return VLC_EGENERIC;
1543 /*****************************************************************************
1544 * Helper function to initalise video IO using the mmap method
1545 *****************************************************************************/
1546 static int InitMmap( demux_t *p_demux, int i_fd )
1548 demux_sys_t *p_sys = p_demux->p_sys;
1549 struct v4l2_requestbuffers req;
1551 memset( &req, 0, sizeof(req) );
1553 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1554 req.memory = V4L2_MEMORY_MMAP;
1556 if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1558 msg_Err( p_demux, "device does not support mmap i/o" );
1564 msg_Err( p_demux, "Insufficient buffer memory" );
1568 p_sys->p_buffers = calloc( req.count, sizeof( *p_sys->p_buffers ) );
1569 if( !p_sys->p_buffers )
1571 msg_Err( p_demux, "Out of memory" );
1575 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < req.count; ++p_sys->i_nbuffers )
1577 struct v4l2_buffer buf;
1579 memset( &buf, 0, sizeof(buf) );
1580 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1581 buf.memory = V4L2_MEMORY_MMAP;
1582 buf.index = p_sys->i_nbuffers;
1584 if( ioctl( i_fd, VIDIOC_QUERYBUF, &buf ) < 0 )
1586 msg_Err( p_demux, "VIDIOC_QUERYBUF" );
1590 p_sys->p_buffers[p_sys->i_nbuffers].length = buf.length;
1591 p_sys->p_buffers[p_sys->i_nbuffers].start =
1592 mmap( NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, buf.m.offset );
1594 if( p_sys->p_buffers[p_sys->i_nbuffers].start == MAP_FAILED )
1596 msg_Err( p_demux, "mmap failed (%m)" );
1604 return VLC_EGENERIC;
1608 /*****************************************************************************
1609 * Helper function to initalise video IO using the userbuf method
1610 *****************************************************************************/
1611 static int InitUserP( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1613 demux_sys_t *p_sys = p_demux->p_sys;
1614 struct v4l2_requestbuffers req;
1615 unsigned int i_page_size;
1617 i_page_size = getpagesize();
1618 i_buffer_size = ( i_buffer_size + i_page_size - 1 ) & ~( i_page_size - 1);
1620 memset( &req, 0, sizeof(req) );
1622 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1623 req.memory = V4L2_MEMORY_USERPTR;
1625 if( ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1627 msg_Err( p_demux, "device does not support user pointer i/o" );
1631 p_sys->p_buffers = calloc( 4, sizeof( *p_sys->p_buffers ) );
1632 if( !p_sys->p_buffers )
1634 msg_Err( p_demux, "Out of memory" );
1638 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < 4; ++p_sys->i_nbuffers )
1640 p_sys->p_buffers[p_sys->i_nbuffers].length = i_buffer_size;
1641 p_sys->p_buffers[p_sys->i_nbuffers].start =
1642 vlc_memalign( &p_sys->p_buffers[p_sys->i_nbuffers].orig_userp,
1643 /* boundary */ i_page_size, i_buffer_size );
1645 if( !p_sys->p_buffers[p_sys->i_nbuffers].start )
1647 msg_Err( p_demux, "out of memory" );
1655 return VLC_EGENERIC;
1659 /*****************************************************************************
1660 * IsPixelFormatSupported: returns true if the specified V4L2 pixel format is
1661 * in the array of supported formats returned by the driver
1662 *****************************************************************************/
1663 static vlc_bool_t IsPixelFormatSupported( demux_t *p_demux, unsigned int i_pixelformat )
1665 demux_sys_t *p_sys = p_demux->p_sys;
1667 for( int i_index = 0; i_index < p_sys->i_codec; i_index++ )
1669 if( p_sys->p_codecs[i_index].pixelformat == i_pixelformat )
1676 /*****************************************************************************
1677 * OpenVideoDev: open and set up the video device and probe for capabilities
1678 *****************************************************************************/
1679 static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, vlc_bool_t b_demux )
1682 struct v4l2_cropcap cropcap;
1683 struct v4l2_crop crop;
1684 struct v4l2_format fmt;
1686 enum v4l2_buf_type buf_type;
1687 char *psz_device = p_sys->psz_vdev;
1689 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1691 msg_Err( p_obj, "cannot open device (%m)" );
1695 /* Select standard */
1697 if( p_sys->i_selected_standard_id != V4L2_STD_UNKNOWN )
1699 if( ioctl( i_fd, VIDIOC_S_STD, &p_sys->i_selected_standard_id ) < 0 )
1701 msg_Err( p_obj, "cannot set standard (%m)" );
1704 msg_Dbg( p_obj, "Set standard" );
1709 if( p_sys->i_selected_input > p_sys->i_input )
1711 msg_Warn( p_obj, "invalid input. Using the default one" );
1712 p_sys->i_selected_input = 0;
1715 if( ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
1717 msg_Err( p_obj, "cannot set input (%m)" );
1721 /* TODO: Move the resolution stuff up here */
1722 /* if MPEG encoder card, no need to do anything else after this */
1723 ControlList( p_obj, i_fd,
1724 var_GetBool( p_obj, "v4l2-controls-reset" ), b_demux );
1725 if( VLC_FALSE == b_demux)
1730 demux_t *p_demux = (demux_t *) p_obj;
1732 /* Verify device support for the various IO methods */
1735 case IO_METHOD_READ:
1736 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE) )
1738 msg_Err( p_demux, "device does not support read i/o" );
1743 case IO_METHOD_MMAP:
1744 case IO_METHOD_USERPTR:
1745 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING) )
1747 msg_Err( p_demux, "device does not support streaming i/o" );
1753 msg_Err( p_demux, "io method not supported" );
1757 /* Reset Cropping */
1758 memset( &cropcap, 0, sizeof(cropcap) );
1759 cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1760 if( ioctl( i_fd, VIDIOC_CROPCAP, &cropcap ) >= 0 )
1762 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1763 crop.c = cropcap.defrect; /* reset to default */
1764 if( ioctl( i_fd, VIDIOC_S_CROP, &crop ) < 0 )
1769 /* Cropping not supported. */
1772 /* Errors ignored. */
1778 /* Try and find default resolution if not specified */
1779 memset( &fmt, 0, sizeof(fmt) );
1780 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1782 if( p_sys->i_width <= 0 || p_sys->i_height <= 0 )
1784 if( ioctl( i_fd, VIDIOC_G_FMT, &fmt ) < 0 )
1786 msg_Err( p_demux, "Cannot get default width and height." );
1790 p_sys->i_width = fmt.fmt.pix.width;
1791 p_sys->i_height = fmt.fmt.pix.height;
1793 if( fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE )
1795 p_sys->i_height = p_sys->i_height * 2;
1800 msg_Dbg( p_demux, "trying specified size %dx%d", p_sys->i_width, p_sys->i_height );
1803 fmt.fmt.pix.width = p_sys->i_width;
1804 fmt.fmt.pix.height = p_sys->i_height;
1805 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
1807 /* Test and set Chroma */
1808 fmt.fmt.pix.pixelformat = 0;
1809 if( p_sys->psz_requested_chroma && strlen( p_sys->psz_requested_chroma ) > 0 )
1811 /* User specified chroma */
1812 if( strlen( p_sys->psz_requested_chroma ) >= 4 )
1814 int i_requested_fourcc = VLC_FOURCC(
1815 p_sys->psz_requested_chroma[0], p_sys->psz_requested_chroma[1],
1816 p_sys->psz_requested_chroma[2], p_sys->psz_requested_chroma[3] );
1817 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
1819 if( v4l2chroma_to_fourcc[i].i_fourcc == i_requested_fourcc )
1821 fmt.fmt.pix.pixelformat = v4l2chroma_to_fourcc[i].i_v4l2;
1826 /* Try and set user chroma */
1827 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ( fmt.fmt.pix.pixelformat && ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 ) )
1829 msg_Warn( p_demux, "Driver is unable to use specified chroma %s. Trying defaults.", p_sys->psz_requested_chroma );
1830 fmt.fmt.pix.pixelformat = 0;
1834 /* If no user specified chroma, find best */
1835 /* This also decides if MPEG encoder card or not */
1836 if( !fmt.fmt.pix.pixelformat )
1839 for( i = 0; i < ARRAY_SIZE( p_chroma_fallbacks ); i++ )
1841 fmt.fmt.pix.pixelformat = p_chroma_fallbacks[i];
1842 if( IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat )
1843 && ioctl( i_fd, VIDIOC_S_FMT, &fmt ) >= 0 )
1846 if( i == ARRAY_SIZE( p_chroma_fallbacks ) )
1848 msg_Warn( p_demux, "Could not select any of the default chromas; attempting to open as MPEG encoder card (access2)" );
1853 /* Reassign width, height and chroma incase driver override */
1854 p_sys->i_width = fmt.fmt.pix.width;
1855 p_sys->i_height = fmt.fmt.pix.height;
1857 /* Look up final fourcc */
1858 p_sys->i_fourcc = 0;
1859 for( int i = 0; v4l2chroma_to_fourcc[i].i_fourcc != 0; i++ )
1861 if( v4l2chroma_to_fourcc[i].i_v4l2 == fmt.fmt.pix.pixelformat )
1863 p_sys->i_fourcc = v4l2chroma_to_fourcc[i].i_fourcc;
1868 /* Buggy driver paranoia */
1869 i_min = fmt.fmt.pix.width * 2;
1870 if( fmt.fmt.pix.bytesperline < i_min )
1871 fmt.fmt.pix.bytesperline = i_min;
1872 i_min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
1873 if( fmt.fmt.pix.sizeimage < i_min )
1874 fmt.fmt.pix.sizeimage = i_min;
1876 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1877 /* This is new in Linux 2.6.19 */
1878 /* List supported frame rates */
1879 struct v4l2_frmivalenum frmival;
1881 frmival.pixel_format = fmt.fmt.pix.pixelformat;
1882 frmival.width = p_sys->i_width;
1883 frmival.height = p_sys->i_height;
1884 if( ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 )
1887 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
1888 vlc_fourcc_to_char( p_sys->i_fourcc, &psz_fourcc );
1889 msg_Dbg( p_demux, "supported frame intervals for %4s, %dx%d:",
1890 psz_fourcc, frmival.width, frmival.height );
1891 switch( frmival.type )
1893 case V4L2_FRMIVAL_TYPE_DISCRETE:
1896 msg_Dbg( p_demux, " supported frame interval: %d/%d",
1897 frmival.discrete.numerator,
1898 frmival.discrete.denominator );
1900 } while( ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 );
1902 case V4L2_FRMIVAL_TYPE_STEPWISE:
1903 msg_Dbg( p_demux, " supported frame intervals: %d/%d to "
1904 "%d/%d using %d/%d increments",
1905 frmival.stepwise.min.numerator,
1906 frmival.stepwise.min.denominator,
1907 frmival.stepwise.max.numerator,
1908 frmival.stepwise.max.denominator,
1909 frmival.stepwise.step.numerator,
1910 frmival.stepwise.step.denominator );
1912 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
1913 msg_Dbg( p_demux, " supported frame intervals: %d/%d to %d/%d",
1914 frmival.stepwise.min.numerator,
1915 frmival.stepwise.min.denominator,
1916 frmival.stepwise.max.numerator,
1917 frmival.stepwise.max.denominator );
1923 /* Init IO method */
1926 case IO_METHOD_READ:
1927 if( InitRead( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
1930 case IO_METHOD_MMAP:
1931 if( InitMmap( p_demux, i_fd ) != VLC_SUCCESS ) goto open_failed;
1934 case IO_METHOD_USERPTR:
1935 if( InitUserP( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
1942 es_format_Init( &es_fmt, VIDEO_ES, p_sys->i_fourcc );
1943 es_fmt.video.i_width = p_sys->i_width;
1944 es_fmt.video.i_height = p_sys->i_height;
1945 es_fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
1947 /* Setup rgb mask for RGB formats */
1948 if( p_sys->i_fourcc == VLC_FOURCC( 'R','V','2','4' ) )
1950 /* This is in BGR format */
1951 es_fmt.video.i_bmask = 0x00ff0000;
1952 es_fmt.video.i_gmask = 0x0000ff00;
1953 es_fmt.video.i_rmask = 0x000000ff;
1956 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
1957 (char*)&es_fmt.i_codec, es_fmt.video.i_width, es_fmt.video.i_height );
1958 p_sys->p_es_video = es_out_Add( p_demux->out, &es_fmt );
1964 case IO_METHOD_READ:
1968 case IO_METHOD_MMAP:
1969 for (unsigned int i = 0; i < p_sys->i_nbuffers; ++i)
1971 struct v4l2_buffer buf;
1973 memset( &buf, 0, sizeof(buf) );
1974 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1975 buf.memory = V4L2_MEMORY_MMAP;
1978 if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
1980 msg_Err( p_demux, "VIDIOC_QBUF failed" );
1985 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1986 if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
1988 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
1994 case IO_METHOD_USERPTR:
1995 for( unsigned int i = 0; i < p_sys->i_nbuffers; ++i )
1997 struct v4l2_buffer buf;
1999 memset( &buf, 0, sizeof(buf) );
2000 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2001 buf.memory = V4L2_MEMORY_USERPTR;
2003 buf.m.userptr = (unsigned long)p_sys->p_buffers[i].start;
2004 buf.length = p_sys->p_buffers[i].length;
2006 if( ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
2008 msg_Err( p_demux, "VIDIOC_QBUF failed" );
2013 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2014 if( ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
2016 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
2024 if( p_sys->f_fps >= 0.1 )
2026 msg_Dbg( p_demux, "User set fps=%f", p_sys->f_fps );
2032 if( i_fd >= 0 ) close( i_fd );
2038 /*****************************************************************************
2039 * ResolveALSADeviceName: Change any . to : in the ALSA device name
2040 *****************************************************************************/
2041 static char *ResolveALSADeviceName( const char *psz_device )
2043 char* psz_alsa_name = strdup( psz_device );
2044 for( unsigned int i = 0; i < strlen( psz_device ); i++ )
2046 if( psz_alsa_name[i] == '.' ) psz_alsa_name[i] = ':';
2048 return psz_alsa_name;
2052 /*****************************************************************************
2053 * OpenAudioDev: open and set up the audio device and probe for capabilities
2054 *****************************************************************************/
2056 static int OpenAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys,
2057 vlc_bool_t b_demux )
2059 char *psz_device = p_sys->psz_adev;
2061 p_sys->p_alsa_pcm = NULL;
2062 char* psz_alsa_device_name = NULL;
2063 snd_pcm_hw_params_t *p_hw_params = NULL;
2064 snd_pcm_uframes_t buffer_size;
2065 snd_pcm_uframes_t chunk_size;
2069 psz_alsa_device_name =
2070 ResolveALSADeviceName( psz_device?: ALSA_DEFAULT );
2072 if( ( i_err = snd_pcm_open( &p_sys->p_alsa_pcm, psz_alsa_device_name,
2073 SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0)
2075 msg_Err( p_this, "Cannot open ALSA audio device %s (%s)",
2076 psz_alsa_device_name, snd_strerror( i_err ) );
2080 if( ( i_err = snd_pcm_nonblock( p_sys->p_alsa_pcm, 1 ) ) < 0)
2082 msg_Err( p_this, "Cannot set ALSA nonblock (%s)",
2083 snd_strerror( i_err ) );
2087 /* Begin setting hardware parameters */
2089 if( ( i_err = snd_pcm_hw_params_malloc( &p_hw_params ) ) < 0 )
2092 "ALSA: cannot allocate hardware parameter structure (%s)",
2093 snd_strerror( i_err ) );
2097 if( ( i_err = snd_pcm_hw_params_any( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
2100 "ALSA: cannot initialize hardware parameter structure (%s)",
2101 snd_strerror( i_err ) );
2105 /* Set Interleaved access */
2106 if( ( i_err = snd_pcm_hw_params_set_access( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 )
2108 msg_Err( p_this, "ALSA: cannot set access type (%s)",
2109 snd_strerror( i_err ) );
2113 /* Set 16 bit little endian */
2114 if( ( i_err = snd_pcm_hw_params_set_format( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_FORMAT_S16_LE ) ) < 0 )
2116 msg_Err( p_this, "ALSA: cannot set sample format (%s)",
2117 snd_strerror( i_err ) );
2121 /* Set sample rate */
2122 #ifdef HAVE_ALSA_NEW_API
2123 i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, &p_sys->i_sample_rate, NULL );
2125 i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, p_sys->i_sample_rate, NULL );
2129 msg_Err( p_this, "ALSA: cannot set sample rate (%s)",
2130 snd_strerror( i_err ) );
2135 unsigned int channels = p_sys->b_stereo ? 2 : 1;
2136 if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
2138 channels = ( channels==1 ) ? 2 : 1;
2139 msg_Warn( p_this, "ALSA: cannot set channel count (%s). "
2140 "Trying with channels=%d",
2141 snd_strerror( i_err ),
2143 if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
2145 msg_Err( p_this, "ALSA: cannot set channel count (%s)",
2146 snd_strerror( i_err ) );
2149 p_sys->b_stereo = ( channels == 2 );
2152 /* Set metrics for buffer calculations later */
2153 unsigned int buffer_time;
2154 if( ( i_err = snd_pcm_hw_params_get_buffer_time_max(p_hw_params, &buffer_time, 0) ) < 0 )
2156 msg_Err( p_this, "ALSA: cannot get buffer time max (%s)",
2157 snd_strerror( i_err ) );
2160 if (buffer_time > 500000) buffer_time = 500000;
2162 /* Set period time */
2163 unsigned int period_time = buffer_time / 4;
2164 #ifdef HAVE_ALSA_NEW_API
2165 i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, &period_time, 0 );
2167 i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, period_time, 0 );
2171 msg_Err( p_this, "ALSA: cannot set period time (%s)",
2172 snd_strerror( i_err ) );
2176 /* Set buffer time */
2177 #ifdef HAVE_ALSA_NEW_API
2178 i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, &buffer_time, 0 );
2180 i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, buffer_time, 0 );
2184 msg_Err( p_this, "ALSA: cannot set buffer time (%s)",
2185 snd_strerror( i_err ) );
2189 /* Apply new hardware parameters */
2190 if( ( i_err = snd_pcm_hw_params( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
2192 msg_Err( p_this, "ALSA: cannot set hw parameters (%s)",
2193 snd_strerror( i_err ) );
2197 /* Get various buffer metrics */
2198 snd_pcm_hw_params_get_period_size( p_hw_params, &chunk_size, 0 );
2199 snd_pcm_hw_params_get_buffer_size( p_hw_params, &buffer_size );
2200 if( chunk_size == buffer_size )
2203 "ALSA: period cannot equal buffer size (%lu == %lu)",
2204 chunk_size, buffer_size);
2208 int bits_per_sample = snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE);
2209 int bits_per_frame = bits_per_sample * channels;
2211 p_sys->i_alsa_chunk_size = chunk_size;
2212 p_sys->i_alsa_frame_size = bits_per_frame / 8;
2213 p_sys->i_audio_max_frame_size = chunk_size * bits_per_frame / 8;
2215 snd_pcm_hw_params_free( p_hw_params );
2219 if( ( i_err = snd_pcm_prepare( p_sys->p_alsa_pcm ) ) < 0 )
2222 "ALSA: cannot prepare audio interface for use (%s)",
2223 snd_strerror( i_err ) );
2227 /* Return a fake handle so other tests work */
2230 free( psz_alsa_device_name );
2232 if( !p_sys->psz_adev )
2233 p_sys->psz_adev = strdup( ALSA_DEFAULT );
2238 if( i_fd >= 0 ) close( i_fd );
2240 if( p_hw_params ) snd_pcm_hw_params_free( p_hw_params );
2241 if( p_sys->p_alsa_pcm ) snd_pcm_close( p_sys->p_alsa_pcm );
2242 free( psz_alsa_device_name );
2249 static int OpenAudioDevOss( vlc_object_t *p_this, demux_sys_t *p_sys,
2250 vlc_bool_t b_demux )
2252 char *psz_device = p_sys->psz_adev;
2256 if( !psz_device ) psz_device = strdup( OSS_DEFAULT ); /* FIXME leak */
2258 if( (i_fd = open( psz_device, O_RDONLY | O_NONBLOCK )) < 0 )
2260 msg_Err( p_this, "cannot open OSS audio device (%m)" );
2264 i_format = AFMT_S16_LE;
2265 if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
2266 || i_format != AFMT_S16_LE )
2269 "cannot set audio format (16b little endian) (%m)" );
2273 if( ioctl( i_fd, SNDCTL_DSP_STEREO,
2274 &p_sys->b_stereo ) < 0 )
2276 msg_Err( p_this, "cannot set audio channels count (%m)" );
2280 if( ioctl( i_fd, SNDCTL_DSP_SPEED,
2281 &p_sys->i_sample_rate ) < 0 )
2283 msg_Err( p_this, "cannot set audio sample rate (%m)" );
2287 p_sys->i_audio_max_frame_size = 6 * 1024;
2289 if( !p_sys->psz_adev )
2290 p_sys->psz_adev = strdup( OSS_DEFAULT );
2295 if( i_fd >= 0 ) close( i_fd );
2300 static int OpenAudioDev( vlc_object_t *p_this, demux_sys_t *p_sys,
2301 vlc_bool_t b_demux )
2307 if( ( p_sys->i_audio_method & AUDIO_METHOD_ALSA ) && i_fd < 0 )
2308 i_fd = OpenAudioDevAlsa( p_this, p_sys, b_demux );
2311 if( ( p_sys->i_audio_method & AUDIO_METHOD_OSS ) && i_fd < 0 )
2312 i_fd = OpenAudioDevOss( p_this, p_sys, b_demux );
2317 psz_device = p_sys->psz_adev;
2319 msg_Dbg( p_this, "opened adev=`%s' %s %dHz",
2320 psz_device, p_sys->b_stereo ? "stereo" : "mono",
2321 p_sys->i_sample_rate );
2324 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
2326 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
2327 fmt.audio.i_rate = p_sys->i_sample_rate;
2328 fmt.audio.i_bitspersample = 16;
2329 fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
2330 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
2332 msg_Dbg( p_this, "new audio es %d channels %dHz",
2333 fmt.audio.i_channels, fmt.audio.i_rate );
2337 demux_t *p_demux = (demux_t *)p_this;
2338 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
2344 /*****************************************************************************
2345 * ProbeVideoDev: probe video for capabilities
2346 *****************************************************************************/
2347 static vlc_bool_t ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
2355 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
2357 msg_Err( p_obj, "cannot open video device (%m)" );
2361 /* Get device capabilites */
2363 if( ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
2365 msg_Err( p_obj, "cannot get video capabilities (%m)" );
2369 msg_Dbg( p_obj, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
2370 p_sys->dev_cap.card,
2371 p_sys->dev_cap.driver,
2372 (p_sys->dev_cap.version >> 16) & 0xFF,
2373 (p_sys->dev_cap.version >> 8) & 0xFF,
2374 p_sys->dev_cap.version & 0xFF,
2375 p_sys->dev_cap.bus_info );
2377 msg_Dbg( p_obj, "the device has the capabilities: (%c) Video Capure, "
2380 ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE ? 'X':' '),
2381 ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO ? 'X':' '),
2382 ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER ? 'X':' ') );
2384 msg_Dbg( p_obj, "supported I/O methods are: (%c) Read/Write, "
2386 "(%c) Asynchronous",
2387 ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
2388 ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
2389 ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
2391 /* Now, enumerate all the video inputs. This is useless at the moment
2392 since we have no way to present that info to the user except with
2395 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2397 struct v4l2_input t_input;
2399 while( ioctl( i_fd, VIDIOC_ENUMINPUT, &t_input ) >= 0 )
2402 t_input.index = p_sys->i_input;
2405 p_sys->p_inputs = calloc( 1, p_sys->i_input * sizeof( struct v4l2_input ) );
2406 if( !p_sys->p_inputs ) goto open_failed;
2408 for( i_index = 0; i_index < p_sys->i_input; i_index++ )
2410 p_sys->p_inputs[i_index].index = i_index;
2412 if( ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
2414 msg_Err( p_obj, "cannot get video input characteristics (%m)" );
2417 msg_Dbg( p_obj, "video input %i (%s) has type: %s",
2419 p_sys->p_inputs[i_index].name,
2420 p_sys->p_inputs[i_index].type
2421 == V4L2_INPUT_TYPE_TUNER ?
2423 "External analog input" );
2427 /* Probe video standards */
2428 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2430 struct v4l2_standard t_standards;
2431 t_standards.index = 0;
2432 while( ioctl( i_fd, VIDIOC_ENUMSTD, &t_standards ) >=0 )
2434 p_sys->i_standard++;
2435 t_standards.index = p_sys->i_standard;
2438 p_sys->p_standards = calloc( 1, p_sys->i_standard * sizeof( struct v4l2_standard ) );
2439 if( !p_sys->p_standards ) goto open_failed;
2441 for( i_standard = 0; i_standard < p_sys->i_standard; i_standard++ )
2443 p_sys->p_standards[i_standard].index = i_standard;
2445 if( ioctl( i_fd, VIDIOC_ENUMSTD, &p_sys->p_standards[i_standard] ) )
2447 msg_Err( p_obj, "cannot get video input standards (%m)" );
2450 msg_Dbg( p_obj, "video standard %i is: %s",
2452 p_sys->p_standards[i_standard].name);
2456 /* initialize the structures for the ioctls */
2457 for( i_index = 0; i_index < 32; i_index++ )
2459 p_sys->p_audios[i_index].index = i_index;
2462 /* Probe audio inputs */
2463 if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
2465 while( p_sys->i_audio < 32 &&
2466 ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
2468 if( ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
2470 msg_Err( p_obj, "cannot get audio input characteristics (%m)" );
2474 msg_Dbg( p_obj, "audio device %i (%s) is %s",
2476 p_sys->p_audios[p_sys->i_audio].name,
2477 p_sys->p_audios[p_sys->i_audio].capability &
2478 V4L2_AUDCAP_STEREO ?
2479 "Stereo" : "Mono" );
2485 /* List tuner caps */
2486 if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
2488 struct v4l2_tuner tuner;
2489 memset( &tuner, 0, sizeof(tuner) );
2490 while( ioctl( i_fd, VIDIOC_G_TUNER, &tuner ) >= 0 )
2493 memset( &tuner, 0, sizeof(tuner) );
2494 tuner.index = p_sys->i_tuner;
2497 p_sys->p_tuners = calloc( 1, p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
2498 if( !p_sys->p_tuners ) goto open_failed;
2500 for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
2502 p_sys->p_tuners[i_index].index = i_index;
2504 if( ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
2506 msg_Err( p_obj, "cannot get tuner characteristics (%m)" );
2509 msg_Dbg( p_obj, "tuner %i (%s) has type: %s, "
2510 "frequency range: %.1f %s -> %.1f %s",
2512 p_sys->p_tuners[i_index].name,
2513 p_sys->p_tuners[i_index].type
2514 == V4L2_TUNER_RADIO ?
2515 "Radio" : "Analog TV",
2516 p_sys->p_tuners[i_index].rangelow * 62.5,
2517 p_sys->p_tuners[i_index].capability &
2518 V4L2_TUNER_CAP_LOW ?
2520 p_sys->p_tuners[i_index].rangehigh * 62.5,
2521 p_sys->p_tuners[i_index].capability &
2522 V4L2_TUNER_CAP_LOW ?
2527 /* Probe for available chromas */
2528 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2530 struct v4l2_fmtdesc codec;
2533 memset( &codec, 0, sizeof(codec) );
2534 codec.index = i_index;
2535 codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2537 while( ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
2540 codec.index = i_index;
2543 p_sys->i_codec = i_index;
2545 p_sys->p_codecs = calloc( 1, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
2547 for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
2549 p_sys->p_codecs[i_index].index = i_index;
2550 p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2552 if( ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
2554 msg_Err( p_obj, "cannot get codec description (%m)" );
2558 /* only print if vlc supports the format */
2559 char psz_fourcc_v4l2[5];
2560 memset( &psz_fourcc_v4l2, 0, sizeof( psz_fourcc_v4l2 ) );
2561 vlc_fourcc_to_char( p_sys->p_codecs[i_index].pixelformat,
2563 vlc_bool_t b_codec_supported = VLC_FALSE;
2564 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
2566 if( v4l2chroma_to_fourcc[i].i_v4l2 == p_sys->p_codecs[i_index].pixelformat )
2568 b_codec_supported = VLC_TRUE;
2571 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
2572 vlc_fourcc_to_char( v4l2chroma_to_fourcc[i].i_fourcc,
2574 msg_Dbg( p_obj, "device supports chroma %4s [%s, %s]",
2576 p_sys->p_codecs[i_index].description,
2579 #ifdef VIDIOC_ENUM_FRAMESIZES
2580 /* This is new in Linux 2.6.19 */
2581 /* List valid frame sizes for this format */
2582 struct v4l2_frmsizeenum frmsize;
2584 frmsize.pixel_format = p_sys->p_codecs[i_index].pixelformat;
2585 if( ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) < 0 )
2587 /* Not all devices support this ioctl */
2588 msg_Warn( p_obj, "Unable to query for frame sizes" );
2592 switch( frmsize.type )
2594 case V4L2_FRMSIZE_TYPE_DISCRETE:
2598 " device supports size %dx%d",
2599 frmsize.discrete.width, frmsize.discrete.height );
2601 } while( ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 );
2603 case V4L2_FRMSIZE_TYPE_STEPWISE:
2605 " device supports sizes %dx%d to %dx%d using %dx%d increments",
2606 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2607 frmsize.stepwise.max_width, frmsize.stepwise.max_height,
2608 frmsize.stepwise.step_width, frmsize.stepwise.step_height );
2610 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
2612 " device supports all sizes %dx%d to %dx%d",
2613 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2614 frmsize.stepwise.max_width, frmsize.stepwise.max_height );
2621 if( !b_codec_supported )
2624 "device codec %4s (%s) not supported as access_demux",
2626 p_sys->p_codecs[i_index].description );
2633 if( i_fd >= 0 ) close( i_fd );
2638 if( i_fd >= 0 ) close( i_fd );
2643 /*****************************************************************************
2644 * ProbeAudioDev: probe audio for capabilities
2645 *****************************************************************************/
2647 static vlc_bool_t ProbeAudioDevAlsa( vlc_object_t *p_this, demux_sys_t *p_sys,
2651 snd_pcm_t *p_alsa_pcm;
2652 char* psz_alsa_device_name = ResolveALSADeviceName( psz_device ?: ALSA_DEFAULT );
2654 if( ( i_err = snd_pcm_open( &p_alsa_pcm, psz_alsa_device_name, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0 )
2656 msg_Err( p_this, "cannot open device %s for ALSA audio (%s)", psz_alsa_device_name, snd_strerror( i_err ) );
2657 free( psz_alsa_device_name );
2661 snd_pcm_close( p_alsa_pcm );
2662 free( psz_alsa_device_name );
2668 static vlc_bool_t ProbeAudioDevOss( vlc_object_t *p_this, demux_sys_t *p_sys,
2673 if( !psz_device ) psz_device = strdup( OSS_DEFAULT ); /* FIXME leak */
2675 if( ( i_fd = open( psz_device, O_RDONLY | O_NONBLOCK ) ) < 0 )
2677 msg_Err( p_this, "cannot open device %s for OSS audio (%m)", psz_device );
2681 /* this will fail if the device is video */
2682 if( ioctl( i_fd, SNDCTL_DSP_GETCAPS, &i_caps ) < 0 )
2684 msg_Err( p_this, "cannot get audio caps (%m)" );
2688 if( i_fd >= 0 ) close( i_fd );
2693 if( i_fd >= 0 ) close( i_fd );
2697 static vlc_bool_t ProbeAudioDev( vlc_object_t *p_this, demux_sys_t *p_sys,
2701 if( ( p_sys->i_audio_method & AUDIO_METHOD_ALSA )
2702 && ProbeAudioDevAlsa( p_this, p_sys, psz_device ) )
2704 p_sys->i_audio_method = AUDIO_METHOD_ALSA;
2709 if( ( p_sys->i_audio_method & AUDIO_METHOD_OSS )
2710 && ProbeAudioDevOss( p_this, p_sys, psz_device ) )
2712 p_sys->i_audio_method = AUDIO_METHOD_OSS;
2716 p_sys->i_audio_method = 0;
2720 /*****************************************************************************
2721 * Print a user-class v4l2 control's details, create the relevant variable,
2722 * change the value if needed.
2723 *****************************************************************************/
2724 static void ControlListPrint( vlc_object_t *p_obj, int i_fd,
2725 struct v4l2_queryctrl queryctrl,
2726 vlc_bool_t b_reset, vlc_bool_t b_demux )
2728 struct v4l2_querymenu querymenu;
2735 vlc_value_t val, val2;
2737 if( queryctrl.flags & V4L2_CTRL_FLAG_GRABBED )
2738 msg_Dbg( p_obj, " control is busy" );
2739 if( queryctrl.flags & V4L2_CTRL_FLAG_READ_ONLY )
2740 msg_Dbg( p_obj, " control is read-only" );
2742 for( i = 0; controls[i].psz_name != NULL; i++ )
2743 if( controls[i].i_cid == queryctrl.id ) break;
2745 if( controls[i].psz_name )
2747 psz_name = strdup( controls[i].psz_name );
2748 char psz_cfg_name[40];
2749 sprintf( psz_cfg_name, CFG_PREFIX "%s", psz_name );
2750 i_val = var_CreateGetInteger( p_obj, psz_cfg_name );
2751 var_Destroy( p_obj, psz_cfg_name );
2756 psz_name = strdup( (const char *)queryctrl.name );
2757 for( psz_buf = psz_name; *psz_buf; psz_buf++ )
2759 if( *psz_buf == ' ' ) *psz_buf = '-';
2764 switch( queryctrl.type )
2766 case V4L2_CTRL_TYPE_INTEGER:
2767 msg_Dbg( p_obj, " integer control" );
2769 " valid values: %d to %d by steps of %d",
2770 queryctrl.minimum, queryctrl.maximum,
2773 var_Create( p_obj, psz_name,
2774 VLC_VAR_INTEGER | VLC_VAR_HASMIN | VLC_VAR_HASMAX
2775 | VLC_VAR_HASSTEP | VLC_VAR_ISCOMMAND );
2776 val.i_int = queryctrl.minimum;
2777 var_Change( p_obj, psz_name, VLC_VAR_SETMIN, &val, NULL );
2778 val.i_int = queryctrl.maximum;
2779 var_Change( p_obj, psz_name, VLC_VAR_SETMAX, &val, NULL );
2780 val.i_int = queryctrl.step;
2781 var_Change( p_obj, psz_name, VLC_VAR_SETSTEP, &val, NULL );
2783 case V4L2_CTRL_TYPE_BOOLEAN:
2784 msg_Dbg( p_obj, " boolean control" );
2785 var_Create( p_obj, psz_name,
2786 VLC_VAR_BOOL | VLC_VAR_ISCOMMAND );
2788 case V4L2_CTRL_TYPE_MENU:
2789 msg_Dbg( p_obj, " menu control" );
2790 var_Create( p_obj, psz_name,
2791 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE
2792 | VLC_VAR_ISCOMMAND );
2793 memset( &querymenu, 0, sizeof( querymenu ) );
2794 for( i_mid = queryctrl.minimum;
2795 i_mid <= (unsigned)queryctrl.maximum;
2798 querymenu.index = i_mid;
2799 querymenu.id = queryctrl.id;
2800 if( ioctl( i_fd, VIDIOC_QUERYMENU, &querymenu ) >= 0 )
2802 msg_Dbg( p_obj, " %d: %s",
2803 querymenu.index, querymenu.name );
2804 val.i_int = querymenu.index;
2805 val2.psz_string = (char *)querymenu.name;
2806 var_Change( p_obj, psz_name,
2807 VLC_VAR_ADDCHOICE, &val, &val2 );
2811 case V4L2_CTRL_TYPE_BUTTON:
2812 msg_Dbg( p_obj, " button control" );
2813 var_Create( p_obj, psz_name,
2814 VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2816 case V4L2_CTRL_TYPE_CTRL_CLASS:
2817 msg_Dbg( p_obj, " control class" );
2818 var_Create( p_obj, psz_name, VLC_VAR_VOID );
2821 msg_Dbg( p_obj, " unknown control type (FIXME)" );
2826 switch( queryctrl.type )
2828 case V4L2_CTRL_TYPE_INTEGER:
2829 case V4L2_CTRL_TYPE_BOOLEAN:
2830 case V4L2_CTRL_TYPE_MENU:
2832 struct v4l2_control control;
2833 msg_Dbg( p_obj, " default value: %d",
2834 queryctrl.default_value );
2835 memset( &control, 0, sizeof( control ) );
2836 control.id = queryctrl.id;
2837 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
2839 msg_Dbg( p_obj, " current value: %d", control.value );
2843 i_val = control.value;
2844 if( b_reset && queryctrl.default_value != control.value )
2846 msg_Dbg( p_obj, " reset value to default" );
2847 Control( p_obj, i_fd, psz_name,
2848 queryctrl.id, queryctrl.default_value );
2853 Control( p_obj, i_fd, psz_name,
2854 queryctrl.id, i_val );
2862 val.psz_string = (char *)queryctrl.name;
2863 var_Change( p_obj, psz_name, VLC_VAR_SETTEXT, &val, NULL );
2864 val.i_int = queryctrl.id;
2865 val2.psz_string = (char *)psz_name;
2866 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
2868 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
2871 var_SetBool( p_obj, psz_name, i_val );
2873 case VLC_VAR_INTEGER:
2874 var_SetInteger( p_obj, psz_name, i_val );
2879 msg_Warn( p_obj, "FIXME: %s %s %d", __FILE__, __func__,
2885 var_AddCallback( p_obj, psz_name,
2886 DemuxControlCallback, (void*)queryctrl.id );
2888 var_AddCallback( p_obj, psz_name,
2889 AccessControlCallback, (void*)queryctrl.id );
2894 /*****************************************************************************
2895 * List all user-class v4l2 controls, set them to the user specified
2896 * value and create the relevant variables to enable runtime changes
2897 *****************************************************************************/
2898 static int ControlList( vlc_object_t *p_obj, int i_fd,
2899 vlc_bool_t b_reset, vlc_bool_t b_demux )
2901 struct v4l2_queryctrl queryctrl;
2904 memset( &queryctrl, 0, sizeof( queryctrl ) );
2906 /* A list of available controls (aka the variable name) will be
2907 * stored as choices in the "controls" variable. We'll thus be able
2908 * to use those to create an appropriate interface */
2909 var_Create( p_obj, "controls", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
2911 var_Create( p_obj, "controls-update", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2913 /* Add a control to reset all controls to their default values */
2914 vlc_value_t val, val2;
2915 var_Create( p_obj, "controls-reset", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2916 val.psz_string = _( "Reset controls to default" );
2917 var_Change( p_obj, "controls-reset", VLC_VAR_SETTEXT, &val, NULL );
2919 val2.psz_string = (char *)"controls-reset";
2920 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
2922 var_AddCallback( p_obj, "controls-reset", DemuxControlResetCallback, NULL );
2924 var_AddCallback( p_obj, "controls-reset", AccessControlResetCallback, NULL );
2926 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2927 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2929 msg_Dbg( p_obj, "Extended control API supported by v4l2 driver" );
2931 /* List extended controls */
2932 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2933 while( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2935 switch( V4L2_CTRL_ID2CLASS( queryctrl.id ) )
2937 case V4L2_CTRL_CLASS_USER:
2938 msg_Dbg( p_obj, "Available control: %s (%x)",
2939 queryctrl.name, queryctrl.id );
2941 case V4L2_CTRL_CLASS_MPEG:
2942 msg_Dbg( p_obj, "Available MPEG control: %s (%x)",
2943 queryctrl.name, queryctrl.id );
2946 msg_Dbg( p_obj, "Available private control: %s (%x)",
2947 queryctrl.name, queryctrl.id );
2950 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
2951 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
2956 msg_Dbg( p_obj, "Extended control API not supported by v4l2 driver" );
2958 /* List public controls */
2959 for( i_cid = V4L2_CID_BASE;
2960 i_cid < V4L2_CID_LASTP1;
2963 queryctrl.id = i_cid;
2964 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2966 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2968 msg_Dbg( p_obj, "Available control: %s (%x)",
2969 queryctrl.name, queryctrl.id );
2970 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
2974 /* List private controls */
2975 for( i_cid = V4L2_CID_PRIVATE_BASE;
2979 queryctrl.id = i_cid;
2980 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2982 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2984 msg_Dbg( p_obj, "Available private control: %s (%x)",
2985 queryctrl.name, queryctrl.id );
2986 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
2996 /*****************************************************************************
2997 * Reset all user-class v4l2 controls to their default value
2998 *****************************************************************************/
2999 static int ControlReset( vlc_object_t *p_obj, int i_fd )
3001 struct v4l2_queryctrl queryctrl;
3004 memset( &queryctrl, 0, sizeof( queryctrl ) );
3006 /* public controls */
3007 for( i_cid = V4L2_CID_BASE;
3008 i_cid < V4L2_CID_LASTP1;
3011 queryctrl.id = i_cid;
3012 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3014 struct v4l2_control control;
3015 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3017 memset( &control, 0, sizeof( control ) );
3018 control.id = queryctrl.id;
3019 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
3020 && queryctrl.default_value != control.value )
3023 for( i = 0; controls[i].psz_name != NULL; i++ )
3024 if( controls[i].i_cid == queryctrl.id ) break;
3025 Control( p_obj, i_fd,
3026 controls[i].psz_name ? controls[i].psz_name
3027 : (const char *)queryctrl.name,
3028 queryctrl.id, queryctrl.default_value );
3033 /* private controls */
3034 for( i_cid = V4L2_CID_PRIVATE_BASE;
3038 queryctrl.id = i_cid;
3039 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
3041 struct v4l2_control control;
3042 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3044 memset( &control, 0, sizeof( control ) );
3045 control.id = queryctrl.id;
3046 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
3047 && queryctrl.default_value != control.value )
3049 Control( p_obj, i_fd, (const char *)queryctrl.name,
3050 queryctrl.id, queryctrl.default_value );
3059 /*****************************************************************************
3060 * Issue user-class v4l2 controls
3061 *****************************************************************************/
3062 static int Control( vlc_object_t *p_obj, int i_fd,
3063 const char *psz_name, int i_cid, int i_value )
3065 struct v4l2_queryctrl queryctrl;
3066 struct v4l2_control control;
3071 memset( &queryctrl, 0, sizeof( queryctrl ) );
3073 queryctrl.id = i_cid;
3075 if( ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) < 0
3076 || queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
3078 msg_Dbg( p_obj, "%s (%x) control is not supported.", psz_name,
3080 return VLC_EGENERIC;
3083 memset( &control, 0, sizeof( control ) );
3088 control.value = i_value;
3089 if( ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
3091 msg_Err( p_obj, "unable to set %s to %d (%m)", psz_name,
3093 return VLC_EGENERIC;
3096 if( ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
3099 msg_Dbg( p_obj, "video %s: %d", psz_name, control.value );
3100 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
3103 val.b_bool = control.value;
3104 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
3105 var_SetVoid( p_obj, "controls-update" );
3107 case VLC_VAR_INTEGER:
3108 val.i_int = control.value;
3109 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
3110 var_SetVoid( p_obj, "controls-update" );
3117 /*****************************************************************************
3118 * On the fly change settings callback
3119 *****************************************************************************/
3120 static int DemuxControlCallback( vlc_object_t *p_this,
3121 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3124 demux_t *p_demux = (demux_t*)p_this;
3125 demux_sys_t *p_sys = p_demux->p_sys;
3126 int i_cid = (int)p_data;
3128 int i_fd = p_sys->i_fd_video;
3131 return VLC_EGENERIC;
3133 Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
3135 return VLC_EGENERIC;
3138 static int DemuxControlResetCallback( vlc_object_t *p_this,
3139 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3142 demux_t *p_demux = (demux_t*)p_this;
3143 demux_sys_t *p_sys = p_demux->p_sys;
3145 int i_fd = p_sys->i_fd_video;
3148 return VLC_EGENERIC;
3150 ControlReset( p_this, i_fd );
3152 return VLC_EGENERIC;
3155 static int AccessControlCallback( vlc_object_t *p_this,
3156 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3159 access_t *p_access = (access_t *)p_this;
3160 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
3161 int i_cid = (int)p_data;
3163 int i_fd = p_sys->i_fd_video;
3166 return VLC_EGENERIC;
3168 Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
3170 return VLC_EGENERIC;
3173 static int AccessControlResetCallback( vlc_object_t *p_this,
3174 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
3177 access_t *p_access = (access_t *)p_this;
3178 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
3180 int i_fd = p_sys->i_fd_video;
3183 return VLC_EGENERIC;
3185 ControlReset( p_this, i_fd );
3187 return VLC_EGENERIC;