blob: cb09293e28904da441676b35a87d05d06b0b5a8c [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>
Shunkai Yao812d5b42022-11-16 18:08:50 +000018#define LOG_TAG "AHAL_EnvReverbSw"
Shunkai Yao6afc8552022-10-26 22:47:20 +000019#include <Utils.h>
20#include <algorithm>
21#include <unordered_set>
22
23#include <android-base/logging.h>
24#include <fmq/AidlMessageQueue.h>
25
Shunkai Yao812d5b42022-11-16 18:08:50 +000026#include "EnvReverbSw.h"
Shunkai Yao6afc8552022-10-26 22:47:20 +000027
Shunkai Yao812d5b42022-11-16 18:08:50 +000028using aidl::android::hardware::audio::effect::EnvReverbSw;
Shunkai Yao6afc8552022-10-26 22:47:20 +000029using aidl::android::hardware::audio::effect::IEffect;
Shunkai Yao812d5b42022-11-16 18:08:50 +000030using aidl::android::hardware::audio::effect::kEnvReverbSwImplUUID;
Shunkai Yao6afc8552022-10-26 22:47:20 +000031using aidl::android::hardware::audio::effect::State;
32using 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 != kEnvReverbSwImplUUID) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000037 LOG(ERROR) << __func__ << "uuid not supported";
38 return EX_ILLEGAL_ARGUMENT;
39 }
40 if (instanceSpp) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000041 *instanceSpp = ndk::SharedRefBase::make<EnvReverbSw>();
Shunkai Yao6afc8552022-10-26 22:47:20 +000042 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
Shunkai Yao812d5b42022-11-16 18:08:50 +000067ndk::ScopedAStatus EnvReverbSw::getDescriptor(Descriptor* _aidl_return) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000068 LOG(DEBUG) << __func__ << kDescriptor.toString();
69 *_aidl_return = kDescriptor;
70 return ndk::ScopedAStatus::ok();
71}
72
Shunkai Yao812d5b42022-11-16 18:08:50 +000073ndk::ScopedAStatus EnvReverbSw::setParameterSpecific(const Parameter::Specific& specific) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000074 RETURN_IF(Parameter::Specific::reverb != specific.getTag(), EX_ILLEGAL_ARGUMENT,
75 "EffectNotSupported");
Shunkai Yao6afc8552022-10-26 22:47:20 +000076
77 mSpecificParam = specific.get<Parameter::Specific::reverb>();
78 LOG(DEBUG) << __func__ << " success with: " << specific.toString();
79 return ndk::ScopedAStatus::ok();
80}
81
Shunkai Yao812d5b42022-11-16 18:08:50 +000082ndk::ScopedAStatus EnvReverbSw::getParameterSpecific(const Parameter::Id& id,
83 Parameter::Specific* specific) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000084 auto tag = id.getTag();
85 RETURN_IF(Parameter::Id::reverbTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
86 specific->set<Parameter::Specific::reverb>(mSpecificParam);
87 return ndk::ScopedAStatus::ok();
88}
89
Shunkai Yao812d5b42022-11-16 18:08:50 +000090std::shared_ptr<EffectContext> EnvReverbSw::createContext(const Parameter::Common& common) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000091 if (mContext) {
92 LOG(DEBUG) << __func__ << " context already exist";
Shraddha Basantwanif627d802022-11-08 14:45:07 +053093 } else {
94 mContext = std::make_shared<EnvReverbSwContext>(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> EnvReverbSw::getContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000101 return mContext;
102}
103
Shunkai Yao812d5b42022-11-16 18:08:50 +0000104RetCode EnvReverbSw::releaseContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000105 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 EnvReverbSw::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