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