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 | |
| 21 | #include <time.h> |
| 22 | #include <pthread.h> |
| 23 | |
| 24 | #include <aaudio/AAudioDefinitions.h> |
| 25 | |
| 26 | #include "HandleTracker.h" |
| 27 | #include "IAAudioService.h" |
| 28 | #include "AAudioServiceDefinitions.h" |
| 29 | #include "AAudioService.h" |
| 30 | #include "AAudioServiceStreamFakeHal.h" |
| 31 | |
| 32 | using namespace android; |
| 33 | using namespace aaudio; |
| 34 | |
| 35 | typedef enum |
| 36 | { |
| 37 | AAUDIO_HANDLE_TYPE_DUMMY1, // TODO remove DUMMYs |
| 38 | AAUDIO_HANDLE_TYPE_DUMMY2, // make server handles different than client |
| 39 | AAUDIO_HANDLE_TYPE_STREAM, |
| 40 | AAUDIO_HANDLE_TYPE_COUNT |
| 41 | } aaudio_service_handle_type_t; |
| 42 | static_assert(AAUDIO_HANDLE_TYPE_COUNT <= HANDLE_TRACKER_MAX_TYPES, "Too many handle types."); |
| 43 | |
| 44 | android::AAudioService::AAudioService() |
| 45 | : BnAAudioService() { |
| 46 | } |
| 47 | |
| 48 | AAudioService::~AAudioService() { |
| 49 | } |
| 50 | |
| 51 | aaudio_handle_t AAudioService::openStream(aaudio::AAudioStreamRequest &request, |
| 52 | aaudio::AAudioStreamConfiguration &configuration) { |
| 53 | AAudioServiceStreamBase *serviceStream = new AAudioServiceStreamFakeHal(); |
| 54 | ALOGD("AAudioService::openStream(): created serviceStream = %p", serviceStream); |
| 55 | aaudio_result_t result = serviceStream->open(request, configuration); |
| 56 | if (result < 0) { |
| 57 | ALOGE("AAudioService::openStream(): open returned %d", result); |
| 58 | return result; |
| 59 | } else { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 60 | aaudio_handle_t handle = mHandleTracker.put(AAUDIO_HANDLE_TYPE_STREAM, serviceStream); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 61 | ALOGD("AAudioService::openStream(): handle = 0x%08X", handle); |
| 62 | if (handle < 0) { |
| 63 | delete serviceStream; |
| 64 | } |
| 65 | return handle; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) { |
| 70 | AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *) |
| 71 | mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM, |
| 72 | streamHandle); |
| 73 | ALOGD("AAudioService.closeStream(0x%08X)", streamHandle); |
| 74 | if (serviceStream != nullptr) { |
| 75 | ALOGD("AAudioService::closeStream(): deleting serviceStream = %p", serviceStream); |
| 76 | delete serviceStream; |
| 77 | return AAUDIO_OK; |
| 78 | } |
| 79 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 80 | } |
| 81 | |
| 82 | AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream( |
| 83 | aaudio_handle_t streamHandle) const { |
| 84 | return (AAudioServiceStreamBase *) mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM, |
| 85 | (aaudio_handle_t)streamHandle); |
| 86 | } |
| 87 | |
| 88 | aaudio_result_t AAudioService::getStreamDescription( |
| 89 | aaudio_handle_t streamHandle, |
| 90 | aaudio::AudioEndpointParcelable &parcelable) { |
| 91 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
| 92 | ALOGD("AAudioService::getStreamDescription(), serviceStream = %p", serviceStream); |
| 93 | if (serviceStream == nullptr) { |
| 94 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 95 | } |
| 96 | return serviceStream->getDescription(parcelable); |
| 97 | } |
| 98 | |
| 99 | aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) { |
| 100 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
| 101 | ALOGD("AAudioService::startStream(), serviceStream = %p", serviceStream); |
| 102 | if (serviceStream == nullptr) { |
| 103 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 104 | } |
| 105 | aaudio_result_t result = serviceStream->start(); |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) { |
| 110 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
| 111 | ALOGD("AAudioService::pauseStream(), serviceStream = %p", serviceStream); |
| 112 | if (serviceStream == nullptr) { |
| 113 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 114 | } |
| 115 | aaudio_result_t result = serviceStream->pause(); |
| 116 | return result; |
| 117 | } |
| 118 | |
| 119 | aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) { |
| 120 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
| 121 | ALOGD("AAudioService::flushStream(), serviceStream = %p", serviceStream); |
| 122 | if (serviceStream == nullptr) { |
| 123 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 124 | } |
| 125 | return serviceStream->flush(); |
| 126 | } |
| 127 | |
| 128 | aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle, |
| 129 | pid_t clientThreadId, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 130 | int64_t periodNanoseconds) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 131 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
| 132 | ALOGD("AAudioService::registerAudioThread(), serviceStream = %p", serviceStream); |
| 133 | if (serviceStream == nullptr) { |
| 134 | ALOGE("AAudioService::registerAudioThread(), serviceStream == nullptr"); |
| 135 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 136 | } |
| 137 | if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) { |
| 138 | ALOGE("AAudioService::registerAudioThread(), thread already registered"); |
| 139 | return AAUDIO_ERROR_INVALID_ORDER; |
| 140 | } |
| 141 | serviceStream->setRegisteredThread(clientThreadId); |
| 142 | // Boost client thread to SCHED_FIFO |
| 143 | struct sched_param sp; |
| 144 | memset(&sp, 0, sizeof(sp)); |
| 145 | sp.sched_priority = 2; // TODO use 'requestPriority' function from frameworks/av/media/utils |
| 146 | int err = sched_setscheduler(clientThreadId, SCHED_FIFO, &sp); |
| 147 | if (err != 0){ |
| 148 | ALOGE("AAudioService::sched_setscheduler() failed, errno = %d, priority = %d", |
| 149 | errno, sp.sched_priority); |
| 150 | return AAUDIO_ERROR_INTERNAL; |
| 151 | } else { |
| 152 | return AAUDIO_OK; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle, |
| 157 | pid_t clientThreadId) { |
| 158 | AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
| 159 | ALOGI("AAudioService::unregisterAudioThread(), serviceStream = %p", serviceStream); |
| 160 | if (serviceStream == nullptr) { |
| 161 | ALOGE("AAudioService::unregisterAudioThread(), serviceStream == nullptr"); |
| 162 | return AAUDIO_ERROR_INVALID_HANDLE; |
| 163 | } |
| 164 | if (serviceStream->getRegisteredThread() != clientThreadId) { |
| 165 | ALOGE("AAudioService::unregisterAudioThread(), wrong thread"); |
| 166 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 167 | } |
| 168 | serviceStream->setRegisteredThread(0); |
| 169 | return AAUDIO_OK; |
| 170 | } |