dshow.cpp 74.6 KB
Newer Older
Gildas Bazin's avatar
 
Gildas Bazin committed
1
/*****************************************************************************
2
 * dshow.cpp : DirectShow access module for vlc
Gildas Bazin's avatar
 
Gildas Bazin committed
3
 *****************************************************************************
4
 * Copyright (C) 2002, 2003 the VideoLAN team
5
 * $Id$
Gildas Bazin's avatar
 
Gildas Bazin committed
6
 *
7
 * Author: Gildas Bazin <gbazin@videolan.org>
8
 *         Damien Fouilleul <damienf@videolan.org>
Gildas Bazin's avatar
 
Gildas Bazin committed
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Gildas Bazin's avatar
 
Gildas Bazin committed
23 24 25 26 27 28 29 30 31 32 33 34 35
 *****************************************************************************/

/*****************************************************************************
 * 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
36
#include "common.h"
Gildas Bazin's avatar
 
Gildas Bazin committed
37 38
#include "filter.h"

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

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

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

Gildas Bazin's avatar
 
Gildas Bazin committed
69
/*****************************************************************************
70
 * Module descriptor
Gildas Bazin's avatar
 
Gildas Bazin committed
71
 *****************************************************************************/
72 73 74 75
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") };
76 77 78
static int  pi_tuner_input[] = { 0, 1, 2 };
static char *ppsz_tuner_input_text[] =
    {N_("Default"), N_("Cable"), N_("Antenna")};
79

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

Gildas Bazin's avatar
Gildas Bazin committed
141 142 143
static int  CommonOpen ( vlc_object_t *, access_sys_t *, vlc_bool_t );
static void CommonClose( vlc_object_t *, access_sys_t * );

144 145 146 147 148 149
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
150
vlc_module_begin();
151
    set_shortname( _("DirectShow") );
Gildas Bazin's avatar
 
Gildas Bazin committed
152
    set_description( _("DirectShow input") );
Clément Stenac's avatar
Clément Stenac committed
153 154
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_ACCESS );
155
    add_integer( "dshow-caching", (mtime_t)(0.2*CLOCK_FREQ) / 1000, NULL,
Gildas Bazin's avatar
 
Gildas Bazin committed
156
                 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
157

158
    add_string( "dshow-vdev", NULL, NULL, VDEV_TEXT, VDEV_LONGTEXT, VLC_FALSE);
159
        change_string_list( ppsz_vdev, ppsz_vdev_text, FindDevicesCallback );
160 161
        change_action_add( FindDevicesCallback, N_("Refresh list") );
        change_action_add( ConfigDevicesCallback, N_("Configure") );
162

163
    add_string( "dshow-adev", NULL, NULL, ADEV_TEXT, ADEV_LONGTEXT, VLC_FALSE);
164
        change_string_list( ppsz_adev, ppsz_adev_text, FindDevicesCallback );
165 166
        change_action_add( FindDevicesCallback, N_("Refresh list") );
        change_action_add( ConfigDevicesCallback, N_("Configure") );
167

168
    add_string( "dshow-size", NULL, NULL, SIZE_TEXT, SIZE_LONGTEXT, VLC_FALSE);
169

170 171
    add_string( "dshow-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
                VLC_TRUE );
Gildas Bazin's avatar
 
Gildas Bazin committed
172

173 174 175
    add_float( "dshow-fps", 0.0f, NULL, FPS_TEXT, FPS_LONGTEXT,
                VLC_TRUE );

Gildas Bazin's avatar
 
Gildas Bazin committed
176
    add_bool( "dshow-config", VLC_FALSE, NULL, CONFIG_TEXT, CONFIG_LONGTEXT,
177
              VLC_TRUE );
Gildas Bazin's avatar
 
Gildas Bazin committed
178

179
    add_bool( "dshow-tuner", VLC_FALSE, NULL, TUNER_TEXT, TUNER_LONGTEXT,
180
              VLC_TRUE );
181

182 183 184 185 186 187 188 189 190 191
    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 );

