Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: webrtc/modules/audio_device/fine_audio_buffer.cc

Issue 2715963002: Simplifies FineAudioBuffer by using rtc::Buffer (Closed)
Patch Set: Improved buffer handling after feedback from kwiberg@ Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/audio_device/fine_audio_buffer.h" 11 #include "webrtc/modules/audio_device/fine_audio_buffer.h"
12 12
13 #include <memory.h> 13 #include <memory.h>
14 #include <stdio.h> 14 #include <stdio.h>
15 #include <algorithm> 15 #include <algorithm>
16 16
17 #include "webrtc/base/checks.h" 17 #include "webrtc/base/checks.h"
18 #include "webrtc/base/logging.h" 18 #include "webrtc/base/logging.h"
19 #include "webrtc/modules/audio_device/audio_device_buffer.h" 19 #include "webrtc/modules/audio_device/audio_device_buffer.h"
20 20
21 namespace webrtc { 21 namespace webrtc {
22 22
23 FineAudioBuffer::FineAudioBuffer(AudioDeviceBuffer* device_buffer, 23 FineAudioBuffer::FineAudioBuffer(AudioDeviceBuffer* device_buffer,
24 size_t desired_frame_size_bytes, 24 size_t desired_frame_size_bytes,
25 int sample_rate) 25 int sample_rate)
26 : device_buffer_(device_buffer), 26 : device_buffer_(device_buffer),
27 desired_frame_size_bytes_(desired_frame_size_bytes), 27 desired_frame_size_bytes_(desired_frame_size_bytes),
28 sample_rate_(sample_rate), 28 sample_rate_(sample_rate),
29 samples_per_10_ms_(static_cast<size_t>(sample_rate_ * 10 / 1000)), 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)), 30 bytes_per_10_ms_(samples_per_10_ms_ * sizeof(int16_t)) {
31 playout_cached_buffer_start_(0), 31 LOG(INFO) << "desired_frame_size_bytes:" << desired_frame_size_bytes;
32 playout_cached_bytes_(0) {
33 playout_cache_buffer_.reset(new int8_t[bytes_per_10_ms_]);
34 } 32 }
35 33
36 FineAudioBuffer::~FineAudioBuffer() {} 34 FineAudioBuffer::~FineAudioBuffer() {}
37 35
38 size_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
45 void FineAudioBuffer::ResetPlayout() { 36 void FineAudioBuffer::ResetPlayout() {
46 playout_cached_buffer_start_ = 0; 37 playout_buffer_.Clear();
47 playout_cached_bytes_ = 0;
48 memset(playout_cache_buffer_.get(), 0, bytes_per_10_ms_);
49 } 38 }
50 39
51 void FineAudioBuffer::ResetRecord() { 40 void FineAudioBuffer::ResetRecord() {
52 record_buffer_.Clear(); 41 record_buffer_.Clear();
53 } 42 }
54 43
55 void FineAudioBuffer::GetPlayoutData(int8_t* buffer) { 44 void FineAudioBuffer::GetPlayoutData(int8_t* buffer) {
56 if (desired_frame_size_bytes_ <= playout_cached_bytes_) { 45 const size_t num_bytes = desired_frame_size_bytes_;
57 memcpy(buffer, &playout_cache_buffer_.get()[playout_cached_buffer_start_], 46 // Ask WebRTC for new data in chunks of 10ms until we have enough to
58 desired_frame_size_bytes_); 47 // fulfill the request. It is possible that the buffer already contains
59 playout_cached_buffer_start_ += desired_frame_size_bytes_; 48 // enough samples from the last round.
60 playout_cached_bytes_ -= desired_frame_size_bytes_; 49 while (playout_buffer_.size() < num_bytes) {
61 RTC_CHECK_LT(playout_cached_buffer_start_ + playout_cached_bytes_, 50 // Get 10ms decoded audio from WebRTC.
62 bytes_per_10_ms_); 51 device_buffer_->RequestPlayoutData(samples_per_10_ms_);
63 return; 52 // Append at most |bytes_per_10_ms_| elements to the end of the buffer.
53 // The call to GetPlayoutData() will write the data starting at the former
54 // end of the buffer. This approach avoids need for any temporary buffer.
kwiberg-webrtc 2017/02/27 12:45:57 You can probably delete all but the first sentence
henrika_webrtc 2017/02/27 12:52:43 Done.
55 const int 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);
kwiberg-webrtc 2017/02/27 12:45:57 Remove the outer parentheses.
henrika_webrtc 2017/02/27 12:52:43 Done.
62 });
63 RTC_DCHECK_EQ(bytes_per_10_ms_, bytes_written);
64 } 64 }
65 memcpy(buffer, &playout_cache_buffer_.get()[playout_cached_buffer_start_], 65 // Provide the requested number of bytes to the consumer.
66 playout_cached_bytes_); 66 memcpy(buffer, playout_buffer_.data(), num_bytes);
67 // Push another n*10ms of audio to |buffer|. n > 1 if 67 // Move remaining samples to start of buffer to prepare for next round.
68 // |desired_frame_size_bytes_| is greater than 10ms of audio. Note that we 68 memmove(playout_buffer_.data(), playout_buffer_.data() + num_bytes,
69 // write the audio after the cached bytes copied earlier. 69 playout_buffer_.size() - num_bytes);
70 int8_t* unwritten_buffer = &buffer[playout_cached_bytes_]; 70 playout_buffer_.SetSize(playout_buffer_.size() - num_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_) {
79 RTC_CHECK_EQ(num_out, 0);
80 playout_cached_bytes_ = 0;
81 return;
82 }
83 unwritten_buffer += bytes_per_10_ms_;
84 RTC_CHECK_GE(bytes_left, 0);
85 bytes_left -= static_cast<int>(bytes_per_10_ms_);
86 }
87 RTC_CHECK_LE(bytes_left, 0);
88 // 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.
96 RTC_CHECK_LE(playout_cached_bytes_, bytes_per_10_ms_);
97 RTC_CHECK_EQ(-bytes_left, playout_cached_bytes_);
98 playout_cached_buffer_start_ = 0;
99 memcpy(playout_cache_buffer_.get(), cache_ptr, playout_cached_bytes_);
100 } 71 }
101 72
102 void FineAudioBuffer::DeliverRecordedData(const int8_t* buffer, 73 void FineAudioBuffer::DeliverRecordedData(const int8_t* buffer,
103 size_t size_in_bytes, 74 size_t size_in_bytes,
104 int playout_delay_ms, 75 int playout_delay_ms,
105 int record_delay_ms) { 76 int record_delay_ms) {
106 // Always append new data and grow the buffer if needed. 77 // Always append new data and grow the buffer if needed.
107 record_buffer_.AppendData(buffer, size_in_bytes); 78 record_buffer_.AppendData(buffer, size_in_bytes);
108 // Consume samples from buffer in chunks of 10ms until there is not 79 // Consume samples from buffer in chunks of 10ms until there is not
109 // enough data left. The number of remaining bytes in the cache is given by 80 // enough data left. The number of remaining bytes in the cache is given by
110 // the new size of the buffer. 81 // the new size of the buffer.
111 while (record_buffer_.size() >= bytes_per_10_ms_) { 82 while (record_buffer_.size() >= bytes_per_10_ms_) {
112 device_buffer_->SetRecordedBuffer(record_buffer_.data(), 83 device_buffer_->SetRecordedBuffer(record_buffer_.data(),
113 samples_per_10_ms_); 84 samples_per_10_ms_);
114 device_buffer_->SetVQEData(playout_delay_ms, record_delay_ms, 0); 85 device_buffer_->SetVQEData(playout_delay_ms, record_delay_ms, 0);
115 device_buffer_->DeliverRecordedData(); 86 device_buffer_->DeliverRecordedData();
116 memmove(record_buffer_.data(), record_buffer_.data() + bytes_per_10_ms_, 87 memmove(record_buffer_.data(), record_buffer_.data() + bytes_per_10_ms_,
117 record_buffer_.size() - bytes_per_10_ms_); 88 record_buffer_.size() - bytes_per_10_ms_);
118 record_buffer_.SetSize(record_buffer_.size() - bytes_per_10_ms_); 89 record_buffer_.SetSize(record_buffer_.size() - bytes_per_10_ms_);
119 } 90 }
120 } 91 }
121 92
122 } // namespace webrtc 93 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_device/fine_audio_buffer.h ('k') | webrtc/modules/audio_device/fine_audio_buffer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698