blob: e2f42d74138434695b8b17636f09067a61787d00 [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_VolumeSw"
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 "VolumeSw.h"
27
28using aidl::android::hardware::audio::effect::IEffect;
29using aidl::android::hardware::audio::effect::State;
30using aidl::android::hardware::audio::effect::VolumeSw;
31using aidl::android::hardware::audio::effect::VolumeSwImplUUID;
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) {
36 if (!in_impl_uuid || *in_impl_uuid != VolumeSwImplUUID) {
37 LOG(ERROR) << __func__ << "uuid not supported";
38 return EX_ILLEGAL_ARGUMENT;
39 }
40 if (instanceSpp) {
41 *instanceSpp = ndk::SharedRefBase::make<VolumeSw>();
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 VolumeSw::getDescriptor(Descriptor* _aidl_return) {
68 LOG(DEBUG) << __func__ << kDescriptor.toString();
69 *_aidl_return = kDescriptor;
70 return ndk::ScopedAStatus::ok();
71}
72
73ndk::ScopedAStatus VolumeSw::setParameterSpecific(const Parameter::Specific& specific) {
74 RETURN_IF(Parameter::Specific::volume != specific.getTag(), EX_ILLEGAL_ARGUMENT,
75 "EffectNotSupported");
76 std::lock_guard lg(mMutex);
77 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
78
79 mSpecificParam = specific.get<Parameter::Specific::volume>();
80 LOG(DEBUG) << __func__ << " success with: " << specific.toString();
81 return ndk::ScopedAStatus::ok();
82}
83
84ndk::ScopedAStatus VolumeSw::getParameterSpecific(const Parameter::Id& id,
85 Parameter::Specific* specific) {
86 auto tag = id.getTag();
87 RETURN_IF(Parameter::Id::volumeTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
88 specific->set<Parameter::Specific::volume>(mSpecificParam);
89 return ndk::ScopedAStatus::ok();
90}
91
92std::shared_ptr<EffectContext> VolumeSw::createContext(const Parameter::Common& common) {
93 if (mContext) {
94 LOG(DEBUG) << __func__ << " context already exist";
95 return mContext;
96 }
97 mContext = std::make_shared<VolumeSwContext>(1 /* statusFmqDepth */, common);
98 return mContext;
99}
100
101RetCode VolumeSw::releaseContext() {
102 if (mContext) {
103 mContext.reset();
104 }
105 return RetCode::SUCCESS;
106}
107
108// Processing method running in EffectWorker thread.
109IEffect::Status VolumeSw::effectProcessImpl(float* in, float* out, int process) {
110 // TODO: get data buffer and process.
111 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " process " << process;
112 for (int i = 0; i < process; i++) {
113 *out++ = *in++;
114 }
115 return {STATUS_OK, process, process};
116}
117
118} // namespace aidl::android::hardware::audio::effect