• 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  * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24 
25 /**
26  * SECTION:element-wasapi2sink
27  * @title: wasapi2sink
28  *
29  * Provides audio playback using the Windows Audio Session API available with
30  * Windows 10.
31  *
32  * ## Example pipelines
33  * |[
34  * gst-launch-1.0 -v audiotestsrc ! wasapi2sink
35  * ]| Generate audio test buffers and render to the default audio device.
36  *
37  * |[
38  * gst-launch-1.0 -v audiotestsink samplesperbuffer=160 ! wasapi2sink low-latency=true
39  * ]| Same as above, but with the minimum possible latency
40  *
41  */
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45 
46 #include "gstwasapi2sink.h"
47 #include "gstwasapi2util.h"
48 #include "gstwasapi2ringbuffer.h"
49 
50 GST_DEBUG_CATEGORY_STATIC (gst_wasapi2_sink_debug);
51 #define GST_CAT_DEFAULT gst_wasapi2_sink_debug
52 
53 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
54     GST_PAD_SINK,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS (GST_WASAPI2_STATIC_CAPS));
57 
58 #define DEFAULT_LOW_LATENCY   FALSE
59 #define DEFAULT_MUTE          FALSE
60 #define DEFAULT_VOLUME        1.0
61 
62 enum
63 {
64   PROP_0,
65   PROP_DEVICE,
66   PROP_LOW_LATENCY,
67   PROP_MUTE,
68   PROP_VOLUME,
69   PROP_DISPATCHER,
70 };
71 
72 struct _GstWasapi2Sink
73 {
74   GstAudioBaseSink parent;
75 
76   /* properties */
77   gchar *device_id;
78   gboolean low_latency;
79   gboolean mute;
80   gdouble volume;
81   gpointer dispatcher;
82 
83   gboolean mute_changed;
84   gboolean volume_changed;
85 };
86 
87 static void gst_wasapi2_sink_finalize (GObject * object);
88 static void gst_wasapi2_sink_set_property (GObject * object, guint prop_id,
89     const GValue * value, GParamSpec * pspec);
90 static void gst_wasapi2_sink_get_property (GObject * object, guint prop_id,
91     GValue * value, GParamSpec * pspec);
92 
93 static GstStateChangeReturn gst_wasapi2_sink_change_state (GstElement *
94     element, GstStateChange transition);
95 
96 static GstCaps *gst_wasapi2_sink_get_caps (GstBaseSink * bsink,
97     GstCaps * filter);
98 static GstAudioRingBuffer *gst_wasapi2_sink_create_ringbuffer (GstAudioBaseSink
99     * sink);
100 
101 static void gst_wasapi2_sink_set_mute (GstWasapi2Sink * self, gboolean mute);
102 static gboolean gst_wasapi2_sink_get_mute (GstWasapi2Sink * self);
103 static void gst_wasapi2_sink_set_volume (GstWasapi2Sink * self, gdouble volume);
104 static gdouble gst_wasapi2_sink_get_volume (GstWasapi2Sink * self);
105 
106 #define gst_wasapi2_sink_parent_class parent_class
107 G_DEFINE_TYPE_WITH_CODE (GstWasapi2Sink, gst_wasapi2_sink,
108     GST_TYPE_AUDIO_BASE_SINK,
109     G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL));
110 
111 static void
gst_wasapi2_sink_class_init(GstWasapi2SinkClass * klass)112 gst_wasapi2_sink_class_init (GstWasapi2SinkClass * klass)
113 {
114   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
115   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
116   GstBaseSinkClass *basesink_class = GST_BASE_SINK_CLASS (klass);
117   GstAudioBaseSinkClass *audiobasesink_class =
118       GST_AUDIO_BASE_SINK_CLASS (klass);
119 
120   gobject_class->finalize = gst_wasapi2_sink_finalize;
121   gobject_class->set_property = gst_wasapi2_sink_set_property;
122   gobject_class->get_property = gst_wasapi2_sink_get_property;
123 
124   g_object_class_install_property (gobject_class, PROP_DEVICE,
125       g_param_spec_string ("device", "Device",
126           "WASAPI playback device as a GUID string",
127           NULL, GST_PARAM_MUTABLE_READY | G_PARAM_READWRITE |
128           G_PARAM_STATIC_STRINGS));
129 
130   g_object_class_install_property (gobject_class, PROP_LOW_LATENCY,
131       g_param_spec_boolean ("low-latency", "Low latency",
132           "Optimize all settings for lowest latency. Always safe to enable.",
133           DEFAULT_LOW_LATENCY, GST_PARAM_MUTABLE_READY | G_PARAM_READWRITE |
134           G_PARAM_STATIC_STRINGS));
135 
136   g_object_class_install_property (gobject_class, PROP_MUTE,
137       g_param_spec_boolean ("mute", "Mute", "Mute state of this stream",
138           DEFAULT_MUTE, GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE |
139           G_PARAM_STATIC_STRINGS));
140 
141   g_object_class_install_property (gobject_class, PROP_VOLUME,
142       g_param_spec_double ("volume", "Volume", "Volume of this stream",
143           0.0, 1.0, DEFAULT_VOLUME,
144           GST_PARAM_MUTABLE_PLAYING | G_PARAM_READWRITE |
145           G_PARAM_STATIC_STRINGS));
146 
147   /**
148    * GstWasapi2Sink:dispatcher:
149    *
150    * ICoreDispatcher COM object used for activating device from UI thread.
151    *
152    * Since: 1.18
153    */
154   g_object_class_install_property (gobject_class, PROP_DISPATCHER,
155       g_param_spec_pointer ("dispatcher", "Dispatcher",
156           "ICoreDispatcher COM object to use. In order for application to ask "
157           "permission of audio device, device activation should be running "
158           "on UI thread via ICoreDispatcher. This element will increase "
159           "the reference count of given ICoreDispatcher and release it after "
160           "use. Therefore, caller does not need to consider additional "
161           "reference count management",
162           GST_PARAM_MUTABLE_READY | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
163 
164   gst_element_class_add_static_pad_template (element_class, &sink_template);
165   gst_element_class_set_static_metadata (element_class, "Wasapi2Sink",
166       "Sink/Audio/Hardware",
167       "Stream audio to an audio capture device through WASAPI",
168       "Nirbheek Chauhan <nirbheek@centricular.com>, "
169       "Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>, "
170       "Seungha Yang <seungha@centricular.com>");
171 
172   element_class->change_state =
173       GST_DEBUG_FUNCPTR (gst_wasapi2_sink_change_state);
174 
175   basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_wasapi2_sink_get_caps);
176 
177   audiobasesink_class->create_ringbuffer =
178       GST_DEBUG_FUNCPTR (gst_wasapi2_sink_create_ringbuffer);
179 
180   GST_DEBUG_CATEGORY_INIT (gst_wasapi2_sink_debug, "wasapi2sink",
181       0, "Windows audio session API sink");
182 }
183 
184 static void
gst_wasapi2_sink_init(GstWasapi2Sink * self)185 gst_wasapi2_sink_init (GstWasapi2Sink * self)
186 {
187   self->low_latency = DEFAULT_LOW_LATENCY;
188   self->mute = DEFAULT_MUTE;
189   self->volume = DEFAULT_VOLUME;
190 }
191 
192 static void
gst_wasapi2_sink_finalize(GObject * object)193 gst_wasapi2_sink_finalize (GObject * object)
194 {
195   GstWasapi2Sink *self = GST_WASAPI2_SINK (object);
196 
197   g_free (self->device_id);
198 
199   G_OBJECT_CLASS (parent_class)->finalize (object);
200 }
201 
202 static void
gst_wasapi2_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)203 gst_wasapi2_sink_set_property (GObject * object, guint prop_id,
204     const GValue * value, GParamSpec * pspec)
205 {
206   GstWasapi2Sink *self = GST_WASAPI2_SINK (object);
207 
208   switch (prop_id) {
209     case PROP_DEVICE:
210       g_free (self->device_id);
211       self->device_id = g_value_dup_string (value);
212       break;
213     case PROP_LOW_LATENCY:
214       self->low_latency = g_value_get_boolean (value);
215       break;
216     case PROP_MUTE:
217       gst_wasapi2_sink_set_mute (self, g_value_get_boolean (value));
218       break;
219     case PROP_VOLUME:
220       gst_wasapi2_sink_set_volume (self, g_value_get_double (value));
221       break;
222     case PROP_DISPATCHER:
223       self->dispatcher = g_value_get_pointer (value);
224       break;
225     default:
226       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227       break;
228   }
229 }
230 
231 static void
gst_wasapi2_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)232 gst_wasapi2_sink_get_property (GObject * object, guint prop_id,
233     GValue * value, GParamSpec * pspec)
234 {
235   GstWasapi2Sink *self = GST_WASAPI2_SINK (object);
236 
237   switch (prop_id) {
238     case PROP_DEVICE:
239       g_value_set_string (value, self->device_id);
240       break;
241     case PROP_LOW_LATENCY:
242       g_value_set_boolean (value, self->low_latency);
243       break;
244     case PROP_MUTE:
245       g_value_set_boolean (value, gst_wasapi2_sink_get_mute (self));
246       break;
247     case PROP_VOLUME:
248       g_value_set_double (value, gst_wasapi2_sink_get_volume (self));
249       break;
250     default:
251       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252       break;
253   }
254 }
255 
256 static GstStateChangeReturn
gst_wasapi2_sink_change_state(GstElement * element,GstStateChange transition)257 gst_wasapi2_sink_change_state (GstElement * element, GstStateChange transition)
258 {
259   GstWasapi2Sink *self = GST_WASAPI2_SINK (element);
260   GstAudioBaseSink *asink = GST_AUDIO_BASE_SINK_CAST (element);
261 
262   switch (transition) {
263     case GST_STATE_CHANGE_READY_TO_PAUSED:
264       /* If we have pending volume/mute values to set, do here */
265       GST_OBJECT_LOCK (self);
266       if (asink->ringbuffer) {
267         GstWasapi2RingBuffer *ringbuffer =
268             GST_WASAPI2_RING_BUFFER (asink->ringbuffer);
269 
270         if (self->volume_changed) {
271           gst_wasapi2_ring_buffer_set_volume (ringbuffer, self->volume);
272           self->volume_changed = FALSE;
273         }
274 
275         if (self->mute_changed) {
276           gst_wasapi2_ring_buffer_set_mute (ringbuffer, self->mute);
277           self->mute_changed = FALSE;
278         }
279       }
280       GST_OBJECT_UNLOCK (self);
281       break;
282     default:
283       break;
284   }
285 
286   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
287 }
288 
289 static GstCaps *
gst_wasapi2_sink_get_caps(GstBaseSink * bsink,GstCaps * filter)290 gst_wasapi2_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
291 {
292   GstAudioBaseSink *asink = GST_AUDIO_BASE_SINK_CAST (bsink);
293   GstCaps *caps = NULL;
294 
295   GST_OBJECT_LOCK (bsink);
296   if (asink->ringbuffer) {
297     GstWasapi2RingBuffer *ringbuffer =
298         GST_WASAPI2_RING_BUFFER (asink->ringbuffer);
299 
300     gst_object_ref (ringbuffer);
301     GST_OBJECT_UNLOCK (bsink);
302 
303     /* Get caps might be able to block if device is not activated yet */
304     caps = gst_wasapi2_ring_buffer_get_caps (ringbuffer);
305     gst_object_unref (ringbuffer);
306   } else {
307     GST_OBJECT_UNLOCK (bsink);
308   }
309 
310   if (!caps)
311     caps = gst_pad_get_pad_template_caps (bsink->sinkpad);
312 
313   if (filter) {
314     GstCaps *filtered =
315         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
316     gst_caps_unref (caps);
317     caps = filtered;
318   }
319 
320   GST_DEBUG_OBJECT (bsink, "returning caps %" GST_PTR_FORMAT, caps);
321 
322   return caps;
323 }
324 
325 static GstAudioRingBuffer *
gst_wasapi2_sink_create_ringbuffer(GstAudioBaseSink * sink)326 gst_wasapi2_sink_create_ringbuffer (GstAudioBaseSink * sink)
327 {
328   GstWasapi2Sink *self = GST_WASAPI2_SINK (sink);
329   GstAudioRingBuffer *ringbuffer;
330   gchar *name;
331 
332   name = g_strdup_printf ("%s-ringbuffer", GST_OBJECT_NAME (sink));
333 
334   ringbuffer =
335       gst_wasapi2_ring_buffer_new (GST_WASAPI2_CLIENT_DEVICE_CLASS_RENDER,
336       self->low_latency, self->device_id, self->dispatcher, name);
337 
338   g_free (name);
339 
340   return ringbuffer;
341 }
342 
343 static void
gst_wasapi2_sink_set_mute(GstWasapi2Sink * self,gboolean mute)344 gst_wasapi2_sink_set_mute (GstWasapi2Sink * self, gboolean mute)
345 {
346   GstAudioBaseSink *bsink = GST_AUDIO_BASE_SINK_CAST (self);
347   HRESULT hr;
348 
349   GST_OBJECT_LOCK (self);
350 
351   self->mute = mute;
352   self->mute_changed = TRUE;
353 
354   if (bsink->ringbuffer) {
355     GstWasapi2RingBuffer *ringbuffer =
356         GST_WASAPI2_RING_BUFFER (bsink->ringbuffer);
357 
358     hr = gst_wasapi2_ring_buffer_set_mute (ringbuffer, mute);
359 
360     if (FAILED (hr)) {
361       GST_INFO_OBJECT (self, "Couldn't set mute");
362     } else {
363       self->mute_changed = FALSE;
364     }
365   }
366 
367   GST_OBJECT_UNLOCK (self);
368 }
369 
370 static gboolean
gst_wasapi2_sink_get_mute(GstWasapi2Sink * self)371 gst_wasapi2_sink_get_mute (GstWasapi2Sink * self)
372 {
373   GstAudioBaseSink *bsink = GST_AUDIO_BASE_SINK_CAST (self);
374   gboolean mute;
375   HRESULT hr;
376 
377   GST_OBJECT_LOCK (self);
378 
379   mute = self->mute;
380 
381   if (bsink->ringbuffer) {
382     GstWasapi2RingBuffer *ringbuffer =
383         GST_WASAPI2_RING_BUFFER (bsink->ringbuffer);
384 
385     hr = gst_wasapi2_ring_buffer_get_mute (ringbuffer, &mute);
386 
387     if (FAILED (hr)) {
388       GST_INFO_OBJECT (self, "Couldn't get mute");
389     } else {
390       self->mute = mute;
391     }
392   }
393 
394   GST_OBJECT_UNLOCK (self);
395 
396   return mute;
397 }
398 
399 static void
gst_wasapi2_sink_set_volume(GstWasapi2Sink * self,gdouble volume)400 gst_wasapi2_sink_set_volume (GstWasapi2Sink * self, gdouble volume)
401 {
402   GstAudioBaseSink *bsink = GST_AUDIO_BASE_SINK_CAST (self);
403   HRESULT hr;
404 
405   GST_OBJECT_LOCK (self);
406 
407   self->volume = volume;
408   /* clip volume value */
409   self->volume = MAX (0.0, self->volume);
410   self->volume = MIN (1.0, self->volume);
411   self->volume_changed = TRUE;
412 
413   if (bsink->ringbuffer) {
414     GstWasapi2RingBuffer *ringbuffer =
415         GST_WASAPI2_RING_BUFFER (bsink->ringbuffer);
416 
417     hr = gst_wasapi2_ring_buffer_set_volume (ringbuffer, (gfloat) self->volume);
418 
419     if (FAILED (hr)) {
420       GST_INFO_OBJECT (self, "Couldn't set volume");
421     } else {
422       self->volume_changed = FALSE;
423     }
424   }
425 
426   GST_OBJECT_UNLOCK (self);
427 }
428 
429 static gdouble
gst_wasapi2_sink_get_volume(GstWasapi2Sink * self)430 gst_wasapi2_sink_get_volume (GstWasapi2Sink * self)
431 {
432   GstAudioBaseSink *bsink = GST_AUDIO_BASE_SINK_CAST (self);
433   gfloat volume;
434   HRESULT hr;
435 
436   GST_OBJECT_LOCK (self);
437 
438   volume = (gfloat) self->volume;
439 
440   if (bsink->ringbuffer) {
441     GstWasapi2RingBuffer *ringbuffer =
442         GST_WASAPI2_RING_BUFFER (bsink->ringbuffer);
443 
444     hr = gst_wasapi2_ring_buffer_get_volume (ringbuffer, &volume);
445 
446     if (FAILED (hr)) {
447       GST_INFO_OBJECT (self, "Couldn't set volume");
448     } else {
449       self->volume = volume;
450     }
451   }
452 
453   GST_OBJECT_UNLOCK (self);
454 
455   volume = MAX (0.0, volume);
456   volume = MIN (1.0, volume);
457 
458   return volume;
459 }
460