blob: 3dc1feba013a989dce4cf9cb27c8fd33217d85b4 [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 Burkcf5f6d22017-05-26 12:35:07 -070055 ALOGD("AAudioEndpointManager::openEndpoint(), found %p for device = %d, dir = %d",
56 endpoint, deviceId, (int)direction);
Phil Burk87c9f642017-05-17 07:22:39 -070057
58 // If we can't find an existing one then open a new one.
Phil Burkc0c70e32017-02-09 13:18:38 -080059 if (endpoint == nullptr) {
Phil Burk87c9f642017-05-17 07:22:39 -070060 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;
Phil Burkc0c70e32017-02-09 13:18:38 -080077 }
78 }
Phil Burk87c9f642017-05-17 07:22:39 -070079
Phil Burkc0c70e32017-02-09 13:18:38 -080080 }
Phil Burk71f35bb2017-04-13 16:05:07 -070081
82 if (endpoint != nullptr) {
83 // Increment the reference count under this lock.
84 endpoint->setReferenceCount(endpoint->getReferenceCount() + 1);
85 }
Phil Burkc0c70e32017-02-09 13:18:38 -080086 return endpoint;
87}
88
Phil Burk71f35bb2017-04-13 16:05:07 -070089void AAudioEndpointManager::closeEndpoint(AAudioServiceEndpoint *serviceEndpoint) {
Phil Burkc0c70e32017-02-09 13:18:38 -080090 std::lock_guard<std::mutex> lock(mLock);
Phil Burk71f35bb2017-04-13 16:05:07 -070091 if (serviceEndpoint == nullptr) {
92 return;
Phil Burkc0c70e32017-02-09 13:18:38 -080093 }
Phil Burk71f35bb2017-04-13 16:05:07 -070094
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 }
Phil Burk87c9f642017-05-17 07:22:39 -0700110
Phil Burk71f35bb2017-04-13 16:05:07 -0700111 serviceEndpoint->close();
112 delete serviceEndpoint;
113 }
114}