static int dshow_read_header(AVFormatContext *avctx) { struct dshow_ctx *ctx = avctx->priv_data; IGraphBuilder *graph = NULL; ICreateDevEnum *devenum = NULL; IMediaControl *control = NULL; IMediaEvent *media_event = NULL; HANDLE media_event_handle; HANDLE proc; int ret = AVERROR(EIO); int r; CoInitialize(0); if (!ctx->list_devices && !parse_device_name(avctx)) { av_log(avctx, AV_LOG_ERROR, "Malformed dshow input string.\n"); goto error; } ctx->video_codec_id = avctx->video_codec_id ? avctx->video_codec_id : AV_CODEC_ID_RAWVIDEO; if (ctx->pixel_format != AV_PIX_FMT_NONE) { if (ctx->video_codec_id != AV_CODEC_ID_RAWVIDEO) { av_log(avctx, AV_LOG_ERROR, "Pixel format may only be set when " "video codec is not set or set to rawvideo\n"); ret = AVERROR(EINVAL); goto error; } } if (ctx->framerate) { r = av_parse_video_rate(&ctx->requested_framerate, ctx->framerate); if (r < 0) { av_log(avctx, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", ctx->framerate); goto error; } } r = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, &IID_IGraphBuilder, (void **) &graph); if (r != S_OK) { av_log(avctx, AV_LOG_ERROR, "Could not create capture graph.\n"); goto error; } ctx->graph = graph; r = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, &IID_ICreateDevEnum, (void **) &devenum); if (r != S_OK) { av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n"); goto error; } if (ctx->list_devices) { av_log(avctx, AV_LOG_INFO, "DirectShow video devices (some may be both video and audio devices)\n"); dshow_cycle_devices(avctx, devenum, VideoDevice, VideoSourceDevice, NULL); av_log(avctx, AV_LOG_INFO, "DirectShow audio devices\n"); dshow_cycle_devices(avctx, devenum, AudioDevice, AudioSourceDevice, NULL); ret = AVERROR_EXIT; goto error; } if (ctx->list_options) { if (ctx->device_name[VideoDevice]) if ((r = dshow_list_device_options(avctx, devenum, VideoDevice, VideoSourceDevice))) { ret = r; goto error; } if (ctx->device_name[AudioDevice]) { if (dshow_list_device_options(avctx, devenum, AudioDevice, AudioSourceDevice)) { /* show audio options from combined video+audio sources as fallback */ if ((r = dshow_list_device_options(avctx, devenum, AudioDevice, VideoSourceDevice))) { ret = r; goto error; } } } } if (ctx->device_name[VideoDevice]) { if ((r = dshow_open_device(avctx, devenum, VideoDevice, VideoSourceDevice)) < 0 || (r = dshow_add_device(avctx, VideoDevice)) < 0) { ret = r; goto error; } } if (ctx->device_name[AudioDevice]) { if ((r = dshow_open_device(avctx, devenum, AudioDevice, AudioSourceDevice)) < 0 || (r = dshow_add_device(avctx, AudioDevice)) < 0) { av_log(avctx, AV_LOG_INFO, "Searching for audio device within video devices for %s\n", ctx->device_name[AudioDevice]); /* see if there's a video source with an audio pin with the given audio name */ if ((r = dshow_open_device(avctx, devenum, AudioDevice, VideoSourceDevice)) < 0 || (r = dshow_add_device(avctx, AudioDevice)) < 0) { ret = r; goto error; } } } if (ctx->list_options) { /* allow it to list crossbar options in dshow_open_device */ ret = AVERROR_EXIT; goto error; } ctx->curbufsize[0] = 0; ctx->curbufsize[1] = 0; ctx->mutex = CreateMutex(NULL, 0, NULL); if (!ctx->mutex) { av_log(avctx, AV_LOG_ERROR, "Could not create Mutex\n"); goto error; } ctx->event[1] = CreateEvent(NULL, 1, 0, NULL); if (!ctx->event[1]) { av_log(avctx, AV_LOG_ERROR, "Could not create Event\n"); goto error; } r = IGraphBuilder_QueryInterface(graph, &IID_IMediaControl, (void **) &control); if (r != S_OK) { av_log(avctx, AV_LOG_ERROR, "Could not get media control.\n"); goto error; } ctx->control = control; r = IGraphBuilder_QueryInterface(graph, &IID_IMediaEvent, (void **) &media_event); if (r != S_OK) { av_log(avctx, AV_LOG_ERROR, "Could not get media event.\n"); goto error; } ctx->media_event = media_event; r = IMediaEvent_GetEventHandle(media_event, (void *) &media_event_handle); if (r != S_OK) { av_log(avctx, AV_LOG_ERROR, "Could not get media event handle.\n"); goto error; } proc = GetCurrentProcess(); r = DuplicateHandle(proc, media_event_handle, proc, &ctx->event[0], 0, 0, DUPLICATE_SAME_ACCESS); if (!r) { av_log(avctx, AV_LOG_ERROR, "Could not duplicate media event handle.\n"); goto error; } r = IMediaControl_Run(control); if (r == S_FALSE) { OAFilterState pfs; r = IMediaControl_GetState(control, 0, &pfs); } if (r != S_OK) { av_log(avctx, AV_LOG_ERROR, "Could not run graph (sometimes caused by a device already in use by other application)\n"); goto error; } ret = 0; error: if (devenum) ICreateDevEnum_Release(devenum); if (ret < 0) dshow_read_close(avctx); return ret; }
static int dshow_read_header(AVFormatContext *avctx) { struct dshow_ctx *ctx = avctx->priv_data; IGraphBuilder *graph = NULL; ICreateDevEnum *devenum = NULL; IMediaControl *control = NULL; int ret = AVERROR(EIO); int r; if (!ctx->list_devices && !parse_device_name(avctx)) { av_log(avctx, AV_LOG_ERROR, "Malformed dshow input string.\n"); goto error; } ctx->video_codec_id = avctx->video_codec_id ? avctx->video_codec_id : AV_CODEC_ID_RAWVIDEO; if (ctx->pixel_format != AV_PIX_FMT_NONE) { if (ctx->video_codec_id != AV_CODEC_ID_RAWVIDEO) { av_log(avctx, AV_LOG_ERROR, "Pixel format may only be set when " "video codec is not set or set to rawvideo\n"); ret = AVERROR(EINVAL); goto error; } } if (ctx->framerate) { r = av_parse_video_rate(&ctx->requested_framerate, ctx->framerate); if (r < 0) { av_log(avctx, AV_LOG_ERROR, "Could not parse framerate '%s'.\n", ctx->framerate); goto error; } } CoInitialize(0); r = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, &IID_IGraphBuilder, (void **) &graph); if (r != S_OK) { av_log(avctx, AV_LOG_ERROR, "Could not create capture graph.\n"); goto error; } ctx->graph = graph; r = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, &IID_ICreateDevEnum, (void **) &devenum); if (r != S_OK) { av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n"); goto error; } if (ctx->list_devices) { av_log(avctx, AV_LOG_INFO, "DirectShow video devices\n"); dshow_cycle_devices(avctx, devenum, VideoDevice, NULL); av_log(avctx, AV_LOG_INFO, "DirectShow audio devices\n"); dshow_cycle_devices(avctx, devenum, AudioDevice, NULL); ret = AVERROR_EXIT; goto error; } if (ctx->list_options) { if (ctx->device_name[VideoDevice]) dshow_list_device_options(avctx, devenum, VideoDevice); if (ctx->device_name[AudioDevice]) dshow_list_device_options(avctx, devenum, AudioDevice); ret = AVERROR_EXIT; goto error; } if (ctx->device_name[VideoDevice]) { if ((r = dshow_open_device(avctx, devenum, VideoDevice)) < 0 || (r = dshow_add_device(avctx, VideoDevice)) < 0) { ret = r; goto error; } } if (ctx->device_name[AudioDevice]) { if ((r = dshow_open_device(avctx, devenum, AudioDevice)) < 0 || (r = dshow_add_device(avctx, AudioDevice)) < 0) { ret = r; goto error; } } ctx->mutex = CreateMutex(NULL, 0, NULL); if (!ctx->mutex) { av_log(avctx, AV_LOG_ERROR, "Could not create Mutex\n"); goto error; } ctx->event = CreateEvent(NULL, 1, 0, NULL); if (!ctx->event) { av_log(avctx, AV_LOG_ERROR, "Could not create Event\n"); goto error; } r = IGraphBuilder_QueryInterface(graph, &IID_IMediaControl, (void **) &control); if (r != S_OK) { av_log(avctx, AV_LOG_ERROR, "Could not get media control.\n"); goto error; } ctx->control = control; r = IMediaControl_Run(control); if (r == S_FALSE) { OAFilterState pfs; r = IMediaControl_GetState(control, 0, &pfs); } if (r != S_OK) { av_log(avctx, AV_LOG_ERROR, "Could not run filter\n"); goto error; } ret = 0; error: if (ret < 0) dshow_read_close(avctx); if (devenum) ICreateDevEnum_Release(devenum); return ret; }
static HRESULT DXDiag_InitDXDiagDirectShowFiltersContainer(IDxDiagContainer* pSubCont) { HRESULT hr = S_OK; static const WCHAR szName[] = {'s','z','N','a','m','e',0}; static const WCHAR szCatName[] = {'s','z','C','a','t','N','a','m','e',0}; static const WCHAR szClsidCat[] = {'s','z','C','l','s','i','d','C','a','t',0}; static const WCHAR szClsidFilter[] = {'s','z','C','l','s','i','d','F','i','l','t','e','r',0}; static const WCHAR dwInputs[] = {'d','w','I','n','p','u','t','s',0}; static const WCHAR dwOutputs[] = {'d','w','O','u','t','p','u','t','s',0}; static const WCHAR dwMerit[] = {'d','w','M','e','r','i','t',0}; /* static const WCHAR szFileName[] = {'s','z','F','i','l','e','N','a','m','e',0}; static const WCHAR szFileVersion[] = {'s','z','F','i','l','e','V','e','r','s','i','o','n',0}; */ VARIANT v; static const WCHAR wszClsidName[] = {'C','L','S','I','D',0}; static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0}; static const WCHAR wszFilterDataName[] = {'F','i','l','t','e','r','D','a','t','a',0}; /*static const WCHAR wszMeritName[] = {'M','e','r','i','t',0};*/ ICreateDevEnum* pCreateDevEnum = NULL; IEnumMoniker* pEmCat = NULL; IMoniker* pMCat = NULL; /** */ hr = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, &IID_ICreateDevEnum, (void**) &pCreateDevEnum); if (FAILED(hr)) return hr; hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &CLSID_ActiveMovieCategories, &pEmCat, 0); if (FAILED(hr)) goto out_show_filters; VariantInit(&v); while (S_OK == IEnumMoniker_Next(pEmCat, 1, &pMCat, NULL)) { IPropertyBag* pPropBag = NULL; CLSID clsidCat; hr = IMoniker_BindToStorage(pMCat, NULL, NULL, &IID_IPropertyBag, (void**) &pPropBag); if (SUCCEEDED(hr)) { WCHAR* wszCatName = NULL; WCHAR* wszCatClsid = NULL; hr = IPropertyBag_Read(pPropBag, wszFriendlyName, &v, 0); wszCatName = SysAllocString(V_BSTR(&v)); VariantClear(&v); hr = IPropertyBag_Read(pPropBag, wszClsidName, &v, 0); wszCatClsid = SysAllocString(V_BSTR(&v)); hr = CLSIDFromString(V_UNION(&v, bstrVal), &clsidCat); VariantClear(&v); /* hr = IPropertyBag_Read(pPropBag, wszMeritName, &v, 0); hr = IDxDiagContainerImpl_AddProp(pSubCont, dwMerit, &v); VariantClear(&v); */ if (SUCCEEDED(hr)) { IEnumMoniker* pEnum = NULL; IMoniker* pMoniker = NULL; hr = ICreateDevEnum_CreateClassEnumerator(pCreateDevEnum, &clsidCat, &pEnum, 0); FIXME("\tClassEnumerator for clsid(%s) pEnum(%p)\n", debugstr_guid(&clsidCat), pEnum); if (FAILED(hr) || pEnum == NULL) { goto class_enum_failed; } while (NULL != pEnum && S_OK == IEnumMoniker_Next(pEnum, 1, &pMoniker, NULL)) { IPropertyBag* pPropFilterBag = NULL; FIXME("\tIEnumMoniker_Next(%p, 1, %p)\n", pEnum, pMoniker); hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (void**) &pPropFilterBag); if (SUCCEEDED(hr)) { LPBYTE pData = NULL; LPBYTE pCurrent = NULL; struct REG_RF* prrf = NULL; VARIANT v_data; DWORD it; DWORD dwNOutputs = 0; DWORD dwNInputs = 0; V_VT(&v) = VT_BSTR; V_BSTR(&v) = SysAllocString(wszCatName); hr = IDxDiagContainerImpl_AddProp(pSubCont, szCatName, &v); VariantClear(&v); V_VT(&v) = VT_BSTR; V_BSTR(&v) = SysAllocString(wszCatClsid); hr = IDxDiagContainerImpl_AddProp(pSubCont, szClsidCat, &v); VariantClear(&v); hr = IPropertyBag_Read(pPropFilterBag, wszFriendlyName, &v, 0); hr = IDxDiagContainerImpl_AddProp(pSubCont, szName, &v); FIXME("\tName:%s\n", debugstr_w(V_BSTR(&v))); VariantClear(&v); hr = IPropertyBag_Read(pPropFilterBag, wszClsidName, &v, 0); FIXME("\tClsid:%s\n", debugstr_w(V_BSTR(&v))); hr = IDxDiagContainerImpl_AddProp(pSubCont, szClsidFilter, &v); VariantClear(&v); hr = IPropertyBag_Read(pPropFilterBag, wszFilterDataName, &v, NULL); hr = SafeArrayAccessData(V_UNION(&v, parray), (LPVOID*) &pData); prrf = (struct REG_RF*) pData; pCurrent = pData; VariantInit(&v_data); V_VT(&v_data) = VT_UI4; V_UI4(&v_data) = prrf->dwVersion; hr = IDxDiagContainerImpl_AddProp(pSubCont, szName, &v_data); VariantClear(&v_data); V_VT(&v_data) = VT_UI4; V_UI4(&v_data) = prrf->dwMerit; hr = IDxDiagContainerImpl_AddProp(pSubCont, dwMerit, &v_data); VariantClear(&v_data); pCurrent += sizeof(struct REG_RF); for (it = 0; it < prrf->dwPins; ++it) { struct REG_RFP* prrfp = (struct REG_RFP*) pCurrent; UINT j; if (prrfp->dwFlags & REG_PINFLAG_B_OUTPUT) ++dwNOutputs; else ++dwNInputs; pCurrent += sizeof(struct REG_RFP); if (prrfp->bCategory) { pCurrent += sizeof(DWORD); } for (j = 0; j < prrfp->dwMediaTypes; ++j) { struct REG_TYPE* prt = (struct REG_TYPE *)pCurrent; pCurrent += sizeof(*prt); } for (j = 0; j < prrfp->dwMediums; ++j) { DWORD dwOffset = *(DWORD*) pCurrent; pCurrent += sizeof(dwOffset); } } V_VT(&v_data) = VT_UI4; V_UI4(&v_data) = dwNInputs; hr = IDxDiagContainerImpl_AddProp(pSubCont, dwInputs, &v_data); VariantClear(&v_data); V_VT(&v_data) = VT_UI4; V_UI4(&v_data) = dwNOutputs; hr = IDxDiagContainerImpl_AddProp(pSubCont, dwOutputs, &v_data); VariantClear(&v_data); SafeArrayUnaccessData(V_UNION(&v, parray)); VariantClear(&v); } IPropertyBag_Release(pPropFilterBag); pPropFilterBag = NULL; } IEnumMoniker_Release(pEnum); pEnum = NULL; } class_enum_failed: SysFreeString(wszCatName); SysFreeString(wszCatClsid); IPropertyBag_Release(pPropBag); pPropBag = NULL; } IEnumMoniker_Release(pMCat); pMCat = NULL; } out_show_filters: if (NULL != pEmCat) { IEnumMoniker_Release(pEmCat); pEmCat = NULL; } if (NULL != pCreateDevEnum) { ICreateDevEnum_Release(pCreateDevEnum); pCreateDevEnum = NULL; } return hr; }
static GValueArray * gst_dshowaudiosrc_get_device_name_values (GstDshowAudioSrc * src) { GValueArray *array = g_value_array_new (0); GValue value = { 0 }; ICreateDevEnum *devices_enum = NULL; IEnumMoniker *moniker_enum = NULL; IMoniker *moniker = NULL; HRESULT hres = S_FALSE; ULONG fetched; g_value_init (&value, G_TYPE_STRING); hres = CoCreateInstance (&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, &IID_ICreateDevEnum, (void **) &devices_enum); if (hres != S_OK) { GST_CAT_ERROR (dshowaudiosrc_debug, "Can't create an instance of the system device enumerator (error=%d)", hres); array = NULL; goto clean; } hres = ICreateDevEnum_CreateClassEnumerator (devices_enum, &CLSID_AudioInputDeviceCategory, &moniker_enum, 0); if (hres != S_OK || !moniker_enum) { GST_CAT_ERROR (dshowaudiosrc_debug, "Can't get enumeration of audio devices (error=%d)", hres); array = NULL; goto clean; } IEnumMoniker_Reset (moniker_enum); while (hres = IEnumMoniker_Next (moniker_enum, 1, &moniker, &fetched), hres == S_OK) { IPropertyBag *property_bag = NULL; hres = IMoniker_BindToStorage (moniker, NULL, NULL, &IID_IPropertyBag, (void **) &property_bag); if (SUCCEEDED (hres) && property_bag) { VARIANT varFriendlyName; VariantInit (&varFriendlyName); hres = IPropertyBag_Read (property_bag, L"FriendlyName", &varFriendlyName, NULL); if (hres == S_OK && varFriendlyName.bstrVal) { gchar *friendly_name = g_utf16_to_utf8 ((const gunichar2 *) varFriendlyName.bstrVal, wcslen (varFriendlyName.bstrVal), NULL, NULL, NULL); g_value_set_string (&value, friendly_name); g_value_array_append (array, &value); g_value_unset (&value); g_free (friendly_name); SysFreeString (varFriendlyName.bstrVal); } IPropertyBag_Release (property_bag); } IMoniker_Release (moniker); } clean: if (moniker_enum) { IEnumMoniker_Release (moniker_enum); } if (devices_enum) { ICreateDevEnum_Release (devices_enum); } return array; }
static void test_devenum(IBindCtx *bind_ctx) { HRESULT res; ICreateDevEnum* create_devenum; IEnumMoniker* enum_moniker = NULL; int i; res = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, &IID_ICreateDevEnum, (LPVOID*)&create_devenum); if (res != S_OK) { skip("Cannot create SystemDeviceEnum object (%x)\n", res); return; } for (i = 0; i < (sizeof(am_categories) / sizeof(struct category)); i++) { if (winetest_debug > 1) trace("%s:\n", am_categories[i].name); res = ICreateDevEnum_CreateClassEnumerator(create_devenum, am_categories[i].clsid, &enum_moniker, 0); ok(SUCCEEDED(res), "Cannot create enum moniker (res = %x)\n", res); if (res == S_OK) { IMoniker* moniker; while (IEnumMoniker_Next(enum_moniker, 1, &moniker, NULL) == S_OK) { IPropertyBag* prop_bag = NULL; VARIANT var; HRESULT hr; VariantInit(&var); hr = IMoniker_BindToStorage(moniker, bind_ctx, NULL, &IID_IPropertyBag, (LPVOID*)&prop_bag); ok(hr == S_OK, "IMoniker_BindToStorage failed with error %x\n", hr); if (SUCCEEDED(hr)) { hr = IPropertyBag_Read(prop_bag, friendly_name, &var, NULL); ok((hr == S_OK) || broken(hr == 0x80070002), "IPropertyBag_Read failed with error %x\n", hr); if (SUCCEEDED(hr)) { if (winetest_debug > 1) trace(" %s\n", wine_dbgstr_w(V_UNION(&var, bstrVal))); VariantClear(&var); } else { trace(" ???\n"); } } if (prop_bag) IPropertyBag_Release(prop_bag); IMoniker_Release(moniker); } IEnumMoniker_Release(enum_moniker); } } ICreateDevEnum_Release(create_devenum); }