Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mac] Allow audio device selection #21

Merged
merged 25 commits into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions modules/audio_device/audio_device_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,17 @@ namespace webrtc {

rtc::scoped_refptr<AudioDeviceModule> AudioDeviceModule::Create(
AudioLayer audio_layer,
TaskQueueFactory* task_queue_factory) {
TaskQueueFactory* task_queue_factory,
bool bypass_voice_processing) {
RTC_DLOG(LS_INFO) << __FUNCTION__;
return AudioDeviceModule::CreateForTest(audio_layer, task_queue_factory);
return AudioDeviceModule::CreateForTest(audio_layer, task_queue_factory, bypass_voice_processing);
}

// static
rtc::scoped_refptr<AudioDeviceModuleForTest> AudioDeviceModule::CreateForTest(
AudioLayer audio_layer,
TaskQueueFactory* task_queue_factory) {
TaskQueueFactory* task_queue_factory,
bool bypass_voice_processing) {
RTC_DLOG(LS_INFO) << __FUNCTION__;

// The "AudioDeviceModule::kWindowsCoreAudio2" audio layer has its own
Expand All @@ -93,7 +95,7 @@ rtc::scoped_refptr<AudioDeviceModuleForTest> AudioDeviceModule::CreateForTest(

// Create the generic reference counted (platform independent) implementation.
auto audio_device = rtc::make_ref_counted<AudioDeviceModuleImpl>(
audio_layer, task_queue_factory);
audio_layer, task_queue_factory, bypass_voice_processing);

// Ensure that the current platform is supported.
if (audio_device->CheckPlatform() == -1) {
Expand All @@ -116,8 +118,13 @@ rtc::scoped_refptr<AudioDeviceModuleForTest> AudioDeviceModule::CreateForTest(

AudioDeviceModuleImpl::AudioDeviceModuleImpl(
AudioLayer audio_layer,
TaskQueueFactory* task_queue_factory)
: audio_layer_(audio_layer), audio_device_buffer_(task_queue_factory) {
TaskQueueFactory* task_queue_factory,
bool bypass_voice_processing)
: audio_layer_(audio_layer),
#if defined(WEBRTC_IOS)
bypass_voice_processing_(bypass_voice_processing),
#endif
audio_device_buffer_(task_queue_factory) {
RTC_DLOG(LS_INFO) << __FUNCTION__;
}

Expand Down Expand Up @@ -280,7 +287,7 @@ int32_t AudioDeviceModuleImpl::CreatePlatformSpecificObjects() {
#if defined(WEBRTC_IOS)
if (audio_layer == kPlatformDefaultAudio) {
audio_device_.reset(
new ios_adm::AudioDeviceIOS(/*bypass_voice_processing=*/false));
new ios_adm::AudioDeviceIOS(/*bypass_voice_processing=*/bypass_voice_processing_));
RTC_LOG(LS_INFO) << "iPhone Audio APIs will be utilized.";
}
// END #if defined(WEBRTC_IOS)
Expand Down
7 changes: 5 additions & 2 deletions modules/audio_device/audio_device_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class AudioDeviceModuleImpl : public AudioDeviceModuleForTest {
int32_t AttachAudioBuffer();

AudioDeviceModuleImpl(AudioLayer audio_layer,
TaskQueueFactory* task_queue_factory);
TaskQueueFactory* task_queue_factory,
bool bypass_voice_processing = false);
~AudioDeviceModuleImpl() override;

// Retrieve the currently utilized audio layer
Expand Down Expand Up @@ -165,7 +166,9 @@ class AudioDeviceModuleImpl : public AudioDeviceModuleForTest {
AudioLayer audio_layer_;
PlatformType platform_type_ = kPlatformNotSupported;
bool initialized_ = false;
#if defined(WEBRTC_ANDROID)
#if defined(WEBRTC_IOS)
bool bypass_voice_processing_;
#elif defined(WEBRTC_ANDROID)
// Should be declared first to ensure that it outlives other resources.
std::unique_ptr<AudioManager> audio_manager_android_;
#endif
Expand Down
6 changes: 4 additions & 2 deletions modules/audio_device/include/audio_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ class AudioDeviceModule : public rtc::RefCountInterface {
// Creates a default ADM for usage in production code.
static rtc::scoped_refptr<AudioDeviceModule> Create(
AudioLayer audio_layer,
TaskQueueFactory* task_queue_factory);
TaskQueueFactory* task_queue_factory,
bool bypass_voice_processing = false);
// Creates an ADM with support for extra test methods. Don't use this factory
// in production code.
static rtc::scoped_refptr<AudioDeviceModuleForTest> CreateForTest(
AudioLayer audio_layer,
TaskQueueFactory* task_queue_factory);
TaskQueueFactory* task_queue_factory,
bool bypass_voice_processing = false);

// Retrieve the currently utilized audio layer
virtual int32_t ActiveAudioLayer(AudioLayer* audioLayer) const = 0;
Expand Down
184 changes: 147 additions & 37 deletions modules/audio_device/mac/audio_device_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,11 @@ AudioDeviceGeneric::InitStatus AudioDeviceMac::Init() {
// but now must be explicitly specified. HAL would otherwise try to use the
// main thread to issue notifications.
AudioObjectPropertyAddress propertyAddress = {
kAudioHardwarePropertyRunLoop, kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster};
kAudioHardwarePropertyRunLoop,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};

CFRunLoopRef runLoop = NULL;
UInt32 size = sizeof(CFRunLoopRef);
int aoerr = AudioObjectSetPropertyData(
Expand All @@ -311,6 +314,16 @@ AudioDeviceGeneric::InitStatus AudioDeviceMac::Init() {
WEBRTC_CA_LOG_ERR(AudioObjectAddPropertyListener(
kAudioObjectSystemObject, &propertyAddress, &objectListenerProc, this));

// Listen for default output device change.
propertyAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
WEBRTC_CA_LOG_ERR(AudioObjectAddPropertyListener(
kAudioObjectSystemObject, &propertyAddress, &objectListenerProc, this));

// Listen for default input device change.
propertyAddress.mSelector = kAudioHardwarePropertyDefaultInputDevice;
WEBRTC_CA_LOG_ERR(AudioObjectAddPropertyListener(
kAudioObjectSystemObject, &propertyAddress, &objectListenerProc, this));

_initialized = true;

return InitStatus::OK;
Expand All @@ -337,9 +350,21 @@ int32_t AudioDeviceMac::Terminate() {
OSStatus err = noErr;
int retVal = 0;

// Remove listeners for global scope.
AudioObjectPropertyAddress propertyAddress = {
kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster};
kAudioHardwarePropertyDevices, // selector
kAudioObjectPropertyScopeGlobal, // scope
kAudioObjectPropertyElementMaster // element
};

WEBRTC_CA_LOG_WARN(AudioObjectRemovePropertyListener(
kAudioObjectSystemObject, &propertyAddress, &objectListenerProc, this));

propertyAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
WEBRTC_CA_LOG_WARN(AudioObjectRemovePropertyListener(
kAudioObjectSystemObject, &propertyAddress, &objectListenerProc, this));

propertyAddress.mSelector = kAudioHardwarePropertyDefaultInputDevice;
WEBRTC_CA_LOG_WARN(AudioObjectRemovePropertyListener(
kAudioObjectSystemObject, &propertyAddress, &objectListenerProc, this));

Expand Down Expand Up @@ -830,12 +855,9 @@ int32_t AudioDeviceMac::PlayoutDeviceName(uint16_t index,
}

memset(name, 0, kAdmMaxDeviceNameSize);
memset(guid, 0, kAdmMaxGuidSize);

if (guid != NULL) {
memset(guid, 0, kAdmMaxGuidSize);
}

return GetDeviceName(kAudioDevicePropertyScopeOutput, index, name);
return GetDeviceName(kAudioDevicePropertyScopeOutput, index, name, guid);
}

int32_t AudioDeviceMac::RecordingDeviceName(uint16_t index,
Expand All @@ -848,12 +870,9 @@ int32_t AudioDeviceMac::RecordingDeviceName(uint16_t index,
}

memset(name, 0, kAdmMaxDeviceNameSize);

if (guid != NULL) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can anything else call this function and pass in NULL for RecordingDeviceName ?

Copy link
Member Author

@hiroshihorie hiroshihorie Mar 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RecordingDeviceName is supposed to populate name and guid of the device,
but populating guid was not implemented in their code.

So I implemented to return the AudioDeviceID which is not technically guid but
it works for identifying the device.

memset(guid, 0, kAdmMaxGuidSize);
}

return GetDeviceName(kAudioDevicePropertyScopeInput, index, name);
memset(guid, 0, kAdmMaxGuidSize);

return GetDeviceName(kAudioDevicePropertyScopeInput, index, name, guid);
}

int16_t AudioDeviceMac::RecordingDevices() {
Expand Down Expand Up @@ -1269,7 +1288,7 @@ int32_t AudioDeviceMac::StartRecording() {
while (CaptureWorkerThread()) {
}
},
"CaptureWorkerThread",
"Audio_CaptureWorkerThread",
rtc::ThreadAttributes().SetPriority(rtc::ThreadPriority::kRealtime));

OSStatus err = noErr;
Expand Down Expand Up @@ -1362,7 +1381,11 @@ int32_t AudioDeviceMac::StopRecording() {

// Remove listeners.
AudioObjectPropertyAddress propertyAddress = {
kAudioDevicePropertyStreamFormat, kAudioDevicePropertyScopeInput, 0};
kAudioDevicePropertyStreamFormat, // selector
kAudioDevicePropertyScopeInput, // scope
0, // element
};

WEBRTC_CA_LOG_WARN(AudioObjectRemovePropertyListener(
_inputDeviceID, &propertyAddress, &objectListenerProc, this));

Expand Down Expand Up @@ -1406,7 +1429,7 @@ int32_t AudioDeviceMac::StartPlayout() {
while (RenderWorkerThread()) {
}
},
"RenderWorkerThread",
"Audio_RenderWorkerThread",
rtc::ThreadAttributes().SetPriority(rtc::ThreadPriority::kRealtime));

if (_twoDevices || !_recording) {
Expand Down Expand Up @@ -1475,7 +1498,11 @@ int32_t AudioDeviceMac::StopPlayout() {

// Remove listeners.
AudioObjectPropertyAddress propertyAddress = {
kAudioDevicePropertyStreamFormat, kAudioDevicePropertyScopeOutput, 0};
kAudioDevicePropertyStreamFormat, // selector
kAudioDevicePropertyScopeOutput, // scope
0, // element
};

WEBRTC_CA_LOG_WARN(AudioObjectRemovePropertyListener(
_outputDeviceID, &propertyAddress, &objectListenerProc, this));

Expand Down Expand Up @@ -1510,8 +1537,11 @@ int32_t AudioDeviceMac::GetNumberDevices(const AudioObjectPropertyScope scope,
OSStatus err = noErr;

AudioObjectPropertyAddress propertyAddress = {
kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster};
kAudioHardwarePropertyDevices,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster,
};

UInt32 size = 0;
WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyDataSize(
kAudioObjectSystemObject, &propertyAddress, 0, NULL, &size));
Expand Down Expand Up @@ -1610,7 +1640,8 @@ int32_t AudioDeviceMac::GetNumberDevices(const AudioObjectPropertyScope scope,

int32_t AudioDeviceMac::GetDeviceName(const AudioObjectPropertyScope scope,
const uint16_t index,
char* name) {
char* name,
char* guid) {
OSStatus err = noErr;
UInt32 len = kAdmMaxDeviceNameSize;
AudioDeviceID deviceIds[MaxNumberDevices];
Expand Down Expand Up @@ -1649,17 +1680,17 @@ int32_t AudioDeviceMac::GetDeviceName(const AudioObjectPropertyScope scope,
}
}

AudioObjectPropertyAddress propertyAddress = {kAudioDevicePropertyDeviceName,
scope, 0};

std::string strData;
if (isDefaultDevice) {
char devName[len];
strData = "default";
} else {
strData = std::to_string(deviceIds[index]);
}
strcpy(guid, strData.c_str());

WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData(usedID, &propertyAddress,
0, NULL, &len, devName));
AudioObjectPropertyAddress propertyAddress = {kAudioDevicePropertyDeviceName,
scope, 0};

sprintf(name, "default (%s)", devName);
} else {
if (index < numberDevices) {
usedID = deviceIds[index];
} else {
Expand All @@ -1668,7 +1699,6 @@ int32_t AudioDeviceMac::GetDeviceName(const AudioObjectPropertyScope scope,

WEBRTC_CA_RETURN_ON_ERR(AudioObjectGetPropertyData(usedID, &propertyAddress,
0, NULL, &len, name));
}

return 0;
}
Expand Down Expand Up @@ -1876,6 +1906,56 @@ OSStatus AudioDeviceMac::implObjectListenerProc(
HandleDataSourceChange(objectId, addresses[i]);
} else if (addresses[i].mSelector == kAudioDeviceProcessorOverload) {
HandleProcessorOverload(addresses[i]);
} else if (addresses[i].mSelector == kAudioHardwarePropertyDefaultOutputDevice) {
RTC_LOG(LS_VERBOSE) << "kAudioHardwarePropertyDefaultOutputDevice";
// default audio output device changed
HandleDefaultOutputDeviceChange();
} else if (addresses[i].mSelector == kAudioHardwarePropertyDefaultInputDevice) {
RTC_LOG(LS_VERBOSE) << "kAudioHardwarePropertyDefaultInputDevice";
// default audio input device changed
HandleDefaultInputDeviceChange();
}
}

return 0;
}

int32_t AudioDeviceMac::HandleDefaultOutputDeviceChange() {

if (SpeakerIsInitialized()) {
RTC_LOG(LS_WARNING) << "Default audio output device has changed";
int32_t renderDeviceIsAlive = AtomicGet32(&_renderDeviceIsAlive);
bool wasPlaying = _playing && renderDeviceIsAlive == 1;

if (wasPlaying && _outputDeviceIsSpecified && _outputDeviceIndex == 0) {

StopPlayout();

// default is already selected _outputDeviceIndex(0)
// re-init and start playout
InitPlayout();
StartPlayout();
}
}

return 0;
}

int32_t AudioDeviceMac::HandleDefaultInputDeviceChange() {

if (MicrophoneIsInitialized()) {
RTC_LOG(LS_WARNING) << "Default audio input device has changed";
int32_t captureDeviceIsAlive = AtomicGet32(&_captureDeviceIsAlive);
bool wasRecording = _recording && captureDeviceIsAlive == 1;

if (wasRecording && _inputDeviceIsSpecified && _inputDeviceIndex == 0) {

StopRecording();

// default is already selected _inputDeviceIndex(0)
// re-init and start recording
InitRecording();
StartRecording();
}
}

Expand All @@ -1898,9 +1978,24 @@ int32_t AudioDeviceMac::HandleDeviceChange() {
&size, &deviceIsAlive);

if (err == kAudioHardwareBadDeviceError || deviceIsAlive == 0) {
RTC_LOG(LS_WARNING) << "Capture device is not alive (probably removed)";
AtomicSet32(&_captureDeviceIsAlive, 0);
_mixerManager.CloseMicrophone();
RTC_LOG(LS_WARNING) << "Audio input device is not alive (probably removed) deviceID: " << _inputDeviceID;
//AtomicSet32(&_captureDeviceIsAlive, 0);

// Logic to switch to default device (if exists)
// when the current device is not alive anymore
int32_t captureDeviceIsAlive = AtomicGet32(&_captureDeviceIsAlive);
bool wasRecording = _recording && captureDeviceIsAlive == 1;

StopRecording();

// was playing & default device exists
if (wasRecording && SetRecordingDevice(0) == 0) {
InitRecording();
StartRecording();
} else {
_mixerManager.CloseMicrophone();
}

} else if (err != noErr) {
logCAMsg(rtc::LS_ERROR, "Error in AudioDeviceGetProperty()",
(const char*)&err);
Expand All @@ -1917,9 +2012,24 @@ int32_t AudioDeviceMac::HandleDeviceChange() {
&size, &deviceIsAlive);

if (err == kAudioHardwareBadDeviceError || deviceIsAlive == 0) {
RTC_LOG(LS_WARNING) << "Render device is not alive (probably removed)";
AtomicSet32(&_renderDeviceIsAlive, 0);
_mixerManager.CloseSpeaker();
RTC_LOG(LS_WARNING) << "Audio output device is not alive (probably removed) deviceID: " << _outputDeviceID;
// AtomicSet32(&_renderDeviceIsAlive, 0); // StopPlayout() does this

// Logic to switch to default device (if exists)
// when the current device is not alive anymore
int32_t renderDeviceIsAlive = AtomicGet32(&_renderDeviceIsAlive);
bool wasPlaying = _playing && renderDeviceIsAlive == 1;

StopPlayout();

// was playing & default device exists
if (wasPlaying && SetPlayoutDevice(0) == 0) {
InitPlayout();
StartPlayout();
} else {
_mixerManager.CloseSpeaker();
}

} else if (err != noErr) {
logCAMsg(rtc::LS_ERROR, "Error in AudioDeviceGetProperty()",
(const char*)&err);
Expand Down
Loading