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