blob: 02d4a19cea94e8ef3c7bd572ba059beb3a93057c [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
Phil Burk4501b352017-06-29 18:12:36 -070051 result << "AAudioEndpointManager:" << "\n";
Andy Hung47c5e532017-06-26 18:28:00 -070052 size_t inputs = mInputs.size();
Phil Burk4501b352017-06-29 18:12:36 -070053 result << "Input Endpoints: " << inputs << "\n";
Andy Hung47c5e532017-06-26 18:28:00 -070054 for (const auto &input : mInputs) {
Phil Burk4501b352017-06-29 18:12:36 -070055 result << " Input: " << input->dump() << "\n";
Andy Hung47c5e532017-06-26 18:28:00 -070056 }
57
58 size_t outputs = mOutputs.size();
Phil Burk4501b352017-06-29 18:12:36 -070059 result << "Output Endpoints: " << outputs << "\n";
Andy Hung47c5e532017-06-26 18:28:00 -070060 for (const auto &output : mOutputs) {
Phil Burk4501b352017-06-29 18:12:36 -070061 result << " Output: " << output->dump() << "\n";
Andy Hung47c5e532017-06-26 18:28:00 -070062 }
63
64 if (isLocked) {
65 mLock.unlock();
66 }
67 return result.str();
68}
69
Eric Laurenta17ae742017-06-29 15:43:55 -070070AAudioServiceEndpoint *AAudioEndpointManager::openEndpoint(AAudioService &audioService,
71 const AAudioStreamConfiguration& configuration, aaudio_direction_t direction) {
Phil Burkc0c70e32017-02-09 13:18:38 -080072 AAudioServiceEndpoint *endpoint = nullptr;
Phil Burk11e8d332017-05-24 09:59:02 -070073 AAudioServiceEndpointCapture *capture = nullptr;
74 AAudioServiceEndpointPlay *player = nullptr;
Phil Burkc0c70e32017-02-09 13:18:38 -080075 std::lock_guard<std::mutex> lock(mLock);
Phil Burk71f35bb2017-04-13 16:05:07 -070076
77 // Try to find an existing endpoint.
Eric Laurenta17ae742017-06-29 15:43:55 -070078
79
80
Phil Burkc0c70e32017-02-09 13:18:38 -080081 switch (direction) {
82 case AAUDIO_DIRECTION_INPUT:
Eric Laurenta17ae742017-06-29 15:43:55 -070083 for (AAudioServiceEndpoint *ep : mInputs) {
84 if (ep->matches(configuration)) {
85 endpoint = ep;
86 break;
87 }
88 }
Phil Burkc0c70e32017-02-09 13:18:38 -080089 break;
90 case AAUDIO_DIRECTION_OUTPUT:
Eric Laurenta17ae742017-06-29 15:43:55 -070091 for (AAudioServiceEndpoint *ep : mOutputs) {
92 if (ep->matches(configuration)) {
93 endpoint = ep;
94 break;
95 }
96 }
Phil Burkc0c70e32017-02-09 13:18:38 -080097 break;
98 default:
99 assert(false); // There are only two possible directions.
100 break;
101 }
Phil Burk11e8d332017-05-24 09:59:02 -0700102 ALOGD("AAudioEndpointManager::openEndpoint(), found %p for device = %d, dir = %d",
Eric Laurenta17ae742017-06-29 15:43:55 -0700103 endpoint, configuration.getDeviceId(), (int)direction);
Phil Burk87c9f642017-05-17 07:22:39 -0700104
105 // If we can't find an existing one then open a new one.
Phil Burk11e8d332017-05-24 09:59:02 -0700106 if (endpoint == nullptr) {
107 switch(direction) {
108 case AAUDIO_DIRECTION_INPUT:
109 capture = new AAudioServiceEndpointCapture(audioService);
Phil Burk87c9f642017-05-17 07:22:39 -0700110 endpoint = capture;
Phil Burk11e8d332017-05-24 09:59:02 -0700111 break;
112 case AAUDIO_DIRECTION_OUTPUT:
113 player = new AAudioServiceEndpointPlay(audioService);
Phil Burk87c9f642017-05-17 07:22:39 -0700114 endpoint = player;
Phil Burk11e8d332017-05-24 09:59:02 -0700115 break;
116 default:
117 break;
118 }
Phil Burk11e8d332017-05-24 09:59:02 -0700119
Phil Burk91692942017-06-30 12:23:05 -0700120 if (endpoint != nullptr) {
121 aaudio_result_t result = endpoint->open(configuration);
122 if (result != AAUDIO_OK) {
123 ALOGE("AAudioEndpointManager::findEndpoint(), open failed");
124 delete endpoint;
125 endpoint = nullptr;
126 } else {
127 switch(direction) {
128 case AAUDIO_DIRECTION_INPUT:
129 mInputs.push_back(capture);
130 break;
131 case AAUDIO_DIRECTION_OUTPUT:
132 mOutputs.push_back(player);
133 break;
134 default:
135 break;
136 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800137 }
138 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700139 ALOGD("AAudioEndpointManager::openEndpoint(), created %p for device = %d, dir = %d",
Eric Laurenta17ae742017-06-29 15:43:55 -0700140 endpoint, configuration.getDeviceId(), (int)direction);
Phil Burkc0c70e32017-02-09 13:18:38 -0800141 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700142
143 if (endpoint != nullptr) {
Phil Burkec89b2e2017-06-20 15:05:06 -0700144 ALOGD("AAudioEndpointManager::openEndpoint(), sampleRate = %d, framesPerBurst = %d",
145 endpoint->getSampleRate(), endpoint->getFramesPerBurst());
Phil Burk71f35bb2017-04-13 16:05:07 -0700146 // Increment the reference count under this lock.
147 endpoint->setReferenceCount(endpoint->getReferenceCount() + 1);
148 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800149 return endpoint;
150}
151
Phil Burk71f35bb2017-04-13 16:05:07 -0700152void AAudioEndpointManager::closeEndpoint(AAudioServiceEndpoint *serviceEndpoint) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800153 std::lock_guard<std::mutex> lock(mLock);
Phil Burk71f35bb2017-04-13 16:05:07 -0700154 if (serviceEndpoint == nullptr) {
155 return;
Phil Burkc0c70e32017-02-09 13:18:38 -0800156 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700157
158 // Decrement the reference count under this lock.
159 int32_t newRefCount = serviceEndpoint->getReferenceCount() - 1;
160 serviceEndpoint->setReferenceCount(newRefCount);
Phil Burkec89b2e2017-06-20 15:05:06 -0700161 ALOGD("AAudioEndpointManager::closeEndpoint(%p) newRefCount = %d",
162 serviceEndpoint, newRefCount);
163
164 // If no longer in use then close and delete it.
Phil Burk71f35bb2017-04-13 16:05:07 -0700165 if (newRefCount <= 0) {
166 aaudio_direction_t direction = serviceEndpoint->getDirection();
Phil Burkec89b2e2017-06-20 15:05:06 -0700167 // Track endpoints based on requested deviceId because UNSPECIFIED
168 // can change to a specific device after opening.
169 int32_t deviceId = serviceEndpoint->getRequestedDeviceId();
Phil Burk71f35bb2017-04-13 16:05:07 -0700170
171 switch (direction) {
172 case AAUDIO_DIRECTION_INPUT:
Eric Laurenta17ae742017-06-29 15:43:55 -0700173 mInputs.erase(
174 std::remove(mInputs.begin(), mInputs.end(), serviceEndpoint), mInputs.end());
Phil Burk71f35bb2017-04-13 16:05:07 -0700175 break;
176 case AAUDIO_DIRECTION_OUTPUT:
Eric Laurenta17ae742017-06-29 15:43:55 -0700177 mOutputs.erase(
178 std::remove(mOutputs.begin(), mOutputs.end(), serviceEndpoint), mOutputs.end());
Phil Burk71f35bb2017-04-13 16:05:07 -0700179 break;
Phil Burk11e8d332017-05-24 09:59:02 -0700180 default:
181 break;
Phil Burk71f35bb2017-04-13 16:05:07 -0700182 }
Phil Burk87c9f642017-05-17 07:22:39 -0700183
Phil Burk71f35bb2017-04-13 16:05:07 -0700184 serviceEndpoint->close();
Phil Burkec89b2e2017-06-20 15:05:06 -0700185 ALOGD("AAudioEndpointManager::closeEndpoint() delete %p for device %d, dir = %d",
186 serviceEndpoint, deviceId, (int)direction);
Phil Burk71f35bb2017-04-13 16:05:07 -0700187 delete serviceEndpoint;
188 }
189}