summaryrefslogtreecommitdiff
path: root/sys/wasapi/gstwasapidevice.c
blob: c13fc6a8aaf74605466c43a454f03f9801a80e04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* GStreamer
 * Copyright (C) 2018 Nirbheek Chauhan <nirbheek@centricular.com>
 * Copyright (C) 2021 Seungha Yang <seungha@centricular.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "gstwasapidevice.h"

GST_DEBUG_CATEGORY_EXTERN (gst_wasapi_debug);
#define GST_CAT_DEFAULT gst_wasapi_debug

G_DEFINE_TYPE (GstWasapiDeviceProvider, gst_wasapi_device_provider,
    GST_TYPE_DEVICE_PROVIDER);

static void gst_wasapi_device_provider_finalize (GObject * object);
static GList *gst_wasapi_device_provider_probe (GstDeviceProvider * provider);
static gboolean gst_wasapi_device_provider_start (GstDeviceProvider * provider);
static void gst_wasapi_device_provider_stop (GstDeviceProvider * provider);

static HRESULT
gst_wasapi_device_provider_device_added (GstMMDeviceEnumerator * enumerator,
    LPCWSTR device_id, gpointer user_data);
static HRESULT
gst_wasapi_device_provider_device_removed (GstMMDeviceEnumerator * enumerator,
    LPCWSTR device_id, gpointer user_data);
static HRESULT
gst_wasapi_device_provider_default_device_changed (GstMMDeviceEnumerator *
    enumerator, EDataFlow flow, ERole role, LPCWSTR device_id,
    gpointer user_data);

static void
gst_wasapi_device_provider_class_init (GstWasapiDeviceProviderClass * klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GstDeviceProviderClass *dm_class = GST_DEVICE_PROVIDER_CLASS (klass);

  gobject_class->finalize = gst_wasapi_device_provider_finalize;

  dm_class->probe = gst_wasapi_device_provider_probe;
  dm_class->start = gst_wasapi_device_provider_start;
  dm_class->stop = gst_wasapi_device_provider_stop;

  gst_device_provider_class_set_static_metadata (dm_class,
      "WASAPI (Windows Audio Session API) Device Provider",
      "Source/Sink/Audio", "List WASAPI source and sink devices",
      "Nirbheek Chauhan <nirbheek@centricular.com>");
}

static void
gst_wasapi_device_provider_init (GstWasapiDeviceProvider * self)
{
  self->enumerator = gst_mm_device_enumerator_new ();
}

static gboolean
gst_wasapi_device_provider_start (GstDeviceProvider * provider)
{
  GstWasapiDeviceProvider *self = GST_WASAPI_DEVICE_PROVIDER (provider);
  GstMMNotificationClientCallbacks callbacks = { NULL, };
  GList *devices = NULL;
  GList *iter;

  if (!self->enumerator) {
    GST_WARNING_OBJECT (self, "Enumerator wasn't configured");
    return FALSE;
  }

  callbacks.device_added = gst_wasapi_device_provider_device_added;
  callbacks.device_removed = gst_wasapi_device_provider_device_removed;
  callbacks.default_device_changed =
      gst_wasapi_device_provider_default_device_changed;

  if (!gst_mm_device_enumerator_set_notification_callback (self->enumerator,
          &callbacks, self)) {
    GST_WARNING_OBJECT (self, "Failed to set callbacks");
    return FALSE;
  }

  /* baseclass will not call probe() once it's started, but we can get
   * notification only add/remove or change case. To this manually */
  devices = gst_wasapi_device_provider_probe (provider);
  if (devices) {
    for (iter = devices; iter; iter = g_list_next (iter)) {
      gst_device_provider_device_add (provider, GST_DEVICE (iter->data));
    }

    g_list_free (devices);
  }

  return TRUE;
}

static void
gst_wasapi_device_provider_stop (GstDeviceProvider * provider)
{
  GstWasapiDeviceProvider *self = GST_WASAPI_DEVICE_PROVIDER (provider);

  if (self->enumerator) {
    gst_mm_device_enumerator_set_notification_callback (self->enumerator,
        NULL, NULL);
  }
}

static void
gst_wasapi_device_provider_finalize (GObject * object)
{
  GstWasapiDeviceProvider *self = GST_WASAPI_DEVICE_PROVIDER (object);

  gst_clear_object (&self->enumerator);

  G_OBJECT_CLASS (gst_wasapi_device_provider_parent_class)->finalize (object);
}

static GList *
gst_wasapi_device_provider_probe (GstDeviceProvider * provider)
{
  GstWasapiDeviceProvider *self = GST_WASAPI_DEVICE_PROVIDER (provider);
  GList *devices = NULL;

  if (!gst_wasapi_util_get_devices (self->enumerator, TRUE, &devices))
    GST_ERROR_OBJECT (self, "Failed to enumerate devices");

  return devices;
}

static gboolean
gst_wasapi_device_is_in_list (GList * list, GstDevice * device)
{
  GList *iter;
  GstStructure *s;
  const gchar *device_id;
  gboolean found = FALSE;

  s = gst_device_get_properties (device);
  g_assert (s);

  device_id = gst_structure_get_string (s, "device.strid");
  g_assert (device_id);

  for (iter = list; iter; iter = g_list_next (iter)) {
    GstStructure *other_s;
    const gchar *other_id;

    other_s = gst_device_get_properties (GST_DEVICE (iter->data));
    g_assert (other_s);

    other_id = gst_structure_get_string (other_s, "device.strid");
    g_assert (other_id);

    if (g_ascii_strcasecmp (device_id, other_id) == 0) {
      found = TRUE;
    }

    gst_structure_free (other_s);
    if (found)
      break;
  }

  gst_structure_free (s);

  return found;
}

