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