dshow.cpp 82.2 KB
Newer Older
Gildas Bazin's avatar
 
Gildas Bazin committed
1
/*****************************************************************************
2
 * dshow.cpp : DirectShow access module for vlc
Gildas Bazin's avatar
 
Gildas Bazin committed
3
 *****************************************************************************
4
 * Copyright (C) 2002, 2003 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 __STDC_CONSTANT_MACROS 1
34 35 36
#define __STDC_FORMAT_MACROS 1
#include <inttypes.h>

37
#include <vlc_common.h>
38
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
39 40
#include <vlc_input.h>
#include <vlc_access.h>
41
#include <vlc_demux.h>
42
#include <vlc_dialog.h>
43
#include <vlc_charset.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
44

Gildas Bazin's avatar
Gildas Bazin committed
45
#include "common.h"
Gildas Bazin's avatar
 
Gildas Bazin committed
46 47
#include "filter.h"

Gildas Bazin's avatar
 
Gildas Bazin committed
48 49 50
/*****************************************************************************
 * Access: local prototypes
 *****************************************************************************/
51
static block_t *ReadCompressed( access_t * );
52
static int AccessControl ( access_t *, int, va_list );
Gildas Bazin's avatar
 
Gildas Bazin committed
53

Gildas Bazin's avatar
Gildas Bazin committed
54 55 56
static int Demux       ( demux_t * );
static int DemuxControl( demux_t *, int, va_list );

57
static int OpenDevice( vlc_object_t *, access_sys_t *, string, bool );
Gildas Bazin's avatar
 
Gildas Bazin committed
58
static IBaseFilter *FindCaptureDevice( vlc_object_t *, string *,
59
                                       list<string> *, bool );
60
static size_t EnumDeviceCaps( vlc_object_t *, IBaseFilter *,
61 62
                              int, int, int, int, int, int,
                              AM_MEDIA_TYPE *mt, size_t );
Gildas Bazin's avatar
Gildas Bazin committed
63 64
static bool ConnectFilters( vlc_object_t *, access_sys_t *,
                            IBaseFilter *, CaptureFilter * );
Gildas Bazin's avatar
 
Gildas Bazin committed
65 66
static int FindDevicesCallback( vlc_object_t *, char const *,
                                vlc_value_t, vlc_value_t, void * );
67 68
static int ConfigDevicesCallback( vlc_object_t *, char const *,
                                  vlc_value_t, vlc_value_t, void * );
Gildas Bazin's avatar
 
Gildas Bazin committed
69

70
static void ShowPropertyPage( IUnknown * );
71
static void ShowDeviceProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
72
                                  IBaseFilter *, bool );
73
static void ShowTunerProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
74
                                 IBaseFilter *, bool );
75 76
static void ConfigTuner( vlc_object_t *, ICaptureGraphBuilder2 *,
                         IBaseFilter * );
Gildas Bazin's avatar
 
Gildas Bazin committed
77

Gildas Bazin's avatar
 
Gildas Bazin committed
78
/*****************************************************************************
79
 * Module descriptor
Gildas Bazin's avatar
 
Gildas Bazin committed
80
 *****************************************************************************/
81 82 83 84 85 86
static const char *const ppsz_vdev[] = { "", "none" };
static const char *const ppsz_vdev_text[] = { N_("Default"), N_("None") };
static const char *const ppsz_adev[] = { "", "none" };
static const char *const ppsz_adev_text[] = { N_("Default"), N_("None") };
static const int pi_tuner_input[] = { 0, 1, 2 };
static const char *const ppsz_tuner_input_text[] =
87
    {N_("Default"), N_("Cable"), N_("Antenna")};
88
static const int pi_amtuner_mode[]  = { AMTUNER_MODE_DEFAULT,
89 90 91 92
                                 AMTUNER_MODE_TV,
                                 AMTUNER_MODE_FM_RADIO,
                                 AMTUNER_MODE_AM_RADIO,
                                 AMTUNER_MODE_DSS };
93
static const char *const ppsz_amtuner_mode_text[] = { N_("Default"),
94 95 96 97
                                          N_("TV"),
                                          N_("FM radio"),
                                          N_("AM radio"),
                                          N_("DSS") };
98

Gildas Bazin's avatar
 
Gildas Bazin committed
99 100
#define CACHING_TEXT N_("Caching value in ms")
#define CACHING_LONGTEXT N_( \
101
    "Caching value for DirectShow streams. " \
102
    "This value should be set in milliseconds." )
103 104
#define VDEV_TEXT N_("Video device name")
#define VDEV_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
105
    "Name of the video device that will be used by the " \
106 107 108 109
    "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
110
    "Name of the audio device that will be used by the " \
111
    "DirectShow plugin. If you don't specify anything, the default device " \
Christophe Mutricy's avatar
Christophe Mutricy committed
112
    "will be used. ")
113 114
#define SIZE_TEXT N_("Video size")
#define SIZE_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
115
    "Size of the video that will be displayed by the " \
116
    "DirectShow plugin. If you don't specify anything the default size for " \
