blob: 306f9d24d377a7fe3ec31e0d01e298f86bf2db63 [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
11#ifndef WEBRTC_MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_
12#define WEBRTC_MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_
13
kwibergf01633e2016-02-24 05:00:36 -080014#include <memory>
15
henrikaf166e1b2017-02-23 02:44:55 -080016#include "webrtc/base/buffer.h"
henrika86d907c2015-09-07 16:09:50 +020017#include "webrtc/typedefs.h"
18
19namespace webrtc {
20
21class AudioDeviceBuffer;
22
23// FineAudioBuffer takes an AudioDeviceBuffer (ADB) which deals with audio data
24// corresponding to 10ms of data. It then allows for this data to be pulled in
25// a finer or coarser granularity. I.e. interacting with this class instead of
26// directly with the AudioDeviceBuffer one can ask for any number of audio data
27// samples. This class also ensures that audio data can be delivered to the ADB
28// in 10ms chunks when the size of the provided audio buffers differs from 10ms.
29// As an example: calling DeliverRecordedData() with 5ms buffers will deliver
30// accumulated 10ms worth of data to the ADB every second call.
31class FineAudioBuffer {
32 public:
33 // |device_buffer| is a buffer that provides 10ms of audio data.
34 // |desired_frame_size_bytes| is the number of bytes of audio data
35 // GetPlayoutData() should return on success. It is also the required size of
36 // each recorded buffer used in DeliverRecordedData() calls.
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
39 // of samples can be calculated.
40 FineAudioBuffer(AudioDeviceBuffer* device_buffer,
41 size_t desired_frame_size_bytes,
42 int sample_rate);
43 ~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
49 // |buffer| must be of equal or greater size than what is returned by
50 // RequiredBufferSize(). This is to avoid unnecessary memcpy.
51 void GetPlayoutData(int8_t* buffer);
52
53 // Consumes the audio data in |buffer| and sends it to the WebRTC layer in
54 // chunks of 10ms. The provided delay estimates in |playout_delay_ms| and
55 // |record_delay_ms| are given to the AEC in the audio processing module.
56 // They can be fixed values on most platforms and they are ignored if an
57 // external (hardware/built-in) AEC is used.
58 // The size of |buffer| is given by |size_in_bytes| and must be equal to
henrikab3ebc1a2017-02-27 05:14:17 -080059 // |desired_frame_size_bytes_|.
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.
63 void DeliverRecordedData(const int8_t* buffer,
64 size_t size_in_bytes,
65 int playout_delay_ms,
66 int record_delay_ms);
67
68 private:
69 // Device buffer that works with 10ms chunks of data both for playout and
70 // for recording. I.e., the WebRTC side will always be asked for audio to be
71 // played out in 10ms chunks and recorded audio will be sent to WebRTC in
72 // 10ms chunks as well. This pointer is owned by the constructor of this
73 // class and the owner must ensure that the pointer is valid during the life-
74 // time of this object.
75 AudioDeviceBuffer* const device_buffer_;
76 // Number of bytes delivered by GetPlayoutData() call and provided to
77 // DeliverRecordedData().
78 const size_t desired_frame_size_bytes_;
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_;
83 // Number of audio bytes per 10ms.
84 const size_t bytes_per_10_ms_;
henrikab3ebc1a2017-02-27 05:14:17 -080085 rtc::BufferT<int8_t> playout_buffer_;
henrika86d907c2015-09-07 16:09:50 +020086 // 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.
henrikaf166e1b2017-02-23 02:44:55 -080088 rtc::BufferT<int8_t> record_buffer_;
henrika86d907c2015-09-07 16:09:50 +020089};
90
91} // namespace webrtc
92
93#endif // WEBRTC_MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_