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 12 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
36 changes: 15 additions & 21 deletions modules/audio_device/mac/audio_device_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -849,12 +849,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 @@ -867,12 +864,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 @@ -1665,7 +1659,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 @@ -1704,17 +1699,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 @@ -1723,7 +1718,6 @@ int32_t AudioDeviceMac::GetDeviceName(const AudioObjectPropertyScope scope,

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

return 0;
}
Expand Down
3 changes: 2 additions & 1 deletion modules/audio_device/mac/audio_device_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ class AudioDeviceMac : public AudioDeviceGeneric {

int32_t GetDeviceName(const AudioObjectPropertyScope scope,
const uint16_t index,
char* name);
char* name,
char* guid);

int32_t InitDevice(uint16_t userDeviceIndex,
AudioDeviceID& deviceId,
Expand Down
60 changes: 59 additions & 1 deletion sdk/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,51 @@ if (is_ios || is_mac) {
}
}

if (is_mac) {
rtc_library("native_api_audio_device_module") {
visibility = [ "*" ]

sources = [
"objc/native/api/audio_device_module.h",
"objc/native/api/audio_device_module.mm",
]

deps = [
":audio_device",
"../modules/audio_device:audio_device_api",
"../modules/audio_device:audio_device_generic",
"../rtc_base:checks",
"../rtc_base:rtc_base_approved",
"../system_wrappers",
]
}

rtc_library("audio_device") {
visibility = [ "*" ]

sources = [
"objc/native/src/audio/helpers.h",
"objc/native/src/audio/helpers.mm",
]

deps = [
":base_objc",
"../api:array_view",
"../modules/audio_device:audio_device_api",
"../modules/audio_device:audio_device_buffer",
"../modules/audio_device:audio_device_generic",
"../rtc_base",
"../rtc_base:checks",
"../rtc_base:threading",
"../system_wrappers:field_trial",
"../system_wrappers:metrics",
]
absl_deps = [ "//third_party/abseil-cpp/absl/base:core_headers" ]

frameworks = [ "AudioToolbox.framework" ]
}
}

rtc_library("videosource_objc") {
sources = [
"objc/api/peerconnection/RTCVideoSource+Private.h",
Expand Down Expand Up @@ -882,6 +927,13 @@ if (is_ios || is_mac) {
"..:no_global_constructors",
]
sources = [
"objc/api/peerconnection/RTCAudioDeviceModule.h",
"objc/api/peerconnection/RTCAudioDeviceModule+Private.h",
"objc/api/peerconnection/RTCAudioDeviceModule.mm",
"objc/api/peerconnection/RTCIODevice.h",
"objc/api/peerconnection/RTCIODevice.mm",
"objc/api/peerconnection/RTCAudioDevice.h",
"objc/api/peerconnection/RTCAudioDevice.mm",
"objc/api/peerconnection/RTCAudioSource+Private.h",
"objc/api/peerconnection/RTCAudioSource.h",
"objc/api/peerconnection/RTCAudioSource.mm",
Expand Down Expand Up @@ -1034,7 +1086,7 @@ if (is_ios || is_mac) {
"../system_wrappers:metrics",
]

if (is_ios) {
if (is_ios || is_mac) {
deps += [ ":native_api_audio_device_module" ]
}
}
Expand Down Expand Up @@ -1273,6 +1325,9 @@ if (is_ios || is_mac) {
"objc/helpers/RTCCameraPreviewView.h",
"objc/helpers/RTCDispatcher.h",
"objc/helpers/UIDevice+RTCDevice.h",
"objc/api/peerconnection/RTCAudioDeviceModule.h",
"objc/api/peerconnection/RTCAudioDevice.h",
"objc/api/peerconnection/RTCIODevice.h",
"objc/api/peerconnection/RTCAudioSource.h",
"objc/api/peerconnection/RTCAudioTrack.h",
"objc/api/peerconnection/RTCConfiguration.h",
Expand Down Expand Up @@ -1387,6 +1442,9 @@ if (is_ios || is_mac) {
output_name = "WebRTC"

sources = [
"objc/api/peerconnection/RTCAudioDeviceModule.h",
"objc/api/peerconnection/RTCAudioDevice.h",
"objc/api/peerconnection/RTCIODevice.h",
"objc/api/peerconnection/RTCAudioSource.h",
"objc/api/peerconnection/RTCAudioTrack.h",
"objc/api/peerconnection/RTCCertificate.h",
Expand Down
23 changes: 23 additions & 0 deletions sdk/objc/api/peerconnection/RTCAudioDevice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/

#import <Foundation/Foundation.h>

#import "RTCMacros.h"
#import "RTCIODevice.h"

NS_ASSUME_NONNULL_BEGIN

RTC_OBJC_EXPORT
@interface RTC_OBJC_TYPE(RTCAudioDevice) : RTC_OBJC_TYPE(RTCIODevice)

@end

NS_ASSUME_NONNULL_END
15 changes: 15 additions & 0 deletions sdk/objc/api/peerconnection/RTCAudioDevice.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/

#import "RTCAudioDevice.h"

@implementation RTCAudioDevice

@end
22 changes: 22 additions & 0 deletions sdk/objc/api/peerconnection/RTCAudioDeviceModule+Private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/

#import "RTCAudioDeviceModule.h"
#import "sdk/objc/native/api/audio_device_module.h"

NS_ASSUME_NONNULL_BEGIN

@interface RTCAudioDeviceModule ()

- (instancetype)initWithNativeModule:(rtc::scoped_refptr<webrtc::AudioDeviceModule> )module;

@end

NS_ASSUME_NONNULL_END
48 changes: 48 additions & 0 deletions sdk/objc/api/peerconnection/RTCAudioDeviceModule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/

#import <CoreMedia/CoreMedia.h>
#import <Foundation/Foundation.h>

#import "RTCMacros.h"
#import "RTCAudioDevice.h"

NS_ASSUME_NONNULL_BEGIN

RTC_OBJC_EXPORT
@interface RTC_OBJC_TYPE (RTCAudioDeviceModule) : NSObject

- (void)captureSampleBuffer:(CMSampleBufferRef)sampleBuffer;

@property(nonatomic, readonly) NSArray<RTC_OBJC_TYPE(RTCAudioDevice) *> *playoutDevices;
@property(nonatomic, readonly) NSArray<RTC_OBJC_TYPE(RTCAudioDevice) *> *recordingDevices;

@property(nonatomic, readonly) BOOL playing;
@property(nonatomic, readonly) BOOL recording;

// Executes low-level API's in sequence to switch the device
- (BOOL)switchPlayoutDevice: (nullable RTCAudioDevice *)device;
- (BOOL)switchRecordingDevice: (nullable RTCAudioDevice *)device;

// Low-level APIs
- (BOOL)setPlayoutDevice:(uint16_t) index;
- (BOOL)startPlayout;
- (BOOL)stopPlayout;
- (BOOL)initPlayout;

// Low-level APIs
- (BOOL)setRecordingDevice:(uint16_t) index;
- (BOOL)startRecording;
- (BOOL)stopRecording;
- (BOOL)initRecording;

@end

NS_ASSUME_NONNULL_END
Loading