dshow.cpp 77 KB
Newer Older
gbazin's avatar
 
gbazin committed
1
/*****************************************************************************
2
 * dshow.cpp : DirectShow access module for vlc
gbazin's avatar
 
gbazin committed
3
 *****************************************************************************
4
 * Copyright (C) 2002, 2003 the VideoLAN team
5
 * $Id$
gbazin's avatar
 
gbazin committed
6
 *
7
 * Author: Gildas Bazin <gbazin@videolan.org>
8
 *         Damien Fouilleul <damienf@videolan.org>
gbazin's avatar
 
gbazin 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
dionoea's avatar
dionoea committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
gbazin's avatar
 
gbazin committed
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
damienf's avatar
damienf committed
28

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

gbazin's avatar
 
gbazin committed
33
#include <vlc/vlc.h>
zorglub's avatar
zorglub committed
34 35
#include <vlc_input.h>
#include <vlc_access.h>
36
#include <vlc_demux.h>
zorglub's avatar
zorglub committed
37
#include <vlc_vout.h>
zorglub's avatar
Round 2  
zorglub committed
38
#include <vlc_interface.h>
gbazin's avatar
 
gbazin committed
39

gbazin's avatar
gbazin committed
40
#include "common.h"
gbazin's avatar
 
gbazin committed
41 42
#include "filter.h"

gbazin's avatar
 
gbazin committed
43 44 45
/*****************************************************************************
 * Access: local prototypes
 *****************************************************************************/
46
static block_t *ReadCompressed( access_t * );
47
static int AccessControl ( access_t *, int, va_list );
gbazin's avatar
 
gbazin committed
48

gbazin's avatar
gbazin committed
49 50 51
static int Demux       ( demux_t * );
static int DemuxControl( demux_t *, int, va_list );

52
static int OpenDevice( vlc_object_t *, access_sys_t *, string, bool );
gbazin's avatar
 
gbazin committed
53
static IBaseFilter *FindCaptureDevice( vlc_object_t *, string *,
54
                                       list<string> *, bool );
55
static size_t EnumDeviceCaps( vlc_object_t *, IBaseFilter *,
56 57
                              int, int, int, int, int, int,
                              AM_MEDIA_TYPE *mt, size_t );
gbazin's avatar
gbazin committed
58 59
static bool ConnectFilters( vlc_object_t *, access_sys_t *,
                            IBaseFilter *, CaptureFilter * );
gbazin's avatar
 
gbazin committed
60 61
static int FindDevicesCallback( vlc_object_t *, char const *,
                                vlc_value_t, vlc_value_t, void * );
62 63
static int ConfigDevicesCallback( vlc_object_t *, char const *,
                                  vlc_value_t, vlc_value_t, void * );
gbazin's avatar
 
gbazin committed
64

65
static void ShowPropertyPage( IUnknown * );
66
static void ShowDeviceProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
67
                                  IBaseFilter *, bool );
68
static void ShowTunerProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
69
                                 IBaseFilter *, bool );
70 71
static void ConfigTuner( vlc_object_t *, ICaptureGraphBuilder2 *,
                         IBaseFilter * );
gbazin's avatar
 
gbazin committed
72

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

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

155
#define AMTUNER_MODE_TEXT N_("AM Tuner mode")
dionoea's avatar
dionoea committed
156
#define AMTUNER_MODE_LONGTEXT N_( \
157 158
    "AM Tuner mode. Can be one of DEFAULT, TV, AM_RADIO, FM_RADIO or DSS.")

159
static int  CommonOpen ( vlc_object_t *, access_sys_t *, bool );
gbazin's avatar
gbazin committed
160 161
static void CommonClose( vlc_object_t *, access_sys_t * );

162 163 164 165 166 167
static int  AccessOpen ( vlc_object_t * );
static void AccessClose( vlc_object_t * );

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

gbazin's avatar
 
gbazin committed
168
vlc_module_begin();
169
    set_shortname( _("DirectShow") );
gbazin's avatar
 
gbazin committed
170
    set_description( _("DirectShow input") );
zorglub's avatar
zorglub committed
171 172
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_ACCESS );
173
    add_integer( "dshow-caching", (mtime_t)(0.2*CLOCK_FREQ) / 1000, NULL,
174
                 CACHING_TEXT, CACHING_LONGTEXT, true );
