blob: dd620e3e0af044c5d290888c185301fe36060a25 [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) {
100 ALOGE("getAAudioService: linkToDeath(mAAudioClient = %p) returned %d",
101 mAAudioClient.get(), status);
102 }
Phil Burk11e8d332017-05-24 09:59:02 -0700103 mAAudioService = interface_cast<IAAudioService>(binder);
104 needToRegister = true;
105 // Make sure callbacks can be received by mAAudioClient
Phil Burk2bc7c182017-08-28 11:45:01 -0700106 ProcessState::self()->startThreadPool();
Phil Burk11e8d332017-05-24 09:59:02 -0700107 } else {
108 ALOGE("AAudioBinderClient could not connect to %s", AAUDIO_SERVICE_NAME);
Phil Burkc0c70e32017-02-09 13:18:38 -0800109 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800110 }
Phil Burk11e8d332017-05-24 09:59:02 -0700111 aaudioService = mAAudioService;
Phil Burkc0c70e32017-02-09 13:18:38 -0800112 }
Phil Burk11e8d332017-05-24 09:59:02 -0700113 // Do this outside the mutex lock.
Phil Burk2bc7c182017-08-28 11:45:01 -0700114 if (needToRegister && aaudioService.get() != nullptr) { // new client?
Phil Burk11e8d332017-05-24 09:59:02 -0700115 aaudioService->registerClient(mAAudioClient);
116 }
117 return aaudioService;
Phil Burkc0c70e32017-02-09 13:18:38 -0800118}
119
Phil Burk11e8d332017-05-24 09:59:02 -0700120void AAudioBinderClient::dropAAudioService() {
121 Mutex::Autolock _l(mServiceLock);
122 mAAudioService.clear(); // force a reconnect
Phil Burk71f35bb2017-04-13 16:05:07 -0700123}
Phil Burkc0c70e32017-02-09 13:18:38 -0800124
Phil Burkc0c70e32017-02-09 13:18:38 -0800125/**
126* @param request info needed to create the stream
127* @param configuration contains information about the created stream
128* @return handle to the stream or a negative error
129*/
130aaudio_handle_t AAudioBinderClient::openStream(const AAudioStreamRequest &request,
131 AAudioStreamConfiguration &configurationOutput) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700132 aaudio_handle_t stream;
133 for (int i = 0; i < 2; i++) {
134 const sp<IAAudioService> &service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700135 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800136
Phil Burk71f35bb2017-04-13 16:05:07 -0700137 stream = service->openStream(request, configurationOutput);
138
139 if (stream == AAUDIO_ERROR_NO_SERVICE) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700140 ALOGE("openStream lost connection to AAudioService.");
Phil Burk71f35bb2017-04-13 16:05:07 -0700141 dropAAudioService(); // force a reconnect
142 } else {
143 break;
144 }
145 }
146 return stream;
Phil Burkc0c70e32017-02-09 13:18:38 -0800147}
148
149aaudio_result_t AAudioBinderClient::closeStream(aaudio_handle_t streamHandle) {
Phil Burk2bc7c182017-08-28 11:45:01 -0700150 const sp<IAAudioService> service = getAAudioService();
151 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800152 return service->closeStream(streamHandle);
153}
154
155/* Get an immutable description of the in-memory queues
156* used to communicate with the underlying HAL or Service.
157*/
158aaudio_result_t AAudioBinderClient::getStreamDescription(aaudio_handle_t streamHandle,
159 AudioEndpointParcelable &parcelable) {
Phil Burk2bc7c182017-08-28 11:45:01 -0700160 const sp<IAAudioService> service = getAAudioService();
161 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800162 return service->getStreamDescription(streamHandle, parcelable);
163}
164
Phil Burkc0c70e32017-02-09 13:18:38 -0800165aaudio_result_t AAudioBinderClient::startStream(aaudio_handle_t streamHandle) {
Phil Burk2bc7c182017-08-28 11:45:01 -0700166 const sp<IAAudioService> service = getAAudioService();
167 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800168 return service->startStream(streamHandle);
169}
170
Phil Burkc0c70e32017-02-09 13:18:38 -0800171aaudio_result_t AAudioBinderClient::pauseStream(aaudio_handle_t streamHandle) {
Phil Burk2bc7c182017-08-28 11:45:01 -0700172 const sp<IAAudioService> service = getAAudioService();
173 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burk71f35bb2017-04-13 16:05:07 -0700174 return service->pauseStream(streamHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -0800175}
176
Phil Burk71f35bb2017-04-13 16:05:07 -0700177aaudio_result_t AAudioBinderClient::stopStream(aaudio_handle_t streamHandle) {
Phil Burk2bc7c182017-08-28 11:45:01 -0700178 const sp<IAAudioService> service = getAAudioService();
179 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burk71f35bb2017-04-13 16:05:07 -0700180 return service->stopStream(streamHandle);
181}
182
Phil Burkc0c70e32017-02-09 13:18:38 -0800183aaudio_result_t AAudioBinderClient::flushStream(aaudio_handle_t streamHandle) {
Phil Burk2bc7c182017-08-28 11:45:01 -0700184 const sp<IAAudioService> service = getAAudioService();
185 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burk71f35bb2017-04-13 16:05:07 -0700186 return service->flushStream(streamHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -0800187}
188
189/**
190* Manage the specified thread as a low latency audio thread.
191*/
192aaudio_result_t AAudioBinderClient::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800193 pid_t clientThreadId,
194 int64_t periodNanoseconds) {
Phil Burk2bc7c182017-08-28 11:45:01 -0700195 const sp<IAAudioService> service = getAAudioService();
196 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800197 return service->registerAudioThread(streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800198 clientThreadId,
199 periodNanoseconds);
200}
201
202aaudio_result_t AAudioBinderClient::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800203 pid_t clientThreadId) {
Phil Burk2bc7c182017-08-28 11:45:01 -0700204 const sp<IAAudioService> service = getAAudioService();
205 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800206 return service->unregisterAudioThread(streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800207 clientThreadId);
208}