dshow.cpp 89 KB
Newer Older
Gildas Bazin's avatar
 
Gildas Bazin committed
1
/*****************************************************************************
2
 * dshow.cpp : DirectShow access and access_demux module for vlc
Gildas Bazin's avatar
 
Gildas Bazin committed
3
 *****************************************************************************
4
 * Copyright (C) 2002-2004, 2006, 2008, 2010 the VideoLAN team
5
 * $Id$
Gildas Bazin's avatar
 
Gildas Bazin committed
6
 *
7
 * Author: Gildas Bazin <gbazin@videolan.org>
8
 *         Damien Fouilleul <damienf@videolan.org>
Gildas Bazin's avatar
 
Gildas Bazin committed
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Gildas Bazin's avatar
 
Gildas Bazin committed
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28

29 30 31 32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#define CFG_PREFIX "dshow-"
34
#include <inttypes.h>
35 36
#include <list>
#include <string>
37
#include <assert.h>
38
#include <stdexcept>
39

40
#define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
41
#include <vlc_common.h>
42
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
43
#include <vlc_access.h>
44
#include <vlc_demux.h>
45

Thomas Guillem's avatar
Thomas Guillem committed
46
#include <vlc_dialog.h>      /* vlc_dialog_display_error */
47
#include <vlc_charset.h>     /* FromWide */
Gildas Bazin's avatar
 
Gildas Bazin committed
48

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
49 50 51
#include <initguid.h>
#include "vlc_dshow.h"

52
#include "access.h"
Gildas Bazin's avatar
 
Gildas Bazin committed
53 54
#include "filter.h"

55 56 57
#define INSTANCEDATA_OF_PROPERTY_PTR(x) ((PKSPROPERTY((x))) + 1)
#define INSTANCEDATA_OF_PROPERTY_SIZE(x) (sizeof((x)) - sizeof(KSPROPERTY))

Gildas Bazin's avatar
 
Gildas Bazin committed
58 59 60
/*****************************************************************************
 * Access: local prototypes
 *****************************************************************************/
61
static block_t *ReadCompressed( access_t *, bool * );
62
static int AccessControl ( access_t *, int, va_list );
Gildas Bazin's avatar
 
Gildas Bazin committed
63

Gildas Bazin's avatar
Gildas Bazin committed
64 65 66
static int Demux       ( demux_t * );
static int DemuxControl( demux_t *, int, va_list );

67
static int OpenDevice( vlc_object_t *, access_sys_t *, std::string, bool );
68
static ComPtr<IBaseFilter> FindCaptureDevice( vlc_object_t *, std::string *,
69
                                       std::list<std::string> *, bool );
70
static size_t EnumDeviceCaps( vlc_object_t *, IBaseFilter *,
71
                              int, int, int, int, int, int,
72
                              AM_MEDIA_TYPE *mt, size_t, bool );
Gildas Bazin's avatar
Gildas Bazin committed
73 74
static bool ConnectFilters( vlc_object_t *, access_sys_t *,
                            IBaseFilter *, CaptureFilter * );
75
static int FindDevices( vlc_object_t *, const char *, char ***, char *** );
Gildas Bazin's avatar
 
Gildas Bazin committed
76

77
static void ShowPropertyPage( IUnknown * );
78
static void ShowDeviceProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
79
                                  IBaseFilter *, bool );
80
static void ShowTunerProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
81
                                 IBaseFilter *, bool );
82 83
static void ConfigTuner( vlc_object_t *, ICaptureGraphBuilder2 *,
                         IBaseFilter * );
Gildas Bazin's avatar
 
Gildas Bazin committed
84

Gildas Bazin's avatar
 
Gildas Bazin committed
85
/*****************************************************************************
86
 * Module descriptor
Gildas Bazin's avatar
 
Gildas Bazin committed
87
 *****************************************************************************/
88 89
static const int pi_tuner_input[] = { 0, 1, 2 };
static const char *const ppsz_tuner_input_text[] =
90
    {N_("Default"), N_("Cable"), N_("Antenna")};
91

92
static const int pi_amtuner_mode[]  = { AMTUNER_MODE_DEFAULT,
93 94 95 96
                                        AMTUNER_MODE_TV,
                                        AMTUNER_MODE_FM_RADIO,
                                        AMTUNER_MODE_AM_RADIO,
                                        AMTUNER_MODE_DSS };