175

176
    add_string( "dshow-vdev", NULL, NULL, VDEV_TEXT, VDEV_LONGTEXT, false);
177
        change_string_list( ppsz_vdev, ppsz_vdev_text, FindDevicesCallback );
178 179
        change_action_add( FindDevicesCallback, N_("Refresh list") );
        change_action_add( ConfigDevicesCallback, N_("Configure") );
180

181
    add_string( "dshow-adev", NULL, NULL, ADEV_TEXT, ADEV_LONGTEXT, false);
182
        change_string_list( ppsz_adev, ppsz_adev_text, FindDevicesCallback );
183 184
        change_action_add( FindDevicesCallback, N_("Refresh list") );
        change_action_add( ConfigDevicesCallback, N_("Configure") );
185

186
    add_string( "dshow-size", NULL, NULL, SIZE_TEXT, SIZE_LONGTEXT, false);
187

188
    add_string( "dshow-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
189
                true );
gbazin's avatar
 
gbazin committed
190

191
    add_float( "dshow-fps", 0.0f, NULL, FPS_TEXT, FPS_LONGTEXT,
192
                true );
193

194 195
    add_bool( "dshow-config", false, NULL, CONFIG_TEXT, CONFIG_LONGTEXT,
              true );
gbazin's avatar
 
gbazin committed
196

197 198
    add_bool( "dshow-tuner", false, NULL, TUNER_TEXT, TUNER_LONGTEXT,
              true );
199

200
    add_integer( "dshow-tuner-channel", 0, NULL, CHANNEL_TEXT,
201
                 CHANNEL_LONGTEXT, true );
202 203

    add_integer( "dshow-tuner-country", 0, NULL, COUNTRY_TEXT,
204
                 COUNTRY_LONGTEXT, true );
205 206

    add_integer( "dshow-tuner-input", 0, NULL, TUNER_INPUT_TEXT,
207
                 TUNER_INPUT_LONGTEXT, true );
208 209
        change_integer_list( pi_tuner_input, ppsz_tuner_input_text, 0 );

210
    add_integer( "dshow-video-input",  -1, NULL, VIDEO_IN_TEXT,
211
                 VIDEO_IN_LONGTEXT, true );
212 213

    add_integer( "dshow-audio-input",  -1, NULL, AUDIO_IN_TEXT,
214
                 AUDIO_IN_LONGTEXT, true );
215 216

    add_integer( "dshow-video-output", -1, NULL, VIDEO_OUT_TEXT,
217
                 VIDEO_OUT_LONGTEXT, true );
218 219

    add_integer( "dshow-audio-output", -1, NULL, AUDIO_OUT_TEXT,
220
                 AUDIO_OUT_LONGTEXT, true );
221

dionoea's avatar
dionoea committed
222
    add_integer( "dshow-amtuner-mode", AMTUNER_MODE_TV, NULL,
223
                AMTUNER_MODE_TEXT, AMTUNER_MODE_LONGTEXT, false);
224 225
        change_integer_list( pi_amtuner_mode, ppsz_amtuner_mode_text, 0 );

gbazin's avatar
 
gbazin committed
226
    add_shortcut( "dshow" );
gbazin's avatar
gbazin committed
227 228
    set_capability( "access_demux", 0 );
    set_callbacks( DemuxOpen, DemuxClose );
gbazin's avatar
 
gbazin committed
229 230

    add_submodule();
gbazin's avatar
gbazin committed
231 232 233
    set_description( _("DirectShow input") );
    set_capability( "access2", 0 );
    set_callbacks( AccessOpen, AccessClose );
gbazin's avatar
 
gbazin committed
234 235 236

vlc_module_end();

gbazin's avatar
gbazin committed
237
/*****************************************************************************
gbazin's avatar
 
gbazin committed
238
 * DirectShow elementary stream descriptor
gbazin's avatar
gbazin committed
239
 *****************************************************************************/
gbazin's avatar
 
gbazin committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253
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;

gbazin's avatar
gbazin committed
254 255 256
    int             i_fourcc;
    es_out_id_t     *p_es;

