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