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" |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 30 | #include "AAudioEndpointManager.h" |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 31 | #include "AAudioService.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 32 | #include "AAudioServiceStreamMMAP.h" |
| 33 | #include "AAudioServiceStreamShared.h" |
| 34 | #include "AAudioServiceStreamMMAP.h" |
| 35 | #include "binding/IAAudioService.h" |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 36 | #include "ServiceUtilities.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 37 | #include "utility/HandleTracker.h" |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 38 | |
| 39 | using namespace android; |
| 40 | using namespace aaudio; |
| 41 | |
| 42 | typedef enum |
| 43 | { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 44 | AAUDIO_HANDLE_TYPE_STREAM |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 45 | } aaudio_service_handle_type_t; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 46 | 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] | 47 | |
| 48 | android::AAudioService::AAudioService() |
| 49 | : BnAAudioService() { |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 50 | mCachedProcessId = getpid(); |
| 51 | mCachedUserId = getuid(); // TODO consider using geteuid() |
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 { |
| 68 | result = mHandleTracker.dump() + AAudioEndpointManager::getInstance().dump(); |
| 69 | } |
| 70 | (void)write(fd, result.c_str(), result.size()); |
| 71 | return NO_ERROR; |
| 72 | } |
| 73 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 74 | aaudio_handle_t AAudioService::openStream(const aaudio::AAudioStreamRequest &request, |
| 75 | aaudio::AAudioStreamConfiguration &configurationOutput) { |
| 76 | aaudio_result_t result = AAUDIO_OK; |
| 77 | AAudioServiceStreamBase *serviceStream = nullptr; |
| 78 | const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration(); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 79 | bool sharingModeMatchRequired = request.isSharingModeMatchRequired(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 80 | aaudio_sharing_mode_t sharingMode = configurationInput.getSharingMode(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 81 | |
| 82 | if (sharingMode != AAUDIO_SHARING_MODE_EXCLUSIVE && sharingMode != AAUDIO_SHARING_MODE_SHARED) { |
| 83 | ALOGE("AAudioService::openStream(): unrecognized sharing mode = %d", sharingMode); |
| 84 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 85 | } |
| 86 | |
| 87 | if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 88 | serviceStream = new AAudioServiceStreamMMAP(); |
| 89 | result = serviceStream->open(request, configurationOutput); |
| 90 | if (result != AAUDIO_OK) { |
| 91 | // fall back to using a shared stream |
| 92 | ALOGD("AAudioService::openStream(), EXCLUSIVE mode failed"); |
| 93 | delete serviceStream; |
| 94 | serviceStream = nullptr; |
| 95 | } else { |
| 96 | configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // if SHARED requested or if EXCLUSIVE failed |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 101 | if (sharingMode == AAUDIO_SHARING_MODE_SHARED |
| 102 | || (serviceStream == nullptr && !sharingModeMatchRequired)) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 103 | serviceStream = new AAudioServiceStreamShared(*this); |
| 104 | result = serviceStream->open(request, configurationOutput); |
| 105 | configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_SHARED); |
| 106 | } |
| 107 | |
| 108 | if (result != AAUDIO_OK) { |
| 109 | delete serviceStream; |
| 110 | ALOGE("AAudioService::openStream(): failed, return %d", result); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 111 | return result; |
| 112 | } else { |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 113 | const uid_t ownerUserId = request.getUserId(); // only set by service, not by client |
| 114 | serviceStream->setOwnerUserId(ownerUserId); |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 115 | aaudio_handle_t handle = mHandleTracker.put(AAUDIO_HANDLE_TYPE_STREAM, serviceStream); |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 116 | ALOGD("AAudioService::openStream(): handle = 0x%08X owned by %d", handle, ownerUserId); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 117 | if (handle < 0) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 118 | ALOGE("AAudioService::openStream(): handle table full"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 119 | delete serviceStream; |
| 120 | } |
| 121 | return handle; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) { |
| 126 | AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *) |
| 127 | mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM, |
| 128 | streamHandle); |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 129 | ALOGV("AAudioService.closeStream(0x%08X)", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 130 | if (serviceStream != nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 131 | serviceStream->close(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 132 | delete serviceStream; |
| 133 | return AAUDIO_OK; |
| 134 | } |
| 135 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 136 | } |
| 137 | |
| 138 | AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream( |
| 139 | aaudio_handle_t streamHandle) const { |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 140 | AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *) |
| 141 | mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM, (aaudio_handle_t)streamHandle); |
| 142 | if (serviceStream != nullptr) { |
| 143 | // Only allow owner or the aaudio service to access the stream. |
| 144 | const uid_t callingUserId = IPCThreadState::self()->getCallingUid(); |
| 145 | const uid_t ownerUserId = serviceStream->getOwnerUserId(); |
| 146 | bool callerOwnsIt = callingUserId == ownerUserId; |
| 147 | bool serverCalling = callingUserId == mCachedUserId; |
| 148 | bool serverOwnsIt = ownerUserId == mCachedUserId; |
| 149 | bool allowed = callerOwnsIt || serverCalling || serverOwnsIt; |
| 150 | if (!allowed) { |
| 151 | ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d", |
| 152 | callingUserId, streamHandle, ownerUserId); |
| 153 | serviceStream = nullptr; |
| 154 | } |
| 155 | } |
| 156 | return serviceStream; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | aaudio_result_t AAudioService::getStreamDescription( |
| 160 | aaudio_handle_t streamHandle, |
| 161 | aaudio::AudioEndpointParcelable &parcelable) { |
| 162 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 163 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 164 | ALOGE("AAudioService::getStreamDescription(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 165 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 166 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 167 | aaudio_result_t result = serviceStream->getDescription(parcelable); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 168 | // parcelable.dump(); |
| 169 | return result; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) { |
| 173 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 174 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 175 | ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 176 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 177 | } |
| 178 | aaudio_result_t result = serviceStream->start(); |
| 179 | return result; |
| 180 | } |
| 181 | |
| 182 | aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) { |
| 183 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 184 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 185 | ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 186 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 187 | } |
| 188 | aaudio_result_t result = serviceStream->pause(); |
| 189 | return result; |
| 190 | } |
| 191 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 192 | aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) { |
| 193 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
| 194 | if (serviceStream == nullptr) { |
| 195 | ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle); |
| 196 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 197 | } |
| 198 | aaudio_result_t result = serviceStream->stop(); |
| 199 | return result; |
| 200 | } |
| 201 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 202 | aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) { |
| 203 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 204 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 205 | ALOGE("AAudioService::flushStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 206 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 207 | } |
| 208 | return serviceStream->flush(); |
| 209 | } |
| 210 | |
| 211 | aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle, |
| 212 | pid_t clientThreadId, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 213 | int64_t periodNanoseconds) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 214 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 215 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 216 | ALOGE("AAudioService::registerAudioThread(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 217 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 218 | } |
| 219 | if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) { |
| 220 | ALOGE("AAudioService::registerAudioThread(), thread already registered"); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 221 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 222 | } |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 223 | |
| 224 | const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 225 | serviceStream->setRegisteredThread(clientThreadId); |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 226 | int err = android::requestPriority(ownerPid, clientThreadId, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 227 | DEFAULT_AUDIO_PRIORITY, true /* isForApp */); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 228 | if (err != 0){ |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 229 | ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d", |
| 230 | clientThreadId, errno, DEFAULT_AUDIO_PRIORITY); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 231 | return AAUDIO_ERROR_INTERNAL; |
| 232 | } else { |
| 233 | return AAUDIO_OK; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 238 | pid_t clientThreadId) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 239 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 240 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 241 | ALOGE("AAudioService::unregisterAudioThread(), illegal stream handle = 0x%0x", |
| 242 | streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 243 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 244 | } |
| 245 | if (serviceStream->getRegisteredThread() != clientThreadId) { |
| 246 | ALOGE("AAudioService::unregisterAudioThread(), wrong thread"); |
| 247 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 248 | } |
| 249 | serviceStream->setRegisteredThread(0); |
| 250 | return AAUDIO_OK; |
| 251 | } |