xref: /aosp_15_r20/external/webrtc/audio/audio_transport_impl.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #include "audio/audio_transport_impl.h"
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker #include <algorithm>
14*d9f75844SAndroid Build Coastguard Worker #include <memory>
15*d9f75844SAndroid Build Coastguard Worker #include <utility>
16*d9f75844SAndroid Build Coastguard Worker 
17*d9f75844SAndroid Build Coastguard Worker #include "audio/remix_resample.h"
18*d9f75844SAndroid Build Coastguard Worker #include "audio/utility/audio_frame_operations.h"
19*d9f75844SAndroid Build Coastguard Worker #include "call/audio_sender.h"
20*d9f75844SAndroid Build Coastguard Worker #include "modules/async_audio_processing/async_audio_processing.h"
21*d9f75844SAndroid Build Coastguard Worker #include "modules/audio_processing/include/audio_frame_proxies.h"
22*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
23*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/trace_event.h"
24*d9f75844SAndroid Build Coastguard Worker 
25*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
26*d9f75844SAndroid Build Coastguard Worker 
27*d9f75844SAndroid Build Coastguard Worker namespace {
28*d9f75844SAndroid Build Coastguard Worker 
29*d9f75844SAndroid Build Coastguard Worker // We want to process at the lowest sample rate and channel count possible
30*d9f75844SAndroid Build Coastguard Worker // without losing information. Choose the lowest native rate at least equal to
31*d9f75844SAndroid Build Coastguard Worker // the minimum of input and codec rates, choose lowest channel count, and
32*d9f75844SAndroid Build Coastguard Worker // configure the audio frame.
InitializeCaptureFrame(int input_sample_rate,int send_sample_rate_hz,size_t input_num_channels,size_t send_num_channels,AudioFrame * audio_frame)33*d9f75844SAndroid Build Coastguard Worker void InitializeCaptureFrame(int input_sample_rate,
34*d9f75844SAndroid Build Coastguard Worker                             int send_sample_rate_hz,
35*d9f75844SAndroid Build Coastguard Worker                             size_t input_num_channels,
36*d9f75844SAndroid Build Coastguard Worker                             size_t send_num_channels,
37*d9f75844SAndroid Build Coastguard Worker                             AudioFrame* audio_frame) {
38*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(audio_frame);
39*d9f75844SAndroid Build Coastguard Worker   int min_processing_rate_hz = std::min(input_sample_rate, send_sample_rate_hz);
40*d9f75844SAndroid Build Coastguard Worker   for (int native_rate_hz : AudioProcessing::kNativeSampleRatesHz) {
41*d9f75844SAndroid Build Coastguard Worker     audio_frame->sample_rate_hz_ = native_rate_hz;
42*d9f75844SAndroid Build Coastguard Worker     if (audio_frame->sample_rate_hz_ >= min_processing_rate_hz) {
43*d9f75844SAndroid Build Coastguard Worker       break;
44*d9f75844SAndroid Build Coastguard Worker     }
45*d9f75844SAndroid Build Coastguard Worker   }
46*d9f75844SAndroid Build Coastguard Worker   audio_frame->num_channels_ = std::min(input_num_channels, send_num_channels);
47*d9f75844SAndroid Build Coastguard Worker }
48*d9f75844SAndroid Build Coastguard Worker 
ProcessCaptureFrame(uint32_t delay_ms,bool key_pressed,bool swap_stereo_channels,AudioProcessing * audio_processing,AudioFrame * audio_frame)49*d9f75844SAndroid Build Coastguard Worker void ProcessCaptureFrame(uint32_t delay_ms,
50*d9f75844SAndroid Build Coastguard Worker                          bool key_pressed,
51*d9f75844SAndroid Build Coastguard Worker                          bool swap_stereo_channels,
52*d9f75844SAndroid Build Coastguard Worker                          AudioProcessing* audio_processing,
53*d9f75844SAndroid Build Coastguard Worker                          AudioFrame* audio_frame) {
54*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(audio_frame);
55*d9f75844SAndroid Build Coastguard Worker   if (audio_processing) {
56*d9f75844SAndroid Build Coastguard Worker     audio_processing->set_stream_delay_ms(delay_ms);
57*d9f75844SAndroid Build Coastguard Worker     audio_processing->set_stream_key_pressed(key_pressed);
58*d9f75844SAndroid Build Coastguard Worker     int error = ProcessAudioFrame(audio_processing, audio_frame);
59*d9f75844SAndroid Build Coastguard Worker 
60*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_EQ(0, error) << "ProcessStream() error: " << error;
61*d9f75844SAndroid Build Coastguard Worker   }
62*d9f75844SAndroid Build Coastguard Worker 
63*d9f75844SAndroid Build Coastguard Worker   if (swap_stereo_channels) {
64*d9f75844SAndroid Build Coastguard Worker     AudioFrameOperations::SwapStereoChannels(audio_frame);
65*d9f75844SAndroid Build Coastguard Worker   }
66*d9f75844SAndroid Build Coastguard Worker }
67*d9f75844SAndroid Build Coastguard Worker 
68*d9f75844SAndroid Build Coastguard Worker // Resample audio in `frame` to given sample rate preserving the
69*d9f75844SAndroid Build Coastguard Worker // channel count and place the result in `destination`.
Resample(const AudioFrame & frame,const int destination_sample_rate,PushResampler<int16_t> * resampler,int16_t * destination)70*d9f75844SAndroid Build Coastguard Worker int Resample(const AudioFrame& frame,
71*d9f75844SAndroid Build Coastguard Worker              const int destination_sample_rate,
72*d9f75844SAndroid Build Coastguard Worker              PushResampler<int16_t>* resampler,
73*d9f75844SAndroid Build Coastguard Worker              int16_t* destination) {
74*d9f75844SAndroid Build Coastguard Worker   TRACE_EVENT2("webrtc", "Resample", "frame sample rate", frame.sample_rate_hz_,
75*d9f75844SAndroid Build Coastguard Worker                "destination_sample_rate", destination_sample_rate);
76*d9f75844SAndroid Build Coastguard Worker   const int number_of_channels = static_cast<int>(frame.num_channels_);
77*d9f75844SAndroid Build Coastguard Worker   const int target_number_of_samples_per_channel =
78*d9f75844SAndroid Build Coastguard Worker       destination_sample_rate / 100;
79*d9f75844SAndroid Build Coastguard Worker   resampler->InitializeIfNeeded(frame.sample_rate_hz_, destination_sample_rate,
80*d9f75844SAndroid Build Coastguard Worker                                 number_of_channels);
81*d9f75844SAndroid Build Coastguard Worker 
82*d9f75844SAndroid Build Coastguard Worker   // TODO(yujo): make resampler take an AudioFrame, and add special case
83*d9f75844SAndroid Build Coastguard Worker   // handling of muted frames.
84*d9f75844SAndroid Build Coastguard Worker   return resampler->Resample(
85*d9f75844SAndroid Build Coastguard Worker       frame.data(), frame.samples_per_channel_ * number_of_channels,
86*d9f75844SAndroid Build Coastguard Worker       destination, number_of_channels * target_number_of_samples_per_channel);
87*d9f75844SAndroid Build Coastguard Worker }
88*d9f75844SAndroid Build Coastguard Worker }  // namespace
89*d9f75844SAndroid Build Coastguard Worker 
AudioTransportImpl(AudioMixer * mixer,AudioProcessing * audio_processing,AsyncAudioProcessing::Factory * async_audio_processing_factory)90*d9f75844SAndroid Build Coastguard Worker AudioTransportImpl::AudioTransportImpl(
91*d9f75844SAndroid Build Coastguard Worker     AudioMixer* mixer,
92*d9f75844SAndroid Build Coastguard Worker     AudioProcessing* audio_processing,
93*d9f75844SAndroid Build Coastguard Worker     AsyncAudioProcessing::Factory* async_audio_processing_factory)
94*d9f75844SAndroid Build Coastguard Worker     : audio_processing_(audio_processing),
95*d9f75844SAndroid Build Coastguard Worker       async_audio_processing_(
96*d9f75844SAndroid Build Coastguard Worker           async_audio_processing_factory
97*d9f75844SAndroid Build Coastguard Worker               ? async_audio_processing_factory->CreateAsyncAudioProcessing(
98*d9f75844SAndroid Build Coastguard Worker                     [this](std::unique_ptr<AudioFrame> frame) {
99*d9f75844SAndroid Build Coastguard Worker                       this->SendProcessedData(std::move(frame));
100*d9f75844SAndroid Build Coastguard Worker                     })
101*d9f75844SAndroid Build Coastguard Worker               : nullptr),
102*d9f75844SAndroid Build Coastguard Worker       mixer_(mixer) {
103*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(mixer);
104*d9f75844SAndroid Build Coastguard Worker }
105*d9f75844SAndroid Build Coastguard Worker 
~AudioTransportImpl()106*d9f75844SAndroid Build Coastguard Worker AudioTransportImpl::~AudioTransportImpl() {}
107*d9f75844SAndroid Build Coastguard Worker 
RecordedDataIsAvailable(const void * audio_data,const size_t number_of_frames,const size_t bytes_per_sample,const size_t number_of_channels,const uint32_t sample_rate,const uint32_t audio_delay_milliseconds,const int32_t clock_drift,const uint32_t volume,const bool key_pressed,uint32_t & new_mic_volume)108*d9f75844SAndroid Build Coastguard Worker int32_t AudioTransportImpl::RecordedDataIsAvailable(
109*d9f75844SAndroid Build Coastguard Worker     const void* audio_data,
110*d9f75844SAndroid Build Coastguard Worker     const size_t number_of_frames,
111*d9f75844SAndroid Build Coastguard Worker     const size_t bytes_per_sample,
112*d9f75844SAndroid Build Coastguard Worker     const size_t number_of_channels,
113*d9f75844SAndroid Build Coastguard Worker     const uint32_t sample_rate,
114*d9f75844SAndroid Build Coastguard Worker     const uint32_t audio_delay_milliseconds,
115*d9f75844SAndroid Build Coastguard Worker     const int32_t clock_drift,
116*d9f75844SAndroid Build Coastguard Worker     const uint32_t volume,
117*d9f75844SAndroid Build Coastguard Worker     const bool key_pressed,
118*d9f75844SAndroid Build Coastguard Worker     uint32_t& new_mic_volume) {  // NOLINT: to avoid changing APIs
119*d9f75844SAndroid Build Coastguard Worker   return RecordedDataIsAvailable(
120*d9f75844SAndroid Build Coastguard Worker       audio_data, number_of_frames, bytes_per_sample, number_of_channels,
121*d9f75844SAndroid Build Coastguard Worker       sample_rate, audio_delay_milliseconds, clock_drift, volume, key_pressed,
122*d9f75844SAndroid Build Coastguard Worker       new_mic_volume, /* estimated_capture_time_ns */ 0);
123*d9f75844SAndroid Build Coastguard Worker }
124*d9f75844SAndroid Build Coastguard Worker 
125*d9f75844SAndroid Build Coastguard Worker // Not used in Chromium. Process captured audio and distribute to all sending
126*d9f75844SAndroid Build Coastguard Worker // streams, and try to do this at the lowest possible sample rate.
RecordedDataIsAvailable(const void * audio_data,const size_t number_of_frames,const size_t bytes_per_sample,const size_t number_of_channels,const uint32_t sample_rate,const uint32_t audio_delay_milliseconds,const int32_t,const uint32_t,const bool key_pressed,uint32_t &,const int64_t estimated_capture_time_ns)127*d9f75844SAndroid Build Coastguard Worker int32_t AudioTransportImpl::RecordedDataIsAvailable(
128*d9f75844SAndroid Build Coastguard Worker     const void* audio_data,
129*d9f75844SAndroid Build Coastguard Worker     const size_t number_of_frames,
130*d9f75844SAndroid Build Coastguard Worker     const size_t bytes_per_sample,
131*d9f75844SAndroid Build Coastguard Worker     const size_t number_of_channels,
132*d9f75844SAndroid Build Coastguard Worker     const uint32_t sample_rate,
133*d9f75844SAndroid Build Coastguard Worker     const uint32_t audio_delay_milliseconds,
134*d9f75844SAndroid Build Coastguard Worker     const int32_t /*clock_drift*/,
135*d9f75844SAndroid Build Coastguard Worker     const uint32_t /*volume*/,
136*d9f75844SAndroid Build Coastguard Worker     const bool key_pressed,
137*d9f75844SAndroid Build Coastguard Worker     uint32_t& /*new_mic_volume*/,
138*d9f75844SAndroid Build Coastguard Worker     const int64_t
139*d9f75844SAndroid Build Coastguard Worker         estimated_capture_time_ns) {  // NOLINT: to avoid changing APIs
140*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(audio_data);
141*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_GE(number_of_channels, 1);
142*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_LE(number_of_channels, 2);
143*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_EQ(2 * number_of_channels, bytes_per_sample);
144*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_GE(sample_rate, AudioProcessing::NativeRate::kSampleRate8kHz);
145*d9f75844SAndroid Build Coastguard Worker   // 100 = 1 second / data duration (10 ms).
146*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_EQ(number_of_frames * 100, sample_rate);
147*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_LE(bytes_per_sample * number_of_frames * number_of_channels,
148*d9f75844SAndroid Build Coastguard Worker                 AudioFrame::kMaxDataSizeBytes);
149*d9f75844SAndroid Build Coastguard Worker 
150*d9f75844SAndroid Build Coastguard Worker   int send_sample_rate_hz = 0;
151*d9f75844SAndroid Build Coastguard Worker   size_t send_num_channels = 0;
152*d9f75844SAndroid Build Coastguard Worker   bool swap_stereo_channels = false;
153*d9f75844SAndroid Build Coastguard Worker   {
154*d9f75844SAndroid Build Coastguard Worker     MutexLock lock(&capture_lock_);
155*d9f75844SAndroid Build Coastguard Worker     send_sample_rate_hz = send_sample_rate_hz_;
156*d9f75844SAndroid Build Coastguard Worker     send_num_channels = send_num_channels_;
157*d9f75844SAndroid Build Coastguard Worker     swap_stereo_channels = swap_stereo_channels_;
158*d9f75844SAndroid Build Coastguard Worker   }
159*d9f75844SAndroid Build Coastguard Worker 
160*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<AudioFrame> audio_frame(new AudioFrame());
161*d9f75844SAndroid Build Coastguard Worker   InitializeCaptureFrame(sample_rate, send_sample_rate_hz, number_of_channels,
162*d9f75844SAndroid Build Coastguard Worker                          send_num_channels, audio_frame.get());
163*d9f75844SAndroid Build Coastguard Worker   voe::RemixAndResample(static_cast<const int16_t*>(audio_data),
164*d9f75844SAndroid Build Coastguard Worker                         number_of_frames, number_of_channels, sample_rate,
165*d9f75844SAndroid Build Coastguard Worker                         &capture_resampler_, audio_frame.get());
166*d9f75844SAndroid Build Coastguard Worker   ProcessCaptureFrame(audio_delay_milliseconds, key_pressed,
167*d9f75844SAndroid Build Coastguard Worker                       swap_stereo_channels, audio_processing_,
168*d9f75844SAndroid Build Coastguard Worker                       audio_frame.get());
169*d9f75844SAndroid Build Coastguard Worker   audio_frame->set_absolute_capture_timestamp_ms(estimated_capture_time_ns /
170*d9f75844SAndroid Build Coastguard Worker                                                  1000000);
171*d9f75844SAndroid Build Coastguard Worker 
172*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_GT(audio_frame->samples_per_channel_, 0);
173*d9f75844SAndroid Build Coastguard Worker   if (async_audio_processing_)
174*d9f75844SAndroid Build Coastguard Worker     async_audio_processing_->Process(std::move(audio_frame));
175*d9f75844SAndroid Build Coastguard Worker   else
176*d9f75844SAndroid Build Coastguard Worker     SendProcessedData(std::move(audio_frame));
177*d9f75844SAndroid Build Coastguard Worker 
178*d9f75844SAndroid Build Coastguard Worker   return 0;
179*d9f75844SAndroid Build Coastguard Worker }
180*d9f75844SAndroid Build Coastguard Worker 
SendProcessedData(std::unique_ptr<AudioFrame> audio_frame)181*d9f75844SAndroid Build Coastguard Worker void AudioTransportImpl::SendProcessedData(
182*d9f75844SAndroid Build Coastguard Worker     std::unique_ptr<AudioFrame> audio_frame) {
183*d9f75844SAndroid Build Coastguard Worker   TRACE_EVENT0("webrtc", "AudioTransportImpl::SendProcessedData");
184*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_GT(audio_frame->samples_per_channel_, 0);
185*d9f75844SAndroid Build Coastguard Worker   MutexLock lock(&capture_lock_);
186*d9f75844SAndroid Build Coastguard Worker   if (audio_senders_.empty())
187*d9f75844SAndroid Build Coastguard Worker     return;
188*d9f75844SAndroid Build Coastguard Worker 
189*d9f75844SAndroid Build Coastguard Worker   auto it = audio_senders_.begin();
190*d9f75844SAndroid Build Coastguard Worker   while (++it != audio_senders_.end()) {
191*d9f75844SAndroid Build Coastguard Worker     auto audio_frame_copy = std::make_unique<AudioFrame>();
192*d9f75844SAndroid Build Coastguard Worker     audio_frame_copy->CopyFrom(*audio_frame);
193*d9f75844SAndroid Build Coastguard Worker     (*it)->SendAudioData(std::move(audio_frame_copy));
194*d9f75844SAndroid Build Coastguard Worker   }
195*d9f75844SAndroid Build Coastguard Worker   // Send the original frame to the first stream w/o copying.
196*d9f75844SAndroid Build Coastguard Worker   (*audio_senders_.begin())->SendAudioData(std::move(audio_frame));
197*d9f75844SAndroid Build Coastguard Worker }
198*d9f75844SAndroid Build Coastguard Worker 
199*d9f75844SAndroid Build Coastguard Worker // Mix all received streams, feed the result to the AudioProcessing module, then
200*d9f75844SAndroid Build Coastguard Worker // resample the result to the requested output rate.
NeedMorePlayData(const size_t nSamples,const size_t nBytesPerSample,const size_t nChannels,const uint32_t samplesPerSec,void * audioSamples,size_t & nSamplesOut,int64_t * elapsed_time_ms,int64_t * ntp_time_ms)201*d9f75844SAndroid Build Coastguard Worker int32_t AudioTransportImpl::NeedMorePlayData(const size_t nSamples,
202*d9f75844SAndroid Build Coastguard Worker                                              const size_t nBytesPerSample,
203*d9f75844SAndroid Build Coastguard Worker                                              const size_t nChannels,
204*d9f75844SAndroid Build Coastguard Worker                                              const uint32_t samplesPerSec,
205*d9f75844SAndroid Build Coastguard Worker                                              void* audioSamples,
206*d9f75844SAndroid Build Coastguard Worker                                              size_t& nSamplesOut,
207*d9f75844SAndroid Build Coastguard Worker                                              int64_t* elapsed_time_ms,
208*d9f75844SAndroid Build Coastguard Worker                                              int64_t* ntp_time_ms) {
209*d9f75844SAndroid Build Coastguard Worker   TRACE_EVENT0("webrtc", "AudioTransportImpl::SendProcessedData");
210*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_EQ(sizeof(int16_t) * nChannels, nBytesPerSample);
211*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_GE(nChannels, 1);
212*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_LE(nChannels, 2);
213*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_GE(
214*d9f75844SAndroid Build Coastguard Worker       samplesPerSec,
215*d9f75844SAndroid Build Coastguard Worker       static_cast<uint32_t>(AudioProcessing::NativeRate::kSampleRate8kHz));
216*d9f75844SAndroid Build Coastguard Worker 
217*d9f75844SAndroid Build Coastguard Worker   // 100 = 1 second / data duration (10 ms).
218*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_EQ(nSamples * 100, samplesPerSec);
219*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_LE(nBytesPerSample * nSamples * nChannels,
220*d9f75844SAndroid Build Coastguard Worker                 AudioFrame::kMaxDataSizeBytes);
221*d9f75844SAndroid Build Coastguard Worker 
222*d9f75844SAndroid Build Coastguard Worker   mixer_->Mix(nChannels, &mixed_frame_);
223*d9f75844SAndroid Build Coastguard Worker   *elapsed_time_ms = mixed_frame_.elapsed_time_ms_;
224*d9f75844SAndroid Build Coastguard Worker   *ntp_time_ms = mixed_frame_.ntp_time_ms_;
225*d9f75844SAndroid Build Coastguard Worker 
226*d9f75844SAndroid Build Coastguard Worker   if (audio_processing_) {
227*d9f75844SAndroid Build Coastguard Worker     const auto error =
228*d9f75844SAndroid Build Coastguard Worker         ProcessReverseAudioFrame(audio_processing_, &mixed_frame_);
229*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_EQ(error, AudioProcessing::kNoError);
230*d9f75844SAndroid Build Coastguard Worker   }
231*d9f75844SAndroid Build Coastguard Worker 
232*d9f75844SAndroid Build Coastguard Worker   nSamplesOut = Resample(mixed_frame_, samplesPerSec, &render_resampler_,
233*d9f75844SAndroid Build Coastguard Worker                          static_cast<int16_t*>(audioSamples));
234*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_EQ(nSamplesOut, nChannels * nSamples);
235*d9f75844SAndroid Build Coastguard Worker   return 0;
236*d9f75844SAndroid Build Coastguard Worker }
237*d9f75844SAndroid Build Coastguard Worker 
238*d9f75844SAndroid Build Coastguard Worker // Used by Chromium - same as NeedMorePlayData() but because Chrome has its
239*d9f75844SAndroid Build Coastguard Worker // own APM instance, does not call audio_processing_->ProcessReverseStream().
PullRenderData(int bits_per_sample,int sample_rate,size_t number_of_channels,size_t number_of_frames,void * audio_data,int64_t * elapsed_time_ms,int64_t * ntp_time_ms)240*d9f75844SAndroid Build Coastguard Worker void AudioTransportImpl::PullRenderData(int bits_per_sample,
241*d9f75844SAndroid Build Coastguard Worker                                         int sample_rate,
242*d9f75844SAndroid Build Coastguard Worker                                         size_t number_of_channels,
243*d9f75844SAndroid Build Coastguard Worker                                         size_t number_of_frames,
244*d9f75844SAndroid Build Coastguard Worker                                         void* audio_data,
245*d9f75844SAndroid Build Coastguard Worker                                         int64_t* elapsed_time_ms,
246*d9f75844SAndroid Build Coastguard Worker                                         int64_t* ntp_time_ms) {
247*d9f75844SAndroid Build Coastguard Worker   TRACE_EVENT2("webrtc", "AudioTransportImpl::PullRenderData", "sample_rate",
248*d9f75844SAndroid Build Coastguard Worker                sample_rate, "number_of_frames", number_of_frames);
249*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_EQ(bits_per_sample, 16);
250*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_GE(number_of_channels, 1);
251*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_GE(sample_rate, AudioProcessing::NativeRate::kSampleRate8kHz);
252*d9f75844SAndroid Build Coastguard Worker 
253*d9f75844SAndroid Build Coastguard Worker   // 100 = 1 second / data duration (10 ms).
254*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_EQ(number_of_frames * 100, sample_rate);
255*d9f75844SAndroid Build Coastguard Worker 
256*d9f75844SAndroid Build Coastguard Worker   // 8 = bits per byte.
257*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_LE(bits_per_sample / 8 * number_of_frames * number_of_channels,
258*d9f75844SAndroid Build Coastguard Worker                 AudioFrame::kMaxDataSizeBytes);
259*d9f75844SAndroid Build Coastguard Worker   mixer_->Mix(number_of_channels, &mixed_frame_);
260*d9f75844SAndroid Build Coastguard Worker   *elapsed_time_ms = mixed_frame_.elapsed_time_ms_;
261*d9f75844SAndroid Build Coastguard Worker   *ntp_time_ms = mixed_frame_.ntp_time_ms_;
262*d9f75844SAndroid Build Coastguard Worker 
263*d9f75844SAndroid Build Coastguard Worker   auto output_samples = Resample(mixed_frame_, sample_rate, &render_resampler_,
264*d9f75844SAndroid Build Coastguard Worker                                  static_cast<int16_t*>(audio_data));
265*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_EQ(output_samples, number_of_channels * number_of_frames);
266*d9f75844SAndroid Build Coastguard Worker }
267*d9f75844SAndroid Build Coastguard Worker 
UpdateAudioSenders(std::vector<AudioSender * > senders,int send_sample_rate_hz,size_t send_num_channels)268*d9f75844SAndroid Build Coastguard Worker void AudioTransportImpl::UpdateAudioSenders(std::vector<AudioSender*> senders,
269*d9f75844SAndroid Build Coastguard Worker                                             int send_sample_rate_hz,
270*d9f75844SAndroid Build Coastguard Worker                                             size_t send_num_channels) {
271*d9f75844SAndroid Build Coastguard Worker   MutexLock lock(&capture_lock_);
272*d9f75844SAndroid Build Coastguard Worker   audio_senders_ = std::move(senders);
273*d9f75844SAndroid Build Coastguard Worker   send_sample_rate_hz_ = send_sample_rate_hz;
274*d9f75844SAndroid Build Coastguard Worker   send_num_channels_ = send_num_channels;
275*d9f75844SAndroid Build Coastguard Worker }
276*d9f75844SAndroid Build Coastguard Worker 
SetStereoChannelSwapping(bool enable)277*d9f75844SAndroid Build Coastguard Worker void AudioTransportImpl::SetStereoChannelSwapping(bool enable) {
278*d9f75844SAndroid Build Coastguard Worker   MutexLock lock(&capture_lock_);
279*d9f75844SAndroid Build Coastguard Worker   swap_stereo_channels_ = enable;
280*d9f75844SAndroid Build Coastguard Worker }
281*d9f75844SAndroid Build Coastguard Worker 
282*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
283