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