add_savefile: remove callback parameter
[vlc/asuraparaju-public.git] / modules / access / dshow / dshow.cpp
blob82b291690f0341317ff8c68072f66c2f59d18102
1 /*****************************************************************************
2 * dshow.cpp : DirectShow access and access_demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2004, 2006, 2008, 2010 the VideoLAN team
5 * $Id$
7 * Author: Gildas Bazin <gbazin@videolan.org>
8 * Damien Fouilleul <damienf@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #define __STDC_CONSTANT_MACROS 1
34 #define __STDC_FORMAT_MACROS 1
35 #define CFG_PREFIX "dshow-"
36 #include <inttypes.h>
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
40 #include <vlc_access.h>
41 #include <vlc_demux.h>
43 #include <vlc_dialog.h> /* dialog_Fatal */
44 #include <vlc_charset.h> /* FromWide */
46 #include "common.h"
47 #include "filter.h"
49 /*****************************************************************************
50 * Access: local prototypes
51 *****************************************************************************/
52 static block_t *ReadCompressed( access_t * );
53 static int AccessControl ( access_t *, int, va_list );
55 static int Demux ( demux_t * );
56 static int DemuxControl( demux_t *, int, va_list );
58 static int OpenDevice( vlc_object_t *, access_sys_t *, string, bool );
59 static IBaseFilter *FindCaptureDevice( vlc_object_t *, string *,
60 list<string> *, bool );
61 static size_t EnumDeviceCaps( vlc_object_t *, IBaseFilter *,
62 int, int, int, int, int, int,
63 AM_MEDIA_TYPE *mt, size_t );
64 static bool ConnectFilters( vlc_object_t *, access_sys_t *,
65 IBaseFilter *, CaptureFilter * );
66 static int FindDevicesCallback( vlc_object_t *, char const *,
67 vlc_value_t, vlc_value_t, void * );
68 static int ConfigDevicesCallback( vlc_object_t *, char const *,
69 vlc_value_t, vlc_value_t, void * );
71 static void ShowPropertyPage( IUnknown * );
72 static void ShowDeviceProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
73 IBaseFilter *, bool );
74 static void ShowTunerProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
75 IBaseFilter *, bool );
76 static void ConfigTuner( vlc_object_t *, ICaptureGraphBuilder2 *,
77 IBaseFilter * );
79 /*****************************************************************************
80 * Module descriptor
81 *****************************************************************************/
82 static const char *const ppsz_vdev[] = { "", "none" };
83 static const char *const ppsz_vdev_text[] = { N_("Default"), N_("None") };
85 static const char *const ppsz_adev[] = { "", "none" };
86 static const char *const ppsz_adev_text[] = { N_("Default"), N_("None") };
88 static const int pi_tuner_input[] = { 0, 1, 2 };
89 static const char *const ppsz_tuner_input_text[] =
90 {N_("Default"), N_("Cable"), N_("Antenna")};
92 static const int pi_amtuner_mode[] = { AMTUNER_MODE_DEFAULT,
93 AMTUNER_MODE_TV,
94 AMTUNER_MODE_FM_RADIO,
95 AMTUNER_MODE_AM_RADIO,
96 AMTUNER_MODE_DSS };
97 static const char *const ppsz_amtuner_mode_text[] = { N_("Default"),
98 N_("TV"),
99 N_("FM radio"),
100 N_("AM radio"),
101 N_("DSS") };
103 static const int i_standards_list[] =
105 KS_AnalogVideo_None,
106 KS_AnalogVideo_NTSC_M, KS_AnalogVideo_NTSC_M_J, KS_AnalogVideo_NTSC_433,
107 KS_AnalogVideo_PAL_B, KS_AnalogVideo_PAL_D, KS_AnalogVideo_PAL_G,
108 KS_AnalogVideo_PAL_H, KS_AnalogVideo_PAL_I, KS_AnalogVideo_PAL_M,
109 KS_AnalogVideo_PAL_N, KS_AnalogVideo_PAL_60,
110 KS_AnalogVideo_SECAM_B, KS_AnalogVideo_SECAM_D, KS_AnalogVideo_SECAM_G,
111 KS_AnalogVideo_SECAM_H, KS_AnalogVideo_SECAM_K, KS_AnalogVideo_SECAM_K1,
112 KS_AnalogVideo_SECAM_L, KS_AnalogVideo_SECAM_L1,
113 KS_AnalogVideo_PAL_N_COMBO
115 static const char *const ppsz_standards_list_text[] =
117 N_("Default"),
118 "NTSC_M", "NTSC_M_J", "NTSC_443",
119 "PAL_B", "PAL_D", "PAL_G",
120 "PAL_H", "PAL_I", "PAL_M",
121 "PAL_N", "PAL_60",
122 "SECAM_B", "SECAM_D", "SECAM_G",
123 "SECAM_H", "SECAM_K", "SECAM_K1",
124 "SECAM_L", "SECAM_L1",
125 "PAL_N_COMBO"
128 #define CACHING_TEXT N_("Caching value in ms")
129 #define CACHING_LONGTEXT N_( \
130 "Caching value for DirectShow streams. " \
131 "This value should be set in milliseconds." )
132 #define VDEV_TEXT N_("Video device name")
133 #define VDEV_LONGTEXT N_( \
134 "Name of the video device that will be used by the " \
135 "DirectShow plugin. If you don't specify anything, the default device " \
136 "will be used.")
137 #define ADEV_TEXT N_("Audio device name")
138 #define ADEV_LONGTEXT N_( \
139 "Name of the audio device that will be used by the " \
140 "DirectShow plugin. If you don't specify anything, the default device " \
141 "will be used. ")
142 #define SIZE_TEXT N_("Video size")
143 #define SIZE_LONGTEXT N_( \
144 "Size of the video that will be displayed by the " \
145 "DirectShow plugin. If you don't specify anything the default size for " \
146 "your device will be used. You can specify a standard size (cif, d1, ...) or <width>x<height>.")
147 #define ASPECT_TEXT N_("Picture aspect-ratio n:m")
148 #define ASPECT_LONGTEXT N_("Define input picture aspect-ratio to use. Default is 4:3" )
149 #define CHROMA_TEXT N_("Video input chroma format")
150 #define CHROMA_LONGTEXT N_( \
151 "Force the DirectShow video input to use a specific chroma format " \
152 "(eg. I420 (default), RV24, etc.)")
153 #define FPS_TEXT N_("Video input frame rate")
154 #define FPS_LONGTEXT N_( \
155 "Force the DirectShow video input to use a specific frame rate" \
156 "(eg. 0 means default, 25, 29.97, 50, 59.94, etc.)")
157 #define CONFIG_TEXT N_("Device properties")
158 #define CONFIG_LONGTEXT N_( \
159 "Show the properties dialog of the selected device before starting the " \
160 "stream.")
161 #define TUNER_TEXT N_("Tuner properties")
162 #define TUNER_LONGTEXT N_( \
163 "Show the tuner properties [channel selection] page." )
164 #define CHANNEL_TEXT N_("Tuner TV Channel")
165 #define CHANNEL_LONGTEXT N_( \
166 "Set the TV channel the tuner will set to " \
167 "(0 means default)." )
168 #define TVFREQ_TEXT N_("Tuner Frequency")
169 #define TVFREQ_LONGTEXT N_( "This overrides the channel. Measured in Hz." )
170 #define STANDARD_TEXT N_( "Standard" )
171 #define STANDARD_LONGTEXT N_( "Video standard (Default, SECAM_D, PAL_B, NTSC_M, etc...)." )
172 #define COUNTRY_TEXT N_("Tuner country code")
173 #define COUNTRY_LONGTEXT N_( \
174 "Set the tuner country code that establishes the current " \
175 "channel-to-frequency mapping (0 means default)." )
176 #define TUNER_INPUT_TEXT N_("Tuner input type")
177 #define TUNER_INPUT_LONGTEXT N_( \
178 "Select the tuner input type (Cable/Antenna)." )
179 #define VIDEO_IN_TEXT N_("Video input pin")
180 #define VIDEO_IN_LONGTEXT N_( \
181 "Select the video input source, such as composite, s-video, " \
182 "or tuner. Since these settings are hardware-specific, you should find good " \
183 "settings in the \"Device config\" area, and use those numbers here. -1 " \
184 "means that settings will not be changed.")
185 #define AUDIO_IN_TEXT N_("Audio input pin")
186 #define AUDIO_IN_LONGTEXT N_( \
187 "Select the audio input source. See the \"video input\" option." )
188 #define VIDEO_OUT_TEXT N_("Video output pin")
189 #define VIDEO_OUT_LONGTEXT N_( \
190 "Select the video output type. See the \"video input\" option." )
191 #define AUDIO_OUT_TEXT N_("Audio output pin")
192 #define AUDIO_OUT_LONGTEXT N_( \
193 "Select the audio output type. See the \"video input\" option." )
195 #define AMTUNER_MODE_TEXT N_("AM Tuner mode")
196 #define AMTUNER_MODE_LONGTEXT N_( \
197 "AM Tuner mode. Can be one of Default (0), TV (1)," \
198 "AM Radio (2), FM Radio (3) or DSS (4).")
200 #define AUDIO_CHANNELS_TEXT N_("Number of audio channels")
201 #define AUDIO_CHANNELS_LONGTEXT N_( \
202 "Select audio input format with the given number of audio channels (if non 0)" )
204 #define AUDIO_SAMPLERATE_TEXT N_("Audio sample rate")
205 #define AUDIO_SAMPLERATE_LONGTEXT N_( \
206 "Select audio input format with the given sample rate (if non 0)" )
208 #define AUDIO_BITSPERSAMPLE_TEXT N_("Audio bits per sample")
209 #define AUDIO_BITSPERSAMPLE_LONGTEXT N_( \
210 "Select audio input format with the given bits/sample (if non 0)" )
212 static int CommonOpen ( vlc_object_t *, access_sys_t *, bool );
213 static void CommonClose( vlc_object_t *, access_sys_t * );
215 static int AccessOpen ( vlc_object_t * );
216 static void AccessClose( vlc_object_t * );
218 static int DemuxOpen ( vlc_object_t * );
219 static void DemuxClose ( vlc_object_t * );
221 vlc_module_begin ()
222 set_shortname( N_("DirectShow") )
223 set_description( N_("DirectShow input") )
224 set_category( CAT_INPUT )
225 set_subcategory( SUBCAT_INPUT_ACCESS )
226 add_integer( CFG_PREFIX "caching", (mtime_t)(0.2*CLOCK_FREQ) / 1000, NULL,
227 CACHING_TEXT, CACHING_LONGTEXT, true )
229 add_string( CFG_PREFIX "vdev", NULL, VDEV_TEXT, VDEV_LONGTEXT, false)
230 change_string_list( ppsz_vdev, ppsz_vdev_text, FindDevicesCallback )
231 change_action_add( FindDevicesCallback, N_("Refresh list") )
232 change_action_add( ConfigDevicesCallback, N_("Configure") )
234 add_string( CFG_PREFIX "adev", NULL, ADEV_TEXT, ADEV_LONGTEXT, false)
235 change_string_list( ppsz_adev, ppsz_adev_text, FindDevicesCallback )
236 change_action_add( FindDevicesCallback, N_("Refresh list") )
237 change_action_add( ConfigDevicesCallback, N_("Configure") )
239 add_string( CFG_PREFIX "size", NULL, SIZE_TEXT, SIZE_LONGTEXT, false)
241 add_string( CFG_PREFIX "aspect-ratio", "4:3", ASPECT_TEXT, ASPECT_LONGTEXT, false)
243 add_string( CFG_PREFIX "chroma", NULL, CHROMA_TEXT, CHROMA_LONGTEXT, true )
245 add_float( CFG_PREFIX "fps", 0.0f, NULL, FPS_TEXT, FPS_LONGTEXT, true )
247 add_bool( CFG_PREFIX "config", false, NULL, CONFIG_TEXT, CONFIG_LONGTEXT, true )
249 add_bool( CFG_PREFIX "tuner", false, NULL, TUNER_TEXT, TUNER_LONGTEXT, true )
251 add_integer( CFG_PREFIX "tuner-channel", 0, NULL, CHANNEL_TEXT, CHANNEL_LONGTEXT,
252 true )
254 add_integer( CFG_PREFIX "tuner-frequency", 0, NULL, TVFREQ_TEXT, TVFREQ_LONGTEXT,
255 true )
257 add_integer( CFG_PREFIX "tuner-country", 0, NULL, COUNTRY_TEXT, COUNTRY_LONGTEXT,
258 true )
260 add_integer( CFG_PREFIX "tuner-standard", 0, NULL, STANDARD_TEXT, STANDARD_LONGTEXT,
261 false )
262 change_integer_list( i_standards_list, ppsz_standards_list_text )
264 add_integer( CFG_PREFIX "tuner-input", 0, NULL, TUNER_INPUT_TEXT,
265 TUNER_INPUT_LONGTEXT, true )
266 change_integer_list( pi_tuner_input, ppsz_tuner_input_text )
268 add_integer( CFG_PREFIX "video-input", -1, NULL, VIDEO_IN_TEXT,
269 VIDEO_IN_LONGTEXT, true )
271 add_integer( CFG_PREFIX "video-output", -1, NULL, VIDEO_OUT_TEXT,
272 VIDEO_OUT_LONGTEXT, true )
274 add_integer( CFG_PREFIX "audio-input", -1, NULL, AUDIO_IN_TEXT,
275 AUDIO_IN_LONGTEXT, true )
277 add_integer( CFG_PREFIX "audio-output", -1, NULL, AUDIO_OUT_TEXT,
278 AUDIO_OUT_LONGTEXT, true )
280 add_integer( CFG_PREFIX "amtuner-mode", AMTUNER_MODE_TV, NULL,
281 AMTUNER_MODE_TEXT, AMTUNER_MODE_LONGTEXT, false)
282 change_integer_list( pi_amtuner_mode, ppsz_amtuner_mode_text )
284 add_integer( CFG_PREFIX "audio-channels", 0, NULL, AUDIO_CHANNELS_TEXT,
285 AUDIO_CHANNELS_LONGTEXT, true )
286 add_integer( CFG_PREFIX "audio-samplerate", 0, NULL, AUDIO_SAMPLERATE_TEXT,
287 AUDIO_SAMPLERATE_LONGTEXT, true )
288 add_integer( CFG_PREFIX "audio-bitspersample", 0, NULL, AUDIO_BITSPERSAMPLE_TEXT,
289 AUDIO_BITSPERSAMPLE_LONGTEXT, true )
291 add_shortcut( "dshow" )
292 set_capability( "access_demux", 0 )
293 set_callbacks( DemuxOpen, DemuxClose )
295 add_submodule ()
296 set_description( N_("DirectShow input") )
297 add_shortcut( "dshow" )
298 set_capability( "access", 0 )
299 set_callbacks( AccessOpen, AccessClose )
301 vlc_module_end ()
304 /*****************************************************************************
305 * DirectShow elementary stream descriptor
306 *****************************************************************************/
307 typedef struct dshow_stream_t
309 string devicename;
310 IBaseFilter *p_device_filter;
311 CaptureFilter *p_capture_filter;
312 AM_MEDIA_TYPE mt;
314 union
316 VIDEOINFOHEADER video;
317 WAVEFORMATEX audio;
319 } header;
321 int i_fourcc;
322 es_out_id_t *p_es;
324 bool b_pts;
326 deque<VLCMediaSample> samples_queue;
327 } dshow_stream_t;
329 /*****************************************************************************
330 * DirectShow utility functions
331 *****************************************************************************/
332 static void CreateDirectShowGraph( access_sys_t *p_sys )
334 p_sys->i_crossbar_route_depth = 0;
336 /* Create directshow filter graph */
337 if( SUCCEEDED( CoCreateInstance( CLSID_FilterGraph, 0, CLSCTX_INPROC,
338 (REFIID)IID_IFilterGraph, (void **)&p_sys->p_graph) ) )
340 /* Create directshow capture graph builder if available */
341 if( SUCCEEDED( CoCreateInstance( CLSID_CaptureGraphBuilder2, 0,
342 CLSCTX_INPROC, (REFIID)IID_ICaptureGraphBuilder2,
343 (void **)&p_sys->p_capture_graph_builder2 ) ) )
345 p_sys->p_capture_graph_builder2->
346 SetFiltergraph((IGraphBuilder *)p_sys->p_graph);
349 p_sys->p_graph->QueryInterface( IID_IMediaControl,
350 (void **)&p_sys->p_control );
354 static void DeleteDirectShowGraph( access_sys_t *p_sys )
356 DeleteCrossbarRoutes( p_sys );
358 /* Remove filters from graph */
359 for( int i = 0; i < p_sys->i_streams; i++ )
361 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_capture_filter );
362 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_device_filter );
363 p_sys->pp_streams[i]->p_capture_filter->Release();
364 p_sys->pp_streams[i]->p_device_filter->Release();
367 /* Release directshow objects */
368 if( p_sys->p_control )
370 p_sys->p_control->Release();
371 p_sys->p_control = NULL;
373 if( p_sys->p_capture_graph_builder2 )
375 p_sys->p_capture_graph_builder2->Release();
376 p_sys->p_capture_graph_builder2 = NULL;
379 if( p_sys->p_graph )
381 p_sys->p_graph->Release();
382 p_sys->p_graph = NULL;
386 /*****************************************************************************
387 * CommonOpen: open direct show device
388 *****************************************************************************/
389 static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
390 bool b_access_demux )
392 char *psz_val;
394 /* Get/parse options and open device(s) */
395 string vdevname, adevname;
396 int i_width = 0, i_height = 0;
397 vlc_fourcc_t i_chroma = 0;
398 bool b_use_audio = true;
399 bool b_use_video = true;
401 /* Initialize OLE/COM */
402 CoInitialize( 0 );
404 var_Create( p_this, CFG_PREFIX "config", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
405 var_Create( p_this, CFG_PREFIX "tuner", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
406 psz_val = var_CreateGetString( p_this, CFG_PREFIX "vdev" );
407 if( psz_val )
409 msg_Dbg( p_this, "dshow-vdev: %s", psz_val ) ;
410 /* skip none device */
411 if ( strncasecmp( psz_val, "none", 4 ) != 0 )
412 vdevname = string( psz_val );
413 else
414 b_use_video = false ;
416 free( psz_val );
418 psz_val = var_CreateGetString( p_this, CFG_PREFIX "adev" );
419 if( psz_val )
421 msg_Dbg( p_this, "dshow-adev: %s", psz_val ) ;
422 /* skip none device */
423 if ( strncasecmp( psz_val, "none", 4 ) != 0 )
424 adevname = string( psz_val );
425 else
426 b_use_audio = false ;
428 free( psz_val );
430 /* DShow Size */
431 static struct {
432 const char *psz_size;
433 int i_width;
434 int i_height;
435 } size_table[] =
436 { { "subqcif", 128, 96 },
437 { "qsif", 160, 120 },
438 { "qcif", 176, 144 },
439 { "sif", 320, 240 },
440 { "cif", 352, 288 },
441 { "d1", 640, 480 },
442 { 0, 0, 0 },
445 psz_val = var_CreateGetString( p_this, CFG_PREFIX "size" );
446 if( !EMPTY_STR(psz_val) )
448 int i;
449 for( i = 0; size_table[i].psz_size; i++ )
451 if( !strcmp( psz_val, size_table[i].psz_size ) )
453 i_width = size_table[i].i_width;
454 i_height = size_table[i].i_height;
455 break;
458 if( !size_table[i].psz_size ) /* Try to parse "WidthxHeight" */
460 char *psz_parser;
461 i_width = strtol( psz_val, &psz_parser, 0 );
462 if( *psz_parser == 'x' || *psz_parser == 'X')
464 i_height = strtol( psz_parser + 1, &psz_parser, 0 );
466 msg_Dbg( p_this, "width x height %dx%d", i_width, i_height );
469 free( psz_val );
471 /* Chroma */
472 psz_val = var_CreateGetString( p_this, CFG_PREFIX "chroma" );
473 i_chroma = vlc_fourcc_GetCodecFromString( UNKNOWN_ES, psz_val );
474 p_sys->b_chroma = i_chroma != 0;
475 free( psz_val );
477 var_Create( p_this, CFG_PREFIX "fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
478 var_Create( p_this, CFG_PREFIX "tuner-channel",
479 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
480 var_Create( p_this, CFG_PREFIX "tuner-frequency",
481 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
482 var_Create( p_this, CFG_PREFIX "tuner-standard",
483 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
484 var_Create( p_this, CFG_PREFIX "tuner-country",
485 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
486 var_Create( p_this, CFG_PREFIX "tuner-input",
487 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
489 var_Create( p_this, CFG_PREFIX "amtuner-mode",
490 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
492 var_Create( p_this, CFG_PREFIX "caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
494 var_Create( p_this, CFG_PREFIX "video-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
495 var_Create( p_this, CFG_PREFIX "audio-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
496 var_Create( p_this, CFG_PREFIX "video-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
497 var_Create( p_this, CFG_PREFIX "audio-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
500 /* Initialize some data */
501 p_sys->i_streams = 0;
502 p_sys->pp_streams = NULL;
503 p_sys->i_width = i_width;
504 p_sys->i_height = i_height;
505 p_sys->i_chroma = i_chroma;
507 p_sys->p_graph = NULL;
508 p_sys->p_capture_graph_builder2 = NULL;
509 p_sys->p_control = NULL;
511 /* Build directshow graph */
512 CreateDirectShowGraph( p_sys );
514 vlc_mutex_init( &p_sys->lock );
515 vlc_cond_init( &p_sys->wait );
517 if( !b_use_video && !b_use_audio )
519 dialog_Fatal( p_this, _("Capture failed"),
520 _("No video or audio device selected.") );
521 return VLC_EGENERIC ;
524 if( !b_use_video )
525 msg_Dbg( p_this, "skipping video device" ) ;
526 bool b_err_video = false ;
528 if( b_use_video && OpenDevice( p_this, p_sys, vdevname, 0 ) != VLC_SUCCESS )
530 msg_Err( p_this, "can't open video device");
531 b_err_video = true ;
534 if ( b_use_video && !b_err_video )
536 /* Check if we can handle the demuxing ourselves or need to spawn
537 * a demuxer module */
538 dshow_stream_t *p_stream = p_sys->pp_streams[p_sys->i_streams-1];
540 if( p_stream->mt.majortype == MEDIATYPE_Video )
542 if( /* Raw DV stream */
543 p_stream->i_fourcc == VLC_CODEC_DV ||
544 /* Raw MPEG video stream */
545 p_stream->i_fourcc == VLC_CODEC_MPGV )
547 b_use_audio = false;
549 if( b_access_demux )
551 /* Let the access (only) take care of that */
552 return VLC_EGENERIC;
557 if( p_stream->mt.majortype == MEDIATYPE_Stream )
559 b_use_audio = false;
561 if( b_access_demux )
563 /* Let the access (only) take care of that */
564 return VLC_EGENERIC;
567 if( var_GetBool( p_this, CFG_PREFIX "tuner" ) )
569 /* FIXME: we do MEDIATYPE_Stream here so we don't do
570 * it twice. */
571 ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2,
572 p_stream->p_device_filter, 0 );
577 if( !b_use_audio )
578 msg_Dbg( p_this, "skipping audio device") ;
580 bool b_err_audio = false ;
582 if( b_use_audio && OpenDevice( p_this, p_sys, adevname, 1 ) != VLC_SUCCESS )
584 msg_Err( p_this, "can't open audio device");
585 b_err_audio = true ;
588 if( ( b_use_video && b_err_video && b_use_audio && b_err_audio ) ||
589 ( !b_use_video && b_use_audio && b_err_audio ) ||
590 ( b_use_video && !b_use_audio && b_err_video ) )
592 msg_Err( p_this, "FATAL: could not open ANY device" ) ;
593 dialog_Fatal( p_this, _("Capture failed"),
594 _("VLC cannot open ANY capture device."
595 "Check the error log for details.") );
596 return VLC_EGENERIC ;
599 for( int i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
601 int i_val = var_GetInteger( p_this, CFG_PREFIX "video-input" );
602 if( i_val >= 0 )
603 p_sys->crossbar_routes[i].VideoInputIndex = i_val;
604 i_val = var_GetInteger( p_this, CFG_PREFIX "video-output" );
605 if( i_val >= 0 )
606 p_sys->crossbar_routes[i].VideoOutputIndex = i_val;
607 i_val = var_GetInteger( p_this, CFG_PREFIX "audio-input" );
608 if( i_val >= 0 )
609 p_sys->crossbar_routes[i].AudioInputIndex = i_val;
610 i_val = var_GetInteger( p_this, CFG_PREFIX "audio-output" );
611 if( i_val >= 0 )
612 p_sys->crossbar_routes[i].AudioOutputIndex = i_val;
614 IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
615 LONG VideoInputIndex = p_sys->crossbar_routes[i].VideoInputIndex;
616 LONG VideoOutputIndex = p_sys->crossbar_routes[i].VideoOutputIndex;
617 LONG AudioInputIndex = p_sys->crossbar_routes[i].AudioInputIndex;
618 LONG AudioOutputIndex = p_sys->crossbar_routes[i].AudioOutputIndex;
620 if( SUCCEEDED(pXbar->Route(VideoOutputIndex, VideoInputIndex)) )
622 msg_Dbg( p_this, "crossbar at depth %d, routed video "
623 "output %ld to video input %ld", i, VideoOutputIndex,
624 VideoInputIndex );
626 if( AudioOutputIndex != -1 && AudioInputIndex != -1 )
628 if( SUCCEEDED( pXbar->Route(AudioOutputIndex,
629 AudioInputIndex)) )
631 msg_Dbg(p_this, "crossbar at depth %d, routed audio "
632 "output %ld to audio input %ld", i,
633 AudioOutputIndex, AudioInputIndex );
637 else
638 msg_Err( p_this, "crossbar at depth %d could not route video "
639 "output %ld to input %ld", i, VideoOutputIndex, VideoInputIndex );
643 ** Show properties pages from other filters in graph
645 if( var_GetBool( p_this, CFG_PREFIX "config" ) )
647 for( int i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
649 IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
650 IBaseFilter *p_XF;
652 if( SUCCEEDED( pXbar->QueryInterface( IID_IBaseFilter,
653 (void **)&p_XF ) ) )
655 ShowPropertyPage( p_XF );
656 p_XF->Release();
661 /* Initialize some data */
662 p_sys->i_current_stream = 0;
664 if( !p_sys->i_streams ) return VLC_EGENERIC;
666 return VLC_SUCCESS;
669 /*****************************************************************************
670 * DemuxOpen: open direct show device as an access_demux module
671 *****************************************************************************/
672 static int DemuxOpen( vlc_object_t *p_this )
674 demux_t *p_demux = (demux_t *)p_this;
675 access_sys_t *p_sys;
677 p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
678 if( !p_sys )
679 return VLC_ENOMEM;
680 p_demux->p_sys = (demux_sys_t *)p_sys;
682 if( CommonOpen( p_this, p_sys, true ) != VLC_SUCCESS )
684 CommonClose( p_this, p_sys );
685 return VLC_EGENERIC;
688 /* Everything is ready. Let's rock baby */
689 msg_Dbg( p_this, "Playing...");
690 p_sys->p_control->Run();
692 p_demux->pf_demux = Demux;
693 p_demux->pf_control = DemuxControl;
694 p_demux->info.i_update = 0;
695 p_demux->info.i_title = 0;
696 p_demux->info.i_seekpoint = 0;
698 for( int i = 0; i < p_sys->i_streams; i++ )
700 dshow_stream_t *p_stream = p_sys->pp_streams[i];
701 es_format_t fmt;
703 if( p_stream->mt.majortype == MEDIATYPE_Video )
705 char *psz_aspect = var_CreateGetString( p_this, CFG_PREFIX "aspect-ratio" );
706 char *psz_delim = !EMPTY_STR( psz_aspect ) ? strchr( psz_aspect, ':' ) : NULL;
708 es_format_Init( &fmt, VIDEO_ES, p_stream->i_fourcc );
710 fmt.video.i_width = p_stream->header.video.bmiHeader.biWidth;
711 fmt.video.i_height = p_stream->header.video.bmiHeader.biHeight;
713 if( psz_delim )
715 fmt.video.i_sar_num = atoi( psz_aspect ) * fmt.video.i_height;
716 fmt.video.i_sar_den = atoi( psz_delim + 1 ) * fmt.video.i_width;
718 else
720 fmt.video.i_sar_num = 4 * fmt.video.i_height;
721 fmt.video.i_sar_den = 3 * fmt.video.i_width;
723 free( psz_aspect );
725 if( !p_stream->header.video.bmiHeader.biCompression )
727 /* RGB DIB are coded from bottom to top */
728 fmt.video.i_height = (unsigned int)(-(int)fmt.video.i_height);
731 /* Setup rgb mask for RGB formats */
732 if( p_stream->i_fourcc == VLC_CODEC_RGB24 )
734 /* This is in RGB format
735 http://msdn.microsoft.com/en-us/library/dd407253%28VS.85%29.aspx?ppud=4
737 fmt.video.i_rmask = 0x00ff0000;
738 fmt.video.i_gmask = 0x0000ff00;
739 fmt.video.i_bmask = 0x000000ff;
742 if( p_stream->header.video.AvgTimePerFrame )
744 fmt.video.i_frame_rate = 10000000;
745 fmt.video.i_frame_rate_base =
746 p_stream->header.video.AvgTimePerFrame;
749 else if( p_stream->mt.majortype == MEDIATYPE_Audio )
751 es_format_Init( &fmt, AUDIO_ES, p_stream->i_fourcc );
753 fmt.audio.i_channels = p_stream->header.audio.nChannels;
754 fmt.audio.i_rate = p_stream->header.audio.nSamplesPerSec;
755 fmt.audio.i_bitspersample = p_stream->header.audio.wBitsPerSample;
756 fmt.audio.i_blockalign = fmt.audio.i_channels *
757 fmt.audio.i_bitspersample / 8;
758 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
759 fmt.audio.i_bitspersample;
762 p_stream->p_es = es_out_Add( p_demux->out, &fmt );
765 return VLC_SUCCESS;
768 /*****************************************************************************
769 * AccessOpen: open direct show device as an access module
770 *****************************************************************************/
771 static int AccessOpen( vlc_object_t *p_this )
773 access_t *p_access = (access_t*)p_this;
774 access_sys_t *p_sys;
776 p_access->p_sys = p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
777 if( !p_sys )
778 return VLC_ENOMEM;
780 if( CommonOpen( p_this, p_sys, false ) != VLC_SUCCESS )
782 CommonClose( p_this, p_sys );
783 return VLC_EGENERIC;
786 dshow_stream_t *p_stream = p_sys->pp_streams[0];
788 /* Check if we need to force demuxers */
789 if( !p_access->psz_demux || !*p_access->psz_demux )
791 if( p_stream->i_fourcc == VLC_CODEC_DV )
793 free( p_access->psz_demux );
794 p_access->psz_demux = strdup( "rawdv" );
796 else if( p_stream->i_fourcc == VLC_CODEC_MPGV )
798 free( p_access->psz_demux );
799 p_access->psz_demux = strdup( "mpgv" );
803 /* Setup Access */
804 p_access->pf_read = NULL;
805 p_access->pf_block = ReadCompressed;
806 p_access->pf_control = AccessControl;
807 p_access->pf_seek = NULL;
808 p_access->info.i_update = 0;
809 p_access->info.i_size = 0;
810 p_access->info.i_pos = 0;
811 p_access->info.b_eof = false;
812 p_access->info.i_title = 0;
813 p_access->info.i_seekpoint = 0;
814 p_access->p_sys = p_sys;
816 /* Everything is ready. Let's rock baby */
817 msg_Dbg( p_this, "Playing...");
818 p_sys->p_control->Run();
820 return VLC_SUCCESS;
823 /*****************************************************************************
824 * CommonClose: close device
825 *****************************************************************************/
826 static void CommonClose( vlc_object_t *p_this, access_sys_t *p_sys )
828 msg_Dbg( p_this, "releasing DirectShow");
830 DeleteDirectShowGraph( p_sys );
832 /* Uninitialize OLE/COM */
833 CoUninitialize();
835 for( int i = 0; i < p_sys->i_streams; i++ ) delete p_sys->pp_streams[i];
836 if( p_sys->i_streams ) free( p_sys->pp_streams );
838 vlc_mutex_destroy( &p_sys->lock );
839 vlc_cond_destroy( &p_sys->wait );
841 free( p_sys );
844 /*****************************************************************************
845 * AccessClose: close device
846 *****************************************************************************/
847 static void AccessClose( vlc_object_t *p_this )
849 access_t *p_access = (access_t *)p_this;
850 access_sys_t *p_sys = p_access->p_sys;
852 /* Stop capturing stuff */
853 p_sys->p_control->Stop();
855 CommonClose( p_this, p_sys );
858 /*****************************************************************************
859 * DemuxClose: close device
860 *****************************************************************************/
861 static void DemuxClose( vlc_object_t *p_this )
863 demux_t *p_demux = (demux_t *)p_this;
864 access_sys_t *p_sys = (access_sys_t *)p_demux->p_sys;
866 /* Stop capturing stuff */
867 p_sys->p_control->Stop();
869 CommonClose( p_this, p_sys );
872 /****************************************************************************
873 * ConnectFilters
874 ****************************************************************************/
875 static bool ConnectFilters( vlc_object_t *p_this, access_sys_t *p_sys,
876 IBaseFilter *p_filter,
877 CaptureFilter *p_capture_filter )
879 CapturePin *p_input_pin = p_capture_filter->CustomGetPin();
881 AM_MEDIA_TYPE mediaType = p_input_pin->CustomGetMediaType();
883 if( p_sys->p_capture_graph_builder2 )
885 if( FAILED(p_sys->p_capture_graph_builder2->
886 RenderStream( &PIN_CATEGORY_CAPTURE, &mediaType.majortype,
887 p_filter, 0, (IBaseFilter *)p_capture_filter )) )
889 return false;
892 // Sort out all the possible video inputs
893 // The class needs to be given the capture filters ANALOGVIDEO input pin
894 IEnumPins *pins = NULL;
895 if( ( mediaType.majortype == MEDIATYPE_Video ||
896 mediaType.majortype == MEDIATYPE_Stream ) &&
897 SUCCEEDED(p_filter->EnumPins(&pins)) )
899 IPin *pP = NULL;
900 ULONG n;
901 PIN_INFO pinInfo;
902 BOOL Found = FALSE;
903 IKsPropertySet *pKs = NULL;
904 GUID guid;
905 DWORD dw;
907 while( !Found && ( S_OK == pins->Next(1, &pP, &n) ) )
909 if( S_OK == pP->QueryPinInfo(&pinInfo) )
911 // is this pin an ANALOGVIDEOIN input pin?
912 if( pinInfo.dir == PINDIR_INPUT &&
913 pP->QueryInterface( IID_IKsPropertySet,
914 (void **)&pKs ) == S_OK )
916 if( pKs->Get( AMPROPSETID_Pin,
917 AMPROPERTY_PIN_CATEGORY, NULL, 0,
918 &guid, sizeof(GUID), &dw ) == S_OK )
920 if( guid == PIN_CATEGORY_ANALOGVIDEOIN )
922 // recursively search crossbar routes
923 FindCrossbarRoutes( p_this, p_sys, pP, 0 );
924 // found it
925 Found = TRUE;
928 pKs->Release();
930 pinInfo.pFilter->Release();
932 pP->Release();
934 pins->Release();
935 msg_Dbg( p_this, "ConnectFilters: graph_builder2 available.") ;
936 if ( !Found )
937 msg_Warn( p_this, "ConnectFilters: No crossBar routes found (incobatible pin types)" ) ;
939 return true;
941 else
943 IEnumPins *p_enumpins;
944 IPin *p_pin;
946 if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return false;
948 while( S_OK == p_enumpins->Next( 1, &p_pin, NULL ) )
950 PIN_DIRECTION pin_dir;
951 p_pin->QueryDirection( &pin_dir );
953 if( pin_dir == PINDIR_OUTPUT &&
954 p_sys->p_graph->ConnectDirect( p_pin, (IPin *)p_input_pin,
955 0 ) == S_OK )
957 p_pin->Release();
958 p_enumpins->Release();
959 return true;
961 p_pin->Release();
964 p_enumpins->Release();
965 return false;
970 * get fourcc priority from arbritary preference, the higher the better
972 static int GetFourCCPriority( int i_fourcc )
974 switch( i_fourcc )
976 case VLC_CODEC_I420:
977 case VLC_CODEC_FL32:
978 return 9;
979 case VLC_CODEC_YV12:
980 case VLC_FOURCC('a','r','a','w'):
981 return 8;
982 case VLC_CODEC_RGB24:
983 return 7;
984 case VLC_CODEC_YUYV:
985 case VLC_CODEC_RGB32:
986 case VLC_CODEC_RGBA:
987 return 6;
990 return 0;
993 #define MAX_MEDIA_TYPES 32
995 static int OpenDevice( vlc_object_t *p_this, access_sys_t *p_sys,
996 string devicename, bool b_audio )
998 /* See if device is already opened */
999 for( int i = 0; i < p_sys->i_streams; i++ )
1001 if( devicename.size() &&
1002 p_sys->pp_streams[i]->devicename == devicename )
1004 /* Already opened */
1005 return VLC_SUCCESS;
1009 list<string> list_devices;
1011 /* Enumerate devices and display their names */
1012 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
1013 if( !list_devices.size() )
1014 return VLC_EGENERIC;
1016 list<string>::iterator iter;
1017 for( iter = list_devices.begin(); iter != list_devices.end(); iter++ )
1018 msg_Dbg( p_this, "found device: %s", iter->c_str() );
1020 /* If no device name was specified, pick the 1st one */
1021 if( devicename.size() == 0 )
1023 /* When none selected */
1024 devicename = *list_devices.begin();
1025 msg_Dbg( p_this, "asking for default device: %s", devicename.c_str() ) ;
1027 else
1028 msg_Dbg( p_this, "asking for device: %s", devicename.c_str() ) ;
1029 // Use the system device enumerator and class enumerator to find
1030 // a capture/preview device, such as a desktop USB video camera.
1031 IBaseFilter *p_device_filter =
1032 FindCaptureDevice( p_this, &devicename, NULL, b_audio );
1034 if( p_device_filter )
1035 msg_Dbg( p_this, "using device: %s", devicename.c_str() );
1036 else
1038 msg_Err( p_this, "can't use device: %s, unsupported device type",
1039 devicename.c_str() );
1040 dialog_Fatal( p_this, _("Capture failed"),
1041 _("VLC cannot use the device \"%s\", because its "
1042 "type is not supported."), devicename.c_str() );
1043 return VLC_EGENERIC;
1046 // Retreive acceptable media types supported by device
1047 AM_MEDIA_TYPE media_types[MAX_MEDIA_TYPES];
1048 size_t media_count =
1049 EnumDeviceCaps( p_this, p_device_filter, b_audio ? 0 : p_sys->i_chroma,
1050 p_sys->i_width, p_sys->i_height,
1051 b_audio ? var_CreateGetInteger( p_this, CFG_PREFIX "audio-channels" ) : 0,
1052 b_audio ? var_CreateGetInteger( p_this, CFG_PREFIX "audio-samplerate" ) : 0,
1053 b_audio ? var_CreateGetInteger( p_this, CFG_PREFIX "audio-bitspersample" ) : 0,
1054 media_types, MAX_MEDIA_TYPES );
1056 AM_MEDIA_TYPE *mt = NULL;
1058 if( media_count > 0 )
1060 mt = (AM_MEDIA_TYPE *)CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * media_count);
1062 // Order and copy returned media types according to arbitrary
1063 // fourcc priority
1064 for( size_t c = 0; c < media_count; c++ )
1066 int slot_priority =
1067 GetFourCCPriority(GetFourCCFromMediaType(media_types[c]));
1068 size_t slot_copy = c;
1069 for( size_t d = c+1; d < media_count; d++ )
1071 int priority =
1072 GetFourCCPriority(GetFourCCFromMediaType(media_types[d]));
1073 if( priority > slot_priority )
1075 slot_priority = priority;
1076 slot_copy = d;
1079 if( slot_copy != c )
1081 mt[c] = media_types[slot_copy];
1082 media_types[slot_copy] = media_types[c];
1084 else
1086 mt[c] = media_types[c];
1090 else {
1091 /* capture device */
1092 msg_Err( p_this, "capture device '%s' does not support required parameters !", devicename.c_str() );
1093 dialog_Fatal( p_this, _("Capture failed"),
1094 _("The capture device \"%s\" does not support the "
1095 "required parameters."), devicename.c_str() );
1096 p_device_filter->Release();
1097 return VLC_EGENERIC;
1100 /* Create and add our capture filter */
1101 CaptureFilter *p_capture_filter =
1102 new CaptureFilter( p_this, p_sys, mt, media_count );
1103 p_sys->p_graph->AddFilter( p_capture_filter, 0 );
1105 /* Add the device filter to the graph (seems necessary with VfW before
1106 * accessing pin attributes). */
1107 p_sys->p_graph->AddFilter( p_device_filter, 0 );
1109 /* Attempt to connect one of this device's capture output pins */
1110 msg_Dbg( p_this, "connecting filters" );
1111 if( ConnectFilters( p_this, p_sys, p_device_filter, p_capture_filter ) )
1113 /* Success */
1114 msg_Dbg( p_this, "filters connected successfully !" );
1116 dshow_stream_t dshow_stream;
1117 dshow_stream.b_pts = false;
1118 dshow_stream.p_es = 0;
1119 dshow_stream.mt =
1120 p_capture_filter->CustomGetPin()->CustomGetMediaType();
1122 /* Show Device properties. Done here so the VLC stream is setup with
1123 * the proper parameters. */
1124 if( var_GetBool( p_this, CFG_PREFIX "config" ) )
1126 ShowDeviceProperties( p_this, p_sys->p_capture_graph_builder2,
1127 p_device_filter, b_audio );
1130 ConfigTuner( p_this, p_sys->p_capture_graph_builder2,
1131 p_device_filter );
1133 if( var_GetBool( p_this, CFG_PREFIX "tuner" ) &&
1134 dshow_stream.mt.majortype != MEDIATYPE_Stream )
1136 /* FIXME: we do MEDIATYPE_Stream later so we don't do it twice. */
1137 ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2,
1138 p_device_filter, b_audio );
1141 dshow_stream.mt =
1142 p_capture_filter->CustomGetPin()->CustomGetMediaType();
1144 dshow_stream.i_fourcc = GetFourCCFromMediaType( dshow_stream.mt );
1145 if( dshow_stream.i_fourcc )
1147 if( dshow_stream.mt.majortype == MEDIATYPE_Video )
1149 dshow_stream.header.video =
1150 *(VIDEOINFOHEADER *)dshow_stream.mt.pbFormat;
1151 msg_Dbg( p_this, "MEDIATYPE_Video" );
1152 msg_Dbg( p_this, "selected video pin accepts format: %4.4s",
1153 (char *)&dshow_stream.i_fourcc);
1155 else if( dshow_stream.mt.majortype == MEDIATYPE_Audio )
1157 dshow_stream.header.audio =
1158 *(WAVEFORMATEX *)dshow_stream.mt.pbFormat;
1159 msg_Dbg( p_this, "MEDIATYPE_Audio" );
1160 msg_Dbg( p_this, "selected audio pin accepts format: %4.4s",
1161 (char *)&dshow_stream.i_fourcc);
1163 else if( dshow_stream.mt.majortype == MEDIATYPE_Stream )
1165 msg_Dbg( p_this, "MEDIATYPE_Stream" );
1166 msg_Dbg( p_this, "selected stream pin accepts format: %4.4s",
1167 (char *)&dshow_stream.i_fourcc);
1169 else
1171 msg_Dbg( p_this, "unknown stream majortype" );
1172 goto fail;
1175 /* Add directshow elementary stream to our list */
1176 dshow_stream.p_device_filter = p_device_filter;
1177 dshow_stream.p_capture_filter = p_capture_filter;
1179 p_sys->pp_streams = (dshow_stream_t **)xrealloc( p_sys->pp_streams,
1180 sizeof(dshow_stream_t *) * (p_sys->i_streams + 1) );
1181 p_sys->pp_streams[p_sys->i_streams] = new dshow_stream_t;
1182 *p_sys->pp_streams[p_sys->i_streams++] = dshow_stream;
1184 return VLC_SUCCESS;
1188 fail:
1189 /* Remove filters from graph */
1190 p_sys->p_graph->RemoveFilter( p_device_filter );
1191 p_sys->p_graph->RemoveFilter( p_capture_filter );
1193 /* Release objects */
1194 p_device_filter->Release();
1195 p_capture_filter->Release();
1197 return VLC_EGENERIC;
1200 /* FindCaptureDevices:: This Function had two purposes :
1201 Returns the list of capture devices when p_listdevices != NULL
1202 Creates an IBaseFilter when p_devicename corresponds to an existing devname
1203 These actions *may* be requested whith a single call.
1205 static IBaseFilter *
1206 FindCaptureDevice( vlc_object_t *p_this, string *p_devicename,
1207 list<string> *p_listdevices, bool b_audio )
1209 IBaseFilter *p_base_filter = NULL;
1210 IMoniker *p_moniker = NULL;
1211 ULONG i_fetched;
1212 HRESULT hr;
1213 list<string> devicelist;
1215 /* Create the system device enumerator */
1216 ICreateDevEnum *p_dev_enum = NULL;
1218 hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
1219 IID_ICreateDevEnum, (void **)&p_dev_enum );
1220 if( FAILED(hr) )
1222 msg_Err( p_this, "failed to create the device enumerator (0x%lx)", hr);
1223 return NULL;
1226 /* Create an enumerator for the video capture devices */
1227 IEnumMoniker *p_class_enum = NULL;
1228 if( !b_audio )
1229 hr = p_dev_enum->CreateClassEnumerator( CLSID_VideoInputDeviceCategory,
1230 &p_class_enum, 0 );
1231 else
1232 hr = p_dev_enum->CreateClassEnumerator( CLSID_AudioInputDeviceCategory,
1233 &p_class_enum, 0 );
1234 p_dev_enum->Release();
1235 if( FAILED(hr) )
1237 msg_Err( p_this, "failed to create the class enumerator (0x%lx)", hr );
1238 return NULL;
1241 /* If there are no enumerators for the requested type, then
1242 * CreateClassEnumerator will succeed, but p_class_enum will be NULL */
1243 if( p_class_enum == NULL )
1245 msg_Err( p_this, "no %s capture device was detected", ( b_audio ? "audio" : "video" ) );
1246 return NULL;
1249 /* Enumerate the devices */
1251 /* Note that if the Next() call succeeds but there are no monikers,
1252 * it will return S_FALSE (which is not a failure). Therefore, we check
1253 * that the return code is S_OK instead of using SUCCEEDED() macro. */
1255 while( p_class_enum->Next( 1, &p_moniker, &i_fetched ) == S_OK )
1257 /* Getting the property page to get the device name */
1258 IPropertyBag *p_bag;
1259 hr = p_moniker->BindToStorage( 0, 0, IID_IPropertyBag,
1260 (void **)&p_bag );
1261 if( SUCCEEDED(hr) )
1263 VARIANT var;
1264 var.vt = VT_BSTR;
1265 hr = p_bag->Read( L"FriendlyName", &var, NULL );
1266 p_bag->Release();
1267 if( SUCCEEDED(hr) )
1269 char *p_buf = FromWide( var.bstrVal );
1270 string devname = string(p_buf);
1271 free( p_buf) ;
1273 int dup = 0;
1274 /* find out if this name is already used by a previously found device */
1275 list<string>::const_iterator iter = devicelist.begin();
1276 list<string>::const_iterator end = devicelist.end();
1277 string ordevname = devname ;
1278 while ( iter != end )
1280 if( 0 == (*iter).compare( devname ) )
1281 { /* devname is on the list. Try another name with sequence
1282 number apended and then rescan until a unique entry is found*/
1283 char seq[16];
1284 snprintf(seq, 16, " #%d", ++dup);
1285 devname = ordevname + seq;
1286 iter = devicelist.begin();
1288 else
1289 ++iter;
1291 devicelist.push_back( devname );
1293 if( p_devicename && *p_devicename == devname )
1295 msg_Dbg( p_this, "asked for %s, binding to %s", p_devicename->c_str() , devname.c_str() ) ;
1296 /* NULL possibly means we don't need BindMoniker BindCtx ?? */
1297 hr = p_moniker->BindToObject( NULL, 0, IID_IBaseFilter,
1298 (void **)&p_base_filter );
1299 if( FAILED(hr) )
1301 msg_Err( p_this, "couldn't bind moniker to filter "
1302 "object (0x%lx)", hr );
1303 p_moniker->Release();
1304 p_class_enum->Release();
1305 return NULL;
1307 p_moniker->Release();
1308 p_class_enum->Release();
1309 return p_base_filter;
1314 p_moniker->Release();
1317 p_class_enum->Release();
1319 if( p_listdevices ) {
1320 devicelist.sort();
1321 *p_listdevices = devicelist;
1323 return NULL;
1326 static size_t EnumDeviceCaps( vlc_object_t *p_this, IBaseFilter *p_filter,
1327 int i_fourcc, int i_width, int i_height,
1328 int i_channels, int i_samplespersec,
1329 int i_bitspersample, AM_MEDIA_TYPE *mt,
1330 size_t mt_max )
1332 IEnumPins *p_enumpins;
1333 IPin *p_output_pin;
1334 IEnumMediaTypes *p_enummt;
1335 size_t mt_count = 0;
1337 LONGLONG i_AvgTimePerFrame = 0;
1338 float r_fps = var_GetFloat( p_this, CFG_PREFIX "fps" );
1339 if( r_fps )
1340 i_AvgTimePerFrame = 10000000000LL/(LONGLONG)(r_fps*1000.0f);
1342 if( FAILED(p_filter->EnumPins( &p_enumpins )) )
1344 msg_Dbg( p_this, "EnumDeviceCaps failed: no pin enumeration !");
1345 return 0;
1348 while( S_OK == p_enumpins->Next( 1, &p_output_pin, NULL ) )
1350 PIN_INFO info;
1352 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1354 msg_Dbg( p_this, "EnumDeviceCaps: %s pin: %S",
1355 info.dir == PINDIR_INPUT ? "input" : "output",
1356 info.achName );
1357 if( info.pFilter ) info.pFilter->Release();
1360 p_output_pin->Release();
1363 p_enumpins->Reset();
1365 while( !mt_count && p_enumpins->Next( 1, &p_output_pin, NULL ) == S_OK )
1367 PIN_INFO info;
1369 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1371 if( info.pFilter ) info.pFilter->Release();
1372 if( info.dir == PINDIR_INPUT )
1374 p_output_pin->Release();
1375 continue;
1377 msg_Dbg( p_this, "EnumDeviceCaps: trying pin %S", info.achName );
1380 AM_MEDIA_TYPE *p_mt;
1383 ** Configure pin with a default compatible media if possible
1386 IAMStreamConfig *pSC;
1387 if( SUCCEEDED(p_output_pin->QueryInterface( IID_IAMStreamConfig,
1388 (void **)&pSC )) )
1390 int piCount, piSize;
1391 if( SUCCEEDED(pSC->GetNumberOfCapabilities(&piCount, &piSize)) )
1393 BYTE *pSCC= (BYTE *)CoTaskMemAlloc(piSize);
1394 if( NULL != pSCC )
1396 int i_priority = -1;
1397 for( int i=0; i<piCount; ++i )
1399 if( SUCCEEDED(pSC->GetStreamCaps(i, &p_mt, pSCC)) )
1401 int i_current_fourcc = GetFourCCFromMediaType( *p_mt );
1402 int i_current_priority = GetFourCCPriority(i_current_fourcc);
1404 if( (i_fourcc && (i_current_fourcc != i_fourcc))
1405 || (i_priority > i_current_priority) )
1407 // unwanted chroma, try next media type
1408 FreeMediaType( *p_mt );
1409 CoTaskMemFree( (PVOID)p_mt );
1410 continue;
1413 if( MEDIATYPE_Video == p_mt->majortype
1414 && FORMAT_VideoInfo == p_mt->formattype )
1416 VIDEO_STREAM_CONFIG_CAPS *pVSCC = reinterpret_cast<VIDEO_STREAM_CONFIG_CAPS*>(pSCC);
1417 VIDEOINFOHEADER *pVih = reinterpret_cast<VIDEOINFOHEADER*>(p_mt->pbFormat);
1419 if( i_AvgTimePerFrame )
1421 if( pVSCC->MinFrameInterval > i_AvgTimePerFrame
1422 || i_AvgTimePerFrame > pVSCC->MaxFrameInterval )
1424 // required frame rate not compatible, try next media type
1425 FreeMediaType( *p_mt );
1426 CoTaskMemFree( (PVOID)p_mt );
1427 continue;
1429 pVih->AvgTimePerFrame = i_AvgTimePerFrame;
1432 if( i_width )
1434 if(( !pVSCC->OutputGranularityX
1435 && i_width != pVSCC->MinOutputSize.cx
1436 && i_width != pVSCC->MaxOutputSize.cx)
1438 ( pVSCC->OutputGranularityX
1439 && ((i_width % pVSCC->OutputGranularityX)
1440 || pVSCC->MinOutputSize.cx > i_width
1441 || i_width > pVSCC->MaxOutputSize.cx )))
1443 // required width not compatible, try next media type
1444 FreeMediaType( *p_mt );
1445 CoTaskMemFree( (PVOID)p_mt );
1446 continue;
1448 pVih->bmiHeader.biWidth = i_width;
1451 if( i_height )
1453 if(( !pVSCC->OutputGranularityY
1454 && i_height != pVSCC->MinOutputSize.cy
1455 && i_height != pVSCC->MaxOutputSize.cy)
1457 ( pVSCC->OutputGranularityY
1458 && ((i_height % pVSCC->OutputGranularityY)
1459 || pVSCC->MinOutputSize.cy > i_height
1460 || i_height > pVSCC->MaxOutputSize.cy )))
1462 // required height not compatible, try next media type
1463 FreeMediaType( *p_mt );
1464 CoTaskMemFree( (PVOID)p_mt );
1465 continue;
1467 pVih->bmiHeader.biHeight = i_height;
1470 // Set the sample size and image size.
1471 // (Round the image width up to a DWORD boundary.)
1472 p_mt->lSampleSize = pVih->bmiHeader.biSizeImage =
1473 ((pVih->bmiHeader.biWidth + 3) & ~3) *
1474 pVih->bmiHeader.biHeight * (pVih->bmiHeader.biBitCount>>3);
1476 // no cropping, use full video input buffer
1477 memset(&(pVih->rcSource), 0, sizeof(RECT));
1478 memset(&(pVih->rcTarget), 0, sizeof(RECT));
1480 // select this format as default
1481 if( SUCCEEDED( pSC->SetFormat(p_mt) ) )
1483 i_priority = i_current_priority;
1484 if( i_fourcc )
1485 // no need to check any more media types
1486 i = piCount;
1489 else if( p_mt->majortype == MEDIATYPE_Audio
1490 && p_mt->formattype == FORMAT_WaveFormatEx )
1492 AUDIO_STREAM_CONFIG_CAPS *pASCC = reinterpret_cast<AUDIO_STREAM_CONFIG_CAPS*>(pSCC);
1493 WAVEFORMATEX *pWfx = reinterpret_cast<WAVEFORMATEX*>(p_mt->pbFormat);
1495 if( i_current_fourcc && (WAVE_FORMAT_PCM == pWfx->wFormatTag) )
1497 int val = i_channels;
1498 if( ! val )
1499 val = 2;
1501 if( ( !pASCC->ChannelsGranularity
1502 && (unsigned int)val != pASCC->MinimumChannels
1503 && (unsigned int)val != pASCC->MaximumChannels)
1505 ( pASCC->ChannelsGranularity
1506 && ((val % pASCC->ChannelsGranularity)
1507 || (unsigned int)val < pASCC->MinimumChannels
1508 || (unsigned int)val > pASCC->MaximumChannels)))
1510 // required number channels not available, try next media type
1511 FreeMediaType( *p_mt );
1512 CoTaskMemFree( (PVOID)p_mt );
1513 continue;
1515 pWfx->nChannels = val;
1517 val = i_samplespersec;
1518 if( ! val )
1519 val = 44100;
1521 if( ( !pASCC->SampleFrequencyGranularity
1522 && (unsigned int)val != pASCC->MinimumSampleFrequency
1523 && (unsigned int)val != pASCC->MaximumSampleFrequency)
1525 ( pASCC->SampleFrequencyGranularity
1526 && ((val % pASCC->SampleFrequencyGranularity)
1527 || (unsigned int)val < pASCC->MinimumSampleFrequency
1528 || (unsigned int)val > pASCC->MaximumSampleFrequency )))
1530 // required sampling rate not available, try next media type
1531 FreeMediaType( *p_mt );
1532 CoTaskMemFree( (PVOID)p_mt );
1533 continue;
1535 pWfx->nSamplesPerSec = val;
1537 val = i_bitspersample;
1538 if( ! val )
1540 if( VLC_CODEC_FL32 == i_current_fourcc )
1541 val = 32;
1542 else
1543 val = 16;
1546 if( ( !pASCC->BitsPerSampleGranularity
1547 && (unsigned int)val != pASCC->MinimumBitsPerSample
1548 && (unsigned int)val != pASCC->MaximumBitsPerSample )
1550 ( pASCC->BitsPerSampleGranularity
1551 && ((val % pASCC->BitsPerSampleGranularity)
1552 || (unsigned int)val < pASCC->MinimumBitsPerSample
1553 || (unsigned int)val > pASCC->MaximumBitsPerSample )))
1555 // required sample size not available, try next media type
1556 FreeMediaType( *p_mt );
1557 CoTaskMemFree( (PVOID)p_mt );
1558 continue;
1561 pWfx->wBitsPerSample = val;
1562 pWfx->nBlockAlign = (pWfx->wBitsPerSample * pWfx->nChannels)/8;
1563 pWfx->nAvgBytesPerSec = pWfx->nSamplesPerSec * pWfx->nBlockAlign;
1565 // select this format as default
1566 if( SUCCEEDED( pSC->SetFormat(p_mt) ) )
1568 i_priority = i_current_priority;
1572 FreeMediaType( *p_mt );
1573 CoTaskMemFree( (PVOID)p_mt );
1576 CoTaskMemFree( (LPVOID)pSCC );
1577 if( i_priority >= 0 )
1578 msg_Dbg( p_this, "EnumDeviceCaps: input pin default format configured");
1581 pSC->Release();
1585 ** Probe pin for available medias (may be a previously configured one)
1588 if( FAILED( p_output_pin->EnumMediaTypes( &p_enummt ) ) )
1590 p_output_pin->Release();
1591 continue;
1594 while( p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
1596 int i_current_fourcc = GetFourCCFromMediaType( *p_mt );
1597 if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Video
1598 && p_mt->formattype == FORMAT_VideoInfo )
1600 int i_current_width = ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth;
1601 int i_current_height = ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight;
1602 LONGLONG i_current_atpf = ((VIDEOINFOHEADER *)p_mt->pbFormat)->AvgTimePerFrame;
1604 if( i_current_height < 0 )
1605 i_current_height = -i_current_height;
1607 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1608 "accepts chroma: %4.4s, width:%i, height:%i, fps:%f",
1609 (char *)&i_current_fourcc, i_current_width,
1610 i_current_height, (10000000.0f/((float)i_current_atpf)) );
1612 if( ( !i_fourcc || i_fourcc == i_current_fourcc ) &&
1613 ( !i_width || i_width == i_current_width ) &&
1614 ( !i_height || i_height == i_current_height ) &&
1615 ( !i_AvgTimePerFrame || i_AvgTimePerFrame == i_current_atpf ) &&
1616 mt_count < mt_max )
1618 /* Pick match */
1619 mt[mt_count++] = *p_mt;
1621 else FreeMediaType( *p_mt );
1623 else if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Audio
1624 && p_mt->formattype == FORMAT_WaveFormatEx)
1626 int i_current_channels =
1627 ((WAVEFORMATEX *)p_mt->pbFormat)->nChannels;
1628 int i_current_samplespersec =
1629 ((WAVEFORMATEX *)p_mt->pbFormat)->nSamplesPerSec;
1630 int i_current_bitspersample =
1631 ((WAVEFORMATEX *)p_mt->pbFormat)->wBitsPerSample;
1633 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1634 "accepts format: %4.4s, channels:%i, "
1635 "samples/sec:%i bits/sample:%i",
1636 (char *)&i_current_fourcc, i_current_channels,
1637 i_current_samplespersec, i_current_bitspersample);
1639 if( (!i_channels || i_channels == i_current_channels) &&
1640 (!i_samplespersec ||
1641 i_samplespersec == i_current_samplespersec) &&
1642 (!i_bitspersample ||
1643 i_bitspersample == i_current_bitspersample) &&
1644 mt_count < mt_max )
1646 /* Pick match */
1647 mt[mt_count++] = *p_mt;
1649 /* Setup a few properties like the audio latency */
1650 IAMBufferNegotiation *p_ambuf;
1651 if( SUCCEEDED( p_output_pin->QueryInterface(
1652 IID_IAMBufferNegotiation, (void **)&p_ambuf ) ) )
1654 ALLOCATOR_PROPERTIES AllocProp;
1655 AllocProp.cbAlign = -1;
1657 /* 100 ms of latency */
1658 AllocProp.cbBuffer = i_current_channels *
1659 i_current_samplespersec *
1660 i_current_bitspersample / 8 / 10;
1662 AllocProp.cbPrefix = -1;
1663 AllocProp.cBuffers = -1;
1664 p_ambuf->SuggestAllocatorProperties( &AllocProp );
1665 p_ambuf->Release();
1668 else FreeMediaType( *p_mt );
1670 else if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Stream )
1672 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1673 "accepts stream format: %4.4s",
1674 (char *)&i_current_fourcc );
1676 if( ( !i_fourcc || i_fourcc == i_current_fourcc ) &&
1677 mt_count < mt_max )
1679 /* Pick match */
1680 mt[mt_count++] = *p_mt;
1681 i_fourcc = i_current_fourcc;
1683 else FreeMediaType( *p_mt );
1685 else
1687 const char * psz_type = "unknown";
1688 if( p_mt->majortype == MEDIATYPE_Video ) psz_type = "video";
1689 if( p_mt->majortype == MEDIATYPE_Audio ) psz_type = "audio";
1690 if( p_mt->majortype == MEDIATYPE_Stream ) psz_type = "stream";
1691 msg_Dbg( p_this, "EnumDeviceCaps: input pin media: unsupported format "
1692 "(%s %4.4s)", psz_type, (char *)&p_mt->subtype );
1694 FreeMediaType( *p_mt );
1696 CoTaskMemFree( (PVOID)p_mt );
1699 if( !mt_count && p_enummt->Reset() == S_OK )
1701 // VLC did not find any supported MEDIATYPE for this output pin.
1702 // However the graph builder might insert converter filters in
1703 // the graph if we use a different codec in VLC filter input pin.
1704 // however, in order to avoid nasty surprises, make use of this
1705 // facility only for known unsupported codecs.
1707 while( !mt_count && p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
1709 // the first four bytes of subtype GUID contains the codec FOURCC
1710 const char *pfcc = (char *)&p_mt->subtype;
1711 int i_current_fourcc = VLC_FOURCC(pfcc[0], pfcc[1], pfcc[2], pfcc[3]);
1712 if( VLC_FOURCC('H','C','W','2') == i_current_fourcc
1713 && p_mt->majortype == MEDIATYPE_Video )
1715 // output format for 'Hauppauge WinTV PVR PCI II Capture'
1716 // try I420 as an input format
1717 i_current_fourcc = VLC_CODEC_I420;
1718 if( !i_fourcc || i_fourcc == i_current_fourcc )
1720 // return alternative media type
1721 AM_MEDIA_TYPE mtr;
1722 VIDEOINFOHEADER vh;
1724 mtr.majortype = MEDIATYPE_Video;
1725 mtr.subtype = MEDIASUBTYPE_I420;
1726 mtr.bFixedSizeSamples = TRUE;
1727 mtr.bTemporalCompression = FALSE;
1728 mtr.pUnk = NULL;
1729 mtr.formattype = FORMAT_VideoInfo;
1730 mtr.cbFormat = sizeof(vh);
1731 mtr.pbFormat = (BYTE *)&vh;
1733 memset(&vh, 0, sizeof(vh));
1735 vh.bmiHeader.biSize = sizeof(vh.bmiHeader);
1736 vh.bmiHeader.biWidth = i_width > 0 ? i_width :
1737 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth;
1738 vh.bmiHeader.biHeight = i_height > 0 ? i_height :
1739 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight;
1740 vh.bmiHeader.biPlanes = 3;
1741 vh.bmiHeader.biBitCount = 12;
1742 vh.bmiHeader.biCompression = VLC_CODEC_I420;
1743 vh.bmiHeader.biSizeImage = vh.bmiHeader.biWidth * 12 *
1744 vh.bmiHeader.biHeight / 8;
1745 mtr.lSampleSize = vh.bmiHeader.biSizeImage;
1747 msg_Dbg( p_this, "EnumDeviceCaps: input pin media: using 'I420' in place of unsupported format 'HCW2'");
1749 if( SUCCEEDED(CopyMediaType(mt+mt_count, &mtr)) )
1750 ++mt_count;
1753 FreeMediaType( *p_mt );
1757 p_enummt->Release();
1758 p_output_pin->Release();
1761 p_enumpins->Release();
1762 return mt_count;
1765 /*****************************************************************************
1766 * ReadCompressed: reads compressed (MPEG/DV) data from the device.
1767 *****************************************************************************
1768 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
1769 * bytes.
1770 *****************************************************************************/
1771 static block_t *ReadCompressed( access_t *p_access )
1773 access_sys_t *p_sys = p_access->p_sys;
1774 dshow_stream_t *p_stream = NULL;
1775 VLCMediaSample sample;
1777 /* Read 1 DV/MPEG frame (they contain the video and audio data) */
1779 /* There must be only 1 elementary stream to produce a valid stream
1780 * of MPEG or DV data */
1781 p_stream = p_sys->pp_streams[0];
1783 while( 1 )
1785 if( !vlc_object_alive (p_access) ) return NULL;
1787 /* Get new sample/frame from the elementary stream (blocking). */
1788 vlc_mutex_lock( &p_sys->lock );
1790 if( p_stream->p_capture_filter->CustomGetPin()
1791 ->CustomGetSample( &sample ) != S_OK )
1793 /* No data available. Wait until some data has arrived */
1794 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
1795 vlc_mutex_unlock( &p_sys->lock );
1796 continue;
1799 vlc_mutex_unlock( &p_sys->lock );
1802 * We got our sample
1804 block_t *p_block;
1805 uint8_t *p_data;
1806 int i_data_size = sample.p_sample->GetActualDataLength();
1808 if( !i_data_size || !(p_block = block_New( p_access, i_data_size )) )
1810 sample.p_sample->Release();
1811 continue;
1814 sample.p_sample->GetPointer( &p_data );
1815 vlc_memcpy( p_block->p_buffer, p_data, i_data_size );
1816 sample.p_sample->Release();
1818 /* The caller got what he wanted */
1819 return p_block;
1822 return NULL; /* never reached */
1825 /****************************************************************************
1826 * Demux:
1827 ****************************************************************************/
1828 static int Demux( demux_t *p_demux )
1830 access_sys_t *p_sys = (access_sys_t *)p_demux->p_sys;
1831 int i_found_samples;
1833 i_found_samples = 0;
1834 vlc_mutex_lock( &p_sys->lock );
1836 while ( !i_found_samples )
1838 /* Try to grab samples from all streams */
1839 for( int i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1841 dshow_stream_t *p_stream = p_sys->pp_streams[i_stream];
1842 if( p_stream->p_capture_filter &&
1843 p_stream->p_capture_filter->CustomGetPin()
1844 ->CustomGetSamples( p_stream->samples_queue ) == S_OK )
1846 i_found_samples = 1;
1850 if ( !i_found_samples)
1852 /* Didn't find any audio nor video sample, just wait till the
1853 * dshow thread pushes some samples */
1854 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
1855 /* Some DShow thread pushed data, or the OS broke the wait all
1856 * by itself. In all cases, it's *strongly* advised to test the
1857 * condition again, so let the loop do the test again */
1861 vlc_mutex_unlock( &p_sys->lock );
1863 for ( int i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1865 int i_samples;
1866 dshow_stream_t *p_stream = p_sys->pp_streams[i_stream];
1868 i_samples = p_stream->samples_queue.size();
1869 while ( i_samples > 0 )
1871 int i_data_size;
1872 uint8_t *p_data;
1873 block_t *p_block;
1874 VLCMediaSample sample;
1876 sample = p_stream->samples_queue.front();
1877 p_stream->samples_queue.pop_front();
1879 i_data_size = sample.p_sample->GetActualDataLength();
1880 sample.p_sample->GetPointer( &p_data );
1882 REFERENCE_TIME i_pts, i_end_date;
1883 HRESULT hr = sample.p_sample->GetTime( &i_pts, &i_end_date );
1884 if( hr != VFW_S_NO_STOP_TIME && hr != S_OK ) i_pts = 0;
1886 if( !i_pts )
1888 if( p_stream->mt.majortype == MEDIATYPE_Video || !p_stream->b_pts )
1890 /* Use our data timestamp */
1891 i_pts = sample.i_timestamp;
1892 p_stream->b_pts = true;
1896 i_pts /= 10; /* Dshow works with 100 nano-seconds resolution */
1898 #if 0
1899 msg_Dbg( p_demux, "Read() stream: %i, size: %i, PTS: %"PRId64,
1900 i_stream, i_data_size, i_pts );
1901 #endif
1903 p_block = block_New( p_demux, i_data_size );
1904 vlc_memcpy( p_block->p_buffer, p_data, i_data_size );
1905 p_block->i_pts = p_block->i_dts = i_pts;
1906 sample.p_sample->Release();
1908 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pts > 0 ? i_pts : 0 );
1909 es_out_Send( p_demux->out, p_stream->p_es, p_block );
1911 i_samples--;
1915 return 1;
1918 /*****************************************************************************
1919 * AccessControl:
1920 *****************************************************************************/
1921 static int AccessControl( access_t *p_access, int i_query, va_list args )
1923 bool *pb_bool;
1924 int64_t *pi_64;
1926 switch( i_query )
1928 /* */
1929 case ACCESS_CAN_SEEK:
1930 case ACCESS_CAN_FASTSEEK:
1931 case ACCESS_CAN_PAUSE:
1932 case ACCESS_CAN_CONTROL_PACE:
1933 pb_bool = (bool*)va_arg( args, bool* );
1934 *pb_bool = false;
1935 break;
1937 /* */
1938 case ACCESS_GET_PTS_DELAY:
1939 pi_64 = (int64_t*)va_arg( args, int64_t * );
1940 *pi_64 = var_GetInteger( p_access, CFG_PREFIX "caching" ) * 1000;
1941 break;
1943 /* */
1944 case ACCESS_SET_PAUSE_STATE:
1945 case ACCESS_GET_TITLE_INFO:
1946 case ACCESS_SET_TITLE:
1947 case ACCESS_SET_SEEKPOINT:
1948 case ACCESS_SET_PRIVATE_ID_STATE:
1949 return VLC_EGENERIC;
1951 default:
1952 msg_Warn( p_access, "unimplemented query in control" );
1953 return VLC_EGENERIC;
1956 return VLC_SUCCESS;
1959 /****************************************************************************
1960 * DemuxControl:
1961 ****************************************************************************/
1962 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
1964 bool *pb;
1965 int64_t *pi64;
1967 switch( i_query )
1969 /* Special for access_demux */
1970 case DEMUX_CAN_PAUSE:
1971 case DEMUX_CAN_SEEK:
1972 case DEMUX_SET_PAUSE_STATE:
1973 case DEMUX_CAN_CONTROL_PACE:
1974 pb = (bool*)va_arg( args, bool * );
1975 *pb = false;
1976 return VLC_SUCCESS;
1978 case DEMUX_GET_PTS_DELAY:
1979 pi64 = (int64_t*)va_arg( args, int64_t * );
1980 *pi64 = var_GetInteger( p_demux, CFG_PREFIX "caching" ) * 1000;
1981 return VLC_SUCCESS;
1983 case DEMUX_GET_TIME:
1984 pi64 = (int64_t*)va_arg( args, int64_t * );
1985 *pi64 = mdate();
1986 return VLC_SUCCESS;
1988 /* TODO implement others */
1989 default:
1990 return VLC_EGENERIC;
1993 return VLC_EGENERIC;
1996 /*****************************************************************************
1997 * config variable callback
1998 *****************************************************************************/
1999 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
2000 vlc_value_t newval, vlc_value_t oldval, void * )
2002 module_config_t *p_item;
2003 bool b_audio = false;
2004 int i;
2006 p_item = config_FindConfig( p_this, psz_name );
2007 if( !p_item ) return VLC_SUCCESS;
2009 if( !strcmp( psz_name, CFG_PREFIX "adev" ) ) b_audio = true;
2011 /* Clear-up the current list */
2012 if( p_item->i_list )
2014 /* Keep the 2 first entries */
2015 for( i = 2; i < p_item->i_list; i++ )
2017 free( const_cast<char *>(p_item->ppsz_list[i]) );
2018 free( const_cast<char *>(p_item->ppsz_list_text[i]) );
2020 /* TODO: Remove when no more needed */
2021 p_item->ppsz_list[i] = NULL;
2022 p_item->ppsz_list_text[i] = NULL;
2024 p_item->i_list = 2;
2026 /* Find list of devices */
2027 list<string> list_devices;
2029 /* Initialize OLE/COM */
2030 CoInitialize( 0 );
2032 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
2034 /* Uninitialize OLE/COM */
2035 CoUninitialize();
2037 if( list_devices.empty() ) return VLC_SUCCESS;
2039 p_item->ppsz_list = (char**)xrealloc( p_item->ppsz_list,
2040 (list_devices.size()+3) * sizeof(char *) );
2041 p_item->ppsz_list_text = (char**)xrealloc( p_item->ppsz_list_text,
2042 (list_devices.size()+3) * sizeof(char *) );
2044 list<string>::iterator iter;
2045 for( iter = list_devices.begin(), i = 2; iter != list_devices.end();
2046 iter++, i++ )
2048 p_item->ppsz_list[i] = strdup( iter->c_str() );
2049 p_item->ppsz_list_text[i] = NULL;
2050 p_item->i_list++;
2052 p_item->ppsz_list[i] = NULL;
2053 p_item->ppsz_list_text[i] = NULL;
2055 /* Signal change to the interface */
2056 p_item->b_dirty = true;
2058 return VLC_SUCCESS;
2061 static int ConfigDevicesCallback( vlc_object_t *p_this, char const *psz_name,
2062 vlc_value_t newval, vlc_value_t oldval, void * )
2064 module_config_t *p_item;
2065 bool b_audio = false;
2066 char *psz_device = NULL;
2067 int i_ret = VLC_SUCCESS;
2069 if( !EMPTY_STR( newval.psz_string ) )
2070 psz_device = strdup( newval.psz_string );
2072 /* Initialize OLE/COM */
2073 CoInitialize( 0 );
2075 p_item = config_FindConfig( p_this, psz_name );
2077 if( !p_item )
2079 free( psz_device );
2080 CoUninitialize();
2081 return VLC_SUCCESS;
2084 if( !strcmp( psz_name, CFG_PREFIX "adev" ) ) b_audio = true;
2086 string devicename;
2088 if( psz_device )
2090 devicename = psz_device ;
2092 else
2094 /* If no device name was specified, pick the 1st one */
2095 list<string> list_devices;
2097 /* Enumerate devices */
2098 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
2099 if( list_devices.empty() )
2101 CoUninitialize();
2102 return VLC_EGENERIC;
2104 devicename = *list_devices.begin();
2107 IBaseFilter *p_device_filter =
2108 FindCaptureDevice( p_this, &devicename, NULL, b_audio );
2109 if( p_device_filter )
2111 ShowPropertyPage( p_device_filter );
2112 p_device_filter->Release();
2114 else
2116 msg_Err( p_this, "didn't find device: %s", devicename.c_str() );
2117 i_ret = VLC_EGENERIC;
2120 /* Uninitialize OLE/COM */
2121 CoUninitialize();
2123 free( psz_device );
2124 return i_ret;
2127 /*****************************************************************************
2128 * Properties
2129 *****************************************************************************/
2131 static void ShowPropertyPage( IUnknown *obj )
2133 ISpecifyPropertyPages *p_spec;
2134 CAUUID cauuid;
2136 HRESULT hr = obj->QueryInterface( IID_ISpecifyPropertyPages,
2137 (void **)&p_spec );
2138 if( FAILED(hr) ) return;
2140 if( SUCCEEDED(p_spec->GetPages( &cauuid )) )
2142 if( cauuid.cElems > 0 )
2144 HWND hwnd_desktop = ::GetDesktopWindow();
2146 OleCreatePropertyFrame( hwnd_desktop, 30, 30, NULL, 1, &obj,
2147 cauuid.cElems, cauuid.pElems, 0, 0, NULL );
2149 CoTaskMemFree( cauuid.pElems );
2151 p_spec->Release();
2155 static void ShowDeviceProperties( vlc_object_t *p_this,
2156 ICaptureGraphBuilder2 *p_graph,
2157 IBaseFilter *p_device_filter,
2158 bool b_audio )
2160 HRESULT hr;
2161 msg_Dbg( p_this, "configuring Device Properties" );
2164 * Video or audio capture filter page
2166 ShowPropertyPage( p_device_filter );
2169 * Audio capture pin
2171 if( p_graph && b_audio )
2173 IAMStreamConfig *p_SC;
2175 msg_Dbg( p_this, "showing WDM Audio Configuration Pages" );
2177 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2178 &MEDIATYPE_Audio, p_device_filter,
2179 IID_IAMStreamConfig, (void **)&p_SC );
2180 if( SUCCEEDED(hr) )
2182 ShowPropertyPage(p_SC);
2183 p_SC->Release();
2187 * TV Audio filter
2189 IAMTVAudio *p_TVA;
2190 HRESULT hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2191 &MEDIATYPE_Audio, p_device_filter,
2192 IID_IAMTVAudio, (void **)&p_TVA );
2193 if( SUCCEEDED(hr) )
2195 ShowPropertyPage(p_TVA);
2196 p_TVA->Release();
2201 * Video capture pin
2203 if( p_graph && !b_audio )
2205 IAMStreamConfig *p_SC;
2207 msg_Dbg( p_this, "showing WDM Video Configuration Pages" );
2209 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2210 &MEDIATYPE_Interleaved, p_device_filter,
2211 IID_IAMStreamConfig, (void **)&p_SC );
2212 if( FAILED(hr) )
2214 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2215 &MEDIATYPE_Video, p_device_filter,
2216 IID_IAMStreamConfig, (void **)&p_SC );
2219 if( FAILED(hr) )
2221 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2222 &MEDIATYPE_Stream, p_device_filter,
2223 IID_IAMStreamConfig, (void **)&p_SC );
2226 if( SUCCEEDED(hr) )
2228 ShowPropertyPage(p_SC);
2229 p_SC->Release();
2234 static void ShowTunerProperties( vlc_object_t *p_this,
2235 ICaptureGraphBuilder2 *p_graph,
2236 IBaseFilter *p_device_filter,
2237 bool b_audio )
2239 HRESULT hr;
2240 msg_Dbg( p_this, "configuring Tuner Properties" );
2242 if( !p_graph || b_audio ) return;
2244 IAMTVTuner *p_TV;
2245 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2246 &MEDIATYPE_Interleaved, p_device_filter,
2247 IID_IAMTVTuner, (void **)&p_TV );
2248 if( FAILED(hr) )
2250 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2251 &MEDIATYPE_Video, p_device_filter,
2252 IID_IAMTVTuner, (void **)&p_TV );
2255 if( FAILED(hr) )
2257 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2258 &MEDIATYPE_Stream, p_device_filter,
2259 IID_IAMTVTuner, (void **)&p_TV );
2262 if( SUCCEEDED(hr) )
2264 ShowPropertyPage(p_TV);
2265 p_TV->Release();
2269 static void ConfigTuner( vlc_object_t *p_this, ICaptureGraphBuilder2 *p_graph,
2270 IBaseFilter *p_device_filter )
2272 int i_channel, i_country, i_input, i_amtuner_mode, i_standard, i_frequency;
2273 long l_modes = 0;
2274 IAMTVTuner *p_TV;
2275 HRESULT hr;
2277 if( !p_graph ) return;
2279 i_channel = var_GetInteger( p_this, CFG_PREFIX "tuner-channel" );
2280 i_country = var_GetInteger( p_this, CFG_PREFIX "tuner-country" );
2281 i_input = var_GetInteger( p_this, CFG_PREFIX "tuner-input" );
2282 i_amtuner_mode = var_GetInteger( p_this, CFG_PREFIX "amtuner-mode" );
2283 i_frequency = var_GetInteger( p_this, CFG_PREFIX "tuner-frequency" );
2284 i_standard = var_GetInteger( p_this, CFG_PREFIX "tuner-standard" );
2286 if( !i_channel && !i_frequency && !i_country && !i_input ) return; /* Nothing to do */
2288 msg_Dbg( p_this, "tuner config: channel %i, frequency %i, country %i, input type %i, standard %s",
2289 i_channel, i_frequency, i_country, i_input, ppsz_standards_list_text[i_standard] );
2292 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Interleaved,
2293 p_device_filter, IID_IAMTVTuner,
2294 (void **)&p_TV );
2295 if( FAILED(hr) )
2297 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,
2298 p_device_filter, IID_IAMTVTuner,
2299 (void **)&p_TV );
2302 if( FAILED(hr) )
2304 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Stream,
2305 p_device_filter, IID_IAMTVTuner,
2306 (void **)&p_TV );
2309 if( FAILED(hr) )
2311 msg_Dbg( p_this, "couldn't find tuner interface" );
2312 return;
2315 hr = p_TV->GetAvailableModes( &l_modes );
2316 if( SUCCEEDED(hr) && (l_modes & i_amtuner_mode) )
2318 hr = p_TV->put_Mode( (AMTunerModeType)i_amtuner_mode );
2321 if( i_input == 1 ) p_TV->put_InputType( 0, TunerInputCable );
2322 else if( i_input == 2 ) p_TV->put_InputType( 0, TunerInputAntenna );
2324 p_TV->put_CountryCode( i_country );
2326 if( i_frequency <= 0 ) p_TV->put_Channel( i_channel, AMTUNER_SUBCHAN_NO_TUNE,
2327 AMTUNER_SUBCHAN_NO_TUNE );
2329 if( i_frequency > 0 || i_standard > 0) {
2330 IKsPropertySet *pKs = NULL;
2331 DWORD dw_supported = 0;
2332 KSPROPERTY_TUNER_MODE_CAPS_S ModeCaps;
2333 KSPROPERTY_TUNER_FREQUENCY_S Frequency;
2334 KSPROPERTY_TUNER_STANDARD_S Standard;
2336 hr = p_TV->QueryInterface(IID_IKsPropertySet,(void **)&pKs);
2337 if (FAILED(hr))
2339 msg_Dbg( p_this, "Couldn't QI for IKsPropertySet" );
2340 p_TV->Release();
2341 return;
2344 memset(&ModeCaps,0,sizeof(KSPROPERTY_TUNER_MODE_CAPS_S));
2345 memset(&Frequency,0,sizeof(KSPROPERTY_TUNER_FREQUENCY_S));
2346 memset(&Standard,0,sizeof(KSPROPERTY_TUNER_STANDARD_S));
2347 ModeCaps.Mode = AMTUNER_MODE_TV;
2349 hr = pKs->QuerySupported(PROPSETID_TUNER,
2350 KSPROPERTY_TUNER_MODE_CAPS,&dw_supported);
2351 if(SUCCEEDED(hr) && dw_supported&KSPROPERTY_SUPPORT_GET)
2353 DWORD cbBytes=0;
2354 hr = pKs->Get(PROPSETID_TUNER,KSPROPERTY_TUNER_MODE_CAPS,
2355 INSTANCEDATA_OF_PROPERTY_PTR(&ModeCaps),
2356 INSTANCEDATA_OF_PROPERTY_SIZE(ModeCaps),
2357 &ModeCaps,
2358 sizeof(ModeCaps),
2359 &cbBytes);
2361 else
2363 msg_Dbg( p_this, "KSPROPERTY_TUNER_MODE_CAPS not supported!" );
2364 goto free_on_error;
2367 msg_Dbg( p_this, "Frequency range supproted from %d to %d.", ModeCaps.MinFrequency, ModeCaps.MaxFrequency);
2368 msg_Dbg( p_this, "Video standards supproted by the tuner: ");
2369 for(int i = 0 ; i < ARRAY_SIZE(ppsz_standards_list_text); i++) {
2370 if(ModeCaps.StandardsSupported & i_standards_list[i])
2371 msg_Dbg( p_this, "%s, ", ppsz_standards_list_text[i]);
2374 if(i_frequency > 0) {
2375 Frequency.Frequency=i_frequency;
2376 if(ModeCaps.Strategy==KS_TUNER_STRATEGY_DRIVER_TUNES)
2377 Frequency.TuningFlags=KS_TUNER_TUNING_FINE;
2378 else
2379 Frequency.TuningFlags=KS_TUNER_TUNING_EXACT;
2381 if(i_frequency>=ModeCaps.MinFrequency && i_frequency<=ModeCaps.MaxFrequency)
2384 hr = pKs->Set(PROPSETID_TUNER,
2385 KSPROPERTY_TUNER_FREQUENCY,
2386 INSTANCEDATA_OF_PROPERTY_PTR(&Frequency),
2387 INSTANCEDATA_OF_PROPERTY_SIZE(Frequency),
2388 &Frequency,
2389 sizeof(Frequency));
2390 if(FAILED(hr))
2392 msg_Dbg( p_this, "Couldn't set KSPROPERTY_TUNER_FREQUENCY!" );
2393 goto free_on_error;
2396 else
2398 msg_Dbg( p_this, "Requested frequency exceeds the supported range!" );
2399 goto free_on_error;
2403 if(i_standard > 0) {
2404 if(i_standard & ModeCaps.StandardsSupported )
2406 Standard.Standard = i_standard;
2407 hr = pKs->Set(PROPSETID_TUNER,
2408 KSPROPERTY_TUNER_STANDARD,
2409 INSTANCEDATA_OF_PROPERTY_PTR(&Standard),
2410 INSTANCEDATA_OF_PROPERTY_SIZE(Standard),
2411 &Standard,
2412 sizeof(Standard));
2413 if(FAILED(hr))
2415 msg_Dbg( p_this, "Couldn't set KSPROPERTY_TUNER_STANDARD!" );
2416 goto free_on_error;
2419 else
2421 msg_Dbg( p_this, "Requested video standard is not supported by the tuner!" );
2422 goto free_on_error;
2425 free_on_error:
2426 pKs->Release();
2429 p_TV->Release();