192
    add_integer( "dshow-video-input",  -1, NULL, VIDEO_IN_TEXT,
193
                 VIDEO_IN_LONGTEXT, VLC_TRUE );
194 195

    add_integer( "dshow-audio-input",  -1, NULL, AUDIO_IN_TEXT,
196
                 AUDIO_IN_LONGTEXT, VLC_TRUE );
197 198

    add_integer( "dshow-video-output", -1, NULL, VIDEO_OUT_TEXT,
199
                 VIDEO_OUT_LONGTEXT, VLC_TRUE );
200 201

    add_integer( "dshow-audio-output", -1, NULL, AUDIO_OUT_TEXT,
202
                 AUDIO_OUT_LONGTEXT, VLC_TRUE );
203

Gildas Bazin's avatar
 
Gildas Bazin committed
204
    add_shortcut( "dshow" );
Gildas Bazin's avatar
Gildas Bazin committed
205 206
    set_capability( "access_demux", 0 );
    set_callbacks( DemuxOpen, DemuxClose );
Gildas Bazin's avatar
 
Gildas Bazin committed
207 208

    add_submodule();
Gildas Bazin's avatar
Gildas Bazin committed
209
    set_description( _("DirectShow input") );
Gildas Bazin's avatar
 
Gildas Bazin committed
210
    add_shortcut( "dshow" );
Gildas Bazin's avatar
Gildas Bazin committed
211 212
    set_capability( "access2", 0 );
    set_callbacks( AccessOpen, AccessClose );
Gildas Bazin's avatar
 
Gildas Bazin committed
213 214 215

vlc_module_end();

Gildas Bazin's avatar
Gildas Bazin committed
216
/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
217
 * DirectShow elementary stream descriptor
Gildas Bazin's avatar
Gildas Bazin committed
218
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232
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
233 234 235
    int             i_fourcc;
    es_out_id_t     *p_es;

236
    vlc_bool_t      b_pts;
Gildas Bazin's avatar
 
Gildas Bazin committed
237 238
} dshow_stream_t;

Gildas Bazin's avatar
Gildas Bazin committed
239
/*****************************************************************************
240
 * DirectShow utility functions
Gildas Bazin's avatar
Gildas Bazin committed
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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
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
296
/*****************************************************************************
Gildas Bazin's avatar
Gildas Bazin committed
297
 * CommonOpen: open direct show device
Gildas Bazin's avatar
 
Gildas Bazin committed
298
 *****************************************************************************/
