v4l2.c 120 KB
Newer Older
1 2 3
/*****************************************************************************
 * v4l2.c : Video4Linux2 input module for vlc
 *****************************************************************************
4 5
 * Copyright (C) 2002-2007 the VideoLAN team
 * $Id$
6
 *
7 8
 * Authors: Benjamin Pracht <bigben at videolan dot org>
 *          Richard Hosking <richard at hovis dot net>
9 10
 *          Antoine Cellerier <dionoea at videolan d.t org>
 *          Dennis Lou <dlou99 at yahoo dot com>
11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
24 25 26
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

27
/*
28 29
 * Sections based on the reference V4L2 capture example at
 * http://v4l2spec.bytesex.org/spec/capture-example.html
30 31 32 33 34
 *
 * ALSA support based on parts of
 * http://www.equalarea.com/paul/alsa-audio.html
 * and hints taken from alsa-utils (aplay/arecord)
 * http://www.alsa-project.org
35
 */
36

37 38 39 40
/*****************************************************************************
 * Preamble
 *****************************************************************************/

41 42 43 44
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

45
#include <vlc_common.h>
46
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
47
#include <vlc_access.h>
48
#include <vlc_demux.h>
Clément Stenac's avatar
Clément Stenac committed
49 50
#include <vlc_input.h>
#include <vlc_vout.h>
51

52
#include <ctype.h>
53 54 55
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
56
#include <sys/mman.h>
57 58 59

#include <linux/videodev2.h>

60 61
#include <sys/soundcard.h>

62
#ifdef HAVE_ALSA
63 64 65
#   define ALSA_PCM_NEW_HW_PARAMS_API
#   define ALSA_PCM_NEW_SW_PARAMS_API
#   include <alsa/asoundlib.h>
66 67
#endif

68 69
#include <poll.h>

70 71
/*****************************************************************************
 * Module descriptior
72
 *****************************************************************************/
73

74 75 76 77
static int  DemuxOpen ( vlc_object_t * );
static void DemuxClose( vlc_object_t * );
static int  AccessOpen ( vlc_object_t * );
static void AccessClose( vlc_object_t * );
78 79 80 81 82

#define DEV_TEXT N_("Device name")
#define DEV_LONGTEXT N_( \
    "Name of the device to use. " \
    "If you don't specify anything, /dev/video0 will be used.")
83 84 85 86 87 88
#define STANDARD_TEXT N_( "Standard" )
#define STANDARD_LONGTEXT N_( \
    "Video standard (Default, SECAM, PAL, or NTSC)." )
#define CHROMA_TEXT N_("Video input chroma format")
#define CHROMA_LONGTEXT N_( \
    "Force the Video4Linux2 video device to use a specific chroma format " \
Antoine Cellerier's avatar
Antoine Cellerier committed
89
    "(eg. I420 or I422 for raw images, MJPG for M-JPEG compressed input) " \
90 91
    "(Complete list: GREY, I240, RV16, RV15, RV24, RV32, YUY2, YUYV, UYVY, " \
    "I41N, I422, I420, I411, I410, MJPG)")
Benjamin Pracht's avatar
Benjamin Pracht committed
92 93
#define INPUT_TEXT N_( "Input" )
#define INPUT_LONGTEXT N_( \
94 95 96 97
    "Input of the card to use (see debug)." )
#define AUDIO_INPUT_TEXT N_( "Audio input" )
#define AUDIO_INPUT_LONGTEXT N_( \
    "Audio input of the card to use (see debug)." )
98 99 100
#define IOMETHOD_TEXT N_( "IO Method" )
#define IOMETHOD_LONGTEXT N_( \
    "IO Method (READ, MMAP, USERPTR)." )
101 102 103 104 105 106
#define WIDTH_TEXT N_( "Width" )
#define WIDTH_LONGTEXT N_( \
    "Force width (-1 for autodetect)." )
#define HEIGHT_TEXT N_( "Height" )
#define HEIGHT_LONGTEXT N_( \
    "Force height (-1 for autodetect)." )
107 108 109 110
#define FPS_TEXT N_( "Framerate" )
#define FPS_LONGTEXT N_( "Framerate to capture, if applicable " \
    "(-1 for autodetect)." )

111 112 113
#define CTRL_RESET_TEXT N_( "Reset v4l2 controls" )
#define CTRL_RESET_LONGTEXT N_( \
    "Reset controls to defaults provided by the v4l2 driver." )
114 115
#define BRIGHTNESS_TEXT N_( "Brightness" )
#define BRIGHTNESS_LONGTEXT N_( \
116
    "Brightness of the video input (if supported by the v4l2 driver)." )
117 118
#define CONTRAST_TEXT N_( "Contrast" )
#define CONTRAST_LONGTEXT N_( \
119
    "Contrast of the video input (if supported by the v4l2 driver)." )
120 121
#define SATURATION_TEXT N_( "Saturation" )
#define SATURATION_LONGTEXT N_( \
122
    "Saturation of the video input (if supported by the v4l2 driver)." )
123 124
#define HUE_TEXT N_( "Hue" )
#define HUE_LONGTEXT N_( \
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    "Hue of the video input (if supported by the v4l2 driver)." )
#define BLACKLEVEL_TEXT N_( "Black level" )
#define BLACKLEVEL_LONGTEXT N_( \
    "Black level of the video input (if supported by the v4l2 driver)." )
#define AUTOWHITEBALANCE_TEXT N_( "Auto white balance" )
#define AUTOWHITEBALANCE_LONGTEXT N_( \
    "Automatically set the white balance of the video input " \
    "(if supported by the v4l2 driver)." )
#define DOWHITEBALANCE_TEXT N_( "Do white balance" )
#define DOWHITEBALANCE_LONGTEXT N_( \
    "Trigger a white balancing action, useless if auto white balance is " \
    "activated (if supported by the v4l2 driver)." )
#define REDBALANCE_TEXT N_( "Red balance" )
#define REDBALANCE_LONGTEXT N_( \
    "Red balance of the video input (if supported by the v4l2 driver)." )
#define BLUEBALANCE_TEXT N_( "Blue balance" )
#define BLUEBALANCE_LONGTEXT N_( \
    "Blue balance of the video input (if supported by the v4l2 driver)." )