97
static const char *const ppsz_amtuner_mode_text[] = { N_("Default"),
98 99 100 101
                                          N_("TV"),
                                          N_("FM radio"),
                                          N_("AM radio"),
                                          N_("DSS") };
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
static const int i_standards_list[] =
    {
        KS_AnalogVideo_None,
        KS_AnalogVideo_NTSC_M, KS_AnalogVideo_NTSC_M_J, KS_AnalogVideo_NTSC_433,
        KS_AnalogVideo_PAL_B, KS_AnalogVideo_PAL_D, KS_AnalogVideo_PAL_G,
        KS_AnalogVideo_PAL_H, KS_AnalogVideo_PAL_I, KS_AnalogVideo_PAL_M,
        KS_AnalogVideo_PAL_N, KS_AnalogVideo_PAL_60,
        KS_AnalogVideo_SECAM_B, KS_AnalogVideo_SECAM_D, KS_AnalogVideo_SECAM_G,
        KS_AnalogVideo_SECAM_H, KS_AnalogVideo_SECAM_K, KS_AnalogVideo_SECAM_K1,
        KS_AnalogVideo_SECAM_L, KS_AnalogVideo_SECAM_L1,
        KS_AnalogVideo_PAL_N_COMBO
    };
static const char *const ppsz_standards_list_text[] =
    {
        N_("Default"),
        "NTSC_M", "NTSC_M_J", "NTSC_443",
        "PAL_B", "PAL_D", "PAL_G",
        "PAL_H", "PAL_I", "PAL_M",
        "PAL_N", "PAL_60",
        "SECAM_B", "SECAM_D", "SECAM_G",
        "SECAM_H", "SECAM_K", "SECAM_K1",
        "SECAM_L", "SECAM_L1",
        "PAL_N_COMBO"
    };

128 129
#define VDEV_TEXT N_("Video device name")
#define VDEV_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
130
    "Name of the video device that will be used by the " \
131 132 133 134
    "DirectShow plugin. If you don't specify anything, the default device " \
    "will be used.")
#define ADEV_TEXT N_("Audio device name")
#define ADEV_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
135
    "Name of the audio device that will be used by the " \
136
    "DirectShow plugin. If you don't specify anything, the default device " \
Christophe Mutricy's avatar
Christophe Mutricy committed
137
    "will be used. ")
138 139
#define SIZE_TEXT N_("Video size")
#define SIZE_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
140
    "Size of the video that will be displayed by the " \
141
    "DirectShow plugin. If you don't specify anything the default size for " \
Christophe Mutricy's avatar
Christophe Mutricy committed
142
    "your device will be used. You can specify a standard size (cif, d1, ...) or <width>x<height>.")
143 144
#define ASPECT_TEXT N_("Picture aspect-ratio n:m")
#define ASPECT_LONGTEXT N_("Define input picture aspect-ratio to use. Default is 4:3" )
145 146 147
#define CHROMA_TEXT N_("Video input chroma format")
#define CHROMA_LONGTEXT N_( \
    "Force the DirectShow video input to use a specific chroma format " \
148
    "(eg. I420 (default), RV24, etc.)")
149 150
#define FPS_TEXT N_("Video input frame rate")
#define FPS_LONGTEXT N_( \
Christoph Miebach's avatar
Christoph Miebach committed
151
    "Force the DirectShow video input to use a specific frame rate " \
Christophe Mutricy's avatar
Christophe Mutricy committed
152
    "(eg. 0 means default, 25, 29.97, 50, 59.94, etc.)")
153
#define CONFIG_TEXT N_("Device properties")
Gildas Bazin's avatar
 
Gildas Bazin committed
154
#define CONFIG_LONGTEXT N_( \
155 156
    "Show the properties dialog of the selected device before starting the " \
    "stream.")
157 158 159
#define TUNER_TEXT N_("Tuner properties")
#define TUNER_LONGTEXT N_( \
    "Show the tuner properties [channel selection] page." )
160 161
#define CHANNEL_TEXT N_("Tuner TV Channel")
#define CHANNEL_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
162
    "Set the TV channel the tuner will set to " \
163
    "(0 means default)." )
164 165
#define TVFREQ_TEXT N_("Tuner Frequency")
#define TVFREQ_LONGTEXT N_(  "This overrides the channel. Measured in Hz." )
166
#define STANDARD_TEXT N_( "Video standard" )
167 168
#define COUNTRY_TEXT N_("Tuner country code")
#define COUNTRY_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
169
    "Set the tuner country code that establishes the current " \
170 171 172
    "channel-to-frequency mapping (0 means default)." )
#define TUNER_INPUT_TEXT N_("Tuner input type")
#define TUNER_INPUT_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
173
    "Select the tuner input type (Cable/Antenna)." )
174 175
#define VIDEO_IN_TEXT N_("Video input pin")
#define VIDEO_IN_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
176
  "Select the video input source, such as composite, s-video, " \
177
  "or tuner. Since these settings are hardware-specific, you should find good " \
Clément Stenac's avatar
Clément Stenac committed
178 179
  "settings in the \"Device config\" area, and use those numbers here. -1 " \
  "means that settings will not be changed.")
180 181
#define AUDIO_IN_TEXT N_("Audio input pin")
#define AUDIO_IN_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
182
  "Select the audio input source. See the \"video input\" option." )
