blob: 94577a67971b8489cb4be17fb088b050053fda2d [file] [log] [blame]
Phil Burkc0c70e32017-02-09 13:18:38 -08001/*
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 Burk71f35bb2017-04-13 16:05:07 -070017#define LOG_TAG "AAudioService"
18//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
Phil Burkc0c70e32017-02-09 13:18:38 -080021#include <assert.h>
Andy Hung47c5e532017-06-26 18:28:00 -070022#include <functional>
Phil Burkc0c70e32017-02-09 13:18:38 -080023#include <map>
24#include <mutex>
Andy Hung47c5e532017-06-26 18:28:00 -070025#include <sstream>
26#include <utility/AAudioUtilities.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080027
28#include "AAudioEndpointManager.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080029
30using namespace android;
31using namespace aaudio;
32
33ANDROID_SINGLETON_STATIC_INSTANCE(AAudioEndpointManager);
34
35AAudioEndpointManager::AAudioEndpointManager()
Phil Burk71f35bb2017-04-13 16:05:07 -070036 : Singleton<AAudioEndpointManager>()
37 , mInputs()
38 , mOutputs() {
Phil Burkc0c70e32017-02-09 13:18:38 -080039}
40
Andy Hung47c5e532017-06-26 18:28:00 -070041std::string AAudioEndpointManager::dump() const {
42 std::stringstream result;
43 const bool isLocked = AAudio_tryUntilTrue(
44 [this]()->bool { return mLock.try_lock(); } /* f */,
45 50 /* times */,
46 20 /* sleepMs */);
47 if (!isLocked) {
48 result << "EndpointManager may be deadlocked\n";
49 }
50
51 size_t inputs = mInputs.size();
52 result << "Inputs: " << inputs << "\n";
53 for (const auto &input : mInputs) {
54 result << " Input(" << input.first << ", " << input.second << ")\n";
55 }
56
57 size_t outputs = mOutputs.size();
58 result << "Outputs: " << outputs << "\n";
59 for (const auto &output : mOutputs) {
60 result << " Output(" << output.first << ", " << output.second << ")\n";
61 }
62
63 if (isLocked) {
64 mLock.unlock();
65 }
66 return result.str();
67}
68
Phil Burk71f35bb2017-04-13 16:05:07 -070069AAudioServiceEndpoint *AAudioEndpointManager::openEndpoint(AAudioService &audioService, int32_t deviceId,
Phil Burkc0c70e32017-02-09 13:18:38 -080070 aaudio_direction_t direction) {
71 AAudioServiceEndpoint *endpoint = nullptr;
Phil Burk11e8d332017-05-24 09:59:02 -070072 AAudioServiceEndpointCapture *capture = nullptr;
73 AAudioServiceEndpointPlay *player = nullptr;
Phil Burkc0c70e32017-02-09 13:18:38 -080074 std::lock_guard<std::mutex> lock(mLock);
Phil Burk71f35bb2017-04-13 16:05:07 -070075
76 // Try to find an existing endpoint.
Phil Burkc0c70e32017-02-09 13:18:38 -080077 switch (direction) {
78 case AAUDIO_DIRECTION_INPUT:
79 endpoint = mInputs[deviceId];
80 break;
81 case AAUDIO_DIRECTION_OUTPUT:
82 endpoint = mOutputs[deviceId];
83 break;
84 default:
85 assert(false); // There are only two possible directions.
86 break;
87 }
Phil Burk11e8d332017-05-24 09:59:02 -070088 ALOGD("AAudioEndpointManager::openEndpoint(), found %p for device = %d, dir = %d",
89 endpoint, deviceId, (int)direction);
Phil Burk87c9f642017-05-17 07:22:39 -070090
91 // If we can't find an existing one then open a new one.
Phil Burk11e8d332017-05-24 09:59:02 -070092 if (endpoint == nullptr) {
93 switch(direction) {
94 case AAUDIO_DIRECTION_INPUT:
95 capture = new AAudioServiceEndpointCapture(audioService);
Phil Burk87c9f642017-05-17 07:22:39 -070096 endpoint = capture;
Phil Burk11e8d332017-05-24 09:59:02 -070097 break;
98 case AAUDIO_DIRECTION_OUTPUT:
99 player = new AAudioServiceEndpointPlay(audioService);
Phil Burk87c9f642017-05-17 07:22:39 -0700100 endpoint = player;
Phil Burk11e8d332017-05-24 09:59:02 -0700101 break;
102 default:
103 break;
104 }
105 }
106
107 if (endpoint != nullptr) {
108 aaudio_result_t result = endpoint->open(deviceId);
109 if (result != AAUDIO_OK) {
110 ALOGE("AAudioEndpointManager::findEndpoint(), open failed");
111 delete endpoint;
112 endpoint = nullptr;
113 } else {
114 switch(direction) {
115 case AAUDIO_DIRECTION_INPUT:
116 mInputs[deviceId] = capture;
117 break;
118 case AAUDIO_DIRECTION_OUTPUT:
119 mOutputs[deviceId] = player;
120 break;
121 default:
122 break;
Phil Burkc0c70e32017-02-09 13:18:38 -0800123 }
124 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700125 ALOGD("AAudioEndpointManager::openEndpoint(), created %p for device = %d, dir = %d",
126 endpoint, deviceId, (int)direction);
Phil Burkc0c70e32017-02-09 13:18:38 -0800127 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700128
129 if (endpoint != nullptr) {
Phil Burkec89b2e2017-06-20 15:05:06 -0700130 ALOGD("AAudioEndpointManager::openEndpoint(), sampleRate = %d, framesPerBurst = %d",
131 endpoint->getSampleRate(), endpoint->getFramesPerBurst());
Phil Burk71f35bb2017-04-13 16:05:07 -0700132 // Increment the reference count under this lock.
133 endpoint->setReferenceCount(endpoint->getReferenceCount() + 1);
134 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800135 return endpoint;
136}
137
Phil Burk71f35bb2017-04-13 16:05:07 -0700138void AAudioEndpointManager::closeEndpoint(AAudioServiceEndpoint *serviceEndpoint) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800139 std::lock_guard<std::mutex> lock(mLock);
Phil Burk71f35bb2017-04-13 16:05:07 -0700140 if (serviceEndpoint == nullptr) {
141 return;
Phil Burkc0c70e32017-02-09 13:18:38 -0800142 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700143
144 // Decrement the reference count under this lock.
145 int32_t newRefCount = serviceEndpoint->getReferenceCount() - 1;
146 serviceEndpoint->setReferenceCount(newRefCount);
Phil Burkec89b2e2017-06-20 15:05:06 -0700147 ALOGD("AAudioEndpointManager::closeEndpoint(%p) newRefCount = %d",
148 serviceEndpoint, newRefCount);
149
150 // If no longer in use then close and delete it.
Phil Burk71f35bb2017-04-13 16:05:07 -0700151 if (newRefCount <= 0) {
152 aaudio_direction_t direction = serviceEndpoint->getDirection();
Phil Burkec89b2e2017-06-20 15:05:06 -0700153 // Track endpoints based on requested deviceId because UNSPECIFIED
154 // can change to a specific device after opening.
155 int32_t deviceId = serviceEndpoint->getRequestedDeviceId();
Phil Burk71f35bb2017-04-13 16:05:07 -0700156
157 switch (direction) {
158 case AAUDIO_DIRECTION_INPUT:
159 mInputs.erase(deviceId);
160 break;
161 case AAUDIO_DIRECTION_OUTPUT:
162 mOutputs.erase(deviceId);
163 break;
Phil Burk11e8d332017-05-24 09:59:02 -0700164 default:
165 break;
Phil Burk71f35bb2017-04-13 16:05:07 -0700166 }
Phil Burk87c9f642017-05-17 07:22:39 -0700167
Phil Burk71f35bb2017-04-13 16:05:07 -0700168 serviceEndpoint->close();
Phil Burkec89b2e2017-06-20 15:05:06 -0700169 ALOGD("AAudioEndpointManager::closeEndpoint() delete %p for device %d, dir = %d",
170 serviceEndpoint, deviceId, (int)direction);
Phil Burk71f35bb2017-04-13 16:05:07 -0700171 delete serviceEndpoint;
172 }
173}