Phil Burk | 2355edb | 2016-12-26 13:54:02 -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 "OboeService" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <time.h> |
| 22 | #include <pthread.h> |
| 23 | |
| 24 | #include <oboe/OboeDefinitions.h> |
| 25 | |
| 26 | #include "HandleTracker.h" |
| 27 | #include "IOboeAudioService.h" |
| 28 | #include "OboeService.h" |
| 29 | #include "OboeAudioService.h" |
| 30 | #include "OboeServiceStreamFakeHal.h" |
| 31 | |
| 32 | using namespace android; |
| 33 | using namespace oboe; |
| 34 | |
| 35 | typedef enum |
| 36 | { |
| 37 | OBOE_HANDLE_TYPE_STREAM, |
| 38 | OBOE_HANDLE_TYPE_COUNT |
| 39 | } oboe_service_handle_type_t; |
| 40 | static_assert(OBOE_HANDLE_TYPE_COUNT <= HANDLE_TRACKER_MAX_TYPES, "Too many handle types."); |
| 41 | |
| 42 | oboe_handle_t OboeAudioService::openStream(oboe::OboeStreamRequest &request, |
| 43 | oboe::OboeStreamConfiguration &configuration) { |
| 44 | OboeServiceStreamBase *serviceStream = new OboeServiceStreamFakeHal(); |
| 45 | ALOGD("OboeAudioService::openStream(): created serviceStream = %p", serviceStream); |
| 46 | oboe_result_t result = serviceStream->open(request, configuration); |
| 47 | if (result < 0) { |
| 48 | ALOGE("OboeAudioService::openStream(): open returned %d", result); |
| 49 | return result; |
| 50 | } else { |
| 51 | OboeStream handle = mHandleTracker.put(OBOE_HANDLE_TYPE_STREAM, serviceStream); |
| 52 | ALOGD("OboeAudioService::openStream(): handle = 0x%08X", handle); |
| 53 | if (handle < 0) { |
| 54 | delete serviceStream; |
| 55 | } |
| 56 | return handle; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | oboe_result_t OboeAudioService::closeStream(oboe_handle_t streamHandle) { |
| 61 | OboeServiceStreamBase *serviceStream = (OboeServiceStreamBase *) |
| 62 | mHandleTracker.remove(OBOE_HANDLE_TYPE_STREAM, |
| 63 | streamHandle); |
| 64 | ALOGI("OboeAudioService.closeStream(0x%08X)", streamHandle); |
| 65 | if (serviceStream != nullptr) { |
| 66 | ALOGD("OboeAudioService::closeStream(): deleting serviceStream = %p", serviceStream); |
| 67 | delete serviceStream; |
| 68 | return OBOE_OK; |
| 69 | } |
| 70 | return OBOE_ERROR_INVALID_HANDLE; |
| 71 | } |
| 72 | |
| 73 | OboeServiceStreamBase *OboeAudioService::convertHandleToServiceStream( |
| 74 | oboe_handle_t streamHandle) const { |
| 75 | return (OboeServiceStreamBase *) mHandleTracker.get(OBOE_HANDLE_TYPE_STREAM, |
| 76 | (oboe_handle_t)streamHandle); |
| 77 | } |
| 78 | |
| 79 | oboe_result_t OboeAudioService::getStreamDescription( |
| 80 | oboe_handle_t streamHandle, |
| 81 | oboe::AudioEndpointParcelable &parcelable) { |
| 82 | ALOGI("OboeAudioService::getStreamDescriptor(), streamHandle = 0x%08x", streamHandle); |
| 83 | OboeServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
| 84 | ALOGI("OboeAudioService::getStreamDescriptor(), serviceStream = %p", serviceStream); |
| 85 | if (serviceStream == nullptr) { |
| 86 | return OBOE_ERROR_INVALID_HANDLE; |
| 87 | } |
| 88 | return serviceStream->getDescription(parcelable); |
| 89 | } |
| 90 | |
| 91 | oboe_result_t OboeAudioService::startStream(oboe_handle_t streamHandle) { |
| 92 | OboeServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
| 93 | ALOGI("OboeAudioService::startStream(), serviceStream = %p", serviceStream); |
| 94 | if (serviceStream == nullptr) { |
| 95 | return OBOE_ERROR_INVALID_HANDLE; |
| 96 | } |
| 97 | mLatestHandle = streamHandle; |
| 98 | return serviceStream->start(); |
| 99 | } |
| 100 | |
| 101 | oboe_result_t OboeAudioService::pauseStream(oboe_handle_t streamHandle) { |
| 102 | OboeServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
| 103 | ALOGI("OboeAudioService::pauseStream(), serviceStream = %p", serviceStream); |
| 104 | if (serviceStream == nullptr) { |
| 105 | return OBOE_ERROR_INVALID_HANDLE; |
| 106 | } |
| 107 | return serviceStream->pause(); |
| 108 | } |
| 109 | |
| 110 | oboe_result_t OboeAudioService::flushStream(oboe_handle_t streamHandle) { |
| 111 | OboeServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
| 112 | ALOGI("OboeAudioService::flushStream(), serviceStream = %p", serviceStream); |
| 113 | if (serviceStream == nullptr) { |
| 114 | return OBOE_ERROR_INVALID_HANDLE; |
| 115 | } |
| 116 | return serviceStream->flush(); |
| 117 | } |
| 118 | |
| 119 | void OboeAudioService::tickle() { |
| 120 | OboeServiceStreamBase *serviceStream = convertHandleToServiceStream(mLatestHandle); |
| 121 | //ALOGI("OboeAudioService::tickle(), serviceStream = %p", serviceStream); |
| 122 | if (serviceStream != nullptr) { |
| 123 | serviceStream->tickle(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | oboe_result_t OboeAudioService::registerAudioThread(oboe_handle_t streamHandle, |
| 128 | pid_t clientThreadId, |
| 129 | oboe_nanoseconds_t periodNanoseconds) { |
| 130 | OboeServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
| 131 | ALOGI("OboeAudioService::registerAudioThread(), serviceStream = %p", serviceStream); |
| 132 | if (serviceStream == nullptr) { |
| 133 | ALOGE("OboeAudioService::registerAudioThread(), serviceStream == nullptr"); |
| 134 | return OBOE_ERROR_INVALID_HANDLE; |
| 135 | } |
| 136 | if (serviceStream->getRegisteredThread() != OboeServiceStreamBase::ILLEGAL_THREAD_ID) { |
| 137 | ALOGE("OboeAudioService::registerAudioThread(), thread already registered"); |
| 138 | return OBOE_ERROR_INVALID_ORDER; |
| 139 | } |
| 140 | serviceStream->setRegisteredThread(clientThreadId); |
| 141 | // Boost client thread to SCHED_FIFO |
| 142 | struct sched_param sp; |
| 143 | memset(&sp, 0, sizeof(sp)); |
| 144 | sp.sched_priority = 2; // TODO use 'requestPriority' function from frameworks/av/media/utils |
| 145 | int err = sched_setscheduler(clientThreadId, SCHED_FIFO, &sp); |
| 146 | if (err != 0){ |
| 147 | ALOGE("OboeAudioService::sched_setscheduler() failed, errno = %d, priority = %d", |
| 148 | errno, sp.sched_priority); |
| 149 | return OBOE_ERROR_INTERNAL; |
| 150 | } else { |
| 151 | return OBOE_OK; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | oboe_result_t OboeAudioService::unregisterAudioThread(oboe_handle_t streamHandle, |
| 156 | pid_t clientThreadId) { |
| 157 | OboeServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle); |
| 158 | ALOGI("OboeAudioService::unregisterAudioThread(), serviceStream = %p", serviceStream); |
| 159 | if (serviceStream == nullptr) { |
| 160 | ALOGE("OboeAudioService::unregisterAudioThread(), serviceStream == nullptr"); |
| 161 | return OBOE_ERROR_INVALID_HANDLE; |
| 162 | } |
| 163 | if (serviceStream->getRegisteredThread() != clientThreadId) { |
| 164 | ALOGE("OboeAudioService::unregisterAudioThread(), wrong thread"); |
| 165 | return OBOE_ERROR_ILLEGAL_ARGUMENT; |
| 166 | } |
| 167 | serviceStream->setRegisteredThread(0); |
| 168 | return OBOE_OK; |
| 169 | } |