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<IBinder> binder;
75 sp<IServiceManager> sm = defaultServiceManager();
76 // Try several times to get the service.
77 int retries = 4;
78 do {
79 binder = sm->getService(String16(AAUDIO_SERVICE_NAME)); // This will wait a while.
80 if (binder.get() != nullptr) {
81 break;
82 }
83 } while (retries-- > 0);
84
85 if (binder.get() != nullptr) {
86 // Ask for notification if the service dies.
87 status_t status = binder->linkToDeath(mAAudioClient);
88 // TODO review what we should do if this fails
89 if (status != NO_ERROR) {
90 ALOGE("%s() - linkToDeath() returned %d", __func__, status);
91 }
92 aaudioService = interface_cast<IAAudioService>(binder);
93 mAdapter = std::make_shared<Adapter>(aaudioService, mAAudioClient);
94 needToRegister = true;
95 // Make sure callbacks can be received by mAAudioClient
96 ProcessState::self()->startThreadPool();
97 } else {
98 ALOGE("AAudioBinderClient could not connect to %s", AAUDIO_SERVICE_NAME);
99 }
100 }
101 result = mAdapter;
102 }
103 // Do this outside the mutex lock.
104 if (needToRegister && aaudioService.get() != nullptr) { // new client?
105 aaudioService->registerClient(mAAudioClient);
106 }
107 return result;
108 }
109
dropAAudioService()110 void AAudioBinderClient::dropAAudioService() {
111 Mutex::Autolock _l(mServiceLock);
112 mAdapter.reset();
113 }
114
115 /**
116 * @param request info needed to create the stream
117 * @param configuration contains information about the created stream
118 * @return handle to the stream or a negative error
119 */
openStream(const AAudioStreamRequest & request,AAudioStreamConfiguration & configuration)120 aaudio_handle_t AAudioBinderClient::openStream(const AAudioStreamRequest &request,
121 AAudioStreamConfiguration &configuration) {
122 aaudio_handle_t stream;
123 for (int i = 0; i < 2; i++) {
124 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
125 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
126
127 stream = service->openStream(request, configuration);
128
129 if (stream == AAUDIO_ERROR_NO_SERVICE) {
130 ALOGE("openStream lost connection to AAudioService.");
131 dropAAudioService(); // force a reconnect
132 } else {
133 break;
134 }
135 }
136 return stream;
137 }
138
closeStream(aaudio_handle_t streamHandle)139 aaudio_result_t AAudioBinderClient::closeStream(aaudio_handle_t streamHandle) {
140 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
141 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
142
143 return service->closeStream(streamHandle);
144 }
145
146 /* Get an immutable description of the in-memory queues
147 * used to communicate with the underlying HAL or Service.
148 */
getStreamDescription(aaudio_handle_t streamHandle,AudioEndpointParcelable & endpointOut)149 aaudio_result_t AAudioBinderClient::getStreamDescription(aaudio_handle_t streamHandle,
150 AudioEndpointParcelable& endpointOut) {
151 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
152 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
153
154 return service->getStreamDescription(streamHandle, endpointOut);
155 }
156
startStream(aaudio_handle_t streamHandle)157 aaudio_result_t AAudioBinderClient::startStream(aaudio_handle_t streamHandle) {
158 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
159 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
160
161 return service->startStream(streamHandle);
162 }
163
pauseStream(aaudio_handle_t streamHandle)164 aaudio_result_t AAudioBinderClient::pauseStream(aaudio_handle_t streamHandle) {
165 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
166 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
167
168 return service->pauseStream(streamHandle);
169 }
170
stopStream(aaudio_handle_t streamHandle)171 aaudio_result_t AAudioBinderClient::stopStream(aaudio_handle_t streamHandle) {
172 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
173 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
174
175 return service->stopStream(streamHandle);
176 }
177
flushStream(aaudio_handle_t streamHandle)178 aaudio_result_t AAudioBinderClient::flushStream(aaudio_handle_t streamHandle) {
179 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
180 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
181
182 return service->flushStream(streamHandle);
183 }
184
185 /**
186 * Manage the specified thread as a low latency audio thread.
187 */
registerAudioThread(aaudio_handle_t streamHandle,pid_t clientThreadId,int64_t periodNanoseconds)188 aaudio_result_t AAudioBinderClient::registerAudioThread(aaudio_handle_t streamHandle,
189 pid_t clientThreadId,
190 int64_t periodNanoseconds) {
191 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
192 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
193
194 return service->registerAudioThread(streamHandle, clientThreadId, periodNanoseconds);
195 }
196
unregisterAudioThread(aaudio_handle_t streamHandle,pid_t clientThreadId)197 aaudio_result_t AAudioBinderClient::unregisterAudioThread(aaudio_handle_t streamHandle,
198 pid_t clientThreadId) {
199 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
200 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
201
202 return service->unregisterAudioThread(streamHandle, clientThreadId);
203 }
204
exitStandby(aaudio_handle_t streamHandle,AudioEndpointParcelable & endpointOut)205 aaudio_result_t AAudioBinderClient::exitStandby(aaudio_handle_t streamHandle,
206 AudioEndpointParcelable &endpointOut) {
207 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
208 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
209
210 return service->exitStandby(streamHandle, endpointOut);
211 }
212