dshow.cpp 58.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
 *****************************************************************************
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 42
static int ReadCompressed( access_t *, byte_t *, int );
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 PropertiesPage( vlc_object_t *, IBaseFilter *,
                            ICaptureGraphBuilder2 *, vlc_bool_t );
Gildas Bazin's avatar
 
Gildas Bazin committed
62

Gildas Bazin's avatar
 
Gildas Bazin committed
63
/*****************************************************************************
64
 * Module descriptor
Gildas Bazin's avatar
 
Gildas Bazin committed
65
 *****************************************************************************/
66 67 68 69 70
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") };

Gildas Bazin's avatar
 
Gildas Bazin committed
71 72
#define CACHING_TEXT N_("Caching value in ms")
#define CACHING_LONGTEXT N_( \
73
    "Allows you to modify the default caching value for DirectShow streams. " \
Rocky Bernstein's avatar
Rocky Bernstein committed
74
    "This value should be set in milliseconds units." )
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#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 " \
93 94
    "(eg. I420 (default), RV24, etc.)")
#define CONFIG_TEXT N_("Device properties")
Gildas Bazin's avatar
 
Gildas Bazin committed
95
#define CONFIG_LONGTEXT N_( \
96 97
    "Show the properties dialog of the selected device before starting the " \
    "stream.")
Gildas Bazin's avatar
 
Gildas Bazin committed
98

Gildas Bazin's avatar
Gildas Bazin committed
99 100 101
static int  CommonOpen ( vlc_object_t *, access_sys_t *, vlc_bool_t );
static void CommonClose( vlc_object_t *, access_sys_t * );

102 103 104 105 106 107
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
108
vlc_module_begin();
109
    set_shortname( _("DirectShow") );
Gildas Bazin's avatar
 
Gildas Bazin committed
110
    set_description( _("DirectShow input") );
111
    add_integer( "dshow-caching", (mtime_t)(0.2*CLOCK_FREQ) / 1000, NULL,
Gildas Bazin's avatar
 
Gildas Bazin committed
112
                 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
113

114
    add_string( "dshow-vdev", NULL, NULL, VDEV_TEXT, VDEV_LONGTEXT, VLC_FALSE);
115
        change_string_list( ppsz_vdev, ppsz_vdev_text, FindDevicesCallback );
116 117
        change_action_add( FindDevicesCallback, N_("Refresh list") );
        change_action_add( ConfigDevicesCallback, N_("Configure") );
118

119
    add_string( "dshow-adev", NULL, NULL, ADEV_TEXT, ADEV_LONGTEXT, VLC_FALSE);
120
        change_string_list( ppsz_adev, ppsz_adev_text, FindDevicesCallback );
121 122
        change_action_add( FindDevicesCallback, N_("Refresh list") );
        change_action_add( ConfigDevicesCallback, N_("Configure") );
123

124
    add_string( "dshow-size", NULL, NULL, SIZE_TEXT, SIZE_LONGTEXT, VLC_FALSE);
125

126 127
    add_string( "dshow-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
                VLC_TRUE );
Gildas Bazin's avatar
 
Gildas Bazin committed
128 129

    add_bool( "dshow-config", VLC_FALSE, NULL, CONFIG_TEXT, CONFIG_LONGTEXT,
130
              VLC_FALSE );
Gildas Bazin's avatar
 
Gildas Bazin committed
131

Gildas Bazin's avatar
 
Gildas Bazin committed
132
    add_shortcut( "dshow" );
Gildas Bazin's avatar
Gildas Bazin committed
133 134
    set_capability( "access_demux", 0 );
    set_callbacks( DemuxOpen, DemuxClose );
Gildas Bazin's avatar
 
Gildas Bazin committed
135 136

    add_submodule();
Gildas Bazin's avatar
Gildas Bazin committed
137
    set_description( _("DirectShow input") );
Gildas Bazin's avatar
 
Gildas Bazin committed
138
    add_shortcut( "dshow" );
Gildas Bazin's avatar
Gildas Bazin committed
139 140
    set_capability( "access2", 0 );
    set_callbacks( AccessOpen, AccessClose );
Gildas Bazin's avatar
 
Gildas Bazin committed
141 142 143

vlc_module_end();

Gildas Bazin's avatar
Gildas Bazin committed
144
/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
145
 * DirectShow elementary stream descriptor
Gildas Bazin's avatar
Gildas Bazin committed
146
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160
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
161 162 163
    int             i_fourcc;
    es_out_id_t     *p_es;

164
    vlc_bool_t      b_pts;
Gildas Bazin's avatar
 
Gildas Bazin committed
165 166 167

} dshow_stream_t;

