summaryrefslogtreecommitdiff
path: root/subprojects/gst-plugins-bad/sys/directsound/gstdirectsounddevice.c
blob: 00b021ebb190851e7965ac3d02d5e867187a953c (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
/* GStreamer
 * Copyright (C) 2018 Sebastian Dröge <sebastian@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 "gstdirectsounddevice.h"

#include <windows.h>
#include <dsound.h>
#include <mmsystem.h>
#include <stdio.h>

#ifdef GST_DIRECTSOUND_SRC_DEVICE_PROVIDER
#include "gstdirectsoundsrc.h"
#else
#include "gstdirectsoundsink.h"
#endif

G_DEFINE_TYPE (GstDirectSoundDeviceProvider, gst_directsound_device_provider,
    GST_TYPE_DEVICE_PROVIDER);

static GList *gst_directsound_device_provider_probe (GstDeviceProvider *
    provider);

static void
gst_directsound_device_provider_class_init (GstDirectSoundDeviceProviderClass *
    klass)
{
  GstDeviceProviderClass *dm_class = GST_DEVICE_PROVIDER_CLASS (klass);

  gst_device_provider_class_set_static_metadata (dm_class,
#ifdef GST_DIRECTSOUND_SRC_DEVICE_PROVIDER
      "DirectSound Source Device Provider", "Source/Audio",
      "List DirectSound source devices",
#else
      "DirectSound Sink Device Provider", "Sink/Audio",
      "List DirectSound sink devices",
#endif
      "Sebastian Dröge <sebastian@centricular.com>");

  dm_class->probe = gst_directsound_device_provider_probe;
}

static void
gst_directsound_device_provider_init (GstDirectSoundDeviceProvider * provider)
{
}

static gchar *
guid_to_string (LPGUID guid)
{
  gunichar2 *wstr = NULL;
  gchar *str = NULL;

  if (StringFromCLSID (guid, &wstr) == S_OK) {
    str = g_utf16_to_utf8 (wstr, -1, NULL, NULL, NULL);
    CoTaskMemFree (wstr);
  }

  return str;
}

typedef struct
{
  GstDirectSoundDeviceProvider *self;
  GList **devices;
} ProbeData;

static BOOL CALLBACK
gst_directsound_enum_callback (GUID * pGUID, TCHAR * strDesc,
    TCHAR * strDrvName, VOID * pContext)
{
  ProbeData *probe_data = (ProbeData *) (pContext);
  gchar *driver, *description, *guid_str;
  GstStructure *props;
  GstDevice *device;
#ifdef GST_DIRECTSOUND_SRC_DEVICE_PROVIDER
  static GstStaticCaps caps = GST_STATIC_CAPS (GST_DIRECTSOUND_SRC_CAPS);
#else
  static GstStaticCaps caps = GST_STATIC_CAPS (GST_DIRECTSOUND_SINK_CAPS);
#endif

  description = g_locale_to_utf8 (strDesc, -1, NULL, NULL, NULL);
  if (!description) {
    GST_ERROR_OBJECT (probe_data->self,
        "Failed to convert description from locale encoding to UTF8");
    return TRUE;
  }

  driver = g_locale_to_utf8 (strDrvName, -1, NULL, NULL, NULL);
  if (!driver) {
    GST_ERROR_OBJECT (probe_data->self,
        "Failed to convert driver name from locale encoding to UTF8");
    return TRUE;
  }

  /* NULL for the primary sound card */
  guid_str = pGUID ? guid_to_string (pGUID) : NULL;

  GST_INFO_OBJECT (probe_data->self, "sound device name: %s, %s (GUID %s)",
      description, driver, GST_STR_NULL (guid_str));

  props = gst_structure_new ("directsound-proplist",
      "device.api", G_TYPE_STRING, "directsound",
      "device.guid", G_TYPE_STRING, GST_STR_NULL (guid_str),
      "directsound.device.driver", G_TYPE_STRING, driver,
      "directsound.device.description", G_TYPE_STRING, description, NULL);

#ifdef GST_DIRECTSOUND_SRC_DEVICE_PROVIDER
  device = g_object_new (GST_TYPE_DIRECTSOUND_DEVICE, "device-guid", guid_str,
      "display-name", description, "caps", gst_static_caps_get (&caps),
      "device-class", "Audio/Source", "properties", props, NULL);
#else
  device = g_object_new (GST_TYPE_DIRECTSOUND_DEVICE, "device-guid", guid_str,
      "display-name", description, "caps", gst_static_caps_get (&caps),
      "device-class", "Audio/Sink", "properties", props, NULL);
#endif

  *probe_data->devices = g_list_prepend (*probe_data->devices, device);

  g_free (description);
  g_free (driver);
  g_free (guid_str);

  gst_structure_free (props);

  return TRUE;
}

