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 #include <aidl/android/hardware/audio/effect/DefaultExtension.h>
23 #define LOG_TAG "AHAL_ExtensionEffect"
24 #include <android-base/logging.h>
25 #include <fmq/AidlMessageQueue.h>
26 #include <system/audio_effects/effect_uuid.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::getEffectImplUuidExtension;
34 using aidl::android::hardware::audio::effect::getEffectTypeUuidExtension;
35 using aidl::android::hardware::audio::effect::IEffect;
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 
createEffect(const AudioUuid * in_impl_uuid,std::shared_ptr<IEffect> * instanceSpp)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 != getEffectImplUuidExtension()) {
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 
queryEffect(const AudioUuid * in_impl_uuid,Descriptor * _aidl_return)56 extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
57     if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidExtension()) {
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 = getEffectTypeUuidExtension(),
71                           .uuid = getEffectImplUuidExtension(),
72                           .proxy = std::nullopt},
73                    .name = ExtensionEffect::kEffectName,
74                    .implementor = "The Android Open Source Project"}};
75 
getDescriptor(Descriptor * _aidl_return)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 
setParameterSpecific(const Parameter::Specific & specific)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, "parcelableNull");
92     RETURN_IF(mContext->setParams(defaultExt->bytes) != RetCode::SUCCESS, EX_ILLEGAL_ARGUMENT,
93               "paramNotSupported");
94 
95     return ndk::ScopedAStatus::ok();
96 }
97 
getParameterSpecific(const Parameter::Id & id,Parameter::Specific * specific)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 extensionId = id.get<Parameter::Id::vendorEffectTag>();
103     std::optional<DefaultExtension> defaultIdExt;
104     RETURN_IF(STATUS_OK != extensionId.extension.getParcelable(&defaultIdExt), EX_ILLEGAL_ARGUMENT,
105               "getIdParcelableFailed");
106     RETURN_IF(!defaultIdExt.has_value(), EX_ILLEGAL_ARGUMENT, "parcelableIdNull");
107 
108     VendorExtension extension;
109     DefaultExtension defaultExt;
110     defaultExt.bytes = mContext->getParams(defaultIdExt->bytes);
111     RETURN_IF(STATUS_OK != extension.extension.setParcelable(defaultExt), EX_ILLEGAL_ARGUMENT,
112               "setParcelableFailed");
113     specific->set<Parameter::Specific::vendorEffect>(extension);
114     return ndk::ScopedAStatus::ok();
115 }
116 
createContext(const Parameter::Common & common)117 std::shared_ptr<EffectContext> ExtensionEffect::createContext(const Parameter::Common& common) {
118     if (mContext) {
119         LOG(DEBUG) << __func__ << " context already exist";
120     } else {
121         mContext = std::make_shared<ExtensionEffectContext>(1 /* statusFmqDepth */, common);
122     }
123     return mContext;
124 }
125 
releaseContext()126 RetCode ExtensionEffect::releaseContext() {
127     if (mContext) {
128         mContext.reset();
129     }
130     return RetCode::SUCCESS;
131 }
132 
133 // Processing method running in EffectWorker thread.
effectProcessImpl(float * in,float * out,int samples)134 IEffect::Status ExtensionEffect::effectProcessImpl(float* in, float* out, int samples) {
135     // TODO: get data buffer and process.
136     LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
137     for (int i = 0; i < samples; i++) {
138         *out++ = *in++;
139     }
140     return {STATUS_OK, samples, samples};
141 }
142 
143 }  // namespace aidl::android::hardware::audio::effect
144