1 /* GStreamer
2 * Copyright (C) 2018 Sebastian Dröge <sebastian@centricular.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "gstdirectsounddevice.h"
25
26 #include <windows.h>
27 #include <dsound.h>
28 #include <mmsystem.h>
29 #include <stdio.h>
30
31 #ifdef GST_DIRECTSOUND_SRC_DEVICE_PROVIDER
32 #include "gstdirectsoundsrc.h"
33 #else
34 #include "gstdirectsoundsink.h"
35 #endif
36
37 G_DEFINE_TYPE (GstDirectSoundDeviceProvider, gst_directsound_device_provider,
38 GST_TYPE_DEVICE_PROVIDER);
39
40 static GList *gst_directsound_device_provider_probe (GstDeviceProvider *
41 provider);
42
43 static void
gst_directsound_device_provider_class_init(GstDirectSoundDeviceProviderClass * klass)44 gst_directsound_device_provider_class_init (GstDirectSoundDeviceProviderClass *
45 klass)
46 {
47 GstDeviceProviderClass *dm_class = GST_DEVICE_PROVIDER_CLASS (klass);
48
49 gst_device_provider_class_set_static_metadata (dm_class,
50 #ifdef GST_DIRECTSOUND_SRC_DEVICE_PROVIDER
51 "DirectSound Source Device Provider", "Source/Audio",
52 "List DirectSound source devices",
53 #else
54 "DirectSound Sink Device Provider", "Sink/Audio",
55 "List DirectSound sink devices",
56 #endif
57 "Sebastian Dröge <sebastian@centricular.com>");
58
59 dm_class->probe = gst_directsound_device_provider_probe;
60 }
61
62 static void
gst_directsound_device_provider_init(GstDirectSoundDeviceProvider * provider)63 gst_directsound_device_provider_init (GstDirectSoundDeviceProvider * provider)
64 {
65 }
66
67 static gchar *
guid_to_string(LPGUID guid)68 guid_to_string (LPGUID guid)
69 {
70 gunichar2 *wstr = NULL;
71 gchar *str = NULL;
72
73 if (StringFromCLSID (guid, &wstr) == S_OK) {
74 str = g_utf16_to_utf8 (wstr, -1, NULL, NULL, NULL);
75 CoTaskMemFree (wstr);
76 }
77
78 return str;
79 }
80
81 typedef struct
82 {
83 GstDirectSoundDeviceProvider *self;
84 GList **devices;
85 } ProbeData;
86
87 static BOOL CALLBACK
gst_directsound_enum_callback(GUID * pGUID,TCHAR * strDesc,TCHAR * strDrvName,VOID * pContext)88 gst_directsound_enum_callback (GUID * pGUID, TCHAR * strDesc,
89 TCHAR * strDrvName, VOID * pContext)
90 {
91 ProbeData *probe_data = (ProbeData *) (pContext);
92 gchar *driver, *description, *guid_str;
93 GstStructure *props;
94 GstDevice *device;
95 #ifdef GST_DIRECTSOUND_SRC_DEVICE_PROVIDER
96 static GstStaticCaps caps = GST_STATIC_CAPS (GST_DIRECTSOUND_SRC_CAPS);
97 #else
98 static GstStaticCaps caps = GST_STATIC_CAPS (GST_DIRECTSOUND_SINK_CAPS);
99 #endif
100
101 description = g_locale_to_utf8 (strDesc, -1, NULL, NULL, NULL);
102 if (!description) {
103 GST_ERROR_OBJECT (probe_data->self,
104 "Failed to convert description from locale encoding to UTF8");
105 return TRUE;
106 }
107
108 driver = g_locale_to_utf8 (strDrvName, -1, NULL, NULL, NULL);
109 if (!driver) {
110 GST_ERROR_OBJECT (probe_data->self,
111 "Failed to convert driver name from locale encoding to UTF8");
112 return TRUE;
113 }
114
115 /* NULL for the primary sound card */
116 guid_str = pGUID ? guid_to_string (pGUID) : NULL;
117
118 GST_INFO_OBJECT (probe_data->self, "sound device name: %s, %s (GUID %s)",
119 description, driver, GST_STR_NULL (guid_str));
120
121 props = gst_structure_new ("directsound-proplist",
122 "device.api", G_TYPE_STRING, "directsound",
123 "device.guid", G_TYPE_STRING, GST_STR_NULL (guid_str),
124 "directsound.device.driver", G_TYPE_STRING, driver,
125 "directsound.device.description", G_TYPE_STRING, description, NULL);
126
127 #ifdef GST_DIRECTSOUND_SRC_DEVICE_PROVIDER
128 device = g_object_new (GST_TYPE_DIRECTSOUND_DEVICE, "device-guid", guid_str,
129 "display-name", description, "caps", gst_static_caps_get (&caps),
130 "device-class", "Audio/Source", "properties", props, NULL);
131 #else
132 device = g_object_new (GST_TYPE_DIRECTSOUND_DEVICE, "device-guid", guid_str,
133 "display-name", description, "caps", gst_static_caps_get (&caps),
134 "device-class", "Audio/Sink", "properties", props, NULL);
135 #endif
136
137 *probe_data->devices = g_list_prepend (*probe_data->devices, device);
138
139 g_free (description);
140 g_free (driver);
141 g_free (guid_str);
142
143 gst_structure_free (props);
144
145 return TRUE;
146 }
147
148 static GList *
gst_directsound_device_provider_probe(GstDeviceProvider * provider)149 gst_directsound_device_provider_probe (GstDeviceProvider * provider)
150 {
151 GstDirectSoundDeviceProvider *self =
152 GST_DIRECTSOUND_DEVICE_PROVIDER (provider);
153 GList *devices = NULL;
154 ProbeData probe_data = { self, &devices };
155 HRESULT hRes;
156
157 #ifdef GST_DIRECTSOUND_SRC_DEVICE_PROVIDER
158 hRes = DirectSoundCaptureEnumerate ((LPDSENUMCALLBACK)
159 gst_directsound_enum_callback, (VOID *) & probe_data);
160 #else
161 hRes = DirectSoundEnumerate ((LPDSENUMCALLBACK)
162 gst_directsound_enum_callback, (VOID *) & probe_data);
163 #endif
164
165 if (FAILED (hRes))
166 GST_ERROR_OBJECT (self, "Failed to enumerate devices");
167
168 return devices;
169 }
170
171 enum
172 {
173 PROP_DEVICE_GUID = 1,
174 };
175
176 G_DEFINE_TYPE (GstDirectSoundDevice, gst_directsound_device, GST_TYPE_DEVICE);
177
178 static void gst_directsound_device_get_property (GObject * object,
179 guint prop_id, GValue * value, GParamSpec * pspec);
180 static void gst_directsound_device_set_property (GObject * object,
181 guint prop_id, const GValue * value, GParamSpec * pspec);
182 static void gst_directsound_device_finalize (GObject * object);
183 static GstElement *gst_directsound_device_create_element (GstDevice * device,
184 const gchar * name);
185
186 static void
gst_directsound_device_class_init(GstDirectSoundDeviceClass * klass)187 gst_directsound_device_class_init (GstDirectSoundDeviceClass * klass)
188 {
189 GstDeviceClass *dev_class = GST_DEVICE_CLASS (klass);
190 GObjectClass *object_class = G_OBJECT_CLASS (klass);
191
192 dev_class->create_element = gst_directsound_device_create_element;
193
194 object_class->get_property = gst_directsound_device_get_property;
195 object_class->set_property = gst_directsound_device_set_property;
196 object_class->finalize = gst_directsound_device_finalize;
197
198 g_object_class_install_property (object_class, PROP_DEVICE_GUID,
199 g_param_spec_string ("device-guid", "Device GUID",
200 "Device GUID", NULL,
201 G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
202 }
203
204 static void
gst_directsound_device_init(GstDirectSoundDevice * device)205 gst_directsound_device_init (GstDirectSoundDevice * device)
206 {
207 }
208
209 static void
gst_directsound_device_finalize(GObject * object)210 gst_directsound_device_finalize (GObject * object)
211 {
212 GstDirectSoundDevice *device = GST_DIRECTSOUND_DEVICE (object);
213
214 g_free (device->guid);
215
216 G_OBJECT_CLASS (gst_directsound_device_parent_class)->finalize (object);
217 }
218
219 static GstElement *
gst_directsound_device_create_element(GstDevice * device,const gchar * name)220 gst_directsound_device_create_element (GstDevice * device, const gchar * name)
221 {
222 GstDirectSoundDevice *directsound_dev = GST_DIRECTSOUND_DEVICE (device);
223 GstElement *elem;
224
225 #ifdef GST_DIRECTSOUND_SRC_DEVICE_PROVIDER
226 elem = gst_element_factory_make ("directsoundsrc", name);
227 #else
228 elem = gst_element_factory_make ("directsoundsink", name);
229 #endif
230
231 g_object_set (elem, "device", directsound_dev->guid, NULL);
232
233 return elem;
234 }
235
236 static void
gst_directsound_device_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)237 gst_directsound_device_get_property (GObject * object, guint prop_id,
238 GValue * value, GParamSpec * pspec)
239 {
240 GstDirectSoundDevice *device = GST_DIRECTSOUND_DEVICE_CAST (object);
241
242 switch (prop_id) {
243 case PROP_DEVICE_GUID:
244 g_value_set_string (value, device->guid);
245 break;
246 default:
247 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
248 break;
249 }
250 }
251
252 static void
gst_directsound_device_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)253 gst_directsound_device_set_property (GObject * object, guint prop_id,
254 const GValue * value, GParamSpec * pspec)
255 {
256 GstDirectSoundDevice *device = GST_DIRECTSOUND_DEVICE_CAST (object);
257
258 switch (prop_id) {
259 case PROP_DEVICE_GUID:
260 device->guid = g_value_dup_string (value);
261 break;
262 default:
263 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
264 break;
265 }
266 }
267