1 /*
2  * Copyright (C) 2021 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 "AudioControl.h"
18 
19 #include <aidl/android/hardware/automotive/audiocontrol/AudioFocusChange.h>
20 #include <aidl/android/hardware/automotive/audiocontrol/DuckingInfo.h>
21 #include <aidl/android/hardware/automotive/audiocontrol/IFocusListener.h>
22 
23 #include <android-base/logging.h>
24 #include <android-base/parseint.h>
25 #include <android-base/strings.h>
26 
27 #include <android_audio_policy_configuration_V7_0.h>
28 #include <private/android_filesystem_config.h>
29 
30 #include <stdio.h>
31 
32 namespace aidl::android::hardware::automotive::audiocontrol {
33 
34 using ::android::base::EqualsIgnoreCase;
35 using ::android::base::ParseInt;
36 using ::std::string;
37 
38 namespace xsd {
39 using namespace ::android::audio::policy::configuration::V7_0;
40 }
41 
AudioControl(const std::string & audio_control_server_addr)42 AudioControl::AudioControl(const std::string& audio_control_server_addr)
43     : mAudioControlServer(MakeAudioControlServer(audio_control_server_addr)) {}
44 
registerFocusListener(const shared_ptr<IFocusListener> & in_listener)45 ndk::ScopedAStatus AudioControl::registerFocusListener(
46         const shared_ptr<IFocusListener>& in_listener) {
47     LOG(DEBUG) << "registering focus listener";
48 
49     if (in_listener) {
50         mAudioControlServer->RegisterFocusListener(mFocusListener = in_listener);
51     } else {
52         LOG(ERROR) << "Unexpected nullptr for listener resulting in no-op.";
53     }
54 
55     return ndk::ScopedAStatus::ok();
56 }
57 
setBalanceTowardRight(float)58 ndk::ScopedAStatus AudioControl::setBalanceTowardRight(float) {
59     return ndk::ScopedAStatus::ok();
60 }
61 
setFadeTowardFront(float)62 ndk::ScopedAStatus AudioControl::setFadeTowardFront(float) {
63     return ndk::ScopedAStatus::ok();
64 }
65 
onAudioFocusChange(const string & in_usage,int32_t in_zoneId,AudioFocusChange in_focusChange)66 ndk::ScopedAStatus AudioControl::onAudioFocusChange(const string& in_usage, int32_t in_zoneId,
67                                                     AudioFocusChange in_focusChange) {
68     LOG(INFO) << "Focus changed: " << toString(in_focusChange).c_str() << " for usage "
69               << in_usage.c_str() << " in zone " << in_zoneId;
70     return ndk::ScopedAStatus::ok();
71 }
72 
onDevicesToDuckChange(const std::vector<DuckingInfo> & in_duckingInfos)73 ndk::ScopedAStatus AudioControl::onDevicesToDuckChange(
74         const std::vector<DuckingInfo>& in_duckingInfos) {
75     LOG(INFO) << "AudioControl::onDevicesToDuckChange";
76     for (const DuckingInfo& duckingInfo : in_duckingInfos) {
77         LOG(INFO) << "zone: " << duckingInfo.zoneId;
78         LOG(INFO) << "Devices to duck:";
79         for (const auto& addressToDuck : duckingInfo.deviceAddressesToDuck) {
80             LOG(INFO) << addressToDuck;
81         }
82         LOG(INFO) << "Devices to unduck:";
83         for (const auto& addressToUnduck : duckingInfo.deviceAddressesToUnduck) {
84             LOG(INFO) << addressToUnduck;
85         }
86         LOG(INFO) << "Usages holding focus:";
87         for (const auto& usage : duckingInfo.usagesHoldingFocus) {
88             LOG(INFO) << usage;
89         }
90     }
91     return ndk::ScopedAStatus::ok();
92 }
93 
onDevicesToMuteChange(const std::vector<MutingInfo> & in_mutingInfos)94 ndk::ScopedAStatus AudioControl::onDevicesToMuteChange(
95         const std::vector<MutingInfo>& in_mutingInfos) {
96     LOG(INFO) << "AudioControl::onDevicesToMuteChange";
97     for (const MutingInfo& mutingInfo : in_mutingInfos) {
98         LOG(INFO) << "zone: " << mutingInfo.zoneId;
99         LOG(INFO) << "Devices to mute:";
100         for (const auto& addressToMute : mutingInfo.deviceAddressesToMute) {
101             LOG(INFO) << addressToMute;
102         }
103         LOG(INFO) << "Devices to unmute:";
104         for (const auto& addressToUnmute : mutingInfo.deviceAddressesToUnmute) {
105             LOG(INFO) << addressToUnmute;
106         }
107     }
108     return ndk::ScopedAStatus::ok();
109 }
110 
dump(int,const char **,uint32_t)111 binder_status_t AudioControl::dump(int /*fd*/, const char** /*args*/, uint32_t /*numArgs*/) {
112     return STATUS_BAD_VALUE;
113 }
114 
isHealthy()115 bool AudioControl::isHealthy() {
116     // TODO(egranata, chenhaosjtuacm): fill this in with a real check
117     // e.g. add a heartbeat message to remote side
118     return true;
119 }
120 
start()121 void AudioControl::start() {
122     mAudioControlServer->Start();
123 }
124 
join()125 void AudioControl::join() {
126     mAudioControlServer->Join();
127 }
128 
129 }  // namespace aidl::android::hardware::automotive::audiocontrol
130