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) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 66 | serviceStream = new AAudioServiceStreamMMAP(); |
| 67 | result = serviceStream->open(request, configurationOutput); |
| 68 | if (result != AAUDIO_OK) { |
| 69 | // fall back to using a shared stream |
| 70 | ALOGD("AAudioService::openStream(), EXCLUSIVE mode failed"); |
| 71 | delete serviceStream; |
| 72 | serviceStream = nullptr; |
| 73 | } else { |
| 74 | configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // if SHARED requested or if EXCLUSIVE failed |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 79 | if (sharingMode == AAUDIO_SHARING_MODE_SHARED |
| 80 | || (serviceStream == nullptr && !sharingModeMatchRequired)) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 81 | serviceStream = new AAudioServiceStreamShared(*this); |
| 82 | result = serviceStream->open(request, configurationOutput); |
| 83 | configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_SHARED); |
| 84 | } |
| 85 | |
| 86 | if (result != AAUDIO_OK) { |
| 87 | delete serviceStream; |
| 88 | ALOGE("AAudioService::openStream(): failed, return %d", result); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 89 | return result; |
| 90 | } else { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 91 | aaudio_handle_t handle = mHandleTracker.put(AAUDIO_HANDLE_TYPE_STREAM, serviceStream); |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 92 | ALOGV("AAudioService::openStream(): handle = 0x%08X", handle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 93 | if (handle < 0) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 94 | ALOGE("AAudioService::openStream(): handle table full"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 95 | delete serviceStream; |
| 96 | } |
| 97 | return handle; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) { |
| 102 | AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *) |
| 103 | mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM, |
| 104 | streamHandle); |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 105 | ALOGV("AAudioService.closeStream(0x%08X)", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 106 | if (serviceStream != nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 107 | serviceStream->close(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 108 | delete serviceStream; |
| 109 | return AAUDIO_OK; |
| 110 | } |
| 111 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 112 | } |
| 113 | |
| 114 | AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream( |
| 115 | aaudio_handle_t streamHandle) const { |
| 116 | return (AAudioServiceStreamBase *) mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM, |
| 117 | (aaudio_handle_t)streamHandle); |
| 118 | } |
| 119 | |
| 120 | aaudio_result_t AAudioService::getStreamDescription( |
| 121 | aaudio_handle_t streamHandle, |
| 122 | aaudio::AudioEndpointParcelable &parcelable) { |
| 123 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 124 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 125 | ALOGE("AAudioService::getStreamDescription(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 126 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 127 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 128 | aaudio_result_t result = serviceStream->getDescription(parcelable); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 129 | // parcelable.dump(); |
| 130 | return result; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) { |
| 134 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 135 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 136 | ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 137 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 138 | } |
| 139 | aaudio_result_t result = serviceStream->start(); |
| 140 | return result; |
| 141 | } |
| 142 | |
| 143 | aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) { |
| 144 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 145 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 146 | ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 147 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 148 | } |
| 149 | aaudio_result_t result = serviceStream->pause(); |
| 150 | return result; |
| 151 | } |
| 152 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 153 | aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) { |
| 154 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
| 155 | if (serviceStream == nullptr) { |
| 156 | ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle); |
| 157 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 158 | } |
| 159 | aaudio_result_t result = serviceStream->stop(); |
| 160 | return result; |
| 161 | } |
| 162 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 163 | aaudio_result_t AAudioService::flushStream(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::flushStream(), 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 | return serviceStream->flush(); |
| 170 | } |
| 171 | |
| 172 | aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 173 | pid_t clientProcessId, |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 174 | pid_t clientThreadId, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 175 | int64_t periodNanoseconds) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 176 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 177 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 178 | ALOGE("AAudioService::registerAudioThread(), illegal stream handle = 0x%0x", streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 179 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 180 | } |
| 181 | if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) { |
| 182 | ALOGE("AAudioService::registerAudioThread(), thread already registered"); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 183 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 184 | } |
| 185 | serviceStream->setRegisteredThread(clientThreadId); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 186 | int err = android::requestPriority(clientProcessId, clientThreadId, |
| 187 | DEFAULT_AUDIO_PRIORITY, true /* isForApp */); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 188 | if (err != 0){ |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 189 | ALOGE("AAudioService::registerAudioThread() failed, errno = %d, priority = %d", |
| 190 | errno, DEFAULT_AUDIO_PRIORITY); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 191 | return AAUDIO_ERROR_INTERNAL; |
| 192 | } else { |
| 193 | return AAUDIO_OK; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 198 | pid_t clientProcessId, |
| 199 | pid_t clientThreadId) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 200 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 201 | if (serviceStream == nullptr) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 202 | ALOGE("AAudioService::unregisterAudioThread(), illegal stream handle = 0x%0x", |
| 203 | streamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 204 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 205 | } |
| 206 | if (serviceStream->getRegisteredThread() != clientThreadId) { |
| 207 | ALOGE("AAudioService::unregisterAudioThread(), wrong thread"); |
| 208 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 209 | } |
| 210 | serviceStream->setRegisteredThread(0); |
| 211 | return AAUDIO_OK; |
| 212 | } |