blob: 24e1e1de39346813c7c4b87ee02016d424f5fc61 [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
Shunkai Yaoc12e0822022-12-12 07:13:58 +000028using aidl::android::hardware::audio::effect::Descriptor;
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::kVolumeSwImplUUID;
Shunkai Yao6afc8552022-10-26 22:47:20 +000031using aidl::android::hardware::audio::effect::State;
32using aidl::android::hardware::audio::effect::VolumeSw;
Shunkai Yao6afc8552022-10-26 22:47:20 +000033using aidl::android::media::audio::common::AudioUuid;
34
35extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
36 std::shared_ptr<IEffect>* instanceSpp) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000037 if (!in_impl_uuid || *in_impl_uuid != kVolumeSwImplUUID) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000038 LOG(ERROR) << __func__ << "uuid not supported";
39 return EX_ILLEGAL_ARGUMENT;
40 }
41 if (instanceSpp) {
42 *instanceSpp = ndk::SharedRefBase::make<VolumeSw>();
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
Shunkai Yaoc12e0822022-12-12 07:13:58 +000051extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
52 if (!in_impl_uuid || *in_impl_uuid != kVolumeSwImplUUID) {
53 LOG(ERROR) << __func__ << "uuid not supported";
54 return EX_ILLEGAL_ARGUMENT;
Shunkai Yao6afc8552022-10-26 22:47:20 +000055 }
Shunkai Yaoc12e0822022-12-12 07:13:58 +000056 *_aidl_return = VolumeSw::kDescriptor;
Shunkai Yao6afc8552022-10-26 22:47:20 +000057 return EX_NONE;
58}
59
60namespace aidl::android::hardware::audio::effect {
61
Shunkai Yaoc12e0822022-12-12 07:13:58 +000062const std::string VolumeSw::kEffectName = "VolumeSw";
Sham Rathod1b6c1f02022-11-22 17:39:22 +053063const Volume::Capability VolumeSw::kCapability = {.maxLevel = Volume::MAX_LEVEL_DB};
Shunkai Yaoc12e0822022-12-12 07:13:58 +000064const Descriptor VolumeSw::kDescriptor = {
65 .common = {.id = {.type = kVolumeTypeUUID,
66 .uuid = kVolumeSwImplUUID,
67 .proxy = std::nullopt},
68 .flags = {.type = Flags::Type::INSERT,
69 .insert = Flags::Insert::FIRST,
70 .volume = Flags::Volume::CTRL},
71 .name = VolumeSw::kEffectName,
72 .implementor = "The Android Open Source Project"},
73 .capability = Capability::make<Capability::volume>(VolumeSw::kCapability)};
74
Shunkai Yao6afc8552022-10-26 22:47:20 +000075ndk::ScopedAStatus VolumeSw::getDescriptor(Descriptor* _aidl_return) {
76 LOG(DEBUG) << __func__ << kDescriptor.toString();
77 *_aidl_return = kDescriptor;
78 return ndk::ScopedAStatus::ok();
79}
80
81ndk::ScopedAStatus VolumeSw::setParameterSpecific(const Parameter::Specific& specific) {
82 RETURN_IF(Parameter::Specific::volume != specific.getTag(), EX_ILLEGAL_ARGUMENT,
83 "EffectNotSupported");
Shunkai Yao6afc8552022-10-26 22:47:20 +000084
Sham Rathod1b6c1f02022-11-22 17:39:22 +053085 auto& volParam = specific.get<Parameter::Specific::volume>();
86 auto tag = volParam.getTag();
87
88 switch (tag) {
89 case Volume::levelDb: {
90 RETURN_IF(mContext->setVolLevel(volParam.get<Volume::levelDb>()) != RetCode::SUCCESS,
91 EX_ILLEGAL_ARGUMENT, "LevelNotSupported");
92 return ndk::ScopedAStatus::ok();
93 }
94 case Volume::mute: {
95 RETURN_IF(mContext->setVolMute(volParam.get<Volume::mute>()) != RetCode::SUCCESS,
96 EX_ILLEGAL_ARGUMENT, "MuteNotSupported");
97 return ndk::ScopedAStatus::ok();
98 }
99 default: {
100 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
101 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
102 "VolumeTagNotSupported");
103 }
104 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000105}
106
107ndk::ScopedAStatus VolumeSw::getParameterSpecific(const Parameter::Id& id,
108 Parameter::Specific* specific) {
109 auto tag = id.getTag();
110 RETURN_IF(Parameter::Id::volumeTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530111 auto volId = id.get<Parameter::Id::volumeTag>();
112 auto volIdTag = volId.getTag();
113 switch (volIdTag) {
114 case Volume::Id::commonTag:
115 return getParameterVolume(volId.get<Volume::Id::commonTag>(), specific);
116 default:
117 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
118 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
119 "VolumeTagNotSupported");
120 }
121}
122
123ndk::ScopedAStatus VolumeSw::getParameterVolume(const Volume::Tag& tag,
124 Parameter::Specific* specific) {
125 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
126
127 Volume volParam;
128 switch (tag) {
129 case Volume::levelDb: {
130 volParam.set<Volume::levelDb>(mContext->getVolLevel());
131 break;
132 }
133 case Volume::mute: {
134 volParam.set<Volume::mute>(mContext->getVolMute());
135 break;
136 }
137 default: {
138 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
139 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
140 "VolumeTagNotSupported");
141 }
142 }
143
144 specific->set<Parameter::Specific::volume>(volParam);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000145 return ndk::ScopedAStatus::ok();
146}
147
148std::shared_ptr<EffectContext> VolumeSw::createContext(const Parameter::Common& common) {
149 if (mContext) {
150 LOG(DEBUG) << __func__ << " context already exist";
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530151 } else {
152 mContext = std::make_shared<VolumeSwContext>(1 /* statusFmqDepth */, common);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000153 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530154
155 return mContext;
156}
157
158std::shared_ptr<EffectContext> VolumeSw::getContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000159 return mContext;
160}
161
162RetCode VolumeSw::releaseContext() {
163 if (mContext) {
164 mContext.reset();
165 }
166 return RetCode::SUCCESS;
167}
168
169// Processing method running in EffectWorker thread.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530170IEffect::Status VolumeSw::effectProcessImpl(float* in, float* out, int samples) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000171 // TODO: get data buffer and process.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530172 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
173 for (int i = 0; i < samples; i++) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000174 *out++ = *in++;
175 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530176 return {STATUS_OK, samples, samples};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000177}
178
179} // namespace aidl::android::hardware::audio::effect