Skip to content

Commit 53559dd

Browse files
cloudwebrtchiroshihoriedavidzhao
committed
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) Co-authored-by: Hiroshi Horie <[email protected]> Co-authored-by: David Zhao <[email protected]>
1 parent aaf97b7 commit 53559dd

Some content is hidden

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

41 files changed

+1132
-203
lines changed

audio/audio_send_stream.cc

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

427+
bool AudioSendStream::GetMuted() {
428+
RTC_DCHECK_RUN_ON(&worker_thread_checker_);
429+
return channel_send_->InputMute();
430+
}
431+
427432
webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const {
428433
return GetStats(true);
429434
}

audio/audio_send_stream.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class AudioSendStream final : public webrtc::AudioSendStream,
9696
int payload_frequency,
9797
int event,
9898
int duration_ms) override;
99+
bool GetMuted() override;
99100
void SetMuted(bool muted) override;
100101
webrtc::AudioSendStream::Stats GetStats() const override;
101102
webrtc::AudioSendStream::Stats GetStats(

audio/audio_state.cc

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,26 @@ void AudioState::AddSendingStream(webrtc::AudioSendStream* stream,
9898
UpdateAudioTransportWithSendingStreams();
9999

100100
// Make sure recording is initialized; start recording if enabled.
101-
auto* adm = config_.audio_device_module.get();
102-
if (!adm->Recording()) {
103-
if (adm->InitRecording() == 0) {
104-
if (recording_enabled_) {
105-
adm->StartRecording();
101+
if (ShouldRecord()) {
102+
auto* adm = config_.audio_device_module.get();
103+
if (!adm->Recording()) {
104+
if (adm->InitRecording() == 0) {
105+
if (recording_enabled_) {
106+
107+
// TODO: Verify if the following windows only logic is still required.
108+
#if defined(WEBRTC_WIN)
109+
if (adm->BuiltInAECIsAvailable() && !adm->Playing()) {
110+
if (!adm->PlayoutIsInitialized()) {
111+
adm->InitPlayout();
112+
}
113+
adm->StartPlayout();
114+
}
115+
#endif
116+
adm->StartRecording();
117+
}
118+
} else {
119+
RTC_DLOG_F(LS_ERROR) << "Failed to initialize recording.";
106120
}
107-
} else {
108-
RTC_DLOG_F(LS_ERROR) << "Failed to initialize recording.";
109121
}
110122
}
111123
}
@@ -115,7 +127,8 @@ void AudioState::RemoveSendingStream(webrtc::AudioSendStream* stream) {
115127
auto count = sending_streams_.erase(stream);
116128
RTC_DCHECK_EQ(1, count);
117129
UpdateAudioTransportWithSendingStreams();
118-
if (sending_streams_.empty()) {
130+
131+
if (!ShouldRecord()) {
119132
config_.audio_device_module->StopRecording();
120133
}
121134
}
@@ -143,7 +156,7 @@ void AudioState::SetRecording(bool enabled) {
143156
if (recording_enabled_ != enabled) {
144157
recording_enabled_ = enabled;
145158
if (enabled) {
146-
if (!sending_streams_.empty()) {
159+
if (ShouldRecord()) {
147160
config_.audio_device_module->StartRecording();
148161
}
149162
} else {
@@ -203,6 +216,39 @@ void AudioState::UpdateNullAudioPollerState() {
203216
null_audio_poller_.Stop();
204217
}
205218
}
219+
220+
void AudioState::OnMuteStreamChanged() {
221+
222+
auto* adm = config_.audio_device_module.get();
223+
bool should_record = ShouldRecord();
224+
225+
if (should_record && !adm->Recording()) {
226+
if (adm->InitRecording() == 0) {
227+
adm->StartRecording();
228+
}
229+
} else if (!should_record && adm->Recording()) {
230+
adm->StopRecording();
231+
}
232+
}
233+
234+
bool AudioState::ShouldRecord() {
235+
// no streams to send
236+
if (sending_streams_.empty()) {
237+
return false;
238+
}
239+
240+
int stream_count = sending_streams_.size();
241+
242+
int muted_count = 0;
243+
for (const auto& kv : sending_streams_) {
244+
if (kv.first->GetMuted()) {
245+
muted_count++;
246+
}
247+
}
248+
249+
return muted_count != stream_count;
250+
}
251+
206252
} // namespace internal
207253

208254
rtc::scoped_refptr<AudioState> AudioState::Create(

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
@@ -98,6 +98,8 @@ class ChannelSend : public ChannelSendInterface,
9898
// Muting, Volume and Level.
9999
void SetInputMute(bool enable) override;
100100

101+
bool InputMute() const override;
102+
101103
// Stats.
102104
ANAStats GetANAStatistics() const override;
103105

@@ -161,8 +163,6 @@ class ChannelSend : public ChannelSendInterface,
161163
size_t payloadSize,
162164
int64_t absolute_capture_timestamp_ms) override;
163165

164-
bool InputMute() const;
165-
166166
int32_t SendRtpAudio(AudioFrameType frameType,
167167
uint8_t payloadType,
168168
uint32_t rtp_timestamp,

audio/channel_send.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ class ChannelSendInterface {
9595
virtual bool SendTelephoneEventOutband(int event, int duration_ms) = 0;
9696
virtual void OnBitrateAllocation(BitrateAllocationUpdate update) = 0;
9797
virtual int GetTargetBitrate() const = 0;
98+
99+
virtual bool InputMute() const = 0;
98100
virtual void SetInputMute(bool muted) = 0;
99101

100102
virtual void ProcessAndEncodeAudio(

call/audio_send_stream.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ class AudioSendStream : public AudioSender {
190190
int event,
191191
int duration_ms) = 0;
192192

193+
virtual bool GetMuted() = 0;
193194
virtual void SetMuted(bool muted) = 0;
194195

195196
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 rtc::RefCountInterface {
5959

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

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

media/engine/webrtc_voice_engine.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,6 +2285,9 @@ bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
22852285
ap->set_output_will_be_muted(all_muted);
22862286
}
22872287

2288+
// Notfy the AudioState that the mute state has updated.
2289+
engine_->audio_state()->OnMuteStreamChanged();
2290+
22882291
return true;
22892292
}
22902293

media/engine/webrtc_voice_engine.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ class WebRtcVoiceEngine final : public VoiceEngineInterface {
9393

9494
absl::optional<webrtc::AudioDeviceModule::Stats> GetAudioDeviceStats()
9595
override;
96+
// Moved to public so WebRtcVoiceMediaChannel can access it.
97+
webrtc::AudioState* audio_state();
9698

9799
private:
98100
// Every option that is "set" will be applied. Every option not "set" will be
@@ -105,7 +107,6 @@ class WebRtcVoiceEngine final : public VoiceEngineInterface {
105107

106108
webrtc::AudioDeviceModule* adm();
107109
webrtc::AudioProcessing* apm() const;
108-
webrtc::AudioState* audio_state();
109110

110111
std::vector<AudioCodec> CollectCodecs(
111112
const std::vector<webrtc::AudioCodecSpec>& specs) const;

0 commit comments

Comments
 (0)