From 30469d45c0a9e3f6f0aa361ef0008e313fd07b3f Mon Sep 17 00:00:00 2001 From: Julien Isorce Date: Tue, 21 Jul 2009 10:49:37 +0200 Subject: start refactoring of dshowsrcwrapper -remove gst-libs/gst/dshow -fakesource is moved from gst-libs/gst/dshow to sys/dshowsrcwrapper -some minor changes (C/C++ check and includes) to make the plugin compile again. --- sys/dshowsrcwrapper/Makefile.am | 22 +- sys/dshowsrcwrapper/gstdshow.cpp | 346 +++++++++++++++++++++++++++++ sys/dshowsrcwrapper/gstdshow.h | 76 +++++++ sys/dshowsrcwrapper/gstdshowaudiosrc.c | 10 +- sys/dshowsrcwrapper/gstdshowaudiosrc.h | 3 +- sys/dshowsrcwrapper/gstdshowfakesink.cpp | 127 +++++++++++ sys/dshowsrcwrapper/gstdshowfakesink.h | 50 +++++ sys/dshowsrcwrapper/gstdshowinterface.h | 163 ++++++++++++++ sys/dshowsrcwrapper/gstdshowsrcwrapper.c | 59 ----- sys/dshowsrcwrapper/gstdshowsrcwrapper.cpp | 66 ++++++ sys/dshowsrcwrapper/gstdshowsrcwrapper.h | 34 --- sys/dshowsrcwrapper/gstdshowvideosrc.c | 24 +- sys/dshowsrcwrapper/gstdshowvideosrc.h | 4 +- sys/dshowsrcwrapper/libgstdshow.def | 8 + 14 files changed, 871 insertions(+), 121 deletions(-) mode change 100644 => 100755 sys/dshowsrcwrapper/Makefile.am create mode 100755 sys/dshowsrcwrapper/gstdshow.cpp create mode 100755 sys/dshowsrcwrapper/gstdshow.h mode change 100644 => 100755 sys/dshowsrcwrapper/gstdshowaudiosrc.c mode change 100644 => 100755 sys/dshowsrcwrapper/gstdshowaudiosrc.h create mode 100755 sys/dshowsrcwrapper/gstdshowfakesink.cpp create mode 100755 sys/dshowsrcwrapper/gstdshowfakesink.h create mode 100755 sys/dshowsrcwrapper/gstdshowinterface.h delete mode 100644 sys/dshowsrcwrapper/gstdshowsrcwrapper.c create mode 100755 sys/dshowsrcwrapper/gstdshowsrcwrapper.cpp delete mode 100644 sys/dshowsrcwrapper/gstdshowsrcwrapper.h mode change 100644 => 100755 sys/dshowsrcwrapper/gstdshowvideosrc.c mode change 100644 => 100755 sys/dshowsrcwrapper/gstdshowvideosrc.h create mode 100755 sys/dshowsrcwrapper/libgstdshow.def (limited to 'sys/dshowsrcwrapper') diff --git a/sys/dshowsrcwrapper/Makefile.am b/sys/dshowsrcwrapper/Makefile.am old mode 100644 new mode 100755 index 38895a1e..3ec4d64a --- a/sys/dshowsrcwrapper/Makefile.am +++ b/sys/dshowsrcwrapper/Makefile.am @@ -1,10 +1,14 @@ -plugin_LTLIBRARIES = libgstdshowsrcwrapper.la +# This plugin isn't buildable with autotools at this point in time, so just +# ensure everything's listed in EXTRA_DIST -libgstdshowsrcwrapper_la_SOURCES = gstdshowaudiosrc.c gstdshowsrcwrapper.c gstdshowvideosrc.c - -libgstdshowsrcwrapper_la_CFLAGS = $(GST_CFLAGS) -libgstdshowsrcwrapper_la_LIBADD = $(GST_LIBS) -libgstdshowsrcwrapper_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) -libgstdshowsrcwrapper_la_LIBTOOLFLAGS = --tag=disable-static - -noinst_HEADERS = gstdshowaudiosrc.h gstdshowsrcwrapper.h gstdshowvideosrc.h +EXTRA_DIST = \ + gstdshowaudiosrc.c \ + gstdshowaudiosrc.h \ + gstdshow.cpp \ + gstdshowfakesink.cpp \ + gstdshowfakesink.h \ + gstdshow.h \ + gstdshowinterface.h \ + gstdshowsrcwrapper.cpp \ + gstdshowvideosrc.c \ + gstdshowvideosrc.h diff --git a/sys/dshowsrcwrapper/gstdshow.cpp b/sys/dshowsrcwrapper/gstdshow.cpp new file mode 100755 index 00000000..ed27d631 --- /dev/null +++ b/sys/dshowsrcwrapper/gstdshow.cpp @@ -0,0 +1,346 @@ +/* GStreamer + * Copyright (C) 2007 Sebastien Moutte + * + * gstdshow.cpp: + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "gstdshow.h" +#include "gstdshowfakesink.h" + +CFactoryTemplate g_Templates[]= +{ + { + L"DSHOW fake sink filter" + , &CLSID_DshowFakeSink + , CDshowFakeSink::CreateInstance + , NULL + , NULL + } +}; + +int g_cTemplates = sizeof(g_Templates)/sizeof(g_Templates[0]); +static HINSTANCE g_hModule = NULL; + +extern "C" BOOL WINAPI DllEntryPoint(HINSTANCE, ULONG, LPVOID); +BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved) +{ + if (!g_hModule) + g_hModule = (HINSTANCE)hModule; + + return DllEntryPoint((HINSTANCE)(hModule), dwReason, lpReserved); +} + +STDAPI DllRegisterServer() +{ + return AMovieDllRegisterServer2 (TRUE); +} + +STDAPI DllUnregisterServer() +{ + return AMovieDllRegisterServer2 (FALSE); +} + +HRESULT gst_dshow_register_fakefilters () +{ + return DllRegisterServer(); +} + +void +gst_dshow_free_mediatype (AM_MEDIA_TYPE *pmt) +{ + if (pmt != NULL) { + if (pmt->cbFormat != 0) { + CoTaskMemFree((PVOID)pmt->pbFormat); + pmt->cbFormat = 0; + pmt->pbFormat = NULL; + } + if (pmt->pUnk != NULL) { + /* Unecessary because pUnk should not be used, but safest. */ + pmt->pUnk->Release(); + pmt->pUnk = NULL; + } + + CoTaskMemFree(pmt); + } +} + +void +gst_dshow_free_pin_mediatype (gpointer pt) +{ + GstCapturePinMediaType * pin_mediatype = (GstCapturePinMediaType *) pt; + if (pin_mediatype) { + if (pin_mediatype->capture_pin) { + pin_mediatype->capture_pin->Release(); + pin_mediatype->capture_pin = NULL; + } + if (pin_mediatype->mediatype) { + gst_dshow_free_mediatype (pin_mediatype->mediatype); + pin_mediatype->mediatype = NULL; + } + } +} + + +void +gst_dshow_free_pins_mediatypes (GList *pins_mediatypes) +{ + guint i = 0; + for (; i < g_list_length (pins_mediatypes); i++) { + GList *mylist = g_list_nth (pins_mediatypes, i); + if (mylist && mylist->data) + gst_dshow_free_pin_mediatype ((GstCapturePinMediaType *)mylist->data); + } + g_list_free (pins_mediatypes); +} + +gboolean +gst_dshow_get_pin_from_filter (IBaseFilter *filter, PIN_DIRECTION pindir, IPin **pin) +{ + gboolean ret = FALSE; + IEnumPins *enumpins = NULL; + IPin *pintmp = NULL; + HRESULT hres; + *pin = NULL; + + hres = filter->EnumPins (&enumpins); + if (FAILED(hres)) { + return ret; + } + + while (enumpins->Next (1, &pintmp, NULL) == S_OK) + { + PIN_DIRECTION pindirtmp; + hres = pintmp->QueryDirection (&pindirtmp); + if (hres == S_OK && pindir == pindirtmp) { + *pin = pintmp; + ret = TRUE; + break; + } + pintmp->Release (); + } + enumpins->Release (); + + return ret; +} + +gboolean gst_dshow_find_filter(CLSID input_majortype, CLSID input_subtype, + CLSID output_majortype, CLSID output_subtype, + gchar * prefered_filter_name, IBaseFilter **filter) +{ + gboolean ret = FALSE; + HRESULT hres; + GUID arrayInTypes[2]; + GUID arrayOutTypes[2]; + IFilterMapper2 *mapper = NULL; + IEnumMoniker *enum_moniker = NULL; + IMoniker *moniker = NULL; + ULONG fetched; + gchar *prefered_filter_upper = NULL; + gboolean exit = FALSE; + + /* initialize output parameter */ + if (filter) + *filter = NULL; + + /* create a private copy of prefered filter substring in upper case */ + if (prefered_filter_name) { + prefered_filter_upper = g_strdup (prefered_filter_name); + _strupr (prefered_filter_upper); + } + + hres = CoCreateInstance(CLSID_FilterMapper2, NULL, CLSCTX_INPROC, + IID_IFilterMapper2, (void **) &mapper); + if (FAILED(hres)) + goto clean; + + memcpy(&arrayInTypes[0], &input_majortype, sizeof (CLSID)); + memcpy(&arrayInTypes[1], &input_subtype, sizeof (CLSID)); + memcpy(&arrayOutTypes[0], &output_majortype, sizeof (CLSID)); + memcpy(&arrayOutTypes[1], &output_subtype, sizeof (CLSID)); + + hres = mapper->EnumMatchingFilters (&enum_moniker, 0, FALSE, MERIT_DO_NOT_USE+1, + TRUE, 1, arrayInTypes, NULL, NULL, FALSE, + TRUE, 1, arrayOutTypes, NULL, NULL); + if (FAILED(hres)) + goto clean; + + enum_moniker->Reset (); + + while(hres = enum_moniker->Next (1, &moniker, &fetched),hres == S_OK + && !exit) { + IBaseFilter *filter_temp = NULL; + IPropertyBag *property_bag = NULL; + gchar * friendly_name = NULL; + + hres = moniker->BindToStorage (NULL, NULL, IID_IPropertyBag, (void **)&property_bag); + if(SUCCEEDED(hres) && property_bag) { + VARIANT varFriendlyName; + VariantInit (&varFriendlyName); + + hres = property_bag->Read (L"FriendlyName", &varFriendlyName, NULL); + if(hres == S_OK && varFriendlyName.bstrVal) { + friendly_name = g_utf16_to_utf8((const gunichar2*)varFriendlyName.bstrVal, + wcslen(varFriendlyName.bstrVal), NULL, NULL, NULL); + if (friendly_name) + _strupr (friendly_name); + SysFreeString (varFriendlyName.bstrVal); + } + property_bag->Release (); + } + + hres = moniker->BindToObject(NULL, NULL, IID_IBaseFilter, (void**)&filter_temp); + if(SUCCEEDED(hres) && filter_temp) { + ret = TRUE; + if (filter) { + if (*filter) + (*filter)->Release (); + + *filter = filter_temp; + (*filter)->AddRef (); + + if (prefered_filter_upper && friendly_name && + strstr(friendly_name, prefered_filter_upper)) + exit = TRUE; + } + + /* if we just want to know if the formats are supported OR + if we don't care about what will be the filter used + => we can stop enumeration */ + if (!filter || !prefered_filter_upper) + exit = TRUE; + + filter_temp->Release (); + } + + if (friendly_name) + g_free (friendly_name); + moniker->Release (); + } + +clean: + if (prefered_filter_upper) + g_free (prefered_filter_upper); + if (enum_moniker) + enum_moniker->Release (); + if (mapper) + mapper->Release (); + + return ret; +} + + +gchar * +gst_dshow_getdevice_from_devicename (const GUID *device_category, gchar **device_name) +{ + gchar *ret = NULL; + ICreateDevEnum *devices_enum = NULL; + IEnumMoniker *enum_moniker = NULL; + IMoniker *moniker = NULL; + HRESULT hres = S_FALSE; + ULONG fetched; + gboolean bfound = FALSE; + + hres = CoCreateInstance (CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, + IID_ICreateDevEnum, (void**)&devices_enum); + if(hres != S_OK) { + /*error*/ + goto clean; + } + + hres = devices_enum->CreateClassEnumerator (*device_category, + &enum_moniker, 0); + if (hres != S_OK || !enum_moniker) { + /*error*/ + goto clean; + } + + enum_moniker->Reset (); + + while(hres = enum_moniker->Next (1, &moniker, &fetched),hres == S_OK + && !bfound) { + IPropertyBag *property_bag = NULL; + hres = moniker->BindToStorage(NULL, NULL, IID_IPropertyBag, (void **)&property_bag); + if(SUCCEEDED(hres) && property_bag) { + VARIANT varFriendlyName; + VariantInit (&varFriendlyName); + + hres = property_bag->Read (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); + + if (!*device_name) { + *device_name = g_strdup (friendly_name); + } + + if (_stricmp(*device_name, friendly_name) == 0) { + WCHAR *wszDisplayName = NULL; + hres = moniker->GetDisplayName (NULL, NULL, &wszDisplayName); + if(hres == S_OK && wszDisplayName) { + ret = g_utf16_to_utf8((const gunichar2*)wszDisplayName, + wcslen(wszDisplayName), NULL, NULL, NULL); + CoTaskMemFree (wszDisplayName); + } + bfound = TRUE; + } + SysFreeString (varFriendlyName.bstrVal); + } + property_bag->Release (); + } + moniker->Release (); + } + +clean: + if (enum_moniker) { + enum_moniker->Release (); + } + + if (devices_enum) { + devices_enum->Release (); + } + + return ret; +} + +gboolean +gst_dshow_show_propertypage (IBaseFilter *base_filter) +{ + gboolean ret = FALSE; + ISpecifyPropertyPages *pProp = NULL; + HRESULT hres = base_filter->QueryInterface (IID_ISpecifyPropertyPages, (void **)&pProp); + if (SUCCEEDED(hres)) + { + /* Get the filter's name and IUnknown pointer.*/ + FILTER_INFO FilterInfo; + CAUUID caGUID; + IUnknown *pFilterUnk = NULL; + hres = base_filter->QueryFilterInfo (&FilterInfo); + base_filter->QueryInterface (IID_IUnknown, (void **)&pFilterUnk); + + /* Show the page. */ + pProp->GetPages (&caGUID); + pProp->Release (); + OleCreatePropertyFrame(GetDesktopWindow(), 0, 0, FilterInfo.achName, + 1, &pFilterUnk, caGUID.cElems, caGUID.pElems, 0, 0, NULL); + + pFilterUnk->Release (); + FilterInfo.pGraph->Release (); + CoTaskMemFree(caGUID.pElems); + } + return ret; +} diff --git a/sys/dshowsrcwrapper/gstdshow.h b/sys/dshowsrcwrapper/gstdshow.h new file mode 100755 index 00000000..ec360b32 --- /dev/null +++ b/sys/dshowsrcwrapper/gstdshow.h @@ -0,0 +1,76 @@ +/* GStreamer + * Copyright (C) 2007 Sebastien Moutte + * + * gstdshow.h: + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef _GSTDSHOW_ +#define _GSTDSHOW_ + +#ifdef __cplusplus +#include +#endif +#include +#include +#include +#include + +#include + +typedef struct _GstCapturePinMediaType +{ + AM_MEDIA_TYPE *mediatype; + IPin *capture_pin; +} GstCapturePinMediaType; + +#ifdef __cplusplus +extern "C" { +#endif + +/* register fake filters as COM object and as Direct Show filters in the registry */ +HRESULT gst_dshow_register_fakefilters (); + +/* free memory of the input pin mediatype */ +void gst_dshow_free_pin_mediatype (gpointer pt); + +/* free memory of the input dshow mediatype */ +void gst_dshow_free_mediatype (AM_MEDIA_TYPE *pmt); + +/* free the memory of all mediatypes of the input list if pin mediatype */ +void gst_dshow_free_pins_mediatypes (GList *mediatypes); + +/* get a pin from directshow filter */ +gboolean gst_dshow_get_pin_from_filter (IBaseFilter *filter, PIN_DIRECTION pindir, IPin **pin); + +/* find and return a filter according to the input and output types */ +gboolean gst_dshow_find_filter(CLSID input_majortype, CLSID input_subtype, + CLSID output_majortype, CLSID output_subtype, + gchar * prefered_filter_name, IBaseFilter **filter); + +/* get the dshow device path from device friendly name. +If friendly name is not set, it will return the first available device */ +gchar *gst_dshow_getdevice_from_devicename (const GUID *device_category, gchar **device_name); + +/* show the capture filter property page (generally used to setup the device). the page is modal*/ +gboolean gst_dshow_show_propertypage (IBaseFilter *base_filter); + +#ifdef __cplusplus +} +#endif + +#endif /* _GSTDSHOW_ */ \ No newline at end of file diff --git a/sys/dshowsrcwrapper/gstdshowaudiosrc.c b/sys/dshowsrcwrapper/gstdshowaudiosrc.c old mode 100644 new mode 100755 index d6edf47d..dcc4e385 --- a/sys/dshowsrcwrapper/gstdshowaudiosrc.c +++ b/sys/dshowsrcwrapper/gstdshowaudiosrc.c @@ -19,12 +19,12 @@ * Boston, MA 02111-1307, USA. */ -#include "gstdshowaudiosrc.h" - #ifdef HAVE_CONFIG_H #include "config.h" #endif +#include "gstdshowaudiosrc.h" + static const GstElementDetails gst_dshowaudiosrc_details = GST_ELEMENT_DETAILS ("Directshow audio capture source", "Source/Audio", @@ -451,7 +451,7 @@ gst_dshowaudiosrc_get_caps (GstBaseSrc * basesrc) &cbReturned); /* we only want capture pins */ - if (UuidCompare (&pin_category, &PIN_CATEGORY_CAPTURE, + if (UuidCompare (&pin_category, (UUID *) &PIN_CATEGORY_CAPTURE, &rpcstatus) == 0) { IAMStreamConfig *streamcaps = NULL; @@ -825,10 +825,10 @@ gst_dshowaudiosrc_getcaps_from_streamcaps (GstDshowAudioSrc * src, IPin * pin, if (!caps) caps = gst_caps_new_empty (); - if ((UuidCompare (&pin_mediatype->mediatype->subtype, &MEDIASUBTYPE_PCM, + if ((UuidCompare (&pin_mediatype->mediatype->subtype, (UUID *) &MEDIASUBTYPE_PCM, &rpcstatus) == 0 && rpcstatus == RPC_S_OK) && (UuidCompare (&pin_mediatype->mediatype->formattype, - &FORMAT_WaveFormatEx, &rpcstatus) == 0 + (UUID *) &FORMAT_WaveFormatEx, &rpcstatus) == 0 && rpcstatus == RPC_S_OK)) { WAVEFORMATEX *wavformat = (WAVEFORMATEX *) pin_mediatype->mediatype->pbFormat; diff --git a/sys/dshowsrcwrapper/gstdshowaudiosrc.h b/sys/dshowsrcwrapper/gstdshowaudiosrc.h old mode 100644 new mode 100755 index fb571015..b8147011 --- a/sys/dshowsrcwrapper/gstdshowaudiosrc.h +++ b/sys/dshowsrcwrapper/gstdshowaudiosrc.h @@ -27,7 +27,8 @@ #include #include -#include "gstdshowsrcwrapper.h" +#include "gstdshow.h" +#include "gstdshowinterface.h" G_BEGIN_DECLS #define GST_TYPE_DSHOWAUDIOSRC (gst_dshowaudiosrc_get_type()) diff --git a/sys/dshowsrcwrapper/gstdshowfakesink.cpp b/sys/dshowsrcwrapper/gstdshowfakesink.cpp new file mode 100755 index 00000000..afc0a5ef --- /dev/null +++ b/sys/dshowsrcwrapper/gstdshowfakesink.cpp @@ -0,0 +1,127 @@ +/* GStreamer + * Copyright (C) 2007 Sebastien Moutte + * + * gstdshowfakesink.cpp: + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "gstdshowfakesink.h" + + +CDshowFakeSink::CDshowFakeSink() + : m_hres(S_OK), CBaseRenderer(CLSID_DshowFakeSink, "DshowFakeSink", NULL, &m_hres) +{ + m_callback = NULL; +} + +CDshowFakeSink::~CDshowFakeSink() +{ + +} + +//Object creation. +CUnknown* WINAPI CDshowFakeSink::CreateInstance(LPUNKNOWN pUnk, HRESULT *pHr) +{ + CDshowFakeSink *pNewObject = new CDshowFakeSink(); + g_print ("CDshowFakeSink::CreateInstance\n"); + if (pNewObject == NULL) { + *pHr = E_OUTOFMEMORY; + } + return pNewObject; +} + +STDMETHODIMP CDshowFakeSink::QueryInterface(REFIID riid, void **ppvObject) +{ + if (riid == IID_IGstDshowInterface) { + *ppvObject = (IGstDshowInterface*) this; + AddRef(); + return S_OK; + } + else + return CBaseRenderer::QueryInterface (riid, ppvObject); +} + +ULONG STDMETHODCALLTYPE CDshowFakeSink::AddRef() +{ + return CBaseRenderer::AddRef(); +} + +ULONG STDMETHODCALLTYPE CDshowFakeSink::Release() +{ + return CBaseRenderer::Release(); +} + + +STDMETHODIMP CDshowFakeSink::gst_set_media_type (AM_MEDIA_TYPE *pmt) +{ + m_MediaType.Set (*pmt); + return S_OK; +} + +STDMETHODIMP CDshowFakeSink::gst_set_buffer_callback (push_buffer_func push, byte *data) +{ + m_callback = push; + m_data = data; + return S_OK; +} + +STDMETHODIMP CDshowFakeSink::gst_push_buffer (byte *buffer, __int64 start, __int64 stop, unsigned int size, bool discount) +{ + return E_NOTIMPL; +} + +STDMETHODIMP CDshowFakeSink::gst_flush () +{ + return E_NOTIMPL; +} + +STDMETHODIMP CDshowFakeSink::gst_set_sample_size(unsigned int size) +{ + return E_NOTIMPL; +} + +HRESULT CDshowFakeSink::CheckMediaType(const CMediaType *pmt) +{ + VIDEOINFOHEADER *p1; + VIDEOINFOHEADER *p2; + if(pmt != NULL) + { + p1 = (VIDEOINFOHEADER *)pmt->Format(); + p2 = (VIDEOINFOHEADER *)m_MediaType.Format(); + if (*pmt == m_MediaType) + return S_OK; + } + + return S_FALSE; +} + +HRESULT CDshowFakeSink::DoRenderSample(IMediaSample *pMediaSample) +{ + if(pMediaSample && m_callback) + { + BYTE *pBuffer = NULL; + LONGLONG lStart = 0, lStop = 0; + pMediaSample->GetPointer(&pBuffer); + long size = pMediaSample->GetActualDataLength(); + pMediaSample->GetTime(&lStart, &lStop); + lStart*=100; + lStop*=100; + m_callback(pBuffer, size, m_data, lStart, lStop); + } + + return S_OK; +} diff --git a/sys/dshowsrcwrapper/gstdshowfakesink.h b/sys/dshowsrcwrapper/gstdshowfakesink.h new file mode 100755 index 00000000..7f419b27 --- /dev/null +++ b/sys/dshowsrcwrapper/gstdshowfakesink.h @@ -0,0 +1,50 @@ +/* GStreamer + * Copyright (C) 2007 Sebastien Moutte + * + * gstdshowfakesink.h: + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "gstdshowinterface.h" + +class CDshowFakeSink : public CBaseRenderer, + public IGstDshowInterface +{ +public: + CDshowFakeSink (); + virtual ~CDshowFakeSink (); + + static CUnknown * WINAPI CreateInstance (LPUNKNOWN pUnk, HRESULT *pHr); + + virtual HRESULT CheckMediaType (const CMediaType *pmt); + virtual HRESULT DoRenderSample (IMediaSample *pMediaSample); + + STDMETHOD (QueryInterface)(REFIID riid, void **ppvObject); + ULONG STDMETHODCALLTYPE AddRef(); + ULONG STDMETHODCALLTYPE Release(); + STDMETHOD (gst_set_media_type) (AM_MEDIA_TYPE *pmt); + STDMETHOD (gst_set_buffer_callback) (push_buffer_func push, byte *data); + STDMETHOD (gst_push_buffer) (byte *buffer, __int64 start, __int64 stop, unsigned int size, bool discount); + STDMETHOD (gst_flush) (); + STDMETHOD (gst_set_sample_size) (unsigned int size); + +protected: + HRESULT m_hres; + CMediaType m_MediaType; + push_buffer_func m_callback; + byte *m_data; +}; \ No newline at end of file diff --git a/sys/dshowsrcwrapper/gstdshowinterface.h b/sys/dshowsrcwrapper/gstdshowinterface.h new file mode 100755 index 00000000..68328b95 --- /dev/null +++ b/sys/dshowsrcwrapper/gstdshowinterface.h @@ -0,0 +1,163 @@ +/* GStreamer + * Copyright (C) 2007 Sebastien Moutte + * + * gstdshowinterface.h: + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __GST_DHOW_INTERFACE_H__ +#define __GST_DHOW_INTERFACE_H__ + +#include "gstdshow.h" + +#ifdef __cplusplus +typedef bool (*push_buffer_func) (byte *buffer, long size, byte *src_object, UINT64 start, UINT64 stop); +#endif + +/* verify that the version is high enough to compile this file*/ +#ifndef __REQUIRED_RPCNDR_H_VERSION__ +#define __REQUIRED_RPCNDR_H_VERSION__ 440 +#endif + +#include "rpc.h" +#include "rpcndr.h" + +#ifndef __RPCNDR_H_VERSION__ +#error this stub requires an updated version of +#endif // __RPCNDR_H_VERSION__ + +#ifndef COM_NO_WINDOWS_H +#include "windows.h" +#include "ole2.h" +#endif /*COM_NO_WINDOWS_H*/ + +//{6A780808-9725-4d0b-8695-A4DD8D210773} +static const GUID CLSID_DshowFakeSink + = { 0x6a780808, 0x9725, 0x4d0b, { 0x86, 0x95, 0xa4, 0xdd, 0x8d, 0x21, 0x7, 0x73 } }; + +// {FC36764C-6CD4-4C73-900F-3F40BF3F191A} +static const GUID IID_IGstDshowInterface = + { 0xfc36764c, 0x6cd4, 0x4c73, { 0x90, 0xf, 0x3f, 0x40, 0xbf, 0x3f, 0x19, 0x1a } }; + +#define CLSID_DSHOWFAKESINK_STRING "{6A780808-9725-4d0b-8695-A4DD8D210773}" + +typedef interface IGstDshowInterface IGstDshowInterface; + +/* header files for imported files */ +#include "oaidl.h" +#include "ocidl.h" + +void __RPC_FAR * __RPC_USER MIDL_user_allocate(size_t); +void __RPC_USER MIDL_user_free( void __RPC_FAR * ); + +#ifndef __IGstDshowInterface_INTERFACE_DEFINED__ +#define __IGstDshowInterface_INTERFACE_DEFINED__ + +#if defined(__cplusplus) && !defined(CINTERFACE) + + MIDL_INTERFACE("542C0A24-8BD1-46cb-AA57-3E46D006D2F3") + IGstDshowInterface : public IUnknown + { + public: + virtual HRESULT STDMETHODCALLTYPE gst_set_media_type( + AM_MEDIA_TYPE __RPC_FAR *pmt) = 0; + + virtual HRESULT STDMETHODCALLTYPE gst_set_buffer_callback( + push_buffer_func push, byte *data) = 0; + + virtual HRESULT STDMETHODCALLTYPE gst_push_buffer( + byte *buffer, __int64 start, __int64 stop, unsigned int size, bool discount) = 0; + + virtual HRESULT STDMETHODCALLTYPE gst_flush() = 0; + + virtual HRESULT STDMETHODCALLTYPE gst_set_sample_size(unsigned int size) = 0; + }; + +#else /* C style interface */ + + typedef struct IGstDshowInterfaceVtbl + { + BEGIN_INTERFACE + + HRESULT ( STDMETHODCALLTYPE __RPC_FAR *QueryInterface )( + IGstDshowInterface __RPC_FAR * This, + REFIID riid, + void __RPC_FAR *__RPC_FAR *ppvObject); + + ULONG ( STDMETHODCALLTYPE __RPC_FAR *AddRef )( + IGstDshowInterface __RPC_FAR * This); + + ULONG ( STDMETHODCALLTYPE __RPC_FAR *Release )( + IGstDshowInterface __RPC_FAR * This); + + HRESULT (STDMETHODCALLTYPE *gst_set_media_type )( + IGstDshowInterface __RPC_FAR * This, + AM_MEDIA_TYPE *pmt); + + HRESULT (STDMETHODCALLTYPE *gst_set_buffer_callback) ( + IGstDshowInterface __RPC_FAR * This, + byte * push, byte *data); + + HRESULT (STDMETHODCALLTYPE *gst_push_buffer) ( + IGstDshowInterface __RPC_FAR * This, + byte *buffer, __int64 start, __int64 stop, + unsigned int size, boolean discount); + + HRESULT (STDMETHODCALLTYPE *gst_flush) ( + IGstDshowInterface __RPC_FAR * This); + + HRESULT (STDMETHODCALLTYPE *gst_set_sample_size) ( + IGstDshowInterface __RPC_FAR * This, + unsigned int size); + + END_INTERFACE + } IGstDshowInterfaceVtbl; + + interface IGstDshowInterface + { + CONST_VTBL struct IGstDshowInterfaceVtbl __RPC_FAR *lpVtbl; + }; + +#define IGstDshowInterface_QueryInterface(This,riid,ppvObject) \ + (This)->lpVtbl -> QueryInterface(This,riid,ppvObject) + +#define IGstDshowInterface_AddRef(This) \ + (This)->lpVtbl -> AddRef(This) + +#define IGstDshowInterface_Release(This) \ + (This)->lpVtbl -> Release(This) + +#define IGstDshowInterface_gst_set_media_type(This, mediatype) \ + (This)->lpVtbl -> gst_set_media_type(This, mediatype) + +#define IGstDshowInterface_gst_set_buffer_callback(This, push, data) \ + (This)->lpVtbl -> gst_set_buffer_callback(This, push, data) + +#define IGstDshowInterface_gst_push_buffer(This, buffer, start, stop, size, discount) \ + (This)->lpVtbl -> gst_push_buffer(This, buffer, start, stop, size, discount) + +#define IGstDshowInterface_gst_flush(This) \ + (This)->lpVtbl -> gst_flush(This) + +#define IGstDshowInterface_gst_set_sample_size(This, size) \ + (This)->lpVtbl -> gst_set_sample_size(This, size) + +#endif /* C style interface */ + +#endif /* __IGstDshowInterface_INTERFACE_DEFINED__ */ + +#endif /* __GST_DSHOW_INTERFACE_H__ */ \ No newline at end of file diff --git a/sys/dshowsrcwrapper/gstdshowsrcwrapper.c b/sys/dshowsrcwrapper/gstdshowsrcwrapper.c deleted file mode 100644 index 803fd807..00000000 --- a/sys/dshowsrcwrapper/gstdshowsrcwrapper.c +++ /dev/null @@ -1,59 +0,0 @@ -/* GStreamer - * Copyright (C) 2007 Sebastien Moutte - * - * gstdshowsrcwrapper.c: - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "gstdshowaudiosrc.h" -#include "gstdshowvideosrc.h" - -const GUID CLSID_GstreamerSrcFilter - = - { 0x6a780808, 0x9725, 0x4d0b, {0x86, 0x95, 0xa4, 0xdd, 0x8d, 0x21, 0x7, - 0x73} }; - -const GUID IID_IGstSrcInterface = - { 0x542c0a24, 0x8bd1, 0x46cb, {0xaa, 0x57, 0x3e, 0x46, 0xd0, 0x6, 0xd2, - 0xf3} }; - - -static gboolean -plugin_init (GstPlugin * plugin) -{ - /* register fake filters */ - gst_dshow_register_fakefilters (); - - if (!gst_element_register (plugin, "dshowaudiosrc", - GST_RANK_NONE, - GST_TYPE_DSHOWAUDIOSRC) || - !gst_element_register (plugin, "dshowvideosrc", - GST_RANK_NONE, GST_TYPE_DSHOWVIDEOSRC)) - return FALSE; - - return TRUE; -} - -GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, - GST_VERSION_MINOR, - "dshowsrcwrapper", - "DirectShow sources wrapper plugin", - plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN) diff --git a/sys/dshowsrcwrapper/gstdshowsrcwrapper.cpp b/sys/dshowsrcwrapper/gstdshowsrcwrapper.cpp new file mode 100755 index 00000000..1e9a8aa2 --- /dev/null +++ b/sys/dshowsrcwrapper/gstdshowsrcwrapper.cpp @@ -0,0 +1,66 @@ +/* GStreamer + * Copyright (C) 2007 Sebastien Moutte + * + * gstdshowsrcwrapper.c: + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gstdshowaudiosrc.h" +#include "gstdshowvideosrc.h" + +const GUID CLSID_GstreamerSrcFilter + = + { 0x6a780808, 0x9725, 0x4d0b, {0x86, 0x95, 0xa4, 0xdd, 0x8d, 0x21, 0x7, + 0x73} }; + +const GUID IID_IGstSrcInterface = + { 0x542c0a24, 0x8bd1, 0x46cb, {0xaa, 0x57, 0x3e, 0x46, 0xd0, 0x6, 0xd2, + 0xf3} }; + +static gboolean +plugin_init (GstPlugin * plugin) +{ + /* register fake filters */ + HRESULT hr = gst_dshow_register_fakefilters (); + if (FAILED (hr)) { + g_warning ("failed to register directshow fakesink filter: 0x%x\n", hr); + return FALSE; + } + + if (!gst_element_register (plugin, "dshowaudiosrc", + GST_RANK_NONE, + GST_TYPE_DSHOWAUDIOSRC) || + !gst_element_register (plugin, "dshowvideosrc", + GST_RANK_NONE, GST_TYPE_DSHOWVIDEOSRC)) + return FALSE; + + return TRUE; +} + +extern "C" { + +GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, + GST_VERSION_MINOR, + "dshowsrcwrapper", + "DirectShow sources wrapper plugin", + plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN) + +} diff --git a/sys/dshowsrcwrapper/gstdshowsrcwrapper.h b/sys/dshowsrcwrapper/gstdshowsrcwrapper.h deleted file mode 100644 index d94d49f7..00000000 --- a/sys/dshowsrcwrapper/gstdshowsrcwrapper.h +++ /dev/null @@ -1,34 +0,0 @@ -/* GStreamer - * Copyright (C) 2007 Sebastien Moutte - * - * gstdshowsrcwrapper.h: - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#ifndef __GST_DSHOW_H__ -#define __GST_DSHOW_H__ - -#include -#include -#include -#include - -#include - -#pragma warning( disable : 4090 4024) - -#endif /* __GST_DSHOW_H__ */ diff --git a/sys/dshowsrcwrapper/gstdshowvideosrc.c b/sys/dshowsrcwrapper/gstdshowvideosrc.c old mode 100644 new mode 100755 index 4a37778c..a54751b7 --- a/sys/dshowsrcwrapper/gstdshowvideosrc.c +++ b/sys/dshowsrcwrapper/gstdshowvideosrc.c @@ -19,12 +19,12 @@ * Boston, MA 02111-1307, USA. */ -#include "gstdshowvideosrc.h" - #ifdef HAVE_CONFIG_H #include "config.h" #endif +#include "gstdshowvideosrc.h" + static const GstElementDetails gst_dshowvideosrc_details = GST_ELEMENT_DETAILS ("DirectShow video capture source", "Source/Video", @@ -520,7 +520,7 @@ gst_dshowvideosrc_get_caps (GstBaseSrc * basesrc) &cbReturned); /* we only want capture pins */ - if (UuidCompare (&pin_category, &PIN_CATEGORY_CAPTURE, + if (UuidCompare (&pin_category, (UUID *) &PIN_CATEGORY_CAPTURE, &rpcstatus) == 0) { IAMStreamConfig *streamcaps = NULL; @@ -625,7 +625,7 @@ gst_dshowvideosrc_start (GstBaseSrc * bsrc) &IID_IBaseFilter, (LPVOID *) & src->dshow_fakesink); if (hres != S_OK || !src->dshow_fakesink) { GST_CAT_ERROR (dshowvideosrc_debug, - "Can't create an instance of our dshow fakesink filter (error=%d)", + "Can't create an instance of our dshow fakesink filter (error=0x%x)", hres); goto error; } @@ -898,10 +898,10 @@ gst_dshowvideosrc_getcaps_from_streamcaps (GstDshowVideoSrc * src, IPin * pin, caps = gst_caps_new_empty (); /* I420 */ - if ((UuidCompare (&pin_mediatype->mediatype->subtype, &MEDIASUBTYPE_I420, + if ((UuidCompare (&pin_mediatype->mediatype->subtype, (UUID *) &MEDIASUBTYPE_I420, &rpcstatus) == 0 && rpcstatus == RPC_S_OK) && (UuidCompare (&pin_mediatype->mediatype->formattype, - &FORMAT_VideoInfo, &rpcstatus) == 0 + (UUID *) &FORMAT_VideoInfo, &rpcstatus) == 0 && rpcstatus == RPC_S_OK)) { video_info = (VIDEOINFOHEADER *) pin_mediatype->mediatype->pbFormat; @@ -923,10 +923,10 @@ gst_dshowvideosrc_getcaps_from_streamcaps (GstDshowVideoSrc * src, IPin * pin, } /* RGB24 */ - if ((UuidCompare (&pin_mediatype->mediatype->subtype, &MEDIASUBTYPE_RGB24, + if ((UuidCompare (&pin_mediatype->mediatype->subtype, (UUID *) &MEDIASUBTYPE_RGB24, &rpcstatus) == 0 && rpcstatus == RPC_S_OK) && (UuidCompare (&pin_mediatype->mediatype->formattype, - &FORMAT_VideoInfo, &rpcstatus) == 0 + (UUID *) &FORMAT_VideoInfo, &rpcstatus) == 0 && rpcstatus == RPC_S_OK)) { video_info = (VIDEOINFOHEADER *) pin_mediatype->mediatype->pbFormat; @@ -952,10 +952,10 @@ gst_dshowvideosrc_getcaps_from_streamcaps (GstDshowVideoSrc * src, IPin * pin, } /* DVSD */ - if ((UuidCompare (&pin_mediatype->mediatype->subtype, &MEDIASUBTYPE_dvsd, + if ((UuidCompare (&pin_mediatype->mediatype->subtype, (UUID *) &MEDIASUBTYPE_dvsd, &rpcstatus) == 0 && rpcstatus == RPC_S_OK) && (UuidCompare (&pin_mediatype->mediatype->formattype, - &FORMAT_VideoInfo, &rpcstatus) == 0 + (UUID *) &FORMAT_VideoInfo, &rpcstatus) == 0 && rpcstatus == RPC_S_OK)) { video_info = (VIDEOINFOHEADER *) pin_mediatype->mediatype->pbFormat; @@ -978,10 +978,10 @@ gst_dshowvideosrc_getcaps_from_streamcaps (GstDshowVideoSrc * src, IPin * pin, } /* DV stream */ - if ((UuidCompare (&pin_mediatype->mediatype->subtype, &MEDIASUBTYPE_dvsd, + if ((UuidCompare (&pin_mediatype->mediatype->subtype, (UUID *) &MEDIASUBTYPE_dvsd, &rpcstatus) == 0 && rpcstatus == RPC_S_OK) && (UuidCompare (&pin_mediatype->mediatype->formattype, - &FORMAT_DvInfo, &rpcstatus) == 0 && rpcstatus == RPC_S_OK)) { + (UUID *) &FORMAT_DvInfo, &rpcstatus) == 0 && rpcstatus == RPC_S_OK)) { mediacaps = gst_caps_new_simple ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE, NULL); diff --git a/sys/dshowsrcwrapper/gstdshowvideosrc.h b/sys/dshowsrcwrapper/gstdshowvideosrc.h old mode 100644 new mode 100755 index 8bc52fa9..b7bfbb19 --- a/sys/dshowsrcwrapper/gstdshowvideosrc.h +++ b/sys/dshowsrcwrapper/gstdshowvideosrc.h @@ -22,11 +22,13 @@ #ifndef __GST_DSHOWVIDEOSRC_H__ #define __GST_DSHOWVIDEOSRC_H__ +#include #include #include #include -#include "gstdshowsrcwrapper.h" +#include "gstdshow.h" +#include "gstdshowinterface.h" // 30323449-0000-0010-8000-00AA00389B71 MEDIASUBTYPE_I420 DEFINE_GUID(MEDIASUBTYPE_I420, 0x30323449, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71); diff --git a/sys/dshowsrcwrapper/libgstdshow.def b/sys/dshowsrcwrapper/libgstdshow.def new file mode 100755 index 00000000..ee8586c9 --- /dev/null +++ b/sys/dshowsrcwrapper/libgstdshow.def @@ -0,0 +1,8 @@ +EXPORTS + DllMain PRIVATE + DllGetClassObject PRIVATE + DllCanUnloadNow PRIVATE + DllRegisterServer PRIVATE + DllUnregisterServer PRIVATE + + -- cgit v1.2.1 From 779ac0d98e77253daa6f09f1b73b67cad6a25447 Mon Sep 17 00:00:00 2001 From: Julien Isorce Date: Wed, 22 Jul 2009 11:01:49 +0200 Subject: dshowvideosrc: converts code to C++ --- sys/dshowsrcwrapper/gstdshowvideosrc.c | 1064 ------------------------------ sys/dshowsrcwrapper/gstdshowvideosrc.cpp | 1047 +++++++++++++++++++++++++++++ 2 files changed, 1047 insertions(+), 1064 deletions(-) delete mode 100755 sys/dshowsrcwrapper/gstdshowvideosrc.c create mode 100755 sys/dshowsrcwrapper/gstdshowvideosrc.cpp (limited to 'sys/dshowsrcwrapper') diff --git a/sys/dshowsrcwrapper/gstdshowvideosrc.c b/sys/dshowsrcwrapper/gstdshowvideosrc.c deleted file mode 100755 index a54751b7..00000000 --- a/sys/dshowsrcwrapper/gstdshowvideosrc.c +++ /dev/null @@ -1,1064 +0,0 @@ -/* GStreamer - * Copyright (C) 2007 Sebastien Moutte - * - * gstdshowvideosrc.c: - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "gstdshowvideosrc.h" - -static const GstElementDetails gst_dshowvideosrc_details = -GST_ELEMENT_DETAILS ("DirectShow video capture source", - "Source/Video", - "Receive data from a directshow video capture graph", - "Sebastien Moutte "); - -GST_DEBUG_CATEGORY_STATIC (dshowvideosrc_debug); -#define GST_CAT_DEFAULT dshowvideosrc_debug - -const GUID MEDIASUBTYPE_I420 - = { 0x30323449, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, - 0x71} -}; - -static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", - GST_PAD_SRC, - GST_PAD_ALWAYS, - GST_STATIC_CAPS ("video/x-raw-rgb," - "bpp = (int) 24," - "depth = (int) 24," - "width = (int) [ 1, MAX ]," - "height = (int) [ 1, MAX ]," - "framerate = (fraction) [ 0, MAX ];" - "video/x-dv," - "systemstream = (boolean) FALSE," - "width = (int) [ 1, MAX ]," - "height = (int) [ 1, MAX ]," - "framerate = (fraction) [ 0, MAX ]," - "format = (fourcc) dvsd;" - "video/x-dv," - "systemstream = (boolean) TRUE;" - "video/x-raw-yuv," - "width = (int) [ 1, MAX ]," - "height = (int) [ 1, MAX ]," - "framerate = (fraction) [ 0, MAX ]," "format = (fourcc) I420") - ); - -static void gst_dshowvideosrc_init_interfaces (GType type); - -GST_BOILERPLATE_FULL (GstDshowVideoSrc, gst_dshowvideosrc, GstPushSrc, - GST_TYPE_PUSH_SRC, gst_dshowvideosrc_init_interfaces); - -enum -{ - PROP_0, - PROP_DEVICE, - PROP_DEVICE_NAME -}; - -static void gst_dshowvideosrc_probe_interface_init (GstPropertyProbeInterface * - iface); -static const GList *gst_dshowvideosrc_probe_get_properties (GstPropertyProbe * - probe); -static GValueArray *gst_dshowvideosrc_probe_get_values (GstPropertyProbe * - probe, guint prop_id, const GParamSpec * pspec); -static GValueArray *gst_dshowvideosrc_get_device_name_values (GstDshowVideoSrc * - src); -static gboolean gst_dshowvideosrc_probe_needs_probe (GstPropertyProbe * probe, - guint prop_id, const GParamSpec * pspec); -static void gst_dshowvideosrc_probe_probe_property (GstPropertyProbe * probe, - guint prop_id, const GParamSpec * pspec); - - -static void gst_dshowvideosrc_dispose (GObject * gobject); -static void gst_dshowvideosrc_set_property (GObject * object, guint prop_id, - const GValue * value, GParamSpec * pspec); -static void gst_dshowvideosrc_get_property (GObject * object, guint prop_id, - GValue * value, GParamSpec * pspec); -static GstCaps *gst_dshowvideosrc_get_caps (GstBaseSrc * src); -static GstStateChangeReturn gst_dshowvideosrc_change_state (GstElement * - element, GstStateChange transition); - - -static gboolean gst_dshowvideosrc_start (GstBaseSrc * bsrc); -static gboolean gst_dshowvideosrc_stop (GstBaseSrc * bsrc); -static gboolean gst_dshowvideosrc_unlock (GstBaseSrc * bsrc); -static gboolean gst_dshowvideosrc_unlock_stop (GstBaseSrc * bsrc); -static gboolean gst_dshowvideosrc_set_caps (GstBaseSrc * bsrc, GstCaps * caps); -static GstCaps *gst_dshowvideosrc_get_caps (GstBaseSrc * bsrc); -static GstFlowReturn gst_dshowvideosrc_create (GstPushSrc * psrc, - GstBuffer ** buf); - -/*utils*/ -static GstCaps *gst_dshowvideosrc_getcaps_from_streamcaps (GstDshowVideoSrc * - src, IPin * pin, IAMStreamConfig * streamcaps); -static gboolean gst_dshowvideosrc_push_buffer (byte * buffer, long size, - byte * src_object, UINT64 start, UINT64 stop); - -static void -gst_dshowvideosrc_init_interfaces (GType type) -{ - static const GInterfaceInfo dshowvideosrc_info = { - (GInterfaceInitFunc) gst_dshowvideosrc_probe_interface_init, - NULL, - NULL, - }; - - g_type_add_interface_static (type, - GST_TYPE_PROPERTY_PROBE, &dshowvideosrc_info); -} - -static void -gst_dshowvideosrc_probe_interface_init (GstPropertyProbeInterface * iface) -{ - iface->get_properties = gst_dshowvideosrc_probe_get_properties; - iface->needs_probe = gst_dshowvideosrc_probe_needs_probe; - iface->probe_property = gst_dshowvideosrc_probe_probe_property; - iface->get_values = gst_dshowvideosrc_probe_get_values; -} - -static void -gst_dshowvideosrc_base_init (gpointer klass) -{ - GstElementClass *element_class = GST_ELEMENT_CLASS (klass); - - gst_element_class_add_pad_template (element_class, - gst_static_pad_template_get (&src_template)); - - gst_element_class_set_details (element_class, &gst_dshowvideosrc_details); -} - -static void -gst_dshowvideosrc_class_init (GstDshowVideoSrcClass * klass) -{ - GObjectClass *gobject_class; - GstElementClass *gstelement_class; - GstBaseSrcClass *gstbasesrc_class; - GstPushSrcClass *gstpushsrc_class; - - gobject_class = (GObjectClass *) klass; - gstelement_class = (GstElementClass *) klass; - gstbasesrc_class = (GstBaseSrcClass *) klass; - gstpushsrc_class = (GstPushSrcClass *) klass; - - gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_dshowvideosrc_dispose); - gobject_class->set_property = - GST_DEBUG_FUNCPTR (gst_dshowvideosrc_set_property); - gobject_class->get_property = - GST_DEBUG_FUNCPTR (gst_dshowvideosrc_get_property); - - gstelement_class->change_state = - GST_DEBUG_FUNCPTR (gst_dshowvideosrc_change_state); - - gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_dshowvideosrc_get_caps); - gstbasesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_dshowvideosrc_set_caps); - gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_dshowvideosrc_start); - gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_dshowvideosrc_stop); - gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_dshowvideosrc_unlock); - gstbasesrc_class->unlock_stop = - GST_DEBUG_FUNCPTR (gst_dshowvideosrc_unlock_stop); - - gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_dshowvideosrc_create); - - g_object_class_install_property - (gobject_class, PROP_DEVICE, - g_param_spec_string ("device", "Device", - "Directshow device path (@..classID/name)", NULL, G_PARAM_READWRITE)); - - g_object_class_install_property - (gobject_class, PROP_DEVICE_NAME, - g_param_spec_string ("device-name", "Device name", - "Human-readable name of the sound device", NULL, G_PARAM_READWRITE)); - - GST_DEBUG_CATEGORY_INIT (dshowvideosrc_debug, "dshowvideosrc", 0, - "Directshow video source"); - -} - -static void -gst_dshowvideosrc_init (GstDshowVideoSrc * src, GstDshowVideoSrcClass * klass) -{ - src->device = NULL; - src->device_name = NULL; - src->video_cap_filter = NULL; - src->dshow_fakesink = NULL; - src->media_filter = NULL; - src->filter_graph = NULL; - src->caps = NULL; - src->pins_mediatypes = NULL; - src->is_rgb = FALSE; - - src->buffer_cond = g_cond_new (); - src->buffer_mutex = g_mutex_new (); - src->buffer = NULL; - src->stop_requested = FALSE; - - CoInitializeEx (NULL, COINIT_MULTITHREADED); - - gst_base_src_set_live (GST_BASE_SRC (src), TRUE); -} - -static void -gst_dshowvideosrc_dispose (GObject * gobject) -{ - GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (gobject); - - if (src->device) { - g_free (src->device); - src->device = NULL; - } - - if (src->device_name) { - g_free (src->device_name); - src->device_name = NULL; - } - - if (src->caps) { - gst_caps_unref (src->caps); - src->caps = NULL; - } - - if (src->pins_mediatypes) { - gst_dshow_free_pins_mediatypes (src->pins_mediatypes); - src->pins_mediatypes = NULL; - } - - /* clean dshow */ - if (src->video_cap_filter) { - IBaseFilter_Release (src->video_cap_filter); - src->video_cap_filter = NULL; - } - - if (src->buffer_mutex) { - g_mutex_free (src->buffer_mutex); - src->buffer_mutex = NULL; - } - - if (src->buffer_cond) { - g_cond_free (src->buffer_cond); - src->buffer_cond = NULL; - } - - if (src->buffer) { - gst_buffer_unref (src->buffer); - src->buffer = NULL; - } - - CoUninitialize (); - - G_OBJECT_CLASS (parent_class)->dispose (gobject); -} - -static gboolean -gst_dshowvideosrc_probe_needs_probe (GstPropertyProbe * probe, - guint prop_id, const GParamSpec * pspec) -{ - static gboolean init = FALSE; - gboolean ret = FALSE; - - if (!init) { - ret = TRUE; - init = TRUE; - } - - return ret; -} - -static void -gst_dshowvideosrc_probe_probe_property (GstPropertyProbe * probe, - guint prop_id, const GParamSpec * pspec) -{ - GObjectClass *klass = G_OBJECT_GET_CLASS (probe); - - switch (prop_id) { - case PROP_DEVICE_NAME: - //gst_v4l_class_probe_devices (klass, FALSE); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec); - break; - } -} - -static const GList * -gst_dshowvideosrc_probe_get_properties (GstPropertyProbe * probe) -{ - GObjectClass *klass = G_OBJECT_GET_CLASS (probe); - static GList *props = NULL; - - if (!props) { - GParamSpec *pspec; - - pspec = g_object_class_find_property (klass, "device-name"); - props = g_list_append (props, pspec); - } - - return props; -} - -static GValueArray * -gst_dshowvideosrc_get_device_name_values (GstDshowVideoSrc * 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 (dshowvideosrc_debug, - "Can't create an instance of the system device enumerator (error=%d)", - hres); - array = NULL; - goto clean; - } - - hres = - ICreateDevEnum_CreateClassEnumerator (devices_enum, - &CLSID_VideoInputDeviceCategory, &moniker_enum, 0); - if (hres != S_OK || !moniker_enum) { - GST_CAT_ERROR (dshowvideosrc_debug, - "Can't get enumeration of video 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 GValueArray * -gst_dshowvideosrc_probe_get_values (GstPropertyProbe * probe, - guint prop_id, const GParamSpec * pspec) -{ - GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (probe); - GValueArray *array = NULL; - - switch (prop_id) { - case PROP_DEVICE_NAME: - array = gst_dshowvideosrc_get_device_name_values (src); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec); - break; - } - - return array; -} - -static void -gst_dshowvideosrc_set_property (GObject * object, guint prop_id, - const GValue * value, GParamSpec * pspec) -{ - GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (object); - - switch (prop_id) { - case PROP_DEVICE: - { - if (src->device) { - g_free (src->device); - src->device = NULL; - } - if (g_value_get_string (value)) { - src->device = g_strdup (g_value_get_string (value)); - } - break; - } - case PROP_DEVICE_NAME: - { - if (src->device_name) { - g_free (src->device_name); - src->device_name = NULL; - } - if (g_value_get_string (value)) { - src->device_name = g_strdup (g_value_get_string (value)); - } - break; - } - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -static void -gst_dshowvideosrc_get_property (GObject * object, guint prop_id, - GValue * value, GParamSpec * pspec) -{ - -} - -static GstCaps * -gst_dshowvideosrc_get_caps (GstBaseSrc * basesrc) -{ - HRESULT hres = S_OK; - IBindCtx *lpbc = NULL; - IMoniker *videom; - DWORD dwEaten; - GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (basesrc); - gunichar2 *unidevice = NULL; - - if (src->device) { - g_free (src->device); - src->device = NULL; - } - - src->device = - gst_dshow_getdevice_from_devicename (&CLSID_VideoInputDeviceCategory, - &src->device_name); - if (!src->device) { - GST_CAT_ERROR (dshowvideosrc_debug, "No video device found."); - return NULL; - } - unidevice = - g_utf8_to_utf16 (src->device, strlen (src->device), NULL, NULL, NULL); - - if (!src->video_cap_filter) { - hres = CreateBindCtx (0, &lpbc); - if (SUCCEEDED (hres)) { - hres = MkParseDisplayName (lpbc, unidevice, &dwEaten, &videom); - if (SUCCEEDED (hres)) { - hres = - IMoniker_BindToObject (videom, lpbc, NULL, &IID_IBaseFilter, - &src->video_cap_filter); - IMoniker_Release (videom); - } - IBindCtx_Release (lpbc); - } - } - - if (!src->caps) { - src->caps = gst_caps_new_empty (); - } - - if (src->video_cap_filter && gst_caps_is_empty (src->caps)) { - /* get the capture pins supported types */ - IPin *capture_pin = NULL; - IEnumPins *enumpins = NULL; - HRESULT hres; - - hres = IBaseFilter_EnumPins (src->video_cap_filter, &enumpins); - if (SUCCEEDED (hres)) { - while (IEnumPins_Next (enumpins, 1, &capture_pin, NULL) == S_OK) { - IKsPropertySet *pKs = NULL; - - hres = - IPin_QueryInterface (capture_pin, &IID_IKsPropertySet, - (void **) &pKs); - if (SUCCEEDED (hres) && pKs) { - DWORD cbReturned; - GUID pin_category; - RPC_STATUS rpcstatus; - - hres = - IKsPropertySet_Get (pKs, &ROPSETID_Pin, - AMPROPERTY_PIN_CATEGORY, NULL, 0, &pin_category, sizeof (GUID), - &cbReturned); - - /* we only want capture pins */ - if (UuidCompare (&pin_category, (UUID *) &PIN_CATEGORY_CAPTURE, - &rpcstatus) == 0) { - IAMStreamConfig *streamcaps = NULL; - - if (SUCCEEDED (IPin_QueryInterface (capture_pin, - &IID_IAMStreamConfig, (void **) &streamcaps))) { - GstCaps *caps = - gst_dshowvideosrc_getcaps_from_streamcaps (src, capture_pin, - streamcaps); - - if (caps) { - gst_caps_append (src->caps, caps); - } - IAMStreamConfig_Release (streamcaps); - } - } - - IKsPropertySet_Release (pKs); - } - - IPin_Release (capture_pin); - } - IEnumPins_Release (enumpins); - } - } - - if (unidevice) { - g_free (unidevice); - } - - if (src->caps) { - GST_CAT_LOG (dshowvideosrc_debug, "getcaps returned %s", - gst_caps_to_string (src->caps)); - return gst_caps_ref (src->caps); - } - - return NULL; -} - -static GstStateChangeReturn -gst_dshowvideosrc_change_state (GstElement * element, GstStateChange transition) -{ - HRESULT hres = S_FALSE; - IAMVfwCaptureDialogs *dialog = NULL; - GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (element); - - switch (transition) { - case GST_STATE_CHANGE_NULL_TO_READY: - break; - case GST_STATE_CHANGE_READY_TO_PAUSED: - break; - case GST_STATE_CHANGE_PAUSED_TO_PLAYING: - if (src->media_filter) - hres = IMediaFilter_Run (src->media_filter, 0); - if (hres != S_OK) { - GST_CAT_ERROR (dshowvideosrc_debug, - "Can't RUN the directshow capture graph (error=%d)", hres); - return GST_STATE_CHANGE_FAILURE; - } - break; - case GST_STATE_CHANGE_PLAYING_TO_PAUSED: - if (src->media_filter) - hres = IMediaFilter_Stop (src->media_filter); - if (hres != S_OK) { - GST_CAT_ERROR (dshowvideosrc_debug, - "Can't STOP the directshow capture graph (error=%d)", hres); - return GST_STATE_CHANGE_FAILURE; - } - break; - case GST_STATE_CHANGE_PAUSED_TO_READY: - break; - case GST_STATE_CHANGE_READY_TO_NULL: - break; - } - - return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); -} - -static gboolean -gst_dshowvideosrc_start (GstBaseSrc * bsrc) -{ - HRESULT hres = S_FALSE; - GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (bsrc); - - hres = CoCreateInstance (&CLSID_FilterGraph, NULL, CLSCTX_INPROC, - &IID_IFilterGraph, (LPVOID *) & src->filter_graph); - if (hres != S_OK || !src->filter_graph) { - GST_CAT_ERROR (dshowvideosrc_debug, - "Can't create an instance of the dshow graph manager (error=%d)", hres); - goto error; - } - - hres = IFilterGraph_QueryInterface (src->filter_graph, &IID_IMediaFilter, - (void **) &src->media_filter); - if (hres != S_OK || !src->media_filter) { - GST_CAT_ERROR (dshowvideosrc_debug, - "Can't get IMediacontrol interface from the graph manager (error=%d)", - hres); - goto error; - } - - hres = CoCreateInstance (&CLSID_DshowFakeSink, NULL, CLSCTX_INPROC, - &IID_IBaseFilter, (LPVOID *) & src->dshow_fakesink); - if (hres != S_OK || !src->dshow_fakesink) { - GST_CAT_ERROR (dshowvideosrc_debug, - "Can't create an instance of our dshow fakesink filter (error=0x%x)", - hres); - goto error; - } - - hres = - IFilterGraph_AddFilter (src->filter_graph, src->video_cap_filter, - L"capture"); - if (hres != S_OK) { - GST_CAT_ERROR (dshowvideosrc_debug, - "Can't add video capture filter to the graph (error=%d)", hres); - goto error; - } - - hres = - IFilterGraph_AddFilter (src->filter_graph, src->dshow_fakesink, L"sink"); - if (hres != S_OK) { - GST_CAT_ERROR (dshowvideosrc_debug, - "Can't add our fakesink filter to the graph (error=%d)", hres); - goto error; - } - - return TRUE; - -error: - if (src->dshow_fakesink) { - IBaseFilter_Release (src->dshow_fakesink); - src->dshow_fakesink = NULL; - } - - if (src->media_filter) { - IMediaFilter_Release (src->media_filter); - src->media_filter = NULL; - } - if (src->filter_graph) { - IFilterGraph_Release (src->filter_graph); - src->filter_graph = NULL; - } - - return FALSE; -} - -static gboolean -gst_dshowvideosrc_set_caps (GstBaseSrc * bsrc, GstCaps * caps) -{ - HRESULT hres; - IGstDshowInterface *srcinterface = NULL; - IPin *input_pin = NULL; - GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (bsrc); - GstStructure *s = gst_caps_get_structure (caps, 0); - - /* search the negociated caps in our caps list to get its index and the corresponding mediatype */ - if (gst_caps_is_subset (caps, src->caps)) { - guint i = 0; - gint res = -1; - - for (; i < gst_caps_get_size (src->caps) && res == -1; i++) { - GstCaps *capstmp = gst_caps_copy_nth (src->caps, i); - - if (gst_caps_is_subset (caps, capstmp)) { - res = i; - } - gst_caps_unref (capstmp); - } - - if (res != -1 && src->pins_mediatypes) { - /* get the corresponding media type and build the dshow graph */ - GstCapturePinMediaType *pin_mediatype = NULL; - gchar *caps_string = NULL; - GList *type = g_list_nth (src->pins_mediatypes, res); - - if (type) { - pin_mediatype = (GstCapturePinMediaType *) type->data; - - hres = - IBaseFilter_QueryInterface (src->dshow_fakesink, - &IID_IGstDshowInterface, (void **) &srcinterface); - - if (hres != S_OK || !srcinterface) { - GST_CAT_ERROR (dshowvideosrc_debug, - "Can't get IGstDshowInterface interface from our dshow fakesink filter (error=%d)", - hres); - goto error; - } - - IGstDshowInterface_gst_set_media_type (srcinterface, - pin_mediatype->mediatype); - IGstDshowInterface_gst_set_buffer_callback (srcinterface, - (byte *) gst_dshowvideosrc_push_buffer, (byte *) src); - - if (srcinterface) { - IGstDshowInterface_Release (srcinterface); - } - - gst_dshow_get_pin_from_filter (src->dshow_fakesink, PINDIR_INPUT, - &input_pin); - if (!input_pin) { - GST_CAT_ERROR (dshowvideosrc_debug, - "Can't get input pin from our dshow fakesink"); - goto error; - } - - hres = - IFilterGraph_ConnectDirect (src->filter_graph, - pin_mediatype->capture_pin, input_pin, NULL); - IPin_Release (input_pin); - - if (hres != S_OK) { - GST_CAT_ERROR (dshowvideosrc_debug, - "Can't connect capture filter with fakesink filter (error=%d)", - hres); - goto error; - } - - /* save width and height negociated */ - gst_structure_get_int (s, "width", &src->width); - gst_structure_get_int (s, "height", &src->height); - - src->is_rgb = FALSE; - caps_string = gst_caps_to_string (caps); - if (caps_string) { - if (strstr (caps_string, "video/x-raw-rgb")) { - src->is_rgb = TRUE; - } else { - src->is_rgb = FALSE; - } - g_free (caps_string); - } - } - } - } - - return TRUE; - -error: - if (srcinterface) { - IGstDshowInterface_Release (srcinterface); - } - - return FALSE; -} - -static gboolean -gst_dshowvideosrc_stop (GstBaseSrc * bsrc) -{ - IPin *input_pin = NULL, *output_pin = NULL; - HRESULT hres = S_FALSE; - GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (bsrc); - - if (!src->filter_graph) - return TRUE; - - /* disconnect filters */ - gst_dshow_get_pin_from_filter (src->video_cap_filter, PINDIR_OUTPUT, - &output_pin); - if (output_pin) { - hres = IFilterGraph_Disconnect (src->filter_graph, output_pin); - IPin_Release (output_pin); - } - - gst_dshow_get_pin_from_filter (src->dshow_fakesink, PINDIR_INPUT, &input_pin); - if (input_pin) { - hres = IFilterGraph_Disconnect (src->filter_graph, input_pin); - IPin_Release (input_pin); - } - - /*remove filters from the graph */ - IFilterGraph_RemoveFilter (src->filter_graph, src->video_cap_filter); - IFilterGraph_RemoveFilter (src->filter_graph, src->dshow_fakesink); - - /*release our gstreamer dshow sink */ - IBaseFilter_Release (src->dshow_fakesink); - src->dshow_fakesink = NULL; - - /*release media filter interface */ - IMediaFilter_Release (src->media_filter); - src->media_filter = NULL; - - /*release the filter graph manager */ - IFilterGraph_Release (src->filter_graph); - src->filter_graph = NULL; - - return TRUE; -} - -static gboolean -gst_dshowvideosrc_unlock (GstBaseSrc * bsrc) -{ - GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (bsrc); - - g_mutex_lock (src->buffer_mutex); - src->stop_requested = TRUE; - g_cond_signal (src->buffer_cond); - g_mutex_unlock (src->buffer_mutex); - - return TRUE; -} - -static gboolean -gst_dshowvideosrc_unlock_stop (GstBaseSrc * bsrc) -{ - GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (bsrc); - - src->stop_requested = FALSE; - - return TRUE; -} - -static GstFlowReturn -gst_dshowvideosrc_create (GstPushSrc * psrc, GstBuffer ** buf) -{ - GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (psrc); - - g_mutex_lock (src->buffer_mutex); - while (src->buffer == NULL && !src->stop_requested) - g_cond_wait (src->buffer_cond, src->buffer_mutex); - *buf = src->buffer; - src->buffer = NULL; - g_mutex_unlock (src->buffer_mutex); - - if (src->stop_requested) { - if (*buf != NULL) { - gst_buffer_unref (*buf); - *buf = NULL; - } - return GST_FLOW_WRONG_STATE; - } - - GST_CAT_DEBUG (dshowvideosrc_debug, - "dshowvideosrc_create => pts %" GST_TIME_FORMAT " duration %" - GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*buf)), - GST_TIME_ARGS (GST_BUFFER_DURATION (*buf))); - - return GST_FLOW_OK; -} - -static GstCaps * -gst_dshowvideosrc_getcaps_from_streamcaps (GstDshowVideoSrc * src, IPin * pin, - IAMStreamConfig * streamcaps) -{ - GstCaps *caps = NULL; - HRESULT hres = S_OK; - RPC_STATUS rpcstatus; - int icount = 0; - int isize = 0; - VIDEO_STREAM_CONFIG_CAPS vscc; - int i = 0; - - if (!streamcaps) - return NULL; - - IAMStreamConfig_GetNumberOfCapabilities (streamcaps, &icount, &isize); - - if (isize != sizeof (vscc)) - return NULL; - - for (; i < icount; i++) { - GstCapturePinMediaType *pin_mediatype = g_new0 (GstCapturePinMediaType, 1); - - IPin_AddRef (pin); - pin_mediatype->capture_pin = pin; - - hres = - IAMStreamConfig_GetStreamCaps (streamcaps, i, &pin_mediatype->mediatype, - (BYTE *) & vscc); - if (hres == S_OK && pin_mediatype->mediatype) { - VIDEOINFOHEADER *video_info; - GstCaps *mediacaps = NULL; - - if (!caps) - caps = gst_caps_new_empty (); - - /* I420 */ - if ((UuidCompare (&pin_mediatype->mediatype->subtype, (UUID *) &MEDIASUBTYPE_I420, - &rpcstatus) == 0 && rpcstatus == RPC_S_OK) - && (UuidCompare (&pin_mediatype->mediatype->formattype, - (UUID *) &FORMAT_VideoInfo, &rpcstatus) == 0 - && rpcstatus == RPC_S_OK)) { - video_info = (VIDEOINFOHEADER *) pin_mediatype->mediatype->pbFormat; - - mediacaps = gst_caps_new_simple ("video/x-raw-yuv", - "width", G_TYPE_INT, video_info->bmiHeader.biWidth, - "height", G_TYPE_INT, video_info->bmiHeader.biHeight, - "framerate", GST_TYPE_FRACTION, - (int) (10000000 / video_info->AvgTimePerFrame), 1, "format", - GST_TYPE_FOURCC, MAKEFOURCC ('I', '4', '2', '0'), NULL); - - if (mediacaps) { - src->pins_mediatypes = - g_list_append (src->pins_mediatypes, pin_mediatype); - gst_caps_append (caps, mediacaps); - } else { - gst_dshow_free_pin_mediatype (pin_mediatype); - } - continue; - } - - /* RGB24 */ - if ((UuidCompare (&pin_mediatype->mediatype->subtype, (UUID *) &MEDIASUBTYPE_RGB24, - &rpcstatus) == 0 && rpcstatus == RPC_S_OK) - && (UuidCompare (&pin_mediatype->mediatype->formattype, - (UUID *) &FORMAT_VideoInfo, &rpcstatus) == 0 - && rpcstatus == RPC_S_OK)) { - video_info = (VIDEOINFOHEADER *) pin_mediatype->mediatype->pbFormat; - - /* ffmpegcolorspace handles RGB24 in BIG_ENDIAN */ - mediacaps = gst_caps_new_simple ("video/x-raw-rgb", - "bpp", G_TYPE_INT, 24, - "depth", G_TYPE_INT, 24, - "width", G_TYPE_INT, video_info->bmiHeader.biWidth, - "height", G_TYPE_INT, video_info->bmiHeader.biHeight, - "framerate", GST_TYPE_FRACTION, - (int) (10000000 / video_info->AvgTimePerFrame), 1, "endianness", - G_TYPE_INT, G_BIG_ENDIAN, "red_mask", G_TYPE_INT, 255, "green_mask", - G_TYPE_INT, 65280, "blue_mask", G_TYPE_INT, 16711680, NULL); - - if (mediacaps) { - src->pins_mediatypes = - g_list_append (src->pins_mediatypes, pin_mediatype); - gst_caps_append (caps, mediacaps); - } else { - gst_dshow_free_pin_mediatype (pin_mediatype); - } - continue; - } - - /* DVSD */ - if ((UuidCompare (&pin_mediatype->mediatype->subtype, (UUID *) &MEDIASUBTYPE_dvsd, - &rpcstatus) == 0 && rpcstatus == RPC_S_OK) - && (UuidCompare (&pin_mediatype->mediatype->formattype, - (UUID *) &FORMAT_VideoInfo, &rpcstatus) == 0 - && rpcstatus == RPC_S_OK)) { - video_info = (VIDEOINFOHEADER *) pin_mediatype->mediatype->pbFormat; - - mediacaps = gst_caps_new_simple ("video/x-dv", - "systemstream", G_TYPE_BOOLEAN, FALSE, - "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('d', 'v', 's', 'd'), - "framerate", GST_TYPE_FRACTION, - (int) (10000000 / video_info->AvgTimePerFrame), 1, "width", - G_TYPE_INT, video_info->bmiHeader.biWidth, "height", G_TYPE_INT, - video_info->bmiHeader.biHeight, NULL); - - if (mediacaps) { - src->pins_mediatypes = - g_list_append (src->pins_mediatypes, pin_mediatype); - gst_caps_append (caps, mediacaps); - } else { - gst_dshow_free_pin_mediatype (pin_mediatype); - } - continue; - } - - /* DV stream */ - if ((UuidCompare (&pin_mediatype->mediatype->subtype, (UUID *) &MEDIASUBTYPE_dvsd, - &rpcstatus) == 0 && rpcstatus == RPC_S_OK) - && (UuidCompare (&pin_mediatype->mediatype->formattype, - (UUID *) &FORMAT_DvInfo, &rpcstatus) == 0 && rpcstatus == RPC_S_OK)) { - - mediacaps = gst_caps_new_simple ("video/x-dv", - "systemstream", G_TYPE_BOOLEAN, TRUE, NULL); - - if (mediacaps) { - src->pins_mediatypes = - g_list_append (src->pins_mediatypes, pin_mediatype); - gst_caps_append (caps, mediacaps); - } else { - gst_dshow_free_pin_mediatype (pin_mediatype); - } - continue; - } - } else { - gst_dshow_free_pin_mediatype (pin_mediatype); - } - } - - if (caps && gst_caps_is_empty (caps)) { - gst_caps_unref (caps); - caps = NULL; - } - - return caps; -} - -static gboolean -gst_dshowvideosrc_push_buffer (byte * buffer, long size, byte * src_object, - UINT64 start, UINT64 stop) -{ - GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (src_object); - GstBuffer *buf; - IPin *pPin = NULL; - HRESULT hres = S_FALSE; - AM_MEDIA_TYPE *pMediaType = NULL; - - if (!buffer || size == 0 || !src) { - return FALSE; - } - - /* create a new buffer assign to it the clock time as timestamp */ - buf = gst_buffer_new_and_alloc (size); - - GST_BUFFER_SIZE (buf) = size; - GST_BUFFER_TIMESTAMP (buf) = gst_clock_get_time (GST_ELEMENT (src)->clock); - GST_BUFFER_TIMESTAMP (buf) -= GST_ELEMENT (src)->base_time; - GST_BUFFER_DURATION (buf) = stop - start; - - if (src->is_rgb) { - /* FOR RGB directshow decoder will return bottom-up BITMAP - * There is probably a way to get top-bottom video frames from - * the decoder... - */ - gint line = 0; - gint stride = size / src->height; - - for (; line < src->height; line++) { - memcpy (GST_BUFFER_DATA (buf) + (line * stride), - buffer + (size - ((line + 1) * (stride))), stride); - } - } else { - memcpy (GST_BUFFER_DATA (buf), buffer, size); - } - - GST_CAT_DEBUG (dshowvideosrc_debug, - "push_buffer => pts %" GST_TIME_FORMAT "duration %" GST_TIME_FORMAT, - GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)), GST_TIME_ARGS (stop - start)); - - /* the negotiate() method already set caps on the source pad */ - gst_buffer_set_caps (buf, GST_PAD_CAPS (GST_BASE_SRC_PAD (src))); - - g_mutex_lock (src->buffer_mutex); - if (src->buffer != NULL) - gst_buffer_unref (src->buffer); - src->buffer = buf; - g_cond_signal (src->buffer_cond); - g_mutex_unlock (src->buffer_mutex); - - return TRUE; -} diff --git a/sys/dshowsrcwrapper/gstdshowvideosrc.cpp b/sys/dshowsrcwrapper/gstdshowvideosrc.cpp new file mode 100755 index 00000000..67e70d96 --- /dev/null +++ b/sys/dshowsrcwrapper/gstdshowvideosrc.cpp @@ -0,0 +1,1047 @@ +/* GStreamer + * Copyright (C) 2007 Sebastien Moutte + * + * gstdshowvideosrc.c: + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gstdshowvideosrc.h" + +static const GstElementDetails gst_dshowvideosrc_details = +GST_ELEMENT_DETAILS ("DirectShow video capture source", + "Source/Video", + "Receive data from a directshow video capture graph", + "Sebastien Moutte "); + +GST_DEBUG_CATEGORY_STATIC (dshowvideosrc_debug); +#define GST_CAT_DEFAULT dshowvideosrc_debug + +const GUID MEDIASUBTYPE_I420 + = { 0x30323449, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, + 0x71} +}; + +static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("video/x-raw-rgb," + "bpp = (int) 24," + "depth = (int) 24," + "width = (int) [ 1, MAX ]," + "height = (int) [ 1, MAX ]," + "framerate = (fraction) [ 0, MAX ];" + "video/x-dv," + "systemstream = (boolean) FALSE," + "width = (int) [ 1, MAX ]," + "height = (int) [ 1, MAX ]," + "framerate = (fraction) [ 0, MAX ]," + "format = (fourcc) dvsd;" + "video/x-dv," + "systemstream = (boolean) TRUE;" + "video/x-raw-yuv," + "width = (int) [ 1, MAX ]," + "height = (int) [ 1, MAX ]," + "framerate = (fraction) [ 0, MAX ]," "format = (fourcc) I420") + ); + +static void gst_dshowvideosrc_init_interfaces (GType type); + +GST_BOILERPLATE_FULL (GstDshowVideoSrc, gst_dshowvideosrc, GstPushSrc, + GST_TYPE_PUSH_SRC, gst_dshowvideosrc_init_interfaces); + +enum +{ + PROP_0, + PROP_DEVICE, + PROP_DEVICE_NAME +}; + +static void gst_dshowvideosrc_probe_interface_init (GstPropertyProbeInterface * + iface); +static const GList *gst_dshowvideosrc_probe_get_properties (GstPropertyProbe * + probe); +static GValueArray *gst_dshowvideosrc_probe_get_values (GstPropertyProbe * + probe, guint prop_id, const GParamSpec * pspec); +static GValueArray *gst_dshowvideosrc_get_device_name_values (GstDshowVideoSrc * + src); +static gboolean gst_dshowvideosrc_probe_needs_probe (GstPropertyProbe * probe, + guint prop_id, const GParamSpec * pspec); +static void gst_dshowvideosrc_probe_probe_property (GstPropertyProbe * probe, + guint prop_id, const GParamSpec * pspec); + + +static void gst_dshowvideosrc_dispose (GObject * gobject); +static void gst_dshowvideosrc_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec); +static void gst_dshowvideosrc_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec); +static GstCaps *gst_dshowvideosrc_get_caps (GstBaseSrc * src); +static GstStateChangeReturn gst_dshowvideosrc_change_state (GstElement * + element, GstStateChange transition); + + +static gboolean gst_dshowvideosrc_start (GstBaseSrc * bsrc); +static gboolean gst_dshowvideosrc_stop (GstBaseSrc * bsrc); +static gboolean gst_dshowvideosrc_unlock (GstBaseSrc * bsrc); +static gboolean gst_dshowvideosrc_unlock_stop (GstBaseSrc * bsrc); +static gboolean gst_dshowvideosrc_set_caps (GstBaseSrc * bsrc, GstCaps * caps); +static GstCaps *gst_dshowvideosrc_get_caps (GstBaseSrc * bsrc); +static GstFlowReturn gst_dshowvideosrc_create (GstPushSrc * psrc, + GstBuffer ** buf); + +/*utils*/ +static GstCaps *gst_dshowvideosrc_getcaps_from_streamcaps (GstDshowVideoSrc * + src, IPin * pin, IAMStreamConfig * streamcaps); +static gboolean gst_dshowvideosrc_push_buffer (byte * buffer, long size, + byte * src_object, UINT64 start, UINT64 stop); + +static void +gst_dshowvideosrc_init_interfaces (GType type) +{ + static const GInterfaceInfo dshowvideosrc_info = { + (GInterfaceInitFunc) gst_dshowvideosrc_probe_interface_init, + NULL, + NULL, + }; + + g_type_add_interface_static (type, + GST_TYPE_PROPERTY_PROBE, &dshowvideosrc_info); +} + +static void +gst_dshowvideosrc_probe_interface_init (GstPropertyProbeInterface * iface) +{ + iface->get_properties = gst_dshowvideosrc_probe_get_properties; + iface->needs_probe = gst_dshowvideosrc_probe_needs_probe; + iface->probe_property = gst_dshowvideosrc_probe_probe_property; + iface->get_values = gst_dshowvideosrc_probe_get_values; +} + +static void +gst_dshowvideosrc_base_init (gpointer klass) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (klass); + + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&src_template)); + + gst_element_class_set_details (element_class, &gst_dshowvideosrc_details); +} + +static void +gst_dshowvideosrc_class_init (GstDshowVideoSrcClass * klass) +{ + GObjectClass *gobject_class; + GstElementClass *gstelement_class; + GstBaseSrcClass *gstbasesrc_class; + GstPushSrcClass *gstpushsrc_class; + + gobject_class = (GObjectClass *) klass; + gstelement_class = (GstElementClass *) klass; + gstbasesrc_class = (GstBaseSrcClass *) klass; + gstpushsrc_class = (GstPushSrcClass *) klass; + + gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_dshowvideosrc_dispose); + gobject_class->set_property = + GST_DEBUG_FUNCPTR (gst_dshowvideosrc_set_property); + gobject_class->get_property = + GST_DEBUG_FUNCPTR (gst_dshowvideosrc_get_property); + + gstelement_class->change_state = + GST_DEBUG_FUNCPTR (gst_dshowvideosrc_change_state); + + gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_dshowvideosrc_get_caps); + gstbasesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_dshowvideosrc_set_caps); + gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_dshowvideosrc_start); + gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_dshowvideosrc_stop); + gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_dshowvideosrc_unlock); + gstbasesrc_class->unlock_stop = + GST_DEBUG_FUNCPTR (gst_dshowvideosrc_unlock_stop); + + gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_dshowvideosrc_create); + + g_object_class_install_property + (gobject_class, PROP_DEVICE, + g_param_spec_string ("device", "Device", + "Directshow device path (@..classID/name)", NULL, + static_cast(G_PARAM_READWRITE))); + + g_object_class_install_property + (gobject_class, PROP_DEVICE_NAME, + g_param_spec_string ("device-name", "Device name", + "Human-readable name of the sound device", NULL, + static_cast(G_PARAM_READWRITE))); + + GST_DEBUG_CATEGORY_INIT (dshowvideosrc_debug, "dshowvideosrc", 0, + "Directshow video source"); + +} + +static void +gst_dshowvideosrc_init (GstDshowVideoSrc * src, GstDshowVideoSrcClass * klass) +{ + src->device = NULL; + src->device_name = NULL; + src->video_cap_filter = NULL; + src->dshow_fakesink = NULL; + src->media_filter = NULL; + src->filter_graph = NULL; + src->caps = NULL; + src->pins_mediatypes = NULL; + src->is_rgb = FALSE; + + src->buffer_cond = g_cond_new (); + src->buffer_mutex = g_mutex_new (); + src->buffer = NULL; + src->stop_requested = FALSE; + + CoInitializeEx (NULL, COINIT_MULTITHREADED); + + gst_base_src_set_live (GST_BASE_SRC (src), TRUE); +} + +static void +gst_dshowvideosrc_dispose (GObject * gobject) +{ + GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (gobject); + + if (src->device) { + g_free (src->device); + src->device = NULL; + } + + if (src->device_name) { + g_free (src->device_name); + src->device_name = NULL; + } + + if (src->caps) { + gst_caps_unref (src->caps); + src->caps = NULL; + } + + if (src->pins_mediatypes) { + gst_dshow_free_pins_mediatypes (src->pins_mediatypes); + src->pins_mediatypes = NULL; + } + + /* clean dshow */ + if (src->video_cap_filter) { + src->video_cap_filter->Release(); + src->video_cap_filter = NULL; + } + + if (src->buffer_mutex) { + g_mutex_free (src->buffer_mutex); + src->buffer_mutex = NULL; + } + + if (src->buffer_cond) { + g_cond_free (src->buffer_cond); + src->buffer_cond = NULL; + } + + if (src->buffer) { + gst_buffer_unref (src->buffer); + src->buffer = NULL; + } + + CoUninitialize (); + + G_OBJECT_CLASS (parent_class)->dispose (gobject); +} + +static gboolean +gst_dshowvideosrc_probe_needs_probe (GstPropertyProbe * probe, + guint prop_id, const GParamSpec * pspec) +{ + static gboolean init = FALSE; + gboolean ret = FALSE; + + if (!init) { + ret = TRUE; + init = TRUE; + } + + return ret; +} + +static void +gst_dshowvideosrc_probe_probe_property (GstPropertyProbe * probe, + guint prop_id, const GParamSpec * pspec) +{ + GObjectClass *klass = G_OBJECT_GET_CLASS (probe); + + switch (prop_id) { + case PROP_DEVICE_NAME: + //gst_v4l_class_probe_devices (klass, FALSE); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec); + break; + } +} + +static const GList * +gst_dshowvideosrc_probe_get_properties (GstPropertyProbe * probe) +{ + GObjectClass *klass = G_OBJECT_GET_CLASS (probe); + static GList *props = NULL; + + if (!props) { + GParamSpec *pspec; + + pspec = g_object_class_find_property (klass, "device-name"); + props = g_list_append (props, pspec); + } + + return props; +} + +static GValueArray * +gst_dshowvideosrc_get_device_name_values (GstDshowVideoSrc * 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, (LPVOID *) &devices_enum); + if (hres != S_OK) { + GST_CAT_ERROR (dshowvideosrc_debug, + "Can't create an instance of the system device enumerator (error=%d)", + hres); + array = NULL; + goto clean; + } + + hres = devices_enum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, + &moniker_enum, 0); + if (hres != S_OK || !moniker_enum) { + GST_CAT_ERROR (dshowvideosrc_debug, + "Can't get enumeration of video devices (error=%d)", hres); + array = NULL; + goto clean; + } + + moniker_enum->Reset(); + + while (hres = moniker_enum->Next(1, &moniker, &fetched), + hres == S_OK) { + IPropertyBag *property_bag = NULL; + + hres = + moniker->BindToStorage(NULL, NULL, IID_IPropertyBag, + (LPVOID *) &property_bag); + if (SUCCEEDED (hres) && property_bag) { + VARIANT varFriendlyName; + + VariantInit (&varFriendlyName); + hres = property_bag->Read(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); + } + property_bag->Release(); + } + moniker->Release(); + } + +clean: + if (moniker_enum) + moniker_enum->Release(); + + if (devices_enum) + devices_enum->Release(); + + return array; +} + +static GValueArray * +gst_dshowvideosrc_probe_get_values (GstPropertyProbe * probe, + guint prop_id, const GParamSpec * pspec) +{ + GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (probe); + GValueArray *array = NULL; + + switch (prop_id) { + case PROP_DEVICE_NAME: + array = gst_dshowvideosrc_get_device_name_values (src); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec); + break; + } + + return array; +} + +static void +gst_dshowvideosrc_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (object); + + switch (prop_id) { + case PROP_DEVICE: + { + if (src->device) { + g_free (src->device); + src->device = NULL; + } + if (g_value_get_string (value)) { + src->device = g_strdup (g_value_get_string (value)); + } + break; + } + case PROP_DEVICE_NAME: + { + if (src->device_name) { + g_free (src->device_name); + src->device_name = NULL; + } + if (g_value_get_string (value)) { + src->device_name = g_strdup (g_value_get_string (value)); + } + break; + } + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_dshowvideosrc_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec) +{ + +} + +static GstCaps * +gst_dshowvideosrc_get_caps (GstBaseSrc * basesrc) +{ + HRESULT hres = S_OK; + IBindCtx *lpbc = NULL; + IMoniker *videom; + DWORD dwEaten; + GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (basesrc); + gunichar2 *unidevice = NULL; + + if (src->device) { + g_free (src->device); + src->device = NULL; + } + + src->device = + gst_dshow_getdevice_from_devicename (&CLSID_VideoInputDeviceCategory, + &src->device_name); + if (!src->device) { + GST_CAT_ERROR (dshowvideosrc_debug, "No video device found."); + return NULL; + } + unidevice = + g_utf8_to_utf16 (src->device, strlen (src->device), NULL, NULL, NULL); + + if (!src->video_cap_filter) { + hres = CreateBindCtx (0, &lpbc); + if (SUCCEEDED (hres)) { + hres = MkParseDisplayName (lpbc, (LPCOLESTR) unidevice, &dwEaten, &videom); + if (SUCCEEDED (hres)) { + hres = videom->BindToObject(lpbc, NULL, IID_IBaseFilter, + (LPVOID *) &src->video_cap_filter); + videom->Release(); + } + lpbc->Release(); + } + } + + if (!src->caps) { + src->caps = gst_caps_new_empty (); + } + + if (src->video_cap_filter && gst_caps_is_empty (src->caps)) { + /* get the capture pins supported types */ + IPin *capture_pin = NULL; + IEnumPins *enumpins = NULL; + HRESULT hres; + + hres = src->video_cap_filter->EnumPins(&enumpins); + if (SUCCEEDED (hres)) { + while (enumpins->Next(1, &capture_pin, NULL) == S_OK) { + IKsPropertySet *pKs = NULL; + hres = capture_pin->QueryInterface(IID_IKsPropertySet, (LPVOID *) &pKs); + if (SUCCEEDED (hres) && pKs) { + DWORD cbReturned; + GUID pin_category; + RPC_STATUS rpcstatus; + + hres = + pKs->Get(AMPROPSETID_Pin, + AMPROPERTY_PIN_CATEGORY, NULL, 0, &pin_category, sizeof (GUID), + &cbReturned); + + /* we only want capture pins */ + if (UuidCompare (&pin_category, (UUID *) &PIN_CATEGORY_CAPTURE, + &rpcstatus) == 0) { + IAMStreamConfig *streamcaps = NULL; + + if (SUCCEEDED (capture_pin->QueryInterface( + IID_IAMStreamConfig, (LPVOID *) &streamcaps))) { + GstCaps *caps = + gst_dshowvideosrc_getcaps_from_streamcaps (src, capture_pin, + streamcaps); + + if (caps) { + gst_caps_append (src->caps, caps); + } + streamcaps->Release(); + } + } + + pKs->Release(); + } + + capture_pin->Release(); + } + enumpins->Release(); + } + } + + if (unidevice) { + g_free (unidevice); + } + + if (src->caps) { + GST_CAT_LOG (dshowvideosrc_debug, "getcaps returned %s", + gst_caps_to_string (src->caps)); + return gst_caps_ref (src->caps); + } + + return NULL; +} + +static GstStateChangeReturn +gst_dshowvideosrc_change_state (GstElement * element, GstStateChange transition) +{ + HRESULT hres = S_FALSE; + IAMVfwCaptureDialogs *dialog = NULL; + GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (element); + + switch (transition) { + case GST_STATE_CHANGE_NULL_TO_READY: + break; + case GST_STATE_CHANGE_READY_TO_PAUSED: + break; + case GST_STATE_CHANGE_PAUSED_TO_PLAYING: + if (src->media_filter) + hres = src->media_filter->Run(0); + if (hres != S_OK) { + GST_CAT_ERROR (dshowvideosrc_debug, + "Can't RUN the directshow capture graph (error=%d)", hres); + return GST_STATE_CHANGE_FAILURE; + } + break; + case GST_STATE_CHANGE_PLAYING_TO_PAUSED: + if (src->media_filter) + hres = src->media_filter->Stop(); + if (hres != S_OK) { + GST_CAT_ERROR (dshowvideosrc_debug, + "Can't STOP the directshow capture graph (error=%d)", hres); + return GST_STATE_CHANGE_FAILURE; + } + break; + case GST_STATE_CHANGE_PAUSED_TO_READY: + break; + case GST_STATE_CHANGE_READY_TO_NULL: + break; + } + + return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); +} + +static gboolean +gst_dshowvideosrc_start (GstBaseSrc * bsrc) +{ + HRESULT hres = S_FALSE; + GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (bsrc); + + hres = CoCreateInstance (CLSID_FilterGraph, NULL, CLSCTX_INPROC, + IID_IFilterGraph, (LPVOID *) & src->filter_graph); + if (hres != S_OK || !src->filter_graph) { + GST_CAT_ERROR (dshowvideosrc_debug, + "Can't create an instance of the dshow graph manager (error=%d)", hres); + goto error; + } + + hres = src->filter_graph->QueryInterface(IID_IMediaFilter, + (LPVOID *) &src->media_filter); + if (hres != S_OK || !src->media_filter) { + GST_CAT_ERROR (dshowvideosrc_debug, + "Can't get IMediacontrol interface from the graph manager (error=%d)", + hres); + goto error; + } + + hres = CoCreateInstance (CLSID_DshowFakeSink, NULL, CLSCTX_INPROC, + IID_IBaseFilter, (LPVOID *) & src->dshow_fakesink); + if (hres != S_OK || !src->dshow_fakesink) { + GST_CAT_ERROR (dshowvideosrc_debug, + "Can't create an instance of our dshow fakesink filter (error=0x%x)", + hres); + goto error; + } + + hres = src->filter_graph->AddFilter(src->video_cap_filter, L"capture"); + if (hres != S_OK) { + GST_CAT_ERROR (dshowvideosrc_debug, + "Can't add video capture filter to the graph (error=%d)", hres); + goto error; + } + + hres = src->filter_graph->AddFilter(src->dshow_fakesink, L"sink"); + if (hres != S_OK) { + GST_CAT_ERROR (dshowvideosrc_debug, + "Can't add our fakesink filter to the graph (error=%d)", hres); + goto error; + } + + return TRUE; + +error: + if (src->dshow_fakesink) { + src->dshow_fakesink->Release(); + src->dshow_fakesink = NULL; + } + + if (src->media_filter) { + src->media_filter->Release(); + src->media_filter = NULL; + } + if (src->filter_graph) { + src->filter_graph->Release(); + src->filter_graph = NULL; + } + + return FALSE; +} + +static gboolean +gst_dshowvideosrc_set_caps (GstBaseSrc * bsrc, GstCaps * caps) +{ + HRESULT hres; + IGstDshowInterface *srcinterface = NULL; + IPin *input_pin = NULL; + GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (bsrc); + GstStructure *s = gst_caps_get_structure (caps, 0); + + /* search the negociated caps in our caps list to get its index and the corresponding mediatype */ + if (gst_caps_is_subset (caps, src->caps)) { + guint i = 0; + gint res = -1; + + for (; i < gst_caps_get_size (src->caps) && res == -1; i++) { + GstCaps *capstmp = gst_caps_copy_nth (src->caps, i); + + if (gst_caps_is_subset (caps, capstmp)) { + res = i; + } + gst_caps_unref (capstmp); + } + + if (res != -1 && src->pins_mediatypes) { + /* get the corresponding media type and build the dshow graph */ + GstCapturePinMediaType *pin_mediatype = NULL; + gchar *caps_string = NULL; + GList *type = g_list_nth (src->pins_mediatypes, res); + + if (type) { + pin_mediatype = (GstCapturePinMediaType *) type->data; + + hres = src->dshow_fakesink->QueryInterface( + IID_IGstDshowInterface, (LPVOID *) &srcinterface); + + if (hres != S_OK || !srcinterface) { + GST_CAT_ERROR (dshowvideosrc_debug, + "Can't get IGstDshowInterface interface from our dshow fakesink filter (error=%d)", + hres); + goto error; + } + + srcinterface->gst_set_media_type(pin_mediatype->mediatype); + srcinterface->gst_set_buffer_callback( + (push_buffer_func) gst_dshowvideosrc_push_buffer, (byte *) src); + + if (srcinterface) + srcinterface->Release(); + + gst_dshow_get_pin_from_filter (src->dshow_fakesink, PINDIR_INPUT, + &input_pin); + if (!input_pin) { + GST_CAT_ERROR (dshowvideosrc_debug, + "Can't get input pin from our dshow fakesink"); + goto error; + } + + hres = src->filter_graph->ConnectDirect(pin_mediatype->capture_pin, + input_pin, NULL); + input_pin->Release(); + + if (hres != S_OK) { + GST_CAT_ERROR (dshowvideosrc_debug, + "Can't connect capture filter with fakesink filter (error=%d)", + hres); + goto error; + } + + /* save width and height negociated */ + gst_structure_get_int (s, "width", &src->width); + gst_structure_get_int (s, "height", &src->height); + + src->is_rgb = FALSE; + caps_string = gst_caps_to_string (caps); + if (caps_string) { + if (strstr (caps_string, "video/x-raw-rgb")) { + src->is_rgb = TRUE; + } else { + src->is_rgb = FALSE; + } + g_free (caps_string); + } + } + } + } + + return TRUE; + +error: + if (srcinterface) + srcinterface->Release(); + + return FALSE; +} + +static gboolean +gst_dshowvideosrc_stop (GstBaseSrc * bsrc) +{ + IPin *input_pin = NULL, *output_pin = NULL; + HRESULT hres = S_FALSE; + GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (bsrc); + + if (!src->filter_graph) + return TRUE; + + /* disconnect filters */ + gst_dshow_get_pin_from_filter (src->video_cap_filter, PINDIR_OUTPUT, + &output_pin); + if (output_pin) { + hres = src->filter_graph->Disconnect(output_pin); + output_pin->Release(); + } + + gst_dshow_get_pin_from_filter (src->dshow_fakesink, PINDIR_INPUT, &input_pin); + if (input_pin) { + hres = src->filter_graph->Disconnect(input_pin); + input_pin->Release(); + } + + /*remove filters from the graph */ + src->filter_graph->RemoveFilter(src->video_cap_filter); + src->filter_graph->RemoveFilter(src->dshow_fakesink); + + /*release our gstreamer dshow sink */ + src->dshow_fakesink->Release(); + src->dshow_fakesink = NULL; + + /*release media filter interface */ + src->media_filter->Release(); + src->media_filter = NULL; + + /*release the filter graph manager */ + src->filter_graph->Release(); + src->filter_graph = NULL; + + return TRUE; +} + +static gboolean +gst_dshowvideosrc_unlock (GstBaseSrc * bsrc) +{ + GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (bsrc); + + g_mutex_lock (src->buffer_mutex); + src->stop_requested = TRUE; + g_cond_signal (src->buffer_cond); + g_mutex_unlock (src->buffer_mutex); + + return TRUE; +} + +static gboolean +gst_dshowvideosrc_unlock_stop (GstBaseSrc * bsrc) +{ + GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (bsrc); + + src->stop_requested = FALSE; + + return TRUE; +} + +static GstFlowReturn +gst_dshowvideosrc_create (GstPushSrc * psrc, GstBuffer ** buf) +{ + GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (psrc); + + g_mutex_lock (src->buffer_mutex); + while (src->buffer == NULL && !src->stop_requested) + g_cond_wait (src->buffer_cond, src->buffer_mutex); + *buf = src->buffer; + src->buffer = NULL; + g_mutex_unlock (src->buffer_mutex); + + if (src->stop_requested) { + if (*buf != NULL) { + gst_buffer_unref (*buf); + *buf = NULL; + } + return GST_FLOW_WRONG_STATE; + } + + GST_CAT_DEBUG (dshowvideosrc_debug, + "dshowvideosrc_create => pts %" GST_TIME_FORMAT " duration %" + GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (*buf)), + GST_TIME_ARGS (GST_BUFFER_DURATION (*buf))); + + return GST_FLOW_OK; +} + +static GstCaps * +gst_dshowvideosrc_getcaps_from_streamcaps (GstDshowVideoSrc * src, IPin * pin, + IAMStreamConfig * streamcaps) +{ + GstCaps *caps = NULL; + HRESULT hres = S_OK; + RPC_STATUS rpcstatus; + int icount = 0; + int isize = 0; + VIDEO_STREAM_CONFIG_CAPS vscc; + int i = 0; + + if (!streamcaps) + return NULL; + + streamcaps->GetNumberOfCapabilities(&icount, &isize); + + if (isize != sizeof (vscc)) + return NULL; + + for (; i < icount; i++) { + GstCapturePinMediaType *pin_mediatype = g_new0 (GstCapturePinMediaType, 1); + + pin->AddRef(); + pin_mediatype->capture_pin = pin; + + hres = streamcaps->GetStreamCaps(i, &pin_mediatype->mediatype, (BYTE *) & vscc); + if (hres == S_OK && pin_mediatype->mediatype) { + VIDEOINFOHEADER *video_info; + GstCaps *mediacaps = NULL; + + if (!caps) + caps = gst_caps_new_empty (); + + /* I420 */ + if ((UuidCompare (&pin_mediatype->mediatype->subtype, (UUID *) &MEDIASUBTYPE_I420, + &rpcstatus) == 0 && rpcstatus == RPC_S_OK) + && (UuidCompare (&pin_mediatype->mediatype->formattype, + (UUID *) &FORMAT_VideoInfo, &rpcstatus) == 0 + && rpcstatus == RPC_S_OK)) { + video_info = (VIDEOINFOHEADER *) pin_mediatype->mediatype->pbFormat; + + mediacaps = gst_caps_new_simple ("video/x-raw-yuv", + "width", G_TYPE_INT, video_info->bmiHeader.biWidth, + "height", G_TYPE_INT, video_info->bmiHeader.biHeight, + "framerate", GST_TYPE_FRACTION, + (int) (10000000 / video_info->AvgTimePerFrame), 1, "format", + GST_TYPE_FOURCC, MAKEFOURCC ('I', '4', '2', '0'), NULL); + + if (mediacaps) { + src->pins_mediatypes = + g_list_append (src->pins_mediatypes, pin_mediatype); + gst_caps_append (caps, mediacaps); + } else { + gst_dshow_free_pin_mediatype (pin_mediatype); + } + continue; + } + + /* RGB24 */ + if ((UuidCompare (&pin_mediatype->mediatype->subtype, (UUID *) &MEDIASUBTYPE_RGB24, + &rpcstatus) == 0 && rpcstatus == RPC_S_OK) + && (UuidCompare (&pin_mediatype->mediatype->formattype, + (UUID *) &FORMAT_VideoInfo, &rpcstatus) == 0 + && rpcstatus == RPC_S_OK)) { + video_info = (VIDEOINFOHEADER *) pin_mediatype->mediatype->pbFormat; + + /* ffmpegcolorspace handles RGB24 in BIG_ENDIAN */ + mediacaps = gst_caps_new_simple ("video/x-raw-rgb", + "bpp", G_TYPE_INT, 24, + "depth", G_TYPE_INT, 24, + "width", G_TYPE_INT, video_info->bmiHeader.biWidth, + "height", G_TYPE_INT, video_info->bmiHeader.biHeight, + "framerate", GST_TYPE_FRACTION, + (int) (10000000 / video_info->AvgTimePerFrame), 1, "endianness", + G_TYPE_INT, G_BIG_ENDIAN, "red_mask", G_TYPE_INT, 255, "green_mask", + G_TYPE_INT, 65280, "blue_mask", G_TYPE_INT, 16711680, NULL); + + if (mediacaps) { + src->pins_mediatypes = + g_list_append (src->pins_mediatypes, pin_mediatype); + gst_caps_append (caps, mediacaps); + } else { + gst_dshow_free_pin_mediatype (pin_mediatype); + } + continue; + } + + /* DVSD */ + if ((UuidCompare (&pin_mediatype->mediatype->subtype, (UUID *) &MEDIASUBTYPE_dvsd, + &rpcstatus) == 0 && rpcstatus == RPC_S_OK) + && (UuidCompare (&pin_mediatype->mediatype->formattype, + (UUID *) &FORMAT_VideoInfo, &rpcstatus) == 0 + && rpcstatus == RPC_S_OK)) { + video_info = (VIDEOINFOHEADER *) pin_mediatype->mediatype->pbFormat; + + mediacaps = gst_caps_new_simple ("video/x-dv", + "systemstream", G_TYPE_BOOLEAN, FALSE, + "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('d', 'v', 's', 'd'), + "framerate", GST_TYPE_FRACTION, + (int) (10000000 / video_info->AvgTimePerFrame), 1, "width", + G_TYPE_INT, video_info->bmiHeader.biWidth, "height", G_TYPE_INT, + video_info->bmiHeader.biHeight, NULL); + + if (mediacaps) { + src->pins_mediatypes = + g_list_append (src->pins_mediatypes, pin_mediatype); + gst_caps_append (caps, mediacaps); + } else { + gst_dshow_free_pin_mediatype (pin_mediatype); + } + continue; + } + + /* DV stream */ + if ((UuidCompare (&pin_mediatype->mediatype->subtype, (UUID *) &MEDIASUBTYPE_dvsd, + &rpcstatus) == 0 && rpcstatus == RPC_S_OK) + && (UuidCompare (&pin_mediatype->mediatype->formattype, + (UUID *) &FORMAT_DvInfo, &rpcstatus) == 0 && rpcstatus == RPC_S_OK)) { + + mediacaps = gst_caps_new_simple ("video/x-dv", + "systemstream", G_TYPE_BOOLEAN, TRUE, NULL); + + if (mediacaps) { + src->pins_mediatypes = + g_list_append (src->pins_mediatypes, pin_mediatype); + gst_caps_append (caps, mediacaps); + } else { + gst_dshow_free_pin_mediatype (pin_mediatype); + } + continue; + } + } else { + gst_dshow_free_pin_mediatype (pin_mediatype); + } + } + + if (caps && gst_caps_is_empty (caps)) { + gst_caps_unref (caps); + caps = NULL; + } + + return caps; +} + +static gboolean +gst_dshowvideosrc_push_buffer (byte * buffer, long size, byte * src_object, + UINT64 start, UINT64 stop) +{ + GstDshowVideoSrc *src = GST_DSHOWVIDEOSRC (src_object); + GstBuffer *buf; + IPin *pPin = NULL; + HRESULT hres = S_FALSE; + AM_MEDIA_TYPE *pMediaType = NULL; + + if (!buffer || size == 0 || !src) { + return FALSE; + } + + /* create a new buffer assign to it the clock time as timestamp */ + buf = gst_buffer_new_and_alloc (size); + + GST_BUFFER_SIZE (buf) = size; + GST_BUFFER_TIMESTAMP (buf) = gst_clock_get_time (GST_ELEMENT (src)->clock); + GST_BUFFER_TIMESTAMP (buf) -= GST_ELEMENT (src)->base_time; + GST_BUFFER_DURATION (buf) = stop - start; + + if (src->is_rgb) { + /* FOR RGB directshow decoder will return bottom-up BITMAP + * There is probably a way to get top-bottom video frames from + * the decoder... + */ + gint line = 0; + gint stride = size / src->height; + + for (; line < src->height; line++) { + memcpy (GST_BUFFER_DATA (buf) + (line * stride), + buffer + (size - ((line + 1) * (stride))), stride); + } + } else { + memcpy (GST_BUFFER_DATA (buf), buffer, size); + } + + GST_CAT_DEBUG (dshowvideosrc_debug, + "push_buffer => pts %" GST_TIME_FORMAT "duration %" GST_TIME_FORMAT, + GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)), GST_TIME_ARGS (stop - start)); + + /* the negotiate() method already set caps on the source pad */ + gst_buffer_set_caps (buf, GST_PAD_CAPS (GST_BASE_SRC_PAD (src))); + + g_mutex_lock (src->buffer_mutex); + if (src->buffer != NULL) + gst_buffer_unref (src->buffer); + src->buffer = buf; + g_cond_signal (src->buffer_cond); + g_mutex_unlock (src->buffer_mutex); + + return TRUE; +} -- cgit v1.2.1 From 0efde4cb7085ca1289c401090ed995a3be2f4041 Mon Sep 17 00:00:00 2001 From: Julien Isorce Date: Wed, 22 Jul 2009 12:24:46 +0200 Subject: dshowaudiosrc: converts code to C++ --- sys/dshowsrcwrapper/gstdshowaudiosrc.c | 881 ------------------------------- sys/dshowsrcwrapper/gstdshowaudiosrc.cpp | 859 ++++++++++++++++++++++++++++++ 2 files changed, 859 insertions(+), 881 deletions(-) delete mode 100755 sys/dshowsrcwrapper/gstdshowaudiosrc.c create mode 100755 sys/dshowsrcwrapper/gstdshowaudiosrc.cpp (limited to 'sys/dshowsrcwrapper') diff --git a/sys/dshowsrcwrapper/gstdshowaudiosrc.c b/sys/dshowsrcwrapper/gstdshowaudiosrc.c deleted file mode 100755 index dcc4e385..00000000 --- a/sys/dshowsrcwrapper/gstdshowaudiosrc.c +++ /dev/null @@ -1,881 +0,0 @@ -/* GStreamer - * Copyright (C) 2007 Sebastien Moutte - * - * gstdshowaudiosrc.c: - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "gstdshowaudiosrc.h" - -static const GstElementDetails gst_dshowaudiosrc_details = -GST_ELEMENT_DETAILS ("Directshow audio capture source", - "Source/Audio", - "Receive data from a directshow audio capture graph", - "Sebastien Moutte "); - -GST_DEBUG_CATEGORY_STATIC (dshowaudiosrc_debug); -#define GST_CAT_DEFAULT dshowaudiosrc_debug - -static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", - GST_PAD_SRC, - GST_PAD_ALWAYS, - GST_STATIC_CAPS ("audio/x-raw-int, " - "endianness = (int) { " G_STRINGIFY (G_BYTE_ORDER) " }, " - "signed = (boolean) { TRUE, FALSE }, " - "width = (int) 16, " - "depth = (int) 16, " - "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]; " - "audio/x-raw-int, " - "signed = (boolean) { TRUE, FALSE }, " - "width = (int) 8, " - "depth = (int) 8, " - "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]") - ); - -static void gst_dshowaudiosrc_init_interfaces (GType type); - -GST_BOILERPLATE_FULL (GstDshowAudioSrc, gst_dshowaudiosrc, GstAudioSrc, - GST_TYPE_AUDIO_SRC, gst_dshowaudiosrc_init_interfaces); - -enum -{ - PROP_0, - PROP_DEVICE, - PROP_DEVICE_NAME -}; - -static void gst_dshowaudiosrc_probe_interface_init (GstPropertyProbeInterface * - iface); -static const GList *gst_dshowaudiosrc_probe_get_properties (GstPropertyProbe * - probe); -static GValueArray *gst_dshowaudiosrc_probe_get_values (GstPropertyProbe * - probe, guint prop_id, const GParamSpec * pspec); -static GValueArray *gst_dshowaudiosrc_get_device_name_values (GstDshowAudioSrc * - src); - - -static void gst_dshowaudiosrc_dispose (GObject * gobject); -static void gst_dshowaudiosrc_set_property (GObject * object, guint prop_id, - const GValue * value, GParamSpec * pspec); -static void gst_dshowaudiosrc_get_property (GObject * object, guint prop_id, - GValue * value, GParamSpec * pspec); -static GstCaps *gst_dshowaudiosrc_get_caps (GstBaseSrc * src); -static GstStateChangeReturn gst_dshowaudiosrc_change_state (GstElement * - element, GstStateChange transition); - -static gboolean gst_dshowaudiosrc_open (GstAudioSrc * asrc); -static gboolean gst_dshowaudiosrc_prepare (GstAudioSrc * asrc, - GstRingBufferSpec * spec); -static gboolean gst_dshowaudiosrc_unprepare (GstAudioSrc * asrc); -static gboolean gst_dshowaudiosrc_close (GstAudioSrc * asrc); -static guint gst_dshowaudiosrc_read (GstAudioSrc * asrc, gpointer data, - guint length); -static guint gst_dshowaudiosrc_delay (GstAudioSrc * asrc); -static void gst_dshowaudiosrc_reset (GstAudioSrc * asrc); - -/* utils */ -static GstCaps *gst_dshowaudiosrc_getcaps_from_streamcaps (GstDshowAudioSrc * - src, IPin * pin, IAMStreamConfig * streamcaps); -static gboolean gst_dshowaudiosrc_push_buffer (byte * buffer, long size, - byte * src_object, UINT64 start, UINT64 stop); - -static void -gst_dshowaudiosrc_init_interfaces (GType type) -{ - static const GInterfaceInfo dshowaudiosrc_info = { - (GInterfaceInitFunc) gst_dshowaudiosrc_probe_interface_init, - NULL, - NULL, - }; - - g_type_add_interface_static (type, - GST_TYPE_PROPERTY_PROBE, &dshowaudiosrc_info); -} - -static void -gst_dshowaudiosrc_probe_interface_init (GstPropertyProbeInterface * iface) -{ - iface->get_properties = gst_dshowaudiosrc_probe_get_properties; -/* iface->needs_probe = gst_dshowaudiosrc_probe_needs_probe; - iface->probe_property = gst_dshowaudiosrc_probe_probe_property;*/ - iface->get_values = gst_dshowaudiosrc_probe_get_values; -} - -static void -gst_dshowaudiosrc_base_init (gpointer klass) -{ - GstElementClass *element_class = GST_ELEMENT_CLASS (klass); - - gst_element_class_add_pad_template (element_class, - gst_static_pad_template_get (&src_template)); - - gst_element_class_set_details (element_class, &gst_dshowaudiosrc_details); -} - -static void -gst_dshowaudiosrc_class_init (GstDshowAudioSrcClass * klass) -{ - GObjectClass *gobject_class; - GstElementClass *gstelement_class; - GstBaseSrcClass *gstbasesrc_class; - GstAudioSrcClass *gstaudiosrc_class; - - gobject_class = (GObjectClass *) klass; - gstelement_class = (GstElementClass *) klass; - gstbasesrc_class = (GstBaseSrcClass *) klass; - gstaudiosrc_class = (GstAudioSrcClass *) klass; - - gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_dispose); - gobject_class->set_property = - GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_set_property); - gobject_class->get_property = - GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_get_property); - - gstelement_class->change_state = - GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_change_state); - - gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_get_caps); - - gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_open); - gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_prepare); - gstaudiosrc_class->unprepare = - GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_unprepare); - gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_close); - gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_read); - gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_delay); - gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_reset); - - g_object_class_install_property - (gobject_class, PROP_DEVICE, - g_param_spec_string ("device", "Device", - "Directshow device reference (classID/name)", - NULL, G_PARAM_READWRITE)); - - g_object_class_install_property - (gobject_class, PROP_DEVICE_NAME, - g_param_spec_string ("device-name", "Device name", - "Human-readable name of the sound device", NULL, G_PARAM_READWRITE)); - - GST_DEBUG_CATEGORY_INIT (dshowaudiosrc_debug, "dshowaudiosrc", 0, - "Directshow audio source"); -} - -static void -gst_dshowaudiosrc_init (GstDshowAudioSrc * src, GstDshowAudioSrcClass * klass) -{ - src->device = NULL; - src->device_name = NULL; - src->audio_cap_filter = NULL; - src->dshow_fakesink = NULL; - src->media_filter = NULL; - src->filter_graph = NULL; - src->caps = NULL; - src->pins_mediatypes = NULL; - - src->gbarray = g_byte_array_new (); - src->gbarray_lock = g_mutex_new (); - - src->is_running = FALSE; - - CoInitializeEx (NULL, COINIT_MULTITHREADED); -} - -static void -gst_dshowaudiosrc_dispose (GObject * gobject) -{ - GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (gobject); - - if (src->device) { - g_free (src->device); - src->device = NULL; - } - - if (src->device_name) { - g_free (src->device_name); - src->device_name = NULL; - } - - if (src->caps) { - gst_caps_unref (src->caps); - src->caps = NULL; - } - - if (src->pins_mediatypes) { - gst_dshow_free_pins_mediatypes (src->pins_mediatypes); - src->pins_mediatypes = NULL; - } - - if (src->gbarray) { - g_byte_array_free (src->gbarray, TRUE); - src->gbarray = NULL; - } - - if (src->gbarray_lock) { - g_mutex_free (src->gbarray_lock); - src->gbarray_lock = NULL; - } - - /* clean dshow */ - if (src->audio_cap_filter) { - IBaseFilter_Release (src->audio_cap_filter); - } - - CoUninitialize (); - - G_OBJECT_CLASS (parent_class)->dispose (gobject); -} - - -static const GList * -gst_dshowaudiosrc_probe_get_properties (GstPropertyProbe * probe) -{ - GObjectClass *klass = G_OBJECT_GET_CLASS (probe); - static GList *props = NULL; - - if (!props) { - GParamSpec *pspec; - - pspec = g_object_class_find_property (klass, "device-name"); - props = g_list_append (props, pspec); - } - - return props; -} - -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 GValueArray * -gst_dshowaudiosrc_probe_get_values (GstPropertyProbe * probe, - guint prop_id, const GParamSpec * pspec) -{ - GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (probe); - GValueArray *array = NULL; - - switch (prop_id) { - case PROP_DEVICE_NAME: - array = gst_dshowaudiosrc_get_device_name_values (src); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec); - break; - } - - return array; -} - -static void -gst_dshowaudiosrc_set_property (GObject * object, guint prop_id, - const GValue * value, GParamSpec * pspec) -{ - GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (object); - - switch (prop_id) { - case PROP_DEVICE: - { - if (src->device) { - g_free (src->device); - src->device = NULL; - } - if (g_value_get_string (value)) { - src->device = g_strdup (g_value_get_string (value)); - } - break; - } - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -static void -gst_dshowaudiosrc_get_property (GObject * object, guint prop_id, - GValue * value, GParamSpec * pspec) -{ - -} - -static GstCaps * -gst_dshowaudiosrc_get_caps (GstBaseSrc * basesrc) -{ - HRESULT hres = S_OK; - IBindCtx *lpbc = NULL; - IMoniker *audiom = NULL; - DWORD dwEaten; - GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (basesrc); - gunichar2 *unidevice = NULL; - - if (src->device) { - g_free (src->device); - src->device = NULL; - } - - src->device = - gst_dshow_getdevice_from_devicename (&CLSID_AudioInputDeviceCategory, - &src->device_name); - if (!src->device) { - GST_CAT_ERROR (dshowaudiosrc_debug, "No audio device found."); - return NULL; - } - unidevice = - g_utf8_to_utf16 (src->device, strlen (src->device), NULL, NULL, NULL); - - if (!src->audio_cap_filter) { - hres = CreateBindCtx (0, &lpbc); - if (SUCCEEDED (hres)) { - hres = MkParseDisplayName (lpbc, unidevice, &dwEaten, &audiom); - if (SUCCEEDED (hres)) { - hres = - IMoniker_BindToObject (audiom, lpbc, NULL, &IID_IBaseFilter, - &src->audio_cap_filter); - IMoniker_Release (audiom); - } - IBindCtx_Release (lpbc); - } - } - - if (src->audio_cap_filter && !src->caps) { - /* get the capture pins supported types */ - IPin *capture_pin = NULL; - IEnumPins *enumpins = NULL; - HRESULT hres; - - hres = IBaseFilter_EnumPins (src->audio_cap_filter, &enumpins); - if (SUCCEEDED (hres)) { - while (IEnumPins_Next (enumpins, 1, &capture_pin, NULL) == S_OK) { - IKsPropertySet *pKs = NULL; - - hres = - IPin_QueryInterface (capture_pin, &IID_IKsPropertySet, - (void **) &pKs); - if (SUCCEEDED (hres) && pKs) { - DWORD cbReturned; - GUID pin_category; - RPC_STATUS rpcstatus; - - hres = - IKsPropertySet_Get (pKs, &ROPSETID_Pin, - AMPROPERTY_PIN_CATEGORY, NULL, 0, &pin_category, sizeof (GUID), - &cbReturned); - - /* we only want capture pins */ - if (UuidCompare (&pin_category, (UUID *) &PIN_CATEGORY_CAPTURE, - &rpcstatus) == 0) { - IAMStreamConfig *streamcaps = NULL; - - if (SUCCEEDED (IPin_QueryInterface (capture_pin, - &IID_IAMStreamConfig, (void **) &streamcaps))) { - src->caps = - gst_dshowaudiosrc_getcaps_from_streamcaps (src, capture_pin, - streamcaps); - IAMStreamConfig_Release (streamcaps); - } - } - IKsPropertySet_Release (pKs); - } - IPin_Release (capture_pin); - } - IEnumPins_Release (enumpins); - } - } - - if (unidevice) { - g_free (unidevice); - } - - if (src->caps) { - return gst_caps_ref (src->caps); - } - - return NULL; -} - -static GstStateChangeReturn -gst_dshowaudiosrc_change_state (GstElement * element, GstStateChange transition) -{ - HRESULT hres = S_FALSE; - GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (element); - - switch (transition) { - case GST_STATE_CHANGE_NULL_TO_READY: - break; - case GST_STATE_CHANGE_READY_TO_PAUSED: - break; - case GST_STATE_CHANGE_PAUSED_TO_PLAYING: - if (src->media_filter) - hres = IMediaFilter_Run (src->media_filter, 0); - if (hres != S_OK) { - GST_CAT_ERROR (dshowaudiosrc_debug, - "Can't RUN the directshow capture graph (error=%d)", hres); - src->is_running = FALSE; - return GST_STATE_CHANGE_FAILURE; - } else { - src->is_running = TRUE; - } - break; - case GST_STATE_CHANGE_PLAYING_TO_PAUSED: - if (src->media_filter) - hres = IMediaFilter_Stop (src->media_filter); - if (hres != S_OK) { - GST_CAT_ERROR (dshowaudiosrc_debug, - "Can't STOP the directshow capture graph (error=%d)", hres); - return GST_STATE_CHANGE_FAILURE; - } - src->is_running = FALSE; - - break; - case GST_STATE_CHANGE_PAUSED_TO_READY: - break; - case GST_STATE_CHANGE_READY_TO_NULL: - break; - default: - break; - } - - return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); -} - -static gboolean -gst_dshowaudiosrc_open (GstAudioSrc * asrc) -{ - HRESULT hres = S_FALSE; - GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (asrc); - - hres = CoCreateInstance (&CLSID_FilterGraph, NULL, CLSCTX_INPROC, - &IID_IFilterGraph, (LPVOID *) & src->filter_graph); - if (hres != S_OK || !src->filter_graph) { - GST_CAT_ERROR (dshowaudiosrc_debug, - "Can't create an instance of the directshow graph manager (error=%d)", - hres); - goto error; - } - - hres = IFilterGraph_QueryInterface (src->filter_graph, &IID_IMediaFilter, - (void **) &src->media_filter); - if (hres != S_OK || !src->media_filter) { - GST_CAT_ERROR (dshowaudiosrc_debug, - "Can't get IMediacontrol interface from the graph manager (error=%d)", - hres); - goto error; - } - - hres = CoCreateInstance (&CLSID_DshowFakeSink, NULL, CLSCTX_INPROC, - &IID_IBaseFilter, (LPVOID *) & src->dshow_fakesink); - if (hres != S_OK || !src->dshow_fakesink) { - GST_CAT_ERROR (dshowaudiosrc_debug, - "Can't create an instance of the directshow fakesink (error=%d)", hres); - goto error; - } - - hres = - IFilterGraph_AddFilter (src->filter_graph, src->audio_cap_filter, - L"capture"); - if (hres != S_OK) { - GST_CAT_ERROR (dshowaudiosrc_debug, - "Can't add the directshow capture filter to the graph (error=%d)", - hres); - goto error; - } - - hres = - IFilterGraph_AddFilter (src->filter_graph, src->dshow_fakesink, - L"fakesink"); - if (hres != S_OK) { - GST_CAT_ERROR (dshowaudiosrc_debug, - "Can't add our fakesink filter to the graph (error=%d)", hres); - goto error; - } - - return TRUE; - -error: - if (src->dshow_fakesink) { - IBaseFilter_Release (src->dshow_fakesink); - src->dshow_fakesink = NULL; - } - - if (src->media_filter) { - IMediaFilter_Release (src->media_filter); - src->media_filter = NULL; - } - if (src->filter_graph) { - IFilterGraph_Release (src->filter_graph); - src->filter_graph = NULL; - } - - return FALSE; -} - -static gboolean -gst_dshowaudiosrc_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec) -{ - HRESULT hres; - IGstDshowInterface *srcinterface = NULL; - IPin *input_pin = NULL; - GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (asrc); - - /* search the negociated caps in our caps list to get its index and the corresponding mediatype */ - if (gst_caps_is_subset (spec->caps, src->caps)) { - guint i = 0; - gint res = -1; - - for (; i < gst_caps_get_size (src->caps) && res == -1; i++) { - GstCaps *capstmp = gst_caps_copy_nth (src->caps, i); - - if (gst_caps_is_subset (spec->caps, capstmp)) { - res = i; - } - gst_caps_unref (capstmp); - } - - if (res != -1 && src->pins_mediatypes) { - /*get the corresponding media type and build the dshow graph */ - GstCapturePinMediaType *pin_mediatype = NULL; - GList *type = g_list_nth (src->pins_mediatypes, res); - - if (type) { - pin_mediatype = (GstCapturePinMediaType *) type->data; - - hres = - IBaseFilter_QueryInterface (src->dshow_fakesink, - &IID_IGstDshowInterface, (void **) &srcinterface); - if (hres != S_OK || !srcinterface) { - GST_CAT_ERROR (dshowaudiosrc_debug, - "Can't get IGstDshowInterface interface from our dshow fakesink filter (error=%d)", - hres); - goto error; - } - - IGstDshowInterface_gst_set_media_type (srcinterface, - pin_mediatype->mediatype); - IGstDshowInterface_gst_set_buffer_callback (srcinterface, - (byte *) gst_dshowaudiosrc_push_buffer, (byte *) src); - - if (srcinterface) { - IGstDshowInterface_Release (srcinterface); - } - - gst_dshow_get_pin_from_filter (src->dshow_fakesink, PINDIR_INPUT, - &input_pin); - if (!input_pin) { - GST_CAT_ERROR (dshowaudiosrc_debug, - "Can't get input pin from our directshow fakesink filter"); - goto error; - } - - hres = - IFilterGraph_ConnectDirect (src->filter_graph, - pin_mediatype->capture_pin, input_pin, NULL); - IPin_Release (input_pin); - - if (hres != S_OK) { - GST_CAT_ERROR (dshowaudiosrc_debug, - "Can't connect capture filter with fakesink filter (error=%d)", - hres); - goto error; - } - - spec->segsize = spec->rate * spec->channels; - spec->segtotal = 1; - } - } - } - - return TRUE; - -error: - if (srcinterface) { - IGstDshowInterface_Release (srcinterface); - } - - return FALSE; -} - -static gboolean -gst_dshowaudiosrc_unprepare (GstAudioSrc * asrc) -{ - IPin *input_pin = NULL, *output_pin = NULL; - HRESULT hres = S_FALSE; - GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (asrc); - - /* disconnect filters */ - gst_dshow_get_pin_from_filter (src->audio_cap_filter, PINDIR_OUTPUT, - &output_pin); - if (output_pin) { - hres = IFilterGraph_Disconnect (src->filter_graph, output_pin); - IPin_Release (output_pin); - } - - gst_dshow_get_pin_from_filter (src->dshow_fakesink, PINDIR_INPUT, &input_pin); - if (input_pin) { - hres = IFilterGraph_Disconnect (src->filter_graph, input_pin); - IPin_Release (input_pin); - } - - return TRUE; -} - -static gboolean -gst_dshowaudiosrc_close (GstAudioSrc * asrc) -{ - GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (asrc); - - if (!src->filter_graph) - return TRUE; - - /*remove filters from the graph */ - IFilterGraph_RemoveFilter (src->filter_graph, src->audio_cap_filter); - IFilterGraph_RemoveFilter (src->filter_graph, src->dshow_fakesink); - - /*release our gstreamer dshow sink */ - IBaseFilter_Release (src->dshow_fakesink); - src->dshow_fakesink = NULL; - - /*release media filter interface */ - IMediaFilter_Release (src->media_filter); - src->media_filter = NULL; - - /*release the filter graph manager */ - IFilterGraph_Release (src->filter_graph); - src->filter_graph = NULL; - - return TRUE; -} - -static guint -gst_dshowaudiosrc_read (GstAudioSrc * asrc, gpointer data, guint length) -{ - GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (asrc); - guint ret = 0; - - if (!src->is_running) - return -1; - - if (src->gbarray) { - test: - if (src->gbarray->len >= length) { - g_mutex_lock (src->gbarray_lock); - memcpy (data, src->gbarray->data + (src->gbarray->len - length), length); - g_byte_array_remove_range (src->gbarray, src->gbarray->len - length, - length); - ret = length; - g_mutex_unlock (src->gbarray_lock); - } else { - if (src->is_running) { - Sleep (100); - goto test; - } - } - } - - return ret; -} - -static guint -gst_dshowaudiosrc_delay (GstAudioSrc * asrc) -{ - GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (asrc); - guint ret = 0; - - if (src->gbarray) { - g_mutex_lock (src->gbarray_lock); - if (src->gbarray->len) { - ret = src->gbarray->len / 4; - } - g_mutex_unlock (src->gbarray_lock); - } - - return ret; -} - -static void -gst_dshowaudiosrc_reset (GstAudioSrc * asrc) -{ - GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (asrc); - - g_mutex_lock (src->gbarray_lock); - g_byte_array_remove_range (src->gbarray, 0, src->gbarray->len); - g_mutex_unlock (src->gbarray_lock); -} - -static GstCaps * -gst_dshowaudiosrc_getcaps_from_streamcaps (GstDshowAudioSrc * src, IPin * pin, - IAMStreamConfig * streamcaps) -{ - GstCaps *caps = NULL; - HRESULT hres = S_OK; - RPC_STATUS rpcstatus; - int icount = 0; - int isize = 0; - AUDIO_STREAM_CONFIG_CAPS ascc; - int i = 0; - - if (!streamcaps) - return NULL; - - IAMStreamConfig_GetNumberOfCapabilities (streamcaps, &icount, &isize); - - if (isize != sizeof (ascc)) - return NULL; - - for (; i < icount; i++) { - GstCapturePinMediaType *pin_mediatype = g_new0 (GstCapturePinMediaType, 1); - - IPin_AddRef (pin); - pin_mediatype->capture_pin = pin; - - hres = - IAMStreamConfig_GetStreamCaps (streamcaps, i, &pin_mediatype->mediatype, - (BYTE *) & ascc); - if (hres == S_OK && pin_mediatype->mediatype) { - GstCaps *mediacaps = NULL; - - if (!caps) - caps = gst_caps_new_empty (); - - if ((UuidCompare (&pin_mediatype->mediatype->subtype, (UUID *) &MEDIASUBTYPE_PCM, - &rpcstatus) == 0 && rpcstatus == RPC_S_OK) - && (UuidCompare (&pin_mediatype->mediatype->formattype, - (UUID *) &FORMAT_WaveFormatEx, &rpcstatus) == 0 - && rpcstatus == RPC_S_OK)) { - WAVEFORMATEX *wavformat = - (WAVEFORMATEX *) pin_mediatype->mediatype->pbFormat; - mediacaps = - gst_caps_new_simple ("audio/x-raw-int", "width", G_TYPE_INT, - wavformat->wBitsPerSample, "depth", G_TYPE_INT, - wavformat->wBitsPerSample, "endianness", G_TYPE_INT, G_BYTE_ORDER, - "signed", G_TYPE_BOOLEAN, TRUE, "channels", G_TYPE_INT, - wavformat->nChannels, "rate", G_TYPE_INT, wavformat->nSamplesPerSec, - NULL); - - if (mediacaps) { - src->pins_mediatypes = - g_list_append (src->pins_mediatypes, pin_mediatype); - gst_caps_append (caps, mediacaps); - } else { - gst_dshow_free_pin_mediatype (pin_mediatype); - } - } else { - gst_dshow_free_pin_mediatype (pin_mediatype); - } - } else { - gst_dshow_free_pin_mediatype (pin_mediatype); - } - } - - if (caps && gst_caps_is_empty (caps)) { - gst_caps_unref (caps); - caps = NULL; - } - - return caps; -} - -static gboolean -gst_dshowaudiosrc_push_buffer (byte * buffer, long size, byte * src_object, - UINT64 start, UINT64 stop) -{ - GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (src_object); - - if (!buffer || size == 0 || !src) { - return FALSE; - } - - g_mutex_lock (src->gbarray_lock); - g_byte_array_prepend (src->gbarray, (guint8 *) buffer, size); - g_mutex_unlock (src->gbarray_lock); - - return TRUE; -} diff --git a/sys/dshowsrcwrapper/gstdshowaudiosrc.cpp b/sys/dshowsrcwrapper/gstdshowaudiosrc.cpp new file mode 100755 index 00000000..8961c96d --- /dev/null +++ b/sys/dshowsrcwrapper/gstdshowaudiosrc.cpp @@ -0,0 +1,859 @@ +/* GStreamer + * Copyright (C) 2007 Sebastien Moutte + * + * gstdshowaudiosrc.c: + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gstdshowaudiosrc.h" + +static const GstElementDetails gst_dshowaudiosrc_details = +GST_ELEMENT_DETAILS ("Directshow audio capture source", + "Source/Audio", + "Receive data from a directshow audio capture graph", + "Sebastien Moutte "); + +GST_DEBUG_CATEGORY_STATIC (dshowaudiosrc_debug); +#define GST_CAT_DEFAULT dshowaudiosrc_debug + +static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("audio/x-raw-int, " + "endianness = (int) { " G_STRINGIFY (G_BYTE_ORDER) " }, " + "signed = (boolean) { TRUE, FALSE }, " + "width = (int) 16, " + "depth = (int) 16, " + "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]; " + "audio/x-raw-int, " + "signed = (boolean) { TRUE, FALSE }, " + "width = (int) 8, " + "depth = (int) 8, " + "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]") + ); + +static void gst_dshowaudiosrc_init_interfaces (GType type); + +GST_BOILERPLATE_FULL (GstDshowAudioSrc, gst_dshowaudiosrc, GstAudioSrc, + GST_TYPE_AUDIO_SRC, gst_dshowaudiosrc_init_interfaces); + +enum +{ + PROP_0, + PROP_DEVICE, + PROP_DEVICE_NAME +}; + +static void gst_dshowaudiosrc_probe_interface_init (GstPropertyProbeInterface * + iface); +static const GList *gst_dshowaudiosrc_probe_get_properties (GstPropertyProbe * + probe); +static GValueArray *gst_dshowaudiosrc_probe_get_values (GstPropertyProbe * + probe, guint prop_id, const GParamSpec * pspec); +static GValueArray *gst_dshowaudiosrc_get_device_name_values (GstDshowAudioSrc * + src); + + +static void gst_dshowaudiosrc_dispose (GObject * gobject); +static void gst_dshowaudiosrc_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec); +static void gst_dshowaudiosrc_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec); +static GstCaps *gst_dshowaudiosrc_get_caps (GstBaseSrc * src); +static GstStateChangeReturn gst_dshowaudiosrc_change_state (GstElement * + element, GstStateChange transition); + +static gboolean gst_dshowaudiosrc_open (GstAudioSrc * asrc); +static gboolean gst_dshowaudiosrc_prepare (GstAudioSrc * asrc, + GstRingBufferSpec * spec); +static gboolean gst_dshowaudiosrc_unprepare (GstAudioSrc * asrc); +static gboolean gst_dshowaudiosrc_close (GstAudioSrc * asrc); +static guint gst_dshowaudiosrc_read (GstAudioSrc * asrc, gpointer data, + guint length); +static guint gst_dshowaudiosrc_delay (GstAudioSrc * asrc); +static void gst_dshowaudiosrc_reset (GstAudioSrc * asrc); + +/* utils */ +static GstCaps *gst_dshowaudiosrc_getcaps_from_streamcaps (GstDshowAudioSrc * + src, IPin * pin, IAMStreamConfig * streamcaps); +static gboolean gst_dshowaudiosrc_push_buffer (byte * buffer, long size, + byte * src_object, UINT64 start, UINT64 stop); + +static void +gst_dshowaudiosrc_init_interfaces (GType type) +{ + static const GInterfaceInfo dshowaudiosrc_info = { + (GInterfaceInitFunc) gst_dshowaudiosrc_probe_interface_init, + NULL, + NULL, + }; + + g_type_add_interface_static (type, + GST_TYPE_PROPERTY_PROBE, &dshowaudiosrc_info); +} + +static void +gst_dshowaudiosrc_probe_interface_init (GstPropertyProbeInterface * iface) +{ + iface->get_properties = gst_dshowaudiosrc_probe_get_properties; +/* iface->needs_probe = gst_dshowaudiosrc_probe_needs_probe; + iface->probe_property = gst_dshowaudiosrc_probe_probe_property;*/ + iface->get_values = gst_dshowaudiosrc_probe_get_values; +} + +static void +gst_dshowaudiosrc_base_init (gpointer klass) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (klass); + + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&src_template)); + + gst_element_class_set_details (element_class, &gst_dshowaudiosrc_details); +} + +static void +gst_dshowaudiosrc_class_init (GstDshowAudioSrcClass * klass) +{ + GObjectClass *gobject_class; + GstElementClass *gstelement_class; + GstBaseSrcClass *gstbasesrc_class; + GstAudioSrcClass *gstaudiosrc_class; + + gobject_class = (GObjectClass *) klass; + gstelement_class = (GstElementClass *) klass; + gstbasesrc_class = (GstBaseSrcClass *) klass; + gstaudiosrc_class = (GstAudioSrcClass *) klass; + + gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_dispose); + gobject_class->set_property = + GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_set_property); + gobject_class->get_property = + GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_get_property); + + gstelement_class->change_state = + GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_change_state); + + gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_get_caps); + + gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_open); + gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_prepare); + gstaudiosrc_class->unprepare = + GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_unprepare); + gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_close); + gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_read); + gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_delay); + gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_dshowaudiosrc_reset); + + g_object_class_install_property + (gobject_class, PROP_DEVICE, + g_param_spec_string ("device", "Device", + "Directshow device reference (classID/name)", NULL, + static_cast(G_PARAM_READWRITE))); + + g_object_class_install_property + (gobject_class, PROP_DEVICE_NAME, + g_param_spec_string ("device-name", "Device name", + "Human-readable name of the sound device", NULL, + static_cast(G_PARAM_READWRITE))); + + GST_DEBUG_CATEGORY_INIT (dshowaudiosrc_debug, "dshowaudiosrc", 0, + "Directshow audio source"); +} + +static void +gst_dshowaudiosrc_init (GstDshowAudioSrc * src, GstDshowAudioSrcClass * klass) +{ + src->device = NULL; + src->device_name = NULL; + src->audio_cap_filter = NULL; + src->dshow_fakesink = NULL; + src->media_filter = NULL; + src->filter_graph = NULL; + src->caps = NULL; + src->pins_mediatypes = NULL; + + src->gbarray = g_byte_array_new (); + src->gbarray_lock = g_mutex_new (); + + src->is_running = FALSE; + + CoInitializeEx (NULL, COINIT_MULTITHREADED); +} + +static void +gst_dshowaudiosrc_dispose (GObject * gobject) +{ + GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (gobject); + + if (src->device) { + g_free (src->device); + src->device = NULL; + } + + if (src->device_name) { + g_free (src->device_name); + src->device_name = NULL; + } + + if (src->caps) { + gst_caps_unref (src->caps); + src->caps = NULL; + } + + if (src->pins_mediatypes) { + gst_dshow_free_pins_mediatypes (src->pins_mediatypes); + src->pins_mediatypes = NULL; + } + + if (src->gbarray) { + g_byte_array_free (src->gbarray, TRUE); + src->gbarray = NULL; + } + + if (src->gbarray_lock) { + g_mutex_free (src->gbarray_lock); + src->gbarray_lock = NULL; + } + + /* clean dshow */ + if (src->audio_cap_filter) + src->audio_cap_filter->Release(); + + CoUninitialize (); + + G_OBJECT_CLASS (parent_class)->dispose (gobject); +} + + +static const GList * +gst_dshowaudiosrc_probe_get_properties (GstPropertyProbe * probe) +{ + GObjectClass *klass = G_OBJECT_GET_CLASS (probe); + static GList *props = NULL; + + if (!props) { + GParamSpec *pspec; + + pspec = g_object_class_find_property (klass, "device-name"); + props = g_list_append (props, pspec); + } + + return props; +} + +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, (LPVOID *) &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 = devices_enum->CreateClassEnumerator(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; + } + + moniker_enum->Reset(); + + while (hres = moniker_enum->Next(1, &moniker, &fetched), + hres == S_OK) { + IPropertyBag *property_bag = NULL; + + hres = moniker->BindToStorage(NULL, NULL, IID_IPropertyBag, + (LPVOID *) &property_bag); + if (SUCCEEDED (hres) && property_bag) { + VARIANT varFriendlyName; + + VariantInit (&varFriendlyName); + hres = property_bag->Read(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); + } + property_bag->Release(); + } + moniker->Release(); + } + +clean: + if (moniker_enum) + moniker_enum->Release(); + + if (devices_enum) + devices_enum->Release(); + + return array; +} + +static GValueArray * +gst_dshowaudiosrc_probe_get_values (GstPropertyProbe * probe, + guint prop_id, const GParamSpec * pspec) +{ + GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (probe); + GValueArray *array = NULL; + + switch (prop_id) { + case PROP_DEVICE_NAME: + array = gst_dshowaudiosrc_get_device_name_values (src); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec); + break; + } + + return array; +} + +static void +gst_dshowaudiosrc_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (object); + + switch (prop_id) { + case PROP_DEVICE: + { + if (src->device) { + g_free (src->device); + src->device = NULL; + } + if (g_value_get_string (value)) { + src->device = g_strdup (g_value_get_string (value)); + } + break; + } + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_dshowaudiosrc_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec) +{ + +} + +static GstCaps * +gst_dshowaudiosrc_get_caps (GstBaseSrc * basesrc) +{ + HRESULT hres = S_OK; + IBindCtx *lpbc = NULL; + IMoniker *audiom = NULL; + DWORD dwEaten; + GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (basesrc); + gunichar2 *unidevice = NULL; + + if (src->device) { + g_free (src->device); + src->device = NULL; + } + + src->device = + gst_dshow_getdevice_from_devicename (&CLSID_AudioInputDeviceCategory, + &src->device_name); + if (!src->device) { + GST_CAT_ERROR (dshowaudiosrc_debug, "No audio device found."); + return NULL; + } + unidevice = + g_utf8_to_utf16 (src->device, strlen (src->device), NULL, NULL, NULL); + + if (!src->audio_cap_filter) { + hres = CreateBindCtx (0, &lpbc); + if (SUCCEEDED (hres)) { + hres = MkParseDisplayName (lpbc, (LPCOLESTR) unidevice, &dwEaten, &audiom); + if (SUCCEEDED (hres)) { + hres = audiom->BindToObject(lpbc, NULL, IID_IBaseFilter, + (LPVOID *) &src->audio_cap_filter); + audiom->Release(); + } + lpbc->Release(); + } + } + + if (src->audio_cap_filter && !src->caps) { + /* get the capture pins supported types */ + IPin *capture_pin = NULL; + IEnumPins *enumpins = NULL; + HRESULT hres; + + hres = src->audio_cap_filter->EnumPins(&enumpins); + if (SUCCEEDED (hres)) { + while (enumpins->Next(1, &capture_pin, NULL) == S_OK) { + IKsPropertySet *pKs = NULL; + + hres = capture_pin->QueryInterface(IID_IKsPropertySet, (LPVOID *) &pKs); + if (SUCCEEDED (hres) && pKs) { + DWORD cbReturned; + GUID pin_category; + RPC_STATUS rpcstatus; + + hres = + pKs->Get(AMPROPSETID_Pin, + AMPROPERTY_PIN_CATEGORY, NULL, 0, &pin_category, sizeof (GUID), + &cbReturned); + + /* we only want capture pins */ + if (UuidCompare (&pin_category, (UUID *) &PIN_CATEGORY_CAPTURE, + &rpcstatus) == 0) { + IAMStreamConfig *streamcaps = NULL; + + if (SUCCEEDED (capture_pin->QueryInterface(IID_IAMStreamConfig, (LPVOID *) &streamcaps))) { + src->caps = + gst_dshowaudiosrc_getcaps_from_streamcaps (src, capture_pin, + streamcaps); + streamcaps->Release(); + } + } + pKs->Release(); + } + capture_pin->Release(); + } + enumpins->Release(); + } + } + + if (unidevice) { + g_free (unidevice); + } + + if (src->caps) { + return gst_caps_ref (src->caps); + } + + return NULL; +} + +static GstStateChangeReturn +gst_dshowaudiosrc_change_state (GstElement * element, GstStateChange transition) +{ + HRESULT hres = S_FALSE; + GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (element); + + switch (transition) { + case GST_STATE_CHANGE_NULL_TO_READY: + break; + case GST_STATE_CHANGE_READY_TO_PAUSED: + break; + case GST_STATE_CHANGE_PAUSED_TO_PLAYING: + if (src->media_filter) + hres = src->media_filter->Run(0); + if (hres != S_OK) { + GST_CAT_ERROR (dshowaudiosrc_debug, + "Can't RUN the directshow capture graph (error=%d)", hres); + src->is_running = FALSE; + return GST_STATE_CHANGE_FAILURE; + } else { + src->is_running = TRUE; + } + break; + case GST_STATE_CHANGE_PLAYING_TO_PAUSED: + if (src->media_filter) + hres = src->media_filter->Stop(); + if (hres != S_OK) { + GST_CAT_ERROR (dshowaudiosrc_debug, + "Can't STOP the directshow capture graph (error=%d)", hres); + return GST_STATE_CHANGE_FAILURE; + } + src->is_running = FALSE; + + break; + case GST_STATE_CHANGE_PAUSED_TO_READY: + break; + case GST_STATE_CHANGE_READY_TO_NULL: + break; + default: + break; + } + + return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); +} + +static gboolean +gst_dshowaudiosrc_open (GstAudioSrc * asrc) +{ + HRESULT hres = S_FALSE; + GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (asrc); + + hres = CoCreateInstance (CLSID_FilterGraph, NULL, CLSCTX_INPROC, + IID_IFilterGraph, (LPVOID *) & src->filter_graph); + if (hres != S_OK || !src->filter_graph) { + GST_CAT_ERROR (dshowaudiosrc_debug, + "Can't create an instance of the directshow graph manager (error=%d)", + hres); + goto error; + } + + hres = src->filter_graph->QueryInterface(IID_IMediaFilter, (LPVOID *) &src->media_filter); + if (hres != S_OK || !src->media_filter) { + GST_CAT_ERROR (dshowaudiosrc_debug, + "Can't get IMediacontrol interface from the graph manager (error=%d)", + hres); + goto error; + } + + hres = CoCreateInstance (CLSID_DshowFakeSink, NULL, CLSCTX_INPROC, + IID_IBaseFilter, (LPVOID *) & src->dshow_fakesink); + if (hres != S_OK || !src->dshow_fakesink) { + GST_CAT_ERROR (dshowaudiosrc_debug, + "Can't create an instance of the directshow fakesink (error=%d)", hres); + goto error; + } + + hres = src->filter_graph->AddFilter(src->audio_cap_filter, L"capture"); + if (hres != S_OK) { + GST_CAT_ERROR (dshowaudiosrc_debug, + "Can't add the directshow capture filter to the graph (error=%d)", + hres); + goto error; + } + + hres = src->filter_graph->AddFilter(src->dshow_fakesink, L"fakesink"); + if (hres != S_OK) { + GST_CAT_ERROR (dshowaudiosrc_debug, + "Can't add our fakesink filter to the graph (error=%d)", hres); + goto error; + } + + return TRUE; + +error: + if (src->dshow_fakesink) { + src->dshow_fakesink->Release(); + src->dshow_fakesink = NULL; + } + + if (src->media_filter) { + src->media_filter->Release(); + src->media_filter = NULL; + } + if (src->filter_graph) { + src->filter_graph->Release(); + src->filter_graph = NULL; + } + + return FALSE; +} + +static gboolean +gst_dshowaudiosrc_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec) +{ + HRESULT hres; + IGstDshowInterface *srcinterface = NULL; + IPin *input_pin = NULL; + GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (asrc); + + /* search the negociated caps in our caps list to get its index and the corresponding mediatype */ + if (gst_caps_is_subset (spec->caps, src->caps)) { + guint i = 0; + gint res = -1; + + for (; i < gst_caps_get_size (src->caps) && res == -1; i++) { + GstCaps *capstmp = gst_caps_copy_nth (src->caps, i); + + if (gst_caps_is_subset (spec->caps, capstmp)) { + res = i; + } + gst_caps_unref (capstmp); + } + + if (res != -1 && src->pins_mediatypes) { + /*get the corresponding media type and build the dshow graph */ + GstCapturePinMediaType *pin_mediatype = NULL; + GList *type = g_list_nth (src->pins_mediatypes, res); + + if (type) { + pin_mediatype = (GstCapturePinMediaType *) type->data; + + hres = src->dshow_fakesink->QueryInterface(IID_IGstDshowInterface, (LPVOID *) &srcinterface); + if (hres != S_OK || !srcinterface) { + GST_CAT_ERROR (dshowaudiosrc_debug, + "Can't get IGstDshowInterface interface from our dshow fakesink filter (error=%d)", + hres); + goto error; + } + + srcinterface->gst_set_media_type(pin_mediatype->mediatype); + srcinterface->gst_set_buffer_callback( + (push_buffer_func) gst_dshowaudiosrc_push_buffer, (byte *) src); + + if (srcinterface) + srcinterface->Release(); + + gst_dshow_get_pin_from_filter (src->dshow_fakesink, PINDIR_INPUT, + &input_pin); + if (!input_pin) { + GST_CAT_ERROR (dshowaudiosrc_debug, + "Can't get input pin from our directshow fakesink filter"); + goto error; + } + + hres = src->filter_graph->ConnectDirect(pin_mediatype->capture_pin, + input_pin, NULL); + input_pin->Release(); + + if (hres != S_OK) { + GST_CAT_ERROR (dshowaudiosrc_debug, + "Can't connect capture filter with fakesink filter (error=%d)", + hres); + goto error; + } + + spec->segsize = spec->rate * spec->channels; + spec->segtotal = 1; + } + } + } + + return TRUE; + +error: + if (srcinterface) + srcinterface->Release(); + + return FALSE; +} + +static gboolean +gst_dshowaudiosrc_unprepare (GstAudioSrc * asrc) +{ + IPin *input_pin = NULL, *output_pin = NULL; + HRESULT hres = S_FALSE; + GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (asrc); + + /* disconnect filters */ + gst_dshow_get_pin_from_filter (src->audio_cap_filter, PINDIR_OUTPUT, + &output_pin); + if (output_pin) { + hres = src->filter_graph->Disconnect(output_pin); + output_pin->Release(); + } + + gst_dshow_get_pin_from_filter (src->dshow_fakesink, PINDIR_INPUT, &input_pin); + if (input_pin) { + hres = src->filter_graph->Disconnect(input_pin); + input_pin->Release(); + } + + return TRUE; +} + +static gboolean +gst_dshowaudiosrc_close (GstAudioSrc * asrc) +{ + GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (asrc); + + if (!src->filter_graph) + return TRUE; + + /*remove filters from the graph */ + src->filter_graph->RemoveFilter(src->audio_cap_filter); + src->filter_graph->RemoveFilter(src->dshow_fakesink); + + /*release our gstreamer dshow sink */ + src->dshow_fakesink->Release(); + src->dshow_fakesink = NULL; + + /*release media filter interface */ + src->media_filter->Release(); + src->media_filter = NULL; + + /*release the filter graph manager */ + src->filter_graph->Release(); + src->filter_graph = NULL; + + return TRUE; +} + +static guint +gst_dshowaudiosrc_read (GstAudioSrc * asrc, gpointer data, guint length) +{ + GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (asrc); + guint ret = 0; + + if (!src->is_running) + return -1; + + if (src->gbarray) { + test: + if (src->gbarray->len >= length) { + g_mutex_lock (src->gbarray_lock); + memcpy (data, src->gbarray->data + (src->gbarray->len - length), length); + g_byte_array_remove_range (src->gbarray, src->gbarray->len - length, + length); + ret = length; + g_mutex_unlock (src->gbarray_lock); + } else { + if (src->is_running) { + Sleep (100); + goto test; + } + } + } + + return ret; +} + +static guint +gst_dshowaudiosrc_delay (GstAudioSrc * asrc) +{ + GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (asrc); + guint ret = 0; + + if (src->gbarray) { + g_mutex_lock (src->gbarray_lock); + if (src->gbarray->len) { + ret = src->gbarray->len / 4; + } + g_mutex_unlock (src->gbarray_lock); + } + + return ret; +} + +static void +gst_dshowaudiosrc_reset (GstAudioSrc * asrc) +{ + GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (asrc); + + g_mutex_lock (src->gbarray_lock); + g_byte_array_remove_range (src->gbarray, 0, src->gbarray->len); + g_mutex_unlock (src->gbarray_lock); +} + +static GstCaps * +gst_dshowaudiosrc_getcaps_from_streamcaps (GstDshowAudioSrc * src, IPin * pin, + IAMStreamConfig * streamcaps) +{ + GstCaps *caps = NULL; + HRESULT hres = S_OK; + RPC_STATUS rpcstatus; + int icount = 0; + int isize = 0; + AUDIO_STREAM_CONFIG_CAPS ascc; + int i = 0; + + if (!streamcaps) + return NULL; + + streamcaps->GetNumberOfCapabilities(&icount, &isize); + + if (isize != sizeof (ascc)) + return NULL; + + for (; i < icount; i++) { + GstCapturePinMediaType *pin_mediatype = g_new0 (GstCapturePinMediaType, 1); + + pin->AddRef(); + pin_mediatype->capture_pin = pin; + + hres = streamcaps->GetStreamCaps(i, &pin_mediatype->mediatype, + (BYTE *) & ascc); + if (hres == S_OK && pin_mediatype->mediatype) { + GstCaps *mediacaps = NULL; + + if (!caps) + caps = gst_caps_new_empty (); + + if ((UuidCompare (&pin_mediatype->mediatype->subtype, (UUID *) &MEDIASUBTYPE_PCM, + &rpcstatus) == 0 && rpcstatus == RPC_S_OK) + && (UuidCompare (&pin_mediatype->mediatype->formattype, + (UUID *) &FORMAT_WaveFormatEx, &rpcstatus) == 0 + && rpcstatus == RPC_S_OK)) { + WAVEFORMATEX *wavformat = + (WAVEFORMATEX *) pin_mediatype->mediatype->pbFormat; + mediacaps = + gst_caps_new_simple ("audio/x-raw-int", "width", G_TYPE_INT, + wavformat->wBitsPerSample, "depth", G_TYPE_INT, + wavformat->wBitsPerSample, "endianness", G_TYPE_INT, G_BYTE_ORDER, + "signed", G_TYPE_BOOLEAN, TRUE, "channels", G_TYPE_INT, + wavformat->nChannels, "rate", G_TYPE_INT, wavformat->nSamplesPerSec, + NULL); + + if (mediacaps) { + src->pins_mediatypes = + g_list_append (src->pins_mediatypes, pin_mediatype); + gst_caps_append (caps, mediacaps); + } else { + gst_dshow_free_pin_mediatype (pin_mediatype); + } + } else { + gst_dshow_free_pin_mediatype (pin_mediatype); + } + } else { + gst_dshow_free_pin_mediatype (pin_mediatype); + } + } + + if (caps && gst_caps_is_empty (caps)) { + gst_caps_unref (caps); + caps = NULL; + } + + return caps; +} + +static gboolean +gst_dshowaudiosrc_push_buffer (byte * buffer, long size, byte * src_object, + UINT64 start, UINT64 stop) +{ + GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (src_object); + + if (!buffer || size == 0 || !src) { + return FALSE; + } + + g_mutex_lock (src->gbarray_lock); + g_byte_array_prepend (src->gbarray, (guint8 *) buffer, size); + g_mutex_unlock (src->gbarray_lock); + + return TRUE; +} -- cgit v1.2.1 From 4d926e3b0d57b70ba6cbe1351a98d77c0e34d019 Mon Sep 17 00:00:00 2001 From: Julien Isorce Date: Wed, 22 Jul 2009 12:34:36 +0200 Subject: dshowaudiosrc: do not remove a range of length 0 --- sys/dshowsrcwrapper/gstdshowaudiosrc.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'sys/dshowsrcwrapper') diff --git a/sys/dshowsrcwrapper/gstdshowaudiosrc.cpp b/sys/dshowsrcwrapper/gstdshowaudiosrc.cpp index 8961c96d..c213aae5 100755 --- a/sys/dshowsrcwrapper/gstdshowaudiosrc.cpp +++ b/sys/dshowsrcwrapper/gstdshowaudiosrc.cpp @@ -765,7 +765,9 @@ gst_dshowaudiosrc_reset (GstAudioSrc * asrc) GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (asrc); g_mutex_lock (src->gbarray_lock); - g_byte_array_remove_range (src->gbarray, 0, src->gbarray->len); + GST_DEBUG ("byte array size= %d", src->gbarray->len); + if (src->gbarray->len > 0) + g_byte_array_remove_range (src->gbarray, 0, src->gbarray->len); g_mutex_unlock (src->gbarray_lock); } -- cgit v1.2.1 From 021f66d6ce84370803d436a954286da70872a4d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Wed, 22 Jul 2009 11:45:21 +0100 Subject: dshowsrcwrapper: update Makefile.am for source file renames as well --- sys/dshowsrcwrapper/Makefile.am | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'sys/dshowsrcwrapper') diff --git a/sys/dshowsrcwrapper/Makefile.am b/sys/dshowsrcwrapper/Makefile.am index 3ec4d64a..74bf83ed 100755 --- a/sys/dshowsrcwrapper/Makefile.am +++ b/sys/dshowsrcwrapper/Makefile.am @@ -2,7 +2,7 @@ # ensure everything's listed in EXTRA_DIST EXTRA_DIST = \ - gstdshowaudiosrc.c \ + gstdshowaudiosrc.cpp \ gstdshowaudiosrc.h \ gstdshow.cpp \ gstdshowfakesink.cpp \ @@ -10,5 +10,5 @@ EXTRA_DIST = \ gstdshow.h \ gstdshowinterface.h \ gstdshowsrcwrapper.cpp \ - gstdshowvideosrc.c \ + gstdshowvideosrc.cpp \ gstdshowvideosrc.h -- cgit v1.2.1