183 184
#define VIDEO_OUT_TEXT N_("Video output pin")
#define VIDEO_OUT_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
185
  "Select the video output type. See the \"video input\" option." )
186 187
#define AUDIO_OUT_TEXT N_("Audio output pin")
#define AUDIO_OUT_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
188
  "Select the audio output type. See the \"video input\" option." )
Gildas Bazin's avatar
 
Gildas Bazin committed
189

190
#define AMTUNER_MODE_TEXT N_("AM Tuner mode")
191
#define AMTUNER_MODE_LONGTEXT N_( \
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
192
    "AM Tuner mode. Can be one of Default (0), TV (1), " \
193
     "AM Radio (2), FM Radio (3) or DSS (4).")
194

195 196
#define AUDIO_CHANNELS_TEXT N_("Number of audio channels")
#define AUDIO_CHANNELS_LONGTEXT N_( \
Felix Paul Kühne's avatar
Felix Paul Kühne committed
197
    "Select audio input format with the given number of audio channels (if non 0)" )
198 199 200 201 202 203 204 205 206

#define AUDIO_SAMPLERATE_TEXT N_("Audio sample rate")
#define AUDIO_SAMPLERATE_LONGTEXT N_( \
    "Select audio input format with the given sample rate (if non 0)" )

#define AUDIO_BITSPERSAMPLE_TEXT N_("Audio bits per sample")
#define AUDIO_BITSPERSAMPLE_LONGTEXT N_( \
    "Select audio input format with the given bits/sample (if non 0)" )

207
static int  CommonOpen ( vlc_object_t *, access_sys_t *, bool );
Gildas Bazin's avatar
Gildas Bazin committed
208 209
static void CommonClose( vlc_object_t *, access_sys_t * );

210 211 212 213 214 215
static int  AccessOpen ( vlc_object_t * );
static void AccessClose( vlc_object_t * );

static int  DemuxOpen  ( vlc_object_t * );
static void DemuxClose ( vlc_object_t * );

216 217 218 219 220
vlc_module_begin ()
    set_shortname( N_("DirectShow") )
    set_description( N_("DirectShow input") )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACCESS )
221

222
    add_string( CFG_PREFIX "vdev", NULL, VDEV_TEXT, VDEV_LONGTEXT, false)
223
        change_string_cb( FindDevices )
224

225
    add_string( CFG_PREFIX "adev", NULL, ADEV_TEXT, ADEV_LONGTEXT, false)
226
        change_string_cb( FindDevices )
227

228
    add_string( CFG_PREFIX "size", NULL, SIZE_TEXT, SIZE_LONGTEXT, false)
229
        change_safe()
230

231
    add_string( CFG_PREFIX "aspect-ratio", "4:3", ASPECT_TEXT, ASPECT_LONGTEXT, false)
232
        change_safe()
233

234
    add_string( CFG_PREFIX "chroma", NULL, CHROMA_TEXT, CHROMA_LONGTEXT, true )
235
        change_safe()
Gildas Bazin's avatar
 
Gildas Bazin committed
236

237
    add_float( CFG_PREFIX "fps", 0.0f, FPS_TEXT, FPS_LONGTEXT, true )
238
        change_safe()
239

240
    add_bool( CFG_PREFIX "config", false, CONFIG_TEXT, CONFIG_LONGTEXT, true )
Gildas Bazin's avatar
 
Gildas Bazin committed
241

242
    add_bool( CFG_PREFIX "tuner", false, TUNER_TEXT, TUNER_LONGTEXT, true )
243

244
    add_integer( CFG_PREFIX "tuner-channel", 0, CHANNEL_TEXT, CHANNEL_LONGTEXT,
245
                true )
246
        change_safe()
247

248
    add_integer( CFG_PREFIX "tuner-frequency", 0, TVFREQ_TEXT, TVFREQ_LONGTEXT,
249
                true )
250
        change_safe()
251

252
    add_integer( CFG_PREFIX "tuner-country", 0, COUNTRY_TEXT, COUNTRY_LONGTEXT,
253
                true )
254

255
    add_integer( CFG_PREFIX "tuner-standard", 0, STANDARD_TEXT, STANDARD_TEXT,
256
                false )
257
        change_integer_list( i_standards_list, ppsz_standards_list_text )
258
        change_safe()
259

260
    add_integer( CFG_PREFIX "tuner-input", 0, TUNER_INPUT_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
261
                 TUNER_INPUT_LONGTEXT, true )
262
        change_integer_list( pi_tuner_input, ppsz_tuner_input_text )
263
        change_safe()
264

265
    add_integer( CFG_PREFIX "video-input",  -1, VIDEO_IN_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
266
                 VIDEO_IN_LONGTEXT, true )
267
        change_safe()
268

269
    add_integer( CFG_PREFIX "video-output", -1, VIDEO_OUT_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
270
                 VIDEO_OUT_LONGTEXT, true )
