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