Skip to content

Enable audio testability through RTCPeerConnectionFactory #6

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

Merged
merged 2 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 13 additions & 7 deletions sdk/objc/api/peerconnection/RTCPeerConnectionFactory+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

#import "RTCPeerConnectionFactory.h"
#import <AVFoundation/AVFoundation.h>

#include "api/peer_connection_interface.h"
#include "api/scoped_refptr.h"
Expand All @@ -18,13 +19,18 @@ NS_ASSUME_NONNULL_BEGIN
@interface RTC_OBJC_TYPE (RTCPeerConnectionFactory)
()

/**
* PeerConnectionFactoryInterface created and held by this
* RTCPeerConnectionFactory object. This is needed to pass to the underlying
* C++ APIs.
*/
@property(nonatomic,
readonly) rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> nativeFactory;


/**
* PeerConnectionFactoryInterface created and held by this
* RTCPeerConnectionFactory object. This is needed to pass to the underlying
* C++ APIs.
*/
@property(nonatomic,
readonly) rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> nativeFactory;

@property(nonatomic,
readonly) rtc::scoped_refptr<webrtc::AudioDeviceModule> internalAudioDeviceModule;

@end

Expand Down
6 changes: 5 additions & 1 deletion sdk/objc/api/peerconnection/RTCPeerConnectionFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

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

#import "RTCMacros.h"

Expand Down Expand Up @@ -91,6 +92,9 @@ RTC_OBJC_EXPORT
/* Stop an active AecDump recording */
- (void)stopAecDump;

- (void)initializeAudioTesting;
- (void)deliverRecordedDataWithNumFrames:(uint32_t)numFrames
ioData:(AudioBufferList*)ioData;
@end

NS_ASSUME_NONNULL_END
NS_ASSUME_NONNULL_END
17 changes: 17 additions & 0 deletions sdk/objc/api/peerconnection/RTCPeerConnectionFactory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ @implementation RTC_OBJC_TYPE (RTCPeerConnectionFactory) {
}

@synthesize nativeFactory = _nativeFactory;
@synthesize internalAudioDeviceModule = _audioDeviceModule;

- (void)initializeAudioTesting {
#if defined(WEBRTC_IOS)
auto adm = _audioDeviceModule.get();
static_cast<webrtc::ios_adm::AudioDeviceModuleIOS *>(adm)->InitializeAudioTesting();
#endif
}
- (void)deliverRecordedDataWithNumFrames:(uint32_t)numFrames
ioData:(AudioBufferList*)ioData {
#if defined(WEBRTC_IOS)
auto adm = _audioDeviceModule.get();
static_cast<webrtc::ios_adm::AudioDeviceModuleIOS *>(adm)->DeliverRecordedData(numFrames, ioData);
#endif
}

- (rtc::scoped_refptr<webrtc::AudioDeviceModule>)audioDeviceModule {
#if defined(WEBRTC_IOS)
Expand Down Expand Up @@ -175,6 +190,7 @@ - (instancetype)initWithNoMedia {
dependencies.worker_thread = _workerThread.get();
dependencies.signaling_thread = _signalingThread.get();
_nativeFactory = webrtc::CreateModularPeerConnectionFactory(std::move(dependencies));
_audioDeviceModule = nil;
NSAssert(_nativeFactory, @"Failed to initialize PeerConnectionFactory!");
}
return self;
Expand Down Expand Up @@ -265,6 +281,7 @@ - (instancetype)initWithNativeAudioEncoderFactory:
dependencies.media_transport_factory = std::move(mediaTransportFactory);
#endif
_nativeFactory = webrtc::CreateModularPeerConnectionFactory(std::move(dependencies));
_audioDeviceModule = audioDeviceModule;
NSAssert(_nativeFactory, @"Failed to initialize PeerConnectionFactory!");
}
return self;
Expand Down
4 changes: 4 additions & 0 deletions sdk/objc/native/src/audio/audio_device_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ class AudioDeviceIOS : public AudioDeviceGeneric,

bool IsInterrupted();

void InitializeAudioTesting();

void DeliverRecordedData(UInt32 num_frames,
AudioBufferList* io_data) const;
private:
// Called by the relevant AudioSessionObserver methods on |thread_|.
void HandleInterruptionBegin();
Expand Down
10 changes: 10 additions & 0 deletions sdk/objc/native/src/audio/audio_device_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1138,5 +1138,15 @@ static void LogDeviceInfo() {
audioSink_ = audioSink;
}

void AudioDeviceIOS::InitializeAudioTesting() {
audio_unit_->InitializeAudioTesting();
}

