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