xref: /aosp_15_r20/frameworks/av/media/libaudiohal/impl/ConversionHelperAidl.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
2  * Copyright (C) 2023 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 
19 #include <string>
20 #include <string_view>
21 #include <variant>
22 #include <vector>
23 
24 #include <aidl/android/hardware/audio/core/IModule.h>
25 #include <aidl/android/hardware/audio/core/IStreamCommon.h>
26 #include <aidl/android/media/audio/IHalAdapterVendorExtension.h>
27 #include <android-base/expected.h>
28 #include <error/Result.h>
29 #include <media/AudioParameter.h>
30 #include <utils/String16.h>
31 #include <utils/Vector.h>
32 
33 namespace android {
34 
35 /*
36  * Helper macro to add instance name, function name in logs
37  * classes should provide getInstanceName API to use these macros.
38  * print function names along with instance name.
39  *
40  * Usage:
41  *  AUGMENT_LOG(D);
42  *  AUGMENT_LOG(I, "hello!");
43  *  AUGMENT_LOG(W, "value: %d", value);
44  *
45  *  AUGMENT_LOG_IF(D, value < 0, "negative");
46  *  AUGMENT_LOG_IF(E, value < 0, "bad value: %d", value);
47  */
48 
49 #define AUGMENT_LOG(level, ...)                                                  \
50     ALOG##level("[%s] %s" __VA_OPT__(": " __android_second(0, __VA_ARGS__, "")), \
51                 getInstanceName().c_str(), __func__ __VA_OPT__(__android_rest(__VA_ARGS__)))
52 
53 #define AUGMENT_LOG_IF(level, cond, ...)                                                    \
54     ALOG##level##_IF(cond, "[%s] %s" __VA_OPT__(": " __android_second(0, __VA_ARGS__, "")), \
55                      getInstanceName().c_str(), __func__ __VA_OPT__(__android_rest(__VA_ARGS__)))
56 
57 class Args {
58   public:
Args(const Vector<String16> & args)59     explicit Args(const Vector<String16>& args)
60             : mValues(args.size()), mPtrs(args.size()) {
61         for (size_t i = 0; i < args.size(); ++i) {
62             mValues[i] = std::string(String8(args[i]));
63             mPtrs[i] = mValues[i].c_str();
64         }
65     }
args()66     const char** args() { return mPtrs.data(); }
67   private:
68     std::vector<std::string> mValues;
69     std::vector<const char*> mPtrs;
70 };
71 
72 class ConversionHelperAidl {
73   protected:
ConversionHelperAidl(std::string_view className,std::string_view instanceName)74     ConversionHelperAidl(std::string_view className, std::string_view instanceName)
75         : mClassName(className), mInstanceName(instanceName) {}
76 
getClassName()77     const std::string& getClassName() const { return mClassName; }
78 
getInstanceName()79     const std::string& getInstanceName() const { return mInstanceName; }
80 
81     const std::string mClassName;
82     const std::string mInstanceName;
83 };
84 
85 // 'action' must accept a value of type 'T' and return 'status_t'.
86 // The function returns 'true' if the parameter was found, and the action has succeeded.
87 // The function returns 'false' if the parameter was not found.
88 // Any errors get propagated, if there are errors it means the parameter was found.
89 template<typename T, typename F>
filterOutAndProcessParameter(AudioParameter & parameters,const String8 & key,const F & action)90 error::Result<bool> filterOutAndProcessParameter(
91         AudioParameter& parameters, const String8& key, const F& action) {
92     if (parameters.containsKey(key)) {
93         T value;
94         status_t status = parameters.get(key, value);
95         if (status == OK) {
96             parameters.remove(key);
97             status = action(value);
98             if (status == OK) return true;
99         }
100         return base::unexpected(status);
101     }
102     return false;
103 }
104 
105 // Must use the same order of elements as IHalAdapterVendorExtension::ParameterScope.
106 using VendorParametersRecipient = std::variant<
107         std::shared_ptr<::aidl::android::hardware::audio::core::IModule>,
108         std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon>>;
109 status_t parseAndGetVendorParameters(
110         std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension> vendorExt,
111         const VendorParametersRecipient& recipient,
112         const AudioParameter& parameterKeys,
113         String8* values);
114 status_t parseAndSetVendorParameters(
115         std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension> vendorExt,
116         const VendorParametersRecipient& recipient,
117         const AudioParameter& parameters);
118 
119 }  // namespace android
120