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 | |
kwiberg | f01633e | 2016-02-24 05:00:36 -0800 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "api/array_view.h" |
| 17 | #include "rtc_base/buffer.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 18 | #include "typedefs.h" // NOLINT(build/include) |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | class AudioDeviceBuffer; |
| 23 | |
| 24 | // FineAudioBuffer takes an AudioDeviceBuffer (ADB) which deals with audio data |
| 25 | // corresponding to 10ms of data. It then allows for this data to be pulled in |
| 26 | // a finer or coarser granularity. I.e. interacting with this class instead of |
| 27 | // directly with the AudioDeviceBuffer one can ask for any number of audio data |
| 28 | // samples. This class also ensures that audio data can be delivered to the ADB |
| 29 | // in 10ms chunks when the size of the provided audio buffers differs from 10ms. |
| 30 | // As an example: calling DeliverRecordedData() with 5ms buffers will deliver |
| 31 | // accumulated 10ms worth of data to the ADB every second call. |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 32 | // TODO(henrika): add support for stereo when mobile platforms need it. |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 33 | class FineAudioBuffer { |
| 34 | public: |
| 35 | // |device_buffer| is a buffer that provides 10ms of audio data. |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 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 |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 38 | // of samples can be calculated. The |capacity| ensures that the buffer size |
| 39 | // can be increased to at least capacity without further reallocation. |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 40 | FineAudioBuffer(AudioDeviceBuffer* device_buffer, |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 41 | int sample_rate, |
| 42 | size_t capacity); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 43 | ~FineAudioBuffer(); |
| 44 | |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 45 | // Clears buffers and counters dealing with playour and/or recording. |
| 46 | void ResetPlayout(); |
| 47 | void ResetRecord(); |
| 48 | |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 49 | // Copies audio samples into |audio_buffer| where number of requested |
| 50 | // elements is specified by |audio_buffer.size()|. The producer will always |
| 51 | // 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^] | 52 | // silence instead. The provided delay estimate in |playout_delay_ms| should |
| 53 | // contain an estime of the latency between when an audio frame is read from |
| 54 | // WebRTC and when it is played out on the speaker. |
| 55 | void GetPlayoutData(rtc::ArrayView<int8_t> audio_buffer, |
| 56 | int playout_delay_ms); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 57 | |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 58 | // 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^] | 59 | // in chunks of 10ms. The sum of the provided delay estimate in |
| 60 | // |record_delay_ms| and the latest |playout_delay_ms| in GetPlayoutData() |
| 61 | // are given to the AEC in the audio processing module. |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 62 | // They can be fixed values on most platforms and they are ignored if an |
| 63 | // external (hardware/built-in) AEC is used. |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 64 | // 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. |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 67 | void DeliverRecordedData(rtc::ArrayView<const int8_t> audio_buffer, |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 68 | int record_delay_ms); |
| 69 | |
| 70 | private: |
| 71 | // Device buffer that works with 10ms chunks of data both for playout and |
| 72 | // for recording. I.e., the WebRTC side will always be asked for audio to be |
| 73 | // played out in 10ms chunks and recorded audio will be sent to WebRTC in |
| 74 | // 10ms chunks as well. This pointer is owned by the constructor of this |
| 75 | // class and the owner must ensure that the pointer is valid during the life- |
| 76 | // time of this object. |
| 77 | AudioDeviceBuffer* const device_buffer_; |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 78 | // Sample rate in Hertz. |
| 79 | const int sample_rate_; |
| 80 | // Number of audio samples per 10ms. |
| 81 | const size_t samples_per_10_ms_; |
| 82 | // Number of audio bytes per 10ms. |
| 83 | const size_t bytes_per_10_ms_; |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 84 | // Storage for output samples from which a consumer can read audio buffers |
| 85 | // in any size using GetPlayoutData(). |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 86 | rtc::BufferT<int8_t> playout_buffer_; |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 87 | // Storage for input samples that are about to be delivered to the WebRTC |
| 88 | // ADB or remains from the last successful delivery of a 10ms audio buffer. |
henrika | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame] | 89 | rtc::BufferT<int8_t> record_buffer_; |
henrika | 883d00f | 2018-03-16 10:09:49 +0100 | [diff] [blame^] | 90 | // Contains latest delay estimate given to GetPlayoutData(). |
| 91 | int playout_delay_ms_ = 0; |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | } // namespace webrtc |
| 95 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 96 | #endif // MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_ |