@@ -461,6 +461,8 @@ AudioDeviceWindowsCore::AudioDeviceWindowsCore()
461
461
_playChannelsPrioList[0 ] = 2 ; // stereo is prio 1
462
462
_playChannelsPrioList[1 ] = 1 ; // mono is prio 2
463
463
464
+ _deviceStateListener = new DeviceStateListener ();
465
+
464
466
HRESULT hr;
465
467
466
468
// We know that this API will work since it has already been verified in
@@ -474,6 +476,8 @@ AudioDeviceWindowsCore::AudioDeviceWindowsCore()
474
476
reinterpret_cast <void **>(&_ptrEnumerator));
475
477
RTC_DCHECK (_ptrEnumerator);
476
478
479
+ _ptrEnumerator->RegisterEndpointNotificationCallback (_deviceStateListener);
480
+
477
481
// DMO initialization for built-in WASAPI AEC.
478
482
{
479
483
IMediaObject* ptrDMO = NULL ;
@@ -499,6 +503,8 @@ AudioDeviceWindowsCore::~AudioDeviceWindowsCore() {
499
503
500
504
Terminate ();
501
505
506
+ _ptrEnumerator->UnregisterEndpointNotificationCallback (_deviceStateListener);
507
+
502
508
// The IMMDeviceEnumerator is created during construction. Must release
503
509
// it here and not in Terminate() since we don't recreate it in Init().
504
510
SAFE_RELEASE (_ptrEnumerator);
@@ -535,6 +541,11 @@ AudioDeviceWindowsCore::~AudioDeviceWindowsCore() {
535
541
_hShutdownCaptureEvent = NULL ;
536
542
}
537
543
544
+ if (NULL != _deviceStateListener) {
545
+ delete _deviceStateListener;
546
+ _deviceStateListener = NULL ;
547
+ }
548
+
538
549
if (_avrtLibrary) {
539
550
BOOL freeOK = FreeLibrary (_avrtLibrary);
540
551
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