blob: 27d193786865b9f9cb7cf50937dfdbd8142de85a [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
11#include "webrtc/modules/audio_device/fine_audio_buffer.h"
12
13#include <memory.h>
14#include <stdio.h>
15#include <algorithm>
16
Henrik Kjellanderdca1e092017-07-01 16:42:22 +020017#include "webrtc/base/checks.h"
18#include "webrtc/base/logging.h"
henrika86d907c2015-09-07 16:09:50 +020019#include "webrtc/modules/audio_device/audio_device_buffer.h"
20
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) {
32 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
henrikabb6f7522017-05-30 02:01:30 -070045void FineAudioBuffer::GetPlayoutData(rtc::ArrayView<int8_t> audio_buffer) {
henrikab3ebc1a2017-02-27 05:14:17 -080046 // Ask WebRTC for new data in chunks of 10ms until we have enough to
47 // fulfill the request. It is possible that the buffer already contains
48 // enough samples from the last round.
henrikabb6f7522017-05-30 02:01:30 -070049 const size_t num_bytes = audio_buffer.size();
henrikab3ebc1a2017-02-27 05:14:17 -080050 while (playout_buffer_.size() < num_bytes) {
51 // Get 10ms decoded audio from WebRTC.
henrika86d907c2015-09-07 16:09:50 +020052 device_buffer_->RequestPlayoutData(samples_per_10_ms_);
henrikab3ebc1a2017-02-27 05:14:17 -080053 // Append |bytes_per_10_ms_| elements to the end of the buffer.
54 const size_t bytes_written = playout_buffer_.AppendData(
55 bytes_per_10_ms_, [&](rtc::ArrayView<int8_t> buf) {
56 const size_t samples_per_channel =
57 device_buffer_->GetPlayoutData(buf.data());
58 // TODO(henrika): this class is only used on mobile devices and is
59 // currently limited to mono. Modifications are needed for stereo.
60 return sizeof(int16_t) * samples_per_channel;
61 });
62 RTC_DCHECK_EQ(bytes_per_10_ms_, bytes_written);
henrika86d907c2015-09-07 16:09:50 +020063 }
henrikab3ebc1a2017-02-27 05:14:17 -080064 // Provide the requested number of bytes to the consumer.
henrikabb6f7522017-05-30 02:01:30 -070065 memcpy(audio_buffer.data(), playout_buffer_.data(), num_bytes);
henrikab3ebc1a2017-02-27 05:14:17 -080066 // Move remaining samples to start of buffer to prepare for next round.
67 memmove(playout_buffer_.data(), playout_buffer_.data() + num_bytes,
68 playout_buffer_.size() - num_bytes);
69 playout_buffer_.SetSize(playout_buffer_.size() - num_bytes);
henrika86d907c2015-09-07 16:09:50 +020070}
71
henrikabb6f7522017-05-30 02:01:30 -070072void FineAudioBuffer::DeliverRecordedData(
73 rtc::ArrayView<const int8_t> audio_buffer,
74 int playout_delay_ms,
75 int record_delay_ms) {
henrikaf166e1b2017-02-23 02:44:55 -080076 // Always append new data and grow the buffer if needed.
henrikabb6f7522017-05-30 02:01:30 -070077 record_buffer_.AppendData(audio_buffer.data(), audio_buffer.size());
henrikaf166e1b2017-02-23 02:44:55 -080078 // Consume samples from buffer in chunks of 10ms until there is not
henrika86d907c2015-09-07 16:09:50 +020079 // enough data left. The number of remaining bytes in the cache is given by
henrikaf166e1b2017-02-23 02:44:55 -080080 // the new size of the buffer.
81 while (record_buffer_.size() >= bytes_per_10_ms_) {
82 device_buffer_->SetRecordedBuffer(record_buffer_.data(),
83 samples_per_10_ms_);
henrika86d907c2015-09-07 16:09:50 +020084 device_buffer_->SetVQEData(playout_delay_ms, record_delay_ms, 0);
85 device_buffer_->DeliverRecordedData();
henrikaf166e1b2017-02-23 02:44:55 -080086 memmove(record_buffer_.data(), record_buffer_.data() + bytes_per_10_ms_,
87 record_buffer_.size() - bytes_per_10_ms_);
88 record_buffer_.SetSize(record_buffer_.size() - bytes_per_10_ms_);
henrika86d907c2015-09-07 16:09:50 +020089 }
90}
91
92} // namespace webrtc