dshow.cpp 71.4 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
 *****************************************************************************
Rocky Bernstein's avatar
Rocky Bernstein committed
4
 * Copyright (C) 2002, 2003 VideoLAN
5
 * $Id$
Gildas Bazin's avatar
 
Gildas Bazin committed
6
 *
7
 * Author: Gildas Bazin <gbazin@videolan.org>
Gildas Bazin's avatar
 
Gildas Bazin committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <vlc/vlc.h>
#include <vlc/input.h>
#include <vlc/vout.h>

Gildas Bazin's avatar
Gildas Bazin committed
35
#include "common.h"
Gildas Bazin's avatar
 
Gildas Bazin committed
36 37
#include "filter.h"

Gildas Bazin's avatar
 
Gildas Bazin committed
38 39 40
/*****************************************************************************
 * Access: local prototypes
 *****************************************************************************/
41
static block_t *ReadCompressed( access_t * );
42
static int AccessControl ( access_t *, int, va_list );
Gildas Bazin's avatar
 
Gildas Bazin committed
43

Gildas Bazin's avatar
Gildas Bazin committed
44 45 46 47
static int Demux       ( demux_t * );
static int DemuxControl( demux_t *, int, va_list );

static int OpenDevice( vlc_object_t *, access_sys_t *, string, vlc_bool_t );
Gildas Bazin's avatar
 
Gildas Bazin committed
48 49
static IBaseFilter *FindCaptureDevice( vlc_object_t *, string *,
                                       list<string> *, vlc_bool_t );
50
static size_t EnumDeviceCaps( vlc_object_t *, IBaseFilter *,
51 52
                              int, int, int, int, int, int,
                              AM_MEDIA_TYPE *mt, size_t );
Gildas Bazin's avatar
Gildas Bazin committed
53 54
static bool ConnectFilters( vlc_object_t *, access_sys_t *,
                            IBaseFilter *, CaptureFilter * );
Gildas Bazin's avatar
 
Gildas Bazin committed
55 56
static int FindDevicesCallback( vlc_object_t *, char const *,
                                vlc_value_t, vlc_value_t, void * );
57 58
static int ConfigDevicesCallback( vlc_object_t *, char const *,
                                  vlc_value_t, vlc_value_t, void * );
Gildas Bazin's avatar
 
Gildas Bazin committed
59

60 61
static void ShowPropertyPage( IUnknown * );
static void ShowDeviceProperties( vlc_object_t *, ICaptureGraphBuilder2 *, 
62
                                  IBaseFilter *, vlc_bool_t );
63 64
static void ShowTunerProperties( vlc_object_t *, ICaptureGraphBuilder2 *, 
                                 IBaseFilter *, vlc_bool_t );
65 66
static void ConfigTuner( vlc_object_t *, ICaptureGraphBuilder2 *,
                         IBaseFilter * );
Gildas Bazin's avatar
 
Gildas Bazin committed
67

Gildas Bazin's avatar
 
Gildas Bazin committed
68
/*****************************************************************************
69
 * Module descriptor
Gildas Bazin's avatar
 
Gildas Bazin committed
70
 *****************************************************************************/
71 72 73 74
static char *ppsz_vdev[] = { "", "none" };
static char *ppsz_vdev_text[] = { N_("Default"), N_("None") };
static char *ppsz_adev[] = { "", "none" };
static char *ppsz_adev_text[] = { N_("Default"), N_("None") };
75 76 77
static int  pi_tuner_input[] = { 0, 1, 2 };
static char *ppsz_tuner_input_text[] =
    {N_("Default"), N_("Cable"), N_("Antenna")};
78

Gildas Bazin's avatar
 
Gildas Bazin committed
79 80
#define CACHING_TEXT N_("Caching value in ms")
#define CACHING_LONGTEXT N_( \
81
    "Allows you to modify the default caching value for DirectShow streams. " \
Rocky Bernstein's avatar
Rocky Bernstein committed
82
    "This value should be set in milliseconds units." )
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#define VDEV_TEXT N_("Video device name")
#define VDEV_LONGTEXT N_( \
    "You can specify the name of the video device that will be used by the " \
    "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_( \
    "You can specify the name of the audio device that will be used by the " \
    "DirectShow plugin. If you don't specify anything, the default device " \
    "will be used.")
#define SIZE_TEXT N_("Video size")
#define SIZE_LONGTEXT N_( \
    "You can specify the size of the video that will be displayed by the " \
    "DirectShow plugin. If you don't specify anything the default size for " \
    "your device will be used.")
#define CHROMA_TEXT N_("Video input chroma format")
#define CHROMA_LONGTEXT N_( \
    "Force the DirectShow video input to use a specific chroma format " \
101
    "(eg. I420 (default), RV24, etc.)")
102 103 104 105
#define FPS_TEXT N_("Video input frame rate")
#define FPS_LONGTEXT N_( \
    "Force the DirectShow video input to use a specific frame rate" \
    "(eg. 0 means default, 25, 29.97, 50, 59.94, etc.)")
106
#define CONFIG_TEXT N_("Device properties")
Gildas Bazin's avatar
 
