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 #define LOG_TAG "ConversionHelperAidl"
18 
19 #include <memory>
20 
21 #include <media/AidlConversionUtil.h>
22 #include <utils/Log.h>
23 
24 #include "ConversionHelperAidl.h"
25 
26 using aidl::android::aidl_utils::statusTFromBinderStatus;
27 using aidl::android::hardware::audio::core::VendorParameter;
28 using aidl::android::media::audio::IHalAdapterVendorExtension;
29 
30 namespace android {
31 
parseAndGetVendorParameters(std::shared_ptr<IHalAdapterVendorExtension> vendorExt,const VendorParametersRecipient & recipient,const AudioParameter & parameterKeys,String8 * values)32 status_t parseAndGetVendorParameters(
33         std::shared_ptr<IHalAdapterVendorExtension> vendorExt,
34         const VendorParametersRecipient& recipient,
35         const AudioParameter& parameterKeys,
36         String8* values) {
37     using ParameterScope = IHalAdapterVendorExtension::ParameterScope;
38     if (parameterKeys.size() == 0) return OK;
39     const String8 rawKeys = parameterKeys.keysToString();
40 
41     std::vector<std::string> parameterIds;
42     RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(vendorExt->parseVendorParameterIds(
43                             ParameterScope(recipient.index()),
44                             std::string(rawKeys.c_str()), &parameterIds)));
45     if (parameterIds.empty()) return OK;
46 
47     std::vector<VendorParameter> parameters;
48     if (recipient.index() == static_cast<int>(ParameterScope::MODULE)) {
49         auto module = std::get<static_cast<int>(ParameterScope::MODULE)>(recipient);
50         RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(module->getVendorParameters(
51                                 parameterIds, &parameters)));
52     } else if (recipient.index() == static_cast<int>(ParameterScope::STREAM)) {
53         auto stream = std::get<static_cast<int>(ParameterScope::STREAM)>(recipient);
54         RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(stream->getVendorParameters(
55                                 parameterIds, &parameters)));
56     } else {
57         LOG_ALWAYS_FATAL("%s: unexpected recipient variant index: %zu",
58                 __func__, recipient.index());
59     }
60     if (!parameters.empty()) {
61         std::string vendorParameters;
62         RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(vendorExt->processVendorParameters(
63                                 ParameterScope(recipient.index()),
64                                 parameters, &vendorParameters)));
65         // Re-parse the vendor-provided string to ensure that it is correct.
66         AudioParameter reparse(String8(vendorParameters.c_str()));
67         if (reparse.size() != 0) {
68             if (values->length() > 0) {
69                 values->append(";");
70             }
71             values->append(reparse.toString().c_str());
72         }
73     }
74     return OK;
75 }
76 
parseAndSetVendorParameters(std::shared_ptr<IHalAdapterVendorExtension> vendorExt,const VendorParametersRecipient & recipient,const AudioParameter & parameters)77 status_t parseAndSetVendorParameters(
78         std::shared_ptr<IHalAdapterVendorExtension> vendorExt,
79         const VendorParametersRecipient& recipient,
80         const AudioParameter& parameters) {
81     using ParameterScope = IHalAdapterVendorExtension::ParameterScope;
82     if (parameters.size() == 0) return OK;
83     const String8 rawKeysAndValues = parameters.toString();
84 
85     std::vector<VendorParameter> syncParameters, asyncParameters;
86     RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(vendorExt->parseVendorParameters(
87                             ParameterScope(recipient.index()),
88                             std::string(rawKeysAndValues.c_str()),
89                             &syncParameters, &asyncParameters)));
90     if (recipient.index() == static_cast<int>(ParameterScope::MODULE)) {
91         auto module = std::get<static_cast<int>(ParameterScope::MODULE)>(recipient);
92         if (!syncParameters.empty()) {
93             RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(module->setVendorParameters(
94                                     syncParameters, false /*async*/)));
95         }
96         if (!asyncParameters.empty()) {
97             RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(module->setVendorParameters(
98                                     asyncParameters, true /*async*/)));
99         }
100     } else if (recipient.index() == static_cast<int>(ParameterScope::STREAM)) {
101         auto stream = std::get<static_cast<int>(ParameterScope::STREAM)>(recipient);
102         if (!syncParameters.empty()) {
103             RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(stream->setVendorParameters(
104                                     syncParameters, false /*async*/)));
105         }
106         if (!asyncParameters.empty()) {
107             RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(stream->setVendorParameters(
108                                     asyncParameters, true /*async*/)));
109         }
110     } else {
111         LOG_ALWAYS_FATAL("%s: unexpected recipient variant index: %zu",
112                 __func__, recipient.index());
113     }
114     return OK;
115 }
116 
117 } // namespace android
118