• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #ifndef MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_
12 #define MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_
13 
14 #include "api/array_view.h"
15 #include "rtc_base/buffer.h"
16 
17 namespace webrtc {
18 
19 class AudioDeviceBuffer;
20 
21 // FineAudioBuffer takes an AudioDeviceBuffer (ADB) which deals with 16-bit PCM
22 // audio samples corresponding to 10ms of data. It then allows for this data
23 // to be pulled in a finer or coarser granularity. I.e. interacting with this
24 // class instead of directly with the AudioDeviceBuffer one can ask for any
25 // number of audio data samples. This class also ensures that audio data can be
26 // delivered to the ADB in 10ms chunks when the size of the provided audio
27 // buffers differs from 10ms.
28 // As an example: calling DeliverRecordedData() with 5ms buffers will deliver
29 // accumulated 10ms worth of data to the ADB every second call.
30 class FineAudioBuffer {
31  public:
32   // |device_buffer| is a buffer that provides 10ms of audio data.
33   FineAudioBuffer(AudioDeviceBuffer* audio_device_buffer);
34   ~FineAudioBuffer();
35 
36   // Clears buffers and counters dealing with playout and/or recording.
37   void ResetPlayout();
38   void ResetRecord();
39 
40   // Utility methods which returns true if valid parameters are acquired at
41   // constructions.
42   bool IsReadyForPlayout() const;
43   bool IsReadyForRecord() const;
44 
45   // Copies audio samples into |audio_buffer| where number of requested
46   // elements is specified by |audio_buffer.size()|. The producer will always
47   // fill up the audio buffer and if no audio exists, the buffer will contain
48   // silence instead. The provided delay estimate in |playout_delay_ms| should
49   // contain an estimate of the latency between when an audio frame is read from
50   // WebRTC and when it is played out on the speaker.
51   void GetPlayoutData(rtc::ArrayView<int16_t> audio_buffer,
52                       int playout_delay_ms);
53 
54   // Consumes the audio data in |audio_buffer| and sends it to the WebRTC layer
55   // in chunks of 10ms. The sum of the provided delay estimate in
56   // |record_delay_ms| and the latest |playout_delay_ms| in GetPlayoutData()
57   // 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.
60   // 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 internal
62   // cache. Call #3 restarts the scheme above.
63   void DeliverRecordedData(rtc::ArrayView<const int16_t> audio_buffer,
64                            int record_delay_ms);
65 
66  private:
67   // Device buffer that works with 10ms chunks of data both for playout and
68   // for recording. I.e., the WebRTC side will always be asked for audio to be
69   // played out in 10ms chunks and recorded audio will be sent to WebRTC in
70   // 10ms chunks as well. This raw pointer is owned by the constructor of this
71   // class and the owner must ensure that the pointer is valid during the life-
72   // time of this object.
73   AudioDeviceBuffer* const audio_device_buffer_;
74   // Number of audio samples per channel per 10ms. Set once at construction
75   // based on parameters in |audio_device_buffer|.
76   const size_t playout_samples_per_channel_10ms_;
77   const size_t record_samples_per_channel_10ms_;
78   // Number of audio channels. Set once at construction based on parameters in
79   // |audio_device_buffer|.
80   const size_t playout_channels_;
81   const size_t record_channels_;
82   // Storage for output samples from which a consumer can read audio buffers
83   // in any size using GetPlayoutData().
84   rtc::BufferT<int16_t> playout_buffer_;
85   // Storage for input samples that are about to be delivered to the WebRTC
86   // ADB or remains from the last successful delivery of a 10ms audio buffer.
87   rtc::BufferT<int16_t> record_buffer_;
88   // Contains latest delay estimate given to GetPlayoutData().
89   int playout_delay_ms_ = 0;
90 };
91 
92 }  // namespace webrtc
93 
94 #endif  // MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_
95