Shraddha Basantwani | aa09525 | 2022-12-13 16:35:23 +0530 | [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 | #define LOG_TAG "AHAL_DownmixImpl" |
| 18 | |
| 19 | #include <android-base/logging.h> |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame^] | 20 | #include <system/audio_effects/effect_uuid.h> |
Shraddha Basantwani | aa09525 | 2022-12-13 16:35:23 +0530 | [diff] [blame] | 21 | |
| 22 | #include "EffectDownmix.h" |
| 23 | |
| 24 | using aidl::android::hardware::audio::effect::Descriptor; |
| 25 | using aidl::android::hardware::audio::effect::DownmixImpl; |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame^] | 26 | using aidl::android::hardware::audio::effect::getEffectImplUuidDownmix; |
| 27 | using aidl::android::hardware::audio::effect::getEffectTypeUuidDownmix; |
Shraddha Basantwani | aa09525 | 2022-12-13 16:35:23 +0530 | [diff] [blame] | 28 | using aidl::android::hardware::audio::effect::IEffect; |
Shraddha Basantwani | aa09525 | 2022-12-13 16:35:23 +0530 | [diff] [blame] | 29 | using aidl::android::media::audio::common::AudioUuid; |
| 30 | |
| 31 | extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid, |
| 32 | std::shared_ptr<IEffect>* instanceSpp) { |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame^] | 33 | if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidDownmix()) { |
Shraddha Basantwani | aa09525 | 2022-12-13 16:35:23 +0530 | [diff] [blame] | 34 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 35 | return EX_ILLEGAL_ARGUMENT; |
| 36 | } |
| 37 | if (instanceSpp) { |
| 38 | *instanceSpp = ndk::SharedRefBase::make<DownmixImpl>(); |
| 39 | LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created"; |
| 40 | return EX_NONE; |
| 41 | } else { |
| 42 | LOG(ERROR) << __func__ << " invalid input parameter!"; |
| 43 | return EX_ILLEGAL_ARGUMENT; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) { |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame^] | 48 | if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidDownmix()) { |
Shraddha Basantwani | aa09525 | 2022-12-13 16:35:23 +0530 | [diff] [blame] | 49 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 50 | return EX_ILLEGAL_ARGUMENT; |
| 51 | } |
| 52 | *_aidl_return = DownmixImpl::kDescriptor; |
| 53 | return EX_NONE; |
| 54 | } |
| 55 | |
| 56 | namespace aidl::android::hardware::audio::effect { |
| 57 | |
| 58 | const std::string DownmixImpl::kEffectName = "Multichannel Downmix To Stereo"; |
| 59 | const Descriptor DownmixImpl::kDescriptor = { |
Shunkai Yao | 399be68 | 2023-03-06 18:54:18 +0000 | [diff] [blame^] | 60 | .common = {.id = {.type = getEffectTypeUuidDownmix(), |
| 61 | .uuid = getEffectImplUuidDownmix(), |
| 62 | .proxy = std::nullopt}, |
| 63 | .flags = {.type = Flags::Type::INSERT, .insert = Flags::Insert::FIRST}, |
| 64 | .name = DownmixImpl::kEffectName, |
| 65 | .implementor = "The Android Open Source Project"}}; |
Shraddha Basantwani | aa09525 | 2022-12-13 16:35:23 +0530 | [diff] [blame] | 66 | |
| 67 | ndk::ScopedAStatus DownmixImpl::getDescriptor(Descriptor* _aidl_return) { |
| 68 | RETURN_IF(!_aidl_return, EX_ILLEGAL_ARGUMENT, "Parameter:nullptr"); |
| 69 | LOG(DEBUG) << __func__ << kDescriptor.toString(); |
| 70 | *_aidl_return = kDescriptor; |
| 71 | return ndk::ScopedAStatus::ok(); |
| 72 | } |
| 73 | |
| 74 | ndk::ScopedAStatus DownmixImpl::setParameterCommon(const Parameter& param) { |
| 75 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 76 | |
| 77 | auto tag = param.getTag(); |
| 78 | switch (tag) { |
| 79 | case Parameter::common: |
| 80 | RETURN_IF(mContext->setCommon(param.get<Parameter::common>()) != RetCode::SUCCESS, |
| 81 | EX_ILLEGAL_ARGUMENT, "setCommFailed"); |
| 82 | break; |
| 83 | case Parameter::deviceDescription: |
| 84 | RETURN_IF(mContext->setOutputDevice(param.get<Parameter::deviceDescription>()) != |
| 85 | RetCode::SUCCESS, |
| 86 | EX_ILLEGAL_ARGUMENT, "setDeviceFailed"); |
| 87 | break; |
| 88 | case Parameter::mode: |
| 89 | RETURN_IF(mContext->setAudioMode(param.get<Parameter::mode>()) != RetCode::SUCCESS, |
| 90 | EX_ILLEGAL_ARGUMENT, "setModeFailed"); |
| 91 | break; |
| 92 | case Parameter::source: |
| 93 | RETURN_IF(mContext->setAudioSource(param.get<Parameter::source>()) != RetCode::SUCCESS, |
| 94 | EX_ILLEGAL_ARGUMENT, "setSourceFailed"); |
| 95 | break; |
| 96 | case Parameter::volumeStereo: |
| 97 | RETURN_IF(mContext->setVolumeStereo(param.get<Parameter::volumeStereo>()) != |
| 98 | RetCode::SUCCESS, |
| 99 | EX_ILLEGAL_ARGUMENT, "setVolumeStereoFailed"); |
| 100 | break; |
| 101 | default: { |
| 102 | LOG(ERROR) << __func__ << " unsupportedParameterTag " << toString(tag); |
| 103 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 104 | "commonParamNotSupported"); |
| 105 | } |
| 106 | } |
| 107 | return ndk::ScopedAStatus::ok(); |
| 108 | } |
| 109 | |
| 110 | ndk::ScopedAStatus DownmixImpl::commandImpl(CommandId command) { |
| 111 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 112 | switch (command) { |
| 113 | case CommandId::START: |
| 114 | mContext->enable(); |
| 115 | break; |
| 116 | case CommandId::STOP: |
| 117 | mContext->disable(); |
| 118 | break; |
| 119 | case CommandId::RESET: |
| 120 | mContext->reset(); |
| 121 | break; |
| 122 | default: |
| 123 | LOG(ERROR) << __func__ << " commandId " << toString(command) << " not supported"; |
| 124 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 125 | "commandIdNotSupported"); |
| 126 | } |
| 127 | return ndk::ScopedAStatus::ok(); |
| 128 | } |
| 129 | |
| 130 | ndk::ScopedAStatus DownmixImpl::setParameterSpecific(const Parameter::Specific& specific) { |
| 131 | RETURN_IF(Parameter::Specific::downmix != specific.getTag(), EX_ILLEGAL_ARGUMENT, |
| 132 | "EffectNotSupported"); |
| 133 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 134 | |
| 135 | auto& dmParam = specific.get<Parameter::Specific::downmix>(); |
| 136 | auto tag = dmParam.getTag(); |
| 137 | |
| 138 | switch (tag) { |
| 139 | case Downmix::type: { |
| 140 | RETURN_IF(mContext->setDmType(dmParam.get<Downmix::type>()) != RetCode::SUCCESS, |
| 141 | EX_ILLEGAL_ARGUMENT, "setTypeFailed"); |
| 142 | return ndk::ScopedAStatus::ok(); |
| 143 | } |
| 144 | default: { |
| 145 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 146 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 147 | "DownmixTagNotSupported"); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | ndk::ScopedAStatus DownmixImpl::getParameterSpecific(const Parameter::Id& id, |
| 153 | Parameter::Specific* specific) { |
| 154 | RETURN_IF(!specific, EX_NULL_POINTER, "nullPtr"); |
| 155 | auto tag = id.getTag(); |
| 156 | RETURN_IF(Parameter::Id::downmixTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag"); |
| 157 | auto dmId = id.get<Parameter::Id::downmixTag>(); |
| 158 | auto dmIdTag = dmId.getTag(); |
| 159 | switch (dmIdTag) { |
| 160 | case Downmix::Id::commonTag: |
| 161 | return getParameterDownmix(dmId.get<Downmix::Id::commonTag>(), specific); |
| 162 | default: |
| 163 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(dmIdTag); |
| 164 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 165 | "DownmixTagNotSupported"); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | ndk::ScopedAStatus DownmixImpl::getParameterDownmix(const Downmix::Tag& tag, |
| 170 | Parameter::Specific* specific) { |
| 171 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 172 | |
| 173 | Downmix dmParam; |
| 174 | switch (tag) { |
| 175 | case Downmix::type: { |
| 176 | dmParam.set<Downmix::type>(mContext->getDmType()); |
| 177 | break; |
| 178 | } |
| 179 | default: { |
| 180 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 181 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 182 | "DownmixTagNotSupported"); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | specific->set<Parameter::Specific::downmix>(dmParam); |
| 187 | return ndk::ScopedAStatus::ok(); |
| 188 | } |
| 189 | |
| 190 | std::shared_ptr<EffectContext> DownmixImpl::createContext(const Parameter::Common& common) { |
| 191 | if (mContext) { |
| 192 | LOG(DEBUG) << __func__ << " context already exist"; |
| 193 | return mContext; |
| 194 | } |
| 195 | |
| 196 | mContext = std::make_shared<DownmixContext>(1 /* statusFmqDepth */, common); |
| 197 | return mContext; |
| 198 | } |
| 199 | |
| 200 | RetCode DownmixImpl::releaseContext() { |
| 201 | if (mContext) { |
| 202 | mContext.reset(); |
| 203 | } |
| 204 | return RetCode::SUCCESS; |
| 205 | } |
| 206 | |
| 207 | // Processing method running in EffectWorker thread. |
| 208 | IEffect::Status DownmixImpl::effectProcessImpl(float* in, float* out, int sampleToProcess) { |
| 209 | if (!mContext) { |
| 210 | LOG(ERROR) << __func__ << " nullContext"; |
| 211 | return {EX_NULL_POINTER, 0, 0}; |
| 212 | } |
| 213 | return mContext->lvmProcess(in, out, sampleToProcess); |
| 214 | } |
| 215 | |
| 216 | } // namespace aidl::android::hardware::audio::effect |