| 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 |
| 11 | #include "audio/audio_state.h" |
| 12 |
| 13 | #include <algorithm> |
| 14 | #include <memory> |
| 15 | #include <utility> |
| 16 | #include <vector> |
| 17 |
| 18 | #include "api/sequence_checker.h" |
| 19 | #include "api/task_queue/task_queue_base.h" |
| 20 | #include "api/units/time_delta.h" |
| 21 | #include "audio/audio_receive_stream.h" |
| 22 | #include "audio/audio_send_stream.h" |
| 23 | #include "modules/audio_device/include/audio_device.h" |
| 24 | #include "rtc_base/checks.h" |
| 25 | #include "rtc_base/logging.h" |
| 26 |
| 27 | namespace webrtc { |
| 28 | namespace internal { |
| 29 |
| 30 | AudioState::AudioState(const AudioState::Config& config) |
| 31 | : config_(config), |
| 32 | audio_transport_(config_.audio_mixer.get(), |
| 33 | config_.audio_processing.get(), |
| 34 | config_.async_audio_processing_factory.get()) { |
| 35 | RTC_DCHECK(config_.audio_mixer); |
| 36 | RTC_DCHECK(config_.audio_device_module); |
| 37 | } |
| 38 |
| 39 | AudioState::~AudioState() { |
| 40 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 41 | RTC_DCHECK(receiving_streams_.empty()); |
| 42 | RTC_DCHECK(sending_streams_.empty()); |
| 43 | RTC_DCHECK(!null_audio_poller_.Running()); |
| 44 | } |
| 45 |
| 46 | AudioProcessing* AudioState::audio_processing() { |
| 47 | return config_.audio_processing.get(); |
| 48 | } |
| 49 |
| 50 | AudioTransport* AudioState::audio_transport() { |
| 51 | return &audio_transport_; |
| 52 | } |
| 53 |
| 54 | void AudioState::AddReceivingStream( |
| 55 | webrtc::AudioReceiveStreamInterface* stream) { |
| 56 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 57 | RTC_DCHECK_EQ(0, receiving_streams_.count(stream)); |
| 58 | receiving_streams_.insert(stream); |
| 59 | if (!config_.audio_mixer->AddSource( |
| 60 | static_cast<AudioReceiveStreamImpl*>(stream))) { |
| 61 | RTC_DLOG(LS_ERROR) << "Failed to add source to mixer."; |
| 62 | } |
| 63 |
| 64 | // Make sure playback is initialized; start playing if enabled. |
| 65 | UpdateNullAudioPollerState(); |
| 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( |
| 79 | webrtc::AudioReceiveStreamInterface* stream) { |
| 80 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 81 | auto count = receiving_streams_.erase(stream); |
| 82 | RTC_DCHECK_EQ(1, count); |
| 83 | config_.audio_mixer->RemoveSource( |
| 84 | static_cast<AudioReceiveStreamImpl*>(stream)); |
| 85 | UpdateNullAudioPollerState(); |
| 86 | if (receiving_streams_.empty()) { |
| 87 | config_.audio_device_module->StopPlayout(); |
| 88 | } |
| 89 | } |
| 90 |
| 91 | void AudioState::AddSendingStream(webrtc::AudioSendStream* stream, |
| 92 | int sample_rate_hz, |
| 93 | size_t num_channels) { |
| 94 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 95 | auto& properties = sending_streams_[stream]; |
| 96 | properties.sample_rate_hz = sample_rate_hz; |
| 97 | properties.num_channels = num_channels; |
| 98 | UpdateAudioTransportWithSendingStreams(); |
| 99 |
| 100 | // 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(); |
| 106 | } |
| 107 | } else { |
| 108 | RTC_DLOG_F(LS_ERROR) << "Failed to initialize recording."; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 |
| 113 | void AudioState::RemoveSendingStream(webrtc::AudioSendStream* stream) { |
| 114 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 115 | auto count = sending_streams_.erase(stream); |
| 116 | RTC_DCHECK_EQ(1, count); |
| 117 | UpdateAudioTransportWithSendingStreams(); |
| 118 | if (sending_streams_.empty()) { |
| 119 | config_.audio_device_module->StopRecording(); |
| 120 | } |
| 121 | } |
| 122 |
| 123 | void AudioState::SetPlayout(bool enabled) { |
| 124 | RTC_LOG(LS_INFO) << "SetPlayout(" << enabled << ")"; |
| 125 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 126 | if (playout_enabled_ != enabled) { |
| 127 | playout_enabled_ = enabled; |
| 128 | if (enabled) { |
| 129 | UpdateNullAudioPollerState(); |
| 130 | if (!receiving_streams_.empty()) { |
| 131 | config_.audio_device_module->StartPlayout(); |
| 132 | } |
| 133 | } else { |
| 134 | config_.audio_device_module->StopPlayout(); |
| 135 | UpdateNullAudioPollerState(); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 |
| 140 | void AudioState::SetRecording(bool enabled) { |
| 141 | RTC_LOG(LS_INFO) << "SetRecording(" << enabled << ")"; |
| 142 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 143 | if (recording_enabled_ != enabled) { |
| 144 | recording_enabled_ = enabled; |
| 145 | if (enabled) { |
| 146 | if (!sending_streams_.empty()) { |
| 147 | config_.audio_device_module->StartRecording(); |
| 148 | } |
| 149 | } else { |
| 150 | config_.audio_device_module->StopRecording(); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 |
| 155 | void AudioState::SetStereoChannelSwapping(bool enable) { |
| 156 | RTC_DCHECK(thread_checker_.IsCurrent()); |
| 157 | audio_transport_.SetStereoChannelSwapping(enable); |
| 158 | } |
| 159 |
| 160 | void AudioState::UpdateAudioTransportWithSendingStreams() { |
| 161 | RTC_DCHECK(thread_checker_.IsCurrent()); |
| 162 | std::vector<AudioSender*> audio_senders; |
| 163 | int max_sample_rate_hz = 8000; |
| 164 | size_t max_num_channels = 1; |
| 165 | for (const auto& kv : sending_streams_) { |
| 166 | audio_senders.push_back(kv.first); |
| 167 | max_sample_rate_hz = std::max(max_sample_rate_hz, kv.second.sample_rate_hz); |
| 168 | max_num_channels = std::max(max_num_channels, kv.second.num_channels); |
| 169 | } |
| 170 | audio_transport_.UpdateAudioSenders(std::move(audio_senders), |
| 171 | max_sample_rate_hz, max_num_channels); |
| 172 | } |
| 173 |
| 174 | void AudioState::UpdateNullAudioPollerState() { |
| 175 | // Run NullAudioPoller when there are receiving streams and playout is |
| 176 | // disabled. |
| 177 | if (!receiving_streams_.empty() && !playout_enabled_) { |
| 178 | if (!null_audio_poller_.Running()) { |
| 179 | AudioTransport* audio_transport = &audio_transport_; |
| 180 | null_audio_poller_ = RepeatingTaskHandle::Start( |
| 181 | TaskQueueBase::Current(), [audio_transport] { |
| 182 | static constexpr size_t kNumChannels = 1; |
| 183 | static constexpr uint32_t kSamplesPerSecond = 48'000; |
| 184 | // 10ms of samples |
| 185 | static constexpr size_t kNumSamples = kSamplesPerSecond / 100; |
| 186 |
| 187 | // Buffer to hold the audio samples. |
| 188 | int16_t buffer[kNumSamples * kNumChannels]; |
| 189 |
| 190 | // Output variables from `NeedMorePlayData`. |
| 191 | size_t n_samples; |
| 192 | int64_t elapsed_time_ms; |
| 193 | int64_t ntp_time_ms; |
| 194 | audio_transport->NeedMorePlayData( |
| 195 | kNumSamples, sizeof(int16_t), kNumChannels, kSamplesPerSecond, |
| 196 | buffer, n_samples, &elapsed_time_ms, &ntp_time_ms); |
| 197 |
| 198 | // Reschedule the next poll iteration. |
| 199 | return TimeDelta::Millis(10); |
| 200 | }); |
| 201 | } |
| 202 | } else { |
| 203 | null_audio_poller_.Stop(); |
| 204 | } |
| 205 | } |
| 206 | } // namespace internal |
| 207 |
| 208 | rtc::scoped_refptr<AudioState> AudioState::Create( |
| 209 | const AudioState::Config& config) { |
| 210 | return rtc::make_ref_counted<internal::AudioState>(config); |
| 211 | } |
| 212 | } // namespace webrtc |
| 213 |