solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "audio/audio_state.h" |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 12 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | #include <utility> |
| 15 | #include <vector> |
| 16 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 17 | #include "absl/memory/memory.h" |
Fredrik Solenberg | d524751 | 2017-12-18 22:41:03 +0100 | [diff] [blame] | 18 | #include "audio/audio_receive_stream.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "modules/audio_device/include/audio_device.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
| 21 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "rtc_base/ref_counted_object.h" |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 23 | #include "rtc_base/thread.h" |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | namespace internal { |
| 27 | |
| 28 | AudioState::AudioState(const AudioState::Config& config) |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 29 | : config_(config), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 30 | audio_transport_(config_.audio_mixer, config_.audio_processing.get()) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 31 | process_thread_checker_.DetachFromThread(); |
aleloi | 10111bc | 2016-11-17 06:48:48 -0800 | [diff] [blame] | 32 | RTC_DCHECK(config_.audio_mixer); |
Fredrik Solenberg | aaedf75 | 2017-12-18 13:09:12 +0100 | [diff] [blame] | 33 | RTC_DCHECK(config_.audio_device_module); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | AudioState::~AudioState() { |
| 37 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
Fredrik Solenberg | d524751 | 2017-12-18 22:41:03 +0100 | [diff] [blame] | 38 | RTC_DCHECK(receiving_streams_.empty()); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 39 | RTC_DCHECK(sending_streams_.empty()); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Mirko Bonadei | 8fdcac3 | 2018-08-28 16:30:18 +0200 | [diff] [blame] | 42 | AudioProcessing* AudioState::audio_processing() { |
| 43 | RTC_DCHECK(config_.audio_processing); |
| 44 | return config_.audio_processing.get(); |
| 45 | } |
| 46 | |
| 47 | AudioTransport* AudioState::audio_transport() { |
| 48 | return &audio_transport_; |
| 49 | } |
| 50 | |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 51 | bool AudioState::typing_noise_detected() const { |
| 52 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 53 | return audio_transport_.typing_noise_detected(); |
| 54 | } |
| 55 | |
Fredrik Solenberg | d524751 | 2017-12-18 22:41:03 +0100 | [diff] [blame] | 56 | void AudioState::AddReceivingStream(webrtc::AudioReceiveStream* stream) { |
| 57 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 58 | RTC_DCHECK_EQ(0, receiving_streams_.count(stream)); |
| 59 | receiving_streams_.insert(stream); |
| 60 | if (!config_.audio_mixer->AddSource( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 61 | static_cast<internal::AudioReceiveStream*>(stream))) { |
Jonas Olsson | 24ea822 | 2018-01-25 10:14:29 +0100 | [diff] [blame] | 62 | RTC_DLOG(LS_ERROR) << "Failed to add source to mixer."; |
Fredrik Solenberg | d524751 | 2017-12-18 22:41:03 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Make sure playback is initialized; start playing if enabled. |
| 66 | auto* adm = config_.audio_device_module.get(); |
| 67 | if (!adm->Playing()) { |
| 68 | if (adm->InitPlayout() == 0) { |
| 69 | if (playout_enabled_) { |
| 70 | adm->StartPlayout(); |
| 71 | } |
| 72 | } else { |
| 73 | RTC_DLOG_F(LS_ERROR) << "Failed to initialize playout."; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void AudioState::RemoveReceivingStream(webrtc::AudioReceiveStream* stream) { |
| 79 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 80 | auto count = receiving_streams_.erase(stream); |
| 81 | RTC_DCHECK_EQ(1, count); |
| 82 | config_.audio_mixer->RemoveSource( |
| 83 | static_cast<internal::AudioReceiveStream*>(stream)); |
| 84 | if (receiving_streams_.empty()) { |
| 85 | config_.audio_device_module->StopPlayout(); |
| 86 | } |
| 87 | } |
| 88 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 89 | void AudioState::AddSendingStream(webrtc::AudioSendStream* stream, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 90 | int sample_rate_hz, |
| 91 | size_t num_channels) { |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 92 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 93 | auto& properties = sending_streams_[stream]; |
| 94 | properties.sample_rate_hz = sample_rate_hz; |
| 95 | properties.num_channels = num_channels; |
| 96 | UpdateAudioTransportWithSendingStreams(); |
Fredrik Solenberg | aaedf75 | 2017-12-18 13:09:12 +0100 | [diff] [blame] | 97 | |
| 98 | // Make sure recording is initialized; start recording if enabled. |
| 99 | auto* adm = config_.audio_device_module.get(); |
| 100 | if (!adm->Recording()) { |
| 101 | if (adm->InitRecording() == 0) { |
| 102 | if (recording_enabled_) { |
| 103 | adm->StartRecording(); |
| 104 | } |
| 105 | } else { |
| 106 | RTC_DLOG_F(LS_ERROR) << "Failed to initialize recording."; |
| 107 | } |
| 108 | } |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void AudioState::RemoveSendingStream(webrtc::AudioSendStream* stream) { |
| 112 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 113 | auto count = sending_streams_.erase(stream); |
| 114 | RTC_DCHECK_EQ(1, count); |
| 115 | UpdateAudioTransportWithSendingStreams(); |
Fredrik Solenberg | aaedf75 | 2017-12-18 13:09:12 +0100 | [diff] [blame] | 116 | if (sending_streams_.empty()) { |
| 117 | config_.audio_device_module->StopRecording(); |
| 118 | } |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 119 | } |
| 120 | |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 121 | void AudioState::SetPlayout(bool enabled) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 122 | RTC_LOG(INFO) << "SetPlayout(" << enabled << ")"; |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 123 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
Fredrik Solenberg | d524751 | 2017-12-18 22:41:03 +0100 | [diff] [blame] | 124 | if (playout_enabled_ != enabled) { |
| 125 | playout_enabled_ = enabled; |
| 126 | if (enabled) { |
| 127 | null_audio_poller_.reset(); |
| 128 | if (!receiving_streams_.empty()) { |
| 129 | config_.audio_device_module->StartPlayout(); |
| 130 | } |
| 131 | } else { |
| 132 | config_.audio_device_module->StopPlayout(); |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 133 | null_audio_poller_ = |
| 134 | absl::make_unique<NullAudioPoller>(&audio_transport_); |
Fredrik Solenberg | d524751 | 2017-12-18 22:41:03 +0100 | [diff] [blame] | 135 | } |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | void AudioState::SetRecording(bool enabled) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 140 | RTC_LOG(INFO) << "SetRecording(" << enabled << ")"; |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 141 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
Fredrik Solenberg | aaedf75 | 2017-12-18 13:09:12 +0100 | [diff] [blame] | 142 | if (recording_enabled_ != enabled) { |
| 143 | recording_enabled_ = enabled; |
| 144 | if (enabled) { |
| 145 | if (!sending_streams_.empty()) { |
| 146 | config_.audio_device_module->StartRecording(); |
| 147 | } |
| 148 | } else { |
| 149 | config_.audio_device_module->StopRecording(); |
| 150 | } |
| 151 | } |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | AudioState::Stats AudioState::GetAudioInputStats() const { |
| 155 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 156 | const voe::AudioLevel& audio_level = audio_transport_.audio_level(); |
| 157 | Stats result; |
| 158 | result.audio_level = audio_level.LevelFullRange(); |
| 159 | RTC_DCHECK_LE(0, result.audio_level); |
| 160 | RTC_DCHECK_GE(32767, result.audio_level); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 161 | result.total_energy = audio_level.TotalEnergy(); |
| 162 | result.total_duration = audio_level.TotalDuration(); |
| 163 | return result; |
| 164 | } |
| 165 | |
| 166 | void AudioState::SetStereoChannelSwapping(bool enable) { |
| 167 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 168 | audio_transport_.SetStereoChannelSwapping(enable); |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 171 | void AudioState::UpdateAudioTransportWithSendingStreams() { |
| 172 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
Fredrik Solenberg | d524751 | 2017-12-18 22:41:03 +0100 | [diff] [blame] | 173 | std::vector<webrtc::AudioSendStream*> sending_streams; |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 174 | int max_sample_rate_hz = 8000; |
| 175 | size_t max_num_channels = 1; |
| 176 | for (const auto& kv : sending_streams_) { |
| 177 | sending_streams.push_back(kv.first); |
| 178 | max_sample_rate_hz = std::max(max_sample_rate_hz, kv.second.sample_rate_hz); |
| 179 | max_num_channels = std::max(max_num_channels, kv.second.num_channels); |
| 180 | } |
| 181 | audio_transport_.UpdateSendingStreams(std::move(sending_streams), |
| 182 | max_sample_rate_hz, max_num_channels); |
| 183 | } |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 184 | } // namespace internal |
| 185 | |
| 186 | rtc::scoped_refptr<AudioState> AudioState::Create( |
| 187 | const AudioState::Config& config) { |
Niels Möller | ac63ac7 | 2019-01-08 13:47:12 +0100 | [diff] [blame] | 188 | return new rtc::RefCountedObject<internal::AudioState>(config); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 189 | } |
| 190 | } // namespace webrtc |