blob: fa5a2da4f843a32aeaf1a677845b2107bdd690cc [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
22#include <binder/IServiceManager.h>
Phil Burk11e8d332017-05-24 09:59:02 -070023#include <binder/ProcessState.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080024#include <utils/Mutex.h>
25#include <utils/RefBase.h>
Phil Burk9b3f8ef2017-05-16 11:37:43 -070026#include <utils/Singleton.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080027#include <aaudio/AAudio.h>
28
29#include "AudioEndpointParcelable.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080030
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070031#include "binding/AAudioBinderClient.h"
32
33#define AAUDIO_SERVICE_NAME "media.aaudio"
Phil Burkc0c70e32017-02-09 13:18:38 -080034
35using android::String16;
36using android::IServiceManager;
37using android::defaultServiceManager;
38using android::interface_cast;
Phil Burk11e8d332017-05-24 09:59:02 -070039using android::IInterface;
Phil Burkc0c70e32017-02-09 13:18:38 -080040using android::Mutex;
Phil Burk2bc7c182017-08-28 11:45:01 -070041using android::ProcessState;
Phil Burkc0c70e32017-02-09 13:18:38 -080042using android::sp;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070043using android::status_t;
Phil Burk11e8d332017-05-24 09:59:02 -070044using android::wp;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070045using android::binder::Status;
Phil Burkc0c70e32017-02-09 13:18:38 -080046
47using namespace aaudio;
48
Phil Burk9b3f8ef2017-05-16 11:37:43 -070049ANDROID_SINGLETON_STATIC_INSTANCE(AAudioBinderClient);
50
Phil Burk2bc7c182017-08-28 11:45:01 -070051// If we don't keep a strong pointer here then this singleton can get deleted!
52android::sp<AAudioBinderClient> gKeepBinderClient;
53
Phil Burk11e8d332017-05-24 09:59:02 -070054AAudioBinderClient::AAudioBinderClient()
55 : AAudioServiceInterface()
56 , Singleton<AAudioBinderClient>() {
Phil Burk2bc7c182017-08-28 11:45:01 -070057 gKeepBinderClient = this; // so this singleton won't get deleted
Phil Burk11e8d332017-05-24 09:59:02 -070058 mAAudioClient = new AAudioClient(this);
Phil Burkfbf031e2017-10-12 15:58:31 -070059 ALOGV("%s - this = %p, created mAAudioClient = %p", __func__, this, mAAudioClient.get());
Phil Burk11e8d332017-05-24 09:59:02 -070060}
61
62AAudioBinderClient::~AAudioBinderClient() {
Phil Burkfbf031e2017-10-12 15:58:31 -070063 ALOGV("%s - destroying %p", __func__, this);
Phil Burk11e8d332017-05-24 09:59:02 -070064 Mutex::Autolock _l(mServiceLock);
Phil Burk11e8d332017-05-24 09:59:02 -070065}
66
Phil Burkc0c70e32017-02-09 13:18:38 -080067// 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
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070070std::shared_ptr<AAudioServiceInterface> AAudioBinderClient::getAAudioService() {
71 std::shared_ptr<AAudioServiceInterface> result;
Phil Burk11e8d332017-05-24 09:59:02 -070072 sp<IAAudioService> aaudioService;
73 bool needToRegister = false;
74 {
75 Mutex::Autolock _l(mServiceLock);
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070076 if (mAdapter == nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -070077 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.
Phil Burk2bc7c182017-08-28 11:45:01 -070083 if (binder.get() != nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -070084 break;
85 }
86 } while (retries-- > 0);
87
Phil Burk2bc7c182017-08-28 11:45:01 -070088 if (binder.get() != nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -070089 // Ask for notification if the service dies.
90 status_t status = binder->linkToDeath(mAAudioClient);
Phil Burkc7abac42017-07-17 11:13:37 -070091 // TODO review what we should do if this fails
92 if (status != NO_ERROR) {
Phil Burk7ba46552019-04-15 08:58:08 -070093 ALOGE("%s() - linkToDeath() returned %d", __func__, status);
Phil Burkc7abac42017-07-17 11:13:37 -070094 }
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070095 aaudioService = interface_cast<IAAudioService>(binder);
96 mAdapter.reset(new Adapter(aaudioService, mAAudioClient));
Phil Burk11e8d332017-05-24 09:59:02 -070097 needToRegister = true;
98 // Make sure callbacks can be received by mAAudioClient
Phil Burk2bc7c182017-08-28 11:45:01 -070099 ProcessState::self()->startThreadPool();
Phil Burk11e8d332017-05-24 09:59:02 -0700100 } else {
101 ALOGE("AAudioBinderClient could not connect to %s", AAUDIO_SERVICE_NAME);
Phil Burkc0c70e32017-02-09 13:18:38 -0800102 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800103 }
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700104 result = mAdapter;
Phil Burkc0c70e32017-02-09 13:18:38 -0800105 }
Phil Burk11e8d332017-05-24 09:59:02 -0700106 // Do this outside the mutex lock.
Phil Burk2bc7c182017-08-28 11:45:01 -0700107 if (needToRegister && aaudioService.get() != nullptr) { // new client?
Phil Burk11e8d332017-05-24 09:59:02 -0700108 aaudioService->registerClient(mAAudioClient);
109 }
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700110 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800111}
112
Phil Burk11e8d332017-05-24 09:59:02 -0700113void AAudioBinderClient::dropAAudioService() {
114 Mutex::Autolock _l(mServiceLock);
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700115 mAdapter.reset();
Phil Burk71f35bb2017-04-13 16:05:07 -0700116}
Phil Burkc0c70e32017-02-09 13:18:38 -0800117
Phil Burkc0c70e32017-02-09 13:18:38 -0800118/**
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*/
123aaudio_handle_t AAudioBinderClient::openStream(const AAudioStreamRequest &request,
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700124 AAudioStreamConfiguration &configuration) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700125 aaudio_handle_t stream;
126 for (int i = 0; i < 2; i++) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700127 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700128 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800129
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700130 stream = service->openStream(request, configuration);
Phil Burk71f35bb2017-04-13 16:05:07 -0700131
132 if (stream == AAUDIO_ERROR_NO_SERVICE) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700133 ALOGE("openStream lost connection to AAudioService.");
Phil Burk71f35bb2017-04-13 16:05:07 -0700134 dropAAudioService(); // force a reconnect
135 } else {
136 break;
137 }
138 }
139 return stream;
Phil Burkc0c70e32017-02-09 13:18:38 -0800140}
141
142aaudio_result_t AAudioBinderClient::closeStream(aaudio_handle_t streamHandle) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700143 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700144 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700145
Phil Burkc0c70e32017-02-09 13:18:38 -0800146 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*/
152aaudio_result_t AAudioBinderClient::getStreamDescription(aaudio_handle_t streamHandle,
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700153 AudioEndpointParcelable& endpointOut) {
154 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700155 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700156
157 return service->getStreamDescription(streamHandle, endpointOut);
Phil Burkc0c70e32017-02-09 13:18:38 -0800158}
159
Phil Burkc0c70e32017-02-09 13:18:38 -0800160aaudio_result_t AAudioBinderClient::startStream(aaudio_handle_t streamHandle) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700161 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700162 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700163
Phil Burkc0c70e32017-02-09 13:18:38 -0800164 return service->startStream(streamHandle);
165}
166
Phil Burkc0c70e32017-02-09 13:18:38 -0800167aaudio_result_t AAudioBinderClient::pauseStream(aaudio_handle_t streamHandle) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700168 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700169 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700170
Phil Burk71f35bb2017-04-13 16:05:07 -0700171 return service->pauseStream(streamHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -0800172}
173
Phil Burk71f35bb2017-04-13 16:05:07 -0700174aaudio_result_t AAudioBinderClient::stopStream(aaudio_handle_t streamHandle) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700175 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700176 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700177
Phil Burk71f35bb2017-04-13 16:05:07 -0700178 return service->stopStream(streamHandle);
179}
180
Phil Burkc0c70e32017-02-09 13:18:38 -0800181aaudio_result_t AAudioBinderClient::flushStream(aaudio_handle_t streamHandle) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700182 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700183 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700184
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) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700194 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700195 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700196
197 return service->registerAudioThread(streamHandle, clientThreadId, periodNanoseconds);
Phil Burkc0c70e32017-02-09 13:18:38 -0800198}
199
200aaudio_result_t AAudioBinderClient::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800201 pid_t clientThreadId) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700202 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700203 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700204
205 return service->unregisterAudioThread(streamHandle, clientThreadId);
Phil Burkc0c70e32017-02-09 13:18:38 -0800206}