summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristoph Reiter <reiter.christoph@gmail.com>2018-05-23 21:15:09 +0200
committerNirbheek Chauhan <nirbheek@centricular.com>2018-05-25 19:06:37 +0530
commit3b1c7ef8e4ee44706b187c64f3f083627e2d3bbf (patch)
treeb5f77a62ba51c56c7b83744cf881ba46cc840fee
parent0aed64426bc2ec443ff637b4b3089174b309eaa8 (diff)
wasapisink: recover from low buffer levels in shared mode
In case the wasapi buffer levels got low in shared mode we would still wait until more buffer is available until writing something in it, which means we could never catch up and recover. Instead only wait for a new buffer in case the existing one is full and always write what we can. Also don't loop until all data is written since the base class can handle that for us and under normal circumstances this doesn't happen anyway. This only works in shared mode, as in exclusive mode we have to exactly fill the buffer and always have to wait first. This fixes noisy (buffer underrun) playback with the wasapisink under load. https://bugzilla.gnome.org/show_bug.cgi?id=796354
-rw-r--r--sys/wasapi/gstwasapisink.c74
1 files changed, 47 insertions, 27 deletions
diff --git a/sys/wasapi/gstwasapisink.c b/sys/wasapi/gstwasapisink.c
index f9992df41..704bf2992 100644
--- a/sys/wasapi/gstwasapisink.c
+++ b/sys/wasapi/gstwasapisink.c
@@ -608,58 +608,78 @@ gst_wasapi_sink_write (GstAudioSink * asink, gpointer data, guint length)
GstWasapiSink *self = GST_WASAPI_SINK (asink);
HRESULT hr;
gint16 *dst = NULL;
- guint pending = length;
+ DWORD dwWaitResult;
+ guint can_frames, have_frames, n_frames, write_len, written_len = 0;
GST_OBJECT_LOCK (self);
if (self->client_needs_restart) {
hr = IAudioClient_Start (self->client);
- HR_FAILED_AND (hr, IAudioClient::Start,
- GST_OBJECT_UNLOCK (self); length = 0; goto beach);
+ HR_FAILED_AND (hr, IAudioClient::Start, GST_OBJECT_UNLOCK (self); goto beach);
self->client_needs_restart = FALSE;
}
GST_OBJECT_UNLOCK (self);
- while (pending > 0) {
- DWORD dwWaitResult;
- guint can_frames, have_frames, n_frames, write_len;
+ /* We have N frames to be written out */
+ have_frames = length / (self->mix_format->nBlockAlign);
+ if (self->sharemode == AUDCLNT_SHAREMODE_EXCLUSIVE) {
+ /* In exlusive mode we have to wait always */
dwWaitResult = WaitForSingleObject (self->event_handle, INFINITE);
if (dwWaitResult != WAIT_OBJECT_0) {
GST_ERROR_OBJECT (self, "Error waiting for event handle: %x",
(guint) dwWaitResult);
- length -= pending;
goto beach;
}
- /* We have N frames to be written out */
- have_frames = pending / (self->mix_format->nBlockAlign);
- /* We have can_frames space in the output buffer */
can_frames = gst_wasapi_sink_get_can_frames (self);
- /* We will write out these many frames, and this much length */
- n_frames = MIN (can_frames, have_frames);
- write_len = n_frames * self->mix_format->nBlockAlign;
+ /* In exclusive mode we need to fill the whole buffer in one go or
+ * GetBuffer will error out */
+ if (can_frames != have_frames) {
+ GST_ERROR_OBJECT (self,
+ "Need at %i frames to write for exclusive mode, but got %i",
+ can_frames, have_frames);
+ written_len = -1;
+ goto beach;
+ }
+ } else {
+ /* In shared mode we can write parts of the buffer, so only wait
+ * in case we can't write anything */
+ can_frames = gst_wasapi_sink_get_can_frames (self);
- GST_DEBUG_OBJECT (self, "total: %i, have_frames: %i (%i bytes), "
- "can_frames: %i, will write: %i (%i bytes)", self->buffer_frame_count,
- have_frames, pending, can_frames, n_frames, write_len);
+ if (can_frames == 0) {
+ dwWaitResult = WaitForSingleObject (self->event_handle, INFINITE);
+ if (dwWaitResult != WAIT_OBJECT_0) {
+ GST_ERROR_OBJECT (self, "Error waiting for event handle: %x",
+ (guint) dwWaitResult);
+ goto beach;
+ }
+ can_frames = gst_wasapi_sink_get_can_frames (self);
+ }
+ }
- hr = IAudioRenderClient_GetBuffer (self->render_client, n_frames,
- (BYTE **) & dst);
- HR_FAILED_AND (hr, IAudioRenderClient::GetBuffer, length = 0; goto beach);
+ /* We will write out these many frames, and this much length */
+ n_frames = MIN (can_frames, have_frames);
+ write_len = n_frames * self->mix_format->nBlockAlign;
- memcpy (dst, data, write_len);
+ GST_DEBUG_OBJECT (self, "total: %i, have_frames: %i (%i bytes), "
+ "can_frames: %i, will write: %i (%i bytes)", self->buffer_frame_count,
+ have_frames, length, can_frames, n_frames, write_len);
- hr = IAudioRenderClient_ReleaseBuffer (self->render_client, n_frames,
- self->mute ? AUDCLNT_BUFFERFLAGS_SILENT : 0);
- HR_FAILED_AND (hr, IAudioRenderClient::ReleaseBuffer, length = 0;
- goto beach);
+ hr = IAudioRenderClient_GetBuffer (self->render_client, n_frames,
+ (BYTE **) & dst);
+ HR_FAILED_AND (hr, IAudioRenderClient::GetBuffer, goto beach);
- pending -= write_len;
- }
+ memcpy (dst, data, write_len);
+
+ hr = IAudioRenderClient_ReleaseBuffer (self->render_client, n_frames,
+ self->mute ? AUDCLNT_BUFFERFLAGS_SILENT : 0);
+ HR_FAILED_AND (hr, IAudioRenderClient::ReleaseBuffer, goto beach);
+
+ written_len = write_len;
beach:
- return length;
+ return written_len;
}
static guint