blob: 8e5facc5e0f83f4050543f04183c0bb77c1c0426 [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);
jiabind5bd06a2021-04-27 22:04:08 +000093 mAdapter = std::make_shared<Adapter>(aaudioService, mAAudioClient);
Phil Burk11e8d332017-05-24 09:59:02 -070094 needToRegister = true;
95 // Make sure callbacks can be received by mAAudioClient
Phil Burk2bc7c182017-08-28 11:45:01 -070096 ProcessState::self()->startThreadPool();
Phil Burk11e8d332017-05-24 09:59:02 -070097 } else {
98 ALOGE("AAudioBinderClient could not connect to %s", AAUDIO_SERVICE_NAME);
Phil Burkc0c70e32017-02-09 13:18:38 -080099 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800100 }
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700101 result = mAdapter;
Phil Burkc0c70e32017-02-09 13:18:38 -0800102 }
Phil Burk11e8d332017-05-24 09:59:02 -0700103 // Do this outside the mutex lock.
Phil Burk2bc7c182017-08-28 11:45:01 -0700104 if (needToRegister && aaudioService.get() != nullptr) { // new client?
Phil Burk11e8d332017-05-24 09:59:02 -0700105 aaudioService->registerClient(mAAudioClient);
106 }
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700107 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800108}
109
Phil Burk11e8d332017-05-24 09:59:02 -0700110void AAudioBinderClient::dropAAudioService() {
111 Mutex::Autolock _l(mServiceLock);
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700112 mAdapter.reset();
Phil Burk71f35bb2017-04-13 16:05:07 -0700113}
Phil Burkc0c70e32017-02-09 13:18:38 -0800114
Phil Burkc0c70e32017-02-09 13:18:38 -0800115/**
116* @param request info needed to create the stream
117* @param configuration contains information about the created stream
118* @return handle to the stream or a negative error
119*/
120aaudio_handle_t AAudioBinderClient::openStream(const AAudioStreamRequest &request,
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700121 AAudioStreamConfiguration &configuration) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700122 aaudio_handle_t stream;
123 for (int i = 0; i < 2; i++) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700124 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700125 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Phil Burkc0c70e32017-02-09 13:18:38 -0800126
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700127 stream = service->openStream(request, configuration);
Phil Burk71f35bb2017-04-13 16:05:07 -0700128
129 if (stream == AAUDIO_ERROR_NO_SERVICE) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700130 ALOGE("openStream lost connection to AAudioService.");
Phil Burk71f35bb2017-04-13 16:05:07 -0700131 dropAAudioService(); // force a reconnect
132 } else {
133 break;
134 }
135 }
136 return stream;
Phil Burkc0c70e32017-02-09 13:18:38 -0800137}
138
139aaudio_result_t AAudioBinderClient::closeStream(aaudio_handle_t streamHandle) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700140 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700141 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700142
Phil Burkc0c70e32017-02-09 13:18:38 -0800143 return service->closeStream(streamHandle);
144}
145
146/* Get an immutable description of the in-memory queues
147* used to communicate with the underlying HAL or Service.
148*/
149aaudio_result_t AAudioBinderClient::getStreamDescription(aaudio_handle_t streamHandle,
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700150 AudioEndpointParcelable& endpointOut) {
151 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700152 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700153
154 return service->getStreamDescription(streamHandle, endpointOut);
Phil Burkc0c70e32017-02-09 13:18:38 -0800155}
156
Phil Burkc0c70e32017-02-09 13:18:38 -0800157aaudio_result_t AAudioBinderClient::startStream(aaudio_handle_t streamHandle) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700158 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700159 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700160
Phil Burkc0c70e32017-02-09 13:18:38 -0800161 return service->startStream(streamHandle);
162}
163
Phil Burkc0c70e32017-02-09 13:18:38 -0800164aaudio_result_t AAudioBinderClient::pauseStream(aaudio_handle_t streamHandle) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700165 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700166 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700167
Phil Burk71f35bb2017-04-13 16:05:07 -0700168 return service->pauseStream(streamHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -0800169}
170
Phil Burk71f35bb2017-04-13 16:05:07 -0700171aaudio_result_t AAudioBinderClient::stopStream(aaudio_handle_t streamHandle) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700172 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700173 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700174
Phil Burk71f35bb2017-04-13 16:05:07 -0700175 return service->stopStream(streamHandle);
176}
177
Phil Burkc0c70e32017-02-09 13:18:38 -0800178aaudio_result_t AAudioBinderClient::flushStream(aaudio_handle_t streamHandle) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700179 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700180 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700181
Phil Burk71f35bb2017-04-13 16:05:07 -0700182 return service->flushStream(streamHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -0800183}
184
185/**
186* Manage the specified thread as a low latency audio thread.
187*/
188aaudio_result_t AAudioBinderClient::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800189 pid_t clientThreadId,
190 int64_t periodNanoseconds) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700191 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700192 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700193
194 return service->registerAudioThread(streamHandle, clientThreadId, periodNanoseconds);
Phil Burkc0c70e32017-02-09 13:18:38 -0800195}
196
197aaudio_result_t AAudioBinderClient::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800198 pid_t clientThreadId) {
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700199 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
Phil Burk2bc7c182017-08-28 11:45:01 -0700200 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700201
202 return service->unregisterAudioThread(streamHandle, clientThreadId);
Phil Burkc0c70e32017-02-09 13:18:38 -0800203}
jiabinf7f06152021-11-22 18:10:14 +0000204
205aaudio_result_t AAudioBinderClient::exitStandby(aaudio_handle_t streamHandle,
206 AudioEndpointParcelable &endpointOut) {
207 std::shared_ptr<AAudioServiceInterface> service = getAAudioService();
208 if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE;
209
210 return service->exitStandby(streamHandle, endpointOut);
211}