• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/modules/audio_device/audio_device_buffer.h"
18 
19 namespace webrtc {
20 
FineAudioBuffer(AudioDeviceBuffer * device_buffer,int desired_frame_size_bytes,int sample_rate)21 FineAudioBuffer::FineAudioBuffer(AudioDeviceBuffer* device_buffer,
22                                  int desired_frame_size_bytes,
23                                  int sample_rate)
24     : device_buffer_(device_buffer),
25       desired_frame_size_bytes_(desired_frame_size_bytes),
26       sample_rate_(sample_rate),
27       samples_per_10_ms_(sample_rate_ * 10 / 1000),
28       bytes_per_10_ms_(samples_per_10_ms_ * sizeof(int16_t)),
29       cached_buffer_start_(0),
30       cached_bytes_(0) {
31   cache_buffer_.reset(new int8_t[bytes_per_10_ms_]);
32 }
33 
~FineAudioBuffer()34 FineAudioBuffer::~FineAudioBuffer() {
35 }
36 
RequiredBufferSizeBytes()37 int FineAudioBuffer::RequiredBufferSizeBytes() {
38   // It is possible that we store the desired frame size - 1 samples. Since new
39   // audio frames are pulled in chunks of 10ms we will need a buffer that can
40   // hold desired_frame_size - 1 + 10ms of data. We omit the - 1.
41   return desired_frame_size_bytes_ + bytes_per_10_ms_;
42 }
43 
GetBufferData(int8_t * buffer)44 void FineAudioBuffer::GetBufferData(int8_t* buffer) {
45   if (desired_frame_size_bytes_ <= cached_bytes_) {
46     memcpy(buffer, &cache_buffer_.get()[cached_buffer_start_],
47            desired_frame_size_bytes_);
48     cached_buffer_start_ += desired_frame_size_bytes_;
49     cached_bytes_ -= desired_frame_size_bytes_;
50     assert(cached_buffer_start_ + cached_bytes_ < bytes_per_10_ms_);
51     return;
52   }
53   memcpy(buffer, &cache_buffer_.get()[cached_buffer_start_], cached_bytes_);
54   // Push another n*10ms of audio to |buffer|. n > 1 if
55   // |desired_frame_size_bytes_| is greater than 10ms of audio. Note that we
56   // write the audio after the cached bytes copied earlier.
57   int8_t* unwritten_buffer = &buffer[cached_bytes_];
58   int bytes_left = desired_frame_size_bytes_ - cached_bytes_;
59   // Ceiling of integer division: 1 + ((x - 1) / y)
60   int number_of_requests = 1 + (bytes_left - 1) / (bytes_per_10_ms_);
61   for (int i = 0; i < number_of_requests; ++i) {
62     device_buffer_->RequestPlayoutData(samples_per_10_ms_);
63     int num_out = device_buffer_->GetPlayoutData(unwritten_buffer);
64     if (num_out != samples_per_10_ms_) {
65       assert(num_out == 0);
66       cached_bytes_ = 0;
67       return;
68     }
69     unwritten_buffer += bytes_per_10_ms_;
70     assert(bytes_left >= 0);
71     bytes_left -= bytes_per_10_ms_;
72   }
73   assert(bytes_left <= 0);
74   // Put the samples that were written to |buffer| but are not used in the
75   // cache.
76   int cache_location = desired_frame_size_bytes_;
77   int8_t* cache_ptr = &buffer[cache_location];
78   cached_bytes_ = number_of_requests * bytes_per_10_ms_ -
79       (desired_frame_size_bytes_ - cached_bytes_);
80   // If cached_bytes_ is larger than the cache buffer, uninitialized memory
81   // will be read.
82   assert(cached_bytes_ <= bytes_per_10_ms_);
83   assert(-bytes_left == cached_bytes_);
84   cached_buffer_start_ = 0;
85   memcpy(cache_buffer_.get(), cache_ptr, cached_bytes_);
86 }
87 
88 }  // namespace webrtc
89