Gildas Bazin's avatar
Gildas Bazin committed
299 300
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
301
{
302
    vlc_value_t  val;
Gildas Bazin's avatar
Gildas Bazin committed
303
    int i;
Gildas Bazin's avatar
 
Gildas Bazin committed
304

305
    /* Get/parse options and open device(s) */
Gildas Bazin's avatar
 
Gildas Bazin committed
306
    string vdevname, adevname;
307
    int i_width = 0, i_height = 0, i_chroma = 0;
Gildas Bazin's avatar
Gildas Bazin committed
308
    vlc_bool_t b_audio = VLC_TRUE;
Gildas Bazin's avatar
 
Gildas Bazin committed
309

310 311
    var_Create( p_this, "dshow-config", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Create( p_this, "dshow-tuner", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
Gildas Bazin's avatar
Gildas Bazin committed
312 313
    var_Create( p_this, "dshow-vdev", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_this, "dshow-vdev", &val );
314 315
    if( val.psz_string ) vdevname = string( val.psz_string );
    if( val.psz_string ) free( val.psz_string );
Gildas Bazin's avatar
 
Gildas Bazin committed
316

Gildas Bazin's avatar
Gildas Bazin committed
317 318
    var_Create( p_this, "dshow-adev", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_this, "dshow-adev", &val );
319 320 321
    if( val.psz_string ) adevname = string( val.psz_string );
    if( val.psz_string ) free( val.psz_string );

Gildas Bazin's avatar
Gildas Bazin committed
322 323
    static struct {char *psz_size; int  i_width; int  i_height;} size_table[] =
    { { "subqcif", 128, 96 }, { "qsif", 160, 120 }, { "qcif", 176, 144 },
324
      { "sif", 320, 240 }, { "cif", 352, 288 }, { "d1", 640, 480 },
Gildas Bazin's avatar
Gildas Bazin committed
325 326 327 328 329
      { 0, 0, 0 },
    };

    var_Create( p_this, "dshow-size", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_this, "dshow-size", &val );
330
    if( val.psz_string && *val.psz_string )
Gildas Bazin's avatar
 
Gildas Bazin committed
331
    {
Gildas Bazin's avatar
Gildas Bazin committed
332
        for( i = 0; size_table[i].psz_size; i++ )
333
        {
Gildas Bazin's avatar
Gildas Bazin committed
334 335 336 337 338 339
            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;
            }
340
        }
Gildas Bazin's avatar
Gildas Bazin committed
341
        if( !size_table[i].psz_size ) /* Try to parse "WidthxHeight" */
342 343 344 345
        {
            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
346
            {
347
                i_height = strtol( psz_parser + 1, &psz_parser, 0 );
Gildas Bazin's avatar
 
Gildas Bazin committed
348
            }
Clément Stenac's avatar
Clément Stenac committed
349
            msg_Dbg( p_this, "width x height %dx%d", i_width, i_height );
Gildas Bazin's avatar
 
Gildas Bazin committed
350 351
        }
    }
352
    if( val.psz_string ) free( val.psz_string );
Gildas Bazin's avatar
 
Gildas Bazin committed
353

354
    p_sys->b_chroma = VLC_FALSE;
Gildas Bazin's avatar
Gildas Bazin committed
355 356
    var_Create( p_this, "dshow-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_this, "dshow-chroma", &val );
357 358 359 360
    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] );
361
        p_sys->b_chroma = VLC_TRUE;
362 363
    }
    if( val.psz_string ) free( val.psz_string );
Gildas Bazin's avatar
 
Gildas Bazin committed
364

365
    var_Create( p_this, "dshow-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
366 367 368 369 370 371 372
    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
373
    var_Create( p_this, "dshow-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
Gildas Bazin's avatar
 
Gildas Bazin committed
374

375 376 377 378 379
    var_Create( p_this, "dshow-video-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Create( p_this, "dshow-audio-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Create( p_this, "dshow-video-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Create( p_this, "dshow-audio-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );

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

Gildas Bazin's avatar
 
Gildas Bazin committed
383 384
    /* Initialize some data */
    p_sys->i_streams = 0;
385
    p_sys->pp_streams = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
386 387 388
    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
389

390 391 392
    p_sys->p_graph = NULL;
    p_sys->p_capture_graph_builder2 = NULL;
    p_sys->p_control = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
393

394 395 396
    vlc_mutex_init( p_this, &p_sys->lock );
    vlc_cond_init( p_this, &p_sys->wait );

397 398
    /* Build directshow graph */
    CreateDirectShowGraph( p_sys );
Gildas Bazin's avatar
 
Gildas Bazin committed
399

Gildas Bazin's avatar
Gildas Bazin committed
400
    if( OpenDevice( p_this, p_sys, vdevname, 0 ) != VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
401
    {
Gildas Bazin's avatar
Gildas Bazin committed
402
        msg_Err( p_this, "can't open video");
Gildas Bazin's avatar
 
Gildas Bazin committed
403
    }
Gildas Bazin's avatar
Gildas Bazin committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    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;
420 421 422 423

                if( b_access_demux )
                {
                    /* Let the access (only) take care of that */
424
                    return VLC_EGENERIC;
425
                }
Gildas Bazin's avatar
Gildas Bazin committed
426 427
            }
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
428

429 430 431 432 433 434 435 436 437
        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;
            }
438 439 440 441 442 443 444 445 446

            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 );
            }
447
        }
