Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 1 | /* |
| 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 Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 28 | using aidl::android::hardware::audio::effect::Descriptor; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 29 | using aidl::android::hardware::audio::effect::IEffect; |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 30 | using aidl::android::hardware::audio::effect::kVolumeSwImplUUID; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 31 | using aidl::android::hardware::audio::effect::State; |
| 32 | using aidl::android::hardware::audio::effect::VolumeSw; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 33 | using aidl::android::media::audio::common::AudioUuid; |
| 34 | |
| 35 | extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid, |
| 36 | std::shared_ptr<IEffect>* instanceSpp) { |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 37 | if (!in_impl_uuid || *in_impl_uuid != kVolumeSwImplUUID) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 38 | 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 Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 51 | extern "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 Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 55 | } |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 56 | *_aidl_return = VolumeSw::kDescriptor; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 57 | return EX_NONE; |
| 58 | } |
| 59 | |
| 60 | namespace aidl::android::hardware::audio::effect { |
| 61 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 62 | const std::string VolumeSw::kEffectName = "VolumeSw"; |
Shunkai Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame] | 63 | |
| 64 | const std::vector<Range::VolumeRange> VolumeSw::kRanges = {MAKE_RANGE(Volume, levelDb, -9600, 0)}; |
| 65 | |
| 66 | const Capability VolumeSw::kCapability = {.range = Range::make<Range::volume>(VolumeSw::kRanges)}; |
| 67 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 68 | const Descriptor VolumeSw::kDescriptor = { |
| 69 | .common = {.id = {.type = kVolumeTypeUUID, |
| 70 | .uuid = kVolumeSwImplUUID, |
| 71 | .proxy = std::nullopt}, |
| 72 | .flags = {.type = Flags::Type::INSERT, |
| 73 | .insert = Flags::Insert::FIRST, |
| 74 | .volume = Flags::Volume::CTRL}, |
| 75 | .name = VolumeSw::kEffectName, |
| 76 | .implementor = "The Android Open Source Project"}, |
Shunkai Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame] | 77 | .capability = VolumeSw::kCapability}; |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 78 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 79 | ndk::ScopedAStatus VolumeSw::getDescriptor(Descriptor* _aidl_return) { |
| 80 | LOG(DEBUG) << __func__ << kDescriptor.toString(); |
| 81 | *_aidl_return = kDescriptor; |
| 82 | return ndk::ScopedAStatus::ok(); |
| 83 | } |
| 84 | |
| 85 | ndk::ScopedAStatus VolumeSw::setParameterSpecific(const Parameter::Specific& specific) { |
| 86 | RETURN_IF(Parameter::Specific::volume != specific.getTag(), EX_ILLEGAL_ARGUMENT, |
| 87 | "EffectNotSupported"); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 88 | |
Sham Rathod | 1b6c1f0 | 2022-11-22 17:39:22 +0530 | [diff] [blame] | 89 | auto& volParam = specific.get<Parameter::Specific::volume>(); |
Shunkai Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame] | 90 | RETURN_IF(!inRange(volParam, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange"); |
Sham Rathod | 1b6c1f0 | 2022-11-22 17:39:22 +0530 | [diff] [blame] | 91 | auto tag = volParam.getTag(); |
| 92 | |
| 93 | switch (tag) { |
| 94 | case Volume::levelDb: { |
| 95 | RETURN_IF(mContext->setVolLevel(volParam.get<Volume::levelDb>()) != RetCode::SUCCESS, |
| 96 | EX_ILLEGAL_ARGUMENT, "LevelNotSupported"); |
| 97 | return ndk::ScopedAStatus::ok(); |
| 98 | } |
| 99 | case Volume::mute: { |
| 100 | RETURN_IF(mContext->setVolMute(volParam.get<Volume::mute>()) != RetCode::SUCCESS, |
| 101 | EX_ILLEGAL_ARGUMENT, "MuteNotSupported"); |
| 102 | return ndk::ScopedAStatus::ok(); |
| 103 | } |
| 104 | default: { |
| 105 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 106 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 107 | "VolumeTagNotSupported"); |
| 108 | } |
| 109 | } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | ndk::ScopedAStatus VolumeSw::getParameterSpecific(const Parameter::Id& id, |
| 113 | Parameter::Specific* specific) { |
| 114 | auto tag = id.getTag(); |
| 115 | RETURN_IF(Parameter::Id::volumeTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag"); |
Sham Rathod | 1b6c1f0 | 2022-11-22 17:39:22 +0530 | [diff] [blame] | 116 | auto volId = id.get<Parameter::Id::volumeTag>(); |
| 117 | auto volIdTag = volId.getTag(); |
| 118 | switch (volIdTag) { |
| 119 | case Volume::Id::commonTag: |
| 120 | return getParameterVolume(volId.get<Volume::Id::commonTag>(), specific); |
| 121 | default: |
| 122 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 123 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 124 | "VolumeTagNotSupported"); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | ndk::ScopedAStatus VolumeSw::getParameterVolume(const Volume::Tag& tag, |
| 129 | Parameter::Specific* specific) { |
| 130 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 131 | |
| 132 | Volume volParam; |
| 133 | switch (tag) { |
| 134 | case Volume::levelDb: { |
| 135 | volParam.set<Volume::levelDb>(mContext->getVolLevel()); |
| 136 | break; |
| 137 | } |
| 138 | case Volume::mute: { |
| 139 | volParam.set<Volume::mute>(mContext->getVolMute()); |
| 140 | break; |
| 141 | } |
| 142 | default: { |
| 143 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 144 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 145 | "VolumeTagNotSupported"); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | specific->set<Parameter::Specific::volume>(volParam); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 150 | return ndk::ScopedAStatus::ok(); |
| 151 | } |
| 152 | |
| 153 | std::shared_ptr<EffectContext> VolumeSw::createContext(const Parameter::Common& common) { |
| 154 | if (mContext) { |
| 155 | LOG(DEBUG) << __func__ << " context already exist"; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 156 | } else { |
| 157 | mContext = std::make_shared<VolumeSwContext>(1 /* statusFmqDepth */, common); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 158 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 159 | |
| 160 | return mContext; |
| 161 | } |
| 162 | |
| 163 | std::shared_ptr<EffectContext> VolumeSw::getContext() { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 164 | return mContext; |
| 165 | } |
| 166 | |
| 167 | RetCode VolumeSw::releaseContext() { |
| 168 | if (mContext) { |
| 169 | mContext.reset(); |
| 170 | } |
| 171 | return RetCode::SUCCESS; |
| 172 | } |
| 173 | |
| 174 | // Processing method running in EffectWorker thread. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 175 | IEffect::Status VolumeSw::effectProcessImpl(float* in, float* out, int samples) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 176 | // TODO: get data buffer and process. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 177 | LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples; |
| 178 | for (int i = 0; i < samples; i++) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 179 | *out++ = *in++; |
| 180 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 181 | return {STATUS_OK, samples, samples}; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Sham Rathod | 85793d8 | 2022-12-22 19:09:10 +0530 | [diff] [blame] | 184 | RetCode VolumeSwContext::setVolLevel(int level) { |
Sham Rathod | 85793d8 | 2022-12-22 19:09:10 +0530 | [diff] [blame] | 185 | mLevel = level; |
| 186 | return RetCode::SUCCESS; |
| 187 | } |
| 188 | |
| 189 | RetCode VolumeSwContext::setVolMute(bool mute) { |
| 190 | // TODO : Add implementation to modify mute |
| 191 | mMute = mute; |
| 192 | return RetCode::SUCCESS; |
| 193 | } |
| 194 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 195 | } // namespace aidl::android::hardware::audio::effect |