aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 13 | namespace webrtc { |
| 14 | |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 15 | namespace { |
| 16 | // Resample audio in |frame| to given sample rate preserving the |
| 17 | // channel count and place the result in |destination|. |
| 18 | int 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 | |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame^] | 28 | // TODO(yujo): make resampler take an AudioFrame, and add special case |
| 29 | // handling of muted frames. |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 30 | return resampler->Resample( |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame^] | 31 | frame.data(), frame.samples_per_channel_ * number_of_channels, |
| 32 | destination, number_of_channels * target_number_of_samples_per_channel); |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 33 | } |
| 34 | } // namespace |
| 35 | |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 36 | AudioTransportProxy::AudioTransportProxy(AudioTransport* voe_audio_transport, |
| 37 | AudioProcessing* apm, |
| 38 | AudioMixer* mixer) |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 39 | : voe_audio_transport_(voe_audio_transport), apm_(apm), mixer_(mixer) { |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 40 | RTC_DCHECK(voe_audio_transport); |
| 41 | RTC_DCHECK(apm); |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 42 | RTC_DCHECK(mixer); |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | AudioTransportProxy::~AudioTransportProxy() {} |
| 46 | |
| 47 | int32_t AudioTransportProxy::RecordedDataIsAvailable( |
| 48 | const void* audioSamples, |
| 49 | const size_t nSamples, |
| 50 | const size_t nBytesPerSample, |
| 51 | const size_t nChannels, |
| 52 | const uint32_t samplesPerSec, |
| 53 | const uint32_t totalDelayMS, |
| 54 | const int32_t clockDrift, |
| 55 | const uint32_t currentMicLevel, |
| 56 | const bool keyPressed, |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 57 | uint32_t& newMicLevel) { // NOLINT: to avoid changing APIs |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 58 | // Pass call through to original audio transport instance. |
| 59 | return voe_audio_transport_->RecordedDataIsAvailable( |
| 60 | audioSamples, nSamples, nBytesPerSample, nChannels, samplesPerSec, |
| 61 | totalDelayMS, clockDrift, currentMicLevel, keyPressed, newMicLevel); |
| 62 | } |
| 63 | |
| 64 | int32_t AudioTransportProxy::NeedMorePlayData(const size_t nSamples, |
| 65 | const size_t nBytesPerSample, |
| 66 | const size_t nChannels, |
| 67 | const uint32_t samplesPerSec, |
| 68 | void* audioSamples, |
| 69 | size_t& nSamplesOut, |
| 70 | int64_t* elapsed_time_ms, |
| 71 | int64_t* ntp_time_ms) { |
| 72 | RTC_DCHECK_EQ(sizeof(int16_t) * nChannels, nBytesPerSample); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 73 | RTC_DCHECK_GE(nChannels, 1); |
| 74 | RTC_DCHECK_LE(nChannels, 2); |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 75 | RTC_DCHECK_GE( |
| 76 | samplesPerSec, |
| 77 | static_cast<uint32_t>(AudioProcessing::NativeRate::kSampleRate8kHz)); |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 78 | |
| 79 | // 100 = 1 second / data duration (10 ms). |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 80 | RTC_DCHECK_EQ(nSamples * 100, samplesPerSec); |
| 81 | RTC_DCHECK_LE(nBytesPerSample * nSamples * nChannels, |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame^] | 82 | AudioFrame::kMaxDataSizeBytes); |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 83 | |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 84 | mixer_->Mix(nChannels, &mixed_frame_); |
| 85 | *elapsed_time_ms = mixed_frame_.elapsed_time_ms_; |
| 86 | *ntp_time_ms = mixed_frame_.ntp_time_ms_; |
| 87 | |
| 88 | const auto error = apm_->ProcessReverseStream(&mixed_frame_); |
| 89 | RTC_DCHECK_EQ(error, AudioProcessing::kNoError); |
| 90 | |
| 91 | nSamplesOut = Resample(mixed_frame_, samplesPerSec, &resampler_, |
| 92 | static_cast<int16_t*>(audioSamples)); |
| 93 | RTC_DCHECK_EQ(nSamplesOut, nChannels * nSamples); |
| 94 | return 0; |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void AudioTransportProxy::PushCaptureData(int voe_channel, |
| 98 | const void* audio_data, |
| 99 | int bits_per_sample, |
| 100 | int sample_rate, |
| 101 | size_t number_of_channels, |
| 102 | size_t number_of_frames) { |
| 103 | // This is part of deprecated VoE interface operating on specific |
| 104 | // VoE channels. It should not be used. |
| 105 | RTC_NOTREACHED(); |
| 106 | } |
| 107 | |
| 108 | void AudioTransportProxy::PullRenderData(int bits_per_sample, |
| 109 | int sample_rate, |
| 110 | size_t number_of_channels, |
| 111 | size_t number_of_frames, |
| 112 | void* audio_data, |
| 113 | int64_t* elapsed_time_ms, |
| 114 | int64_t* ntp_time_ms) { |
kwiberg | 352444f | 2016-11-28 15:58:53 -0800 | [diff] [blame] | 115 | RTC_DCHECK_EQ(bits_per_sample, 16); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 116 | RTC_DCHECK_GE(number_of_channels, 1); |
| 117 | RTC_DCHECK_LE(number_of_channels, 2); |
kwiberg | 352444f | 2016-11-28 15:58:53 -0800 | [diff] [blame] | 118 | RTC_DCHECK_GE(sample_rate, AudioProcessing::NativeRate::kSampleRate8kHz); |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 119 | |
| 120 | // 100 = 1 second / data duration (10 ms). |
kwiberg | 352444f | 2016-11-28 15:58:53 -0800 | [diff] [blame] | 121 | RTC_DCHECK_EQ(number_of_frames * 100, sample_rate); |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 122 | |
| 123 | // 8 = bits per byte. |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 124 | RTC_DCHECK_LE(bits_per_sample / 8 * number_of_frames * number_of_channels, |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame^] | 125 | AudioFrame::kMaxDataSizeBytes); |
aleloi | 04c0722 | 2016-11-22 06:42:53 -0800 | [diff] [blame] | 126 | mixer_->Mix(number_of_channels, &mixed_frame_); |
| 127 | *elapsed_time_ms = mixed_frame_.elapsed_time_ms_; |
| 128 | *ntp_time_ms = mixed_frame_.ntp_time_ms_; |
| 129 | |
| 130 | const auto output_samples = Resample(mixed_frame_, sample_rate, &resampler_, |
| 131 | static_cast<int16_t*>(audio_data)); |
| 132 | RTC_DCHECK_EQ(output_samples, number_of_channels * number_of_frames); |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | } // namespace webrtc |