static GList *
gst_directsound_device_provider_probe (GstDeviceProvider * provider)
{
  GstDirectSoundDeviceProvider *self =
      GST_DIRECTSOUND_DEVICE_PROVIDER (provider);
  GList *devices = NULL;
  ProbeData probe_data = { self, &devices };
  HRESULT hRes;

#ifdef GST_DIRECTSOUND_SRC_DEVICE_PROVIDER
  hRes = DirectSoundCaptureEnumerate ((LPDSENUMCALLBACK)
      gst_directsound_enum_callback, (VOID *) & probe_data);
#else
  hRes = DirectSoundEnumerate ((LPDSENUMCALLBACK)
      gst_directsound_enum_callback, (VOID *) & probe_data);
#endif

  if (FAILED (hRes))
    GST_ERROR_OBJECT (self, "Failed to enumerate devices");

  return devices;
}

enum
{
  PROP_DEVICE_GUID = 1,
};

G_DEFINE_TYPE (GstDirectSoundDevice, gst_directsound_device, GST_TYPE_DEVICE);

static void gst_directsound_device_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec);
static void gst_directsound_device_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec);
static void gst_directsound_device_finalize (GObject * object);
static GstElement *gst_directsound_device_create_element (GstDevice * device,
    const gchar * name);

static void
gst_directsound_device_class_init (GstDirectSoundDeviceClass * klass)
{
  GstDeviceClass *dev_class = GST_DEVICE_CLASS (klass);
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  dev_class->create_element = gst_directsound_device_create_element;

  object_class->get_property = gst_directsound_device_get_property;
  object_class->set_property = gst_directsound_device_set_property;
  object_class->finalize = gst_directsound_device_finalize;

  g_object_class_install_property (object_class, PROP_DEVICE_GUID,
      g_param_spec_string ("device-guid", "Device GUID",
          "Device GUID", NULL,
          G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}

static void
gst_directsound_device_init (GstDirectSoundDevice * device)
{
}

static void
gst_directsound_device_finalize (GObject * object)
{
  GstDirectSoundDevice *device = GST_DIRECTSOUND_DEVICE (object);

  g_free (device->guid);

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

static GstElement *
gst_directsound_device_create_element (GstDevice * device, const gchar * name)
{
  GstDirectSoundDevice *directsound_dev = GST_DIRECTSOUND_DEVICE (device);
  GstElement *elem;

#ifdef GST_DIRECTSOUND_SRC_DEVICE_PROVIDER
  elem = gst_element_factory_make ("directsoundsrc", name);
#else
  elem = gst_element_factory_make ("directsoundsink", name);
#endif

  g_object_set (elem, "device", directsound_dev->guid, NULL);

  return elem;
}

static void
gst_directsound_device_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstDirectSoundDevice *device = GST_DIRECTSOUND_DEVICE_CAST (object);

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

static void
gst_directsound_device_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstDirectSoundDevice *device = GST_DIRECTSOUND_DEVICE_CAST (object);

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