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() { |
| 50 | } |
| 51 | |
| 52 | AAudioService::~AAudioService() { |
| 53 | } |
| 54 | |
Andy Hung | 47c5e53 | 2017-06-26 18:28:00 -0700 | [diff] [blame] | 55 | status_t AAudioService::dump(int fd, const Vector<String16>& args) { |
| 56 | std::string result; |
| 57 | |
| 58 | if (!dumpAllowed()) { |
| 59 | std::stringstream ss; |
| 60 | ss << "Permission denial: can't dump AAudioService from pid=" |
| 61 | << IPCThreadState::self()->getCallingPid() << ", uid=" |
| 62 | << IPCThreadState::self()->getCallingUid() << "\n"; |
| 63 | result = ss.str(); |
| 64 | ALOGW("%s", result.c_str()); |
| 65 | } else { |
| 66 | result = mHandleTracker.dump() + AAudioEndpointManager::getInstance().dump(); |
| 67 | } |
| 68 | (void)write(fd, result.c_str(), result.size()); |
| 69 | return NO_ERROR; |
| 70 | } |
| 71 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 72 | aaudio_handle_t AAudioService::openStream(const aaudio::AAudioStreamRequest &request, |
| 73 | aaudio::AAudioStreamConfiguration &configurationOutput) { |
| 74 | aaudio_result_t result = AAUDIO_OK; |
| 75 | AAudioServiceStreamBase *serviceStream = nullptr; |
| 76 | const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration(); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 77 | bool sharingModeMatchRequired = request.isSharingModeMatchRequired(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 78 | aaudio_sharing_mode_t sharingMode = configurationInput.getSharingMode(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 79 | |
| 80 | if (sharingMode != AAUDIO_SHARING_MODE_EXCLUSIVE && sharingMode != AAUDIO_SHARING_MODE_SHARED) { |
| 81 | ALOGE("AAudioService::openStream(): unrecognized sharing mode = %d", sharingMode); |
| 82 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 83 | } |
| 84 | |
| 85 | if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 86 | serviceStream = new AAudioServiceStreamMMAP(); |
| 87 | result = serviceStream->open(request, configurationOutput); |
| 88 | if (result != AAUDIO_OK) { |
| 89 | // fall back to using a shared stream |
| 90 | ALOGD("AAudioService::openStream(), EXCLUSIVE mode failed"); |
| 91 | delete serviceStream; |
| 92 | serviceStream = nullptr; |
| 93 | } else { |
| 94 | configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // if SHARED requested or if EXCLUSIVE failed |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 99 | if (sharingMode == AAUDIO_SHARING_MODE_SHARED |
| 100 | || (serviceStream == nullptr && !sharingModeMatchRequired)) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 101 | serviceStream = new AAudioServiceStreamShared(*this); |
| 102 | result = serviceStream->open(request, configurationOutput); |
| 103 | configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_SHARED); |
| 104 | } |
| 105 | |
| 106 | if (result != AAUDIO_OK) { |
| 107 | delete serviceStream; |
| 108 | ALOGE("AAudioService::openStream(): failed, return %d", result); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 109 | return result; |
| 110 | } else { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 111 | aaudio_handle_t handle = mHandleTracker.put(AAUDIO_HANDLE_TYPE_STREAM, serviceStream); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 112 | ALOGD("AAudioService::openStream(): handle = 0x%08X", handle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 113 | if (handle < 0) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 114 | ALOGE("AAudioService::openStream(): handle table full"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 115 | delete serviceStream; |
| 116 | } |
| 117 | return handle; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) { |
| 122 | AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *) |
| 123 | mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM, |
| 124 | streamHandle); |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 125 | ALOGV("AAudioService.closeStream(0x%08X)", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 126 | if (serviceStream != nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 127 | serviceStream->close(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 128 | delete serviceStream; |
| 129 | return AAUDIO_OK; |
| 130 | } |
| 131 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 132 | } |
| 133 | |
| 134 | AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream( |
| 135 | aaudio_handle_t streamHandle) const { |
| 136 | return (AAudioServiceStreamBase *) mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM, |
| 137 | (aaudio_handle_t)streamHandle); |
| 138 | } |
| 139 | |
| 140 | aaudio_result_t AAudioService::getStreamDescription( |
| 141 | aaudio_handle_t streamHandle, |
| 142 | aaudio::AudioEndpointParcelable &parcelable) { |
| 143 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 144 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 145 | ALOGE("AAudioService::getStreamDescription(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 146 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 147 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 148 | aaudio_result_t result = serviceStream->getDescription(parcelable); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 149 | // parcelable.dump(); |
| 150 | return result; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) { |
| 154 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 155 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 156 | ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 157 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 158 | } |
| 159 | aaudio_result_t result = serviceStream->start(); |
| 160 | return result; |
| 161 | } |
| 162 | |
| 163 | aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) { |
| 164 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 165 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 166 | ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 167 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 168 | } |
| 169 | aaudio_result_t result = serviceStream->pause(); |
| 170 | return result; |
| 171 | } |
| 172 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 173 | aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) { |
| 174 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
| 175 | if (serviceStream == nullptr) { |
| 176 | ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle); |
| 177 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 178 | } |
| 179 | aaudio_result_t result = serviceStream->stop(); |
| 180 | return result; |
| 181 | } |
| 182 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 183 | aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) { |
| 184 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 185 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 186 | ALOGE("AAudioService::flushStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 187 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 188 | } |
| 189 | return serviceStream->flush(); |
| 190 | } |
| 191 | |
| 192 | aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 193 | pid_t clientProcessId, |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 194 | pid_t clientThreadId, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 195 | int64_t periodNanoseconds) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 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::registerAudioThread(), 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 | if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) { |
| 202 | ALOGE("AAudioService::registerAudioThread(), thread already registered"); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 203 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 204 | } |
| 205 | serviceStream->setRegisteredThread(clientThreadId); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 206 | int err = android::requestPriority(clientProcessId, clientThreadId, |
| 207 | DEFAULT_AUDIO_PRIORITY, true /* isForApp */); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 208 | if (err != 0){ |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 209 | ALOGE("AAudioService::registerAudioThread() failed, errno = %d, priority = %d", |
| 210 | errno, DEFAULT_AUDIO_PRIORITY); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 211 | return AAUDIO_ERROR_INTERNAL; |
| 212 | } else { |
| 213 | return AAUDIO_OK; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 218 | pid_t clientProcessId, |
| 219 | pid_t clientThreadId) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 220 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 221 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 222 | ALOGE("AAudioService::unregisterAudioThread(), illegal stream handle = 0x%0x", |
| 223 | streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 224 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 225 | } |
| 226 | if (serviceStream->getRegisteredThread() != clientThreadId) { |
| 227 | ALOGE("AAudioService::unregisterAudioThread(), wrong thread"); |
| 228 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 229 | } |
| 230 | serviceStream->setRegisteredThread(0); |
| 231 | return AAUDIO_OK; |
| 232 | } |