• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (vendorExt == nullptr) {
41         ALOGW("%s: unknown parameters, ignored: \"%s\"", __func__, rawKeys.c_str());
42         return OK;
43     }
44 
45     std::vector<std::string> parameterIds;
46     RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(vendorExt->parseVendorParameterIds(
47                             ParameterScope(recipient.index()),
48                             std::string(rawKeys.c_str()), &parameterIds)));
49     if (parameterIds.empty()) return OK;
50 
51     std::vector<VendorParameter> parameters;
52     if (recipient.index() == static_cast<int>(ParameterScope::MODULE)) {
53         auto module = std::get<static_cast<int>(ParameterScope::MODULE)>(recipient);
54         RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(module->getVendorParameters(
55                                 parameterIds, &parameters)));
56     } else if (recipient.index() == static_cast<int>(ParameterScope::STREAM)) {
57         auto stream = std::get<static_cast<int>(ParameterScope::STREAM)>(recipient);
58         RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(stream->getVendorParameters(
59                                 parameterIds, &parameters)));
60     } else {
61         LOG_ALWAYS_FATAL("%s: unexpected recipient variant index: %zu",
62                 __func__, recipient.index());
63     }
64     if (!parameters.empty()) {
65         std::string vendorParameters;
66         RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(vendorExt->processVendorParameters(
67                                 ParameterScope(recipient.index()),
68                                 parameters, &vendorParameters)));
69         // Re-parse the vendor-provided string to ensure that it is correct.
70         AudioParameter reparse(String8(vendorParameters.c_str()));
71         if (reparse.size() != 0) {
72             if (!values->empty()) {
73                 values->append(";");
74             }
75             values->append(reparse.toString().c_str());
76         }
77     }
78     return OK;
79 }
80 
parseAndSetVendorParameters(std::shared_ptr<IHalAdapterVendorExtension> vendorExt,const VendorParametersRecipient & recipient,const AudioParameter & parameters)81 status_t parseAndSetVendorParameters(
82         std::shared_ptr<IHalAdapterVendorExtension> vendorExt,
83         const VendorParametersRecipient& recipient,
84         const AudioParameter& parameters) {
85     using ParameterScope = IHalAdapterVendorExtension::ParameterScope;
86     if (parameters.size() == 0) return OK;
87     const String8 rawKeysAndValues = parameters.toString();
88     if (vendorExt == nullptr) {
89         ALOGW("%s: unknown parameters, ignored: \"%s\"", __func__, rawKeysAndValues.c_str());
90         return OK;
91     }
92 
93     std::vector<VendorParameter> syncParameters, asyncParameters;
94     RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(vendorExt->parseVendorParameters(
95                             ParameterScope(recipient.index()),
96                             std::string(rawKeysAndValues.c_str()),
97                             &syncParameters, &asyncParameters)));
98     if (recipient.index() == static_cast<int>(ParameterScope::MODULE)) {
99         auto module = std::get<static_cast<int>(ParameterScope::MODULE)>(recipient);
100         if (!syncParameters.empty()) {
101             RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(module->setVendorParameters(
102                                     syncParameters, false /*async*/)));
103         }
104         if (!asyncParameters.empty()) {
105             RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(module->setVendorParameters(
106                                     asyncParameters, true /*async*/)));
107         }
108     } else if (recipient.index() == static_cast<int>(ParameterScope::STREAM)) {
109         auto stream = std::get<static_cast<int>(ParameterScope::STREAM)>(recipient);
110         if (!syncParameters.empty()) {
111             RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(stream->setVendorParameters(
112                                     syncParameters, false /*async*/)));
113         }
114         if (!asyncParameters.empty()) {
115             RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(stream->setVendorParameters(
116                                     asyncParameters, true /*async*/)));
117         }
118     } else {
119         LOG_ALWAYS_FATAL("%s: unexpected recipient variant index: %zu",
120                 __func__, recipient.index());
121     }
122     return OK;
123 }
124 
125 } // namespace android
126