blob: 14d5e0cf061829c48027fe24e5346359d1bbdcc2 [file] [log] [blame]
henrika86d907c2015-09-07 16:09:50 +02001/*
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 CHECK will be hit if this is not the case.
62 // Example: buffer size is 5ms => call #1 stores 5ms of data, call #2 stores
63 // 5ms of data and sends a total of 10ms to WebRTC and clears the intenal
64 // cache. Call #3 restarts the scheme above.
65 void DeliverRecordedData(const int8_t* buffer,
66 size_t size_in_bytes,
67 int playout_delay_ms,
68 int record_delay_ms);
69
70 private:
71 // Device buffer that works with 10ms chunks of data both for playout and
72 // for recording. I.e., the WebRTC side will always be asked for audio to be
73 // played out in 10ms chunks and recorded audio will be sent to WebRTC in
74 // 10ms chunks as well. This pointer is owned by the constructor of this
75 // class and the owner must ensure that the pointer is valid during the life-
76 // time of this object.
77 AudioDeviceBuffer* const device_buffer_;
78 // Number of bytes delivered by GetPlayoutData() call and provided to
79 // DeliverRecordedData().
80 const size_t desired_frame_size_bytes_;
81 // Sample rate in Hertz.
82 const int sample_rate_;
83 // Number of audio samples per 10ms.
84 const size_t samples_per_10_ms_;
85 // Number of audio bytes per 10ms.
86 const size_t bytes_per_10_ms_;
87 // Storage for output samples that are not yet asked for.
88 rtc::scoped_ptr<int8_t[]> playout_cache_buffer_;
89 // Location of first unread output sample.
90 size_t playout_cached_buffer_start_;
91 // Number of bytes stored in output (contain samples to be played out) cache.
92 size_t playout_cached_bytes_;
93 // Storage for input samples that are about to be delivered to the WebRTC
94 // ADB or remains from the last successful delivery of a 10ms audio buffer.
95 rtc::scoped_ptr<int8_t[]> record_cache_buffer_;
96 // Required (max) size in bytes of the |record_cache_buffer_|.
97 const size_t required_record_buffer_size_bytes_;
98 // Number of bytes in input (contains recorded samples) cache.
99 size_t record_cached_bytes_;
100 // Read and write pointers used in the buffering scheme on the recording side.
101 size_t record_read_pos_;
102 size_t record_write_pos_;
103};
104
105} // namespace webrtc
106
107#endif // WEBRTC_MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_