1 /* GStreamer
2 * Copyright (C) 2021 Seungha Yang <seungha@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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "gstwin32devicewatcher.h"
25 #include <dbt.h>
26
27 GST_DEBUG_CATEGORY_STATIC (gst_win32_device_watcher_debug);
28 #define GST_CAT_DEFAULT gst_win32_device_watcher_debug
29
30 G_LOCK_DEFINE_STATIC (create_lock);
31
32 #define GST_WIN32_HWND_PROP_NAME "gst-win32-device-watcher"
33
34 struct _GstWin32DeviceWatcher
35 {
36 GstObject parent;
37
38 GMutex lock;
39 GCond cond;
40
41 GThread *thread;
42 GMainContext *context;
43 GMainLoop *loop;
44
45 GstWin32DeviceWatcherCallbacks callbacks;
46 gpointer user_data;
47
48 HDEVNOTIFY device_notify;
49 HWND hwnd;
50 DWORD device_type;
51 GUID class_guid;
52 };
53
54 #define gst_win32_device_watcher_parent_class parent_class
55 G_DEFINE_TYPE (GstWin32DeviceWatcher, gst_win32_device_watcher,
56 GST_TYPE_OBJECT);
57
58 static void gst_win32_device_watcher_constructed (GObject * object);
59 static void gst_win32_device_watcher_finalize (GObject * object);
60
61 static gpointer
62 gst_win32_device_watcher_thread_func (GstWin32DeviceWatcher * self);
63
64 static void
gst_win32_device_watcher_class_init(GstWin32DeviceWatcherClass * klass)65 gst_win32_device_watcher_class_init (GstWin32DeviceWatcherClass * klass)
66 {
67 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
68
69 gobject_class->constructed = gst_win32_device_watcher_constructed;
70 gobject_class->finalize = gst_win32_device_watcher_finalize;
71
72 GST_DEBUG_CATEGORY_INIT (gst_win32_device_watcher_debug,
73 "win32devicewatcher", 0, "win32devicewatcher");
74 }
75
76 static void
gst_win32_device_watcher_init(GstWin32DeviceWatcher * self)77 gst_win32_device_watcher_init (GstWin32DeviceWatcher * self)
78 {
79 g_mutex_init (&self->lock);
80 g_cond_init (&self->cond);
81 self->context = g_main_context_new ();
82 self->loop = g_main_loop_new (self->context, FALSE);
83 }
84
85 static void
gst_win32_device_watcher_constructed(GObject * object)86 gst_win32_device_watcher_constructed (GObject * object)
87 {
88 GstWin32DeviceWatcher *self = GST_WIN32_DEVICE_WATCHER (object);
89
90 /* Create a new thread for WIN32 message pumping */
91 g_mutex_lock (&self->lock);
92 self->thread = g_thread_new ("GstWin32DeviceWatcher",
93 (GThreadFunc) gst_win32_device_watcher_thread_func, self);
94 while (!g_main_loop_is_running (self->loop))
95 g_cond_wait (&self->cond, &self->lock);
96 g_mutex_unlock (&self->lock);
97 }
98
99 static void
gst_win32_device_watcher_finalize(GObject * object)100 gst_win32_device_watcher_finalize (GObject * object)
101 {
102 GstWin32DeviceWatcher *self = GST_WIN32_DEVICE_WATCHER (object);
103
104 g_main_loop_quit (self->loop);
105 if (g_thread_self () != self->thread) {
106 g_thread_join (self->thread);
107 g_main_loop_unref (self->loop);
108 g_main_context_unref (self->context);
109 } else {
110 g_warning ("Trying join from self-thread");
111 }
112
113 g_mutex_clear (&self->lock);
114 g_cond_clear (&self->cond);
115
116 G_OBJECT_CLASS (parent_class)->finalize (object);
117 }
118
119 static LRESULT CALLBACK
window_proc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)120 window_proc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
121 {
122 GstWin32DeviceWatcher * self;
123
124 switch (msg) {
125 case WM_CREATE:
126 self = (GstWin32DeviceWatcher *)
127 ((LPCREATESTRUCT) lparam)->lpCreateParams;
128
129 GST_DEBUG_OBJECT (self, "WM_CREATE");
130
131 SetPropA (hwnd, GST_WIN32_HWND_PROP_NAME, self);
132 break;
133 case WM_DEVICECHANGE:
134 {
135 self = (GstWin32DeviceWatcher *) GetPropA
136 (hwnd, GST_WIN32_HWND_PROP_NAME);
137 if (!self) {
138 GST_WARNING ("Failed to get watcher object");
139 break;
140 }
141
142 self->callbacks.device_changed (self, wparam, lparam, self->user_data);
143 break;
144 }
145 default:
146 break;
147 }
148
149 return DefWindowProc (hwnd, msg, wparam, lparam);
150 }
151
152 static HWND
create_hwnd(GstWin32DeviceWatcher * self)153 create_hwnd (GstWin32DeviceWatcher * self)
154 {
155 WNDCLASSEXA wc;
156 ATOM atom = 0;
157 HINSTANCE hinstance = GetModuleHandle (NULL);
158 static const gchar *klass_name = "GstWin32DeviceWatcher";
159 HWND hwnd;
160
161 G_LOCK (create_lock);
162 atom = GetClassInfoExA (hinstance, klass_name, &wc);
163 if (atom == 0) {
164 GST_LOG_OBJECT (self, "Register window class");
165 ZeroMemory (&wc, sizeof (WNDCLASSEX));
166
167 wc.cbSize = sizeof (WNDCLASSEXA);
168 wc.lpfnWndProc = window_proc;
169 wc.hInstance = hinstance;
170 wc.lpszClassName = klass_name;
171 atom = RegisterClassExA (&wc);
172
173 if (atom == 0) {
174 G_UNLOCK (create_lock);
175 GST_ERROR_OBJECT (self, "Failed to register window class, lastError 0x%x",
176 (guint) GetLastError ());
177 return NULL;
178 }
179 } else {
180 GST_LOG_OBJECT (self, "window class was already registered");
181 }
182 G_UNLOCK (create_lock);
183
184 hwnd = CreateWindowExA (0, klass_name, "", 0, 0, 0, 0, 0,
185 HWND_MESSAGE, NULL, hinstance, self);
186 if (!hwnd) {
187 GST_ERROR_OBJECT (self, "Failed to create window handle, lastError 0x%x",
188 (guint) GetLastError ());
189 return nullptr;
190 }
191
192 return hwnd;
193 }
194
195 static gboolean
loop_running_cb(GstWin32DeviceWatcher * self)196 loop_running_cb (GstWin32DeviceWatcher * self)
197 {
198 g_mutex_lock (&self->lock);
199 g_cond_signal (&self->cond);
200 g_mutex_unlock (&self->lock);
201
202 return G_SOURCE_REMOVE;
203 }
204
205 static gboolean
win32_msg_cb(GIOChannel * source,GIOCondition condition,gpointer data)206 win32_msg_cb (GIOChannel * source, GIOCondition condition, gpointer data)
207 {
208 MSG msg;
209
210 if (!PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
211 return G_SOURCE_CONTINUE;
212
213 TranslateMessage (&msg);
214 DispatchMessage (&msg);
215
216 return G_SOURCE_CONTINUE;
217 }
218
219 static gpointer
gst_win32_device_watcher_thread_func(GstWin32DeviceWatcher * self)220 gst_win32_device_watcher_thread_func (GstWin32DeviceWatcher * self)
221 {
222 GSource *idle_source;
223 HWND hwnd = nullptr;
224 GIOChannel *msg_io_channel = nullptr;
225 GSource *msg_source = nullptr;
226
227 g_main_context_push_thread_default (self->context);
228
229 idle_source = g_idle_source_new ();
230 g_source_set_callback (idle_source,
231 (GSourceFunc) loop_running_cb, self, nullptr);
232 g_source_attach (idle_source, self->context);
233 g_source_unref (idle_source);
234
235 hwnd = create_hwnd (self);
236 if (!hwnd)
237 goto run_loop;
238
239 msg_io_channel = g_io_channel_win32_new_messages ((gsize) hwnd);
240 msg_source = g_io_create_watch (msg_io_channel, G_IO_IN);
241 g_source_set_callback (msg_source,
242 (GSourceFunc) win32_msg_cb, nullptr, nullptr);
243 g_source_attach (msg_source, self->context);
244
245 self->hwnd = hwnd;
246
247 run_loop:
248 GST_INFO_OBJECT (self, "Starting loop");
249 g_main_loop_run (self->loop);
250 GST_INFO_OBJECT (self, "Stopped loop");
251
252 if (self->device_notify) {
253 UnregisterDeviceNotification (self->device_notify);
254 self->device_notify = nullptr;
255 }
256
257 if (msg_source) {
258 g_source_destroy (msg_source);
259 g_source_unref (msg_source);
260 }
261
262 if (msg_io_channel)
263 g_io_channel_unref (msg_io_channel);
264
265 if (hwnd)
266 DestroyWindow (hwnd);
267
268 g_main_context_pop_thread_default (self->context);
269
270 return nullptr;
271 }
272
273 GstWin32DeviceWatcher *
gst_win32_device_watcher_new(DWORD device_type,const GUID * class_guid,const GstWin32DeviceWatcherCallbacks * callbacks,gpointer user_data)274 gst_win32_device_watcher_new (DWORD device_type, const GUID * class_guid,
275 const GstWin32DeviceWatcherCallbacks * callbacks, gpointer user_data)
276 {
277 GstWin32DeviceWatcher *self;
278
279 g_return_val_if_fail (class_guid != nullptr, nullptr);
280 g_return_val_if_fail (callbacks != nullptr, nullptr);
281
282 self = (GstWin32DeviceWatcher *)
283 g_object_new (GST_TYPE_WIN32_DEVICE_WATCHER, nullptr);
284
285 if (!self->hwnd) {
286 gst_object_unref (self);
287 return nullptr;
288 }
289
290 self->callbacks = *callbacks;
291 self->user_data = user_data;
292 self->device_type = device_type;
293 self->class_guid = *class_guid;
294
295 gst_object_ref_sink (self);
296
297 return self;
298 }
299
300 typedef struct
301 {
302 GstWin32DeviceWatcher * self;
303
304 gboolean handled;
305 gboolean ret;
306 } DeviceNotificationData;
307
308 static gboolean
register_device_notification(DeviceNotificationData * data)309 register_device_notification (DeviceNotificationData * data)
310 {
311 GstWin32DeviceWatcher * self = data->self;
312 DEV_BROADCAST_DEVICEINTERFACE di = { 0, };
313
314 if (self->device_notify)
315 goto out;
316
317 di.dbcc_size = sizeof (di);
318 di.dbcc_devicetype = self->device_type;
319 di.dbcc_classguid = self->class_guid;
320
321 self->device_notify = RegisterDeviceNotificationW (self->hwnd,
322 &di, DEVICE_NOTIFY_WINDOW_HANDLE);
323
324 out:
325 if (self->device_notify)
326 data->ret = TRUE;
327
328 g_mutex_lock (&self->lock);
329 data->handled = TRUE;
330 g_cond_broadcast (&self->cond);
331 g_mutex_unlock (&self->lock);
332
333 return G_SOURCE_REMOVE;
334 }
335
336 gboolean
gst_win32_device_watcher_start(GstWin32DeviceWatcher * watcher)337 gst_win32_device_watcher_start (GstWin32DeviceWatcher * watcher)
338 {
339 DeviceNotificationData data;
340
341 g_return_val_if_fail (GST_IS_WIN32_DEVICE_WATCHER (watcher), FALSE);
342
343 data.self = watcher;
344 data.handled = FALSE;
345 data.ret = FALSE;
346
347 g_main_context_invoke (watcher->context,
348 (GSourceFunc) register_device_notification, &data);
349 g_mutex_lock (&watcher->lock);
350 while (!data.handled)
351 g_cond_wait (&watcher->cond, &watcher->lock);
352 g_mutex_unlock (&watcher->lock);
353
354 return data.ret;
355 }
356
357 static gboolean
unregister_device_notification(DeviceNotificationData * data)358 unregister_device_notification (DeviceNotificationData * data)
359 {
360 GstWin32DeviceWatcher * self = data->self;
361
362 if (!self->device_notify)
363 goto out;
364
365 UnregisterDeviceNotification (self->device_notify);
366 self->device_notify = nullptr;
367
368 out:
369 g_mutex_lock (&self->lock);
370 data->handled = TRUE;
371 g_cond_broadcast (&self->cond);
372 g_mutex_unlock (&self->lock);
373
374 return G_SOURCE_REMOVE;
375 }
376
377 void
gst_win32_device_watcher_stop(GstWin32DeviceWatcher * watcher)378 gst_win32_device_watcher_stop (GstWin32DeviceWatcher * watcher)
379 {
380 DeviceNotificationData data;
381
382 g_return_if_fail (GST_IS_WIN32_DEVICE_WATCHER (watcher));
383
384 data.self = watcher;
385 data.handled = FALSE;
386
387 g_main_context_invoke (watcher->context,
388 (GSourceFunc) register_device_notification, &data);
389 g_mutex_lock (&watcher->lock);
390 while (!data.handled)
391 g_cond_wait (&watcher->cond, &watcher->lock);
392 g_mutex_unlock (&watcher->lock);
393 }
394