Gildas Bazin committed
107
#define CONFIG_LONGTEXT N_( \
108 109
    "Show the properties dialog of the selected device before starting the " \
    "stream.")
110 111 112
#define TUNER_TEXT N_("Tuner properties")
#define TUNER_LONGTEXT N_( \
    "Show the tuner properties [channel selection] page." )
113 114 115 116 117 118 119 120 121 122 123
#define CHANNEL_TEXT N_("Tuner TV Channel")
#define CHANNEL_LONGTEXT N_( \
    "Allows you to set the TV channel the tuner will set to " \
    "(0 means default)." )
#define COUNTRY_TEXT N_("Tuner country code")
#define COUNTRY_LONGTEXT N_( \
    "Allows you to set the tuner country code that establishes the current " \
    "channel-to-frequency mapping (0 means default)." )
#define TUNER_INPUT_TEXT N_("Tuner input type")
#define TUNER_INPUT_LONGTEXT N_( \
    "Allows you to select the tuner input type (Cable/Antenna)." )
Gildas Bazin's avatar
 
Gildas Bazin committed
124

Gildas Bazin's avatar
Gildas Bazin committed
125 126 127
static int  CommonOpen ( vlc_object_t *, access_sys_t *, vlc_bool_t );
static void CommonClose( vlc_object_t *, access_sys_t * );

128 129 130 131 132 133
static int  AccessOpen ( vlc_object_t * );
static void AccessClose( vlc_object_t * );

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

Gildas Bazin's avatar
 
Gildas Bazin committed
134
vlc_module_begin();
135
    set_shortname( _("DirectShow") );
Gildas Bazin's avatar
 
Gildas Bazin committed
136
    set_description( _("DirectShow input") );
Clément Stenac's avatar
Clément Stenac committed
137 138
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_ACCESS );
139
    add_integer( "dshow-caching", (mtime_t)(0.2*CLOCK_FREQ) / 1000, NULL,
Gildas Bazin's avatar
 
Gildas Bazin committed
140
                 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
141

142
    add_string( "dshow-vdev", NULL, NULL, VDEV_TEXT, VDEV_LONGTEXT, VLC_FALSE);
143
        change_string_list( ppsz_vdev, ppsz_vdev_text, FindDevicesCallback );
144 145
        change_action_add( FindDevicesCallback, N_("Refresh list") );
        change_action_add( ConfigDevicesCallback, N_("Configure") );
146

147
    add_string( "dshow-adev", NULL, NULL, ADEV_TEXT, ADEV_LONGTEXT, VLC_FALSE);
148
        change_string_list( ppsz_adev, ppsz_adev_text, FindDevicesCallback );
149 150
        change_action_add( FindDevicesCallback, N_("Refresh list") );
        change_action_add( ConfigDevicesCallback, N_("Configure") );
151

152
    add_string( "dshow-size", NULL, NULL, SIZE_TEXT, SIZE_LONGTEXT, VLC_FALSE);
153

154 155
    add_string( "dshow-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
                VLC_TRUE );
Gildas Bazin's avatar
 
Gildas Bazin committed
156

157 158 159
    add_float( "dshow-fps", 0.0f, NULL, FPS_TEXT, FPS_LONGTEXT,
                VLC_TRUE );

Gildas Bazin's avatar
 
Gildas Bazin committed
160
    add_bool( "dshow-config", VLC_FALSE, NULL, CONFIG_TEXT, CONFIG_LONGTEXT,
161
              VLC_FALSE );
Gildas Bazin's avatar
 
Gildas Bazin committed
162

163 164 165
    add_bool( "dshow-tuner", VLC_FALSE, NULL, TUNER_TEXT, TUNER_LONGTEXT,
              VLC_FALSE );

166 167 168 169 170 171 172 173 174 175
    add_integer( "dshow-tuner-channel", 0, NULL, CHANNEL_TEXT,
                 CHANNEL_LONGTEXT, VLC_TRUE );

    add_integer( "dshow-tuner-country", 0, NULL, COUNTRY_TEXT,
                 COUNTRY_LONGTEXT, VLC_TRUE );

    add_integer( "dshow-tuner-input", 0, NULL, TUNER_INPUT_TEXT,
                 TUNER_INPUT_LONGTEXT, VLC_TRUE );
        change_integer_list( pi_tuner_input, ppsz_tuner_input_text, 0 );

Gildas Bazin's avatar
 
Gildas Bazin committed
176
    add_shortcut( "dshow" );
Gildas Bazin's avatar
Gildas Bazin committed
177 178
    set_capability( "access_demux", 0 );
    set_callbacks( DemuxOpen, DemuxClose );
Gildas Bazin's avatar
 
Gildas Bazin committed
179 180

    add_submodule();
Gildas Bazin's avatar
Gildas Bazin committed
181
    set_description( _("DirectShow input") );
Gildas Bazin's avatar
 
Gildas Bazin committed
182
    add_shortcut( "dshow" );
Gildas Bazin's avatar
Gildas Bazin committed
183 184
    set_capability( "access2", 0 );
    set_callbacks( AccessOpen, AccessClose );
Gildas Bazin's avatar
 
Gildas Bazin committed
185 186 187

vlc_module_end();

Gildas Bazin's avatar
Gildas Bazin committed
188
/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
189
 * DirectShow elementary stream descriptor
Gildas Bazin's avatar
Gildas Bazin committed
190
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204
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
205 206 207
    int             i_fourcc;
    es_out_id_t     *p_es;

208
    vlc_bool_t      b_pts;
Gildas Bazin's avatar
 
Gildas Bazin committed
209 210 211

} dshow_stream_t;