257
    bool      b_pts;
gbazin's avatar
 
gbazin committed
258 259
} dshow_stream_t;

gbazin's avatar
gbazin committed
260
/*****************************************************************************
261
 * DirectShow utility functions
gbazin's avatar
gbazin committed
262
 *****************************************************************************/
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 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
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;
    }
}

gbazin's avatar
 
gbazin committed
317
/*****************************************************************************
gbazin's avatar
gbazin committed
318
 * CommonOpen: open direct show device
gbazin's avatar
 
gbazin committed
319
 *****************************************************************************/
gbazin's avatar
gbazin committed
320
static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
321
                       bool b_access_demux )
gbazin's avatar
 
gbazin committed
322
{
323
    vlc_value_t  val;
gbazin's avatar
gbazin committed
324
    int i;
gbazin's avatar
 
gbazin committed
325

326
    /* Get/parse options and open device(s) */
gbazin's avatar
 
gbazin committed
327
    string vdevname, adevname;
328
    int i_width = 0, i_height = 0, i_chroma = 0;
329
    bool b_audio = true;
gbazin's avatar
 
gbazin committed
330

331 332
    var_Create( p_this, "dshow-config", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_this, "dshow-tuner", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
gbazin's avatar
gbazin committed
333 334
    var_Create( p_this, "dshow-vdev", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_this, "dshow-vdev", &val );
335
    if( val.psz_string ) vdevname = string( val.psz_string );
336
    free( val.psz_string );
gbazin's avatar
 
gbazin committed
337

gbazin's avatar
gbazin committed
338 339
    var_Create( p_this, "dshow-adev", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_this, "dshow-adev", &val );
340
    if( val.psz_string ) adevname = string( val.psz_string );
341
    free( val.psz_string );
342

gbazin's avatar
gbazin committed
343 344
    static struct {char *psz_size; int  i_width; int  i_height;} size_table[] =
    { { "subqcif", 128, 96 }, { "qsif", 160, 120 }, { "qcif", 176, 144 },
345
      { "sif", 320, 240 }, { "cif", 352, 288 }, { "d1", 640, 480 },
gbazin's avatar
gbazin committed
346 347 348 349 350
      { 0, 0, 0 },
    };

    var_Create( p_this, "dshow-size", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_this, "dshow-size", &val );
351
    if( val.psz_string && *val.psz_string )
gbazin's avatar
 
gbazin committed
352
    {
gbazin's avatar
gbazin committed
353
        for( i = 0; size_table[i].psz_size; i++ )
354
        {
gbazin's avatar
gbazin committed
355 356 357 358 359 360
            if( !strcmp( val.psz_string, size_table[i].psz_size ) )
            {
                i_width = size_table[i].i_width;
                i_height = size_table[i].i_height;
                break;
            }
361
        }
gbazin's avatar
gbazin committed
362
        if( !size_table[i].psz_size ) /* Try to parse "WidthxHeight" */
363 364 365 366
        {
            char *psz_parser;
            i_width = strtol( val.psz_string, &psz_parser, 0 );
            if( *psz_parser == 'x' || *psz_parser == 'X')
gbazin's avatar
 
gbazin committed
367
            {
368
                i_height = strtol( psz_parser + 1, &psz_parser, 0 );
gbazin's avatar
 
gbazin committed
369
            }
zorglub's avatar
zorglub committed
370
            msg_Dbg( p_this, "width x height %dx%d", i_width, i_height );
gbazin's avatar
 
gbazin committed
371 372
        }
    }
373
    free( val.psz_string );
gbazin's avatar
 
gbazin committed
374

375
    p_sys->b_chroma = false;
gbazin's avatar
gbazin committed
376 377
    var_Create( p_this, "dshow-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_this, "dshow-chroma", &val );
378 379 380 381
    if( val.psz_string && strlen( val.psz_string ) >= 4 )
    {
        i_chroma = VLC_FOURCC( val.psz_string[0], val.psz_string[1],
                               val.psz_string[2], val.psz_string[3] );
382
        p_sys->b_chroma = true;
383
    }
384
    free( val.psz_string );
gbazin's avatar
 
gbazin committed
385

386
    var_Create( p_this, "dshow-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
387 388 389 390 391 392 393
    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 );

394 395 396
    var_Create( p_this, "dshow-amtuner-mode",
                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );

gbazin's avatar
gbazin committed
397
    var_Create( p_this, "dshow-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
gbazin's avatar
 
gbazin committed
398

399 400 401 402 403
    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 );

gbazin's avatar
 
gbazin committed
404
    /* Initialize OLE/COM */
405
    CoInitialize( 0 );
gbazin's avatar
 
gbazin committed
406

gbazin's avatar
 
gbazin committed
407 408
    /* Initialize some data */
    p_sys->i_streams = 0;
409
    p_sys->pp_streams = 0;
gbazin's avatar
 
gbazin committed
410 411 412
    p_sys->i_width = i_width;
    p_sys->i_height = i_height;
    p_sys->i_chroma = i_chroma;
gbazin's avatar
 
gbazin committed
413

414 415 416
    p_sys->p_graph = NULL;
    p_sys->p_capture_graph_builder2 = NULL;
    p_sys->p_control = NULL;
gbazin's avatar
 
gbazin committed
417

418 419 420
    vlc_mutex_init( p_this, &p_sys->lock );
    vlc_cond_init( p_this, &p_sys->wait );

421 422
    /* Build directshow graph */
    CreateDirectShowGraph( p_sys );
gbazin's avatar
 
gbazin committed
423

gbazin's avatar
gbazin committed
424
    if( OpenDevice( p_this, p_sys, vdevname, 0 ) != VLC_SUCCESS )
gbazin's avatar
 
gbazin committed
425
    {
gbazin's avatar
gbazin committed
426
        msg_Err( p_this, "can't open video");
gbazin's avatar
 
gbazin committed
427
    }
gbazin's avatar
gbazin committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
    else
    {
        /* 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 */
                p_stream->i_fourcc == VLC_FOURCC('d','v','s','l') ||
                p_stream->i_fourcc == VLC_FOURCC('d','v','s','d') ||
                p_stream->i_fourcc == VLC_FOURCC('d','v','h','d') ||
                /* Raw MPEG video stream */
                p_stream->i_fourcc == VLC_FOURCC('m','p','2','v') )
            {
443
                b_audio = false;
444 445 446 447

                if( b_access_demux )
                {
                    /* Let the access (only) take care of that */
448
                    return VLC_EGENERIC;
449
                }
gbazin's avatar
gbazin committed
450 451
            }
        }
gbazin's avatar
 
gbazin committed
452

453 454
        if( p_stream->mt.majortype == MEDIATYPE_Stream )
        {
455
            b_audio = false;
456 457 458 459 460 461

            if( b_access_demux )
            {
                /* Let the access (only) take care of that */
                return VLC_EGENERIC;
            }
462 463 464 465 466 467 468 469 470

            var_Get( p_this, "dshow-tuner", &val );
            if( val.b_bool )
            {
                /* 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 );
            }
471
        }
gbazin's avatar
gbazin committed
472 473 474
    }

    if( b_audio && OpenDevice( p_this, p_sys, adevname, 1 ) != VLC_SUCCESS )
gbazin's avatar
 
gbazin committed
475
    {
gbazin's avatar
gbazin committed
476
        msg_Err( p_this, "can't open audio");
gbazin's avatar
 
gbazin committed
477
    }
gbazin's avatar
gbazin committed
478

479
    for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
gbazin's avatar
 
gbazin committed
480
    {
481
            var_Get( p_this, "dshow-video-input", &val );
482
            if( val.i_int >= 0 )
483 484
                    p_sys->crossbar_routes[i].VideoInputIndex=val.i_int;
            var_Get( p_this, "dshow-video-output", &val );
485
            if( val.i_int >= 0 )
486 487
                    p_sys->crossbar_routes[i].VideoOutputIndex=val.i_int;
            var_Get( p_this, "dshow-audio-input", &val );
488
            if( val.i_int >= 0 )
489 490
                    p_sys->crossbar_routes[i].AudioInputIndex=val.i_int;
            var_Get( p_this, "dshow-audio-output", &val );
491
            if( val.i_int >= 0 )
492 493
                    p_sys->crossbar_routes[i].AudioOutputIndex=val.i_int;

494 495 496 497 498
        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;
gbazin's avatar
 
gbazin committed
499

500 501
        if( SUCCEEDED(pXbar->Route(VideoOutputIndex, VideoInputIndex)) )
        {
zorglub's avatar
zorglub committed
502 503
            msg_Dbg( p_this, "crossbar at depth %d, routed video "
                     "output %ld to video input %ld", i, VideoOutputIndex,
504
                     VideoInputIndex );
gbazin's avatar
 
gbazin committed
505

506 507 508 509 510
            if( AudioOutputIndex != -1 && AudioInputIndex != -1 )
            {
                if( SUCCEEDED( pXbar->Route(AudioOutputIndex,
                                            AudioInputIndex)) )
                {
zorglub's avatar
zorglub committed
511 512
                    msg_Dbg(p_this, "crossbar at depth %d, routed audio "
                            "output %ld to audio input %ld", i,
513 514 515 516 517 518
                            AudioOutputIndex, AudioInputIndex );
                }
            }
        }
    }

519 520 521 522
    /*
    ** Show properties pages from other filters in graph
    */
    var_Get( p_this, "dshow-config", &val );
523
    if( val.b_bool )
524 525 526 527 528
    {
        for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
        {
            IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
            IBaseFilter *p_XF;
529 530 531

            if( SUCCEEDED( pXbar->QueryInterface( IID_IBaseFilter,
                                                  (void **)&p_XF ) ) )
532 533 534 535
            {
                ShowPropertyPage( p_XF );
                p_XF->Release();
            }
536
        }
537 538
    }

gbazin's avatar
 
gbazin committed
539 540
    /* Initialize some data */
    p_sys->i_current_stream = 0;
541

gbazin's avatar
gbazin committed
542
    if( !p_sys->i_streams ) return VLC_EGENERIC;
gbazin's avatar
 
gbazin committed
543

gbazin's avatar
 
gbazin committed
544 545 546 547
    return VLC_SUCCESS;
}

/*****************************************************************************
gbazin's avatar
gbazin committed
548
 * DemuxOpen: open direct show device as an access_demux module
gbazin's avatar
 
gbazin committed
549
 *****************************************************************************/
gbazin's avatar
gbazin committed
550
static int DemuxOpen( vlc_object_t *p_this )
gbazin's avatar
 
gbazin committed
551
{
gbazin's avatar
gbazin committed
552 553 554
    demux_t      *p_demux = (demux_t *)p_this;
    access_sys_t *p_sys;
    int i;
gbazin's avatar
 
gbazin committed
555

gbazin's avatar
gbazin committed
556 557 558
    p_sys = (access_sys_t *)malloc( sizeof( access_sys_t ) );
    memset( p_sys, 0, sizeof( access_sys_t ) );
    p_demux->p_sys = (demux_sys_t *)p_sys;
gbazin's avatar
 
gbazin committed
559

560
    if( CommonOpen( p_this, p_sys, true ) != VLC_SUCCESS )
gbazin's avatar
gbazin committed
561 562 563 564
    {
        CommonClose( p_this, p_sys );
        return VLC_EGENERIC;
    }
565

gbazin's avatar
gbazin committed
566 567 568
    /* Everything is ready. Let's rock baby */
    msg_Dbg( p_this, "Playing...");
    p_sys->p_control->Run();
569

gbazin's avatar
gbazin committed
570 571 572 573 574 575 576
    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++ )
577
    {
gbazin's avatar
gbazin committed
578 579
        dshow_stream_t *p_stream = p_sys->pp_streams[i];
        es_format_t fmt;
580

gbazin's avatar
gbazin committed
581 582 583
        if( p_stream->mt.majortype == MEDIATYPE_Video )
        {
            es_format_Init( &fmt, VIDEO_ES, p_stream->i_fourcc );
584

gbazin's avatar
gbazin committed
585 586 587
            fmt.video.i_width  = p_stream->header.video.bmiHeader.biWidth;
            fmt.video.i_height = p_stream->header.video.bmiHeader.biHeight;
            fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
588

gbazin's avatar
gbazin committed
589 590 591 592 593
            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);
            }
594

gbazin's avatar
gbazin committed
595 596 597 598
            /* Setup rgb mask for RGB formats */
            if( p_stream->i_fourcc == VLC_FOURCC('R','V','2','4') )
            {
                /* This is in BGR format */
599 600 601
                fmt.video.i_bmask = 0x00ff0000;
                fmt.video.i_gmask = 0x0000ff00;
                fmt.video.i_rmask = 0x000000ff;
gbazin's avatar
gbazin committed
602
            }
603 604 605 606 607 608 609

            if( p_stream->header.video.AvgTimePerFrame )
            {
                fmt.video.i_frame_rate = 10000000;
                fmt.video.i_frame_rate_base =
                    p_stream->header.video.AvgTimePerFrame;
            }
gbazin's avatar
gbazin committed
610 611 612 613
        }
        else if( p_stream->mt.majortype == MEDIATYPE_Audio )
        {
            es_format_Init( &fmt, AUDIO_ES, p_stream->i_fourcc );
614

gbazin's avatar
gbazin committed
615 616 617 618 619 620 621 622 623 624
            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 );
625
    }
gbazin's avatar
gbazin committed
626

627
    return VLC_SUCCESS;
628 629
}

