Skip to content

Commit 4237b27

Browse files
committed
Audio Device Optimization (#29)
* 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) Co-authored-by: Hiroshi Horie <[email protected]> Co-authored-by: David Zhao <[email protected]> * fix compilation errors --------- Co-authored-by: CloudWebRTC <[email protected]> Co-authored-by: Hiroshi Horie <[email protected]> Co-authored-by: David Zhao <[email protected]> # 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/RTCPeerConnectionFactory+Native.h # sdk/objc/api/peerconnection/RTCPeerConnectionFactory.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 6230b3c commit 4237b27

Some content is hidden

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

42 files changed

+1212
-209
lines changed

api/audio/audio_device.h

Lines changed: 17 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,13 @@ 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,
75+
bool bypass_voice_processing = false);
6676
// Creates an ADM with support for extra test methods. Don't use this factory
6777
// in production code.
6878
static scoped_refptr<AudioDeviceModuleForTest> CreateForTest(
69-
AudioLayer audio_layer, TaskQueueFactory* task_queue_factory);
79+
AudioLayer audio_layer, TaskQueueFactory* task_queue_factory,
80+
bool bypass_voice_processing = false);
7081

7182
// Retrieve the currently utilized audio layer
7283
virtual int32_t ActiveAudioLayer(AudioLayer* audioLayer) const = 0;
@@ -176,6 +187,10 @@ class AudioDeviceModule : public webrtc::RefCountInterface {
176187
virtual int GetRecordAudioParameters(AudioParameters* params) const = 0;
177188
#endif // WEBRTC_IOS
178189

190+
virtual int32_t SetAudioDeviceSink(AudioDeviceSink* sink) const { return -1; }
191+
virtual int32_t GetPlayoutDevice() const { return -1; }
192+
virtual int32_t GetRecordingDevice() const { return -1; }
193+
179194
protected:
180195
~AudioDeviceModule() override {}
181196
};

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: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,25 @@ void AudioState::SetPlayout(bool enabled) {
7171
UpdateNullAudioPollerState();
7272
}
7373

74+
void AudioState::SetRecording(bool enabled) {
75+
RTC_LOG(LS_INFO) << "SetRecording(" << enabled << ")";
76+
RTC_DCHECK_RUN_ON(&thread_checker_);
77+
auto* adm = config_.audio_device_module.get();
78+
if (enabled) {
79+
if (ShouldRecord()) {
80+
if (!adm->Recording()) {
81+
if (adm->InitRecording() == 0) {
82+
adm->StartRecording();
83+
}
84+
}
85+
}
86+
} else {
87+
// Disable recording.
88+
adm->StopRecording();
89+
}
90+
recording_enabled_ = enabled;
91+
}
92+
7493
void AudioState::AddReceivingStream(
7594
webrtc::AudioReceiveStreamInterface* stream) {
7695
RTC_DCHECK_RUN_ON(&thread_checker_);
@@ -106,25 +125,6 @@ void AudioState::RemoveReceivingStream(
106125
UpdateNullAudioPollerState();
107126
}
108127

109-
void AudioState::SetRecording(bool enabled) {
110-
RTC_LOG(LS_INFO) << "SetRecording(" << enabled << ")";
111-
RTC_DCHECK_RUN_ON(&thread_checker_);
112-
auto* adm = config_.audio_device_module.get();
113-
if (enabled) {
114-
if (!sending_streams_.empty()) {
115-
if (!adm->Recording()) {
116-
if (adm->InitRecording() == 0) {
117-
adm->StartRecording();
118-
}
119-
}
120-
}
121-
} else {
122-
// Disable recording.
123-
adm->StopRecording();
124-
}
125-
recording_enabled_ = enabled;
126-
}
127-
128128
void AudioState::AddSendingStream(webrtc::AudioSendStream* stream,
129129
int sample_rate_hz,
130130
size_t num_channels) {
@@ -135,13 +135,13 @@ 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_) {
140-
if (!adm->Recording()) {
141-
if (adm->InitRecording() == 0) {
142-
adm->StartRecording();
143-
} else {
144-
RTC_DLOG_F(LS_ERROR) << "Failed to initialize recording.";
138+
if (ShouldRecord()) {
139+
auto* adm = config_.audio_device_module.get();
140+
if (recording_enabled_) {
141+
if (!adm->Recording()) {
142+
if (adm->InitRecording() == 0) {
143+
adm->StartRecording();
144+
}
145145
}
146146
}
147147
}
@@ -152,7 +152,8 @@ void AudioState::RemoveSendingStream(webrtc::AudioSendStream* stream) {
152152
auto count = sending_streams_.erase(stream);
153153
RTC_DCHECK_EQ(1, count);
154154
UpdateAudioTransportWithSendingStreams();
155-
if (sending_streams_.empty()) {
155+
156+
if (!ShouldRecord()) {
156157
config_.audio_device_module->StopRecording();
157158
}
158159
}
@@ -208,6 +209,39 @@ void AudioState::UpdateNullAudioPollerState() {
208209
null_audio_poller_.Stop();
209210
}
210211
}
212+
213+
void AudioState::OnMuteStreamChanged() {
214+
215+
auto* adm = config_.audio_device_module.get();
216+
bool should_record = ShouldRecord();
217+
218+
if (should_record && !adm->Recording()) {
219+
if (adm->InitRecording() == 0) {
220+
adm->StartRecording();
221+
}
222+
} else if (!should_record && adm->Recording()) {
223+
adm->StopRecording();
224+
}
225+
}
226+
227+
bool AudioState::ShouldRecord() {
228+
// no streams to send
229+
if (sending_streams_.empty()) {
230+
return false;
231+
}
232+
233+
int stream_count = sending_streams_.size();
234+
235+
int muted_count = 0;
236+
for (const auto& kv : sending_streams_) {
237+
if (kv.first->GetMuted()) {
238+
muted_count++;
239+
}
240+
}
241+
242+
return muted_count != stream_count;
243+
}
244+
211245
} // namespace internal
212246

213247
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ class ChannelSend : public ChannelSendInterface,
235235
size_t payloadSize,
236236
int64_t absolute_capture_timestamp_ms) override;
237237

238-
bool InputMute() const;
238+
bool InputMute() const override;
239239

240240
int32_t SendRtpAudio(AudioFrameType frameType,
241241
uint8_t payloadType,

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(

audio/mock_voe_channel_proxy.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ class MockChannelSend : public voe::ChannelSendInterface {
173173
OnBitrateAllocation,
174174
(BitrateAllocationUpdate update),
175175
(override));
176+
MOCK_METHOD(bool, InputMute, (), (const, override));
176177
MOCK_METHOD(void, SetInputMute, (bool muted), (override));
177178
MOCK_METHOD(void,
178179
ReceivedRTCPPacket,

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
@@ -59,6 +59,9 @@ class AudioState : public RefCountInterface {
5959
virtual void SetStereoChannelSwapping(bool enable) = 0;
6060

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

6366
~AudioState() override {}
6467
};

0 commit comments

Comments
 (0)