Gildas Bazin's avatar
Gildas Bazin committed
168
/*****************************************************************************
169
 * DirectShow utility functions
Gildas Bazin's avatar
Gildas Bazin committed
170
 *****************************************************************************/
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
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
225
/*****************************************************************************
Gildas Bazin's avatar
Gildas Bazin committed
226
 * CommonOpen: open direct show device
Gildas Bazin's avatar
 
Gildas Bazin committed
227
 *****************************************************************************/
Gildas Bazin's avatar
Gildas Bazin committed
228 229
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
230
{
231
    vlc_value_t  val;
Gildas Bazin's avatar
Gildas Bazin committed
232
    int i;
Gildas Bazin's avatar
 
Gildas Bazin committed
233

234
    /* Get/parse options and open device(s) */
Gildas Bazin's avatar
 
Gildas Bazin committed
235
    string vdevname, adevname;
236
    int i_width = 0, i_height = 0, i_chroma = 0;
Gildas Bazin's avatar
Gildas Bazin committed
237
    vlc_bool_t b_audio = VLC_TRUE;
Gildas Bazin's avatar
 
Gildas Bazin committed
238

Gildas Bazin's avatar
Gildas Bazin committed
239
    var_Create( p_this, "dshow-config", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
240

Gildas Bazin's avatar
Gildas Bazin committed
241 242
    var_Create( p_this, "dshow-vdev", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_this, "dshow-vdev", &val );
243 244
    if( val.psz_string ) vdevname = string( val.psz_string );
    if( val.psz_string ) free( val.psz_string );
Gildas Bazin's avatar
 
Gildas Bazin committed
245

Gildas Bazin's avatar
Gildas Bazin committed
246 247
    var_Create( p_this, "dshow-adev", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_this, "dshow-adev", &val );
248 249 250
    if( val.psz_string ) adevname = string( val.psz_string );
    if( val.psz_string ) free( val.psz_string );

Gildas Bazin's avatar
Gildas Bazin committed
251 252 253 254 255 256 257 258
    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 );
259
    if( val.psz_string && *val.psz_string )
Gildas Bazin's avatar
 
Gildas Bazin committed
260
    {
Gildas Bazin's avatar
Gildas Bazin committed
261
        for( i = 0; size_table[i].psz_size; i++ )
262
        {
Gildas Bazin's avatar
Gildas Bazin committed
263 264 265 266 267 268
            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;
            }
269
        }
Gildas Bazin's avatar
Gildas Bazin committed
270
        if( !size_table[i].psz_size ) /* Try to parse "WidthxHeight" */
271 272 273 274
        {
            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
275
            {
276
                i_height = strtol( psz_parser + 1, &psz_parser, 0 );
Gildas Bazin's avatar
 
Gildas Bazin committed
277
            }
Gildas Bazin's avatar
Gildas Bazin committed
278
            msg_Dbg( p_this, "Width x Height %dx%d", i_width, i_height );
Gildas Bazin's avatar
 
Gildas Bazin committed
279 280
        }
    }
281
    if( val.psz_string ) free( val.psz_string );
Gildas Bazin's avatar
 
Gildas Bazin committed
282