Gildas Bazin's avatar
Gildas Bazin committed
212
/*****************************************************************************
213
 * DirectShow utility functions
Gildas Bazin's avatar
Gildas Bazin committed
214
 *****************************************************************************/
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
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
269
/*****************************************************************************
Gildas Bazin's avatar
Gildas Bazin committed
270
 * CommonOpen: open direct show device
Gildas Bazin's avatar
 
Gildas Bazin committed
271
 *****************************************************************************/
Gildas Bazin's avatar
Gildas Bazin committed
272 273
static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
                       vlc_bool_t b_access_demux )
Gildas Bazin's avatar
 
Gildas Bazin committed
274
{
275
    vlc_value_t  val;
Gildas Bazin's avatar
Gildas Bazin committed
276
    int i;
Gildas Bazin's avatar
 
Gildas Bazin committed
277

278
    /* Get/parse options and open device(s) */
Gildas Bazin's avatar
 
Gildas Bazin committed
279
    string vdevname, adevname;
280
    int i_width = 0, i_height = 0, i_chroma = 0;
Gildas Bazin's avatar
Gildas Bazin committed
281
    vlc_bool_t b_audio = VLC_TRUE;
Gildas Bazin's avatar
 
Gildas Bazin committed
282

283 284
    var_Create( p_this, "dshow-config", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_this, "dshow-tuner", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
285

Gildas Bazin's avatar
Gildas Bazin committed
286 287
    var_Create( p_this, "dshow-vdev", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_this, "dshow-vdev", &val );
288 289
    if( val.psz_string ) vdevname = string( val.psz_string );
    if( val.psz_string ) free( val.psz_string );
Gildas Bazin's avatar
 
Gildas Bazin committed
290

Gildas Bazin's avatar
Gildas Bazin committed
291 292
    var_Create( p_this, "dshow-adev", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_this, "dshow-adev", &val );
293 294 295
    if( val.psz_string ) adevname = string( val.psz_string );
    if( val.psz_string ) free( val.psz_string );

Gildas Bazin's avatar
Gildas Bazin committed
296 297 298 299 300 301 302 303
    static struct {char *psz_size; int  i_width; int  i_height;} size_table[] =
    { { "subqcif", 128, 96 }, { "qsif", 160, 120 }, { "qcif", 176, 144 },
      { "sif", 320, 240 }, { "cif", 352, 288 }, { "cif", 640, 480 },
      { 0, 0, 0 },
    };

    var_Create( p_this, "dshow-size", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_this, "dshow-size", &val );
304
    if( val.psz_string && *val.psz_string )
Gildas Bazin's avatar
 
Gildas Bazin committed
305
    {
Gildas Bazin's avatar
Gildas Bazin committed
306
        for( i = 0; size_table[i].psz_size; i++ )
307
        {
Gildas Bazin's avatar
Gildas Bazin committed
308 309 310 311 312 313
            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;
            }
314
        }
Gildas Bazin's avatar
Gildas Bazin committed
315
        if( !size_table[i].psz_size ) /* Try to parse "WidthxHeight" */
316 317 318 319
        {
            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
320
            {
321
                i_height = strtol( psz_parser + 1, &psz_parser, 0 );
Gildas Bazin's avatar
 
Gildas Bazin committed
322
            }
Gildas Bazin's avatar
Gildas Bazin committed
323
            msg_Dbg( p_this, "Width x Height %dx%d", i_width, i_height );
Gildas Bazin's avatar
 
Gildas Bazin committed
324 325
        }
    }
326
    if( val.psz_string ) free( val.psz_string );
Gildas Bazin's avatar
 
Gildas Bazin committed
327

Gildas Bazin's avatar
Gildas Bazin committed
328 329
    var_Create( p_this, "dshow-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_this, "dshow-chroma", &val );
330 331 332 333 334 335
    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] );
    }
    if( val.psz_string ) free( val.psz_string );
Gildas Bazin's avatar
 
Gildas Bazin committed
336

337
    var_Create( p_this, "dshow-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
338 339 340 341 342 343 344
    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 );

