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