wasapisrc: Correctly handle BUFFERFLAGS_SILENT
[platform/upstream/gstreamer.git] / sys / wasapi / gstwasapisrc.c
index 2425c68..2286b0d 100644 (file)
  * gst-launch-1.0 -v wasapisrc ! fakesink
  * ]| Capture from the default audio device and render to fakesink.
  *
+ * |[
+ * gst-launch-1.0 -v wasapisrc low-latency=true ! fakesink
+ * ]| Capture from the default audio device with the minimum possible latency and render to fakesink.
+ *
  */
 #ifdef HAVE_CONFIG_H
 #  include <config.h>
@@ -49,16 +53,23 @@ static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
     GST_STATIC_CAPS (GST_WASAPI_STATIC_CAPS));
 
 #define DEFAULT_ROLE          GST_WASAPI_DEVICE_ROLE_CONSOLE
+#define DEFAULT_LOOPBACK      FALSE
 #define DEFAULT_EXCLUSIVE     FALSE
 #define DEFAULT_LOW_LATENCY   FALSE
+#define DEFAULT_AUDIOCLIENT3  FALSE
+/* The clock provided by WASAPI is always off and causes buffers to be late
+ * very quickly on the sink. Disable pending further investigation. */
+#define DEFAULT_PROVIDE_CLOCK FALSE
 
 enum
 {
   PROP_0,
   PROP_ROLE,
   PROP_DEVICE,
+  PROP_LOOPBACK,
   PROP_EXCLUSIVE,
-  PROP_LOW_LATENCY
+  PROP_LOW_LATENCY,
+  PROP_AUDIOCLIENT3
 };
 
 static void gst_wasapi_src_dispose (GObject * object);
