blob: 44cac446ba300c4216af43d2239b54d72fce6290 [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
Shunkai Yao6afc8552022-10-26 22:47:20 +000017#include <algorithm>
Mikhail Naganov872d4a62023-03-09 18:19:01 -080018#include <cstddef>
Shunkai Yao6afc8552022-10-26 22:47:20 +000019
Mikhail Naganov872d4a62023-03-09 18:19:01 -080020#define LOG_TAG "AHAL_VolumeSw"
Shunkai Yao6afc8552022-10-26 22:47:20 +000021#include <android-base/logging.h>
22#include <fmq/AidlMessageQueue.h>
23
24#include "VolumeSw.h"
25
Shunkai Yaoc12e0822022-12-12 07:13:58 +000026using aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yao6afc8552022-10-26 22:47:20 +000027using aidl::android::hardware::audio::effect::IEffect;
Shunkai Yao812d5b42022-11-16 18:08:50 +000028using aidl::android::hardware::audio::effect::kVolumeSwImplUUID;
Shunkai Yao6afc8552022-10-26 22:47:20 +000029using aidl::android::hardware::audio::effect::State;
30using aidl::android::hardware::audio::effect::VolumeSw;
Shunkai Yao6afc8552022-10-26 22:47:20 +000031using aidl::android::media::audio::common::AudioUuid;
32
33extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
34 std::shared_ptr<IEffect>* instanceSpp) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000035 if (!in_impl_uuid || *in_impl_uuid != kVolumeSwImplUUID) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000036 LOG(ERROR) << __func__ << "uuid not supported";
37 return EX_ILLEGAL_ARGUMENT;
38 }
39 if (instanceSpp) {
40 *instanceSpp = ndk::SharedRefBase::make<VolumeSw>();
41 LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
42 return EX_NONE;
43 } else {
44 LOG(ERROR) << __func__ << " invalid input parameter!";
45 return EX_ILLEGAL_ARGUMENT;
46 }
47}
48
Shunkai Yaoc12e0822022-12-12 07:13:58 +000049extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
50 if (!in_impl_uuid || *in_impl_uuid != kVolumeSwImplUUID) {
51 LOG(ERROR) << __func__ << "uuid not supported";
52 return EX_ILLEGAL_ARGUMENT;
Shunkai Yao6afc8552022-10-26 22:47:20 +000053 }
Shunkai Yaoc12e0822022-12-12 07:13:58 +000054 *_aidl_return = VolumeSw::kDescriptor;
Shunkai Yao6afc8552022-10-26 22:47:20 +000055 return EX_NONE;
56}
57
58namespace aidl::android::hardware::audio::effect {
59
Shunkai Yaoc12e0822022-12-12 07:13:58 +000060const std::string VolumeSw::kEffectName = "VolumeSw";
Shunkai Yao87811022023-02-13 17:40:37 +000061
62const std::vector<Range::VolumeRange> VolumeSw::kRanges = {MAKE_RANGE(Volume, levelDb, -9600, 0)};
63
64const Capability VolumeSw::kCapability = {.range = Range::make<Range::volume>(VolumeSw::kRanges)};
65
Shunkai Yaoc12e0822022-12-12 07:13:58 +000066const Descriptor VolumeSw::kDescriptor = {
67 .common = {.id = {.type = kVolumeTypeUUID,
68 .uuid = kVolumeSwImplUUID,
69 .proxy = std::nullopt},
70 .flags = {.type = Flags::Type::INSERT,
71 .insert = Flags::Insert::FIRST,
72 .volume = Flags::Volume::CTRL},
73 .name = VolumeSw::kEffectName,
74 .implementor = "The Android Open Source Project"},
Shunkai Yao87811022023-02-13 17:40:37 +000075 .capability = VolumeSw::kCapability};
Shunkai Yaoc12e0822022-12-12 07:13:58 +000076
Shunkai Yao6afc8552022-10-26 22:47:20 +000077ndk::ScopedAStatus VolumeSw::getDescriptor(Descriptor* _aidl_return) {
78 LOG(DEBUG) << __func__ << kDescriptor.toString();
79 *_aidl_return = kDescriptor;
80 return ndk::ScopedAStatus::ok();
81}
82
83ndk::ScopedAStatus VolumeSw::setParameterSpecific(const Parameter::Specific& specific) {
84 RETURN_IF(Parameter::Specific::volume != specific.getTag(), EX_ILLEGAL_ARGUMENT,
85 "EffectNotSupported");
Shunkai Yao6afc8552022-10-26 22:47:20 +000086
Sham Rathod1b6c1f02022-11-22 17:39:22 +053087 auto& volParam = specific.get<Parameter::Specific::volume>();
Shunkai Yao87811022023-02-13 17:40:37 +000088 RETURN_IF(!inRange(volParam, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange");
Sham Rathod1b6c1f02022-11-22 17:39:22 +053089 auto tag = volParam.getTag();
90
91 switch (tag) {
92 case Volume::levelDb: {
93 RETURN_IF(mContext->setVolLevel(volParam.get<Volume::levelDb>()) != RetCode::SUCCESS,
94 EX_ILLEGAL_ARGUMENT, "LevelNotSupported");
95 return ndk::ScopedAStatus::ok();
96 }
97 case Volume::mute: {
98 RETURN_IF(mContext->setVolMute(volParam.get<Volume::mute>()) != RetCode::SUCCESS,
99 EX_ILLEGAL_ARGUMENT, "MuteNotSupported");
100 return ndk::ScopedAStatus::ok();
101 }
102 default: {
103 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
104 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
105 "VolumeTagNotSupported");
106 }
107 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000108}
109
110ndk::ScopedAStatus VolumeSw::getParameterSpecific(const Parameter::Id& id,
111 Parameter::Specific* specific) {
112 auto tag = id.getTag();
113 RETURN_IF(Parameter::Id::volumeTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530114 auto volId = id.get<Parameter::Id::volumeTag>();
115 auto volIdTag = volId.getTag();
116 switch (volIdTag) {
117 case Volume::Id::commonTag:
118 return getParameterVolume(volId.get<Volume::Id::commonTag>(), specific);
119 default:
120 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
121 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
122 "VolumeTagNotSupported");
123 }
124}
125
126ndk::ScopedAStatus VolumeSw::getParameterVolume(const Volume::Tag& tag,
127 Parameter::Specific* specific) {
128 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
129
130 Volume volParam;
131 switch (tag) {
132 case Volume::levelDb: {
133 volParam.set<Volume::levelDb>(mContext->getVolLevel());
134 break;
135 }
136 case Volume::mute: {
137 volParam.set<Volume::mute>(mContext->getVolMute());
138 break;
139 }
140 default: {
141 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
142 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
143 "VolumeTagNotSupported");
144 }
145 }
146
147 specific->set<Parameter::Specific::volume>(volParam);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000148 return ndk::ScopedAStatus::ok();
149}
150
151std::shared_ptr<EffectContext> VolumeSw::createContext(const Parameter::Common& common) {
152 if (mContext) {
153 LOG(DEBUG) << __func__ << " context already exist";
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530154 } else {
155 mContext = std::make_shared<VolumeSwContext>(1 /* statusFmqDepth */, common);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000156 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530157
158 return mContext;
159}
160
161std::shared_ptr<EffectContext> VolumeSw::getContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000162 return mContext;
163}
164
165RetCode VolumeSw::releaseContext() {
166 if (mContext) {
167 mContext.reset();
168 }
169 return RetCode::SUCCESS;
170}
171
172// Processing method running in EffectWorker thread.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530173IEffect::Status VolumeSw::effectProcessImpl(float* in, float* out, int samples) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000174 // TODO: get data buffer and process.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530175 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
176 for (int i = 0; i < samples; i++) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000177 *out++ = *in++;
178 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530179 return {STATUS_OK, samples, samples};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000180}
181
Sham Rathod85793d82022-12-22 19:09:10 +0530182RetCode VolumeSwContext::setVolLevel(int level) {
Sham Rathod85793d82022-12-22 19:09:10 +0530183 mLevel = level;
184 return RetCode::SUCCESS;
185}
186
187RetCode VolumeSwContext::setVolMute(bool mute) {
188 // TODO : Add implementation to modify mute
189 mMute = mute;
190 return RetCode::SUCCESS;
191}
192
Shunkai Yao6afc8552022-10-26 22:47:20 +0000193} // namespace aidl::android::hardware::audio::effect