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