Christophe Mutricy's avatar
Christophe Mutricy committed
117
    "your device will be used. You can specify a standard size (cif, d1, ...) or <width>x<height>.")
118 119 120
#define CHROMA_TEXT N_("Video input chroma format")
#define CHROMA_LONGTEXT N_( \
    "Force the DirectShow video input to use a specific chroma format " \
121
    "(eg. I420 (default), RV24, etc.)")
122 123 124
#define FPS_TEXT N_("Video input frame rate")
#define FPS_LONGTEXT N_( \
    "Force the DirectShow video input to use a specific frame rate" \
Christophe Mutricy's avatar
Christophe Mutricy committed
125
    "(eg. 0 means default, 25, 29.97, 50, 59.94, etc.)")
126
#define CONFIG_TEXT N_("Device properties")
Gildas Bazin's avatar
 
Gildas Bazin committed
127
#define CONFIG_LONGTEXT N_( \
128 129
    "Show the properties dialog of the selected device before starting the " \
    "stream.")
130 131 132
#define TUNER_TEXT N_("Tuner properties")
#define TUNER_LONGTEXT N_( \
    "Show the tuner properties [channel selection] page." )
133 134
#define CHANNEL_TEXT N_("Tuner TV Channel")
#define CHANNEL_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
135
    "Set the TV channel the tuner will set to " \
136 137 138
    "(0 means default)." )
#define COUNTRY_TEXT N_("Tuner country code")
#define COUNTRY_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
139
    "Set the tuner country code that establishes the current " \
140 141 142
    "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
143
    "Select the tuner input type (Cable/Antenna)." )
144 145
#define VIDEO_IN_TEXT N_("Video input pin")
#define VIDEO_IN_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
146
  "Select the video input source, such as composite, s-video, " \
147
  "or tuner. Since these settings are hardware-specific, you should find good " \
Clément Stenac's avatar
Clément Stenac committed
148 149
  "settings in the \"Device config\" area, and use those numbers here. -1 " \
  "means that settings will not be changed.")
150 151
#define AUDIO_IN_TEXT N_("Audio input pin")
#define AUDIO_IN_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
152
  "Select the audio input source. See the \"video input\" option." )
153 154
#define VIDEO_OUT_TEXT N_("Video output pin")
#define VIDEO_OUT_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
155
  "Select the video output type. See the \"video input\" option." )
156 157
#define AUDIO_OUT_TEXT N_("Audio output pin")
#define AUDIO_OUT_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
158
  "Select the audio output type. See the \"video input\" option." )
Gildas Bazin's avatar
 
Gildas Bazin committed
159

160
#define AMTUNER_MODE_TEXT N_("AM Tuner mode")
161
#define AMTUNER_MODE_LONGTEXT N_( \
162 163
    "AM Tuner mode. Can be one of Default (0), TV (1)," \
     "AM Radio (2), FM Radio (3) or DSS (4).")
164

165 166
#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
167
    "Select audio input format with the given number of audio channels (if non 0)" )
168 169 170 171 172 173 174 175 176

#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)" )

177
static int  CommonOpen ( vlc_object_t *, access_sys_t *, bool );
Gildas Bazin's avatar
Gildas Bazin committed
178 179
static void CommonClose( vlc_object_t *, access_sys_t * );

180 181 182 183 184 185
static int  AccessOpen ( vlc_object_t * );
static void AccessClose( vlc_object_t * );

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

186 187 188 189 190
vlc_module_begin ()
    set_shortname( N_("DirectShow") )
    set_description( N_("DirectShow input") )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACCESS )
191
    add_integer( "dshow-caching", (mtime_t)(0.2*CLOCK_FREQ) / 1000, NULL,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
192
                 CACHING_TEXT, CACHING_LONGTEXT, true )
193

194
    add_string( "dshow-vdev", NULL, NULL, VDEV_TEXT, VDEV_LONGTEXT, false)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
195
        change_string_list( ppsz_vdev, ppsz_vdev_text, FindDevicesCallback )
196 197
        change_action_add( FindDevicesCallback, N_("Refresh list") )
        change_action_add( ConfigDevicesCallback, N_("Configure") )
198

199
    add_string( "dshow-adev", NULL, NULL, ADEV_TEXT, ADEV_LONGTEXT, false)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
200
        change_string_list( ppsz_adev, ppsz_adev_text, FindDevicesCallback )
201 202
        change_action_add( FindDevicesCallback, N_("Refresh list") )
        change_action_add( ConfigDevicesCallback, N_("Configure") )
203

204
    add_string( "dshow-size", NULL, NULL, SIZE_TEXT, SIZE_LONGTEXT, false)
205