Gildas Bazin's avatar
Gildas Bazin committed
283 284
    var_Create( p_this, "dshow-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_this, "dshow-chroma", &val );
285 286 287 288 289 290
    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
291

Gildas Bazin's avatar
Gildas Bazin committed
292
    var_Create( p_this, "dshow-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
Gildas Bazin's avatar
 
Gildas Bazin committed
293 294

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

Gildas Bazin's avatar
 
Gildas Bazin committed
297 298 299
    /* Initialize some data */
    p_sys->i_streams = 0;
    p_sys->pp_streams = (dshow_stream_t **)malloc( 1 );
Gildas Bazin's avatar
 
Gildas Bazin committed
300 301 302
    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
303

304 305 306
    p_sys->p_graph = NULL;
    p_sys->p_capture_graph_builder2 = NULL;
    p_sys->p_control = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
307

308 309
    /* Build directshow graph */
    CreateDirectShowGraph( p_sys );
Gildas Bazin's avatar
 
Gildas Bazin committed
310

Gildas Bazin's avatar
Gildas Bazin committed
311
    if( OpenDevice( p_this, p_sys, vdevname, 0 ) != VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
312
    {
Gildas Bazin's avatar
Gildas Bazin committed
313
        msg_Err( p_this, "can't open video");
Gildas Bazin's avatar
 
Gildas Bazin committed
314
    }
Gildas Bazin's avatar
Gildas Bazin committed
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    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;
            }
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
333

Gildas Bazin's avatar
Gildas Bazin committed
334 335 336 337
        if( p_stream->mt.majortype == MEDIATYPE_Stream ) b_audio = VLC_FALSE;
    }

    if( b_audio && OpenDevice( p_this, p_sys, adevname, 1 ) != VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
338
    {
Gildas Bazin's avatar
Gildas Bazin committed
339
        msg_Err( p_this, "can't open audio");
Gildas Bazin's avatar
 
Gildas Bazin committed
340
    }
Gildas Bazin's avatar
Gildas Bazin committed
341 342

    for( i = 0; i < p_sys->i_crossbar_route_depth; i++ )
Gildas Bazin's avatar
 
Gildas Bazin committed
343
    {
344 345 346 347 348
        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
349

350 351
        if( SUCCEEDED(pXbar->Route(VideoOutputIndex, VideoInputIndex)) )
        {
Gildas Bazin's avatar
Gildas Bazin committed
352
            msg_Dbg( p_this, "Crossbar at depth %d, Routed video "
353
                     "ouput %ld to video input %ld", i, VideoOutputIndex,
354
                     VideoInputIndex );
Gildas Bazin's avatar
 
Gildas Bazin committed
355

356 357 358 359 360
            if( AudioOutputIndex != -1 && AudioInputIndex != -1 )
            {
                if( SUCCEEDED( pXbar->Route(AudioOutputIndex,
                                            AudioInputIndex)) )
                {
Gildas Bazin's avatar
Gildas Bazin committed
361
                    msg_Dbg(p_this, "Crossbar at depth %d, Routed audio "
362
                            "ouput %ld to audio input %ld", i,
363 364 365 366 367 368
                            AudioOutputIndex, AudioInputIndex );
                }
            }
        }
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
369 370
    /* Initialize some data */
    p_sys->i_current_stream = 0;
Gildas Bazin's avatar
Gildas Bazin committed
371 372
    vlc_mutex_init( p_this, &p_sys->lock );
    vlc_cond_init( p_this, &p_sys->wait );
373

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

Gildas Bazin's avatar
 
Gildas Bazin committed
376 377 378 379
    return VLC_SUCCESS;
}

/*****************************************************************************
Gildas Bazin's avatar
Gildas Bazin committed
380
 * DemuxOpen: open direct show device as an access_demux module
Gildas Bazin's avatar
 
Gildas Bazin committed
381
 *****************************************************************************/
Gildas Bazin's avatar
Gildas Bazin committed
382
static int DemuxOpen( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
383
{
Gildas Bazin's avatar
Gildas Bazin committed
384 385 386
    demux_t      *p_demux = (demux_t *)p_this;
    access_sys_t *p_sys;
    int i;
Gildas Bazin's avatar
 
Gildas Bazin committed
387

Gildas Bazin's avatar
Gildas Bazin committed
388 389 390
    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
391

Gildas Bazin's avatar
Gildas Bazin committed
392 393 394 395 396
    if( CommonOpen( p_this, p_sys, VLC_TRUE ) != VLC_SUCCESS )
    {
        CommonClose( p_this, p_sys );
        return VLC_EGENERIC;
    }
397

Gildas Bazin's avatar
Gildas Bazin committed
398 399 400
    /* Everything is ready. Let's rock baby */
    msg_Dbg( p_this, "Playing...");
    p_sys->p_control->Run();
401

Gildas Bazin's avatar
Gildas Bazin committed
402 403 404 405 406 407 408
    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++ )
409
    {
Gildas Bazin's avatar
Gildas Bazin committed
410 411
        dshow_stream_t *p_stream = p_sys->pp_streams[i];
        es_format_t fmt;
412

Gildas Bazin's avatar
Gildas Bazin committed
413 414 415
        if( p_stream->mt.majortype == MEDIATYPE_Video )
        {
            es_format_Init( &fmt, VIDEO_ES, p_stream->i_fourcc );
416

Gildas Bazin's avatar
Gildas Bazin committed
417 418 419
            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;
420

Gildas Bazin's avatar
Gildas Bazin committed
421 422 423 424 425
            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);
            }
426

Gildas Bazin's avatar
Gildas Bazin committed
427 428 429 430 431 432 433 434 435 436 437 438
            /* Setup rgb mask for RGB formats */
            if( p_stream->i_fourcc == VLC_FOURCC('R','V','2','4') )
            {
                /* This is in BGR format */
              fmt.video.i_bmask = 0x00ff0000;
              fmt.video.i_gmask = 0x0000ff00;
              fmt.video.i_rmask = 0x000000ff;
            }
        }
        else if( p_stream->mt.majortype == MEDIATYPE_Audio )
        {
            es_format_Init( &fmt, AUDIO_ES, p_stream->i_fourcc );
439

Gildas Bazin's avatar
Gildas Bazin committed
440 441 442 443 444 445 446 447 448 449
            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 );
450
    }
Gildas Bazin's avatar
Gildas Bazin committed
451

452
    return VLC_SUCCESS;
453 454
}

