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