blob: 19ab2e8f6a558b55d374a97c38cdb1e2545db454 [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
Sham Rathod40f55bd2022-11-14 14:24:49 +053017#include <algorithm>
Mikhail Naganov872d4a62023-03-09 18:19:01 -080018#include <cstddef>
Sham Rathod40f55bd2022-11-14 14:24:49 +053019
Mikhail Naganov872d4a62023-03-09 18:19:01 -080020#define LOG_TAG "AHAL_DownmixSw"
Sham Rathod40f55bd2022-11-14 14:24:49 +053021#include <android-base/logging.h>
22#include <fmq/AidlMessageQueue.h>
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000023#include <system/audio_effects/effect_uuid.h>
Sham Rathod40f55bd2022-11-14 14:24:49 +053024
25#include "DownmixSw.h"
26
27using aidl::android::hardware::audio::effect::Descriptor;
28using aidl::android::hardware::audio::effect::DownmixSw;
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000029using aidl::android::hardware::audio::effect::getEffectImplUuidDownmixSw;
30using aidl::android::hardware::audio::effect::getEffectTypeUuidDownmix;
Sham Rathod40f55bd2022-11-14 14:24:49 +053031using aidl::android::hardware::audio::effect::IEffect;
Sham Rathod40f55bd2022-11-14 14:24:49 +053032using 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) {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000037 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidDownmixSw()) {
Sham Rathod40f55bd2022-11-14 14:24:49 +053038 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) {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000052 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidDownmixSw()) {
Sham Rathod40f55bd2022-11-14 14:24:49 +053053 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";
Sham Rathod40f55bd2022-11-14 14:24:49 +053063const Descriptor DownmixSw::kDescriptor = {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000064 .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"}};
Sham Rathod40f55bd2022-11-14 14:24:49 +053072
73ndk::ScopedAStatus DownmixSw::getDescriptor(Descriptor* _aidl_return) {
74 LOG(DEBUG) << __func__ << kDescriptor.toString();
75 *_aidl_return = kDescriptor;
76 return ndk::ScopedAStatus::ok();
77}
78
79ndk::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
101ndk::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
117ndk::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
137std::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
Sham Rathod40f55bd2022-11-14 14:24:49 +0530147RetCode DownmixSw::releaseContext() {
148 if (mContext) {
149 mContext.reset();
150 }
151 return RetCode::SUCCESS;
152}
153
154// Processing method running in EffectWorker thread.
155IEffect::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