271

272
    add_integer( CFG_PREFIX "audio-input",  -1, AUDIO_IN_TEXT,
273
                 AUDIO_IN_LONGTEXT, true )
274
        change_safe()
275

276
    add_integer( CFG_PREFIX "audio-output", -1, AUDIO_OUT_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
277
                 AUDIO_OUT_LONGTEXT, true )
278

279
    add_integer( CFG_PREFIX "amtuner-mode", AMTUNER_MODE_TV,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
280
                AMTUNER_MODE_TEXT, AMTUNER_MODE_LONGTEXT, false)
281
        change_integer_list( pi_amtuner_mode, ppsz_amtuner_mode_text )
282
        change_safe()
283

284
    add_integer( CFG_PREFIX "audio-channels", 0, AUDIO_CHANNELS_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
285
                 AUDIO_CHANNELS_LONGTEXT, true )
286
    add_integer( CFG_PREFIX "audio-samplerate", 0, AUDIO_SAMPLERATE_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
287
                 AUDIO_SAMPLERATE_LONGTEXT, true )
288
    add_integer( CFG_PREFIX "audio-bitspersample", 0, AUDIO_BITSPERSAMPLE_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
289
                 AUDIO_BITSPERSAMPLE_LONGTEXT, true )
290

291 292 293
    add_shortcut( "dshow" )
    set_capability( "access_demux", 0 )
    set_callbacks( DemuxOpen, DemuxClose )
Gildas Bazin's avatar
 
Gildas Bazin committed
294

295 296
    add_submodule ()
    set_description( N_("DirectShow input") )
297
    add_shortcut( "dshow" )
298 299
    set_capability( "access", 0 )
    set_callbacks( AccessOpen, AccessClose )
Gildas Bazin's avatar
 
Gildas Bazin committed
300

301
vlc_module_end ()
Gildas Bazin's avatar
 
Gildas Bazin committed
302

303 304 305 306 307 308 309 310 311 312 313 314
struct ComContext
{
    ComContext( int mode )
    {
        if( FAILED( CoInitializeEx( NULL, mode ) ) )
            throw std::runtime_error( "CoInitializeEx failed" );
    }
    ~ComContext()
    {
        CoUninitialize();
    }
};
315

Gildas Bazin's avatar
Gildas Bazin committed
316
/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
317
 * DirectShow elementary stream descriptor
Gildas Bazin's avatar
Gildas Bazin committed
318
 *****************************************************************************/
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
319
struct dshow_stream_t
Gildas Bazin's avatar
 
Gildas Bazin committed
320
{
321 322 323
    std::string           devicename;
    ComPtr<IBaseFilter>   p_device_filter;
    ComPtr<CaptureFilter> p_capture_filter;
Gildas Bazin's avatar
 
Gildas Bazin committed
324 325 326 327 328 329 330 331 332
    AM_MEDIA_TYPE   mt;

    union
    {
      VIDEOINFOHEADER video;
      WAVEFORMATEX    audio;

    } header;

Gildas Bazin's avatar
Gildas Bazin committed
333 334 335
    int             i_fourcc;
    es_out_id_t     *p_es;

336
    bool      b_pts;
337

338
    std::deque<VLCMediaSample> samples_queue;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
339
};
Gildas Bazin's avatar
 
Gildas Bazin committed
340

Gildas Bazin's avatar
Gildas Bazin committed
341
/*****************************************************************************
342
 * DirectShow utility functions
Gildas Bazin's avatar
Gildas Bazin committed
343
 *****************************************************************************/
344 345 346 347 348 349
static void CreateDirectShowGraph( access_sys_t *p_sys )
{
    p_sys->i_crossbar_route_depth = 0;

    /* Create directshow filter graph */
    if( SUCCEEDED( CoCreateInstance( CLSID_FilterGraph, 0, CLSCTX_INPROC,
350
                       (REFIID)IID_IFilterGraph, (void**)p_sys->p_graph.GetAddressOf() ) ) )
351 352 353 354
    {
        /* Create directshow capture graph builder if available */
        if( SUCCEEDED( CoCreateInstance( CLSID_CaptureGraphBuilder2, 0,
                         CLSCTX_INPROC, (REFIID)IID_ICaptureGraphBuilder2,
355
                         (void**)p_sys->p_capture_graph_builder2.GetAddressOf() ) ) )
356 357
        {
            p_sys->p_capture_graph_builder2->
358
                SetFiltergraph((IGraphBuilder *)p_sys->p_graph.Get() );
359 360 361
        }

        p_sys->p_graph->QueryInterface( IID_IMediaControl,
362
                                        (void**)p_sys->p_control.GetAddressOf() );
363 364 365
    }
}

