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 | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 55 | |
| 56 | // If we can't find an existing one then open a new one. |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame^] | 57 | if (endpoint != nullptr) { |
| 58 | ALOGD("AAudioEndpointManager::openEndpoint(), found %p for device = %d, dir = %d", |
| 59 | endpoint, deviceId, (int)direction); |
| 60 | |
| 61 | } else { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 62 | if (direction == AAUDIO_DIRECTION_INPUT) { |
| 63 | AAudioServiceEndpointCapture *capture = new AAudioServiceEndpointCapture(audioService); |
| 64 | if (capture->open(deviceId) != AAUDIO_OK) { |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame^] | 65 | ALOGE("AAudioEndpointManager::openEndpoint(), open input failed"); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 66 | delete capture; |
| 67 | } else { |
| 68 | mInputs[deviceId] = capture; |
| 69 | endpoint = capture; |
| 70 | } |
| 71 | } else if (direction == AAUDIO_DIRECTION_OUTPUT) { |
| 72 | AAudioServiceEndpointPlay *player = new AAudioServiceEndpointPlay(audioService); |
| 73 | if (player->open(deviceId) != AAUDIO_OK) { |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame^] | 74 | ALOGE("AAudioEndpointManager::openEndpoint(), open output failed"); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 75 | delete player; |
| 76 | } else { |
| 77 | mOutputs[deviceId] = player; |
| 78 | endpoint = player; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame^] | 81 | ALOGD("AAudioEndpointManager::openEndpoint(), created %p for device = %d, dir = %d", |
| 82 | endpoint, deviceId, (int)direction); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 83 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 84 | |
| 85 | if (endpoint != nullptr) { |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame^] | 86 | ALOGD("AAudioEndpointManager::openEndpoint(), sampleRate = %d, framesPerBurst = %d", |
| 87 | endpoint->getSampleRate(), endpoint->getFramesPerBurst()); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 88 | // Increment the reference count under this lock. |
| 89 | endpoint->setReferenceCount(endpoint->getReferenceCount() + 1); |
| 90 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 91 | return endpoint; |
| 92 | } |
| 93 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 94 | void AAudioEndpointManager::closeEndpoint(AAudioServiceEndpoint *serviceEndpoint) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 95 | std::lock_guard<std::mutex> lock(mLock); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 96 | if (serviceEndpoint == nullptr) { |
| 97 | return; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 98 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 99 | |
| 100 | // Decrement the reference count under this lock. |
| 101 | int32_t newRefCount = serviceEndpoint->getReferenceCount() - 1; |
| 102 | serviceEndpoint->setReferenceCount(newRefCount); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame^] | 103 | ALOGD("AAudioEndpointManager::closeEndpoint(%p) newRefCount = %d", |
| 104 | serviceEndpoint, newRefCount); |
| 105 | |
| 106 | // If no longer in use then close and delete it. |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 107 | if (newRefCount <= 0) { |
| 108 | aaudio_direction_t direction = serviceEndpoint->getDirection(); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame^] | 109 | // Track endpoints based on requested deviceId because UNSPECIFIED |
| 110 | // can change to a specific device after opening. |
| 111 | int32_t deviceId = serviceEndpoint->getRequestedDeviceId(); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 112 | |
| 113 | switch (direction) { |
| 114 | case AAUDIO_DIRECTION_INPUT: |
| 115 | mInputs.erase(deviceId); |
| 116 | break; |
| 117 | case AAUDIO_DIRECTION_OUTPUT: |
| 118 | mOutputs.erase(deviceId); |
| 119 | break; |
| 120 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 121 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 122 | serviceEndpoint->close(); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame^] | 123 | ALOGD("AAudioEndpointManager::closeEndpoint() delete %p for device %d, dir = %d", |
| 124 | serviceEndpoint, deviceId, (int)direction); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 125 | delete serviceEndpoint; |
| 126 | } |
| 127 | } |