blob: 15cbd82f9d5f6209dfd5541e402c404da99e5484 [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
Eric Laurentcb4dae22017-07-01 19:39:32 -070017#define LOG_TAG "AAudioServiceEndpoint"
Phil Burk71f35bb2017-04-13 16:05:07 -070018//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
Phil Burk4501b352017-06-29 18:12:36 -070021#include <algorithm>
Phil Burk71f35bb2017-04-13 16:05:07 -070022#include <assert.h>
23#include <map>
24#include <mutex>
Phil Burk4501b352017-06-29 18:12:36 -070025#include <sstream>
26#include <vector>
27
Phil Burk71f35bb2017-04-13 16:05:07 -070028#include <utils/Singleton.h>
29
Phil Burkc0c70e32017-02-09 13:18:38 -080030
31#include "core/AudioStreamBuilder.h"
Phil Burk6e463ce2020-04-13 10:20:20 -070032
33#include "AAudioEndpointManager.h"
34#include "AAudioClientTracker.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080035#include "AAudioServiceEndpoint.h"
36#include "AAudioServiceStreamShared.h"
37
38using namespace android; // TODO just import names needed
39using namespace aaudio; // TODO just import names needed
40
Phil Burk4501b352017-06-29 18:12:36 -070041std::string AAudioServiceEndpoint::dump() const {
42 std::stringstream result;
43
44 const bool isLocked = AAudio_tryUntilTrue(
45 [this]()->bool { return mLockStreams.try_lock(); } /* f */,
46 50 /* times */,
47 20 /* sleepMs */);
48 if (!isLocked) {
Phil Burk39f02dd2017-08-04 09:13:31 -070049 result << "AAudioServiceEndpoint may be deadlocked\n";
Phil Burk4501b352017-06-29 18:12:36 -070050 }
51
Phil Burk39f02dd2017-08-04 09:13:31 -070052 result << " Direction: " << ((getDirection() == AAUDIO_DIRECTION_OUTPUT)
53 ? "OUTPUT" : "INPUT") << "\n";
Phil Burk39f02dd2017-08-04 09:13:31 -070054 result << " Requested Device Id: " << mRequestedDeviceId << "\n";
55 result << " Device Id: " << getDeviceId() << "\n";
Phil Burka62fb952018-01-16 12:44:06 -080056 result << " Sample Rate: " << getSampleRate() << "\n";
57 result << " Channel Count: " << getSamplesPerFrame() << "\n";
Phil Burk0127c1b2018-03-29 13:48:06 -070058 result << " Format: " << getFormat() << "\n";
Phil Burka62fb952018-01-16 12:44:06 -080059 result << " Frames Per Burst: " << mFramesPerBurst << "\n";
60 result << " Usage: " << getUsage() << "\n";
61 result << " ContentType: " << getContentType() << "\n";
62 result << " InputPreset: " << getInputPreset() << "\n";
63 result << " Reference Count: " << mOpenCount << "\n";
Phil Burk4e1af9f2018-01-03 15:54:35 -080064 result << " Session Id: " << getSessionId() << "\n";
Eric Laurentd17c8502019-10-24 15:58:35 -070065 result << " Privacy Sensitive: " << isPrivacySensitive() << "\n";
Phil Burkbe0a5b62017-10-12 15:56:00 -070066 result << " Connected: " << mConnected.load() << "\n";
Phil Burk523b3042017-09-13 13:03:08 -070067 result << " Registered Streams:" << "\n";
Phil Burka5222e22017-07-28 13:31:14 -070068 result << AAudioServiceStreamShared::dumpHeader() << "\n";
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -080069 for (const auto& stream : mRegisteredStreams) {
Phil Burk39f02dd2017-08-04 09:13:31 -070070 result << stream->dump() << "\n";
Phil Burk4501b352017-06-29 18:12:36 -070071 }
72
73 if (isLocked) {
74 mLockStreams.unlock();
75 }
76 return result.str();
77}
78
Phil Burkbbd52862018-04-13 11:37:42 -070079// @return true if stream found
80bool AAudioServiceEndpoint::isStreamRegistered(audio_port_handle_t portHandle) {
81 std::lock_guard<std::mutex> lock(mLockStreams);
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -080082 for (const auto& stream : mRegisteredStreams) {
Phil Burkbbd52862018-04-13 11:37:42 -070083 if (stream->getPortHandle() == portHandle) {
84 return true;
85 }
86 }
87 return false;
88}
89
Phil Burk6e463ce2020-04-13 10:20:20 -070090std::vector<android::sp<AAudioServiceStreamBase>>
91 AAudioServiceEndpoint::disconnectRegisteredStreams() {
92 std::vector<android::sp<AAudioServiceStreamBase>> streamsDisconnected;
Phil Burk7ebbc112020-05-13 15:55:17 -070093 {
94 std::lock_guard<std::mutex> lock(mLockStreams);
95 mRegisteredStreams.swap(streamsDisconnected);
96 }
Phil Burkbe0a5b62017-10-12 15:56:00 -070097 mConnected.store(false);
Phil Burk7ebbc112020-05-13 15:55:17 -070098 for (const auto &stream : streamsDisconnected) {
Phil Burk6e463ce2020-04-13 10:20:20 -070099 ALOGD("%s() - stop and disconnect port %d", __func__, stream->getPortHandle());
Phil Burk39f02dd2017-08-04 09:13:31 -0700100 stream->stop();
101 stream->disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800102 }
Phil Burk6e463ce2020-04-13 10:20:20 -0700103 return streamsDisconnected;
104}
105
106void AAudioServiceEndpoint::releaseRegisteredStreams() {
107 // List of streams to be closed after we disconnect everything.
108 std::vector<android::sp<AAudioServiceStreamBase>> streamsToClose
109 = disconnectRegisteredStreams();
110
111 // Close outside the lock to avoid recursive locks.
112 AAudioService *aaudioService = AAudioClientTracker::getInstance().getAAudioService();
113 for (const auto& serviceStream : streamsToClose) {
114 ALOGD("%s() - close stream 0x%08X", __func__, serviceStream->getHandle());
115 aaudioService->closeStream(serviceStream);
116 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800117}
Eric Laurenta17ae742017-06-29 15:43:55 -0700118
Phil Burk39f02dd2017-08-04 09:13:31 -0700119aaudio_result_t AAudioServiceEndpoint::registerStream(sp<AAudioServiceStreamBase>stream) {
120 std::lock_guard<std::mutex> lock(mLockStreams);
121 mRegisteredStreams.push_back(stream);
122 return AAUDIO_OK;
123}
124
125aaudio_result_t AAudioServiceEndpoint::unregisterStream(sp<AAudioServiceStreamBase>stream) {
126 std::lock_guard<std::mutex> lock(mLockStreams);
127 mRegisteredStreams.erase(std::remove(
128 mRegisteredStreams.begin(), mRegisteredStreams.end(), stream),
129 mRegisteredStreams.end());
130 return AAUDIO_OK;
131}
132
Eric Laurenta17ae742017-06-29 15:43:55 -0700133bool AAudioServiceEndpoint::matches(const AAudioStreamConfiguration& configuration) {
Phil Burkbe0a5b62017-10-12 15:56:00 -0700134 if (!mConnected.load()) {
135 return false; // Only use an endpoint if it is connected to a device.
136 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700137 if (configuration.getDirection() != getDirection()) {
138 return false;
139 }
Eric Laurenta17ae742017-06-29 15:43:55 -0700140 if (configuration.getDeviceId() != AAUDIO_UNSPECIFIED &&
Phil Burk39f02dd2017-08-04 09:13:31 -0700141 configuration.getDeviceId() != getDeviceId()) {
Eric Laurenta17ae742017-06-29 15:43:55 -0700142 return false;
143 }
Phil Burk4e1af9f2018-01-03 15:54:35 -0800144 if (configuration.getSessionId() != AAUDIO_SESSION_ID_ALLOCATE &&
145 configuration.getSessionId() != getSessionId()) {
146 return false;
147 }
Eric Laurenta17ae742017-06-29 15:43:55 -0700148 if (configuration.getSampleRate() != AAUDIO_UNSPECIFIED &&
Phil Burk39f02dd2017-08-04 09:13:31 -0700149 configuration.getSampleRate() != getSampleRate()) {
Eric Laurenta17ae742017-06-29 15:43:55 -0700150 return false;
151 }
152 if (configuration.getSamplesPerFrame() != AAUDIO_UNSPECIFIED &&
Phil Burk39f02dd2017-08-04 09:13:31 -0700153 configuration.getSamplesPerFrame() != getSamplesPerFrame()) {
Eric Laurenta17ae742017-06-29 15:43:55 -0700154 return false;
155 }
Eric Laurenta17ae742017-06-29 15:43:55 -0700156 return true;
157}
jiabind1f1cb62020-03-24 11:57:57 -0700158
159// static
160audio_attributes_t AAudioServiceEndpoint::getAudioAttributesFrom(
161 const AAudioStreamParameters *params) {
162 if (params == nullptr) {
163 return {};
164 }
165 const aaudio_direction_t direction = params->getDirection();
166
167 const audio_content_type_t contentType =
168 AAudioConvert_contentTypeToInternal(params->getContentType());
169 // Usage only used for OUTPUT
170 const audio_usage_t usage = (direction == AAUDIO_DIRECTION_OUTPUT)
171 ? AAudioConvert_usageToInternal(params->getUsage())
172 : AUDIO_USAGE_UNKNOWN;
173 const audio_source_t source = (direction == AAUDIO_DIRECTION_INPUT)
174 ? AAudioConvert_inputPresetToAudioSource(params->getInputPreset())
175 : AUDIO_SOURCE_DEFAULT;
176 audio_flags_mask_t flags;
177 if (direction == AAUDIO_DIRECTION_OUTPUT) {
178 flags = AUDIO_FLAG_LOW_LATENCY
179 | AAudioConvert_allowCapturePolicyToAudioFlagsMask(params->getAllowedCapturePolicy());
180 } else {
181 flags = AUDIO_FLAG_LOW_LATENCY
182 | AAudioConvert_privacySensitiveToAudioFlagsMask(params->isPrivacySensitive());
183 }
184 return {
185 .content_type = contentType,
186 .usage = usage,
187 .source = source,
188 .flags = flags,
189 .tags = "" };
190}