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 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 22 | #include <binder/IInterface.h> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 23 | #include <binder/IServiceManager.h> |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 24 | #include <binder/ProcessState.h> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 25 | #include <utils/Mutex.h> |
| 26 | #include <utils/RefBase.h> |
Phil Burk | 9b3f8ef | 2017-05-16 11:37:43 -0700 | [diff] [blame] | 27 | #include <utils/Singleton.h> |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 28 | #include <media/AudioSystem.h> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 29 | |
| 30 | #include <aaudio/AAudio.h> |
| 31 | |
| 32 | #include "AudioEndpointParcelable.h" |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 33 | #include "binding/AAudioBinderClient.h" |
| 34 | //#include "binding/AAudioStreamRequest.h" |
| 35 | //#include "binding/AAudioStreamConfiguration.h" |
| 36 | //#include "binding/IAAudioService.h" |
| 37 | //#include "binding/AAudioServiceMessage.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 38 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 39 | //#include "AAudioServiceInterface.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 40 | |
| 41 | using android::String16; |
| 42 | using android::IServiceManager; |
| 43 | using android::defaultServiceManager; |
| 44 | using android::interface_cast; |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 45 | using android::IInterface; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 46 | using android::IAAudioService; |
| 47 | using android::Mutex; |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 48 | using android::ProcessState; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 49 | using android::sp; |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 50 | using android::wp; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 51 | |
| 52 | using namespace aaudio; |
| 53 | |
Phil Burk | 9b3f8ef | 2017-05-16 11:37:43 -0700 | [diff] [blame] | 54 | ANDROID_SINGLETON_STATIC_INSTANCE(AAudioBinderClient); |
| 55 | |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 56 | // If we don't keep a strong pointer here then this singleton can get deleted! |
| 57 | android::sp<AAudioBinderClient> gKeepBinderClient; |
| 58 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 59 | AAudioBinderClient::AAudioBinderClient() |
| 60 | : AAudioServiceInterface() |
| 61 | , Singleton<AAudioBinderClient>() { |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 62 | gKeepBinderClient = this; // so this singleton won't get deleted |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 63 | mAAudioClient = new AAudioClient(this); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 64 | ALOGV("%s - this = %p, created mAAudioClient = %p", __func__, this, mAAudioClient.get()); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | AAudioBinderClient::~AAudioBinderClient() { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 68 | ALOGV("%s - destroying %p", __func__, this); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 69 | Mutex::Autolock _l(mServiceLock); |
| 70 | if (mAAudioService != 0) { |
| 71 | IInterface::asBinder(mAAudioService)->unlinkToDeath(mAAudioClient); |
| 72 | } |
| 73 | } |
| 74 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 75 | // TODO Share code with other service clients. |
| 76 | // Helper function to get access to the "AAudioService" service. |
| 77 | // This code was modeled after frameworks/av/media/libaudioclient/AudioSystem.cpp |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 78 | const sp<IAAudioService> AAudioBinderClient::getAAudioService() { |
| 79 | sp<IAAudioService> aaudioService; |
| 80 | bool needToRegister = false; |
| 81 | { |
| 82 | Mutex::Autolock _l(mServiceLock); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 83 | if (mAAudioService.get() == nullptr) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 84 | sp<IBinder> binder; |
| 85 | sp<IServiceManager> sm = defaultServiceManager(); |
| 86 | // Try several times to get the service. |
| 87 | int retries = 4; |
| 88 | do { |
| 89 | binder = sm->getService(String16(AAUDIO_SERVICE_NAME)); // This will wait a while. |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 90 | if (binder.get() != nullptr) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 91 | break; |
| 92 | } |
| 93 | } while (retries-- > 0); |
| 94 | |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 95 | if (binder.get() != nullptr) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 96 | // Ask for notification if the service dies. |
| 97 | status_t status = binder->linkToDeath(mAAudioClient); |
Phil Burk | c7abac4 | 2017-07-17 11:13:37 -0700 | [diff] [blame] | 98 | // TODO review what we should do if this fails |
| 99 | if (status != NO_ERROR) { |
Phil Burk | 7ba4655 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 100 | ALOGE("%s() - linkToDeath() returned %d", __func__, status); |
Phil Burk | c7abac4 | 2017-07-17 11:13:37 -0700 | [diff] [blame] | 101 | } |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 102 | mAAudioService = interface_cast<IAAudioService>(binder); |
| 103 | needToRegister = true; |
| 104 | // Make sure callbacks can be received by mAAudioClient |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 105 | ProcessState::self()->startThreadPool(); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 106 | } else { |
| 107 | ALOGE("AAudioBinderClient could not connect to %s", AAUDIO_SERVICE_NAME); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 108 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 109 | } |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 110 | aaudioService = mAAudioService; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 111 | } |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 112 | // Do this outside the mutex lock. |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 113 | if (needToRegister && aaudioService.get() != nullptr) { // new client? |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 114 | aaudioService->registerClient(mAAudioClient); |
| 115 | } |
| 116 | return aaudioService; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 119 | void AAudioBinderClient::dropAAudioService() { |
| 120 | Mutex::Autolock _l(mServiceLock); |
| 121 | mAAudioService.clear(); // force a reconnect |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 122 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 123 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 124 | /** |
| 125 | * @param request info needed to create the stream |
| 126 | * @param configuration contains information about the created stream |
| 127 | * @return handle to the stream or a negative error |
| 128 | */ |
| 129 | aaudio_handle_t AAudioBinderClient::openStream(const AAudioStreamRequest &request, |
| 130 | AAudioStreamConfiguration &configurationOutput) { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 131 | aaudio_handle_t stream; |
| 132 | for (int i = 0; i < 2; i++) { |
| 133 | const sp<IAAudioService> &service = getAAudioService(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 134 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 135 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 136 | stream = service->openStream(request, configurationOutput); |
| 137 | |
| 138 | if (stream == AAUDIO_ERROR_NO_SERVICE) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 139 | ALOGE("openStream lost connection to AAudioService."); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 140 | dropAAudioService(); // force a reconnect |
| 141 | } else { |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | return stream; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | aaudio_result_t AAudioBinderClient::closeStream(aaudio_handle_t streamHandle) { |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 149 | const sp<IAAudioService> service = getAAudioService(); |
| 150 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 151 | return service->closeStream(streamHandle); |
| 152 | } |
| 153 | |
| 154 | /* Get an immutable description of the in-memory queues |
| 155 | * used to communicate with the underlying HAL or Service. |
| 156 | */ |
| 157 | aaudio_result_t AAudioBinderClient::getStreamDescription(aaudio_handle_t streamHandle, |
| 158 | AudioEndpointParcelable &parcelable) { |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 159 | const sp<IAAudioService> service = getAAudioService(); |
| 160 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 161 | return service->getStreamDescription(streamHandle, parcelable); |
| 162 | } |
| 163 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 164 | aaudio_result_t AAudioBinderClient::startStream(aaudio_handle_t streamHandle) { |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 165 | const sp<IAAudioService> service = getAAudioService(); |
| 166 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 167 | return service->startStream(streamHandle); |
| 168 | } |
| 169 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 170 | aaudio_result_t AAudioBinderClient::pauseStream(aaudio_handle_t streamHandle) { |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 171 | const sp<IAAudioService> service = getAAudioService(); |
| 172 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 173 | return service->pauseStream(streamHandle); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 176 | aaudio_result_t AAudioBinderClient::stopStream(aaudio_handle_t streamHandle) { |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 177 | const sp<IAAudioService> service = getAAudioService(); |
| 178 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 179 | return service->stopStream(streamHandle); |
| 180 | } |
| 181 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 182 | aaudio_result_t AAudioBinderClient::flushStream(aaudio_handle_t streamHandle) { |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 183 | const sp<IAAudioService> service = getAAudioService(); |
| 184 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
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) { |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 194 | const sp<IAAudioService> service = getAAudioService(); |
| 195 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 196 | return service->registerAudioThread(streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 197 | clientThreadId, |
| 198 | periodNanoseconds); |
| 199 | } |
| 200 | |
| 201 | aaudio_result_t AAudioBinderClient::unregisterAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 202 | pid_t clientThreadId) { |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 203 | const sp<IAAudioService> service = getAAudioService(); |
| 204 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 205 | return service->unregisterAudioThread(streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 206 | clientThreadId); |
| 207 | } |