143 144
#define GAMMA_TEXT N_( "Gamma" )
#define GAMMA_LONGTEXT N_( \
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    "Gamma of the video input (if supported by the v4l2 driver)." )
#define EXPOSURE_TEXT N_( "Exposure" )
#define EXPOSURE_LONGTEXT N_( \
    "Exposure of the video input (if supported by the v4L2 driver)." )
#define AUTOGAIN_TEXT N_( "Auto gain" )
#define AUTOGAIN_LONGTEXT N_( \
    "Automatically set the video input's gain (if supported by the " \
    "v4l2 driver)." )
#define GAIN_TEXT N_( "Gain" )
#define GAIN_LONGTEXT N_( \
    "Video input's gain (if supported by the v4l2 driver)." )
#define HFLIP_TEXT N_( "Horizontal flip" )
#define HFLIP_LONGTEXT N_( \
    "Flip the video horizontally (if supported by the v4l2 driver)." )
#define VFLIP_TEXT N_( "Vertical flip" )
#define VFLIP_LONGTEXT N_( \
    "Flip the video vertically (if supported by the v4l2 driver)." )
#define HCENTER_TEXT N_( "Horizontal centering" )
#define HCENTER_LONGTEXT N_( \
    "Set the camera's horizontal centering (if supported by the v4l2 driver)." )
#define VCENTER_TEXT N_( "Vertical centering" )
#define VCENTER_LONGTEXT N_( \
    "Set the camera's vertical centering (if supported by the v4l2 driver)." )
168 169

#define ADEV_TEXT N_("Audio device name")
170 171 172 173 174
#ifndef HAVE_ALSA
#define ADEV_LONGTEXT N_( \
    "Name of the audio device to use. " \
    "If you don't specify anything, \"/dev/dsp\" will be used for OSS.")
#else
175 176
#define ADEV_LONGTEXT N_( \
    "Name of the audio device to use. " \
177
    "If you don't specify anything, \"/dev/dsp\" will be used for OSS, " \
178
    "\"hw\" for Alsa.")
179
#endif
180
#define AUDIO_METHOD_TEXT N_( "Audio method" )
181
#ifndef HAVE_ALSA
182
#define AUDIO_METHOD_LONGTEXT N_( \
183 184 185 186
    "Audio method to use: 0 to disable audio, 1 for OSS." )
#else
#define AUDIO_METHOD_LONGTEXT N_( \
    "Audio method to use: 0 to disable audio, 1 for OSS, 2 for ALSA, " \
Sam Hocevar's avatar
Sam Hocevar committed
187
    "3 for ALSA or OSS (ALSA is preferred)." )
188
#endif
189 190
#define AUDIO_VOLUME_TEXT N_( "Volume" )
#define AUDIO_VOLUME_LONGTEXT N_( \
191
    "Volume of the audio input (if supported by the v4l2 driver)." )
192 193
#define AUDIO_BALANCE_TEXT N_( "Balance" )
#define AUDIO_BALANCE_LONGTEXT N_( \
194
    "Balance of the audio input (if supported by the v4l2 driver)." )
195 196
#define AUDIO_MUTE_TEXT N_( "Mute" )
#define AUDIO_MUTE_LONGTEXT N_( \
197 198 199 200 201 202 203 204 205 206 207
    "Mute audio input (if supported by the v4l2 driver)." )
#define AUDIO_BASS_TEXT N_( "Bass" )
#define AUDIO_BASS_LONGTEXT N_( \
    "Bass level of the audio input (if supported by the v4l2 driver)." )
#define AUDIO_TREBLE_TEXT N_( "Treble" )
#define AUDIO_TREBLE_LONGTEXT N_( \
    "Treble level of the audio input (if supported by the v4l2 driver)." )
#define AUDIO_LOUDNESS_TEXT N_( "Loudness" )
#define AUDIO_LOUDNESS_LONGTEXT N_( \
    "Loudness of the audio input (if supported by the v4l2 driver)." )

208 209 210 211 212
#define STEREO_TEXT N_( "Stereo" )
#define STEREO_LONGTEXT N_( \
    "Capture the audio stream in stereo." )
#define SAMPLERATE_TEXT N_( "Samplerate" )
#define SAMPLERATE_LONGTEXT N_( \
213
    "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
214

215 216 217 218
#define CACHING_TEXT N_("Caching value in ms")
#define CACHING_LONGTEXT N_( \
    "Caching value for V4L2 captures. This " \
    "value should be set in milliseconds." )
219 220 221 222 223 224 225
#define S_CTRLS_TEXT N_("v4l2 driver controls")
#define S_CTRLS_LONGTEXT N_( \
    "Set the v4l2 driver controls to the values specified using a comma " \
    "separated list optionally encapsulated by curly braces " \
    "(e.g.: {video_bitrate=6000000,audio_crc=0,stream_type=3} ). " \
    "To list available controls, increase verbosity (-vvv) " \
    "or use the v4l2-ctl application." )
226

227 228 229 230 231 232 233 234 235 236
#define TUNER_TEXT N_("Tuner id")
#define TUNER_LONGTEXT N_( \
    "Tuner id (see debug output)." )
#define FREQUENCY_TEXT N_("Frequency")
#define FREQUENCY_LONGTEXT N_( \
    "Tuner frequency in Hz or kHz (see debug output)" )
#define TUNER_AUDIO_MODE_TEXT N_("Audio mode")
#define TUNER_AUDIO_MODE_LONGTEXT N_( \
    "Tuner audio mono/stereo and track selection." )

237 238 239 240 241 242
typedef enum {
    IO_METHOD_READ,
    IO_METHOD_MMAP,
    IO_METHOD_USERPTR,
} io_method;

