blob: 7528bf0765c8cc18467baa43f42112b121d49181 [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"
Olga Sharonova09ceed22020-09-30 18:27:39 +020020#include "modules/async_audio_processing/async_audio_processing.h"
Per Åhgren71652f42020-03-17 13:23:58 +010021#include "modules/audio_processing/include/audio_frame_proxies.h"
Yves Gerey988cc082018-10-23 12:03:01 +020022#include "rtc_base/checks.h"
Olga Sharonova2d0ba282022-09-27 15:22:34 +020023#include "rtc_base/trace_event.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010024
25namespace webrtc {
26
27namespace {
28
29// We want to process at the lowest sample rate and channel count possible
30// without losing information. Choose the lowest native rate at least equal to
31// the minimum of input and codec rates, choose lowest channel count, and
32// configure the audio frame.
33void InitializeCaptureFrame(int input_sample_rate,
34 int send_sample_rate_hz,
35 size_t input_num_channels,
36 size_t send_num_channels,
37 AudioFrame* audio_frame) {
38 RTC_DCHECK(audio_frame);
39 int min_processing_rate_hz = std::min(input_sample_rate, send_sample_rate_hz);
40 for (int native_rate_hz : AudioProcessing::kNativeSampleRatesHz) {
41 audio_frame->sample_rate_hz_ = native_rate_hz;
42 if (audio_frame->sample_rate_hz_ >= min_processing_rate_hz) {
43 break;
44 }
45 }
46 audio_frame->num_channels_ = std::min(input_num_channels, send_num_channels);
47}
48
henrika649a3852017-12-22 13:58:29 +010049void ProcessCaptureFrame(uint32_t delay_ms,
Fredrik Solenberg2a877972017-12-15 16:42:15 +010050 bool key_pressed,
51 bool swap_stereo_channels,
52 AudioProcessing* audio_processing,
53 AudioFrame* audio_frame) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +010054 RTC_DCHECK(audio_frame);
Per Åhgrencc73ed32020-04-26 23:56:17 +020055 if (audio_processing) {
56 audio_processing->set_stream_delay_ms(delay_ms);
57 audio_processing->set_stream_key_pressed(key_pressed);
58 int error = ProcessAudioFrame(audio_processing, audio_frame);
Per Åhgren71652f42020-03-17 13:23:58 +010059
Per Åhgrencc73ed32020-04-26 23:56:17 +020060 RTC_DCHECK_EQ(0, error) << "ProcessStream() error: " << error;
61 }
62
Fredrik Solenberg2a877972017-12-15 16:42:15 +010063 if (swap_stereo_channels) {
64 AudioFrameOperations::SwapStereoChannels(audio_frame);
65 }
66}
67
Artem Titovb0ea6372021-07-26 11:47:07 +020068// Resample audio in `frame` to given sample rate preserving the
69// channel count and place the result in `destination`.
Fredrik Solenberg2a877972017-12-15 16:42:15 +010070int Resample(const AudioFrame& frame,
71 const int destination_sample_rate,
72 PushResampler<int16_t>* resampler,
73 int16_t* destination) {
74 const int number_of_channels = static_cast<int>(frame.num_channels_);
75 const int target_number_of_samples_per_channel =
76 destination_sample_rate / 100;
77 resampler->InitializeIfNeeded(frame.sample_rate_hz_, destination_sample_rate,
78 number_of_channels);
79
80 // TODO(yujo): make resampler take an AudioFrame, and add special case
81 // handling of muted frames.
82 return resampler->Resample(
83 frame.data(), frame.samples_per_channel_ * number_of_channels,
84 destination, number_of_channels * target_number_of_samples_per_channel);
85}
86} // namespace
87
Olga Sharonova09ceed22020-09-30 18:27:39 +020088AudioTransportImpl::AudioTransportImpl(
89 AudioMixer* mixer,
90 AudioProcessing* audio_processing,
91 AsyncAudioProcessing::Factory* async_audio_processing_factory)
92 : audio_processing_(audio_processing),
93 async_audio_processing_(
94 async_audio_processing_factory
95 ? async_audio_processing_factory->CreateAsyncAudioProcessing(
96 [this](std::unique_ptr<AudioFrame> frame) {
97 this->SendProcessedData(std::move(frame));
98 })
99 : nullptr),
100 mixer_(mixer) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100101 RTC_DCHECK(mixer);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100102}
103
104AudioTransportImpl::~AudioTransportImpl() {}
105
Olov Brändströmb732bd52022-01-28 15:07:39 +0100106int32_t AudioTransportImpl::RecordedDataIsAvailable(
107 const void* audio_data,
108 const size_t number_of_frames,
109 const size_t bytes_per_sample,
110 const size_t number_of_channels,
111 const uint32_t sample_rate,
112 const uint32_t audio_delay_milliseconds,
113 const int32_t clock_drift,
114 const uint32_t volume,
115 const bool key_pressed,
116 uint32_t& new_mic_volume) { // NOLINT: to avoid changing APIs
117 return RecordedDataIsAvailable(
118 audio_data, number_of_frames, bytes_per_sample, number_of_channels,
119 sample_rate, audio_delay_milliseconds, clock_drift, volume, key_pressed,
120 new_mic_volume, /* estimated_capture_time_ns */ 0);
121}
122
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100123// Not used in Chromium. Process captured audio and distribute to all sending
124// streams, and try to do this at the lowest possible sample rate.
125int32_t AudioTransportImpl::RecordedDataIsAvailable(
126 const void* audio_data,
127 const size_t number_of_frames,
128 const size_t bytes_per_sample,
129 const size_t number_of_channels,
130 const uint32_t sample_rate,
131 const uint32_t audio_delay_milliseconds,
132 const int32_t /*clock_drift*/,
henrika649a3852017-12-22 13:58:29 +0100133 const uint32_t /*volume*/,
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100134 const bool key_pressed,
Olov Brändströmb732bd52022-01-28 15:07:39 +0100135 uint32_t& /*new_mic_volume*/,
136 const int64_t
137 estimated_capture_time_ns) { // NOLINT: to avoid changing APIs
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100138 RTC_DCHECK(audio_data);
139 RTC_DCHECK_GE(number_of_channels, 1);
140 RTC_DCHECK_LE(number_of_channels, 2);
141 RTC_DCHECK_EQ(2 * number_of_channels, bytes_per_sample);
142 RTC_DCHECK_GE(sample_rate, AudioProcessing::NativeRate::kSampleRate8kHz);
143 // 100 = 1 second / data duration (10 ms).
144 RTC_DCHECK_EQ(number_of_frames * 100, sample_rate);
145 RTC_DCHECK_LE(bytes_per_sample * number_of_frames * number_of_channels,
146 AudioFrame::kMaxDataSizeBytes);
147
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100148 int send_sample_rate_hz = 0;
149 size_t send_num_channels = 0;
150 bool swap_stereo_channels = false;
151 {
Markus Handell62872802020-07-06 15:15:07 +0200152 MutexLock lock(&capture_lock_);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100153 send_sample_rate_hz = send_sample_rate_hz_;
154 send_num_channels = send_num_channels_;
155 swap_stereo_channels = swap_stereo_channels_;
156 }
157
158 std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
Yves Gerey665174f2018-06-19 15:03:05 +0200159 InitializeCaptureFrame(sample_rate, send_sample_rate_hz, number_of_channels,
160 send_num_channels, audio_frame.get());
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100161 voe::RemixAndResample(static_cast<const int16_t*>(audio_data),
162 number_of_frames, number_of_channels, sample_rate,
163 &capture_resampler_, audio_frame.get());
henrika649a3852017-12-22 13:58:29 +0100164 ProcessCaptureFrame(audio_delay_milliseconds, key_pressed,
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100165 swap_stereo_channels, audio_processing_,
166 audio_frame.get());
Olov Brändströmb732bd52022-01-28 15:07:39 +0100167 audio_frame->set_absolute_capture_timestamp_ms(estimated_capture_time_ns /
168 1000000);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100169
Olga Sharonova09ceed22020-09-30 18:27:39 +0200170 RTC_DCHECK_GT(audio_frame->samples_per_channel_, 0);
171 if (async_audio_processing_)
172 async_audio_processing_->Process(std::move(audio_frame));
173 else
174 SendProcessedData(std::move(audio_frame));
175
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100176 return 0;
177}
178
Olga Sharonova09ceed22020-09-30 18:27:39 +0200179void AudioTransportImpl::SendProcessedData(
180 std::unique_ptr<AudioFrame> audio_frame) {
Olga Sharonova2d0ba282022-09-27 15:22:34 +0200181 TRACE_EVENT0("webrtc", "AudioTransportImpl::SendProcessedData");
Olga Sharonova09ceed22020-09-30 18:27:39 +0200182 RTC_DCHECK_GT(audio_frame->samples_per_channel_, 0);
183 MutexLock lock(&capture_lock_);
184 if (audio_senders_.empty())
185 return;
186
187 auto it = audio_senders_.begin();
188 while (++it != audio_senders_.end()) {
189 auto audio_frame_copy = std::make_unique<AudioFrame>();
190 audio_frame_copy->CopyFrom(*audio_frame);
191 (*it)->SendAudioData(std::move(audio_frame_copy));
192 }
193 // Send the original frame to the first stream w/o copying.
194 (*audio_senders_.begin())->SendAudioData(std::move(audio_frame));
195}
196
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100197// Mix all received streams, feed the result to the AudioProcessing module, then
198// resample the result to the requested output rate.
199int32_t AudioTransportImpl::NeedMorePlayData(const size_t nSamples,
Yves Gerey665174f2018-06-19 15:03:05 +0200200 const size_t nBytesPerSample,
201 const size_t nChannels,
202 const uint32_t samplesPerSec,
203 void* audioSamples,
204 size_t& nSamplesOut,
205 int64_t* elapsed_time_ms,
206 int64_t* ntp_time_ms) {
Olga Sharonova2d0ba282022-09-27 15:22:34 +0200207 TRACE_EVENT0("webrtc", "AudioTransportImpl::SendProcessedData");
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100208 RTC_DCHECK_EQ(sizeof(int16_t) * nChannels, nBytesPerSample);
209 RTC_DCHECK_GE(nChannels, 1);
210 RTC_DCHECK_LE(nChannels, 2);
211 RTC_DCHECK_GE(
212 samplesPerSec,
213 static_cast<uint32_t>(AudioProcessing::NativeRate::kSampleRate8kHz));
214
215 // 100 = 1 second / data duration (10 ms).
216 RTC_DCHECK_EQ(nSamples * 100, samplesPerSec);
217 RTC_DCHECK_LE(nBytesPerSample * nSamples * nChannels,
218 AudioFrame::kMaxDataSizeBytes);
219
220 mixer_->Mix(nChannels, &mixed_frame_);
221 *elapsed_time_ms = mixed_frame_.elapsed_time_ms_;
222 *ntp_time_ms = mixed_frame_.ntp_time_ms_;
223
Per Åhgrencc73ed32020-04-26 23:56:17 +0200224 if (audio_processing_) {
225 const auto error =
226 ProcessReverseAudioFrame(audio_processing_, &mixed_frame_);
227 RTC_DCHECK_EQ(error, AudioProcessing::kNoError);
228 }
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100229
230 nSamplesOut = Resample(mixed_frame_, samplesPerSec, &render_resampler_,
231 static_cast<int16_t*>(audioSamples));
232 RTC_DCHECK_EQ(nSamplesOut, nChannels * nSamples);
233 return 0;
234}
235
236// Used by Chromium - same as NeedMorePlayData() but because Chrome has its
237// own APM instance, does not call audio_processing_->ProcessReverseStream().
238void AudioTransportImpl::PullRenderData(int bits_per_sample,
Yves Gerey665174f2018-06-19 15:03:05 +0200239 int sample_rate,
240 size_t number_of_channels,
241 size_t number_of_frames,
242 void* audio_data,
243 int64_t* elapsed_time_ms,
244 int64_t* ntp_time_ms) {
Olga Sharonova2d0ba282022-09-27 15:22:34 +0200245 TRACE_EVENT2("webrtc", "AudioTransportImpl::PullRenderData", "sample_rate",
246 sample_rate, "number_of_frames", number_of_frames);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100247 RTC_DCHECK_EQ(bits_per_sample, 16);
248 RTC_DCHECK_GE(number_of_channels, 1);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100249 RTC_DCHECK_GE(sample_rate, AudioProcessing::NativeRate::kSampleRate8kHz);
250
251 // 100 = 1 second / data duration (10 ms).
252 RTC_DCHECK_EQ(number_of_frames * 100, sample_rate);
253
254 // 8 = bits per byte.
255 RTC_DCHECK_LE(bits_per_sample / 8 * number_of_frames * number_of_channels,
256 AudioFrame::kMaxDataSizeBytes);
257 mixer_->Mix(number_of_channels, &mixed_frame_);
258 *elapsed_time_ms = mixed_frame_.elapsed_time_ms_;
259 *ntp_time_ms = mixed_frame_.ntp_time_ms_;
260
261 auto output_samples = Resample(mixed_frame_, sample_rate, &render_resampler_,
262 static_cast<int16_t*>(audio_data));
263 RTC_DCHECK_EQ(output_samples, number_of_channels * number_of_frames);
264}
265
Tim Nab8c775a2020-01-10 10:33:05 -0800266void AudioTransportImpl::UpdateAudioSenders(std::vector<AudioSender*> senders,
267 int send_sample_rate_hz,
268 size_t send_num_channels) {
Markus Handell62872802020-07-06 15:15:07 +0200269 MutexLock lock(&capture_lock_);
Tim Nab8c775a2020-01-10 10:33:05 -0800270 audio_senders_ = std::move(senders);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100271 send_sample_rate_hz_ = send_sample_rate_hz;
272 send_num_channels_ = send_num_channels;
273}
274
275void AudioTransportImpl::SetStereoChannelSwapping(bool enable) {
Markus Handell62872802020-07-06 15:15:07 +0200276 MutexLock lock(&capture_lock_);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100277 swap_stereo_channels_ = enable;
278}
279
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100280} // namespace webrtc