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#ifndef WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_FINE_AUDIO_BUFFER_H_
12#define WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_FINE_AUDIO_BUFFER_H_
13
14#include "webrtc/system_wrappers/interface/scoped_ptr.h"
15#include "webrtc/typedefs.h"
16
17namespace webrtc {
18
19class AudioDeviceBuffer;
20
21// FineAudioBuffer takes an AudioDeviceBuffer which delivers audio data
22// corresponding to 10ms of data. It then allows for this data to be pulled in
23// a finer or coarser granularity. I.e. interacting with this class instead of
24// directly with the AudioDeviceBuffer one can ask for any number of audio data
25// samples.
26class FineAudioBuffer {
27 public:
28  // |device_buffer| is a buffer that provides 10ms of audio data.
29  // |desired_frame_size_bytes| is the number of bytes of audio data
30  // (not samples) |GetBufferData| should return on success.
31  // |sample_rate| is the sample rate of the audio data. This is needed because
32  // |device_buffer| delivers 10ms of data. Given the sample rate the number
33  // of samples can be calculated.
34  FineAudioBuffer(AudioDeviceBuffer* device_buffer,
35                  int desired_frame_size_bytes,
36                  int sample_rate);
37  ~FineAudioBuffer();
38
39  // Returns the required size of |buffer| when calling GetBufferData. If the
40  // buffer is smaller memory trampling will happen.
41  // |desired_frame_size_bytes| and |samples_rate| are as described in the
42  // constructor.
43  int RequiredBufferSizeBytes();
44
45  // |buffer| must be of equal or greater size than what is returned by
46  // RequiredBufferSize. This is to avoid unnecessary memcpy.
47  void GetBufferData(int8_t* buffer);
48
49 private:
50  // Device buffer that provides 10ms chunks of data.
51  AudioDeviceBuffer* device_buffer_;
52  int desired_frame_size_bytes_;  // Number of bytes delivered per GetBufferData
53  int sample_rate_;
54  int samples_per_10_ms_;
55  // Convenience parameter to avoid converting from samples
56  int bytes_per_10_ms_;
57
58  // Storage for samples that are not yet asked for.
59  scoped_ptr<int8_t[]> cache_buffer_;
60  int cached_buffer_start_;  // Location of first unread sample.
61  int cached_bytes_;  // Number of bytes stored in cache.
62};
63
64}  // namespace webrtc
65
66#endif  // WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_FINE_AUDIO_BUFFER_H_
67