| 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 | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 39 | using android::IInterface; | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 40 | using android::Mutex; | 
| Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 41 | using android::ProcessState; | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 42 | using android::sp; | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 43 | using android::status_t; | 
| Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 44 | using android::wp; | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 45 | using android::binder::Status; | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 46 |  | 
|  | 47 | using namespace aaudio; | 
|  | 48 |  | 
| Phil Burk | 9b3f8ef | 2017-05-16 11:37:43 -0700 | [diff] [blame] | 49 | ANDROID_SINGLETON_STATIC_INSTANCE(AAudioBinderClient); | 
|  | 50 |  | 
| Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 51 | // If we don't keep a strong pointer here then this singleton can get deleted! | 
|  | 52 | android::sp<AAudioBinderClient> gKeepBinderClient; | 
|  | 53 |  | 
| Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 54 | AAudioBinderClient::AAudioBinderClient() | 
|  | 55 | : AAudioServiceInterface() | 
|  | 56 | , Singleton<AAudioBinderClient>() { | 
| Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 57 | gKeepBinderClient = this; // so this singleton won't get deleted | 
| Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 58 | mAAudioClient = new AAudioClient(this); | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 59 | ALOGV("%s - this = %p, created mAAudioClient = %p", __func__, this, mAAudioClient.get()); | 
| Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 60 | } | 
|  | 61 |  | 
|  | 62 | AAudioBinderClient::~AAudioBinderClient() { | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 63 | ALOGV("%s - destroying %p", __func__, this); | 
| Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 64 | Mutex::Autolock _l(mServiceLock); | 
| Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 65 | } | 
|  | 66 |  | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 67 | // 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-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 70 | std::shared_ptr<AAudioServiceInterface> AAudioBinderClient::getAAudioService() { | 
|  | 71 | std::shared_ptr<AAudioServiceInterface> result; | 
| Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 72 | sp<IAAudioService> aaudioService; | 
|  | 73 | bool needToRegister = false; | 
|  | 74 | { | 
|  | 75 | Mutex::Autolock _l(mServiceLock); | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 76 | if (mAdapter == nullptr) { | 
| Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 77 | 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 Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 83 | if (binder.get() != nullptr) { | 
| Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 84 | break; | 
|  | 85 | } | 
|  | 86 | } while (retries-- > 0); | 
|  | 87 |  | 
| Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 88 | if (binder.get() != nullptr) { | 
| Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 89 | // Ask for notification if the service dies. | 
|  | 90 | status_t status = binder->linkToDeath(mAAudioClient); | 
| Phil Burk | c7abac4 | 2017-07-17 11:13:37 -0700 | [diff] [blame] | 91 | // TODO review what we should do if this fails | 
|  | 92 | if (status != NO_ERROR) { | 
| Phil Burk | 7ba4655 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 93 | ALOGE("%s() - linkToDeath() returned %d", __func__, status); | 
| Phil Burk | c7abac4 | 2017-07-17 11:13:37 -0700 | [diff] [blame] | 94 | } | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 95 | aaudioService = interface_cast<IAAudioService>(binder); | 
|  | 96 | mAdapter.reset(new Adapter(aaudioService, mAAudioClient)); | 
| Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 97 | needToRegister = true; | 
|  | 98 | // Make sure callbacks can be received by mAAudioClient | 
| Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 99 | ProcessState::self()->startThreadPool(); | 
| Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 100 | } else { | 
|  | 101 | ALOGE("AAudioBinderClient could not connect to %s", AAUDIO_SERVICE_NAME); | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 102 | } | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 103 | } | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 104 | result = mAdapter; | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 105 | } | 
| Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 106 | // Do this outside the mutex lock. | 
| Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 107 | if (needToRegister && aaudioService.get() != nullptr) { // new client? | 
| Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 108 | aaudioService->registerClient(mAAudioClient); | 
|  | 109 | } | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 110 | return result; | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 111 | } | 
|  | 112 |  | 
| Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 113 | void AAudioBinderClient::dropAAudioService() { | 
|  | 114 | Mutex::Autolock _l(mServiceLock); | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 115 | mAdapter.reset(); | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 116 | } | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 117 |  | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 118 | /** | 
|  | 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 | */ | 
|  | 123 | aaudio_handle_t AAudioBinderClient::openStream(const AAudioStreamRequest &request, | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 124 | AAudioStreamConfiguration &configuration) { | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 125 | aaudio_handle_t stream; | 
|  | 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(); | 
| Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 128 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 129 |  | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 130 | stream = service->openStream(request, configuration); | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 131 |  | 
|  | 132 | if (stream == AAUDIO_ERROR_NO_SERVICE) { | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 133 | ALOGE("openStream lost connection to AAudioService."); | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 134 | dropAAudioService(); // force a reconnect | 
|  | 135 | } else { | 
|  | 136 | break; | 
|  | 137 | } | 
|  | 138 | } | 
|  | 139 | return stream; | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 140 | } | 
|  | 141 |  | 
|  | 142 | aaudio_result_t AAudioBinderClient::closeStream(aaudio_handle_t streamHandle) { | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 143 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); | 
| Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 144 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 145 |  | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 146 | 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 | */ | 
|  | 152 | aaudio_result_t AAudioBinderClient::getStreamDescription(aaudio_handle_t streamHandle, | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 153 | AudioEndpointParcelable& endpointOut) { | 
|  | 154 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); | 
| Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 155 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 156 |  | 
|  | 157 | return service->getStreamDescription(streamHandle, endpointOut); | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 158 | } | 
|  | 159 |  | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 160 | aaudio_result_t AAudioBinderClient::startStream(aaudio_handle_t streamHandle) { | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 161 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); | 
| Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 162 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 163 |  | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 164 | return service->startStream(streamHandle); | 
|  | 165 | } | 
|  | 166 |  | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 167 | aaudio_result_t AAudioBinderClient::pauseStream(aaudio_handle_t streamHandle) { | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 168 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); | 
| Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 169 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 170 |  | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 171 | return service->pauseStream(streamHandle); | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 172 | } | 
|  | 173 |  | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 174 | aaudio_result_t AAudioBinderClient::stopStream(aaudio_handle_t streamHandle) { | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 175 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); | 
| Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 176 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 177 |  | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 178 | return service->stopStream(streamHandle); | 
|  | 179 | } | 
|  | 180 |  | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 181 | aaudio_result_t AAudioBinderClient::flushStream(aaudio_handle_t streamHandle) { | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 182 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); | 
| Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 183 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 184 |  | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 185 | return service->flushStream(streamHandle); | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 186 | } | 
|  | 187 |  | 
|  | 188 | /** | 
|  | 189 | * Manage the specified thread as a low latency audio thread. | 
|  | 190 | */ | 
|  | 191 | aaudio_result_t AAudioBinderClient::registerAudioThread(aaudio_handle_t streamHandle, | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 192 | pid_t clientThreadId, | 
|  | 193 | int64_t periodNanoseconds) { | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 194 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); | 
| Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 195 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 196 |  | 
|  | 197 | return service->registerAudioThread(streamHandle, clientThreadId, periodNanoseconds); | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 198 | } | 
|  | 199 |  | 
|  | 200 | aaudio_result_t AAudioBinderClient::unregisterAudioThread(aaudio_handle_t streamHandle, | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 201 | pid_t clientThreadId) { | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 202 | std::shared_ptr<AAudioServiceInterface> service = getAAudioService(); | 
| Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 203 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; | 
| Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 204 |  | 
|  | 205 | return service->unregisterAudioThread(streamHandle, clientThreadId); | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 206 | } |