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 | |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 21 | #include <sstream> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 22 | //#include <time.h> |
| 23 | //#include <pthread.h> |
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 | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 38 | #include "utility/HandleTracker.h" |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 39 | |
| 40 | using namespace android; |
| 41 | using namespace aaudio; |
| 42 | |
| 43 | typedef enum |
| 44 | { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 45 | AAUDIO_HANDLE_TYPE_STREAM |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 46 | } aaudio_service_handle_type_t; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 47 | static_assert(AAUDIO_HANDLE_TYPE_STREAM < HANDLE_TRACKER_MAX_TYPES, "Too many handle types."); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 48 | |
| 49 | android::AAudioService::AAudioService() |
| 50 | : BnAAudioService() { |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 51 | mCachedProcessId = getpid(); |
| 52 | mCachedUserId = getuid(); // TODO consider using geteuid() |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 53 | AAudioClientTracker::getInstance().setAAudioService(this); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | AAudioService::~AAudioService() { |
| 57 | } |
| 58 | |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 59 | status_t AAudioService::dump(int fd, const Vector<String16>& args) { |
| 60 | std::string result; |
| 61 | |
| 62 | if (!dumpAllowed()) { |
| 63 | std::stringstream ss; |
| 64 | ss << "Permission denial: can't dump AAudioService from pid=" |
| 65 | << IPCThreadState::self()->getCallingPid() << ", uid=" |
| 66 | << IPCThreadState::self()->getCallingUid() << "\n"; |
| 67 | result = ss.str(); |
| 68 | ALOGW("%s", result.c_str()); |
| 69 | } else { |
| 70 | result = mHandleTracker.dump() + AAudioEndpointManager::getInstance().dump(); |
| 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 | |
| 89 | if (sharingMode != AAUDIO_SHARING_MODE_EXCLUSIVE && sharingMode != AAUDIO_SHARING_MODE_SHARED) { |
| 90 | ALOGE("AAudioService::openStream(): unrecognized sharing mode = %d", sharingMode); |
| 91 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 92 | } |
| 93 | |
| 94 | if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 95 | serviceStream = new AAudioServiceStreamMMAP(); |
| 96 | result = serviceStream->open(request, configurationOutput); |
| 97 | if (result != AAUDIO_OK) { |
| 98 | // fall back to using a shared stream |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 99 | ALOGW("AAudioService::openStream(), could not open in EXCLUSIVE mode"); |
| 100 | serviceStream.clear(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 101 | } else { |
| 102 | configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // if SHARED requested or if EXCLUSIVE failed |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 107 | if (sharingMode == AAUDIO_SHARING_MODE_SHARED |
| 108 | || (serviceStream == nullptr && !sharingModeMatchRequired)) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 109 | serviceStream = new AAudioServiceStreamShared(*this); |
| 110 | result = serviceStream->open(request, configurationOutput); |
| 111 | configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_SHARED); |
| 112 | } |
| 113 | |
| 114 | if (result != AAUDIO_OK) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 115 | serviceStream.clear(); |
| 116 | ALOGE("AAudioService::openStream(): failed, return %d = %s", |
| 117 | result, AAudio_convertResultToText(result)); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 118 | return result; |
| 119 | } else { |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 120 | const uid_t ownerUserId = request.getUserId(); // only set by service, not by client |
| 121 | serviceStream->setOwnerUserId(ownerUserId); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 122 | aaudio_handle_t handle = mHandleTracker.put(AAUDIO_HANDLE_TYPE_STREAM, serviceStream.get()); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 123 | if (handle < 0) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 124 | ALOGE("AAudioService::openStream(): handle table full"); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 125 | serviceStream.clear(); |
| 126 | } else { |
| 127 | ALOGD("AAudioService::openStream(): handle = 0x%08X", handle); |
| 128 | serviceStream->setHandle(handle); |
| 129 | pid_t pid = request.getProcessId(); |
Phil Burk | b63320a | 2017-06-30 10:28:20 -0700 | [diff] [blame^] | 130 | serviceStream->setOwnerProcessId(pid); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 131 | AAudioClientTracker::getInstance().registerClientStream(pid, serviceStream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 132 | } |
| 133 | return handle; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) { |
| 138 | AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *) |
| 139 | mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM, |
| 140 | streamHandle); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 141 | ALOGD("AAudioService.closeStream(0x%08X)", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 142 | if (serviceStream != nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 143 | serviceStream->close(); |
Phil Burk | b63320a | 2017-06-30 10:28:20 -0700 | [diff] [blame^] | 144 | pid_t pid = serviceStream->getOwnerProcessId(); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 145 | AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 146 | return AAUDIO_OK; |
| 147 | } |
| 148 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 149 | } |
| 150 | |
| 151 | AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream( |
| 152 | aaudio_handle_t streamHandle) const { |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 153 | AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *) |
| 154 | mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM, (aaudio_handle_t)streamHandle); |
| 155 | if (serviceStream != nullptr) { |
| 156 | // Only allow owner or the aaudio service to access the stream. |
| 157 | const uid_t callingUserId = IPCThreadState::self()->getCallingUid(); |
| 158 | const uid_t ownerUserId = serviceStream->getOwnerUserId(); |
| 159 | bool callerOwnsIt = callingUserId == ownerUserId; |
| 160 | bool serverCalling = callingUserId == mCachedUserId; |
| 161 | bool serverOwnsIt = ownerUserId == mCachedUserId; |
| 162 | bool allowed = callerOwnsIt || serverCalling || serverOwnsIt; |
| 163 | if (!allowed) { |
| 164 | ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d", |
| 165 | callingUserId, streamHandle, ownerUserId); |
| 166 | serviceStream = nullptr; |
| 167 | } |
| 168 | } |
| 169 | return serviceStream; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | aaudio_result_t AAudioService::getStreamDescription( |
| 173 | aaudio_handle_t streamHandle, |
| 174 | aaudio::AudioEndpointParcelable &parcelable) { |
| 175 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 176 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 177 | ALOGE("AAudioService::getStreamDescription(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 178 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 179 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 180 | aaudio_result_t result = serviceStream->getDescription(parcelable); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 181 | // parcelable.dump(); |
| 182 | return result; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) { |
| 186 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 187 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 188 | ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 189 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 190 | } |
| 191 | aaudio_result_t result = serviceStream->start(); |
| 192 | return result; |
| 193 | } |
| 194 | |
| 195 | aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) { |
| 196 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 197 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 198 | ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 199 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 200 | } |
| 201 | aaudio_result_t result = serviceStream->pause(); |
| 202 | return result; |
| 203 | } |
| 204 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 205 | aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) { |
| 206 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
| 207 | if (serviceStream == nullptr) { |
| 208 | ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle); |
| 209 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 210 | } |
| 211 | aaudio_result_t result = serviceStream->stop(); |
| 212 | return result; |
| 213 | } |
| 214 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 215 | aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) { |
| 216 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 217 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 218 | ALOGE("AAudioService::flushStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 219 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 220 | } |
| 221 | return serviceStream->flush(); |
| 222 | } |
| 223 | |
| 224 | aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 225 | pid_t clientThreadId, |
| 226 | int64_t periodNanoseconds) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 227 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 228 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 229 | ALOGE("AAudioService::registerAudioThread(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 230 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 231 | } |
| 232 | if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) { |
| 233 | ALOGE("AAudioService::registerAudioThread(), thread already registered"); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 234 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 235 | } |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 236 | |
| 237 | const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 238 | serviceStream->setRegisteredThread(clientThreadId); |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 239 | int err = android::requestPriority(ownerPid, clientThreadId, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 240 | DEFAULT_AUDIO_PRIORITY, true /* isForApp */); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 241 | if (err != 0){ |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 242 | ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d", |
| 243 | clientThreadId, errno, DEFAULT_AUDIO_PRIORITY); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 244 | return AAUDIO_ERROR_INTERNAL; |
| 245 | } else { |
| 246 | return AAUDIO_OK; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 251 | pid_t clientThreadId) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 252 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 253 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 254 | ALOGE("AAudioService::unregisterAudioThread(), illegal stream handle = 0x%0x", |
| 255 | streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 256 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 257 | } |
| 258 | if (serviceStream->getRegisteredThread() != clientThreadId) { |
| 259 | ALOGE("AAudioService::unregisterAudioThread(), wrong thread"); |
| 260 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 261 | } |
| 262 | serviceStream->setRegisteredThread(0); |
| 263 | return AAUDIO_OK; |
| 264 | } |