Gildas Bazin's avatar
Gildas Bazin committed
345
    var_Create( p_this, "dshow-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
Gildas Bazin's avatar
 
Gildas Bazin committed
346 347

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

Gildas Bazin's avatar
 
Gildas Bazin committed
350 351
    /* Initialize some data */
    p_sys->i_streams = 0;
352
    p_sys->pp_streams = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
353 354 355
    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
356

357 358 359
    p_sys->p_graph = NULL;
    p_sys->p_capture_graph_builder2 = NULL;
    p_sys->p_control = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
360

361 362 363
    vlc_mutex_init( p_this, &p_sys->lock );
    vlc_cond_init( p_this, &p_sys->wait );

364 365
    /* Build directshow graph */
    CreateDirectShowGraph( p_sys );
Gildas Bazin's avatar
 
Gildas Bazin committed
366

Gildas Bazin's avatar
Gildas Bazin committed
367
    if( OpenDevice( p_this, p_sys, vdevname, 0 ) != VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
368
    {
Gildas Bazin's avatar
Gildas Bazin committed
369
        msg_Err( p_this, "can't open video");
Gildas Bazin's avatar
 
Gildas Bazin committed
370
    }
Gildas Bazin's avatar
Gildas Bazin committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
    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') )
            {
                b_audio = VLC_FALSE;
387 388 389 390

                if( b_access_demux )
                {
                    /* Let the access (only) take care of that */
391
                    return VLC_EGENERIC;
392
                }
Gildas Bazin's avatar
Gildas Bazin committed
393 394
            }
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
395

396 397 398 399 400 401 402 403 404
        if( p_stream->mt.majortype == MEDIATYPE_Stream )
        {
            b_audio = VLC_FALSE;

            if( b_access_demux )
            {
                /* Let the access (only) take care of that */
                return VLC_EGENERIC;
            }
405 406 407 408 409 410 411 412 413

            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 );
            }
414
        }
