dshow.cpp 82.6 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 34 35
#define __STDC_FORMAT_MACROS 1
#include <inttypes.h>

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

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

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

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

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

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

Gildas Bazin's avatar
 
Gildas Bazin committed
77
/*****************************************************************************
78
 * Module descriptor
Gildas Bazin's avatar
 
Gildas Bazin committed
79
 *****************************************************************************/
80 81 82 83 84 85
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[] =
86
    {N_("Default"), N_("Cable"), N_("Antenna")};
87
static const int pi_amtuner_mode[]  = { AMTUNER_MODE_DEFAULT,
88 89 90 91
                                 AMTUNER_MODE_TV,
                                 AMTUNER_MODE_FM_RADIO,
                                 AMTUNER_MODE_AM_RADIO,
                                 AMTUNER_MODE_DSS };
92
static const char *const ppsz_amtuner_mode_text[] = { N_("Default"),
93 94 95 96
                                          N_("TV"),
                                          N_("FM radio"),
                                          N_("AM radio"),
                                          N_("DSS") };
97

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Gildas Bazin's avatar
Gildas Bazin committed
258
/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
259
 * DirectShow elementary stream descriptor
Gildas Bazin's avatar
Gildas Bazin committed
260
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274
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
275 276 277
    int             i_fourcc;
    es_out_id_t     *p_es;

278
    bool      b_pts;
279 280

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

Gildas Bazin's avatar
Gildas Bazin committed
283
/*****************************************************************************
284
 * DirectShow utility functions
Gildas Bazin's avatar
Gildas Bazin committed
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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
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
340
/*****************************************************************************
Gildas Bazin's avatar
Gildas Bazin committed
341
 * CommonOpen: open direct show device
Gildas Bazin's avatar
 
Gildas Bazin committed
342
 *****************************************************************************/
Gildas Bazin's avatar
Gildas Bazin committed
343
static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
344
                       bool b_access_demux )
Gildas Bazin's avatar
 
Gildas Bazin committed
345
{
346
    vlc_value_t  val;
Gildas Bazin's avatar
Gildas Bazin committed
347
    int i;
Gildas Bazin's avatar
 
Gildas Bazin committed
348

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

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

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

Gildas Bazin's avatar
Gildas Bazin committed
374 375
    var_Create( p_this, "dshow-adev", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_this, "dshow-adev", &val );
376 377 378 379 380 381 382 383 384
    if( val.psz_string )
    {
        msg_Dbg( p_this, "dshow-adev: %s", val.psz_string ) ;
        /* skip none device */
        if ( strncasecmp( val.psz_string, "none", 4 ) != 0 )
            adevname = string( val.psz_string );
        else
            b_use_audio = false ;
    }
385
    free( val.psz_string );
386

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

    var_Create( p_this, "dshow-size", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_this, "dshow-size", &val );
395
    if( val.psz_string && *val.psz_string )
Gildas Bazin's avatar
 
Gildas Bazin committed
396
    {
Gildas Bazin's avatar
Gildas Bazin committed
397
        for( i = 0; size_table[i].psz_size; i++ )
398
        {
Gildas Bazin's avatar
Gildas Bazin committed
399 400 401 402 403 404
            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;
            }
405
        }
Gildas Bazin's avatar
Gildas Bazin committed
406
        if( !size_table[i].psz_size ) /* Try to parse "WidthxHeight" */
407 408 409 410
        {
            char *psz_parser;
            i_width = strtol( val.psz_string, &psz_parser, 0 );
            if( *psz_parser == 'x' || *psz_parser == 'X')
Gildas Bazin's avatar
 
Gildas Bazin committed
411
            {
412
                i_height = strtol( psz_parser + 1, &psz_parser, 0 );
Gildas Bazin's avatar
 
Gildas Bazin committed
413
            }
Clément Stenac's avatar
Clément Stenac committed
414
            msg_Dbg( p_this, "width x height %dx%d", i_width, i_height );
Gildas Bazin's avatar
 
Gildas Bazin committed
415 416
        }
    }