243
static const int i_standards_list[] =
244
    { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
245
static const char *const psz_standards_list_text[] =
246
    { N_("Default"), N_("SECAM"), N_("PAL"),  N_("NTSC") };
247

248
static const int i_iomethod_list[] =
249
    { IO_METHOD_READ, IO_METHOD_MMAP, IO_METHOD_USERPTR };
250
static const char *const psz_iomethod_list_text[] =
251
    { N_("READ"), N_("MMAP"),  N_("USERPTR") };
252

253
static const int i_tuner_audio_modes_list[] =
254 255 256
    { V4L2_TUNER_MODE_MONO, V4L2_TUNER_MODE_STEREO,
      V4L2_TUNER_MODE_LANG1, V4L2_TUNER_MODE_LANG2,
      V4L2_TUNER_MODE_SAP, V4L2_TUNER_MODE_LANG1_LANG2 };
257
static const char *const psz_tuner_audio_modes_list_text[] =
258 259 260 261 262 263 264
    { N_( "Mono" ),
      N_( "Stereo" ),
      N_( "Primary language (Analog TV tuners only)" ),
      N_( "Secondary language (Analog TV tuners only)" ),
      N_( "Second audio program (Analog TV tuners only)" ),
      N_( "Primary language left, Secondary language right" ) };

265 266 267 268 269 270
#define FIND_VIDEO 1
#define FIND_AUDIO 2

#define AUDIO_METHOD_OSS 1
#define OSS_DEFAULT "/dev/dsp"
#define AUDIO_METHOD_ALSA 2
271
#define ALSA_DEFAULT "hw"
272 273
#define CFG_PREFIX "v4l2-"

274
vlc_module_begin();
275 276
    set_shortname( N_("Video4Linux2") );
    set_description( N_("Video4Linux2 input") );
277 278 279
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_ACCESS );

280
    set_section( N_( "Video input" ), NULL );
281
    add_string( CFG_PREFIX "dev", "/dev/video0", 0, DEV_TEXT, DEV_LONGTEXT,
282
                false );
283
    add_integer( CFG_PREFIX "standard", 0, NULL, STANDARD_TEXT,
284
                 STANDARD_LONGTEXT, false );
285
        change_integer_list( i_standards_list, psz_standards_list_text, NULL );
286
    add_string( CFG_PREFIX "chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
287
                true );
288
    add_integer( CFG_PREFIX "input", 0, NULL, INPUT_TEXT, INPUT_LONGTEXT,
289
                true );
290
    add_integer( CFG_PREFIX "audio-input", 0, NULL, AUDIO_INPUT_TEXT,
291
                 AUDIO_INPUT_LONGTEXT, true );
292
    add_integer( CFG_PREFIX "io", IO_METHOD_MMAP, NULL, IOMETHOD_TEXT,
293
                 IOMETHOD_LONGTEXT, true );
294
        change_integer_list( i_iomethod_list, psz_iomethod_list_text, NULL );
295
    add_integer( CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
296
                WIDTH_LONGTEXT, true );
297
    add_integer( CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
298 299
                HEIGHT_LONGTEXT, true );
    add_float( CFG_PREFIX "fps", 0, NULL, FPS_TEXT, FPS_LONGTEXT, true );
300 301

    set_section( N_( "Audio input" ), NULL );
302
    add_string( CFG_PREFIX "adev", NULL, 0, ADEV_TEXT, ADEV_LONGTEXT,
303
                false );
304
    add_integer( CFG_PREFIX "audio-method", AUDIO_METHOD_OSS|AUDIO_METHOD_ALSA,
305 306 307
                 NULL, AUDIO_METHOD_TEXT, AUDIO_METHOD_LONGTEXT, true );
    add_bool( CFG_PREFIX "stereo", true, NULL, STEREO_TEXT, STEREO_LONGTEXT,
                true );
308
    add_integer( CFG_PREFIX "samplerate", 48000, NULL, SAMPLERATE_TEXT,
309
                SAMPLERATE_LONGTEXT, true );
310
    add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
311
                CACHING_TEXT, CACHING_LONGTEXT, true );
312

313 314
    set_section( N_( "Tuner" ), NULL );
    add_integer( CFG_PREFIX "tuner", 0, NULL, TUNER_TEXT, TUNER_LONGTEXT,
315
                 true );
316
    add_integer( CFG_PREFIX "tuner-frequency", -1, NULL, FREQUENCY_TEXT,
317
                 FREQUENCY_LONGTEXT, true );
318
    add_integer( CFG_PREFIX "tuner-audio-mode", -1, NULL, TUNER_AUDIO_MODE_TEXT,
319
                 TUNER_AUDIO_MODE_LONGTEXT, true );
320 321 322
        change_integer_list( i_tuner_audio_modes_list,
                             psz_tuner_audio_modes_list_text, 0 );

323 324
    set_section( N_( "Controls" ),
                 N_( "v4l2 driver controls, if supported by your v4l2 driver." ) );
325 326
    add_bool( CFG_PREFIX "controls-reset", false, NULL, CTRL_RESET_TEXT,
              CTRL_RESET_LONGTEXT, true );
327
    add_integer( CFG_PREFIX "brightness", -1, NULL, BRIGHTNESS_TEXT,
328
                 BRIGHTNESS_LONGTEXT, true );
329
    add_integer( CFG_PREFIX "contrast", -1, NULL, CONTRAST_TEXT,
330
                 CONTRAST_LONGTEXT, true );
331
    add_integer( CFG_PREFIX "saturation", -1, NULL, SATURATION_TEXT,
332
                 SATURATION_LONGTEXT, true );
333
    add_integer( CFG_PREFIX "hue", -1, NULL, HUE_TEXT,
334
                 HUE_LONGTEXT, true );
335
    add_integer( CFG_PREFIX "black-level", -1, NULL, BLACKLEVEL_TEXT,
336
                 BLACKLEVEL_LONGTEXT, true );
337
    add_integer( CFG_PREFIX "auto-white-balance", -1, NULL,
338
                 AUTOWHITEBALANCE_TEXT, AUTOWHITEBALANCE_LONGTEXT, true );
339
    add_integer( CFG_PREFIX "do-white-balance", -1, NULL, DOWHITEBALANCE_TEXT,
340
                 DOWHITEBALANCE_LONGTEXT, true );
341
    add_integer( CFG_PREFIX "red-balance", -1, NULL, REDBALANCE_TEXT,
342
                 REDBALANCE_LONGTEXT, true );
343
    add_integer( CFG_PREFIX "blue-balance", -1, NULL, BLUEBALANCE_TEXT,
344
                 BLUEBALANCE_LONGTEXT, true );
