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 | d5bd06a | 2021-04-27 22:04:08 +0000 | [diff] [blame] | 93 | mAdapter = std::make_shared<Adapter>(aaudioService, mAAudioClient); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 94 | needToRegister = true; |
| 95 | // Make sure callbacks can be received by mAAudioClient |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 96 | ProcessState::self()->startThreadPool(); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 97 | } else { |
| 98 | ALOGE("AAudioBinderClient could not connect to %s", AAUDIO_SERVICE_NAME); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 99 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 100 | } |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 101 | result = mAdapter; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 102 | } |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 103 | // Do this outside the mutex lock. |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 104 | if (needToRegister && aaudioService.get() != nullptr) { // new client? |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 105 | aaudioService->registerClient(mAAudioClient); |
| 106 | } |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 107 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 110 | void AAudioBinderClient::dropAAudioService() { |
| 111 | Mutex::Autolock _l(mServiceLock); |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 112 | mAdapter.reset(); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 113 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 114 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 115 | /** |
| 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 | */ |
| 120 | aaudio_handle_t AAudioBinderClient::openStream(const AAudioStreamRequest &request, |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 121 | AAudioStreamConfiguration &configuration) { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 122 | aaudio_handle_t stream; |
| 123 | for (int i = 0; i < 2; i++) { |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 124 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 125 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 126 | |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 127 | stream = service->openStream(request, configuration); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 128 | |
| 129 | if (stream == AAUDIO_ERROR_NO_SERVICE) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 130 | ALOGE("openStream lost connection to AAudioService."); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 131 | dropAAudioService(); // force a reconnect |
| 132 | } else { |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | return stream; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | aaudio_result_t AAudioBinderClient::closeStream(aaudio_handle_t streamHandle) { |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 140 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 141 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 142 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 143 | 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 | */ |
| 149 | aaudio_result_t AAudioBinderClient::getStreamDescription(aaudio_handle_t streamHandle, |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 150 | AudioEndpointParcelable& endpointOut) { |
| 151 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 152 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 153 | |
| 154 | return service->getStreamDescription(streamHandle, endpointOut); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 157 | aaudio_result_t AAudioBinderClient::startStream(aaudio_handle_t streamHandle) { |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 158 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 159 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 160 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 161 | return service->startStream(streamHandle); |
| 162 | } |
| 163 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 164 | aaudio_result_t AAudioBinderClient::pauseStream(aaudio_handle_t streamHandle) { |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 165 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 166 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 167 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 168 | return service->pauseStream(streamHandle); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 171 | aaudio_result_t AAudioBinderClient::stopStream(aaudio_handle_t streamHandle) { |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 172 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 173 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 174 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 175 | return service->stopStream(streamHandle); |
| 176 | } |
| 177 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 178 | aaudio_result_t AAudioBinderClient::flushStream(aaudio_handle_t streamHandle) { |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 179 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 180 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 181 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 182 | return service->flushStream(streamHandle); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Manage the specified thread as a low latency audio thread. |
| 187 | */ |
| 188 | aaudio_result_t AAudioBinderClient::registerAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 189 | pid_t clientThreadId, |
| 190 | int64_t periodNanoseconds) { |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 191 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 192 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 193 | |
| 194 | return service->registerAudioThread(streamHandle, clientThreadId, periodNanoseconds); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | aaudio_result_t AAudioBinderClient::unregisterAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 198 | pid_t clientThreadId) { |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 199 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 200 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 201 | |
| 202 | return service->unregisterAudioThread(streamHandle, clientThreadId); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 203 | } |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame^] | 204 | |
| 205 | aaudio_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 | } |