417
    free( val.psz_string );
Gildas Bazin's avatar
 
Gildas Bazin committed
418

419
    p_sys->b_chroma = false;
Gildas Bazin's avatar
Gildas Bazin committed
420 421
    var_Create( p_this, "dshow-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_this, "dshow-chroma", &val );
422 423 424

    i_chroma = vlc_fourcc_GetCodecFromString( UNKNOWN_ES, val.psz_string );
    p_sys->b_chroma = i_chroma != 0;
425
    free( val.psz_string );
Gildas Bazin's avatar
 
Gildas Bazin committed
426

427
    var_Create( p_this, "dshow-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
428 429 430 431 432 433 434
    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 );

435 436 437
    var_Create( p_this, "dshow-amtuner-mode",
                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );

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

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

452 453 454
    p_sys->p_graph = NULL;
    p_sys->p_capture_graph_builder2 = NULL;
    p_sys->p_control = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
455

456 457 458
    /* Build directshow graph */
    CreateDirectShowGraph( p_sys );

459
    vlc_mutex_init( &p_sys->lock );
460
    vlc_cond_init( &p_sys->wait );
461

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

469 470 471 472 473
    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
474
    {
475 476
        msg_Err( p_this, "can't open video device");
        b_err_video = true ;
Gildas Bazin's avatar
 
Gildas Bazin committed
477
    }
478 479

    if ( b_use_video && !b_err_video )
Gildas Bazin's avatar
Gildas Bazin committed
480 481 482 483 484 485 486 487
    {
        /* 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 */
488
                p_stream->i_fourcc == VLC_CODEC_DV ||
Gildas Bazin's avatar
Gildas Bazin committed
489
                /* Raw MPEG video stream */
Laurent Aimar's avatar
Laurent Aimar committed
490
                p_stream->i_fourcc == VLC_CODEC_MPGV )
Gildas Bazin's avatar
Gildas Bazin committed
491
            {
492
                b_use_audio = false;
493 494 495 496

                if( b_access_demux )
                {
                    /* Let the access (only) take care of that */
497
                    return VLC_EGENERIC;
498
                }
Gildas Bazin's avatar
Gildas Bazin committed
499 500
            }
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
501

502 503
        if( p_stream->mt.majortype == MEDIATYPE_Stream )
        {
504
            b_use_audio = false;
505 506 507 508 509 510

            if( b_access_demux )
            {
                /* Let the access (only) take care of that */
                return VLC_EGENERIC;
            }
511 512 513 514 515 516 517 518 519

            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 );
            }
520
        }
Gildas Bazin's avatar
Gildas Bazin committed
521 522
    }

523 524 525 526 527 528
    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
529
    {
530 531 532 533 534 535 536 537 538
        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
539
        dialog_Fatal( p_this,  _("Capture failed"),
540 541 542
                        _("VLC cannot open ANY capture device."
                          "Check the error log for details.") );
        return VLC_EGENERIC ;
Gildas Bazin's avatar
 
Gildas Bazin committed
543
    }
Gildas Bazin's avatar
Gildas Bazin committed
544

545
    for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
Gildas Bazin's avatar
 
