• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SPATIALIZATION_ENABLED_CHANGE_FOR_CURRENT_DEVICE):
53             HandleSpatializationEnabledChangeForCurrentDevice(data, reply);
54             break;
55         case static_cast<uint32_t>(AudioPolicyClientCode::ON_HEAD_TRACKING_ENABLED_CHANGE):
56             HandleHeadTrackingEnabledChange(data, reply);
57             break;
58         case static_cast<uint32_t>(AudioPolicyClientCode::ON_HEAD_TRACKING_ENABLED_CHANGE_FOR_ANY_DEVICE):
59             HandleHeadTrackingEnabledChangeForAnyDevice(data, reply);
60             break;
61         case static_cast<uint32_t>(AudioPolicyClientCode::ON_NN_STATE_CHANGE):
62             HandleNnStateChange(data, reply);
63             break;
64         case static_cast<uint32_t>(AudioPolicyClientCode::ON_AUDIO_SESSION_DEACTIVE):
65             HandleAudioSessionCallback(data, reply);
66             break;
67         case static_cast<uint32_t>(AudioPolicyClientCode::ON_AUDIO_SCENE_CHANGED):
68             HandleAudioSceneChange(data, reply);
69             break;
70         default:
71             break;
72     }
73 }
74 
OnMaxRemoteRequest(uint32_t updateCode,MessageParcel & data,MessageParcel & reply)75 void AudioPolicyClientStub::OnMaxRemoteRequest(uint32_t updateCode, MessageParcel &data, MessageParcel &reply)
76 {
77     switch (updateCode) {
78         case static_cast<uint32_t>(AudioPolicyClientCode::ON_RINGERMODE_UPDATE):
79             HandleRingerModeUpdated(data, reply);
80             break;
81         case static_cast<uint32_t>(AudioPolicyClientCode::ON_MIC_STATE_UPDATED):
82             HandleMicStateChange(data, reply);
83             break;
84         case static_cast<uint32_t>(AudioPolicyClientCode::ON_ACTIVE_OUTPUT_DEVICE_UPDATED):
85             HandlePreferredOutputDeviceUpdated(data, reply);
86             break;
87         case static_cast<uint32_t>(AudioPolicyClientCode::ON_ACTIVE_INPUT_DEVICE_UPDATED):
88             HandlePreferredInputDeviceUpdated(data, reply);
89             break;
90         case static_cast<uint32_t>(AudioPolicyClientCode::ON_RENDERERSTATE_CHANGE):
91             HandleRendererStateChange(data, reply);
92             break;
93         case static_cast<uint32_t>(AudioPolicyClientCode::ON_CAPTURERSTATE_CHANGE):
94             HandleCapturerStateChange(data, reply);
95             break;
96         case static_cast<uint32_t>(AudioPolicyClientCode::ON_RENDERER_DEVICE_CHANGE):
97             HandleRendererDeviceChange(data, reply);
98             break;
99         case static_cast<uint32_t>(AudioPolicyClientCode::ON_DISTRIBUTED_OUTPUT_CHANGE):
100             HandleDistribuitedOutputChange(data, reply);
101             break;
102         case static_cast<uint32_t>(AudioPolicyClientCode::ON_RECREATE_RENDERER_STREAM_EVENT):
103             HandleRecreateRendererStreamEvent(data, reply);
104             break;
105         case static_cast<uint32_t>(AudioPolicyClientCode::ON_RECREATE_CAPTURER_STREAM_EVENT):
106             HandleRecreateCapturerStreamEvent(data, reply);
107             break;
108         default:
109             OnFirMaxRemoteRequest(updateCode, data, reply);
110             break;
111     }
112 }
113 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)114 int AudioPolicyClientStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
115     MessageOption &option)
116 {
117     if (data.ReadInterfaceToken() != GetDescriptor()) {
118         AUDIO_ERR_LOG("AudioPolicyClientStub: ReadInterfaceToken failed");
119         return -1;
120     }
121     switch (code) {
122         case UPDATE_CALLBACK_CLIENT: {
123             uint32_t updateCode = static_cast<uint32_t>(data.ReadInt32());
124             if (updateCode > static_cast<uint32_t>(AudioPolicyClientCode::AUDIO_POLICY_CLIENT_CODE_MAX)) {
125                 return -1;
126             }
127             switch (updateCode) {
128                 case static_cast<uint32_t>(AudioPolicyClientCode::ON_VOLUME_KEY_EVENT):
129                     HandleVolumeKeyEvent(data, reply);
130                     break;
131                 case static_cast<uint32_t>(AudioPolicyClientCode::ON_FOCUS_INFO_CHANGED):
132                     HandleAudioFocusInfoChange(data, reply);
133                     break;
134                 case static_cast<uint32_t>(AudioPolicyClientCode::ON_FOCUS_REQUEST_CHANGED):
135                     HandleAudioFocusRequested(data, reply);
136                     break;
137                 case static_cast<uint32_t>(AudioPolicyClientCode::ON_FOCUS_ABANDON_CHANGED):
138                     HandleAudioFocusAbandoned(data, reply);
139                     break;
140                 case static_cast<uint32_t>(AudioPolicyClientCode::ON_DEVICE_CHANGE):
141                     HandleDeviceChange(data, reply);
142                     break;
143                 case static_cast<uint32_t>(AudioPolicyClientCode::ON_MICRO_PHONE_BLOCKED):
144                     HandleMicrophoneBlocked(data, reply);
145                     break;
146                 case static_cast<uint32_t>(AudioPolicyClientCode::ON_APP_VOLUME_CHANGE):
147                     HandleAppVolumeChange(data, reply);
148                     break;
149                 default:
150                     OnMaxRemoteRequest(updateCode, data, reply);
151                     break;
152             }
153             break;
154         }
155         default: {
156             reply.WriteInt32(ERR_INVALID_OPERATION);
157             break;
158         }
159     }
160     return SUCCESS;
161 }
162 
HandleAppVolumeChange(MessageParcel & data,MessageParcel & reply)163 void AudioPolicyClientStub::HandleAppVolumeChange(MessageParcel &data, MessageParcel &reply)
164 {
165     AUDIO_INFO_LOG("Handle AppVolume Change");
166     int32_t appUid = data.ReadInt32();
167     VolumeEvent volumeEvent;
168     volumeEvent.Unmarshalling(data);
169     OnAppVolumeChanged(appUid, volumeEvent);
170 }
171 
HandleVolumeKeyEvent(MessageParcel & data,MessageParcel & reply)172 void AudioPolicyClientStub::HandleVolumeKeyEvent(MessageParcel &data, MessageParcel &reply)
173 {
174     VolumeEvent event;
175     event.volumeType = static_cast<AudioStreamType>(data.ReadInt32());
176     event.volume = data.ReadInt32();
177     event.updateUi = data.ReadBool();
178     event.volumeGroupId = data.ReadInt32();
179     event.networkId = data.ReadString();
180     OnVolumeKeyEvent(event);
181 }
182 
HandleAudioFocusInfoChange(MessageParcel & data,MessageParcel & reply)183 void AudioPolicyClientStub::HandleAudioFocusInfoChange(MessageParcel &data, MessageParcel &reply)
184 {
185     std::list<std::pair<AudioInterrupt, AudioFocuState>> infoList;
186     std::pair<AudioInterrupt, AudioFocuState> focusInfo = {};
187     int32_t size = data.ReadInt32();
188     CHECK_AND_RETURN_LOG(size < FOCUS_INFO_VALID_SIZE, "get invalid size : %{public}d", size);
189 
190     for (int32_t i = 0; i < size; i++) {
191         AudioInterrupt::Unmarshalling(data, focusInfo.first);
192         focusInfo.second = static_cast<AudioFocuState>(data.ReadInt32());
193         infoList.emplace_back(focusInfo);
194     }
195     OnAudioFocusInfoChange(infoList);
196 }
197 
HandleAudioFocusRequested(MessageParcel & data,MessageParcel & reply)198 void AudioPolicyClientStub::HandleAudioFocusRequested(MessageParcel &data, MessageParcel &reply)
199 {
200     AudioInterrupt requestFocus = {};
201     AudioInterrupt::Unmarshalling(data, requestFocus);
202     OnAudioFocusRequested(requestFocus);
203 }
204 
HandleAudioFocusAbandoned(MessageParcel & data,MessageParcel & reply)205 void AudioPolicyClientStub::HandleAudioFocusAbandoned(MessageParcel &data, MessageParcel &reply)
206 {
207     AudioInterrupt abandonFocus = {};
208     AudioInterrupt::Unmarshalling(data, abandonFocus);
209     OnAudioFocusAbandoned(abandonFocus);
210 }
211 
HandleDeviceChange(MessageParcel & data,MessageParcel & reply)212 void AudioPolicyClientStub::HandleDeviceChange(MessageParcel &data, MessageParcel &reply)
213 {
214     DeviceChangeAction deviceChange;
215     deviceChange.type = static_cast<DeviceChangeType>(data.ReadUint32());
216     deviceChange.flag = static_cast<DeviceFlag>(data.ReadUint32());
217     int32_t size = data.ReadInt32();
218     CHECK_AND_RETURN_LOG(size < DEVICE_CHANGE_VALID_SIZE, "get invalid size : %{public}d", size);
219 
220     for (int32_t i = 0; i < size; i++) {
221         deviceChange.deviceDescriptors.emplace_back(AudioDeviceDescriptor::UnmarshallingPtr(data));
222     }
223     OnDeviceChange(deviceChange);
224 }
225 
HandleDistribuitedOutputChange(MessageParcel & data,MessageParcel & reply)226 void AudioPolicyClientStub::HandleDistribuitedOutputChange(MessageParcel &data, MessageParcel &reply)
227 {
228     auto descDesc = AudioDeviceDescriptor::UnmarshallingPtr(data);
229     CHECK_AND_RETURN_LOG(descDesc, "descDesc is nullptr");
230     bool isRemote = data.ReadBool();
231     OnDistribuitedOutputChange(*descDesc, isRemote);
232 }
233 
HandleMicrophoneBlocked(MessageParcel & data,MessageParcel & reply)234 void AudioPolicyClientStub::HandleMicrophoneBlocked(MessageParcel &data, MessageParcel &reply)
235 {
236     MicrophoneBlockedInfo microphoneBlocked;
237     microphoneBlocked.blockStatus = static_cast<DeviceBlockStatus>(data.ReadUint32());
238     int32_t size = data.ReadInt32();
239     CHECK_AND_RETURN_LOG(size < MIC_BLOCKED_VALID_SIZE, "get invalid size : %{public}d", size);
240 
241     for (int32_t i = 0; i < size; i++) {
242         microphoneBlocked.devices.emplace_back(AudioDeviceDescriptor::UnmarshallingPtr(data));
243     }
244     OnMicrophoneBlocked(microphoneBlocked);
245 }
246 
HandleRingerModeUpdated(MessageParcel & data,MessageParcel & reply)247 void AudioPolicyClientStub::HandleRingerModeUpdated(MessageParcel &data, MessageParcel &reply)
248 {
249     AudioRingerMode ringMode = static_cast<AudioRingerMode>(data.ReadInt32());
250     OnRingerModeUpdated(ringMode);
251 }
252 
HandleMicStateChange(MessageParcel & data,MessageParcel & reply)253 void AudioPolicyClientStub::HandleMicStateChange(MessageParcel &data, MessageParcel &reply)
254 {
255     MicStateChangeEvent micState;
256     micState.mute = data.ReadBool();
257     OnMicStateUpdated(micState);
258 }
259 
HandlePreferredOutputDeviceUpdated(MessageParcel & data,MessageParcel & reply)260 void AudioPolicyClientStub::HandlePreferredOutputDeviceUpdated(MessageParcel &data, MessageParcel &reply)
261 {
262     AudioRendererInfo rendererInfo;
263     rendererInfo.Unmarshalling(data);
264     std::vector<std::shared_ptr<AudioDeviceDescriptor>> deviceDescriptor;
265     int32_t size = data.ReadInt32();
266     CHECK_AND_RETURN_LOG(size < PREFERRED_DEVICE_VALID_SIZE, "get invalid size : %{public}d", size);
267 
268     for (int32_t i = 0; i < size; i++) {
269         deviceDescriptor.push_back(AudioDeviceDescriptor::UnmarshallingPtr(data));
270     }
271     OnPreferredOutputDeviceUpdated(rendererInfo, deviceDescriptor);
272 }
273 
HandlePreferredInputDeviceUpdated(MessageParcel & data,MessageParcel & reply)274 void AudioPolicyClientStub::HandlePreferredInputDeviceUpdated(MessageParcel &data, MessageParcel &reply)
275 {
276     AudioCapturerInfo capturerInfo;
277     capturerInfo.Unmarshalling(data);
278     std::vector<std::shared_ptr<AudioDeviceDescriptor>> deviceDescriptor;
279     int32_t size = data.ReadInt32();
280     CHECK_AND_RETURN_LOG(size < PREFERRED_DEVICE_VALID_SIZE, "get invalid size : %{public}d", size);
281 
282     for (int32_t i = 0; i < size; i++) {
283         deviceDescriptor.push_back(AudioDeviceDescriptor::UnmarshallingPtr(data));
284     }
285     OnPreferredInputDeviceUpdated(capturerInfo, deviceDescriptor);
286 }
287 
HandleRendererStateChange(MessageParcel & data,MessageParcel & reply)288 void AudioPolicyClientStub::HandleRendererStateChange(MessageParcel &data, MessageParcel &reply)
289 {
290     std::vector<std::shared_ptr<AudioRendererChangeInfo>> audioRenderChangeInfo;
291     int32_t size = data.ReadInt32();
292     Trace trace("HandleRendererStateChange size:" + std::to_string(size));
293     CHECK_AND_RETURN_LOG(size < STATE_VALID_SIZE, "get invalid size : %{public}d", size);
294 
295     while (size > 0) {
296         std::shared_ptr<AudioRendererChangeInfo> rendererChangeInfo = std::make_shared<AudioRendererChangeInfo>();
297         if (rendererChangeInfo == nullptr) {
298             AUDIO_ERR_LOG("AudioPolicyClientStub::HandleRendererStateChange, No memory!");
299             return;
300         }
301         rendererChangeInfo->Unmarshalling(data);
302         audioRenderChangeInfo.push_back(move(rendererChangeInfo));
303         size--;
304     }
305     OnRendererStateChange(audioRenderChangeInfo);
306 }
307 
HandleCapturerStateChange(MessageParcel & data,MessageParcel & reply)308 void AudioPolicyClientStub::HandleCapturerStateChange(MessageParcel &data, MessageParcel &reply)
309 {
310     std::vector<std::shared_ptr<AudioCapturerChangeInfo>> audioCapturerChangeInfo;
311     int32_t size = data.ReadInt32();
312     CHECK_AND_RETURN_LOG(size < STATE_VALID_SIZE, "get invalid size : %{public}d", size);
313 
314     while (size > 0) {
315         std::shared_ptr<AudioCapturerChangeInfo> capturerChangeInfo = std::make_shared<AudioCapturerChangeInfo>();
316         if (capturerChangeInfo == nullptr) {
317             AUDIO_ERR_LOG("AudioPolicyClientStub::HandleCapturerStateChange, No memory!");
318             return;
319         }
320         capturerChangeInfo->Unmarshalling(data);
321         audioCapturerChangeInfo.push_back(move(capturerChangeInfo));
322         size--;
323     }
324     OnCapturerStateChange(audioCapturerChangeInfo);
325 }
326 
HandleRendererDeviceChange(MessageParcel & data,MessageParcel & reply)327 void AudioPolicyClientStub::HandleRendererDeviceChange(MessageParcel &data, MessageParcel &reply)
328 {
329     const uint32_t sessionId = data.ReadUint32();
330     AudioDeviceDescriptor deviceInfo(AudioDeviceDescriptor::DEVICE_INFO);
331     deviceInfo.Unmarshalling(data);
332     const AudioStreamDeviceChangeReasonExt reason
333         = static_cast<AudioStreamDeviceChangeReasonExt::ExtEnum> (data.ReadInt32());
334 
335     OnRendererDeviceChange(sessionId, deviceInfo, reason);
336 }
337 
HandleRecreateRendererStreamEvent(MessageParcel & data,MessageParcel & reply)338 void AudioPolicyClientStub::HandleRecreateRendererStreamEvent(MessageParcel &data, MessageParcel &reply)
339 {
340     const uint32_t sessionId = data.ReadUint32();
341     const uint32_t streamFlag = data.ReadUint32();
342     const AudioStreamDeviceChangeReasonExt reason
343         = static_cast<AudioStreamDeviceChangeReasonExt::ExtEnum> (data.ReadInt32());
344     OnRecreateRendererStreamEvent(sessionId, streamFlag, reason);
345 }
346 
HandleRecreateCapturerStreamEvent(MessageParcel & data,MessageParcel & reply)347 void AudioPolicyClientStub::HandleRecreateCapturerStreamEvent(MessageParcel &data, MessageParcel &reply)
348 {
349     const uint32_t sessionId = data.ReadUint32();
350     const uint32_t streamFlag = data.ReadUint32();
351     const AudioStreamDeviceChangeReasonExt reason
352         = static_cast<AudioStreamDeviceChangeReasonExt::ExtEnum> (data.ReadInt32());
353     OnRecreateCapturerStreamEvent(sessionId, streamFlag, reason);
354 }
355 
HandleHeadTrackingDeviceChange(MessageParcel & data,MessageParcel & reply)356 void AudioPolicyClientStub::HandleHeadTrackingDeviceChange(MessageParcel &data, MessageParcel &reply)
357 {
358     std::unordered_map<std::string, bool> changeInfo;
359     int32_t size = data.ReadInt32();
360     CHECK_AND_RETURN_LOG(size < DEVICE_CHANGE_VALID_SIZE, "get invalid size : %{public}d", size);
361 
362     while (size > 0) {
363         std::string macAddress = data.ReadString();
364         bool isHeadTrackingDataRequested = data.ReadBool();
365         changeInfo[macAddress] = isHeadTrackingDataRequested;
366         size--;
367     }
368     OnHeadTrackingDeviceChange(changeInfo);
369 }
370 
HandleSpatializationEnabledChange(MessageParcel & data,MessageParcel & reply)371 void AudioPolicyClientStub::HandleSpatializationEnabledChange(MessageParcel &data, MessageParcel &reply)
372 {
373     bool enabled = data.ReadBool();
374     OnSpatializationEnabledChange(enabled);
375 }
376 
HandleAudioSceneChange(MessageParcel & data,MessageParcel & reply)377 void AudioPolicyClientStub::HandleAudioSceneChange(MessageParcel &data, MessageParcel &reply)
378 {
379     AudioScene audioScene = static_cast<AudioScene>(data.ReadInt32());
380     CHECK_AND_RETURN_LOG(audioScene < AUDIO_SCENE_MAX && audioScene > AUDIO_SCENE_INVALID, \
381         "get invalid audioScene : %{public}d", audioScene);
382 
383     OnAudioSceneChange(audioScene);
384 }
385 
HandleSpatializationEnabledChangeForAnyDevice(MessageParcel & data,MessageParcel & reply)386 void AudioPolicyClientStub::HandleSpatializationEnabledChangeForAnyDevice(MessageParcel &data, MessageParcel &reply)
387 {
388     std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor = AudioDeviceDescriptor::UnmarshallingPtr(data);
389     CHECK_AND_RETURN_LOG(audioDeviceDescriptor != nullptr, "Unmarshalling fail.");
390     bool enabled = data.ReadBool();
391     OnSpatializationEnabledChangeForAnyDevice(audioDeviceDescriptor, enabled);
392 }
393 
HandleSpatializationEnabledChangeForCurrentDevice(MessageParcel & data,MessageParcel & reply)394 void AudioPolicyClientStub::HandleSpatializationEnabledChangeForCurrentDevice(MessageParcel &data, MessageParcel &reply)
395 {
396     bool enabled = data.ReadBool();
397     OnSpatializationEnabledChangeForCurrentDevice(enabled);
398 }
399 
HandleHeadTrackingEnabledChange(MessageParcel & data,MessageParcel & reply)400 void AudioPolicyClientStub::HandleHeadTrackingEnabledChange(MessageParcel &data, MessageParcel &reply)
401 {
402     bool enabled = data.ReadBool();
403     OnHeadTrackingEnabledChange(enabled);
404 }
405 
HandleHeadTrackingEnabledChangeForAnyDevice(MessageParcel & data,MessageParcel & reply)406 void AudioPolicyClientStub::HandleHeadTrackingEnabledChangeForAnyDevice(MessageParcel &data, MessageParcel &reply)
407 {
408     std::shared_ptr<AudioDeviceDescriptor> audioDeviceDescriptor = AudioDeviceDescriptor::UnmarshallingPtr(data);
409     CHECK_AND_RETURN_LOG(audioDeviceDescriptor != nullptr, "Unmarshalling fail.");
410     bool enabled = data.ReadBool();
411     OnHeadTrackingEnabledChangeForAnyDevice(audioDeviceDescriptor, enabled);
412 }
413 
HandleNnStateChange(MessageParcel & data,MessageParcel & reply)414 void AudioPolicyClientStub::HandleNnStateChange(MessageParcel &data, MessageParcel &reply)
415 {
416     int32_t state = data.ReadInt32();
417     OnNnStateChange(state);
418 }
419 
HandleAudioSessionCallback(MessageParcel & data,MessageParcel & reply)420 void AudioPolicyClientStub::HandleAudioSessionCallback(MessageParcel &data, MessageParcel &reply)
421 {
422     AUDIO_INFO_LOG("HandleAudioSessionCallback");
423     AudioSessionDeactiveEvent deactiveEvent;
424     deactiveEvent.deactiveReason = static_cast<AudioSessionDeactiveReason>(data.ReadInt32());
425     OnAudioSessionDeactive(deactiveEvent);
426 }
427 } // namespace AudioStandard
428 } // namespace OHOS
429