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
16 #include "event_aggregate.h"
17 #include "log.h"
18 #include "media_monitor_info.h"
19 #include "monitor_utils.h"
20 #include "audio_system_manager.h"
21 #include "audio_device_info.h"
22
23 #include "iservice_registry.h"
24
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FOUNDATION, "EventAggregate"};
27 }
28
29 using OHOS::AudioStandard::AudioSystemManager;
30
31 namespace OHOS {
32 namespace Media {
33 namespace MediaMonitor {
34
35 static constexpr int32_t NEED_INCREASE_FREQUENCY = 30;
36 static constexpr int32_t UNINITIALIZED = -1;
37
EventAggregate()38 EventAggregate::EventAggregate()
39 :audioMemo_(AudioMemo::GetAudioMemo()),
40 mediaMonitorPolicy_(MediaMonitorPolicy::GetMediaMonitorPolicy())
41 {
42 MEDIA_LOG_D("EventAggregate Constructor");
43 }
44
~EventAggregate()45 EventAggregate::~EventAggregate()
46 {
47 MEDIA_LOG_D("EventAggregate Destructor");
48 }
49
WriteEvent(std::shared_ptr<EventBean> & bean)50 void EventAggregate::WriteEvent(std::shared_ptr<EventBean> &bean)
51 {
52 MEDIA_LOG_D("WriteEvent enter");
53 if (bean == nullptr) {
54 MEDIA_LOG_E("eventBean is nullptr");
55 return;
56 }
57
58 EventId eventId = bean->GetEventId();
59 switch (eventId) {
60 case HEADSET_CHANGE:
61 case AUDIO_ROUTE_CHANGE:
62 case LOAD_CONFIG_ERROR:
63 case APP_WRITE_MUTE:
64 case HDI_EXCEPTION:
65 case AUDIO_SERVICE_STARTUP_ERROR:
66 case STREAM_STANDBY:
67 case SMARTPA_STATUS:
68 case DB_ACCESS_EXCEPTION:
69 case DEVICE_CHANGE_EXCEPTION:
70 case AI_VOICE_NOISE_SUPPRESSION:
71 case LOAD_EFFECT_ENGINE_ERROR:
72 case SYSTEM_TONE_PLAYBACK:
73 case ADD_REMOVE_CUSTOMIZED_TONE:
74 case RECORD_ERROR:
75 case STREAM_OCCUPANCY:
76 mediaMonitorPolicy_.WriteEvent(bean->GetEventId(), bean);
77 break;
78 default:
79 UpdateAggregateEventList(bean);
80 break;
81 }
82 }
83
UpdateAggregateEventList(std::shared_ptr<EventBean> & bean)84 void EventAggregate::UpdateAggregateEventList(std::shared_ptr<EventBean> &bean)
85 {
86 MEDIA_LOG_D("Update Aggregate Event List");
87 EventId eventId = bean->GetEventId();
88 switch (eventId) {
89 case DEVICE_CHANGE:
90 HandleDeviceChangeEvent(bean);
91 break;
92 case STREAM_CHANGE:
93 HandleStreamChangeEvent(bean);
94 break;
95 case AUDIO_STREAM_EXHAUSTED_STATS:
96 HandleStreamExhaustedErrorEvent(bean);
97 break;
98 case AUDIO_STREAM_CREATE_ERROR_STATS:
99 HandleStreamCreateErrorEvent(bean);
100 break;
101 case BACKGROUND_SILENT_PLAYBACK:
102 HandleBackgroundSilentPlayback(bean);
103 break;
104 case PERFORMANCE_UNDER_OVERRUN_STATS:
105 HandleUnderrunStatistic(bean);
106 break;
107 case SET_FORCE_USE_AUDIO_DEVICE:
108 HandleForceUseDevice(bean);
109 break;
110 case CAPTURE_MUTE_STATUS_CHANGE:
111 HandleCaptureMutedStatusChange(bean);
112 break;
113 case VOLUME_CHANGE:
114 HandleVolumeChange(bean);
115 break;
116 case AUDIO_PIPE_CHANGE:
117 HandlePipeChange(bean);
118 break;
119 case AUDIO_FOCUS_MIGRATE:
120 HandleFocusMigrate(bean);
121 break;
122 case JANK_PLAYBACK:
123 HandleJankPlaybackEvent(bean);
124 break;
125 case EXCLUDE_OUTPUT_DEVICE:
126 HandleExcludedOutputDevices(bean);
127 break;
128 default:
129 break;
130 }
131 }
132
HandleDeviceChangeEvent(std::shared_ptr<EventBean> & bean)133 void EventAggregate::HandleDeviceChangeEvent(std::shared_ptr<EventBean> &bean)
134 {
135 MEDIA_LOG_D("Begin handle device change event");
136 HandleDeviceChangeForDeviceUsage(bean);
137 HandleDeviceChangeForCaptureMuted(bean);
138 HandleDeviceChangeForVolume(bean);
139 mediaMonitorPolicy_.WriteEvent(bean->GetEventId(), bean);
140 }
141
HandleDeviceChangeForDeviceUsage(std::shared_ptr<EventBean> & bean)142 void EventAggregate::HandleDeviceChangeForDeviceUsage(std::shared_ptr<EventBean> &bean)
143 {
144 MEDIA_LOG_D("Handle device change for device event aggregate.");
145 auto isExist = [&bean](const std::shared_ptr<EventBean> &deviceUsageBean) {
146 if (bean->GetIntValue("ISOUTPUT") == deviceUsageBean->GetIntValue("IS_PLAYBACK") &&
147 bean->GetIntValue("STREAMID") == deviceUsageBean->GetIntValue("STREAMID")) {
148 MEDIA_LOG_D("Find the existing device usage");
149 return true;
150 }
151 return false;
152 };
153 auto it = std::find_if(deviceUsageVector_.begin(), deviceUsageVector_.end(), isExist);
154 if (it != deviceUsageVector_.end()) {
155 bean->Add("STREAM_TYPE", (*it)->GetIntValue("STREAM_TYPE"));
156 HandleDeviceChangeForDuration(FOR_DEVICE_EVENT, bean, *it);
157 }
158 }
159
HandleDeviceChangeForCaptureMuted(std::shared_ptr<EventBean> & bean)160 void EventAggregate::HandleDeviceChangeForCaptureMuted(std::shared_ptr<EventBean> &bean)
161 {
162 MEDIA_LOG_D("Handle device change for capture muted event aggregate");
163 if (bean->GetIntValue("ISOUTPUT")) {
164 MEDIA_LOG_W("HandleDeviceChangeForCaptureMuted is playback");
165 return;
166 }
167 auto isExist = [&bean](const std::shared_ptr<EventBean> &captureMutedBean) {
168 if (bean->GetIntValue("STREAMID") == captureMutedBean->GetIntValue("STREAMID")) {
169 MEDIA_LOG_D("Find the existing capture muted");
170 return true;
171 }
172 return false;
173 };
174 auto it = std::find_if(captureMutedVector_.begin(), captureMutedVector_.end(), isExist);
175 if (it != captureMutedVector_.end() && (*it)->GetIntValue("MUTED")) {
176 HandleDeviceChangeForDuration(FOR_CAPTURE_MUTE_EVENT, bean, *it);
177 }
178 }
179
HandleDeviceChangeForVolume(std::shared_ptr<EventBean> & bean)180 void EventAggregate::HandleDeviceChangeForVolume(std::shared_ptr<EventBean> &bean)
181 {
182 MEDIA_LOG_D("Handle device change for volume event aggregate");
183 if (!bean->GetIntValue("ISOUTPUT")) {
184 MEDIA_LOG_D("HandleDeviceChangeForVolume is not playback");
185 return;
186 }
187 auto isExist = [&bean](const std::shared_ptr<EventBean> &volumeBean) {
188 if (bean->GetIntValue("STREAMID") == volumeBean->GetIntValue("STREAMID")) {
189 MEDIA_LOG_D("Find the existing volume");
190 return true;
191 }
192 return false;
193 };
194 auto it = std::find_if(volumeVector_.begin(), volumeVector_.end(), isExist);
195 if (it != volumeVector_.end()) {
196 HandleDeviceChangeForDuration(FOR_VOLUME_CHANGE_EVENT, bean, *it);
197 }
198 }
199
HandleDeviceChangeForDuration(const DeviceChangeEvent & event,std::shared_ptr<EventBean> & bean,std::shared_ptr<EventBean> & beanInVector)200 void EventAggregate::HandleDeviceChangeForDuration(const DeviceChangeEvent &event,
201 std::shared_ptr<EventBean> &bean, std::shared_ptr<EventBean> &beanInVector)
202 {
203 if (bean->GetIntValue("DEVICETYPE") != beanInVector->GetIntValue("DEVICE_TYPE")) {
204 uint64_t duration = TimeUtils::GetCurSec() - beanInVector->GetUint64Value("START_TIME");
205 if (duration > 0 && (static_cast<int64_t>(duration) > NEED_INCREASE_FREQUENCY)) {
206 beanInVector->Add("DURATION", duration);
207 if (event == FOR_DEVICE_EVENT) {
208 mediaMonitorPolicy_.HandDeviceUsageToEventVector(beanInVector);
209 } else if (event == FOR_CAPTURE_MUTE_EVENT) {
210 mediaMonitorPolicy_.HandleCaptureMutedToEventVector(beanInVector);
211 } else if (event == FOR_VOLUME_CHANGE_EVENT) {
212 mediaMonitorPolicy_.HandleVolumeToEventVector(beanInVector);
213 }
214 mediaMonitorPolicy_.WhetherToHiSysEvent();
215 }
216 beanInVector->UpdateIntMap("DEVICE_TYPE", bean->GetIntValue("DEVICETYPE"));
217 beanInVector->UpdateUint64Map("START_TIME", TimeUtils::GetCurSec());
218 }
219 }
220
HandleStreamChangeEvent(std::shared_ptr<EventBean> & bean)221 void EventAggregate::HandleStreamChangeEvent(std::shared_ptr<EventBean> &bean)
222 {
223 MEDIA_LOG_D("Handle stream change event");
224 if (bean->GetIntValue("STATE") == AudioStandard::State::RUNNING) {
225 MEDIA_LOG_D("Stream State RUNNING");
226 uint64_t curruntTime = TimeUtils::GetCurSec();
227 AddToDeviceUsage(bean, curruntTime);
228 AddToStreamUsage(bean, curruntTime);
229 AddToStreamPropertyVector(bean, curruntTime);
230 AddToCaptureMuteUsage(bean, curruntTime);
231 AddToVolumeVector(bean, curruntTime);
232 } else if (bean->GetIntValue("STATE") == AudioStandard::State::STOPPED ||
233 bean->GetIntValue("STATE") == AudioStandard::State::PAUSED ||
234 bean->GetIntValue("STATE") == AudioStandard::State::RELEASED) {
235 MEDIA_LOG_D("Stream State STOPPED/PAUSED/RELEASED");
236 HandleDeviceUsage(bean);
237 HandleStreamUsage(bean);
238 HandleCaptureMuted(bean);
239 HandleStreamChangeForVolume(bean);
240 HandleStreamPropertyStats(bean);
241 }
242 mediaMonitorPolicy_.WriteEvent(bean->GetEventId(), bean);
243 }
244
AddToDeviceUsage(std::shared_ptr<EventBean> & bean,uint64_t curruntTime)245 void EventAggregate::AddToDeviceUsage(std::shared_ptr<EventBean> &bean, uint64_t curruntTime)
246 {
247 MEDIA_LOG_D("Add to device usage from stream change event");
248 auto isExist = [&bean](const std::shared_ptr<EventBean> &eventBean) {
249 if (bean->GetIntValue("ISOUTPUT") == eventBean->GetIntValue("IS_PLAYBACK") &&
250 bean->GetIntValue("STREAMID") == eventBean->GetIntValue("STREAMID") &&
251 bean->GetIntValue("UID") == eventBean->GetIntValue("UID") &&
252 bean->GetIntValue("PID") == eventBean->GetIntValue("PID") &&
253 bean->GetIntValue("STREAM_TYPE") == eventBean->GetIntValue("STREAM_TYPE") &&
254 bean->GetIntValue("STATE") == eventBean->GetIntValue("STATE") &&
255 bean->GetIntValue("DEVICETYPE") == eventBean->GetIntValue("DEVICE_TYPE")) {
256 MEDIA_LOG_D("Find the existing device usage");
257 return true;
258 }
259 return false;
260 };
261 auto it = std::find_if(deviceUsageVector_.begin(), deviceUsageVector_.end(), isExist);
262 if (it != deviceUsageVector_.end()) {
263 MEDIA_LOG_D("The current device already exists, do not add it again");
264 return;
265 }
266 std::shared_ptr<EventBean> deviceUsageBean = std::make_shared<EventBean>();
267 int32_t deviceType = bean->GetIntValue("DEVICETYPE");
268 deviceUsageBean->Add("IS_PLAYBACK", bean->GetIntValue("ISOUTPUT"));
269 deviceUsageBean->Add("STREAMID", bean->GetIntValue("STREAMID"));
270 deviceUsageBean->Add("UID", bean->GetIntValue("UID"));
271 deviceUsageBean->Add("PID", bean->GetIntValue("PID"));
272 deviceUsageBean->Add("STREAM_TYPE", bean->GetIntValue("STREAM_TYPE"));
273 deviceUsageBean->Add("STATE", bean->GetIntValue("STATE"));
274 deviceUsageBean->Add("DEVICE_TYPE", deviceType);
275 deviceUsageBean->Add("START_TIME", curruntTime);
276 deviceUsageVector_.push_back(deviceUsageBean);
277 if (deviceType == AudioStandard::DEVICE_TYPE_BLUETOOTH_SCO ||
278 deviceType == AudioStandard::DEVICE_TYPE_BLUETOOTH_A2DP) {
279 deviceUsageBean->Add("BT_TYPE", bean->GetIntValue("BT_TYPE"));
280 }
281 }
282
AddToStreamUsage(std::shared_ptr<EventBean> & bean,uint64_t curruntTime)283 void EventAggregate::AddToStreamUsage(std::shared_ptr<EventBean> &bean, uint64_t curruntTime)
284 {
285 MEDIA_LOG_D("Add to stream usage from stream change event");
286 auto isExist = [&bean](const std::shared_ptr<EventBean> &eventBean) {
287 if (bean->GetIntValue("STREAMID") == eventBean->GetIntValue("STREAMID") &&
288 bean->GetIntValue("UID") == eventBean->GetIntValue("UID") &&
289 bean->GetIntValue("PID") == eventBean->GetIntValue("PID") &&
290 bean->GetIntValue("STREAM_TYPE") == eventBean->GetIntValue("STREAM_TYPE") &&
291 bean->GetIntValue("DEVICETYPE") == eventBean->GetIntValue("DEVICE_TYPE") &&
292 bean->GetIntValue("ISOUTPUT") == eventBean->GetIntValue("IS_PLAYBACK") &&
293 bean->GetIntValue("PIPE_TYPE") == eventBean->GetIntValue("PIPE_TYPE") &&
294 bean->GetIntValue("SAMPLE_RATE") == eventBean->GetIntValue("SAMPLE_RATE")) {
295 MEDIA_LOG_D("Find the existing stream usage");
296 return true;
297 }
298 return false;
299 };
300 auto it = std::find_if(streamUsageVector_.begin(), streamUsageVector_.end(), isExist);
301 if (it != streamUsageVector_.end()) {
302 MEDIA_LOG_D("The current stream already exists, do not add it again");
303 return;
304 }
305 std::shared_ptr<EventBean> streamUsageBean = std::make_shared<EventBean>();
306 streamUsageBean->Add("STREAMID", bean->GetIntValue("STREAMID"));
307 streamUsageBean->Add("UID", bean->GetIntValue("UID"));
308 streamUsageBean->Add("PID", bean->GetIntValue("PID"));
309 streamUsageBean->Add("STREAM_TYPE", bean->GetIntValue("STREAM_TYPE"));
310 streamUsageBean->Add("DEVICE_TYPE", bean->GetIntValue("DEVICETYPE"));
311 streamUsageBean->Add("IS_PLAYBACK", bean->GetIntValue("ISOUTPUT"));
312 streamUsageBean->Add("PIPE_TYPE", bean->GetIntValue("PIPE_TYPE"));
313 streamUsageBean->Add("SAMPLE_RATE", bean->GetIntValue("SAMPLE_RATE"));
314 streamUsageBean->Add("APP_NAME", bean->GetStringValue("APP_NAME"));
315 streamUsageBean->Add("STATE", bean->GetIntValue("STATE"));
316 streamUsageBean->Add("EFFECT_CHAIN", bean->GetIntValue("EFFECT_CHAIN"));
317 streamUsageBean->Add("START_TIME", curruntTime);
318 streamUsageVector_.push_back(streamUsageBean);
319 }
320
AddToStreamPropertyVector(std::shared_ptr<EventBean> & bean,uint64_t curruntTime)321 void EventAggregate::AddToStreamPropertyVector(std::shared_ptr<EventBean> &bean, uint64_t curruntTime)
322 {
323 MEDIA_LOG_D("Add to stream prorerty vector from stream change event");
324 auto isExist = [&bean](const std::shared_ptr<EventBean> &eventBean) {
325 if (bean->GetUint64Value("CHANNEL_LAYOUT") == eventBean->GetUint64Value("CHANNEL_LAYOUT") &&
326 bean->GetIntValue("UID") == eventBean->GetIntValue("UID") &&
327 bean->GetIntValue("ENCODING_TYPE") == eventBean->GetIntValue("ENCODING_TYPE") &&
328 bean->GetIntValue("STREAM_TYPE") == eventBean->GetIntValue("STREAM_TYPE") &&
329 bean->GetIntValue("ISOUTPUT") == eventBean->GetIntValue("IS_PLAYBACK")) {
330 MEDIA_LOG_D("Find the existing stream property");
331 return true;
332 }
333 return false;
334 };
335 auto it = std::find_if(streamPropertyVector_.begin(), streamPropertyVector_.end(), isExist);
336 if (it != streamPropertyVector_.end()) {
337 MEDIA_LOG_D("The current stream property already exists, do not add it again");
338 return;
339 }
340 std::shared_ptr<EventBean> streamPropertyBean = std::make_shared<EventBean>();
341 streamPropertyBean->Add("ENCODING_TYPE", bean->GetIntValue("ENCODING_TYPE"));
342 streamPropertyBean->Add("UID", bean->GetIntValue("UID"));
343 streamPropertyBean->Add("STREAM_TYPE", bean->GetIntValue("STREAM_TYPE"));
344 streamPropertyBean->Add("IS_PLAYBACK", bean->GetIntValue("ISOUTPUT"));
345 streamPropertyBean->Add("CHANNEL_LAYOUT", bean->GetUint64Value("CHANNEL_LAYOUT"));
346 streamPropertyBean->Add("APP_NAME", bean->GetStringValue("APP_NAME"));
347 streamPropertyBean->Add("STATE", bean->GetIntValue("STATE"));
348 streamPropertyBean->Add("START_TIME", curruntTime);
349 streamPropertyVector_.push_back(streamPropertyBean);
350 }
351
AddToCaptureMuteUsage(std::shared_ptr<EventBean> & bean,uint64_t curruntTime)352 void EventAggregate::AddToCaptureMuteUsage(std::shared_ptr<EventBean> &bean, uint64_t curruntTime)
353 {
354 MEDIA_LOG_D("Add to capture mute usage from stream change event");
355 if (bean->GetIntValue("ISOUTPUT")) {
356 MEDIA_LOG_D("AddToCaptureMuteUsage is playback");
357 return;
358 }
359 if (!bean->GetIntValue("MUTED")) {
360 MEDIA_LOG_D("AddToCaptureMuteUsage is not muted");
361 return;
362 }
363 auto isExist = [&bean](const std::shared_ptr<EventBean> &eventBean) {
364 if (bean->GetIntValue("STREAMID") == eventBean->GetIntValue("STREAMID") &&
365 bean->GetIntValue("STREAM_TYPE") == eventBean->GetIntValue("STREAM_TYPE") &&
366 bean->GetIntValue("DEVICETYPE") == eventBean->GetIntValue("DEVICE_TYPE")) {
367 MEDIA_LOG_D("Find the existing capture muted usage");
368 return true;
369 }
370 return false;
371 };
372 auto it = std::find_if(captureMutedVector_.begin(), captureMutedVector_.end(), isExist);
373 if (it != captureMutedVector_.end()) {
374 MEDIA_LOG_D("The current capture already exists, do not add it again");
375 return;
376 }
377 std::shared_ptr<EventBean> captureMutedBean = std::make_shared<EventBean>();
378 captureMutedBean->Add("STREAMID", bean->GetIntValue("STREAMID"));
379 captureMutedBean->Add("STREAM_TYPE", bean->GetIntValue("STREAM_TYPE"));
380 captureMutedBean->Add("DEVICE_TYPE", bean->GetIntValue("DEVICETYPE"));
381 captureMutedBean->Add("START_TIME", curruntTime);
382 captureMutedVector_.push_back(captureMutedBean);
383 }
384
AddToVolumeVector(std::shared_ptr<EventBean> & bean,uint64_t curruntTime)385 void EventAggregate::AddToVolumeVector(std::shared_ptr<EventBean> &bean, uint64_t curruntTime)
386 {
387 MEDIA_LOG_D("Add to volume vector from stream change event");
388 if (!bean->GetIntValue("ISOUTPUT")) {
389 MEDIA_LOG_D("EventAggregate AddToVolumeVector is not playback");
390 return;
391 }
392 auto isExist = [&bean](const std::shared_ptr<EventBean> &volumeBean) {
393 if (bean->GetIntValue("STREAMID") == volumeBean->GetIntValue("STREAMID")) {
394 MEDIA_LOG_D("Find the existing capture volume vector");
395 return true;
396 }
397 return false;
398 };
399 auto it = std::find_if(volumeVector_.begin(), volumeVector_.end(), isExist);
400 if (it != volumeVector_.end()) {
401 MEDIA_LOG_D("The current volume already exists, do not add it again");
402 return;
403 }
404 std::shared_ptr<EventBean> volumeBean = std::make_shared<EventBean>();
405 volumeBean->Add("STREAMID", bean->GetIntValue("STREAMID"));
406 volumeBean->Add("STREAM_TYPE", bean->GetIntValue("STREAM_TYPE"));
407 volumeBean->Add("DEVICE_TYPE", bean->GetIntValue("DEVICETYPE"));
408 volumeBean->Add("LEVEL", systemVol_);
409 volumeBean->Add("START_TIME", TimeUtils::GetCurSec());
410 volumeVector_.push_back(volumeBean);
411 }
412
HandleDeviceUsage(std::shared_ptr<EventBean> & bean)413 void EventAggregate::HandleDeviceUsage(std::shared_ptr<EventBean> &bean)
414 {
415 MEDIA_LOG_D("Handle device usage");
416 auto isExist = [&bean](const std::shared_ptr<EventBean> &deviceUsageBean) {
417 if (bean->GetIntValue("STREAMID") == deviceUsageBean->GetIntValue("STREAMID") &&
418 bean->GetIntValue("UID") == deviceUsageBean->GetIntValue("UID") &&
419 bean->GetIntValue("PID") == deviceUsageBean->GetIntValue("PID") &&
420 bean->GetIntValue("STREAM_TYPE") == deviceUsageBean->GetIntValue("STREAM_TYPE") &&
421 bean->GetIntValue("DEVICETYPE") == deviceUsageBean->GetIntValue("DEVICE_TYPE") &&
422 bean->GetIntValue("ISOUTPUT") == deviceUsageBean->GetIntValue("IS_PLAYBACK")) {
423 MEDIA_LOG_D("Find the existing device usage");
424 return true;
425 }
426 return false;
427 };
428 auto it = std::find_if(deviceUsageVector_.begin(), deviceUsageVector_.end(), isExist);
429 if (it != deviceUsageVector_.end()) {
430 uint64_t duration = TimeUtils::GetCurSec() - (*it)->GetUint64Value("START_TIME");
431 int32_t deviceType = (*it)->GetIntValue("DEVICE_TYPE");
432 if (duration > 0 && (static_cast<int64_t>(duration) > NEED_INCREASE_FREQUENCY)) {
433 (*it)->Add("DURATION", duration);
434 mediaMonitorPolicy_.HandDeviceUsageToEventVector(*it);
435 if (deviceType == AudioStandard::DEVICE_TYPE_BLUETOOTH_SCO ||
436 deviceType == AudioStandard::DEVICE_TYPE_BLUETOOTH_A2DP) {
437 mediaMonitorPolicy_.HandBtUsageToEventVector(*it);
438 }
439 mediaMonitorPolicy_.WhetherToHiSysEvent();
440 }
441 deviceUsageVector_.erase(it);
442 }
443 }
444
HandleStreamUsage(std::shared_ptr<EventBean> & bean)445 void EventAggregate::HandleStreamUsage(std::shared_ptr<EventBean> &bean)
446 {
447 MEDIA_LOG_D("Handle stream usage");
448 auto isExist = [&bean](const std::shared_ptr<EventBean> &streamUsageBean) {
449 if (bean->GetIntValue("STREAMID") == streamUsageBean->GetIntValue("STREAMID") &&
450 bean->GetIntValue("UID") == streamUsageBean->GetIntValue("UID") &&
451 bean->GetIntValue("PID") == streamUsageBean->GetIntValue("PID") &&
452 bean->GetIntValue("STREAM_TYPE") == streamUsageBean->GetIntValue("STREAM_TYPE") &&
453 bean->GetIntValue("ISOUTPUT") == streamUsageBean->GetIntValue("IS_PLAYBACK") &&
454 bean->GetIntValue("PIPE_TYPE") == streamUsageBean->GetIntValue("PIPE_TYPE") &&
455 bean->GetIntValue("SAMPLE_RATE") == streamUsageBean->GetIntValue("SAMPLE_RATE")) {
456 MEDIA_LOG_D("Find the existing stream usage");
457 return true;
458 }
459 return false;
460 };
461 auto it = std::find_if(streamUsageVector_.begin(), streamUsageVector_.end(), isExist);
462 if (it != streamUsageVector_.end()) {
463 uint64_t duration = TimeUtils::GetCurSec() - (*it)->GetUint64Value("START_TIME");
464 if (duration > 0 && (static_cast<int64_t>(duration) > NEED_INCREASE_FREQUENCY)) {
465 (*it)->Add("DURATION", duration);
466 mediaMonitorPolicy_.HandStreamUsageToEventVector(*it);
467 mediaMonitorPolicy_.WhetherToHiSysEvent();
468 }
469 streamUsageVector_.erase(it);
470 }
471 }
472
HandleStreamPropertyStats(std::shared_ptr<EventBean> & bean)473 void EventAggregate::HandleStreamPropertyStats(std::shared_ptr<EventBean> &bean)
474 {
475 MEDIA_LOG_D("Handle stream property stats");
476 auto isExist = [&bean](const std::shared_ptr<EventBean> &streamPropertyBean) {
477 if (bean->GetUint64Value("CHANNEL_LAYOUT") == streamPropertyBean->GetUint64Value("CHANNEL_LAYOUT") &&
478 bean->GetIntValue("UID") == streamPropertyBean->GetIntValue("UID") &&
479 bean->GetIntValue("ENCODING_TYPE") == streamPropertyBean->GetIntValue("ENCODING_TYPE") &&
480 bean->GetIntValue("STREAM_TYPE") == streamPropertyBean->GetIntValue("STREAM_TYPE") &&
481 bean->GetIntValue("ISOUTPUT") == streamPropertyBean->GetIntValue("IS_PLAYBACK")) {
482 MEDIA_LOG_D("Find the existing stream property");
483 return true;
484 }
485 return false;
486 };
487 auto it = std::find_if(streamPropertyVector_.begin(), streamPropertyVector_.end(), isExist);
488 if (it != streamPropertyVector_.end()) {
489 uint64_t duration = TimeUtils::GetCurSec() - (*it)->GetUint64Value("START_TIME");
490 if (duration > 0 && (static_cast<int64_t>(duration) > NEED_INCREASE_FREQUENCY)) {
491 (*it)->Add("DURATION", duration);
492 mediaMonitorPolicy_.HandStreamPropertyToEventVector(*it);
493 mediaMonitorPolicy_.WhetherToHiSysEvent();
494 }
495 streamPropertyVector_.erase(it);
496 }
497 }
498
HandleCaptureMuted(std::shared_ptr<EventBean> & bean)499 void EventAggregate::HandleCaptureMuted(std::shared_ptr<EventBean> &bean)
500 {
501 MEDIA_LOG_D("Handle capture muted");
502 if (bean->GetIntValue("ISOUTPUT")) {
503 MEDIA_LOG_D("HandleCaptureMuted is playback");
504 return;
505 }
506 auto isExist = [&bean](const std::shared_ptr<EventBean> &captureMutedBean) {
507 if (bean->GetIntValue("STREAMID") == captureMutedBean->GetIntValue("STREAMID") &&
508 bean->GetIntValue("STREAM_TYPE") == captureMutedBean->GetIntValue("STREAM_TYPE") &&
509 bean->GetIntValue("DEVICETYPE") == captureMutedBean->GetIntValue("DEVICE_TYPE")) {
510 MEDIA_LOG_D("Find the existing capture muted");
511 return true;
512 }
513 return false;
514 };
515 auto it = std::find_if(captureMutedVector_.begin(), captureMutedVector_.end(), isExist);
516 if (it != captureMutedVector_.end()) {
517 uint64_t duration = TimeUtils::GetCurSec() - (*it)->GetUint64Value("START_TIME");
518 if ((*it)->GetIntValue("MUTED") && duration > 0 && (static_cast<int64_t>(duration) > NEED_INCREASE_FREQUENCY)) {
519 (*it)->Add("DURATION", duration);
520 mediaMonitorPolicy_.HandleCaptureMutedToEventVector(*it);
521 mediaMonitorPolicy_.WhetherToHiSysEvent();
522 }
523 captureMutedVector_.erase(it);
524 }
525 }
526
HandleStreamChangeForVolume(std::shared_ptr<EventBean> & bean)527 void EventAggregate::HandleStreamChangeForVolume(std::shared_ptr<EventBean> &bean)
528 {
529 MEDIA_LOG_D("Handle stream Change for volume");
530 auto isExist = [&bean](const std::shared_ptr<EventBean> &volumeBean) {
531 if (bean->GetIntValue("ISOUTPUT") &&
532 bean->GetIntValue("STREAMID") == volumeBean->GetIntValue("STREAMID") &&
533 bean->GetIntValue("STREAM_TYPE") == volumeBean->GetIntValue("STREAM_TYPE") &&
534 bean->GetIntValue("DEVICETYPE") == volumeBean->GetIntValue("DEVICE_TYPE")) {
535 MEDIA_LOG_D("Find the existing volume vector");
536 return true;
537 }
538 return false;
539 };
540
541 auto it = std::find_if(volumeVector_.begin(), volumeVector_.end(), isExist);
542 if (it != volumeVector_.end()) {
543 uint64_t duration = TimeUtils::GetCurSec() - (*it)->GetUint64Value("START_TIME");
544 if (duration > 0 && (static_cast<int64_t>(duration) > NEED_INCREASE_FREQUENCY)) {
545 (*it)->Add("DURATION", duration);
546 mediaMonitorPolicy_.HandleVolumeToEventVector(*it);
547 mediaMonitorPolicy_.WhetherToHiSysEvent();
548 }
549 volumeVector_.erase(it);
550 }
551 }
552
HandleCaptureMutedStatusChange(std::shared_ptr<EventBean> & bean)553 void EventAggregate::HandleCaptureMutedStatusChange(std::shared_ptr<EventBean> &bean)
554 {
555 MEDIA_LOG_D("Handle capture muted status change");
556 if (!bean->GetIntValue("MUTED")) {
557 HandleCaptureMuted(bean);
558 } else {
559 uint64_t curruntTime = TimeUtils::GetCurSec();
560 AddToCaptureMuteUsage(bean, curruntTime);
561 }
562 }
563
HandleVolumeChange(std::shared_ptr<EventBean> & bean)564 void EventAggregate::HandleVolumeChange(std::shared_ptr<EventBean> &bean)
565 {
566 MEDIA_LOG_D("Handle volume change");
567 mediaMonitorPolicy_.WriteEvent(bean->GetEventId(), bean);
568
569 if (!bean->GetIntValue("ISOUTPUT")) {
570 MEDIA_LOG_D("EventAggregate HandleVolumeChange is not playback");
571 return;
572 }
573 auto isExist = [&bean](const std::shared_ptr<EventBean> &volumeBean) {
574 if (bean->GetIntValue("STREAMID") == volumeBean->GetIntValue("STREAMID") &&
575 bean->GetIntValue("SYSVOLUME") != volumeBean->GetIntValue("LEVEL")) {
576 MEDIA_LOG_D("Find the existing volume vector");
577 return true;
578 }
579 return false;
580 };
581 auto it = std::find_if(volumeVector_.begin(), volumeVector_.end(), isExist);
582 if (it != volumeVector_.end()) {
583 if ((*it)->GetUint64Value("START_TIME") >= 0) {
584 uint64_t duration = TimeUtils::GetCurSec() - (*it)->GetUint64Value("START_TIME");
585 if (duration > 0 && (static_cast<int64_t>(duration) > NEED_INCREASE_FREQUENCY) &&
586 (*it)->GetIntValue("LEVEL") != UNINITIALIZED) {
587 (*it)->Add("DURATION", duration);
588 mediaMonitorPolicy_.HandleVolumeToEventVector(*it);
589 mediaMonitorPolicy_.WhetherToHiSysEvent();
590 }
591 (*it)->UpdateIntMap("LEVEL", bean->GetIntValue("SYSVOLUME"));
592 }
593 }
594 // Record volume for stream state 2->5->2
595 systemVol_ = bean->GetIntValue("SYSVOLUME");
596 }
597
HandlePipeChange(std::shared_ptr<EventBean> & bean)598 void EventAggregate::HandlePipeChange(std::shared_ptr<EventBean> &bean)
599 {
600 MEDIA_LOG_D("Handle pipe change");
601 mediaMonitorPolicy_.WriteEvent(bean->GetEventId(), bean);
602 }
603
HandleStreamExhaustedErrorEvent(std::shared_ptr<EventBean> & bean)604 void EventAggregate::HandleStreamExhaustedErrorEvent(std::shared_ptr<EventBean> &bean)
605 {
606 MEDIA_LOG_D("Handle stream exhausted error event");
607 mediaMonitorPolicy_.HandleExhaustedToEventVector(bean);
608 mediaMonitorPolicy_.WhetherToHiSysEvent();
609 }
610
HandleStreamCreateErrorEvent(std::shared_ptr<EventBean> & bean)611 void EventAggregate::HandleStreamCreateErrorEvent(std::shared_ptr<EventBean> &bean)
612 {
613 MEDIA_LOG_D("Handle stream create error event");
614 mediaMonitorPolicy_.HandleCreateErrorToEventVector(bean);
615 mediaMonitorPolicy_.WhetherToHiSysEvent();
616 }
617
HandleBackgroundSilentPlayback(std::shared_ptr<EventBean> & bean)618 void EventAggregate::HandleBackgroundSilentPlayback(std::shared_ptr<EventBean> &bean)
619 {
620 MEDIA_LOG_D("Handle background silent playback");
621 mediaMonitorPolicy_.HandleSilentPlaybackToEventVector(bean);
622 mediaMonitorPolicy_.WhetherToHiSysEvent();
623 bean->SetEventType(Media::MediaMonitor::BEHAVIOR_EVENT); // report behavior event BG_SILENT_PLAYBACK
624 mediaMonitorPolicy_.WriteEvent(bean->GetEventId(), bean);
625 }
626
HandleUnderrunStatistic(std::shared_ptr<EventBean> & bean)627 void EventAggregate::HandleUnderrunStatistic(std::shared_ptr<EventBean> &bean)
628 {
629 MEDIA_LOG_D("Handle underrun statistic");
630 mediaMonitorPolicy_.HandleUnderrunToEventVector(bean);
631 mediaMonitorPolicy_.WhetherToHiSysEvent();
632 }
633
HandleForceUseDevice(std::shared_ptr<EventBean> & bean)634 void EventAggregate::HandleForceUseDevice(std::shared_ptr<EventBean> &bean)
635 {
636 MEDIA_LOG_D("Handle force use device");
637 audioMemo_.UpdataRouteInfo(bean);
638 mediaMonitorPolicy_.WriteEvent(bean->GetEventId(), bean);
639 }
640
HandleFocusMigrate(std::shared_ptr<EventBean> & bean)641 void EventAggregate::HandleFocusMigrate(std::shared_ptr<EventBean> &bean)
642 {
643 MEDIA_LOG_D("Handle focus use migrate");
644 mediaMonitorPolicy_.WriteEvent(bean->GetEventId(), bean);
645 }
646
HandleJankPlaybackEvent(std::shared_ptr<EventBean> & bean)647 void EventAggregate::HandleJankPlaybackEvent(std::shared_ptr<EventBean> &bean)
648 {
649 MEDIA_LOG_D("Handle jank playback event");
650 mediaMonitorPolicy_.WriteEvent(bean->GetEventId(), bean);
651 }
652
HandleExcludedOutputDevices(std::shared_ptr<EventBean> & bean)653 void EventAggregate::HandleExcludedOutputDevices(std::shared_ptr<EventBean> &bean)
654 {
655 MEDIA_LOG_D("Handle exclude output devices");
656 audioMemo_.UpdateExcludedDevice(bean);
657 mediaMonitorPolicy_.WriteEvent(bean->GetEventId(), bean);
658 }
659
WriteInfo(int32_t fd,std::string & dumpString)660 void EventAggregate::WriteInfo(int32_t fd, std::string &dumpString)
661 {
662 if (fd != -1) {
663 mediaMonitorPolicy_.WriteInfo(fd, dumpString);
664 }
665 }
666
667 } // namespace MediaMonitor
668 } // namespace Media
669 } // namespace OHOS