Shunkai Yao | 85b0169 | 2023-02-15 22:04:57 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | #include <algorithm> |
| 18 | #include <cstddef> |
| 19 | #include <memory> |
| 20 | #include <unordered_set> |
| 21 | |
| 22 | #define LOG_TAG "AHAL_ExtensionEffect" |
| 23 | #include <Utils.h> |
| 24 | #include <aidl/android/hardware/audio/effect/DefaultExtension.h> |
| 25 | #include <android-base/logging.h> |
| 26 | #include <fmq/AidlMessageQueue.h> |
| 27 | |
| 28 | #include "ExtensionEffect.h" |
| 29 | |
| 30 | using aidl::android::hardware::audio::effect::DefaultExtension; |
| 31 | using aidl::android::hardware::audio::effect::Descriptor; |
| 32 | using aidl::android::hardware::audio::effect::ExtensionEffect; |
| 33 | using aidl::android::hardware::audio::effect::IEffect; |
| 34 | using aidl::android::hardware::audio::effect::kExtensionEffectImplUUID; |
| 35 | using aidl::android::hardware::audio::effect::kExtensionEffectTypeUUID; |
| 36 | using aidl::android::hardware::audio::effect::Range; |
| 37 | using aidl::android::hardware::audio::effect::VendorExtension; |
| 38 | using aidl::android::media::audio::common::AudioUuid; |
| 39 | |
| 40 | extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid, |
| 41 | std::shared_ptr<IEffect>* instanceSpp) { |
| 42 | if (!in_impl_uuid || *in_impl_uuid != kExtensionEffectImplUUID) { |
| 43 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 44 | return EX_ILLEGAL_ARGUMENT; |
| 45 | } |
| 46 | if (instanceSpp) { |
| 47 | *instanceSpp = ndk::SharedRefBase::make<ExtensionEffect>(); |
| 48 | LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created"; |
| 49 | return EX_NONE; |
| 50 | } else { |
| 51 | LOG(ERROR) << __func__ << " invalid input parameter!"; |
| 52 | return EX_ILLEGAL_ARGUMENT; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) { |
| 57 | if (!in_impl_uuid || *in_impl_uuid != kExtensionEffectImplUUID) { |
| 58 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 59 | return EX_ILLEGAL_ARGUMENT; |
| 60 | } |
| 61 | *_aidl_return = ExtensionEffect::kDescriptor; |
| 62 | return EX_NONE; |
| 63 | } |
| 64 | |
| 65 | namespace aidl::android::hardware::audio::effect { |
| 66 | |
| 67 | const std::string ExtensionEffect::kEffectName = "ExtensionEffectExample"; |
| 68 | |
| 69 | const Descriptor ExtensionEffect::kDescriptor = { |
| 70 | .common = {.id = {.type = kExtensionEffectTypeUUID, |
| 71 | .uuid = kExtensionEffectImplUUID, |
| 72 | .proxy = std::nullopt}, |
| 73 | .name = ExtensionEffect::kEffectName, |
| 74 | .implementor = "The Android Open Source Project"}}; |
| 75 | |
| 76 | ndk::ScopedAStatus ExtensionEffect::getDescriptor(Descriptor* _aidl_return) { |
| 77 | LOG(DEBUG) << __func__ << kDescriptor.toString(); |
| 78 | *_aidl_return = kDescriptor; |
| 79 | return ndk::ScopedAStatus::ok(); |
| 80 | } |
| 81 | |
| 82 | ndk::ScopedAStatus ExtensionEffect::setParameterSpecific(const Parameter::Specific& specific) { |
| 83 | RETURN_IF(Parameter::Specific::vendorEffect != specific.getTag(), EX_ILLEGAL_ARGUMENT, |
| 84 | "EffectNotSupported"); |
| 85 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 86 | |
| 87 | auto& vendorEffect = specific.get<Parameter::Specific::vendorEffect>(); |
| 88 | std::optional<DefaultExtension> defaultExt; |
| 89 | RETURN_IF(STATUS_OK != vendorEffect.extension.getParcelable(&defaultExt), EX_ILLEGAL_ARGUMENT, |
| 90 | "getParcelableFailed"); |
| 91 | RETURN_IF(!defaultExt.has_value(), EX_ILLEGAL_ARGUMENT, "parcelableNulld"); |
| 92 | RETURN_IF(mContext->setParams(defaultExt->bytes) != RetCode::SUCCESS, EX_ILLEGAL_ARGUMENT, |
| 93 | "paramNotSupported"); |
| 94 | |
| 95 | return ndk::ScopedAStatus::ok(); |
| 96 | } |
| 97 | |
| 98 | ndk::ScopedAStatus ExtensionEffect::getParameterSpecific(const Parameter::Id& id, |
| 99 | Parameter::Specific* specific) { |
| 100 | auto tag = id.getTag(); |
| 101 | RETURN_IF(Parameter::Id::vendorEffectTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag"); |
| 102 | auto specificId = id.get<Parameter::Id::vendorEffectTag>(); |
| 103 | VendorExtension extension; |
| 104 | DefaultExtension defaultExt; |
| 105 | defaultExt.bytes = mContext->getParams(specificId); |
| 106 | RETURN_IF(STATUS_OK != extension.extension.setParcelable(defaultExt), EX_ILLEGAL_ARGUMENT, |
| 107 | "setParcelableFailed"); |
| 108 | specific->set<Parameter::Specific::vendorEffect>(extension); |
| 109 | return ndk::ScopedAStatus::ok(); |
| 110 | } |
| 111 | |
| 112 | std::shared_ptr<EffectContext> ExtensionEffect::createContext(const Parameter::Common& common) { |
| 113 | if (mContext) { |
| 114 | LOG(DEBUG) << __func__ << " context already exist"; |
| 115 | } else { |
| 116 | mContext = std::make_shared<ExtensionEffectContext>(1 /* statusFmqDepth */, common); |
| 117 | } |
| 118 | return mContext; |
| 119 | } |
| 120 | |
| 121 | std::shared_ptr<EffectContext> ExtensionEffect::getContext() { |
| 122 | return mContext; |
| 123 | } |
| 124 | |
| 125 | RetCode ExtensionEffect::releaseContext() { |
| 126 | if (mContext) { |
| 127 | mContext.reset(); |
| 128 | } |
| 129 | return RetCode::SUCCESS; |
| 130 | } |
| 131 | |
| 132 | // Processing method running in EffectWorker thread. |
| 133 | IEffect::Status ExtensionEffect::effectProcessImpl(float* in, float* out, int samples) { |
| 134 | // TODO: get data buffer and process. |
| 135 | LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples; |
| 136 | for (int i = 0; i < samples; i++) { |
| 137 | *out++ = *in++; |
| 138 | } |
| 139 | return {STATUS_OK, samples, samples}; |
| 140 | } |
| 141 | |
| 142 | } // namespace aidl::android::hardware::audio::effect |