AudioStreamInternal.h revision ec89b2e2f8c84a7d3936db1a888034f4a4b0df16
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 <media/PlayerBase.h> 22#include <aaudio/AAudio.h> 23 24#include "binding/IAAudioService.h" 25#include "binding/AudioEndpointParcelable.h" 26#include "binding/AAudioServiceInterface.h" 27#include "client/IsochronousClockModel.h" 28#include "client/AudioEndpoint.h" 29#include "core/AudioStream.h" 30#include "utility/LinearRamp.h" 31 32using android::sp; 33using android::IAAudioService; 34 35namespace aaudio { 36 37// A stream that talks to the AAudioService or directly to a HAL. 38class AudioStreamInternal : public AudioStream, public android::PlayerBase { 39 40public: 41 AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService); 42 virtual ~AudioStreamInternal(); 43 44 // =========== Begin ABSTRACT methods =========================== 45 aaudio_result_t requestStart() override; 46 47 aaudio_result_t requestPause() override; 48 49 aaudio_result_t requestFlush() 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 updateStateWhileWaiting() override; 58 // =========== End ABSTRACT methods =========================== 59 60 aaudio_result_t open(const AudioStreamBuilder &builder) override; 61 62 aaudio_result_t close() override; 63 64 aaudio_result_t setBufferSize(int32_t requestedFrames) override; 65 66 int32_t getBufferSize() const override; 67 68 int32_t getBufferCapacity() const override; 69 70 int32_t getFramesPerBurst() const override; 71 72 int32_t getXRunCount() const override { 73 return mXRunCount; 74 } 75 76 aaudio_result_t registerThread() override; 77 78 aaudio_result_t unregisterThread() override; 79 80 aaudio_result_t joinThread(void** returnArg); 81 82 // Called internally from 'C' 83 virtual void *callbackLoop() = 0; 84 85 86 bool isMMap() override { 87 return true; 88 } 89 90 // Calculate timeout based on framesPerBurst 91 int64_t calculateReasonableTimeout(); 92 93 //PlayerBase virtuals 94 virtual void destroy(); 95 96protected: 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 processCommands(); 115 116 aaudio_result_t requestPauseInternal(); 117 aaudio_result_t requestStopInternal(); 118 119 aaudio_result_t stopCallback(); 120 121 122 void onFlushFromServer(); 123 124 aaudio_result_t onEventFromServer(AAudioServiceMessage *message); 125 126 aaudio_result_t onTimestampFromServer(AAudioServiceMessage *message); 127 128 void logTimestamp(AAudioServiceMessage &message); 129 130 // Calculate timeout for an operation involving framesPerOperation. 131 int64_t calculateReasonableTimeout(int32_t framesPerOperation); 132 133 void doSetVolume(); 134 135 //PlayerBase virtuals 136 virtual status_t playerStart(); 137 virtual status_t playerPause(); 138 virtual status_t playerStop(); 139 virtual status_t playerSetVolume(); 140 141 aaudio_format_t mDeviceFormat = AAUDIO_FORMAT_UNSPECIFIED; 142 143 IsochronousClockModel mClockModel; // timing model for chasing the HAL 144 145 AudioEndpoint mAudioEndpoint; // source for reads or sink for writes 146 aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service 147 148 int32_t mFramesPerBurst; // frames per HAL transfer 149 int32_t mXRunCount = 0; // how many underrun events? 150 151 LinearRamp mVolumeRamp; 152 float mStreamVolume; 153 154 // Offset from underlying frame position. 155 int64_t mFramesOffsetFromService = 0; // offset for timestamps 156 157 uint8_t *mCallbackBuffer = nullptr; 158 int32_t mCallbackFrames = 0; 159 160 // The service uses this for SHARED mode. 161 bool mInService = false; // Is this running in the client or the service? 162 163private: 164 /* 165 * Asynchronous write with data conversion. 166 * @param buffer 167 * @param numFrames 168 * @return fdrames written or negative error 169 */ 170 aaudio_result_t writeNowWithConversion(const void *buffer, 171 int32_t numFrames); 172 173 // Adjust timing model based on timestamp from service. 174 void processTimestamp(uint64_t position, int64_t time); 175 176 AudioEndpointParcelable mEndPointParcelable; // description of the buffers filled by service 177 EndpointDescriptor mEndpointDescriptor; // buffer description with resolved addresses 178 AAudioServiceInterface &mServiceInterface; // abstract interface to the service 179 180}; 181 182} /* namespace aaudio */ 183 184#endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H 185