From: Anton Baskanov Subject: [PATCH v2 3/5] amstream: Implement AMAudioStream::Receive. Message-Id: <20200422161045.2095-3-baskanov@gmail.com> Date: Wed, 22 Apr 2020 23:10:43 +0700 In-Reply-To: <20200422161045.2095-2-baskanov@gmail.com> References: <20200422161045.2095-2-baskanov@gmail.com> Signed-off-by: Anton Baskanov --- dlls/amstream/audiostream.c | 71 ++++++++++++++++- dlls/amstream/tests/amstream.c | 135 +++++++++++++++++++++++++++++++++ 2 files changed, 204 insertions(+), 2 deletions(-) diff --git a/dlls/amstream/audiostream.c b/dlls/amstream/audiostream.c index b3fb9c696c..9c849a4831 100644 --- a/dlls/amstream/audiostream.c +++ b/dlls/amstream/audiostream.c @@ -27,6 +27,15 @@ WINE_DEFAULT_DEBUG_CHANNEL(amstream); static const WCHAR sink_id[] = L"I{A35FF56B-9FDA-11D0-8FDF-00C04FD9189D}"; +struct queued_receive +{ + struct list entry; + IMediaSample *sample; + DWORD length; + BYTE *pointer; + DWORD position; +}; + struct audio_stream { IAMMediaStream IAMMediaStream_iface; @@ -47,6 +56,7 @@ struct audio_stream WAVEFORMATEX format; FILTER_STATE state; BOOL eos; + struct list receive_queue; }; typedef struct { @@ -56,6 +66,24 @@ typedef struct { IAudioData *audio_data; } IAudioStreamSampleImpl; +static void remove_queued_receive(struct queued_receive *receive) +{ + list_remove(&receive->entry); + IMediaSample_Release(receive->sample); + HeapFree(GetProcessHeap(), 0, receive); +} + +static void flush_receive_queue(struct audio_stream *stream) +{ + while (!list_empty(&stream->receive_queue)) + { + struct queued_receive *receive = + LIST_ENTRY(list_head(&stream->receive_queue), struct queued_receive, entry); + + remove_queued_receive(receive); + } +} + static inline IAudioStreamSampleImpl *impl_from_IAudioStreamSample(IAudioStreamSample *iface) { return CONTAINING_RECORD(iface, IAudioStreamSampleImpl, IAudioStreamSample_iface); @@ -348,6 +376,8 @@ static HRESULT WINAPI audio_IAMMediaStream_SetState(IAMMediaStream *iface, FILTE EnterCriticalSection(&stream->cs); + if (State_Stopped == state) + flush_receive_queue(stream); if (State_Stopped == stream->state) stream->eos = FALSE; @@ -1048,8 +1078,44 @@ static HRESULT WINAPI audio_meminput_GetAllocatorRequirements(IMemInputPin *ifac static HRESULT WINAPI audio_meminput_Receive(IMemInputPin *iface, IMediaSample *sample) { - FIXME("iface %p, sample %p, stub!\n", iface, sample); - return E_NOTIMPL; + struct audio_stream *stream = impl_from_IMemInputPin(iface); + struct queued_receive *receive; + BYTE *pointer; + HRESULT hr; + + TRACE("(%p)->(%p)\n", stream, sample); + + EnterCriticalSection(&stream->cs); + + if (stream->state == State_Stopped) + { + LeaveCriticalSection(&stream->cs); + return VFW_E_WRONG_STATE; + } + + hr = IMediaSample_GetPointer(sample, &pointer); + if (FAILED(hr)) + { + LeaveCriticalSection(&stream->cs); + return hr; + } + + receive = calloc(1, sizeof(*receive)); + if (!receive) + { + LeaveCriticalSection(&stream->cs); + return E_OUTOFMEMORY; + } + + receive->length = IMediaSample_GetActualDataLength(sample); + receive->pointer = pointer; + receive->sample = sample; + IMediaSample_AddRef(receive->sample); + list_add_tail(&stream->receive_queue, &receive->entry); + + LeaveCriticalSection(&stream->cs); + + return S_OK; } static HRESULT WINAPI audio_meminput_ReceiveMultiple(IMemInputPin *iface, @@ -1102,6 +1168,7 @@ HRESULT audio_stream_create(IMultiMediaStream *parent, const MSPID *purpose_id, object->parent = parent; object->purpose_id = *purpose_id; object->stream_type = stream_type; + list_init(&object->receive_queue); *media_stream = &object->IAMMediaStream_iface; diff --git a/dlls/amstream/tests/amstream.c b/dlls/amstream/tests/amstream.c index 2ac492bc3c..2582d3c69d 100644 --- a/dlls/amstream/tests/amstream.c +++ b/dlls/amstream/tests/amstream.c @@ -2904,6 +2904,140 @@ void test_audiostream_end_of_stream(void) ok(!ref, "Got outstanding refcount %d.\n", ref); } +static void test_audiostream_receive(void) +{ + static const WAVEFORMATEX format = + { + .wFormatTag = WAVE_FORMAT_PCM, + .nChannels = 1, + .nSamplesPerSec = 11025, + .wBitsPerSample = 16, + .nBlockAlign = 2, + .nAvgBytesPerSec = 2 * 11025, + }; + + const AM_MEDIA_TYPE mt = + { + .majortype = MEDIATYPE_Audio, + .subtype = MEDIASUBTYPE_PCM, + .formattype = FORMAT_WaveFormatEx, + .cbFormat = sizeof(WAVEFORMATEX), + .pbFormat = (BYTE *)&format, + }; + + ALLOCATOR_PROPERTIES properties = + { + .cBuffers = 3, + .cbBuffer = 16, + .cbAlign = 1, + }; + + IAMMultiMediaStream *mmstream = create_ammultimediastream(); + ALLOCATOR_PROPERTIES actual; + struct testfilter source; + IMemAllocator *allocator; + IGraphBuilder *graph; + IMediaStream *stream; + IMediaSample *sample1; + IMediaSample *sample2; + IMediaSample *sample3; + HRESULT hr; + ULONG ref; + IPin *pin; + + hr = IAMMultiMediaStream_Initialize(mmstream, STREAMTYPE_READ, 0, NULL); + ok(hr == S_OK, "Got hr %#x.\n", hr); + hr = IAMMultiMediaStream_AddMediaStream(mmstream, NULL, &MSPID_PrimaryAudio, 0, &stream); + ok(hr == S_OK, "Got hr %#x.\n", hr); + hr = IMediaStream_QueryInterface(stream, &IID_IPin, (void **)&pin); + ok(hr == S_OK, "Got hr %#x.\n", hr); + hr = IAMMultiMediaStream_GetFilterGraph(mmstream, &graph); + ok(hr == S_OK, "Got hr %#x.\n", hr); + ok(graph != NULL, "Expected non-null graph\n"); + testfilter_init(&source); + hr = IGraphBuilder_AddFilter(graph, &source.filter.IBaseFilter_iface, NULL); + ok(hr == S_OK, "Got hr %#x.\n", hr); + hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC_SERVER, &IID_IMemAllocator, (void **)&allocator); + ok(hr == S_OK, "Got hr %#x.\n", hr); + hr = IMemAllocator_SetProperties(allocator, &properties, &actual); + ok(hr == S_OK, "Got hr %#x.\n", hr); + hr = IMemAllocator_Commit(allocator); + ok(hr == S_OK, "Got hr %#x.\n", hr); + + hr = IGraphBuilder_ConnectDirect(graph, &source.source.pin.IPin_iface, pin, &mt); + ok(hr == S_OK, "Got hr %#x.\n", hr); + + hr = IMemAllocator_GetBuffer(allocator, &sample1, NULL, NULL, 0); + ok(hr == S_OK, "Got hr %#x.\n", hr); + hr = IMemInputPin_Receive(source.source.pMemInputPin, sample1); + ok(hr == VFW_E_WRONG_STATE, "Got hr %#x.\n", hr); + ref = IMediaSample_Release(sample1); + ok(!ref, "Got outstanding refcount %d.\n", ref); + + hr = IAMMultiMediaStream_SetState(mmstream, STREAMSTATE_RUN); + ok(hr == S_OK, "Got hr %#x.\n", hr); + + hr = IMemAllocator_GetBuffer(allocator, &sample1, NULL, NULL, 0); + ok(hr == S_OK, "Got hr %#x.\n", hr); + hr = IMemInputPin_Receive(source.source.pMemInputPin, sample1); + ok(hr == S_OK, "Got hr %#x.\n", hr); + IMediaSample_AddRef(sample1); + ref = IMediaSample_Release(sample1); + ok(ref == 2, "Got outstanding refcount %d.\n", ref); + + hr = IMemAllocator_GetBuffer(allocator, &sample2, NULL, NULL, 0); + ok(hr == S_OK, "Got hr %#x.\n", hr); + hr = IMemInputPin_Receive(source.source.pMemInputPin, sample2); + ok(hr == S_OK, "Got hr %#x.\n", hr); + IMediaSample_AddRef(sample2); + ref = IMediaSample_Release(sample2); + ok(ref == 2, "Got outstanding refcount %d.\n", ref); + + hr = IPin_EndOfStream(pin); + ok(hr == S_OK, "Got hr %#x.\n", hr); + + hr = IMemAllocator_GetBuffer(allocator, &sample3, NULL, NULL, 0); + ok(hr == S_OK, "Got hr %#x.\n", hr); + hr = IMemInputPin_Receive(source.source.pMemInputPin, sample3); + ok(hr == S_OK, "Got hr %#x.\n", hr); + IMediaSample_AddRef(sample3); + ref = IMediaSample_Release(sample3); + ok(ref == 2, "Got outstanding refcount %d.\n", ref); + + hr = IAMMultiMediaStream_SetState(mmstream, STREAMSTATE_STOP); + ok(hr == S_OK, "Got hr %#x.\n", hr); + + ref = IMediaSample_Release(sample1); + ok(!ref, "Got outstanding refcount %d.\n", ref); + ref = IMediaSample_Release(sample2); + ok(!ref, "Got outstanding refcount %d.\n", ref); + ref = IMediaSample_Release(sample3); + ok(!ref, "Got outstanding refcount %d.\n", ref); + + hr = IMemAllocator_GetBuffer(allocator, &sample1, NULL, NULL, 0); + ok(hr == S_OK, "Got hr %#x.\n", hr); + hr = IMemInputPin_Receive(source.source.pMemInputPin, sample1); + ok(hr == VFW_E_WRONG_STATE, "Got hr %#x.\n", hr); + ref = IMediaSample_Release(sample1); + ok(!ref, "Got outstanding refcount %d.\n", ref); + + IGraphBuilder_Disconnect(graph, pin); + IGraphBuilder_Disconnect(graph, &source.source.pin.IPin_iface); + + hr = IMemAllocator_Decommit(allocator); + ok(hr == S_OK, "Got hr %#x.\n", hr); + + ref = IAMMultiMediaStream_Release(mmstream); + ok(!ref, "Got outstanding refcount %d.\n", ref); + ref = IGraphBuilder_Release(graph); + ok(!ref, "Got outstanding refcount %d.\n", ref); + IPin_Release(pin); + ref = IMediaStream_Release(stream); + ok(!ref, "Got outstanding refcount %d.\n", ref); + ref = IMemAllocator_Release(allocator); + ok(!ref, "Got outstanding refcount %d.\n", ref); +} + void test_mediastreamfilter_get_state(void) { IAMMultiMediaStream *mmstream = create_ammultimediastream(); @@ -3077,6 +3211,7 @@ START_TEST(amstream) test_audiostream_receive_connection(); test_audiostream_set_state(); test_audiostream_end_of_stream(); + test_audiostream_receive(); test_mediastreamfilter_get_state(); test_mediastreamfilter_stop_pause_run(); -- 2.17.1