blob: 5c6825d73e780c9bb0b228d74a7c2ddfe2d56313 [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>
22#include <map>
23#include <mutex>
Phil Burkc0c70e32017-02-09 13:18:38 -080024
25#include "AAudioEndpointManager.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080026
27using namespace android;
28using namespace aaudio;
29
30ANDROID_SINGLETON_STATIC_INSTANCE(AAudioEndpointManager);
31
32AAudioEndpointManager::AAudioEndpointManager()
Phil Burk71f35bb2017-04-13 16:05:07 -070033 : Singleton<AAudioEndpointManager>()
34 , mInputs()
35 , mOutputs() {
Phil Burkc0c70e32017-02-09 13:18:38 -080036}
37
Phil Burk71f35bb2017-04-13 16:05:07 -070038AAudioServiceEndpoint *AAudioEndpointManager::openEndpoint(AAudioService &audioService, int32_t deviceId,
Phil Burkc0c70e32017-02-09 13:18:38 -080039 aaudio_direction_t direction) {
40 AAudioServiceEndpoint *endpoint = nullptr;
41 std::lock_guard<std::mutex> lock(mLock);
Phil Burk71f35bb2017-04-13 16:05:07 -070042
43 // Try to find an existing endpoint.
Phil Burkc0c70e32017-02-09 13:18:38 -080044 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 Burk87c9f642017-05-17 07:22:39 -070055
56 // If we can't find an existing one then open a new one.
Phil Burkec89b2e2017-06-20 15:05:06 -070057 if (endpoint != nullptr) {
58 ALOGD("AAudioEndpointManager::openEndpoint(), found %p for device = %d, dir = %d",
59 endpoint, deviceId, (int)direction);
60
61 } else {
Phil Burk87c9f642017-05-17 07:22:39 -070062 if (direction == AAUDIO_DIRECTION_INPUT) {
63 AAudioServiceEndpointCapture *capture = new AAudioServiceEndpointCapture(audioService);
64 if (capture->open(deviceId) != AAUDIO_OK) {
Phil Burkec89b2e2017-06-20 15:05:06 -070065 ALOGE("AAudioEndpointManager::openEndpoint(), open input failed");
Phil Burk87c9f642017-05-17 07:22:39 -070066 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 Burkec89b2e2017-06-20 15:05:06 -070074 ALOGE("AAudioEndpointManager::openEndpoint(), open output failed");
Phil Burk87c9f642017-05-17 07:22:39 -070075 delete player;
76 } else {
77 mOutputs[deviceId] = player;
78 endpoint = player;
Phil Burkc0c70e32017-02-09 13:18:38 -080079 }
80 }
Phil Burkec89b2e2017-06-20 15:05:06 -070081 ALOGD("AAudioEndpointManager::openEndpoint(), created %p for device = %d, dir = %d",
82 endpoint, deviceId, (int)direction);
Phil Burkc0c70e32017-02-09 13:18:38 -080083 }
Phil Burk71f35bb2017-04-13 16:05:07 -070084
85 if (endpoint != nullptr) {
Phil Burkec89b2e2017-06-20 15:05:06 -070086 ALOGD("AAudioEndpointManager::openEndpoint(), sampleRate = %d, framesPerBurst = %d",
87 endpoint->getSampleRate(), endpoint->getFramesPerBurst());
Phil Burk71f35bb2017-04-13 16:05:07 -070088 // Increment the reference count under this lock.
89 endpoint->setReferenceCount(endpoint->getReferenceCount() + 1);
90 }
Phil Burkc0c70e32017-02-09 13:18:38 -080091 return endpoint;
92}
93
Phil Burk71f35bb2017-04-13 16:05:07 -070094void AAudioEndpointManager::closeEndpoint(AAudioServiceEndpoint *serviceEndpoint) {
Phil Burkc0c70e32017-02-09 13:18:38 -080095 std::lock_guard<std::mutex> lock(mLock);
Phil Burk71f35bb2017-04-13 16:05:07 -070096 if (serviceEndpoint == nullptr) {
97 return;
Phil Burkc0c70e32017-02-09 13:18:38 -080098 }
Phil Burk71f35bb2017-04-13 16:05:07 -070099
100 // Decrement the reference count under this lock.
101 int32_t newRefCount = serviceEndpoint->getReferenceCount() - 1;
102 serviceEndpoint->setReferenceCount(newRefCount);
Phil Burkec89b2e2017-06-20 15:05:06 -0700103 ALOGD("AAudioEndpointManager::closeEndpoint(%p) newRefCount = %d",
104 serviceEndpoint, newRefCount);
105
106 // If no longer in use then close and delete it.
Phil Burk71f35bb2017-04-13 16:05:07 -0700107 if (newRefCount <= 0) {
108 aaudio_direction_t direction = serviceEndpoint->getDirection();
Phil Burkec89b2e2017-06-20 15:05:06 -0700109 // Track endpoints based on requested deviceId because UNSPECIFIED
110 // can change to a specific device after opening.
111 int32_t deviceId = serviceEndpoint->getRequestedDeviceId();
Phil Burk71f35bb2017-04-13 16:05:07 -0700112
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 Burk87c9f642017-05-17 07:22:39 -0700121
Phil Burk71f35bb2017-04-13 16:05:07 -0700122 serviceEndpoint->close();
Phil Burkec89b2e2017-06-20 15:05:06 -0700123 ALOGD("AAudioEndpointManager::closeEndpoint() delete %p for device %d, dir = %d",
124 serviceEndpoint, deviceId, (int)direction);
Phil Burk71f35bb2017-04-13 16:05:07 -0700125 delete serviceEndpoint;
126 }
127}