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