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 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AAudioEndpointManager" |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 21 | #include <assert.h> |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 22 | #include <functional> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 23 | #include <map> |
| 24 | #include <mutex> |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 25 | #include <sstream> |
| 26 | #include <utility/AAudioUtilities.h> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 27 | |
| 28 | #include "AAudioEndpointManager.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 29 | |
| 30 | using namespace android; |
| 31 | using namespace aaudio; |
| 32 | |
| 33 | ANDROID_SINGLETON_STATIC_INSTANCE(AAudioEndpointManager); |
| 34 | |
| 35 | AAudioEndpointManager::AAudioEndpointManager() |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 36 | : Singleton<AAudioEndpointManager>() |
| 37 | , mInputs() |
| 38 | , mOutputs() { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 41 | std::string AAudioEndpointManager::dump() const { |
| 42 | std::stringstream result; |
| 43 | const bool isLocked = AAudio_tryUntilTrue( |
| 44 | [this]()->bool { return mLock.try_lock(); } /* f */, |
| 45 | 50 /* times */, |
| 46 | 20 /* sleepMs */); |
| 47 | if (!isLocked) { |
| 48 | result << "EndpointManager may be deadlocked\n"; |
| 49 | } |
| 50 | |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 51 | result << "AAudioEndpointManager:" << "\n"; |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 52 | size_t inputs = mInputs.size(); |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 53 | result << "Input Endpoints: " << inputs << "\n"; |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 54 | for (const auto &input : mInputs) { |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 55 | result << " Input: " << input->dump() << "\n"; |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | size_t outputs = mOutputs.size(); |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 59 | result << "Output Endpoints: " << outputs << "\n"; |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 60 | for (const auto &output : mOutputs) { |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 61 | result << " Output: " << output->dump() << "\n"; |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | if (isLocked) { |
| 65 | mLock.unlock(); |
| 66 | } |
| 67 | return result.str(); |
| 68 | } |
| 69 | |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 70 | AAudioServiceEndpoint *AAudioEndpointManager::openEndpoint(AAudioService &audioService, |
| 71 | const AAudioStreamConfiguration& configuration, aaudio_direction_t direction) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 72 | AAudioServiceEndpoint *endpoint = nullptr; |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 73 | AAudioServiceEndpointCapture *capture = nullptr; |
| 74 | AAudioServiceEndpointPlay *player = nullptr; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 75 | std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 76 | |
| 77 | // Try to find an existing endpoint. |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 78 | |
| 79 | |
| 80 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 81 | switch (direction) { |
| 82 | case AAUDIO_DIRECTION_INPUT: |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 83 | for (AAudioServiceEndpoint *ep : mInputs) { |
| 84 | if (ep->matches(configuration)) { |
| 85 | endpoint = ep; |
| 86 | break; |
| 87 | } |
| 88 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 89 | break; |
| 90 | case AAUDIO_DIRECTION_OUTPUT: |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 91 | for (AAudioServiceEndpoint *ep : mOutputs) { |
| 92 | if (ep->matches(configuration)) { |
| 93 | endpoint = ep; |
| 94 | break; |
| 95 | } |
| 96 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 97 | break; |
| 98 | default: |
| 99 | assert(false); // There are only two possible directions. |
| 100 | break; |
| 101 | } |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 102 | ALOGD("AAudioEndpointManager::openEndpoint(), found %p for device = %d, dir = %d", |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 103 | endpoint, configuration.getDeviceId(), (int)direction); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 104 | |
| 105 | // If we can't find an existing one then open a new one. |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 106 | if (endpoint == nullptr) { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 107 | // we must call openStream with audioserver identity |
| 108 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 109 | switch(direction) { |
| 110 | case AAUDIO_DIRECTION_INPUT: |
| 111 | capture = new AAudioServiceEndpointCapture(audioService); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 112 | endpoint = capture; |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 113 | break; |
| 114 | case AAUDIO_DIRECTION_OUTPUT: |
| 115 | player = new AAudioServiceEndpointPlay(audioService); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 116 | endpoint = player; |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 117 | break; |
| 118 | default: |
| 119 | break; |
| 120 | } |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 121 | |
Phil Burk | 9169294 | 2017-06-30 12:23:05 -0700 | [diff] [blame] | 122 | if (endpoint != nullptr) { |
| 123 | aaudio_result_t result = endpoint->open(configuration); |
| 124 | if (result != AAUDIO_OK) { |
| 125 | ALOGE("AAudioEndpointManager::findEndpoint(), open failed"); |
| 126 | delete endpoint; |
| 127 | endpoint = nullptr; |
| 128 | } else { |
| 129 | switch(direction) { |
| 130 | case AAUDIO_DIRECTION_INPUT: |
| 131 | mInputs.push_back(capture); |
| 132 | break; |
| 133 | case AAUDIO_DIRECTION_OUTPUT: |
| 134 | mOutputs.push_back(player); |
| 135 | break; |
| 136 | default: |
| 137 | break; |
| 138 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 139 | } |
| 140 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 141 | ALOGD("AAudioEndpointManager::openEndpoint(), created %p for device = %d, dir = %d", |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 142 | endpoint, configuration.getDeviceId(), (int)direction); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 143 | IPCThreadState::self()->restoreCallingIdentity(token); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 144 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 145 | |
| 146 | if (endpoint != nullptr) { |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 147 | ALOGD("AAudioEndpointManager::openEndpoint(), sampleRate = %d, framesPerBurst = %d", |
| 148 | endpoint->getSampleRate(), endpoint->getFramesPerBurst()); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 149 | // Increment the reference count under this lock. |
| 150 | endpoint->setReferenceCount(endpoint->getReferenceCount() + 1); |
| 151 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 152 | return endpoint; |
| 153 | } |
| 154 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 155 | void AAudioEndpointManager::closeEndpoint(AAudioServiceEndpoint *serviceEndpoint) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 156 | std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 157 | if (serviceEndpoint == nullptr) { |
| 158 | return; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 159 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 160 | |
| 161 | // Decrement the reference count under this lock. |
| 162 | int32_t newRefCount = serviceEndpoint->getReferenceCount() - 1; |
| 163 | serviceEndpoint->setReferenceCount(newRefCount); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 164 | ALOGD("AAudioEndpointManager::closeEndpoint(%p) newRefCount = %d", |
| 165 | serviceEndpoint, newRefCount); |
| 166 | |
| 167 | // If no longer in use then close and delete it. |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 168 | if (newRefCount <= 0) { |
| 169 | aaudio_direction_t direction = serviceEndpoint->getDirection(); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 170 | // Track endpoints based on requested deviceId because UNSPECIFIED |
| 171 | // can change to a specific device after opening. |
| 172 | int32_t deviceId = serviceEndpoint->getRequestedDeviceId(); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 173 | |
| 174 | switch (direction) { |
| 175 | case AAUDIO_DIRECTION_INPUT: |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 176 | mInputs.erase( |
| 177 | std::remove(mInputs.begin(), mInputs.end(), serviceEndpoint), mInputs.end()); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 178 | break; |
| 179 | case AAUDIO_DIRECTION_OUTPUT: |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 180 | mOutputs.erase( |
| 181 | std::remove(mOutputs.begin(), mOutputs.end(), serviceEndpoint), mOutputs.end()); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 182 | break; |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 183 | default: |
| 184 | break; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 185 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 186 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 187 | serviceEndpoint->close(); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 188 | ALOGD("AAudioEndpointManager::closeEndpoint() delete %p for device %d, dir = %d", |
| 189 | serviceEndpoint, deviceId, (int)direction); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 190 | delete serviceEndpoint; |
| 191 | } |
| 192 | } |