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_FINE_AUDIO_BUFFER_H_
12#define WEBRTC_MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_
13
14#include "webrtc/base/scoped_ptr.h"
15#include "webrtc/typedefs.h"
16
17namespace webrtc {
18
19class AudioDeviceBuffer;
20
21// FineAudioBuffer takes an AudioDeviceBuffer (ADB) which deals with 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. This class also ensures that audio data can be delivered to the ADB
26// in 10ms chunks when the size of the provided audio buffers differs from 10ms.
27// As an example: calling DeliverRecordedData() with 5ms buffers will deliver
28// accumulated 10ms worth of data to the ADB every second call.
29class FineAudioBuffer {
30 public:
31  // |device_buffer| is a buffer that provides 10ms of audio data.
32  // |desired_frame_size_bytes| is the number of bytes of audio data
33  // GetPlayoutData() should return on success. It is also the required size of
34  // each recorded buffer used in DeliverRecordedData() calls.
35  // |sample_rate| is the sample rate of the audio data. This is needed because
36  // |device_buffer| delivers 10ms of data. Given the sample rate the number
37  // of samples can be calculated.
38  FineAudioBuffer(AudioDeviceBuffer* device_buffer,
39                  size_t desired_frame_size_bytes,
40                  int sample_rate);
41  ~FineAudioBuffer();
42
43  // Returns the required size of |buffer| when calling GetPlayoutData(). If
44  // the buffer is smaller memory trampling will happen.
45  size_t RequiredPlayoutBufferSizeBytes();
46
47  // Clears buffers and counters dealing with playour and/or recording.
48  void ResetPlayout();
49  void ResetRecord();
50
51  // |buffer| must be of equal or greater size than what is returned by
52  // RequiredBufferSize(). This is to avoid unnecessary memcpy.
53  void GetPlayoutData(int8_t* buffer);
54
55  // Consumes the audio data in |buffer| and sends it to the WebRTC layer in
56  // chunks of 10ms. The provided delay estimates in |playout_delay_ms| and
57  // |record_delay_ms| are given to the AEC in the audio processing module.
58  // They can be fixed values on most platforms and they are ignored if an
59  // external (hardware/built-in) AEC is used.
60  // The size of |buffer| is given by |size_in_bytes| and must be equal to
61  // |desired_frame_size_bytes_|. A RTC_CHECK will be hit if this is not the
62  // case.
63  // Example: buffer size is 5ms => call #1 stores 5ms of data, call #2 stores
64  // 5ms of data and sends a total of 10ms to WebRTC and clears the intenal
65  // cache. Call #3 restarts the scheme above.
66  void DeliverRecordedData(const int8_t* buffer,
67                           size_t size_in_bytes,
68                           int playout_delay_ms,
69                           int record_delay_ms);
70
71 private:
72  // Device buffer that works with 10ms chunks of data both for playout and
73  // for recording. I.e., the WebRTC side will always be asked for audio to be
74  // played out in 10ms chunks and recorded audio will be sent to WebRTC in
75  // 10ms chunks as well. This pointer is owned by the constructor of this
76  // class and the owner must ensure that the pointer is valid during the life-
77  // time of this object.
78  AudioDeviceBuffer* const device_buffer_;
79  // Number of bytes delivered by GetPlayoutData() call and provided to
80  // DeliverRecordedData().
81  const size_t desired_frame_size_bytes_;
82  // Sample rate in Hertz.
83  const int sample_rate_;
84  // Number of audio samples per 10ms.
85  const size_t samples_per_10_ms_;
86  // Number of audio bytes per 10ms.
87  const size_t bytes_per_10_ms_;
88  // Storage for output samples that are not yet asked for.
89  rtc::scoped_ptr<int8_t[]> playout_cache_buffer_;
90  // Location of first unread output sample.
91  size_t playout_cached_buffer_start_;
92  // Number of bytes stored in output (contain samples to be played out) cache.
93  size_t playout_cached_bytes_;
94  // Storage for input samples that are about to be delivered to the WebRTC
95  // ADB or remains from the last successful delivery of a 10ms audio buffer.
96  rtc::scoped_ptr<int8_t[]> record_cache_buffer_;
97  // Required (max) size in bytes of the |record_cache_buffer_|.
98  const size_t required_record_buffer_size_bytes_;
99  // Number of bytes in input (contains recorded samples) cache.
100  size_t record_cached_bytes_;
101  // Read and write pointers used in the buffering scheme on the recording side.
102  size_t record_read_pos_;
103  size_t record_write_pos_;
104};
105
106}  // namespace webrtc
107
108#endif  // WEBRTC_MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_
109