1 /* 2 * Copyright (C) 2017 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 AAUDIO_SERVICE_ENDPOINT_H 18 #define AAUDIO_SERVICE_ENDPOINT_H 19 20 #include <atomic> 21 #include <functional> 22 #include <mutex> 23 #include <vector> 24 25 #include <android-base/thread_annotations.h> 26 27 #include "client/AudioStreamInternal.h" 28 #include "client/AudioStreamInternalPlay.h" 29 #include "core/AAudioStreamParameters.h" 30 #include "binding/AAudioServiceMessage.h" 31 #include "binding/AAudioStreamConfiguration.h" 32 33 #include "AAudioServiceStreamBase.h" 34 35 namespace aaudio { 36 37 /** 38 * AAudioServiceEndpoint is used by a subclass of AAudioServiceStreamBase 39 * to communicate with the underlying audio device or port. 40 */ 41 class AAudioServiceEndpoint 42 : public virtual android::RefBase 43 , public AAudioStreamParameters { 44 public: 45 46 virtual ~AAudioServiceEndpoint(); 47 48 virtual std::string dump() const; 49 50 virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request) = 0; 51 52 /* 53 * Perform any cleanup necessary before deleting the stream. 54 * This might include releasing and closing internal streams. 55 */ 56 virtual void close() = 0; 57 58 aaudio_result_t registerStream(android::sp<AAudioServiceStreamBase> stream); 59 60 aaudio_result_t unregisterStream(android::sp<AAudioServiceStreamBase> stream); 61 62 virtual aaudio_result_t startStream(android::sp<AAudioServiceStreamBase> stream, 63 audio_port_handle_t *clientHandle) = 0; 64 65 virtual aaudio_result_t stopStream(android::sp<AAudioServiceStreamBase> stream, 66 audio_port_handle_t clientHandle) = 0; 67 startClient(const android::AudioClient & client,const audio_attributes_t * attr,audio_port_handle_t * clientHandle)68 virtual aaudio_result_t startClient(const android::AudioClient& client, 69 const audio_attributes_t *attr, 70 audio_port_handle_t *clientHandle) { 71 ALOGD("AAudioServiceEndpoint::startClient(...) AAUDIO_ERROR_UNAVAILABLE"); 72 return AAUDIO_ERROR_UNAVAILABLE; 73 } 74 stopClient(audio_port_handle_t clientHandle)75 virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle) { 76 ALOGD("AAudioServiceEndpoint::stopClient(...) AAUDIO_ERROR_UNAVAILABLE"); 77 return AAUDIO_ERROR_UNAVAILABLE; 78 } 79 standby()80 virtual aaudio_result_t standby() { 81 ALOGD("AAudioServiceEndpoint::standby() AAUDIO_ERROR_UNAVAILABLE"); 82 return AAUDIO_ERROR_UNAVAILABLE; 83 } 84 exitStandby(AudioEndpointParcelable * parcelable)85 virtual aaudio_result_t exitStandby(AudioEndpointParcelable* parcelable) { 86 ALOGD("AAudioServiceEndpoint::exitStandby() AAUDIO_ERROR_UNAVAILABLE"); 87 return AAUDIO_ERROR_UNAVAILABLE; 88 } 89 90 /** 91 * @param positionFrames 92 * @param timeNanos 93 * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error 94 */ 95 virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0; 96 97 /** 98 * Set time that the associated frame was presented to the hardware. 99 * 100 * @param positionFrames receive position, input value is ignored 101 * @param timeNanos receive time, input value is ignored 102 * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error 103 */ 104 virtual aaudio_result_t getTimestamp(int64_t *positionFrames, int64_t *timeNanos) = 0; 105 getFramesPerBurst()106 int32_t getFramesPerBurst() const { 107 return mFramesPerBurst; 108 } 109 getRequestedDeviceId()110 int32_t getRequestedDeviceId() const { return mRequestedDeviceId; } 111 112 bool matches(const AAudioStreamConfiguration& configuration); 113 114 // This should only be called from the AAudioEndpointManager under a mutex. getOpenCount()115 int32_t getOpenCount() const { 116 return mOpenCount; 117 } 118 119 // This should only be called from the AAudioEndpointManager under a mutex. setOpenCount(int32_t count)120 void setOpenCount(int32_t count) { 121 mOpenCount = count; 122 } 123 isConnected()124 bool isConnected() const { 125 return mConnected; 126 } 127 128 static audio_attributes_t getAudioAttributesFrom(const AAudioStreamParameters *params); 129 130 // Stop, disconnect and release any streams registered on this endpoint. 131 void releaseRegisteredStreams(); 132 isForSharing()133 bool isForSharing() const { 134 return mForSharing; 135 } 136 137 /** 138 * 139 * @param flag true if this endpoint is to be shared between multiple streams 140 */ setForSharing(bool flag)141 void setForSharing(bool flag) { 142 mForSharing = flag; 143 } 144 145 protected: 146 147 /** 148 * @param portHandle 149 * @return return true if a stream with the given portHandle is registered 150 */ 151 bool isStreamRegistered(audio_port_handle_t portHandle); 152 153 std::vector<android::sp<AAudioServiceStreamBase>> disconnectRegisteredStreams(); 154 155 mutable std::mutex mLockStreams; 156 std::vector<android::sp<AAudioServiceStreamBase>> mRegisteredStreams 157 GUARDED_BY(mLockStreams); 158 159 SimpleDoubleBuffer<Timestamp> mAtomicEndpointTimestamp; 160 161 android::AudioClient mMmapClient; // set in open, used in open and startStream 162 163 int32_t mFramesPerBurst = 0; 164 int32_t mOpenCount = 0; 165 int32_t mRequestedDeviceId = 0; 166 167 // True if this will be shared by one or more other streams. 168 bool mForSharing = false; 169 170 std::atomic<bool> mConnected{true}; 171 172 }; 173 174 } /* namespace aaudio */ 175 176 177 #endif //AAUDIO_SERVICE_ENDPOINT_H 178