Gildas Bazin's avatar
Gildas Bazin committed
455 456 457 458
/*****************************************************************************
 * AccessOpen: open direct show device as an access module
 *****************************************************************************/
static int AccessOpen( vlc_object_t *p_this )
459
{
Gildas Bazin's avatar
Gildas Bazin committed
460 461
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;
462

Gildas Bazin's avatar
Gildas Bazin committed
463 464
    p_access->p_sys = p_sys = (access_sys_t *)malloc( sizeof( access_sys_t ) );
    memset( p_sys, 0, sizeof( access_sys_t ) );
465

Gildas Bazin's avatar
Gildas Bazin committed
466 467 468 469 470
    if( CommonOpen( p_this, p_sys, VLC_TRUE ) != VLC_SUCCESS )
    {
        CommonClose( p_this, p_sys );
        return VLC_EGENERIC;
    }
471

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

Gildas Bazin's avatar
Gildas Bazin committed
474 475
    /* Check if we need to force demuxers */
    if( !p_access->psz_demux || !*p_access->psz_demux )
Gildas Bazin's avatar
 
Gildas Bazin committed
476
    {
Gildas Bazin's avatar
Gildas Bazin committed
477 478 479
        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') )
480
        {
Gildas Bazin's avatar
Gildas Bazin committed
481 482 483 484 485
            p_access->psz_demux = strdup( "rawdv" );
        }
        else if( p_stream->i_fourcc == VLC_FOURCC('m','p','2','v') )
        {
            p_access->psz_demux = "mpgv";
486
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
487
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
488

Gildas Bazin's avatar
Gildas Bazin committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
    /* Setup Access */
    p_access->pf_read = ReadCompressed;
    p_access->pf_block = NULL;
    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;
507
}
Gildas Bazin's avatar
 
Gildas Bazin committed
508

Gildas Bazin's avatar
Gildas Bazin committed
509 510 511 512
/*****************************************************************************
 * CommonClose: close device
 *****************************************************************************/
static void CommonClose( vlc_object_t *p_this, access_sys_t *p_sys )
513
{
Gildas Bazin's avatar
Gildas Bazin committed
514
    msg_Dbg( p_this, "Releasing DirectShow");
515

Gildas Bazin's avatar
Gildas Bazin committed
516
    DeleteDirectShowGraph( p_sys );
517

Gildas Bazin's avatar
Gildas Bazin committed
518 519
    /* Uninitialize OLE/COM */
    CoUninitialize();
520

Gildas Bazin's avatar
Gildas Bazin committed
521 522
    /* Remove filters from graph */
    for( int i = 0; i < p_sys->i_streams; i++ )
523
    {
Gildas Bazin's avatar
Gildas Bazin committed
524
        delete p_sys->pp_streams[i];
525
    }
Gildas Bazin's avatar
Gildas Bazin committed
526 527 528 529
    free( p_sys->pp_streams );

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

Gildas Bazin's avatar
Gildas Bazin committed
531
    free( p_sys );
532 533
}

Gildas Bazin's avatar
Gildas Bazin committed
534 535 536 537
/*****************************************************************************
 * AccessClose: close device
 *****************************************************************************/
static void AccessClose( vlc_object_t *p_this )
538
{
Gildas Bazin's avatar
Gildas Bazin committed
539 540
    access_t     *p_access = (access_t *)p_this;
    access_sys_t *p_sys    = p_access->p_sys;
541

Gildas Bazin's avatar
Gildas Bazin committed
542 543
    /* Stop capturing stuff */
    p_sys->p_control->Stop();
544

Gildas Bazin's avatar
Gildas Bazin committed
545 546
    CommonClose( p_this, p_sys );
}
547

Gildas Bazin's avatar
Gildas Bazin committed
548 549 550 551 552 553 554
/*****************************************************************************
 * 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;
555

Gildas Bazin's avatar
Gildas Bazin committed
556 557
    /* Stop capturing stuff */
    p_sys->p_control->Stop();
558

Gildas Bazin's avatar
Gildas Bazin committed
559
    CommonClose( p_this, p_sys );
Gildas Bazin's avatar
 
Gildas Bazin committed
560 561 562 563 564
}

/****************************************************************************
 * ConnectFilters
 ****************************************************************************/
Gildas Bazin's avatar
Gildas Bazin committed
565 566
static bool ConnectFilters( vlc_object_t *p_this, access_sys_t *p_sys,
                            IBaseFilter *p_filter,
567
                            CaptureFilter *p_capture_filter )
Gildas Bazin's avatar
 
Gildas Bazin committed
568
{
569
    CapturePin *p_input_pin = p_capture_filter->CustomGetPin();
Gildas Bazin's avatar
 
Gildas Bazin committed
570

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

573
    if( p_sys->p_capture_graph_builder2 )
Gildas Bazin's avatar
 
Gildas Bazin committed
574
    {
575
        if( FAILED(p_sys->p_capture_graph_builder2->
Gildas Bazin's avatar
Gildas Bazin committed
576 577
                RenderStream( &PIN_CATEGORY_CAPTURE, &mediaType.majortype,
                              p_filter, 0, (IBaseFilter *)p_capture_filter )) )
578 579 580
        {
            return false;
        }
581

582 583 584 585 586
        // Sort out all the possible video inputs
        // The class needs to be given the capture filters ANALOGVIDEO input pin
        IEnumPins *pins = 0;
        if( mediaType.majortype == MEDIATYPE_Video &&
            SUCCEEDED(p_filter->EnumPins(&pins)) )
Gildas Bazin's avatar
 
Gildas Bazin committed
587
        {
588 589 590 591 592 593 594 595
            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
596
            while( !Found && ( S_OK == pins->Next(1, &pP, &n) ) )
597
            {
Gildas Bazin's avatar
Gildas Bazin committed
598
                if( S_OK == pP->QueryPinInfo(&pinInfo) )
599
                {
Gildas Bazin's avatar
Gildas Bazin committed
600 601 602 603
                    // is this pin an ANALOGVIDEOIN input pin?
                    if( pinInfo.dir == PINDIR_INPUT &&
                        pP->QueryInterface( IID_IKsPropertySet,
                                            (void **)&pKs ) == S_OK )
604
                    {
Gildas Bazin's avatar
Gildas Bazin committed
605 606 607
                        if( pKs->Get( AMPROPSETID_Pin,
                                      AMPROPERTY_PIN_CATEGORY, NULL, 0,
                                      &guid, sizeof(GUID), &dw ) == S_OK )
608
                        {
Gildas Bazin's avatar
Gildas Bazin committed
609
                            if( guid == PIN_CATEGORY_ANALOGVIDEOIN )
610
                            {
Gildas Bazin's avatar
Gildas Bazin committed
611 612 613 614 615
                                // recursively search crossbar routes
                                FindCrossbarRoutes( p_this, p_sys, pP,
                                                    PhysConn_Video_Tuner );
                                // found it
                                Found = TRUE;
616 617
                            }
                        }
Gildas Bazin's avatar
Gildas Bazin committed
618
                        pKs->Release();
619 620 621 622 623 624
                    }
                    pinInfo.pFilter->Release();
                }
                pP->Release();
            }
            pins->Release();
Gildas Bazin's avatar
 
Gildas Bazin committed
625
        }
626
        return true;
Gildas Bazin's avatar
 
Gildas Bazin committed
627
    }
628 629 630 631
    else
    {
        IEnumPins *p_enumpins;
        IPin *p_pin;
Gildas Bazin's avatar
 
Gildas Bazin committed
632

633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
        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
654 655
}

656
/*
Gildas Bazin's avatar
Gildas Bazin committed
657 658 659
 * get fourcc priority from arbritary preference, the higher the better
 */
static int GetFourCCPriority( int i_fourcc )
660 661 662
{
    switch( i_fourcc )
    {
Gildas Bazin's avatar
Gildas Bazin committed
663 664 665 666 667 668 669 670 671 672 673 674
    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;
675
    }
Gildas Bazin's avatar
Gildas Bazin committed
676

677 678 679 680 681
    return 0;
}

#define MAX_MEDIA_TYPES 32

Gildas Bazin's avatar
Gildas Bazin committed
682 683
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
684
{
685 686 687
    /* See if device is already opened */
    for( int i = 0; i < p_sys->i_streams; i++ )
    {
Gildas Bazin's avatar
Gildas Bazin committed
688 689
        if( devicename.size() &&
            p_sys->pp_streams[i]->devicename == devicename )
690 691 692 693 694 695
        {
            /* Already opened */
            return VLC_SUCCESS;
        }
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
696 697
    list<string> list_devices;

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

701 702 703
    if( !list_devices.size() )
        return VLC_EGENERIC;

Gildas Bazin's avatar
 
Gildas Bazin committed
704 705
    list<string>::iterator iter;
    for( iter = list_devices.begin(); iter != list_devices.end(); iter++ )
Gildas Bazin's avatar
Gildas Bazin committed
706
        msg_Dbg( p_this, "found device: %s", iter->c_str() );
Gildas Bazin's avatar
 
Gildas Bazin committed
707 708 709 710 711 712 713 714

    /* 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
715 716
    // a capture/preview device, such as a desktop USB video camera.
    IBaseFilter *p_device_filter =
Gildas Bazin's avatar
Gildas Bazin committed
717
        FindCaptureDevice( p_this, &devicename, 0, b_audio );
Gildas Bazin's avatar
 
Gildas Bazin committed
718
    if( p_device_filter )
Gildas Bazin's avatar
Gildas Bazin committed
719
        msg_Dbg( p_this, "using device: %s", devicename.c_str() );
Gildas Bazin's avatar
 
Gildas Bazin committed
720 721
    else
    {
Gildas Bazin's avatar
Gildas Bazin committed
722
        msg_Err( p_this, "can't use device: %s, unsupported device type",
723
                 devicename.c_str() );
Gildas Bazin's avatar
 
Gildas Bazin committed
724 725
        return VLC_EGENERIC;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
726

727
    AM_MEDIA_TYPE *mt;
728 729
    AM_MEDIA_TYPE media_types[MAX_MEDIA_TYPES];

Gildas Bazin's avatar
Gildas Bazin committed
730
    size_t mt_count = EnumDeviceCaps( p_this, p_device_filter, p_sys->i_chroma,
731 732
                                      p_sys->i_width, p_sys->i_height,
                                      0, 0, 0, media_types, MAX_MEDIA_TYPES );
733

734
    if( mt_count > 0 )
735
    {
Gildas Bazin's avatar
Gildas Bazin committed
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
        mt = (AM_MEDIA_TYPE *)malloc( sizeof(AM_MEDIA_TYPE)*mt_count );

        // Order and copy returned media types according to arbitrary
        // fourcc priority
        for( size_t c = 0; c < mt_count; c++ )
        {
            int slot_priority =
                GetFourCCPriority(GetFourCCFromMediaType(media_types[c]));
            size_t slot_copy = c;
            for( size_t d = c+1; d < mt_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] = media_types[slot_copy];
                media_types[slot_copy] = media_types[c];
            }
            else
            {
                mt[c] = media_types[c];
            }
        }
765
    }
Gildas Bazin's avatar
Gildas Bazin committed
766 767 768
    else if( !b_audio )
    {
        // Use default video media type
769
        AM_MEDIA_TYPE mtr;
Gildas Bazin's avatar
Gildas Bazin committed
770 771 772 773 774 775 776 777 778 779 780
        VIDEOINFOHEADER vh;

        mtr.majortype            = MEDIATYPE_Video;
        mtr.subtype              = MEDIASUBTYPE_I420;
        mtr.bFixedSizeSamples    = TRUE;
        mtr.bTemporalCompression = FALSE;
        mtr.lSampleSize          = 0;
        mtr.pUnk                 = NULL;
        mtr.formattype           = FORMAT_VideoInfo;
        mtr.cbFormat             = sizeof(vh);
        mtr.pbFormat             = (BYTE *)&vh;
781 782 783

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

Gildas Bazin's avatar
Gildas Bazin committed
784 785 786 787 788 789 790
        vh.bmiHeader.biSize   = sizeof(vh.bmiHeader);
        vh.bmiHeader.biWidth  = p_sys->i_width > 0 ? p_sys->i_width: 320;
        vh.bmiHeader.biHeight = p_sys->i_height > 0 ? p_sys->i_height : 240;
        vh.bmiHeader.biPlanes      = 1;
        vh.bmiHeader.biBitCount    = 12;
        vh.bmiHeader.biCompression = VLC_FOURCC('I','4','2','0');
        vh.bmiHeader.biSizeImage   = p_sys->i_width * 12 * p_sys->i_height / 8;
791

Gildas Bazin's avatar
Gildas Bazin committed
792 793
        msg_Warn( p_this, "device %s using built-in video media type",
                  devicename.c_str() );
794

Gildas Bazin's avatar
Gildas Bazin committed
795 796 797
        mt_count = 1;
        mt = (AM_MEDIA_TYPE *)malloc( sizeof(AM_MEDIA_TYPE)*mt_count );
        CopyMediaType(mt, &mtr);
798
    }
Gildas Bazin's avatar
Gildas Bazin committed
799 800 801
    else
    {
        // Use default audio media type
802
        AM_MEDIA_TYPE mtr;
Gildas Bazin's avatar
Gildas Bazin committed
803 804 805 806 807 808 809 810 811 812 813
        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;
814 815 816

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

Gildas Bazin's avatar
Gildas Bazin committed
817 818 819 820 821 822 823
        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;
824

Gildas Bazin's avatar
Gildas Bazin committed
825 826
        msg_Warn( p_this, "device %s using built-in audio media type",
                  devicename.c_str() );
827

Gildas Bazin's avatar
Gildas Bazin committed
828 829 830
        mt_count = 1;
        mt = (AM_MEDIA_TYPE *)malloc( sizeof(AM_MEDIA_TYPE)*mt_count );
        CopyMediaType(mt, &mtr);
831
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
832

Gildas Bazin's avatar
 
Gildas Bazin committed
833
    /* Create and add our capture filter */
834
    CaptureFilter *p_capture_filter =
Gildas Bazin's avatar
Gildas Bazin committed
835
        new CaptureFilter( p_this, p_sys, mt, mt_count );
Gildas Bazin's avatar
 
Gildas Bazin committed
836
    p_sys->p_graph->AddFilter( p_capture_filter, 0 );
Gildas Bazin's avatar
 
Gildas Bazin committed
837 838 839

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

Gildas Bazin's avatar
 
Gildas Bazin committed
842
    /* Attempt to connect one of this device's capture output pins */
Gildas Bazin's avatar
Gildas Bazin committed
843 844
    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
845 846
    {
        /* Success */
Gildas Bazin's avatar
Gildas Bazin committed
847
        msg_Dbg( p_this, "filters connected successfully !" );
848

Gildas Bazin's avatar
 
Gildas Bazin committed
849
        dshow_stream_t dshow_stream;
850
        dshow_stream.b_pts = VLC_FALSE;
Gildas Bazin's avatar
Gildas Bazin committed
851
        dshow_stream.p_es = 0;
852 853 854 855 856 857
        dshow_stream.mt =
            p_capture_filter->CustomGetPin()->CustomGetMediaType();

        /* Show properties. Done here so the VLC stream is setup with the
         * proper parameters. */
        vlc_value_t val;
Gildas Bazin's avatar
Gildas Bazin committed
858
        var_Get( p_this, "dshow-config", &val );
859 860
        if( val.i_int )
        {
Gildas Bazin's avatar
Gildas Bazin committed
861
            PropertiesPage( p_this, p_device_filter,
862
                            p_sys->p_capture_graph_builder2,
863
                            dshow_stream.mt.majortype == MEDIATYPE_Audio );
864 865
        }

Gildas Bazin's avatar
 
Gildas Bazin committed
866 867
        dshow_stream.mt =
            p_capture_filter->CustomGetPin()->CustomGetMediaType();
Gildas Bazin's avatar
 
Gildas Bazin committed
868

Gildas Bazin's avatar
Gildas Bazin committed
869 870
        dshow_stream.i_fourcc = GetFourCCFromMediaType( dshow_stream.mt );
        if( dshow_stream.i_fourcc )
871 872 873 874 875
        {
            if( dshow_stream.mt.majortype == MEDIATYPE_Video )
            {
                dshow_stream.header.video =
                    *(VIDEOINFOHEADER *)dshow_stream.mt.pbFormat;
Gildas Bazin's avatar
Gildas Bazin committed
876 877 878
                msg_Dbg( p_this, "MEDIATYPE_Video" );
                msg_Dbg( p_this, "selected video pin accepts format: %4.4s",
                         (char *)&dshow_stream.i_fourcc);
879 880 881 882 883
            }
            else if( dshow_stream.mt.majortype == MEDIATYPE_Audio )
            {
                dshow_stream.header.audio =
                    *(WAVEFORMATEX *)dshow_stream.mt.pbFormat;
Gildas Bazin's avatar
Gildas Bazin committed
884 885 886
                msg_Dbg( p_this, "MEDIATYPE_Audio" );
                msg_Dbg( p_this, "selected audio pin accepts format: %4.4s",
                         (char *)&dshow_stream.i_fourcc);
887 888 889
            }
            else if( dshow_stream.mt.majortype == MEDIATYPE_Stream )
            {
Gildas Bazin's avatar
Gildas Bazin committed
890 891
                msg_Dbg( p_this, "MEDIATYPE_Stream" );
                msg_Dbg( p_this, "selected stream pin accepts format: %4.4s",
892 893 894 895
                         (char *)&dshow_stream.i_fourcc);
            }
            else
            {
Gildas Bazin's avatar
Gildas Bazin committed
896
                msg_Dbg( p_this, "unknown stream majortype" );
897 898
                goto fail;
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
899

900 901 902
            /* Add directshow elementary stream to our list */
            dshow_stream.p_device_filter = p_device_filter;
            dshow_stream.p_capture_filter = p_capture_filter;
Gildas Bazin's avatar
 
Gildas Bazin committed
903

904 905 906 907
            p_sys->pp_streams = (dshow_stream_t **)realloc( p_sys->pp_streams,
                sizeof(dshow_stream_t *) * (p_sys->i_streams + 1) );
            p_sys->pp_streams[p_sys->i_streams] = new dshow_stream_t;
            *p_sys->pp_streams[p_sys->i_streams++] = dshow_stream;
Gildas Bazin's avatar
 
Gildas Bazin committed
908

909 910
            return VLC_SUCCESS;
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
911 912
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
913
 fail:
Gildas Bazin's avatar
 
Gildas Bazin committed
914
    /* Remove filters from graph */
Gildas Bazin's avatar
 
Gildas Bazin committed
915 916
    p_sys->p_graph->RemoveFilter( p_device_filter );
    p_sys->p_graph->RemoveFilter( p_capture_filter );
Gildas Bazin's avatar
 
Gildas Bazin committed
917 918

    /* Release objects */
Gildas Bazin's avatar
 
Gildas Bazin committed
919 920
    p_device_filter->Release();
    p_capture_filter->Release();
Gildas Bazin's avatar
 
Gildas Bazin committed
921 922 923 924 925 926

    return VLC_EGENERIC;
}

static IBaseFilter *
FindCaptureDevice( vlc_object_t *p_this, string *p_devicename,
Gildas Bazin's avatar
 
Gildas Bazin committed
927
                   list<string> *p_listdevices, vlc_bool_t b_audio )
Gildas Bazin's avatar
 
Gildas Bazin committed
928
{
Gildas Bazin's avatar
 
Gildas Bazin committed
929 930 931
    IBaseFilter *p_base_filter = NULL;
    IMoniker *p_moniker = NULL;
    ULONG i_fetched;
Gildas Bazin's avatar
 
Gildas Bazin committed
932 933 934
    HRESULT hr;

    /* Create the system device enumerator */
Gildas Bazin's avatar
 
Gildas Bazin committed
935
    ICreateDevEnum *p_dev_enum = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
936 937

    hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
Gildas Bazin's avatar
 
Gildas Bazin committed
938
                           IID_ICreateDevEnum, (void **)&p_dev_enum );
Gildas Bazin's avatar
 
Gildas Bazin committed
939 940
    if( FAILED(hr) )
    {
941
        msg_Err( p_this, "failed to create the device enumerator (0x%lx)", hr);
Gildas Bazin's avatar
 
Gildas Bazin committed
942 943 944 945
        return NULL;
    }

    /* Create an enumerator for the video capture devices */
Gildas Bazin's avatar
 
Gildas Bazin committed
946 947 948 949 950 951 952 953
    IEnumMoniker *p_class_enum = NULL;
    if( !b_audio )
        hr = p_dev_enum->CreateClassEnumerator( CLSID_VideoInputDeviceCategory,
                                                &p_class_enum, 0 );
    else
        hr = p_dev_enum->CreateClassEnumerator( CLSID_AudioInputDeviceCategory,
                                                &p_class_enum, 0 );
    p_dev_enum->Release();
Gildas Bazin's avatar
 
Gildas Bazin committed
954 955
    if( FAILED(hr) )
    {
956
        msg_Err( p_this, "failed to create the class enumerator (0x%lx)", hr );
Gildas Bazin's avatar
 
Gildas Bazin committed
957 958 959
        return NULL;
    }

Sam Hocevar's avatar
Sam Hocevar committed
960
    /* If there are no enumerators for the requested type, then
Gildas Bazin's avatar
 
Gildas Bazin committed
961 962
     * CreateClassEnumerator will succeed, but p_class_enum will be NULL */
    if( p_class_enum == NULL )
Gildas Bazin's avatar
 
Gildas Bazin committed
963
    {
964
        msg_Err( p_this, "no capture device was detected" );
Gildas Bazin's avatar
 
Gildas Bazin committed