@@ -80,8 +91,10 @@ static guint gst_wasapi_src_read (GstAudioSrc * asrc, gpointer data,
 static guint gst_wasapi_src_delay (GstAudioSrc * asrc);
 static void gst_wasapi_src_reset (GstAudioSrc * asrc);
 
+#if DEFAULT_PROVIDE_CLOCK
 static GstClockTime gst_wasapi_src_get_time (GstClock * clock,
     gpointer user_data);
+#endif
 
 #define gst_wasapi_src_parent_class parent_class
 G_DEFINE_TYPE (GstWasapiSrc, gst_wasapi_src, GST_TYPE_AUDIO_SRC);
@@ -113,6 +126,12 @@ gst_wasapi_src_class_init (GstWasapiSrcClass * klass)
           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
   g_object_class_install_property (gobject_class,
+      PROP_LOOPBACK,
+      g_param_spec_boolean ("loopback", "Loopback recording",
+          "Open the sink device for loopback recording",
+          DEFAULT_LOOPBACK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_property (gobject_class,
       PROP_EXCLUSIVE,
       g_param_spec_boolean ("exclusive", "Exclusive mode",
           "Open the device in exclusive mode",
@@ -121,13 +140,20 @@ gst_wasapi_src_class_init (GstWasapiSrcClass * klass)
   g_object_class_install_property (gobject_class,
       PROP_LOW_LATENCY,
       g_param_spec_boolean ("low-latency", "Low latency",
-          "Optimize all settings for lowest latency",
+          "Optimize all settings for lowest latency. Always safe to enable.",
           DEFAULT_LOW_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
+  g_object_class_install_property (gobject_class,
+      PROP_AUDIOCLIENT3,
+      g_param_spec_boolean ("use-audioclient3", "Use the AudioClient3 API",
+          "Whether to use the Windows 10 AudioClient3 API when available",
+          DEFAULT_AUDIOCLIENT3, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   gst_element_class_add_static_pad_template (gstelement_class, &src_template);
   gst_element_class_set_static_metadata (gstelement_class, "WasapiSrc",
-      "Source/Audio",
+      "Source/Audio/Hardware",
       "Stream audio from an audio capture device through WASAPI",
+      "Nirbheek Chauhan <nirbheek@centricular.com>, "
       "Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>");
 
   gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_wasapi_src_get_caps);
@@ -147,6 +173,7 @@ gst_wasapi_src_class_init (GstWasapiSrcClass * klass)
 static void
 gst_wasapi_src_init (GstWasapiSrc * self)
 {
+#if DEFAULT_PROVIDE_CLOCK
   /* override with a custom clock */
   if (GST_AUDIO_BASE_SRC (self)->clock)
     gst_object_unref (GST_AUDIO_BASE_SRC (self)->clock);
@@ -154,10 +181,18 @@ gst_wasapi_src_init (GstWasapiSrc * self)
   GST_AUDIO_BASE_SRC (self)->clock = gst_audio_clock_new ("GstWasapiSrcClock",
       gst_wasapi_src_get_time, gst_object_ref (self),
       (GDestroyNotify) gst_object_unref);
+#endif
 
+  self->role = DEFAULT_ROLE;
+  self->sharemode = AUDCLNT_SHAREMODE_SHARED;
+  self->loopback = DEFAULT_LOOPBACK;
+  self->low_latency = DEFAULT_LOW_LATENCY;
+  self->try_audioclient3 = DEFAULT_AUDIOCLIENT3;
   self->event_handle = CreateEvent (NULL, FALSE, FALSE, NULL);
+  self->client_needs_restart = FALSE;
+  self->adapter = gst_adapter_new ();
 
-  CoInitialize (NULL);
+  CoInitializeEx (NULL, COINIT_MULTITHREADED);
 }
 
 static void
@@ -193,7 +228,8 @@ gst_wasapi_src_finalize (GObject * object)
 {
   GstWasapiSrc *self = GST_WASAPI_SRC (object);
 
-  g_clear_pointer (&self->mix_format, CoTaskMemFree);
+  CoTaskMemFree (self->mix_format);
+  self->mix_format = NULL;
 
   CoUninitialize ();
 
@@ -201,6 +237,9 @@ gst_wasapi_src_finalize (GObject * object)
   g_clear_pointer (&self->positions, g_free);
   g_clear_pointer (&self->device_strid, g_free);
 
+  g_object_unref (self->adapter);
+  self->adapter = NULL;
+
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
@@ -222,6 +261,9 @@ gst_wasapi_src_set_property (GObject * object, guint prop_id,
           device ? g_utf8_to_utf16 (device, -1, NULL, NULL, NULL) : NULL;
       break;
     }
+    case PROP_LOOPBACK:
+      self->loopback = g_value_get_boolean (value);
+      break;
     case PROP_EXCLUSIVE:
       self->sharemode = g_value_get_boolean (value)
           ? AUDCLNT_SHAREMODE_EXCLUSIVE : AUDCLNT_SHAREMODE_SHARED;
@@ -229,6 +271,9 @@ gst_wasapi_src_set_property (GObject * object, guint prop_id,
     case PROP_LOW_LATENCY:
       self->low_latency = g_value_get_boolean (value);
       break;
+    case PROP_AUDIOCLIENT3:
+      self->try_audioclient3 = g_value_get_boolean (value);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -249,6 +294,9 @@ gst_wasapi_src_get_property (GObject * object, guint prop_id,
       g_value_take_string (value, self->device_strid ?
           g_utf16_to_utf8 (self->device_strid, -1, NULL, NULL, NULL) : NULL);
       break;
+    case PROP_LOOPBACK:
+      g_value_set_boolean (value, self->loopback);
+      break;
     case PROP_EXCLUSIVE:
       g_value_set_boolean (value,
           self->sharemode == AUDCLNT_SHAREMODE_EXCLUSIVE);
@@ -256,12 +304,24 @@ gst_wasapi_src_get_property (GObject * object, guint prop_id,
     case PROP_LOW_LATENCY:
       g_value_set_boolean (value, self->low_latency);
       break;
+    case PROP_AUDIOCLIENT3:
+      g_value_set_boolean (value, self->try_audioclient3);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
   }
 }
 
+static gboolean
+gst_wasapi_src_can_audioclient3 (GstWasapiSrc * self)
+{
+  if (self->sharemode == AUDCLNT_SHAREMODE_SHARED &&
+      self->try_audioclient3 && gst_wasapi_util_have_audioclient3 ())
+    return TRUE;
+  return FALSE;
+}
+
 static GstCaps *
 gst_wasapi_src_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
 {
@@ -279,22 +339,26 @@ gst_wasapi_src_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
 
     template_caps = gst_pad_get_pad_template_caps (bsrc->srcpad);
 
-    if (!self->client)
-      gst_wasapi_src_open (GST_AUDIO_SRC (bsrc));
+    if (!self->client) {
+      caps = template_caps;
+      goto out;
+    }
 
     ret = gst_wasapi_util_get_device_format (GST_ELEMENT (self),
         self->sharemode, self->device, self->client, &format);
     if (!ret) {
       GST_ELEMENT_ERROR (self, STREAM, FORMAT, (NULL),
           ("failed to detect format"));
-      goto out;
+      gst_caps_unref (template_caps);
+      return NULL;
     }
 
     gst_wasapi_util_parse_waveformatex ((WAVEFORMATEXTENSIBLE *) format,
         template_caps, &caps, &self->positions);
     if (caps == NULL) {
       GST_ELEMENT_ERROR (self, STREAM, FORMAT, (NULL), ("unknown format"));
-      goto out;
+      gst_caps_unref (template_caps);
+      return NULL;
     }
 
     {
@@ -316,9 +380,8 @@ gst_wasapi_src_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
     caps = filtered;
   }
 
-  GST_DEBUG_OBJECT (self, "returning caps %" GST_PTR_FORMAT, caps);
-
 out:
+  GST_DEBUG_OBJECT (self, "returning caps %" GST_PTR_FORMAT, caps);
   return caps;
 }
 
@@ -337,8 +400,9 @@ gst_wasapi_src_open (GstAudioSrc * asrc)
    * even if the old device was unplugged. We need to handle this somehow.
    * For example, perhaps we should automatically switch to the new device if
    * the default device is changed and a device isn't explicitly selected. */
-  if (!gst_wasapi_util_get_device_client (GST_ELEMENT (self), TRUE,
-          self->role, self->device_strid, &device, &client)) {
+  if (!gst_wasapi_util_get_device_client (GST_ELEMENT (self),
+          self->loopback ? eRender : eCapture, self->role, self->device_strid,
+          &device, &client)) {
     if (!self->device_strid)
       GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ, (NULL),
           ("Failed to get default device"));
@@ -380,128 +444,65 @@ gst_wasapi_src_prepare (GstAudioSrc * asrc, GstAudioRingBufferSpec * spec)
 {
   GstWasapiSrc *self = GST_WASAPI_SRC (asrc);
   gboolean res = FALSE;
-  REFERENCE_TIME latency_rt, default_period, min_period;
-  REFERENCE_TIME device_period, device_buffer_duration;
-  guint bpf, rate, buffer_frames;
+  REFERENCE_TIME latency_rt;
+  guint bpf, rate, devicep_frames, buffer_frames;
   HRESULT hr;
 
-  hr = IAudioClient_GetDevicePeriod (self->client, &default_period,
-      &min_period);
-  if (hr != S_OK) {
-    GST_ERROR_OBJECT (self, "IAudioClient::GetDevicePeriod failed");
-    return FALSE;
-  }
-  GST_INFO_OBJECT (self, "wasapi default period: %" G_GINT64_FORMAT
-      ", min period: %" G_GINT64_FORMAT, default_period, min_period);
+  CoInitializeEx (NULL, COINIT_MULTITHREADED);
 
-  bpf = GST_AUDIO_INFO_BPF (&spec->info);
-  rate = GST_AUDIO_INFO_RATE (&spec->info);
-
-  if (self->low_latency) {
-    if (self->sharemode == AUDCLNT_SHAREMODE_SHARED) {
-      device_period = default_period;
-      device_buffer_duration = 0;
-    } else {
-      device_period = min_period;
-      device_buffer_duration = min_period;
-    }
+  if (gst_wasapi_src_can_audioclient3 (self)) {
+    if (!gst_wasapi_util_initialize_audioclient3 (GST_ELEMENT (self), spec,
+            (IAudioClient3 *) self->client, self->mix_format, self->low_latency,
+            self->loopback, &devicep_frames))
+      goto beach;
   } else {
-    /* Clamp values to integral multiples of an appropriate period */
-    gst_wasapi_util_get_best_buffer_sizes (spec,
-        self->sharemode == AUDCLNT_SHAREMODE_EXCLUSIVE, default_period,
-        min_period, &device_period, &device_buffer_duration);
-  }
-
-  /* For some reason, we need to call this a second time for exclusive mode */
-  if (self->sharemode == AUDCLNT_SHAREMODE_EXCLUSIVE)
-    CoInitialize (NULL);
-
-  hr = IAudioClient_Initialize (self->client, self->sharemode,
-      AUDCLNT_STREAMFLAGS_EVENTCALLBACK, device_buffer_duration,
-      /* This must always be 0 in shared mode */
-      self->sharemode == AUDCLNT_SHAREMODE_SHARED ? 0 : device_period,
-      self->mix_format, NULL);
-
-  if (hr == AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED &&
-      self->sharemode == AUDCLNT_SHAREMODE_EXCLUSIVE) {
-    guint32 n_frames;
-
-    GST_WARNING_OBJECT (self, "initialize failed due to unaligned period %i",
-        (int) device_period);
-
-    /* Calculate a new aligned period. First get the aligned buffer size. */
-    hr = IAudioClient_GetBufferSize (self->client, &n_frames);
-    if (hr != S_OK) {
-      gchar *msg = gst_wasapi_util_hresult_to_string (hr);
-      GST_ELEMENT_ERROR (self, RESOURCE, OPEN_WRITE, (NULL),
-          ("IAudioClient::GetBufferSize() failed: %s", msg));
-      g_free (msg);
+    if (!gst_wasapi_util_initialize_audioclient (GST_ELEMENT (self), spec,
+            self->client, self->mix_format, self->sharemode, self->low_latency,
+            self->loopback, &devicep_frames))
       goto beach;
-    }
-
-    device_period = (GST_SECOND / 100) * n_frames / rate;
-
-    GST_WARNING_OBJECT (self, "trying to re-initialize with period %i "
-        "(%i frames, %i rate)", (int) device_period, n_frames, rate);
-
-    hr = IAudioClient_Initialize (self->client, self->sharemode,
-        AUDCLNT_STREAMFLAGS_EVENTCALLBACK, device_period,
-        device_period, self->mix_format, NULL);
-  }
-  if (hr != S_OK) {
-    gchar *msg = gst_wasapi_util_hresult_to_string (hr);
-    GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ, (NULL),
-        ("IAudioClient::Initialize () failed: %s", msg));
-    g_free (msg);
-    goto beach;
   }
 
+  bpf = GST_AUDIO_INFO_BPF (&spec->info);
+  rate = GST_AUDIO_INFO_RATE (&spec->info);
+
   /* Total size in frames of the allocated buffer that we will read from */
   hr = IAudioClient_GetBufferSize (self->client, &buffer_frames);
-  if (hr != S_OK) {
-    GST_ERROR_OBJECT (self, "IAudioClient::GetBufferSize failed");
-    goto beach;
-  }
+  HR_FAILED_GOTO (hr, IAudioClient::GetBufferSize, beach);
 
-  GST_INFO_OBJECT (self, "buffer size is %i frames, bpf is %i bytes, "
-      "rate is %i Hz", buffer_frames, bpf, rate);
+  GST_INFO_OBJECT (self, "buffer size is %i frames, device period is %i "
+      "frames, bpf is %i bytes, rate is %i Hz", buffer_frames,
+      devicep_frames, bpf, rate);
 
-  spec->segsize = gst_util_uint64_scale_int_round (rate * bpf,
-      device_period * 100, GST_SECOND);
+  /* Actual latency-time/buffer-time will be different now */
+  spec->segsize = devicep_frames * bpf;
 
   /* We need a minimum of 2 segments to ensure glitch-free playback */
-  spec->segtotal = MAX (self->buffer_frame_count * bpf / spec->segsize, 2);
+  spec->segtotal = MAX (buffer_frames * bpf / spec->segsize, 2);
 
   GST_INFO_OBJECT (self, "segsize is %i, segtotal is %i", spec->segsize,
       spec->segtotal);
 
   /* Get WASAPI latency for logging */
   hr = IAudioClient_GetStreamLatency (self->client, &latency_rt);
-  if (hr != S_OK) {
-    GST_ERROR_OBJECT (self, "IAudioClient::GetStreamLatency failed");
-    goto beach;
-  }
+  HR_FAILED_GOTO (hr, IAudioClient::GetStreamLatency, beach);
+
   GST_INFO_OBJECT (self, "wasapi stream latency: %" G_GINT64_FORMAT " (%"
       G_GINT64_FORMAT " ms)", latency_rt, latency_rt / 10000);
 
   /* Set the event handler which will trigger reads */
   hr = IAudioClient_SetEventHandle (self->client, self->event_handle);
-  if (hr != S_OK) {
-    GST_ERROR_OBJECT (self, "IAudioClient::SetEventHandle failed");
-    goto beach;
-  }
+  HR_FAILED_GOTO (hr, IAudioClient::SetEventHandle, beach);
 
   /* Get the clock and the clock freq */
   if (!gst_wasapi_util_get_clock (GST_ELEMENT (self), self->client,
-          &self->client_clock)) {
+          &self->client_clock))
     goto beach;
-  }
 
   hr = IAudioClock_GetFrequency (self->client_clock, &self->client_clock_freq);
-  if (hr != S_OK) {
-    GST_ERROR_OBJECT (self, "IAudioClock::GetFrequency failed");
-    goto beach;
-  }
+  HR_FAILED_GOTO (hr, IAudioClock::GetFrequency, beach);
+
+  GST_INFO_OBJECT (self, "wasapi clock freq is %" G_GUINT64_FORMAT,
+      self->client_clock_freq);
 
   /* Get capture source client and start it up */
   if (!gst_wasapi_util_get_capture_client (GST_ELEMENT (self), self->client,
@@ -510,23 +511,12 @@ gst_wasapi_src_prepare (GstAudioSrc * asrc, GstAudioRingBufferSpec * spec)
   }
 
   hr = IAudioClient_Start (self->client);
-  if (hr != S_OK) {
-    GST_ERROR_OBJECT (self, "IAudioClient::Start failed");
-    goto beach;
-  }
+  HR_FAILED_GOTO (hr, IAudioClock::Start, beach);
+  self->client_needs_restart = FALSE;
 
   gst_audio_ring_buffer_set_channel_positions (GST_AUDIO_BASE_SRC
       (self)->ringbuffer, self->positions);
 
-#if defined(_MSC_VER) || defined(GST_FORCE_WIN_AVRT)
-  /* Increase the thread priority to reduce glitches */
-  {
-    DWORD taskIndex = 0;
-    self->thread_priority_handle =
-        AvSetMmThreadCharacteristics (TEXT ("Pro Audio"), &taskIndex);
-  }
-#endif
-
   res = TRUE;
 beach:
   /* unprepare() is not called if prepare() fails, but we want it to be, so call
@@ -542,16 +532,6 @@ gst_wasapi_src_unprepare (GstAudioSrc * asrc)
 {
   GstWasapiSrc *self = GST_WASAPI_SRC (asrc);
 
-  if (self->sharemode == AUDCLNT_SHAREMODE_EXCLUSIVE)
-    CoUninitialize ();
-
-#if defined(_MSC_VER) || defined(GST_FORCE_WIN_AVRT)
-  if (self->thread_priority_handle != NULL) {
-    AvRevertMmThreadCharacteristics (self->thread_priority_handle);
-    self->thread_priority_handle = NULL;
-  }
-#endif
-
   if (self->client != NULL) {
     IAudioClient_Stop (self->client);
   }
@@ -568,6 +548,8 @@ gst_wasapi_src_unprepare (GstAudioSrc * asrc)
 
   self->client_clock_freq = 0;
 
+  CoUninitialize ();
+
   return TRUE;
 }
 
@@ -579,76 +561,108 @@ gst_wasapi_src_read (GstAudioSrc * asrc, gpointer data, guint length,
   HRESULT hr;
   gint16 *from = NULL;
   guint wanted = length;
+  guint bpf;
   DWORD flags;
 
+  GST_OBJECT_LOCK (self);
+  if (self->client_needs_restart) {
+    hr = IAudioClient_Start (self->client);
+    HR_FAILED_ELEMENT_ERROR_AND (hr, IAudioClient::Start, self,
+        GST_OBJECT_UNLOCK (self); goto err);
+    self->client_needs_restart = FALSE;
+    gst_adapter_clear (self->adapter);
+  }
+
+  bpf = self->mix_format->nBlockAlign;
+  GST_OBJECT_UNLOCK (self);
+
+  /* If we've accumulated enough data, return it immediately */
+  if (gst_adapter_available (self->adapter) >= wanted) {
+    memcpy (data, gst_adapter_map (self->adapter, wanted), wanted);
+    gst_adapter_flush (self->adapter, wanted);
+    GST_DEBUG_OBJECT (self, "Adapter has enough data, returning %i", wanted);
+    goto out;
+  }
+
   while (wanted > 0) {
-    guint have_frames, n_frames, want_frames, read_len;
+    DWORD dwWaitResult;
+    guint got_frames, avail_frames, n_frames, want_frames, read_len;
 
     /* Wait for data to become available */
-    WaitForSingleObject (self->event_handle, INFINITE);
+    dwWaitResult = WaitForSingleObject (self->event_handle, INFINITE);
+    if (dwWaitResult != WAIT_OBJECT_0) {
+      GST_ERROR_OBJECT (self, "Error waiting for event handle: %x",
+          (guint) dwWaitResult);
+      goto err;
+    }
 
     hr = IAudioCaptureClient_GetBuffer (self->capture_client,
-        (BYTE **) & from, &have_frames, &flags, NULL, NULL);
+        (BYTE **) & from, &got_frames, &flags, NULL, NULL);
     if (hr != S_OK) {
-      gchar *msg = gst_wasapi_util_hresult_to_string (hr);
-      if (hr == AUDCLNT_S_BUFFER_EMPTY)
+      if (hr == AUDCLNT_S_BUFFER_EMPTY) {
+        gchar *msg = gst_wasapi_util_hresult_to_string (hr);
         GST_WARNING_OBJECT (self, "IAudioCaptureClient::GetBuffer failed: %s"
             ", retrying", msg);
-      else
-        GST_ERROR_OBJECT (self, "IAudioCaptureClient::GetBuffer failed: %s",
-            msg);
-      g_free (msg);
-      length = 0;
-      goto beach;
+        g_free (msg);
+        length = 0;
+        goto out;
+      }
+      HR_FAILED_ELEMENT_ERROR_AND (hr, IAudioCaptureClient::GetBuffer, self,
+          goto err);
     }
 
-    if (flags != 0)
-      GST_INFO_OBJECT (self, "buffer flags=%#08x", (guint) flags);
-
-    /* XXX: How do we handle AUDCLNT_BUFFERFLAGS_SILENT? We're supposed to write
-     * out silence when that flag is set? See:
-     * https://msdn.microsoft.com/en-us/library/windows/desktop/dd370800(v=vs.85).aspx */
+    if (G_UNLIKELY (flags != 0)) {
+      /* https://docs.microsoft.com/en-us/windows/win32/api/audioclient/ne-audioclient-_audclnt_bufferflags */
+      if (flags & AUDCLNT_BUFFERFLAGS_DATA_DISCONTINUITY)
+        GST_DEBUG_OBJECT (self, "WASAPI reported discontinuity (glitch?)");
+      if (flags & AUDCLNT_BUFFERFLAGS_TIMESTAMP_ERROR)
+        GST_DEBUG_OBJECT (self, "WASAPI reported a timestamp error");
+    }
 
-    if (flags & AUDCLNT_BUFFERFLAGS_DATA_DISCONTINUITY)
-      GST_WARNING_OBJECT (self, "WASAPI reported glitch in buffer");
+    /* Copy all the frames we got into the adapter, and then extract at most
+     * @wanted size of frames from it. This helps when ::GetBuffer returns more
+     * data than we can handle right now. */
+    {
+      GstBuffer *tmp = gst_buffer_new_allocate (NULL, got_frames * bpf, NULL);
+      /* If flags has AUDCLNT_BUFFERFLAGS_SILENT, we will ignore the actual
+       * data and write out silence, see:
+       * https://docs.microsoft.com/en-us/windows/win32/api/audioclient/ne-audioclient-_audclnt_bufferflags */
+      if (flags & AUDCLNT_BUFFERFLAGS_SILENT)
+        memset (from, 0, got_frames * bpf);
+      gst_buffer_fill (tmp, 0, from, got_frames * bpf);
+      gst_adapter_push (self->adapter, tmp);
+    }
 
-    want_frames = wanted / self->mix_format->nBlockAlign;
+    /* Release all captured buffers; we copied them above */
+    hr = IAudioCaptureClient_ReleaseBuffer (self->capture_client, got_frames);
+    from = NULL;
+    HR_FAILED_ELEMENT_ERROR_AND (hr, IAudioCaptureClient::ReleaseBuffer, self,
+        goto err);
 
-    /* If GetBuffer is returning more frames than we can handle, all we can do is
-     * hope that this is temporary and that things will settle down later. */
-    if (G_UNLIKELY (have_frames > want_frames))
-      GST_WARNING_OBJECT (self, "captured too many frames: have %i, want %i",
-          have_frames, want_frames);
+    want_frames = wanted / bpf;
+    avail_frames = gst_adapter_available (self->adapter) / bpf;
 
     /* Only copy data that will fit into the allocated buffer of size @length */
-    n_frames = MIN (have_frames, want_frames);
-    read_len = n_frames * self->mix_format->nBlockAlign;
+    n_frames = MIN (avail_frames, want_frames);
+    read_len = n_frames * bpf;
 
-    {
-      guint bpf = self->mix_format->nBlockAlign;
-      GST_DEBUG_OBJECT (self, "have: %i (%i bytes), can read: %i (%i bytes), "
-          "will read: %i (%i bytes)", have_frames, have_frames * bpf,
-          want_frames, wanted, n_frames, read_len);
-    }
+    GST_DEBUG_OBJECT (self, "frames captured: %i (%i bytes), "
+        "can read: %i (%i bytes), will read: %i (%i bytes), "
+        "adapter has: %i (%i bytes)", got_frames, got_frames * bpf, want_frames,
+        wanted, n_frames, read_len, avail_frames, avail_frames * bpf);
 
-    memcpy (data, from, read_len);
+    memcpy (data, gst_adapter_map (self->adapter, read_len), read_len);
+    gst_adapter_flush (self->adapter, read_len);
     wanted -= read_len;
-
-    /* Always release all captured buffers if we've captured any at all */
-    hr = IAudioCaptureClient_ReleaseBuffer (self->capture_client, have_frames);
-    if (hr != S_OK) {
-      gchar *msg = gst_wasapi_util_hresult_to_string (hr);
-      GST_ERROR_OBJECT (self,
-          "IAudioCaptureClient::ReleaseBuffer () failed: %s", msg);
-      g_free (msg);
-      goto beach;
-    }
   }
 
 
-beach:
-
+out:
   return length;
+
+err:
+  length = -1;
+  goto out;
 }
 
 static guint
@@ -659,12 +673,7 @@ gst_wasapi_src_delay (GstAudioSrc * asrc)
   HRESULT hr;
 
   hr = IAudioClient_GetCurrentPadding (self->client, &delay);
-  if (hr != S_OK) {
-    gchar *msg = gst_wasapi_util_hresult_to_string (hr);
-    GST_ELEMENT_ERROR (self, RESOURCE, READ, (NULL),
-        ("IAudioClient::GetCurrentPadding failed %s", msg));
-    g_free (msg);
-  }
+  HR_FAILED_RET (hr, IAudioClock::GetCurrentPadding, 0);
 
   return delay;
 }
@@ -675,25 +684,21 @@ gst_wasapi_src_reset (GstAudioSrc * asrc)
   GstWasapiSrc *self = GST_WASAPI_SRC (asrc);
   HRESULT hr;
 
-  if (self->client) {
-    hr = IAudioClient_Stop (self->client);
-    if (hr != S_OK) {
-      gchar *msg = gst_wasapi_util_hresult_to_string (hr);
-      GST_ERROR_OBJECT (self, "IAudioClient::Stop () failed: %s", msg);
-      g_free (msg);
-      return;
-    }
+  if (!self->client)
+    return;
 
-    hr = IAudioClient_Reset (self->client);
-    if (hr != S_OK) {
-      gchar *msg = gst_wasapi_util_hresult_to_string (hr);
-      GST_ERROR_OBJECT (self, "IAudioClient::Reset () failed: %s", msg);
-      g_free (msg);
-      return;
-    }
-  }
+  GST_OBJECT_LOCK (self);
+  hr = IAudioClient_Stop (self->client);
+  HR_FAILED_RET (hr, IAudioClock::Stop,);
+
+  hr = IAudioClient_Reset (self->client);
+  HR_FAILED_RET (hr, IAudioClock::Reset,);
+
+  self->client_needs_restart = TRUE;
+  GST_OBJECT_UNLOCK (self);
 }
 
+#if DEFAULT_PROVIDE_CLOCK
 static GstClockTime
 gst_wasapi_src_get_time (GstClock * clock, gpointer user_data)
 {
@@ -706,8 +711,7 @@ gst_wasapi_src_get_time (GstClock * clock, gpointer user_data)
     return GST_CLOCK_TIME_NONE;
 
   hr = IAudioClock_GetPosition (self->client_clock, &devpos, NULL);
-  if (G_UNLIKELY (hr != S_OK))
-    return GST_CLOCK_TIME_NONE;
+  HR_FAILED_RET (hr, IAudioClock::GetPosition, GST_CLOCK_TIME_NONE);
 
   result = gst_util_uint64_scale_int (devpos, GST_SECOND,
       self->client_clock_freq);
@@ -721,3 +725,4 @@ gst_wasapi_src_get_time (GstClock * clock, gpointer user_data)
 
   return result;
 }
+#endif