Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 1 | /* |
| 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 Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 18 | #define LOG_TAG "AAudioBinderClient" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 |
| 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include <binder/IServiceManager.h> |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 23 | #include <binder/ProcessState.h> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 24 | #include <utils/Mutex.h> |
| 25 | #include <utils/RefBase.h> |
Phil Burk | 9b3f8ef | 2017-05-16 11:37:43 -0700 | [diff] [blame] | 26 | #include <utils/Singleton.h> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 27 | #include <aaudio/AAudio.h> |
| 28 | |
| 29 | #include "AudioEndpointParcelable.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 30 | |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 31 | #include "binding/AAudioBinderClient.h" |
| 32 | |
| 33 | #define AAUDIO_SERVICE_NAME "media.aaudio" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 34 | |
| 35 | using android::String16; |
| 36 | using android::IServiceManager; |
| 37 | using android::defaultServiceManager; |
| 38 | using android::interface_cast; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 39 | using android::Mutex; |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 40 | using android::ProcessState; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 41 | using android::sp; |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 42 | using android::status_t; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 43 | |
| 44 | using namespace aaudio; |
| 45 | |
Phil Burk | 9b3f8ef | 2017-05-16 11:37:43 -0700 | [diff] [blame] | 46 | ANDROID_SINGLETON_STATIC_INSTANCE(AAudioBinderClient); |
| 47 | |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 48 | // If we don't keep a strong pointer here then this singleton can get deleted! |
| 49 | android::sp<AAudioBinderClient> gKeepBinderClient; |
| 50 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 51 | AAudioBinderClient::AAudioBinderClient() |
| 52 | : AAudioServiceInterface() |
| 53 | , Singleton<AAudioBinderClient>() { |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 54 | gKeepBinderClient = this; // so this singleton won't get deleted |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 55 | mAAudioClient = new AAudioClient(this); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 56 | ALOGV("%s - this = %p, created mAAudioClient = %p", __func__, this, mAAudioClient.get()); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | AAudioBinderClient::~AAudioBinderClient() { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 60 | ALOGV("%s - destroying %p", __func__, this); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 61 | Mutex::Autolock _l(mServiceLock); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 64 | // 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-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 67 | std::shared_ptr<AAudioServiceInterface> AAudioBinderClient::getAAudioService() { |
| 68 | std::shared_ptr<AAudioServiceInterface> result; |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 69 | sp<IAAudioService> aaudioService; |
| 70 | bool needToRegister = false; |
| 71 | { |
| 72 | Mutex::Autolock _l(mServiceLock); |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 73 | if (mAdapter == nullptr) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 74 | 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 Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 80 | if (binder.get() != nullptr) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 81 | break; |
| 82 | } |
| 83 | } while (retries-- > 0); |
| 84 | |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 85 | if (binder.get() != nullptr) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 86 | // Ask for notification if the service dies. |
| 87 | status_t status = binder->linkToDeath(mAAudioClient); |
Phil Burk | c7abac4 | 2017-07-17 11:13:37 -0700 | [diff] [blame] | 88 | // TODO review what we should do if this fails |
| 89 | if (status != NO_ERROR) { |
Phil Burk | 7ba4655 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 90 | ALOGE("%s() - linkToDeath() returned %d", __func__, status); |
Phil Burk | c7abac4 | 2017-07-17 11:13:37 -0700 | [diff] [blame] | 91 | } |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 92 | aaudioService = interface_cast<IAAudioService>(binder); |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 93 | mAdapter = std::make_shared<Adapter>( |
| 94 | aaudioService, mAAudioClient, mAAudioClient->getServiceLifetimeId()); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 95 | needToRegister = true; |
| 96 | // Make sure callbacks can be received by mAAudioClient |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 97 | ProcessState::self()->startThreadPool(); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 98 | } else { |
| 99 | ALOGE("AAudioBinderClient could not connect to %s", AAUDIO_SERVICE_NAME); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 100 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 101 | } |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 102 | result = mAdapter; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 103 | } |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 104 | // Do this outside the mutex lock. |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 105 | if (needToRegister && aaudioService.get() != nullptr) { // new client? |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 106 | aaudioService->registerClient(mAAudioClient); |
| 107 | } |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 108 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 111 | void AAudioBinderClient::dropAAudioService() { |
| 112 | Mutex::Autolock _l(mServiceLock); |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 113 | mAdapter.reset(); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 114 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 115 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 116 | /** |
| 117 | * @param request info needed to create the stream |
| 118 | * @param configuration contains information about the created stream |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 119 | * @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 Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 123 | */ |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 124 | AAudioHandleInfo AAudioBinderClient::openStream(const AAudioStreamRequest &request, |
| 125 | AAudioStreamConfiguration &configuration) { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 126 | for (int i = 0; i < 2; i++) { |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 127 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 128 | if (service.get() == nullptr) { |
| 129 | return {}; |
| 130 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 131 | |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 132 | AAudioHandleInfo handleInfo = service->openStream(request, configuration); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 133 | |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 134 | if (handleInfo.getHandle() == AAUDIO_ERROR_NO_SERVICE) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 135 | ALOGE("openStream lost connection to AAudioService."); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 136 | dropAAudioService(); // force a reconnect |
| 137 | } else { |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 138 | return handleInfo; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 141 | return {}; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 142 | } |
| 143 | |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 144 | aaudio_result_t AAudioBinderClient::closeStream(const AAudioHandleInfo& streamHandleInfo) { |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 145 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 146 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 147 | |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 148 | return service->closeStream(streamHandleInfo); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* Get an immutable description of the in-memory queues |
| 152 | * used to communicate with the underlying HAL or Service. |
| 153 | */ |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 154 | aaudio_result_t AAudioBinderClient::getStreamDescription(const AAudioHandleInfo& streamHandleInfo, |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 155 | AudioEndpointParcelable& endpointOut) { |
| 156 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 157 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 158 | |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 159 | return service->getStreamDescription(streamHandleInfo, endpointOut); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 160 | } |
| 161 | |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 162 | aaudio_result_t AAudioBinderClient::startStream(const AAudioHandleInfo& streamHandleInfo) { |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 163 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 164 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 165 | |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 166 | return service->startStream(streamHandleInfo); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 167 | } |
| 168 | |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 169 | aaudio_result_t AAudioBinderClient::pauseStream(const AAudioHandleInfo& streamHandleInfo) { |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 170 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 171 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 172 | |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 173 | return service->pauseStream(streamHandleInfo); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 174 | } |
| 175 | |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 176 | aaudio_result_t AAudioBinderClient::stopStream(const AAudioHandleInfo& streamHandleInfo) { |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 177 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 178 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 179 | |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 180 | return service->stopStream(streamHandleInfo); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 181 | } |
| 182 | |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 183 | aaudio_result_t AAudioBinderClient::flushStream(const AAudioHandleInfo& streamHandleInfo) { |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 184 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 185 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 186 | |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 187 | return service->flushStream(streamHandleInfo); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Manage the specified thread as a low latency audio thread. |
| 192 | */ |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 193 | aaudio_result_t AAudioBinderClient::registerAudioThread(const AAudioHandleInfo& streamHandleInfo, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 194 | pid_t clientThreadId, |
| 195 | int64_t periodNanoseconds) { |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 196 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 197 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 198 | |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 199 | return service->registerAudioThread(streamHandleInfo, clientThreadId, periodNanoseconds); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 200 | } |
| 201 | |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 202 | aaudio_result_t AAudioBinderClient::unregisterAudioThread(const AAudioHandleInfo& streamHandleInfo, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 203 | pid_t clientThreadId) { |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 204 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 205 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 206 | |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 207 | return service->unregisterAudioThread(streamHandleInfo, clientThreadId); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 208 | } |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 209 | |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 210 | aaudio_result_t AAudioBinderClient::exitStandby(const AAudioHandleInfo& streamHandleInfo, |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 211 | AudioEndpointParcelable &endpointOut) { |
| 212 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); |
| 213 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
| 214 | |
jiabin | 5f78781 | 2023-03-02 20:42:43 +0000 | [diff] [blame] | 215 | return service->exitStandby(streamHandleInfo, endpointOut); |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 216 | } |