blob: fd546f6fe1d303058bf291d119182ee49fb31e00 [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 Burk3d201942021-04-08 23:27:04 +000041AAudioServiceEndpoint::~AAudioServiceEndpoint() {
42 ALOGD("%s() called", __func__);
43}
44
Phil Burk0bd745e2020-10-17 18:20:01 +000045std::string AAudioServiceEndpoint::dump() const NO_THREAD_SAFETY_ANALYSIS {
Phil Burk4501b352017-06-29 18:12:36 -070046 std::stringstream result;
47
48 const bool isLocked = AAudio_tryUntilTrue(
49 [this]()->bool { return mLockStreams.try_lock(); } /* f */,
50 50 /* times */,
51 20 /* sleepMs */);
52 if (!isLocked) {
Phil Burk39f02dd2017-08-04 09:13:31 -070053 result << "AAudioServiceEndpoint may be deadlocked\n";
Phil Burk4501b352017-06-29 18:12:36 -070054 }
55
Phil Burk39f02dd2017-08-04 09:13:31 -070056 result << " Direction: " << ((getDirection() == AAUDIO_DIRECTION_OUTPUT)
57 ? "OUTPUT" : "INPUT") << "\n";
Phil Burk39f02dd2017-08-04 09:13:31 -070058 result << " Requested Device Id: " << mRequestedDeviceId << "\n";
59 result << " Device Id: " << getDeviceId() << "\n";
Phil Burka62fb952018-01-16 12:44:06 -080060 result << " Sample Rate: " << getSampleRate() << "\n";
61 result << " Channel Count: " << getSamplesPerFrame() << "\n";
jiabina9094092021-06-28 20:36:45 +000062 result << " Channel Mask: 0x" << std::hex << getChannelMask() << std::dec << "\n";
Phil Burked782c82022-02-08 21:43:53 +000063 result << " Format: " << getFormat()
64 << " (" << audio_format_to_string(getFormat()) << ")\n";
Phil Burka62fb952018-01-16 12:44:06 -080065 result << " Frames Per Burst: " << mFramesPerBurst << "\n";
66 result << " Usage: " << getUsage() << "\n";
67 result << " ContentType: " << getContentType() << "\n";
68 result << " InputPreset: " << getInputPreset() << "\n";
69 result << " Reference Count: " << mOpenCount << "\n";
Phil Burk4e1af9f2018-01-03 15:54:35 -080070 result << " Session Id: " << getSessionId() << "\n";
Eric Laurentd17c8502019-10-24 15:58:35 -070071 result << " Privacy Sensitive: " << isPrivacySensitive() << "\n";
Robert Wu310037a2022-09-06 21:48:18 +000072 result << " Hardware Channel Count:" << getHardwareSamplesPerFrame() << "\n";
73 result << " Hardware Format: " << getHardwareFormat() << " ("
74 << audio_format_to_string(getHardwareFormat()) << ")\n";
75 result << " Hardware Sample Rate: " << getHardwareSampleRate() << "\n";
Phil Burkbe0a5b62017-10-12 15:56:00 -070076 result << " Connected: " << mConnected.load() << "\n";
Phil Burk523b3042017-09-13 13:03:08 -070077 result << " Registered Streams:" << "\n";
Phil Burka5222e22017-07-28 13:31:14 -070078 result << AAudioServiceStreamShared::dumpHeader() << "\n";
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -080079 for (const auto& stream : mRegisteredStreams) {
Phil Burk39f02dd2017-08-04 09:13:31 -070080 result << stream->dump() << "\n";
Phil Burk4501b352017-06-29 18:12:36 -070081 }
82
83 if (isLocked) {
84 mLockStreams.unlock();
85 }
86 return result.str();
87}
88
Phil Burkbbd52862018-04-13 11:37:42 -070089// @return true if stream found
90bool AAudioServiceEndpoint::isStreamRegistered(audio_port_handle_t portHandle) {
91 std::lock_guard<std::mutex> lock(mLockStreams);
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -080092 for (const auto& stream : mRegisteredStreams) {
Phil Burkbbd52862018-04-13 11:37:42 -070093 if (stream->getPortHandle() == portHandle) {
94 return true;
95 }
96 }
97 return false;
98}
99
Phil Burk6e463ce2020-04-13 10:20:20 -0700100std::vector<android::sp<AAudioServiceStreamBase>>
101 AAudioServiceEndpoint::disconnectRegisteredStreams() {
102 std::vector<android::sp<AAudioServiceStreamBase>> streamsDisconnected;
Phil Burk7ebbc112020-05-13 15:55:17 -0700103 {
104 std::lock_guard<std::mutex> lock(mLockStreams);
105 mRegisteredStreams.swap(streamsDisconnected);
106 }
Phil Burkbe0a5b62017-10-12 15:56:00 -0700107 mConnected.store(false);
Phil Burk8e2255a2020-06-05 09:58:26 -0700108 // We need to stop all the streams before we disconnect them.
109 // Otherwise there is a race condition where the first disconnected app
110 // tries to reopen a stream as MMAP but is blocked by the second stream,
111 // which hasn't stopped yet. Then the first app ends up with a Legacy stream.
Phil Burk7ebbc112020-05-13 15:55:17 -0700112 for (const auto &stream : streamsDisconnected) {
Phil Burk8e2255a2020-06-05 09:58:26 -0700113 ALOGD("%s() - stop(), port = %d", __func__, stream->getPortHandle());
Phil Burk39f02dd2017-08-04 09:13:31 -0700114 stream->stop();
Phil Burk8e2255a2020-06-05 09:58:26 -0700115 }
116 for (const auto &stream : streamsDisconnected) {
117 ALOGD("%s() - disconnect(), port = %d", __func__, stream->getPortHandle());
Phil Burk39f02dd2017-08-04 09:13:31 -0700118 stream->disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800119 }
Phil Burk6e463ce2020-04-13 10:20:20 -0700120 return streamsDisconnected;
121}
122
123void AAudioServiceEndpoint::releaseRegisteredStreams() {
124 // List of streams to be closed after we disconnect everything.
125 std::vector<android::sp<AAudioServiceStreamBase>> streamsToClose
126 = disconnectRegisteredStreams();
127
128 // Close outside the lock to avoid recursive locks.
129 AAudioService *aaudioService = AAudioClientTracker::getInstance().getAAudioService();
130 for (const auto& serviceStream : streamsToClose) {
131 ALOGD("%s() - close stream 0x%08X", __func__, serviceStream->getHandle());
132 aaudioService->closeStream(serviceStream);
133 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800134}
Eric Laurenta17ae742017-06-29 15:43:55 -0700135
Phil Burk39f02dd2017-08-04 09:13:31 -0700136aaudio_result_t AAudioServiceEndpoint::registerStream(sp<AAudioServiceStreamBase>stream) {
137 std::lock_guard<std::mutex> lock(mLockStreams);
138 mRegisteredStreams.push_back(stream);
139 return AAUDIO_OK;
140}
141
142aaudio_result_t AAudioServiceEndpoint::unregisterStream(sp<AAudioServiceStreamBase>stream) {
143 std::lock_guard<std::mutex> lock(mLockStreams);
144 mRegisteredStreams.erase(std::remove(
145 mRegisteredStreams.begin(), mRegisteredStreams.end(), stream),
146 mRegisteredStreams.end());
147 return AAUDIO_OK;
148}
149
Eric Laurenta17ae742017-06-29 15:43:55 -0700150bool AAudioServiceEndpoint::matches(const AAudioStreamConfiguration& configuration) {
Phil Burkbe0a5b62017-10-12 15:56:00 -0700151 if (!mConnected.load()) {
152 return false; // Only use an endpoint if it is connected to a device.
153 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700154 if (configuration.getDirection() != getDirection()) {
155 return false;
156 }
Eric Laurenta17ae742017-06-29 15:43:55 -0700157 if (configuration.getDeviceId() != AAUDIO_UNSPECIFIED &&
Phil Burk39f02dd2017-08-04 09:13:31 -0700158 configuration.getDeviceId() != getDeviceId()) {
Eric Laurenta17ae742017-06-29 15:43:55 -0700159 return false;
160 }
Phil Burk4e1af9f2018-01-03 15:54:35 -0800161 if (configuration.getSessionId() != AAUDIO_SESSION_ID_ALLOCATE &&
162 configuration.getSessionId() != getSessionId()) {
163 return false;
164 }
Eric Laurenta17ae742017-06-29 15:43:55 -0700165 if (configuration.getSampleRate() != AAUDIO_UNSPECIFIED &&
Phil Burk39f02dd2017-08-04 09:13:31 -0700166 configuration.getSampleRate() != getSampleRate()) {
Eric Laurenta17ae742017-06-29 15:43:55 -0700167 return false;
168 }
169 if (configuration.getSamplesPerFrame() != AAUDIO_UNSPECIFIED &&
Phil Burk39f02dd2017-08-04 09:13:31 -0700170 configuration.getSamplesPerFrame() != getSamplesPerFrame()) {
Eric Laurenta17ae742017-06-29 15:43:55 -0700171 return false;
172 }
jiabina9094092021-06-28 20:36:45 +0000173 if (configuration.getChannelMask() != AAUDIO_UNSPECIFIED &&
174 configuration.getChannelMask() != getChannelMask()) {
175 return false;
176 }
Eric Laurenta17ae742017-06-29 15:43:55 -0700177 return true;
178}
jiabind1f1cb62020-03-24 11:57:57 -0700179
180// static
181audio_attributes_t AAudioServiceEndpoint::getAudioAttributesFrom(
182 const AAudioStreamParameters *params) {
183 if (params == nullptr) {
184 return {};
185 }
186 const aaudio_direction_t direction = params->getDirection();
187
188 const audio_content_type_t contentType =
189 AAudioConvert_contentTypeToInternal(params->getContentType());
190 // Usage only used for OUTPUT
191 const audio_usage_t usage = (direction == AAUDIO_DIRECTION_OUTPUT)
192 ? AAudioConvert_usageToInternal(params->getUsage())
193 : AUDIO_USAGE_UNKNOWN;
194 const audio_source_t source = (direction == AAUDIO_DIRECTION_INPUT)
195 ? AAudioConvert_inputPresetToAudioSource(params->getInputPreset())
196 : AUDIO_SOURCE_DEFAULT;
197 audio_flags_mask_t flags;
198 if (direction == AAUDIO_DIRECTION_OUTPUT) {
Mikhail Naganov55773032020-10-01 15:08:13 -0700199 flags = static_cast<audio_flags_mask_t>(AUDIO_FLAG_LOW_LATENCY
200 | AAudioConvert_allowCapturePolicyToAudioFlagsMask(
Jean-Michel Trivi656bfdc2021-09-20 18:42:37 -0700201 params->getAllowedCapturePolicy(),
202 params->getSpatializationBehavior(),
203 params->isContentSpatialized()));
jiabind1f1cb62020-03-24 11:57:57 -0700204 } else {
Mikhail Naganov55773032020-10-01 15:08:13 -0700205 flags = static_cast<audio_flags_mask_t>(AUDIO_FLAG_LOW_LATENCY
206 | AAudioConvert_privacySensitiveToAudioFlagsMask(params->isPrivacySensitive()));
jiabind1f1cb62020-03-24 11:57:57 -0700207 }
208 return {
209 .content_type = contentType,
210 .usage = usage,
211 .source = source,
212 .flags = flags,
213 .tags = "" };
214}