blob: fb80e98842c456d074ada213c9ee2d7d66aca611 [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
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.
henrikabb6f7522017-05-30 02:01:30 -070032// TODO(henrika): add support for stereo when mobile platforms need it.
henrika86d907c2015-09-07 16:09:50 +020033class FineAudioBuffer {
34 public:
35 // |device_buffer| is a buffer that provides 10ms of audio data.
henrika86d907c2015-09-07 16:09:50 +020036 // |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
henrikabb6f7522017-05-30 02:01:30 -070038 // of samples can be calculated. The |capacity| ensures that the buffer size
39 // can be increased to at least capacity without further reallocation.
henrika86d907c2015-09-07 16:09:50 +020040 FineAudioBuffer(AudioDeviceBuffer* device_buffer,
henrikabb6f7522017-05-30 02:01:30 -070041 int sample_rate,
42 size_t capacity);
henrika86d907c2015-09-07 16:09:50 +020043 ~FineAudioBuffer();
44
henrika86d907c2015-09-07 16:09:50 +020045 // Clears buffers and counters dealing with playour and/or recording.
46 void ResetPlayout();
47 void ResetRecord();
48
henrikabb6f7522017-05-30 02:01:30 -070049 // 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
henrika883d00f2018-03-16 10:09:49 +010052 // 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);
henrika86d907c2015-09-07 16:09:50 +020057
henrikabb6f7522017-05-30 02:01:30 -070058 // Consumes the audio data in |audio_buffer| and sends it to the WebRTC layer
henrika883d00f2018-03-16 10:09:49 +010059 // 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.
henrika86d907c2015-09-07 16:09:50 +020062 // They can be fixed values on most platforms and they are ignored if an
63 // external (hardware/built-in) AEC is used.
henrika86d907c2015-09-07 16:09:50 +020064 // 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.
henrikabb6f7522017-05-30 02:01:30 -070067 void DeliverRecordedData(rtc::ArrayView<const int8_t> audio_buffer,
henrika86d907c2015-09-07 16:09:50 +020068 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_;
henrika86d907c2015-09-07 16:09:50 +020078 // 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_;
henrikabb6f7522017-05-30 02:01:30 -070084 // Storage for output samples from which a consumer can read audio buffers
85 // in any size using GetPlayoutData().
henrikab3ebc1a2017-02-27 05:14:17 -080086 rtc::BufferT<int8_t> playout_buffer_;
henrika86d907c2015-09-07 16:09:50 +020087 // 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.
henrikaf166e1b2017-02-23 02:44:55 -080089 rtc::BufferT<int8_t> record_buffer_;
henrika883d00f2018-03-16 10:09:49 +010090 // Contains latest delay estimate given to GetPlayoutData().
91 int playout_delay_ms_ = 0;
henrika86d907c2015-09-07 16:09:50 +020092};
93
94} // namespace webrtc
95
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020096#endif // MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_