blob: c82c23bde822e7d8effa89cbacbc665b04031fc5 [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
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() {
174 /**
175 * wait for the EventFlag without lock, it's ok because the mEfGroup pointer will not change
176 * in the life cycle of workerThread (threadLoop).
177 */
178 uint32_t efState = 0;
179 if (!mEventFlag || ::android::OK != mEventFlag->wait(kEventFlagNotEmpty, &efState)) {
180 LOG(ERROR) << getEffectName() << __func__ << ": StatusEventFlag invalid";
181 }
182
183 {
184 std::lock_guard lg(mImplMutex);
185 RETURN_VALUE_IF(!mImplContext, void(), "nullContext");
186 auto statusMQ = mImplContext->getStatusFmq();
187 auto inputMQ = mImplContext->getInputDataFmq();
188 auto outputMQ = mImplContext->getOutputDataFmq();
189 auto buffer = mImplContext->getWorkBuffer();
190 if (!inputMQ || !outputMQ) {
191 return;
192 }
193
194 const auto availableToRead = inputMQ->availableToRead();
195 const auto availableToWrite = outputMQ->availableToWrite() *
196 mImplContext->getInputFrameSize() /
197 mImplContext->getOutputFrameSize();
198 auto processSamples = std::min(availableToRead, availableToWrite);
199 if (processSamples) {
200 inputMQ->read(buffer, processSamples);
201 IEffect::Status status = effectProcessImpl(buffer, buffer, processSamples);
202 outputMQ->write(buffer, status.fmqProduced);
203 statusMQ->writeBlocking(&status, 1);
204 LOG(VERBOSE) << getEffectName() << __func__ << ": done processing, effect consumed "
205 << status.fmqConsumed << " produced " << status.fmqProduced;
206 }
207 }
208}
209
Shraddha Basantwaniaa095252022-12-13 16:35:23 +0530210// Processing method running in EffectWorker thread.
211IEffect::Status DownmixImpl::effectProcessImpl(float* in, float* out, int sampleToProcess) {
212 if (!mContext) {
213 LOG(ERROR) << __func__ << " nullContext";
214 return {EX_NULL_POINTER, 0, 0};
215 }
David Li28aeab12023-12-19 16:43:32 +0800216 return mContext->downmixProcess(in, out, sampleToProcess);
Shraddha Basantwaniaa095252022-12-13 16:35:23 +0530217}
218
219} // namespace aidl::android::hardware::audio::effect