blob: 0af95d0bd4fc36aa9c939fab016d9278949708ff [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
17#include <cstddef>
18#define LOG_TAG "AHAL_DownmixSw"
19#include <Utils.h>
20#include <algorithm>
21#include <unordered_set>
22
23#include <android-base/logging.h>
24#include <fmq/AidlMessageQueue.h>
25
26#include "DownmixSw.h"
27
28using aidl::android::hardware::audio::effect::Descriptor;
29using aidl::android::hardware::audio::effect::DownmixSw;
30using aidl::android::hardware::audio::effect::IEffect;
31using aidl::android::hardware::audio::effect::kDownmixSwImplUUID;
32using 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) {
37 if (!in_impl_uuid || *in_impl_uuid != kDownmixSwImplUUID) {
38 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) {
52 if (!in_impl_uuid || *in_impl_uuid != kDownmixSwImplUUID) {
53 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 Yao87811022023-02-13 17:40:37 +000064 .common = {
65 .id = {.type = kDownmixTypeUUID, .uuid = kDownmixSwImplUUID, .proxy = std::nullopt},
66 .flags = {.type = Flags::Type::INSERT,
67 .insert = Flags::Insert::FIRST,
68 .volume = Flags::Volume::CTRL},
69 .name = kEffectName,
70 .implementor = "The Android Open Source Project"}};
Sham Rathod40f55bd2022-11-14 14:24:49 +053071
72ndk::ScopedAStatus DownmixSw::getDescriptor(Descriptor* _aidl_return) {
73 LOG(DEBUG) << __func__ << kDescriptor.toString();
74 *_aidl_return = kDescriptor;
75 return ndk::ScopedAStatus::ok();
76}
77
78ndk::ScopedAStatus DownmixSw::setParameterSpecific(const Parameter::Specific& specific) {
79 RETURN_IF(Parameter::Specific::downmix != specific.getTag(), EX_ILLEGAL_ARGUMENT,
80 "EffectNotSupported");
81 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
82
83 auto& dmParam = specific.get<Parameter::Specific::downmix>();
84 auto tag = dmParam.getTag();
85
86 switch (tag) {
87 case Downmix::type: {
88 RETURN_IF(mContext->setDmType(dmParam.get<Downmix::type>()) != RetCode::SUCCESS,
89 EX_ILLEGAL_ARGUMENT, "setTypeFailed");
90 return ndk::ScopedAStatus::ok();
91 }
92 default: {
93 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
94 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
95 "DownmixTagNotSupported");
96 }
97 }
98}
99
100ndk::ScopedAStatus DownmixSw::getParameterSpecific(const Parameter::Id& id,
101 Parameter::Specific* specific) {
102 auto tag = id.getTag();
103 RETURN_IF(Parameter::Id::downmixTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
104 auto dmId = id.get<Parameter::Id::downmixTag>();
105 auto dmIdTag = dmId.getTag();
106 switch (dmIdTag) {
107 case Downmix::Id::commonTag:
108 return getParameterDownmix(dmId.get<Downmix::Id::commonTag>(), specific);
109 default:
110 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
111 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
112 "DownmixTagNotSupported");
113 }
114}
115
116ndk::ScopedAStatus DownmixSw::getParameterDownmix(const Downmix::Tag& tag,
117 Parameter::Specific* specific) {
118 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
119 Downmix dmParam;
120 switch (tag) {
121 case Downmix::type: {
122 dmParam.set<Downmix::type>(mContext->getDmType());
123 break;
124 }
125 default: {
126 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
127 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
128 "DownmixTagNotSupported");
129 }
130 }
131
132 specific->set<Parameter::Specific::downmix>(dmParam);
133 return ndk::ScopedAStatus::ok();
134}
135
136std::shared_ptr<EffectContext> DownmixSw::createContext(const Parameter::Common& common) {
137 if (mContext) {
138 LOG(DEBUG) << __func__ << " context already exist";
139 } else {
140 mContext = std::make_shared<DownmixSwContext>(1 /* statusFmqDepth */, common);
141 }
142
143 return mContext;
144}
145
146std::shared_ptr<EffectContext> DownmixSw::getContext() {
147 return mContext;
148}
149
150RetCode DownmixSw::releaseContext() {
151 if (mContext) {
152 mContext.reset();
153 }
154 return RetCode::SUCCESS;
155}
156
157// Processing method running in EffectWorker thread.
158IEffect::Status DownmixSw::effectProcessImpl(float* in, float* out, int samples) {
159 // TODO: get data buffer and process.
160 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
161 for (int i = 0; i < samples; i++) {
162 *out++ = *in++;
163 }
164 return {STATUS_OK, samples, samples};
165}
166
167} // namespace aidl::android::hardware::audio::effect