blob: 702a6f0f2a89b8170f3d281adaa69f27a9765718 [file] [log] [blame]
Shraddha Basantwaniaa095252022-12-13 16:35:23 +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#define LOG_TAG "AHAL_DownmixImpl"
18
19#include <android-base/logging.h>
Shunkai Yao399be682023-03-06 18:54:18 +000020#include <system/audio_effects/effect_uuid.h>
Shraddha Basantwaniaa095252022-12-13 16:35:23 +053021
22#include "EffectDownmix.h"
23
24using aidl::android::hardware::audio::effect::Descriptor;
25using aidl::android::hardware::audio::effect::DownmixImpl;
Shunkai Yao399be682023-03-06 18:54:18 +000026using aidl::android::hardware::audio::effect::getEffectImplUuidDownmix;
27using aidl::android::hardware::audio::effect::getEffectTypeUuidDownmix;
Shraddha Basantwaniaa095252022-12-13 16:35:23 +053028using aidl::android::hardware::audio::effect::IEffect;
Shraddha Basantwaniaa095252022-12-13 16:35:23 +053029using aidl::android::media::audio::common::AudioUuid;
30
31extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
32 std::shared_ptr<IEffect>* instanceSpp) {
Shunkai Yao399be682023-03-06 18:54:18 +000033 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidDownmix()) {
Shraddha Basantwaniaa095252022-12-13 16:35:23 +053034 LOG(ERROR) << __func__ << "uuid not supported";
35 return EX_ILLEGAL_ARGUMENT;
36 }
37 if (instanceSpp) {
38 *instanceSpp = ndk::SharedRefBase::make<DownmixImpl>();
39 LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
40 return EX_NONE;
41 } else {
42 LOG(ERROR) << __func__ << " invalid input parameter!";
43 return EX_ILLEGAL_ARGUMENT;
44 }
45}
46
47extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
Shunkai Yao399be682023-03-06 18:54:18 +000048 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidDownmix()) {
Shraddha Basantwaniaa095252022-12-13 16:35:23 +053049 LOG(ERROR) << __func__ << "uuid not supported";
50 return EX_ILLEGAL_ARGUMENT;
51 }
52 *_aidl_return = DownmixImpl::kDescriptor;
53 return EX_NONE;
54}
55
56namespace aidl::android::hardware::audio::effect {
57
58const std::string DownmixImpl::kEffectName = "Multichannel Downmix To Stereo";
59const Descriptor DownmixImpl::kDescriptor = {
Shunkai Yao399be682023-03-06 18:54:18 +000060 .common = {.id = {.type = getEffectTypeUuidDownmix(),
61 .uuid = getEffectImplUuidDownmix(),
62 .proxy = std::nullopt},
63 .flags = {.type = Flags::Type::INSERT, .insert = Flags::Insert::FIRST},
64 .name = DownmixImpl::kEffectName,
65 .implementor = "The Android Open Source Project"}};
Shraddha Basantwaniaa095252022-12-13 16:35:23 +053066
67ndk::ScopedAStatus DownmixImpl::getDescriptor(Descriptor* _aidl_return) {
68 RETURN_IF(!_aidl_return, EX_ILLEGAL_ARGUMENT, "Parameter:nullptr");
69 LOG(DEBUG) << __func__ << kDescriptor.toString();
70 *_aidl_return = kDescriptor;
71 return ndk::ScopedAStatus::ok();
72}
73
74ndk::ScopedAStatus DownmixImpl::setParameterCommon(const Parameter& param) {
75 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
76
77 auto tag = param.getTag();
78 switch (tag) {
79 case Parameter::common:
80 RETURN_IF(mContext->setCommon(param.get<Parameter::common>()) != RetCode::SUCCESS,
81 EX_ILLEGAL_ARGUMENT, "setCommFailed");
82 break;
83 case Parameter::deviceDescription:
84 RETURN_IF(mContext->setOutputDevice(param.get<Parameter::deviceDescription>()) !=
85 RetCode::SUCCESS,
86 EX_ILLEGAL_ARGUMENT, "setDeviceFailed");
87 break;
88 case Parameter::mode:
89 RETURN_IF(mContext->setAudioMode(param.get<Parameter::mode>()) != RetCode::SUCCESS,
90 EX_ILLEGAL_ARGUMENT, "setModeFailed");
91 break;
92 case Parameter::source:
93 RETURN_IF(mContext->setAudioSource(param.get<Parameter::source>()) != RetCode::SUCCESS,
94 EX_ILLEGAL_ARGUMENT, "setSourceFailed");
95 break;
96 case Parameter::volumeStereo:
97 RETURN_IF(mContext->setVolumeStereo(param.get<Parameter::volumeStereo>()) !=
98 RetCode::SUCCESS,
99 EX_ILLEGAL_ARGUMENT, "setVolumeStereoFailed");
100 break;
101 default: {
102 LOG(ERROR) << __func__ << " unsupportedParameterTag " << toString(tag);
103 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
104 "commonParamNotSupported");
105 }
106 }
107 return ndk::ScopedAStatus::ok();
108}
109
110ndk::ScopedAStatus DownmixImpl::commandImpl(CommandId command) {
111 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
112 switch (command) {
113 case CommandId::START:
114 mContext->enable();
115 break;
116 case CommandId::STOP:
117 mContext->disable();
118 break;
119 case CommandId::RESET:
120 mContext->reset();
121 break;
122 default:
123 LOG(ERROR) << __func__ << " commandId " << toString(command) << " not supported";
124 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
125 "commandIdNotSupported");
126 }
127 return ndk::ScopedAStatus::ok();
128}
129
130ndk::ScopedAStatus DownmixImpl::setParameterSpecific(const Parameter::Specific& specific) {
131 RETURN_IF(Parameter::Specific::downmix != specific.getTag(), EX_ILLEGAL_ARGUMENT,
132 "EffectNotSupported");
133 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
134
135 auto& dmParam = specific.get<Parameter::Specific::downmix>();
136 auto tag = dmParam.getTag();
137
138 switch (tag) {
139 case Downmix::type: {
140 RETURN_IF(mContext->setDmType(dmParam.get<Downmix::type>()) != RetCode::SUCCESS,
141 EX_ILLEGAL_ARGUMENT, "setTypeFailed");
142 return ndk::ScopedAStatus::ok();
143 }
144 default: {
145 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
146 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
147 "DownmixTagNotSupported");
148 }
149 }
150}
151
152ndk::ScopedAStatus DownmixImpl::getParameterSpecific(const Parameter::Id& id,
153 Parameter::Specific* specific) {
154 RETURN_IF(!specific, EX_NULL_POINTER, "nullPtr");
155 auto tag = id.getTag();
156 RETURN_IF(Parameter::Id::downmixTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
157 auto dmId = id.get<Parameter::Id::downmixTag>();
158 auto dmIdTag = dmId.getTag();
159 switch (dmIdTag) {
160 case Downmix::Id::commonTag:
161 return getParameterDownmix(dmId.get<Downmix::Id::commonTag>(), specific);
162 default:
163 LOG(ERROR) << __func__ << " unsupported tag: " << toString(dmIdTag);
164 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
165 "DownmixTagNotSupported");
166 }
167}
168
169ndk::ScopedAStatus DownmixImpl::getParameterDownmix(const Downmix::Tag& tag,
170 Parameter::Specific* specific) {
171 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
172
173 Downmix dmParam;
174 switch (tag) {
175 case Downmix::type: {
176 dmParam.set<Downmix::type>(mContext->getDmType());
177 break;
178 }
179 default: {
180 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
181 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
182 "DownmixTagNotSupported");
183 }
184 }
185
186 specific->set<Parameter::Specific::downmix>(dmParam);
187 return ndk::ScopedAStatus::ok();
188}
189
190std::shared_ptr<EffectContext> DownmixImpl::createContext(const Parameter::Common& common) {
191 if (mContext) {
192 LOG(DEBUG) << __func__ << " context already exist";
193 return mContext;
194 }
195
David Li28aeab12023-12-19 16:43:32 +0800196 if (!DownmixContext::validateCommonConfig(common)) return nullptr;
197
Shraddha Basantwaniaa095252022-12-13 16:35:23 +0530198 mContext = std::make_shared<DownmixContext>(1 /* statusFmqDepth */, common);
199 return mContext;
200}
201
202RetCode DownmixImpl::releaseContext() {
203 if (mContext) {
204 mContext.reset();
205 }
206 return RetCode::SUCCESS;
207}
208
209// Processing method running in EffectWorker thread.
210IEffect::Status DownmixImpl::effectProcessImpl(float* in, float* out, int sampleToProcess) {
211 if (!mContext) {
212 LOG(ERROR) << __func__ << " nullContext";
213 return {EX_NULL_POINTER, 0, 0};
214 }
David Li28aeab12023-12-19 16:43:32 +0800215 return mContext->downmixProcess(in, out, sampleToProcess);
Shraddha Basantwaniaa095252022-12-13 16:35:23 +0530216}
217
218} // namespace aidl::android::hardware::audio::effect