1 /*****************************************************************************
2 * v4l2.c : Video4Linux2 input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2009 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
32 /*****************************************************************************
34 *****************************************************************************/
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
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>
68 # define v4l2_close close
70 # define v4l2_ioctl ioctl
71 # define v4l2_read read
72 # define v4l2_mmap mmap
73 # define v4l2_munmap munmap
76 /*****************************************************************************
78 *****************************************************************************/
80 static int DemuxOpen ( vlc_object_t * );
81 static void DemuxClose( vlc_object_t * );
82 static int AccessOpen ( vlc_object_t * );
83 static void AccessClose( vlc_object_t * );
85 #define STANDARD_TEXT N_( "Standard" )
86 #define STANDARD_LONGTEXT N_( \
87 "Video standard (Default, SECAM, PAL, or NTSC)." )
88 #define CHROMA_TEXT N_("Video input chroma format")
89 #define CHROMA_LONGTEXT N_( \
90 "Force the Video4Linux2 video device to use a specific chroma format " \
91 "(eg. I420 or I422 for raw images, MJPG for M-JPEG compressed input) " \
92 "(Complete list: GREY, I240, RV16, RV15, RV24, RV32, YUY2, YUYV, UYVY, " \
93 "I41N, I422, I420, I411, I410, MJPG)")
94 #define INPUT_TEXT N_( "Input" )
95 #define INPUT_LONGTEXT N_( \
96 "Input of the card to use (see debug)." )
97 #define AUDIO_INPUT_TEXT N_( "Audio input" )
98 #define AUDIO_INPUT_LONGTEXT N_( \
99 "Audio input of the card to use (see debug)." )
100 #define IOMETHOD_TEXT N_( "IO Method" )
101 #define IOMETHOD_LONGTEXT N_( \
102 "IO Method (READ, MMAP, USERPTR)." )
103 #define WIDTH_TEXT N_( "Width" )
104 #define WIDTH_LONGTEXT N_( \
105 "Force width (-1 for autodetect)." )
106 #define HEIGHT_TEXT N_( "Height" )
107 #define HEIGHT_LONGTEXT N_( \
108 "Force height (-1 for autodetect)." )
109 #define FPS_TEXT N_( "Framerate" )
110 #define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
111 "(-1 for autodetect)." )
113 #define CTRL_RESET_TEXT N_( "Reset v4l2 controls" )
114 #define CTRL_RESET_LONGTEXT N_( \
115 "Reset controls to defaults provided by the v4l2 driver." )
116 #define BRIGHTNESS_TEXT N_( "Brightness" )
117 #define BRIGHTNESS_LONGTEXT N_( \
118 "Brightness of the video input (if supported by the v4l2 driver)." )
119 #define CONTRAST_TEXT N_( "Contrast" )
120 #define CONTRAST_LONGTEXT N_( \
121 "Contrast of the video input (if supported by the v4l2 driver)." )
122 #define SATURATION_TEXT N_( "Saturation" )
123 #define SATURATION_LONGTEXT N_( \
124 "Saturation of the video input (if supported by the v4l2 driver)." )
125 #define HUE_TEXT N_( "Hue" )
126 #define HUE_LONGTEXT N_( \
127 "Hue of the video input (if supported by the v4l2 driver)." )
128 #define BLACKLEVEL_TEXT N_( "Black level" )
129 #define BLACKLEVEL_LONGTEXT N_( \
130 "Black level of the video input (if supported by the v4l2 driver)." )
131 #define AUTOWHITEBALANCE_TEXT N_( "Auto white balance" )
132 #define AUTOWHITEBALANCE_LONGTEXT N_( \
133 "Automatically set the white balance of the video input " \
134 "(if supported by the v4l2 driver)." )
135 #define DOWHITEBALANCE_TEXT N_( "Do white balance" )
136 #define DOWHITEBALANCE_LONGTEXT N_( \
137 "Trigger a white balancing action, useless if auto white balance is " \
138 "activated (if supported by the v4l2 driver)." )
139 #define REDBALANCE_TEXT N_( "Red balance" )
140 #define REDBALANCE_LONGTEXT N_( \
141 "Red balance of the video input (if supported by the v4l2 driver)." )
142 #define BLUEBALANCE_TEXT N_( "Blue balance" )
143 #define BLUEBALANCE_LONGTEXT N_( \
144 "Blue balance of the video input (if supported by the v4l2 driver)." )
145 #define GAMMA_TEXT N_( "Gamma" )
146 #define GAMMA_LONGTEXT N_( \
147 "Gamma of the video input (if supported by the v4l2 driver)." )
148 #define EXPOSURE_TEXT N_( "Exposure" )
149 #define EXPOSURE_LONGTEXT N_( \
150 "Exposure of the video input (if supported by the v4L2 driver)." )
151 #define AUTOGAIN_TEXT N_( "Auto gain" )
152 #define AUTOGAIN_LONGTEXT N_( \
153 "Automatically set the video input's gain (if supported by the " \
155 #define GAIN_TEXT N_( "Gain" )
156 #define GAIN_LONGTEXT N_( \
157 "Video input's gain (if supported by the v4l2 driver)." )
158 #define HFLIP_TEXT N_( "Horizontal flip" )
159 #define HFLIP_LONGTEXT N_( \
160 "Flip the video horizontally (if supported by the v4l2 driver)." )
161 #define VFLIP_TEXT N_( "Vertical flip" )
162 #define VFLIP_LONGTEXT N_( \
163 "Flip the video vertically (if supported by the v4l2 driver)." )
164 #define HCENTER_TEXT N_( "Horizontal centering" )
165 #define HCENTER_LONGTEXT N_( \
166 "Set the camera's horizontal centering (if supported by the v4l2 driver)." )
167 #define VCENTER_TEXT N_( "Vertical centering" )
168 #define VCENTER_LONGTEXT N_( \
169 "Set the camera's vertical centering (if supported by the v4l2 driver)." )
171 #define AUDIO_VOLUME_TEXT N_( "Volume" )
172 #define AUDIO_VOLUME_LONGTEXT N_( \
173 "Volume of the audio input (if supported by the v4l2 driver)." )
174 #define AUDIO_BALANCE_TEXT N_( "Balance" )
175 #define AUDIO_BALANCE_LONGTEXT N_( \
176 "Balance of the audio input (if supported by the v4l2 driver)." )
177 #define AUDIO_MUTE_TEXT N_( "Mute" )
178 #define AUDIO_MUTE_LONGTEXT N_( \
179 "Mute audio input (if supported by the v4l2 driver)." )
180 #define AUDIO_BASS_TEXT N_( "Bass" )
181 #define AUDIO_BASS_LONGTEXT N_( \
182 "Bass level of the audio input (if supported by the v4l2 driver)." )
183 #define AUDIO_TREBLE_TEXT N_( "Treble" )
184 #define AUDIO_TREBLE_LONGTEXT N_( \
185 "Treble level of the audio input (if supported by the v4l2 driver)." )
186 #define AUDIO_LOUDNESS_TEXT N_( "Loudness" )
187 #define AUDIO_LOUDNESS_LONGTEXT N_( \
188 "Loudness of the audio input (if supported by the v4l2 driver)." )
190 #define CACHING_TEXT N_("Caching value in ms")
191 #define CACHING_LONGTEXT N_( \
192 "Caching value for V4L2 captures. This " \
193 "value should be set in milliseconds." )
194 #define S_CTRLS_TEXT N_("v4l2 driver controls")
195 #define S_CTRLS_LONGTEXT N_( \
196 "Set the v4l2 driver controls to the values specified using a comma " \
197 "separated list optionally encapsulated by curly braces " \
198 "(e.g.: {video_bitrate=6000000,audio_crc=0,stream_type=3} ). " \
199 "To list available controls, increase verbosity (-vvv) " \
200 "or use the v4l2-ctl application." )
202 #define TUNER_TEXT N_("Tuner id")
203 #define TUNER_LONGTEXT N_( \
204 "Tuner id (see debug output)." )
205 #define FREQUENCY_TEXT N_("Frequency")
206 #define FREQUENCY_LONGTEXT N_( \
207 "Tuner frequency in Hz or kHz (see debug output)" )
208 #define TUNER_AUDIO_MODE_TEXT N_("Audio mode")
209 #define TUNER_AUDIO_MODE_LONGTEXT N_( \
210 "Tuner audio mono/stereo and track selection." )
212 #define AUDIO_DEPRECATED_ERROR N_( \
213 "Alsa or OSS audio capture in the v4l2 access is deprecated. " \
214 "please use 'v4l2:/""/ :input-slave=alsa:/""/' or " \
215 "'v4l2:/""/ :input-slave=oss:/""/' instead." )
223 static const int i_standards_list[] =
224 { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
225 static const char *const psz_standards_list_text[] =
226 { N_("Default"), N_("SECAM"), N_("PAL"), N_("NTSC") };
228 static const int i_iomethod_list[] =
229 { IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR };
230 static const char *const psz_iomethod_list_text[] =
231 { N_("READ"), N_("MMAP"), N_("USERPTR") };
233 static const int i_tuner_audio_modes_list[] =
234 { V4L2_TUNER_MODE_MONO, V4L2_TUNER_MODE_STEREO,
235 V4L2_TUNER_MODE_LANG1, V4L2_TUNER_MODE_LANG2,
236 V4L2_TUNER_MODE_SAP, V4L2_TUNER_MODE_LANG1_LANG2 };
237 static const char *const psz_tuner_audio_modes_list_text[] =
240 N_( "Primary language (Analog TV tuners only)" ),
241 N_( "Secondary language (Analog TV tuners only)" ),
242 N_( "Second audio program (Analog TV tuners only)" ),
243 N_( "Primary language left, Secondary language right" ) };
245 #define V4L2_DEFAULT "/dev/video0"
246 #define CFG_PREFIX "v4l2-"
249 set_shortname( N_("Video4Linux2") )
250 set_description( N_("Video4Linux2 input") )
251 set_category( CAT_INPUT )
252 set_subcategory( SUBCAT_INPUT_ACCESS )
254 set_section( N_( "Video input" ), NULL )
255 add_integer( CFG_PREFIX "standard", 0, NULL, STANDARD_TEXT,
256 STANDARD_LONGTEXT, false );
257 change_integer_list( i_standards_list, psz_standards_list_text, NULL );
258 add_string( CFG_PREFIX "chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
260 add_integer( CFG_PREFIX "input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
262 add_integer( CFG_PREFIX "audio-input", 0, NULL, AUDIO_INPUT_TEXT,
263 AUDIO_INPUT_LONGTEXT, true );
264 add_integer( CFG_PREFIX "io", IO_METHOD_MMAP, NULL, IOMETHOD_TEXT,
265 IOMETHOD_LONGTEXT, true );
266 change_integer_list( i_iomethod_list, psz_iomethod_list_text, NULL );
267 add_integer( CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
268 WIDTH_LONGTEXT, true );
269 add_integer( CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
270 HEIGHT_LONGTEXT, true );
271 add_float( CFG_PREFIX "fps", 0, NULL, FPS_TEXT, FPS_LONGTEXT, true )
272 add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
273 CACHING_TEXT, CACHING_LONGTEXT, true );
275 set_section( N_( "Tuner" ), NULL )
276 add_integer( CFG_PREFIX "tuner", 0, NULL, TUNER_TEXT, TUNER_LONGTEXT,
278 add_integer( CFG_PREFIX "tuner-frequency", -1, NULL, FREQUENCY_TEXT,
279 FREQUENCY_LONGTEXT, true );
280 add_integer( CFG_PREFIX "tuner-audio-mode", -1, NULL, TUNER_AUDIO_MODE_TEXT,
281 TUNER_AUDIO_MODE_LONGTEXT, true );
282 change_integer_list( i_tuner_audio_modes_list,
283 psz_tuner_audio_modes_list_text, 0 );
285 set_section( N_( "Controls" ),
286 N_( "v4l2 driver controls, if supported by your v4l2 driver." ) );
287 add_bool( CFG_PREFIX "controls-reset", false, NULL, CTRL_RESET_TEXT,
288 CTRL_RESET_LONGTEXT, true );
289 add_integer( CFG_PREFIX "brightness", -1, NULL, BRIGHTNESS_TEXT,
290 BRIGHTNESS_LONGTEXT, true );
291 add_integer( CFG_PREFIX "contrast", -1, NULL, CONTRAST_TEXT,
292 CONTRAST_LONGTEXT, true );
293 add_integer( CFG_PREFIX "saturation", -1, NULL, SATURATION_TEXT,
294 SATURATION_LONGTEXT, true );
295 add_integer( CFG_PREFIX "hue", -1, NULL, HUE_TEXT,
296 HUE_LONGTEXT, true );
297 add_integer( CFG_PREFIX "black-level", -1, NULL, BLACKLEVEL_TEXT,
298 BLACKLEVEL_LONGTEXT, true );
299 add_integer( CFG_PREFIX "auto-white-balance", -1, NULL,
300 AUTOWHITEBALANCE_TEXT, AUTOWHITEBALANCE_LONGTEXT, true );
301 add_integer( CFG_PREFIX "do-white-balance", -1, NULL, DOWHITEBALANCE_TEXT,
302 DOWHITEBALANCE_LONGTEXT, true );
303 add_integer( CFG_PREFIX "red-balance", -1, NULL, REDBALANCE_TEXT,
304 REDBALANCE_LONGTEXT, true );
305 add_integer( CFG_PREFIX "blue-balance", -1, NULL, BLUEBALANCE_TEXT,
306 BLUEBALANCE_LONGTEXT, true );
307 add_integer( CFG_PREFIX "gamma", -1, NULL, GAMMA_TEXT,
308 GAMMA_LONGTEXT, true );
309 add_integer( CFG_PREFIX "exposure", -1, NULL, EXPOSURE_TEXT,
310 EXPOSURE_LONGTEXT, true );
311 add_integer( CFG_PREFIX "autogain", -1, NULL, AUTOGAIN_TEXT,
312 AUTOGAIN_LONGTEXT, true );
313 add_integer( CFG_PREFIX "gain", -1, NULL, GAIN_TEXT,
314 GAIN_LONGTEXT, true );
315 add_integer( CFG_PREFIX "hflip", -1, NULL, HFLIP_TEXT,
316 HFLIP_LONGTEXT, true );
317 add_integer( CFG_PREFIX "vflip", -1, NULL, VFLIP_TEXT,
318 VFLIP_LONGTEXT, true );
319 add_integer( CFG_PREFIX "hcenter", -1, NULL, HCENTER_TEXT,
320 HCENTER_LONGTEXT, true );
321 add_integer( CFG_PREFIX "vcenter", -1, NULL, VCENTER_TEXT,
322 VCENTER_LONGTEXT, true );
323 add_integer( CFG_PREFIX "audio-volume", -1, NULL, AUDIO_VOLUME_TEXT,
324 AUDIO_VOLUME_LONGTEXT, true );
325 add_integer( CFG_PREFIX "audio-balance", -1, NULL, AUDIO_BALANCE_TEXT,
326 AUDIO_BALANCE_LONGTEXT, true );
327 add_bool( CFG_PREFIX "audio-mute", false, NULL, AUDIO_MUTE_TEXT,
328 AUDIO_MUTE_LONGTEXT, true );
329 add_integer( CFG_PREFIX "audio-bass", -1, NULL, AUDIO_BASS_TEXT,
330 AUDIO_BASS_LONGTEXT, true );
331 add_integer( CFG_PREFIX "audio-treble", -1, NULL, AUDIO_TREBLE_TEXT,
332 AUDIO_TREBLE_LONGTEXT, true );
333 add_integer( CFG_PREFIX "audio-loudness", -1, NULL, AUDIO_LOUDNESS_TEXT,
334 AUDIO_LOUDNESS_LONGTEXT, true );
335 add_string( CFG_PREFIX "set-ctrls", NULL, NULL, S_CTRLS_TEXT,
336 S_CTRLS_LONGTEXT, true );
338 add_obsolete_string( CFG_PREFIX "dev" );
340 add_obsolete_string( CFG_PREFIX "adev" );
341 add_obsolete_integer( CFG_PREFIX "audio-method" );
342 add_obsolete_bool( CFG_PREFIX "stereo" );
343 add_obsolete_integer( CFG_PREFIX "samplerate" );
345 add_shortcut( "v4l2" )
346 set_capability( "access_demux", 10 )
347 set_callbacks( DemuxOpen, DemuxClose )
350 add_shortcut( "v4l2c" )
351 set_description( N_("Video4Linux2 Compressed A/V") )
352 set_capability( "access", 0 )
353 /* use these when open as access_demux fails; VLC will use another demux */
354 set_callbacks( AccessOpen, AccessClose )
358 /*****************************************************************************
359 * Access: local prototypes
360 *****************************************************************************/
362 static void CommonClose( vlc_object_t *, demux_sys_t * );
363 static void ParseMRL( demux_sys_t *, char *, vlc_object_t * );
364 static void GetV4L2Params( demux_sys_t *, vlc_object_t * );
365 static void SetAvailControlsByString( vlc_object_t *, demux_sys_t *, int );
367 static int DemuxControl( demux_t *, int, va_list );
368 static int AccessControl( access_t *, int, va_list );
370 static int Demux( demux_t * );
371 static ssize_t AccessRead( access_t *, uint8_t *, size_t );
373 static block_t* GrabVideo( demux_t *p_demux );
374 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t );
376 static bool IsPixelFormatSupported( demux_t *p_demux,
377 unsigned int i_pixelformat );
379 static int OpenVideoDev( vlc_object_t *, demux_sys_t *, bool );
380 static bool ProbeVideoDev( vlc_object_t *, demux_sys_t *,
381 const char *psz_device );
383 static int ControlList( vlc_object_t *, int , bool, bool );
384 static int Control( vlc_object_t *, int i_fd,
385 const char *psz_name, int i_cid, int i_value );
387 static int DemuxControlCallback( vlc_object_t *p_this, const char *psz_var,
388 vlc_value_t oldval, vlc_value_t newval,
390 static int DemuxControlResetCallback( vlc_object_t *p_this, const char *psz_var,
391 vlc_value_t oldval, vlc_value_t newval,
393 static int AccessControlCallback( vlc_object_t *p_this, const char *psz_var,
394 vlc_value_t oldval, vlc_value_t newval,
396 static int AccessControlResetCallback( vlc_object_t *p_this,
397 const char *psz_var, vlc_value_t oldval,
398 vlc_value_t newval, void *p_data );
407 } v4l2chroma_to_fourcc[] =
410 { V4L2_PIX_FMT_GREY, VLC_FOURCC('G','R','E','Y'), 0, 0, 0 },
411 { V4L2_PIX_FMT_HI240, VLC_FOURCC('I','2','4','0'), 0, 0, 0 },
412 { V4L2_PIX_FMT_RGB555, VLC_FOURCC('R','V','1','5'), 0x001f,0x03e0,0x7c00 },
413 { V4L2_PIX_FMT_RGB565, VLC_FOURCC('R','V','1','6'), 0x001f,0x07e0,0xf800 },
414 /* Won't work since we don't know how to handle such gmask values
416 { V4L2_PIX_FMT_RGB555X, VLC_FOURCC('R','V','1','5'), 0x007c,0xe003,0x1f00 },
417 { V4L2_PIX_FMT_RGB565X, VLC_FOURCC('R','V','1','6'), 0x00f8,0xe007,0x1f00 },
419 { V4L2_PIX_FMT_BGR24, VLC_FOURCC('R','V','2','4'), 0xff0000,0xff00,0xff },
420 { V4L2_PIX_FMT_RGB24, VLC_FOURCC('R','V','2','4'), 0xff,0xff00,0xff0000 },
421 { V4L2_PIX_FMT_BGR32, VLC_FOURCC('R','V','3','2'), 0xff0000,0xff00,0xff },
422 { V4L2_PIX_FMT_RGB32, VLC_FOURCC('R','V','3','2'), 0xff,0xff00,0xff0000 },
423 { V4L2_PIX_FMT_YUYV, VLC_FOURCC('Y','U','Y','2'), 0, 0, 0 },
424 { V4L2_PIX_FMT_YUYV, VLC_FOURCC('Y','U','Y','V'), 0, 0, 0 },
425 { V4L2_PIX_FMT_UYVY, VLC_FOURCC('U','Y','V','Y'), 0, 0, 0 },
426 { V4L2_PIX_FMT_Y41P, VLC_FOURCC('I','4','1','N'), 0, 0, 0 },
427 { V4L2_PIX_FMT_YUV422P, VLC_FOURCC('I','4','2','2'), 0, 0, 0 },
428 { V4L2_PIX_FMT_YVU420, VLC_FOURCC('Y','V','1','2'), 0, 0, 0 },
429 { V4L2_PIX_FMT_YUV411P, VLC_FOURCC('I','4','1','1'), 0, 0, 0 },
430 { V4L2_PIX_FMT_YUV410, VLC_FOURCC('I','4','1','0'), 0, 0, 0 },
432 /* Raw data types, not in V4L2 spec but still in videodev2.h and supported
434 { V4L2_PIX_FMT_YUV420, VLC_FOURCC('I','4','2','0'), 0, 0, 0 },
435 /* FIXME { V4L2_PIX_FMT_RGB444, VLC_FOURCC('R','V','3','2') }, */
437 /* Compressed data types */
438 { V4L2_PIX_FMT_MJPEG, VLC_FOURCC('M','J','P','G'), 0, 0, 0 },
440 { V4L2_PIX_FMT_JPEG, VLC_FOURCC('J','P','E','G') },
441 { V4L2_PIX_FMT_DV, VLC_FOURCC('?','?','?','?') },
442 { V4L2_PIX_FMT_MPEG, VLC_FOURCC('?','?','?','?') },
448 * List of V4L2 chromas were confident enough to use as fallbacks if the
449 * user hasn't provided a --v4l2-chroma value.
451 * Try YUV chromas first, then RGB little endian and MJPEG as last resort.
453 static const __u32 p_chroma_fallbacks[] =
454 { V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_YVU420, V4L2_PIX_FMT_YUV422P,
455 V4L2_PIX_FMT_YUYV, V4L2_PIX_FMT_UYVY, V4L2_PIX_FMT_BGR24,
456 V4L2_PIX_FMT_BGR32, V4L2_PIX_FMT_MJPEG };
460 const char *psz_name;
464 { "brightness", V4L2_CID_BRIGHTNESS },
465 { "contrast", V4L2_CID_CONTRAST },
466 { "saturation", V4L2_CID_SATURATION },
467 { "hue", V4L2_CID_HUE },
468 { "audio-volume", V4L2_CID_AUDIO_VOLUME },
469 { "audio-balance", V4L2_CID_AUDIO_BALANCE },
470 { "audio-bass", V4L2_CID_AUDIO_BASS },
471 { "audio-treble", V4L2_CID_AUDIO_TREBLE },
472 { "audio-mute", V4L2_CID_AUDIO_MUTE },
473 { "audio-loudness", V4L2_CID_AUDIO_LOUDNESS },
474 { "black-level", V4L2_CID_BLACK_LEVEL },
475 { "auto-white-balance", V4L2_CID_AUTO_WHITE_BALANCE },
476 { "do-white-balance", V4L2_CID_DO_WHITE_BALANCE },
477 { "red-balance", V4L2_CID_RED_BALANCE },
478 { "blue-balance", V4L2_CID_BLUE_BALANCE },
479 { "gamma", V4L2_CID_GAMMA },
480 { "exposure", V4L2_CID_EXPOSURE },
481 { "autogain", V4L2_CID_AUTOGAIN },
482 { "gain", V4L2_CID_GAIN },
483 { "hflip", V4L2_CID_HFLIP },
484 { "vflip", V4L2_CID_VFLIP },
485 { "hcenter", V4L2_CID_HCENTER },
486 { "vcenter", V4L2_CID_VCENTER },
498 char *psz_device; /* Main device from MRL */
501 char *psz_requested_chroma;
508 struct v4l2_capability dev_cap;
511 struct v4l2_input *p_inputs;
512 int i_selected_input;
515 struct v4l2_standard *p_standards;
516 v4l2_std_id i_selected_standard_id;
519 /* V4L2 devices cannot have more than 32 audio inputs */
520 struct v4l2_audio p_audios[32];
521 int i_selected_audio_input;
524 struct v4l2_tuner *p_tuners;
527 struct v4l2_fmtdesc *p_codecs;
529 struct buffer_t *p_buffers;
530 unsigned int i_nbuffers;
534 float f_fps; /* <= 0.0 mean to grab at full rate */
535 mtime_t i_video_pts; /* only used when f_fps > 0 */
549 static int FindMainDevice( vlc_object_t *p_this, demux_sys_t *p_sys,
552 /* TODO: if using default device, loop through all /dev/video* until
554 msg_Dbg( p_this, "opening device '%s'", p_sys->psz_device );
555 if( ProbeVideoDev( p_this, p_sys, p_sys->psz_device ) )
557 msg_Dbg( p_this, "'%s' is a video device", p_sys->psz_device );
558 p_sys->i_fd = OpenVideoDev( p_this, p_sys, b_demux );
561 if( p_sys->i_fd < 0 ) return VLC_EGENERIC;
565 /*****************************************************************************
566 * DemuxOpen: opens v4l2 device, access_demux callback
567 *****************************************************************************
569 * url: <video device>::::
571 *****************************************************************************/
572 static int DemuxOpen( vlc_object_t *p_this )
574 demux_t *p_demux = (demux_t*)p_this;
577 /* Only when selected */
578 if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
581 p_demux->pf_control = DemuxControl;
582 p_demux->pf_demux = Demux;
583 p_demux->info.i_update = 0;
584 p_demux->info.i_title = 0;
585 p_demux->info.i_seekpoint = 0;
587 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
588 if( p_sys == NULL ) return VLC_ENOMEM;
590 GetV4L2Params(p_sys, (vlc_object_t *) p_demux);
592 ParseMRL( p_sys, p_demux->psz_path, (vlc_object_t *) p_demux );
594 if( FindMainDevice( p_this, p_sys, true ) != VLC_SUCCESS)
596 DemuxClose( p_this );
603 /*****************************************************************************
604 * GetV4L2Params: fill in p_sys parameters (shared by DemuxOpen and AccessOpen)
605 *****************************************************************************/
606 static void GetV4L2Params( demux_sys_t *p_sys, vlc_object_t *p_obj )
608 p_sys->i_video_pts = -1;
610 p_sys->i_selected_standard_id =
611 i_standards_list[var_CreateGetInteger( p_obj, "v4l2-standard" )];
613 p_sys->i_selected_input = var_CreateGetInteger( p_obj, "v4l2-input" );
614 p_sys->i_selected_audio_input =
615 var_CreateGetInteger( p_obj, "v4l2-audio-input" );
617 p_sys->io = var_CreateGetInteger( p_obj, "v4l2-io" );
619 p_sys->i_width = var_CreateGetInteger( p_obj, "v4l2-width" );
620 p_sys->i_height = var_CreateGetInteger( p_obj, "v4l2-height" );
622 var_Create( p_obj, "v4l2-controls-reset", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
624 p_sys->f_fps = var_CreateGetFloat( p_obj, "v4l2-fps" );
625 p_sys->psz_requested_chroma = var_CreateGetString( p_obj, "v4l2-chroma" );
627 p_sys->i_cache = var_CreateGetInteger( p_obj, "v4l2-caching" );
629 p_sys->i_cur_tuner = var_CreateGetInteger( p_obj, "v4l2-tuner" );
630 p_sys->i_frequency = var_CreateGetInteger( p_obj, "v4l2-tuner-frequency" );
631 p_sys->i_audio_mode = var_CreateGetInteger( p_obj, "v4l2-tuner-audio-mode" );
633 p_sys->psz_set_ctrls = var_CreateGetString( p_obj, "v4l2-set-ctrls" );
635 p_sys->psz_device = NULL;
641 /*****************************************************************************
642 * ParseMRL: parse the options contained in the MRL
643 *****************************************************************************/
644 static void ParseMRL( demux_sys_t *p_sys, char *psz_path, vlc_object_t *p_obj )
646 char *psz_dup = strdup( psz_path );
647 char *psz_parser = psz_dup;
649 while( *psz_parser && *psz_parser != ':' )
654 if( *psz_parser == ':' )
659 *psz_parser++ = '\0';
661 if( !strncmp( psz_parser, "standard=", strlen( "standard=" ) ) )
663 psz_parser += strlen( "standard=" );
664 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
666 p_sys->i_selected_standard_id = V4L2_STD_PAL;
667 psz_parser += strlen( "pal" );
669 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
671 p_sys->i_selected_standard_id = V4L2_STD_NTSC;
672 psz_parser += strlen( "ntsc" );
674 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
676 p_sys->i_selected_standard_id = V4L2_STD_SECAM;
677 psz_parser += strlen( "secam" );
679 else if( !strncmp( psz_parser, "default", strlen( "default" ) ) )
681 p_sys->i_selected_standard_id = V4L2_STD_UNKNOWN;
682 psz_parser += strlen( "default" );
686 p_sys->i_selected_standard_id = i_standards_list[strtol( psz_parser, &psz_parser, 0 )];
689 else if( !strncmp( psz_parser, "chroma=", strlen( "chroma=" ) ) )
693 psz_parser += strlen( "chroma=" );
694 if( strchr( psz_parser, ':' ) )
696 i_len = strchr( psz_parser, ':' ) - psz_parser;
700 i_len = strlen( psz_parser );
703 free( p_sys->psz_requested_chroma );
704 p_sys->psz_requested_chroma = strndup( psz_parser, i_len );
708 else if( !strncmp( psz_parser, "input=", strlen( "input=" ) ) )
710 p_sys->i_selected_input = strtol( psz_parser + strlen( "input=" ),
713 else if( !strncmp( psz_parser, "audio-input=", strlen( "audio-input=" ) ) )
715 p_sys->i_selected_audio_input = strtol( psz_parser + strlen( "audio-input=" ),
718 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
720 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
723 else if( !strncmp( psz_parser, "io=", strlen( "io=" ) ) )
725 psz_parser += strlen( "io=" );
726 if( !strncmp( psz_parser, "read", strlen( "read" ) ) )
728 p_sys->io = IO_METHOD_READ;
729 psz_parser += strlen( "read" );
731 else if( !strncmp( psz_parser, "mmap", strlen( "mmap" ) ) )
733 p_sys->io = IO_METHOD_MMAP;
734 psz_parser += strlen( "mmap" );
736 else if( !strncmp( psz_parser, "userptr", strlen( "userptr" ) ) )
738 p_sys->io = IO_METHOD_USERPTR;
739 psz_parser += strlen( "userptr" );
743 p_sys->io = strtol( psz_parser, &psz_parser, 0 );
746 else if( !strncmp( psz_parser, "width=",
747 strlen( "width=" ) ) )
750 strtol( psz_parser + strlen( "width=" ),
753 else if( !strncmp( psz_parser, "height=",
754 strlen( "height=" ) ) )
757 strtol( psz_parser + strlen( "height=" ),
760 else if( !strncmp( psz_parser, "controls-reset",
761 strlen( "controls-reset" ) ) )
763 var_SetBool( p_obj, "v4l2-controls-reset", true );
764 psz_parser += strlen( "controls-reset" );
767 else if( !strncmp( psz_parser, "brightness=",
768 strlen( "brightness=" ) ) )
770 var_SetInteger( p_obj, "brightness",
771 strtol( psz_parser + strlen( "brightness=" ),
774 else if( !strncmp( psz_parser, "contrast=",
775 strlen( "contrast=" ) ) )
777 var_SetInteger( p_obj, "contrast",
778 strtol( psz_parser + strlen( "contrast=" ),
781 else if( !strncmp( psz_parser, "saturation=",
782 strlen( "saturation=" ) ) )
784 var_SetInteger( p_obj, "saturation",
785 strtol( psz_parser + strlen( "saturation=" ),
788 else if( !strncmp( psz_parser, "hue=",
791 var_SetInteger( p_obj, "hue",
792 strtol( psz_parser + strlen( "hue=" ),
795 else if( !strncmp( psz_parser, "gamma=",
796 strlen( "gamma=" ) ) )
798 var_SetInteger( p_obj, "gamma",
799 strtol( psz_parser + strlen( "gamma=" ),
803 else if( !strncmp( psz_parser, "caching=", strlen( "caching=" ) ) )
805 p_sys->i_cache = strtol( psz_parser + strlen( "caching=" ),
808 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
810 p_sys->i_cur_tuner = strtol( psz_parser + strlen( "tuner=" ),
813 else if( !strncmp( psz_parser, "tuner-frequency=", strlen( "tuner-frequency=" ) ) )
815 p_sys->i_frequency = strtol( psz_parser
816 + strlen( "tuner-frequency=" ),
819 else if( !strncmp( psz_parser, "tuner-audio-mode=", strlen( "tuner-audio-mode=" ) ) )
821 p_sys->i_audio_mode = strtol( psz_parser
822 + strlen( "tuner-audio-mode=" ),
825 else if( !strncmp( psz_parser, "set-ctrls=", strlen( "set-ctrls=" )) )
829 psz_parser += strlen( "set-ctrls=" );
830 if( strchr( psz_parser, ':' ) )
832 i_len = strchr( psz_parser, ':' ) - psz_parser;
836 i_len = strlen( psz_parser );
839 p_sys->psz_set_ctrls = strndup( psz_parser, i_len );
843 else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) )
844 || !strncmp( psz_parser, "samplerate=", strlen( "samplerate=" ) )
845 || !strncmp( psz_parser, "audio-method", strlen( "audio-method" ) )
846 || !strncmp( psz_parser, "stereo", strlen( "stereo" ) )
847 || !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
849 if( strchr( psz_parser, ':' ) )
851 psz_parser = strchr( psz_parser, ':' );
855 psz_parser += strlen( psz_parser );
858 msg_Err( p_obj, AUDIO_DEPRECATED_ERROR );
862 char *psz_unk = strchr( psz_parser, ':' );
864 psz_unk = strndup( psz_parser, psz_unk - psz_parser );
866 psz_unk = strdup( psz_parser);
867 msg_Warn( p_obj, "unknown option %s", psz_unk );
871 while( *psz_parser && *psz_parser != ':' )
876 if( *psz_parser == '\0' )
885 p_sys->psz_device = strdup( psz_dup );
887 p_sys->psz_device = strdup( V4L2_DEFAULT );
891 /*****************************************************************************
892 * Close: close device, free resources
893 *****************************************************************************/
894 static void AccessClose( vlc_object_t *p_this )
896 access_t *p_access = (access_t *)p_this;
897 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
899 CommonClose( p_this, p_sys );
902 static void DemuxClose( vlc_object_t *p_this )
904 struct v4l2_buffer buf;
905 enum v4l2_buf_type buf_type;
908 demux_t *p_demux = (demux_t *)p_this;
909 demux_sys_t *p_sys = p_demux->p_sys;
911 /* Stop video capture */
912 if( p_sys->i_fd >= 0 )
921 case IO_METHOD_USERPTR:
922 /* Some drivers 'hang' internally if this is not done before streamoff */
923 for( unsigned int i = 0; i < p_sys->i_nbuffers; i++ )
925 memset( &buf, 0, sizeof(buf) );
926 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
927 buf.memory = ( p_sys->io == IO_METHOD_USERPTR ) ?
928 V4L2_MEMORY_USERPTR : V4L2_MEMORY_MMAP;
929 v4l2_ioctl( p_sys->i_fd, VIDIOC_DQBUF, &buf ); /* ignore result */
932 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
933 if( v4l2_ioctl( p_sys->i_fd, VIDIOC_STREAMOFF, &buf_type ) < 0 ) {
934 msg_Err( p_this, "VIDIOC_STREAMOFF failed" );
941 /* Free Video Buffers */
942 if( p_sys->p_buffers ) {
946 free( p_sys->p_buffers[0].start );
950 for( i = 0; i < p_sys->i_nbuffers; ++i )
952 if( v4l2_munmap( p_sys->p_buffers[i].start, p_sys->p_buffers[i].length ) )
954 msg_Err( p_this, "munmap failed" );
959 case IO_METHOD_USERPTR:
960 for( i = 0; i < p_sys->i_nbuffers; ++i )
962 free( p_sys->p_buffers[i].start );
966 free( p_sys->p_buffers );
969 CommonClose( p_this, p_sys );
972 static void CommonClose( vlc_object_t *p_this, demux_sys_t *p_sys )
976 if( p_sys->i_fd >= 0 ) v4l2_close( p_sys->i_fd );
977 free( p_sys->psz_device );
978 free( p_sys->p_standards );
979 free( p_sys->p_inputs );
980 free( p_sys->p_tuners );
981 free( p_sys->p_codecs );
982 free( p_sys->psz_requested_chroma );
983 free( p_sys->psz_set_ctrls );
988 /*****************************************************************************
989 * AccessOpen: opens v4l2 device, access callback
990 *****************************************************************************
992 * url: <video device>::::
994 *****************************************************************************/
995 static int AccessOpen( vlc_object_t * p_this )
997 access_t *p_access = (access_t*) p_this;
1000 /* Only when selected */
1001 if( *p_access->psz_access == '\0' ) return VLC_EGENERIC;
1003 p_access->pf_read = AccessRead;
1004 p_access->pf_block = NULL;
1005 p_access->pf_seek = NULL;
1006 p_access->pf_control = AccessControl;
1007 p_access->info.i_update = 0;
1008 p_access->info.i_size = 0;
1009 p_access->info.i_pos = 0;
1010 p_access->info.b_eof = false;
1011 p_access->info.i_title = 0;
1012 p_access->info.i_seekpoint = 0;
1014 p_sys = calloc( 1, sizeof( demux_sys_t ) );
1015 p_access->p_sys = (access_sys_t *) p_sys;
1016 if( p_sys == NULL ) return VLC_ENOMEM;
1018 GetV4L2Params( p_sys, (vlc_object_t *) p_access );
1020 ParseMRL( p_sys, p_access->psz_path, (vlc_object_t *) p_access );
1022 if( FindMainDevice( p_this, p_sys, false ) != VLC_SUCCESS )
1024 AccessClose( p_this );
1025 return VLC_EGENERIC;
1031 /*****************************************************************************
1033 *****************************************************************************/
1034 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
1036 demux_sys_t *p_sys = p_demux->p_sys;
1042 /* Special for access_demux */
1043 case DEMUX_CAN_PAUSE:
1044 case DEMUX_CAN_SEEK:
1045 case DEMUX_SET_PAUSE_STATE:
1046 case DEMUX_CAN_CONTROL_PACE:
1047 pb = (bool*)va_arg( args, bool * );
1051 case DEMUX_GET_PTS_DELAY:
1052 pi64 = (int64_t*)va_arg( args, int64_t * );
1053 *pi64 = (int64_t)p_sys->i_cache * 1000;
1056 case DEMUX_GET_TIME:
1057 pi64 = (int64_t*)va_arg( args, int64_t * );
1061 /* TODO implement others */
1063 return VLC_EGENERIC;
1066 return VLC_EGENERIC;
1069 /*****************************************************************************
1070 * AccessControl: access callback
1071 *****************************************************************************/
1072 static int AccessControl( access_t *p_access, int i_query, va_list args )
1077 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1082 case ACCESS_CAN_SEEK:
1083 case ACCESS_CAN_FASTSEEK:
1084 pb_bool = (bool*)va_arg( args, bool* );
1087 case ACCESS_CAN_PAUSE:
1088 pb_bool = (bool*)va_arg( args, bool* );
1091 case ACCESS_CAN_CONTROL_PACE:
1092 pb_bool = (bool*)va_arg( args, bool* );
1097 case ACCESS_GET_MTU:
1098 pi_int = (int*)va_arg( args, int * );
1102 case ACCESS_GET_PTS_DELAY:
1103 pi_64 = (int64_t*)va_arg( args, int64_t * );
1104 *pi_64 = (int64_t) p_sys->i_cache * 1000;
1108 case ACCESS_SET_PAUSE_STATE:
1112 case ACCESS_GET_TITLE_INFO:
1113 case ACCESS_SET_TITLE:
1114 case ACCESS_SET_SEEKPOINT:
1115 case ACCESS_SET_PRIVATE_ID_STATE:
1116 case ACCESS_GET_CONTENT_TYPE:
1117 case ACCESS_GET_META:
1118 return VLC_EGENERIC;
1121 msg_Warn( p_access, "Unimplemented query in control(%d).", i_query);
1122 return VLC_EGENERIC;
1128 /*****************************************************************************
1129 * AccessRead: access callback
1130 ******************************************************************************/
1131 static ssize_t AccessRead( access_t * p_access, uint8_t * p_buffer, size_t i_len )
1133 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
1137 ufd.fd = p_sys->i_fd;
1138 ufd.events = POLLIN;
1140 if( p_access->info.b_eof )
1145 if( !vlc_object_alive (p_access) )
1150 while( ( i_ret = poll( &ufd, 1, 500 ) ) == 0 );
1154 msg_Err( p_access, "Polling error (%m)." );
1158 i_ret = v4l2_read( p_sys->i_fd, p_buffer, i_len );
1161 p_access->info.b_eof = true;
1163 else if( i_ret > 0 )
1165 p_access->info.i_pos += i_ret;
1171 /*****************************************************************************
1172 * Demux: Processes the audio or video frame
1173 *****************************************************************************/
1174 static int Demux( demux_t *p_demux )
1176 demux_sys_t *p_sys = p_demux->p_sys;
1179 fd.fd = p_sys->i_fd;
1180 fd.events = POLLIN|POLLPRI;
1184 if( poll( &fd, 1, 500 ) ) /* Timeout after 0.5 seconds since I don't know if pf_demux can be blocking. */
1186 if( fd.revents & (POLLIN|POLLPRI) )
1188 block_t *p_block = GrabVideo( p_demux );
1191 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
1192 es_out_Send( p_demux->out, p_sys->p_es, p_block );
1200 /*****************************************************************************
1201 * GrabVideo: Grab a video frame
1202 *****************************************************************************/
1203 static block_t* GrabVideo( demux_t *p_demux )
1205 demux_sys_t *p_sys = p_demux->p_sys;
1207 block_t *p_block = NULL;
1208 struct v4l2_buffer buf;
1211 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1213 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1215 /* Did we wait long enough ? (frame rate reduction) */
1216 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1219 /* Grab Video Frame */
1222 case IO_METHOD_READ:
1223 i_ret = v4l2_read( p_sys->i_fd, p_sys->p_buffers[0].start, p_sys->p_buffers[0].length );
1231 /* Could ignore EIO, see spec. */
1234 msg_Err( p_demux, "Failed to read frame" );
1239 p_block = ProcessVideoFrame( p_demux, (uint8_t*)p_sys->p_buffers[0].start, i_ret );
1240 if( !p_block ) return 0;
1244 case IO_METHOD_MMAP:
1245 memset( &buf, 0, sizeof(buf) );
1246 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1247 buf.memory = V4L2_MEMORY_MMAP;
1249 /* Wait for next frame */
1250 if (v4l2_ioctl( p_sys->i_fd, VIDIOC_DQBUF, &buf ) < 0 )
1257 /* Could ignore EIO, see spec. */
1260 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1265 if( buf.index >= p_sys->i_nbuffers ) {
1266 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1270 p_block = ProcessVideoFrame( p_demux, p_sys->p_buffers[buf.index].start, buf.bytesused );
1271 if( !p_block ) return 0;
1274 if( v4l2_ioctl( p_sys->i_fd, VIDIOC_QBUF, &buf ) < 0 )
1276 msg_Err( p_demux, "Failed to unlock (VIDIOC_QBUF)" );
1277 block_Release( p_block );
1283 case IO_METHOD_USERPTR:
1284 memset( &buf, 0, sizeof(buf) );
1285 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1286 buf.memory = V4L2_MEMORY_USERPTR;
1288 /* Wait for next frame */
1289 if (v4l2_ioctl( p_sys->i_fd, VIDIOC_DQBUF, &buf ) < 0 )
1296 /* Could ignore EIO, see spec. */
1299 msg_Err( p_demux, "Failed to wait (VIDIOC_DQBUF)" );
1306 for( i = 0; i < p_sys->i_nbuffers; i++ )
1308 if( buf.m.userptr == (unsigned long)p_sys->p_buffers[i].start &&
1309 buf.length == p_sys->p_buffers[i].length ) break;
1312 if( i >= p_sys->i_nbuffers )
1314 msg_Err( p_demux, "Failed capturing new frame as i>=nbuffers" );
1318 p_block = ProcessVideoFrame( p_demux, (uint8_t*)buf.m.userptr, buf.bytesused );
1319 if( !p_block ) return 0;
1322 if( v4l2_ioctl( p_sys->i_fd, VIDIOC_QBUF, &buf ) < 0 )
1324 msg_Err( p_demux, "Failed to unlock (VIDIOC_QBUF)" );
1325 block_Release( p_block );
1334 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();
1339 /*****************************************************************************
1340 * ProcessVideoFrame: Helper function to take a buffer and copy it into
1342 *****************************************************************************/
1343 static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t i_size )
1347 if( !p_frame ) return 0;
1350 if( !( p_block = block_New( p_demux, i_size ) ) )
1352 msg_Warn( p_demux, "Cannot get new block" );
1357 memcpy( p_block->p_buffer, p_frame, i_size );
1362 /*****************************************************************************
1363 * Helper function to initalise video IO using the Read method
1364 *****************************************************************************/
1365 static int InitRead( demux_t *p_demux, unsigned int i_buffer_size )
1367 demux_sys_t *p_sys = p_demux->p_sys;
1369 p_sys->p_buffers = calloc( 1, sizeof( *p_sys->p_buffers ) );
1370 if( !p_sys->p_buffers )
1371 return VLC_EGENERIC;
1373 p_sys->p_buffers[0].length = i_buffer_size;
1374 p_sys->p_buffers[0].start = malloc( i_buffer_size );
1375 if( !p_sys->p_buffers[0].start )
1381 /*****************************************************************************
1382 * Helper function to initalise video IO using the mmap method
1383 *****************************************************************************/
1384 static int InitMmap( demux_t *p_demux, int i_fd )
1386 demux_sys_t *p_sys = p_demux->p_sys;
1387 struct v4l2_requestbuffers req;
1389 memset( &req, 0, sizeof(req) );
1391 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1392 req.memory = V4L2_MEMORY_MMAP;
1394 if( v4l2_ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1396 msg_Err( p_demux, "device does not support mmap i/o" );
1402 msg_Err( p_demux, "Insufficient buffer memory" );
1406 p_sys->p_buffers = calloc( req.count, sizeof( *p_sys->p_buffers ) );
1407 if( !p_sys->p_buffers )
1410 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < req.count; ++p_sys->i_nbuffers )
1412 struct v4l2_buffer buf;
1414 memset( &buf, 0, sizeof(buf) );
1415 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1416 buf.memory = V4L2_MEMORY_MMAP;
1417 buf.index = p_sys->i_nbuffers;
1419 if( v4l2_ioctl( i_fd, VIDIOC_QUERYBUF, &buf ) < 0 )
1421 msg_Err( p_demux, "VIDIOC_QUERYBUF" );
1425 p_sys->p_buffers[p_sys->i_nbuffers].length = buf.length;
1426 p_sys->p_buffers[p_sys->i_nbuffers].start =
1427 v4l2_mmap( NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, buf.m.offset );
1429 if( p_sys->p_buffers[p_sys->i_nbuffers].start == MAP_FAILED )
1431 msg_Err( p_demux, "mmap failed (%m)" );
1439 return VLC_EGENERIC;
1443 /*****************************************************************************
1444 * Helper function to initalise video IO using the userbuf method
1445 *****************************************************************************/
1446 static int InitUserP( demux_t *p_demux, int i_fd, unsigned int i_buffer_size )
1448 demux_sys_t *p_sys = p_demux->p_sys;
1449 struct v4l2_requestbuffers req;
1450 unsigned int i_page_size;
1452 i_page_size = sysconf(_SC_PAGESIZE);
1453 i_buffer_size = ( i_buffer_size + i_page_size - 1 ) & ~( i_page_size - 1);
1455 memset( &req, 0, sizeof(req) );
1457 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1458 req.memory = V4L2_MEMORY_USERPTR;
1460 if( v4l2_ioctl( i_fd, VIDIOC_REQBUFS, &req ) < 0 )
1462 msg_Err( p_demux, "device does not support user pointer i/o" );
1463 return VLC_EGENERIC;
1466 p_sys->p_buffers = calloc( 4, sizeof( *p_sys->p_buffers ) );
1467 if( !p_sys->p_buffers )
1470 for( p_sys->i_nbuffers = 0; p_sys->i_nbuffers < 4; ++p_sys->i_nbuffers )
1472 p_sys->p_buffers[p_sys->i_nbuffers].length = i_buffer_size;
1473 if( posix_memalign( &p_sys->p_buffers[p_sys->i_nbuffers].start,
1474 /* boundary */ i_page_size, i_buffer_size ) )
1481 free( p_sys->p_buffers );
1482 return VLC_EGENERIC;
1486 /*****************************************************************************
1487 * IsPixelFormatSupported: returns true if the specified V4L2 pixel format is
1488 * in the array of supported formats returned by the driver
1489 *****************************************************************************/
1490 static bool IsPixelFormatSupported( demux_t *p_demux, unsigned int i_pixelformat )
1492 demux_sys_t *p_sys = p_demux->p_sys;
1494 for( int i_index = 0; i_index < p_sys->i_codec; i_index++ )
1496 if( p_sys->p_codecs[i_index].pixelformat == i_pixelformat )
1503 /*****************************************************************************
1504 * OpenVideoDev: open and set up the video device and probe for capabilities
1505 *****************************************************************************/
1506 static int OpenVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys, bool b_demux )
1509 struct v4l2_cropcap cropcap;
1510 struct v4l2_crop crop;
1511 struct v4l2_format fmt;
1513 enum v4l2_buf_type buf_type;
1514 const char *psz_device = p_sys->psz_device;
1518 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1520 msg_Err( p_obj, "cannot open device (%m)" );
1525 /* Note the v4l2_xxx functions are designed so that if they get passed an
1526 unknown fd, the will behave exactly as their regular xxx counterparts,
1527 so if v4l2_fd_open fails, we continue as normal (missing the libv4l2
1528 custom cam format to normal formats conversion). Chances are big we will
1529 still fail then though, as normally v4l2_fd_open only fails if the
1530 device is not a v4l2 device. */
1531 libv4l2_fd = v4l2_fd_open( i_fd, V4L2_ENABLE_ENUM_FMT_EMULATION );
1532 if( libv4l2_fd != -1 )
1538 /* Tune the tuner */
1539 if( p_sys->i_frequency >= 0 )
1541 if( p_sys->i_cur_tuner < 0 || p_sys->i_cur_tuner >= p_sys->i_tuner )
1543 msg_Err( p_obj, "invalid tuner %d.", p_sys->i_cur_tuner );
1546 struct v4l2_frequency frequency;
1547 memset( &frequency, 0, sizeof( frequency ) );
1548 frequency.tuner = p_sys->i_cur_tuner;
1549 frequency.type = p_sys->p_tuners[p_sys->i_cur_tuner].type;
1550 frequency.frequency = p_sys->i_frequency / 62.5;
1551 if( v4l2_ioctl( i_fd, VIDIOC_S_FREQUENCY, &frequency ) < 0 )
1553 msg_Err( p_obj, "cannot set tuner frequency (%m)" );
1556 msg_Dbg( p_obj, "Tuner frequency set" );
1559 /* Set the tuner's audio mode */
1560 if( p_sys->i_audio_mode >= 0 )
1562 if( p_sys->i_cur_tuner < 0 || p_sys->i_cur_tuner >= p_sys->i_tuner )
1564 msg_Err( p_obj, "invalid tuner %d.", p_sys->i_cur_tuner );
1567 struct v4l2_tuner tuner;
1568 memset( &tuner, 0, sizeof( tuner ) );
1569 tuner.index = p_sys->i_cur_tuner;
1570 tuner.audmode = p_sys->i_audio_mode;
1571 if( v4l2_ioctl( i_fd, VIDIOC_S_TUNER, &tuner ) < 0 )
1573 msg_Err( p_obj, "cannot set tuner audio mode (%m)" );
1576 msg_Dbg( p_obj, "Tuner audio mode set" );
1579 /* Select standard */
1581 if( p_sys->i_selected_standard_id != V4L2_STD_UNKNOWN )
1583 if( v4l2_ioctl( i_fd, VIDIOC_S_STD, &p_sys->i_selected_standard_id ) < 0 )
1585 msg_Err( p_obj, "cannot set standard (%m)" );
1588 msg_Dbg( p_obj, "Set standard" );
1593 if( p_sys->i_selected_input >= p_sys->i_input )
1595 msg_Warn( p_obj, "invalid input. Using the default one" );
1596 p_sys->i_selected_input = 0;
1599 if( v4l2_ioctl( i_fd, VIDIOC_S_INPUT, &p_sys->i_selected_input ) < 0 )
1601 msg_Err( p_obj, "cannot set input (%m)" );
1605 /* Set audio input */
1607 if( p_sys->i_audio > 0 )
1609 if( p_sys->i_selected_audio_input < 0
1610 || p_sys->i_selected_audio_input >= p_sys->i_audio )
1612 msg_Warn( p_obj, "invalid audio input. Using the default one" );
1613 p_sys->i_selected_audio_input = 0;
1616 if( v4l2_ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_selected_audio_input] ) < 0 )
1618 msg_Err( p_obj, "cannot set audio input (%m)" );
1621 msg_Dbg( p_obj, "Audio input set to %d", p_sys->i_selected_audio_input );
1625 /* TODO: Move the resolution stuff up here */
1626 /* if MPEG encoder card, no need to do anything else after this */
1627 ControlList( p_obj, i_fd,
1628 var_GetBool( p_obj, "v4l2-controls-reset" ), b_demux );
1629 SetAvailControlsByString( p_obj, p_sys, i_fd );
1630 if( false == b_demux)
1635 demux_t *p_demux = (demux_t *) p_obj;
1637 /* Verify device support for the various IO methods */
1640 case IO_METHOD_READ:
1641 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE) )
1643 msg_Err( p_demux, "device does not support read i/o" );
1648 case IO_METHOD_MMAP:
1649 case IO_METHOD_USERPTR:
1650 if( !(p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING) )
1652 msg_Err( p_demux, "device does not support streaming i/o" );
1658 msg_Err( p_demux, "io method not supported" );
1662 /* Reset Cropping */
1663 memset( &cropcap, 0, sizeof(cropcap) );
1664 cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1665 if( v4l2_ioctl( i_fd, VIDIOC_CROPCAP, &cropcap ) >= 0 )
1667 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1668 crop.c = cropcap.defrect; /* reset to default */
1669 if( v4l2_ioctl( i_fd, VIDIOC_S_CROP, &crop ) < 0 )
1674 /* Cropping not supported. */
1677 /* Errors ignored. */
1683 /* Try and find default resolution if not specified */
1684 memset( &fmt, 0, sizeof(fmt) );
1685 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1687 if( p_sys->i_width <= 0 || p_sys->i_height <= 0 )
1689 if( v4l2_ioctl( i_fd, VIDIOC_G_FMT, &fmt ) < 0 )
1691 msg_Err( p_demux, "Cannot get default width and height." );
1695 p_sys->i_width = fmt.fmt.pix.width;
1696 p_sys->i_height = fmt.fmt.pix.height;
1698 if( fmt.fmt.pix.field == V4L2_FIELD_ALTERNATE )
1700 p_sys->i_height = p_sys->i_height * 2;
1705 msg_Dbg( p_demux, "trying specified size %dx%d", p_sys->i_width, p_sys->i_height );
1708 fmt.fmt.pix.width = p_sys->i_width;
1709 fmt.fmt.pix.height = p_sys->i_height;
1710 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
1712 /* Test and set Chroma */
1713 fmt.fmt.pix.pixelformat = 0;
1714 if( p_sys->psz_requested_chroma && strlen( p_sys->psz_requested_chroma ) > 0 )
1716 /* User specified chroma */
1717 if( strlen( p_sys->psz_requested_chroma ) >= 4 )
1719 int i_requested_fourcc = VLC_FOURCC(
1720 p_sys->psz_requested_chroma[0], p_sys->psz_requested_chroma[1],
1721 p_sys->psz_requested_chroma[2], p_sys->psz_requested_chroma[3] );
1722 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
1724 if( v4l2chroma_to_fourcc[i].i_fourcc == i_requested_fourcc )
1726 fmt.fmt.pix.pixelformat = v4l2chroma_to_fourcc[i].i_v4l2;
1731 /* Try and set user chroma */
1732 if( !IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat ) || ( fmt.fmt.pix.pixelformat && v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) < 0 ) )
1734 msg_Warn( p_demux, "Driver is unable to use specified chroma %s. Trying defaults.", p_sys->psz_requested_chroma );
1735 fmt.fmt.pix.pixelformat = 0;
1739 /* If no user specified chroma, find best */
1740 /* This also decides if MPEG encoder card or not */
1741 if( !fmt.fmt.pix.pixelformat )
1744 for( i = 0; i < ARRAY_SIZE( p_chroma_fallbacks ); i++ )
1746 fmt.fmt.pix.pixelformat = p_chroma_fallbacks[i];
1747 if( IsPixelFormatSupported( p_demux, fmt.fmt.pix.pixelformat )
1748 && v4l2_ioctl( i_fd, VIDIOC_S_FMT, &fmt ) >= 0 )
1751 if( i == ARRAY_SIZE( p_chroma_fallbacks ) )
1753 msg_Warn( p_demux, "Could not select any of the default chromas; attempting to open as MPEG encoder card (access)" );
1758 /* Reassign width, height and chroma incase driver override */
1759 p_sys->i_width = fmt.fmt.pix.width;
1760 p_sys->i_height = fmt.fmt.pix.height;
1762 /* Look up final fourcc */
1763 p_sys->i_fourcc = 0;
1764 for( int i = 0; v4l2chroma_to_fourcc[i].i_fourcc != 0; i++ )
1766 if( v4l2chroma_to_fourcc[i].i_v4l2 == fmt.fmt.pix.pixelformat )
1768 p_sys->i_fourcc = v4l2chroma_to_fourcc[i].i_fourcc;
1769 es_format_Init( &es_fmt, VIDEO_ES, p_sys->i_fourcc );
1770 es_fmt.video.i_rmask = v4l2chroma_to_fourcc[i].i_rmask;
1771 es_fmt.video.i_gmask = v4l2chroma_to_fourcc[i].i_gmask;
1772 es_fmt.video.i_bmask = v4l2chroma_to_fourcc[i].i_bmask;
1777 /* Buggy driver paranoia */
1778 i_min = fmt.fmt.pix.width * 2;
1779 if( fmt.fmt.pix.bytesperline < i_min )
1780 fmt.fmt.pix.bytesperline = i_min;
1781 i_min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
1782 if( fmt.fmt.pix.sizeimage < i_min )
1783 fmt.fmt.pix.sizeimage = i_min;
1785 #ifdef VIDIOC_ENUM_FRAMEINTERVALS
1786 /* This is new in Linux 2.6.19 */
1787 /* List supported frame rates */
1788 struct v4l2_frmivalenum frmival;
1790 frmival.pixel_format = fmt.fmt.pix.pixelformat;
1791 frmival.width = p_sys->i_width;
1792 frmival.height = p_sys->i_height;
1793 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 )
1796 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
1797 vlc_fourcc_to_char( p_sys->i_fourcc, &psz_fourcc );
1798 msg_Dbg( p_demux, "supported frame intervals for %4s, %dx%d:",
1799 psz_fourcc, frmival.width, frmival.height );
1800 switch( frmival.type )
1802 case V4L2_FRMIVAL_TYPE_DISCRETE:
1805 msg_Dbg( p_demux, " supported frame interval: %d/%d",
1806 frmival.discrete.numerator,
1807 frmival.discrete.denominator );
1809 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival ) >= 0 );
1811 case V4L2_FRMIVAL_TYPE_STEPWISE:
1812 msg_Dbg( p_demux, " supported frame intervals: %d/%d to "
1813 "%d/%d using %d/%d increments",
1814 frmival.stepwise.min.numerator,
1815 frmival.stepwise.min.denominator,
1816 frmival.stepwise.max.numerator,
1817 frmival.stepwise.max.denominator,
1818 frmival.stepwise.step.numerator,
1819 frmival.stepwise.step.denominator );
1821 case V4L2_FRMIVAL_TYPE_CONTINUOUS:
1822 msg_Dbg( p_demux, " supported frame intervals: %d/%d to %d/%d",
1823 frmival.stepwise.min.numerator,
1824 frmival.stepwise.min.denominator,
1825 frmival.stepwise.max.numerator,
1826 frmival.stepwise.max.denominator );
1832 /* Init IO method */
1835 case IO_METHOD_READ:
1836 if( InitRead( p_demux, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
1839 case IO_METHOD_MMAP:
1840 if( InitMmap( p_demux, i_fd ) != VLC_SUCCESS ) goto open_failed;
1843 case IO_METHOD_USERPTR:
1844 if( InitUserP( p_demux, i_fd, fmt.fmt.pix.sizeimage ) != VLC_SUCCESS ) goto open_failed;
1850 es_fmt.video.i_width = p_sys->i_width;
1851 es_fmt.video.i_height = p_sys->i_height;
1852 es_fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
1854 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
1855 (char*)&es_fmt.i_codec, es_fmt.video.i_width, es_fmt.video.i_height );
1856 p_sys->p_es = es_out_Add( p_demux->out, &es_fmt );
1862 case IO_METHOD_READ:
1866 case IO_METHOD_MMAP:
1867 for (unsigned int i = 0; i < p_sys->i_nbuffers; ++i)
1869 struct v4l2_buffer buf;
1871 memset( &buf, 0, sizeof(buf) );
1872 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1873 buf.memory = V4L2_MEMORY_MMAP;
1876 if( v4l2_ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
1878 msg_Err( p_demux, "VIDIOC_QBUF failed" );
1883 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1884 if( v4l2_ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
1886 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
1892 case IO_METHOD_USERPTR:
1893 for( unsigned int i = 0; i < p_sys->i_nbuffers; ++i )
1895 struct v4l2_buffer buf;
1897 memset( &buf, 0, sizeof(buf) );
1898 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1899 buf.memory = V4L2_MEMORY_USERPTR;
1901 buf.m.userptr = (unsigned long)p_sys->p_buffers[i].start;
1902 buf.length = p_sys->p_buffers[i].length;
1904 if( v4l2_ioctl( i_fd, VIDIOC_QBUF, &buf ) < 0 )
1906 msg_Err( p_demux, "VIDIOC_QBUF failed" );
1911 buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1912 if( v4l2_ioctl( i_fd, VIDIOC_STREAMON, &buf_type ) < 0 )
1914 msg_Err( p_demux, "VIDIOC_STREAMON failed" );
1922 if( p_sys->f_fps >= 0.1 )
1924 msg_Dbg( p_demux, "User set fps=%f", p_sys->f_fps );
1930 if( i_fd >= 0 ) v4l2_close( i_fd );
1935 /*****************************************************************************
1936 * ProbeVideoDev: probe video for capabilities
1937 *****************************************************************************/
1938 static bool ProbeVideoDev( vlc_object_t *p_obj, demux_sys_t *p_sys,
1939 const char *psz_device )
1947 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
1949 msg_Err( p_obj, "cannot open video device '%s' (%m)", psz_device );
1954 /* Note the v4l2_xxx functions are designed so that if they get passed an
1955 unknown fd, the will behave exactly as their regular xxx counterparts,
1956 so if v4l2_fd_open fails, we continue as normal (missing the libv4l2
1957 custom cam format to normal formats conversion). Chances are big we will
1958 still fail then though, as normally v4l2_fd_open only fails if the
1959 device is not a v4l2 device. */
1960 libv4l2_fd = v4l2_fd_open( i_fd, V4L2_ENABLE_ENUM_FMT_EMULATION );
1961 if( libv4l2_fd != -1 )
1967 /* Get device capabilites */
1969 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCAP, &p_sys->dev_cap ) < 0 )
1971 msg_Err( p_obj, "cannot get video capabilities (%m)" );
1975 msg_Dbg( p_obj, "V4L2 device: %s using driver: %s (version: %u.%u.%u) on %s",
1976 p_sys->dev_cap.card,
1977 p_sys->dev_cap.driver,
1978 (p_sys->dev_cap.version >> 16) & 0xFF,
1979 (p_sys->dev_cap.version >> 8) & 0xFF,
1980 p_sys->dev_cap.version & 0xFF,
1981 p_sys->dev_cap.bus_info );
1983 msg_Dbg( p_obj, "the device has the capabilities: (%c) Video Capure, "
1986 ( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE ? 'X':' '),
1987 ( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO ? 'X':' '),
1988 ( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER ? 'X':' ') );
1990 msg_Dbg( p_obj, "supported I/O methods are: (%c) Read/Write, "
1992 "(%c) Asynchronous",
1993 ( p_sys->dev_cap.capabilities & V4L2_CAP_READWRITE ? 'X':' ' ),
1994 ( p_sys->dev_cap.capabilities & V4L2_CAP_STREAMING ? 'X':' ' ),
1995 ( p_sys->dev_cap.capabilities & V4L2_CAP_ASYNCIO ? 'X':' ' ) );
1997 /* Now, enumerate all the video inputs. This is useless at the moment
1998 since we have no way to present that info to the user except with
2001 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2003 struct v4l2_input t_input;
2005 while( v4l2_ioctl( i_fd, VIDIOC_ENUMINPUT, &t_input ) >= 0 )
2008 t_input.index = p_sys->i_input;
2011 p_sys->p_inputs = calloc( 1, p_sys->i_input * sizeof( struct v4l2_input ) );
2012 if( !p_sys->p_inputs ) goto open_failed;
2014 for( i_index = 0; i_index < p_sys->i_input; i_index++ )
2016 p_sys->p_inputs[i_index].index = i_index;
2018 if( v4l2_ioctl( i_fd, VIDIOC_ENUMINPUT, &p_sys->p_inputs[i_index] ) )
2020 msg_Err( p_obj, "cannot get video input characteristics (%m)" );
2023 msg_Dbg( p_obj, "video input %i (%s) has type: %s %c",
2025 p_sys->p_inputs[i_index].name,
2026 p_sys->p_inputs[i_index].type
2027 == V4L2_INPUT_TYPE_TUNER ?
2029 "External analog input",
2030 i_index == p_sys->i_selected_input ? '*' : ' ' );
2034 /* Probe video standards */
2035 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2037 struct v4l2_standard t_standards;
2038 t_standards.index = 0;
2039 while( v4l2_ioctl( i_fd, VIDIOC_ENUMSTD, &t_standards ) >=0 )
2041 p_sys->i_standard++;
2042 t_standards.index = p_sys->i_standard;
2045 p_sys->p_standards = calloc( 1, p_sys->i_standard * sizeof( struct v4l2_standard ) );
2046 if( !p_sys->p_standards ) goto open_failed;
2048 for( i_standard = 0; i_standard < p_sys->i_standard; i_standard++ )
2050 p_sys->p_standards[i_standard].index = i_standard;
2052 if( v4l2_ioctl( i_fd, VIDIOC_ENUMSTD, &p_sys->p_standards[i_standard] ) )
2054 msg_Err( p_obj, "cannot get video input standards (%m)" );
2057 msg_Dbg( p_obj, "video standard %i is: %s %c",
2059 p_sys->p_standards[i_standard].name,
2060 (unsigned)i_standard == p_sys->i_selected_standard_id ? '*' : ' ' );
2064 /* initialize the structures for the ioctls */
2065 for( i_index = 0; i_index < 32; i_index++ )
2067 p_sys->p_audios[i_index].index = i_index;
2070 /* Probe audio inputs */
2071 if( p_sys->dev_cap.capabilities & V4L2_CAP_AUDIO )
2073 while( p_sys->i_audio < 32 &&
2074 v4l2_ioctl( i_fd, VIDIOC_S_AUDIO, &p_sys->p_audios[p_sys->i_audio] ) >= 0 )
2076 if( v4l2_ioctl( i_fd, VIDIOC_G_AUDIO, &p_sys->p_audios[ p_sys->i_audio] ) < 0 )
2078 msg_Err( p_obj, "cannot get audio input characteristics (%m)" );
2082 msg_Dbg( p_obj, "audio input %i (%s) is %s %s %c",
2084 p_sys->p_audios[p_sys->i_audio].name,
2085 p_sys->p_audios[p_sys->i_audio].capability &
2086 V4L2_AUDCAP_STEREO ?
2088 p_sys->p_audios[p_sys->i_audio].capability &
2090 "(Automatic Volume Level supported)" : "",
2091 p_sys->i_audio == p_sys->i_selected_audio_input ? '*' : ' ' );
2097 /* List tuner caps */
2098 if( p_sys->dev_cap.capabilities & V4L2_CAP_TUNER )
2100 struct v4l2_tuner tuner;
2101 memset( &tuner, 0, sizeof(tuner) );
2102 while( v4l2_ioctl( i_fd, VIDIOC_G_TUNER, &tuner ) >= 0 )
2105 memset( &tuner, 0, sizeof(tuner) );
2106 tuner.index = p_sys->i_tuner;
2109 p_sys->p_tuners = calloc( 1, p_sys->i_tuner * sizeof( struct v4l2_tuner ) );
2110 if( !p_sys->p_tuners ) goto open_failed;
2112 for( i_index = 0; i_index < p_sys->i_tuner; i_index++ )
2114 p_sys->p_tuners[i_index].index = i_index;
2116 if( v4l2_ioctl( i_fd, VIDIOC_G_TUNER, &p_sys->p_tuners[i_index] ) )
2118 msg_Err( p_obj, "cannot get tuner characteristics (%m)" );
2121 msg_Dbg( p_obj, "tuner %i (%s) has type: %s, "
2122 "frequency range: %.1f %s -> %.1f %s",
2124 p_sys->p_tuners[i_index].name,
2125 p_sys->p_tuners[i_index].type
2126 == V4L2_TUNER_RADIO ?
2127 "Radio" : "Analog TV",
2128 p_sys->p_tuners[i_index].rangelow * 62.5,
2129 p_sys->p_tuners[i_index].capability &
2130 V4L2_TUNER_CAP_LOW ?
2132 p_sys->p_tuners[i_index].rangehigh * 62.5,
2133 p_sys->p_tuners[i_index].capability &
2134 V4L2_TUNER_CAP_LOW ?
2137 struct v4l2_frequency frequency;
2138 memset( &frequency, 0, sizeof( frequency ) );
2139 if( v4l2_ioctl( i_fd, VIDIOC_G_FREQUENCY, &frequency ) < 0 )
2141 msg_Err( p_obj, "cannot get tuner frequency (%m)" );
2144 msg_Dbg( p_obj, "tuner %i (%s) frequency: %.1f %s",
2146 p_sys->p_tuners[i_index].name,
2147 frequency.frequency * 62.5,
2148 p_sys->p_tuners[i_index].capability &
2149 V4L2_TUNER_CAP_LOW ?
2154 /* Probe for available chromas */
2155 if( p_sys->dev_cap.capabilities & V4L2_CAP_VIDEO_CAPTURE )
2157 struct v4l2_fmtdesc codec;
2160 memset( &codec, 0, sizeof(codec) );
2161 codec.index = i_index;
2162 codec.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2164 while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FMT, &codec ) >= 0 )
2167 codec.index = i_index;
2170 p_sys->i_codec = i_index;
2172 p_sys->p_codecs = calloc( 1, p_sys->i_codec * sizeof( struct v4l2_fmtdesc ) );
2174 for( i_index = 0; i_index < p_sys->i_codec; i_index++ )
2176 p_sys->p_codecs[i_index].index = i_index;
2177 p_sys->p_codecs[i_index].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2179 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FMT, &p_sys->p_codecs[i_index] ) < 0 )
2181 msg_Err( p_obj, "cannot get codec description (%m)" );
2185 /* only print if vlc supports the format */
2186 char psz_fourcc_v4l2[5];
2187 memset( &psz_fourcc_v4l2, 0, sizeof( psz_fourcc_v4l2 ) );
2188 vlc_fourcc_to_char( p_sys->p_codecs[i_index].pixelformat,
2190 bool b_codec_supported = false;
2191 for( int i = 0; v4l2chroma_to_fourcc[i].i_v4l2 != 0; i++ )
2193 if( v4l2chroma_to_fourcc[i].i_v4l2 == p_sys->p_codecs[i_index].pixelformat )
2195 b_codec_supported = true;
2198 memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
2199 vlc_fourcc_to_char( v4l2chroma_to_fourcc[i].i_fourcc,
2201 msg_Dbg( p_obj, "device supports chroma %4s [%s, %s]",
2203 p_sys->p_codecs[i_index].description,
2206 #ifdef VIDIOC_ENUM_FRAMESIZES
2207 /* This is new in Linux 2.6.19 */
2208 /* List valid frame sizes for this format */
2209 struct v4l2_frmsizeenum frmsize;
2211 frmsize.pixel_format = p_sys->p_codecs[i_index].pixelformat;
2212 if( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) < 0 )
2214 /* Not all devices support this ioctl */
2215 msg_Warn( p_obj, "Unable to query for frame sizes" );
2219 switch( frmsize.type )
2221 case V4L2_FRMSIZE_TYPE_DISCRETE:
2225 " device supports size %dx%d",
2226 frmsize.discrete.width, frmsize.discrete.height );
2228 } while( v4l2_ioctl( i_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize ) >= 0 );
2230 case V4L2_FRMSIZE_TYPE_STEPWISE:
2232 " device supports sizes %dx%d to %dx%d using %dx%d increments",
2233 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2234 frmsize.stepwise.max_width, frmsize.stepwise.max_height,
2235 frmsize.stepwise.step_width, frmsize.stepwise.step_height );
2237 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
2239 " device supports all sizes %dx%d to %dx%d",
2240 frmsize.stepwise.min_width, frmsize.stepwise.min_height,
2241 frmsize.stepwise.max_width, frmsize.stepwise.max_height );
2248 if( !b_codec_supported )
2251 "device codec %4s (%s) not supported as access_demux",
2253 p_sys->p_codecs[i_index].description );
2260 if( i_fd >= 0 ) v4l2_close( i_fd );
2265 if( i_fd >= 0 ) v4l2_close( i_fd );
2270 static void name2var( unsigned char *name )
2272 for( ; *name; name++ )
2273 *name = (*name == ' ') ? '_' : tolower( *name );
2276 /*****************************************************************************
2277 * Print a user-class v4l2 control's details, create the relevant variable,
2278 * change the value if needed.
2279 *****************************************************************************/
2280 static void ControlListPrint( vlc_object_t *p_obj, int i_fd,
2281 struct v4l2_queryctrl queryctrl,
2282 bool b_reset, bool b_demux )
2284 struct v4l2_querymenu querymenu;
2291 vlc_value_t val, val2;
2293 if( queryctrl.flags & V4L2_CTRL_FLAG_GRABBED )
2294 msg_Dbg( p_obj, " control is busy" );
2295 if( queryctrl.flags & V4L2_CTRL_FLAG_READ_ONLY )
2296 msg_Dbg( p_obj, " control is read-only" );
2298 for( i = 0; controls[i].psz_name != NULL; i++ )
2299 if( controls[i].i_cid == queryctrl.id ) break;
2301 if( controls[i].psz_name )
2303 psz_name = strdup( controls[i].psz_name );
2304 char psz_cfg_name[40];
2305 sprintf( psz_cfg_name, CFG_PREFIX "%s", psz_name );
2306 i_val = var_CreateGetInteger( p_obj, psz_cfg_name );
2307 var_Destroy( p_obj, psz_cfg_name );
2311 psz_name = strdup( (const char *)queryctrl.name );
2312 name2var( (unsigned char *)psz_name );
2316 switch( queryctrl.type )
2318 case V4L2_CTRL_TYPE_INTEGER:
2319 msg_Dbg( p_obj, " integer control" );
2321 " valid values: %d to %d by steps of %d",
2322 queryctrl.minimum, queryctrl.maximum,
2325 var_Create( p_obj, psz_name,
2326 VLC_VAR_INTEGER | VLC_VAR_HASMIN | VLC_VAR_HASMAX
2327 | VLC_VAR_HASSTEP | VLC_VAR_ISCOMMAND );
2328 val.i_int = queryctrl.minimum;
2329 var_Change( p_obj, psz_name, VLC_VAR_SETMIN, &val, NULL );
2330 val.i_int = queryctrl.maximum;
2331 var_Change( p_obj, psz_name, VLC_VAR_SETMAX, &val, NULL );
2332 val.i_int = queryctrl.step;
2333 var_Change( p_obj, psz_name, VLC_VAR_SETSTEP, &val, NULL );
2335 case V4L2_CTRL_TYPE_BOOLEAN:
2336 msg_Dbg( p_obj, " boolean control" );
2337 var_Create( p_obj, psz_name,
2338 VLC_VAR_BOOL | VLC_VAR_ISCOMMAND );
2340 case V4L2_CTRL_TYPE_MENU:
2341 msg_Dbg( p_obj, " menu control" );
2342 var_Create( p_obj, psz_name,
2343 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE
2344 | VLC_VAR_ISCOMMAND );
2345 memset( &querymenu, 0, sizeof( querymenu ) );
2346 for( i_mid = queryctrl.minimum;
2347 i_mid <= (unsigned)queryctrl.maximum;
2350 querymenu.index = i_mid;
2351 querymenu.id = queryctrl.id;
2352 if( v4l2_ioctl( i_fd, VIDIOC_QUERYMENU, &querymenu ) >= 0 )
2354 msg_Dbg( p_obj, " %d: %s",
2355 querymenu.index, querymenu.name );
2356 val.i_int = querymenu.index;
2357 val2.psz_string = (char *)querymenu.name;
2358 var_Change( p_obj, psz_name,
2359 VLC_VAR_ADDCHOICE, &val, &val2 );
2363 case V4L2_CTRL_TYPE_BUTTON:
2364 msg_Dbg( p_obj, " button control" );
2365 var_Create( p_obj, psz_name,
2366 VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2368 case V4L2_CTRL_TYPE_CTRL_CLASS:
2369 msg_Dbg( p_obj, " control class" );
2370 var_Create( p_obj, psz_name, VLC_VAR_VOID );
2373 msg_Dbg( p_obj, " unknown control type (FIXME)" );
2378 switch( queryctrl.type )
2380 case V4L2_CTRL_TYPE_INTEGER:
2381 case V4L2_CTRL_TYPE_BOOLEAN:
2382 case V4L2_CTRL_TYPE_MENU:
2384 struct v4l2_control control;
2385 msg_Dbg( p_obj, " default value: %d",
2386 queryctrl.default_value );
2387 memset( &control, 0, sizeof( control ) );
2388 control.id = queryctrl.id;
2389 if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
2391 msg_Dbg( p_obj, " current value: %d", control.value );
2395 i_val = control.value;
2396 if( b_reset && queryctrl.default_value != control.value )
2398 msg_Dbg( p_obj, " reset value to default" );
2399 Control( p_obj, i_fd, psz_name,
2400 queryctrl.id, queryctrl.default_value );
2405 Control( p_obj, i_fd, psz_name,
2406 queryctrl.id, i_val );
2414 val.psz_string = (char *)queryctrl.name;
2415 var_Change( p_obj, psz_name, VLC_VAR_SETTEXT, &val, NULL );
2416 val.i_int = queryctrl.id;
2417 val2.psz_string = (char *)psz_name;
2418 var_Change( p_obj, "allcontrols", VLC_VAR_ADDCHOICE, &val, &val2 );
2419 /* bad things happen changing MPEG mid-stream
2420 * so don't add to Ext Settings GUI */
2421 if( V4L2_CTRL_ID2CLASS( queryctrl.id ) != V4L2_CTRL_CLASS_MPEG )
2422 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
2424 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
2427 var_SetBool( p_obj, psz_name, i_val );
2429 case VLC_VAR_INTEGER:
2430 var_SetInteger( p_obj, psz_name, i_val );
2435 msg_Warn( p_obj, "FIXME: %s %s %d", __FILE__, __func__,
2441 var_AddCallback( p_obj, psz_name,
2442 DemuxControlCallback, (void*)(intptr_t)queryctrl.id );
2444 var_AddCallback( p_obj, psz_name,
2445 AccessControlCallback, (void*)(intptr_t)queryctrl.id );
2450 /*****************************************************************************
2451 * List all user-class v4l2 controls, set them to the user specified
2452 * value and create the relevant variables to enable runtime changes
2453 *****************************************************************************/
2454 static int ControlList( vlc_object_t *p_obj, int i_fd,
2455 bool b_reset, bool b_demux )
2457 struct v4l2_queryctrl queryctrl;
2460 memset( &queryctrl, 0, sizeof( queryctrl ) );
2462 /* A list of available controls (aka the variable name) will be
2463 * stored as choices in the "allcontrols" variable. We'll thus be able
2464 * to use those to create an appropriate interface
2465 * A list of available controls that can be changed mid-stream will
2466 * be stored in the "controls" variable */
2467 var_Create( p_obj, "controls", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
2468 var_Create( p_obj, "allcontrols", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
2470 var_Create( p_obj, "controls-update", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2472 /* Add a control to reset all controls to their default values */
2473 vlc_value_t val, val2;
2474 var_Create( p_obj, "controls-reset", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
2475 val.psz_string = _( "Reset controls to default" );
2476 var_Change( p_obj, "controls-reset", VLC_VAR_SETTEXT, &val, NULL );
2478 val2.psz_string = (char *)"controls-reset";
2479 var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
2481 var_AddCallback( p_obj, "controls-reset", DemuxControlResetCallback, NULL );
2483 var_AddCallback( p_obj, "controls-reset", AccessControlResetCallback, NULL );
2485 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2486 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2488 msg_Dbg( p_obj, "Extended control API supported by v4l2 driver" );
2490 /* List extended controls */
2491 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2492 while( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2494 if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS )
2496 msg_Dbg( p_obj, "%s", queryctrl.name );
2497 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
2500 switch( V4L2_CTRL_ID2CLASS( queryctrl.id ) )
2502 case V4L2_CTRL_CLASS_USER:
2503 msg_Dbg( p_obj, "Available control: %s (%x)",
2504 queryctrl.name, queryctrl.id );
2506 case V4L2_CTRL_CLASS_MPEG:
2507 name2var( queryctrl.name );
2508 msg_Dbg( p_obj, "Available MPEG control: %s (%x)",
2509 queryctrl.name, queryctrl.id );
2512 msg_Dbg( p_obj, "Available private control: %s (%x)",
2513 queryctrl.name, queryctrl.id );
2516 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
2517 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
2522 msg_Dbg( p_obj, "Extended control API not supported by v4l2 driver" );
2524 /* List public controls */
2525 for( i_cid = V4L2_CID_BASE;
2526 i_cid < V4L2_CID_LASTP1;
2529 queryctrl.id = i_cid;
2530 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2532 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2534 msg_Dbg( p_obj, "Available control: %s (%x)",
2535 queryctrl.name, queryctrl.id );
2536 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
2540 /* List private controls */
2541 for( i_cid = V4L2_CID_PRIVATE_BASE;
2545 queryctrl.id = i_cid;
2546 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2548 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2550 msg_Dbg( p_obj, "Available private control: %s (%x)",
2551 queryctrl.name, queryctrl.id );
2552 ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
2562 static void SetAvailControlsByString( vlc_object_t *p_obj, demux_sys_t *p_sys,
2565 char *psz_parser = p_sys->psz_set_ctrls;
2566 vlc_value_t val, text, name;
2568 if( psz_parser == NULL )
2571 if( *psz_parser == '{' )
2574 int i_ret = var_Change( p_obj, "allcontrols", VLC_VAR_GETCHOICES,
2578 msg_Err( p_obj, "Oops, can't find 'allcontrols' variable." );
2582 while( *psz_parser && *psz_parser != '}' )
2584 char *psz_delim, *psz_assign;
2586 while( *psz_parser == ',' || *psz_parser == ' ' )
2589 psz_delim = strchr( psz_parser, ',' );
2590 if( psz_delim == NULL )
2591 psz_delim = strchr( psz_parser, '}' );
2592 if( psz_delim == NULL )
2593 psz_delim = psz_parser + strlen( psz_parser );
2595 psz_assign = memchr( psz_parser, '=', psz_delim - psz_parser );
2596 if( psz_assign == NULL )
2598 char *psz_name = strndup( psz_parser, psz_delim - psz_parser );
2599 msg_Err( p_obj, "%s missing '='", psz_name );
2601 psz_parser = psz_delim + 1;
2606 i < val.p_list->i_count ;//&& psz_parser < psz_assign;
2609 const char *psz_var = text.p_list->p_values[i].psz_string;
2610 int i_cid = val.p_list->p_values[i].i_int;
2611 var_Change( p_obj, psz_var, VLC_VAR_GETTEXT, &name, NULL );
2612 const char *psz_name = name.psz_string;
2614 int i_availstrlen = strlen( psz_name );
2615 int i_parsestrlen = psz_assign - psz_parser;
2616 int i_maxstrlen = __MAX( i_availstrlen, i_parsestrlen);
2618 if( !strncasecmp( psz_name, psz_parser, i_maxstrlen ) )
2620 Control( p_obj, i_fd, psz_name, i_cid,
2621 strtol( ++psz_assign, &psz_parser, 0) );
2625 if( psz_parser < psz_assign )
2627 char *psz_name = strndup( psz_parser, psz_assign - psz_parser );
2628 msg_Err( p_obj, "Control %s not available", psz_name );
2630 psz_parser = ( *psz_delim ) ? ( psz_delim + 1 ) : psz_delim;
2635 /*****************************************************************************
2636 * Reset all user-class v4l2 controls to their default value
2637 *****************************************************************************/
2638 static int ControlReset( vlc_object_t *p_obj, int i_fd )
2640 struct v4l2_queryctrl queryctrl;
2642 memset( &queryctrl, 0, sizeof( queryctrl ) );
2644 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2645 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2647 /* Extended control API supported */
2648 queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
2649 while( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2651 if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS
2652 || V4L2_CTRL_ID2CLASS( queryctrl.id ) == V4L2_CTRL_CLASS_MPEG )
2654 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
2657 struct v4l2_control control;
2658 memset( &control, 0, sizeof( control ) );
2659 control.id = queryctrl.id;
2660 if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
2661 && queryctrl.default_value != control.value )
2664 for( i = 0; controls[i].psz_name != NULL; i++ )
2665 if( controls[i].i_cid == queryctrl.id ) break;
2666 name2var( queryctrl.name );
2667 Control( p_obj, i_fd,
2668 controls[i].psz_name ? : (const char *)queryctrl.name,
2669 queryctrl.id, queryctrl.default_value );
2671 queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
2677 /* public controls */
2678 for( i_cid = V4L2_CID_BASE;
2679 i_cid < V4L2_CID_LASTP1;
2682 queryctrl.id = i_cid;
2683 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2685 struct v4l2_control control;
2686 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2688 memset( &control, 0, sizeof( control ) );
2689 control.id = queryctrl.id;
2690 if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
2691 && queryctrl.default_value != control.value )
2694 for( i = 0; controls[i].psz_name != NULL; i++ )
2695 if( controls[i].i_cid == queryctrl.id ) break;
2696 name2var( queryctrl.name );
2697 Control( p_obj, i_fd,
2698 controls[i].psz_name ? : (const char *)queryctrl.name,
2699 queryctrl.id, queryctrl.default_value );
2704 /* private controls */
2705 for( i_cid = V4L2_CID_PRIVATE_BASE;
2709 queryctrl.id = i_cid;
2710 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
2712 struct v4l2_control control;
2713 if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2715 memset( &control, 0, sizeof( control ) );
2716 control.id = queryctrl.id;
2717 if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
2718 && queryctrl.default_value != control.value )
2720 name2var( queryctrl.name );
2721 Control( p_obj, i_fd, (const char *)queryctrl.name,
2722 queryctrl.id, queryctrl.default_value );
2732 /*****************************************************************************
2733 * Issue user-class v4l2 controls
2734 *****************************************************************************/
2735 static int Control( vlc_object_t *p_obj, int i_fd,
2736 const char *psz_name, int i_cid, int i_value )
2738 struct v4l2_queryctrl queryctrl;
2739 struct v4l2_control control;
2740 struct v4l2_ext_control ext_control;
2741 struct v4l2_ext_controls ext_controls;
2746 memset( &queryctrl, 0, sizeof( queryctrl ) );
2748 queryctrl.id = i_cid;
2750 if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) < 0
2751 || queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
2753 msg_Dbg( p_obj, "%s (%x) control is not supported.", psz_name, i_cid );
2754 return VLC_EGENERIC;
2757 memset( &control, 0, sizeof( control ) );
2758 memset( &ext_control, 0, sizeof( ext_control ) );
2759 memset( &ext_controls, 0, sizeof( ext_controls ) );
2761 ext_control.id = i_cid;
2762 ext_controls.ctrl_class = V4L2_CTRL_ID2CLASS( i_cid );
2763 ext_controls.count = 1;
2764 ext_controls.controls = &ext_control;
2770 ext_control.value = i_value;
2771 if( v4l2_ioctl( i_fd, VIDIOC_S_EXT_CTRLS, &ext_controls ) < 0 )
2773 control.value = i_value;
2774 if( v4l2_ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
2776 msg_Err( p_obj, "unable to set %s (%x) to %d (%m)",
2777 psz_name, i_cid, i_value );
2778 return VLC_EGENERIC;
2780 i_ret = v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control );
2784 i_ret = v4l2_ioctl( i_fd, VIDIOC_G_EXT_CTRLS, &ext_controls );
2785 control.value = ext_control.value;
2792 msg_Dbg( p_obj, "video %s: %d", psz_name, control.value );
2793 switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
2796 val.b_bool = control.value;
2797 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
2798 var_SetVoid( p_obj, "controls-update" );
2800 case VLC_VAR_INTEGER:
2801 val.i_int = control.value;
2802 var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
2803 var_SetVoid( p_obj, "controls-update" );
2810 /*****************************************************************************
2811 * On the fly change settings callback
2812 *****************************************************************************/
2813 static int DemuxControlCallback( vlc_object_t *p_this,
2814 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
2818 demux_t *p_demux = (demux_t*)p_this;
2819 demux_sys_t *p_sys = p_demux->p_sys;
2820 int i_cid = (long int)p_data;
2822 int i_fd = p_sys->i_fd;
2825 return VLC_EGENERIC;
2827 Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
2829 return VLC_EGENERIC;
2832 static int DemuxControlResetCallback( vlc_object_t *p_this,
2833 const char *psz_var, vlc_value_t oldval, vlc_value_t newval, void *p_data )
2835 (void)psz_var; (void)oldval; (void)newval; (void)p_data;
2836 demux_t *p_demux = (demux_t*)p_this;
2837 demux_sys_t *p_sys = p_demux->p_sys;
2839 int i_fd = p_sys->i_fd;
2842 return VLC_EGENERIC;
2844 ControlReset( p_this, i_fd );
2846 return VLC_EGENERIC;
2849 static int AccessControlCallback( vlc_object_t *p_this,
2850 const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
2854 access_t *p_access = (access_t *)p_this;
2855 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
2856 int i_cid = (long int)p_data;
2858 int i_fd = p_sys->i_fd;
2861 return VLC_EGENERIC;
2863 Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
2865 return VLC_EGENERIC;
2868 static int AccessControlResetCallback( vlc_object_t *p_this,
2869 const char *psz_var, vlc_value_t oldval, vlc_value_t newval, void *p_data )
2871 (void)psz_var; (void)oldval; (void)newval; (void)p_data;
2872 access_t *p_access = (access_t *)p_this;
2873 demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
2875 int i_fd = p_sys->i_fd;
2878 return VLC_EGENERIC;
2880 ControlReset( p_this, i_fd );
2882 return VLC_EGENERIC;