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

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

Issue 1254883002: Refactor the AudioDevice for iOS and improve the performance and stability (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Extended unit test for FineAudioBuffer with recorded data Created 5 years, 3 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/android/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/modules/audio_device/audio_device_buffer.h" 19 #include "webrtc/modules/audio_device/audio_device_buffer.h"
19 20
20 namespace webrtc { 21 namespace webrtc {
21 22
22 FineAudioBuffer::FineAudioBuffer(AudioDeviceBuffer* device_buffer, 23 FineAudioBuffer::FineAudioBuffer(AudioDeviceBuffer* device_buffer,
23 size_t desired_frame_size_bytes, 24 size_t desired_frame_size_bytes,
24 int sample_rate) 25 int sample_rate)
25 : device_buffer_(device_buffer), 26 : device_buffer_(device_buffer),
26 desired_frame_size_bytes_(desired_frame_size_bytes), 27 desired_frame_size_bytes_(desired_frame_size_bytes),
27 sample_rate_(sample_rate), 28 sample_rate_(sample_rate),
28 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)),
29 bytes_per_10_ms_(samples_per_10_ms_ * sizeof(int16_t)), 30 bytes_per_10_ms_(samples_per_10_ms_ * sizeof(int16_t)),
30 cached_buffer_start_(0), 31 cached_buffer_start_(0),
31 cached_bytes_(0) { 32 cached_bytes_(0),
tkchin_webrtc 2015/09/01 20:54:49 nit: consider naming this playout_cached_bytes_
henrika_webrtc 2015/09/03 13:44:41 Done.
33 record_cached_bytes_(0) {
32 cache_buffer_.reset(new int8_t[bytes_per_10_ms_]); 34 cache_buffer_.reset(new int8_t[bytes_per_10_ms_]);
tkchin_webrtc 2015/09/01 20:54:49 nit: consider naming this playout_cache_buffer_ to
henrika_webrtc 2015/09/03 13:44:41 Done.
35 record_cache_buffer_.reset(
36 new int8_t[desired_frame_size_bytes + bytes_per_10_ms_]);
tkchin_webrtc 2015/09/01 20:54:49 nit: pull out size into a local var since it's the
henrika_webrtc 2015/09/03 13:44:41 Thanks. Done.
37 memset(record_cache_buffer_.get(), 0,
38 desired_frame_size_bytes + bytes_per_10_ms_);
33 } 39 }
34 40
35 FineAudioBuffer::~FineAudioBuffer() { 41 FineAudioBuffer::~FineAudioBuffer() {
36 } 42 }
37 43
38 size_t FineAudioBuffer::RequiredBufferSizeBytes() { 44 size_t FineAudioBuffer::RequiredPlayoutBufferSizeBytes() {
39 // It is possible that we store the desired frame size - 1 samples. Since new 45 // 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 46 // 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. 47 // hold desired_frame_size - 1 + 10ms of data. We omit the - 1.
42 return desired_frame_size_bytes_ + bytes_per_10_ms_; 48 return desired_frame_size_bytes_ + bytes_per_10_ms_;
43 } 49 }
44 50
45 void FineAudioBuffer::GetBufferData(int8_t* buffer) { 51 void FineAudioBuffer::ResetPlayout() {
52 cached_buffer_start_ = 0;
53 cached_bytes_ = 0;
54 memset(cache_buffer_.get(), 0, bytes_per_10_ms_);
55 }
56
57 void FineAudioBuffer::ResetRecord() {
58 record_cached_bytes_ = 0;
59 memset(record_cache_buffer_.get(), 0,
60 desired_frame_size_bytes_ + bytes_per_10_ms_);
61 }
62
63 void FineAudioBuffer::GetPlayoutData(int8_t* buffer) {
46 if (desired_frame_size_bytes_ <= cached_bytes_) { 64 if (desired_frame_size_bytes_ <= cached_bytes_) {
47 memcpy(buffer, &cache_buffer_.get()[cached_buffer_start_], 65 memcpy(buffer, &cache_buffer_.get()[cached_buffer_start_],
48 desired_frame_size_bytes_); 66 desired_frame_size_bytes_);
49 cached_buffer_start_ += desired_frame_size_bytes_; 67 cached_buffer_start_ += desired_frame_size_bytes_;
50 cached_bytes_ -= desired_frame_size_bytes_; 68 cached_bytes_ -= desired_frame_size_bytes_;
51 CHECK_LT(cached_buffer_start_ + cached_bytes_, bytes_per_10_ms_); 69 CHECK_LT(cached_buffer_start_ + cached_bytes_, bytes_per_10_ms_);
52 return; 70 return;
53 } 71 }
54 memcpy(buffer, &cache_buffer_.get()[cached_buffer_start_], cached_bytes_); 72 memcpy(buffer, &cache_buffer_.get()[cached_buffer_start_], cached_bytes_);
55 // Push another n*10ms of audio to |buffer|. n > 1 if 73 // Push another n*10ms of audio to |buffer|. n > 1 if
56 // |desired_frame_size_bytes_| is greater than 10ms of audio. Note that we 74 // |desired_frame_size_bytes_| is greater than 10ms of audio. Note that we
57 // write the audio after the cached bytes copied earlier. 75 // write the audio after the cached bytes copied earlier.
58 int8_t* unwritten_buffer = &buffer[cached_bytes_]; 76 int8_t* unwritten_buffer = &buffer[cached_bytes_];
59 int bytes_left = static_cast<int>(desired_frame_size_bytes_ - cached_bytes_); 77 int bytes_left = static_cast<int>(desired_frame_size_bytes_ - cached_bytes_);
60 // Ceiling of integer division: 1 + ((x - 1) / y) 78 // Ceiling of integer division: 1 + ((x - 1) / y)
61 size_t number_of_requests = 1 + (bytes_left - 1) / (bytes_per_10_ms_); 79 size_t number_of_requests = 1 + (bytes_left - 1) / (bytes_per_10_ms_);
62 for (size_t i = 0; i < number_of_requests; ++i) { 80 for (size_t i = 0; i < number_of_requests; ++i) {
63 device_buffer_->RequestPlayoutData(samples_per_10_ms_); 81 device_buffer_->RequestPlayoutData(samples_per_10_ms_);
64 int num_out = device_buffer_->GetPlayoutData(unwritten_buffer); 82 int num_out = device_buffer_->GetPlayoutData(unwritten_buffer);
65 if (static_cast<size_t>(num_out) != samples_per_10_ms_) { 83 if (static_cast<size_t>(num_out) != samples_per_10_ms_) {
66 CHECK_EQ(num_out, 0); 84 CHECK_EQ(num_out, 0);
67 cached_bytes_ = 0; 85 cached_bytes_ = 0;
68 return; 86 return;
69 } 87 }
70 unwritten_buffer += bytes_per_10_ms_; 88 unwritten_buffer += bytes_per_10_ms_;
71 CHECK_GE(bytes_left, 0); 89 CHECK_GE(bytes_left, 0);
72 bytes_left -= bytes_per_10_ms_; 90 bytes_left -= static_cast<int>(bytes_per_10_ms_);
73 } 91 }
74 CHECK_LE(bytes_left, 0); 92 CHECK_LE(bytes_left, 0);
75 // Put the samples that were written to |buffer| but are not used in the 93 // Put the samples that were written to |buffer| but are not used in the
76 // cache. 94 // cache.
77 size_t cache_location = desired_frame_size_bytes_; 95 size_t cache_location = desired_frame_size_bytes_;
78 int8_t* cache_ptr = &buffer[cache_location]; 96 int8_t* cache_ptr = &buffer[cache_location];
79 cached_bytes_ = number_of_requests * bytes_per_10_ms_ - 97 cached_bytes_ = number_of_requests * bytes_per_10_ms_ -
80 (desired_frame_size_bytes_ - cached_bytes_); 98 (desired_frame_size_bytes_ - cached_bytes_);
81 // If cached_bytes_ is larger than the cache buffer, uninitialized memory 99 // If cached_bytes_ is larger than the cache buffer, uninitialized memory
82 // will be read. 100 // will be read.
83 CHECK_LE(cached_bytes_, bytes_per_10_ms_); 101 CHECK_LE(cached_bytes_, bytes_per_10_ms_);
84 CHECK_EQ(static_cast<size_t>(-bytes_left), cached_bytes_); 102 CHECK_EQ(static_cast<size_t>(-bytes_left), cached_bytes_);
85 cached_buffer_start_ = 0; 103 cached_buffer_start_ = 0;
86 memcpy(cache_buffer_.get(), cache_ptr, cached_bytes_); 104 memcpy(cache_buffer_.get(), cache_ptr, cached_bytes_);
87 } 105 }
88 106
107 void FineAudioBuffer::DeliverRecordedData(const int8_t* buffer,
108 size_t size_in_bytes,
109 int playout_delay_ms,
110 int record_delay_ms) {
111 CHECK_EQ(size_in_bytes, desired_frame_size_bytes_);
112 // Store the recorded sampled in a temporary buffer.
113 memcpy(record_cache_buffer_.get() + record_cached_bytes_, buffer,
114 size_in_bytes);
115 record_cached_bytes_ += size_in_bytes;
116 // Ensure that we don't write outside the boundaries.
117 CHECK_LE(record_cached_bytes_, desired_frame_size_bytes_ + bytes_per_10_ms_);
118 // Consume samples in temporary buffer in chunks of 10ms until there is not
119 // enough data left. Any remaining part is moved to the beginning of the
120 // temporary buffer and |record_cached_bytes_| points to the first byte where
121 // new data can be stored.
122 while (record_cached_bytes_ >= bytes_per_10_ms_) {
123 device_buffer_->SetRecordedBuffer(record_cache_buffer_.get(),
124 samples_per_10_ms_);
125 device_buffer_->SetVQEData(playout_delay_ms, record_delay_ms, 0);
126 device_buffer_->DeliverRecordedData();
127 memmove(record_cache_buffer_.get(),
tkchin_webrtc 2015/09/01 20:54:49 Isn't this expensive? Performs a copy of all remai
henrika_webrtc 2015/09/03 13:44:41 I have modified the existing scheme by extending t
128 record_cache_buffer_.get() + bytes_per_10_ms_,
129 record_cached_bytes_ - bytes_per_10_ms_);
130 record_cached_bytes_ -= bytes_per_10_ms_;
131 }
132 }
133
89 } // namespace webrtc 134 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698