void AudioDeviceIOS::DeliverRecordedData(UInt32 num_frames,
AudioBufferList* io_data) const {
rtc::ArrayView<int16_t> recorded_audio(reinterpret_cast<int16_t*>(io_data->mBuffers[0].mData), num_frames);
fine_audio_buffer_->DeliverRecordedData(recorded_audio, kFixedRecordDelayEstimate);
}

} // namespace ios_adm
} // namespace webrtc
3 changes: 3 additions & 0 deletions sdk/objc/native/src/audio/audio_device_module_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ class AudioDeviceModuleIOS : public AudioDeviceModule {
int32_t EnableBuiltInNS(bool enable) override;

int32_t GetPlayoutUnderrunCount() const override;
void InitializeAudioTesting() const;
void DeliverRecordedData(UInt32 num_frames,
AudioBufferList* io_data) const;
#if defined(WEBRTC_IOS)
int GetPlayoutAudioParameters(AudioParameters* params) const override;
int GetRecordAudioParameters(AudioParameters* params) const override;
Expand Down
8 changes: 8 additions & 0 deletions sdk/objc/native/src/audio/audio_device_module_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,14 @@
return ok;
}

void AudioDeviceModuleIOS::InitializeAudioTesting() const {
audio_device_->InitializeAudioTesting();
}

void AudioDeviceModuleIOS::DeliverRecordedData(UInt32 num_frames,
AudioBufferList* io_data) const {
audio_device_->DeliverRecordedData(num_frames, io_data);
}
#if defined(WEBRTC_IOS)
int AudioDeviceModuleIOS::GetPlayoutAudioParameters(
AudioParameters* params) const {
Expand Down
4 changes: 4 additions & 0 deletions sdk/objc/native/src/audio/voice_processing_audio_unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ class VoiceProcessingAudioUnit {
UInt32 num_frames,
AudioBufferList* io_data);

void InitializeAudioTesting();

static bool vpio_enabled;

private:
// The C API used to set callbacks requires static functions. When these are
// called, they will invoke the relevant instance method by casting
Expand Down
13 changes: 12 additions & 1 deletion sdk/objc/native/src/audio/voice_processing_audio_unit.mm
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ static OSStatus GetAGCState(AudioUnit audio_unit, UInt32* enabled) {
DisposeAudioUnit();
}

bool VoiceProcessingAudioUnit::vpio_enabled = true;
const UInt32 VoiceProcessingAudioUnit::kBytesPerSample = 2;

bool VoiceProcessingAudioUnit::Init() {
Expand Down Expand Up @@ -395,8 +396,13 @@ static OSStatus GetAGCState(AudioUnit audio_unit, UInt32* enabled) {
AudioBufferList* io_data) {
VoiceProcessingAudioUnit* audio_unit =
static_cast<VoiceProcessingAudioUnit*>(in_ref_con);
return audio_unit->NotifyDeliverRecordedData(flags, time_stamp, bus_number,

if (VoiceProcessingAudioUnit::vpio_enabled) {
return audio_unit->NotifyDeliverRecordedData(flags, time_stamp, bus_number,
num_frames, io_data);
} else {
return noErr;
}
}

OSStatus VoiceProcessingAudioUnit::NotifyGetPlayoutData(
Expand Down Expand Up @@ -466,5 +472,10 @@ static OSStatus GetAGCState(AudioUnit audio_unit, UInt32* enabled) {
}
}

void VoiceProcessingAudioUnit::InitializeAudioTesting() {
VoiceProcessingAudioUnit::vpio_enabled = false;
RTCLog(@"Initializing Audio Testing...Removing Audio Unit Input Callback.");
}

} // namespace ios_adm
} // namespace webrtc
2 changes: 1 addition & 1 deletion tools_webrtc/ios/build_ios_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _ParseArgs():
'If specified together with -c, deletes the dir.')
parser.add_argument('-r', '--revision', type=int, default=0,
help='Specifies a revision number to embed if building the framework.')
parser.add_argument('-e', '--bitcode', action='store_true', default=True,
parser.add_argument('-e', '--bitcode', action='store_true', default=False,
Copy link

@x-0o0 x-0o0 Oct 15, 2020

Choose a reason for hiding this comment

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

Is this only for test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Default option is False, but it was changed in the main branch. Because we don't really need bitcode for debugging or testing, I've turned off the default value.

help='Compile with bitcode.')
parser.add_argument('--verbose', action='store_true', default=False,
help='Debug logging.')
Expand Down