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 | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 21 | //#include <time.h> |
| 22 | //#include <pthread.h> |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 23 | |
Phil Burk | a4eb0d8 | 2017-04-12 15:44:06 -0700 | [diff] [blame] | 24 | #include <aaudio/AAudio.h> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 25 | #include <mediautils/SchedulingPolicyService.h> |
| 26 | #include <utils/String16.h> |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 27 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 28 | #include "binding/AAudioServiceMessage.h" |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 29 | #include "AAudioService.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 30 | #include "AAudioServiceStreamMMAP.h" |
| 31 | #include "AAudioServiceStreamShared.h" |
| 32 | #include "AAudioServiceStreamMMAP.h" |
| 33 | #include "binding/IAAudioService.h" |
| 34 | #include "utility/HandleTracker.h" |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 35 | |
| 36 | using namespace android; |
| 37 | using namespace aaudio; |
| 38 | |
| 39 | typedef enum |
| 40 | { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 41 | AAUDIO_HANDLE_TYPE_STREAM |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 42 | } aaudio_service_handle_type_t; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 43 | 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] | 44 | |
| 45 | android::AAudioService::AAudioService() |
| 46 | : BnAAudioService() { |
| 47 | } |
| 48 | |
| 49 | AAudioService::~AAudioService() { |
| 50 | } |
| 51 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 52 | aaudio_handle_t AAudioService::openStream(const aaudio::AAudioStreamRequest &request, |
| 53 | aaudio::AAudioStreamConfiguration &configurationOutput) { |
| 54 | aaudio_result_t result = AAUDIO_OK; |
| 55 | AAudioServiceStreamBase *serviceStream = nullptr; |
| 56 | const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration(); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 57 | bool sharingModeMatchRequired = request.isSharingModeMatchRequired(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 58 | aaudio_sharing_mode_t sharingMode = configurationInput.getSharingMode(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 59 | |
| 60 | if (sharingMode != AAUDIO_SHARING_MODE_EXCLUSIVE && sharingMode != AAUDIO_SHARING_MODE_SHARED) { |
| 61 | ALOGE("AAudioService::openStream(): unrecognized sharing mode = %d", sharingMode); |
| 62 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 63 | } |
| 64 | |
| 65 | if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE) { |
| 66 | ALOGD("AAudioService::openStream(), sharingMode = AAUDIO_SHARING_MODE_EXCLUSIVE"); |
| 67 | serviceStream = new AAudioServiceStreamMMAP(); |
| 68 | result = serviceStream->open(request, configurationOutput); |
| 69 | if (result != AAUDIO_OK) { |
| 70 | // fall back to using a shared stream |
| 71 | ALOGD("AAudioService::openStream(), EXCLUSIVE mode failed"); |
| 72 | delete serviceStream; |
| 73 | serviceStream = nullptr; |
| 74 | } else { |
| 75 | configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // if SHARED requested or if EXCLUSIVE failed |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 80 | if (sharingMode == AAUDIO_SHARING_MODE_SHARED |
| 81 | || (serviceStream == nullptr && !sharingModeMatchRequired)) { |
| 82 | ALOGD("AAudioService::openStream(), try AAUDIO_SHARING_MODE_SHARED"); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 83 | serviceStream = new AAudioServiceStreamShared(*this); |
| 84 | result = serviceStream->open(request, configurationOutput); |
| 85 | configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_SHARED); |
| 86 | } |
| 87 | |
| 88 | if (result != AAUDIO_OK) { |
| 89 | delete serviceStream; |
| 90 | ALOGE("AAudioService::openStream(): failed, return %d", result); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 91 | return result; |
| 92 | } else { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 93 | aaudio_handle_t handle = mHandleTracker.put(AAUDIO_HANDLE_TYPE_STREAM, serviceStream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 94 | ALOGD("AAudioService::openStream(): handle = 0x%08X", handle); |
| 95 | if (handle < 0) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 96 | ALOGE("AAudioService::openStream(): handle table full"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 97 | delete serviceStream; |
| 98 | } |
| 99 | return handle; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) { |
| 104 | AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *) |
| 105 | mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM, |
| 106 | streamHandle); |
| 107 | ALOGD("AAudioService.closeStream(0x%08X)", streamHandle); |
| 108 | if (serviceStream != nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 109 | serviceStream->close(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 110 | delete serviceStream; |
| 111 | return AAUDIO_OK; |
| 112 | } |
| 113 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 114 | } |
| 115 | |
| 116 | AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream( |
| 117 | aaudio_handle_t streamHandle) const { |
| 118 | return (AAudioServiceStreamBase *) mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM, |
| 119 | (aaudio_handle_t)streamHandle); |
| 120 | } |
| 121 | |
| 122 | aaudio_result_t AAudioService::getStreamDescription( |
| 123 | aaudio_handle_t streamHandle, |
| 124 | aaudio::AudioEndpointParcelable &parcelable) { |
| 125 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(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 | ALOGE("AAudioService::getStreamDescription(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 128 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 129 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 130 | aaudio_result_t result = serviceStream->getDescription(parcelable); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 131 | // parcelable.dump(); |
| 132 | return result; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) { |
| 136 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 137 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 138 | ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 139 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 140 | } |
| 141 | aaudio_result_t result = serviceStream->start(); |
| 142 | return result; |
| 143 | } |
| 144 | |
| 145 | aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) { |
| 146 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 147 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 148 | ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 149 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 150 | } |
| 151 | aaudio_result_t result = serviceStream->pause(); |
| 152 | return result; |
| 153 | } |
| 154 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 155 | aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) { |
| 156 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
| 157 | if (serviceStream == nullptr) { |
| 158 | ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle); |
| 159 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 160 | } |
| 161 | aaudio_result_t result = serviceStream->stop(); |
| 162 | return result; |
| 163 | } |
| 164 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 165 | aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) { |
| 166 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 167 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 168 | ALOGE("AAudioService::flushStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 169 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 170 | } |
| 171 | return serviceStream->flush(); |
| 172 | } |
| 173 | |
| 174 | aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 175 | pid_t clientProcessId, |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 176 | pid_t clientThreadId, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 177 | int64_t periodNanoseconds) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 178 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 179 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 180 | ALOGE("AAudioService::registerAudioThread(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 181 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 182 | } |
| 183 | if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) { |
| 184 | ALOGE("AAudioService::registerAudioThread(), thread already registered"); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 185 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 186 | } |
| 187 | serviceStream->setRegisteredThread(clientThreadId); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 188 | int err = android::requestPriority(clientProcessId, clientThreadId, |
| 189 | DEFAULT_AUDIO_PRIORITY, true /* isForApp */); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 190 | if (err != 0){ |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 191 | ALOGE("AAudioService::registerAudioThread() failed, errno = %d, priority = %d", |
| 192 | errno, DEFAULT_AUDIO_PRIORITY); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 193 | return AAUDIO_ERROR_INTERNAL; |
| 194 | } else { |
| 195 | return AAUDIO_OK; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 200 | pid_t clientProcessId, |
| 201 | pid_t clientThreadId) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 202 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 203 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 204 | ALOGE("AAudioService::unregisterAudioThread(), illegal stream handle = 0x%0x", |
| 205 | streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 206 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 207 | } |
| 208 | if (serviceStream->getRegisteredThread() != clientThreadId) { |
| 209 | ALOGE("AAudioService::unregisterAudioThread(), wrong thread"); |
| 210 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 211 | } |
| 212 | serviceStream->setRegisteredThread(0); |
| 213 | return AAUDIO_OK; |
| 214 | } |