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(); |
| 130 | AAudioClientTracker::getInstance().registerClientStream(pid, serviceStream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 131 | } |
| 132 | return handle; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) { |
| 137 | AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *) |
| 138 | mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM, |
| 139 | streamHandle); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 140 | ALOGD("AAudioService.closeStream(0x%08X)", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 141 | if (serviceStream != nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 142 | serviceStream->close(); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 143 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 144 | AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 145 | return AAUDIO_OK; |
| 146 | } |
| 147 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 148 | } |
| 149 | |
| 150 | AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream( |
| 151 | aaudio_handle_t streamHandle) const { |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 152 | AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *) |
| 153 | mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM, (aaudio_handle_t)streamHandle); |
| 154 | if (serviceStream != nullptr) { |
| 155 | // Only allow owner or the aaudio service to access the stream. |
| 156 | const uid_t callingUserId = IPCThreadState::self()->getCallingUid(); |
| 157 | const uid_t ownerUserId = serviceStream->getOwnerUserId(); |
| 158 | bool callerOwnsIt = callingUserId == ownerUserId; |
| 159 | bool serverCalling = callingUserId == mCachedUserId; |
| 160 | bool serverOwnsIt = ownerUserId == mCachedUserId; |
| 161 | bool allowed = callerOwnsIt || serverCalling || serverOwnsIt; |
| 162 | if (!allowed) { |
| 163 | ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d", |
| 164 | callingUserId, streamHandle, ownerUserId); |
| 165 | serviceStream = nullptr; |
| 166 | } |
| 167 | } |
| 168 | return serviceStream; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | aaudio_result_t AAudioService::getStreamDescription( |
| 172 | aaudio_handle_t streamHandle, |
| 173 | aaudio::AudioEndpointParcelable &parcelable) { |
| 174 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 175 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 176 | ALOGE("AAudioService::getStreamDescription(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 177 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 178 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 179 | aaudio_result_t result = serviceStream->getDescription(parcelable); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 180 | // parcelable.dump(); |
| 181 | return result; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) { |
| 185 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 186 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 187 | ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 188 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 189 | } |
| 190 | aaudio_result_t result = serviceStream->start(); |
| 191 | return result; |
| 192 | } |
| 193 | |
| 194 | aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) { |
| 195 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 196 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 197 | ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 198 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 199 | } |
| 200 | aaudio_result_t result = serviceStream->pause(); |
| 201 | return result; |
| 202 | } |
| 203 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 204 | aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) { |
| 205 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
| 206 | if (serviceStream == nullptr) { |
| 207 | ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle); |
| 208 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 209 | } |
| 210 | aaudio_result_t result = serviceStream->stop(); |
| 211 | return result; |
| 212 | } |
| 213 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 214 | aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) { |
| 215 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 216 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 217 | ALOGE("AAudioService::flushStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 218 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 219 | } |
| 220 | return serviceStream->flush(); |
| 221 | } |
| 222 | |
| 223 | aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 224 | pid_t clientThreadId, |
| 225 | int64_t periodNanoseconds) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 226 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 227 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 228 | ALOGE("AAudioService::registerAudioThread(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 229 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 230 | } |
| 231 | if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) { |
| 232 | ALOGE("AAudioService::registerAudioThread(), thread already registered"); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 233 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 234 | } |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 235 | |
| 236 | const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 237 | serviceStream->setRegisteredThread(clientThreadId); |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 238 | int err = android::requestPriority(ownerPid, clientThreadId, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 239 | DEFAULT_AUDIO_PRIORITY, true /* isForApp */); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 240 | if (err != 0){ |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 241 | ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d", |
| 242 | clientThreadId, errno, DEFAULT_AUDIO_PRIORITY); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 243 | return AAUDIO_ERROR_INTERNAL; |
| 244 | } else { |
| 245 | return AAUDIO_OK; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 250 | pid_t clientThreadId) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 251 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 252 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 253 | ALOGE("AAudioService::unregisterAudioThread(), illegal stream handle = 0x%0x", |
| 254 | streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 255 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 256 | } |
| 257 | if (serviceStream->getRegisteredThread() != clientThreadId) { |
| 258 | ALOGE("AAudioService::unregisterAudioThread(), wrong thread"); |
| 259 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 260 | } |
| 261 | serviceStream->setRegisteredThread(0); |
| 262 | return AAUDIO_OK; |
| 263 | } |