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