1 /* GStreamer
2 * Copyright (C) 2018 Nirbheek Chauhan <nirbheek@centricular.com>
3 * Copyright (C) 2021 Seungha Yang <seungha@centricular.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "gstwasapidevice.h"
26
27 GST_DEBUG_CATEGORY_EXTERN (gst_wasapi_debug);
28 #define GST_CAT_DEFAULT gst_wasapi_debug
29
30 G_DEFINE_TYPE (GstWasapiDeviceProvider, gst_wasapi_device_provider,
31 GST_TYPE_DEVICE_PROVIDER);
32
33 static void gst_wasapi_device_provider_finalize (GObject * object);
34 static GList *gst_wasapi_device_provider_probe (GstDeviceProvider * provider);
35 static gboolean gst_wasapi_device_provider_start (GstDeviceProvider * provider);
36 static void gst_wasapi_device_provider_stop (GstDeviceProvider * provider);
37
38 static HRESULT
39 gst_wasapi_device_provider_device_added (GstMMDeviceEnumerator * enumerator,
40 LPCWSTR device_id, gpointer user_data);
41 static HRESULT
42 gst_wasapi_device_provider_device_removed (GstMMDeviceEnumerator * enumerator,
43 LPCWSTR device_id, gpointer user_data);
44 static HRESULT
45 gst_wasapi_device_provider_default_device_changed (GstMMDeviceEnumerator *
46 enumerator, EDataFlow flow, ERole role, LPCWSTR device_id,
47 gpointer user_data);
48
49 static void
gst_wasapi_device_provider_class_init(GstWasapiDeviceProviderClass * klass)50 gst_wasapi_device_provider_class_init (GstWasapiDeviceProviderClass * klass)
51 {
52 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
53 GstDeviceProviderClass *dm_class = GST_DEVICE_PROVIDER_CLASS (klass);
54
55 gobject_class->finalize = gst_wasapi_device_provider_finalize;
56
57 dm_class->probe = gst_wasapi_device_provider_probe;
58 dm_class->start = gst_wasapi_device_provider_start;
59 dm_class->stop = gst_wasapi_device_provider_stop;
60
61 gst_device_provider_class_set_static_metadata (dm_class,
62 "WASAPI (Windows Audio Session API) Device Provider",
63 "Source/Sink/Audio", "List WASAPI source and sink devices",
64 "Nirbheek Chauhan <nirbheek@centricular.com>");
65 }
66
67 static void
gst_wasapi_device_provider_init(GstWasapiDeviceProvider * self)68 gst_wasapi_device_provider_init (GstWasapiDeviceProvider * self)
69 {
70 self->enumerator = gst_mm_device_enumerator_new ();
71 }
72
73 static gboolean
gst_wasapi_device_provider_start(GstDeviceProvider * provider)74 gst_wasapi_device_provider_start (GstDeviceProvider * provider)
75 {
76 GstWasapiDeviceProvider *self = GST_WASAPI_DEVICE_PROVIDER (provider);
77 GstMMNotificationClientCallbacks callbacks = { NULL, };
78 GList *devices = NULL;
79 GList *iter;
80
81 if (!self->enumerator) {
82 GST_WARNING_OBJECT (self, "Enumerator wasn't configured");
83 return FALSE;
84 }
85
86 callbacks.device_added = gst_wasapi_device_provider_device_added;
87 callbacks.device_removed = gst_wasapi_device_provider_device_removed;
88 callbacks.default_device_changed =
89 gst_wasapi_device_provider_default_device_changed;
90
91 if (!gst_mm_device_enumerator_set_notification_callback (self->enumerator,
92 &callbacks, self)) {
93 GST_WARNING_OBJECT (self, "Failed to set callbacks");
94 return FALSE;
95 }
96
97 /* baseclass will not call probe() once it's started, but we can get
98 * notification only add/remove or change case. To this manually */
99 devices = gst_wasapi_device_provider_probe (provider);
100 if (devices) {
101 for (iter = devices; iter; iter = g_list_next (iter)) {
102 gst_device_provider_device_add (provider, GST_DEVICE (iter->data));
103 }
104
105 g_list_free (devices);
106 }
107
108 return TRUE;
109 }
110
111 static void
gst_wasapi_device_provider_stop(GstDeviceProvider * provider)112 gst_wasapi_device_provider_stop (GstDeviceProvider * provider)
113 {
114 GstWasapiDeviceProvider *self = GST_WASAPI_DEVICE_PROVIDER (provider);
115
116 if (self->enumerator) {
117 gst_mm_device_enumerator_set_notification_callback (self->enumerator,
118 NULL, NULL);
119 }
120 }
121
122 static void
gst_wasapi_device_provider_finalize(GObject * object)123 gst_wasapi_device_provider_finalize (GObject * object)
124 {
125 GstWasapiDeviceProvider *self = GST_WASAPI_DEVICE_PROVIDER (object);
126
127 gst_clear_object (&self->enumerator);
128
129 G_OBJECT_CLASS (gst_wasapi_device_provider_parent_class)->finalize (object);
130 }
131
132 static GList *
gst_wasapi_device_provider_probe(GstDeviceProvider * provider)133 gst_wasapi_device_provider_probe (GstDeviceProvider * provider)
134 {
135 GstWasapiDeviceProvider *self = GST_WASAPI_DEVICE_PROVIDER (provider);
136 GList *devices = NULL;
137
138 if (!gst_wasapi_util_get_devices (self->enumerator, TRUE, &devices))
139 GST_ERROR_OBJECT (self, "Failed to enumerate devices");
140
141 return devices;
142 }
143
144 static gboolean
gst_wasapi_device_is_in_list(GList * list,GstDevice * device)145 gst_wasapi_device_is_in_list (GList * list, GstDevice * device)
146 {
147 GList *iter;
148 GstStructure *s;
149 const gchar *device_id;
150 gboolean found = FALSE;
151
152 s = gst_device_get_properties (device);
153 g_assert (s);
154
155 device_id = gst_structure_get_string (s, "device.strid");
156 g_assert (device_id);
157
158 for (iter = list; iter; iter = g_list_next (iter)) {
159 GstStructure *other_s;
160 const gchar *other_id;
161
162 other_s = gst_device_get_properties (GST_DEVICE (iter->data));
163 g_assert (other_s);
164
165 other_id = gst_structure_get_string (other_s, "device.strid");
166 g_assert (other_id);
167
168 if (g_ascii_strcasecmp (device_id, other_id) == 0) {
169 found = TRUE;
170 }
171
172 gst_structure_free (other_s);
173 if (found)
174 break;
175 }
176
177 gst_structure_free (s);
178
179 return found;
180 }
181
182 static void
gst_wasapi_device_provider_update_devices(GstWasapiDeviceProvider * self)183 gst_wasapi_device_provider_update_devices (GstWasapiDeviceProvider * self)
184 {
185 GstDeviceProvider *provider = GST_DEVICE_PROVIDER_CAST (self);
186 GList *prev_devices = NULL;
187 GList *new_devices = NULL;
188 GList *to_add = NULL;
189 GList *to_remove = NULL;
190 GList *iter;
191
192 GST_OBJECT_LOCK (self);
193 prev_devices = g_list_copy_deep (provider->devices,
194 (GCopyFunc) gst_object_ref, NULL);
195 GST_OBJECT_UNLOCK (self);
196
197 new_devices = gst_wasapi_device_provider_probe (provider);
198
199 /* Ownership of GstDevice for gst_device_provider_device_add()
200 * and gst_device_provider_device_remove() is a bit complicated.
201 * Remove floating reference here for things to be clear */
202 for (iter = new_devices; iter; iter = g_list_next (iter))
203 gst_object_ref_sink (iter->data);
204
205 /* Check newly added devices */
206 for (iter = new_devices; iter; iter = g_list_next (iter)) {
207 if (!gst_wasapi_device_is_in_list (prev_devices, GST_DEVICE (iter->data))) {
208 to_add = g_list_prepend (to_add, gst_object_ref (iter->data));
209 }
210 }
211
212 /* Check removed device */
213 for (iter = prev_devices; iter; iter = g_list_next (iter)) {
214 if (!gst_wasapi_device_is_in_list (new_devices, GST_DEVICE (iter->data))) {
215 to_remove = g_list_prepend (to_remove, gst_object_ref (iter->data));
216 }
217 }
218
219 for (iter = to_remove; iter; iter = g_list_next (iter))
220 gst_device_provider_device_remove (provider, GST_DEVICE (iter->data));
221
222 for (iter = to_add; iter; iter = g_list_next (iter))
223 gst_device_provider_device_add (provider, GST_DEVICE (iter->data));
224
225 if (prev_devices)
226 g_list_free_full (prev_devices, (GDestroyNotify) gst_object_unref);
227
228 if (to_add)
229 g_list_free_full (to_add, (GDestroyNotify) gst_object_unref);
230
231 if (to_remove)
232 g_list_free_full (to_remove, (GDestroyNotify) gst_object_unref);
233 }
234
235 static HRESULT
gst_wasapi_device_provider_device_added(GstMMDeviceEnumerator * enumerator,LPCWSTR device_id,gpointer user_data)236 gst_wasapi_device_provider_device_added (GstMMDeviceEnumerator * enumerator,
237 LPCWSTR device_id, gpointer user_data)
238 {
239 GstWasapiDeviceProvider *self = GST_WASAPI_DEVICE_PROVIDER (user_data);
240
241 gst_wasapi_device_provider_update_devices (self);
242
243 return S_OK;
244 }
245
246 static HRESULT
gst_wasapi_device_provider_device_removed(GstMMDeviceEnumerator * enumerator,LPCWSTR device_id,gpointer user_data)247 gst_wasapi_device_provider_device_removed (GstMMDeviceEnumerator * enumerator,
248 LPCWSTR device_id, gpointer user_data)
249 {
250 GstWasapiDeviceProvider *self = GST_WASAPI_DEVICE_PROVIDER (user_data);
251
252 gst_wasapi_device_provider_update_devices (self);
253
254 return S_OK;
255 }
256
257 static HRESULT
gst_wasapi_device_provider_default_device_changed(GstMMDeviceEnumerator * enumerator,EDataFlow flow,ERole role,LPCWSTR device_id,gpointer user_data)258 gst_wasapi_device_provider_default_device_changed (GstMMDeviceEnumerator *
259 enumerator, EDataFlow flow, ERole role, LPCWSTR device_id,
260 gpointer user_data)
261 {
262 GstWasapiDeviceProvider *self = GST_WASAPI_DEVICE_PROVIDER (user_data);
263
264 gst_wasapi_device_provider_update_devices (self);
265
266 return S_OK;
267 }
268
269 /* GstWasapiDevice begins */
270
271 enum
272 {
273 PROP_DEVICE_STRID = 1,
274 };
275
276 G_DEFINE_TYPE (GstWasapiDevice, gst_wasapi_device, GST_TYPE_DEVICE);
277
278 static void gst_wasapi_device_get_property (GObject * object,
279 guint prop_id, GValue * value, GParamSpec * pspec);
280 static void gst_wasapi_device_set_property (GObject * object,
281 guint prop_id, const GValue * value, GParamSpec * pspec);
282 static void gst_wasapi_device_finalize (GObject * object);
283 static GstElement *gst_wasapi_device_create_element (GstDevice * device,
284 const gchar * name);
285
286 static void
gst_wasapi_device_class_init(GstWasapiDeviceClass * klass)287 gst_wasapi_device_class_init (GstWasapiDeviceClass * klass)
288 {
289 GstDeviceClass *dev_class = GST_DEVICE_CLASS (klass);
290 GObjectClass *object_class = G_OBJECT_CLASS (klass);
291
292 dev_class->create_element = gst_wasapi_device_create_element;
293
294 object_class->get_property = gst_wasapi_device_get_property;
295 object_class->set_property = gst_wasapi_device_set_property;
296 object_class->finalize = gst_wasapi_device_finalize;
297
298 g_object_class_install_property (object_class, PROP_DEVICE_STRID,
299 g_param_spec_string ("device", "Device string ID",
300 "Device strId", NULL,
301 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
302 }
303
304 static void
gst_wasapi_device_init(GstWasapiDevice * device)305 gst_wasapi_device_init (GstWasapiDevice * device)
306 {
307 }
308
309 static void
gst_wasapi_device_finalize(GObject * object)310 gst_wasapi_device_finalize (GObject * object)
311 {
312 GstWasapiDevice *device = GST_WASAPI_DEVICE (object);
313
314 g_free (device->strid);
315
316 G_OBJECT_CLASS (gst_wasapi_device_parent_class)->finalize (object);
317 }
318
319 static GstElement *
gst_wasapi_device_create_element(GstDevice * device,const gchar * name)320 gst_wasapi_device_create_element (GstDevice * device, const gchar * name)
321 {
322 GstWasapiDevice *wasapi_dev = GST_WASAPI_DEVICE (device);
323 GstElement *elem;
324
325 elem = gst_element_factory_make (wasapi_dev->element, name);
326
327 g_object_set (elem, "device", wasapi_dev->strid, NULL);
328
329 return elem;
330 }
331
332 static void
gst_wasapi_device_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)333 gst_wasapi_device_get_property (GObject * object, guint prop_id,
334 GValue * value, GParamSpec * pspec)
335 {
336 GstWasapiDevice *device = GST_WASAPI_DEVICE_CAST (object);
337
338 switch (prop_id) {
339 case PROP_DEVICE_STRID:
340 g_value_set_string (value, device->strid);
341 break;
342 default:
343 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
344 break;
345 }
346 }
347
348 static void
gst_wasapi_device_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)349 gst_wasapi_device_set_property (GObject * object, guint prop_id,
350 const GValue * value, GParamSpec * pspec)
351 {
352 GstWasapiDevice *device = GST_WASAPI_DEVICE_CAST (object);
353
354 switch (prop_id) {
355 case PROP_DEVICE_STRID:
356 device->strid = g_value_dup_string (value);
357 break;
358 default:
359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
360 break;
361 }
362 }
363