2 * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
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.
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.
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.
24 #include "gstmfconfig.h"
26 #include "gstmfvideosrc.h"
27 #include "gstmfutils.h"
28 #include "gstmfsourceobject.h"
30 #include "gstmfdevice.h"
32 #if GST_MF_WINAPI_DESKTOP
33 #include "gstwin32devicewatcher.h"
34 #include "gstmfcapturedshow.h"
41 DEFINE_GUID (GST_KSCATEGORY_CAPTURE, 0x65E8773DL, 0x8F56,
42 0x11D0, 0xA3, 0xB9, 0x00, 0xA0, 0xC9, 0x22, 0x31, 0x96);
46 #include <gst/winrt/gstwinrt.h>
48 using namespace ABI::Windows::Devices::Enumeration;
52 GST_DEBUG_CATEGORY_EXTERN (gst_mf_debug);
53 #define GST_CAT_DEFAULT gst_mf_debug
68 G_DEFINE_TYPE (GstMFDevice, gst_mf_device, GST_TYPE_DEVICE);
70 static void gst_mf_device_get_property (GObject * object,
71 guint prop_id, GValue * value, GParamSpec * pspec);
72 static void gst_mf_device_set_property (GObject * object,
73 guint prop_id, const GValue * value, GParamSpec * pspec);
74 static void gst_mf_device_finalize (GObject * object);
75 static GstElement *gst_mf_device_create_element (GstDevice * device,
79 gst_mf_device_class_init (GstMFDeviceClass * klass)
81 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
82 GstDeviceClass *dev_class = GST_DEVICE_CLASS (klass);
84 dev_class->create_element = gst_mf_device_create_element;
86 gobject_class->get_property = gst_mf_device_get_property;
87 gobject_class->set_property = gst_mf_device_set_property;
88 gobject_class->finalize = gst_mf_device_finalize;
90 g_object_class_install_property (gobject_class, PROP_DEVICE_PATH,
91 g_param_spec_string ("device-path", "Device Path",
92 "The device path", nullptr,
93 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
94 G_PARAM_STATIC_STRINGS)));
98 gst_mf_device_init (GstMFDevice * self)
103 gst_mf_device_finalize (GObject * object)
105 GstMFDevice *self = GST_MF_DEVICE (object);
107 g_free (self->device_path);
109 G_OBJECT_CLASS (gst_mf_device_parent_class)->finalize (object);
113 gst_mf_device_create_element (GstDevice * device, const gchar * name)
115 GstMFDevice *self = GST_MF_DEVICE (device);
118 elem = gst_element_factory_make ("mfvideosrc", name);
120 g_object_set (elem, "device-path", self->device_path, nullptr);
126 gst_mf_device_get_property (GObject * object, guint prop_id,
127 GValue * value, GParamSpec * pspec)
129 GstMFDevice *self = GST_MF_DEVICE (object);
132 case PROP_DEVICE_PATH:
133 g_value_set_string (value, self->device_path);
136 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142 gst_mf_device_set_property (GObject * object, guint prop_id,
143 const GValue * value, GParamSpec * pspec)
145 GstMFDevice *self = GST_MF_DEVICE (object);
148 case PROP_DEVICE_PATH:
149 self->device_path = g_value_dup_string (value);
152 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
157 struct _GstMFDeviceProvider
159 GstDeviceProvider parent;
166 gboolean enum_completed;
169 G_DEFINE_TYPE (GstMFDeviceProvider, gst_mf_device_provider,
170 GST_TYPE_DEVICE_PROVIDER);
172 static void gst_mf_device_provider_dispose (GObject * object);
173 static void gst_mf_device_provider_finalize (GObject * object);
175 static GList *gst_mf_device_provider_probe (GstDeviceProvider * provider);
176 static gboolean gst_mf_device_provider_start (GstDeviceProvider * provider);
177 static void gst_mf_device_provider_stop (GstDeviceProvider * provider);
179 #if GST_MF_WINAPI_DESKTOP
180 static gboolean gst_mf_device_provider_start_win32 (GstDeviceProvider * self);
181 static void gst_mf_device_provider_device_changed (GstWin32DeviceWatcher *
182 watcher, WPARAM wparam, LPARAM lparam, gpointer user_data);
185 #if GST_MF_WINAPI_APP
186 static gboolean gst_mf_device_provider_start_winrt (GstDeviceProvider * self);
188 gst_mf_device_provider_device_added (GstWinRTDeviceWatcher * watcher,
189 IDeviceInformation * info, gpointer user_data);
191 gst_mf_device_provider_device_updated (GstWinRTDeviceWatcher * watcher,
192 IDeviceInformationUpdate * info_update, gpointer user_data);
193 static void gst_mf_device_provider_device_removed (GstWinRTDeviceWatcher *
194 watcher, IDeviceInformationUpdate * info_update, gpointer user_data);
196 gst_mf_device_provider_device_enum_completed (GstWinRTDeviceWatcher *
197 watcher, gpointer user_data);
201 gst_mf_device_provider_on_device_updated (GstMFDeviceProvider * self);
204 gst_mf_device_provider_class_init (GstMFDeviceProviderClass * klass)
206 GstDeviceProviderClass *provider_class = GST_DEVICE_PROVIDER_CLASS (klass);
208 provider_class->probe = GST_DEBUG_FUNCPTR (gst_mf_device_provider_probe);
209 provider_class->start = GST_DEBUG_FUNCPTR (gst_mf_device_provider_start);
210 provider_class->stop = GST_DEBUG_FUNCPTR (gst_mf_device_provider_stop);
212 gst_device_provider_class_set_static_metadata (provider_class,
213 "Media Foundation Device Provider",
214 "Source/Video", "List Media Foundation source devices",
215 "Seungha Yang <seungha@centricular.com>");
219 gst_mf_device_provider_init (GstMFDeviceProvider * self)
221 #if GST_MF_WINAPI_DESKTOP
222 GstWin32DeviceWatcherCallbacks win32_callbacks;
224 win32_callbacks.device_changed = gst_mf_device_provider_device_changed;
225 self->watcher = (GstObject *)
226 gst_win32_device_watcher_new (DBT_DEVTYP_DEVICEINTERFACE,
227 &GST_KSCATEGORY_CAPTURE, &win32_callbacks, self);
229 #if GST_MF_WINAPI_APP
230 if (!self->watcher) {
231 GstWinRTDeviceWatcherCallbacks winrt_callbacks;
232 winrt_callbacks.added = gst_mf_device_provider_device_added;
233 winrt_callbacks.updated = gst_mf_device_provider_device_updated;
234 winrt_callbacks.removed = gst_mf_device_provider_device_removed;
235 winrt_callbacks.enumeration_completed =
236 gst_mf_device_provider_device_enum_completed;
238 self->watcher = (GstObject *)
239 gst_winrt_device_watcher_new (GST_WINRT_DEVICE_CLASS_VIDEO_CAPTURE,
240 &winrt_callbacks, self);
244 g_mutex_init (&self->lock);
245 g_cond_init (&self->cond);
249 gst_mf_device_provider_dispose (GObject * object)
251 GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (object);
253 gst_clear_object (&self->watcher);
255 G_OBJECT_CLASS (gst_mf_device_provider_parent_class)->dispose (object);
259 gst_mf_device_provider_finalize (GObject * object)
261 GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (object);
263 g_mutex_clear (&self->lock);
264 g_cond_clear (&self->cond);
266 G_OBJECT_CLASS (gst_mf_device_provider_parent_class)->finalize (object);
270 gst_mf_device_provider_probe_internal (GstDeviceProvider * provider,
271 gboolean try_dshow, GList ** list)
273 GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (provider);
277 GstMFSourceObject *obj = nullptr;
279 GstStructure *props = nullptr;
280 GstCaps *caps = nullptr;
281 gchar *device_name = nullptr;
282 gchar *device_path = nullptr;
284 #if GST_MF_WINAPI_DESKTOP
286 obj = gst_mf_capture_dshow_new (GST_MF_SOURCE_TYPE_VIDEO, i,
289 obj = gst_mf_source_object_new (GST_MF_SOURCE_TYPE_VIDEO,
290 i, nullptr, nullptr, nullptr);
293 obj = gst_mf_source_object_new (GST_MF_SOURCE_TYPE_VIDEO,
294 i, nullptr, nullptr, nullptr);
299 caps = gst_mf_source_object_get_caps (obj);
301 GST_WARNING_OBJECT (self, "Empty caps for device index %d", i);
306 "device-path", &device_path, "device-name", &device_name, nullptr);
309 GST_WARNING_OBJECT (self, "Device path is unavailable");
314 GST_WARNING_OBJECT (self, "Device name is unavailable");
318 props = gst_structure_new ("mf-proplist",
319 "device.api", G_TYPE_STRING, "mediafoundation",
320 "device.path", G_TYPE_STRING, device_path,
321 "device.name", G_TYPE_STRING, device_name, nullptr);
323 device = (GstDevice *) g_object_new (GST_TYPE_MF_DEVICE,
324 "device-path", device_path,
325 "display-name", device_name, "caps", caps,
326 "device-class", "Source/Video", "properties", props, nullptr);
328 *list = g_list_append (*list, device);
332 gst_caps_unref (caps);
334 gst_structure_free (props);
335 g_free (device_path);
336 g_free (device_name);
337 gst_object_unref (obj);
342 gst_mf_device_provider_probe (GstDeviceProvider * provider)
344 GList *list = nullptr;
346 gst_mf_device_provider_probe_internal (provider, FALSE, &list);
347 #if GST_MF_WINAPI_DESKTOP
348 gst_mf_device_provider_probe_internal (provider, TRUE, &list);
354 #if GST_MF_WINAPI_DESKTOP
356 gst_mf_device_provider_start_win32 (GstDeviceProvider * provider)
358 GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (provider);
359 GstWin32DeviceWatcher *watcher;
360 GList *devices = nullptr;
363 if (!GST_IS_WIN32_DEVICE_WATCHER (self->watcher))
366 GST_DEBUG_OBJECT (self, "Starting Win32 watcher");
368 watcher = GST_WIN32_DEVICE_WATCHER (self->watcher);
370 devices = gst_mf_device_provider_probe (provider);
372 for (iter = devices; iter; iter = g_list_next (iter)) {
373 gst_device_provider_device_add (provider, GST_DEVICE (iter->data));
376 g_list_free (devices);
379 return gst_win32_device_watcher_start (watcher);
383 #if GST_MF_WINAPI_APP
385 gst_mf_device_provider_start_winrt (GstDeviceProvider * provider)
387 GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (provider);
388 GstWinRTDeviceWatcher *watcher;
389 GList *devices = nullptr;
392 if (!GST_IS_WINRT_DEVICE_WATCHER (self->watcher))
395 GST_DEBUG_OBJECT (self, "Starting WinRT watcher");
396 watcher = GST_WINRT_DEVICE_WATCHER (self->watcher);
398 self->enum_completed = FALSE;
400 if (!gst_winrt_device_watcher_start (watcher))
403 /* Wait for initial enumeration to be completed */
404 g_mutex_lock (&self->lock);
405 while (!self->enum_completed)
406 g_cond_wait (&self->cond, &self->lock);
408 devices = gst_mf_device_provider_probe (provider);
410 for (iter = devices; iter; iter = g_list_next (iter)) {
411 gst_device_provider_device_add (provider, GST_DEVICE (iter->data));
414 g_list_free (devices);
416 g_mutex_unlock (&self->lock);
423 gst_mf_device_provider_start (GstDeviceProvider * provider)
425 GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (provider);
426 gboolean ret = FALSE;
428 if (!self->watcher) {
429 GST_ERROR_OBJECT (self, "DeviceWatcher object wasn't configured");
432 #if GST_MF_WINAPI_DESKTOP
433 ret = gst_mf_device_provider_start_win32 (provider);
436 #if GST_MF_WINAPI_APP
438 ret = gst_mf_device_provider_start_winrt (provider);
445 gst_mf_device_provider_stop (GstDeviceProvider * provider)
447 GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (provider);
450 #if GST_MF_WINAPI_DESKTOP
451 if (GST_IS_WIN32_DEVICE_WATCHER (self->watcher)) {
452 gst_win32_device_watcher_stop (GST_WIN32_DEVICE_WATCHER (self->watcher));
455 #if GST_MF_WINAPI_APP
456 if (GST_IS_WINRT_DEVICE_WATCHER (self->watcher)) {
457 gst_winrt_device_watcher_stop (GST_WINRT_DEVICE_WATCHER (self->watcher));
464 gst_mf_device_is_in_list (GList * list, GstDevice * device)
468 const gchar *device_id;
469 gboolean found = FALSE;
471 s = gst_device_get_properties (device);
474 device_id = gst_structure_get_string (s, "device.path");
475 g_assert (device_id);
477 for (iter = list; iter; iter = g_list_next (iter)) {
478 GstStructure *other_s;
479 const gchar *other_id;
481 other_s = gst_device_get_properties (GST_DEVICE (iter->data));
484 other_id = gst_structure_get_string (other_s, "device.path");
487 if (g_ascii_strcasecmp (device_id, other_id) == 0) {
491 gst_structure_free (other_s);
496 gst_structure_free (s);
502 gst_mf_device_provider_update_devices (GstMFDeviceProvider * self)
504 GstDeviceProvider *provider = GST_DEVICE_PROVIDER_CAST (self);
505 GList *prev_devices = nullptr;
506 GList *new_devices = nullptr;
507 GList *to_add = nullptr;
508 GList *to_remove = nullptr;
511 GST_OBJECT_LOCK (self);
512 prev_devices = g_list_copy_deep (provider->devices,
513 (GCopyFunc) gst_object_ref, nullptr);
514 GST_OBJECT_UNLOCK (self);
516 new_devices = gst_mf_device_provider_probe (provider);
518 /* Ownership of GstDevice for gst_device_provider_device_add()
519 * and gst_device_provider_device_remove() is a bit complicated.
520 * Remove floating reference here for things to be clear */
521 for (iter = new_devices; iter; iter = g_list_next (iter))
522 gst_object_ref_sink (iter->data);
524 /* Check newly added devices */
525 for (iter = new_devices; iter; iter = g_list_next (iter)) {
526 if (!gst_mf_device_is_in_list (prev_devices, GST_DEVICE (iter->data))) {
527 to_add = g_list_prepend (to_add, gst_object_ref (iter->data));
531 /* Check removed device */
532 for (iter = prev_devices; iter; iter = g_list_next (iter)) {
533 if (!gst_mf_device_is_in_list (new_devices, GST_DEVICE (iter->data))) {
534 to_remove = g_list_prepend (to_remove, gst_object_ref (iter->data));
538 for (iter = to_remove; iter; iter = g_list_next (iter))
539 gst_device_provider_device_remove (provider, GST_DEVICE (iter->data));
541 for (iter = to_add; iter; iter = g_list_next (iter))
542 gst_device_provider_device_add (provider, GST_DEVICE (iter->data));
545 g_list_free_full (prev_devices, (GDestroyNotify) gst_object_unref);
548 g_list_free_full (to_add, (GDestroyNotify) gst_object_unref);
551 g_list_free_full (to_remove, (GDestroyNotify) gst_object_unref);
554 #if GST_MF_WINAPI_DESKTOP
556 gst_mf_device_provider_device_changed (GstWin32DeviceWatcher * watcher,
557 WPARAM wparam, LPARAM lparam, gpointer user_data)
559 GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (user_data);
561 if (wparam == DBT_DEVICEARRIVAL || wparam == DBT_DEVICEREMOVECOMPLETE) {
562 gst_mf_device_provider_update_devices (self);
567 #if GST_MF_WINAPI_APP
569 gst_mf_device_provider_device_added (GstWinRTDeviceWatcher * watcher,
570 IDeviceInformation * info, gpointer user_data)
572 GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (user_data);
574 if (self->enum_completed)
575 gst_mf_device_provider_update_devices (self);
579 gst_mf_device_provider_device_removed (GstWinRTDeviceWatcher * watcher,
580 IDeviceInformationUpdate * info_update, gpointer user_data)
582 GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (user_data);
584 if (self->enum_completed)
585 gst_mf_device_provider_update_devices (self);
590 gst_mf_device_provider_device_updated (GstWinRTDeviceWatcher * watcher,
591 IDeviceInformationUpdate * info_update, gpointer user_data)
593 GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (user_data);
595 gst_mf_device_provider_update_devices (self);
599 gst_mf_device_provider_device_enum_completed (GstWinRTDeviceWatcher *
600 watcher, gpointer user_data)
602 GstMFDeviceProvider *self = GST_MF_DEVICE_PROVIDER (user_data);
604 g_mutex_lock (&self->lock);
605 GST_DEBUG_OBJECT (self, "Enumeration completed");
606 self->enum_completed = TRUE;
607 g_cond_signal (&self->cond);
608 g_mutex_unlock (&self->lock);