1 // Copyright (C) 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <android-base/logging.h>
16 #include <android/binder_manager.h>
17 #include <android/binder_process.h>
18 #include <hidl/HidlTransportSupport.h>
19 
20 #include <optional>
21 
22 #include "AudioProxyError.h"
23 #include "AudioProxyImpl.h"
24 #include "DevicesFactoryImpl.h"
25 #include "ServiceConfig.h"
26 
27 using android::sp;
28 using android::status_t;
29 
30 using namespace audio_proxy::service;
31 
main(int argc,char ** argv)32 int main(int argc, char** argv) {
33   auto config = parseServiceConfigFromCommandLine(argc, argv);
34   if (!config) {
35     return ERROR_INVALID_ARGS;
36   }
37 
38   // Default stream config.
39   StreamConfig defaultStreamConfig = {10, 10};
40   config->streams.emplace("default", defaultStreamConfig);
41 
42   // Config thread pool.
43   ABinderProcess_setThreadPoolMaxThreadCount(1);
44   android::hardware::configureRpcThreadpool(1, false /* callerWillJoin */);
45 
46   // Register AudioProxy service.
47   auto audioProxy = ndk::SharedRefBase::make<AudioProxyImpl>();
48   const std::string audioProxyName =
49       std::string(AudioProxyImpl::descriptor) + "/" + config->name;
50 
51   binder_status_t binder_status = AServiceManager_addService(
52       audioProxy->asBinder().get(), audioProxyName.c_str());
53   if (binder_status != STATUS_OK) {
54     LOG(ERROR) << "Failed to start " << config->name
55                << " AudioProxy service, status " << binder_status;
56     return ERROR_AIDL_FAILURE;
57   }
58 
59   // Register AudioProxy audio HAL.
60   auto devicesFactory =
61       sp<DevicesFactoryImpl>::make(audioProxy->getBusStreamProvider(), *config);
62   status_t status = devicesFactory->registerAsService(config->name);
63   if (status != android::OK) {
64     LOG(ERROR) << "Failed to start " << config->name << " audio HAL, status "
65                << status;
66     return ERROR_HIDL_FAILURE;
67   }
68 
69   ABinderProcess_joinThreadPool();
70 
71   // `ABinderProcess_joinThreadpool` should never return. Return -2 here for
72   // unexpected process exit.
73   return ERROR_UNEXPECTED;
74 }
75