blob: 7b0d31f88a4f22e26ce7d69dd87359ff6e56cf50 [file] [log] [blame]
Phil Burkc0c70e32017-02-09 13:18:38 -08001/*
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
Phil Burkfbf031e2017-10-12 15:58:31 -070018#define LOG_TAG "AAudioBinderClient"
Phil Burkc0c70e32017-02-09 13:18:38 -080019//#define LOG_NDEBUG 0
20#include <utils/Log.h>
21
Phil Burk11e8d332017-05-24 09:59:02 -070022#include <binder/IInterface.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080023#include <binder/IServiceManager.h>
Phil Burk11e8d332017-05-24 09:59:02 -070024#include <binder/ProcessState.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080025#include <utils/Mutex.h>
26#include <utils/RefBase.h>
Phil Burk9b3f8ef2017-05-16 11:37:43 -070027#include <utils/Singleton.h>
Phil Burk11e8d332017-05-24 09:59:02 -070028#include <media/AudioSystem.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080029
30#include <aaudio/AAudio.h>
31
32#include "AudioEndpointParcelable.h"
Phil Burk11e8d332017-05-24 09:59:02 -070033#include "binding/AAudioBinderClient.h"
34//#include "binding/AAudioStreamRequest.h"
35//#include "binding/AAudioStreamConfiguration.h"
36//#include "binding/IAAudioService.h"
37//#include "binding/AAudioServiceMessage.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080038
Phil Burk11e8d332017-05-24 09:59:02 -070039//#include "AAudioServiceInterface.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080040
41using android::String16;
42using android::IServiceManager;
43using android::defaultServiceManager;
44using android::interface_cast;
Phil Burk11e8d332017-05-24 09:59:02 -070045using android::IInterface;
Phil Burkc0c70e32017-02-09 13:18:38 -080046using android::IAAudioService;
47using android::Mutex;
Phil Burk2bc7c182017-08-28 11:45:01 -070048using android::ProcessState;
Phil Burkc0c70e32017-02-09 13:18:38 -080049using android::sp;
Phil Burk11e8d332017-05-24 09:59:02 -070050using android::wp;
Phil Burkc0c70e32017-02-09 13:18:38 -080051
52using namespace aaudio;
53
Phil Burk9b3f8ef2017-05-16 11:37:43 -070054ANDROID_SINGLETON_STATIC_INSTANCE(AAudioBinderClient);
55
Phil Burk2bc7c182017-08-28 11:45:01 -070056// If we don't keep a strong pointer here then this singleton can get deleted!
57android::sp<AAudioBinderClient> gKeepBinderClient;
58
Phil Burk11e8d332017-05-24 09:59:02 -070059AAudioBinderClient::AAudioBinderClient()
60 : AAudioServiceInterface()
61 , Singleton<AAudioBinderClient>() {
Phil Burk2bc7c182017-08-28 11:45:01 -070062 gKeepBinderClient = this; // so this singleton won't get deleted
Phil Burk11e8d332017-05-24 09:59:02 -070063 mAAudioClient = new AAudioClient(this);
Phil Burkfbf031e2017-10-12 15:58:31 -070064 ALOGV("%s - this = %p, created mAAudioClient = %p", __func__, this, mAAudioClient.get());
Phil Burk11e8d332017-05-24 09:59:02 -070065}
66
67AAudioBinderClient::~AAudioBinderClient() {
Phil Burkfbf031e2017-10-12 15:58:31 -070068 ALOGV("%s - destroying %p", __func__, this);
Phil Burk11e8d332017-05-24 09:59:02 -070069 Mutex::Autolock _l(mServiceLock);
70 if (mAAudioService != 0) {
71 IInterface::asBinder(mAAudioService)->unlinkToDeath(mAAudioClient);
72 }
73}
74
Phil Burkc0c70e32017-02-09 13:18:38 -080075// 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
Phil Burk11e8d332017-05-24 09:59:02 -070078const sp<IAAudioService> AAudioBinderClient::getAAudioService() {
79 sp<IAAudioService> aaudioService;
80 bool needToRegister = false;
81 {
82 Mutex::Autolock _l(mServiceLock);
Phil Burk2bc7c182017-08-28 11:45:01 -070083 if (mAAudioService.get() == nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -070084 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.
Phil Burk2bc7c182017-08-28 11:45:01 -070090 if (binder.get() != nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -070091 break;
92 }
93 } while (retries-- > 0);
94
Phil Burk2bc7c182017-08-28 11:45:01 -070095 if (binder.get() != nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -070096 // Ask for notification if the service dies.
97 status_t status = binder->linkToDeath(mAAudioClient);
Phil Burkc7abac42017-07-17 11:13:37 -070098 // TODO review what we should do if this fails
99 if (status != NO_ERROR) {
Phil Burk7ba46552019-04-15 08:58:08 -0700100 ALOGE("%s() - linkToDeath() returned %d", __func__, status);
Phil Burkc7abac42017-07-17 11:13:37 -0700101 }
Phil Burk11e8d332017-05-24 09:59:02 -0700102 mAAudioService = interface_cast<IAAudioService>(binder);
103 needToRegister = true;
104 // Make sure callbacks can be received by mAAudioClient
Phil Burk2bc7c182017-08-28 11:45:01 -0700105 ProcessState::self()->startThreadPool();
Phil Burk11e8d332017-05-24 09:59:02 -0700106 } else {
107 ALOGE("AAudioBinderClient could not connect to %s", AAUDIO_SERVICE_NAME);
Phil Burkc0c70e32017-02-09 13:18:38 -0800108 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800109 }
Phil Burk11e8d332017-05-24 09:59:02 -0700110 aaudioService = mAAudioService;
Phil Burkc0c70e32017-02-09 13:18:38 -0800111 }
Phil Burk11e8d332017-05-24 09:59:02 -0700112 // Do this outside the mutex lock.
Phil Burk2bc7c182017-08-28 11:45:01 -0700113 if (needToRegister && aaudioService.get() != nullptr) { // new client?
Phil Burk11e8d332017-05-24 09:59:02 -0700114 aaudioService->registerClient(mAAudioClient);
115 }
116 return aaudioService;
Phil Burkc0c70e32017-02-09 13:18:38 -0800117}
118
Phil Burk11e8d332017-05-24 09:59:02 -0700119void AAudioBinderClient::dropAAudioService() {
120 Mutex::Autolock _l(mServiceLock);
121 mAAudioService.clear(); // force a reconnect
Phil Burk71f35bb2017-04-13 16:05:07 -0700122}
Phil Burkc0c70e32017-02-09 13:18:38 -0800123
Phil Burkc0c70e32017-02-09 13:18:38 -0800124/**
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*/
129aaudio_handle_t AAudioBinderClient::openStream(const AAudioStreamRequest &request,
130 AAudioStreamConfiguration &configurationOutput) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700131 aaudio_handle_t stream;
132 for (int i = 0; i < 2; i++) {
133 const sp<IAAudioService> &service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700134 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800135
Phil Burk71f35bb2017-04-13 16:05:07 -0700136 stream = service->openStream(request, configurationOutput);
137
138 if (stream == AAUDIO_ERROR_NO_SERVICE) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700139 ALOGE("openStream lost connection to AAudioService.");
Phil Burk71f35bb2017-04-13 16:05:07 -0700140 dropAAudioService(); // force a reconnect
141 } else {
142 break;
143 }
144 }
145 return stream;
Phil Burkc0c70e32017-02-09 13:18:38 -0800146}
147
148aaudio_result_t AAudioBinderClient::closeStream(aaudio_handle_t streamHandle) {
Phil Burk2bc7c182017-08-28 11:45:01 -0700149 const sp<IAAudioService> service = getAAudioService();
150 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800151 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*/
157aaudio_result_t AAudioBinderClient::getStreamDescription(aaudio_handle_t streamHandle,
158 AudioEndpointParcelable &parcelable) {
Phil Burk2bc7c182017-08-28 11:45:01 -0700159 const sp<IAAudioService> service = getAAudioService();
160 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800161 return service->getStreamDescription(streamHandle, parcelable);
162}
163
Phil Burkc0c70e32017-02-09 13:18:38 -0800164aaudio_result_t AAudioBinderClient::startStream(aaudio_handle_t streamHandle) {
Phil Burk2bc7c182017-08-28 11:45:01 -0700165 const sp<IAAudioService> service = getAAudioService();
166 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800167 return service->startStream(streamHandle);
168}
169
Phil Burkc0c70e32017-02-09 13:18:38 -0800170aaudio_result_t AAudioBinderClient::pauseStream(aaudio_handle_t streamHandle) {
Phil Burk2bc7c182017-08-28 11:45:01 -0700171 const sp<IAAudioService> service = getAAudioService();
172 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burk71f35bb2017-04-13 16:05:07 -0700173 return service->pauseStream(streamHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -0800174}
175
Phil Burk71f35bb2017-04-13 16:05:07 -0700176aaudio_result_t AAudioBinderClient::stopStream(aaudio_handle_t streamHandle) {
Phil Burk2bc7c182017-08-28 11:45:01 -0700177 const sp<IAAudioService> service = getAAudioService();
178 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burk71f35bb2017-04-13 16:05:07 -0700179 return service->stopStream(streamHandle);
180}
181
Phil Burkc0c70e32017-02-09 13:18:38 -0800182aaudio_result_t AAudioBinderClient::flushStream(aaudio_handle_t streamHandle) {
Phil Burk2bc7c182017-08-28 11:45:01 -0700183 const sp<IAAudioService> service = getAAudioService();
184 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burk71f35bb2017-04-13 16:05:07 -0700185 return service->flushStream(streamHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -0800186}
187
188/**
189* Manage the specified thread as a low latency audio thread.
190*/
191aaudio_result_t AAudioBinderClient::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800192 pid_t clientThreadId,
193 int64_t periodNanoseconds) {
Phil Burk2bc7c182017-08-28 11:45:01 -0700194 const sp<IAAudioService> service = getAAudioService();
195 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800196 return service->registerAudioThread(streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800197 clientThreadId,
198 periodNanoseconds);
199}
200
201aaudio_result_t AAudioBinderClient::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800202 pid_t clientThreadId) {
Phil Burk2bc7c182017-08-28 11:45:01 -0700203 const sp<IAAudioService> service = getAAudioService();
204 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800205 return service->unregisterAudioThread(streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800206 clientThreadId);
207}