Gildas Bazin's avatar
Gildas Bazin committed
448 449 450
    }

    if( b_audio && OpenDevice( p_this, p_sys, adevname, 1 ) != VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
451
    {
Gildas Bazin's avatar
Gildas Bazin committed
452
        msg_Err( p_this, "can't open audio");
Gildas Bazin's avatar
 
Gildas Bazin committed
453
    }
Gildas Bazin's avatar
Gildas Bazin committed
454

455
    for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
Gildas Bazin's avatar
 
Gildas Bazin committed
456
    {
457 458 459 460 461 462 463 464 465 466 467 468 469
            var_Get( p_this, "dshow-video-input", &val );
            if( val.i_int > 0 )
                    p_sys->crossbar_routes[i].VideoInputIndex=val.i_int;
            var_Get( p_this, "dshow-video-output", &val );
            if( val.i_int > 0 )
                    p_sys->crossbar_routes[i].VideoOutputIndex=val.i_int;
            var_Get( p_this, "dshow-audio-input", &val );
            if( val.i_int > 0 )
                    p_sys->crossbar_routes[i].AudioInputIndex=val.i_int;
            var_Get( p_this, "dshow-audio-output", &val );
            if( val.i_int > 0 )
                    p_sys->crossbar_routes[i].AudioOutputIndex=val.i_int;

470 471 472 473 474
        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
475

476 477
        if( SUCCEEDED(pXbar->Route(VideoOutputIndex, VideoInputIndex)) )
        {
Clément Stenac's avatar
Clément Stenac committed
478 479
            msg_Dbg( p_this, "crossbar at depth %d, routed video "
                     "output %ld to video input %ld", i, VideoOutputIndex,
480
                     VideoInputIndex );
Gildas Bazin's avatar
 
Gildas Bazin committed
481

482 483 484 485 486
            if( AudioOutputIndex != -1 && AudioInputIndex != -1 )
            {
                if( SUCCEEDED( pXbar->Route(AudioOutputIndex,
                                            AudioInputIndex)) )
                {
Clément Stenac's avatar
Clément Stenac committed
487 488
                    msg_Dbg(p_this, "crossbar at depth %d, routed audio "
                            "output %ld to audio input %ld", i,
489 490 491 492 493 494
                            AudioOutputIndex, AudioInputIndex );
                }
            }
        }
    }

495 496 497 498
    /*
    ** Show properties pages from other filters in graph
    */
    var_Get( p_this, "dshow-config", &val );
499
    if( val.b_bool )
500 501 502 503 504
    {
        for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
        {
            IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
            IBaseFilter *p_XF;
505 506 507

            if( SUCCEEDED( pXbar->QueryInterface( IID_IBaseFilter,
                                                  (void **)&p_XF ) ) )
508 509 510 511
            {
                ShowPropertyPage( p_XF );
                p_XF->Release();
            }
512
        }
513 514
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
515 516
    /* Initialize some data */
    p_sys->i_current_stream = 0;
517

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

Gildas Bazin's avatar
 
Gildas Bazin committed
520 521 522 523
    return VLC_SUCCESS;
}

/*****************************************************************************
Gildas Bazin's avatar
Gildas Bazin committed
524
 * DemuxOpen: open direct show device as an access_demux module
Gildas Bazin's avatar
 
Gildas Bazin committed
525
 *****************************************************************************/
Gildas Bazin's avatar
Gildas Bazin committed
526
static int DemuxOpen( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
527
{
Gildas Bazin's avatar
Gildas Bazin committed
528 529 530
    demux_t      *p_demux = (demux_t *)p_this;
    access_sys_t *p_sys;
    int i;
Gildas Bazin's avatar
 
Gildas Bazin committed
531

Gildas Bazin's avatar
Gildas Bazin committed
532 533 534
    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
535

Gildas Bazin's avatar
Gildas Bazin committed
536 537 538 539 540
    if( CommonOpen( p_this, p_sys, VLC_TRUE ) != VLC_SUCCESS )
    {
        CommonClose( p_this, p_sys );
        return VLC_EGENERIC;
    }
541

Gildas Bazin's avatar
Gildas Bazin committed
542 543 544
    /* Everything is ready. Let's rock baby */
    msg_Dbg( p_this, "Playing...");
    p_sys->p_control->Run();
545

Gildas Bazin's avatar
Gildas Bazin committed
546 547 548 549 550 551 552
    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++ )
553
    {
Gildas Bazin's avatar
Gildas Bazin committed
554 555
        dshow_stream_t *p_stream = p_sys->pp_streams[i];
        es_format_t fmt;
556

Gildas Bazin's avatar
Gildas Bazin committed
557 558 559
        if( p_stream->mt.majortype == MEDIATYPE_Video )
        {
            es_format_Init( &fmt, VIDEO_ES, p_stream->i_fourcc );
560

Gildas Bazin's avatar
Gildas Bazin committed
561 562 563
            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;
564

Gildas Bazin's avatar
Gildas Bazin committed
565 566 567 568 569
            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);
            }
570

Gildas Bazin's avatar
Gildas Bazin committed
571 572 573 574
            /* Setup rgb mask for RGB formats */
            if( p_stream->i_fourcc == VLC_FOURCC('R','V','2','4') )
            {
                /* This is in BGR format */
575 576 577
                fmt.video.i_bmask = 0x00ff0000;
                fmt.video.i_gmask = 0x0000ff00;
                fmt.video.i_rmask = 0x000000ff;
Gildas Bazin's avatar
Gildas Bazin committed
578
            }
579 580 581 582 583 584 585

            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
586 587 588 589
        }
        else if( p_stream->mt.majortype == MEDIATYPE_Audio )
        {
            es_format_Init( &fmt, AUDIO_ES, p_stream->i_fourcc );
590

Gildas Bazin's avatar
Gildas Bazin committed
591 592 593 594 595 596 597 598 599 600
            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 );
601
    }
