blob: 5bc42ba2140030dc89f8d489154408d0a178d9c6 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_
12#define MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_
henrika86d907c2015-09-07 16:09:50 +020013
kwibergf01633e2016-02-24 05:00:36 -080014#include <memory>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/array_view.h"
17#include "rtc_base/buffer.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020018#include "typedefs.h" // NOLINT(build/include)
henrika86d907c2015-09-07 16:09:50 +020019
20namespace webrtc {
21
22class AudioDeviceBuffer;
23
henrika8d7393b2018-04-19 13:40:15 +020024// FineAudioBuffer takes an AudioDeviceBuffer (ADB) which deals with 16-bit PCM
25// audio samples corresponding to 10ms of data. It then allows for this data
26// to be pulled in a finer or coarser granularity. I.e. interacting with this
27// class instead of directly with the AudioDeviceBuffer one can ask for any
28// number of audio data samples. This class also ensures that audio data can be
29// delivered to the ADB in 10ms chunks when the size of the provided audio
30// buffers differs from 10ms.
henrika86d907c2015-09-07 16:09:50 +020031// As an example: calling DeliverRecordedData() with 5ms buffers will deliver
32// accumulated 10ms worth of data to the ADB every second call.
henrikabb6f7522017-05-30 02:01:30 -070033// TODO(henrika): add support for stereo when mobile platforms need it.
henrika86d907c2015-09-07 16:09:50 +020034class FineAudioBuffer {
35 public:
36 // |device_buffer| is a buffer that provides 10ms of audio data.
henrika86d907c2015-09-07 16:09:50 +020037 // |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
henrikabb6f7522017-05-30 02:01:30 -070039 // of samples can be calculated. The |capacity| ensures that the buffer size
40 // can be increased to at least capacity without further reallocation.
henrika86d907c2015-09-07 16:09:50 +020041 FineAudioBuffer(AudioDeviceBuffer* device_buffer,
henrikabb6f7522017-05-30 02:01:30 -070042 int sample_rate,
43 size_t capacity);
henrika86d907c2015-09-07 16:09:50 +020044 ~FineAudioBuffer();
45
henrika8d7393b2018-04-19 13:40:15 +020046 // Clears buffers and counters dealing with playout and/or recording.
henrika86d907c2015-09-07 16:09:50 +020047 void ResetPlayout();
48 void ResetRecord();
49
henrikabb6f7522017-05-30 02:01:30 -070050 // Copies audio samples into |audio_buffer| where number of requested
51 // elements is specified by |audio_buffer.size()|. The producer will always
52 // fill up the audio buffer and if no audio exists, the buffer will contain
henrika883d00f2018-03-16 10:09:49 +010053 // silence instead. The provided delay estimate in |playout_delay_ms| should
54 // contain an estime of the latency between when an audio frame is read from
55 // WebRTC and when it is played out on the speaker.
henrika8d7393b2018-04-19 13:40:15 +020056 void GetPlayoutData(rtc::ArrayView<int16_t> audio_buffer,
henrika883d00f2018-03-16 10:09:49 +010057 int playout_delay_ms);
henrika86d907c2015-09-07 16:09:50 +020058
henrikabb6f7522017-05-30 02:01:30 -070059 // Consumes the audio data in |audio_buffer| and sends it to the WebRTC layer
henrika883d00f2018-03-16 10:09:49 +010060 // in chunks of 10ms. The sum of the provided delay estimate in
61 // |record_delay_ms| and the latest |playout_delay_ms| in GetPlayoutData()
62 // are given to the AEC in the audio processing module.
henrika86d907c2015-09-07 16:09:50 +020063 // They can be fixed values on most platforms and they are ignored if an
64 // external (hardware/built-in) AEC is used.
henrika86d907c2015-09-07 16:09:50 +020065 // Example: buffer size is 5ms => call #1 stores 5ms of data, call #2 stores
henrika8d7393b2018-04-19 13:40:15 +020066 // 5ms of data and sends a total of 10ms to WebRTC and clears the internal
henrika86d907c2015-09-07 16:09:50 +020067 // cache. Call #3 restarts the scheme above.
henrika8d7393b2018-04-19 13:40:15 +020068 void DeliverRecordedData(rtc::ArrayView<const int16_t> audio_buffer,
henrika86d907c2015-09-07 16:09:50 +020069 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_;
henrika86d907c2015-09-07 16:09:50 +020079 // Sample rate in Hertz.
80 const int sample_rate_;
81 // Number of audio samples per 10ms.
82 const size_t samples_per_10_ms_;
henrikabb6f7522017-05-30 02:01:30 -070083 // Storage for output samples from which a consumer can read audio buffers
84 // in any size using GetPlayoutData().
henrika8d7393b2018-04-19 13:40:15 +020085 rtc::BufferT<int16_t> playout_buffer_;
henrika86d907c2015-09-07 16:09:50 +020086 // Storage for input samples that are about to be delivered to the WebRTC
87 // ADB or remains from the last successful delivery of a 10ms audio buffer.
henrika8d7393b2018-04-19 13:40:15 +020088 rtc::BufferT<int16_t> record_buffer_;
henrika883d00f2018-03-16 10:09:49 +010089 // Contains latest delay estimate given to GetPlayoutData().
90 int playout_delay_ms_ = 0;
henrika86d907c2015-09-07 16:09:50 +020091};
92
93} // namespace webrtc
94
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020095#endif // MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_