345
    add_integer( CFG_PREFIX "gamma", -1, NULL, GAMMA_TEXT,
346
                 GAMMA_LONGTEXT, true );
347
    add_integer( CFG_PREFIX "exposure", -1, NULL, EXPOSURE_TEXT,
348
                 EXPOSURE_LONGTEXT, true );
349
    add_integer( CFG_PREFIX "autogain", -1, NULL, AUTOGAIN_TEXT,
350
                 AUTOGAIN_LONGTEXT, true );
351
    add_integer( CFG_PREFIX "gain", -1, NULL, GAIN_TEXT,
352
                 GAIN_LONGTEXT, true );
353
    add_integer( CFG_PREFIX "hflip", -1, NULL, HFLIP_TEXT,
354
                 HFLIP_LONGTEXT, true );
355
    add_integer( CFG_PREFIX "vflip", -1, NULL, VFLIP_TEXT,
356
                 VFLIP_LONGTEXT, true );
357
    add_integer( CFG_PREFIX "hcenter", -1, NULL, HCENTER_TEXT,
358
                 HCENTER_LONGTEXT, true );
359
    add_integer( CFG_PREFIX "vcenter", -1, NULL, VCENTER_TEXT,
360
                 VCENTER_LONGTEXT, true );
361
    add_integer( CFG_PREFIX "audio-volume", -1, NULL, AUDIO_VOLUME_TEXT,
362
                AUDIO_VOLUME_LONGTEXT, true );
363
    add_integer( CFG_PREFIX "audio-balance", -1, NULL, AUDIO_BALANCE_TEXT,
364 365 366
                AUDIO_BALANCE_LONGTEXT, true );
    add_bool( CFG_PREFIX "audio-mute", false, NULL, AUDIO_MUTE_TEXT,
              AUDIO_MUTE_LONGTEXT, true );
367
    add_integer( CFG_PREFIX "audio-bass", -1, NULL, AUDIO_BASS_TEXT,
368
                AUDIO_BASS_LONGTEXT, true );
369
    add_integer( CFG_PREFIX "audio-treble", -1, NULL, AUDIO_TREBLE_TEXT,
370
                AUDIO_TREBLE_LONGTEXT, true );
371
    add_integer( CFG_PREFIX "audio-loudness", -1, NULL, AUDIO_LOUDNESS_TEXT,
372
                AUDIO_LOUDNESS_LONGTEXT, true );
373
    add_string( CFG_PREFIX "set-ctrls", NULL, NULL, S_CTRLS_TEXT,
374
              S_CTRLS_LONGTEXT, true );
375

376 377
    add_shortcut( "v4l2" );
    set_capability( "access_demux", 10 );
378 379 380
    set_callbacks( DemuxOpen, DemuxClose );

    add_submodule();
381
    add_shortcut( "v4l2c" );
382
    set_description( N_("Video4Linux2 Compressed A/V") );
383
    set_capability( "access", 0 );
384 385 386
    /* use these when open as access_demux fails; VLC will use another demux */
    set_callbacks( AccessOpen, AccessClose );

387 388 389 390 391 392
vlc_module_end();

/*****************************************************************************
 * Access: local prototypes
 *****************************************************************************/

393 394
static void CommonClose( vlc_object_t *, demux_sys_t * );
static void ParseMRL( demux_sys_t *, char *, vlc_object_t * );
395
static void GetV4L2Params( demux_sys_t *, vlc_object_t * );
396
static void SetAvailControlsByString( vlc_object_t *, demux_sys_t *, int );
397

398
static int DemuxControl( demux_t *, int, va_list );
399
static int AccessControl( access_t *, int, va_list );
400

401
static int Demux( demux_t * );
402 403
static ssize_t AccessRead( access_t *, uint8_t *, size_t );

404
static block_t* GrabVideo( demux_t *p_demux );
405
static block_t* ProcessVideoFrame( demux_t *p_demux, uint8_t *p_frame, size_t );
406
static block_t* GrabAudio( demux_t *p_demux );
Benjamin Pracht's avatar
Benjamin Pracht committed
407

408
static bool IsPixelFormatSupported( demux_t *p_demux,
409
                                          unsigned int i_pixelformat );
410

411
#ifdef HAVE_ALSA
412
static char* ResolveALSADeviceName( const char *psz_device );
413
#endif
414 415 416
static int OpenVideoDev( vlc_object_t *, demux_sys_t *, bool );
static int OpenAudioDev( vlc_object_t *, demux_sys_t *, bool );
static bool ProbeVideoDev( vlc_object_t *, demux_sys_t *,
417
                                 char *psz_device );
418
static bool ProbeAudioDev( vlc_object_t *, demux_sys_t *,
419
                                 char *psz_device );
420

421
static int ControlList( vlc_object_t *, int , bool, bool );
422
static int Control( vlc_object_t *, int i_fd,
423
                    const char *psz_name, int i_cid, int i_value );
424 425 426 427 428 429 430 431 432 433

static int DemuxControlCallback( vlc_object_t *p_this, const char *psz_var,
                                 vlc_value_t oldval, vlc_value_t newval,
                                 void *p_data );
static int DemuxControlResetCallback( vlc_object_t *p_this, const char *psz_var,
                                      vlc_value_t oldval, vlc_value_t newval,
                                      void *p_data );
static int AccessControlCallback( vlc_object_t *p_this, const char *psz_var,
                                  vlc_value_t oldval, vlc_value_t newval,
                                  void *p_data );
434
static int AccessControlResetCallback( vlc_object_t *p_this,
435 436
                                       const char *psz_var, vlc_value_t oldval,
                                       vlc_value_t newval, void *p_data );
437

