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