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 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 17 | #include <algorithm> |
Mikhail Naganov | 872d4a6 | 2023-03-09 18:19:01 -0800 | [diff] [blame] | 18 | #include <cstddef> |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 19 | |
Mikhail Naganov | 872d4a6 | 2023-03-09 18:19:01 -0800 | [diff] [blame] | 20 | #define LOG_TAG "AHAL_VolumeSw" |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 21 | #include <android-base/logging.h> |
| 22 | #include <fmq/AidlMessageQueue.h> |
| 23 | |
| 24 | #include "VolumeSw.h" |
| 25 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 26 | using aidl::android::hardware::audio::effect::Descriptor; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 27 | using aidl::android::hardware::audio::effect::IEffect; |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 28 | using aidl::android::hardware::audio::effect::kVolumeSwImplUUID; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 29 | using aidl::android::hardware::audio::effect::State; |
| 30 | using aidl::android::hardware::audio::effect::VolumeSw; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 31 | using aidl::android::media::audio::common::AudioUuid; |
| 32 | |
| 33 | extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid, |
| 34 | std::shared_ptr<IEffect>* instanceSpp) { |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 35 | if (!in_impl_uuid || *in_impl_uuid != kVolumeSwImplUUID) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 36 | 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 Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 49 | extern "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 Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 53 | } |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 54 | *_aidl_return = VolumeSw::kDescriptor; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 55 | return EX_NONE; |
| 56 | } |
| 57 | |
| 58 | namespace aidl::android::hardware::audio::effect { |
| 59 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 60 | const std::string VolumeSw::kEffectName = "VolumeSw"; |
Shunkai Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame] | 61 | |
| 62 | const std::vector<Range::VolumeRange> VolumeSw::kRanges = {MAKE_RANGE(Volume, levelDb, -9600, 0)}; |
| 63 | |
| 64 | const Capability VolumeSw::kCapability = {.range = Range::make<Range::volume>(VolumeSw::kRanges)}; |
| 65 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 66 | const 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 Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame] | 75 | .capability = VolumeSw::kCapability}; |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 76 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 77 | ndk::ScopedAStatus VolumeSw::getDescriptor(Descriptor* _aidl_return) { |
| 78 | LOG(DEBUG) << __func__ << kDescriptor.toString(); |
| 79 | *_aidl_return = kDescriptor; |
| 80 | return ndk::ScopedAStatus::ok(); |
| 81 | } |
| 82 | |
| 83 | ndk::ScopedAStatus VolumeSw::setParameterSpecific(const Parameter::Specific& specific) { |
| 84 | RETURN_IF(Parameter::Specific::volume != specific.getTag(), EX_ILLEGAL_ARGUMENT, |
| 85 | "EffectNotSupported"); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 86 | |
Sham Rathod | 1b6c1f0 | 2022-11-22 17:39:22 +0530 | [diff] [blame] | 87 | auto& volParam = specific.get<Parameter::Specific::volume>(); |
Shunkai Yao | 8781102 | 2023-02-13 17:40:37 +0000 | [diff] [blame] | 88 | RETURN_IF(!inRange(volParam, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange"); |
Sham Rathod | 1b6c1f0 | 2022-11-22 17:39:22 +0530 | [diff] [blame] | 89 | 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 Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | ndk::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 Rathod | 1b6c1f0 | 2022-11-22 17:39:22 +0530 | [diff] [blame] | 114 | 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 | |
| 126 | ndk::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 Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 148 | return ndk::ScopedAStatus::ok(); |
| 149 | } |
| 150 | |
| 151 | std::shared_ptr<EffectContext> VolumeSw::createContext(const Parameter::Common& common) { |
| 152 | if (mContext) { |
| 153 | LOG(DEBUG) << __func__ << " context already exist"; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 154 | } else { |
| 155 | mContext = std::make_shared<VolumeSwContext>(1 /* statusFmqDepth */, common); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 156 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 157 | |
| 158 | return mContext; |
| 159 | } |
| 160 | |
| 161 | std::shared_ptr<EffectContext> VolumeSw::getContext() { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 162 | return mContext; |
| 163 | } |
| 164 | |
| 165 | RetCode VolumeSw::releaseContext() { |
| 166 | if (mContext) { |
| 167 | mContext.reset(); |
| 168 | } |
| 169 | return RetCode::SUCCESS; |
| 170 | } |
| 171 | |
| 172 | // Processing method running in EffectWorker thread. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 173 | IEffect::Status VolumeSw::effectProcessImpl(float* in, float* out, int samples) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 174 | // TODO: get data buffer and process. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 175 | LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples; |
| 176 | for (int i = 0; i < samples; i++) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 177 | *out++ = *in++; |
| 178 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 179 | return {STATUS_OK, samples, samples}; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Sham Rathod | 85793d8 | 2022-12-22 19:09:10 +0530 | [diff] [blame] | 182 | RetCode VolumeSwContext::setVolLevel(int level) { |
Sham Rathod | 85793d8 | 2022-12-22 19:09:10 +0530 | [diff] [blame] | 183 | mLevel = level; |
| 184 | return RetCode::SUCCESS; |
| 185 | } |
| 186 | |
| 187 | RetCode VolumeSwContext::setVolMute(bool mute) { |
| 188 | // TODO : Add implementation to modify mute |
| 189 | mMute = mute; |
| 190 | return RetCode::SUCCESS; |
| 191 | } |
| 192 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 193 | } // namespace aidl::android::hardware::audio::effect |