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