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> |
| 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" |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 36 | #include "ServiceUtilities.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; |
| 61 | ss << "Permission denial: can't dump AAudioService from pid=" |
| 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 | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 81 | aaudio_handle_t AAudioService::openStream(const aaudio::AAudioStreamRequest &request, |
| 82 | aaudio::AAudioStreamConfiguration &configurationOutput) { |
| 83 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 84 | sp<AAudioServiceStreamBase> serviceStream; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 85 | const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration(); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 86 | bool sharingModeMatchRequired = request.isSharingModeMatchRequired(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 87 | aaudio_sharing_mode_t sharingMode = configurationInput.getSharingMode(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 88 | |
Phil Burk | 9169294 | 2017-06-30 12:23:05 -0700 | [diff] [blame] | 89 | // Enforce limit on client processes. |
| 90 | pid_t pid = request.getProcessId(); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 91 | if (pid != mAudioClient.clientPid) { |
Phil Burk | 9169294 | 2017-06-30 12:23:05 -0700 | [diff] [blame] | 92 | int32_t count = AAudioClientTracker::getInstance().getStreamCount(pid); |
| 93 | if (count >= MAX_STREAMS_PER_PROCESS) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 94 | ALOGE("openStream(): exceeded max streams per process %d >= %d", |
Phil Burk | 9169294 | 2017-06-30 12:23:05 -0700 | [diff] [blame] | 95 | count, MAX_STREAMS_PER_PROCESS); |
| 96 | return AAUDIO_ERROR_UNAVAILABLE; |
| 97 | } |
| 98 | } |
| 99 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 100 | if (sharingMode != AAUDIO_SHARING_MODE_EXCLUSIVE && sharingMode != AAUDIO_SHARING_MODE_SHARED) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 101 | ALOGE("openStream(): unrecognized sharing mode = %d", sharingMode); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 102 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 103 | } |
| 104 | |
| 105 | if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE) { |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 106 | // only trust audioserver for in service indication |
| 107 | bool inService = false; |
| 108 | if (mAudioClient.clientPid == IPCThreadState::self()->getCallingPid() && |
| 109 | mAudioClient.clientUid == IPCThreadState::self()->getCallingUid()) { |
| 110 | inService = request.isInService(); |
| 111 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 112 | serviceStream = new AAudioServiceStreamMMAP(*this, inService); |
| 113 | result = serviceStream->open(request); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 114 | if (result != AAUDIO_OK) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 115 | // 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] | 116 | ALOGW("openStream(), could not open in EXCLUSIVE mode"); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 117 | serviceStream.clear(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | // if SHARED requested or if EXCLUSIVE failed |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 122 | if (sharingMode == AAUDIO_SHARING_MODE_SHARED |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 123 | || (serviceStream.get() == nullptr && !sharingModeMatchRequired)) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 124 | serviceStream = new AAudioServiceStreamShared(*this); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 125 | result = serviceStream->open(request); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | if (result != AAUDIO_OK) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 129 | serviceStream.clear(); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 130 | ALOGE("openStream(): failed, return %d = %s", |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 131 | result, AAudio_convertResultToText(result)); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 132 | return result; |
| 133 | } else { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 134 | aaudio_handle_t handle = mStreamTracker.addStreamForHandle(serviceStream.get()); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 135 | ALOGD("openStream(): handle = 0x%08X", handle); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 136 | serviceStream->setHandle(handle); |
| 137 | pid_t pid = request.getProcessId(); |
| 138 | AAudioClientTracker::getInstance().registerClientStream(pid, serviceStream); |
| 139 | configurationOutput.copyFrom(*serviceStream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 140 | return handle; |
| 141 | } |
| 142 | } |
| 143 | |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 144 | // If a close request is pending then close the stream |
| 145 | bool AAudioService::releaseStream(const sp<AAudioServiceStreamBase> &serviceStream) { |
| 146 | bool closed = false; |
| 147 | if ((serviceStream->decrementServiceReferenceCount() == 0) && serviceStream->isCloseNeeded()) { |
| 148 | // removeStreamByHandle() uses a lock so that if there are two simultaneous closes |
| 149 | // then only one will get the pointer and do the close. |
| 150 | sp<AAudioServiceStreamBase> foundStream = mStreamTracker.removeStreamByHandle(serviceStream->getHandle()); |
| 151 | if (foundStream.get() != nullptr) { |
| 152 | foundStream->close(); |
| 153 | pid_t pid = foundStream->getOwnerProcessId(); |
| 154 | AAudioClientTracker::getInstance().unregisterClientStream(pid, foundStream); |
| 155 | } |
| 156 | closed = true; |
| 157 | } |
| 158 | return closed; |
| 159 | } |
| 160 | |
| 161 | aaudio_result_t AAudioService::checkForPendingClose( |
| 162 | const sp<AAudioServiceStreamBase> &serviceStream, |
| 163 | aaudio_result_t defaultResult) { |
| 164 | return releaseStream(serviceStream) ? AAUDIO_ERROR_INVALID_STATE : defaultResult; |
| 165 | } |
| 166 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 167 | aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) { |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 168 | // Check permission and ownership first. |
| 169 | sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 170 | if (serviceStream.get() == nullptr) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 171 | ALOGE("closeStream(0x%0x), illegal stream handle", streamHandle); |
Phil Burk | 9169294 | 2017-06-30 12:23:05 -0700 | [diff] [blame] | 172 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 173 | } |
| 174 | |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 175 | pid_t pid = serviceStream->getOwnerProcessId(); |
| 176 | AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 177 | |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 178 | serviceStream->setCloseNeeded(true); |
| 179 | (void) releaseStream(serviceStream); |
| 180 | return AAUDIO_OK; |
| 181 | } |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 182 | |
| 183 | sp<AAudioServiceStreamBase> AAudioService::convertHandleToServiceStream( |
| 184 | aaudio_handle_t streamHandle) { |
| 185 | sp<AAudioServiceStreamBase> serviceStream = mStreamTracker.getStreamByHandle(streamHandle); |
| 186 | if (serviceStream.get() != nullptr) { |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 187 | // Only allow owner or the aaudio service to access the stream. |
| 188 | const uid_t callingUserId = IPCThreadState::self()->getCallingUid(); |
| 189 | const uid_t ownerUserId = serviceStream->getOwnerUserId(); |
| 190 | bool callerOwnsIt = callingUserId == ownerUserId; |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 191 | bool serverCalling = callingUserId == mAudioClient.clientUid; |
| 192 | bool serverOwnsIt = ownerUserId == mAudioClient.clientUid; |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 193 | bool allowed = callerOwnsIt || serverCalling || serverOwnsIt; |
| 194 | if (!allowed) { |
| 195 | ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d", |
| 196 | callingUserId, streamHandle, ownerUserId); |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 197 | serviceStream.clear(); |
| 198 | } else { |
| 199 | serviceStream->incrementServiceReferenceCount(); |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | return serviceStream; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | aaudio_result_t AAudioService::getStreamDescription( |
| 206 | aaudio_handle_t streamHandle, |
| 207 | aaudio::AudioEndpointParcelable &parcelable) { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 208 | sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle); |
| 209 | if (serviceStream.get() == nullptr) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 210 | ALOGE("getStreamDescription(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 211 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 212 | } |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 213 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 214 | aaudio_result_t result = serviceStream->getDescription(parcelable); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 215 | // parcelable.dump(); |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 216 | return checkForPendingClose(serviceStream, result); |
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 | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 222 | ALOGE("startStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 223 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 224 | } |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 225 | |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 226 | aaudio_result_t result = serviceStream->start(); |
| 227 | return checkForPendingClose(serviceStream, result); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 231 | sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle); |
| 232 | if (serviceStream.get() == nullptr) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 233 | ALOGE("pauseStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 234 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 235 | } |
| 236 | aaudio_result_t result = serviceStream->pause(); |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 237 | return checkForPendingClose(serviceStream, result); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 240 | aaudio_result_t AAudioService::stopStream(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("stopStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 244 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 245 | } |
| 246 | aaudio_result_t result = serviceStream->stop(); |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 247 | return checkForPendingClose(serviceStream, result); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 250 | aaudio_result_t AAudioService::flushStream(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("flushStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 254 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 255 | } |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 256 | aaudio_result_t result = serviceStream->flush(); |
| 257 | return checkForPendingClose(serviceStream, result); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 261 | pid_t clientThreadId, |
| 262 | int64_t periodNanoseconds) { |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 263 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 264 | sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle); |
| 265 | if (serviceStream.get() == nullptr) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 266 | ALOGE("registerAudioThread(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 267 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 268 | } |
| 269 | if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) { |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 270 | ALOGE("AAudioService::registerAudioThread(), thread already registered"); |
| 271 | result = AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 272 | } else { |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 273 | const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review |
| 274 | serviceStream->setRegisteredThread(clientThreadId); |
| 275 | int err = android::requestPriority(ownerPid, clientThreadId, |
| 276 | DEFAULT_AUDIO_PRIORITY, true /* isForApp */); |
| 277 | if (err != 0) { |
| 278 | ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d", |
| 279 | clientThreadId, errno, DEFAULT_AUDIO_PRIORITY); |
| 280 | result = AAUDIO_ERROR_INTERNAL; |
| 281 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 282 | } |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 283 | return checkForPendingClose(serviceStream, result); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 287 | pid_t clientThreadId) { |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 288 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 289 | sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle); |
| 290 | if (serviceStream.get() == nullptr) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 291 | ALOGE("unregisterAudioThread(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 292 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 293 | } |
| 294 | if (serviceStream->getRegisteredThread() != clientThreadId) { |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 295 | ALOGE("AAudioService::unregisterAudioThread(), wrong thread"); |
| 296 | result = AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 297 | } else { |
| 298 | serviceStream->setRegisteredThread(0); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 299 | } |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 300 | return checkForPendingClose(serviceStream, result); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 301 | } |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 302 | |
| 303 | aaudio_result_t AAudioService::startClient(aaudio_handle_t streamHandle, |
| 304 | const android::AudioClient& client, |
| 305 | audio_port_handle_t *clientHandle) { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 306 | sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle); |
| 307 | if (serviceStream.get() == nullptr) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 308 | ALOGE("startClient(), illegal stream handle = 0x%0x", streamHandle); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 309 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 310 | } |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 311 | aaudio_result_t result = serviceStream->startClient(client, clientHandle); |
| 312 | return checkForPendingClose(serviceStream, result); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | aaudio_result_t AAudioService::stopClient(aaudio_handle_t streamHandle, |
| 316 | audio_port_handle_t clientHandle) { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 317 | sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle); |
| 318 | if (serviceStream.get() == nullptr) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 319 | ALOGE("stopClient(), illegal stream handle = 0x%0x", streamHandle); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 320 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 321 | } |
Phil Burk | 9486252 | 2017-09-13 21:31:36 -0700 | [diff] [blame] | 322 | aaudio_result_t result = serviceStream->stopClient(clientHandle); |
| 323 | return checkForPendingClose(serviceStream, result); |
Eric Laurent | a54f128 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 324 | } |