Gildas Bazin committed
546
    {
547
            var_Get( p_this, "dshow-video-input", &val );
548
            if( val.i_int >= 0 )
549 550
                    p_sys->crossbar_routes[i].VideoInputIndex=val.i_int;
            var_Get( p_this, "dshow-video-output", &val );
551
            if( val.i_int >= 0 )
552 553
                    p_sys->crossbar_routes[i].VideoOutputIndex=val.i_int;
            var_Get( p_this, "dshow-audio-input", &val );
554
            if( val.i_int >= 0 )
555 556
                    p_sys->crossbar_routes[i].AudioInputIndex=val.i_int;
            var_Get( p_this, "dshow-audio-output", &val );
557
            if( val.i_int >= 0 )
558 559
                    p_sys->crossbar_routes[i].AudioOutputIndex=val.i_int;

560 561 562 563 564
        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
565

566 567
        if( SUCCEEDED(pXbar->Route(VideoOutputIndex, VideoInputIndex)) )
        {
Clément Stenac's avatar
Clément Stenac committed
568 569
            msg_Dbg( p_this, "crossbar at depth %d, routed video "
                     "output %ld to video input %ld", i, VideoOutputIndex,
570
                     VideoInputIndex );
Gildas Bazin's avatar
 
Gildas Bazin committed
571

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

588 589 590 591
    /*
    ** Show properties pages from other filters in graph
    */
    var_Get( p_this, "dshow-config", &val );
592
    if( val.b_bool )
593 594 595 596 597
    {
        for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
        {
            IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
            IBaseFilter *p_XF;
598 599 600

            if( SUCCEEDED( pXbar->QueryInterface( IID_IBaseFilter,
                                                  (void **)&p_XF ) ) )
601 602 603 604
            {
                ShowPropertyPage( p_XF );
                p_XF->Release();
            }
605
        }
606 607
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
608 609
    /* Initialize some data */
    p_sys->i_current_stream = 0;
610

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

Gildas Bazin's avatar
 
Gildas Bazin committed
613 614 615 616
    return VLC_SUCCESS;
}

/*****************************************************************************
Gildas Bazin's avatar
Gildas Bazin committed
617
 * DemuxOpen: open direct show device as an access_demux module
Gildas Bazin's avatar
 
Gildas Bazin committed
618
 *****************************************************************************/
Gildas Bazin's avatar
Gildas Bazin committed
619
static int DemuxOpen( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
620
{
Gildas Bazin's avatar
Gildas Bazin committed
621 622 623
    demux_t      *p_demux = (demux_t *)p_this;
    access_sys_t *p_sys;
    int i;
Gildas Bazin's avatar
 
Gildas Bazin committed
624

Rémi Duraffort's avatar
Rémi Duraffort committed
625
    p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
Rémi Duraffort's avatar
Rémi Duraffort committed
626 627
    if( !p_sys )
        return VLC_ENOMEM;
Gildas Bazin's avatar
Gildas Bazin committed
628
    p_demux->p_sys = (demux_sys_t *)p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
629

630
    if( CommonOpen( p_this, p_sys, true ) != VLC_SUCCESS )
Gildas Bazin's avatar
Gildas Bazin committed
631 632 633 634
    {
        CommonClose( p_this, p_sys );
        return VLC_EGENERIC;
    }
635

Gildas Bazin's avatar
Gildas Bazin committed
636 637 638
    /* Everything is ready. Let's rock baby */
    msg_Dbg( p_this, "Playing...");
    p_sys->p_control->Run();
639

Gildas Bazin's avatar
Gildas Bazin committed
640 641 642 643 644 645 646
    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++ )
647
    {
Gildas Bazin's avatar
Gildas Bazin committed
648 649
        dshow_stream_t *p_stream = p_sys->pp_streams[i];
        es_format_t fmt;
650

Gildas Bazin's avatar
Gildas Bazin committed
651 652 653
        if( p_stream->mt.majortype == MEDIATYPE_Video )
        {
            es_format_Init( &fmt, VIDEO_ES, p_stream->i_fourcc );
654

Gildas Bazin's avatar
Gildas Bazin committed
655 656 657
            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;
658

Gildas Bazin's avatar
Gildas Bazin committed
659 660 661 662 663
            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);
            }
664

Gildas Bazin's avatar
Gildas Bazin committed
665
            /* Setup rgb mask for RGB formats */
666
            if( p_stream->i_fourcc == VLC_CODEC_RGB24 )
Gildas Bazin's avatar
Gildas Bazin committed
667 668
            {
                /* This is in BGR format */
669 670 671
                fmt.video.i_bmask = 0x00ff0000;
                fmt.video.i_gmask = 0x0000ff00;
                fmt.video.i_rmask = 0x000000ff;
Gildas Bazin's avatar
Gildas Bazin committed
672
            }
673 674 675 676 677 678 679

            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
680 681 682 683
        }
        else if( p_stream->mt.majortype == MEDIATYPE_Audio )
        {
            es_format_Init( &fmt, AUDIO_ES, p_stream->i_fourcc );
684

Gildas Bazin's avatar
Gildas Bazin committed
685 686 687 688 689 690 691 692 693 694
            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 );
695
    }
Gildas Bazin's avatar
Gildas Bazin committed
696

697
    return VLC_SUCCESS;
698 699
}

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