366
static void DeleteDirectShowGraph( vlc_object_t *p_this, access_sys_t *p_sys )
367 368 369 370
{
    DeleteCrossbarRoutes( p_sys );

    /* Remove filters from graph */
371
    msg_Dbg( p_this, "DeleteDirectShowGraph: Removing filters" );
372 373 374
    std::vector<dshow_stream_t*>::iterator it = p_sys->pp_streams.begin();
    std::vector<dshow_stream_t*>::iterator end = p_sys->pp_streams.end();
    for( ; it != end; ++it )
375
    {
376 377 378
        /* RemoveFilter does an undocumented Release()
         * but does not set item to NULL */
        msg_Dbg( p_this, "DeleteDirectShowGraph: Removing capture filter" );
379 380
        p_sys->p_graph->RemoveFilter( (*it)->p_capture_filter.Get() );
        (*it)->p_capture_filter.Reset();
381 382

        msg_Dbg( p_this, "DeleteDirectShowGraph: Removing device filter" );
383 384
        p_sys->p_graph->RemoveFilter( (*it)->p_device_filter.Get() );
        (*it)->p_device_filter.Reset();
385 386 387
    }

    /* Release directshow objects */
388 389 390
    p_sys->p_control.Reset();
    p_sys->p_capture_graph_builder2.Reset();
    p_sys->p_graph.Reset();
391 392
}

Gildas Bazin's avatar
 
Gildas Bazin committed
393
/*****************************************************************************
Gildas Bazin's avatar
Gildas Bazin committed
394
 * CommonOpen: open direct show device
Gildas Bazin's avatar
 
Gildas Bazin committed
395
 *****************************************************************************/
Gildas Bazin's avatar
Gildas Bazin committed
396
static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
397
                       bool b_access_demux )
Gildas Bazin's avatar
 