206
    add_string( "dshow-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT, true )
Gildas Bazin's avatar
 
Gildas Bazin committed
207

208
    add_float( "dshow-fps", 0.0f, NULL, FPS_TEXT, FPS_LONGTEXT, true )
209

210
    add_bool( "dshow-config", false, NULL, CONFIG_TEXT, CONFIG_LONGTEXT, true )
Gildas Bazin's avatar
 
Gildas Bazin committed
211

212
    add_bool( "dshow-tuner", false, NULL, TUNER_TEXT, TUNER_LONGTEXT, true )
213

214 215
    add_integer( "dshow-tuner-channel", 0, NULL, CHANNEL_TEXT, CHANNEL_LONGTEXT,
                true )
216

217 218
    add_integer( "dshow-tuner-country", 0, NULL, COUNTRY_TEXT, COUNTRY_LONGTEXT,
                true )
219 220

    add_integer( "dshow-tuner-input", 0, NULL, TUNER_INPUT_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
221 222
                 TUNER_INPUT_LONGTEXT, true )
        change_integer_list( pi_tuner_input, ppsz_tuner_input_text, NULL )
223

224
    add_integer( "dshow-video-input",  -1, NULL, VIDEO_IN_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
225
                 VIDEO_IN_LONGTEXT, true )
226 227

    add_integer( "dshow-video-output", -1, NULL, VIDEO_OUT_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
228
                 VIDEO_OUT_LONGTEXT, true )
229

230 231 232
    add_integer( "dshow-audio-input",  -1, NULL, AUDIO_IN_TEXT,
                 AUDIO_IN_LONGTEXT, true )

233
    add_integer( "dshow-audio-output", -1, NULL, AUDIO_OUT_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
234
                 AUDIO_OUT_LONGTEXT, true )
235

236
    add_integer( "dshow-amtuner-mode", AMTUNER_MODE_TV, NULL,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
237 238
                AMTUNER_MODE_TEXT, AMTUNER_MODE_LONGTEXT, false)
        change_integer_list( pi_amtuner_mode, ppsz_amtuner_mode_text, NULL )
239

240
    add_integer( "dshow-audio-channels", 0, NULL, AUDIO_CHANNELS_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
241
                 AUDIO_CHANNELS_LONGTEXT, true )
242
    add_integer( "dshow-audio-samplerate", 0, NULL, AUDIO_SAMPLERATE_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
243
                 AUDIO_SAMPLERATE_LONGTEXT, true )
244
    add_integer( "dshow-audio-bitspersample", 0, NULL, AUDIO_BITSPERSAMPLE_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
245
                 AUDIO_BITSPERSAMPLE_LONGTEXT, true )
246

247 248 249
    add_shortcut( "dshow" )
    set_capability( "access_demux", 0 )
    set_callbacks( DemuxOpen, DemuxClose )
Gildas Bazin's avatar
 
Gildas Bazin committed
250

251 252
    add_submodule ()
    set_description( N_("DirectShow input") )
253
    add_shortcut( "dshow" )
254 255
    set_capability( "access", 0 )
    set_callbacks( AccessOpen, AccessClose )
Gildas Bazin's avatar
 
Gildas Bazin committed
256

257
vlc_module_end ()
Gildas Bazin's avatar
 
Gildas Bazin committed
258

Gildas Bazin's avatar
Gildas Bazin committed
259
/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
260
 * DirectShow elementary stream descriptor
Gildas Bazin's avatar
Gildas Bazin committed
261
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275
typedef struct dshow_stream_t
{
    string          devicename;
    IBaseFilter     *p_device_filter;
    CaptureFilter   *p_capture_filter;
    AM_MEDIA_TYPE   mt;

    union
    {
      VIDEOINFOHEADER video;
      WAVEFORMATEX    audio;

    } header;

Gildas Bazin's avatar
Gildas Bazin committed
276 277 278
    int             i_fourcc;
    es_out_id_t     *p_es;

279
    bool      b_pts;
280 281

    deque<VLCMediaSample> samples_queue;
Gildas Bazin's avatar
 
Gildas Bazin committed
282 283
} dshow_stream_t;

Gildas Bazin's avatar
Gildas Bazin committed
284
/*****************************************************************************
285
 * DirectShow utility functions
Gildas Bazin's avatar
Gildas Bazin committed
286
 *****************************************************************************/
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
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,
                       (REFIID)IID_IFilterGraph, (void **)&p_sys->p_graph) ) )
    {
        /* Create directshow capture graph builder if available */
        if( SUCCEEDED( CoCreateInstance( CLSID_CaptureGraphBuilder2, 0,
                         CLSCTX_INPROC, (REFIID)IID_ICaptureGraphBuilder2,
                         (void **)&p_sys->p_capture_graph_builder2 ) ) )
        {
            p_sys->p_capture_graph_builder2->
                SetFiltergraph((IGraphBuilder *)p_sys->p_graph);
        }

        p_sys->p_graph->QueryInterface( IID_IMediaControl,
                                        (void **)&p_sys->p_control );
    }
}

static void DeleteDirectShowGraph( access_sys_t *p_sys )
{
    DeleteCrossbarRoutes( p_sys );

    /* Remove filters from graph */
    for( int i = 0; i < p_sys->i_streams; i++ )
    {
        p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_capture_filter );
        p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_device_filter );
        p_sys->pp_streams[i]->p_capture_filter->Release();
        p_sys->pp_streams[i]->p_device_filter->Release();
    }

    /* Release directshow objects */
    if( p_sys->p_control )
    {
        p_sys->p_control->Release();
        p_sys->p_control = NULL;
    }
    if( p_sys->p_capture_graph_builder2 )
    {
        p_sys->p_capture_graph_builder2->Release();
        p_sys->p_capture_graph_builder2 = NULL;
    }

    if( p_sys->p_graph )
    {
        p_sys->p_graph->Release();
        p_sys->p_graph = NULL;
    }
}

