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