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