438
static const struct
439
{
440 441 442 443
    unsigned int i_v4l2;
    int i_fourcc;
} v4l2chroma_to_fourcc[] =
{
444 445 446 447 448 449 450 451 452 453 454 455
    /* Raw data types */
    { V4L2_PIX_FMT_GREY,    VLC_FOURCC('G','R','E','Y') },
    { V4L2_PIX_FMT_HI240,   VLC_FOURCC('I','2','4','0') },
    { V4L2_PIX_FMT_RGB565,  VLC_FOURCC('R','V','1','6') },
    { V4L2_PIX_FMT_RGB555,  VLC_FOURCC('R','V','1','5') },
    { V4L2_PIX_FMT_BGR24,   VLC_FOURCC('R','V','2','4') },
    { V4L2_PIX_FMT_BGR32,   VLC_FOURCC('R','V','3','2') },
    { V4L2_PIX_FMT_YUYV,    VLC_FOURCC('Y','U','Y','2') },
    { V4L2_PIX_FMT_YUYV,    VLC_FOURCC('Y','U','Y','V') },
    { V4L2_PIX_FMT_UYVY,    VLC_FOURCC('U','Y','V','Y') },
    { V4L2_PIX_FMT_Y41P,    VLC_FOURCC('I','4','1','N') },
    { V4L2_PIX_FMT_YUV422P, VLC_FOURCC('I','4','2','2') },
456
    { V4L2_PIX_FMT_YVU420,  VLC_FOURCC('Y','V','1','2') },
457 458
    { V4L2_PIX_FMT_YUV411P, VLC_FOURCC('I','4','1','1') },
    { V4L2_PIX_FMT_YUV410,  VLC_FOURCC('I','4','1','0') },
459 460 461 462

    /* Raw data types, not in V4L2 spec but still in videodev2.h and supported
     * by VLC */
    { V4L2_PIX_FMT_YUV420,  VLC_FOURCC('I','4','2','0') },
Antoine Cellerier's avatar
Antoine Cellerier committed
463
    /* FIXME { V4L2_PIX_FMT_RGB444,  VLC_FOURCC('R','V','3','2') }, */
464

465 466 467 468 469 470 471
    /* Compressed data types */
    { V4L2_PIX_FMT_MJPEG,   VLC_FOURCC('M','J','P','G') },
#if 0
    { V4L2_PIX_FMT_JPEG,    VLC_FOURCC('J','P','E','G') },
    { V4L2_PIX_FMT_DV,      VLC_FOURCC('?','?','?','?') },
    { V4L2_PIX_FMT_MPEG,    VLC_FOURCC('?','?','?','?') },
#endif
472 473
    { 0, 0 }
};
474

475 476 477 478 479 480
/**
 * List of V4L2 chromas were confident enough to use as fallbacks if the
 * user hasn't provided a --v4l2-chroma value.
 */
static const __u32 p_chroma_fallbacks[] =
{ V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_YVU420, V4L2_PIX_FMT_YUV422P,
481
  V4L2_PIX_FMT_YUYV, V4L2_PIX_FMT_UYVY, V4L2_PIX_FMT_MJPEG };
482

483
static const struct
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
{
    const char *psz_name;
    unsigned int i_cid;
} controls[] =
{
    { "brightness", V4L2_CID_BRIGHTNESS },
    { "contrast", V4L2_CID_CONTRAST },
    { "saturation", V4L2_CID_SATURATION },
    { "hue", V4L2_CID_HUE },
    { "audio-volume", V4L2_CID_AUDIO_VOLUME },
    { "audio-balance", V4L2_CID_AUDIO_BALANCE },
    { "audio-bass", V4L2_CID_AUDIO_BASS },
    { "audio-treble", V4L2_CID_AUDIO_TREBLE },
    { "audio-mute", V4L2_CID_AUDIO_MUTE },
    { "audio-loudness", V4L2_CID_AUDIO_LOUDNESS },
    { "black-level", V4L2_CID_BLACK_LEVEL },
    { "auto-white-balance", V4L2_CID_AUTO_WHITE_BALANCE },
    { "do-white-balance", V4L2_CID_DO_WHITE_BALANCE },
    { "red-balance", V4L2_CID_RED_BALANCE },
    { "blue-balance", V4L2_CID_BLUE_BALANCE },
    { "gamma", V4L2_CID_GAMMA },
    { "exposure", V4L2_CID_EXPOSURE },
    { "autogain", V4L2_CID_AUTOGAIN },
    { "gain", V4L2_CID_GAIN },
    { "hflip", V4L2_CID_HFLIP },
    { "vflip", V4L2_CID_VFLIP },
    { "hcenter", V4L2_CID_HCENTER },
    { "vcenter", V4L2_CID_VCENTER },
    { NULL, 0 }
};

515
struct buffer_t
516 517 518 519 520 521 522 523
{
    void *  start;
    size_t  length;
};

struct demux_sys_t
{
    char *psz_device;  /* Main device from MRL, can be video or audio */
524

525 526 527 528 529
    char *psz_vdev;
    int  i_fd_video;

    char *psz_adev;
    int  i_fd_audio;
530

531
    char *psz_requested_chroma;
532

533 534
    /* Video */
    io_method io;
535

536
    int i_pts;
537 538 539 540 541

    struct v4l2_capability dev_cap;

    int i_input;
    struct v4l2_input *p_inputs;
Benjamin Pracht's avatar
Benjamin Pracht committed
542
    int i_selected_input;
543

544 545 546
    int i_standard;
    struct v4l2_standard *p_standards;
    v4l2_std_id i_selected_standard_id;
547 548 549 550

    int i_audio;
    /* V4L2 devices cannot have more than 32 audio inputs */
    struct v4l2_audio p_audios[32];
551
    int i_selected_audio_input;
552 553 554

    int i_tuner;
    struct v4l2_tuner *p_tuners;
Benjamin Pracht's avatar
Benjamin Pracht committed
555 556 557

    int i_codec;
    struct v4l2_fmtdesc *p_codecs;
558

559 560
    struct buffer_t *p_buffers;
    unsigned int i_nbuffers;
561

562 563 564 565 566
    int i_width;
    int i_height;
    float f_fps;            /* <= 0.0 mean to grab at full rate */
    mtime_t i_video_pts;    /* only used when f_fps > 0 */
    int i_fourcc;
567

568
    es_out_id_t *p_es_video;
569

570
    /* Audio */
571
    unsigned int i_sample_rate;
572
    bool b_stereo;
573
    size_t i_audio_max_frame_size;
574 575
    block_t *p_block_audio;
    es_out_id_t *p_es_audio;
576

577 578
    int i_audio_method;

579
#ifdef HAVE_ALSA
580 581
    /* ALSA Audio */
    snd_pcm_t *p_alsa_pcm;
582
    size_t i_alsa_frame_size;
583 584
    int i_alsa_chunk_size;
#endif
585

586 587 588 589 590 591
    /* Tuner */
    int i_cur_tuner;
    int i_frequency;
    int i_audio_mode;