Gildas Bazin's avatar
Gildas Bazin committed
602

603
    return VLC_SUCCESS;
604 605
}

Gildas Bazin's avatar
Gildas Bazin committed
606 607 608 609
/*****************************************************************************
 * AccessOpen: open direct show device as an access module
 *****************************************************************************/
static int AccessOpen( vlc_object_t *p_this )
610
{
Gildas Bazin's avatar
Gildas Bazin committed
611 612
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;
613

Gildas Bazin's avatar
Gildas Bazin committed
614 615
    p_access->p_sys = p_sys = (access_sys_t *)malloc( sizeof( access_sys_t ) );
    memset( p_sys, 0, sizeof( access_sys_t ) );
616

617
    if( CommonOpen( p_this, p_sys, VLC_FALSE ) != VLC_SUCCESS )
Gildas Bazin's avatar
Gildas Bazin committed
618 619 620 621
    {
        CommonClose( p_this, p_sys );
        return VLC_EGENERIC;
    }
622

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

Gildas Bazin's avatar
Gildas Bazin committed
625 626
    /* Check if we need to force demuxers */
    if( !p_access->psz_demux || !*p_access->psz_demux )
Gildas Bazin's avatar
 
Gildas Bazin committed
627
    {
Gildas Bazin's avatar
Gildas Bazin committed
628 629 630
        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') )
631
        {
Gildas Bazin's avatar
Gildas Bazin committed
632 633 634 635 636
            p_access->psz_demux = strdup( "rawdv" );
        }
        else if( p_stream->i_fourcc == VLC_FOURCC('m','p','2','v') )
        {
            p_access->psz_demux = "mpgv";
637
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
638
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
639

Gildas Bazin's avatar
Gildas Bazin committed
640
    /* Setup Access */
641 642
    p_access->pf_read = NULL;
    p_access->pf_block = ReadCompressed;
Gildas Bazin's avatar
Gildas Bazin committed
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
    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;
658
}
Gildas Bazin's avatar
 
Gildas Bazin committed
659

Gildas Bazin's avatar
Gildas Bazin committed
660 661 662 663
/*****************************************************************************
 * CommonClose: close device
 *****************************************************************************/
static void CommonClose( vlc_object_t *p_this, access_sys_t *p_sys )
664
{
Clément Stenac's avatar
Clément Stenac committed
665
    msg_Dbg( p_this, "releasing DirectShow");
666

Gildas Bazin's avatar
Gildas Bazin committed
667
    DeleteDirectShowGraph( p_sys );
668

Gildas Bazin's avatar
Gildas Bazin committed
669 670
    /* Uninitialize OLE/COM */
    CoUninitialize();
671

672 673
    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
674 675 676

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

Gildas Bazin's avatar
Gildas Bazin committed
678
    free( p_sys );
679 680
}

Gildas Bazin's avatar
Gildas Bazin committed
681 682 683 684
/*****************************************************************************
 * AccessClose: close device
 *****************************************************************************/
static void AccessClose( vlc_object_t *p_this )
685
{
Gildas Bazin's avatar
Gildas Bazin committed
686 687
    access_t     *p_access = (access_t *)p_this;
    access_sys_t *p_sys    = p_access->p_sys;
688

Gildas Bazin's avatar
Gildas Bazin committed
689 690
    /* Stop capturing stuff */
    p_sys->p_control->Stop();
691

Gildas Bazin's avatar
Gildas Bazin committed
692 693
    CommonClose( p_this, p_sys );
}
694

Gildas Bazin's avatar
Gildas Bazin committed
695 696 697 698 699 700 701
/*****************************************************************************
 * 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;
702

Gildas Bazin's avatar
Gildas Bazin committed
703 704
    /* Stop capturing stuff */
    p_sys->p_control->Stop();
705

Gildas Bazin's avatar
Gildas Bazin committed
706
    CommonClose( p_this, p_sys );
Gildas Bazin's avatar
 
Gildas Bazin committed
707 708 709 710 711
}

/****************************************************************************
 * ConnectFilters
 ****************************************************************************/
Gildas Bazin's avatar
Gildas Bazin committed
712 713
static bool ConnectFilters( vlc_object_t *p_this, access_sys_t *p_sys,
                            IBaseFilter *p_filter,
714
                            CaptureFilter *p_capture_filter )
Gildas Bazin's avatar
 
Gildas Bazin committed
715
{
716
    CapturePin *p_input_pin = p_capture_filter->CustomGetPin();
Gildas Bazin's avatar
 
Gildas Bazin committed
717

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

720
    if( p_sys->p_capture_graph_builder2 )
Gildas Bazin's avatar
 
Gildas Bazin committed
721
    {
722
        if( FAILED(p_sys->p_capture_graph_builder2->
Gildas Bazin's avatar
Gildas Bazin committed
723 724
                RenderStream( &PIN_CATEGORY_CAPTURE, &mediaType.majortype,
                              p_filter, 0, (IBaseFilter *)p_capture_filter )) )
725 726 727
        {
            return false;
        }
728

729 730 731
        // Sort out all the possible video inputs
        // The class needs to be given the capture filters ANALOGVIDEO input pin
        IEnumPins *pins = 0;
732 733
        if( ( mediaType.majortype == MEDIATYPE_Video ||
              mediaType.majortype == MEDIATYPE_Stream ) &&
734
            SUCCEEDED(p_filter->EnumPins(&pins)) )
Gildas Bazin's avatar
 
Gildas Bazin committed
735
        {
736 737 738 739 740 741 742 743
            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
744
            while( !Found && ( S_OK == pins->Next(1, &pP, &n) ) )
745
            {
Gildas Bazin's avatar
Gildas Bazin committed
746
                if( S_OK == pP->QueryPinInfo(&pinInfo) )
747
                {
Gildas Bazin's avatar
Gildas Bazin committed
748 749 750 751
                    // is this pin an ANALOGVIDEOIN input pin?
                    if( pinInfo.dir == PINDIR_INPUT &&
                        pP->QueryInterface( IID_IKsPropertySet,
                                            (void **)&pKs ) == S_OK )
752
                    {
Gildas Bazin's avatar
Gildas Bazin committed
753 754 755
                        if( pKs->Get( AMPROPSETID_Pin,
                                      AMPROPERTY_PIN_CATEGORY, NULL, 0,
                                      &guid, sizeof(GUID), &dw ) == S_OK )
756
                        {
Gildas Bazin's avatar
Gildas Bazin committed
757
                            if( guid == PIN_CATEGORY_ANALOGVIDEOIN )
758
                            {
Gildas Bazin's avatar
Gildas Bazin committed
759
                                // recursively search crossbar routes
760
                                FindCrossbarRoutes( p_this, p_sys, pP, 0 );
Gildas Bazin's avatar
Gildas Bazin committed
761 762
                                // found it
                                Found = TRUE;
763 764
                            }
                        }
Gildas Bazin's avatar
Gildas Bazin committed
765
                        pKs->Release();
766 767 768 769 770 771
                    }
                    pinInfo.pFilter->Release();
                }
                pP->Release();
            }
            pins->Release();
Gildas Bazin's avatar
 
Gildas Bazin committed
772
        }
773
        return true;
Gildas Bazin's avatar
 
Gildas Bazin committed
774
    }
775 776 777 778
    else
    {
        IEnumPins *p_enumpins;
        IPin *p_pin;
Gildas Bazin's avatar
 
Gildas Bazin committed
779

780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
        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
801 802
}

803
/*
Gildas Bazin's avatar
Gildas Bazin committed
804 805 806
 * get fourcc priority from arbritary preference, the higher the better
 */
static int GetFourCCPriority( int i_fourcc )
807 808 809
{
    switch( i_fourcc )
    {
Gildas Bazin's avatar
Gildas Bazin committed
810 811 812 813 814 815 816 817 818 819 820 821
    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;
822
    }
Gildas Bazin's avatar
Gildas Bazin committed
823

824 825 826 827 828
    return 0;
}

#define MAX_MEDIA_TYPES 32

Gildas Bazin's avatar
Gildas Bazin committed
829 830
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
831
{
832 833 834
    /* See if device is already opened */
    for( int i = 0; i < p_sys->i_streams; i++ )
    {
Gildas Bazin's avatar
Gildas Bazin committed
835 836
        if( devicename.size() &&
            p_sys->pp_streams[i]->devicename == devicename )
837 838 839 840 841 842
        {
            /* Already opened */
            return VLC_SUCCESS;
        }
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
843 844
    list<string> list_devices;

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

848 849 850
    if( !list_devices.size() )
        return VLC_EGENERIC;

Gildas Bazin's avatar
 
Gildas Bazin committed
851 852
    list<string>::iterator iter;
    for( iter = list_devices.begin(); iter != list_devices.end(); iter++ )
Gildas Bazin's avatar
Gildas Bazin committed
853
        msg_Dbg( p_this, "found device: %s", iter->c_str() );
Gildas Bazin's avatar
 
Gildas Bazin committed
854 855 856 857 858 859 860 861

    /* 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
862 863
    // a capture/preview device, such as a desktop USB video camera.
    IBaseFilter *p_device_filter =
Gildas Bazin's avatar
Gildas Bazin committed
864
        FindCaptureDevice( p_this, &devicename, 0, b_audio );
Gildas Bazin's avatar
 
Gildas Bazin committed
865
    if( p_device_filter )
Gildas Bazin's avatar
Gildas Bazin committed
866
        msg_Dbg( p_this, "using device: %s", devicename.c_str() );
Gildas Bazin's avatar
 
Gildas Bazin committed
867 868
    else
    {
Gildas Bazin's avatar
Gildas Bazin committed
869
        msg_Err( p_this, "can't use device: %s, unsupported device type",
870
                 devicename.c_str() );
Gildas Bazin's avatar
 
Gildas Bazin committed
871 872
        return VLC_EGENERIC;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
873

874 875 876 877 878 879 880
    // 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 );

881
    AM_MEDIA_TYPE *mt = NULL;
Gildas Bazin's avatar
Gildas Bazin committed
882

883 884
    if( media_count > 0 )
    {
885
        mt = (AM_MEDIA_TYPE *)CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * media_count);
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905

        // 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 )
            {
906
                mt[c] = media_types[slot_copy];
907 908 909 910
                media_types[slot_copy] = media_types[c];
            }
            else
            {
911
                mt[c] = media_types[c];
912 913
            }
        }