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) { |
| 100 | ALOGE("getAAudioService: linkToDeath(mAAudioClient = %p) returned %d", |
| 101 | mAAudioClient.get(), status); |
| 102 | } |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 103 | mAAudioService = interface_cast<IAAudioService>(binder); |
| 104 | needToRegister = true; |
| 105 | // Make sure callbacks can be received by mAAudioClient |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 106 | ProcessState::self()->startThreadPool(); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 107 | } else { |
| 108 | ALOGE("AAudioBinderClient could not connect to %s", AAUDIO_SERVICE_NAME); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 109 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 110 | } |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 111 | aaudioService = mAAudioService; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 112 | } |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 113 | // Do this outside the mutex lock. |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 114 | if (needToRegister && aaudioService.get() != nullptr) { // new client? |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 115 | aaudioService->registerClient(mAAudioClient); |
| 116 | } |
| 117 | return aaudioService; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 120 | void AAudioBinderClient::dropAAudioService() { |
| 121 | Mutex::Autolock _l(mServiceLock); |
| 122 | mAAudioService.clear(); // force a reconnect |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 123 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 124 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 125 | /** |
| 126 | * @param request info needed to create the stream |
| 127 | * @param configuration contains information about the created stream |
| 128 | * @return handle to the stream or a negative error |
| 129 | */ |
| 130 | aaudio_handle_t AAudioBinderClient::openStream(const AAudioStreamRequest &request, |
| 131 | AAudioStreamConfiguration &configurationOutput) { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 132 | aaudio_handle_t stream; |
| 133 | for (int i = 0; i < 2; i++) { |
| 134 | const sp<IAAudioService> &service = getAAudioService(); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 135 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 136 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 137 | stream = service->openStream(request, configurationOutput); |
| 138 | |
| 139 | if (stream == AAUDIO_ERROR_NO_SERVICE) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 140 | ALOGE("openStream lost connection to AAudioService."); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 141 | dropAAudioService(); // force a reconnect |
| 142 | } else { |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | return stream; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | aaudio_result_t AAudioBinderClient::closeStream(aaudio_handle_t streamHandle) { |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 150 | const sp<IAAudioService> service = getAAudioService(); |
| 151 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 152 | return service->closeStream(streamHandle); |
| 153 | } |
| 154 | |
| 155 | /* Get an immutable description of the in-memory queues |
| 156 | * used to communicate with the underlying HAL or Service. |
| 157 | */ |
| 158 | aaudio_result_t AAudioBinderClient::getStreamDescription(aaudio_handle_t streamHandle, |
| 159 | AudioEndpointParcelable &parcelable) { |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 160 | const sp<IAAudioService> service = getAAudioService(); |
| 161 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 162 | return service->getStreamDescription(streamHandle, parcelable); |
| 163 | } |
| 164 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 165 | aaudio_result_t AAudioBinderClient::startStream(aaudio_handle_t streamHandle) { |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 166 | const sp<IAAudioService> service = getAAudioService(); |
| 167 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 168 | return service->startStream(streamHandle); |
| 169 | } |
| 170 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 171 | aaudio_result_t AAudioBinderClient::pauseStream(aaudio_handle_t streamHandle) { |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 172 | const sp<IAAudioService> service = getAAudioService(); |
| 173 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 174 | return service->pauseStream(streamHandle); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 177 | aaudio_result_t AAudioBinderClient::stopStream(aaudio_handle_t streamHandle) { |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 178 | const sp<IAAudioService> service = getAAudioService(); |
| 179 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 180 | return service->stopStream(streamHandle); |
| 181 | } |
| 182 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 183 | aaudio_result_t AAudioBinderClient::flushStream(aaudio_handle_t streamHandle) { |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 184 | const sp<IAAudioService> service = getAAudioService(); |
| 185 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 186 | return service->flushStream(streamHandle); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Manage the specified thread as a low latency audio thread. |
| 191 | */ |
| 192 | aaudio_result_t AAudioBinderClient::registerAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 193 | pid_t clientThreadId, |
| 194 | int64_t periodNanoseconds) { |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 195 | const sp<IAAudioService> service = getAAudioService(); |
| 196 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 197 | return service->registerAudioThread(streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 198 | clientThreadId, |
| 199 | periodNanoseconds); |
| 200 | } |
| 201 | |
| 202 | aaudio_result_t AAudioBinderClient::unregisterAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 203 | pid_t clientThreadId) { |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 204 | const sp<IAAudioService> service = getAAudioService(); |
| 205 | if (service.get() == nullptr) return AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 206 | return service->unregisterAudioThread(streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 207 | clientThreadId); |
| 208 | } |