wasapi-notify.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "wasapi-notify.hpp"
  2. #include <windows.h>
  3. #include <assert.h>
  4. #include <util/threading.h>
  5. class NotificationClient : public IMMNotificationClient {
  6. volatile long refs = 1;
  7. WASAPINotifyDefaultDeviceChangedCallback cb;
  8. public:
  9. NotificationClient(WASAPINotifyDefaultDeviceChangedCallback cb) : cb(cb) { assert(cb); }
  10. STDMETHODIMP_(ULONG) AddRef() { return (ULONG)os_atomic_inc_long(&refs); }
  11. STDMETHODIMP_(ULONG) STDMETHODCALLTYPE Release()
  12. {
  13. long val = os_atomic_dec_long(&refs);
  14. if (val == 0)
  15. delete this;
  16. return (ULONG)val;
  17. }
  18. STDMETHODIMP QueryInterface(REFIID riid, void **ptr)
  19. {
  20. if (riid == IID_IUnknown) {
  21. *ptr = (IUnknown *)this;
  22. } else if (riid == __uuidof(IMMNotificationClient)) {
  23. *ptr = (IMMNotificationClient *)this;
  24. } else {
  25. *ptr = nullptr;
  26. return E_NOINTERFACE;
  27. }
  28. InterlockedIncrement(&refs);
  29. return S_OK;
  30. }
  31. STDMETHODIMP OnDeviceAdded(LPCWSTR) { return S_OK; }
  32. STDMETHODIMP OnDeviceRemoved(LPCWSTR) { return S_OK; }
  33. STDMETHODIMP OnDeviceStateChanged(LPCWSTR, DWORD) { return S_OK; }
  34. STDMETHODIMP OnPropertyValueChanged(LPCWSTR, const PROPERTYKEY) { return S_OK; }
  35. STDMETHODIMP OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR id)
  36. {
  37. if (cb && id)
  38. cb(flow, role, id);
  39. return S_OK;
  40. }
  41. };
  42. WASAPINotify::WASAPINotify()
  43. {
  44. HRESULT res = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),
  45. (LPVOID *)enumerator.Assign());
  46. if (SUCCEEDED(res)) {
  47. notificationClient = new NotificationClient(std::bind(&WASAPINotify::OnDefaultDeviceChanged, this,
  48. std::placeholders::_1, std::placeholders::_2,
  49. std::placeholders::_3));
  50. enumerator->RegisterEndpointNotificationCallback(notificationClient);
  51. } else {
  52. enumerator.Clear();
  53. }
  54. }
  55. WASAPINotify::~WASAPINotify()
  56. {
  57. if (enumerator) {
  58. enumerator->UnregisterEndpointNotificationCallback(notificationClient);
  59. enumerator.Clear();
  60. }
  61. notificationClient.Clear();
  62. }
  63. void WASAPINotify::AddDefaultDeviceChangedCallback(void *handle, WASAPINotifyDefaultDeviceChangedCallback cb)
  64. {
  65. if (!handle)
  66. return;
  67. std::lock_guard<std::mutex> l(mutex);
  68. defaultDeviceChangedCallbacks[handle] = cb;
  69. }
  70. void WASAPINotify::RemoveDefaultDeviceChangedCallback(void *handle)
  71. {
  72. if (!handle)
  73. return;
  74. std::lock_guard<std::mutex> l(mutex);
  75. defaultDeviceChangedCallbacks.erase(handle);
  76. }
  77. void WASAPINotify::OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR id)
  78. {
  79. std::lock_guard<std::mutex> l(mutex);
  80. for (const auto &cb : defaultDeviceChangedCallbacks)
  81. cb.second(flow, role, id);
  82. }