Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 1 | /* |
| 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 Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AAudioServiceEndpoint" |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 21 | #include <algorithm> |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 22 | #include <assert.h> |
| 23 | #include <map> |
| 24 | #include <mutex> |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 25 | #include <sstream> |
| 26 | #include <vector> |
| 27 | |
Jiabin Huang | 51a8a77 | 2024-10-30 21:57:48 +0000 | [diff] [blame] | 28 | #include <system/aaudio/AAudio.h> |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 29 | #include <utils/Singleton.h> |
| 30 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 31 | |
| 32 | #include "core/AudioStreamBuilder.h" |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 33 | |
| 34 | #include "AAudioEndpointManager.h" |
| 35 | #include "AAudioClientTracker.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 36 | #include "AAudioServiceEndpoint.h" |
| 37 | #include "AAudioServiceStreamShared.h" |
| 38 | |
| 39 | using namespace android; // TODO just import names needed |
| 40 | using namespace aaudio; // TODO just import names needed |
| 41 | |
Phil Burk | 3d20194 | 2021-04-08 23:27:04 +0000 | [diff] [blame] | 42 | AAudioServiceEndpoint::~AAudioServiceEndpoint() { |
| 43 | ALOGD("%s() called", __func__); |
| 44 | } |
| 45 | |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 46 | std::string AAudioServiceEndpoint::dump() const NO_THREAD_SAFETY_ANALYSIS { |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 47 | std::stringstream result; |
| 48 | |
| 49 | const bool isLocked = AAudio_tryUntilTrue( |
| 50 | [this]()->bool { return mLockStreams.try_lock(); } /* f */, |
| 51 | 50 /* times */, |
| 52 | 20 /* sleepMs */); |
| 53 | if (!isLocked) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 54 | result << "AAudioServiceEndpoint may be deadlocked\n"; |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 57 | result << " Direction: " << ((getDirection() == AAUDIO_DIRECTION_OUTPUT) |
| 58 | ? "OUTPUT" : "INPUT") << "\n"; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 59 | result << " Requested Device Id: " << mRequestedDeviceId << "\n"; |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 60 | result << " Device Ids: " << android::toString(getDeviceIds()).c_str() << "\n"; |
Phil Burk | a62fb95 | 2018-01-16 12:44:06 -0800 | [diff] [blame] | 61 | result << " Sample Rate: " << getSampleRate() << "\n"; |
| 62 | result << " Channel Count: " << getSamplesPerFrame() << "\n"; |
jiabin | a909409 | 2021-06-28 20:36:45 +0000 | [diff] [blame] | 63 | result << " Channel Mask: 0x" << std::hex << getChannelMask() << std::dec << "\n"; |
Phil Burk | ed782c8 | 2022-02-08 21:43:53 +0000 | [diff] [blame] | 64 | result << " Format: " << getFormat() |
| 65 | << " (" << audio_format_to_string(getFormat()) << ")\n"; |
Phil Burk | a62fb95 | 2018-01-16 12:44:06 -0800 | [diff] [blame] | 66 | result << " Frames Per Burst: " << mFramesPerBurst << "\n"; |
| 67 | result << " Usage: " << getUsage() << "\n"; |
| 68 | result << " ContentType: " << getContentType() << "\n"; |
| 69 | result << " InputPreset: " << getInputPreset() << "\n"; |
| 70 | result << " Reference Count: " << mOpenCount << "\n"; |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 71 | result << " Session Id: " << getSessionId() << "\n"; |
Eric Laurent | d17c850 | 2019-10-24 15:58:35 -0700 | [diff] [blame] | 72 | result << " Privacy Sensitive: " << isPrivacySensitive() << "\n"; |
Robert Wu | 310037a | 2022-09-06 21:48:18 +0000 | [diff] [blame] | 73 | result << " Hardware Channel Count:" << getHardwareSamplesPerFrame() << "\n"; |
| 74 | result << " Hardware Format: " << getHardwareFormat() << " (" |
| 75 | << audio_format_to_string(getHardwareFormat()) << ")\n"; |
| 76 | result << " Hardware Sample Rate: " << getHardwareSampleRate() << "\n"; |
Phil Burk | be0a5b6 | 2017-10-12 15:56:00 -0700 | [diff] [blame] | 77 | result << " Connected: " << mConnected.load() << "\n"; |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 78 | result << " Registered Streams:" << "\n"; |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 79 | result << AAudioServiceStreamShared::dumpHeader() << "\n"; |
Chih-Hung Hsieh | 3ef324d | 2018-12-11 11:48:12 -0800 | [diff] [blame] | 80 | for (const auto& stream : mRegisteredStreams) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 81 | result << stream->dump() << "\n"; |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | if (isLocked) { |
| 85 | mLockStreams.unlock(); |
| 86 | } |
| 87 | return result.str(); |
| 88 | } |
| 89 | |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 90 | // @return true if stream found |
| 91 | bool AAudioServiceEndpoint::isStreamRegistered(audio_port_handle_t portHandle) { |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 92 | const std::lock_guard<std::mutex> lock(mLockStreams); |
Chih-Hung Hsieh | 3ef324d | 2018-12-11 11:48:12 -0800 | [diff] [blame] | 93 | for (const auto& stream : mRegisteredStreams) { |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 94 | if (stream->getPortHandle() == portHandle) { |
| 95 | return true; |
| 96 | } |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 101 | std::vector<android::sp<AAudioServiceStreamBase>> |
| 102 | AAudioServiceEndpoint::disconnectRegisteredStreams() { |
| 103 | std::vector<android::sp<AAudioServiceStreamBase>> streamsDisconnected; |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 104 | { |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 105 | const std::lock_guard<std::mutex> lock(mLockStreams); |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 106 | mRegisteredStreams.swap(streamsDisconnected); |
| 107 | } |
Phil Burk | be0a5b6 | 2017-10-12 15:56:00 -0700 | [diff] [blame] | 108 | mConnected.store(false); |
Phil Burk | 8e2255a | 2020-06-05 09:58:26 -0700 | [diff] [blame] | 109 | // We need to stop all the streams before we disconnect them. |
| 110 | // Otherwise there is a race condition where the first disconnected app |
| 111 | // tries to reopen a stream as MMAP but is blocked by the second stream, |
| 112 | // which hasn't stopped yet. Then the first app ends up with a Legacy stream. |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 113 | for (const auto &stream : streamsDisconnected) { |
Phil Burk | 8e2255a | 2020-06-05 09:58:26 -0700 | [diff] [blame] | 114 | ALOGD("%s() - stop(), port = %d", __func__, stream->getPortHandle()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 115 | stream->stop(); |
Phil Burk | 8e2255a | 2020-06-05 09:58:26 -0700 | [diff] [blame] | 116 | } |
| 117 | for (const auto &stream : streamsDisconnected) { |
| 118 | ALOGD("%s() - disconnect(), port = %d", __func__, stream->getPortHandle()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 119 | stream->disconnect(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 120 | } |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 121 | return streamsDisconnected; |
| 122 | } |
| 123 | |
| 124 | void AAudioServiceEndpoint::releaseRegisteredStreams() { |
| 125 | // List of streams to be closed after we disconnect everything. |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 126 | const std::vector<android::sp<AAudioServiceStreamBase>> streamsToClose |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 127 | = disconnectRegisteredStreams(); |
| 128 | |
| 129 | // Close outside the lock to avoid recursive locks. |
| 130 | AAudioService *aaudioService = AAudioClientTracker::getInstance().getAAudioService(); |
| 131 | for (const auto& serviceStream : streamsToClose) { |
| 132 | ALOGD("%s() - close stream 0x%08X", __func__, serviceStream->getHandle()); |
| 133 | aaudioService->closeStream(serviceStream); |
| 134 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 135 | } |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 136 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 137 | aaudio_result_t AAudioServiceEndpoint::registerStream(const sp<AAudioServiceStreamBase>& stream) { |
| 138 | const std::lock_guard<std::mutex> lock(mLockStreams); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 139 | mRegisteredStreams.push_back(stream); |
| 140 | return AAUDIO_OK; |
| 141 | } |
| 142 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 143 | aaudio_result_t AAudioServiceEndpoint::unregisterStream(const sp<AAudioServiceStreamBase>& stream) { |
| 144 | const std::lock_guard<std::mutex> lock(mLockStreams); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 145 | mRegisteredStreams.erase(std::remove( |
| 146 | mRegisteredStreams.begin(), mRegisteredStreams.end(), stream), |
| 147 | mRegisteredStreams.end()); |
| 148 | return AAUDIO_OK; |
| 149 | } |
| 150 | |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 151 | bool AAudioServiceEndpoint::matches(const AAudioStreamConfiguration& configuration) { |
Phil Burk | be0a5b6 | 2017-10-12 15:56:00 -0700 | [diff] [blame] | 152 | if (!mConnected.load()) { |
| 153 | return false; // Only use an endpoint if it is connected to a device. |
| 154 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 155 | if (configuration.getDirection() != getDirection()) { |
| 156 | return false; |
| 157 | } |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 158 | if (!configuration.getDeviceIds().empty() && |
| 159 | !android::areDeviceIdsEqual(configuration.getDeviceIds(), getDeviceIds())) { |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 160 | return false; |
| 161 | } |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 162 | if (configuration.getSessionId() != AAUDIO_SESSION_ID_ALLOCATE && |
| 163 | configuration.getSessionId() != getSessionId()) { |
| 164 | return false; |
| 165 | } |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 166 | if (configuration.getSampleRate() != AAUDIO_UNSPECIFIED && |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 167 | configuration.getSampleRate() != getSampleRate()) { |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 168 | return false; |
| 169 | } |
| 170 | if (configuration.getSamplesPerFrame() != AAUDIO_UNSPECIFIED && |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 171 | configuration.getSamplesPerFrame() != getSamplesPerFrame()) { |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 172 | return false; |
| 173 | } |
jiabin | a909409 | 2021-06-28 20:36:45 +0000 | [diff] [blame] | 174 | if (configuration.getChannelMask() != AAUDIO_UNSPECIFIED && |
| 175 | configuration.getChannelMask() != getChannelMask()) { |
| 176 | return false; |
| 177 | } |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 178 | return true; |
| 179 | } |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 180 | |
| 181 | // static |
| 182 | audio_attributes_t AAudioServiceEndpoint::getAudioAttributesFrom( |
| 183 | const AAudioStreamParameters *params) { |
| 184 | if (params == nullptr) { |
| 185 | return {}; |
| 186 | } |
| 187 | const aaudio_direction_t direction = params->getDirection(); |
| 188 | |
| 189 | const audio_content_type_t contentType = |
| 190 | AAudioConvert_contentTypeToInternal(params->getContentType()); |
| 191 | // Usage only used for OUTPUT |
| 192 | const audio_usage_t usage = (direction == AAUDIO_DIRECTION_OUTPUT) |
| 193 | ? AAudioConvert_usageToInternal(params->getUsage()) |
| 194 | : AUDIO_USAGE_UNKNOWN; |
| 195 | const audio_source_t source = (direction == AAUDIO_DIRECTION_INPUT) |
| 196 | ? AAudioConvert_inputPresetToAudioSource(params->getInputPreset()) |
| 197 | : AUDIO_SOURCE_DEFAULT; |
| 198 | audio_flags_mask_t flags; |
jiabin | 3b5e5ed | 2024-11-18 19:26:27 +0000 | [diff] [blame] | 199 | std::string tags; |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 200 | if (direction == AAUDIO_DIRECTION_OUTPUT) { |
Eric Laurent | fa7fe4d | 2023-04-21 15:58:59 +0200 | [diff] [blame] | 201 | flags = AAudio_computeAudioFlagsMask( |
Jean-Michel Trivi | 656bfdc | 2021-09-20 18:42:37 -0700 | [diff] [blame] | 202 | params->getAllowedCapturePolicy(), |
| 203 | params->getSpatializationBehavior(), |
Eric Laurent | fa7fe4d | 2023-04-21 15:58:59 +0200 | [diff] [blame] | 204 | params->isContentSpatialized(), |
| 205 | AUDIO_OUTPUT_FLAG_FAST); |
jiabin | 3b5e5ed | 2024-11-18 19:26:27 +0000 | [diff] [blame] | 206 | tags = params->getTagsAsString(); |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 207 | } else { |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 208 | flags = static_cast<audio_flags_mask_t>(AUDIO_FLAG_LOW_LATENCY |
| 209 | | AAudioConvert_privacySensitiveToAudioFlagsMask(params->isPrivacySensitive())); |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 210 | } |
Jiabin Huang | 51a8a77 | 2024-10-30 21:57:48 +0000 | [diff] [blame] | 211 | audio_attributes_t nativeAttributes = { |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 212 | .content_type = contentType, |
| 213 | .usage = usage, |
| 214 | .source = source, |
| 215 | .flags = flags, |
Jiabin Huang | 51a8a77 | 2024-10-30 21:57:48 +0000 | [diff] [blame] | 216 | .tags = "" |
| 217 | }; |
jiabin | 3b5e5ed | 2024-11-18 19:26:27 +0000 | [diff] [blame] | 218 | if (!tags.empty()) { |
| 219 | strncpy(nativeAttributes.tags, tags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE); |
| 220 | nativeAttributes.tags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1] = '\0'; |
Jiabin Huang | 51a8a77 | 2024-10-30 21:57:48 +0000 | [diff] [blame] | 221 | } |
| 222 | return nativeAttributes; |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 223 | } |