blob: 0b3e3ba00dada73b53ee97de4fc6753f27f164e4 [file] [log] [blame]
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001/*
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
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000017#include <cstdlib>
18#include <ctime>
Mikhail Naganovdf5feba2022-12-15 00:11:14 +000019#include <utility>
Mikhail Naganov521fc492023-07-11 17:24:08 -070020#include <vector>
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000021
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000022#define LOG_TAG "AHAL_Main"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000023#include <android-base/logging.h>
Mikhail Naganov521fc492023-07-11 17:24:08 -070024#include <android-base/properties.h>
Shunkai Yao39bf2c32022-12-06 03:25:59 +000025#include <android/binder_ibinder_platform.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000026#include <android/binder_manager.h>
27#include <android/binder_process.h>
28
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000029#include "core-impl/AudioPolicyConfigXmlConverter.h"
30#include "core-impl/ChildInterface.h"
Lorena Torres-Huertabc585bd2022-10-23 20:41:35 +000031#include "core-impl/Config.h"
32#include "core-impl/Module.h"
33
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000034using aidl::android::hardware::audio::core::ChildInterface;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000035using aidl::android::hardware::audio::core::Config;
36using aidl::android::hardware::audio::core::Module;
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000037using aidl::android::hardware::audio::core::internal::AudioPolicyConfigXmlConverter;
38
39namespace {
40
41ChildInterface<Module> createModule(const std::string& name,
42 std::unique_ptr<Module::Configuration>&& config) {
43 ChildInterface<Module> result;
44 {
45 auto moduleType = Module::typeFromString(name);
46 if (!moduleType.has_value()) {
47 LOG(ERROR) << __func__ << ": module type \"" << name << "\" is not supported";
48 return result;
49 }
50 auto module = Module::createInstance(*moduleType, std::move(config));
51 if (module == nullptr) return result;
52 result = std::move(module);
53 }
54 const std::string moduleFqn = std::string().append(Module::descriptor).append("/").append(name);
55 binder_status_t status = AServiceManager_addService(result.getBinder(), moduleFqn.c_str());
56 if (status != STATUS_OK) {
57 LOG(ERROR) << __func__ << ": failed to register service for \"" << moduleFqn << "\"";
58 return ChildInterface<Module>();
59 }
60 return result;
61};
62
63} // namespace
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000064
65int main() {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000066 // Random values are used in the implementation.
67 std::srand(std::time(nullptr));
68
Mikhail Naganov16db9b72022-06-17 21:36:18 +000069 // This is a debug implementation, always enable debug logging.
70 android::base::SetMinimumLogSeverity(::android::base::DEBUG);
Mikhail Naganov1f72fd42023-02-02 15:26:04 -080071 // For more logs, use VERBOSE, however this may hinder performance.
72 // android::base::SetMinimumLogSeverity(::android::base::VERBOSE);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000073 ABinderProcess_setThreadPoolMaxThreadCount(16);
Mikhail Naganov1840d352024-08-19 14:48:28 -070074 ABinderProcess_startThreadPool();
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000075
Steven Moreland705c39b2023-03-06 14:30:15 +000076 // Guaranteed log for b/210919187 and logd_integration_test
77 LOG(INFO) << "Init for Audio AIDL HAL";
78
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000079 AudioPolicyConfigXmlConverter audioPolicyConverter{
80 ::android::audio_get_audio_policy_config_file()};
81
Mikhail Naganov16db9b72022-06-17 21:36:18 +000082 // Make the default config service
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000083 auto config = ndk::SharedRefBase::make<Config>(audioPolicyConverter);
84 const std::string configFqn = std::string().append(Config::descriptor).append("/default");
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000085 binder_status_t status =
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000086 AServiceManager_addService(config->asBinder().get(), configFqn.c_str());
87 if (status != STATUS_OK) {
88 LOG(ERROR) << "failed to register service for \"" << configFqn << "\"";
89 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000090
Mikhail Naganovc8e43122022-12-09 00:33:47 +000091 // Make modules
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000092 std::vector<ChildInterface<Module>> moduleInstances;
93 auto configs(audioPolicyConverter.releaseModuleConfigs());
94 for (std::pair<std::string, std::unique_ptr<Module::Configuration>>& configPair : *configs) {
95 std::string name = configPair.first;
96 if (auto instance = createModule(name, std::move(configPair.second)); instance) {
97 moduleInstances.push_back(std::move(instance));
98 }
99 }
Mikhail Naganovc8e43122022-12-09 00:33:47 +0000100
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000101 ABinderProcess_joinThreadPool();
102 return EXIT_FAILURE; // should not reach
103}