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> |
| 22 | #include <map> |
| 23 | #include <mutex> |
| 24 | #include <utils/Singleton.h> |
| 25 | |
| 26 | #include "AAudioEndpointManager.h" |
| 27 | #include "AAudioServiceEndpoint.h" |
| 28 | |
| 29 | using namespace android; |
| 30 | using namespace aaudio; |
| 31 | |
| 32 | ANDROID_SINGLETON_STATIC_INSTANCE(AAudioEndpointManager); |
| 33 | |
| 34 | AAudioEndpointManager::AAudioEndpointManager() |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame^] | 35 | : Singleton<AAudioEndpointManager>() |
| 36 | , mInputs() |
| 37 | , mOutputs() { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame^] | 40 | AAudioServiceEndpoint *AAudioEndpointManager::openEndpoint(AAudioService &audioService, int32_t deviceId, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 41 | aaudio_direction_t direction) { |
| 42 | AAudioServiceEndpoint *endpoint = nullptr; |
| 43 | std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame^] | 44 | |
| 45 | // Try to find an existing endpoint. |
| 46 | ALOGD("AAudioEndpointManager::openEndpoint(), device = %d, dir = %d", deviceId, direction); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 47 | switch (direction) { |
| 48 | case AAUDIO_DIRECTION_INPUT: |
| 49 | endpoint = mInputs[deviceId]; |
| 50 | break; |
| 51 | case AAUDIO_DIRECTION_OUTPUT: |
| 52 | endpoint = mOutputs[deviceId]; |
| 53 | break; |
| 54 | default: |
| 55 | assert(false); // There are only two possible directions. |
| 56 | break; |
| 57 | } |
| 58 | |
| 59 | // If we can't find an existing one then open one. |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame^] | 60 | ALOGD("AAudioEndpointManager::openEndpoint(), found %p", endpoint); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 61 | if (endpoint == nullptr) { |
| 62 | endpoint = new AAudioServiceEndpoint(audioService); |
| 63 | if (endpoint->open(deviceId, direction) != AAUDIO_OK) { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame^] | 64 | ALOGE("AAudioEndpointManager::findEndpoint(), open failed"); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 65 | delete endpoint; |
| 66 | endpoint = nullptr; |
| 67 | } else { |
| 68 | switch(direction) { |
| 69 | case AAUDIO_DIRECTION_INPUT: |
| 70 | mInputs[deviceId] = endpoint; |
| 71 | break; |
| 72 | case AAUDIO_DIRECTION_OUTPUT: |
| 73 | mOutputs[deviceId] = endpoint; |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame^] | 78 | |
| 79 | if (endpoint != nullptr) { |
| 80 | // Increment the reference count under this lock. |
| 81 | endpoint->setReferenceCount(endpoint->getReferenceCount() + 1); |
| 82 | } |
| 83 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 84 | return endpoint; |
| 85 | } |
| 86 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame^] | 87 | void AAudioEndpointManager::closeEndpoint(AAudioServiceEndpoint *serviceEndpoint) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 88 | std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame^] | 89 | if (serviceEndpoint == nullptr) { |
| 90 | return; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 91 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame^] | 92 | |
| 93 | // Decrement the reference count under this lock. |
| 94 | int32_t newRefCount = serviceEndpoint->getReferenceCount() - 1; |
| 95 | serviceEndpoint->setReferenceCount(newRefCount); |
| 96 | if (newRefCount <= 0) { |
| 97 | aaudio_direction_t direction = serviceEndpoint->getDirection(); |
| 98 | int32_t deviceId = serviceEndpoint->getDeviceId(); |
| 99 | |
| 100 | switch (direction) { |
| 101 | case AAUDIO_DIRECTION_INPUT: |
| 102 | mInputs.erase(deviceId); |
| 103 | break; |
| 104 | case AAUDIO_DIRECTION_OUTPUT: |
| 105 | mOutputs.erase(deviceId); |
| 106 | break; |
| 107 | } |
| 108 | serviceEndpoint->close(); |
| 109 | delete serviceEndpoint; |
| 110 | } |
| 111 | } |