• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>
3  * Copyright (C) 2013 Collabora Ltd.
4  *   Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
5  * Copyright (C) 2018 Centricular Ltd.
6  *   Author: Nirbheek Chauhan <nirbheek@centricular.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 /**
25  * SECTION:element-wasapisink
26  * @title: wasapisink
27  *
28  * Provides audio playback using the Windows Audio Session API available with
29  * Vista and newer.
30  *
31  * ## Example pipelines
32  * |[
33  * gst-launch-1.0 -v audiotestsrc samplesperbuffer=160 ! wasapisink
34  * ]| Generate 20 ms buffers and render to the default audio device.
35  *
36  * |[
37  * gst-launch-1.0 -v audiotestsrc samplesperbuffer=160 ! wasapisink low-latency=true
38  * ]| Same as above, but with the minimum possible latency
39  *
40  */
41 #ifdef HAVE_CONFIG_H
42 #  include <config.h>
43 #endif
44 
45 #include "gstwasapisink.h"
46 
47 #include <avrt.h>
48 
49 GST_DEBUG_CATEGORY_STATIC (gst_wasapi_sink_debug);
50 #define GST_CAT_DEFAULT gst_wasapi_sink_debug
51 
52 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
53     GST_PAD_SINK,
54     GST_PAD_ALWAYS,
55     GST_STATIC_CAPS (GST_WASAPI_STATIC_CAPS));
56 
57 #define DEFAULT_ROLE          GST_WASAPI_DEVICE_ROLE_CONSOLE
58 #define DEFAULT_MUTE          FALSE
59 #define DEFAULT_EXCLUSIVE     FALSE
60 #define DEFAULT_LOW_LATENCY   FALSE
61 #define DEFAULT_AUDIOCLIENT3  TRUE
62 
63 enum
64 {
65   PROP_0,
66   PROP_ROLE,
67   PROP_MUTE,
68   PROP_DEVICE,
69   PROP_EXCLUSIVE,
70   PROP_LOW_LATENCY,
71   PROP_AUDIOCLIENT3
72 };
73 
74 static void gst_wasapi_sink_dispose (GObject * object);
75 static void gst_wasapi_sink_finalize (GObject * object);
76 static void gst_wasapi_sink_set_property (GObject * object, guint prop_id,
77     const GValue * value, GParamSpec * pspec);
78 static void gst_wasapi_sink_get_property (GObject * object, guint prop_id,
79     GValue * value, GParamSpec * pspec);
80 
81 static GstCaps *gst_wasapi_sink_get_caps (GstBaseSink * bsink,
82     GstCaps * filter);
83 
84 static gboolean gst_wasapi_sink_prepare (GstAudioSink * asink,
85     GstAudioRingBufferSpec * spec);
86 static gboolean gst_wasapi_sink_unprepare (GstAudioSink * asink);
87 static gboolean gst_wasapi_sink_open (GstAudioSink * asink);
88 static gboolean gst_wasapi_sink_close (GstAudioSink * asink);
89 static gint gst_wasapi_sink_write (GstAudioSink * asink,
90     gpointer data, guint length);
91 static guint gst_wasapi_sink_delay (GstAudioSink * asink);
92 static void gst_wasapi_sink_reset (GstAudioSink * asink);
93 
94 #define gst_wasapi_sink_parent_class parent_class
95 G_DEFINE_TYPE (GstWasapiSink, gst_wasapi_sink, GST_TYPE_AUDIO_SINK);
96 
97 static void
gst_wasapi_sink_class_init(GstWasapiSinkClass * klass)98 gst_wasapi_sink_class_init (GstWasapiSinkClass * klass)
99 {
100   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
101   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
102   GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
103   GstAudioSinkClass *gstaudiosink_class = GST_AUDIO_SINK_CLASS (klass);
104 
105   gobject_class->dispose = gst_wasapi_sink_dispose;
106   gobject_class->finalize = gst_wasapi_sink_finalize;
107   gobject_class->set_property = gst_wasapi_sink_set_property;
108   gobject_class->get_property = gst_wasapi_sink_get_property;
109 
110   g_object_class_install_property (gobject_class,
111       PROP_ROLE,
112       g_param_spec_enum ("role", "Role",
113           "Role of the device: communications, multimedia, etc",
114           GST_WASAPI_DEVICE_TYPE_ROLE, DEFAULT_ROLE, G_PARAM_READWRITE |
115           G_PARAM_STATIC_STRINGS | GST_PARAM_MUTABLE_READY));
116 
117   g_object_class_install_property (gobject_class,
118       PROP_MUTE,
119       g_param_spec_boolean ("mute", "Mute", "Mute state of this stream",
120           DEFAULT_MUTE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
121           GST_PARAM_MUTABLE_PLAYING));
122 
123   g_object_class_install_property (gobject_class,
124       PROP_DEVICE,
125       g_param_spec_string ("device", "Device",
126           "WASAPI playback device as a GUID string",
127           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
128 
129   g_object_class_install_property (gobject_class,
130       PROP_EXCLUSIVE,
131       g_param_spec_boolean ("exclusive", "Exclusive mode",
132           "Open the device in exclusive mode",
133           DEFAULT_EXCLUSIVE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
134 
135   g_object_class_install_property (gobject_class,
136       PROP_LOW_LATENCY,
137       g_param_spec_boolean ("low-latency", "Low latency",
138           "Optimize all settings for lowest latency. Always safe to enable.",
139           DEFAULT_LOW_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
140 
141   g_object_class_install_property (gobject_class,
142       PROP_AUDIOCLIENT3,
143       g_param_spec_boolean ("use-audioclient3", "Use the AudioClient3 API",
144           "Use the Windows 10 AudioClient3 API when available and if the "
145           "low-latency property is set to TRUE",
146           DEFAULT_AUDIOCLIENT3, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
147 
148   gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
149   gst_element_class_set_static_metadata (gstelement_class, "WasapiSrc",
150       "Sink/Audio/Hardware",
151       "Stream audio to an audio capture device through WASAPI",
152       "Nirbheek Chauhan <nirbheek@centricular.com>, "
153       "Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>");
154 
155   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wasapi_sink_get_caps);
156 
157   gstaudiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_wasapi_sink_prepare);
158   gstaudiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_wasapi_sink_unprepare);
159   gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_wasapi_sink_open);
160   gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_wasapi_sink_close);
161   gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_wasapi_sink_write);
162   gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_wasapi_sink_delay);
163   gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_wasapi_sink_reset);
164 
165   GST_DEBUG_CATEGORY_INIT (gst_wasapi_sink_debug, "wasapisink",
166       0, "Windows audio session API sink");
167 
168   gst_type_mark_as_plugin_api (GST_WASAPI_DEVICE_TYPE_ROLE, 0);
169 }
170 
171 static void
gst_wasapi_sink_init(GstWasapiSink * self)172 gst_wasapi_sink_init (GstWasapiSink * self)
173 {
174   self->role = DEFAULT_ROLE;
175   self->mute = DEFAULT_MUTE;
176   self->sharemode = AUDCLNT_SHAREMODE_SHARED;
177   self->low_latency = DEFAULT_LOW_LATENCY;
178   self->try_audioclient3 = DEFAULT_AUDIOCLIENT3;
179   self->event_handle = CreateEvent (NULL, FALSE, FALSE, NULL);
180   self->cancellable = CreateEvent (NULL, TRUE, FALSE, NULL);
181   self->client_needs_restart = FALSE;
182 
183   self->enumerator = gst_mm_device_enumerator_new ();
184 }
185 
186 static void
gst_wasapi_sink_dispose(GObject * object)187 gst_wasapi_sink_dispose (GObject * object)
188 {
189   GstWasapiSink *self = GST_WASAPI_SINK (object);
190 
191   if (self->event_handle != NULL) {
192     CloseHandle (self->event_handle);
193     self->event_handle = NULL;
194   }
195 
196   if (self->cancellable != NULL) {
197     CloseHandle (self->cancellable);
198     self->cancellable = NULL;
199   }
200 
201   if (self->client != NULL) {
202     IUnknown_Release (self->client);
203     self->client = NULL;
204   }
205 
206   if (self->render_client != NULL) {
207     IUnknown_Release (self->render_client);
208     self->render_client = NULL;
209   }
210 
211   gst_clear_object (&self->enumerator);
212 
213   G_OBJECT_CLASS (gst_wasapi_sink_parent_class)->dispose (object);
214 }
215 
216 static void
gst_wasapi_sink_finalize(GObject * object)217 gst_wasapi_sink_finalize (GObject * object)
218 {
219   GstWasapiSink *self = GST_WASAPI_SINK (object);
220 
221   CoTaskMemFree (self->mix_format);
222   self->mix_format = NULL;
223 
224   if (self->cached_caps != NULL) {
225     gst_caps_unref (self->cached_caps);
226     self->cached_caps = NULL;
227   }
228 
229   g_clear_pointer (&self->positions, g_free);
230   g_clear_pointer (&self->device_strid, g_free);
231   self->mute = FALSE;
232 
233   G_OBJECT_CLASS (gst_wasapi_sink_parent_class)->finalize (object);
234 }
235 
236 static void
gst_wasapi_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)237 gst_wasapi_sink_set_property (GObject * object, guint prop_id,
238     const GValue * value, GParamSpec * pspec)
239 {
240   GstWasapiSink *self = GST_WASAPI_SINK (object);
241 
242   switch (prop_id) {
243     case PROP_ROLE:
244       self->role = gst_wasapi_device_role_to_erole (g_value_get_enum (value));
245       break;
246     case PROP_MUTE:
247       self->mute = g_value_get_boolean (value);
248       break;
249     case PROP_DEVICE:
250     {
251       const gchar *device = g_value_get_string (value);
252       g_free (self->device_strid);
253       self->device_strid =
254           device ? g_utf8_to_utf16 (device, -1, NULL, NULL, NULL) : NULL;
255       break;
256     }
257     case PROP_EXCLUSIVE:
258       self->sharemode = g_value_get_boolean (value)
259           ? AUDCLNT_SHAREMODE_EXCLUSIVE : AUDCLNT_SHAREMODE_SHARED;
260       break;
261     case PROP_LOW_LATENCY:
262       self->low_latency = g_value_get_boolean (value);
263       break;
264     case PROP_AUDIOCLIENT3:
265       self->try_audioclient3 = g_value_get_boolean (value);
266       break;
267     default:
268       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
269       break;
270   }
271 }
272 
273 static void
gst_wasapi_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)274 gst_wasapi_sink_get_property (GObject * object, guint prop_id,
275     GValue * value, GParamSpec * pspec)
276 {
277   GstWasapiSink *self = GST_WASAPI_SINK (object);
278 
279   switch (prop_id) {
280     case PROP_ROLE:
281       g_value_set_enum (value, gst_wasapi_erole_to_device_role (self->role));
282       break;
283     case PROP_MUTE:
284       g_value_set_boolean (value, self->mute);
285       break;
286     case PROP_DEVICE:
287       g_value_take_string (value, self->device_strid ?
288           g_utf16_to_utf8 (self->device_strid, -1, NULL, NULL, NULL) : NULL);
289       break;
290     case PROP_EXCLUSIVE:
291       g_value_set_boolean (value,
292           self->sharemode == AUDCLNT_SHAREMODE_EXCLUSIVE);
293       break;
294     case PROP_LOW_LATENCY:
295       g_value_set_boolean (value, self->low_latency);
296       break;
297     case PROP_AUDIOCLIENT3:
298       g_value_set_boolean (value, self->try_audioclient3);
299       break;
300     default:
301       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
302       break;
303   }
304 }
305 
306 static gboolean
gst_wasapi_sink_can_audioclient3(GstWasapiSink * self)307 gst_wasapi_sink_can_audioclient3 (GstWasapiSink * self)
308 {
309   /* AudioClient3 API only makes sense in shared mode */
310   if (self->sharemode != AUDCLNT_SHAREMODE_SHARED)
311     return FALSE;
312 
313   if (!self->try_audioclient3) {
314     GST_INFO_OBJECT (self, "AudioClient3 disabled by user");
315     return FALSE;
316   }
317 
318   if (!gst_wasapi_util_have_audioclient3 ()) {
319     GST_INFO_OBJECT (self, "AudioClient3 not available on this OS");
320     return FALSE;
321   }
322 
323   /* Only use audioclient3 when low-latency is requested because otherwise
324    * very slow machines and VMs with 1 CPU allocated will get glitches:
325    * https://bugzilla.gnome.org/show_bug.cgi?id=794497 */
326   if (!self->low_latency) {
327     GST_INFO_OBJECT (self, "AudioClient3 disabled because low-latency mode "
328         "was not requested");
329     return FALSE;
330   }
331 
332   return TRUE;
333 }
334 
335 static GstCaps *
gst_wasapi_sink_get_caps(GstBaseSink * bsink,GstCaps * filter)336 gst_wasapi_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
337 {
338   GstWasapiSink *self = GST_WASAPI_SINK (bsink);
339   WAVEFORMATEX *format = NULL;
340   GstCaps *caps = NULL;
341 
342   GST_DEBUG_OBJECT (self, "entering get caps");
343 
344   if (self->cached_caps) {
345     caps = gst_caps_ref (self->cached_caps);
346   } else {
347     GstCaps *template_caps;
348     gboolean ret;
349 
350     template_caps = gst_pad_get_pad_template_caps (bsink->sinkpad);
351 
352     if (!self->client) {
353       caps = template_caps;
354       goto out;
355     }
356 
357     ret = gst_wasapi_util_get_device_format (GST_ELEMENT (self),
358         self->sharemode, self->device, self->client, &format);
359     if (!ret) {
360       GST_ELEMENT_ERROR (self, STREAM, FORMAT, (NULL),
361           ("failed to detect format"));
362       gst_caps_unref (template_caps);
363       return NULL;
364     }
365 
366     gst_wasapi_util_parse_waveformatex ((WAVEFORMATEXTENSIBLE *) format,
367         template_caps, &caps, &self->positions);
368     if (caps == NULL) {
369       GST_ELEMENT_ERROR (self, STREAM, FORMAT, (NULL), ("unknown format"));
370       gst_caps_unref (template_caps);
371       return NULL;
372     }
373 
374     {
375       gchar *pos_str = gst_audio_channel_positions_to_string (self->positions,
376           format->nChannels);
377       GST_INFO_OBJECT (self, "positions are: %s", pos_str);
378       g_free (pos_str);
379     }
380 
381     self->mix_format = format;
382     gst_caps_replace (&self->cached_caps, caps);
383     gst_caps_unref (template_caps);
384   }
385 
386   if (filter) {
387     GstCaps *filtered =
388         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
389     gst_caps_unref (caps);
390     caps = filtered;
391   }
392 
393 out:
394   GST_DEBUG_OBJECT (self, "returning caps %" GST_PTR_FORMAT, caps);
395   return caps;
396 }
397 
398 static gboolean
gst_wasapi_sink_open(GstAudioSink * asink)399 gst_wasapi_sink_open (GstAudioSink * asink)
400 {
401   GstWasapiSink *self = GST_WASAPI_SINK (asink);
402   gboolean res = FALSE;
403   IMMDevice *device = NULL;
404   IAudioClient *client = NULL;
405 
406   GST_DEBUG_OBJECT (self, "opening device");
407 
408   if (self->client)
409     return TRUE;
410 
411   /* FIXME: Switching the default device does not switch the stream to it,
412    * even if the old device was unplugged. We need to handle this somehow.
413    * For example, perhaps we should automatically switch to the new device if
414    * the default device is changed and a device isn't explicitly selected. */
415   if (!gst_wasapi_util_get_device (self->enumerator, eRender,
416           self->role, self->device_strid, &device)
417       || !gst_wasapi_util_get_audio_client (GST_ELEMENT (self),
418           device, &client)) {
419     if (!self->device_strid)
420       GST_ELEMENT_ERROR (self, RESOURCE, OPEN_WRITE, (NULL),
421           ("Failed to get default device"));
422     else
423       GST_ELEMENT_ERROR (self, RESOURCE, OPEN_WRITE, (NULL),
424           ("Failed to open device %S", self->device_strid));
425     goto beach;
426   }
427 
428   self->client = client;
429   self->device = device;
430   res = TRUE;
431 
432 beach:
433 
434   return res;
435 }
436 
437 static gboolean
gst_wasapi_sink_close(GstAudioSink * asink)438 gst_wasapi_sink_close (GstAudioSink * asink)
439 {
440   GstWasapiSink *self = GST_WASAPI_SINK (asink);
441 
442   if (self->device != NULL) {
443     IUnknown_Release (self->device);
444     self->device = NULL;
445   }
446 
447   if (self->client != NULL) {
448     IUnknown_Release (self->client);
449     self->client = NULL;
450   }
451 
452   return TRUE;
453 }
454 
455 /* Get the empty space in the buffer that we have to write to */
456 static gint
gst_wasapi_sink_get_can_frames(GstWasapiSink * self)457 gst_wasapi_sink_get_can_frames (GstWasapiSink * self)
458 {
459   HRESULT hr;
460   guint n_frames_padding;
461 
462   /* There is no padding in exclusive mode since there is no ringbuffer */
463   if (self->sharemode == AUDCLNT_SHAREMODE_EXCLUSIVE) {
464     GST_DEBUG_OBJECT (self, "exclusive mode, can write: %i",
465         self->buffer_frame_count);
466     return self->buffer_frame_count;
467   }
468 
469   /* Frames the card hasn't rendered yet */
470   hr = IAudioClient_GetCurrentPadding (self->client, &n_frames_padding);
471   HR_FAILED_ELEMENT_ERROR_RET (hr, IAudioClient::GetCurrentPadding, self, -1);
472 
473   GST_DEBUG_OBJECT (self, "%i unread frames (padding)", n_frames_padding);
474 
475   /* We can write out these many frames */
476   return self->buffer_frame_count - n_frames_padding;
477 }
478 
479 static gboolean
gst_wasapi_sink_prepare(GstAudioSink * asink,GstAudioRingBufferSpec * spec)480 gst_wasapi_sink_prepare (GstAudioSink * asink, GstAudioRingBufferSpec * spec)
481 {
482   GstWasapiSink *self = GST_WASAPI_SINK (asink);
483   gboolean res = FALSE;
484   REFERENCE_TIME latency_rt;
485   guint bpf, rate, devicep_frames;
486   HRESULT hr;
487 
488   if (!self->client) {
489     GST_DEBUG_OBJECT (self, "no IAudioClient, creating a new one");
490     if (!gst_wasapi_util_get_audio_client (GST_ELEMENT (self),
491             self->device, &self->client))
492       goto beach;
493   }
494 
495   if (gst_wasapi_sink_can_audioclient3 (self)) {
496     if (!gst_wasapi_util_initialize_audioclient3 (GST_ELEMENT (self), spec,
497             (IAudioClient3 *) self->client, self->mix_format, self->low_latency,
498             FALSE, &devicep_frames))
499       goto beach;
500   } else {
501     if (!gst_wasapi_util_initialize_audioclient (GST_ELEMENT (self), spec,
502             self->client, self->mix_format, self->sharemode, self->low_latency,
503             FALSE, &devicep_frames))
504       goto beach;
505   }
506 
507   bpf = GST_AUDIO_INFO_BPF (&spec->info);
508   rate = GST_AUDIO_INFO_RATE (&spec->info);
509 
510   /* Total size of the allocated buffer that we will write to */
511   hr = IAudioClient_GetBufferSize (self->client, &self->buffer_frame_count);
512   HR_FAILED_GOTO (hr, IAudioClient::GetBufferSize, beach);
513 
514   GST_INFO_OBJECT (self, "buffer size is %i frames, device period is %i "
515       "frames, bpf is %i bytes, rate is %i Hz", self->buffer_frame_count,
516       devicep_frames, bpf, rate);
517 
518   /* Actual latency-time/buffer-time will be different now */
519   spec->segsize = devicep_frames * bpf;
520 
521   /* We need a minimum of 2 segments to ensure glitch-free playback */
522   spec->segtotal = MAX (self->buffer_frame_count * bpf / spec->segsize, 2);
523 
524   GST_INFO_OBJECT (self, "segsize is %i, segtotal is %i", spec->segsize,
525       spec->segtotal);
526 
527   /* Get latency for logging */
528   hr = IAudioClient_GetStreamLatency (self->client, &latency_rt);
529   HR_FAILED_GOTO (hr, IAudioClient::GetStreamLatency, beach);
530 
531   GST_INFO_OBJECT (self, "wasapi stream latency: %" G_GINT64_FORMAT " (%"
532       G_GINT64_FORMAT "ms)", latency_rt, latency_rt / 10000);
533 
534   /* Set the event handler which will trigger writes */
535   hr = IAudioClient_SetEventHandle (self->client, self->event_handle);
536   HR_FAILED_GOTO (hr, IAudioClient::SetEventHandle, beach);
537 
538   /* Get render sink client and start it up */
539   if (!gst_wasapi_util_get_render_client (GST_ELEMENT (self), self->client,
540           &self->render_client)) {
541     goto beach;
542   }
543 
544   GST_INFO_OBJECT (self, "got render client");
545 
546   /* To avoid start-up glitches, before starting the streaming, we fill the
547    * buffer with silence as recommended by the documentation:
548    * https://msdn.microsoft.com/en-us/library/windows/desktop/dd370879%28v=vs.85%29.aspx */
549   {
550     gint n_frames, len;
551     gint16 *dst = NULL;
552 
553     n_frames = gst_wasapi_sink_get_can_frames (self);
554     if (n_frames < 1) {
555       GST_ELEMENT_ERROR (self, RESOURCE, WRITE, (NULL),
556           ("should have more than %i frames to write", n_frames));
557       goto beach;
558     }
559 
560     len = n_frames * self->mix_format->nBlockAlign;
561 
562     hr = IAudioRenderClient_GetBuffer (self->render_client, n_frames,
563         (BYTE **) & dst);
564     HR_FAILED_GOTO (hr, IAudioRenderClient::GetBuffer, beach);
565 
566     GST_DEBUG_OBJECT (self, "pre-wrote %i bytes of silence", len);
567 
568     hr = IAudioRenderClient_ReleaseBuffer (self->render_client, n_frames,
569         AUDCLNT_BUFFERFLAGS_SILENT);
570     HR_FAILED_GOTO (hr, IAudioRenderClient::ReleaseBuffer, beach);
571   }
572 
573   hr = IAudioClient_Start (self->client);
574   HR_FAILED_GOTO (hr, IAudioClient::Start, beach);
575   self->client_needs_restart = FALSE;
576 
577   gst_audio_ring_buffer_set_channel_positions (GST_AUDIO_BASE_SINK
578       (self)->ringbuffer, self->positions);
579 
580   res = TRUE;
581 
582   /* reset cancellable event handle */
583   ResetEvent (self->cancellable);
584 
585 beach:
586   /* unprepare() is not called if prepare() fails, but we want it to be, so call
587    * it manually when needed */
588   if (!res)
589     gst_wasapi_sink_unprepare (asink);
590 
591   return res;
592 }
593 
594 static gboolean
gst_wasapi_sink_unprepare(GstAudioSink * asink)595 gst_wasapi_sink_unprepare (GstAudioSink * asink)
596 {
597   GstWasapiSink *self = GST_WASAPI_SINK (asink);
598 
599   if (self->client != NULL) {
600     IUnknown_Release (self->client);
601     self->client = NULL;
602   }
603 
604   if (self->render_client != NULL) {
605     IUnknown_Release (self->render_client);
606     self->render_client = NULL;
607   }
608 
609   return TRUE;
610 }
611 
612 static gint
gst_wasapi_sink_write(GstAudioSink * asink,gpointer data,guint length)613 gst_wasapi_sink_write (GstAudioSink * asink, gpointer data, guint length)
614 {
615   GstWasapiSink *self = GST_WASAPI_SINK (asink);
616   HRESULT hr;
617   gint16 *dst = NULL;
618   DWORD dwWaitResult;
619   guint can_frames, have_frames, n_frames, write_len, written_len = 0;
620   HANDLE event_handle[2];
621 
622   event_handle[0] = self->event_handle;
623   event_handle[1] = self->cancellable;
624 
625   GST_OBJECT_LOCK (self);
626   if (self->client_needs_restart) {
627     hr = IAudioClient_Start (self->client);
628     HR_FAILED_ELEMENT_ERROR_AND (hr, IAudioClient::Start, self,
629         GST_OBJECT_UNLOCK (self); goto err);
630     self->client_needs_restart = FALSE;
631     ResetEvent (self->cancellable);
632   }
633   GST_OBJECT_UNLOCK (self);
634 
635   /* We have N frames to be written out */
636   have_frames = length / (self->mix_format->nBlockAlign);
637 
638   if (self->sharemode == AUDCLNT_SHAREMODE_EXCLUSIVE) {
639     /* In exclusive mode we have to wait always */
640     dwWaitResult = WaitForMultipleObjects (2, event_handle, FALSE, INFINITE);
641     if (dwWaitResult != WAIT_OBJECT_0 && dwWaitResult != WAIT_OBJECT_0 + 1) {
642       GST_ERROR_OBJECT (self, "Error waiting for event handle: %x",
643           (guint) dwWaitResult);
644       goto err;
645     }
646 
647     /* ::reset was requested */
648     if (dwWaitResult == WAIT_OBJECT_0 + 1) {
649       GST_DEBUG_OBJECT (self, "operation was cancelled");
650       return -1;
651     }
652 
653     can_frames = gst_wasapi_sink_get_can_frames (self);
654     if (can_frames < 0) {
655       GST_ERROR_OBJECT (self, "Error getting frames to write to");
656       goto err;
657     }
658     /* In exclusive mode we need to fill the whole buffer in one go or
659      * GetBuffer will error out */
660     if (can_frames != have_frames) {
661       GST_ERROR_OBJECT (self,
662           "Need at %i frames to write for exclusive mode, but got %i",
663           can_frames, have_frames);
664       goto err;
665     }
666   } else {
667     /* In shared mode we can write parts of the buffer, so only wait
668      * in case we can't write anything */
669     can_frames = gst_wasapi_sink_get_can_frames (self);
670     if (can_frames < 0) {
671       GST_ERROR_OBJECT (self, "Error getting frames to write to");
672       goto err;
673     }
674 
675     if (can_frames == 0) {
676       dwWaitResult = WaitForMultipleObjects (2, event_handle, FALSE, INFINITE);
677       if (dwWaitResult != WAIT_OBJECT_0 && dwWaitResult != WAIT_OBJECT_0 + 1) {
678         GST_ERROR_OBJECT (self, "Error waiting for event handle: %x",
679             (guint) dwWaitResult);
680         goto err;
681       }
682 
683       /* ::reset was requested */
684       if (dwWaitResult == WAIT_OBJECT_0 + 1) {
685         GST_DEBUG_OBJECT (self, "operation was cancelled");
686         return -1;
687       }
688 
689       can_frames = gst_wasapi_sink_get_can_frames (self);
690       if (can_frames < 0) {
691         GST_ERROR_OBJECT (self, "Error getting frames to write to");
692         goto err;
693       }
694     }
695   }
696 
697   /* We will write out these many frames, and this much length */
698   n_frames = MIN (can_frames, have_frames);
699   write_len = n_frames * self->mix_format->nBlockAlign;
700 
701   GST_DEBUG_OBJECT (self, "total: %i, have_frames: %i (%i bytes), "
702       "can_frames: %i, will write: %i (%i bytes)", self->buffer_frame_count,
703       have_frames, length, can_frames, n_frames, write_len);
704 
705   hr = IAudioRenderClient_GetBuffer (self->render_client, n_frames,
706       (BYTE **) & dst);
707   HR_FAILED_ELEMENT_ERROR_AND (hr, IAudioRenderClient::GetBuffer, self,
708       goto err);
709 
710   memcpy (dst, data, write_len);
711 
712   hr = IAudioRenderClient_ReleaseBuffer (self->render_client, n_frames,
713       self->mute ? AUDCLNT_BUFFERFLAGS_SILENT : 0);
714   HR_FAILED_ELEMENT_ERROR_AND (hr, IAudioRenderClient::ReleaseBuffer, self,
715       goto err);
716 
717   written_len = write_len;
718 
719 out:
720   return written_len;
721 
722 err:
723   written_len = -1;
724   goto out;
725 }
726 
727 static guint
gst_wasapi_sink_delay(GstAudioSink * asink)728 gst_wasapi_sink_delay (GstAudioSink * asink)
729 {
730   GstWasapiSink *self = GST_WASAPI_SINK (asink);
731   guint delay = 0;
732   HRESULT hr;
733 
734   hr = IAudioClient_GetCurrentPadding (self->client, &delay);
735   HR_FAILED_RET (hr, IAudioClient::GetCurrentPadding, 0);
736 
737   return delay;
738 }
739 
740 static void
gst_wasapi_sink_reset(GstAudioSink * asink)741 gst_wasapi_sink_reset (GstAudioSink * asink)
742 {
743   GstWasapiSink *self = GST_WASAPI_SINK (asink);
744   HRESULT hr;
745 
746   GST_INFO_OBJECT (self, "reset called");
747 
748   if (!self->client)
749     return;
750 
751   SetEvent (self->cancellable);
752 
753   GST_OBJECT_LOCK (self);
754   hr = IAudioClient_Stop (self->client);
755   HR_FAILED_AND (hr, IAudioClient::Stop, goto err);
756 
757   hr = IAudioClient_Reset (self->client);
758   HR_FAILED_AND (hr, IAudioClient::Reset, goto err);
759 
760 err:
761   self->client_needs_restart = TRUE;
762   GST_OBJECT_UNLOCK (self);
763 }
764