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