1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
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 #ifndef LOG_TAG
16 #define LOG_TAG "AudioPolicyProxy"
17 #endif
18 
19 #include "audio_policy_proxy.h"
20 
21 #include "audio_policy_log.h"
22 
23 namespace OHOS {
24 namespace AudioStandard {
25 using namespace std;
26 
SetMicrophoneMute(bool isMute)27 int32_t AudioPolicyProxy::SetMicrophoneMute(bool isMute)
28 {
29     MessageParcel data;
30     MessageParcel reply;
31     MessageOption option;
32 
33     bool ret = data.WriteInterfaceToken(GetDescriptor());
34     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
35     data.WriteBool(isMute);
36     int32_t error = Remote()->SendRequest(
37         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_MICROPHONE_MUTE), data, reply, option);
38     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "set microphoneMute failed, error: %d", error);
39 
40     return reply.ReadInt32();
41 }
42 
SetMicrophoneMuteAudioConfig(bool isMute)43 int32_t AudioPolicyProxy::SetMicrophoneMuteAudioConfig(bool isMute)
44 {
45     MessageParcel data;
46     MessageParcel reply;
47     MessageOption option;
48 
49     bool ret = data.WriteInterfaceToken(GetDescriptor());
50     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
51     data.WriteBool(isMute);
52     int32_t error = Remote()->SendRequest(
53         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_MICROPHONE_MUTE_AUDIO_CONFIG), data, reply, option);
54     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "set microphoneMute failed, error: %d", error);
55 
56     return reply.ReadInt32();
57 }
58 
SetMicrophoneMutePersistent(const bool isMute,const PolicyType type)59 int32_t AudioPolicyProxy::SetMicrophoneMutePersistent(const bool isMute, const PolicyType type)
60 {
61     MessageParcel data;
62     MessageParcel reply;
63     MessageOption option;
64     bool ret = data.WriteInterfaceToken(GetDescriptor());
65     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
66     data.WriteBool(isMute);
67     data.WriteInt32(static_cast<int32_t>(type));
68     int32_t error = Remote()->SendRequest(
69         static_cast<uint32_t>(AudioPolicyInterfaceCode::SET_MICROPHONE_MUTE_PERSISTENT), data, reply, option);
70     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "set microphoneMute persistent failed, error: %d", error);
71 
72     return reply.ReadInt32();
73 }
74 
IsMicrophoneMuteLegacy()75 bool AudioPolicyProxy::IsMicrophoneMuteLegacy()
76 {
77     MessageParcel data;
78     MessageParcel reply;
79     MessageOption option;
80 
81     bool ret = data.WriteInterfaceToken(GetDescriptor());
82     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
83 
84     int32_t error = Remote()->SendRequest(
85         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_MICROPHONE_MUTE_LEGACY), data, reply, option);
86     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "set microphoneMute failed, error: %d", error);
87 
88     return reply.ReadBool();
89 }
90 
IsMicrophoneMute()91 bool AudioPolicyProxy::IsMicrophoneMute()
92 {
93     MessageParcel data;
94     MessageParcel reply;
95     MessageOption option;
96 
97     bool ret = data.WriteInterfaceToken(GetDescriptor());
98     CHECK_AND_RETURN_RET_LOG(ret, -1, "WriteInterfaceToken failed");
99 
100     int32_t error = Remote()->SendRequest(
101         static_cast<uint32_t>(AudioPolicyInterfaceCode::IS_MICROPHONE_MUTE), data, reply, option);
102     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, error, "set microphoneMute failed, error: %d", error);
103 
104     return reply.ReadBool();
105 }
106 
GetAudioCapturerMicrophoneDescriptors(int32_t sessionId)107 std::vector<sptr<MicrophoneDescriptor>> AudioPolicyProxy::GetAudioCapturerMicrophoneDescriptors(
108     int32_t sessionId)
109 {
110     MessageParcel data;
111     MessageParcel reply;
112     MessageOption option;
113     std::vector<sptr<MicrophoneDescriptor>> micDescs;
114 
115     bool ret = data.WriteInterfaceToken(GetDescriptor());
116     CHECK_AND_RETURN_RET_LOG(ret, micDescs, "WriteInterfaceToken failed");
117 
118     data.WriteInt32(sessionId);
119     int32_t error = Remote()->SendRequest(
120         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_AUDIO_CAPTURER_MICROPHONE_DESCRIPTORS),
121         data, reply, option);
122     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, micDescs,
123         "Get audio capturer microphonedescriptors failed, error: %d", error);
124 
125     int32_t size = reply.ReadInt32();
126     CHECK_AND_RETURN_RET_LOG(size >= 0 && size <= static_cast<int32_t>(AUDIO_DEVICE_INFO_SIZE_LIMIT),
127         micDescs, "Using tainted data [size:%{public}d] as loop bound", size);
128     for (int32_t i = 0; i < size; i++) {
129         micDescs.push_back(MicrophoneDescriptor::Unmarshalling(reply));
130     }
131 
132     return micDescs;
133 }
134 
GetAvailableMicrophones()135 std::vector<sptr<MicrophoneDescriptor>> AudioPolicyProxy::GetAvailableMicrophones()
136 {
137     MessageParcel data;
138     MessageParcel reply;
139     MessageOption option;
140     std::vector<sptr<MicrophoneDescriptor>> micDescs;
141 
142     bool ret = data.WriteInterfaceToken(GetDescriptor());
143     CHECK_AND_RETURN_RET_LOG(ret, micDescs, "WriteInterfaceToken failed");
144 
145     int32_t error = Remote()->SendRequest(
146         static_cast<uint32_t>(AudioPolicyInterfaceCode::GET_AVAILABLE_MICROPHONE_DESCRIPTORS), data, reply, option);
147     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, micDescs,
148         "Get available microphonedescriptors failed, error: %d", error);
149 
150     int32_t size = reply.ReadInt32();
151     CHECK_AND_RETURN_RET_LOG(size >= 0 && size <= static_cast<int32_t>(AUDIO_DEVICE_INFO_SIZE_LIMIT),
152         micDescs, "Using tainted data [size:%{public}d] as loop bound", size);
153     for (int32_t i = 0; i < size; i++) {
154         micDescs.push_back(MicrophoneDescriptor::Unmarshalling(reply));
155     }
156 
157     return micDescs;
158 }
159 
160 } // namespace AudioStandard
161 } // namespace OHOS