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