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 | |
| 17 | #include <assert.h> |
| 18 | #include <map> |
| 19 | #include <mutex> |
| 20 | #include <utils/Singleton.h> |
| 21 | |
| 22 | #include "AAudioEndpointManager.h" |
| 23 | #include "AAudioServiceEndpoint.h" |
| 24 | |
| 25 | using namespace android; |
| 26 | using namespace aaudio; |
| 27 | |
| 28 | ANDROID_SINGLETON_STATIC_INSTANCE(AAudioEndpointManager); |
| 29 | |
| 30 | AAudioEndpointManager::AAudioEndpointManager() |
| 31 | : Singleton<AAudioEndpointManager>() { |
| 32 | } |
| 33 | |
| 34 | AAudioServiceEndpoint *AAudioEndpointManager::findEndpoint(AAudioService &audioService, int32_t deviceId, |
| 35 | aaudio_direction_t direction) { |
| 36 | AAudioServiceEndpoint *endpoint = nullptr; |
| 37 | std::lock_guard<std::mutex> lock(mLock); |
| 38 | switch (direction) { |
| 39 | case AAUDIO_DIRECTION_INPUT: |
| 40 | endpoint = mInputs[deviceId]; |
| 41 | break; |
| 42 | case AAUDIO_DIRECTION_OUTPUT: |
| 43 | endpoint = mOutputs[deviceId]; |
| 44 | break; |
| 45 | default: |
| 46 | assert(false); // There are only two possible directions. |
| 47 | break; |
| 48 | } |
| 49 | |
| 50 | // If we can't find an existing one then open one. |
| 51 | ALOGD("AAudioEndpointManager::findEndpoint(), found %p", endpoint); |
| 52 | if (endpoint == nullptr) { |
| 53 | endpoint = new AAudioServiceEndpoint(audioService); |
| 54 | if (endpoint->open(deviceId, direction) != AAUDIO_OK) { |
| 55 | ALOGD("AAudioEndpointManager::findEndpoint(), open failed"); |
| 56 | delete endpoint; |
| 57 | endpoint = nullptr; |
| 58 | } else { |
| 59 | switch(direction) { |
| 60 | case AAUDIO_DIRECTION_INPUT: |
| 61 | mInputs[deviceId] = endpoint; |
| 62 | break; |
| 63 | case AAUDIO_DIRECTION_OUTPUT: |
| 64 | mOutputs[deviceId] = endpoint; |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | return endpoint; |
| 70 | } |
| 71 | |
| 72 | // FIXME add reference counter for serviceEndpoints and removed on last use. |
| 73 | |
| 74 | void AAudioEndpointManager::removeEndpoint(AAudioServiceEndpoint *serviceEndpoint) { |
| 75 | aaudio_direction_t direction = serviceEndpoint->getDirection(); |
| 76 | int32_t deviceId = serviceEndpoint->getDeviceId(); |
| 77 | |
| 78 | std::lock_guard<std::mutex> lock(mLock); |
| 79 | switch(direction) { |
| 80 | case AAUDIO_DIRECTION_INPUT: |
| 81 | mInputs.erase(deviceId); |
| 82 | break; |
| 83 | case AAUDIO_DIRECTION_OUTPUT: |
| 84 | mOutputs.erase(deviceId); |
| 85 | break; |
| 86 | } |
| 87 | } |