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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
|
/* GStreamer
* Copyright (C) 2021 Seungha Yang <seungha@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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstmmdeviceenumerator.h"
#ifndef INITGUID
#include <initguid.h>
#endif
/* *INDENT-OFF* */
G_BEGIN_DECLS
GST_DEBUG_CATEGORY_EXTERN (gst_wasapi_debug);
#define GST_CAT_DEFAULT gst_wasapi_debug
G_END_DECLS
/* IMMNotificationClient implementation */
class GstIMMNotificationClient : public IMMNotificationClient
{
public:
static HRESULT
CreateInstance (GstMMDeviceEnumerator * enumerator,
const GstMMNotificationClientCallbacks * callbacks,
gpointer user_data,
IMMNotificationClient ** client)
{
GstIMMNotificationClient *self;
self = new GstIMMNotificationClient ();
self->callbacks_ = *callbacks;
self->user_data_ = user_data;
g_weak_ref_set (&self->enumerator_, enumerator);
*client = (IMMNotificationClient *) self;
return S_OK;
}
/* IUnknown */
STDMETHODIMP
QueryInterface (REFIID riid, void ** object)
{
if (!object)
return E_POINTER;
if (riid == IID_IUnknown) {
*object = static_cast<IUnknown *> (this);
} else if (riid == __uuidof(IMMNotificationClient)) {
*object = static_cast<IMMNotificationClient *> (this);
} else {
*object = nullptr;
return E_NOINTERFACE;
}
AddRef ();
return S_OK;
}
STDMETHODIMP_ (ULONG)
AddRef (void)
{
GST_TRACE ("%p, %d", this, (guint) ref_count_);
return InterlockedIncrement (&ref_count_);
}
STDMETHODIMP_ (ULONG)
Release (void)
{
ULONG ref_count;
GST_TRACE ("%p, %d", this, (guint) ref_count_);
ref_count = InterlockedDecrement (&ref_count_);
if (ref_count == 0) {
GST_TRACE ("Delete instance %p", this);
delete this;
}
return ref_count;
}
/* IMMNotificationClient */
STDMETHODIMP
OnDeviceStateChanged (LPCWSTR device_id, DWORD new_state)
{
GstMMDeviceEnumerator *listener;
HRESULT hr;
if (!callbacks_.device_state_changed)
return S_OK;
listener = (GstMMDeviceEnumerator *) g_weak_ref_get (&enumerator_);
if (!listener)
return S_OK;
hr = callbacks_.device_state_changed (listener, device_id, new_state,
user_data_);
gst_object_unref (listener);
return hr;
}
STDMETHODIMP
OnDeviceAdded (LPCWSTR device_id)
{
GstMMDeviceEnumerator *listener;
HRESULT hr;
if (!callbacks_.device_added)
return S_OK;
listener = (GstMMDeviceEnumerator *) g_weak_ref_get (&enumerator_);
if (!listener)
return S_OK;
hr = callbacks_.device_added (listener, device_id, user_data_);
gst_object_unref (listener);
return hr;
}
STDMETHODIMP
OnDeviceRemoved (LPCWSTR device_id)
{
GstMMDeviceEnumerator *listener;
HRESULT hr;
if (!callbacks_.device_removed)
return S_OK;
listener = (GstMMDeviceEnumerator *) g_weak_ref_get (&enumerator_);
if (!listener)
return S_OK;
hr = callbacks_.device_removed (listener, device_id, user_data_);
gst_object_unref (listener);
return hr;
}
STDMETHODIMP
OnDefaultDeviceChanged (EDataFlow flow, ERole role, LPCWSTR default_device_id)
{
GstMMDeviceEnumerator *listener;
HRESULT hr;
if (!callbacks_.default_device_changed)
return S_OK;
listener = (GstMMDeviceEnumerator *) g_weak_ref_get (&enumerator_);
if (!listener)
return S_OK;
hr = callbacks_.default_device_changed (listener,
flow, role, default_device_id, user_data_);
gst_object_unref (listener);
return hr;
}
STDMETHODIMP
OnPropertyValueChanged (LPCWSTR device_id, const PROPERTYKEY key)
{
GstMMDeviceEnumerator *listener;
HRESULT hr;
if (!callbacks_.property_value_changed)
return S_OK;
listener = (GstMMDeviceEnumerator *) g_weak_ref_get (&enumerator_);
if (!device_id)
return S_OK;
hr = callbacks_.property_value_changed (listener,
device_id, key, user_data_);
gst_object_unref (listener);
return hr;
}
private:
GstIMMNotificationClient ()
: ref_count_ (1)
{
g_weak_ref_init (&enumerator_, nullptr);
}
virtual ~GstIMMNotificationClient ()
{
g_weak_ref_clear (&enumerator_);
}
private:
ULONG ref_count_;
GstMMNotificationClientCallbacks callbacks_;
gpointer user_data_;
GWeakRef enumerator_;
};
/* *INDENT-ON* */
struct _GstMMDeviceEnumerator
{
GstObject parent;
IMMDeviceEnumerator *handle;
IMMNotificationClient *client;
GMutex lock;
GCond cond;
GThread *thread;
GMainContext *context;
GMainLoop *loop;
gboolean running;
};
static void gst_mm_device_enumerator_constructed (GObject * object);
static void gst_mm_device_enumerator_finalize (GObject * object);
static gpointer
gst_mm_device_enumerator_thread_func (GstMMDeviceEnumerator * self);
#define gst_mm_device_enumerator_parent_class parent_class
G_DEFINE_TYPE (GstMMDeviceEnumerator,
gst_mm_device_enumerator, GST_TYPE_OBJECT);
static void
gst_mm_device_enumerator_class_init (GstMMDeviceEnumeratorClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->constructed = gst_mm_device_enumerator_constructed;
gobject_class->finalize = gst_mm_device_enumerator_finalize;
}
static void
gst_mm_device_enumerator_init (GstMMDeviceEnumerator * 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_mm_device_enumerator_constructed (GObject * object)
{
GstMMDeviceEnumerator *self = GST_MM_DEVICE_ENUMERATOR (object);
G_OBJECT_CLASS (parent_class)->constructed (object);
g_mutex_lock (&self->lock);
self->thread = g_thread_new ("GstMMDeviceEnumerator",
(GThreadFunc) gst_mm_device_enumerator_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_mm_device_enumerator_finalize (GObject * object)
{
GstMMDeviceEnumerator *self = GST_MM_DEVICE_ENUMERATOR (object);
g_main_loop_quit (self->loop);
g_thread_join (self->thread);
g_main_loop_unref (self->loop);
g_main_context_unref (self->context);
g_mutex_clear (&self->lock);
g_cond_clear (&self->cond);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static gboolean
loop_running_cb (GstMMDeviceEnumerator * self)
{
g_mutex_lock (&self->lock);
g_cond_signal (&self->cond);
g_mutex_unlock (&self->lock);
return G_SOURCE_REMOVE;
}
static gpointer
gst_mm_device_enumerator_thread_func (GstMMDeviceEnumerator * self)
{
GSource *idle_source;
IMMDeviceEnumerator *enumerator = nullptr;
HRESULT hr;
CoInitializeEx (NULL, COINIT_MULTITHREADED);
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);
hr = CoCreateInstance (__uuidof (MMDeviceEnumerator),
nullptr, CLSCTX_ALL, IID_PPV_ARGS (&enumerator));
if (FAILED (hr)) {
GST_ERROR_OBJECT (self, "Failed to create IMMDeviceEnumerator instance");
goto run_loop;
}
self->handle = enumerator;
run_loop:
GST_INFO_OBJECT (self, "Starting loop");
g_main_loop_run (self->loop);
GST_INFO_OBJECT (self, "Stopped loop");
if (self->client && self->handle) {
self->handle->UnregisterEndpointNotificationCallback (self->client);
self->client->Release ();
}
if (self->handle)
self->handle->Release ();
g_main_context_pop_thread_default (self->context);
CoUninitialize ();
return nullptr;
}
GstMMDeviceEnumerator *
gst_mm_device_enumerator_new (void)
{
GstMMDeviceEnumerator *self;
self = (GstMMDeviceEnumerator *) g_object_new (GST_TYPE_MM_DEVICE_ENUMERATOR,
nullptr);
if (!self->handle) {
gst_object_unref (self);
return nullptr;
}
gst_object_ref_sink (self);
return self;
}
IMMDeviceEnumerator *
gst_mm_device_enumerator_get_handle (GstMMDeviceEnumerator * enumerator)
{
g_return_val_if_fail (GST_IS_MM_DEVICE_ENUMERATOR (enumerator), nullptr);
return enumerator->handle;
}
typedef struct
{
GstMMDeviceEnumerator *self;
GstMMNotificationClientCallbacks *callbacks;
gpointer user_data;
gboolean handled;
GMutex lock;
GCond cond;
gboolean ret;
} SetNotificationCallbackData;
static gboolean
set_notification_callback (SetNotificationCallbackData * data)
{
GstMMDeviceEnumerator *self = data->self;
HRESULT hr;
g_mutex_lock (&data->lock);
g_mutex_lock (&self->lock);
data->ret = TRUE;
if (self->client) {
self->handle->UnregisterEndpointNotificationCallback (self->client);
self->client->Release ();
self->client = nullptr;
}
if (data->callbacks) {
IMMNotificationClient *client;
hr = GstIMMNotificationClient::CreateInstance (self, data->callbacks,
data->user_data, &client);
if (FAILED (hr)) {
GST_ERROR_OBJECT (self,
"Failed to create IMMNotificationClient instance");
data->ret = FALSE;
goto out;
}
hr = self->handle->RegisterEndpointNotificationCallback (client);
if (FAILED (hr)) {
GST_ERROR_OBJECT (self, "Failed to register callback");
client->Release ();
data->ret = FALSE;
goto out;
}
self->client = client;
}
out:
data->handled = TRUE;
g_cond_signal (&data->cond);
g_mutex_unlock (&self->lock);
g_mutex_unlock (&data->lock);
return G_SOURCE_REMOVE;
}
gboolean
gst_mm_device_enumerator_set_notification_callback (GstMMDeviceEnumerator *
enumerator, GstMMNotificationClientCallbacks * callbacks,
gpointer user_data)
{
SetNotificationCallbackData data;
gboolean ret;
g_return_val_if_fail (GST_IS_MM_DEVICE_ENUMERATOR (enumerator), FALSE);
data.self = enumerator;
data.callbacks = callbacks;
data.user_data = user_data;
data.handled = FALSE;
g_mutex_init (&data.lock);
g_cond_init (&data.cond);
g_main_context_invoke (enumerator->context,
(GSourceFunc) set_notification_callback, &data);
g_mutex_lock (&data.lock);
while (!data.handled)
g_cond_wait (&data.cond, &data.lock);
g_mutex_unlock (&data.lock);
ret = data.ret;
g_mutex_clear (&data.lock);
g_cond_clear (&data.cond);
return ret;
}
|