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