1 /* 2 * Copyright 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef OBOE_STREAM_AAUDIO_H_ 18 #define OBOE_STREAM_AAUDIO_H_ 19 20 #include <atomic> 21 #include <shared_mutex> 22 #include <mutex> 23 #include <thread> 24 25 #include "oboe/AudioStreamBuilder.h" 26 #include "oboe/AudioStream.h" 27 #include "oboe/Definitions.h" 28 #include "AAudioLoader.h" 29 30 namespace oboe { 31 32 /** 33 * Implementation of OboeStream that uses AAudio. 34 * 35 * Do not create this class directly. 36 * Use an OboeStreamBuilder to create one. 37 */ 38 class AudioStreamAAudio : public AudioStream { 39 public: 40 AudioStreamAAudio(); 41 explicit AudioStreamAAudio(const AudioStreamBuilder &builder); 42 43 virtual ~AudioStreamAAudio() = default; 44 45 /** 46 * 47 * @return true if AAudio is supported on this device. 48 */ 49 static bool isSupported(); 50 51 // These functions override methods in AudioStream. 52 // See AudioStream for documentation. 53 Result open() override; 54 Result close() override; 55 56 Result requestStart() override; 57 Result requestPause() override; 58 Result requestFlush() override; 59 Result requestStop() override; 60 61 ResultWithValue<int32_t> write(const void *buffer, 62 int32_t numFrames, 63 int64_t timeoutNanoseconds) override; 64 65 ResultWithValue<int32_t> read(void *buffer, 66 int32_t numFrames, 67 int64_t timeoutNanoseconds) override; 68 69 ResultWithValue<int32_t> setBufferSizeInFrames(int32_t requestedFrames) override; 70 int32_t getBufferSizeInFrames() override; 71 ResultWithValue<int32_t> getXRunCount() override; isXRunCountSupported()72 bool isXRunCountSupported() const override { return true; } 73 74 ResultWithValue<double> calculateLatencyMillis() override; 75 76 Result waitForStateChange(StreamState currentState, 77 StreamState *nextState, 78 int64_t timeoutNanoseconds) override; 79 80 Result getTimestamp(clockid_t clockId, 81 int64_t *framePosition, 82 int64_t *timeNanoseconds) override; 83 84 StreamState getState() override; 85 getAudioApi()86 AudioApi getAudioApi() const override { 87 return AudioApi::AAudio; 88 } 89 90 DataCallbackResult callOnAudioReady(AAudioStream *stream, 91 void *audioData, 92 int32_t numFrames); 93 94 bool isMMapUsed(); 95 96 protected: 97 static void internalErrorCallback( 98 AAudioStream *stream, 99 void *userData, 100 aaudio_result_t error); 101 getUnderlyingStream()102 void *getUnderlyingStream() const override { 103 return mAAudioStream.load(); 104 } 105 106 void updateFramesRead() override; 107 void updateFramesWritten() override; 108 109 void logUnsupportedAttributes(); 110 111 private: 112 // Must call under mLock. And stream must NOT be nullptr. 113 Result requestStop_l(AAudioStream *stream); 114 115 /** 116 * Launch a thread that will stop the stream. 117 */ 118 void launchStopThread(); 119 120 public: 121 int32_t getMDelayBeforeCloseMillis() const; 122 123 void setDelayBeforeCloseMillis(int32_t mDelayBeforeCloseMillis); 124 125 private: 126 127 std::atomic<bool> mCallbackThreadEnabled; 128 std::atomic<bool> mStopThreadAllowed{false}; 129 130 // pointer to the underlying 'C' AAudio stream, valid if open, null if closed 131 std::atomic<AAudioStream *> mAAudioStream{nullptr}; 132 std::shared_mutex mAAudioStreamLock; // to protect mAAudioStream while closing 133 134 static AAudioLoader *mLibLoader; 135 136 // We may not use this but it is so small that it is not worth allocating dynamically. 137 AudioStreamErrorCallback mDefaultErrorCallback; 138 }; 139 140 } // namespace oboe 141 142 #endif // OBOE_STREAM_AAUDIO_H_ 143