Skip to content

Commit 1305fc5

Browse files
cloudwebrtcpblazej
authored andcommitted
Audio Device Optimization
allow listen-only mode in AudioUnit, adjust when category changes (#2) release mic when category changes (#5) Change defaults to iOS defaults (#7) Sync audio session config (#8) feat: support bypass voice processing for iOS. (#15) Remove MacBookPro audio pan right code (#22) fix: Fix can't open mic alone when built-in AEC is enabled. (#29) feat: add audio device changes detect for windows. (#41) fix Linux compile (#47) AudioUnit: Don't rely on category switch for mic indicator to turn off (#52) Stop recording on mute (turn off mic indicator) (#55) Cherry pick audio selection from m97 release (#35) [Mac] Allow audio device selection (#21) RTCAudioDeviceModule.outputDevice / inputDevice getter and setter (#80) Allow custom audio processing by exposing AudioProcessingModule (#85) Expose audio sample buffers for Android (#89) feat: add external audio processor for android. (#103) android: make audio output attributes modifiable (#118) Fix external audio processor sample rate calculation (#108) Expose remote audio sample buffers on RTCAudioTrack (#84) Fix memory leak when creating audio CMSampleBuffer #86 Co-authored-by: Hiroshi Horie <548776+hiroshihorie@users.noreply.github.com> Co-authored-by: David Zhao <dz@livekit.io> Co-authored-by: davidliu <davidliu@deviange.net> (cherry picked from commit 7454824) # Conflicts: # audio/audio_state.cc # call/audio_state.h # media/engine/webrtc_voice_engine.h # modules/audio_device/audio_device_impl.cc # modules/audio_device/include/audio_device.h # modules/audio_device/mac/audio_device_mac.cc # modules/audio_device/mac/audio_device_mac.h # sdk/objc/api/peerconnection/RTCAudioTrack+Private.h # sdk/objc/api/peerconnection/RTCPeerConnectionFactory+Native.h # sdk/objc/api/peerconnection/RTCPeerConnectionFactory.mm # sdk/objc/api/peerconnection/RTCPeerConnectionFactoryBuilder.mm # sdk/objc/components/audio/RTCAudioSessionConfiguration.m # sdk/objc/native/src/audio/audio_device_ios.mm # sdk/objc/native/src/audio/audio_device_module_ios.mm # sdk/objc/native/src/audio/voice_processing_audio_unit.mm
1 parent 59a2d4f commit 1305fc5

File tree

70 files changed

+3032
-251
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+3032
-251
lines changed

api/audio/audio_device.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ namespace webrtc {
2222

2323
class AudioDeviceModuleForTest;
2424

25+
// Sink for callbacks related to a audio device.
26+
class AudioDeviceSink {
27+
public:
28+
virtual ~AudioDeviceSink() = default;
29+
30+
// input/output devices updated or default device changed
31+
virtual void OnDevicesUpdated() = 0;
32+
};
33+
2534
class AudioDeviceModule : public webrtc::RefCountInterface {
2635
public:
2736
enum AudioLayer {
@@ -62,11 +71,11 @@ class AudioDeviceModule : public webrtc::RefCountInterface {
6271
public:
6372
// Creates a default ADM for usage in production code.
6473
static scoped_refptr<AudioDeviceModule> Create(
65-
AudioLayer audio_layer, TaskQueueFactory* task_queue_factory);
74+
AudioLayer audio_layer, TaskQueueFactory* task_queue_factory, bool bypass_voice_processing = false);
6675
// Creates an ADM with support for extra test methods. Don't use this factory
6776
// in production code.
6877
static scoped_refptr<AudioDeviceModuleForTest> CreateForTest(
69-
AudioLayer audio_layer, TaskQueueFactory* task_queue_factory);
78+
AudioLayer audio_layer, TaskQueueFactory* task_queue_factory, bool bypass_voice_processing = false);
7079

7180
// Retrieve the currently utilized audio layer
7281
virtual int32_t ActiveAudioLayer(AudioLayer* audioLayer) const = 0;
@@ -176,6 +185,10 @@ class AudioDeviceModule : public webrtc::RefCountInterface {
176185
virtual int GetRecordAudioParameters(AudioParameters* params) const = 0;
177186
#endif // WEBRTC_IOS
178187

188+
virtual int32_t SetAudioDeviceSink(AudioDeviceSink* sink) const { return -1; }
189+
virtual int32_t GetPlayoutDevice() const { return -1; }
190+
virtual int32_t GetRecordingDevice() const { return -1; }
191+
179192
protected:
180193
~AudioDeviceModule() override {}
181194
};

audio/audio_send_stream.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,11 @@ void AudioSendStream::SetMuted(bool muted) {
400400
channel_send_->SetInputMute(muted);
401401
}
402402

403+
bool AudioSendStream::GetMuted() {
404+
RTC_DCHECK_RUN_ON(&worker_thread_checker_);
405+
return channel_send_->InputMute();
406+
}
407+
403408
webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const {
404409
return GetStats(true);
405410
}

audio/audio_send_stream.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class AudioSendStream final : public webrtc::AudioSendStream,
8888
int payload_frequency,
8989
int event,
9090
int duration_ms) override;
91+
bool GetMuted() override;
9192
void SetMuted(bool muted) override;
9293
webrtc::AudioSendStream::Stats GetStats() const override;
9394
webrtc::AudioSendStream::Stats GetStats(

audio/audio_state.cc

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,23 @@ void AudioState::AddSendingStream(webrtc::AudioSendStream* stream,
135135
UpdateAudioTransportWithSendingStreams();
136136

137137
// Make sure recording is initialized; start recording if enabled.
138-
auto* adm = config_.audio_device_module.get();
139-
if (recording_enabled_) {
138+
if (ShouldRecord()) {
139+
auto* adm = config_.audio_device_module.get();
140140
if (!adm->Recording()) {
141141
if (adm->InitRecording() == 0) {
142-
adm->StartRecording();
142+
if (recording_enabled_) {
143+
144+
// TODO: Verify if the following windows only logic is still required.
145+
#if defined(WEBRTC_WIN)
146+
if (adm->BuiltInAECIsAvailable() && !adm->Playing()) {
147+
if (!adm->PlayoutIsInitialized()) {
148+
adm->InitPlayout();
149+
}
150+
adm->StartPlayout();
151+
}
152+
#endif
153+
adm->StartRecording();
154+
}
143155
} else {
144156
RTC_DLOG_F(LS_ERROR) << "Failed to initialize recording.";
145157
}
@@ -152,7 +164,8 @@ void AudioState::RemoveSendingStream(webrtc::AudioSendStream* stream) {
152164
auto count = sending_streams_.erase(stream);
153165
RTC_DCHECK_EQ(1, count);
154166
UpdateAudioTransportWithSendingStreams();
155-
if (sending_streams_.empty()) {
167+
168+
if (!ShouldRecord()) {
156169
config_.audio_device_module->StopRecording();
157170
}
158171
}
@@ -208,6 +221,39 @@ void AudioState::UpdateNullAudioPollerState() {
208221
null_audio_poller_.Stop();
209222
}
210223
}
224+
225+
void AudioState::OnMuteStreamChanged() {
226+
227+
auto* adm = config_.audio_device_module.get();
228+
bool should_record = ShouldRecord();
229+
230+
if (should_record && !adm->Recording()) {
231+
if (adm->InitRecording() == 0) {
232+
adm->StartRecording();
233+
}
234+
} else if (!should_record && adm->Recording()) {
235+
adm->StopRecording();
236+
}
237+
}
238+
239+
bool AudioState::ShouldRecord() {
240+
// no streams to send
241+
if (sending_streams_.empty()) {
242+
return false;
243+
}
244+
245+
int stream_count = sending_streams_.size();
246+
247+
int muted_count = 0;
248+
for (const auto& kv : sending_streams_) {
249+
if (kv.first->GetMuted()) {
250+
muted_count++;
251+
}
252+
}
253+
254+
return muted_count != stream_count;
255+
}
256+
211257
} // namespace internal
212258

213259
scoped_refptr<AudioState> AudioState::Create(const AudioState::Config& config) {

audio/audio_state.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class AudioState : public webrtc::AudioState {
4747

4848
void SetStereoChannelSwapping(bool enable) override;
4949

50+
void OnMuteStreamChanged() override;
51+
5052
AudioDeviceModule* audio_device_module() {
5153
RTC_DCHECK(config_.audio_device_module);
5254
return config_.audio_device_module.get();
@@ -64,6 +66,9 @@ class AudioState : public webrtc::AudioState {
6466
void UpdateAudioTransportWithSendingStreams();
6567
void UpdateNullAudioPollerState() RTC_RUN_ON(&thread_checker_);
6668

69+
// Returns true when at least 1 stream exists and all streams are not muted.
70+
bool ShouldRecord();
71+
6772
SequenceChecker thread_checker_;
6873
SequenceChecker process_thread_checker_{SequenceChecker::kDetached};
6974
const webrtc::AudioState::Config config_;

audio/channel_send.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ class ChannelSend : public ChannelSendInterface,
163163
// Muting, Volume and Level.
164164
void SetInputMute(bool enable) override;
165165

166+
bool InputMute() const override;
167+
166168
// Stats.
167169
ANAStats GetANAStatistics() const override;
168170

@@ -235,8 +237,6 @@ class ChannelSend : public ChannelSendInterface,
235237
size_t payloadSize,
236238
int64_t absolute_capture_timestamp_ms) override;
237239

238-
bool InputMute() const;
239-
240240
int32_t SendRtpAudio(AudioFrameType frameType,
241241
uint8_t payloadType,
242242
uint32_t rtp_timestamp_without_offset,

audio/channel_send.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ class ChannelSendInterface {
8989
virtual bool SendTelephoneEventOutband(int event, int duration_ms) = 0;
9090
virtual void OnBitrateAllocation(BitrateAllocationUpdate update) = 0;
9191
virtual int GetTargetBitrate() const = 0;
92+
93+
virtual bool InputMute() const = 0;
9294
virtual void SetInputMute(bool muted) = 0;
9395

9496
virtual void ProcessAndEncodeAudio(

call/audio_send_stream.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ class AudioSendStream : public AudioSender {
193193
int event,
194194
int duration_ms) = 0;
195195

196+
virtual bool GetMuted() = 0;
196197
virtual void SetMuted(bool muted) = 0;
197198

198199
virtual Stats GetStats() const = 0;

call/audio_state.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class AudioState : public RefCountInterface {
5858

5959
virtual void SetStereoChannelSwapping(bool enable) = 0;
6060

61+
// Notify the AudioState that a stream updated it's mute state.
62+
virtual void OnMuteStreamChanged() = 0;
63+
6164
static scoped_refptr<AudioState> Create(const AudioState::Config& config);
6265

6366
~AudioState() override {}

modules/audio_device/audio_device_data_observer.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ class ADMWrapper : public AudioDeviceModule, public AudioTransport {
298298
}
299299
#endif // WEBRTC_IOS
300300

301+
int32_t SetAudioDeviceSink(AudioDeviceSink* sink) const override {
302+
return impl_->SetAudioDeviceSink(sink);
303+
}
304+
301305
protected:
302306
scoped_refptr<AudioDeviceModule> impl_;
303307
std::unique_ptr<AudioDeviceDataObserver> observer_;

0 commit comments

Comments
 (0)