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 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; } 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 guint gst_dshowaudiosrc_read (GstAudioSrc * asrc, gpointer data, guint length, GstClockTime *timestamp) { 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 (GST_AUDIO_BASE_SRC(src)->ringbuffer->spec.latency_time / GST_MSECOND / 10); goto test; } } } return ret; }
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 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 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 gboolean gst_dshowaudiosrc_push_buffer (guint8 * buffer, guint size, gpointer src_object, GstClockTime duration) { 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, buffer, size); g_mutex_unlock (src->gbarray_lock); return TRUE; }
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; }
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_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 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_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 GstCaps * gst_dshowaudiosrc_get_caps (GstBaseSrc * basesrc, GstCaps * filter) { 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_ERROR ("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) { GstCaps *caps; if (filter) { caps = gst_caps_intersect_full (filter, src->caps, GST_CAPS_INTERSECT_FIRST); } else { caps = gst_caps_ref (src->caps); } return caps; } return NULL; }
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_ERROR ("Can't create an instance of the directshow graph manager (error=0x%x)", hres); goto error; } hres = src->filter_graph->QueryInterface (IID_IMediaFilter, (LPVOID *) & src->media_filter); if (hres != S_OK || !src->media_filter) { GST_ERROR ("Can't get IMediacontrol interface from the graph manager (error=0x%x)", hres); goto error; } src->dshow_fakesink = new CDshowFakeSink; src->dshow_fakesink->AddRef (); hres = src->filter_graph->AddFilter (src->audio_cap_filter, L"capture"); if (hres != S_OK) { GST_ERROR ("Can't add the directshow capture filter to the graph (error=0x%x)", hres); goto error; } hres = src->filter_graph->AddFilter (src->dshow_fakesink, L"fakesink"); if (hres != S_OK) { GST_ERROR ("Can't add our fakesink filter to the graph (error=0x%x)", 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 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, &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 gboolean gst_dshowaudiosrc_prepare (GstAudioSrc * asrc, GstAudioRingBufferSpec * spec) { HRESULT hres; IPin *input_pin = NULL; GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (asrc); GstCaps *current_caps = gst_pad_get_current_caps (GST_BASE_SRC_PAD (asrc)); if (current_caps) { if (gst_caps_is_equal (spec->caps, current_caps)) { gst_caps_unref (current_caps); return TRUE; } gst_caps_unref (current_caps); } /* In 1.0, prepare() seems to be called in the PLAYING state. Most of the time you can't do much on a running graph. */ gboolean was_running = src->is_running; if (was_running) { HRESULT hres = src->media_filter->Stop (); if (hres != S_OK) { GST_ERROR("Can't STOP the directshow capture graph for preparing (error=0x%x)", hres); return FALSE; } src->is_running = FALSE; } /* 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; src->dshow_fakesink->gst_set_media_type (pin_mediatype->mediatype); src->dshow_fakesink->gst_set_buffer_callback ( (push_buffer_func) gst_dshowaudiosrc_push_buffer, src); gst_dshow_get_pin_from_filter (src->dshow_fakesink, PINDIR_INPUT, &input_pin); if (!input_pin) { GST_ERROR ("Can't get input pin from our directshow fakesink filter"); goto error; } spec->segsize = (gint) (spec->info.bpf * spec->info.rate * spec->latency_time / GST_MSECOND); spec->segtotal = (gint) ((gfloat) spec->buffer_time / (gfloat) spec->latency_time + 0.5); if (!gst_dshow_configure_latency (pin_mediatype->capture_pin, spec->segsize)) { GST_WARNING ("Could not change capture latency"); spec->segsize = spec->info.rate * spec->info.channels; spec->segtotal = 2; }; GST_INFO ("Configuring with segsize:%d segtotal:%d", spec->segsize, spec->segtotal); if (gst_dshow_is_pin_connected (pin_mediatype->capture_pin)) { GST_DEBUG_OBJECT (src, "capture_pin already connected, disconnecting"); src->filter_graph->Disconnect (pin_mediatype->capture_pin); } if (gst_dshow_is_pin_connected (input_pin)) { GST_DEBUG_OBJECT (src, "input_pin already connected, disconnecting"); src->filter_graph->Disconnect (input_pin); } hres = src->filter_graph->ConnectDirect (pin_mediatype->capture_pin, input_pin, NULL); input_pin->Release (); if (hres != S_OK) { GST_ERROR ("Can't connect capture filter with fakesink filter (error=0x%x)", hres); goto error; } } } } if (was_running) { HRESULT hres = src->media_filter->Run (0); if (hres != S_OK) { GST_ERROR("Can't RUN the directshow capture graph after prepare (error=0x%x)", hres); return FALSE; } src->is_running = TRUE; } return TRUE; error: /* Don't restart the graph, we're out anyway. */ return FALSE; }
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; }