blob: 5f34a75c632f9ddc36a4ff47f3bf8ef295f3ca30 [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 Burkc0c70e32017-02-09 13:18:38 -080039using android::Mutex;
Phil Burk2bc7c182017-08-28 11:45:01 -070040using android::ProcessState;
Phil Burkc0c70e32017-02-09 13:18:38 -080041using android::sp;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070042using android::status_t;
Phil Burkc0c70e32017-02-09 13:18:38 -080043
44using namespace aaudio;
45
Phil Burk9b3f8ef2017-05-16 11:37:43 -070046ANDROID_SINGLETON_STATIC_INSTANCE(AAudioBinderClient);
47
Phil Burk2bc7c182017-08-28 11:45:01 -070048// If we don't keep a strong pointer here then this singleton can get deleted!
49android::sp<AAudioBinderClient> gKeepBinderClient;
50
Phil Burk11e8d332017-05-24 09:59:02 -070051AAudioBinderClient::AAudioBinderClient()
52 : AAudioServiceInterface()
53 , Singleton<AAudioBinderClient>() {
Phil Burk2bc7c182017-08-28 11:45:01 -070054 gKeepBinderClient = this; // so this singleton won't get deleted
Phil Burk11e8d332017-05-24 09:59:02 -070055 mAAudioClient = new AAudioClient(this);
Phil Burkfbf031e2017-10-12 15:58:31 -070056 ALOGV("%s - this = %p, created mAAudioClient = %p", __func__, this, mAAudioClient.get());
Phil Burk11e8d332017-05-24 09:59:02 -070057}
58
59AAudioBinderClient::~AAudioBinderClient() {
Phil Burkfbf031e2017-10-12 15:58:31 -070060 ALOGV("%s - destroying %p", __func__, this);
Phil Burk11e8d332017-05-24 09:59:02 -070061 Mutex::Autolock _l(mServiceLock);
Phil Burk11e8d332017-05-24 09:59:02 -070062}
63
Phil Burkc0c70e32017-02-09 13:18:38 -080064// TODO Share code with other service clients.
65// Helper function to get access to the "AAudioService" service.
66// This code was modeled after frameworks/av/media/libaudioclient/AudioSystem.cpp
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070067std::shared_ptr<AAudioServiceInterface> AAudioBinderClient::getAAudioService() {
68 std::shared_ptr<AAudioServiceInterface> result;
Phil Burk11e8d332017-05-24 09:59:02 -070069 sp<IAAudioService> aaudioService;
70 bool needToRegister = false;
71 {
72 Mutex::Autolock _l(mServiceLock);
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070073 if (mAdapter == nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -070074 sp<IBinder> binder;
75 sp<IServiceManager> sm = defaultServiceManager();
76 // Try several times to get the service.
77 int retries = 4;
78 do {
79 binder = sm->getService(String16(AAUDIO_SERVICE_NAME)); // This will wait a while.
Phil Burk2bc7c182017-08-28 11:45:01 -070080 if (binder.get() != nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -070081 break;
82 }
83 } while (retries-- > 0);
84
Phil Burk2bc7c182017-08-28 11:45:01 -070085 if (binder.get() != nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -070086 // Ask for notification if the service dies.
87 status_t status = binder->linkToDeath(mAAudioClient);
Phil Burkc7abac42017-07-17 11:13:37 -070088 // TODO review what we should do if this fails
89 if (status != NO_ERROR) {
Phil Burk7ba46552019-04-15 08:58:08 -070090 ALOGE("%s() - linkToDeath() returned %d", __func__, status);
Phil Burkc7abac42017-07-17 11:13:37 -070091 }
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070092 aaudioService = interface_cast<IAAudioService>(binder);
jiabin5f787812023-03-02 20:42:43 +000093 mAdapter = std::make_shared<Adapter>(
94 aaudioService, mAAudioClient, mAAudioClient->getServiceLifetimeId());
Phil Burk11e8d332017-05-24 09:59:02 -070095 needToRegister = true;
96 // Make sure callbacks can be received by mAAudioClient
Phil Burk2bc7c182017-08-28 11:45:01 -070097 ProcessState::self()->startThreadPool();
Phil Burk11e8d332017-05-24 09:59:02 -070098 } else {
99 ALOGE("AAudioBinderClient could not connect to %s", AAUDIO_SERVICE_NAME);
Phil Burkc0c70e32017-02-09 13:18:38 -0800100 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800101 }
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700102 result = mAdapter;
Phil Burkc0c70e32017-02-09 13:18:38 -0800103 }
Phil Burk11e8d332017-05-24 09:59:02 -0700104 // Do this outside the mutex lock.
Phil Burk2bc7c182017-08-28 11:45:01 -0700105 if (needToRegister && aaudioService.get() != nullptr) { // new client?
Phil Burk11e8d332017-05-24 09:59:02 -0700106 aaudioService->registerClient(mAAudioClient);
107 }
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700108 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800109}
110
Phil Burk11e8d332017-05-24 09:59:02 -0700111void AAudioBinderClient::dropAAudioService() {
112 Mutex::Autolock _l(mServiceLock);
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700113 mAdapter.reset();
Phil Burk71f35bb2017-04-13 16:05:07 -0700114}
Phil Burkc0c70e32017-02-09 13:18:38 -0800115
Phil Burkc0c70e32017-02-09 13:18:38 -0800116/**
117* @param request info needed to create the stream
118* @param configuration contains information about the created stream
jiabin5f787812023-03-02 20:42:43 +0000119* @return an object for aaudio handle information, which includes the connected
120* aaudio service lifetime id to recognize the connected aaudio service
121* and aaudio handle to recognize the stream. If an error occurs, the
122* aaudio handle will be set as the negative error.
Phil Burkc0c70e32017-02-09 13:18:38 -0800123*/
jiabin5f787812023-03-02 20:42:43 +0000124AAudioHandleInfo AAudioBinderClient::openStream(const AAudioStreamRequest &request,
125 AAudioStreamConfiguration &configuration) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700126 for (int i = 0; i < 2; i++) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700127 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
jiabin5f787812023-03-02 20:42:43 +0000128 if (service.get() == nullptr) {
129 return {};
130 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800131
jiabin5f787812023-03-02 20:42:43 +0000132 AAudioHandleInfo handleInfo = service->openStream(request, configuration);
Phil Burk71f35bb2017-04-13 16:05:07 -0700133
jiabin5f787812023-03-02 20:42:43 +0000134 if (handleInfo.getHandle() == AAUDIO_ERROR_NO_SERVICE) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700135 ALOGE("openStream lost connection to AAudioService.");
Phil Burk71f35bb2017-04-13 16:05:07 -0700136 dropAAudioService(); // force a reconnect
137 } else {
jiabin5f787812023-03-02 20:42:43 +0000138 return handleInfo;
Phil Burk71f35bb2017-04-13 16:05:07 -0700139 }
140 }
jiabin5f787812023-03-02 20:42:43 +0000141 return {};
Phil Burkc0c70e32017-02-09 13:18:38 -0800142}
143
jiabin5f787812023-03-02 20:42:43 +0000144aaudio_result_t AAudioBinderClient::closeStream(const AAudioHandleInfo& streamHandleInfo) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700145 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700146 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700147
jiabin5f787812023-03-02 20:42:43 +0000148 return service->closeStream(streamHandleInfo);
Phil Burkc0c70e32017-02-09 13:18:38 -0800149}
150
151/* Get an immutable description of the in-memory queues
152* used to communicate with the underlying HAL or Service.
153*/
jiabin5f787812023-03-02 20:42:43 +0000154aaudio_result_t AAudioBinderClient::getStreamDescription(const AAudioHandleInfo& streamHandleInfo,
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700155 AudioEndpointParcelable& endpointOut) {
156 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700157 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700158
jiabin5f787812023-03-02 20:42:43 +0000159 return service->getStreamDescription(streamHandleInfo, endpointOut);
Phil Burkc0c70e32017-02-09 13:18:38 -0800160}
161
jiabin5f787812023-03-02 20:42:43 +0000162aaudio_result_t AAudioBinderClient::startStream(const AAudioHandleInfo& streamHandleInfo) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700163 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700164 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700165
jiabin5f787812023-03-02 20:42:43 +0000166 return service->startStream(streamHandleInfo);
Phil Burkc0c70e32017-02-09 13:18:38 -0800167}
168
jiabin5f787812023-03-02 20:42:43 +0000169aaudio_result_t AAudioBinderClient::pauseStream(const AAudioHandleInfo& streamHandleInfo) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700170 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700171 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700172
jiabin5f787812023-03-02 20:42:43 +0000173 return service->pauseStream(streamHandleInfo);
Phil Burkc0c70e32017-02-09 13:18:38 -0800174}
175
jiabin5f787812023-03-02 20:42:43 +0000176aaudio_result_t AAudioBinderClient::stopStream(const AAudioHandleInfo& streamHandleInfo) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700177 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700178 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700179
jiabin5f787812023-03-02 20:42:43 +0000180 return service->stopStream(streamHandleInfo);
Phil Burk71f35bb2017-04-13 16:05:07 -0700181}
182
jiabin5f787812023-03-02 20:42:43 +0000183aaudio_result_t AAudioBinderClient::flushStream(const AAudioHandleInfo& streamHandleInfo) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700184 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700185 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700186
jiabin5f787812023-03-02 20:42:43 +0000187 return service->flushStream(streamHandleInfo);
Phil Burkc0c70e32017-02-09 13:18:38 -0800188}
189
190/**
191* Manage the specified thread as a low latency audio thread.
192*/
jiabin5f787812023-03-02 20:42:43 +0000193aaudio_result_t AAudioBinderClient::registerAudioThread(const AAudioHandleInfo& streamHandleInfo,
Phil Burkc0c70e32017-02-09 13:18:38 -0800194 pid_t clientThreadId,
195 int64_t periodNanoseconds) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700196 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700197 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700198
jiabin5f787812023-03-02 20:42:43 +0000199 return service->registerAudioThread(streamHandleInfo, clientThreadId, periodNanoseconds);
Phil Burkc0c70e32017-02-09 13:18:38 -0800200}
201
jiabin5f787812023-03-02 20:42:43 +0000202aaudio_result_t AAudioBinderClient::unregisterAudioThread(const AAudioHandleInfo& streamHandleInfo,
Phil Burkc0c70e32017-02-09 13:18:38 -0800203 pid_t clientThreadId) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700204 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700205 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700206
jiabin5f787812023-03-02 20:42:43 +0000207 return service->unregisterAudioThread(streamHandleInfo, clientThreadId);
Phil Burkc0c70e32017-02-09 13:18:38 -0800208}
jiabinf7f06152021-11-22 18:10:14 +0000209
jiabin5f787812023-03-02 20:42:43 +0000210aaudio_result_t AAudioBinderClient::exitStandby(const AAudioHandleInfo& streamHandleInfo,
jiabinf7f06152021-11-22 18:10:14 +0000211 AudioEndpointParcelable &endpointOut) {
212 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
213 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
214
jiabin5f787812023-03-02 20:42:43 +0000215 return service->exitStandby(streamHandleInfo, endpointOut);
jiabinf7f06152021-11-22 18:10:14 +0000216}