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
18 #define LOG_TAG "AAudioBinderClient"
19 //#define LOG_NDEBUG 0
20 #include <utils/Log.h>
21
22 #include <binder/IServiceManager.h>
23 #include <binder/ProcessState.h>
24 #include <utils/Mutex.h>
25 #include <utils/RefBase.h>
26 #include <utils/Singleton.h>
27 #include <aaudio/AAudio.h>
28
29 #include "AudioEndpointParcelable.h"
30
31 #include "binding/AAudioBinderClient.h"
32
33 #define AAUDIO_SERVICE_NAME "media.aaudio"
34
35 using android::String16;
36 using android::IServiceManager;
37 using android::defaultServiceManager;
38 using android::interface_cast;
39 using android::Mutex;
40 using android::ProcessState;
41 using android::sp;
42 using android::status_t;
43
44 using namespace aaudio;
45
46 ANDROID_SINGLETON_STATIC_INSTANCE(AAudioBinderClient);
47
48 // If we don't keep a strong pointer here then this singleton can get deleted!
49 android::sp<AAudioBinderClient> gKeepBinderClient;
50
AAudioBinderClient()51 AAudioBinderClient::AAudioBinderClient()
52 : AAudioServiceInterface()
53 , Singleton<AAudioBinderClient>() {
54 gKeepBinderClient = this; // so this singleton won't get deleted
55 mAAudioClient = new AAudioClient(this);
56 ALOGV("%s - this = %p, created mAAudioClient = %p", __func__, this, mAAudioClient.get());
57 }
58
~AAudioBinderClient()59 AAudioBinderClient::~AAudioBinderClient() {
60 ALOGV("%s - destroying %p", __func__, this);
61 Mutex::Autolock _l(mServiceLock);
62 }
63
64 // TODO Share code with other service clients.
65 // Helper function to get access to the "AAudioService" service.
66 // This code was modeled after frameworks/av/media/libaudioclient/AudioSystem.cpp
getAAudioService()67 std::shared_ptr<AAudioServiceInterface> AAudioBinderClient::getAAudioService() {
68 std::shared_ptr<AAudioServiceInterface> result;
69 sp<IAAudioService> aaudioService;
70 bool needToRegister = false;
71 {
72 Mutex::Autolock _l(mServiceLock);
73 if (mAdapter == nullptr) {
74 sp<IServiceManager> sm = defaultServiceManager();
75 sp<IBinder> binder = sm->waitForService(String16(AAUDIO_SERVICE_NAME));
76
77 if (binder != nullptr) {
78 // Ask for notification if the service dies.
79 status_t status = binder->linkToDeath(mAAudioClient);
80 // TODO review what we should do if this fails
81 if (status != NO_ERROR) {
82 ALOGE("%s() - linkToDeath() returned %d", __func__, status);
83 }
84 aaudioService = interface_cast<IAAudioService>(binder);
85 mAdapter = std::make_shared<Adapter>(
86 aaudioService, mAAudioClient, mAAudioClient->getServiceLifetimeId());
87 needToRegister = true;
88 // Make sure callbacks can be received by mAAudioClient
89 ProcessState::self()->startThreadPool();
90 } else {
91 ALOGE("AAudioBinderClient could not connect to %s", AAUDIO_SERVICE_NAME);
92 }
93 }
94 result = mAdapter;
95 }
96 // Do this outside the mutex lock.
97 if (needToRegister && aaudioService.get() != nullptr) { // new client?
98 aaudioService->registerClient(mAAudioClient);
99 }
100 return result;
101 }
102
dropAAudioService()103 void AAudioBinderClient::dropAAudioService() {
104 Mutex::Autolock _l(mServiceLock);
105 mAdapter.reset();
106 }
107
108 /**
109 * @param request info needed to create the stream
110 * @param configuration contains information about the created stream
111 * @return an object for aaudio handle information, which includes the connected
112 * aaudio service lifetime id to recognize the connected aaudio service
113 * and aaudio handle to recognize the stream. If an error occurs, the
114 * aaudio handle will be set as the negative error.
115 */
openStream(const AAudioStreamRequest & request,AAudioStreamConfiguration & configuration)116 AAudioHandleInfo AAudioBinderClient::openStream(const AAudioStreamRequest &request,
117 AAudioStreamConfiguration &configuration) {
118 for (int i = 0; i < 2; i++) {
119 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
120 if (service.get() == nullptr) {
121 return {};
122 }
123
124 AAudioHandleInfo handleInfo = service->openStream(request, configuration);
125
126 if (handleInfo.getHandle() == AAUDIO_ERROR_NO_SERVICE) {
127 ALOGE("openStream lost connection to AAudioService.");
128 dropAAudioService(); // force a reconnect
129 } else {
130 return handleInfo;
131 }
132 }
133 return {};
134 }
135
closeStream(const AAudioHandleInfo & streamHandleInfo)136 aaudio_result_t AAudioBinderClient::closeStream(const AAudioHandleInfo& streamHandleInfo) {
137 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
138 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
139
140 return service->closeStream(streamHandleInfo);
141 }
142
143 /* Get an immutable description of the in-memory queues
144 * used to communicate with the underlying HAL or Service.
145 */
getStreamDescription(const AAudioHandleInfo & streamHandleInfo,AudioEndpointParcelable & endpointOut)146 aaudio_result_t AAudioBinderClient::getStreamDescription(const AAudioHandleInfo& streamHandleInfo,
147 AudioEndpointParcelable& endpointOut) {
148 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
149 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
150
151 return service->getStreamDescription(streamHandleInfo, endpointOut);
152 }
153
startStream(const AAudioHandleInfo & streamHandleInfo)154 aaudio_result_t AAudioBinderClient::startStream(const AAudioHandleInfo& streamHandleInfo) {
155 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
156 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
157
158 return service->startStream(streamHandleInfo);
159 }
160
pauseStream(const AAudioHandleInfo & streamHandleInfo)161 aaudio_result_t AAudioBinderClient::pauseStream(const AAudioHandleInfo& streamHandleInfo) {
162 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
163 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
164
165 return service->pauseStream(streamHandleInfo);
166 }
167
stopStream(const AAudioHandleInfo & streamHandleInfo)168 aaudio_result_t AAudioBinderClient::stopStream(const AAudioHandleInfo& streamHandleInfo) {
169 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
170 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
171
172 return service->stopStream(streamHandleInfo);
173 }
174
flushStream(const AAudioHandleInfo & streamHandleInfo)175 aaudio_result_t AAudioBinderClient::flushStream(const AAudioHandleInfo& streamHandleInfo) {
176 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
177 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
178
179 return service->flushStream(streamHandleInfo);
180 }
181
182 /**
183 * Manage the specified thread as a low latency audio thread.
184 */
registerAudioThread(const AAudioHandleInfo & streamHandleInfo,pid_t clientThreadId,int64_t periodNanoseconds)185 aaudio_result_t AAudioBinderClient::registerAudioThread(const AAudioHandleInfo& streamHandleInfo,
186 pid_t clientThreadId,
187 int64_t periodNanoseconds) {
188 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
189 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
190
191 return service->registerAudioThread(streamHandleInfo, clientThreadId, periodNanoseconds);
192 }
193
unregisterAudioThread(const AAudioHandleInfo & streamHandleInfo,pid_t clientThreadId)194 aaudio_result_t AAudioBinderClient::unregisterAudioThread(const AAudioHandleInfo& streamHandleInfo,
195 pid_t clientThreadId) {
196 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
197 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
198
199 return service->unregisterAudioThread(streamHandleInfo, clientThreadId);
200 }
201
exitStandby(const AAudioHandleInfo & streamHandleInfo,AudioEndpointParcelable & endpointOut)202 aaudio_result_t AAudioBinderClient::exitStandby(const AAudioHandleInfo& streamHandleInfo,
203 AudioEndpointParcelable &endpointOut) {
204 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
205 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
206
207 return service->exitStandby(streamHandleInfo, endpointOut);
208 }
209