1 /*
2 * Copyright (c) 2023-2023 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 "AudioPolicyClientStub"
17 #endif
18
19 #include "audio_policy_client_stub.h"
20 #include "audio_errors.h"
21 #include "audio_policy_log.h"
22 #include "audio_utils.h"
23
24 using namespace std;
25 namespace OHOS {
26 namespace AudioStandard {
27
28 static const int32_t FOCUS_INFO_VALID_SIZE = 128;
29 static const int32_t DEVICE_CHANGE_VALID_SIZE = 128;
30 static const int32_t PREFERRED_DEVICE_VALID_SIZE = 128;
31 static const int32_t STATE_VALID_SIZE = 1024;
32 static const int32_t MIC_BLOCKED_VALID_SIZE = 128;
33
AudioPolicyClientStub()34 AudioPolicyClientStub::AudioPolicyClientStub()
35 {}
36
~AudioPolicyClientStub()37 AudioPolicyClientStub::~AudioPolicyClientStub()
38 {}
39
OnFirMaxRemoteRequest(uint32_t updateCode,MessageParcel & data,MessageParcel & reply)40 void AudioPolicyClientStub::OnFirMaxRemoteRequest(uint32_t updateCode, MessageParcel &data, MessageParcel &reply)
41 {
42 switch (updateCode) {
43 case static_cast<uint32_t>(AudioPolicyClientCode::ON_HEAD_TRACKING_DEVICE_CHANGE):
44 HandleHeadTrackingDeviceChange(data, reply);
45 break;
46 case static_cast<uint32_t>(AudioPolicyClientCode::ON_SPATIALIZATION_ENABLED_CHANGE):
47 HandleSpatializationEnabledChange(data, reply);
48 break;
49 case static_cast<uint32_t>(AudioPolicyClientCode::ON_SPATIALIZATION_ENABLED_CHANGE_FOR_ANY_DEVICE):
50 HandleSpatializationEnabledChangeForAnyDevice(data, reply);
51 break;
52 case static_cast<uint32_t>(AudioPolicyClientCode::ON_HEAD_TRACKING_ENABLED_CHANGE):
53 HandleHeadTrackingEnabledChange(data, reply);
54 break;
55 case static_cast<uint32_t>(AudioPolicyClientCode::ON_HEAD_TRACKING_ENABLED_CHANGE_FOR_ANY_DEVICE):
56 HandleHeadTrackingEnabledChangeForAnyDevice(data, reply);
57 break;
58 case static_cast<uint32_t>(AudioPolicyClientCode::ON_AUDIO_SESSION_DEACTIVE):
59 HandleAudioSessionCallback(data, reply);
60 break;
61 default:
62 break;
63 }
64 }
65
OnMaxRemoteRequest(uint32_t updateCode,MessageParcel & data,MessageParcel & reply)66 void AudioPolicyClientStub::OnMaxRemoteRequest(uint32_t updateCode, MessageParcel &data, MessageParcel &reply)
67 {
68 switch (updateCode) {
69 case static_cast<uint32_t>(AudioPolicyClientCode::ON_RINGERMODE_UPDATE):
70 HandleRingerModeUpdated(data, reply);
71 break;
72 case static_cast<uint32_t>(AudioPolicyClientCode::ON_MIC_STATE_UPDATED):
73 HandleMicStateChange(data, reply);
74 break;
75 case static_cast<uint32_t>(AudioPolicyClientCode::ON_ACTIVE_OUTPUT_DEVICE_UPDATED):
76 HandlePreferredOutputDeviceUpdated(data, reply);
77 break;
78 case static_cast<uint32_t>(AudioPolicyClientCode::ON_ACTIVE_INPUT_DEVICE_UPDATED):
79 HandlePreferredInputDeviceUpdated(data, reply);
80 break;
81 case static_cast<uint32_t>(AudioPolicyClientCode::ON_RENDERERSTATE_CHANGE):
82 HandleRendererStateChange(data, reply);
83 break;
84 case static_cast<uint32_t>(AudioPolicyClientCode::ON_CAPTURERSTATE_CHANGE):
85 HandleCapturerStateChange(data, reply);
86 break;
87 case static_cast<uint32_t>(AudioPolicyClientCode::ON_RENDERER_DEVICE_CHANGE):
88 HandleRendererDeviceChange(data, reply);
89 break;
90 case static_cast<uint32_t>(AudioPolicyClientCode::ON_RECREATE_RENDERER_STREAM_EVENT):
91 HandleRecreateRendererStreamEvent(data, reply);
92 break;
93 case static_cast<uint32_t>(AudioPolicyClientCode::ON_RECREATE_CAPTURER_STREAM_EVENT):
94 HandleRecreateCapturerStreamEvent(data, reply);
95 break;
96 default:
97 OnFirMaxRemoteRequest(updateCode, data, reply);
98 break;
99 }
100 }
101
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)102 int AudioPolicyClientStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
103 MessageOption &option)
104 {
105 if (data.ReadInterfaceToken() != GetDescriptor()) {
106 AUDIO_ERR_LOG("AudioPolicyClientStub: ReadInterfaceToken failed");
107 return -1;
108 }
109 switch (code) {
110 case UPDATE_CALLBACK_CLIENT: {
111 uint32_t updateCode = static_cast<uint32_t>(data.ReadInt32());
112 if (updateCode > static_cast<uint32_t>(AudioPolicyClientCode::AUDIO_POLICY_CLIENT_CODE_MAX)) {
113 return -1;
114 }
115 switch (updateCode) {
116 case static_cast<uint32_t>(AudioPolicyClientCode::ON_VOLUME_KEY_EVENT):
117 HandleVolumeKeyEvent(data, reply);
118 break;
119 case static_cast<uint32_t>(AudioPolicyClientCode::ON_FOCUS_INFO_CHANGED):
120 HandleAudioFocusInfoChange(data, reply);
121 break;
122 case static_cast<uint32_t>(AudioPolicyClientCode::ON_FOCUS_REQUEST_CHANGED):
123 HandleAudioFocusRequested(data, reply);
124 break;
125 case static_cast<uint32_t>(AudioPolicyClientCode::ON_FOCUS_ABANDON_CHANGED):
126 HandleAudioFocusAbandoned(data, reply);
127 break;
128 case static_cast<uint32_t>(AudioPolicyClientCode::ON_DEVICE_CHANGE):
129 HandleDeviceChange(data, reply);
130 break;
131 case static_cast<uint32_t>(AudioPolicyClientCode::ON_MICRO_PHONE_BLOCKED):
132 HandleMicrophoneBlocked(data, reply);
133 break;
134 default:
135 OnMaxRemoteRequest(updateCode, data, reply);
136 break;
137 }
138 break;
139 }
140 default: {
141 reply.WriteInt32(ERR_INVALID_OPERATION);
142 break;
143 }
144 }
145 return SUCCESS;
146 }
147
HandleVolumeKeyEvent(MessageParcel & data,MessageParcel & reply)148 void AudioPolicyClientStub::HandleVolumeKeyEvent(MessageParcel &data, MessageParcel &reply)
149 {
150 VolumeEvent event;
151 event.volumeType = static_cast<AudioStreamType>(data.ReadInt32());
152 event.volume = data.ReadInt32();
153 event.updateUi = data.ReadBool();
154 event.volumeGroupId = data.ReadInt32();
155 event.networkId = data.ReadString();
156 OnVolumeKeyEvent(event);
157 }
158
HandleAudioFocusInfoChange(MessageParcel & data,MessageParcel & reply)159 void AudioPolicyClientStub::HandleAudioFocusInfoChange(MessageParcel &data, MessageParcel &reply)
160 {
161 std::list<std::pair<AudioInterrupt, AudioFocuState>> infoList;
162 std::pair<AudioInterrupt, AudioFocuState> focusInfo = {};
163 int32_t size = data.ReadInt32();
164 CHECK_AND_RETURN_LOG(size < FOCUS_INFO_VALID_SIZE, "get invalid size : %{public}d", size);
165
166 for (int32_t i = 0; i < size; i++) {
167 AudioInterrupt::Unmarshalling(data, focusInfo.first);
168 focusInfo.second = static_cast<AudioFocuState>(data.ReadInt32());
169 infoList.emplace_back(focusInfo);
170 }
171 OnAudioFocusInfoChange(infoList);
172 }
173
HandleAudioFocusRequested(MessageParcel & data,MessageParcel & reply)174 void AudioPolicyClientStub::HandleAudioFocusRequested(MessageParcel &data, MessageParcel &reply)
175 {
176 AudioInterrupt requestFocus = {};
177 AudioInterrupt::Unmarshalling(data, requestFocus);
178 OnAudioFocusRequested(requestFocus);
179 }
180
HandleAudioFocusAbandoned(MessageParcel & data,MessageParcel & reply)181 void AudioPolicyClientStub::HandleAudioFocusAbandoned(MessageParcel &data, MessageParcel &reply)
182 {
183 AudioInterrupt abandonFocus = {};
184 AudioInterrupt::Unmarshalling(data, abandonFocus);
185 OnAudioFocusAbandoned(abandonFocus);
186 }
187
HandleDeviceChange(MessageParcel & data,MessageParcel & reply)188 void AudioPolicyClientStub::HandleDeviceChange(MessageParcel &data, MessageParcel &reply)
189 {
190 DeviceChangeAction deviceChange;
191 deviceChange.type = static_cast<DeviceChangeType>(data.ReadUint32());
192 deviceChange.flag = static_cast<DeviceFlag>(data.ReadUint32());
193 int32_t size = data.ReadInt32();
194 CHECK_AND_RETURN_LOG(size < DEVICE_CHANGE_VALID_SIZE, "get invalid size : %{public}d", size);
195
196 for (int32_t i = 0; i < size; i++) {
197 deviceChange.deviceDescriptors.emplace_back(AudioDeviceDescriptor::Unmarshalling(data));
198 }
199 OnDeviceChange(deviceChange);
200 }
201
HandleMicrophoneBlocked(MessageParcel & data,MessageParcel & reply)202 void AudioPolicyClientStub::HandleMicrophoneBlocked(MessageParcel &data, MessageParcel &reply)
203 {
204 MicrophoneBlockedInfo microphoneBlocked;
205 microphoneBlocked.blockStatus = static_cast<DeviceBlockStatus>(data.ReadUint32());
206 int32_t size = data.ReadInt32();
207 CHECK_AND_RETURN_LOG(size < MIC_BLOCKED_VALID_SIZE, "get invalid size : %{public}d", size);
208
209 for (int32_t i = 0; i < size; i++) {
210 microphoneBlocked.devices.emplace_back(AudioDeviceDescriptor::Unmarshalling(data));
211 }
212 OnMicrophoneBlocked(microphoneBlocked);
213 }
214
HandleRingerModeUpdated(MessageParcel & data,MessageParcel & reply)215 void AudioPolicyClientStub::HandleRingerModeUpdated(MessageParcel &data, MessageParcel &reply)
216 {
217 AudioRingerMode ringMode = static_cast<AudioRingerMode>(data.ReadInt32());
218 OnRingerModeUpdated(ringMode);
219 }
220
HandleMicStateChange(MessageParcel & data,MessageParcel & reply)221 void AudioPolicyClientStub::HandleMicStateChange(MessageParcel &data, MessageParcel &reply)
222 {
223 MicStateChangeEvent micState;
224 micState.mute = data.ReadBool();
225 OnMicStateUpdated(micState);
226 }
227
HandlePreferredOutputDeviceUpdated(MessageParcel & data,MessageParcel & reply)228 void AudioPolicyClientStub::HandlePreferredOutputDeviceUpdated(MessageParcel &data, MessageParcel &reply)
229 {
230 std::vector<sptr<AudioDeviceDescriptor>> deviceDescriptor;
231 int32_t size = data.ReadInt32();
232 CHECK_AND_RETURN_LOG(size < PREFERRED_DEVICE_VALID_SIZE, "get invalid size : %{public}d", size);
233
234 for (int32_t i = 0; i < size; i++) {
235 deviceDescriptor.push_back(AudioDeviceDescriptor::Unmarshalling(data));
236 }
237 OnPreferredOutputDeviceUpdated(deviceDescriptor);
238 }
239
HandlePreferredInputDeviceUpdated(MessageParcel & data,MessageParcel & reply)240 void AudioPolicyClientStub::HandlePreferredInputDeviceUpdated(MessageParcel &data, MessageParcel &reply)
241 {
242 std::vector<sptr<AudioDeviceDescriptor>> deviceDescriptor;
243 int32_t size = data.ReadInt32();
244 CHECK_AND_RETURN_LOG(size < PREFERRED_DEVICE_VALID_SIZE, "get invalid size : %{public}d", size);
245
246 for (int32_t i = 0; i < size; i++) {
247 deviceDescriptor.push_back(AudioDeviceDescriptor::Unmarshalling(data));
248 }
249 OnPreferredInputDeviceUpdated(deviceDescriptor);
250 }
251
HandleRendererStateChange(MessageParcel & data,MessageParcel & reply)252 void AudioPolicyClientStub::HandleRendererStateChange(MessageParcel &data, MessageParcel &reply)
253 {
254 std::vector<std::unique_ptr<AudioRendererChangeInfo>> audioRenderChangeInfo;
255 int32_t size = data.ReadInt32();
256 Trace trace("HandleRendererStateChange size:" + std::to_string(size));
257 CHECK_AND_RETURN_LOG(size < STATE_VALID_SIZE, "get invalid size : %{public}d", size);
258
259 while (size > 0) {
260 std::unique_ptr<AudioRendererChangeInfo> rendererChangeInfo = std::make_unique<AudioRendererChangeInfo>();
261 if (rendererChangeInfo == nullptr) {
262 AUDIO_ERR_LOG("AudioPolicyClientStub::HandleRendererStateChange, No memory!");
263 return;
264 }
265 rendererChangeInfo->Unmarshalling(data);
266 audioRenderChangeInfo.push_back(move(rendererChangeInfo));
267 size--;
268 }
269 OnRendererStateChange(audioRenderChangeInfo);
270 }
271
HandleCapturerStateChange(MessageParcel & data,MessageParcel & reply)272 void AudioPolicyClientStub::HandleCapturerStateChange(MessageParcel &data, MessageParcel &reply)
273 {
274 std::vector<std::unique_ptr<AudioCapturerChangeInfo>> audioCapturerChangeInfo;
275 int32_t size = data.ReadInt32();
276 CHECK_AND_RETURN_LOG(size < STATE_VALID_SIZE, "get invalid size : %{public}d", size);
277
278 while (size > 0) {
279 std::unique_ptr<AudioCapturerChangeInfo> capturerChangeInfo = std::make_unique<AudioCapturerChangeInfo>();
280 if (capturerChangeInfo == nullptr) {
281 AUDIO_ERR_LOG("AudioPolicyClientStub::HandleCapturerStateChange, No memory!");
282 return;
283 }
284 capturerChangeInfo->Unmarshalling(data);
285 audioCapturerChangeInfo.push_back(move(capturerChangeInfo));
286 size--;
287 }
288 OnCapturerStateChange(audioCapturerChangeInfo);
289 }
290
HandleRendererDeviceChange(MessageParcel & data,MessageParcel & reply)291 void AudioPolicyClientStub::HandleRendererDeviceChange(MessageParcel &data, MessageParcel &reply)
292 {
293 const uint32_t sessionId = data.ReadUint32();
294 DeviceInfo deviceInfo;
295 deviceInfo.Unmarshalling(data);
296 const AudioStreamDeviceChangeReasonExt reason
297 = static_cast<AudioStreamDeviceChangeReasonExt::ExtEnum> (data.ReadInt32());
298
299 OnRendererDeviceChange(sessionId, deviceInfo, reason);
300 }
301
HandleRecreateRendererStreamEvent(MessageParcel & data,MessageParcel & reply)302 void AudioPolicyClientStub::HandleRecreateRendererStreamEvent(MessageParcel &data, MessageParcel &reply)
303 {
304 const uint32_t sessionId = data.ReadUint32();
305 const uint32_t streamFlag = data.ReadUint32();
306 const AudioStreamDeviceChangeReasonExt reason
307 = static_cast<AudioStreamDeviceChangeReasonExt::ExtEnum> (data.ReadInt32());
308 OnRecreateRendererStreamEvent(sessionId, streamFlag, reason);
309 }
310
HandleRecreateCapturerStreamEvent(MessageParcel & data,MessageParcel & reply)311 void AudioPolicyClientStub::HandleRecreateCapturerStreamEvent(MessageParcel &data, MessageParcel &reply)
312 {
313 const uint32_t sessionId = data.ReadUint32();
314 const uint32_t streamFlag = data.ReadUint32();
315 const AudioStreamDeviceChangeReasonExt reason
316 = static_cast<AudioStreamDeviceChangeReasonExt::ExtEnum> (data.ReadInt32());
317 OnRecreateCapturerStreamEvent(sessionId, streamFlag, reason);
318 }
319
HandleHeadTrackingDeviceChange(MessageParcel & data,MessageParcel & reply)320 void AudioPolicyClientStub::HandleHeadTrackingDeviceChange(MessageParcel &data, MessageParcel &reply)
321 {
322 std::unordered_map<std::string, bool> changeInfo;
323 int32_t size = data.ReadInt32();
324 CHECK_AND_RETURN_LOG(size < DEVICE_CHANGE_VALID_SIZE, "get invalid size : %{public}d", size);
325
326 while (size > 0) {
327 std::string macAddress = data.ReadString();
328 bool isHeadTrackingDataRequested = data.ReadBool();
329 changeInfo[macAddress] = isHeadTrackingDataRequested;
330 size--;
331 }
332 OnHeadTrackingDeviceChange(changeInfo);
333 }
334
HandleSpatializationEnabledChange(MessageParcel & data,MessageParcel & reply)335 void AudioPolicyClientStub::HandleSpatializationEnabledChange(MessageParcel &data, MessageParcel &reply)
336 {
337 bool enabled = data.ReadBool();
338 OnSpatializationEnabledChange(enabled);
339 }
340
HandleSpatializationEnabledChangeForAnyDevice(MessageParcel & data,MessageParcel & reply)341 void AudioPolicyClientStub::HandleSpatializationEnabledChangeForAnyDevice(MessageParcel &data, MessageParcel &reply)
342 {
343 sptr<AudioDeviceDescriptor> audioDeviceDescriptor = AudioDeviceDescriptor::Unmarshalling(data);
344 CHECK_AND_RETURN_LOG(audioDeviceDescriptor != nullptr, "Unmarshalling fail.");
345 bool enabled = data.ReadBool();
346 OnSpatializationEnabledChangeForAnyDevice(audioDeviceDescriptor, enabled);
347 }
348
HandleHeadTrackingEnabledChange(MessageParcel & data,MessageParcel & reply)349 void AudioPolicyClientStub::HandleHeadTrackingEnabledChange(MessageParcel &data, MessageParcel &reply)
350 {
351 bool enabled = data.ReadBool();
352 OnHeadTrackingEnabledChange(enabled);
353 }
354
HandleHeadTrackingEnabledChangeForAnyDevice(MessageParcel & data,MessageParcel & reply)355 void AudioPolicyClientStub::HandleHeadTrackingEnabledChangeForAnyDevice(MessageParcel &data, MessageParcel &reply)
356 {
357 sptr<AudioDeviceDescriptor> audioDeviceDescriptor = AudioDeviceDescriptor::Unmarshalling(data);
358 CHECK_AND_RETURN_LOG(audioDeviceDescriptor != nullptr, "Unmarshalling fail.");
359 bool enabled = data.ReadBool();
360 OnHeadTrackingEnabledChangeForAnyDevice(audioDeviceDescriptor, enabled);
361 }
362
HandleAudioSessionCallback(MessageParcel & data,MessageParcel & reply)363 void AudioPolicyClientStub::HandleAudioSessionCallback(MessageParcel &data, MessageParcel &reply)
364 {
365 AUDIO_INFO_LOG("HandleAudioSessionCallback");
366 AudioSessionDeactiveEvent deactiveEvent;
367 deactiveEvent.deactiveReason = static_cast<AudioSessionDeactiveReason>(data.ReadInt32());
368 OnAudioSessionDeactive(deactiveEvent);
369 }
370 } // namespace AudioStandard
371 } // namespace OHOS
372