blob: 125fbee455674195c56a34390747f7783e92abdc [file] [log] [blame]
Shunkai Yao6afc8552022-10-26 22:47:20 +00001/*
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_VirtualizerSw"
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 "VirtualizerSw.h"
27
28using aidl::android::hardware::audio::effect::IEffect;
Shunkai Yao812d5b42022-11-16 18:08:50 +000029using aidl::android::hardware::audio::effect::kVirtualizerSwImplUUID;
Shunkai Yao6afc8552022-10-26 22:47:20 +000030using aidl::android::hardware::audio::effect::State;
31using aidl::android::hardware::audio::effect::VirtualizerSw;
Shunkai Yao6afc8552022-10-26 22:47:20 +000032using aidl::android::media::audio::common::AudioUuid;
33
34extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
35 std::shared_ptr<IEffect>* instanceSpp) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000036 if (!in_impl_uuid || *in_impl_uuid != kVirtualizerSwImplUUID) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000037 LOG(ERROR) << __func__ << "uuid not supported";
38 return EX_ILLEGAL_ARGUMENT;
39 }
40 if (instanceSpp) {
41 *instanceSpp = ndk::SharedRefBase::make<VirtualizerSw>();
42 LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
43 return EX_NONE;
44 } else {
45 LOG(ERROR) << __func__ << " invalid input parameter!";
46 return EX_ILLEGAL_ARGUMENT;
47 }
48}
49
50extern "C" binder_exception_t destroyEffect(const std::shared_ptr<IEffect>& instanceSp) {
51 if (!instanceSp) {
52 return EX_NONE;
53 }
54 State state;
55 ndk::ScopedAStatus status = instanceSp->getState(&state);
56 if (!status.isOk() || State::INIT != state) {
57 LOG(ERROR) << __func__ << " instance " << instanceSp.get()
58 << " in state: " << toString(state) << ", status: " << status.getDescription();
59 return EX_ILLEGAL_STATE;
60 }
61 LOG(DEBUG) << __func__ << " instance " << instanceSp.get() << " destroyed";
62 return EX_NONE;
63}
64
65namespace aidl::android::hardware::audio::effect {
66
67ndk::ScopedAStatus VirtualizerSw::getDescriptor(Descriptor* _aidl_return) {
68 LOG(DEBUG) << __func__ << kDescriptor.toString();
69 *_aidl_return = kDescriptor;
70 return ndk::ScopedAStatus::ok();
71}
72
73ndk::ScopedAStatus VirtualizerSw::setParameterSpecific(const Parameter::Specific& specific) {
74 RETURN_IF(Parameter::Specific::virtualizer != specific.getTag(), EX_ILLEGAL_ARGUMENT,
75 "EffectNotSupported");
Shunkai Yao6afc8552022-10-26 22:47:20 +000076
77 mSpecificParam = specific.get<Parameter::Specific::virtualizer>();
78 LOG(DEBUG) << __func__ << " success with: " << specific.toString();
79 return ndk::ScopedAStatus::ok();
80}
81
82ndk::ScopedAStatus VirtualizerSw::getParameterSpecific(const Parameter::Id& id,
83 Parameter::Specific* specific) {
84 auto tag = id.getTag();
85 RETURN_IF(Parameter::Id::virtualizerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
86 specific->set<Parameter::Specific::virtualizer>(mSpecificParam);
87 return ndk::ScopedAStatus::ok();
88}
89
90std::shared_ptr<EffectContext> VirtualizerSw::createContext(const Parameter::Common& common) {
91 if (mContext) {
92 LOG(DEBUG) << __func__ << " context already exist";
Shraddha Basantwanif627d802022-11-08 14:45:07 +053093 } else {
94 mContext = std::make_shared<VirtualizerSwContext>(1 /* statusFmqDepth */, common);
Shunkai Yao6afc8552022-10-26 22:47:20 +000095 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +053096
97 return mContext;
98}
99
100std::shared_ptr<EffectContext> VirtualizerSw::getContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000101 return mContext;
102}
103
104RetCode VirtualizerSw::releaseContext() {
105 if (mContext) {
106 mContext.reset();
107 }
108 return RetCode::SUCCESS;
109}
110
111// Processing method running in EffectWorker thread.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530112IEffect::Status VirtualizerSw::effectProcessImpl(float* in, float* out, int samples) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000113 // TODO: get data buffer and process.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530114 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
115 for (int i = 0; i < samples; i++) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000116 *out++ = *in++;
117 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530118 return {STATUS_OK, samples, samples};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000119}
120
121} // namespace aidl::android::hardware::audio::effect