blob: 210eda834a8c06ae48c5fb06fcee9cc0148b89ff [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "api/array_view.h"
15#include "rtc_base/buffer.h"
henrika86d907c2015-09-07 16:09:50 +020016
17namespace webrtc {
18
19class AudioDeviceBuffer;
20
henrika8d7393b2018-04-19 13:40:15 +020021// FineAudioBuffer takes an AudioDeviceBuffer (ADB) which deals with 16-bit PCM
22// audio samples corresponding to 10ms of data. It then allows for this data
23// to be pulled in a finer or coarser granularity. I.e. interacting with this
24// class instead of directly with the AudioDeviceBuffer one can ask for any
25// number of audio data samples. This class also ensures that audio data can be
26// delivered to the ADB in 10ms chunks when the size of the provided audio
27// buffers differs from 10ms.
henrika86d907c2015-09-07 16:09:50 +020028// 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.
henrika29e865a2018-04-24 13:22:31 +020033 FineAudioBuffer(AudioDeviceBuffer* audio_device_buffer);
henrika86d907c2015-09-07 16:09:50 +020034 ~FineAudioBuffer();
35
henrika8d7393b2018-04-19 13:40:15 +020036 // Clears buffers and counters dealing with playout and/or recording.
henrika86d907c2015-09-07 16:09:50 +020037 void ResetPlayout();
38 void ResetRecord();
39
henrika29e865a2018-04-24 13:22:31 +020040 // Utility methods which returns true if valid parameters are acquired at
41 // constructions.
42 bool IsReadyForPlayout() const;
43 bool IsReadyForRecord() const;
44
henrikabb6f7522017-05-30 02:01:30 -070045 // Copies audio samples into |audio_buffer| where number of requested
46 // elements is specified by |audio_buffer.size()|. The producer will always
47 // fill up the audio buffer and if no audio exists, the buffer will contain
henrika883d00f2018-03-16 10:09:49 +010048 // silence instead. The provided delay estimate in |playout_delay_ms| should
henrika29e865a2018-04-24 13:22:31 +020049 // contain an estimate of the latency between when an audio frame is read from
henrika883d00f2018-03-16 10:09:49 +010050 // WebRTC and when it is played out on the speaker.
henrika8d7393b2018-04-19 13:40:15 +020051 void GetPlayoutData(rtc::ArrayView<int16_t> audio_buffer,
henrika883d00f2018-03-16 10:09:49 +010052 int playout_delay_ms);
henrika86d907c2015-09-07 16:09:50 +020053
henrikabb6f7522017-05-30 02:01:30 -070054 // Consumes the audio data in |audio_buffer| and sends it to the WebRTC layer
henrika883d00f2018-03-16 10:09:49 +010055 // in chunks of 10ms. The sum of the provided delay estimate in
56 // |record_delay_ms| and the latest |playout_delay_ms| in GetPlayoutData()
57 // are given to the AEC in the audio processing module.
henrika86d907c2015-09-07 16:09:50 +020058 // They can be fixed values on most platforms and they are ignored if an
59 // external (hardware/built-in) AEC is used.
henrika86d907c2015-09-07 16:09:50 +020060 // Example: buffer size is 5ms => call #1 stores 5ms of data, call #2 stores
henrika8d7393b2018-04-19 13:40:15 +020061 // 5ms of data and sends a total of 10ms to WebRTC and clears the internal
henrika86d907c2015-09-07 16:09:50 +020062 // cache. Call #3 restarts the scheme above.
henrika8d7393b2018-04-19 13:40:15 +020063 void DeliverRecordedData(rtc::ArrayView<const int16_t> audio_buffer,
henrika86d907c2015-09-07 16:09:50 +020064 int record_delay_ms);
65
66 private:
67 // Device buffer that works with 10ms chunks of data both for playout and
68 // for recording. I.e., the WebRTC side will always be asked for audio to be
69 // played out in 10ms chunks and recorded audio will be sent to WebRTC in
henrika29e865a2018-04-24 13:22:31 +020070 // 10ms chunks as well. This raw pointer is owned by the constructor of this
henrika86d907c2015-09-07 16:09:50 +020071 // class and the owner must ensure that the pointer is valid during the life-
72 // time of this object.
henrika29e865a2018-04-24 13:22:31 +020073 AudioDeviceBuffer* const audio_device_buffer_;
74 // Number of audio samples per channel per 10ms. Set once at construction
75 // based on parameters in |audio_device_buffer|.
76 const size_t playout_samples_per_channel_10ms_;
77 const size_t record_samples_per_channel_10ms_;
78 // Number of audio channels. Set once at construction based on parameters in
79 // |audio_device_buffer|.
80 const size_t playout_channels_;
81 const size_t record_channels_;
henrikabb6f7522017-05-30 02:01:30 -070082 // Storage for output samples from which a consumer can read audio buffers
83 // in any size using GetPlayoutData().
henrika8d7393b2018-04-19 13:40:15 +020084 rtc::BufferT<int16_t> playout_buffer_;
henrika86d907c2015-09-07 16:09:50 +020085 // Storage for input samples that are about to be delivered to the WebRTC
86 // ADB or remains from the last successful delivery of a 10ms audio buffer.
henrika8d7393b2018-04-19 13:40:15 +020087 rtc::BufferT<int16_t> record_buffer_;
henrika883d00f2018-03-16 10:09:49 +010088 // Contains latest delay estimate given to GetPlayoutData().
89 int playout_delay_ms_ = 0;
henrika86d907c2015-09-07 16:09:50 +020090};
91
92} // namespace webrtc
93
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020094#endif // MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_