gbazin's avatar
gbazin committed
630 631 632 633
/*****************************************************************************
 * AccessOpen: open direct show device as an access module
 *****************************************************************************/
static int AccessOpen( vlc_object_t *p_this )
634
{
gbazin's avatar
gbazin committed
635 636
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;
637

gbazin's avatar
gbazin committed
638 639
    p_access->p_sys = p_sys = (access_sys_t *)malloc( sizeof( access_sys_t ) );
    memset( p_sys, 0, sizeof( access_sys_t ) );
640

641
    if( CommonOpen( p_this, p_sys, false ) != VLC_SUCCESS )
gbazin's avatar
gbazin committed
642 643 644 645
    {
        CommonClose( p_this, p_sys );
        return VLC_EGENERIC;
    }
646

gbazin's avatar
gbazin committed
647
    dshow_stream_t *p_stream = p_sys->pp_streams[0];
648

gbazin's avatar
gbazin committed
649 650
    /* Check if we need to force demuxers */
    if( !p_access->psz_demux || !*p_access->psz_demux )
gbazin's avatar
 
gbazin committed
651
    {
gbazin's avatar
gbazin committed
652 653 654
        if( p_stream->i_fourcc == VLC_FOURCC('d','v','s','l') ||
            p_stream->i_fourcc == VLC_FOURCC('d','v','s','d') ||
            p_stream->i_fourcc == VLC_FOURCC('d','v','h','d') )
655
        {
gbazin's avatar
gbazin committed
656 657 658 659 660
            p_access->psz_demux = strdup( "rawdv" );
        }
        else if( p_stream->i_fourcc == VLC_FOURCC('m','p','2','v') )
        {
            p_access->psz_demux = "mpgv";
661
        }
gbazin's avatar
 
gbazin committed
662
    }
gbazin's avatar
 
gbazin committed
663

gbazin's avatar
gbazin committed
664
    /* Setup Access */
665 666
    p_access->pf_read = NULL;
    p_access->pf_block = ReadCompressed;
gbazin's avatar
gbazin committed
667 668 669 670 671
    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;
672
    p_access->info.b_eof = false;
gbazin's avatar
gbazin committed
673 674 675 676 677 678 679 680 681
    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;
682
}
gbazin's avatar
 
