@@ -462,6 +462,8 @@ AudioDeviceWindowsCore::AudioDeviceWindowsCore()
462
462
_playChannelsPrioList[0 ] = 2 ; // stereo is prio 1
463
463
_playChannelsPrioList[1 ] = 1 ; // mono is prio 2
464
464
465
+ _deviceStateListener = new DeviceStateListener ();
466
+
465
467
HRESULT hr;
466
468
467
469
// We know that this API will work since it has already been verified in
@@ -475,6 +477,8 @@ AudioDeviceWindowsCore::AudioDeviceWindowsCore()
475
477
reinterpret_cast <void **>(&_ptrEnumerator));
476
478
RTC_DCHECK (_ptrEnumerator);
477
479
480
+ _ptrEnumerator->RegisterEndpointNotificationCallback (_deviceStateListener);
481
+
478
482
// DMO initialization for built-in WASAPI AEC.
479
483
{
480
484
IMediaObject* ptrDMO = NULL ;
@@ -500,6 +504,8 @@ AudioDeviceWindowsCore::~AudioDeviceWindowsCore() {
500
504
501
505
Terminate ();
502
506
507
+ _ptrEnumerator->UnregisterEndpointNotificationCallback (_deviceStateListener);
508
+
503
509
// The IMMDeviceEnumerator is created during construction. Must release
504
510
// it here and not in Terminate() since we don't recreate it in Init().
505
511
SAFE_RELEASE (_ptrEnumerator);
@@ -536,6 +542,11 @@ AudioDeviceWindowsCore::~AudioDeviceWindowsCore() {
536
542
_hShutdownCaptureEvent = NULL ;
537
543
}
538
544
545
+ if (NULL != _deviceStateListener) {
546
+ delete _deviceStateListener;
547
+ _deviceStateListener = NULL ;
548
+ }
549
+
539
550
if (_avrtLibrary) {
540
551
BOOL freeOK = FreeLibrary (_avrtLibrary);
541
552
if (!freeOK) {
@@ -3894,6 +3905,65 @@ int32_t AudioDeviceWindowsCore::_GetDeviceID(IMMDevice* pDevice,
3894
3905
return 0 ;
3895
3906
}
3896
3907
3908
+ int32_t AudioDeviceWindowsCore::SetAudioDeviceSink (AudioDeviceSink* sink) {
3909
+ _deviceStateListener->SetAudioDeviceSink (sink);
3910
+ return 0 ;
3911
+ }
3912
+
3913
+ void AudioDeviceWindowsCore::DeviceStateListener::SetAudioDeviceSink (AudioDeviceSink *sink) {
3914
+ callback_ = sink;
3915
+ }
3916
+
3917
+ HRESULT AudioDeviceWindowsCore::DeviceStateListener::OnDeviceStateChanged (LPCWSTR pwstrDeviceId, DWORD dwNewState) {
3918
+ RTC_DLOG (LS_INFO) << " AudioDeviceWindowsCore::OnDeviceStateChanged => " << pwstrDeviceId << " , NewState => " << dwNewState;
3919
+ if (callback_) callback_->OnDevicesUpdated ();
3920
+ return S_OK;
3921
+ }
3922
+
3923
+ HRESULT AudioDeviceWindowsCore::DeviceStateListener::OnDeviceAdded (LPCWSTR pwstrDeviceId) {
3924
+ RTC_DLOG (LS_INFO) << " AudioDeviceWindowsCore::OnDeviceAdded => " << pwstrDeviceId;
3925
+ return S_OK;
3926
+ }
3927
+
3928
+ HRESULT AudioDeviceWindowsCore::DeviceStateListener::OnDeviceRemoved (LPCWSTR pwstrDeviceId) {
3929
+ RTC_DLOG (LS_INFO) << " AudioDeviceWindowsCore::OnDeviceRemoved => " << pwstrDeviceId;
3930
+ return S_OK;
3931
+ }
3932
+
3933
+ HRESULT AudioDeviceWindowsCore::DeviceStateListener::OnDefaultDeviceChanged (EDataFlow flow, ERole role, LPCWSTR pwstrDefaultDeviceId) {
3934
+ RTC_DLOG (LS_INFO) << " AudioDeviceWindowsCore::OnDefaultDeviceChanged => " << pwstrDefaultDeviceId;
3935
+ return S_OK;
3936
+ }
3937
+
3938
+ HRESULT AudioDeviceWindowsCore::DeviceStateListener::OnPropertyValueChanged (LPCWSTR pwstrDeviceId, const PROPERTYKEY key) {
3939
+ // RTC_DLOG(LS_INFO) << "AudioDeviceWindowsCore::OnPropertyValueChanged => " << pwstrDeviceId;
3940
+ return S_OK;
3941
+ }
3942
+
3943
+ ULONG AudioDeviceWindowsCore::DeviceStateListener::AddRef () {
3944
+ ULONG new_ref = InterlockedIncrement (&ref_count_);
3945
+ // RTC_DLOG(LS_INFO) << "__AddRef => " << new_ref;
3946
+ return new_ref;
3947
+ }
3948
+
3949
+ ULONG AudioDeviceWindowsCore::DeviceStateListener::Release () {
3950
+ ULONG new_ref = InterlockedDecrement (&ref_count_);
3951
+ // RTC_DLOG(LS_INFO) << "__Release => " << new_ref;
3952
+ return new_ref;
3953
+ }
3954
+
3955
+ HRESULT AudioDeviceWindowsCore::DeviceStateListener::QueryInterface (REFIID iid, void ** object) {
3956
+ if (object == nullptr ) {
3957
+ return E_POINTER;
3958
+ }
3959
+ if (iid == IID_IUnknown || iid == __uuidof (IMMNotificationClient)) {
3960
+ *object = static_cast <IMMNotificationClient*>(this );
3961
+ return S_OK;
3962
+ }
3963
+ *object = nullptr ;
3964
+ return E_NOINTERFACE;
3965
+ }
3966
+
3897
3967
// ----------------------------------------------------------------------------
3898
3968
// _GetDefaultDevice
3899
3969
// ----------------------------------------------------------------------------
0 commit comments