henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_ |
| 12 | #define MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_ |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "api/array_view.h" |
| 15 | #include "rtc_base/buffer.h" |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | class AudioDeviceBuffer; |
| 20 | |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame] | 21 | // 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. |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 28 | // As an example: calling DeliverRecordedData() with 5ms buffers will deliver |
| 29 | // accumulated 10ms worth of data to the ADB every second call. |
| 30 | class FineAudioBuffer { |
| 31 | public: |
| 32 | // |device_buffer| is a buffer that provides 10ms of audio data. |
henrika | 29e865a | 2018-04-24 13:22:31 +0200 | [diff] [blame] | 33 | FineAudioBuffer(AudioDeviceBuffer* audio_device_buffer); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 34 | ~FineAudioBuffer(); |
| 35 | |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame] | 36 | // Clears buffers and counters dealing with playout and/or recording. |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 37 | void ResetPlayout(); |
| 38 | void ResetRecord(); |
| 39 | |
henrika | 29e865a | 2018-04-24 13:22:31 +0200 | [diff] [blame] | 40 | // Utility methods which returns true if valid parameters are acquired at |
| 41 | // constructions. |
| 42 | bool IsReadyForPlayout() const; |
| 43 | bool IsReadyForRecord() const; |
| 44 | |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 45 | // 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 |
henrika | 883d00f | 2018-03-16 10:09:49 +0100 | [diff] [blame] | 48 | // silence instead. The provided delay estimate in |playout_delay_ms| should |
henrika | 29e865a | 2018-04-24 13:22:31 +0200 | [diff] [blame] | 49 | // contain an estimate of the latency between when an audio frame is read from |
henrika | 883d00f | 2018-03-16 10:09:49 +0100 | [diff] [blame] | 50 | // WebRTC and when it is played out on the speaker. |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame] | 51 | void GetPlayoutData(rtc::ArrayView<int16_t> audio_buffer, |
henrika | 883d00f | 2018-03-16 10:09:49 +0100 | [diff] [blame] | 52 | int playout_delay_ms); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 53 | |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 54 | // Consumes the audio data in |audio_buffer| and sends it to the WebRTC layer |
henrika | 883d00f | 2018-03-16 10:09:49 +0100 | [diff] [blame] | 55 | // 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. |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 58 | // They can be fixed values on most platforms and they are ignored if an |
| 59 | // external (hardware/built-in) AEC is used. |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 60 | // Example: buffer size is 5ms => call #1 stores 5ms of data, call #2 stores |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame] | 61 | // 5ms of data and sends a total of 10ms to WebRTC and clears the internal |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 62 | // cache. Call #3 restarts the scheme above. |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame] | 63 | void DeliverRecordedData(rtc::ArrayView<const int16_t> audio_buffer, |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 64 | 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 |
henrika | 29e865a | 2018-04-24 13:22:31 +0200 | [diff] [blame] | 70 | // 10ms chunks as well. This raw pointer is owned by the constructor of this |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 71 | // class and the owner must ensure that the pointer is valid during the life- |
| 72 | // time of this object. |
henrika | 29e865a | 2018-04-24 13:22:31 +0200 | [diff] [blame] | 73 | 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_; |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 82 | // Storage for output samples from which a consumer can read audio buffers |
| 83 | // in any size using GetPlayoutData(). |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame] | 84 | rtc::BufferT<int16_t> playout_buffer_; |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 85 | // 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. |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame] | 87 | rtc::BufferT<int16_t> record_buffer_; |
henrika | 883d00f | 2018-03-16 10:09:49 +0100 | [diff] [blame] | 88 | // Contains latest delay estimate given to GetPlayoutData(). |
| 89 | int playout_delay_ms_ = 0; |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | } // namespace webrtc |
| 93 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 94 | #endif // MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_ |