blob: aca6f9baf666a90d1920db52e3c963685aef01cf [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"
19#include "call/audio_send_stream.h"
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "rtc_base/checks.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010021
22namespace webrtc {
23
24namespace {
25
26// We want to process at the lowest sample rate and channel count possible
27// without losing information. Choose the lowest native rate at least equal to
28// the minimum of input and codec rates, choose lowest channel count, and
29// configure the audio frame.
30void InitializeCaptureFrame(int input_sample_rate,
31 int send_sample_rate_hz,
32 size_t input_num_channels,
33 size_t send_num_channels,
34 AudioFrame* audio_frame) {
35 RTC_DCHECK(audio_frame);
36 int min_processing_rate_hz = std::min(input_sample_rate, send_sample_rate_hz);
37 for (int native_rate_hz : AudioProcessing::kNativeSampleRatesHz) {
38 audio_frame->sample_rate_hz_ = native_rate_hz;
39 if (audio_frame->sample_rate_hz_ >= min_processing_rate_hz) {
40 break;
41 }
42 }
43 audio_frame->num_channels_ = std::min(input_num_channels, send_num_channels);
44}
45
henrika649a3852017-12-22 13:58:29 +010046void ProcessCaptureFrame(uint32_t delay_ms,
Fredrik Solenberg2a877972017-12-15 16:42:15 +010047 bool key_pressed,
48 bool swap_stereo_channels,
49 AudioProcessing* audio_processing,
50 AudioFrame* audio_frame) {
51 RTC_DCHECK(audio_processing);
52 RTC_DCHECK(audio_frame);
Fredrik Solenberg2a877972017-12-15 16:42:15 +010053 audio_processing->set_stream_delay_ms(delay_ms);
54 audio_processing->set_stream_key_pressed(key_pressed);
henrika649a3852017-12-22 13:58:29 +010055 int error = audio_processing->ProcessStream(audio_frame);
Fredrik Solenberg2a877972017-12-15 16:42:15 +010056 RTC_DCHECK_EQ(0, error) << "ProcessStream() error: " << error;
57 if (swap_stereo_channels) {
58 AudioFrameOperations::SwapStereoChannels(audio_frame);
59 }
60}
61
62// Resample audio in |frame| to given sample rate preserving the
63// channel count and place the result in |destination|.
64int Resample(const AudioFrame& frame,
65 const int destination_sample_rate,
66 PushResampler<int16_t>* resampler,
67 int16_t* destination) {
68 const int number_of_channels = static_cast<int>(frame.num_channels_);
69 const int target_number_of_samples_per_channel =
70 destination_sample_rate / 100;
71 resampler->InitializeIfNeeded(frame.sample_rate_hz_, destination_sample_rate,
72 number_of_channels);
73
74 // TODO(yujo): make resampler take an AudioFrame, and add special case
75 // handling of muted frames.
76 return resampler->Resample(
77 frame.data(), frame.samples_per_channel_ * number_of_channels,
78 destination, number_of_channels * target_number_of_samples_per_channel);
79}
80} // namespace
81
82AudioTransportImpl::AudioTransportImpl(AudioMixer* mixer,
henrika649a3852017-12-22 13:58:29 +010083 AudioProcessing* audio_processing)
Yves Gerey665174f2018-06-19 15:03:05 +020084 : audio_processing_(audio_processing), mixer_(mixer) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +010085 RTC_DCHECK(mixer);
86 RTC_DCHECK(audio_processing);
Fredrik Solenberg2a877972017-12-15 16:42:15 +010087}
88
89AudioTransportImpl::~AudioTransportImpl() {}
90
91// Not used in Chromium. Process captured audio and distribute to all sending
92// streams, and try to do this at the lowest possible sample rate.
93int32_t AudioTransportImpl::RecordedDataIsAvailable(
94 const void* audio_data,
95 const size_t number_of_frames,
96 const size_t bytes_per_sample,
97 const size_t number_of_channels,
98 const uint32_t sample_rate,
99 const uint32_t audio_delay_milliseconds,
100 const int32_t /*clock_drift*/,
henrika649a3852017-12-22 13:58:29 +0100101 const uint32_t /*volume*/,
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100102 const bool key_pressed,
103 uint32_t& /*new_mic_volume*/) { // NOLINT: to avoid changing APIs
104 RTC_DCHECK(audio_data);
105 RTC_DCHECK_GE(number_of_channels, 1);
106 RTC_DCHECK_LE(number_of_channels, 2);
107 RTC_DCHECK_EQ(2 * number_of_channels, bytes_per_sample);
108 RTC_DCHECK_GE(sample_rate, AudioProcessing::NativeRate::kSampleRate8kHz);
109 // 100 = 1 second / data duration (10 ms).
110 RTC_DCHECK_EQ(number_of_frames * 100, sample_rate);
111 RTC_DCHECK_LE(bytes_per_sample * number_of_frames * number_of_channels,
112 AudioFrame::kMaxDataSizeBytes);
113
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100114 int send_sample_rate_hz = 0;
115 size_t send_num_channels = 0;
116 bool swap_stereo_channels = false;
117 {
118 rtc::CritScope lock(&capture_lock_);
119 send_sample_rate_hz = send_sample_rate_hz_;
120 send_num_channels = send_num_channels_;
121 swap_stereo_channels = swap_stereo_channels_;
122 }
123
124 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
Yves Gerey665174f2018-06-19 15:03:05 +0200125 InitializeCaptureFrame(sample_rate, send_sample_rate_hz, number_of_channels,
126 send_num_channels, audio_frame.get());
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100127 voe::RemixAndResample(static_cast<const int16_t*>(audio_data),
128 number_of_frames, number_of_channels, sample_rate,
129 &capture_resampler_, audio_frame.get());
henrika649a3852017-12-22 13:58:29 +0100130 ProcessCaptureFrame(audio_delay_milliseconds, key_pressed,
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100131 swap_stereo_channels, audio_processing_,
132 audio_frame.get());
133
134 // Typing detection (utilizes the APM/VAD decision). We let the VAD determine
135 // if we're using this feature or not.
Sam Zackrissonba502232019-01-04 10:36:48 +0100136 // TODO(solenberg): GetConfig() takes a lock. Work around that.
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100137 bool typing_detected = false;
Sam Zackrissonba502232019-01-04 10:36:48 +0100138 if (audio_processing_->GetConfig().voice_detection.enabled) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100139 if (audio_frame->vad_activity_ != AudioFrame::kVadUnknown) {
140 bool vad_active = audio_frame->vad_activity_ == AudioFrame::kVadActive;
141 typing_detected = typing_detection_.Process(key_pressed, vad_active);
142 }
143 }
144
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100145 // Copy frame and push to each sending stream. The copy is required since an
146 // encoding task will be posted internally to each stream.
147 {
148 rtc::CritScope lock(&capture_lock_);
149 typing_noise_detected_ = typing_detected;
150
151 RTC_DCHECK_GT(audio_frame->samples_per_channel_, 0);
152 if (!sending_streams_.empty()) {
153 auto it = sending_streams_.begin();
154 while (++it != sending_streams_.end()) {
155 std::unique_ptr<AudioFrame> audio_frame_copy(new AudioFrame());
Benjamin Wright17b050f2019-03-13 17:35:46 -0700156 audio_frame_copy->CopyFrom(*audio_frame);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100157 (*it)->SendAudioData(std::move(audio_frame_copy));
158 }
159 // Send the original frame to the first stream w/o copying.
160 (*sending_streams_.begin())->SendAudioData(std::move(audio_frame));
161 }
162 }
163
164 return 0;
165}
166
167// Mix all received streams, feed the result to the AudioProcessing module, then
168// resample the result to the requested output rate.
169int32_t AudioTransportImpl::NeedMorePlayData(const size_t nSamples,
Yves Gerey665174f2018-06-19 15:03:05 +0200170 const size_t nBytesPerSample,
171 const size_t nChannels,
172 const uint32_t samplesPerSec,
173 void* audioSamples,
174 size_t& nSamplesOut,
175 int64_t* elapsed_time_ms,
176 int64_t* ntp_time_ms) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100177 RTC_DCHECK_EQ(sizeof(int16_t) * nChannels, nBytesPerSample);
178 RTC_DCHECK_GE(nChannels, 1);
179 RTC_DCHECK_LE(nChannels, 2);
180 RTC_DCHECK_GE(
181 samplesPerSec,
182 static_cast<uint32_t>(AudioProcessing::NativeRate::kSampleRate8kHz));
183
184 // 100 = 1 second / data duration (10 ms).
185 RTC_DCHECK_EQ(nSamples * 100, samplesPerSec);
186 RTC_DCHECK_LE(nBytesPerSample * nSamples * nChannels,
187 AudioFrame::kMaxDataSizeBytes);
188
189 mixer_->Mix(nChannels, &mixed_frame_);
190 *elapsed_time_ms = mixed_frame_.elapsed_time_ms_;
191 *ntp_time_ms = mixed_frame_.ntp_time_ms_;
192
193 const auto error = audio_processing_->ProcessReverseStream(&mixed_frame_);
194 RTC_DCHECK_EQ(error, AudioProcessing::kNoError);
195
196 nSamplesOut = Resample(mixed_frame_, samplesPerSec, &render_resampler_,
197 static_cast<int16_t*>(audioSamples));
198 RTC_DCHECK_EQ(nSamplesOut, nChannels * nSamples);
199 return 0;
200}
201
202// Used by Chromium - same as NeedMorePlayData() but because Chrome has its
203// own APM instance, does not call audio_processing_->ProcessReverseStream().
204void AudioTransportImpl::PullRenderData(int bits_per_sample,
Yves Gerey665174f2018-06-19 15:03:05 +0200205 int sample_rate,
206 size_t number_of_channels,
207 size_t number_of_frames,
208 void* audio_data,
209 int64_t* elapsed_time_ms,
210 int64_t* ntp_time_ms) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100211 RTC_DCHECK_EQ(bits_per_sample, 16);
212 RTC_DCHECK_GE(number_of_channels, 1);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100213 RTC_DCHECK_GE(sample_rate, AudioProcessing::NativeRate::kSampleRate8kHz);
214
215 // 100 = 1 second / data duration (10 ms).
216 RTC_DCHECK_EQ(number_of_frames * 100, sample_rate);
217
218 // 8 = bits per byte.
219 RTC_DCHECK_LE(bits_per_sample / 8 * number_of_frames * number_of_channels,
220 AudioFrame::kMaxDataSizeBytes);
221 mixer_->Mix(number_of_channels, &mixed_frame_);
222 *elapsed_time_ms = mixed_frame_.elapsed_time_ms_;
223 *ntp_time_ms = mixed_frame_.ntp_time_ms_;
224
225 auto output_samples = Resample(mixed_frame_, sample_rate, &render_resampler_,
226 static_cast<int16_t*>(audio_data));
227 RTC_DCHECK_EQ(output_samples, number_of_channels * number_of_frames);
228}
229
230void AudioTransportImpl::UpdateSendingStreams(
Yves Gerey665174f2018-06-19 15:03:05 +0200231 std::vector<AudioSendStream*> streams,
232 int send_sample_rate_hz,
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100233 size_t send_num_channels) {
234 rtc::CritScope lock(&capture_lock_);
235 sending_streams_ = std::move(streams);
236 send_sample_rate_hz_ = send_sample_rate_hz;
237 send_num_channels_ = send_num_channels;
238}
239
240void AudioTransportImpl::SetStereoChannelSwapping(bool enable) {
241 rtc::CritScope lock(&capture_lock_);
242 swap_stereo_channels_ = enable;
243}
244
245bool AudioTransportImpl::typing_noise_detected() const {
246 rtc::CritScope lock(&capture_lock_);
247 return typing_noise_detected_;
248}
249} // namespace webrtc