blob: 6bd047eca46f9f4d805b8236a0669daa59740892 [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
52 // silence instead.
53 void GetPlayoutData(rtc::ArrayView<int8_t> audio_buffer);
henrika86d907c2015-09-07 16:09:50 +020054
henrikabb6f7522017-05-30 02:01:30 -070055 // Consumes the audio data in |audio_buffer| and sends it to the WebRTC layer
56 // in chunks of 10ms. The provided delay estimates in |playout_delay_ms| and
henrika86d907c2015-09-07 16:09:50 +020057 // |record_delay_ms| are given to the AEC in the audio processing module.
58 // 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
61 // 5ms of data and sends a total of 10ms to WebRTC and clears the intenal
62 // cache. Call #3 restarts the scheme above.
henrikabb6f7522017-05-30 02:01:30 -070063 void DeliverRecordedData(rtc::ArrayView<const int8_t> audio_buffer,
henrika86d907c2015-09-07 16:09:50 +020064 int playout_delay_ms,
65 int record_delay_ms);
66
67 private:
68 // Device buffer that works with 10ms chunks of data both for playout and
69 // for recording. I.e., the WebRTC side will always be asked for audio to be
70 // played out in 10ms chunks and recorded audio will be sent to WebRTC in
71 // 10ms chunks as well. This pointer is owned by the constructor of this
72 // class and the owner must ensure that the pointer is valid during the life-
73 // time of this object.
74 AudioDeviceBuffer* const device_buffer_;
henrika86d907c2015-09-07 16:09:50 +020075 // Sample rate in Hertz.
76 const int sample_rate_;
77 // Number of audio samples per 10ms.
78 const size_t samples_per_10_ms_;
79 // Number of audio bytes per 10ms.
80 const size_t bytes_per_10_ms_;
henrikabb6f7522017-05-30 02:01:30 -070081 // Storage for output samples from which a consumer can read audio buffers
82 // in any size using GetPlayoutData().
henrikab3ebc1a2017-02-27 05:14:17 -080083 rtc::BufferT<int8_t> playout_buffer_;
henrika86d907c2015-09-07 16:09:50 +020084 // Storage for input samples that are about to be delivered to the WebRTC
85 // ADB or remains from the last successful delivery of a 10ms audio buffer.
henrikaf166e1b2017-02-23 02:44:55 -080086 rtc::BufferT<int8_t> record_buffer_;
henrika86d907c2015-09-07 16:09:50 +020087};
88
89} // namespace webrtc
90
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020091#endif // MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_