• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>(
94                         aaudioService, mAAudioClient, mAAudioClient->getServiceLifetimeId());
95                 needToRegister = true;
96                 // Make sure callbacks can be received by mAAudioClient
97                 ProcessState::self()->startThreadPool();
98             } else {
99                 ALOGE("AAudioBinderClient could not connect to %s", AAUDIO_SERVICE_NAME);
100             }
101         }
102         result = mAdapter;
103     }
104     // Do this outside the mutex lock.
105     if (needToRegister && aaudioService.get() != nullptr) { // new client?
106         aaudioService->registerClient(mAAudioClient);
107     }
108     return result;
109 }
110 
dropAAudioService()111 void AAudioBinderClient::dropAAudioService() {
112     Mutex::Autolock _l(mServiceLock);
113     mAdapter.reset();
114 }
115 
116 /**
117 * @param request info needed to create the stream
118 * @param configuration contains information about the created stream
119 * @return an object for aaudio handle information, which includes the connected
120 *         aaudio service lifetime id to recognize the connected aaudio service
121 *         and aaudio handle to recognize the stream. If an error occurs, the
122 *         aaudio handle will be set as the negative error.
123 */
openStream(const AAudioStreamRequest & request,AAudioStreamConfiguration & configuration)124 AAudioHandleInfo AAudioBinderClient::openStream(const AAudioStreamRequest &request,
125                                                 AAudioStreamConfiguration &configuration) {
126     for (int i = 0; i < 2; i++) {
127         std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
128         if (service.get() == nullptr) {
129             return {};
130         }
131 
132         AAudioHandleInfo handleInfo = service->openStream(request, configuration);
133 
134         if (handleInfo.getHandle() == AAUDIO_ERROR_NO_SERVICE) {
135             ALOGE("openStream lost connection to AAudioService.");
136             dropAAudioService(); // force a reconnect
137         } else {
138             return handleInfo;
139         }
140     }
141     return {};
142 }
143 
closeStream(const AAudioHandleInfo & streamHandleInfo)144 aaudio_result_t AAudioBinderClient::closeStream(const AAudioHandleInfo& streamHandleInfo) {
145     std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
146     if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
147 
148     return service->closeStream(streamHandleInfo);
149 }
150 
151 /* Get an immutable description of the in-memory queues
152 * used to communicate with the underlying HAL or Service.
153 */
getStreamDescription(const AAudioHandleInfo & streamHandleInfo,AudioEndpointParcelable & endpointOut)154 aaudio_result_t AAudioBinderClient::getStreamDescription(const AAudioHandleInfo& streamHandleInfo,
155                                                          AudioEndpointParcelable& endpointOut) {
156     std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
157     if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
158 
159     return service->getStreamDescription(streamHandleInfo, endpointOut);
160 }
161 
startStream(const AAudioHandleInfo & streamHandleInfo)162 aaudio_result_t AAudioBinderClient::startStream(const AAudioHandleInfo& streamHandleInfo) {
163     std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
164     if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
165 
166     return service->startStream(streamHandleInfo);
167 }
168 
pauseStream(const AAudioHandleInfo & streamHandleInfo)169 aaudio_result_t AAudioBinderClient::pauseStream(const AAudioHandleInfo& streamHandleInfo) {
170     std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
171     if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
172 
173     return service->pauseStream(streamHandleInfo);
174 }
175 
stopStream(const AAudioHandleInfo & streamHandleInfo)176 aaudio_result_t AAudioBinderClient::stopStream(const AAudioHandleInfo& streamHandleInfo) {
177     std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
178     if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
179 
180     return service->stopStream(streamHandleInfo);
181 }
182 
flushStream(const AAudioHandleInfo & streamHandleInfo)183 aaudio_result_t AAudioBinderClient::flushStream(const AAudioHandleInfo& streamHandleInfo) {
184     std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
185     if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
186 
187     return service->flushStream(streamHandleInfo);
188 }
189 
190 /**
191 * Manage the specified thread as a low latency audio thread.
192 */
registerAudioThread(const AAudioHandleInfo & streamHandleInfo,pid_t clientThreadId,int64_t periodNanoseconds)193 aaudio_result_t AAudioBinderClient::registerAudioThread(const AAudioHandleInfo& streamHandleInfo,
194                                                         pid_t clientThreadId,
195                                                         int64_t periodNanoseconds) {
196     std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
197     if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
198 
199     return service->registerAudioThread(streamHandleInfo, clientThreadId, periodNanoseconds);
200 }
201 
unregisterAudioThread(const AAudioHandleInfo & streamHandleInfo,pid_t clientThreadId)202 aaudio_result_t AAudioBinderClient::unregisterAudioThread(const AAudioHandleInfo& streamHandleInfo,
203                                                           pid_t clientThreadId) {
204     std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
205     if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
206 
207     return service->unregisterAudioThread(streamHandleInfo, clientThreadId);
208 }
209 
exitStandby(const AAudioHandleInfo & streamHandleInfo,AudioEndpointParcelable & endpointOut)210 aaudio_result_t AAudioBinderClient::exitStandby(const AAudioHandleInfo& streamHandleInfo,
211                                                 AudioEndpointParcelable &endpointOut) {
212     std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
213     if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
214 
215     return service->exitStandby(streamHandleInfo, endpointOut);
216 }
217