blob: db1e4a4bf34888ec1b79f44c1e9f6bf59134ecdc [file] [log] [blame]
Shunkai Yao85b01692023-02-15 22:04:57 +00001/*
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 Yao85b01692023-02-15 22:04:57 +000022#include <aidl/android/hardware/audio/effect/DefaultExtension.h>
Mikhail Naganov872d4a62023-03-09 18:19:01 -080023#define LOG_TAG "AHAL_ExtensionEffect"
Shunkai Yao85b01692023-02-15 22:04:57 +000024#include <android-base/logging.h>
25#include <fmq/AidlMessageQueue.h>
26
27#include "ExtensionEffect.h"
28
29using aidl::android::hardware::audio::effect::DefaultExtension;
30using aidl::android::hardware::audio::effect::Descriptor;
31using aidl::android::hardware::audio::effect::ExtensionEffect;
32using aidl::android::hardware::audio::effect::IEffect;
33using aidl::android::hardware::audio::effect::kExtensionEffectImplUUID;
34using aidl::android::hardware::audio::effect::kExtensionEffectTypeUUID;
35using aidl::android::hardware::audio::effect::Range;
36using aidl::android::hardware::audio::effect::VendorExtension;
37using aidl::android::media::audio::common::AudioUuid;
38
39extern "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
55extern "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
64namespace aidl::android::hardware::audio::effect {
65
66const std::string ExtensionEffect::kEffectName = "ExtensionEffectExample";
67
68const 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
75ndk::ScopedAStatus ExtensionEffect::getDescriptor(Descriptor* _aidl_return) {
76 LOG(DEBUG) << __func__ << kDescriptor.toString();
77 *_aidl_return = kDescriptor;
78 return ndk::ScopedAStatus::ok();
79}
80
81ndk::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 Yaob2325e52023-03-03 19:34:47 +000090 RETURN_IF(!defaultExt.has_value(), EX_ILLEGAL_ARGUMENT, "parcelableNull");
Shunkai Yao85b01692023-02-15 22:04:57 +000091 RETURN_IF(mContext->setParams(defaultExt->bytes) != RetCode::SUCCESS, EX_ILLEGAL_ARGUMENT,
92 "paramNotSupported");
93
94 return ndk::ScopedAStatus::ok();
95}
96
97ndk::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 Yaob2325e52023-03-03 19:34:47 +0000101 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 Yao85b01692023-02-15 22:04:57 +0000107 VendorExtension extension;
108 DefaultExtension defaultExt;
Shunkai Yaob2325e52023-03-03 19:34:47 +0000109 defaultExt.bytes = mContext->getParams(defaultIdExt->bytes);
Shunkai Yao85b01692023-02-15 22:04:57 +0000110 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
116std::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
125std::shared_ptr<EffectContext> ExtensionEffect::getContext() {
126 return mContext;
127}
128
129RetCode ExtensionEffect::releaseContext() {
130 if (mContext) {
131 mContext.reset();
132 }
133 return RetCode::SUCCESS;
134}
135
136// Processing method running in EffectWorker thread.
137IEffect::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