aaudio_player.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_PLAYER_H_
  11. #define MODULES_AUDIO_DEVICE_ANDROID_AAUDIO_PLAYER_H_
  12. #include <aaudio/AAudio.h>
  13. #include <memory>
  14. #include "modules/audio_device/android/aaudio_wrapper.h"
  15. #include "modules/audio_device/include/audio_device_defines.h"
  16. #include "rtc_base/message_handler.h"
  17. #include "rtc_base/thread.h"
  18. #include "rtc_base/thread_annotations.h"
  19. #include "rtc_base/thread_checker.h"
  20. namespace webrtc {
  21. class AudioDeviceBuffer;
  22. class FineAudioBuffer;
  23. class AudioManager;
  24. // Implements low-latency 16-bit mono PCM audio output support for Android
  25. // using the C based AAudio API.
  26. //
  27. // An instance must be created and destroyed on one and the same thread.
  28. // All public methods must also be called on the same thread. A thread checker
  29. // will DCHECK if any method is called on an invalid thread. Audio buffers
  30. // are requested on a dedicated high-priority thread owned by AAudio.
  31. //
  32. // The existing design forces the user to call InitPlayout() after StopPlayout()
  33. // to be able to call StartPlayout() again. This is in line with how the Java-
  34. // based implementation works.
  35. //
  36. // An audio stream can be disconnected, e.g. when an audio device is removed.
  37. // This implementation will restart the audio stream using the new preferred
  38. // device if such an event happens.
  39. //
  40. // Also supports automatic buffer-size adjustment based on underrun detections
  41. // where the internal AAudio buffer can be increased when needed. It will
  42. // reduce the risk of underruns (~glitches) at the expense of an increased
  43. // latency.
  44. class AAudioPlayer final : public AAudioObserverInterface,
  45. public rtc::MessageHandler {
  46. public:
  47. explicit AAudioPlayer(AudioManager* audio_manager);
  48. ~AAudioPlayer();
  49. int Init();
  50. int Terminate();
  51. int InitPlayout();
  52. bool PlayoutIsInitialized() const;
  53. int StartPlayout();
  54. int StopPlayout();
  55. bool Playing() const;
  56. void AttachAudioBuffer(AudioDeviceBuffer* audioBuffer);
  57. // Not implemented in AAudio.
  58. int SpeakerVolumeIsAvailable(bool& available); // NOLINT
  59. int SetSpeakerVolume(uint32_t volume) { return -1; }
  60. int SpeakerVolume(uint32_t& volume) const { return -1; } // NOLINT
  61. int MaxSpeakerVolume(uint32_t& maxVolume) const { return -1; } // NOLINT
  62. int MinSpeakerVolume(uint32_t& minVolume) const { return -1; } // NOLINT
  63. protected:
  64. // AAudioObserverInterface implementation.
  65. // For an output stream, this function should render and write |num_frames|
  66. // of data in the streams current data format to the |audio_data| buffer.
  67. // Called on a real-time thread owned by AAudio.
  68. aaudio_data_callback_result_t OnDataCallback(void* audio_data,
  69. int32_t num_frames) override;
  70. // AAudio calls this functions if any error occurs on a callback thread.
  71. // Called on a real-time thread owned by AAudio.
  72. void OnErrorCallback(aaudio_result_t error) override;
  73. // rtc::MessageHandler used for restart messages from the error-callback
  74. // thread to the main (creating) thread.
  75. void OnMessage(rtc::Message* msg) override;
  76. private:
  77. // Closes the existing stream and starts a new stream.
  78. void HandleStreamDisconnected();
  79. // Ensures that methods are called from the same thread as this object is
  80. // created on.
  81. rtc::ThreadChecker main_thread_checker_;
  82. // Stores thread ID in first call to AAudioPlayer::OnDataCallback from a
  83. // real-time thread owned by AAudio. Detached during construction of this
  84. // object.
  85. rtc::ThreadChecker thread_checker_aaudio_;
  86. // The thread on which this object is created on.
  87. rtc::Thread* main_thread_;
  88. // Wraps all AAudio resources. Contains an output stream using the default
  89. // output audio device. Can be accessed on both the main thread and the
  90. // real-time thread owned by AAudio. See separate AAudio documentation about
  91. // thread safety.
  92. AAudioWrapper aaudio_;
  93. // FineAudioBuffer takes an AudioDeviceBuffer which delivers audio data
  94. // in chunks of 10ms. It then allows for this data to be pulled in
  95. // a finer or coarser granularity. I.e. interacting with this class instead
  96. // of directly with the AudioDeviceBuffer one can ask for any number of
  97. // audio data samples.
  98. // Example: native buffer size can be 192 audio frames at 48kHz sample rate.
  99. // WebRTC will provide 480 audio frames per 10ms but AAudio asks for 192
  100. // in each callback (once every 4th ms). This class can then ask for 192 and
  101. // the FineAudioBuffer will ask WebRTC for new data approximately only every
  102. // second callback and also cache non-utilized audio.
  103. std::unique_ptr<FineAudioBuffer> fine_audio_buffer_;
  104. // Counts number of detected underrun events reported by AAudio.
  105. int32_t underrun_count_ = 0;
  106. // True only for the first data callback in each audio session.
  107. bool first_data_callback_ = true;
  108. // Raw pointer handle provided to us in AttachAudioBuffer(). Owned by the
  109. // AudioDeviceModuleImpl class and set by AudioDeviceModule::Create().
  110. AudioDeviceBuffer* audio_device_buffer_ RTC_GUARDED_BY(main_thread_checker_) =
  111. nullptr;
  112. bool initialized_ RTC_GUARDED_BY(main_thread_checker_) = false;
  113. bool playing_ RTC_GUARDED_BY(main_thread_checker_) = false;
  114. // Estimated latency between writing an audio frame to the output stream and
  115. // the time that same frame is played out on the output audio device.
  116. double latency_millis_ RTC_GUARDED_BY(thread_checker_aaudio_) = 0;
  117. };
  118. } // namespace webrtc
  119. #endif // MODULES_AUDIO_DEVICE_ANDROID_AAUDIO_PLAYER_H_