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

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

Issue 2894873002: Improved audio buffer handling for iOS (Closed)
Patch Set: Added capacity parameter Created 3 years, 6 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 int sample_rate,
25 int sample_rate) 25 size_t capacity)
26 : device_buffer_(device_buffer), 26 : device_buffer_(device_buffer),
27 desired_frame_size_bytes_(desired_frame_size_bytes),
28 sample_rate_(sample_rate), 27 sample_rate_(sample_rate),
29 samples_per_10_ms_(static_cast<size_t>(sample_rate_ * 10 / 1000)), 28 samples_per_10_ms_(static_cast<size_t>(sample_rate_ * 10 / 1000)),
30 bytes_per_10_ms_(samples_per_10_ms_ * sizeof(int16_t)) { 29 bytes_per_10_ms_(samples_per_10_ms_ * sizeof(int16_t)),
31 LOG(INFO) << "desired_frame_size_bytes:" << desired_frame_size_bytes; 30 playout_buffer_(0, capacity) {
kwiberg-webrtc 2017/05/29 04:09:02 You don't specify an initial capacity for record_b
henrika_webrtc 2017/05/29 10:33:51 This CL is focusing on playout side since that is
kwiberg-webrtc 2017/05/29 11:07:00 Acknowledged. It might make sense to call the arg
henrika_webrtc 2017/05/29 14:30:05 Actually. I kept "capacity" and ensured that the r
31 LOG(INFO) << "samples_per_10_ms_:" << samples_per_10_ms_;
32 } 32 }
33 33
34 FineAudioBuffer::~FineAudioBuffer() {} 34 FineAudioBuffer::~FineAudioBuffer() {}
35 35
36 void FineAudioBuffer::ResetPlayout() { 36 void FineAudioBuffer::ResetPlayout() {
37 playout_buffer_.Clear(); 37 playout_buffer_.Clear();
38 } 38 }
39 39
40 void FineAudioBuffer::ResetRecord() { 40 void FineAudioBuffer::ResetRecord() {
41 record_buffer_.Clear(); 41 record_buffer_.Clear();
42 } 42 }
43 43
44 void FineAudioBuffer::GetPlayoutData(int8_t* buffer) { 44 void FineAudioBuffer::GetPlayoutData(rtc::ArrayView<int8_t> audio_buffer) {
45 const size_t num_bytes = desired_frame_size_bytes_;
46 // Ask WebRTC for new data in chunks of 10ms until we have enough to 45 // 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 46 // fulfill the request. It is possible that the buffer already contains
48 // enough samples from the last round. 47 // enough samples from the last round.
48 const size_t num_bytes = audio_buffer.size();
49 while (playout_buffer_.size() < num_bytes) { 49 while (playout_buffer_.size() < num_bytes) {
50 // Get 10ms decoded audio from WebRTC. 50 // Get 10ms decoded audio from WebRTC.
51 device_buffer_->RequestPlayoutData(samples_per_10_ms_); 51 device_buffer_->RequestPlayoutData(samples_per_10_ms_);
52 // Append |bytes_per_10_ms_| elements to the end of the buffer. 52 // Append |bytes_per_10_ms_| elements to the end of the buffer.
53 const size_t bytes_written = playout_buffer_.AppendData( 53 const size_t bytes_written = playout_buffer_.AppendData(
54 bytes_per_10_ms_, [&](rtc::ArrayView<int8_t> buf) { 54 bytes_per_10_ms_, [&](rtc::ArrayView<int8_t> buf) {
55 const size_t samples_per_channel = 55 const size_t samples_per_channel =
56 device_buffer_->GetPlayoutData(buf.data()); 56 device_buffer_->GetPlayoutData(buf.data());
57 // TODO(henrika): this class is only used on mobile devices and is 57 // TODO(henrika): this class is only used on mobile devices and is
58 // currently limited to mono. Modifications are needed for stereo. 58 // currently limited to mono. Modifications are needed for stereo.
59 return sizeof(int16_t) * samples_per_channel; 59 return sizeof(int16_t) * samples_per_channel;
60 }); 60 });
61 RTC_DCHECK_EQ(bytes_per_10_ms_, bytes_written); 61 RTC_DCHECK_EQ(bytes_per_10_ms_, bytes_written);
62 } 62 }
63 // Provide the requested number of bytes to the consumer. 63 // Provide the requested number of bytes to the consumer.
64 memcpy(buffer, playout_buffer_.data(), num_bytes); 64 memcpy(audio_buffer.data(), playout_buffer_.data(), num_bytes);
65 // Move remaining samples to start of buffer to prepare for next round. 65 // Move remaining samples to start of buffer to prepare for next round.
66 memmove(playout_buffer_.data(), playout_buffer_.data() + num_bytes, 66 memmove(playout_buffer_.data(), playout_buffer_.data() + num_bytes,
67 playout_buffer_.size() - num_bytes); 67 playout_buffer_.size() - num_bytes);
68 playout_buffer_.SetSize(playout_buffer_.size() - num_bytes); 68 playout_buffer_.SetSize(playout_buffer_.size() - num_bytes);
69 } 69 }
70 70
71 void FineAudioBuffer::DeliverRecordedData(const int8_t* buffer, 71 void FineAudioBuffer::DeliverRecordedData(const int8_t* buffer,
72 size_t size_in_bytes, 72 size_t size_in_bytes,
73 int playout_delay_ms, 73 int playout_delay_ms,
74 int record_delay_ms) { 74 int record_delay_ms) {
75 // Always append new data and grow the buffer if needed. 75 // Always append new data and grow the buffer if needed.
76 record_buffer_.AppendData(buffer, size_in_bytes); 76 record_buffer_.AppendData(buffer, size_in_bytes);
77 // Consume samples from buffer in chunks of 10ms until there is not 77 // Consume samples from buffer in chunks of 10ms until there is not
78 // enough data left. The number of remaining bytes in the cache is given by 78 // enough data left. The number of remaining bytes in the cache is given by
79 // the new size of the buffer. 79 // the new size of the buffer.
80 while (record_buffer_.size() >= bytes_per_10_ms_) { 80 while (record_buffer_.size() >= bytes_per_10_ms_) {
81 device_buffer_->SetRecordedBuffer(record_buffer_.data(), 81 device_buffer_->SetRecordedBuffer(record_buffer_.data(),
82 samples_per_10_ms_); 82 samples_per_10_ms_);
83 device_buffer_->SetVQEData(playout_delay_ms, record_delay_ms, 0); 83 device_buffer_->SetVQEData(playout_delay_ms, record_delay_ms, 0);
84 device_buffer_->DeliverRecordedData(); 84 device_buffer_->DeliverRecordedData();
85 memmove(record_buffer_.data(), record_buffer_.data() + bytes_per_10_ms_, 85 memmove(record_buffer_.data(), record_buffer_.data() + bytes_per_10_ms_,
86 record_buffer_.size() - bytes_per_10_ms_); 86 record_buffer_.size() - bytes_per_10_ms_);
87 record_buffer_.SetSize(record_buffer_.size() - bytes_per_10_ms_); 87 record_buffer_.SetSize(record_buffer_.size() - bytes_per_10_ms_);
88 } 88 }
89 } 89 }
90 90
91 } // namespace webrtc 91 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698