Rémi Duraffort's avatar
Rémi Duraffort committed
708
    p_access->p_sys = p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
Rémi Duraffort's avatar
Rémi Duraffort committed
709 710
    if( !p_sys )
        return VLC_ENOMEM;
711

712
    if( CommonOpen( p_this, p_sys, false ) != VLC_SUCCESS )
Gildas Bazin's avatar
Gildas Bazin committed
713 714 715 716
    {
        CommonClose( p_this, p_sys );
        return VLC_EGENERIC;
    }
717

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

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

Gildas Bazin's avatar
Gildas Bazin committed
735
    /* Setup Access */
736 737
    p_access->pf_read = NULL;
    p_access->pf_block = ReadCompressed;
Gildas Bazin's avatar
Gildas Bazin committed
738 739 740 741 742
    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;
743
    p_access->info.b_eof = false;
Gildas Bazin's avatar
Gildas Bazin committed
744 745 746 747 748 749 750 751 752
    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;
753
}
Gildas Bazin's avatar
 
Gildas Bazin committed
754

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

Gildas Bazin's avatar
Gildas Bazin committed
762
    DeleteDirectShowGraph( p_sys );
763

Gildas Bazin's avatar
Gildas Bazin committed
764 765
    /* Uninitialize OLE/COM */
    CoUninitialize();
766

767 768
    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
769 770 771

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

Gildas Bazin's avatar
Gildas Bazin committed
773
    free( p_sys );
774 775
}

Gildas Bazin's avatar
Gildas Bazin committed
776 777 778 779
/*****************************************************************************
 * AccessClose: close device
 *****************************************************************************/
static void AccessClose( vlc_object_t *p_this )
780
{
Gildas Bazin's avatar
Gildas Bazin committed
781 782
    access_t     *p_access = (access_t *)p_this;
    access_sys_t *p_sys    = p_access->p_sys;
783

Gildas Bazin's avatar
Gildas Bazin committed
784 785
    /* Stop capturing stuff */
    p_sys->p_control->Stop();
786

Gildas Bazin's avatar
Gildas Bazin committed
787 788
    CommonClose( p_this, p_sys );
}
789

Gildas Bazin's avatar
Gildas Bazin committed
790 791 792 793 794 795 796
/*****************************************************************************
 * 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;
797

Gildas Bazin's avatar
Gildas Bazin committed
798 799
    /* Stop capturing stuff */
    p_sys->p_control->Stop();
800

Gildas Bazin's avatar
Gildas Bazin committed
801
    CommonClose( p_this, p_sys );
Gildas Bazin's avatar
 
Gildas Bazin committed
802 803 804 805 806
}

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

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

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

824 825
        // 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
826
        IEnumPins *pins = NULL;
827 828
        if( ( mediaType.majortype == MEDIATYPE_Video ||
              mediaType.majortype == MEDIATYPE_Stream ) &&
829
            SUCCEEDED(p_filter->EnumPins(&pins)) )
Gildas Bazin's avatar
 
Gildas Bazin committed
830
        {
Rémi Duraffort's avatar
Rémi Duraffort committed
831
            IPin        *pP = NULL;
832