Gildas Bazin committed
398
{
399
    char *psz_val;
Gildas Bazin's avatar
 
Gildas Bazin committed
400

401
    /* Get/parse options and open device(s) */
402
    std::string vdevname, adevname;
403 404
    int i_width = 0, i_height = 0;
    vlc_fourcc_t i_chroma = 0;
405 406 407
    bool b_use_audio = true;
    bool b_use_video = true;

408 409 410
    var_Create( p_this,  CFG_PREFIX "config", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_this,  CFG_PREFIX "tuner", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    psz_val = var_CreateGetString( p_this, CFG_PREFIX "vdev" );
411
    if( psz_val )
412
    {
413
        msg_Dbg( p_this, "dshow-vdev: %s", psz_val ) ;
414
        /* skip none device */
415
        if ( strncasecmp( psz_val, "none", 4 ) != 0 )
416
            vdevname = std::string( psz_val );
417 418 419
        else
            b_use_video = false ;
    }
420
    free( psz_val );
Gildas Bazin's avatar
 
Gildas Bazin committed
421

422
    psz_val = var_CreateGetString( p_this, CFG_PREFIX "adev" );
423
    if( psz_val )
424
    {
425
        msg_Dbg( p_this, "dshow-adev: %s", psz_val ) ;
426
        /* skip none device */
427
        if ( strncasecmp( psz_val, "none", 4 ) != 0 )
428
            adevname = std::string( psz_val );
429 430 431
        else
            b_use_audio = false ;
    }
432
    free( psz_val );
433

434 435 436 437 438 439 440 441 442 443 444 445
    /* DShow Size */
    static struct {
        const char *psz_size;
        int  i_width;
        int  i_height;
    } size_table[] =
    { { "subqcif", 128, 96  },
      {    "qsif", 160, 120 },
      {    "qcif", 176, 144 },
      {     "sif", 320, 240 },
      {     "cif", 352, 288 },
      {      "d1", 640, 480 },
Gildas Bazin's avatar
Gildas Bazin committed
446 447 448
      { 0, 0, 0 },
    };

449
    psz_val = var_CreateGetString( p_this, CFG_PREFIX "size" );
450
    if( !EMPTY_STR(psz_val) )
Gildas Bazin's avatar
 
Gildas Bazin committed
451
    {
452
        int i;
Gildas Bazin's avatar
Gildas Bazin committed
453
        for( i = 0; size_table[i].psz_size; i++ )
454
        {
455
            if( !strcmp( psz_val, size_table[i].psz_size ) )
Gildas Bazin's avatar
Gildas Bazin committed
456 457 458 459 460
            {
                i_width = size_table[i].i_width;
                i_height = size_table[i].i_height;
                break;
            }
461
        }
Gildas Bazin's avatar
Gildas Bazin committed
462
        if( !size_table[i].psz_size ) /* Try to parse "WidthxHeight" */
463 464
        {
            char *psz_parser;
465
            i_width = strtol( psz_val, &psz_parser, 0 );
466
            if( *psz_parser == 'x' || *psz_parser == 'X')
Gildas Bazin's avatar
 
Gildas Bazin committed
467
            {
468
                i_height = strtol( psz_parser + 1, &psz_parser, 0 );
Gildas Bazin's avatar
 
Gildas Bazin committed
469
            }
Clément Stenac's avatar
Clément Stenac committed
470
            msg_Dbg( p_this, "width x height %dx%d", i_width, i_height );
Gildas Bazin's avatar
 
Gildas Bazin committed
471 472
        }
    }
473
    free( psz_val );
Gildas Bazin's avatar
 
Gildas Bazin committed
474

475
    /* Chroma */
476
    psz_val = var_CreateGetString( p_this, CFG_PREFIX "chroma" );
477
    i_chroma = vlc_fourcc_GetCodecFromString( UNKNOWN_ES, psz_val );
478
    p_sys->b_chroma = i_chroma != 0;
479
    free( psz_val );
Gildas Bazin's avatar
 
Gildas Bazin committed
480

481 482
    var_Create( p_this, CFG_PREFIX "fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
    var_Create( p_this, CFG_PREFIX "tuner-channel",
483
                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
484
    var_Create( p_this, CFG_PREFIX "tuner-frequency",
485
                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
486
    var_Create( p_this, CFG_PREFIX "tuner-standard",
487
                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
488
    var_Create( p_this, CFG_PREFIX "tuner-country",
489
                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
490
    var_Create( p_this, CFG_PREFIX "tuner-input",
491 492
                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );

493
    var_Create( p_this, CFG_PREFIX "amtuner-mode",
494 495
                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );

496 497 498 499
    var_Create( p_this, CFG_PREFIX "video-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Create( p_this, CFG_PREFIX "audio-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Create( p_this, CFG_PREFIX "video-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Create( p_this, CFG_PREFIX "audio-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
500

501

Gildas Bazin's avatar
 
Gildas Bazin committed
502
    /* Initialize some data */
Gildas Bazin's avatar
 
Gildas Bazin committed
503 504 505
    p_sys->i_width = i_width;
    p_sys->i_height = i_height;
    p_sys->i_chroma = i_chroma;
Gildas Bazin's avatar
 
Gildas Bazin committed
506

507 508 509
    /* Build directshow graph */
    CreateDirectShowGraph( p_sys );

510
    vlc_mutex_init( &p_sys->lock );
511
    vlc_cond_init( &p_sys->wait );
512

513 514
    if( !b_use_video && !b_use_audio )
    {
Thomas Guillem's avatar
Thomas Guillem committed
515
        vlc_dialog_display_error( p_this, _("Capture failed"),
basos g's avatar
basos g committed
516
                        _("No video or audio device selected.") );
517 518
        return VLC_EGENERIC ;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
519

520 521 522 523 524
    if( !b_use_video )
        msg_Dbg( p_this, "skipping video device" ) ;
    bool b_err_video = false ;

    if( b_use_video && OpenDevice( p_this, p_sys, vdevname, 0 ) != VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
525
    {
526 527
        msg_Err( p_this, "can't open video device");
        b_err_video = true ;
Gildas Bazin's avatar
 
Gildas Bazin committed
528
    }
529 530

    if ( b_use_video && !b_err_video )
Gildas Bazin's avatar
Gildas Bazin committed
531 532 533
    {
        /* Check if we can handle the demuxing ourselves or need to spawn
         * a demuxer module */
534
        dshow_stream_t *p_stream = *(p_sys->pp_streams.rbegin());
Gildas Bazin's avatar
Gildas Bazin committed
535 536 537 538

        if( p_stream->mt.majortype == MEDIATYPE_Video )
        {
            if( /* Raw DV stream */
539
                p_stream->i_fourcc == VLC_CODEC_DV ||
Gildas Bazin's avatar
Gildas Bazin committed
540
                /* Raw MPEG video stream */
Laurent Aimar's avatar
Laurent Aimar committed
541
                p_stream->i_fourcc == VLC_CODEC_MPGV )
Gildas Bazin's avatar
Gildas Bazin committed
542
            {
543
                b_use_audio = false;
544 545 546 547

                if( b_access_demux )
                {
                    /* Let the access (only) take care of that */
548
                    return VLC_EGENERIC;
549
                }
Gildas Bazin's avatar
Gildas Bazin committed
550 551
            }
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
552

553 554
        if( p_stream->mt.majortype == MEDIATYPE_Stream )
        {
555
            b_use_audio = false;
556 557 558 559 560 561

            if( b_access_demux )
            {
                /* Let the access (only) take care of that */
                return VLC_EGENERIC;
            }
562

563
            if( var_GetBool( p_this, CFG_PREFIX "tuner" ) )
564 565 566
            {
                /* FIXME: we do MEDIATYPE_Stream here so we don't do
                 * it twice. */
567 568
                ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2.Get(),
                                     p_stream->p_device_filter.Get(), 0 );
569
            }
570
        }
Gildas Bazin's avatar
Gildas Bazin committed
571 572
    }

573 574 575 576 577 578
    if( !b_use_audio )
        msg_Dbg( p_this, "skipping audio device") ;

    bool b_err_audio = false ;

    if( b_use_audio && OpenDevice( p_this, p_sys, adevname, 1 ) != VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
579
    {
580 581 582 583 584 585 586 587 588
        msg_Err( p_this, "can't open audio device");
        b_err_audio = true ;
    }

    if( ( b_use_video && b_err_video && b_use_audio && b_err_audio ) ||
        ( !b_use_video && b_use_audio && b_err_audio ) ||
        ( b_use_video && !b_use_audio && b_err_video ) )
    {
        msg_Err( p_this, "FATAL: could not open ANY device" ) ;
Thomas Guillem's avatar
Thomas Guillem committed
589
        vlc_dialog_display_error( p_this,  _("Capture failed"),
590
                        _("VLC cannot open ANY capture device. "
591 592
                          "Check the error log for details.") );
        return VLC_EGENERIC ;
Gildas Bazin's avatar
 
Gildas Bazin committed
593
    }
Gildas Bazin's avatar
Gildas Bazin committed
594

595
    for( int i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
Gildas Bazin's avatar
 
Gildas Bazin committed
596
    {
597
        int i_val = var_GetInteger( p_this, CFG_PREFIX "video-input" );
598 599
        if( i_val >= 0 )
            p_sys->crossbar_routes[i].VideoInputIndex = i_val;
600
        i_val = var_GetInteger( p_this, CFG_PREFIX "video-output" );
601 602
        if( i_val >= 0 )
            p_sys->crossbar_routes[i].VideoOutputIndex = i_val;
603
        i_val = var_GetInteger( p_this, CFG_PREFIX "audio-input" );
604 605
        if( i_val >= 0 )
            p_sys->crossbar_routes[i].AudioInputIndex = i_val;
606
        i_val = var_GetInteger( p_this, CFG_PREFIX "audio-output" );
607 608
        if( i_val >= 0 )
            p_sys->crossbar_routes[i].AudioOutputIndex = i_val;
609

610
        IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar.Get();
611 612 613 614
        LONG VideoInputIndex = p_sys->crossbar_routes[i].VideoInputIndex;
        LONG VideoOutputIndex = p_sys->crossbar_routes[i].VideoOutputIndex;
        LONG AudioInputIndex = p_sys->crossbar_routes[i].AudioInputIndex;
        LONG AudioOutputIndex = p_sys->crossbar_routes[i].AudioOutputIndex;
Gildas Bazin's avatar
 
Gildas Bazin committed
615

616 617
        if( SUCCEEDED(pXbar->Route(VideoOutputIndex, VideoInputIndex)) )
        {
Clément Stenac's avatar
Clément Stenac committed
618 619
            msg_Dbg( p_this, "crossbar at depth %d, routed video "
                     "output %ld to video input %ld", i, VideoOutputIndex,
620
                     VideoInputIndex );
Gildas Bazin's avatar
 
Gildas Bazin committed
621

622 623 624 625 626
            if( AudioOutputIndex != -1 && AudioInputIndex != -1 )
            {
                if( SUCCEEDED( pXbar->Route(AudioOutputIndex,
                                            AudioInputIndex)) )
                {
Clément Stenac's avatar
Clément Stenac committed
627 628
                    msg_Dbg(p_this, "crossbar at depth %d, routed audio "
                            "output %ld to audio input %ld", i,
629 630 631 632
                            AudioOutputIndex, AudioInputIndex );
                }
            }
        }
633 634 635
        else
            msg_Err( p_this, "crossbar at depth %d could not route video "
                     "output %ld to input %ld", i, VideoOutputIndex, VideoInputIndex );
636 637
    }

638 639 640
    /*
    ** Show properties pages from other filters in graph
    */
641
    if( var_GetBool( p_this, CFG_PREFIX "config" ) )
642
    {
643
        for( int i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
644
        {
645 646
            IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar.Get();
            ComPtr<IBaseFilter> p_XF;
647 648

            if( SUCCEEDED( pXbar->QueryInterface( IID_IBaseFilter,
649
                                                  (void**)p_XF.ReleaseAndGetAddressOf() ) ) )
650
            {
651
                ShowPropertyPage( p_XF.Get() );
652
            }
653
        }
654 655
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
656 657
    /* Initialize some data */
    p_sys->i_current_stream = 0;
658

659
    if( p_sys->pp_streams.empty() ) return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
660

Gildas Bazin's avatar
 
Gildas Bazin committed
661 662 663 664
    return VLC_SUCCESS;
}

/*****************************************************************************
Gildas Bazin's avatar
Gildas Bazin committed
665
 * DemuxOpen: open direct show device as an access_demux module
Gildas Bazin's avatar
 
Gildas Bazin committed
666
 *****************************************************************************/
Gildas Bazin's avatar
Gildas Bazin committed
667
static int DemuxOpen( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
668
{
Gildas Bazin's avatar
Gildas Bazin committed
669 670
    demux_t      *p_demux = (demux_t *)p_this;
    access_sys_t *p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
671

Rémi Duraffort's avatar
Rémi Duraffort committed
672
    p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
Rémi Duraffort's avatar
Rémi Duraffort committed
673 674
    if( !p_sys )
        return VLC_ENOMEM;
Gildas Bazin's avatar
Gildas Bazin committed
675
    p_demux->p_sys = (demux_sys_t *)p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
676

677 678
    ComContext ctx( COINIT_MULTITHREADED );

679
    if( CommonOpen( p_this, p_sys, true ) != VLC_SUCCESS )
Gildas Bazin's avatar
Gildas Bazin committed
680 681 682 683
    {
        CommonClose( p_this, p_sys );
        return VLC_EGENERIC;
    }
684

Gildas Bazin's avatar
Gildas Bazin committed
685 686
    /* Everything is ready. Let's rock baby */
    msg_Dbg( p_this, "Playing...");
687 688 689 690 691 692
    if( FAILED( p_sys->p_control->Run() ) )
    {
        msg_Err( p_this, "Failed to run graph. Capture device may be in use." );
        CommonClose( p_this, p_sys );
        return VLC_EGENERIC;
    }
693

Gildas Bazin's avatar
Gildas Bazin committed
694 695 696 697 698
    p_demux->pf_demux   = Demux;
    p_demux->pf_control = DemuxControl;
    p_demux->info.i_title = 0;
    p_demux->info.i_seekpoint = 0;

699 700 701
    std::vector<dshow_stream_t*>::iterator it = p_sys->pp_streams.begin();
    std::vector<dshow_stream_t*>::iterator end = p_sys->pp_streams.end();
    for ( ; it != end; ++it )
702
    {
703
        dshow_stream_t *p_stream = *it;
Gildas Bazin's avatar
Gildas Bazin committed
704
        es_format_t fmt;
705

Gildas Bazin's avatar
Gildas Bazin committed
706 707
        if( p_stream->mt.majortype == MEDIATYPE_Video )
        {
708
            char *psz_aspect = var_CreateGetString( p_this, CFG_PREFIX "aspect-ratio" );
709 710
            char *psz_delim = !EMPTY_STR( psz_aspect ) ? strchr( psz_aspect, ':' ) : NULL;

Gildas Bazin's avatar
Gildas Bazin committed
711
            es_format_Init( &fmt, VIDEO_ES, p_stream->i_fourcc );
712

Gildas Bazin's avatar
Gildas Bazin committed
713 714
            fmt.video.i_width  = p_stream->header.video.bmiHeader.biWidth;
            fmt.video.i_height = p_stream->header.video.bmiHeader.biHeight;
715 716 717 718 719 720 721 722 723 724 725 726

            if( psz_delim )
            {
                fmt.video.i_sar_num = atoi( psz_aspect ) * fmt.video.i_height;
                fmt.video.i_sar_den = atoi( psz_delim + 1 ) * fmt.video.i_width;
            }
            else
            {
                fmt.video.i_sar_num = 4 * fmt.video.i_height;
                fmt.video.i_sar_den = 3 * fmt.video.i_width;
            }
            free( psz_aspect );
727

Gildas Bazin's avatar
Gildas Bazin committed
728 729 730
            if( !p_stream->header.video.bmiHeader.biCompression )
            {
                /* RGB DIB are coded from bottom to top */
731
                fmt.video.orientation = ORIENT_BOTTOM_LEFT;
Gildas Bazin's avatar
Gildas Bazin committed
732
            }
733

Gildas Bazin's avatar
Gildas Bazin committed
734
            /* Setup rgb mask for RGB formats */
735
            if( p_stream->i_fourcc == VLC_CODEC_RGB24 )
Gildas Bazin's avatar
Gildas Bazin committed
736
            {
737 738 739 740
                /* This is in RGB format
            http://msdn.microsoft.com/en-us/library/dd407253%28VS.85%29.aspx?ppud=4
                 */
                fmt.video.i_rmask = 0x00ff0000;
741
                fmt.video.i_gmask = 0x0000ff00;
742
                fmt.video.i_bmask = 0x000000ff;
Gildas Bazin's avatar
Gildas Bazin committed
743
            }
744 745 746 747 748 749 750

            if( p_stream->header.video.AvgTimePerFrame )
            {
                fmt.video.i_frame_rate = 10000000;
                fmt.video.i_frame_rate_base =
                    p_stream->header.video.AvgTimePerFrame;
            }
Gildas Bazin's avatar
Gildas Bazin committed
751 752 753 754
        }
        else if( p_stream->mt.majortype == MEDIATYPE_Audio )
        {
            es_format_Init( &fmt, AUDIO_ES, p_stream->i_fourcc );