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/LinearRamp.h" 30 31 using android::sp; 32 using android::IAAudioService; 33 34 namespace aaudio { 35 36 // A stream that talks to the AAudioService or directly to a HAL. 37 class AudioStreamInternal : public AudioStream { 38 39 public: 40 AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService); 41 virtual ~AudioStreamInternal(); 42 43 // =========== Begin ABSTRACT methods =========================== 44 aaudio_result_t requestStart() override; 45 46 aaudio_result_t requestPause() override; 47 48 aaudio_result_t requestFlush() override; 49 50 aaudio_result_t requestStop() override; 51 52 aaudio_result_t getTimestamp(clockid_t clockId, 53 int64_t *framePosition, 54 int64_t *timeNanoseconds) override; 55 56 virtual aaudio_result_t updateStateWhileWaiting() override; 57 // =========== End ABSTRACT methods =========================== 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 protected: 93 94 aaudio_result_t processData(void *buffer, 95 int32_t numFrames, 96 int64_t timeoutNanoseconds); 97 98 /** 99 * Low level data processing that will not block. It will just read or write as much as it can. 100 * 101 * It passed back a recommended time to wake up if wakeTimePtr is not NULL. 102 * 103 * @return the number of frames processed or a negative error code. 104 */ 105 virtual aaudio_result_t processDataNow(void *buffer, 106 int32_t numFrames, 107 int64_t currentTimeNanos, 108 int64_t *wakeTimePtr) = 0; 109 110 aaudio_result_t processCommands(); 111 112 aaudio_result_t requestPauseInternal(); 113 aaudio_result_t requestStopInternal(); 114 115 aaudio_result_t stopCallback(); 116 117 118 void onFlushFromServer(); 119 120 aaudio_result_t onEventFromServer(AAudioServiceMessage *message); 121 122 aaudio_result_t onTimestampFromServer(AAudioServiceMessage *message); 123 124 // Calculate timeout for an operation involving framesPerOperation. 125 int64_t calculateReasonableTimeout(int32_t framesPerOperation); 126 127 aaudio_format_t mDeviceFormat = AAUDIO_FORMAT_UNSPECIFIED; 128 129 IsochronousClockModel mClockModel; // timing model for chasing the HAL 130 131 AudioEndpoint mAudioEndpoint; // source for reads or sink for writes 132 aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service 133 134 int32_t mFramesPerBurst; // frames per HAL transfer 135 int32_t mXRunCount = 0; // how many underrun events? 136 137 LinearRamp mVolumeRamp; 138 139 // Offset from underlying frame position. 140 int64_t mFramesOffsetFromService = 0; // offset for timestamps 141 142 uint8_t *mCallbackBuffer = nullptr; 143 int32_t mCallbackFrames = 0; 144 145 private: 146 /* 147 * Asynchronous write with data conversion. 148 * @param buffer 149 * @param numFrames 150 * @return fdrames written or negative error 151 */ 152 aaudio_result_t writeNowWithConversion(const void *buffer, 153 int32_t numFrames); 154 155 // Adjust timing model based on timestamp from service. 156 void processTimestamp(uint64_t position, int64_t time); 157 158 AudioEndpointParcelable mEndPointParcelable; // description of the buffers filled by service 159 EndpointDescriptor mEndpointDescriptor; // buffer description with resolved addresses 160 AAudioServiceInterface &mServiceInterface; // abstract interface to the service 161 162 // The service uses this for SHARED mode. 163 bool mInService = false; // Is this running in the client or the service? 164 }; 165 166 } /* namespace aaudio */ 167 168 #endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H 169