    /* Controls */
592
    char *psz_set_ctrls;
593 594
};

595
static int FindMainDevice( vlc_object_t *p_this, demux_sys_t *p_sys,
596 597
                           int i_flags, bool b_demux,
                           bool b_forced )
598
{
599 600 601
    /* Find main device (video or audio) */
    if( p_sys->psz_device && *p_sys->psz_device )
    {
602
        msg_Dbg( p_this, "main device='%s'", p_sys->psz_device );
603

604
        bool b_maindevice_is_video = false;
605

606
        /* Try to open as video device */
607
        if( i_flags & FIND_VIDEO )
608
        {
609 610
            msg_Dbg( p_this, "trying device '%s' as video", p_sys->psz_device );
            if( ProbeVideoDev( p_this, p_sys, p_sys->psz_device ) )
611
            {
612 613
                msg_Dbg( p_this, "'%s' is a video device", p_sys->psz_device );
                /* Device was a video device */
614
                free( p_sys->psz_vdev );
615 616 617 618 619
                p_sys->psz_vdev = p_sys->psz_device;
                p_sys->psz_device = NULL;
                p_sys->i_fd_video = OpenVideoDev( p_this, p_sys, b_demux );
                if( p_sys->i_fd_video < 0 )
                    return VLC_EGENERIC;
620
                b_maindevice_is_video = true;
621
                /* If successful we carry on to try the audio if access is forced */
622 623
            }
        }
624

625 626
        /* Try to open as audio device only if main device was not detected as video above */
        if( i_flags & FIND_AUDIO && !b_maindevice_is_video )
627
        {
628 629
            msg_Dbg( p_this, "trying device '%s' as audio", p_sys->psz_device );
            if( ProbeAudioDev( p_this, p_sys, p_sys->psz_device ) )
630
            {
631
                msg_Dbg( p_this, "'%s' is an audio device", p_sys->psz_device );
632
                /* Device was an audio device */
633
                free( p_sys->psz_adev );
634 635
                p_sys->psz_adev = p_sys->psz_device;
                p_sys->psz_device = NULL;
636
                p_sys->i_fd_audio = OpenAudioDev( p_this, p_sys, b_demux );
637
                if( p_sys->i_fd_audio < 0 )
638
                    return VLC_EGENERIC;
639
                /* If successful we carry on to try the video if access is forced */
640 641 642
            }
        }
    }
643

644
    /* If no device opened, only continue if the access was forced */
645
    if( b_forced == false
646 647
        && !( ( i_flags & FIND_VIDEO && p_sys->i_fd_video >= 0 )
           || ( i_flags & FIND_AUDIO && p_sys->i_fd_audio >= 0 ) ) )
648
    {
649
        return VLC_EGENERIC;
650
    }
Benjamin Pracht's avatar
Benjamin Pracht committed
651

652
    /* Find video device */
653
    if( i_flags & FIND_VIDEO && p_sys->i_fd_video < 0 )
654 655 656
    {
        if( !p_sys->psz_vdev || !*p_sys->psz_vdev )
        {
657
            free( p_sys->psz_vdev );
658
            p_sys->psz_vdev = var_CreateGetString( p_this, "v4l2-dev" );
659 660
        }

661 662 663
        msg_Dbg( p_this, "opening '%s' as video", p_sys->psz_vdev );
        if( p_sys->psz_vdev && *p_sys->psz_vdev
         && ProbeVideoDev( p_this, p_sys, p_sys->psz_vdev ) )
664
        {
665
            p_sys->i_fd_video = OpenVideoDev( p_this, p_sys, b_demux );
666 667 668 669
        }
    }

    /* Find audio device */
670
    if( i_flags & FIND_AUDIO && p_sys->i_fd_audio < 0 )
671
    {
672
        if( !p_sys->psz_adev )
673
        {
674
            p_sys->psz_adev = var_CreateGetNonEmptyString( p_this, "v4l2-adev" );
675 676
        }

677
        msg_Dbg( p_this, "opening '%s' as audio", p_sys->psz_adev );
678
        if( ProbeAudioDev( p_this, p_sys, p_sys->psz_adev ) )
679
        {
680
            p_sys->i_fd_audio = OpenAudioDev( p_this, p_sys, b_demux );
681 682 683
        }
    }

684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
    if( !( ( i_flags & FIND_VIDEO && p_sys->i_fd_video >= 0 )
        || ( i_flags & FIND_AUDIO && p_sys->i_fd_audio >= 0 ) ) )
    {
        return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}

/*****************************************************************************
 * DemuxOpen: opens v4l2 device, access_demux callback
 *****************************************************************************
 *
 * url: <video device>::::
 *
 *****************************************************************************/
static int DemuxOpen( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;

    /* Only when selected */
    if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;

    /* Set up p_demux */
    p_demux->pf_control = DemuxControl;
    p_demux->pf_demux = Demux;
    p_demux->info.i_update = 0;
    p_demux->info.i_title = 0;
    p_demux->info.i_seekpoint = 0;

    p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
    if( p_sys == NULL ) return VLC_ENOMEM;

    GetV4L2Params(p_sys, (vlc_object_t *) p_demux);

    ParseMRL( p_sys, p_demux->psz_path, (vlc_object_t *) p_demux );

#ifdef HAVE_ALSA
    /* Alsa support available? */
    msg_Dbg( p_demux, "ALSA input support available" );
#endif

    if( FindMainDevice( p_this, p_sys, FIND_VIDEO|FIND_AUDIO,
727
        true, !strncmp( p_demux->psz_access, "v4l2", 4 ) ) != VLC_SUCCESS )
728
    {
729
        DemuxClose( p_this );
730 731
        return VLC_EGENERIC;
    }
732 733 734 735

    return VLC_SUCCESS;
}

736
/*****************************************************************************
737
 * GetV4L2Params: fill in p_sys parameters (shared by DemuxOpen and AccessOpen)
738
 *****************************************************************************/
739
static void GetV4L2Params( demux_sys_t *p_sys, vlc_object_t *p_obj )
740
{
741 742
    p_sys->i_video_pts = -1;

743 744
    p_sys->i_selected_standard_id =
        i_standards_list[var_CreateGetInteger( p_obj, "v4l2-standard" )];
745 746

    p_sys->i_selected_input = var_CreateGetInteger( p_obj, "v4l2-input" );
747 748
    p_sys->i_selected_audio_input =
        var_CreateGetInteger( p_obj, "v4l2-audio-input" );
749 750 751 752 753 754 755 756

    p_sys->io = var_CreateGetInteger( p_obj, "v4l2-io" );

    p_sys->i_width = var_CreateGetInteger( p_obj, "v4l2-width" );
    p_sys->i_height = var_CreateGetInteger( p_obj, "v4l2-height" );

    var_CreateGetBool( p_obj, "v4l2-controls-reset" );

757 758
    p_sys->f_fps = var_CreateGetFloat( p_obj, "v4l2-fps" );
    p_sys->i_sample_rate = var_CreateGetInteger( p_obj, "v4l2-samplerate" );
759 760
    p_sys->psz_requested_chroma = var_CreateGetString( p_obj, "v4l2-chroma" );

761
    p_sys->i_audio_method = var_CreateGetInteger( p_obj, "v4l2-audio-method" );
762

763
    p_sys->b_stereo = var_CreateGetBool( p_obj, "v4l2-stereo" );
764

765 766
    p_sys->i_pts = var_CreateGetInteger( p_obj, "v4l2-caching" );

767 768 769 770
    p_sys->i_cur_tuner = var_CreateGetInteger( p_obj, "v4l2-tuner" );
    p_sys->i_frequency = var_CreateGetInteger( p_obj, "v4l2-tuner-frequency" );
    p_sys->i_audio_mode = var_CreateGetInteger( p_obj, "v4l2-tuner-audio-mode" );

771 772
    p_sys->psz_set_ctrls = var_CreateGetString( p_obj, "v4l2-set-ctrls" );

773 774 775 776 777 778 779 780 781 782 783 784 785 786
    p_sys->psz_device = p_sys->psz_vdev = p_sys->psz_adev = NULL;
    p_sys->i_fd_video = -1;
    p_sys->i_fd_audio = -1;

    p_sys->p_es_video = p_sys->p_es_audio = 0;
    p_sys->p_block_audio = 0;
}

/*****************************************************************************
 * ParseMRL: parse the options contained in the MRL
 *****************************************************************************/
static void ParseMRL( demux_sys_t *p_sys, char *psz_path, vlc_object_t *p_obj )
{
    char *psz_dup = strdup( psz_path );
787 788 789 790 791 792 793 794 795 796 797 798 799
    char *psz_parser = psz_dup;

    while( *psz_parser && *psz_parser != ':' )
    {
        psz_parser++;
    }

    if( *psz_parser == ':' )
    {
        /* read options */
        for( ;; )
        {
            *psz_parser++ = '\0';
800

801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
            if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
            {
                int  i_len;

                psz_parser += strlen( "adev=" );
                if( strchr( psz_parser, ':' ) )
                {
                    i_len = strchr( psz_parser, ':' ) - psz_parser;
                }
                else
                {
                    i_len = strlen( psz_parser );
                }

                p_sys->psz_adev = strndup( psz_parser, i_len );
816 817 818 819 820
                if( !*p_sys->psz_adev )
                {
                    free( p_sys->psz_adev );
                    p_sys->psz_adev = NULL;
                }
821

822
                psz_parser += i_len;
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
            }
            else if( !strncmp( psz_parser, "standard=", strlen( "standard=" ) ) )
            {
                psz_parser += strlen( "standard=" );
                if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
                {
                    p_sys->i_selected_standard_id = V4L2_STD_PAL;
                    psz_parser += strlen( "pal" );
                }
                else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
                {
                    p_sys->i_selected_standard_id = V4L2_STD_NTSC;
                    psz_parser += strlen( "ntsc" );
                }
                else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
                {
                    p_sys->i_selected_standard_id = V4L2_STD_SECAM;
                    psz_parser += strlen( "secam" );
                }
                else if( !strncmp( psz_parser, "default", strlen( "default" ) ) )
                {
                    p_sys->i_selected_standard_id = V4L2_STD_UNKNOWN;
                    psz_parser += strlen( "default" );
                }
                else
                {
                    p_sys->i_selected_standard_id = i_standards_list[strtol( psz_parser, &psz_parser, 0 )];
                }
            }
            else if( !strncmp( psz_parser, "chroma=", strlen( "chroma=" ) ) )
            {
                int  i_len;

                psz_parser += strlen( "chroma=" );
                if( strchr( psz_parser, ':' ) )
                {
                    i_len = strchr( psz_parser, ':' ) - psz_parser;
                }
                else
                {
                    i_len = strlen( psz_parser );
                }

866
                free( p_sys->psz_requested_chroma );
867
                p_sys->psz_requested_chroma = strndup( psz_parser, i_len );
868

869
                psz_parser += i_len;
870 871 872 873 874
            }
            else if( !strncmp( psz_parser, "input=", strlen( "input=" ) ) )
            {
                p_sys->i_selected_input = strtol( psz_parser + strlen( "input=" ),
                                       &psz_parser, 0 );
875
            }
876 877 878 879 880
            else if( !strncmp( psz_parser, "audio-input=", strlen( "audio-input=" ) ) )
            {
                p_sys->i_selected_audio_input = strtol( psz_parser + strlen( "audio-input=" ),
                                       &psz_parser, 0 );
            }
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
            else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
            {
                p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
                                       &psz_parser );
            }
            else if( !strncmp( psz_parser, "io=", strlen( "io=" ) ) )
            {
                psz_parser += strlen( "io=" );
                if( !strncmp( psz_parser, "read", strlen( "read" ) ) )
                {
                    p_sys->io = IO_METHOD_READ;
                    psz_parser += strlen( "read" );
                }
                else if( !strncmp( psz_parser, "mmap", strlen( "mmap" ) ) )
                {
                    p_sys->io = IO_METHOD_MMAP;
                    psz_parser += strlen( "mmap" );
                }
                else if( !strncmp( psz_parser, "userptr", strlen( "userptr" ) ) )
                {
                    p_sys->io = IO_METHOD_USERPTR;
                    psz_parser += strlen( "userptr" );
                }
                else
                {
                    p_sys->io = strtol( psz_parser, &psz_parser, 0 );
                }
            }
909 910 911 912 913 914 915 916 917 918 919 920 921
            else if( !strncmp( psz_parser, "width=",
                               strlen( "width=" ) ) )
            {
                p_sys->i_width =
                    strtol( psz_parser + strlen( "width=" ),
                            &psz_parser, 0 );
            }
            else if( !strncmp( psz_parser, "height=",
                               strlen( "height=" ) ) )
            {
                p_sys->i_height =
                    strtol( psz_parser + strlen( "height=" ),
                            &psz_parser, 0 );
922
            }
923 924
            else if( !strncmp( psz_parser, "controls-reset",
                               strlen( "controls-reset" ) ) )
925
            {
926
                var_SetBool( p_obj, "v4l2-controls-reset", true );
927
                psz_parser += strlen( "controls-reset" );
928
            }
929
#if 0
930 931 932
            else if( !strncmp( psz_parser, "brightness=",
                               strlen( "brightness=" ) ) )
            {
933
                var_SetInteger( p_obj, "brightness",
934
                    strtol( psz_parser + strlen( "brightness=" ),
935
                            &psz_parser, 0 ) );
936 937 938 939
            }
            else if( !strncmp( psz_parser, "contrast=",
                               strlen( "contrast=" ) ) )
            {
940
                var_SetInteger( p_obj, "contrast",
941
                    strtol( psz_parser + strlen( "contrast=" ),
942
                            &psz_parser, 0 ) );
943 944 945 946
            }
            else if( !strncmp( psz_parser, "saturation=",
                               strlen( "saturation=" ) ) )
            {
947
                var_SetInteger( p_obj, "saturation",
948
                    strtol( psz_parser + strlen( "saturation=" ),
949
                            &psz_parser, 0 ) );
950 951 952 953
            }
            else if( !strncmp( psz_parser, "hue=",
                               strlen( "hue=" ) ) )
            {
954
                var_SetInteger( p_obj, "hue",
955
                    strtol( psz_parser + strlen( "hue=" ),
956
                            &psz_parser, 0 ) );
957
            }
958 959 960
            else if( !strncmp( psz_parser, "gamma=",
                               strlen( "gamma=" ) ) )
            {
961
                var_SetInteger( p_obj, "gamma",
962
                    strtol( psz_parser + strlen( "gamma=" ),
963
                            &psz_parser, 0 ) );
964
            }
965
#endif
966 967 968 969 970 971 972
            else if( !strncmp( psz_parser, "samplerate=",
                               strlen( "samplerate=" ) ) )
            {
                p_sys->i_sample_rate =
                    strtol( psz_parser + strlen( "samplerate=" ),
                            &psz_parser, 0 );
            }
973
            else if( !strncmp( psz_parser, "audio-method", strlen( "audio-method" ) ) )
974
            {
975 976 977
                p_sys->i_audio_method =
                    strtol( psz_parser + strlen( "audio-method" ),
                            &psz_parser, 0 );
978
            }
979 980 981
            else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
            {
                psz_parser += strlen( "stereo" );
982
                p_sys->b_stereo = true;
983 984 985 986
            }
            else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
            {
                psz_parser += strlen( "mono" );
987
                p_sys->b_stereo = false;
988 989 990 991
            }
            else if( !strncmp( psz_parser, "caching=", strlen( "caching=" ) ) )
            {
                p_sys->i_pts = strtol( psz_parser + strlen( "caching=" ),
992
                                       &psz_parser, 0 );
993
            }
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
            else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
            {
                p_sys->i_cur_tuner = strtol( psz_parser + strlen( "tuner=" ),
                                         &psz_parser, 0 );
            }
            else if( !strncmp( psz_parser, "tuner-frequency=", strlen( "tuner-frequency=" ) ) )
            {
                p_sys->i_frequency = strtol( psz_parser
                                          + strlen( "tuner-frequency=" ),
                                          &psz_parser, 0 );
            }
            else if( !strncmp( psz_parser, "tuner-audio-mode=", strlen( "tuner-audio-mode=" ) ) )
            {
                p_sys->i_audio_mode = strtol( psz_parser
                                          + strlen( "tuner-audio-mode=" ),
                                          &psz_parser, 0 );
            }
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
            else if( !strncmp( psz_parser, "set-ctrls=", strlen( "set-ctrls=" )) )
            {
                int  i_len;

                psz_parser += strlen( "set-ctrls=" );
                if( strchr( psz_parser, ':' ) )
                {
                    i_len = strchr( psz_parser, ':' ) - psz_parser;
                }
                else
                {
                    i_len = strlen( psz_parser );
                }

                p_sys->psz_set_ctrls = strndup( psz_parser, i_len );

                psz_parser += i_len;
            }
1029 1030
            else
            {
1031 1032 1033 1034 1035 1036 1037
                char *psz_unk = strchr( psz_parser, ':' );
                if (psz_unk)
                    psz_unk = strndup( psz_parser, psz_unk - psz_parser );
                else
                    psz_unk = strdup( psz_parser);
                msg_Warn( p_obj, "unknown option %s", psz_unk );
                free (psz_unk);
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
            }

            while( *psz_parser && *psz_parser != ':' )
            {
                psz_parser++;
            }

            if( *psz_parser == '\0' )
            {
                break;
            }
        }
    }

    /* Main device */
    if( *psz_dup )
    {
        p_sys->psz_device = strdup( psz_dup );
    }
1057
    free( psz_dup );
1058 1059
}

1060 1061 1062
/*****************************************************************************
 * Close: close device, free resources
 *****************************************************************************/
1063 1064 1065 1066 1067 1068 1069 1070 1071
static void AccessClose( vlc_object_t *p_this )
{
    access_t    *p_access = (access_t *)p_this;
    demux_sys_t *p_sys   = (demux_sys_t *) p_access->p_sys;

    CommonClose( p_this, p_sys );
}

static void DemuxClose( vlc_object_t *p_this )
1072
{
Jean-Paul Saman's avatar