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