Gildas Bazin's avatar
Gildas Bazin committed
415 416 417
    }

    if( b_audio && OpenDevice( p_this, p_sys, adevname, 1 ) != VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
418
    {
Gildas Bazin's avatar
Gildas Bazin committed
419
        msg_Err( p_this, "can't open audio");
Gildas Bazin's avatar
 
Gildas Bazin committed
420
    }
Gildas Bazin's avatar
Gildas Bazin committed
421

422
    for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
Gildas Bazin's avatar
 
Gildas Bazin committed
423
    {
424 425 426 427 428
        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
429

430 431
        if( SUCCEEDED(pXbar->Route(VideoOutputIndex, VideoInputIndex)) )
        {
Gildas Bazin's avatar
Gildas Bazin committed
432
            msg_Dbg( p_this, "Crossbar at depth %d, Routed video "
433
                     "ouput %ld to video input %ld", i, VideoOutputIndex,
434
                     VideoInputIndex );
Gildas Bazin's avatar
 
Gildas Bazin committed
435

436 437 438 439 440
            if( AudioOutputIndex != -1 && AudioInputIndex != -1 )
            {
                if( SUCCEEDED( pXbar->Route(AudioOutputIndex,
                                            AudioInputIndex)) )
                {
Gildas Bazin's avatar
Gildas Bazin committed
441
                    msg_Dbg(p_this, "Crossbar at depth %d, Routed audio "
442
                            "ouput %ld to audio input %ld", i,
443 444 445 446 447 448
                            AudioOutputIndex, AudioInputIndex );
                }
            }
        }
    }

449 450 451 452
    /*
    ** Show properties pages from other filters in graph
    */
    var_Get( p_this, "dshow-config", &val );
453
    if( val.b_bool )
454 455 456 457 458
    {
        for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
        {
            IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
            IBaseFilter *p_XF;
459 460 461

            if( SUCCEEDED( pXbar->QueryInterface( IID_IBaseFilter,
                                                  (void **)&p_XF ) ) )
462 463 464 465
            {
                ShowPropertyPage( p_XF );
                p_XF->Release();
            }
466
        }
467 468
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
469 470
    /* Initialize some data */
    p_sys->i_current_stream = 0;
471

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

Gildas Bazin's avatar
 
Gildas Bazin committed
474 475 476 477
    return VLC_SUCCESS;
}

/*****************************************************************************
Gildas Bazin's avatar
Gildas Bazin committed
478
 * DemuxOpen: open direct show device as an access_demux module
Gildas Bazin's avatar
 
Gildas Bazin committed
479
 *****************************************************************************/
Gildas Bazin's avatar
Gildas Bazin committed
480
static int DemuxOpen( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
481
{
Gildas Bazin's avatar
Gildas Bazin committed
482 483 484
    demux_t      *p_demux = (demux_t *)p_this;
    access_sys_t *p_sys;
    int i;
Gildas Bazin's avatar
 
Gildas Bazin committed
485

Gildas Bazin's avatar
Gildas Bazin committed
486 487 488
    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;
Gildas Bazin's avatar
 
Gildas Bazin committed
489

Gildas Bazin's avatar
Gildas Bazin committed
490 491 492 493 494
    if( CommonOpen( p_this, p_sys, VLC_TRUE ) != VLC_SUCCESS )
    {
        CommonClose( p_this, p_sys );
        return VLC_EGENERIC;
    }
495

Gildas Bazin's avatar
Gildas Bazin committed
496 497 498
    /* Everything is ready. Let's rock baby */
    msg_Dbg( p_this, "Playing...");
    p_sys->p_control->Run();
499

Gildas Bazin's avatar
Gildas Bazin committed
500 501 502 503 504 505 506
    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++ )
507
    {
Gildas Bazin's avatar
Gildas Bazin committed
508 509
        dshow_stream_t *p_stream = p_sys->pp_streams[i];
        es_format_t fmt;
510

Gildas Bazin's avatar
Gildas Bazin committed
511 512 513
        if( p_stream->mt.majortype == MEDIATYPE_Video )
        {
            es_format_Init( &fmt, VIDEO_ES, p_stream->i_fourcc );
514

Gildas Bazin's avatar
Gildas Bazin committed
515 516 517
            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;
518

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

Gildas Bazin's avatar
Gildas Bazin committed
525 526 527 528
            /* Setup rgb mask for RGB formats */
            if( p_stream->i_fourcc == VLC_FOURCC('R','V','2','4') )
            {
                /* This is in BGR format */
529 530 531
                fmt.video.i_bmask = 0x00ff0000;
                fmt.video.i_gmask = 0x0000ff00;
                fmt.video.i_rmask = 0x000000ff;
Gildas Bazin's avatar
Gildas Bazin committed
532
            }
533 534 535 536 537 538 539

            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
540 541 542 543
        }
        else if( p_stream->mt.majortype == MEDIATYPE_Audio )
        {
            es_format_Init( &fmt, AUDIO_ES, p_stream->i_fourcc );
544

Gildas Bazin's avatar
Gildas Bazin committed
545 546 547 548 549 550 551 552 553 554
            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 );
555
    }
Gildas Bazin's avatar
Gildas Bazin committed
556

557
    return VLC_SUCCESS;
558 559
}

Gildas Bazin's avatar
Gildas Bazin committed
560 561 562 563
/*****************************************************************************
 * AccessOpen: open direct show device as an access module
 *****************************************************************************/
static int AccessOpen( vlc_object_t *p_this )
564
{
Gildas Bazin's avatar
Gildas Bazin committed
565 566
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;
567

Gildas Bazin's avatar
Gildas Bazin committed
568 569
    p_access->p_sys = p_sys = (access_sys_t *)malloc( sizeof( access_sys_t ) );
    memset( p_sys, 0, sizeof( access_sys_t ) );
570

571
    if( CommonOpen( p_this, p_sys, VLC_FALSE ) != VLC_SUCCESS )
Gildas Bazin's avatar
Gildas Bazin committed
572 573 574 575
    {
        CommonClose( p_this, p_sys );
        return VLC_EGENERIC;
    }
576

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

Gildas Bazin's avatar
Gildas Bazin committed
579 580
    /* Check if we need to force demuxers */
    if( !p_access->psz_demux || !*p_access->psz_demux )
Gildas Bazin's avatar
 
Gildas Bazin committed
581
    {
Gildas Bazin's avatar
Gildas Bazin committed
582 583 584
        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') )
585
        {
Gildas Bazin's avatar
Gildas Bazin committed
586 587 588 589 590
            p_access->psz_demux = strdup( "rawdv" );
        }
        else if( p_stream->i_fourcc == VLC_FOURCC('m','p','2','v') )
        {
            p_access->psz_demux = "mpgv";
591
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
592
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
593

Gildas Bazin's avatar
Gildas Bazin committed
594
    /* Setup Access */
595 596
    p_access->pf_read = NULL;
    p_access->pf_block = ReadCompressed;
Gildas Bazin's avatar
Gildas Bazin committed
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
    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;
    p_access->info.b_eof = VLC_FALSE;
    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;
612
}
Gildas Bazin's avatar
 
Gildas Bazin committed
613

Gildas Bazin's avatar
Gildas Bazin committed
614 615 616 617
/*****************************************************************************
 * CommonClose: close device
 *****************************************************************************/
static void CommonClose( vlc_object_t *p_this, access_sys_t *p_sys )
618
{
Gildas Bazin's avatar
Gildas Bazin committed
619
    msg_Dbg( p_this, "Releasing DirectShow");
620

Gildas Bazin's avatar
Gildas Bazin committed
621
    DeleteDirectShowGraph( p_sys );
622

Gildas Bazin's avatar
Gildas Bazin committed
623 624
    /* Uninitialize OLE/COM */
    CoUninitialize();
625

626 627
    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
628 629 630

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

Gildas Bazin's avatar
Gildas Bazin committed
632
    free( p_sys );
633 634
}

Gildas Bazin's avatar
Gildas Bazin committed
635 636 637 638
/*****************************************************************************
 * AccessClose: close device
 *****************************************************************************/
static void AccessClose( vlc_object_t *p_this )
639
{
Gildas Bazin's avatar
Gildas Bazin committed
640 641
    access_t     *p_access = (access_t *)p_this;
    access_sys_t *p_sys    = p_access->p_sys;
642

Gildas Bazin's avatar
Gildas Bazin committed
643 644
    /* Stop capturing stuff */
    p_sys->p_control->Stop();
645

Gildas Bazin's avatar
Gildas Bazin committed
646 647
    CommonClose( p_this, p_sys );
}
648

Gildas Bazin's avatar
Gildas Bazin committed
649 650 651 652 653 654 655
/*****************************************************************************
 * 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;
656

Gildas Bazin's avatar
Gildas Bazin committed
657 658
    /* Stop capturing stuff */
    p_sys->p_control->Stop();
659

Gildas Bazin's avatar
Gildas Bazin committed
660
    CommonClose( p_this, p_sys );
Gildas Bazin's avatar
 
Gildas Bazin committed
661 662 663 664 665
}

/****************************************************************************
 * ConnectFilters
 ****************************************************************************/
Gildas Bazin's avatar
Gildas Bazin committed
666 667
static bool ConnectFilters( vlc_object_t *p_this, access_sys_t *p_sys,
                            IBaseFilter *p_filter,
668
                            CaptureFilter *p_capture_filter )
Gildas Bazin's avatar
 
Gildas Bazin committed
669
{
670
    CapturePin *p_input_pin = p_capture_filter->CustomGetPin();
Gildas Bazin's avatar
 
Gildas Bazin committed
671

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

674
    if( p_sys->p_capture_graph_builder2 )
Gildas Bazin's avatar
 
Gildas Bazin committed
675
    {
676
        if( FAILED(p_sys->p_capture_graph_builder2->
Gildas Bazin's avatar
Gildas Bazin committed
677 678
                RenderStream( &PIN_CATEGORY_CAPTURE, &mediaType.majortype,
                              p_filter, 0, (IBaseFilter *)p_capture_filter )) )
679 680 681
        {
            return false;
        }
682

683 684 685
        // Sort out all the possible video inputs
        // The class needs to be given the capture filters ANALOGVIDEO input pin
        IEnumPins *pins = 0;
686 687
        if( ( mediaType.majortype == MEDIATYPE_Video ||
              mediaType.majortype == MEDIATYPE_Stream ) &&
688
            SUCCEEDED(p_filter->EnumPins(&pins)) )
Gildas Bazin's avatar
 
Gildas Bazin committed
689
        {
690 691 692 693 694 695 696 697
            IPin        *pP = 0;
            ULONG        n;
            PIN_INFO     pinInfo;
            BOOL         Found = FALSE;
            IKsPropertySet *pKs=0;
            GUID guid;
            DWORD dw;

Gildas Bazin's avatar
Gildas Bazin committed
698
            while( !Found && ( S_OK == pins->Next(1, &pP, &n) ) )
699
            {
Gildas Bazin's avatar
Gildas Bazin committed
700
                if( S_OK == pP->QueryPinInfo(&pinInfo) )
701
                {
Gildas Bazin's avatar
Gildas Bazin committed
702 703 704 705
                    // is this pin an ANALOGVIDEOIN input pin?
                    if( pinInfo.dir == PINDIR_INPUT &&
                        pP->QueryInterface( IID_IKsPropertySet,
                                            (void **)&pKs ) == S_OK )
706
                    {
Gildas Bazin's avatar
Gildas Bazin committed
707 708 709
                        if( pKs->Get( AMPROPSETID_Pin,
                                      AMPROPERTY_PIN_CATEGORY, NULL, 0,
                                      &guid, sizeof(GUID), &dw ) == S_OK )
710
                        {
Gildas Bazin's avatar
Gildas Bazin committed
711
                            if( guid == PIN_CATEGORY_ANALOGVIDEOIN )
712
                            {
Gildas Bazin's avatar
Gildas Bazin committed
713
                                // recursively search crossbar routes
714
                                FindCrossbarRoutes( p_this, p_sys, pP, 0 );
Gildas Bazin's avatar
Gildas Bazin committed
715 716
                                // found it
                                Found = TRUE;
717 718
                            }
                        }
Gildas Bazin's avatar
Gildas Bazin committed
719
                        pKs->Release();
720 721 722 723 724 725
                    }
                    pinInfo.pFilter->Release();
                }
                pP->Release();
            }
            pins->Release();
Gildas Bazin's avatar
 
Gildas Bazin committed
726
        }
727
        return true;
Gildas Bazin's avatar
 
Gildas Bazin committed
728
    }
729 730 731 732
    else
    {
        IEnumPins *p_enumpins;
        IPin *p_pin;
Gildas Bazin's avatar
 
Gildas Bazin committed
733

734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
        if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return false;

        while( S_OK == p_enumpins->Next( 1, &p_pin, NULL ) )
        {
            PIN_DIRECTION pin_dir;
            p_pin->QueryDirection( &pin_dir );

            if( pin_dir == PINDIR_OUTPUT &&
                p_sys->p_graph->ConnectDirect( p_pin, (IPin *)p_input_pin,
                                               0 ) == S_OK )
            {
                p_pin->Release();
                p_enumpins->Release();
                return true;
            }
            p_pin->Release();
        }

        p_enumpins->Release();
        return false;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
755 756
}

757
/*
Gildas Bazin's avatar
Gildas Bazin committed
758 759 760
 * get fourcc priority from arbritary preference, the higher the better
 */
static int GetFourCCPriority( int i_fourcc )
761 762 763
{
    switch( i_fourcc )
    {
Gildas Bazin's avatar
Gildas Bazin committed
764 765 766 767 768 769 770 771 772 773 774 775
    case VLC_FOURCC('I','4','2','0'):
    case VLC_FOURCC('f','l','3','2'):
        return 9;
    case VLC_FOURCC('Y','V','1','2'):
    case VLC_FOURCC('a','r','a','w'):
        return 8;
    case VLC_FOURCC('R','V','2','4'):
        return 7;
    case VLC_FOURCC('Y','U','Y','2'):
    case VLC_FOURCC('R','V','3','2'):
    case VLC_FOURCC('R','G','B','A'):
        return 6;
776
    }
Gildas Bazin's avatar
Gildas Bazin committed
777

778 779 780 781 782
    return 0;
}

#define MAX_MEDIA_TYPES 32

Gildas Bazin's avatar
Gildas Bazin committed
783 784
static int OpenDevice( vlc_object_t *p_this, access_sys_t *p_sys,
                       string devicename, vlc_bool_t b_audio )
Gildas Bazin's avatar
 
Gildas Bazin committed
785
{
786 787 788
    /* See if device is already opened */
    for( int i = 0; i < p_sys->i_streams; i++ )
    {
Gildas Bazin's avatar
Gildas Bazin committed
789 790
        if( devicename.size() &&
            p_sys->pp_streams[i]->devicename == devicename )
791 792 793 794 795 796
        {
            /* Already opened */
            return VLC_SUCCESS;
        }
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
797 798
    list<string> list_devices;

799
    /* Enumerate devices and display their names */
Gildas Bazin's avatar
Gildas Bazin committed
800
    FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
Gildas Bazin's avatar
 
Gildas Bazin committed
801

802 803 804
    if( !list_devices.size() )
        return VLC_EGENERIC;

Gildas Bazin's avatar
 
Gildas Bazin committed
805 806
    list<string>::iterator iter;
    for( iter = list_devices.begin(); iter != list_devices.end(); iter++ )
Gildas Bazin's avatar
Gildas Bazin committed
807
        msg_Dbg( p_this, "found device: %s", iter->c_str() );
Gildas Bazin's avatar
 
Gildas Bazin committed
808 809 810 811 812 813 814 815

    /* If no device name was specified, pick the 1st one */
    if( devicename.size() == 0 )
    {
        devicename = *list_devices.begin();
    }

    // Use the system device enumerator and class enumerator to find
Gildas Bazin's avatar
 
Gildas Bazin committed
816 817
    // a capture/preview device, such as a desktop USB video camera.
    IBaseFilter *p_device_filter =
Gildas Bazin's avatar
Gildas Bazin committed
818
        FindCaptureDevice( p_this, &devicename, 0, b_audio );
Gildas Bazin's avatar
 
Gildas Bazin committed
819
    if( p_device_filter )
Gildas Bazin's avatar
Gildas Bazin committed
820
        msg_Dbg( p_this, "using device: %s", devicename.c_str() );
Gildas Bazin's avatar
 
Gildas Bazin committed
821 822
    else
    {
Gildas Bazin's avatar
Gildas Bazin committed
823
        msg_Err( p_this, "can't use device: %s, unsupported device type",
824
                 devicename.c_str() );
Gildas Bazin's avatar
 
Gildas Bazin committed
825 826
        return VLC_EGENERIC;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
827

828 829 830 831 832 833 834 835 836 837 838
    // Retreive acceptable media types supported by device
    AM_MEDIA_TYPE media_types[MAX_MEDIA_TYPES];
    size_t media_count =
        EnumDeviceCaps( p_this, p_device_filter, p_sys->i_chroma,
                        p_sys->i_width, p_sys->i_height,
                        0, 0, 0, media_types, MAX_MEDIA_TYPES );

    /* Find out if the pin handles MEDIATYPE_Stream, in which case we
     * won't add a prefered media type as this doesn't seem to work well
     * -- to investigate. */
    vlc_bool_t b_stream_type = VLC_FALSE;
839
    for( size_t i = 0; i < media_count; i++ )
840 841 842 843 844 845 846 847
    {
        if( media_types[i].majortype == MEDIATYPE_Stream )
        {
            b_stream_type = VLC_TRUE;
            break;
        }
    }

848 849
    size_t mt_count = 0;
    AM_MEDIA_TYPE *mt = NULL;
Gildas Bazin's avatar
Gildas Bazin committed
850

851
    if( !b_stream_type && !b_audio )
Gildas Bazin's avatar
Gildas Bazin committed
852
    {
853
        // Insert prefered video media type
854
        AM_MEDIA_TYPE mtr;
Gildas Bazin's avatar
Gildas Bazin committed
855 856 857 858 859 860 861 862 863 864
        VIDEOINFOHEADER vh;

        mtr.majortype            = MEDIATYPE_Video;
        mtr.subtype              = MEDIASUBTYPE_I420;
        mtr.bFixedSizeSamples    = TRUE;
        mtr.bTemporalCompression = FALSE;
        mtr.pUnk                 = NULL;
        mtr.formattype           = FORMAT_VideoInfo;
        mtr.cbFormat             = sizeof(vh);
        mtr.pbFormat             = (BYTE *)&vh;
865 866 867

        memset(&vh, 0, sizeof(vh));

Gildas Bazin's avatar
Gildas Bazin committed
868
        vh.bmiHeader.biSize   = sizeof(vh.bmiHeader);
869
        vh.bmiHeader.biWidth  = p_sys->i_width > 0 ? p_sys->i_width : 320;
Gildas Bazin's avatar
Gildas Bazin committed
870
        vh.bmiHeader.biHeight = p_sys->i_height > 0 ? p_sys->i_height : 240;
871
        vh.bmiHeader.biPlanes      = 3;
Gildas Bazin's avatar
Gildas Bazin committed
872 873
        vh.bmiHeader.biBitCount    = 12;
        vh.bmiHeader.biCompression = VLC_FOURCC('I','4','2','0');
874 875 876
        vh.bmiHeader.biSizeImage   = vh.bmiHeader.biWidth * 12 *
            vh.bmiHeader.biHeight / 8;
        mtr.lSampleSize            = vh.bmiHeader.biSizeImage;
877

Gildas Bazin's avatar
Gildas Bazin committed
878 879 880
        mt_count = 1;
        mt = (AM_MEDIA_TYPE *)malloc( sizeof(AM_MEDIA_TYPE)*mt_count );
        CopyMediaType(mt, &mtr);
881
    }
882
    else if( !b_stream_type )
Gildas Bazin's avatar
Gildas Bazin committed
883
    {
884
        // Insert prefered audio media type
885
        AM_MEDIA_TYPE mtr;
Gildas Bazin's avatar
Gildas Bazin committed
886 887 888 889 890 891 892 893 894 895 896
        WAVEFORMATEX wf;

        mtr.majortype            = MEDIATYPE_Audio;
        mtr.subtype              = MEDIASUBTYPE_PCM;
        mtr.bFixedSizeSamples    = TRUE;
        mtr.bTemporalCompression = FALSE;
        mtr.lSampleSize          = 0;
        mtr.pUnk                 = NULL;
        mtr.formattype           = FORMAT_WaveFormatEx;
        mtr.cbFormat             = sizeof(wf);
        mtr.pbFormat             = (BYTE *)&wf;
897 898 899

        memset(&wf, 0, sizeof(wf));

Gildas Bazin's avatar
Gildas Bazin committed
900 901 902 903 904 905 906
        wf.wFormatTag = WAVE_FORMAT_PCM;
        wf.nChannels = 2;
        wf.nSamplesPerSec = 44100;
        wf.wBitsPerSample = 16;
        wf.nBlockAlign = wf.nSamplesPerSec * wf.wBitsPerSample / 8;
        wf.nAvgBytesPerSec = wf.nSamplesPerSec * wf.nBlockAlign;
        wf.cbSize = 0;
907

Gildas Bazin's avatar
Gildas Bazin committed
908 909 910
        mt_count = 1;
        mt = (AM_MEDIA_TYPE *)malloc( sizeof(AM_MEDIA_TYPE)*mt_count );
        CopyMediaType(mt, &mtr);
911
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
912

913 914
    if( media_count > 0 )
    {
915 916
        mt = (AM_MEDIA_TYPE *)realloc( mt, sizeof(AM_MEDIA_TYPE) *
                                       (mt_count + media_count) );
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947

        // Order and copy returned media types according to arbitrary
        // fourcc priority
        for( size_t c = 0; c < media_count; c++ )
        {
            int slot_priority =
                GetFourCCPriority(GetFourCCFromMediaType(media_types[c]));
            size_t slot_copy = c;
            for( size_t d = c+1; d < media_count; d++ )
            {
                int priority =
                    GetFourCCPriority(GetFourCCFromMediaType(media_types[d]));
                if( priority > slot_priority )
                {
                    slot_priority = priority;
                    slot_copy = d;
                }
            }
            if( slot_copy != c )
            {
                mt[c+mt_count] = media_types[slot_copy];
                media_types[slot_copy] = media_types[c];
            }
            else
            {
                mt[c+mt_count] = media_types[c];
            }
        }
        mt_count += media_count;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
948
    /* Create and add our capture filter */
949
    CaptureFilter *p_capture_filter =
Gildas Bazin's avatar
Gildas Bazin committed
950
        new CaptureFilter( p_this, p_sys, mt, mt_count );
Gildas Bazin's avatar
 
Gildas Bazin committed
951
    p_sys->p_graph->AddFilter( p_capture_filter, 0 );
Gildas Bazin's avatar
 
Gildas Bazin committed
952 953 954

    /* Add the device filter to the graph (seems necessary with VfW before
     * accessing pin attributes). */
Gildas Bazin's avatar
 
Gildas Bazin committed
955
    p_sys->p_graph->AddFilter( p_device_filter, 0 );
Gildas Bazin's avatar
 
Gildas Bazin committed
956

Gildas Bazin's avatar
 
Gildas Bazin committed
957
    /* Attempt to connect one of this device's capture output pins */
Gildas Bazin's avatar
Gildas Bazin committed
958 959
    msg_Dbg( p_this, "connecting filters" );
    if( ConnectFilters( p_this, p_sys, p_device_filter, p_capture_filter ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
960 961
    {
        /* Success */
Gildas Bazin's avatar
Gildas Bazin committed
962
        msg_Dbg( p_this, "filters connected successfully !" );
963

Gildas Bazin's avatar
 
Gildas Bazin committed
964
        dshow_stream_t dshow_stream;
965
        dshow_stream.b_pts = VLC_FALSE;
Gildas Bazin's avatar
Gildas Bazin committed
966
        dshow_stream.p_es = 0;
967 968 969
        dshow_stream.mt =
            p_capture_filter->CustomGetPin()->CustomGetMediaType();

970 971