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 #define LOG_TAG "AAudioService" 18 //#define LOG_NDEBUG 0 19 #include <utils/Log.h> 20 21 #include <assert.h> 22 #include <map> 23 #include <mutex> 24 25 #include "AAudioEndpointManager.h" 26 27 using namespace android; 28 using namespace aaudio; 29 30 ANDROID_SINGLETON_STATIC_INSTANCE(AAudioEndpointManager); 31 32 AAudioEndpointManager::AAudioEndpointManager() 33 : Singleton<AAudioEndpointManager>() 34 , mInputs() 35 , mOutputs() { 36 } 37 38 AAudioServiceEndpoint *AAudioEndpointManager::openEndpoint(AAudioService &audioService, int32_t deviceId, 39 aaudio_direction_t direction) { 40 AAudioServiceEndpoint *endpoint = nullptr; 41 std::lock_guard<std::mutex> lock(mLock); 42 43 // Try to find an existing endpoint. 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 } 55 ALOGD("AAudioEndpointManager::openEndpoint(), found %p for device = %d, dir = %d", 56 endpoint, deviceId, (int)direction); 57 58 // If we can't find an existing one then open a new one. 59 if (endpoint == nullptr) { 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; 77 } 78 } 79 80 } 81 82 if (endpoint != nullptr) { 83 // Increment the reference count under this lock. 84 endpoint->setReferenceCount(endpoint->getReferenceCount() + 1); 85 } 86 return endpoint; 87 } 88 89 void AAudioEndpointManager::closeEndpoint(AAudioServiceEndpoint *serviceEndpoint) { 90 std::lock_guard<std::mutex> lock(mLock); 91 if (serviceEndpoint == nullptr) { 92 return; 93 } 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 } 110 111 serviceEndpoint->close(); 112 delete serviceEndpoint; 113 } 114 } 115