1 /* 2 * Copyright (C) 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 ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H 18 #define ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H 19 20 #include <stdint.h> 21 #include <aaudio/AAudio.h> 22 23 #include "binding/IAAudioService.h" 24 #include "binding/AudioEndpointParcelable.h" 25 #include "binding/AAudioServiceInterface.h" 26 #include "client/IsochronousClockModel.h" 27 #include "client/AudioEndpoint.h" 28 #include "core/AudioStream.h" 29 #include "utility/AudioClock.h" 30 31 using android::sp; 32 using android::IAAudioService; 33 34 namespace aaudio { 35 36 // These are intended to be outside the range of what is normally encountered. 37 // TODO MAXes should probably be much bigger. 38 constexpr int32_t MIN_FRAMES_PER_BURST = 16; // arbitrary 39 constexpr int32_t MAX_FRAMES_PER_BURST = 16 * 1024; // arbitrary 40 constexpr int32_t MAX_BUFFER_CAPACITY_IN_FRAMES = 32 * 1024; // arbitrary 41 42 // A stream that talks to the AAudioService or directly to a HAL. 43 class AudioStreamInternal : public AudioStream { 44 45 public: 46 AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService); 47 virtual ~AudioStreamInternal(); 48 49 aaudio_result_t requestStart() override; 50 51 aaudio_result_t requestStop() override; 52 53 aaudio_result_t getTimestamp(clockid_t clockId, 54 int64_t *framePosition, 55 int64_t *timeNanoseconds) override; 56 57 virtual aaudio_result_t updateStateMachine() override; 58 59 aaudio_result_t open(const AudioStreamBuilder &builder) override; 60 61 aaudio_result_t close() override; 62 63 aaudio_result_t setBufferSize(int32_t requestedFrames) override; 64 65 int32_t getBufferSize() const override; 66 67 int32_t getBufferCapacity() const override; 68 69 int32_t getFramesPerBurst() const override; 70 getXRunCount()71 int32_t getXRunCount() const override { 72 return mXRunCount; 73 } 74 75 aaudio_result_t registerThread() override; 76 77 aaudio_result_t unregisterThread() override; 78 79 aaudio_result_t joinThread(void** returnArg); 80 81 // Called internally from 'C' 82 virtual void *callbackLoop() = 0; 83 84 isMMap()85 bool isMMap() override { 86 return true; 87 } 88 89 // Calculate timeout based on framesPerBurst 90 int64_t calculateReasonableTimeout(); 91 92 aaudio_result_t startClient(const android::AudioClient& client, 93 audio_port_handle_t *clientHandle); 94 95 aaudio_result_t stopClient(audio_port_handle_t clientHandle); 96 getServiceHandle()97 aaudio_handle_t getServiceHandle() const { 98 return mServiceStreamHandle; 99 } 100 101 protected: 102 103 aaudio_result_t processData(void *buffer, 104 int32_t numFrames, 105 int64_t timeoutNanoseconds); 106 107 /** 108 * Low level data processing that will not block. It will just read or write as much as it can. 109 * 110 * It passed back a recommended time to wake up if wakeTimePtr is not NULL. 111 * 112 * @return the number of frames processed or a negative error code. 113 */ 114 virtual aaudio_result_t processDataNow(void *buffer, 115 int32_t numFrames, 116 int64_t currentTimeNanos, 117 int64_t *wakeTimePtr) = 0; 118 119 aaudio_result_t drainTimestampsFromService(); 120 121 aaudio_result_t processCommands(); 122 123 aaudio_result_t stopCallback(); 124 125 virtual void advanceClientToMatchServerPosition() = 0; 126 onFlushFromServer()127 virtual void onFlushFromServer() {} 128 129 aaudio_result_t onEventFromServer(AAudioServiceMessage *message); 130 131 aaudio_result_t onTimestampService(AAudioServiceMessage *message); 132 133 aaudio_result_t onTimestampHardware(AAudioServiceMessage *message); 134 135 void logTimestamp(AAudioServiceMessage &message); 136 137 // Calculate timeout for an operation involving framesPerOperation. 138 int64_t calculateReasonableTimeout(int32_t framesPerOperation); 139 getDeviceChannelCount()140 int32_t getDeviceChannelCount() const { return mDeviceChannelCount; } 141 142 /** 143 * @return true if running in audio service, versus in app process 144 */ isInService()145 bool isInService() const { return mInService; } 146 147 /** 148 * Is the service FIFO position currently controlled by the AAudio service or HAL, 149 * or set based on the Clock Model. 150 * 151 * @return true if the ClockModel is currently determining the FIFO position 152 */ 153 bool isClockModelInControl() const; 154 155 IsochronousClockModel mClockModel; // timing model for chasing the HAL 156 157 AudioEndpoint mAudioEndpoint; // source for reads or sink for writes 158 aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service 159 160 int32_t mFramesPerBurst = MIN_FRAMES_PER_BURST; // frames per HAL transfer 161 int32_t mXRunCount = 0; // how many underrun events? 162 163 // Offset from underlying frame position. 164 int64_t mFramesOffsetFromService = 0; // offset for timestamps 165 166 uint8_t *mCallbackBuffer = nullptr; 167 int32_t mCallbackFrames = 0; 168 169 // The service uses this for SHARED mode. 170 bool mInService = false; // Is this running in the client or the service? 171 172 AAudioServiceInterface &mServiceInterface; // abstract interface to the service 173 174 SimpleDoubleBuffer<Timestamp> mAtomicInternalTimestamp; 175 176 AtomicRequestor mNeedCatchUp; // Ask read() or write() to sync on first timestamp. 177 178 float mStreamVolume = 1.0f; 179 180 private: 181 /* 182 * Asynchronous write with data conversion. 183 * @param buffer 184 * @param numFrames 185 * @return fdrames written or negative error 186 */ 187 aaudio_result_t writeNowWithConversion(const void *buffer, 188 int32_t numFrames); 189 190 // Adjust timing model based on timestamp from service. 191 void processTimestamp(uint64_t position, int64_t time); 192 193 // Thread on other side of FIFO will have wakeup jitter. 194 // By delaying slightly we can avoid waking up before other side is ready. 195 const int32_t mWakeupDelayNanos; // delay past typical wakeup jitter 196 const int32_t mMinimumSleepNanos; // minimum sleep while polling 197 198 AudioEndpointParcelable mEndPointParcelable; // description of the buffers filled by service 199 EndpointDescriptor mEndpointDescriptor; // buffer description with resolved addresses 200 201 int64_t mServiceLatencyNanos = 0; 202 203 // Sometimes the hardware is operating with a different channel count from the app. 204 // Then we require conversion in AAudio. 205 int32_t mDeviceChannelCount = 0; 206 }; 207 208 } /* namespace aaudio */ 209 210 #endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H 211