gbazin committed
683

gbazin's avatar
gbazin committed
684 685 686 687
/*****************************************************************************
 * CommonClose: close device
 *****************************************************************************/
static void CommonClose( vlc_object_t *p_this, access_sys_t *p_sys )
688
{
zorglub's avatar
zorglub committed
689
    msg_Dbg( p_this, "releasing DirectShow");
690

gbazin's avatar
gbazin committed
691
    DeleteDirectShowGraph( p_sys );
692

gbazin's avatar
gbazin committed
693 694
    /* Uninitialize OLE/COM */
    CoUninitialize();
695

696 697
    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 );
gbazin's avatar
gbazin committed
698 699 700

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

gbazin's avatar
gbazin committed
702
    free( p_sys );
703 704
}

gbazin's avatar
gbazin committed
705 706 707 708
/*****************************************************************************
 * AccessClose: close device
 *****************************************************************************/
static void AccessClose( vlc_object_t *p_this )
709
{
gbazin's avatar
gbazin committed
710 711
    access_t     *p_access = (access_t *)p_this;
    access_sys_t *p_sys    = p_access->p_sys;
712

gbazin's avatar
gbazin committed
713 714
    /* Stop capturing stuff */
    p_sys->p_control->Stop();
715

gbazin's avatar
gbazin committed
716 717
    CommonClose( p_this, p_sys );
}
718

