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

Side by Side Diff: webrtc/modules/audio_device/android/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: Rebased and cleaned up 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
(Empty)
1 /*
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/android/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/modules/audio_device/audio_device_buffer.h"
19
20 namespace webrtc {
21
22 FineAudioBuffer::FineAudioBuffer(AudioDeviceBuffer* device_buffer,
23 size_t desired_frame_size_bytes,
24 int sample_rate)
25 : device_buffer_(device_buffer),
26 desired_frame_size_bytes_(desired_frame_size_bytes),
27 sample_rate_(sample_rate),
28 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 cached_buffer_start_(0),
31 cached_bytes_(0) {
32 cache_buffer_.reset(new int8_t[bytes_per_10_ms_]);
33 }
34
35 FineAudioBuffer::~FineAudioBuffer() {
36 }
37
38 size_t FineAudioBuffer::RequiredBufferSizeBytes() {
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::GetBufferData(int8_t* buffer) {
46 if (desired_frame_size_bytes_ <= cached_bytes_) {
47 memcpy(buffer, &cache_buffer_.get()[cached_buffer_start_],
48 desired_frame_size_bytes_);
49 cached_buffer_start_ += desired_frame_size_bytes_;
50 cached_bytes_ -= desired_frame_size_bytes_;
51 CHECK_LT(cached_buffer_start_ + cached_bytes_, bytes_per_10_ms_);
52 return;
53 }
54 memcpy(buffer, &cache_buffer_.get()[cached_buffer_start_], cached_bytes_);
55 // 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
57 // write the audio after the cached bytes copied earlier.
58 int8_t* unwritten_buffer = &buffer[cached_bytes_];
59 int bytes_left = static_cast<int>(desired_frame_size_bytes_ - cached_bytes_);
60 // Ceiling of integer division: 1 + ((x - 1) / y)
61 size_t number_of_requests = 1 + (bytes_left - 1) / (bytes_per_10_ms_);
62 for (size_t i = 0; i < number_of_requests; ++i) {
63 device_buffer_->RequestPlayoutData(samples_per_10_ms_);
64 int num_out = device_buffer_->GetPlayoutData(unwritten_buffer);
65 if (static_cast<size_t>(num_out) != samples_per_10_ms_) {
66 CHECK_EQ(num_out, 0);
67 cached_bytes_ = 0;
68 return;
69 }
70 unwritten_buffer += bytes_per_10_ms_;
71 CHECK_GE(bytes_left, 0);
72 bytes_left -= bytes_per_10_ms_;
73 }
74 CHECK_LE(bytes_left, 0);
75 // Put the samples that were written to |buffer| but are not used in the
76 // cache.
77 size_t cache_location = desired_frame_size_bytes_;
78 int8_t* cache_ptr = &buffer[cache_location];
79 cached_bytes_ = number_of_requests * bytes_per_10_ms_ -
80 (desired_frame_size_bytes_ - cached_bytes_);
81 // If cached_bytes_ is larger than the cache buffer, uninitialized memory
82 // will be read.
83 CHECK_LE(cached_bytes_, bytes_per_10_ms_);
84 CHECK_EQ(static_cast<size_t>(-bytes_left), cached_bytes_);
85 cached_buffer_start_ = 0;
86 memcpy(cache_buffer_.get(), cache_ptr, cached_bytes_);
87 }
88
89 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698