• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <algorithm>
18 #include <map>
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 
23 #define LOG_TAG "DevicesFactoryHalAidl"
24 //#define LOG_NDEBUG 0
25 
26 #include <aidl/android/hardware/audio/core/IModule.h>
27 #include <aidl/android/media/audio/BnHalAdapterVendorExtension.h>
28 #include <android/binder_manager.h>
29 #include <cutils/properties.h>
30 #include <media/AidlConversionNdkCpp.h>
31 #include <media/AidlConversionUtil.h>
32 #include <utils/Log.h>
33 
34 #include "AidlUtils.h"
35 #include "DeviceHalAidl.h"
36 #include "DevicesFactoryHalAidl.h"
37 
38 using aidl::android::aidl_utils::statusTFromBinderStatus;
39 using aidl::android::hardware::audio::core::IConfig;
40 using aidl::android::hardware::audio::core::IModule;
41 using aidl::android::hardware::audio::core::SurroundSoundConfig;
42 using aidl::android::hardware::audio::core::VendorParameter;
43 using aidl::android::media::audio::common::AudioHalEngineConfig;
44 using aidl::android::media::audio::IHalAdapterVendorExtension;
45 using android::detail::AudioHalVersionInfo;
46 
47 namespace android {
48 
49 namespace {
50 
51 ConversionResult<media::SurroundSoundConfig::SurroundFormatFamily>
ndk2cpp_SurroundSoundConfigFormatFamily(const SurroundSoundConfig::SurroundFormatFamily & ndk)52 ndk2cpp_SurroundSoundConfigFormatFamily(const SurroundSoundConfig::SurroundFormatFamily& ndk) {
53     media::SurroundSoundConfig::SurroundFormatFamily cpp;
54     cpp.primaryFormat = VALUE_OR_RETURN(ndk2cpp_AudioFormatDescription(ndk.primaryFormat));
55     cpp.subFormats = VALUE_OR_RETURN(::aidl::android::convertContainer<std::vector<
56             media::audio::common::AudioFormatDescription>>(ndk.subFormats,
57                     ndk2cpp_AudioFormatDescription));
58     return cpp;
59 }
60 
61 ConversionResult<media::SurroundSoundConfig>
ndk2cpp_SurroundSoundConfig(const SurroundSoundConfig & ndk)62 ndk2cpp_SurroundSoundConfig(const SurroundSoundConfig& ndk) {
63     media::SurroundSoundConfig cpp;
64     cpp.formatFamilies = VALUE_OR_RETURN(::aidl::android::convertContainer<std::vector<
65             media::SurroundSoundConfig::SurroundFormatFamily>>(ndk.formatFamilies,
66                     ndk2cpp_SurroundSoundConfigFormatFamily));
67     return cpp;
68 }
69 
70 class HalAdapterVendorExtensionWrapper :
71             public ::aidl::android::media::audio::BnHalAdapterVendorExtension {
72   private:
73     template<typename F>
callWithRetryOnCrash(F method)74     ndk::ScopedAStatus callWithRetryOnCrash(F method) {
75         ndk::ScopedAStatus status = ndk::ScopedAStatus::ok();
76         for (auto service = getService(); service != nullptr; service = getService(true)) {
77             status = method(service);
78             if (status.getStatus() != STATUS_DEAD_OBJECT) break;
79         }
80         return status;
81     }
82 
parseVendorParameterIds(ParameterScope in_scope,const std::string & in_rawKeys,std::vector<std::string> * _aidl_return)83     ndk::ScopedAStatus parseVendorParameterIds(ParameterScope in_scope,
84                                                const std::string& in_rawKeys,
85                                                std::vector<std::string>* _aidl_return) override {
86         return callWithRetryOnCrash([&](auto service) {
87             return service->parseVendorParameterIds(in_scope, in_rawKeys, _aidl_return);
88         });
89     }
90 
parseVendorParameters(ParameterScope in_scope,const std::string & in_rawKeysAndValues,std::vector<VendorParameter> * out_syncParameters,std::vector<VendorParameter> * out_asyncParameters)91     ndk::ScopedAStatus parseVendorParameters(
92             ParameterScope in_scope, const std::string& in_rawKeysAndValues,
93             std::vector<VendorParameter>* out_syncParameters,
94             std::vector<VendorParameter>* out_asyncParameters) override {
95         return callWithRetryOnCrash([&](auto service) {
96             return service->parseVendorParameters(in_scope, in_rawKeysAndValues,
97                     out_syncParameters, out_asyncParameters);
98         });
99     }
100 
parseBluetoothA2dpReconfigureOffload(const std::string & in_rawValue,std::vector<VendorParameter> * _aidl_return)101     ndk::ScopedAStatus parseBluetoothA2dpReconfigureOffload(
102             const std::string& in_rawValue, std::vector<VendorParameter>* _aidl_return) override {
103         return callWithRetryOnCrash([&](auto service) {
104             return service->parseBluetoothA2dpReconfigureOffload(in_rawValue, _aidl_return);
105         });
106     }
107 
parseBluetoothLeReconfigureOffload(const std::string & in_rawValue,std::vector<VendorParameter> * _aidl_return)108     ndk::ScopedAStatus parseBluetoothLeReconfigureOffload(const std::string& in_rawValue,
109             std::vector<VendorParameter>* _aidl_return) override {
110         return callWithRetryOnCrash([&](auto service) {
111             return service->parseBluetoothLeReconfigureOffload(in_rawValue, _aidl_return);
112         });
113     }
114 
processVendorParameters(ParameterScope in_scope,const std::vector<VendorParameter> & in_parameters,std::string * _aidl_return)115     ndk::ScopedAStatus processVendorParameters(ParameterScope in_scope,
116                                                const std::vector<VendorParameter>& in_parameters,
117                                                std::string* _aidl_return) override {
118         return callWithRetryOnCrash([&](auto service) {
119             return service->processVendorParameters(in_scope, in_parameters, _aidl_return);
120         });
121     }
122 
getService(bool reset=false)123     std::shared_ptr<IHalAdapterVendorExtension> getService(bool reset = false) {
124         std::lock_guard l(mLock);
125         if (reset || !mVendorExt.has_value()) {
126             if (property_get_bool("ro.audio.ihaladaptervendorextension_enabled", false)) {
127                 auto serviceName = std::string(IHalAdapterVendorExtension::descriptor) + "/default";
128                 mVendorExt = std::shared_ptr<IHalAdapterVendorExtension>(
129                         IHalAdapterVendorExtension::fromBinder(ndk::SpAIBinder(
130                                         AServiceManager_waitForService(serviceName.c_str()))));
131             } else {
132                 mVendorExt = nullptr;
133             }
134         }
135         return mVendorExt.value();
136     }
137 
138     std::mutex mLock;
139     std::optional<std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension>>
140             mVendorExt GUARDED_BY(mLock);
141 };
142 
143 }  // namespace
144 
DevicesFactoryHalAidl(std::shared_ptr<IConfig> config)145 DevicesFactoryHalAidl::DevicesFactoryHalAidl(std::shared_ptr<IConfig> config)
146         : mConfig(std::move(config)),
147           mVendorExt(ndk::SharedRefBase::make<HalAdapterVendorExtensionWrapper>()) {
148 }
149 
getDeviceNames(std::vector<std::string> * names)150 status_t DevicesFactoryHalAidl::getDeviceNames(std::vector<std::string> *names) {
151     if (names == nullptr) {
152         return BAD_VALUE;
153     }
154     AServiceManager_forEachDeclaredInstance(IModule::descriptor, static_cast<void*>(names),
155             [](const char* instance, void* context) {
156                 if (strcmp(instance, "default") == 0) instance = "primary";
157                 static_cast<decltype(names)>(context)->push_back(instance);
158             });
159     std::sort(names->begin(), names->end(), [](const std::string& lhs,
160                     const std::string& rhs) {
161         // This order corresponds to the canonical order of modules as specified in
162         // the reference 'audio_policy_configuration_7_0.xml' file.
163         static const std::map<std::string, int> kPriorities{
164             { "primary", 0 }, { "a2dp", 1 }, { "usb", 2 }, { "r_submix", 3 },
165             { "bluetooth", 4 }, { "hearing_aid", 5 }, { "msd", 6 }, { "stub", 7 }
166         };
167         auto lhsIt = kPriorities.find(lhs);
168         auto rhsIt = kPriorities.find(rhs);
169         if (lhsIt != kPriorities.end() && rhsIt != kPriorities.end()) {
170             return lhsIt->second < rhsIt->second;
171         }
172         return lhsIt != kPriorities.end();
173     });
174     return OK;
175 }
176 
177 // Opens a device with the specified name. To close the device, it is
178 // necessary to release references to the returned object.
openDevice(const char * name,sp<DeviceHalInterface> * device)179 status_t DevicesFactoryHalAidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
180     if (name == nullptr || device == nullptr) {
181         return BAD_VALUE;
182     }
183     if (strcmp(name, "primary") == 0) name = "default";
184     *device = sp<DeviceHalAidl>::make(name, getServiceInstance<IModule>(name), mVendorExt);
185     return OK;
186 }
187 
setCallbackOnce(sp<DevicesFactoryHalCallback> callback)188 status_t DevicesFactoryHalAidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) {
189     // Dynamic registration of module instances is not supported. The functionality
190     // in the audio server which is related to this callback can be removed together
191     // with HIDL support.
192     ALOG_ASSERT(callback != nullptr);
193     if (callback != nullptr) {
194         callback->onNewDevicesAvailable();
195     }
196     return NO_ERROR;
197 }
198 
getHalVersion() const199 AudioHalVersionInfo DevicesFactoryHalAidl::getHalVersion() const {
200     int32_t versionNumber = 0;
201     if (ndk::ScopedAStatus status = mConfig->getInterfaceVersion(&versionNumber); !status.isOk()) {
202         ALOGE("%s getInterfaceVersion failed: %s", __func__, status.getDescription().c_str());
203     }
204     // AIDL does not have minor version, fill 0 for all versions
205     return AudioHalVersionInfo(AudioHalVersionInfo::Type::AIDL, versionNumber);
206 }
207 
getSurroundSoundConfig(media::SurroundSoundConfig * config)208 status_t DevicesFactoryHalAidl::getSurroundSoundConfig(media::SurroundSoundConfig *config) {
209     SurroundSoundConfig ndkConfig;
210     RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getSurroundSoundConfig(&ndkConfig)));
211     *config = VALUE_OR_RETURN_STATUS(ndk2cpp_SurroundSoundConfig(ndkConfig));
212     return OK;
213 }
214 
getEngineConfig(media::audio::common::AudioHalEngineConfig * config)215 status_t DevicesFactoryHalAidl::getEngineConfig(
216         media::audio::common::AudioHalEngineConfig *config) {
217     AudioHalEngineConfig ndkConfig;
218     RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mConfig->getEngineConfig(&ndkConfig)));
219     *config = VALUE_OR_RETURN_STATUS(ndk2cpp_AudioHalEngineConfig(ndkConfig));
220     return OK;
221 }
222 
223 // Main entry-point to the shared library.
createIDevicesFactoryImpl()224 extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() {
225     return new DevicesFactoryHalAidl(getServiceInstance<IConfig>("default"));
226 }
227 
228 } // namespace android
229