Signed-off-by: Derek Lesho dlesho@codeweavers.com --- dlls/winegstreamer/audioconvert.c | 174 ++++++++++++++++++++++++++++-- dlls/winegstreamer/gst_private.h | 1 + dlls/winegstreamer/mfplat.c | 69 ++++++++++++ 3 files changed, 238 insertions(+), 6 deletions(-)
diff --git a/dlls/winegstreamer/audioconvert.c b/dlls/winegstreamer/audioconvert.c index 141cdef48db..087b274901c 100644 --- a/dlls/winegstreamer/audioconvert.c +++ b/dlls/winegstreamer/audioconvert.c @@ -21,6 +21,8 @@ struct audio_converter IMFMediaType *input_type; IMFMediaType *output_type; CRITICAL_SECTION cs; + BOOL inflight; + GstElement *container, *appsrc, *audioconvert, *resampler, *appsink; };
static struct audio_converter *impl_audio_converter_from_IMFTransform(IMFTransform *iface) @@ -66,6 +68,7 @@ static ULONG WINAPI audio_converter_Release(IMFTransform *iface) { transform->cs.DebugInfo->Spare[0] = 0; DeleteCriticalSection(&transform->cs); + gst_object_unref(transform->container); heap_free(transform); }
@@ -292,7 +295,8 @@ static HRESULT WINAPI audio_converter_SetInputType(IMFTransform *iface, DWORD id if (!(input_caps = caps_from_mf_media_type(type))) return MF_E_INVALIDTYPE;
- gst_caps_unref(input_caps); + if (flags & MFT_SET_TYPE_TEST_ONLY) + gst_caps_unref(input_caps); }
if (flags & MFT_SET_TYPE_TEST_ONLY) @@ -301,6 +305,7 @@ static HRESULT WINAPI audio_converter_SetInputType(IMFTransform *iface, DWORD id EnterCriticalSection(&converter->cs);
hr = S_OK; + gst_element_set_state(converter->container, GST_STATE_READY);
if (type) { @@ -310,6 +315,9 @@ static HRESULT WINAPI audio_converter_SetInputType(IMFTransform *iface, DWORD id if (SUCCEEDED(hr)) hr = IMFMediaType_CopyAllItems(type, (IMFAttributes *) converter->input_type);
+ g_object_set(converter->appsrc, "caps", input_caps, NULL); + gst_caps_unref(input_caps); + if (FAILED(hr)) { IMFMediaType_Release(converter->input_type); @@ -322,6 +330,9 @@ static HRESULT WINAPI audio_converter_SetInputType(IMFTransform *iface, DWORD id converter->input_type = NULL; }
+ if (converter->input_type && converter->output_type) + gst_element_set_state(converter->container, GST_STATE_PLAYING); + LeaveCriticalSection(&converter->cs);
return hr; @@ -367,7 +378,8 @@ static HRESULT WINAPI audio_converter_SetOutputType(IMFTransform *iface, DWORD i if (!(output_caps = caps_from_mf_media_type(type))) return MF_E_INVALIDTYPE;
- gst_caps_unref(output_caps); + if (flags & MFT_SET_TYPE_TEST_ONLY) + gst_caps_unref(output_caps); }
if (flags & MFT_SET_TYPE_TEST_ONLY) @@ -376,6 +388,7 @@ static HRESULT WINAPI audio_converter_SetOutputType(IMFTransform *iface, DWORD i EnterCriticalSection(&converter->cs);
hr = S_OK; + gst_element_set_state(converter->container, GST_STATE_READY);
if (type) { @@ -385,6 +398,9 @@ static HRESULT WINAPI audio_converter_SetOutputType(IMFTransform *iface, DWORD i if (SUCCEEDED(hr)) hr = IMFMediaType_CopyAllItems(type, (IMFAttributes *) converter->output_type);
+ g_object_set(converter->appsink, "caps", output_caps, NULL); + gst_caps_unref(output_caps); + if (FAILED(hr)) { IMFMediaType_Release(converter->output_type); @@ -397,6 +413,9 @@ static HRESULT WINAPI audio_converter_SetOutputType(IMFTransform *iface, DWORD i converter->output_type = NULL; }
+ if (converter->input_type && converter->output_type) + gst_element_set_state(converter->container, GST_STATE_PLAYING); + LeaveCriticalSection(&converter->cs);
return hr; @@ -460,17 +479,100 @@ static HRESULT WINAPI audio_converter_ProcessMessage(IMFTransform *iface, MFT_ME
static HRESULT WINAPI audio_converter_ProcessInput(IMFTransform *iface, DWORD id, IMFSample *sample, DWORD flags) { - FIXME("%p, %u, %p, %#x.\n", iface, id, sample, flags); + struct audio_converter *converter = impl_audio_converter_from_IMFTransform(iface); + GstBuffer *gst_buffer; + int ret;
- return E_NOTIMPL; + TRACE("%p, %u, %p, %#x.\n", iface, id, sample, flags); + + if (flags) + WARN("Unsupported flags %#x\n", flags); + + if (id != 0) + return MF_E_INVALIDSTREAMNUMBER; + + EnterCriticalSection(&converter->cs); + + if (!converter->input_type || !converter->output_type) + { + LeaveCriticalSection(&converter->cs); + return MF_E_TRANSFORM_TYPE_NOT_SET; + } + + if (converter->inflight) + { + LeaveCriticalSection(&converter->cs); + return MF_E_NOTACCEPTING; + } + + if (!(gst_buffer = gst_buffer_from_mf_sample(sample))) + { + LeaveCriticalSection(&converter->cs); + return E_FAIL; + } + + g_signal_emit_by_name(converter->appsrc, "push-buffer", gst_buffer, &ret); + gst_buffer_unref(gst_buffer); + if (ret != GST_FLOW_OK) + { + ERR("Couldn't push buffer ret = %d (%s)\n", ret, gst_flow_get_name(ret)); + LeaveCriticalSection(&converter->cs); + return E_FAIL; + } + + converter->inflight = TRUE; + LeaveCriticalSection(&converter->cs); + + return S_OK; }
static HRESULT WINAPI audio_converter_ProcessOutput(IMFTransform *iface, DWORD flags, DWORD count, MFT_OUTPUT_DATA_BUFFER *samples, DWORD *status) { - FIXME("%p, %#x, %u, %p, %p.\n", iface, flags, count, samples, status); + struct audio_converter *converter = impl_audio_converter_from_IMFTransform(iface); + GstSample *sample;
- return E_NOTIMPL; + TRACE("%p, %#x, %u, %p, %p.\n", iface, flags, count, samples, status); + + if (flags) + WARN("Unsupported flags %#x\n", flags); + + if (!count) + return S_OK; + + if (count != 1) + return MF_E_INVALIDSTREAMNUMBER; + + if (samples[0].dwStreamID != 0) + return MF_E_INVALIDSTREAMNUMBER; + + EnterCriticalSection(&converter->cs); + + if (!converter->input_type || !converter->output_type) + { + LeaveCriticalSection(&converter->cs); + return MF_E_TRANSFORM_TYPE_NOT_SET; + } + + if (!converter->inflight) + { + LeaveCriticalSection(&converter->cs); + return MF_E_TRANSFORM_NEED_MORE_INPUT; + } + + g_signal_emit_by_name(converter->appsink, "pull-sample", &sample); + + converter->inflight = FALSE; + + samples[0].pSample = mf_sample_from_gst_buffer(gst_sample_get_buffer(sample)); + gst_sample_unref(sample); + samples[0].dwStatus = S_OK; + samples[0].pEvents = NULL; + *status = 0; + + LeaveCriticalSection(&converter->cs); + + return S_OK; }
static const IMFTransformVtbl audio_converter_vtbl = @@ -506,6 +608,7 @@ static const IMFTransformVtbl audio_converter_vtbl = HRESULT audio_converter_create(REFIID riid, void **ret) { struct audio_converter *object; + HRESULT hr;
TRACE("%s %p\n", debugstr_guid(riid), ret);
@@ -518,6 +621,65 @@ HRESULT audio_converter_create(REFIID riid, void **ret) InitializeCriticalSection(&object->cs); object->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": audio_converter_lock");
+ object->container = gst_bin_new(NULL); + + if (!(object->appsrc = gst_element_factory_make("appsrc", NULL))) + { + ERR("Failed to create appsrc, are %u-bit Gstreamer "base" plugins installed?\n", + 8 * (int)sizeof(void *)); + IMFTransform_Release(&object->IMFTransform_iface); + return E_FAIL; + } + gst_bin_add(GST_BIN(object->container), object->appsrc); + + if (!(object->audioconvert = gst_element_factory_make("audioconvert", NULL))) + { + ERR("Failed to create audioconvert, are %u-bit Gstreamer "base" plugins installed?\n", + 8 * (int)sizeof(void *)); + IMFTransform_Release(&object->IMFTransform_iface); + return E_FAIL; + } + gst_bin_add(GST_BIN(object->container), object->audioconvert); + + if (!(object->resampler = gst_element_factory_make("audioresample", NULL))) + { + ERR("Failed to create audioresample, are %u-bit Gstreamer "base" plugins installed?\n", + 8 * (int)sizeof(void *)); + IMFTransform_Release(&object->IMFTransform_iface); + return E_FAIL; + } + gst_bin_add(GST_BIN(object->container), object->resampler); + + if (!(object->appsink = gst_element_factory_make("appsink", NULL))) + { + ERR("Failed to create appsink, are %u-bit Gstreamer "base" plugins installed?\n", + 8 * (int)sizeof(void *)); + IMFTransform_Release(&object->IMFTransform_iface); + return E_FAIL; + } + gst_bin_add(GST_BIN(object->container), object->appsink); + + if (!(gst_element_link(object->appsrc, object->audioconvert))) + { + ERR("Failed to link appsrc to audioconvert\n"); + IMFTransform_Release(&object->IMFTransform_iface); + return E_FAIL; + } + + if (!(gst_element_link(object->audioconvert, object->resampler))) + { + ERR("Failed to link audioconvert to resampler\n"); + IMFTransform_Release(&object->IMFTransform_iface); + return E_FAIL; + } + + if (!(gst_element_link(object->resampler, object->appsink))) + { + ERR("Failed to link resampler to appsink\n"); + IMFTransform_Release(&object->IMFTransform_iface); + return E_FAIL; + } + *ret = &object->IMFTransform_iface; return S_OK; } diff --git a/dlls/winegstreamer/gst_private.h b/dlls/winegstreamer/gst_private.h index 9518f721504..14b6a011ac2 100644 --- a/dlls/winegstreamer/gst_private.h +++ b/dlls/winegstreamer/gst_private.h @@ -82,6 +82,7 @@ HRESULT winegstreamer_stream_handler_create(REFIID riid, void **obj) DECLSPEC_HI IMFMediaType *mf_media_type_from_caps(const GstCaps *caps) DECLSPEC_HIDDEN; GstCaps *caps_from_mf_media_type(IMFMediaType *type) DECLSPEC_HIDDEN; IMFSample *mf_sample_from_gst_buffer(GstBuffer *in) DECLSPEC_HIDDEN; +GstBuffer *gst_buffer_from_mf_sample(IMFSample *in) DECLSPEC_HIDDEN;
HRESULT winegstreamer_stream_handler_create(REFIID riid, void **obj) DECLSPEC_HIDDEN;
diff --git a/dlls/winegstreamer/mfplat.c b/dlls/winegstreamer/mfplat.c index f300988fc5c..883084b2d89 100644 --- a/dlls/winegstreamer/mfplat.c +++ b/dlls/winegstreamer/mfplat.c @@ -865,3 +865,72 @@ done:
return out; } + +GstBuffer* gst_buffer_from_mf_sample(IMFSample *mf_sample) +{ + GstBuffer *out = gst_buffer_new(); + IMFMediaBuffer *mf_buffer = NULL; + LONGLONG duration, time; + DWORD buffer_count; + unsigned int i; + HRESULT hr; + + if (FAILED(hr = IMFSample_GetSampleDuration(mf_sample, &duration))) + goto fail; + + if (FAILED(hr = IMFSample_GetSampleTime(mf_sample, &time))) + goto fail; + + GST_BUFFER_DURATION(out) = duration; + GST_BUFFER_PTS(out) = time * 100; + + if (FAILED(hr = IMFSample_GetBufferCount(mf_sample, &buffer_count))) + goto fail; + + for (i = 0; i < buffer_count; i++) + { + DWORD buffer_size; + GstMapInfo map_info; + GstMemory *memory; + BYTE *buf_data; + + if (FAILED(hr = IMFSample_GetBufferByIndex(mf_sample, i, &mf_buffer))) + goto fail; + + if (FAILED(hr = IMFMediaBuffer_GetCurrentLength(mf_buffer, &buffer_size))) + goto fail; + + memory = gst_allocator_alloc(NULL, buffer_size, NULL); + gst_memory_resize(memory, 0, buffer_size); + + if (!(gst_memory_map(memory, &map_info, GST_MAP_WRITE))) + { + hr = E_FAIL; + goto fail; + } + + if (FAILED(hr = IMFMediaBuffer_Lock(mf_buffer, &buf_data, NULL, NULL))) + goto fail; + + memcpy(map_info.data, buf_data, buffer_size); + + if (FAILED(hr = IMFMediaBuffer_Unlock(mf_buffer))) + goto fail; + + gst_memory_unmap(memory, &map_info); + + gst_buffer_append_memory(out, memory); + + IMFMediaBuffer_Release(mf_buffer); + mf_buffer = NULL; + } + + return out; + +fail: + ERR("Failed to copy IMFSample to GstBuffer, hr = %#x\n", hr); + if (mf_buffer) + IMFMediaBuffer_Release(mf_buffer); + gst_buffer_unref(out); + return NULL; +}