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