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