blob: 0829e982038cbf74fec5f760ed48fddbe0e19d77 [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)),
30 bytes_per_10_ms_(samples_per_10_ms_ * sizeof(int16_t)),
31 playout_cached_buffer_start_(0),
henrikaf166e1b2017-02-23 02:44:55 -080032 playout_cached_bytes_(0) {
henrika86d907c2015-09-07 16:09:50 +020033 playout_cache_buffer_.reset(new int8_t[bytes_per_10_ms_]);
henrika86d907c2015-09-07 16:09:50 +020034}
35
36FineAudioBuffer::~FineAudioBuffer() {}
37
38size_t FineAudioBuffer::RequiredPlayoutBufferSizeBytes() {
39 // It is possible that we store the desired frame size - 1 samples. Since new
40 // audio frames are pulled in chunks of 10ms we will need a buffer that can
41 // hold desired_frame_size - 1 + 10ms of data. We omit the - 1.
42 return desired_frame_size_bytes_ + bytes_per_10_ms_;
43}
44
45void FineAudioBuffer::ResetPlayout() {
46 playout_cached_buffer_start_ = 0;
47 playout_cached_bytes_ = 0;
48 memset(playout_cache_buffer_.get(), 0, bytes_per_10_ms_);
49}
50
51void FineAudioBuffer::ResetRecord() {
henrikaf166e1b2017-02-23 02:44:55 -080052 record_buffer_.Clear();
henrika86d907c2015-09-07 16:09:50 +020053}
54
55void FineAudioBuffer::GetPlayoutData(int8_t* buffer) {
56 if (desired_frame_size_bytes_ <= playout_cached_bytes_) {
57 memcpy(buffer, &playout_cache_buffer_.get()[playout_cached_buffer_start_],
58 desired_frame_size_bytes_);
59 playout_cached_buffer_start_ += desired_frame_size_bytes_;
60 playout_cached_bytes_ -= desired_frame_size_bytes_;
henrikg91d6ede2015-09-17 00:24:34 -070061 RTC_CHECK_LT(playout_cached_buffer_start_ + playout_cached_bytes_,
62 bytes_per_10_ms_);
henrika86d907c2015-09-07 16:09:50 +020063 return;
64 }
65 memcpy(buffer, &playout_cache_buffer_.get()[playout_cached_buffer_start_],
66 playout_cached_bytes_);
67 // Push another n*10ms of audio to |buffer|. n > 1 if
68 // |desired_frame_size_bytes_| is greater than 10ms of audio. Note that we
69 // write the audio after the cached bytes copied earlier.
70 int8_t* unwritten_buffer = &buffer[playout_cached_bytes_];
71 int bytes_left =
72 static_cast<int>(desired_frame_size_bytes_ - playout_cached_bytes_);
73 // Ceiling of integer division: 1 + ((x - 1) / y)
74 size_t number_of_requests = 1 + (bytes_left - 1) / (bytes_per_10_ms_);
75 for (size_t i = 0; i < number_of_requests; ++i) {
76 device_buffer_->RequestPlayoutData(samples_per_10_ms_);
77 int num_out = device_buffer_->GetPlayoutData(unwritten_buffer);
78 if (static_cast<size_t>(num_out) != samples_per_10_ms_) {
henrikg91d6ede2015-09-17 00:24:34 -070079 RTC_CHECK_EQ(num_out, 0);
henrika86d907c2015-09-07 16:09:50 +020080 playout_cached_bytes_ = 0;
81 return;
82 }
83 unwritten_buffer += bytes_per_10_ms_;
henrikg91d6ede2015-09-17 00:24:34 -070084 RTC_CHECK_GE(bytes_left, 0);
henrika86d907c2015-09-07 16:09:50 +020085 bytes_left -= static_cast<int>(bytes_per_10_ms_);
86 }
henrikg91d6ede2015-09-17 00:24:34 -070087 RTC_CHECK_LE(bytes_left, 0);
henrika86d907c2015-09-07 16:09:50 +020088 // Put the samples that were written to |buffer| but are not used in the
89 // cache.
90 size_t cache_location = desired_frame_size_bytes_;
91 int8_t* cache_ptr = &buffer[cache_location];
92 playout_cached_bytes_ = number_of_requests * bytes_per_10_ms_ -
93 (desired_frame_size_bytes_ - playout_cached_bytes_);
94 // If playout_cached_bytes_ is larger than the cache buffer, uninitialized
95 // memory will be read.
henrikg91d6ede2015-09-17 00:24:34 -070096 RTC_CHECK_LE(playout_cached_bytes_, bytes_per_10_ms_);
kwiberg352444f2016-11-28 15:58:53 -080097 RTC_CHECK_EQ(-bytes_left, playout_cached_bytes_);
henrika86d907c2015-09-07 16:09:50 +020098 playout_cached_buffer_start_ = 0;
99 memcpy(playout_cache_buffer_.get(), cache_ptr, playout_cached_bytes_);
100}
101
102void FineAudioBuffer::DeliverRecordedData(const int8_t* buffer,
103 size_t size_in_bytes,
104 int playout_delay_ms,
105 int record_delay_ms) {
henrikaf166e1b2017-02-23 02:44:55 -0800106 // Always append new data and grow the buffer if needed.
107 record_buffer_.AppendData(buffer, size_in_bytes);
108 // Consume samples from buffer in chunks of 10ms until there is not
henrika86d907c2015-09-07 16:09:50 +0200109 // enough data left. The number of remaining bytes in the cache is given by
henrikaf166e1b2017-02-23 02:44:55 -0800110 // the new size of the buffer.
111 while (record_buffer_.size() >= bytes_per_10_ms_) {
112 device_buffer_->SetRecordedBuffer(record_buffer_.data(),
113 samples_per_10_ms_);
henrika86d907c2015-09-07 16:09:50 +0200114 device_buffer_->SetVQEData(playout_delay_ms, record_delay_ms, 0);
115 device_buffer_->DeliverRecordedData();
henrikaf166e1b2017-02-23 02:44:55 -0800116 memmove(record_buffer_.data(), record_buffer_.data() + bytes_per_10_ms_,
117 record_buffer_.size() - bytes_per_10_ms_);
118 record_buffer_.SetSize(record_buffer_.size() - bytes_per_10_ms_);
henrika86d907c2015-09-07 16:09:50 +0200119 }
120}
121
122} // namespace webrtc