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