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 #pragma once 18 #include <string> 19 20 #include <aidl/android/hardware/audio/effect/BnEffect.h> 21 #include <aidl/android/hardware/audio/effect/Range.h> 22 #include <android-base/logging.h> 23 #include <system/audio_effects/aidl_effects_utils.h> 24 25 typedef binder_exception_t (*EffectCreateFunctor)( 26 const ::aidl::android::media::audio::common::AudioUuid*, 27 std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>*); 28 typedef binder_exception_t (*EffectDestroyFunctor)( 29 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>&); 30 typedef binder_exception_t (*EffectQueryFunctor)( 31 const ::aidl::android::media::audio::common::AudioUuid*, 32 ::aidl::android::hardware::audio::effect::Descriptor*); 33 34 struct effect_dl_interface_s { 35 EffectCreateFunctor createEffectFunc; 36 EffectDestroyFunctor destroyEffectFunc; 37 EffectQueryFunctor queryEffectFunc; 38 }; 39 40 namespace aidl::android::hardware::audio::effect { 41 42 enum class RetCode { 43 SUCCESS, 44 ERROR_ILLEGAL_PARAMETER, /* Illegal parameter */ 45 ERROR_THREAD, /* Effect thread error */ 46 ERROR_NULL_POINTER, /* NULL pointer */ 47 ERROR_ALIGNMENT_ERROR, /* Memory alignment error */ 48 ERROR_BLOCK_SIZE_EXCEED, /* Maximum block size exceeded */ 49 ERROR_EFFECT_LIB_ERROR 50 }; 51 52 static const int INVALID_AUDIO_SESSION_ID = -1; 53 54 inline std::ostream& operator<<(std::ostream& out, const RetCode& code) { 55 switch (code) { 56 case RetCode::SUCCESS: 57 return out << "SUCCESS"; 58 case RetCode::ERROR_ILLEGAL_PARAMETER: 59 return out << "ERROR_ILLEGAL_PARAMETER"; 60 case RetCode::ERROR_THREAD: 61 return out << "ERROR_THREAD"; 62 case RetCode::ERROR_NULL_POINTER: 63 return out << "ERROR_NULL_POINTER"; 64 case RetCode::ERROR_ALIGNMENT_ERROR: 65 return out << "ERROR_ALIGNMENT_ERROR"; 66 case RetCode::ERROR_BLOCK_SIZE_EXCEED: 67 return out << "ERROR_BLOCK_SIZE_EXCEED"; 68 case RetCode::ERROR_EFFECT_LIB_ERROR: 69 return out << "ERROR_EFFECT_LIB_ERROR"; 70 } 71 72 return out << "EnumError: " << code; 73 } 74 75 #define RETURN_IF_ASTATUS_NOT_OK(status, message) \ 76 do { \ 77 const ::ndk::ScopedAStatus curr_status = (status); \ 78 if (!curr_status.isOk()) { \ 79 LOG(ERROR) << __func__ << ":" << __LINE__ \ 80 << "return with status: " << curr_status.getDescription() << (message); \ 81 return ndk::ScopedAStatus::fromExceptionCodeWithMessage( \ 82 curr_status.getExceptionCode(), (message)); \ 83 } \ 84 } while (0) 85 86 #define RETURN_IF(expr, exception, message) \ 87 do { \ 88 if (expr) { \ 89 LOG(ERROR) << __func__ << ":" << __LINE__ << " return with expr " << #expr; \ 90 return ndk::ScopedAStatus::fromExceptionCodeWithMessage((exception), (message)); \ 91 } \ 92 } while (0) 93 94 #define RETURN_OK_IF(expr) \ 95 do { \ 96 if (expr) { \ 97 LOG(INFO) << __func__ << ":" << __LINE__ << " return with expr " << #expr; \ 98 return ndk::ScopedAStatus::ok(); \ 99 } \ 100 } while (0) 101 102 #define RETURN_VALUE_IF(expr, ret, log) \ 103 do { \ 104 if (expr) { \ 105 LOG(ERROR) << __func__ << ":" << __LINE__ << " return with expr \"" << #expr \ 106 << "\":" << (log); \ 107 return ret; \ 108 } \ 109 } while (0) 110 111 #define RETURN_IF_BINDER_EXCEPTION(functor) \ 112 { \ 113 binder_exception_t exception = functor; \ 114 if (EX_NONE != exception) { \ 115 LOG(ERROR) << #functor << ": failed with error " << exception; \ 116 return ndk::ScopedAStatus::fromExceptionCode(exception); \ 117 } \ 118 } 119 120 /** 121 * Make a Range::$EffectType$Range. 122 * T: The $EffectType$, Visualizer for example. 123 * Tag: The union tag name in $EffectType$ definition, latencyMs for example. 124 * l: The value of Range::$EffectType$Range.min. 125 * r: The value of Range::$EffectType$Range.max. 126 */ 127 #define MAKE_RANGE(T, Tag, l, r) \ 128 { .min = T::make<T::Tag>(l), .max = T::make<T::Tag>(r) } 129 130 } // namespace aidl::android::hardware::audio::effect 131