Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | #define LOG_TAG "AAudioService" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 21 | #include <iomanip> |
| 22 | #include <iostream> |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 23 | #include <sstream> |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 24 | |
Phil Burk | a4eb0d8 | 2017-04-12 15:44:06 -0700 | [diff] [blame] | 25 | #include <aaudio/AAudio.h> |
Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 26 | #include <mediautils/ServiceUtilities.h> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 27 | #include <utils/String16.h> |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 28 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 29 | #include "binding/AAudioServiceMessage.h" |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 30 | #include "AAudioClientTracker.h" |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 31 | #include "AAudioEndpointManager.h" |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 32 | #include "AAudioService.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 33 | #include "AAudioServiceStreamMMAP.h" |
| 34 | #include "AAudioServiceStreamShared.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 35 | #include "binding/IAAudioService.h" |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 36 | |
| 37 | using namespace android; |
| 38 | using namespace aaudio; |
| 39 | |
Phil Burk | 9169294 | 2017-06-30 12:23:05 -0700 | [diff] [blame] | 40 | #define MAX_STREAMS_PER_PROCESS 8 |
| 41 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 42 | using android::AAudioService; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 43 | |
| 44 | android::AAudioService::AAudioService() |
| 45 | : BnAAudioService() { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 46 | mAudioClient.clientUid = getuid(); // TODO consider using geteuid() |
| 47 | mAudioClient.clientPid = getpid(); |
| 48 | mAudioClient.packageName = String16(""); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 49 | AAudioClientTracker::getInstance().setAAudioService(this); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | AAudioService::~AAudioService() { |
| 53 | } |
| 54 | |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 55 | status_t AAudioService::dump(int fd, const Vector<String16>& args) { |
| 56 | std::string result; |
| 57 | |
| 58 | if (!dumpAllowed()) { |
| 59 | std::stringstream ss; |
Andy Hung | 6357b5f | 2018-10-22 19:47:04 -0700 | [diff] [blame] | 60 | ss << "Permission Denial: can't dump AAudioService from pid=" |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 61 | << IPCThreadState::self()->getCallingPid() << ", uid=" |
| 62 | << IPCThreadState::self()->getCallingUid() << "\n"; |
| 63 | result = ss.str(); |
| 64 | ALOGW("%s", result.c_str()); |
| 65 | } else { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 66 | result = "------------ AAudio Service ------------\n" |
| 67 | + mStreamTracker.dump() |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 68 | + AAudioClientTracker::getInstance().dump() |
| 69 | + AAudioEndpointManager::getInstance().dump(); |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 70 | } |
| 71 | (void)write(fd, result.c_str(), result.size()); |
| 72 | return NO_ERROR; |
| 73 | } |
| 74 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 75 | void AAudioService::registerClient(const sp<IAAudioClient>& client) { |
| 76 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 77 | AAudioClientTracker::getInstance().registerClient(pid, client); |
| 78 | } |
| 79 | |
Phil Burk | 2ebf662 | 2019-04-17 11:10:25 -0700 | [diff] [blame] | 80 | bool AAudioService::isCallerInService() { |
| 81 | return mAudioClient.clientPid == IPCThreadState::self()->getCallingPid() && |
| 82 | mAudioClient.clientUid == IPCThreadState::self()->getCallingUid(); |
| 83 | } |
| 84 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 85 | aaudio_handle_t AAudioService::openStream(const aaudio::AAudioStreamRequest &request, |
| 86 | aaudio::AAudioStreamConfiguration &configurationOutput) { |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 87 | // A lock in is used to order the opening of endpoints when an |
| 88 | // EXCLUSIVE endpoint is stolen. We want the order to be: |
| 89 | // 1) Thread A opens exclusive MMAP endpoint |
| 90 | // 2) Thread B wants to open an exclusive MMAP endpoint so it steals the one from A |
| 91 | // under this lock. |
| 92 | // 3) Thread B opens a shared MMAP endpoint. |
| 93 | // 4) Thread A can then get the lock and also open a shared stream. |
| 94 | // Without the lock. Thread A might sneak in and reallocate an exclusive stream |
| 95 | // before B can open the shared stream. |
| 96 | std::unique_lock<std::recursive_mutex> lock(mOpenLock); |
| 97 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 98 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 99 | sp<AAudioServiceStreamBase> serviceStream; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 100 | const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration(); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 101 | bool sharingModeMatchRequired = request.isSharingModeMatchRequired(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 102 | aaudio_sharing_mode_t sharingMode = configurationInput.getSharingMode(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 103 | |
Phil Burk | 9169294 | 2017-06-30 12:23:05 -0700 | [diff] [blame] | 104 | // Enforce limit on client processes. |
| 105 | pid_t pid = request.getProcessId(); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 106 | if (pid != mAudioClient.clientPid) { |
Phil Burk | 9169294 | 2017-06-30 12:23:05 -0700 | [diff] [blame] | 107 | int32_t count = AAudioClientTracker::getInstance().getStreamCount(pid); |
| 108 | if (count >= MAX_STREAMS_PER_PROCESS) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 109 | ALOGE("openStream(): exceeded max streams per process %d >= %d", |
Phil Burk | 9169294 | 2017-06-30 12:23:05 -0700 | [diff] [blame] | 110 | count, MAX_STREAMS_PER_PROCESS); |
| 111 | return AAUDIO_ERROR_UNAVAILABLE; |
| 112 | } |
| 113 | } |
| 114 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 115 | if (sharingMode != AAUDIO_SHARING_MODE_EXCLUSIVE && sharingMode != AAUDIO_SHARING_MODE_SHARED) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 116 | ALOGE("openStream(): unrecognized sharing mode = %d", sharingMode); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 117 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 118 | } |
| 119 | |
Phil Burk | 836f9df | 2020-05-29 13:20:28 -0700 | [diff] [blame] | 120 | if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE |
| 121 | && AAudioClientTracker::getInstance().isExclusiveEnabled(request.getProcessId())) { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 122 | // only trust audioserver for in service indication |
| 123 | bool inService = false; |
Phil Burk | 2ebf662 | 2019-04-17 11:10:25 -0700 | [diff] [blame] | 124 | if (isCallerInService()) { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 125 | inService = request.isInService(); |
| 126 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 127 | serviceStream = new AAudioServiceStreamMMAP(*this, inService); |
| 128 | result = serviceStream->open(request); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 129 | if (result != AAUDIO_OK) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 130 | // Clear it so we can possibly fall back to using a shared stream. |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 131 | ALOGW("openStream(), could not open in EXCLUSIVE mode"); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 132 | serviceStream.clear(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Phil Burk | a3901e9 | 2018-10-08 13:54:38 -0700 | [diff] [blame] | 136 | // Try SHARED if SHARED requested or if EXCLUSIVE failed. |
Phil Burk | 15f97c9 | 2018-09-04 14:06:27 -0700 | [diff] [blame] | 137 | if (sharingMode == AAUDIO_SHARING_MODE_SHARED) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 138 | serviceStream = new AAudioServiceStreamShared(*this); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 139 | result = serviceStream->open(request); |
Phil Burk | 15f97c9 | 2018-09-04 14:06:27 -0700 | [diff] [blame] | 140 | } else if (serviceStream.get() == nullptr && !sharingModeMatchRequired) { |
| 141 | aaudio::AAudioStreamRequest modifiedRequest = request; |
| 142 | // Overwrite the original EXCLUSIVE mode with SHARED. |
| 143 | modifiedRequest.getConfiguration().setSharingMode(AAUDIO_SHARING_MODE_SHARED); |
| 144 | serviceStream = new AAudioServiceStreamShared(*this); |
| 145 | result = serviceStream->open(modifiedRequest); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | if (result != AAUDIO_OK) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 149 | serviceStream.clear(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 150 | return result; |
| 151 | } else { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 152 | aaudio_handle_t handle = mStreamTracker.addStreamForHandle(serviceStream.get()); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 153 | serviceStream->setHandle(handle); |
| 154 | pid_t pid = request.getProcessId(); |
| 155 | AAudioClientTracker::getInstance().registerClientStream(pid, serviceStream); |
| 156 | configurationOutput.copyFrom(*serviceStream); |
Phil Burk | a987670 | 2020-04-20 18:16:15 -0700 | [diff] [blame] | 157 | // Log open in MediaMetrics after we have the handle because we need the handle to |
| 158 | // create the metrics ID. |
| 159 | serviceStream->logOpen(handle); |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 160 | ALOGV("%s(): return handle = 0x%08X", __func__, handle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 161 | return handle; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) { |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 166 | // Check permission and ownership first. |
| 167 | sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 168 | if (serviceStream.get() == nullptr) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 169 | ALOGE("closeStream(0x%0x), illegal stream handle", streamHandle); |
Phil Burk | 9169294 | 2017-06-30 12:23:05 -0700 | [diff] [blame] | 170 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 171 | } |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 172 | return closeStream(serviceStream); |
| 173 | } |
Phil Burk | 9169294 | 2017-06-30 12:23:05 -0700 | [diff] [blame] | 174 | |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 175 | aaudio_result_t AAudioService::closeStream(sp<AAudioServiceStreamBase> serviceStream) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 176 | // This is protected by a lock in AAudioClientTracker. |
| 177 | // It is safe to unregister the same stream twice. |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 178 | pid_t pid = serviceStream->getOwnerProcessId(); |
| 179 | AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream); |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 180 | // This is protected by a lock in mStreamTracker. |
| 181 | // It is safe to remove the same stream twice. |
| 182 | mStreamTracker.removeStreamByHandle(serviceStream->getHandle()); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 183 | |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 184 | return serviceStream->close(); |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 185 | } |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 186 | |
| 187 | sp<AAudioServiceStreamBase> AAudioService::convertHandleToServiceStream( |
| 188 | aaudio_handle_t streamHandle) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 189 | sp<AAudioServiceStreamBase> serviceStream = mStreamTracker.getStreamByHandle( |
Phil Burk | 2fe718b | 2018-05-14 12:28:32 -0700 | [diff] [blame] | 190 | streamHandle); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 191 | if (serviceStream.get() != nullptr) { |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 192 | // Only allow owner or the aaudio service to access the stream. |
| 193 | const uid_t callingUserId = IPCThreadState::self()->getCallingUid(); |
| 194 | const uid_t ownerUserId = serviceStream->getOwnerUserId(); |
| 195 | bool callerOwnsIt = callingUserId == ownerUserId; |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 196 | bool serverCalling = callingUserId == mAudioClient.clientUid; |
| 197 | bool serverOwnsIt = ownerUserId == mAudioClient.clientUid; |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 198 | bool allowed = callerOwnsIt || serverCalling || serverOwnsIt; |
| 199 | if (!allowed) { |
| 200 | ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d", |
| 201 | callingUserId, streamHandle, ownerUserId); |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 202 | serviceStream.clear(); |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | return serviceStream; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | aaudio_result_t AAudioService::getStreamDescription( |
| 209 | aaudio_handle_t streamHandle, |
| 210 | aaudio::AudioEndpointParcelable &parcelable) { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 211 | sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle); |
| 212 | if (serviceStream.get() == nullptr) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 213 | ALOGE("getStreamDescription(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 214 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 215 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 216 | return serviceStream->getDescription(parcelable); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 220 | sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle); |
| 221 | if (serviceStream.get() == nullptr) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 222 | ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 223 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 224 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 225 | return serviceStream->start(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 229 | sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle); |
| 230 | if (serviceStream.get() == nullptr) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 231 | ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 232 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 233 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 234 | return serviceStream->pause(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 237 | aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 238 | sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle); |
| 239 | if (serviceStream.get() == nullptr) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 240 | ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 241 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 242 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 243 | return serviceStream->stop(); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 246 | aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 247 | sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle); |
| 248 | if (serviceStream.get() == nullptr) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 249 | ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 250 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 251 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 252 | return serviceStream->flush(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 256 | pid_t clientThreadId, |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 257 | int64_t /* periodNanoseconds */) { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 258 | sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle); |
| 259 | if (serviceStream.get() == nullptr) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 260 | ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 261 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 262 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 263 | int32_t priority = isCallerInService() |
| 264 | ? kRealTimeAudioPriorityService : kRealTimeAudioPriorityClient; |
| 265 | return serviceStream->registerAudioThread(clientThreadId, priority); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 269 | pid_t clientThreadId) { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 270 | sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle); |
| 271 | if (serviceStream.get() == nullptr) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 272 | ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 273 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 274 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 275 | return serviceStream->unregisterAudioThread(clientThreadId); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 276 | } |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 277 | |
| 278 | aaudio_result_t AAudioService::startClient(aaudio_handle_t streamHandle, |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 279 | const android::AudioClient& client, |
| 280 | const audio_attributes_t *attr, |
| 281 | audio_port_handle_t *clientHandle) { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 282 | sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle); |
| 283 | if (serviceStream.get() == nullptr) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 284 | ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 285 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 286 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 287 | return serviceStream->startClient(client, attr, clientHandle); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | aaudio_result_t AAudioService::stopClient(aaudio_handle_t streamHandle, |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 291 | audio_port_handle_t portHandle) { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 292 | sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle); |
| 293 | if (serviceStream.get() == nullptr) { |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 294 | ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 295 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 296 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 297 | return serviceStream->stopClient(portHandle); |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // This is only called internally when AudioFlinger wants to tear down a stream. |
| 301 | // So we do not have to check permissions. |
| 302 | aaudio_result_t AAudioService::disconnectStreamByPortHandle(audio_port_handle_t portHandle) { |
| 303 | ALOGD("%s(%d) called", __func__, portHandle); |
| 304 | sp<AAudioServiceStreamBase> serviceStream = |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 305 | mStreamTracker.findStreamByPortHandle(portHandle); |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 306 | if (serviceStream.get() == nullptr) { |
| 307 | ALOGE("%s(), could not find stream with portHandle = %d", __func__, portHandle); |
| 308 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 309 | } |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 310 | // This is protected by a lock and will just return if already stopped. |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 311 | aaudio_result_t result = serviceStream->stop(); |
| 312 | serviceStream->disconnect(); |
Phil Burk | 7ebbc11 | 2020-05-13 15:55:17 -0700 | [diff] [blame] | 313 | return result; |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 314 | } |