fine_audio_buffer.h 4.3 KB

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