1 /*
2 * Copyright (C) 2022 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
20 #define LOG_TAG "AHAL_DownmixSw"
21 #include <android-base/logging.h>
22 #include <fmq/AidlMessageQueue.h>
23 #include <system/audio_effects/effect_uuid.h>
24
25 #include "DownmixSw.h"
26
27 using aidl::android::hardware::audio::effect::Descriptor;
28 using aidl::android::hardware::audio::effect::DownmixSw;
29 using aidl::android::hardware::audio::effect::getEffectImplUuidDownmixSw;
30 using aidl::android::hardware::audio::effect::getEffectTypeUuidDownmix;
31 using aidl::android::hardware::audio::effect::IEffect;
32 using aidl::android::hardware::audio::effect::State;
33 using aidl::android::media::audio::common::AudioUuid;
34
createEffect(const AudioUuid * in_impl_uuid,std::shared_ptr<IEffect> * instanceSpp)35 extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
36 std::shared_ptr<IEffect>* instanceSpp) {
37 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidDownmixSw()) {
38 LOG(ERROR) << __func__ << "uuid not supported";
39 return EX_ILLEGAL_ARGUMENT;
40 }
41 if (instanceSpp) {
42 *instanceSpp = ndk::SharedRefBase::make<DownmixSw>();
43 LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
44 return EX_NONE;
45 } else {
46 LOG(ERROR) << __func__ << " invalid input parameter!";
47 return EX_ILLEGAL_ARGUMENT;
48 }
49 }
50
queryEffect(const AudioUuid * in_impl_uuid,Descriptor * _aidl_return)51 extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
52 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidDownmixSw()) {
53 LOG(ERROR) << __func__ << "uuid not supported";
54 return EX_ILLEGAL_ARGUMENT;
55 }
56 *_aidl_return = DownmixSw::kDescriptor;
57 return EX_NONE;
58 }
59
60 namespace aidl::android::hardware::audio::effect {
61
62 const std::string DownmixSw::kEffectName = "DownmixSw";
63 const Descriptor DownmixSw::kDescriptor = {
64 .common = {.id = {.type = getEffectTypeUuidDownmix(),
65 .uuid = getEffectImplUuidDownmixSw(),
66 .proxy = std::nullopt},
67 .flags = {.type = Flags::Type::INSERT,
68 .insert = Flags::Insert::FIRST,
69 .volume = Flags::Volume::CTRL},
70 .name = kEffectName,
71 .implementor = "The Android Open Source Project"}};
72
getDescriptor(Descriptor * _aidl_return)73 ndk::ScopedAStatus DownmixSw::getDescriptor(Descriptor* _aidl_return) {
74 LOG(DEBUG) << __func__ << kDescriptor.toString();
75 *_aidl_return = kDescriptor;
76 return ndk::ScopedAStatus::ok();
77 }
78
setParameterSpecific(const Parameter::Specific & specific)79 ndk::ScopedAStatus DownmixSw::setParameterSpecific(const Parameter::Specific& specific) {
80 RETURN_IF(Parameter::Specific::downmix != specific.getTag(), EX_ILLEGAL_ARGUMENT,
81 "EffectNotSupported");
82 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
83
84 auto& dmParam = specific.get<Parameter::Specific::downmix>();
85 auto tag = dmParam.getTag();
86
87 switch (tag) {
88 case Downmix::type: {
89 RETURN_IF(mContext->setDmType(dmParam.get<Downmix::type>()) != RetCode::SUCCESS,
90 EX_ILLEGAL_ARGUMENT, "setTypeFailed");
91 return ndk::ScopedAStatus::ok();
92 }
93 default: {
94 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
95 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
96 "DownmixTagNotSupported");
97 }
98 }
99 }
100
getParameterSpecific(const Parameter::Id & id,Parameter::Specific * specific)101 ndk::ScopedAStatus DownmixSw::getParameterSpecific(const Parameter::Id& id,
102 Parameter::Specific* specific) {
103 auto tag = id.getTag();
104 RETURN_IF(Parameter::Id::downmixTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
105 auto dmId = id.get<Parameter::Id::downmixTag>();
106 auto dmIdTag = dmId.getTag();
107 switch (dmIdTag) {
108 case Downmix::Id::commonTag:
109 return getParameterDownmix(dmId.get<Downmix::Id::commonTag>(), specific);
110 default:
111 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
112 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
113 "DownmixTagNotSupported");
114 }
115 }
116
getParameterDownmix(const Downmix::Tag & tag,Parameter::Specific * specific)117 ndk::ScopedAStatus DownmixSw::getParameterDownmix(const Downmix::Tag& tag,
118 Parameter::Specific* specific) {
119 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
120 Downmix dmParam;
121 switch (tag) {
122 case Downmix::type: {
123 dmParam.set<Downmix::type>(mContext->getDmType());
124 break;
125 }
126 default: {
127 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
128 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
129 "DownmixTagNotSupported");
130 }
131 }
132
133 specific->set<Parameter::Specific::downmix>(dmParam);
134 return ndk::ScopedAStatus::ok();
135 }
136
createContext(const Parameter::Common & common)137 std::shared_ptr<EffectContext> DownmixSw::createContext(const Parameter::Common& common) {
138 if (mContext) {
139 LOG(DEBUG) << __func__ << " context already exist";
140 } else {
141 mContext = std::make_shared<DownmixSwContext>(1 /* statusFmqDepth */, common);
142 }
143
144 return mContext;
145 }
146
releaseContext()147 RetCode DownmixSw::releaseContext() {
148 if (mContext) {
149 mContext.reset();
150 }
151 return RetCode::SUCCESS;
152 }
153
154 // Processing method running in EffectWorker thread.
effectProcessImpl(float * in,float * out,int samples)155 IEffect::Status DownmixSw::effectProcessImpl(float* in, float* out, int samples) {
156 // TODO: get data buffer and process.
157 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
158 for (int i = 0; i < samples; i++) {
159 *out++ = *in++;
160 }
161 return {STATUS_OK, samples, samples};
162 }
163
164 } // namespace aidl::android::hardware::audio::effect
165