blob: c1f12855e511edbf9e01537a06ccbee5f1eab7c8 [file] [log] [blame]
henrika86d907c2015-09-07 16:09:50 +02001/*
2 * Copyright (c) 2013 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_device/fine_audio_buffer.h"
henrika86d907c2015-09-07 16:09:50 +020012
13#include <memory.h>
14#include <stdio.h>
15#include <algorithm>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_device/audio_device_buffer.h"
18#include "rtc_base/checks.h"
19#include "rtc_base/logging.h"
henrika86d907c2015-09-07 16:09:50 +020020
21namespace webrtc {
22
23FineAudioBuffer::FineAudioBuffer(AudioDeviceBuffer* device_buffer,
henrikabb6f7522017-05-30 02:01:30 -070024 int sample_rate,
25 size_t capacity)
henrika86d907c2015-09-07 16:09:50 +020026 : device_buffer_(device_buffer),
henrika86d907c2015-09-07 16:09:50 +020027 sample_rate_(sample_rate),
28 samples_per_10_ms_(static_cast<size_t>(sample_rate_ * 10 / 1000)),
henrikabb6f7522017-05-30 02:01:30 -070029 bytes_per_10_ms_(samples_per_10_ms_ * sizeof(int16_t)),
30 playout_buffer_(0, capacity),
31 record_buffer_(0, capacity) {
henrika883d00f2018-03-16 10:09:49 +010032 RTC_LOG(INFO) << "samples_per_10_ms_: " << samples_per_10_ms_;
henrika86d907c2015-09-07 16:09:50 +020033}
34
35FineAudioBuffer::~FineAudioBuffer() {}
36
henrika86d907c2015-09-07 16:09:50 +020037void FineAudioBuffer::ResetPlayout() {
henrikab3ebc1a2017-02-27 05:14:17 -080038 playout_buffer_.Clear();
henrika86d907c2015-09-07 16:09:50 +020039}
40
41void FineAudioBuffer::ResetRecord() {
henrikaf166e1b2017-02-23 02:44:55 -080042 record_buffer_.Clear();
henrika86d907c2015-09-07 16:09:50 +020043}
44
henrika883d00f2018-03-16 10:09:49 +010045void FineAudioBuffer::GetPlayoutData(rtc::ArrayView<int8_t> audio_buffer,
46 int playout_delay_ms) {
henrikab3ebc1a2017-02-27 05:14:17 -080047 // Ask WebRTC for new data in chunks of 10ms until we have enough to
48 // fulfill the request. It is possible that the buffer already contains
49 // enough samples from the last round.
henrikabb6f7522017-05-30 02:01:30 -070050 const size_t num_bytes = audio_buffer.size();
henrikab3ebc1a2017-02-27 05:14:17 -080051 while (playout_buffer_.size() < num_bytes) {
52 // Get 10ms decoded audio from WebRTC.
henrika86d907c2015-09-07 16:09:50 +020053 device_buffer_->RequestPlayoutData(samples_per_10_ms_);
henrikab3ebc1a2017-02-27 05:14:17 -080054 // Append |bytes_per_10_ms_| elements to the end of the buffer.
55 const size_t bytes_written = playout_buffer_.AppendData(
56 bytes_per_10_ms_, [&](rtc::ArrayView<int8_t> buf) {
57 const size_t samples_per_channel =
58 device_buffer_->GetPlayoutData(buf.data());
59 // TODO(henrika): this class is only used on mobile devices and is
60 // currently limited to mono. Modifications are needed for stereo.
61 return sizeof(int16_t) * samples_per_channel;
62 });
63 RTC_DCHECK_EQ(bytes_per_10_ms_, bytes_written);
henrika86d907c2015-09-07 16:09:50 +020064 }
henrikab3ebc1a2017-02-27 05:14:17 -080065 // Provide the requested number of bytes to the consumer.
henrikabb6f7522017-05-30 02:01:30 -070066 memcpy(audio_buffer.data(), playout_buffer_.data(), num_bytes);
henrikab3ebc1a2017-02-27 05:14:17 -080067 // Move remaining samples to start of buffer to prepare for next round.
68 memmove(playout_buffer_.data(), playout_buffer_.data() + num_bytes,
69 playout_buffer_.size() - num_bytes);
70 playout_buffer_.SetSize(playout_buffer_.size() - num_bytes);
henrika883d00f2018-03-16 10:09:49 +010071 // Cache playout latency for usage in DeliverRecordedData();
72 playout_delay_ms_ = playout_delay_ms;
henrika86d907c2015-09-07 16:09:50 +020073}
74
henrikabb6f7522017-05-30 02:01:30 -070075void FineAudioBuffer::DeliverRecordedData(
76 rtc::ArrayView<const int8_t> audio_buffer,
henrikabb6f7522017-05-30 02:01:30 -070077 int record_delay_ms) {
henrikaf166e1b2017-02-23 02:44:55 -080078 // Always append new data and grow the buffer if needed.
henrikabb6f7522017-05-30 02:01:30 -070079 record_buffer_.AppendData(audio_buffer.data(), audio_buffer.size());
henrikaf166e1b2017-02-23 02:44:55 -080080 // Consume samples from buffer in chunks of 10ms until there is not
henrika86d907c2015-09-07 16:09:50 +020081 // enough data left. The number of remaining bytes in the cache is given by
henrikaf166e1b2017-02-23 02:44:55 -080082 // the new size of the buffer.
83 while (record_buffer_.size() >= bytes_per_10_ms_) {
84 device_buffer_->SetRecordedBuffer(record_buffer_.data(),
85 samples_per_10_ms_);
henrika883d00f2018-03-16 10:09:49 +010086 device_buffer_->SetVQEData(playout_delay_ms_, record_delay_ms);
henrika86d907c2015-09-07 16:09:50 +020087 device_buffer_->DeliverRecordedData();
henrikaf166e1b2017-02-23 02:44:55 -080088 memmove(record_buffer_.data(), record_buffer_.data() + bytes_per_10_ms_,
89 record_buffer_.size() - bytes_per_10_ms_);
90 record_buffer_.SetSize(record_buffer_.size() - bytes_per_10_ms_);
henrika86d907c2015-09-07 16:09:50 +020091 }
92}
93
94} // namespace webrtc