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 ANDROID_AAUDIO_AAUDIO_BINDER_CLIENT_H 18 #define ANDROID_AAUDIO_AAUDIO_BINDER_CLIENT_H 19 20 #include <mutex> 21 22 #include <utils/RefBase.h> 23 #include <utils/Singleton.h> 24 25 #include <aaudio/AAudio.h> 26 #include <binder/IInterface.h> 27 28 #include "aaudio/BnAAudioClient.h" 29 #include "aaudio/IAAudioService.h" 30 #include "AAudioServiceInterface.h" 31 #include "binding/AAudioBinderAdapter.h" 32 #include "binding/AAudioStreamRequest.h" 33 #include "binding/AudioEndpointParcelable.h" 34 #include "core/AAudioStreamParameters.h" 35 36 /** 37 * Implements the AAudioServiceInterface by talking to the service through Binder. 38 */ 39 40 namespace aaudio { 41 42 class AAudioBinderClient : public virtual android::RefBase 43 , public AAudioServiceInterface 44 , public android::Singleton<AAudioBinderClient> { 45 46 public: 47 48 AAudioBinderClient(); 49 50 virtual ~AAudioBinderClient(); 51 registerClient(const android::sp<IAAudioClient> & client __unused)52 void registerClient(const android::sp<IAAudioClient>& client __unused) override {} 53 54 /** 55 * @param request info needed to create the stream 56 * @param configuration contains resulting information about the created stream 57 * @return an object for aaudio handle information, which includes the connected 58 * aaudio service lifetime id to recognize the connected aaudio service 59 * and aaudio handle to recognize the stream. If an error occurs, the 60 * aaudio handle will be set as the negative error. 61 */ 62 AAudioHandleInfo openStream(const AAudioStreamRequest &request, 63 AAudioStreamConfiguration &configurationOutput) override; 64 65 aaudio_result_t closeStream(const AAudioHandleInfo& streamHandleInfo) override; 66 67 /* Get an immutable description of the in-memory queues 68 * used to communicate with the underlying HAL or Service. 69 */ 70 aaudio_result_t getStreamDescription(const AAudioHandleInfo& streamHandleInfo, 71 AudioEndpointParcelable &endpointOut) override; 72 73 /** 74 * Start the flow of data. 75 * This is asynchronous. When complete, the service will send a STARTED event. 76 */ 77 aaudio_result_t startStream(const AAudioHandleInfo& streamHandleInfo) override; 78 79 /** 80 * Stop the flow of data such that start() can resume without loss of data. 81 * This is asynchronous. When complete, the service will send a PAUSED event. 82 */ 83 aaudio_result_t pauseStream(const AAudioHandleInfo& streamHandleInfo) override; 84 85 aaudio_result_t stopStream(const AAudioHandleInfo& streamHandleInfo) override; 86 87 /** 88 * Discard any data held by the underlying HAL or Service. 89 * This is asynchronous. When complete, the service will send a FLUSHED event. 90 */ 91 aaudio_result_t flushStream(const AAudioHandleInfo& streamHandleInfo) override; 92 93 /** 94 * Manage the specified thread as a low latency audio thread. 95 * TODO Consider passing this information as part of the startStream() call. 96 */ 97 aaudio_result_t registerAudioThread(const AAudioHandleInfo& streamHandleInfo, 98 pid_t clientThreadId, 99 int64_t periodNanoseconds) override; 100 101 aaudio_result_t unregisterAudioThread(const AAudioHandleInfo& streamHandleInfo, 102 pid_t clientThreadId) override; 103 startClient(const AAudioHandleInfo & streamHandleInfo __unused,const android::AudioClient & client __unused,const audio_attributes_t * attr __unused,audio_port_handle_t * clientHandle __unused)104 aaudio_result_t startClient(const AAudioHandleInfo& streamHandleInfo __unused, 105 const android::AudioClient& client __unused, 106 const audio_attributes_t *attr __unused, 107 audio_port_handle_t *clientHandle __unused) override { 108 return AAUDIO_ERROR_UNAVAILABLE; 109 } 110 stopClient(const AAudioHandleInfo & streamHandleInfo __unused,audio_port_handle_t clientHandle __unused)111 aaudio_result_t stopClient(const AAudioHandleInfo& streamHandleInfo __unused, 112 audio_port_handle_t clientHandle __unused) override { 113 return AAUDIO_ERROR_UNAVAILABLE; 114 } 115 116 aaudio_result_t exitStandby(const AAudioHandleInfo& streamHandleInfo, 117 AudioEndpointParcelable &endpointOut) override; 118 onStreamChange(aaudio_handle_t,int32_t,int32_t)119 void onStreamChange(aaudio_handle_t /*handle*/, int32_t /*opcode*/, int32_t /*value*/) { 120 // TODO This is just a stub so we can have a client Binder to pass to the service. 121 // TODO Implemented in a later CL. 122 ALOGW("onStreamChange called!"); 123 } 124 getServiceLifetimeId()125 int32_t getServiceLifetimeId() const { 126 return mAAudioClient->getServiceLifetimeId(); 127 } 128 129 class AAudioClient : public android::IBinder::DeathRecipient, public BnAAudioClient { 130 public: AAudioClient(const android::wp<AAudioBinderClient> & aaudioBinderClient)131 explicit AAudioClient(const android::wp<AAudioBinderClient>& aaudioBinderClient) 132 : mBinderClient(aaudioBinderClient) { 133 } 134 135 // implement DeathRecipient binderDied(const android::wp<android::IBinder> & who __unused)136 virtual void binderDied(const android::wp<android::IBinder>& who __unused) { 137 mServiceLifetimeId++; 138 android::sp<AAudioBinderClient> client = mBinderClient.promote(); 139 if (client.get() != nullptr) { 140 client->dropAAudioService(); 141 } 142 ALOGW("AAudio service binderDied()!"); 143 } 144 145 // implement BnAAudioClient onStreamChange(int32_t handle,int32_t opcode,int32_t value)146 android::binder::Status onStreamChange(int32_t handle, int32_t opcode, int32_t value) { 147 static_assert(std::is_same_v<aaudio_handle_t, int32_t>); 148 android::sp<AAudioBinderClient> client = mBinderClient.promote(); 149 if (client.get() != nullptr) { 150 client->onStreamChange(handle, opcode, value); 151 } 152 return android::binder::Status::ok(); 153 } 154 getServiceLifetimeId()155 int32_t getServiceLifetimeId() const { 156 return mServiceLifetimeId.load(); 157 } 158 private: 159 android::wp<AAudioBinderClient> mBinderClient; 160 std::atomic_int mServiceLifetimeId{0}; 161 }; 162 163 // This adapter is used to convert the binder interface (delegate) to the AudioServiceInterface 164 // conventions (translating between data types and respective parcelables, translating error 165 // codes and calling conventions). 166 // The adapter also owns the underlying service object and is responsible to unlink its death 167 // listener when destroyed. 168 class Adapter : public AAudioBinderAdapter { 169 public: Adapter(const android::sp<IAAudioService> & delegate,android::sp<AAudioClient> aaudioClient,int32_t serviceLifetimeId)170 Adapter(const android::sp<IAAudioService>& delegate, 171 android::sp<AAudioClient> aaudioClient, 172 int32_t serviceLifetimeId) 173 : AAudioBinderAdapter(delegate.get(), serviceLifetimeId), 174 mDelegate(delegate), 175 mAAudioClient(std::move(aaudioClient)) {} 176 ~Adapter()177 virtual ~Adapter() { 178 if (mDelegate != nullptr) { 179 android::IInterface::asBinder(mDelegate)->unlinkToDeath(mAAudioClient); 180 } 181 } 182 183 // This should never be called (call is rejected at the AudioBinderClient level). startClient(const AAudioHandleInfo & streamHandle __unused,const android::AudioClient & client __unused,const audio_attributes_t * attr __unused,audio_port_handle_t * clientHandle __unused)184 aaudio_result_t startClient(const AAudioHandleInfo& streamHandle __unused, 185 const android::AudioClient& client __unused, 186 const audio_attributes_t* attr __unused, 187 audio_port_handle_t* clientHandle __unused) override { 188 LOG_ALWAYS_FATAL("Shouldn't get here"); 189 return AAUDIO_ERROR_UNAVAILABLE; 190 } 191 192 // This should never be called (call is rejected at the AudioBinderClient level). stopClient(const AAudioHandleInfo & streamHandle __unused,audio_port_handle_t clientHandle __unused)193 aaudio_result_t stopClient(const AAudioHandleInfo& streamHandle __unused, 194 audio_port_handle_t clientHandle __unused) override { 195 LOG_ALWAYS_FATAL("Shouldn't get here"); 196 return AAUDIO_ERROR_UNAVAILABLE; 197 } 198 199 private: 200 android::sp<IAAudioService> mDelegate; 201 android::sp<AAudioClient> mAAudioClient; 202 }; 203 204 private: 205 android::Mutex mServiceLock; 206 std::shared_ptr<AAudioServiceInterface> mAdapter PT_GUARDED_BY(mServiceLock); 207 android::sp<AAudioClient> mAAudioClient; 208 209 std::shared_ptr<AAudioServiceInterface> getAAudioService() EXCLUDES(mServiceLock); 210 211 void dropAAudioService() EXCLUDES(mServiceLock); 212 213 }; 214 215 216 } /* namespace aaudio */ 217 218 #endif //ANDROID_AAUDIO_AAUDIO_BINDER_CLIENT_H 219