tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

fine_audio_buffer.h (4671B)


      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 <cstddef>
     15 #include <cstdint>
     16 #include <optional>
     17 
     18 #include "api/array_view.h"
     19 #include "rtc_base/buffer.h"
     20 
     21 namespace webrtc {
     22 
     23 class AudioDeviceBuffer;
     24 
     25 // FineAudioBuffer takes an AudioDeviceBuffer (ADB) which deals with 16-bit PCM
     26 // audio samples corresponding to 10ms of data. It then allows for this data
     27 // to be pulled in a finer or coarser granularity. I.e. interacting with this
     28 // class instead of directly with the AudioDeviceBuffer one can ask for any
     29 // number of audio data samples. This class also ensures that audio data can be
     30 // delivered to the ADB in 10ms chunks when the size of the provided audio
     31 // buffers differs from 10ms.
     32 // As an example: calling DeliverRecordedData() with 5ms buffers will deliver
     33 // accumulated 10ms worth of data to the ADB every second call.
     34 class FineAudioBuffer {
     35 public:
     36  // `device_buffer` is a buffer that provides 10ms of audio data.
     37  FineAudioBuffer(AudioDeviceBuffer* audio_device_buffer);
     38  ~FineAudioBuffer();
     39 
     40  // Clears buffers and counters dealing with playout and/or recording.
     41  void ResetPlayout();
     42  void ResetRecord();
     43 
     44  // Utility methods which returns true if valid parameters are acquired at
     45  // constructions.
     46  bool IsReadyForPlayout() const;
     47  bool IsReadyForRecord() const;
     48 
     49  // 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. The provided delay estimate in `playout_delay_ms` should
     53  // contain an estimate 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(ArrayView<int16_t> audio_buffer, int playout_delay_ms);
     56 
     57  // Consumes the audio data in `audio_buffer` and sends it to the WebRTC layer
     58  // in chunks of 10ms. The sum of the provided delay estimate in
     59  // `record_delay_ms` and the latest `playout_delay_ms` in GetPlayoutData()
     60  // are given to the AEC in the audio processing module.
     61  // They can be fixed values on most platforms and they are ignored if an
     62  // external (hardware/built-in) AEC is used.
     63  // Example: buffer size is 5ms => call #1 stores 5ms of data, call #2 stores
     64  // 5ms of data and sends a total of 10ms to WebRTC and clears the internal
     65  // cache. Call #3 restarts the scheme above.
     66  void DeliverRecordedData(ArrayView<const int16_t> audio_buffer,
     67                           int record_delay_ms) {
     68    DeliverRecordedData(audio_buffer, record_delay_ms, std::nullopt);
     69  }
     70  void DeliverRecordedData(ArrayView<const int16_t> audio_buffer,
     71                           int record_delay_ms,
     72                           std::optional<int64_t> capture_time_ns);
     73 
     74 private:
     75  // Device buffer that works with 10ms chunks of data both for playout and
     76  // for recording. I.e., the WebRTC side will always be asked for audio to be
     77  // played out in 10ms chunks and recorded audio will be sent to WebRTC in
     78  // 10ms chunks as well. This raw pointer is owned by the constructor of this
     79  // class and the owner must ensure that the pointer is valid during the life-
     80  // time of this object.
     81  AudioDeviceBuffer* const audio_device_buffer_;
     82  // Number of audio samples per channel per 10ms. Set once at construction
     83  // based on parameters in `audio_device_buffer`.
     84  const size_t playout_samples_per_channel_10ms_;
     85  const size_t record_samples_per_channel_10ms_;
     86  // Number of audio channels. Set once at construction based on parameters in
     87  // `audio_device_buffer`.
     88  const size_t playout_channels_;
     89  const size_t record_channels_;
     90  // Storage for output samples from which a consumer can read audio buffers
     91  // in any size using GetPlayoutData().
     92  BufferT<int16_t> playout_buffer_;
     93  // Storage for input samples that are about to be delivered to the WebRTC
     94  // ADB or remains from the last successful delivery of a 10ms audio buffer.
     95  BufferT<int16_t> record_buffer_;
     96  // Contains latest delay estimate given to GetPlayoutData().
     97  int playout_delay_ms_ = 0;
     98 };
     99 
    100 }  // namespace webrtc
    101 
    102 #endif  // MODULES_AUDIO_DEVICE_FINE_AUDIO_BUFFER_H_