blob: de60ca492104b84a3e26b9041da160db2ec6515b [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
Shunkai Yaoac61ee92024-03-14 21:57:46 +000017#define ATRACE_TAG ATRACE_TAG_AUDIO
Shraddha Basantwaniaa095252022-12-13 16:35:23 +053018#define LOG_TAG "AHAL_DownmixImpl"
19
20#include <android-base/logging.h>
Shunkai Yao399be682023-03-06 18:54:18 +000021#include <system/audio_effects/effect_uuid.h>
Shunkai Yaoac61ee92024-03-14 21:57:46 +000022#include <utils/Trace.h>
Shraddha Basantwaniaa095252022-12-13 16:35:23 +053023
24#include "EffectDownmix.h"
25
26using aidl::android::hardware::audio::effect::Descriptor;
27using aidl::android::hardware::audio::effect::DownmixImpl;
Shunkai Yao399be682023-03-06 18:54:18 +000028using aidl::android::hardware::audio::effect::getEffectImplUuidDownmix;
29using aidl::android::hardware::audio::effect::getEffectTypeUuidDownmix;
Shraddha Basantwaniaa095252022-12-13 16:35:23 +053030using aidl::android::hardware::audio::effect::IEffect;
Shraddha Basantwaniaa095252022-12-13 16:35:23 +053031using aidl::android::media::audio::common::AudioUuid;
32
33extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
34 std::shared_ptr<IEffect>* instanceSpp) {
Shunkai Yao399be682023-03-06 18:54:18 +000035 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidDownmix()) {
Shraddha Basantwaniaa095252022-12-13 16:35:23 +053036 LOG(ERROR) << __func__ << "uuid not supported";
37 return EX_ILLEGAL_ARGUMENT;
38 }
39 if (instanceSpp) {
40 *instanceSpp = ndk::SharedRefBase::make<DownmixImpl>();
Shraddha Basantwaniaa095252022-12-13 16:35:23 +053041 return EX_NONE;
42 } else {
43 LOG(ERROR) << __func__ << " invalid input parameter!";
44 return EX_ILLEGAL_ARGUMENT;
45 }
46}
47
48extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
Shunkai Yao399be682023-03-06 18:54:18 +000049 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidDownmix()) {
Shraddha Basantwaniaa095252022-12-13 16:35:23 +053050 LOG(ERROR) << __func__ << "uuid not supported";
51 return EX_ILLEGAL_ARGUMENT;
52 }
53 *_aidl_return = DownmixImpl::kDescriptor;
54 return EX_NONE;
55}
56
57namespace aidl::android::hardware::audio::effect {
58
59const std::string DownmixImpl::kEffectName = "Multichannel Downmix To Stereo";
60const Descriptor DownmixImpl::kDescriptor = {
Shunkai Yao399be682023-03-06 18:54:18 +000061 .common = {.id = {.type = getEffectTypeUuidDownmix(),
62 .uuid = getEffectImplUuidDownmix(),
63 .proxy = std::nullopt},
64 .flags = {.type = Flags::Type::INSERT, .insert = Flags::Insert::FIRST},
65 .name = DownmixImpl::kEffectName,
66 .implementor = "The Android Open Source Project"}};
Shraddha Basantwaniaa095252022-12-13 16:35:23 +053067
68ndk::ScopedAStatus DownmixImpl::getDescriptor(Descriptor* _aidl_return) {
69 RETURN_IF(!_aidl_return, EX_ILLEGAL_ARGUMENT, "Parameter:nullptr");
Shraddha Basantwaniaa095252022-12-13 16:35:23 +053070 *_aidl_return = kDescriptor;
71 return ndk::ScopedAStatus::ok();
72}
73
Shraddha Basantwaniaa095252022-12-13 16:35:23 +053074ndk::ScopedAStatus DownmixImpl::commandImpl(CommandId command) {
75 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
76 switch (command) {
77 case CommandId::START:
78 mContext->enable();
79 break;
80 case CommandId::STOP:
81 mContext->disable();
82 break;
83 case CommandId::RESET:
84 mContext->reset();
85 break;
86 default:
87 LOG(ERROR) << __func__ << " commandId " << toString(command) << " not supported";
88 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
89 "commandIdNotSupported");
90 }
91 return ndk::ScopedAStatus::ok();
92}
93
94ndk::ScopedAStatus DownmixImpl::setParameterSpecific(const Parameter::Specific& specific) {
95 RETURN_IF(Parameter::Specific::downmix != specific.getTag(), EX_ILLEGAL_ARGUMENT,
96 "EffectNotSupported");
97 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
98
99 auto& dmParam = specific.get<Parameter::Specific::downmix>();
100 auto tag = dmParam.getTag();
101
102 switch (tag) {
103 case Downmix::type: {
104 RETURN_IF(mContext->setDmType(dmParam.get<Downmix::type>()) != RetCode::SUCCESS,
105 EX_ILLEGAL_ARGUMENT, "setTypeFailed");
106 return ndk::ScopedAStatus::ok();
107 }
108 default: {
109 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
110 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
111 "DownmixTagNotSupported");
112 }
113 }
114}
115
116ndk::ScopedAStatus DownmixImpl::getParameterSpecific(const Parameter::Id& id,
117 Parameter::Specific* specific) {
118 RETURN_IF(!specific, EX_NULL_POINTER, "nullPtr");
119 auto tag = id.getTag();
120 RETURN_IF(Parameter::Id::downmixTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
121 auto dmId = id.get<Parameter::Id::downmixTag>();
122 auto dmIdTag = dmId.getTag();
123 switch (dmIdTag) {
124 case Downmix::Id::commonTag:
125 return getParameterDownmix(dmId.get<Downmix::Id::commonTag>(), specific);
126 default:
127 LOG(ERROR) << __func__ << " unsupported tag: " << toString(dmIdTag);
128 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
129 "DownmixTagNotSupported");
130 }
131}
132
133ndk::ScopedAStatus DownmixImpl::getParameterDownmix(const Downmix::Tag& tag,
134 Parameter::Specific* specific) {
135 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
136
137 Downmix dmParam;
138 switch (tag) {
139 case Downmix::type: {
140 dmParam.set<Downmix::type>(mContext->getDmType());
141 break;
142 }
143 default: {
144 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
145 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
146 "DownmixTagNotSupported");
147 }
148 }
149
150 specific->set<Parameter::Specific::downmix>(dmParam);
151 return ndk::ScopedAStatus::ok();
152}
153
154std::shared_ptr<EffectContext> DownmixImpl::createContext(const Parameter::Common& common) {
155 if (mContext) {
156 LOG(DEBUG) << __func__ << " context already exist";
157 return mContext;
158 }
159
David Li28aeab12023-12-19 16:43:32 +0800160 if (!DownmixContext::validateCommonConfig(common)) return nullptr;
161
Shraddha Basantwaniaa095252022-12-13 16:35:23 +0530162 mContext = std::make_shared<DownmixContext>(1 /* statusFmqDepth */, common);
163 return mContext;
164}
165
166RetCode DownmixImpl::releaseContext() {
167 if (mContext) {
168 mContext.reset();
169 }
170 return RetCode::SUCCESS;
171}
172
Shunkai Yao1afb46c2024-01-09 20:40:45 +0000173void DownmixImpl::process() {
Shunkai Yaoac61ee92024-03-14 21:57:46 +0000174 ATRACE_NAME("Downmix::process");
Shunkai Yao1afb46c2024-01-09 20:40:45 +0000175 /**
176 * wait for the EventFlag without lock, it's ok because the mEfGroup pointer will not change
177 * in the life cycle of workerThread (threadLoop).
178 */
179 uint32_t efState = 0;
180 if (!mEventFlag || ::android::OK != mEventFlag->wait(kEventFlagNotEmpty, &efState)) {
181 LOG(ERROR) << getEffectName() << __func__ << ": StatusEventFlag invalid";
182 }
183
184 {
185 std::lock_guard lg(mImplMutex);
186 RETURN_VALUE_IF(!mImplContext, void(), "nullContext");
187 auto statusMQ = mImplContext->getStatusFmq();
188 auto inputMQ = mImplContext->getInputDataFmq();
189 auto outputMQ = mImplContext->getOutputDataFmq();
190 auto buffer = mImplContext->getWorkBuffer();
191 if (!inputMQ || !outputMQ) {
192 return;
193 }
194
195 const auto availableToRead = inputMQ->availableToRead();
196 const auto availableToWrite = outputMQ->availableToWrite() *
197 mImplContext->getInputFrameSize() /
198 mImplContext->getOutputFrameSize();
Shunkai Yaod0e64b12024-01-25 00:44:33 +0000199 assert(mImplContext->getWorkBufferSize() >=
200 std::max(availableToRead(), availableToWrite));
Shunkai Yao1afb46c2024-01-09 20:40:45 +0000201 auto processSamples = std::min(availableToRead, availableToWrite);
202 if (processSamples) {
203 inputMQ->read(buffer, processSamples);
204 IEffect::Status status = effectProcessImpl(buffer, buffer, processSamples);
205 outputMQ->write(buffer, status.fmqProduced);
206 statusMQ->writeBlocking(&status, 1);
Shunkai Yao1afb46c2024-01-09 20:40:45 +0000207 }
208 }
209}
210
Shraddha Basantwaniaa095252022-12-13 16:35:23 +0530211// Processing method running in EffectWorker thread.
212IEffect::Status DownmixImpl::effectProcessImpl(float* in, float* out, int sampleToProcess) {
213 if (!mContext) {
214 LOG(ERROR) << __func__ << " nullContext";
215 return {EX_NULL_POINTER, 0, 0};
216 }
David Li28aeab12023-12-19 16:43:32 +0800217 return mContext->downmixProcess(in, out, sampleToProcess);
Shraddha Basantwaniaa095252022-12-13 16:35:23 +0530218}
219
220} // namespace aidl::android::hardware::audio::effect