summaryrefslogtreecommitdiff
path: root/sys/wasapi/gstwasapisrc.c
diff options
context:
space:
mode:
authorSeungha Yang <seungha@centricular.com>2020-06-09 22:38:28 +0900
committerGStreamer Merge Bot <gitlab-merge-bot@gstreamer-foundation.org>2020-06-11 11:40:26 +0000
commit7ab51e85ab226ce5107d1d9d4ed408f91d9058fc (patch)
tree1070d9ad5df3ea267957512c54ecb1eb07ef2c5e /sys/wasapi/gstwasapisrc.c
parent3e507dc0731d077ecd41fd8274846d8f93e0989c (diff)
downloadgstreamer-plugins-bad-7ab51e85ab226ce5107d1d9d4ed408f91d9058fc.tar.gz
wasapi: Fix possible deadlock while downwards state change
IAudioClient::Stop() doesn't seem to wake up the event handle, then read() or write() could be blocked forever by WaitForSingleObject. Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/merge_requests/1329>
Diffstat (limited to 'sys/wasapi/gstwasapisrc.c')
-rw-r--r--sys/wasapi/gstwasapisrc.c28
1 files changed, 26 insertions, 2 deletions
diff --git a/sys/wasapi/gstwasapisrc.c b/sys/wasapi/gstwasapisrc.c
index 7dac971b8..411c9f501 100644
--- a/sys/wasapi/gstwasapisrc.c
+++ b/sys/wasapi/gstwasapisrc.c
@@ -189,6 +189,7 @@ gst_wasapi_src_init (GstWasapiSrc * self)
self->low_latency = DEFAULT_LOW_LATENCY;
self->try_audioclient3 = DEFAULT_AUDIOCLIENT3;
self->event_handle = CreateEvent (NULL, FALSE, FALSE, NULL);
+ self->cancellable = CreateEvent (NULL, TRUE, FALSE, NULL);
self->client_needs_restart = FALSE;
self->adapter = gst_adapter_new ();
@@ -205,6 +206,11 @@ gst_wasapi_src_dispose (GObject * object)
self->event_handle = NULL;
}
+ if (self->cancellable != NULL) {
+ CloseHandle (self->cancellable);
+ self->cancellable = NULL;
+ }
+
if (self->client_clock != NULL) {
IUnknown_Release (self->client_clock);
self->client_clock = NULL;
@@ -516,7 +522,12 @@ gst_wasapi_src_prepare (GstAudioSrc * asrc, GstAudioRingBufferSpec * spec)
(self)->ringbuffer, self->positions);
res = TRUE;
+
+ /* reset cancellable event handle */
+ ResetEvent (self->cancellable);
+
beach:
+
/* unprepare() is not called if prepare() fails, but we want it to be, so call
* it manually when needed */
if (!res)
@@ -568,6 +579,7 @@ gst_wasapi_src_read (GstAudioSrc * asrc, gpointer data, guint length,
HR_FAILED_ELEMENT_ERROR_AND (hr, IAudioClient::Start, self,
GST_OBJECT_UNLOCK (self); goto err);
self->client_needs_restart = FALSE;
+ ResetEvent (self->cancellable);
gst_adapter_clear (self->adapter);
}
@@ -585,15 +597,25 @@ gst_wasapi_src_read (GstAudioSrc * asrc, gpointer data, guint length,
while (wanted > 0) {
DWORD dwWaitResult;
guint got_frames, avail_frames, n_frames, want_frames, read_len;
+ HANDLE event_handle[2];
+
+ event_handle[0] = self->event_handle;
+ event_handle[1] = self->cancellable;
/* Wait for data to become available */
- dwWaitResult = WaitForSingleObject (self->event_handle, INFINITE);
- if (dwWaitResult != WAIT_OBJECT_0) {
+ dwWaitResult = WaitForMultipleObjects (2, event_handle, FALSE, INFINITE);
+ if (dwWaitResult != WAIT_OBJECT_0 && dwWaitResult != WAIT_OBJECT_0 + 1) {
GST_ERROR_OBJECT (self, "Error waiting for event handle: %x",
(guint) dwWaitResult);
goto err;
}
+ /* ::reset was requested */
+ if (dwWaitResult == WAIT_OBJECT_0 + 1) {
+ GST_DEBUG_OBJECT (self, "operation was cancelled");
+ return -1;
+ }
+
hr = IAudioCaptureClient_GetBuffer (self->capture_client,
(BYTE **) & from, &got_frames, &flags, NULL, NULL);
if (hr != S_OK) {
@@ -685,6 +707,8 @@ gst_wasapi_src_reset (GstAudioSrc * asrc)
if (!self->client)
return;
+ SetEvent (self->cancellable);
+
GST_OBJECT_LOCK (self);
hr = IAudioClient_Stop (self->client);
HR_FAILED_RET (hr, IAudioClock::Stop,);