blob: 73b009154035ad61481396d91fa05671f5a5e9c9 [file] [log] [blame]
aleloi7713f332016-11-17 14:28:591/*
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 "webrtc/audio/audio_transport_proxy.h"
12
13namespace webrtc {
14
aleloi441ecb32016-11-22 14:42:5315namespace {
16// Resample audio in |frame| to given sample rate preserving the
17// channel count and place the result in |destination|.
18int Resample(const AudioFrame& frame,
19 const int destination_sample_rate,
20 PushResampler<int16_t>* resampler,
21 int16_t* destination) {
22 const int number_of_channels = static_cast<int>(frame.num_channels_);
23 const int target_number_of_samples_per_channel =
24 destination_sample_rate / 100;
25 resampler->InitializeIfNeeded(frame.sample_rate_hz_, destination_sample_rate,
26 number_of_channels);
27
yujo7d973072017-06-12 19:45:3228 // TODO(yujo): make resampler take an AudioFrame, and add special case
29 // handling of muted frames.
aleloi441ecb32016-11-22 14:42:5330 return resampler->Resample(
yujo7d973072017-06-12 19:45:3231 frame.data(), frame.samples_per_channel_ * number_of_channels,
32 destination, number_of_channels * target_number_of_samples_per_channel);
aleloi441ecb32016-11-22 14:42:5333}
34} // namespace
35
aleloi7713f332016-11-17 14:28:5936AudioTransportProxy::AudioTransportProxy(AudioTransport* voe_audio_transport,
peah588f7612017-06-29 15:32:0937 AudioProcessing* audio_processing,
aleloi7713f332016-11-17 14:28:5938 AudioMixer* mixer)
peah588f7612017-06-29 15:32:0939 : voe_audio_transport_(voe_audio_transport),
40 audio_processing_(audio_processing),
41 mixer_(mixer) {
aleloi7713f332016-11-17 14:28:5942 RTC_DCHECK(voe_audio_transport);
peah588f7612017-06-29 15:32:0943 RTC_DCHECK(audio_processing);
aleloi441ecb32016-11-22 14:42:5344 RTC_DCHECK(mixer);
aleloi7713f332016-11-17 14:28:5945}
46
47AudioTransportProxy::~AudioTransportProxy() {}
48
49int32_t AudioTransportProxy::RecordedDataIsAvailable(
50 const void* audioSamples,
51 const size_t nSamples,
52 const size_t nBytesPerSample,
53 const size_t nChannels,
54 const uint32_t samplesPerSec,
55 const uint32_t totalDelayMS,
56 const int32_t clockDrift,
57 const uint32_t currentMicLevel,
58 const bool keyPressed,
oprypin0f20d582017-03-09 14:25:0659 uint32_t& newMicLevel) { // NOLINT: to avoid changing APIs
aleloi7713f332016-11-17 14:28:5960 // Pass call through to original audio transport instance.
61 return voe_audio_transport_->RecordedDataIsAvailable(
62 audioSamples, nSamples, nBytesPerSample, nChannels, samplesPerSec,
63 totalDelayMS, clockDrift, currentMicLevel, keyPressed, newMicLevel);
64}
65
66int32_t AudioTransportProxy::NeedMorePlayData(const size_t nSamples,
67 const size_t nBytesPerSample,
68 const size_t nChannels,
69 const uint32_t samplesPerSec,
70 void* audioSamples,
71 size_t& nSamplesOut,
72 int64_t* elapsed_time_ms,
73 int64_t* ntp_time_ms) {
74 RTC_DCHECK_EQ(sizeof(int16_t) * nChannels, nBytesPerSample);
kwiberg4cb29f62016-11-28 23:21:3975 RTC_DCHECK_GE(nChannels, 1);
76 RTC_DCHECK_LE(nChannels, 2);
aleloi7713f332016-11-17 14:28:5977 RTC_DCHECK_GE(
78 samplesPerSec,
79 static_cast<uint32_t>(AudioProcessing::NativeRate::kSampleRate8kHz));
aleloi441ecb32016-11-22 14:42:5380
81 // 100 = 1 second / data duration (10 ms).
aleloi7713f332016-11-17 14:28:5982 RTC_DCHECK_EQ(nSamples * 100, samplesPerSec);
83 RTC_DCHECK_LE(nBytesPerSample * nSamples * nChannels,
yujo7d973072017-06-12 19:45:3284 AudioFrame::kMaxDataSizeBytes);
aleloi7713f332016-11-17 14:28:5985
aleloi441ecb32016-11-22 14:42:5386 mixer_->Mix(nChannels, &mixed_frame_);
87 *elapsed_time_ms = mixed_frame_.elapsed_time_ms_;
88 *ntp_time_ms = mixed_frame_.ntp_time_ms_;
89
peah588f7612017-06-29 15:32:0990 const auto error = audio_processing_->ProcessReverseStream(&mixed_frame_);
aleloi441ecb32016-11-22 14:42:5391 RTC_DCHECK_EQ(error, AudioProcessing::kNoError);
92
93 nSamplesOut = Resample(mixed_frame_, samplesPerSec, &resampler_,
94 static_cast<int16_t*>(audioSamples));
95 RTC_DCHECK_EQ(nSamplesOut, nChannels * nSamples);
96 return 0;
aleloi7713f332016-11-17 14:28:5997}
98
99void AudioTransportProxy::PushCaptureData(int voe_channel,
100 const void* audio_data,
101 int bits_per_sample,
102 int sample_rate,
103 size_t number_of_channels,
104 size_t number_of_frames) {
105 // This is part of deprecated VoE interface operating on specific
106 // VoE channels. It should not be used.
107 RTC_NOTREACHED();
108}
109
110void AudioTransportProxy::PullRenderData(int bits_per_sample,
111 int sample_rate,
112 size_t number_of_channels,
113 size_t number_of_frames,
114 void* audio_data,
115 int64_t* elapsed_time_ms,
116 int64_t* ntp_time_ms) {
kwiberg13d93262016-11-28 23:58:53117 RTC_DCHECK_EQ(bits_per_sample, 16);
kwiberg4cb29f62016-11-28 23:21:39118 RTC_DCHECK_GE(number_of_channels, 1);
119 RTC_DCHECK_LE(number_of_channels, 2);
kwiberg13d93262016-11-28 23:58:53120 RTC_DCHECK_GE(sample_rate, AudioProcessing::NativeRate::kSampleRate8kHz);
aleloi441ecb32016-11-22 14:42:53121
122 // 100 = 1 second / data duration (10 ms).
kwiberg13d93262016-11-28 23:58:53123 RTC_DCHECK_EQ(number_of_frames * 100, sample_rate);
aleloi441ecb32016-11-22 14:42:53124
125 // 8 = bits per byte.
aleloi7713f332016-11-17 14:28:59126 RTC_DCHECK_LE(bits_per_sample / 8 * number_of_frames * number_of_channels,
yujo7d973072017-06-12 19:45:32127 AudioFrame::kMaxDataSizeBytes);
aleloi441ecb32016-11-22 14:42:53128 mixer_->Mix(number_of_channels, &mixed_frame_);
129 *elapsed_time_ms = mixed_frame_.elapsed_time_ms_;
130 *ntp_time_ms = mixed_frame_.ntp_time_ms_;
131
132 const auto output_samples = Resample(mixed_frame_, sample_rate, &resampler_,
133 static_cast<int16_t*>(audio_data));
134 RTC_DCHECK_EQ(output_samples, number_of_channels * number_of_frames);
aleloi7713f332016-11-17 14:28:59135}
136
137} // namespace webrtc