Gildas Bazin's avatar
 
Gildas Bazin committed
341
/*****************************************************************************
Gildas Bazin's avatar
Gildas Bazin committed
342
 * CommonOpen: open direct show device
Gildas Bazin's avatar
 
Gildas Bazin committed
343
 *****************************************************************************/
Gildas Bazin's avatar
Gildas Bazin committed
344
static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
345
                       bool b_access_demux )
Gildas Bazin's avatar
 
Gildas Bazin committed
346
{
Gildas Bazin's avatar
Gildas Bazin committed
347
    int i;
348
    char *psz_val;
Gildas Bazin's avatar
 
Gildas Bazin committed
349

350
    /* Get/parse options and open device(s) */
Gildas Bazin's avatar
 
Gildas Bazin committed
351
    string vdevname, adevname;
352 353
    int i_width = 0, i_height = 0;
    vlc_fourcc_t i_chroma = 0;
354 355 356 357 358
    bool b_use_audio = true;
    bool b_use_video = true;

    /* Initialize OLE/COM */
    CoInitialize( 0 );
Gildas Bazin's avatar
 
Gildas Bazin committed
359

360 361
    var_Create( p_this, "dshow-config", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_this, "dshow-tuner", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
362 363
    psz_val = var_CreateGetString( p_this, "dshow-vdev" );
    if( psz_val )
364
    {
365
        msg_Dbg( p_this, "dshow-vdev: %s", psz_val ) ;
366
        /* skip none device */
367 368
        if ( strncasecmp( psz_val, "none", 4 ) != 0 )
            vdevname = string( psz_val );
369 370 371
        else
            b_use_video = false ;
    }
372
    free( psz_val );
Gildas Bazin's avatar
 
Gildas Bazin committed
373

374 375
    psz_val = var_CreateGetString( p_this, "dshow-adev" );
    if( psz_val )
376
    {
377
        msg_Dbg( p_this, "dshow-adev: %s", psz_val ) ;
378
        /* skip none device */
379 380
        if ( strncasecmp( psz_val, "none", 4 ) != 0 )
            adevname = string( psz_val );
381 382 383
        else
            b_use_audio = false ;
    }
384
    free( psz_val );
385

386
    static struct {const char *psz_size; int  i_width; int  i_height;} size_table[] =
Gildas Bazin's avatar
Gildas Bazin committed
387
    { { "subqcif", 128, 96 }, { "qsif", 160, 120 }, { "qcif", 176, 144 },
388
      { "sif", 320, 240 }, { "cif", 352, 288 }, { "d1", 640, 480 },
Gildas Bazin's avatar
Gildas Bazin committed
389 390 391
      { 0, 0, 0 },
    };

392 393
    psz_val = var_CreateGetString( p_this, "dshow-size" );
    if( !EMPTY_STR(psz_val) )
Gildas Bazin's avatar
 
Gildas Bazin committed
394
    {
Gildas Bazin's avatar
Gildas Bazin committed
395
        for( i = 0; size_table[i].psz_size; i++ )
396
        {
397
            if( !strcmp( psz_val, size_table[i].psz_size ) )
Gildas Bazin's avatar
Gildas Bazin committed
398 399 400 401 402
            {
                i_width = size_table[i].i_width;
                i_height = size_table[i].i_height;
                break;
            }
403
        }
Gildas Bazin's avatar
Gildas Bazin committed
404
        if( !size_table[i].psz_size ) /* Try to parse "WidthxHeight" */
405 406
        {
            char *psz_parser;
407
            i_width = strtol( psz_val, &psz_parser, 0 );
408
            if( *psz_parser == 'x' || *psz_parser == 'X')
Gildas Bazin's avatar
 
Gildas Bazin committed
409
            {
410
                i_height = strtol( psz_parser + 1, &psz_parser, 0 );
Gildas Bazin's avatar
 
Gildas Bazin committed
411
            }
Clément Stenac's avatar
Clément Stenac committed
412
            msg_Dbg( p_this, "width x height %dx%d", i_width, i_height );
Gildas Bazin's avatar
 
Gildas Bazin committed
413 414
        }
    }
415
    free( psz_val );
Gildas Bazin's avatar
 
Gildas Bazin committed
416

417 418
    psz_val = var_CreateGetString( p_this, "dshow-chroma" );
    i_chroma = vlc_fourcc_GetCodecFromString( UNKNOWN_ES, psz_val );
419
    p_sys->b_chroma = i_chroma != 0;
420
    free( psz_val );
Gildas Bazin's avatar
 
Gildas Bazin committed
421

422
    var_Create( p_this, "dshow-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
423 424 425 426 427 428 429
    var_Create( p_this, "dshow-tuner-channel",
                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Create( p_this, "dshow-tuner-country",
                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Create( p_this, "dshow-tuner-input",
                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );

430 431 432
    var_Create( p_this, "dshow-amtuner-mode",
                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );

Gildas Bazin's avatar
Gildas Bazin committed
433
    var_Create( p_this, "dshow-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
Gildas Bazin's avatar
 
Gildas Bazin committed
434

435 436 437 438 439
    var_Create( p_this, "dshow-video-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Create( p_this, "dshow-audio-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Create( p_this, "dshow-video-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Create( p_this, "dshow-audio-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );

Gildas Bazin's avatar
 
Gildas Bazin committed
440 441
    /* Initialize some data */
    p_sys->i_streams = 0;
Rémi Duraffort's avatar
Rémi Duraffort committed
442
    p_sys->pp_streams = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
443 444 445
    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
446

447 448 449
    p_sys->p_graph = NULL;
    p_sys->p_capture_graph_builder2 = NULL;
    p_sys->p_control = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
450

451 452 453
    /* Build directshow graph */
    CreateDirectShowGraph( p_sys );

454
    vlc_mutex_init( &p_sys->lock );
455
    vlc_cond_init( &p_sys->wait );
456

457 458
    if( !b_use_video && !b_use_audio )
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
459
        dialog_Fatal( p_this, _("Capture failed"),
basos g's avatar
basos g committed
460
                        _("No video or audio device selected.") );
461 462
        return VLC_EGENERIC ;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
463

464 465 466 467 468
    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
469
    {
470 471
        msg_Err( p_this, "can't open video device");
        b_err_video = true ;
Gildas Bazin's avatar
 
Gildas Bazin committed
472
    }
473 474

    if ( b_use_video && !b_err_video )
Gildas Bazin's avatar
Gildas Bazin committed
475 476 477 478 479 480 481 482
    {
        /* Check if we can handle the demuxing ourselves or need to spawn
         * a demuxer module */
        dshow_stream_t *p_stream = p_sys->pp_streams[p_sys->i_streams-1];

        if( p_stream->mt.majortype == MEDIATYPE_Video )
        {
            if( /* Raw DV stream */
483
                p_stream->i_fourcc == VLC_CODEC_DV ||
Gildas Bazin's avatar
Gildas Bazin committed
484
                /* Raw MPEG video stream */
Laurent Aimar's avatar
Laurent Aimar committed
485
                p_stream->i_fourcc == VLC_CODEC_MPGV )
Gildas Bazin's avatar
Gildas Bazin committed
486
            {
487
                b_use_audio = false;
488 489 490 491

                if( b_access_demux )
                {
                    /* Let the access (only) take care of that */
492
                    return VLC_EGENERIC;
493
                }
Gildas Bazin's avatar
Gildas Bazin committed
494 495
            }
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
496

497 498
        if( p_stream->mt.majortype == MEDIATYPE_Stream )
        {
499
            b_use_audio = false;
500 501 502 503 504 505

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

507
            if( var_GetBool( p_this, "dshow-tuner" ) )
508 509 510 511 512 513
            {
                /* FIXME: we do MEDIATYPE_Stream here so we don't do
                 * it twice. */
                ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2,
                                     p_stream->p_device_filter, 0 );
            }
514
        }
Gildas Bazin's avatar
Gildas Bazin committed
515 516
    }

517 518 519 520 521 522
    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
523
    {
524 525 526 527 528 529 530 531 532
        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" ) ;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
533
        dialog_Fatal( p_this,  _("Capture failed"),
534 535 536
                        _("VLC cannot open ANY capture device."
                          "Check the error log for details.") );
        return VLC_EGENERIC ;
Gildas Bazin's avatar
 
Gildas Bazin committed
537
    }
Gildas Bazin's avatar
Gildas Bazin committed
538

539
    for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
Gildas Bazin's avatar
 
Gildas Bazin committed
540
    {
541 542 543 544 545 546 547 548 549 550 551 552
        int i_val = var_GetInteger( p_this, "dshow-video-input" );
        if( i_val >= 0 )
            p_sys->crossbar_routes[i].VideoInputIndex = i_val;
        i_val = var_GetInteger( p_this, "dshow-video-output" );
        if( i_val >= 0 )
            p_sys->crossbar_routes[i].VideoOutputIndex = i_val;
        i_val = var_GetInteger( p_this, "dshow-audio-input" );
        if( i_val >= 0 )
            p_sys->crossbar_routes[i].AudioInputIndex = i_val;
        i_val = var_GetInteger( p_this, "dshow-audio-output" );
        if( i_val >= 0 )
            p_sys->crossbar_routes[i].AudioOutputIndex = i_val;
553

554 555 556 557 558
        IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
        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
559

560 561
        if( SUCCEEDED(pXbar->Route(VideoOutputIndex, VideoInputIndex)) )
        {
Clément Stenac's avatar
Clément Stenac committed
562 563
            msg_Dbg( p_this, "crossbar at depth %d, routed video "
                     "output %ld to video input %ld", i, VideoOutputIndex,
564
                     VideoInputIndex );
Gildas Bazin's avatar
 
Gildas Bazin committed
565

566 567 568 569 570
            if( AudioOutputIndex != -1 && AudioInputIndex != -1 )
            {
                if( SUCCEEDED( pXbar->Route(AudioOutputIndex,
                                            AudioInputIndex)) )
                {
Clément Stenac's avatar
Clément Stenac committed
571 572
                    msg_Dbg(p_this, "crossbar at depth %d, routed audio "
                            "output %ld to audio input %ld", i,
573 574 575 576
                            AudioOutputIndex, AudioInputIndex );
                }
            }
        }
577 578 579
        else
            msg_Err( p_this, "crossbar at depth %d could not route video "
                     "output %ld to input %ld", i, VideoOutputIndex, VideoInputIndex );
580 581
    }

582 583 584
    /*
    ** Show properties pages from other filters in graph
    */
585
    if( var_GetBool( p_this, "dshow-config" ) )
586 587 588 589 590
    {
        for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
        {
            IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
            IBaseFilter *p_XF;
591 592 593

            if( SUCCEEDED( pXbar->QueryInterface( IID_IBaseFilter,
                                                  (void **)&p_XF ) ) )
594 595 596 597
            {
                ShowPropertyPage( p_XF );
                p_XF->Release();
            }
598
        }
599 600
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
601 602
    /* Initialize some data */
    p_sys->i_current_stream = 0;
603

Gildas Bazin's avatar
Gildas Bazin committed
604
    if( !p_sys->i_streams ) return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
605

Gildas Bazin's avatar
 
Gildas Bazin committed
606 607 608 609
    return VLC_SUCCESS;
}

/*****************************************************************************
Gildas Bazin's avatar
Gildas Bazin committed
610
 * DemuxOpen: open direct show device as an access_demux module
Gildas Bazin's avatar
 
Gildas Bazin committed
611
 *****************************************************************************/
Gildas Bazin's avatar
Gildas Bazin committed
612
static int DemuxOpen( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
613
{
Gildas Bazin's avatar
Gildas Bazin committed
614 615 616
    demux_t      *p_demux = (demux_t *)p_this;
    access_sys_t *p_sys;
    int i;
Gildas Bazin's avatar
 
Gildas Bazin committed
617

Rémi Duraffort's avatar
Rémi Duraffort committed
618
    p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
Rémi Duraffort's avatar
Rémi Duraffort committed
619 620
    if( !p_sys )
        return VLC_ENOMEM;
Gildas Bazin's avatar
Gildas Bazin committed
621
    p_demux->p_sys = (demux_sys_t *)p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
622

623
    if( CommonOpen( p_this, p_sys, true ) != VLC_SUCCESS )
Gildas Bazin's avatar
Gildas Bazin committed
624 625 626 627
    {
        CommonClose( p_this, p_sys );
        return VLC_EGENERIC;
    }
628

Gildas Bazin's avatar
Gildas Bazin committed
629 630 631
    /* Everything is ready. Let's rock baby */
    msg_Dbg( p_this, "Playing...");
    p_sys->p_control->Run();
632

Gildas Bazin's avatar
Gildas Bazin committed
633 634 635 636 637 638 639
    p_demux->pf_demux   = Demux;
    p_demux->pf_control = DemuxControl;
    p_demux->info.i_update = 0;
    p_demux->info.i_title = 0;
    p_demux->info.i_seekpoint = 0;

    for( i = 0; i < p_sys->i_streams; i++ )
640
    {
Gildas Bazin's avatar
Gildas Bazin committed
641 642
        dshow_stream_t *p_stream = p_sys->pp_streams[i];
        es_format_t fmt;
643

Gildas Bazin's avatar
Gildas Bazin committed
644 645 646
        if( p_stream->mt.majortype == MEDIATYPE_Video )
        {
            es_format_Init( &fmt, VIDEO_ES, p_stream->i_fourcc );
647

Gildas Bazin's avatar
Gildas Bazin committed
648 649
            fmt.video.i_width  = p_stream->header.video.bmiHeader.biWidth;
            fmt.video.i_height = p_stream->header.video.bmiHeader.biHeight;
Laurent Aimar's avatar
Laurent Aimar committed
650 651
            fmt.video.i_sar_num = 4 * fmt.video.i_height;
            fmt.video.i_sar_den = 4 * fmt.video.i_width;
652

Gildas Bazin's avatar
Gildas Bazin committed
653 654 655 656 657
            if( !p_stream->header.video.bmiHeader.biCompression )
            {
                /* RGB DIB are coded from bottom to top */
                fmt.video.i_height = (unsigned int)(-(int)fmt.video.i_height);
            }
658

Gildas Bazin's avatar
Gildas Bazin committed
659
            /* Setup rgb mask for RGB formats */
660
            if( p_stream->i_fourcc == VLC_CODEC_RGB24 )
Gildas Bazin's avatar
Gildas Bazin committed
661
            {
662 663 664 665
                /* This is in RGB format
            http://msdn.microsoft.com/en-us/library/dd407253%28VS.85%29.aspx?ppud=4
                 */
                fmt.video.i_rmask = 0x00ff0000;
666
                fmt.video.i_gmask = 0x0000ff00;
667
                fmt.video.i_bmask = 0x000000ff;
Gildas Bazin's avatar
Gildas Bazin committed
668
            }
669 670 671 672 673 674 675

            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
676 677 678 679
        }
        else if( p_stream->mt.majortype == MEDIATYPE_Audio )
        {
            es_format_Init( &fmt, AUDIO_ES, p_stream->i_fourcc );
680

Gildas Bazin's avatar
Gildas Bazin committed
681 682 683 684 685 686 687 688 689 690
            fmt.audio.i_channels = p_stream->header.audio.nChannels;
            fmt.audio.i_rate = p_stream->header.audio.nSamplesPerSec;
            fmt.audio.i_bitspersample = p_stream->header.audio.wBitsPerSample;
            fmt.audio.i_blockalign = fmt.audio.i_channels *
                fmt.audio.i_bitspersample / 8;
            fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
                fmt.audio.i_bitspersample;
        }

        p_stream->p_es = es_out_Add( p_demux->out, &fmt );
691
    }
Gildas Bazin's avatar
Gildas Bazin committed
692

693
    return VLC_SUCCESS;
694 695
}

Gildas Bazin's avatar
Gildas Bazin committed
696 697 698 699
/*****************************************************************************
 * AccessOpen: open direct show device as an access module
 *****************************************************************************/
static int AccessOpen( vlc_object_t *p_this )
700
{
Gildas Bazin's avatar
Gildas Bazin committed
701 702
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;
703

Rémi Duraffort's avatar
Rémi Duraffort committed
704
    p_access->p_sys = p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
Rémi Duraffort's avatar
Rémi Duraffort committed
705 706
    if( !p_sys )
        return VLC_ENOMEM;
707

708
    if( CommonOpen( p_this, p_sys, false ) != VLC_SUCCESS )
Gildas Bazin's avatar
Gildas Bazin committed
709 710 711 712
    {
        CommonClose( p_this, p_sys );
        return VLC_EGENERIC;
    }
713

Gildas Bazin's avatar
Gildas Bazin committed
714
    dshow_stream_t *p_stream = p_sys->pp_streams[0];
715

Gildas Bazin's avatar
Gildas Bazin committed
716 717
    /* Check if we need to force demuxers */
    if( !p_access->psz_demux || !*p_access->psz_demux )
Gildas Bazin's avatar
 
Gildas Bazin committed
718
    {
719
        if( p_stream->i_fourcc == VLC_CODEC_DV )
720
        {
Rafaël Carré's avatar
Rafaël Carré committed
721
            free( p_access->psz_demux );
Gildas Bazin's avatar
Gildas Bazin committed
722 723
            p_access->psz_demux = strdup( "rawdv" );
        }
Laurent Aimar's avatar
Laurent Aimar committed
724
        else if( p_stream->i_fourcc == VLC_CODEC_MPGV )
Gildas Bazin's avatar
Gildas Bazin committed
725
        {
Rafaël Carré's avatar
Rafaël Carré committed
726 727
            free( p_access->psz_demux );
            p_access->psz_demux = strdup( "mpgv" );
728
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
729
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
730

Gildas Bazin's avatar
Gildas Bazin committed
731
    /* Setup Access */
732 733
    p_access->pf_read = NULL;
    p_access->pf_block = ReadCompressed;
Gildas Bazin's avatar
Gildas Bazin committed
734 735 736 737 738
    p_access->pf_control = AccessControl;
    p_access->pf_seek = NULL;
    p_access->info.i_update = 0;
    p_access->info.i_size = 0;
    p_access->info.i_pos = 0;
739
    p_access->info.b_eof = false;
Gildas Bazin's avatar
Gildas Bazin committed
740 741 742 743 744 745 746 747 748
    p_access->info.i_title = 0;
    p_access->info.i_seekpoint = 0;
    p_access->p_sys = p_sys;

    /* Everything is ready. Let's rock baby */
    msg_Dbg( p_this, "Playing...");
    p_sys->p_control->Run();

    return VLC_SUCCESS;
749
}
Gildas Bazin's avatar
 
Gildas Bazin committed
750

Gildas Bazin's avatar
Gildas Bazin committed
751 752 753 754
/*****************************************************************************
 * CommonClose: close device
 *****************************************************************************/
static void CommonClose( vlc_object_t *p_this, access_sys_t *p_sys )
755
{
Clément Stenac's avatar
Clément Stenac committed
756
    msg_Dbg( p_this, "releasing DirectShow");
757

Gildas Bazin's avatar
Gildas Bazin committed
758
    DeleteDirectShowGraph( p_sys );
759

Gildas Bazin's avatar
Gildas Bazin committed
760 761
    /* Uninitialize OLE/COM */
    CoUninitialize();
762

763 764
    for( int i = 0; i < p_sys->i_streams; i++ ) delete p_sys->pp_streams[i];
    if( p_sys->i_streams ) free( p_sys->pp_streams );
Gildas Bazin's avatar
Gildas Bazin committed
765 766 767

    vlc_mutex_destroy( &p_sys->lock );
    vlc_cond_destroy( &p_sys->wait );
768

Gildas Bazin's avatar
Gildas Bazin committed
769
    free( p_sys );
770 771
}

Gildas Bazin's avatar
Gildas Bazin committed
772 773 774 775
/*****************************************************************************
 * AccessClose: close device
 *****************************************************************************/
static void AccessClose( vlc_object_t *p_this )
776
{
Gildas Bazin's avatar
Gildas Bazin committed
777 778
    access_t     *p_access = (access_t *)p_this;
    access_sys_t *p_sys    = p_access->p_sys;
779

Gildas Bazin's avatar
Gildas Bazin committed
780 781
    /* Stop capturing stuff */
    p_sys->p_control->Stop();
782

Gildas Bazin's avatar
Gildas Bazin committed
783 784
    CommonClose( p_this, p_sys );
}
785

Gildas Bazin's avatar
Gildas Bazin committed
786 787 788 789 790 791 792
/*****************************************************************************
 * DemuxClose: close device
 *****************************************************************************/
static void DemuxClose( vlc_object_t *p_this )
{
    demux_t      *p_demux = (demux_t *)p_this;
    access_sys_t *p_sys   = (access_sys_t *)p_demux->p_sys;
793

Gildas Bazin's avatar
Gildas Bazin committed
794 795
    /* Stop capturing stuff */
    p_sys->p_control->Stop();
796

Gildas Bazin's avatar
Gildas Bazin committed
797
    CommonClose( p_this, p_sys );
Gildas Bazin's avatar
 
Gildas Bazin committed
798 799 800 801 802
}

/****************************************************************************
 * ConnectFilters
 ****************************************************************************/
Gildas Bazin's avatar
Gildas Bazin committed
803 804
static bool ConnectFilters( vlc_object_t *p_this, access_sys_t *p_sys,
                            IBaseFilter *p_filter,
805
                            CaptureFilter *p_capture_filter )
Gildas Bazin's avatar
 
Gildas Bazin committed
806
{
807
    CapturePin *p_input_pin = p_capture_filter->CustomGetPin();
Gildas Bazin's avatar
 
Gildas Bazin committed
808

809
    AM_MEDIA_TYPE mediaType = p_input_pin->CustomGetMediaType();
Gildas Bazin's avatar
 
Gildas Bazin committed
810

811
    if( p_sys->p_capture_graph_builder2 )
Gildas Bazin's avatar
 
Gildas Bazin committed
812
    {
813
        if( FAILED(p_sys->p_capture_graph_builder2->
Gildas Bazin's avatar
Gildas Bazin committed
814 815
                RenderStream( &PIN_CATEGORY_CAPTURE, &mediaType.majortype,
                              p_filter, 0, (IBaseFilter *)p_capture_filter )) )
816 817 818
        {
            return false;
        }
819

820 821
        // Sort out all the possible video inputs
        // The class needs to be given the capture filters ANALOGVIDEO input pin
Rémi Duraffort's avatar
Rémi Duraffort committed
822
        IEnumPins *pins = NULL;
823 824
        if( ( mediaType.majortype == MEDIATYPE_Video ||
              mediaType.majortype == MEDIATYPE_Stream ) &&
825
            SUCCEEDED(p_filter->EnumPins(&pins)) )
Gildas Bazin's avatar
 
Gildas Bazin committed
826
        {
Rémi Duraffort's avatar
Rémi Duraffort committed
827
            IPin        *pP = NULL;
828 829 830
            ULONG        n;
            PIN_INFO     pinInfo;
            BOOL         Found = FALSE;
Rémi Duraffort's avatar
Rémi Duraffort committed
831
            IKsPropertySet *pKs = NULL;
832 833 834
            GUID guid;
            DWORD dw;

Gildas Bazin's avatar
Gildas Bazin committed
835
            while( !Found && ( S_OK == pins->Next(1, &pP, &n) ) )
836
            {
Gildas Bazin's avatar
Gildas Bazin committed
837
                if( S_OK == pP->QueryPinInfo(&pinInfo) )
838
                {
Gildas Bazin's avatar
Gildas Bazin committed
839 840 841 842
                    // is this pin an ANALOGVIDEOIN input pin?
                    if( pinInfo.dir == PINDIR_INPUT &&
                        pP->QueryInterface( IID_IKsPropertySet,
                                            (void **)&pKs ) == S_OK )
843
                    {
Gildas Bazin's avatar
Gildas Bazin committed
844 845 846
                        if( pKs->Get( AMPROPSETID_Pin,
                                      AMPROPERTY_PIN_CATEGORY, NULL, 0,
                                      &guid, sizeof(GUID), &dw ) == S_OK )
847
                        {
Gildas Bazin's avatar
Gildas Bazin committed
848
                            if( guid == PIN_CATEGORY_ANALOGVIDEOIN )