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