static void
gst_wasapi_device_provider_update_devices (GstWasapiDeviceProvider * self)
{
  GstDeviceProvider *provider = GST_DEVICE_PROVIDER_CAST (self);
  GList *prev_devices = NULL;
  GList *new_devices = NULL;
  GList *to_add = NULL;
  GList *to_remove = NULL;
  GList *iter;

  GST_OBJECT_LOCK (self);
  prev_devices = g_list_copy_deep (provider->devices,
      (GCopyFunc) gst_object_ref, NULL);
  GST_OBJECT_UNLOCK (self);

  new_devices = gst_wasapi_device_provider_probe (provider);

  /* Ownership of GstDevice for gst_device_provider_device_add()
   * and gst_device_provider_device_remove() is a bit complicated.
   * Remove floating reference here for things to be clear */
  for (iter = new_devices; iter; iter = g_list_next (iter))
    gst_object_ref_sink (iter->data);

  /* Check newly added devices */
  for (iter = new_devices; iter; iter = g_list_next (iter)) {
    if (!gst_wasapi_device_is_in_list (prev_devices, GST_DEVICE (iter->data))) {
      to_add = g_list_prepend (to_add, gst_object_ref (iter->data));
    }
  }

  /* Check removed device */
  for (iter = prev_devices; iter; iter = g_list_next (iter)) {
    if (!gst_wasapi_device_is_in_list (new_devices, GST_DEVICE (iter->data))) {
      to_remove = g_list_prepend (to_remove, gst_object_ref (iter->data));
    }
  }

  for (iter = to_remove; iter; iter = g_list_next (iter))
    gst_device_provider_device_remove (provider, GST_DEVICE (iter->data));

  for (iter = to_add; iter; iter = g_list_next (iter))
    gst_device_provider_device_add (provider, GST_DEVICE (iter->data));

  if (prev_devices)
    g_list_free_full (prev_devices, (GDestroyNotify) gst_object_unref);

  if (to_add)
    g_list_free_full (to_add, (GDestroyNotify) gst_object_unref);

  if (to_remove)
    g_list_free_full (to_remove, (GDestroyNotify) gst_object_unref);
}

static HRESULT
gst_wasapi_device_provider_device_added (GstMMDeviceEnumerator * enumerator,
    LPCWSTR device_id, gpointer user_data)
{
  GstWasapiDeviceProvider *self = GST_WASAPI_DEVICE_PROVIDER (user_data);

  gst_wasapi_device_provider_update_devices (self);

  return S_OK;
}

static HRESULT
gst_wasapi_device_provider_device_removed (GstMMDeviceEnumerator * enumerator,
    LPCWSTR device_id, gpointer user_data)
{
  GstWasapiDeviceProvider *self = GST_WASAPI_DEVICE_PROVIDER (user_data);

  gst_wasapi_device_provider_update_devices (self);

  return S_OK;
}

static HRESULT
gst_wasapi_device_provider_default_device_changed (GstMMDeviceEnumerator *
    enumerator, EDataFlow flow, ERole role, LPCWSTR device_id,
    gpointer user_data)
{
  GstWasapiDeviceProvider *self = GST_WASAPI_DEVICE_PROVIDER (user_data);

  gst_wasapi_device_provider_update_devices (self);

  return S_OK;
}

/* GstWasapiDevice begins */

enum
{
  PROP_DEVICE_STRID = 1,
};

G_DEFINE_TYPE (GstWasapiDevice, gst_wasapi_device, GST_TYPE_DEVICE);

static void gst_wasapi_device_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec);
static void gst_wasapi_device_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec);
static void gst_wasapi_device_finalize (GObject * object);
static GstElement *gst_wasapi_device_create_element (GstDevice * device,
    const gchar * name);

static void
gst_wasapi_device_class_init (GstWasapiDeviceClass * klass)
{
  GstDeviceClass *dev_class = GST_DEVICE_CLASS (klass);
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  dev_class->create_element = gst_wasapi_device_create_element;

  object_class->get_property = gst_wasapi_device_get_property;
  object_class->set_property = gst_wasapi_device_set_property;
  object_class->finalize = gst_wasapi_device_finalize;

  g_object_class_install_property (object_class, PROP_DEVICE_STRID,
      g_param_spec_string ("device", "Device string ID",
          "Device strId", NULL,
          G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}

static void
gst_wasapi_device_init (GstWasapiDevice * device)
{
}

static void
gst_wasapi_device_finalize (GObject * object)
{
  GstWasapiDevice *device = GST_WASAPI_DEVICE (object);

  g_free (device->strid);

  G_OBJECT_CLASS (gst_wasapi_device_parent_class)->finalize (object);
}

static GstElement *
gst_wasapi_device_create_element (GstDevice * device, const gchar * name)
{
  GstWasapiDevice *wasapi_dev = GST_WASAPI_DEVICE (device);
  GstElement *elem;

  elem = gst_element_factory_make (wasapi_dev->element, name);

  g_object_set (elem, "device", wasapi_dev->strid, NULL);

  return elem;
}

static void
gst_wasapi_device_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstWasapiDevice *device = GST_WASAPI_DEVICE_CAST (object);

  switch (prop_id) {
    case PROP_DEVICE_STRID:
      g_value_set_string (value, device->strid);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_wasapi_device_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstWasapiDevice *device = GST_WASAPI_DEVICE_CAST (object);

  switch (prop_id) {
    case PROP_DEVICE_STRID:
      device->strid = g_value_dup_string (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}