From feb5a5aae6100e0e64a66fab30efc6b3ff876270 Mon Sep 17 00:00:00 2001 From: Seungha Yang Date: Fri, 27 Aug 2021 19:19:57 +0900 Subject: [PATCH] mfdeviceprovider: Add support for device update Similar to the wasapi2 plugin, GstWinRT library will be used for UWP, and adding new GstWin32DeviceWatcher object implementation for Win32 desktop application. Part-of: --- .../sys/mediafoundation/gstmfdevice.c | 367 +++++++++++++++- .../mediafoundation/gstwin32devicewatcher.cpp | 393 ++++++++++++++++++ .../mediafoundation/gstwin32devicewatcher.h | 52 +++ .../sys/mediafoundation/meson.build | 11 +- 4 files changed, 821 insertions(+), 2 deletions(-) create mode 100644 subprojects/gst-plugins-bad/sys/mediafoundation/gstwin32devicewatcher.cpp create mode 100644 subprojects/gst-plugins-bad/sys/mediafoundation/gstwin32devicewatcher.h diff --git a/subprojects/gst-plugins-bad/sys/mediafoundation/gstmfdevice.c b/subprojects/gst-plugins-bad/sys/mediafoundation/gstmfdevice.c index 7e2d74ad75..7ed92ad6fa 100644 --- a/subprojects/gst-plugins-bad/sys/mediafoundation/gstmfdevice.c +++ b/subprojects/gst-plugins-bad/sys/mediafoundation/gstmfdevice.c @@ -29,6 +29,22 @@ #include "gstmfdevice.h" +#if GST_MF_WINAPI_DESKTOP +#include "gstwin32devicewatcher.h" + +#ifndef INITGUID +#include +#endif + +#include +DEFINE_GUID (GST_KSCATEGORY_CAPTURE, 0x65E8773DL, 0x8F56, + 0x11D0, 0xA3, 0xB9, 0x00, 0xA0, 0xC9, 0x22, 0x31, 0x96); +#endif + +#if GST_MF_WINAPI_APP +#include +#endif + GST_DEBUG_CATEGORY_EXTERN (gst_mf_debug); #define GST_CAT_DEFAULT gst_mf_debug @@ -136,12 +152,52 @@ gst_mf_device_set_property (GObject * object, guint prop_id, struct _GstMFDeviceProvider { GstDeviceProvider parent; + + GstObject *watcher; + + GMutex lock; + GCond cond; + + gboolean enum_completed; }; G_DEFINE_TYPE (GstMFDeviceProvider, gst_mf_device_provider, GST_TYPE_DEVICE_PROVIDER); +static void gst_mf_device_provider_dispose (GObject * object); +static void gst_mf_device_provider_finalize (GObject * object); + static GList *gst_mf_device_provider_probe (GstDeviceProvider * provider); +static gboolean gst_mf_device_provider_start (GstDeviceProvider * provider); +static void gst_mf_device_provider_stop (GstDeviceProvider * provider); + +#if GST_MF_WINAPI_DESKTOP +static gboolean gst_mf_device_provider_start_win32 (GstDeviceProvider * self); +static void gst_mf_device_provider_device_changed (GstWin32DeviceWatcher * + watcher, WPARAM wparam, LPARAM lparam, gpointer user_data); +#endif + +#if GST_MF_WINAPI_APP +static gboolean gst_mf_device_provider_start_winrt (GstDeviceProvider * self); +static void +gst_mf_device_provider_device_added (GstWinRTDeviceWatcher * watcher, + __x_ABI_CWindows_CDevices_CEnumeration_CIDeviceInformation * info, + gpointer user_data); +static void +gst_mf_device_provider_device_updated (GstWinRTDeviceWatcher * watcher, + __x_ABI_CWindows_CDevices_CEnumeration_CIDeviceInformationUpdate * + info_update, gpointer user_data); +static void gst_mf_device_provider_device_removed (GstWinRTDeviceWatcher * + watcher, + __x_ABI_CWindows_CDevices_CEnumeration_CIDeviceInformationUpdate * + info_update, gpointer user_data); +static void +gst_mf_device_provider_device_enum_completed (GstWinRTDeviceWatcher * + watcher, gpointer user_data); +#endif + +static void +gst_mf_device_provider_on_device_updated (GstMFDeviceProvider * self); static void gst_mf_device_provider_class_init (GstMFDeviceProviderClass * klass) @@ -149,6 +205,8 @@ gst_mf_device_provider_class_init (GstMFDeviceProviderClass * klass) GstDeviceProviderClass *provider_class = GST_DEVICE_PROVIDER_CLASS (klass); provider_class->probe = GST_DEBUG_FUNCPTR (gst_mf_device_provider_probe); + provider_class->start = GST_DEBUG_FUNCPTR (gst_mf_device_provider_start); + provider_class->stop = GST_DEBUG_FUNCPTR (gst_mf_device_provider_stop); gst_device_provider_class_set_static_metadata (provider_class, "Media Foundation Device Provider", @@ -157,8 +215,54 @@ gst_mf_device_provider_class_init (GstMFDeviceProviderClass * klass) } static void -gst_mf_device_provider_init (GstMFDeviceProvider * provider) +gst_mf_device_provider_init (GstMFDeviceProvider * self) { +#if GST_MF_WINAPI_DESKTOP + GstWin32DeviceWatcherCallbacks win32_callbacks; + + win32_callbacks.device_changed = gst_mf_device_provider_device_changed; + self->watcher = (GstObject *) + gst_win32_device_watcher_new (DBT_DEVTYP_DEVICEINTERFACE, + &GST_KSCATEGORY_CAPTURE, &win32_callbacks, self); +#endif +#if GST_MF_WINAPI_APP + if (!self->watcher) { + GstWinRTDeviceWatcherCallbacks winrt_callbacks; + winrt_callbacks.added = gst_mf_device_provider_device_added; + winrt_callbacks.updated = gst_mf_device_provider_device_updated; + winrt_callbacks.removed = gst_mf_device_provider_device_removed; + winrt_callbacks.enumeration_completed = + gst_mf_device_provider_device_enum_completed; + + self->watcher = (GstObject *) + gst_winrt_device_watcher_new (GST_WINRT_DEVICE_CLASS_VIDEO_CAPTURE, + &winrt_callbacks, self); + } +#endif + + g_mutex_init (&self->lock); + g_cond_init (&self->cond); +} + +static void +gst_mf_device_provider_dispose (GObject * object) +{ + GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (object); + + gst_clear_object (&self->watcher); + + G_OBJECT_CLASS (gst_mf_device_provider_parent_class)->dispose (object); +} + +static void +gst_mf_device_provider_finalize (GObject * object) +{ + GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (object); + + g_mutex_clear (&self->lock); + g_cond_clear (&self->cond); + + G_OBJECT_CLASS (gst_mf_device_provider_parent_class)->finalize (object); } static GList * @@ -223,3 +327,264 @@ gst_mf_device_provider_probe (GstDeviceProvider * provider) return list; } + +#if GST_MF_WINAPI_DESKTOP +static gboolean +gst_mf_device_provider_start_win32 (GstDeviceProvider * provider) +{ + GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (provider); + GstWin32DeviceWatcher *watcher; + GList *devices = NULL; + GList *iter; + + if (!GST_IS_WIN32_DEVICE_WATCHER (self->watcher)) + return FALSE; + + GST_DEBUG_OBJECT (self, "Starting Win32 watcher"); + + watcher = GST_WIN32_DEVICE_WATCHER (self->watcher); + + devices = gst_mf_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 gst_win32_device_watcher_start (watcher); +} +#endif + +#if GST_MF_WINAPI_APP +static gboolean +gst_mf_device_provider_start_winrt (GstDeviceProvider * provider) +{ + GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (provider); + GstWinRTDeviceWatcher *watcher; + GList *devices = NULL; + GList *iter; + + if (!GST_IS_WINRT_DEVICE_WATCHER (self->watcher)) + return FALSE; + + GST_DEBUG_OBJECT (self, "Starting WinRT watcher"); + watcher = GST_WINRT_DEVICE_WATCHER (self->watcher); + + self->enum_completed = FALSE; + + if (!gst_winrt_device_watcher_start (watcher)) + return FALSE; + + /* Wait for initial enumeration to be completed */ + g_mutex_lock (&self->lock); + while (!self->enum_completed) + g_cond_wait (&self->cond, &self->lock); + + devices = gst_mf_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); + } + g_mutex_unlock (&self->lock); + + return TRUE; +} +#endif + +static gboolean +gst_mf_device_provider_start (GstDeviceProvider * provider) +{ + GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (provider); + gboolean ret = FALSE; + + if (!self->watcher) { + GST_ERROR_OBJECT (self, "DeviceWatcher object wasn't configured"); + return FALSE; + } +#if GST_MF_WINAPI_DESKTOP + ret = gst_mf_device_provider_start_win32 (provider); +#endif + +#if GST_MF_WINAPI_APP + if (!ret) + ret = gst_mf_device_provider_start_winrt (provider); +#endif + + return ret; +} + +static void +gst_mf_device_provider_stop (GstDeviceProvider * provider) +{ + GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (provider); + + if (self->watcher) { +#if GST_MF_WINAPI_DESKTOP + if (GST_IS_WIN32_DEVICE_WATCHER (self->watcher)) { + gst_win32_device_watcher_stop (GST_WIN32_DEVICE_WATCHER (self->watcher)); + } +#endif +#if GST_MF_WINAPI_APP + if (GST_IS_WINRT_DEVICE_WATCHER (self->watcher)) { + gst_winrt_device_watcher_stop (GST_WINRT_DEVICE_WATCHER (self->watcher)); + } +#endif + } +} + +static gboolean +gst_mf_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.path"); + 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.path"); + 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_mf_device_provider_update_devices (GstMFDeviceProvider * 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_mf_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_mf_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_mf_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); +} + +#if GST_MF_WINAPI_DESKTOP +static void +gst_mf_device_provider_device_changed (GstWin32DeviceWatcher * watcher, + WPARAM wparam, LPARAM lparam, gpointer user_data) +{ + GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (user_data); + + if (wparam == DBT_DEVICEARRIVAL || wparam == DBT_DEVICEREMOVECOMPLETE) { + gst_mf_device_provider_update_devices (self); + } +} +#endif + +#if GST_MF_WINAPI_APP +static void +gst_mf_device_provider_device_added (GstWinRTDeviceWatcher * watcher, + __x_ABI_CWindows_CDevices_CEnumeration_CIDeviceInformation * info, + gpointer user_data) +{ + GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (user_data); + + if (self->enum_completed) + gst_mf_device_provider_update_devices (self); +} + +static void +gst_mf_device_provider_device_removed (GstWinRTDeviceWatcher * watcher, + __x_ABI_CWindows_CDevices_CEnumeration_CIDeviceInformationUpdate * + info_update, gpointer user_data) +{ + GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (user_data); + + if (self->enum_completed) + gst_mf_device_provider_update_devices (self); +} + + +static void +gst_mf_device_provider_device_updated (GstWinRTDeviceWatcher * watcher, + __x_ABI_CWindows_CDevices_CEnumeration_CIDeviceInformationUpdate * + info_update, gpointer user_data) +{ + GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (user_data); + + gst_mf_device_provider_update_devices (self); +} + +static void +gst_mf_device_provider_device_enum_completed (GstWinRTDeviceWatcher * + watcher, gpointer user_data) +{ + GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (user_data); + + g_mutex_lock (&self->lock); + GST_DEBUG_OBJECT (self, "Enumeration completed"); + self->enum_completed = TRUE; + g_cond_signal (&self->cond); + g_mutex_unlock (&self->lock); +} +#endif diff --git a/subprojects/gst-plugins-bad/sys/mediafoundation/gstwin32devicewatcher.cpp b/subprojects/gst-plugins-bad/sys/mediafoundation/gstwin32devicewatcher.cpp new file mode 100644 index 0000000000..4b1d1e46ae --- /dev/null +++ b/subprojects/gst-plugins-bad/sys/mediafoundation/gstwin32devicewatcher.cpp @@ -0,0 +1,393 @@ +/* GStreamer + * Copyright (C) 2021 Seungha Yang + * + * 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., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gstwin32devicewatcher.h" +#include + +GST_DEBUG_CATEGORY_STATIC (gst_win32_device_watcher_debug); +#define GST_CAT_DEFAULT gst_win32_device_watcher_debug + +G_LOCK_DEFINE_STATIC (create_lock); + +#define GST_WIN32_HWND_PROP_NAME "gst-win32-device-watcher" + +struct _GstWin32DeviceWatcher +{ + GstObject parent; + + GMutex lock; + GCond cond; + + GThread *thread; + GMainContext *context; + GMainLoop *loop; + + GstWin32DeviceWatcherCallbacks callbacks; + gpointer user_data; + + HDEVNOTIFY device_notify; + HWND hwnd; + DWORD device_type; + GUID class_guid; +}; + +#define gst_win32_device_watcher_parent_class parent_class +G_DEFINE_TYPE (GstWin32DeviceWatcher, gst_win32_device_watcher, + GST_TYPE_OBJECT); + +static void gst_win32_device_watcher_constructed (GObject * object); +static void gst_win32_device_watcher_finalize (GObject * object); + +static gpointer +gst_win32_device_watcher_thread_func (GstWin32DeviceWatcher * self); + +static void +gst_win32_device_watcher_class_init (GstWin32DeviceWatcherClass * klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + + gobject_class->constructed = gst_win32_device_watcher_constructed; + gobject_class->finalize = gst_win32_device_watcher_finalize; + + GST_DEBUG_CATEGORY_INIT (gst_win32_device_watcher_debug, + "win32devicewatcher", 0, "win32devicewatcher"); +} + +static void +gst_win32_device_watcher_init (GstWin32DeviceWatcher * self) +{ + g_mutex_init (&self->lock); + g_cond_init (&self->cond); + self->context = g_main_context_new (); + self->loop = g_main_loop_new (self->context, FALSE); +} + +static void +gst_win32_device_watcher_constructed (GObject * object) +{ + GstWin32DeviceWatcher *self = GST_WIN32_DEVICE_WATCHER (object); + + /* Create a new thread for WIN32 message pumping */ + g_mutex_lock (&self->lock); + self->thread = g_thread_new ("GstWin32DeviceWatcher", + (GThreadFunc) gst_win32_device_watcher_thread_func, self); + while (!g_main_loop_is_running (self->loop)) + g_cond_wait (&self->cond, &self->lock); + g_mutex_unlock (&self->lock); +} + +static void +gst_win32_device_watcher_finalize (GObject * object) +{ + GstWin32DeviceWatcher *self = GST_WIN32_DEVICE_WATCHER (object); + + g_main_loop_quit (self->loop); + if (g_thread_self () != self->thread) { + g_thread_join (self->thread); + g_main_loop_unref (self->loop); + g_main_context_unref (self->context); + } else { + g_warning ("Trying join from self-thread"); + } + + g_mutex_clear (&self->lock); + g_cond_clear (&self->cond); + + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +static LRESULT CALLBACK +window_proc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) +{ + GstWin32DeviceWatcher * self; + + switch (msg) { + case WM_CREATE: + self = (GstWin32DeviceWatcher *) + ((LPCREATESTRUCT) lparam)->lpCreateParams; + + GST_DEBUG_OBJECT (self, "WM_CREATE"); + + SetPropA (hwnd, GST_WIN32_HWND_PROP_NAME, self); + break; + case WM_DEVICECHANGE: + { + self = (GstWin32DeviceWatcher *) GetPropA + (hwnd, GST_WIN32_HWND_PROP_NAME); + if (!self) { + GST_WARNING ("Failed to get watcher object"); + break; + } + + self->callbacks.device_changed (self, wparam, lparam, self->user_data); + break; + } + default: + break; + } + + return DefWindowProc (hwnd, msg, wparam, lparam); +} + +static HWND +create_hwnd (GstWin32DeviceWatcher * self) +{ + WNDCLASSEXA wc; + ATOM atom = 0; + HINSTANCE hinstance = GetModuleHandle (NULL); + static const gchar *klass_name = "GstWin32DeviceWatcher"; + HWND hwnd; + + G_LOCK (create_lock); + atom = GetClassInfoExA (hinstance, klass_name, &wc); + if (atom == 0) { + GST_LOG_OBJECT (self, "Register window class"); + ZeroMemory (&wc, sizeof (WNDCLASSEX)); + + wc.cbSize = sizeof (WNDCLASSEXA); + wc.lpfnWndProc = window_proc; + wc.hInstance = hinstance; + wc.lpszClassName = klass_name; + atom = RegisterClassExA (&wc); + + if (atom == 0) { + G_UNLOCK (create_lock); + GST_ERROR_OBJECT (self, "Failed to register window class, lastError 0x%x", + (guint) GetLastError ()); + return NULL; + } + } else { + GST_LOG_OBJECT (self, "window class was already registered"); + } + G_UNLOCK (create_lock); + + hwnd = CreateWindowExA (0, klass_name, "", 0, 0, 0, 0, 0, + HWND_MESSAGE, NULL, hinstance, self); + if (!hwnd) { + GST_ERROR_OBJECT (self, "Failed to create window handle, lastError 0x%x", + (guint) GetLastError ()); + return nullptr; + } + + return hwnd; +} + +static gboolean +loop_running_cb (GstWin32DeviceWatcher * self) +{ + g_mutex_lock (&self->lock); + g_cond_signal (&self->cond); + g_mutex_unlock (&self->lock); + + return G_SOURCE_REMOVE; +} + +static gboolean +win32_msg_cb (GIOChannel * source, GIOCondition condition, gpointer data) +{ + MSG msg; + + if (!PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) + return G_SOURCE_CONTINUE; + + TranslateMessage (&msg); + DispatchMessage (&msg); + + return G_SOURCE_CONTINUE; +} + +static gpointer +gst_win32_device_watcher_thread_func (GstWin32DeviceWatcher * self) +{ + GSource *idle_source; + HWND hwnd = nullptr; + GIOChannel *msg_io_channel = nullptr; + GSource *msg_source = nullptr; + + g_main_context_push_thread_default (self->context); + + idle_source = g_idle_source_new (); + g_source_set_callback (idle_source, + (GSourceFunc) loop_running_cb, self, nullptr); + g_source_attach (idle_source, self->context); + g_source_unref (idle_source); + + hwnd = create_hwnd (self); + if (!hwnd) + goto run_loop; + + msg_io_channel = g_io_channel_win32_new_messages ((gsize) hwnd); + msg_source = g_io_create_watch (msg_io_channel, G_IO_IN); + g_source_set_callback (msg_source, + (GSourceFunc) win32_msg_cb, nullptr, nullptr); + g_source_attach (msg_source, self->context); + + self->hwnd = hwnd; + +run_loop: + GST_INFO_OBJECT (self, "Starting loop"); + g_main_loop_run (self->loop); + GST_INFO_OBJECT (self, "Stopped loop"); + + if (self->device_notify) { + UnregisterDeviceNotification (self->device_notify); + self->device_notify = nullptr; + } + + if (msg_source) { + g_source_destroy (msg_source); + g_source_unref (msg_source); + } + + if (msg_io_channel) + g_io_channel_unref (msg_io_channel); + + if (hwnd) + DestroyWindow (hwnd); + + g_main_context_pop_thread_default (self->context); + + return nullptr; +} + +GstWin32DeviceWatcher * +gst_win32_device_watcher_new (DWORD device_type, const GUID * class_guid, + const GstWin32DeviceWatcherCallbacks * callbacks, gpointer user_data) +{ + GstWin32DeviceWatcher *self; + + g_return_val_if_fail (class_guid != nullptr, nullptr); + g_return_val_if_fail (callbacks != nullptr, nullptr); + + self = (GstWin32DeviceWatcher *) + g_object_new (GST_TYPE_WIN32_DEVICE_WATCHER, nullptr); + + if (!self->hwnd) { + gst_object_unref (self); + return nullptr; + } + + self->callbacks = *callbacks; + self->user_data = user_data; + self->device_type = device_type; + self->class_guid = *class_guid; + + gst_object_ref_sink (self); + + return self; +} + +typedef struct +{ + GstWin32DeviceWatcher * self; + + gboolean handled; + gboolean ret; +} DeviceNotificationData; + +static gboolean +register_device_notification (DeviceNotificationData * data) +{ + GstWin32DeviceWatcher * self = data->self; + DEV_BROADCAST_DEVICEINTERFACE di = { 0, }; + + if (self->device_notify) + goto out; + + di.dbcc_size = sizeof (di); + di.dbcc_devicetype = self->device_type; + di.dbcc_classguid = self->class_guid; + + self->device_notify = RegisterDeviceNotificationW (self->hwnd, + &di, DEVICE_NOTIFY_WINDOW_HANDLE); + +out: + if (self->device_notify) + data->ret = TRUE; + + g_mutex_lock (&self->lock); + data->handled = TRUE; + g_cond_broadcast (&self->cond); + g_mutex_unlock (&self->lock); + + return G_SOURCE_REMOVE; +} + +gboolean +gst_win32_device_watcher_start (GstWin32DeviceWatcher * watcher) +{ + DeviceNotificationData data; + + g_return_val_if_fail (GST_IS_WIN32_DEVICE_WATCHER (watcher), FALSE); + + data.self = watcher; + data.handled = FALSE; + data.ret = FALSE; + + g_main_context_invoke (watcher->context, + (GSourceFunc) register_device_notification, &data); + g_mutex_lock (&watcher->lock); + while (!data.handled) + g_cond_wait (&watcher->cond, &watcher->lock); + g_mutex_unlock (&watcher->lock); + + return data.ret; +} + +static gboolean +unregister_device_notification (DeviceNotificationData * data) +{ + GstWin32DeviceWatcher * self = data->self; + + if (!self->device_notify) + goto out; + + UnregisterDeviceNotification (self->device_notify); + self->device_notify = nullptr; + +out: + g_mutex_lock (&self->lock); + data->handled = TRUE; + g_cond_broadcast (&self->cond); + g_mutex_unlock (&self->lock); + + return G_SOURCE_REMOVE; +} + +void +gst_win32_device_watcher_stop (GstWin32DeviceWatcher * watcher) +{ + DeviceNotificationData data; + + g_return_if_fail (GST_IS_WIN32_DEVICE_WATCHER (watcher)); + + data.self = watcher; + data.handled = FALSE; + + g_main_context_invoke (watcher->context, + (GSourceFunc) register_device_notification, &data); + g_mutex_lock (&watcher->lock); + while (!data.handled) + g_cond_wait (&watcher->cond, &watcher->lock); + g_mutex_unlock (&watcher->lock); +} diff --git a/subprojects/gst-plugins-bad/sys/mediafoundation/gstwin32devicewatcher.h b/subprojects/gst-plugins-bad/sys/mediafoundation/gstwin32devicewatcher.h new file mode 100644 index 0000000000..153e17d4e6 --- /dev/null +++ b/subprojects/gst-plugins-bad/sys/mediafoundation/gstwin32devicewatcher.h @@ -0,0 +1,52 @@ +/* GStreamer + * Copyright (C) 2021 Seungha Yang + * + * 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., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __GST_WIN32_DEVICE_WATCHER_H__ +#define __GST_WIN32_DEVICE_WATCHER_H__ + +#include +#include + +G_BEGIN_DECLS + +#define GST_TYPE_WIN32_DEVICE_WATCHER (gst_win32_device_watcher_get_type()) +G_DECLARE_FINAL_TYPE (GstWin32DeviceWatcher, + gst_win32_device_watcher, GST, WIN32_DEVICE_WATCHER, GstObject); + +typedef struct _GstWin32DeviceWatcherCallbacks +{ + void (*device_changed) (GstWin32DeviceWatcher * watcher, + WPARAM wparam, + LPARAM lparam, + gpointer user_data); + +} GstWin32DeviceWatcherCallbacks; + +GstWin32DeviceWatcher * gst_win32_device_watcher_new (DWORD device_type, + const GUID * class_guid, + const GstWin32DeviceWatcherCallbacks * callbacks, + gpointer user_data); + +gboolean gst_win32_device_watcher_start (GstWin32DeviceWatcher * watcher); + +void gst_win32_device_watcher_stop (GstWin32DeviceWatcher * watcher); + +G_END_DECLS + +#endif /* __GST_WIN32_DEVICE_WATCHER_H__ */ diff --git a/subprojects/gst-plugins-bad/sys/mediafoundation/meson.build b/subprojects/gst-plugins-bad/sys/mediafoundation/meson.build index 8289549097..b4722365d8 100644 --- a/subprojects/gst-plugins-bad/sys/mediafoundation/meson.build +++ b/subprojects/gst-plugins-bad/sys/mediafoundation/meson.build @@ -17,6 +17,7 @@ mf_sources = [ mf_desktop_sources = [ 'gstmfsourcereader.cpp', + 'gstwin32devicewatcher.cpp', ] mf_app_sources = [ @@ -117,8 +118,16 @@ if not winapi_desktop and not winapi_app endif if winapi_app + if not gstwinrt_dep.found() + if mf_option.enabled() + error('The mediafoundation plugin was enabled explicitly, but GstWinRt library is unavailable') + else + subdir_done() + endif + endif + mf_sources += mf_app_sources - mf_lib_deps += [runtimeobject_lib] + mf_lib_deps += [runtimeobject_lib, gstwinrt_dep] endif if winapi_desktop