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 | |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame^] | 24 | // 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. |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 31 | // As an example: calling DeliverRecordedData() with 5ms buffers will deliver |
| 32 | // accumulated 10ms worth of data to the ADB every second call. |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 33 | // TODO(henrika): add support for stereo when mobile platforms need it. |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 34 | class FineAudioBuffer { |
| 35 | public: |
| 36 | // |device_buffer| is a buffer that provides 10ms of audio data. |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 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 |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 39 | // of samples can be calculated. The |capacity| ensures that the buffer size |
| 40 | // can be increased to at least capacity without further reallocation. |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 41 | FineAudioBuffer(AudioDeviceBuffer* device_buffer, |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 42 | int sample_rate, |
| 43 | size_t capacity); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 44 | ~FineAudioBuffer(); |
| 45 | |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame^] | 46 | // Clears buffers and counters dealing with playout and/or recording. |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 47 | void ResetPlayout(); |
| 48 | void ResetRecord(); |
| 49 | |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 50 | // 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 |
henrika | 883d00f | 2018-03-16 10:09:49 +0100 | [diff] [blame] | 53 | // 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. |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame^] | 56 | void GetPlayoutData(rtc::ArrayView<int16_t> audio_buffer, |
henrika | 883d00f | 2018-03-16 10:09:49 +0100 | [diff] [blame] | 57 | int playout_delay_ms); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 58 | |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 59 | // 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] | 60 | // 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. |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 63 | // They can be fixed values on most platforms and they are ignored if an |
| 64 | // external (hardware/built-in) AEC is used. |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 65 | // 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^] | 66 | // 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] | 67 | // cache. Call #3 restarts the scheme above. |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame^] | 68 | void DeliverRecordedData(rtc::ArrayView<const int16_t> audio_buffer, |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 69 | 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_; |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 79 | // Sample rate in Hertz. |
| 80 | const int sample_rate_; |
| 81 | // Number of audio samples per 10ms. |
| 82 | const size_t samples_per_10_ms_; |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 83 | // Storage for output samples from which a consumer can read audio buffers |
| 84 | // in any size using GetPlayoutData(). |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame^] | 85 | rtc::BufferT<int16_t> playout_buffer_; |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 86 | // 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. |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame^] | 88 | rtc::BufferT<int16_t> record_buffer_; |
henrika | 883d00f | 2018-03-16 10:09:49 +0100 | [diff] [blame] | 89 | // Contains latest delay estimate given to GetPlayoutData(). |
| 90 | int playout_delay_ms_ = 0; |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | } // namespace webrtc |
| 94 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 95 | #endif // MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_ |