blob: 7648fb948fc81a087079823419323e05647a2b28 [file] [log] [blame]
Fredrik Solenberg2a877972017-12-15 16:42:15 +01001/*
2 * Copyright (c) 2016 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
11#include "audio/audio_transport_impl.h"
12
13#include <algorithm>
14#include <memory>
15#include <utility>
16
Fredrik Solenberga8b7c7f2018-01-17 11:18:31 +010017#include "audio/remix_resample.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010018#include "audio/utility/audio_frame_operations.h"
Tim Nab8c775a2020-01-10 10:33:05 -080019#include "call/audio_sender.h"
Per Åhgren71652f42020-03-17 13:23:58 +010020#include "modules/audio_processing/include/audio_frame_proxies.h"
Yves Gerey988cc082018-10-23 12:03:01 +020021#include "rtc_base/checks.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010022
23namespace webrtc {
24
25namespace {
26
27// We want to process at the lowest sample rate and channel count possible
28// without losing information. Choose the lowest native rate at least equal to
29// the minimum of input and codec rates, choose lowest channel count, and
30// configure the audio frame.
31void InitializeCaptureFrame(int input_sample_rate,
32 int send_sample_rate_hz,
33 size_t input_num_channels,
34 size_t send_num_channels,
35 AudioFrame* audio_frame) {
36 RTC_DCHECK(audio_frame);
37 int min_processing_rate_hz = std::min(input_sample_rate, send_sample_rate_hz);
38 for (int native_rate_hz : AudioProcessing::kNativeSampleRatesHz) {
39 audio_frame->sample_rate_hz_ = native_rate_hz;
40 if (audio_frame->sample_rate_hz_ >= min_processing_rate_hz) {
41 break;
42 }
43 }
44 audio_frame->num_channels_ = std::min(input_num_channels, send_num_channels);
45}
46
henrika649a3852017-12-22 13:58:29 +010047void ProcessCaptureFrame(uint32_t delay_ms,
Fredrik Solenberg2a877972017-12-15 16:42:15 +010048 bool key_pressed,
49 bool swap_stereo_channels,
50 AudioProcessing* audio_processing,
51 AudioFrame* audio_frame) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +010052 RTC_DCHECK(audio_frame);
Per Åhgrencc73ed32020-04-26 23:56:17 +020053 if (audio_processing) {
54 audio_processing->set_stream_delay_ms(delay_ms);
55 audio_processing->set_stream_key_pressed(key_pressed);
56 int error = ProcessAudioFrame(audio_processing, audio_frame);
Per Åhgren71652f42020-03-17 13:23:58 +010057
Per Åhgrencc73ed32020-04-26 23:56:17 +020058 RTC_DCHECK_EQ(0, error) << "ProcessStream() error: " << error;
59 }
60
Fredrik Solenberg2a877972017-12-15 16:42:15 +010061 if (swap_stereo_channels) {
62 AudioFrameOperations::SwapStereoChannels(audio_frame);
63 }
64}
65
66// Resample audio in |frame| to given sample rate preserving the
67// channel count and place the result in |destination|.
68int Resample(const AudioFrame& frame,
69 const int destination_sample_rate,
70 PushResampler<int16_t>* resampler,
71 int16_t* destination) {
72 const int number_of_channels = static_cast<int>(frame.num_channels_);
73 const int target_number_of_samples_per_channel =
74 destination_sample_rate / 100;
75 resampler->InitializeIfNeeded(frame.sample_rate_hz_, destination_sample_rate,
76 number_of_channels);
77
78 // TODO(yujo): make resampler take an AudioFrame, and add special case
79 // handling of muted frames.
80 return resampler->Resample(
81 frame.data(), frame.samples_per_channel_ * number_of_channels,
82 destination, number_of_channels * target_number_of_samples_per_channel);
83}
84} // namespace
85
86AudioTransportImpl::AudioTransportImpl(AudioMixer* mixer,
henrika649a3852017-12-22 13:58:29 +010087 AudioProcessing* audio_processing)
Yves Gerey665174f2018-06-19 15:03:05 +020088 : audio_processing_(audio_processing), mixer_(mixer) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +010089 RTC_DCHECK(mixer);
Fredrik Solenberg2a877972017-12-15 16:42:15 +010090}
91
92AudioTransportImpl::~AudioTransportImpl() {}
93
94// Not used in Chromium. Process captured audio and distribute to all sending
95// streams, and try to do this at the lowest possible sample rate.
96int32_t AudioTransportImpl::RecordedDataIsAvailable(
97 const void* audio_data,
98 const size_t number_of_frames,
99 const size_t bytes_per_sample,
100 const size_t number_of_channels,
101 const uint32_t sample_rate,
102 const uint32_t audio_delay_milliseconds,
103 const int32_t /*clock_drift*/,
henrika649a3852017-12-22 13:58:29 +0100104 const uint32_t /*volume*/,
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100105 const bool key_pressed,
106 uint32_t& /*new_mic_volume*/) { // NOLINT: to avoid changing APIs
107 RTC_DCHECK(audio_data);
108 RTC_DCHECK_GE(number_of_channels, 1);
109 RTC_DCHECK_LE(number_of_channels, 2);
110 RTC_DCHECK_EQ(2 * number_of_channels, bytes_per_sample);
111 RTC_DCHECK_GE(sample_rate, AudioProcessing::NativeRate::kSampleRate8kHz);
112 // 100 = 1 second / data duration (10 ms).
113 RTC_DCHECK_EQ(number_of_frames * 100, sample_rate);
114 RTC_DCHECK_LE(bytes_per_sample * number_of_frames * number_of_channels,
115 AudioFrame::kMaxDataSizeBytes);
116
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100117 int send_sample_rate_hz = 0;
118 size_t send_num_channels = 0;
119 bool swap_stereo_channels = false;
120 {
121 rtc::CritScope lock(&capture_lock_);
122 send_sample_rate_hz = send_sample_rate_hz_;
123 send_num_channels = send_num_channels_;
124 swap_stereo_channels = swap_stereo_channels_;
125 }
126
127 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
Yves Gerey665174f2018-06-19 15:03:05 +0200128 InitializeCaptureFrame(sample_rate, send_sample_rate_hz, number_of_channels,
129 send_num_channels, audio_frame.get());
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100130 voe::RemixAndResample(static_cast<const int16_t*>(audio_data),
131 number_of_frames, number_of_channels, sample_rate,
132 &capture_resampler_, audio_frame.get());
henrika649a3852017-12-22 13:58:29 +0100133 ProcessCaptureFrame(audio_delay_milliseconds, key_pressed,
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100134 swap_stereo_channels, audio_processing_,
135 audio_frame.get());
136
137 // Typing detection (utilizes the APM/VAD decision). We let the VAD determine
138 // if we're using this feature or not.
Sam Zackrissonba502232019-01-04 10:36:48 +0100139 // TODO(solenberg): GetConfig() takes a lock. Work around that.
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100140 bool typing_detected = false;
Per Åhgrencc73ed32020-04-26 23:56:17 +0200141 if (audio_processing_ &&
142 audio_processing_->GetConfig().voice_detection.enabled) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100143 if (audio_frame->vad_activity_ != AudioFrame::kVadUnknown) {
144 bool vad_active = audio_frame->vad_activity_ == AudioFrame::kVadActive;
145 typing_detected = typing_detection_.Process(key_pressed, vad_active);
146 }
147 }
148
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100149 // Copy frame and push to each sending stream. The copy is required since an
150 // encoding task will be posted internally to each stream.
151 {
152 rtc::CritScope lock(&capture_lock_);
153 typing_noise_detected_ = typing_detected;
154
155 RTC_DCHECK_GT(audio_frame->samples_per_channel_, 0);
Tim Nab8c775a2020-01-10 10:33:05 -0800156 if (!audio_senders_.empty()) {
157 auto it = audio_senders_.begin();
158 while (++it != audio_senders_.end()) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100159 std::unique_ptr<AudioFrame> audio_frame_copy(new AudioFrame());
Benjamin Wright17b050f2019-03-13 17:35:46 -0700160 audio_frame_copy->CopyFrom(*audio_frame);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100161 (*it)->SendAudioData(std::move(audio_frame_copy));
162 }
163 // Send the original frame to the first stream w/o copying.
Tim Nab8c775a2020-01-10 10:33:05 -0800164 (*audio_senders_.begin())->SendAudioData(std::move(audio_frame));
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100165 }
166 }
167
168 return 0;
169}
170
171// Mix all received streams, feed the result to the AudioProcessing module, then
172// resample the result to the requested output rate.
173int32_t AudioTransportImpl::NeedMorePlayData(const size_t nSamples,
Yves Gerey665174f2018-06-19 15:03:05 +0200174 const size_t nBytesPerSample,
175 const size_t nChannels,
176 const uint32_t samplesPerSec,
177 void* audioSamples,
178 size_t& nSamplesOut,
179 int64_t* elapsed_time_ms,
180 int64_t* ntp_time_ms) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100181 RTC_DCHECK_EQ(sizeof(int16_t) * nChannels, nBytesPerSample);
182 RTC_DCHECK_GE(nChannels, 1);
183 RTC_DCHECK_LE(nChannels, 2);
184 RTC_DCHECK_GE(
185 samplesPerSec,
186 static_cast<uint32_t>(AudioProcessing::NativeRate::kSampleRate8kHz));
187
188 // 100 = 1 second / data duration (10 ms).
189 RTC_DCHECK_EQ(nSamples * 100, samplesPerSec);
190 RTC_DCHECK_LE(nBytesPerSample * nSamples * nChannels,
191 AudioFrame::kMaxDataSizeBytes);
192
193 mixer_->Mix(nChannels, &mixed_frame_);
194 *elapsed_time_ms = mixed_frame_.elapsed_time_ms_;
195 *ntp_time_ms = mixed_frame_.ntp_time_ms_;
196
Per Åhgrencc73ed32020-04-26 23:56:17 +0200197 if (audio_processing_) {
198 const auto error =
199 ProcessReverseAudioFrame(audio_processing_, &mixed_frame_);
200 RTC_DCHECK_EQ(error, AudioProcessing::kNoError);
201 }
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100202
203 nSamplesOut = Resample(mixed_frame_, samplesPerSec, &render_resampler_,
204 static_cast<int16_t*>(audioSamples));
205 RTC_DCHECK_EQ(nSamplesOut, nChannels * nSamples);
206 return 0;
207}
208
209// Used by Chromium - same as NeedMorePlayData() but because Chrome has its
210// own APM instance, does not call audio_processing_->ProcessReverseStream().
211void AudioTransportImpl::PullRenderData(int bits_per_sample,
Yves Gerey665174f2018-06-19 15:03:05 +0200212 int sample_rate,
213 size_t number_of_channels,
214 size_t number_of_frames,
215 void* audio_data,
216 int64_t* elapsed_time_ms,
217 int64_t* ntp_time_ms) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100218 RTC_DCHECK_EQ(bits_per_sample, 16);
219 RTC_DCHECK_GE(number_of_channels, 1);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100220 RTC_DCHECK_GE(sample_rate, AudioProcessing::NativeRate::kSampleRate8kHz);
221
222 // 100 = 1 second / data duration (10 ms).
223 RTC_DCHECK_EQ(number_of_frames * 100, sample_rate);
224
225 // 8 = bits per byte.
226 RTC_DCHECK_LE(bits_per_sample / 8 * number_of_frames * number_of_channels,
227 AudioFrame::kMaxDataSizeBytes);
228 mixer_->Mix(number_of_channels, &mixed_frame_);
229 *elapsed_time_ms = mixed_frame_.elapsed_time_ms_;
230 *ntp_time_ms = mixed_frame_.ntp_time_ms_;
231
232 auto output_samples = Resample(mixed_frame_, sample_rate, &render_resampler_,
233 static_cast<int16_t*>(audio_data));
234 RTC_DCHECK_EQ(output_samples, number_of_channels * number_of_frames);
235}
236
Tim Nab8c775a2020-01-10 10:33:05 -0800237void AudioTransportImpl::UpdateAudioSenders(std::vector<AudioSender*> senders,
238 int send_sample_rate_hz,
239 size_t send_num_channels) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100240 rtc::CritScope lock(&capture_lock_);
Tim Nab8c775a2020-01-10 10:33:05 -0800241 audio_senders_ = std::move(senders);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100242 send_sample_rate_hz_ = send_sample_rate_hz;
243 send_num_channels_ = send_num_channels;
244}
245
246void AudioTransportImpl::SetStereoChannelSwapping(bool enable) {
247 rtc::CritScope lock(&capture_lock_);
248 swap_stereo_channels_ = enable;
249}
250
251bool AudioTransportImpl::typing_noise_detected() const {
252 rtc::CritScope lock(&capture_lock_);
253 return typing_noise_detected_;
254}
255} // namespace webrtc