blob: 83775741d85550bb0cfc585ca60cc145984440c7 [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
17#include "webrtc/base/checks.h"
18#include "webrtc/base/logging.h"
19#include "webrtc/modules/audio_device/audio_device_buffer.h"
20
21namespace webrtc {
22
23FineAudioBuffer::FineAudioBuffer(AudioDeviceBuffer* device_buffer,
24 size_t desired_frame_size_bytes,
25 int sample_rate)
26 : device_buffer_(device_buffer),
27 desired_frame_size_bytes_(desired_frame_size_bytes),
28 sample_rate_(sample_rate),
29 samples_per_10_ms_(static_cast<size_t>(sample_rate_ * 10 / 1000)),
henrikab3ebc1a2017-02-27 05:14:17 -080030 bytes_per_10_ms_(samples_per_10_ms_ * sizeof(int16_t)) {
31 LOG(INFO) << "desired_frame_size_bytes:" << desired_frame_size_bytes;
henrika86d907c2015-09-07 16:09:50 +020032}
33
34FineAudioBuffer::~FineAudioBuffer() {}
35
henrika86d907c2015-09-07 16:09:50 +020036void FineAudioBuffer::ResetPlayout() {
henrikab3ebc1a2017-02-27 05:14:17 -080037 playout_buffer_.Clear();
henrika86d907c2015-09-07 16:09:50 +020038}
39
40void FineAudioBuffer::ResetRecord() {
henrikaf166e1b2017-02-23 02:44:55 -080041 record_buffer_.Clear();
henrika86d907c2015-09-07 16:09:50 +020042}
43
44void FineAudioBuffer::GetPlayoutData(int8_t* buffer) {
henrikab3ebc1a2017-02-27 05:14:17 -080045 const size_t num_bytes = desired_frame_size_bytes_;
46 // 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.
49 while (playout_buffer_.size() < num_bytes) {
50 // Get 10ms decoded audio from WebRTC.
henrika86d907c2015-09-07 16:09:50 +020051 device_buffer_->RequestPlayoutData(samples_per_10_ms_);
henrikab3ebc1a2017-02-27 05:14:17 -080052 // Append |bytes_per_10_ms_| elements to the end of the buffer.
53 const size_t bytes_written = playout_buffer_.AppendData(
54 bytes_per_10_ms_, [&](rtc::ArrayView<int8_t> buf) {
55 const size_t samples_per_channel =
56 device_buffer_->GetPlayoutData(buf.data());
57 // TODO(henrika): this class is only used on mobile devices and is
58 // currently limited to mono. Modifications are needed for stereo.
59 return sizeof(int16_t) * samples_per_channel;
60 });
61 RTC_DCHECK_EQ(bytes_per_10_ms_, bytes_written);
henrika86d907c2015-09-07 16:09:50 +020062 }
henrikab3ebc1a2017-02-27 05:14:17 -080063 // Provide the requested number of bytes to the consumer.
64 memcpy(buffer, playout_buffer_.data(), num_bytes);
65 // Move remaining samples to start of buffer to prepare for next round.
66 memmove(playout_buffer_.data(), playout_buffer_.data() + num_bytes,
67 playout_buffer_.size() - num_bytes);
68 playout_buffer_.SetSize(playout_buffer_.size() - num_bytes);
henrika86d907c2015-09-07 16:09:50 +020069}
70
71void FineAudioBuffer::DeliverRecordedData(const int8_t* buffer,
72 size_t size_in_bytes,
73 int playout_delay_ms,
74 int record_delay_ms) {
henrikaf166e1b2017-02-23 02:44:55 -080075 // Always append new data and grow the buffer if needed.
76 record_buffer_.AppendData(buffer, size_in_bytes);
77 // Consume samples from buffer in chunks of 10ms until there is not
henrika86d907c2015-09-07 16:09:50 +020078 // enough data left. The number of remaining bytes in the cache is given by
henrikaf166e1b2017-02-23 02:44:55 -080079 // the new size of the buffer.
80 while (record_buffer_.size() >= bytes_per_10_ms_) {
81 device_buffer_->SetRecordedBuffer(record_buffer_.data(),
82 samples_per_10_ms_);
henrika86d907c2015-09-07 16:09:50 +020083 device_buffer_->SetVQEData(playout_delay_ms, record_delay_ms, 0);
84 device_buffer_->DeliverRecordedData();
henrikaf166e1b2017-02-23 02:44:55 -080085 memmove(record_buffer_.data(), record_buffer_.data() + bytes_per_10_ms_,
86 record_buffer_.size() - bytes_per_10_ms_);
87 record_buffer_.SetSize(record_buffer_.size() - bytes_per_10_ms_);
henrika86d907c2015-09-07 16:09:50 +020088 }
89}
90
91} // namespace webrtc