gbazin's avatar
gbazin committed
719 720 721 722 723 724 725
/*****************************************************************************
 * 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;
726

gbazin's avatar
gbazin committed
727 728
    /* Stop capturing stuff */
    p_sys->p_control->Stop();
729

gbazin's avatar
gbazin committed
730
    CommonClose( p_this, p_sys );
gbazin's avatar
 
gbazin committed
731 732 733 734 735
}

/****************************************************************************
 * ConnectFilters
 ****************************************************************************/
gbazin's avatar
gbazin committed
736 737
static bool ConnectFilters( vlc_object_t *p_this, access_sys_t *p_sys,
                            IBaseFilter *p_filter,
738
                            CaptureFilter *p_capture_filter )
gbazin's avatar
 
gbazin committed
739
{
740
    CapturePin *p_input_pin = p_capture_filter->CustomGetPin();
gbazin's avatar
 
gbazin committed
741

742
    AM_MEDIA_TYPE mediaType = p_input_pin->CustomGetMediaType();
gbazin's avatar
 
gbazin committed
743

744
    if( p_sys->p_capture_graph_builder2 )
gbazin's avatar
 
gbazin committed
745
    {
746
        if( FAILED(p_sys->p_capture_graph_builder2->
gbazin's avatar
gbazin committed
747 748
                RenderStream( &PIN_CATEGORY_CAPTURE, &mediaType.majortype,
                              p_filter, 0, (IBaseFilter *)p_capture_filter )) )
749 750 751
        {
            return false;
        }
752

753 754 755
        // Sort out all the possible video inputs
        // The class needs to be given the capture filters ANALOGVIDEO input pin
        IEnumPins *pins = 0;
756 757
        if( ( mediaType.majortype == MEDIATYPE_Video ||
              mediaType.majortype == MEDIATYPE_Stream ) &&
758
            SUCCEEDED(p_filter->EnumPins(&pins)) )
gbazin's avatar
 
gbazin committed
759
        {
760 761 762 763 764 765 766 767
            IPin        *pP = 0;
            ULONG        n;
            PIN_INFO     pinInfo;
            BOOL         Found = FALSE;
            IKsPropertySet *pKs=0;
            GUID guid;
            DWORD dw;

gbazin's avatar
gbazin committed
768
            while( !Found && ( S_OK == pins->Next(1, &pP, &n) ) )
769
            {
gbazin's avatar
gbazin committed
770
                if( S_OK == pP->QueryPinInfo(&pinInfo) )
771
                {
gbazin's avatar
gbazin committed
772 773 774 775
                    // is this pin an ANALOGVIDEOIN input pin?
                    if( pinInfo.dir == PINDIR_INPUT &&
                        pP->QueryInterface( IID_IKsPropertySet,
                                            (void **)&pKs ) == S_OK )
776
                    {
gbazin's avatar
gbazin committed
777 778 779
                        if( pKs->Get( AMPROPSETID_Pin,
                                      AMPROPERTY_PIN_CATEGORY, NULL, 0,
                                      &guid, sizeof(GUID), &dw ) == S_OK )
780
                        {
gbazin's avatar
gbazin committed
781
                            if( guid == PIN_CATEGORY_ANALOGVIDEOIN )
782
                            {
gbazin's avatar
gbazin committed
783
                                // recursively search crossbar routes
784
                                FindCrossbarRoutes( p_this, p_sys, pP, 0 );
gbazin's avatar
gbazin committed
785 786
                                // found it
                                Found = TRUE;
787 788
                            }
                        }
gbazin's avatar
gbazin committed
789
                        pKs->Release();