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