• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 "AudioPolicyManagerStub"
17 #endif
18 
19 #include "audio_policy_manager_stub.h"
20 
21 #include "audio_errors.h"
22 #include "audio_policy_log.h"
23 
24 namespace OHOS {
25 namespace AudioStandard {
26 
GetDevicesInternal(MessageParcel & data,MessageParcel & reply)27 void AudioPolicyManagerStub::GetDevicesInternal(MessageParcel &data, MessageParcel &reply)
28 {
29     int deviceFlag = data.ReadInt32();
30     DeviceFlag deviceFlagConfig = static_cast<DeviceFlag>(deviceFlag);
31     std::vector<std::shared_ptr<AudioDeviceDescriptor>> devices = GetDevices(deviceFlagConfig);
32     int32_t size = static_cast<int32_t>(devices.size());
33     reply.WriteInt32(size);
34     for (int i = 0; i < size; i++) {
35         devices[i]->Marshalling(reply);
36     }
37 }
38 
GetDevicesInnerInternal(MessageParcel & data,MessageParcel & reply)39 void AudioPolicyManagerStub::GetDevicesInnerInternal(MessageParcel &data, MessageParcel &reply)
40 {
41     int deviceFlag = data.ReadInt32();
42     DeviceFlag deviceFlagConfig = static_cast<DeviceFlag>(deviceFlag);
43     std::vector<std::shared_ptr<AudioDeviceDescriptor>> devices = GetDevicesInner(deviceFlagConfig);
44     int32_t size = static_cast<int32_t>(devices.size());
45     reply.WriteInt32(size);
46     for (int i = 0; i < size; i++) {
47         devices[i]->Marshalling(reply);
48     }
49 }
50 
GetPreferredOutputDeviceDescriptorsInternal(MessageParcel & data,MessageParcel & reply)51 void AudioPolicyManagerStub::GetPreferredOutputDeviceDescriptorsInternal(MessageParcel &data, MessageParcel &reply)
52 {
53     AudioRendererInfo rendererInfo;
54     rendererInfo.Unmarshalling(data);
55     bool forceNoBTPermission = data.ReadBool();
56     std::vector<std::shared_ptr<AudioDeviceDescriptor>> devices =
57         GetPreferredOutputDeviceDescriptors(rendererInfo, forceNoBTPermission);
58     int32_t size = static_cast<int32_t>(devices.size());
59     reply.WriteInt32(size);
60     for (int i = 0; i < size; i++) {
61         devices[i]->Marshalling(reply);
62     }
63 }
64 
GetPreferredInputDeviceDescriptorsInternal(MessageParcel & data,MessageParcel & reply)65 void AudioPolicyManagerStub::GetPreferredInputDeviceDescriptorsInternal(MessageParcel &data, MessageParcel &reply)
66 {
67     AudioCapturerInfo captureInfo;
68     captureInfo.Unmarshalling(data);
69     std::vector<std::shared_ptr<AudioDeviceDescriptor>> devices = GetPreferredInputDeviceDescriptors(captureInfo);
70     uint32_t size = static_cast<uint32_t>(devices.size());
71     reply.WriteInt32(size);
72     for (uint32_t i = 0; i < size; i++) {
73         devices[i]->Marshalling(reply);
74     }
75 }
76 
SetDeviceActiveInternal(MessageParcel & data,MessageParcel & reply)77 void AudioPolicyManagerStub::SetDeviceActiveInternal(MessageParcel &data, MessageParcel &reply)
78 {
79     InternalDeviceType deviceType = static_cast<InternalDeviceType>(data.ReadInt32());
80     bool active = data.ReadBool();
81     int32_t uid = data.ReadInt32();
82     int32_t result = SetDeviceActive(deviceType, active, uid);
83     if (result == SUCCESS)
84         reply.WriteInt32(AUDIO_OK);
85     else
86         reply.WriteInt32(AUDIO_ERR);
87 }
88 
IsDeviceActiveInternal(MessageParcel & data,MessageParcel & reply)89 void AudioPolicyManagerStub::IsDeviceActiveInternal(MessageParcel &data, MessageParcel &reply)
90 {
91     InternalDeviceType deviceType = static_cast<InternalDeviceType>(data.ReadInt32());
92     bool result = IsDeviceActive(deviceType);
93     reply.WriteBool(result);
94 }
95 
GetActiveOutputDeviceInternal(MessageParcel & data,MessageParcel & reply)96 void AudioPolicyManagerStub::GetActiveOutputDeviceInternal(MessageParcel &data, MessageParcel &reply)
97 {
98     InternalDeviceType deviceType = GetActiveOutputDevice();
99     reply.WriteInt32(static_cast<int>(deviceType));
100 }
101 
GetActiveInputDeviceInternal(MessageParcel & data,MessageParcel & reply)102 void AudioPolicyManagerStub::GetActiveInputDeviceInternal(MessageParcel &data, MessageParcel &reply)
103 {
104     InternalDeviceType deviceType = GetActiveInputDevice();
105     reply.WriteInt32(static_cast<int>(deviceType));
106 }
107 
SelectOutputDeviceInternal(MessageParcel & data,MessageParcel & reply)108 void AudioPolicyManagerStub::SelectOutputDeviceInternal(MessageParcel &data, MessageParcel &reply)
109 {
110     sptr<AudioRendererFilter> audioRendererFilter = AudioRendererFilter::Unmarshalling(data);
111     CHECK_AND_RETURN_LOG(audioRendererFilter != nullptr, "AudioRendererFilter unmarshall fail.");
112 
113     int validSize = 20; // Use 20 as limit.
114     int size = data.ReadInt32();
115     if (size <= 0 || size > validSize) {
116         AUDIO_ERR_LOG("SelectOutputDevice get invalid device size.");
117         return;
118     }
119     std::vector<std::shared_ptr<AudioDeviceDescriptor>> targetOutputDevice;
120     for (int i = 0; i < size; i++) {
121         std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor = AudioDeviceDescriptor::UnmarshallingPtr(data);
122         CHECK_AND_RETURN_LOG(audioDeviceDescriptor != nullptr, "Unmarshalling fail.");
123         MapExternalToInternalDeviceType(*audioDeviceDescriptor);
124         targetOutputDevice.push_back(audioDeviceDescriptor);
125     }
126 
127     int32_t ret = SelectOutputDevice(audioRendererFilter, targetOutputDevice);
128     reply.WriteInt32(ret);
129 }
130 
GetSelectedDeviceInfoInternal(MessageParcel & data,MessageParcel & reply)131 void AudioPolicyManagerStub::GetSelectedDeviceInfoInternal(MessageParcel &data, MessageParcel &reply)
132 {
133     int32_t uid = data.ReadInt32();
134     int32_t pid = data.ReadInt32();
135     AudioStreamType streamType =  static_cast<AudioStreamType>(data.ReadInt32());
136 
137     std::string deviceName = GetSelectedDeviceInfo(uid, pid, streamType);
138     reply.WriteString(deviceName);
139 }
140 
SelectInputDeviceInternal(MessageParcel & data,MessageParcel & reply)141 void AudioPolicyManagerStub::SelectInputDeviceInternal(MessageParcel &data, MessageParcel &reply)
142 {
143     sptr<AudioCapturerFilter> audioCapturerFilter = AudioCapturerFilter::Unmarshalling(data);
144     CHECK_AND_RETURN_LOG(audioCapturerFilter != nullptr, "AudioCapturerFilter unmarshall fail.");
145 
146     int validSize = 10; // Use 10 as limit.
147     int size = data.ReadInt32();
148     CHECK_AND_RETURN_LOG(size > 0 && size <= validSize, "SelectInputDevice get invalid device size.");
149     std::vector<std::shared_ptr<AudioDeviceDescriptor>> targetInputDevice;
150     for (int i = 0; i < size; i++) {
151         std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor = AudioDeviceDescriptor::UnmarshallingPtr(data);
152         CHECK_AND_RETURN_LOG(audioDeviceDescriptor != nullptr, "Unmarshalling fail.");
153         MapExternalToInternalDeviceType(*audioDeviceDescriptor);
154         targetInputDevice.push_back(audioDeviceDescriptor);
155     }
156 
157     int32_t ret = SelectInputDevice(audioCapturerFilter, targetInputDevice);
158     reply.WriteInt32(ret);
159 }
160 
ExcludeOutputDevicesInternal(MessageParcel & data,MessageParcel & reply)161 void AudioPolicyManagerStub::ExcludeOutputDevicesInternal(MessageParcel &data, MessageParcel &reply)
162 {
163     AudioDeviceUsage audioDevUsage = static_cast<AudioDeviceUsage>(data.ReadInt32());
164     int validSize = 20; // Use 20 as limit.
165     int size = data.ReadInt32();
166     CHECK_AND_RETURN_LOG(size > 0 && size <= validSize, "ExcludeOutputDevices get invalid device size.");
167     std::vector<std::shared_ptr<AudioDeviceDescriptor>> audioDeviceDescriptors;
168     for (int i = 0; i < size; i++) {
169         std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor = AudioDeviceDescriptor::UnmarshallingPtr(data);
170         CHECK_AND_RETURN_LOG(audioDeviceDescriptor != nullptr, "Unmarshalling fail.");
171         audioDeviceDescriptors.push_back(audioDeviceDescriptor);
172     }
173 
174     int32_t ret = ExcludeOutputDevices(audioDevUsage, audioDeviceDescriptors);
175     reply.WriteInt32(ret);
176 }
177 
UnexcludeOutputDevicesInternal(MessageParcel & data,MessageParcel & reply)178 void AudioPolicyManagerStub::UnexcludeOutputDevicesInternal(MessageParcel &data, MessageParcel &reply)
179 {
180     AudioDeviceUsage audioDevUsage = static_cast<AudioDeviceUsage>(data.ReadInt32());
181     int validSize = 20; // Use 20 as limit.
182     int size = data.ReadInt32();
183     CHECK_AND_RETURN_LOG(size > 0 && size <= validSize, "UnexcludeOutputDevices get invalid device size.");
184     std::vector<std::shared_ptr<AudioDeviceDescriptor>> audioDeviceDescriptors;
185     for (int i = 0; i < size; i++) {
186         std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor = AudioDeviceDescriptor::UnmarshallingPtr(data);
187         CHECK_AND_RETURN_LOG(audioDeviceDescriptor != nullptr, "Unmarshalling fail.");
188         audioDeviceDescriptors.push_back(audioDeviceDescriptor);
189     }
190 
191     int32_t ret = UnexcludeOutputDevices(audioDevUsage, audioDeviceDescriptors);
192     reply.WriteInt32(ret);
193 }
194 
GetExcludedDevicesInternal(MessageParcel & data,MessageParcel & reply)195 void AudioPolicyManagerStub::GetExcludedDevicesInternal(MessageParcel &data, MessageParcel &reply)
196 {
197     AudioDeviceUsage audioDevUsage = static_cast<AudioDeviceUsage>(data.ReadInt32());
198     std::vector<std::shared_ptr<AudioDeviceDescriptor>> devices = GetExcludedDevices(audioDevUsage);
199     int32_t size = static_cast<int32_t>(devices.size());
200     reply.WriteInt32(size);
201     for (int i = 0; i < size; i++) {
202         devices[i]->Marshalling(reply);
203     }
204 }
205 
GetAvailableDevicesInternal(MessageParcel & data,MessageParcel & reply)206 void AudioPolicyManagerStub::GetAvailableDevicesInternal(MessageParcel &data, MessageParcel &reply)
207 {
208     AudioDeviceUsage usage  = static_cast<AudioDeviceUsage>(data.ReadInt32());
209     std::vector<std::shared_ptr<AudioDeviceDescriptor>> descs = GetAvailableDevices(usage);
210     int32_t size = static_cast<int32_t>(descs.size());
211     reply.WriteInt32(size);
212     for (int32_t i = 0; i < size; i++) {
213         descs[i]->Marshalling(reply);
214     }
215 }
216 
SetAvailableDeviceChangeCallbackInternal(MessageParcel & data,MessageParcel & reply)217 void AudioPolicyManagerStub::SetAvailableDeviceChangeCallbackInternal(MessageParcel &data, MessageParcel &reply)
218 {
219     int32_t clientId = data.ReadInt32();
220     AudioDeviceUsage usage = static_cast<AudioDeviceUsage>(data.ReadInt32());
221     sptr<IRemoteObject> object = data.ReadRemoteObject();
222     CHECK_AND_RETURN_LOG(object != nullptr, "AudioInterruptCallback obj is null");
223     int32_t result = SetAvailableDeviceChangeCallback(clientId, usage, object);
224     reply.WriteInt32(result);
225 }
226 
UnsetAvailableDeviceChangeCallbackInternal(MessageParcel & data,MessageParcel & reply)227 void AudioPolicyManagerStub::UnsetAvailableDeviceChangeCallbackInternal(MessageParcel &data, MessageParcel &reply)
228 {
229     int32_t clientId = data.ReadInt32();
230     AudioDeviceUsage usage = static_cast<AudioDeviceUsage>(data.ReadInt32());
231     int32_t result = UnsetAvailableDeviceChangeCallback(clientId, usage);
232     reply.WriteInt32(result);
233 }
234 
SetCallDeviceActiveInternal(MessageParcel & data,MessageParcel & reply)235 void AudioPolicyManagerStub::SetCallDeviceActiveInternal(MessageParcel &data, MessageParcel &reply)
236 {
237     InternalDeviceType deviceType = static_cast<InternalDeviceType>(data.ReadInt32());
238     bool active = data.ReadBool();
239     std::string address = data.ReadString();
240     int32_t uid = data.ReadInt32();
241     int32_t result = SetCallDeviceActive(deviceType, active, address, uid);
242     reply.WriteInt32(result);
243 }
244 
GetActiveBluetoothDeviceInternal(MessageParcel & data,MessageParcel & reply)245 void AudioPolicyManagerStub::GetActiveBluetoothDeviceInternal(MessageParcel &data, MessageParcel &reply)
246 {
247     std::shared_ptr<AudioDeviceDescriptor> desc = GetActiveBluetoothDevice();
248     desc->Marshalling(reply);
249 }
250 
FetchOutputDeviceForTrackInternal(MessageParcel & data,MessageParcel & reply)251 void AudioPolicyManagerStub::FetchOutputDeviceForTrackInternal(MessageParcel &data, MessageParcel &reply)
252 {
253     AudioStreamChangeInfo streamChangeInfo = {};
254     streamChangeInfo.audioRendererChangeInfo.Unmarshalling(data);
255     AudioStreamDeviceChangeReasonExt reason = static_cast<AudioStreamDeviceChangeReasonExt::ExtEnum>(data.ReadInt32());
256     FetchOutputDeviceForTrack(streamChangeInfo, reason);
257 }
258 
FetchInputDeviceForTrackInternal(MessageParcel & data,MessageParcel & reply)259 void AudioPolicyManagerStub::FetchInputDeviceForTrackInternal(MessageParcel &data, MessageParcel &reply)
260 {
261     AudioStreamChangeInfo streamChangeInfo = {};
262     streamChangeInfo.audioCapturerChangeInfo.Unmarshalling(data);
263     FetchInputDeviceForTrack(streamChangeInfo);
264 }
265 
GetOutputDeviceInternal(MessageParcel & data,MessageParcel & reply)266 void AudioPolicyManagerStub::GetOutputDeviceInternal(MessageParcel &data, MessageParcel &reply)
267 {
268     sptr<AudioRendererFilter> audioRendererFilter = AudioRendererFilter::Unmarshalling(data);
269     CHECK_AND_RETURN_LOG(audioRendererFilter != nullptr, "AudioRendererFilter unmarshall fail.");
270     std::vector<std::shared_ptr<AudioDeviceDescriptor>> devices = GetOutputDevice(audioRendererFilter);
271     int32_t size = static_cast<int32_t>(devices.size());
272     reply.WriteInt32(size);
273     for (int i = 0; i < size; i++) {
274         devices[i]->Marshalling(reply);
275     }
276 }
277 
GetInputDeviceInternal(MessageParcel & data,MessageParcel & reply)278 void AudioPolicyManagerStub::GetInputDeviceInternal(MessageParcel &data, MessageParcel &reply)
279 {
280     sptr<AudioCapturerFilter> audioCapturerFilter = AudioCapturerFilter::Unmarshalling(data);
281     CHECK_AND_RETURN_LOG(audioCapturerFilter != nullptr, "AudioCapturerFilter unmarshall fail.");
282     std::vector<std::shared_ptr<AudioDeviceDescriptor>> devices = GetInputDevice(audioCapturerFilter);
283     int32_t size = static_cast<int32_t>(devices.size());
284     reply.WriteInt32(size);
285     for (int i = 0; i < size; i++) {
286         devices[i]->Marshalling(reply);
287     }
288 }
289 } // namespace AudioStandard
290 } // namespace OHOS
291