aaudio_wrapper.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2018 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_ANDROID_AAUDIO_WRAPPER_H_
  11. #define MODULES_AUDIO_DEVICE_ANDROID_AAUDIO_WRAPPER_H_
  12. #include <aaudio/AAudio.h>
  13. #include "modules/audio_device/include/audio_device_defines.h"
  14. #include "rtc_base/thread_checker.h"
  15. namespace webrtc {
  16. class AudioManager;
  17. // AAudio callback interface for audio transport to/from the AAudio stream.
  18. // The interface also contains an error callback method for notifications of
  19. // e.g. device changes.
  20. class AAudioObserverInterface {
  21. public:
  22. // Audio data will be passed in our out of this function dependning on the
  23. // direction of the audio stream. This callback function will be called on a
  24. // real-time thread owned by AAudio.
  25. virtual aaudio_data_callback_result_t OnDataCallback(void* audio_data,
  26. int32_t num_frames) = 0;
  27. // AAudio will call this functions if any error occurs on a callback thread.
  28. // In response, this function could signal or launch another thread to reopen
  29. // a stream on another device. Do not reopen the stream in this callback.
  30. virtual void OnErrorCallback(aaudio_result_t error) = 0;
  31. protected:
  32. virtual ~AAudioObserverInterface() {}
  33. };
  34. // Utility class which wraps the C-based AAudio API into a more handy C++ class
  35. // where the underlying resources (AAudioStreamBuilder and AAudioStream) are
  36. // encapsulated. User must set the direction (in or out) at construction since
  37. // it defines the stream type and the direction of the data flow in the
  38. // AAudioObserverInterface.
  39. //
  40. // AAudio is a new Android C API introduced in the Android O (26) release.
  41. // It is designed for high-performance audio applications that require low
  42. // latency. Applications communicate with AAudio by reading and writing data
  43. // to streams.
  44. //
  45. // Each stream is attached to a single audio device, where each audio device
  46. // has a unique ID. The ID can be used to bind an audio stream to a specific
  47. // audio device but this implementation lets AAudio choose the default primary
  48. // device instead (device selection takes place in Java). A stream can only
  49. // move data in one direction. When a stream is opened, Android checks to
  50. // ensure that the audio device and stream direction agree.
  51. class AAudioWrapper {
  52. public:
  53. AAudioWrapper(AudioManager* audio_manager,
  54. aaudio_direction_t direction,
  55. AAudioObserverInterface* observer);
  56. ~AAudioWrapper();
  57. bool Init();
  58. bool Start();
  59. bool Stop();
  60. // For output streams: estimates latency between writing an audio frame to
  61. // the output stream and the time that same frame is played out on the output
  62. // audio device.
  63. // For input streams: estimates latency between reading an audio frame from
  64. // the input stream and the time that same frame was recorded on the input
  65. // audio device.
  66. double EstimateLatencyMillis() const;
  67. // Increases the internal buffer size for output streams by one burst size to
  68. // reduce the risk of underruns. Can be used while a stream is active.
  69. bool IncreaseOutputBufferSize();
  70. // Drains the recording stream of any existing data by reading from it until
  71. // it's empty. Can be used to clear out old data before starting a new audio
  72. // session.
  73. void ClearInputStream(void* audio_data, int32_t num_frames);
  74. AAudioObserverInterface* observer() const;
  75. AudioParameters audio_parameters() const;
  76. int32_t samples_per_frame() const;
  77. int32_t buffer_size_in_frames() const;
  78. int32_t buffer_capacity_in_frames() const;
  79. int32_t device_id() const;
  80. int32_t xrun_count() const;
  81. int32_t format() const;
  82. int32_t sample_rate() const;
  83. int32_t channel_count() const;
  84. int32_t frames_per_callback() const;
  85. aaudio_sharing_mode_t sharing_mode() const;
  86. aaudio_performance_mode_t performance_mode() const;
  87. aaudio_stream_state_t stream_state() const;
  88. int64_t frames_written() const;
  89. int64_t frames_read() const;
  90. aaudio_direction_t direction() const { return direction_; }
  91. AAudioStream* stream() const { return stream_; }
  92. int32_t frames_per_burst() const { return frames_per_burst_; }
  93. private:
  94. void SetStreamConfiguration(AAudioStreamBuilder* builder);
  95. bool OpenStream(AAudioStreamBuilder* builder);
  96. void CloseStream();
  97. void LogStreamConfiguration();
  98. void LogStreamState();
  99. bool VerifyStreamConfiguration();
  100. bool OptimizeBuffers();
  101. rtc::ThreadChecker thread_checker_;
  102. rtc::ThreadChecker aaudio_thread_checker_;
  103. AudioParameters audio_parameters_;
  104. const aaudio_direction_t direction_;
  105. AAudioObserverInterface* observer_ = nullptr;
  106. AAudioStream* stream_ = nullptr;
  107. int32_t frames_per_burst_ = 0;
  108. };
  109. } // namespace webrtc
  110. #endif // MODULES_AUDIO_DEVICE_ANDROID_AAUDIO_WRAPPER_H_