• 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 "media_monitor_policy.h"
17 #include "log.h"
18 #include "parameter.h"
19 #include "parameters.h"
20 
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FOUNDATION, "MediaMonitorPolicy"};
23 }
24 
25 namespace OHOS {
26 namespace Media {
27 namespace MediaMonitor {
28 
29 static constexpr int32_t INITIAL_VALUE = 1;
30 
MediaMonitorPolicy()31 MediaMonitorPolicy::MediaMonitorPolicy()
32     : mediaEventBaseWriter_(MediaEventBaseWriter::GetMediaEventBaseWriter())
33 {
34     MEDIA_LOG_D("MediaMonitorPolicy Constructor");
35     ReadParameter();
36     StartTimeThread();
37 }
38 
~MediaMonitorPolicy()39 MediaMonitorPolicy::~MediaMonitorPolicy()
40 {
41     MEDIA_LOG_D("MediaMonitorPolicy Destructor");
42     StopTimeThread();
43 }
44 
ReadParameter()45 void MediaMonitorPolicy::ReadParameter()
46 {
47     const int32_t length = 6;
48     char aggregationFrequency[length] = {0};
49     char aggregationTime[length] = {0};
50     int32_t ret = GetParameter("persist.multimedia.mediafoundation.aggregationfrequency", "1000",
51         aggregationFrequency, sizeof(aggregationFrequency) - 1);
52     if (ret > 0) {
53         aggregationFrequency_ = atoi(aggregationFrequency);
54         MEDIA_LOG_I("Get aggregationFrequency_ success %{public}d", aggregationFrequency_);
55     } else {
56         MEDIA_LOG_E("Get aggregationFrequency_ failed %{public}d", ret);
57     }
58 
59     ret = GetParameter("persist.multimedia.mediafoundation.aggregationtime", "1440",
60         aggregationTime, sizeof(aggregationTime) - 1);
61     if (ret > 0) {
62         aggregationTime_ = atoi(aggregationTime);
63         MEDIA_LOG_I("Get aggregationTime_ success %{public}d", aggregationTime_);
64     } else {
65         MEDIA_LOG_E("Get aggregationTime_ failed %{public}d", ret);
66     }
67 }
68 
TimeFunc()69 void MediaMonitorPolicy::TimeFunc()
70 {
71     while (startThread_.load(std::memory_order_acquire)) {
72         curruntTime_ = TimeUtils::GetCurSec();
73         std::this_thread::sleep_for(std::chrono::minutes(aggregationTime_));
74         HandleToHiSysEvent();
75     }
76 }
77 
StartTimeThread()78 void MediaMonitorPolicy::StartTimeThread()
79 {
80     timeThread_ = std::make_unique<std::thread>(&MediaMonitorPolicy::TimeFunc, this);
81     pthread_setname_np(timeThread_->native_handle(), "DFXTiming");
82 }
83 
StopTimeThread()84 void MediaMonitorPolicy::StopTimeThread()
85 {
86     startThread_.store(false, std::memory_order_release);
87 }
88 
WriteEvent(EventId eventId,std::shared_ptr<EventBean> & bean)89 void MediaMonitorPolicy::WriteEvent(EventId eventId, std::shared_ptr<EventBean> &bean)
90 {
91     MEDIA_LOG_D("Write event");
92     if (bean == nullptr) {
93         MEDIA_LOG_E("eventBean is nullptr");
94         return;
95     }
96     if (bean->GetEventType() == BEHAVIOR_EVENT) {
97         WriteBehaviorEvent(eventId, bean);
98     } else if (bean->GetEventType() == FAULT_EVENT) {
99         WriteFaultEvent(eventId, bean);
100     } else {
101         WriteAggregationEvent(eventId, bean);
102     }
103 }
104 
WriteBehaviorEvent(EventId eventId,std::shared_ptr<EventBean> & bean)105 void MediaMonitorPolicy::WriteBehaviorEvent(EventId eventId, std::shared_ptr<EventBean> &bean)
106 {
107     MEDIA_LOG_D("Write behavior event");
108     BundleInfo bundleInfo = {};
109     switch (eventId) {
110         case DEVICE_CHANGE:
111             mediaEventBaseWriter_.WriteDeviceChange(bean);
112             break;
113         case HEADSET_CHANGE:
114             mediaEventBaseWriter_.WriteHeasetChange(bean);
115             break;
116         case STREAM_CHANGE:
117             setAppNameToEventVector("UID", bean);
118             mediaEventBaseWriter_.WriteStreamChange(bean);
119             break;
120         case VOLUME_CHANGE:
121             mediaEventBaseWriter_.WriteVolumeChange(bean);
122             break;
123         case AUDIO_ROUTE_CHANGE:
124             mediaEventBaseWriter_.WriteAudioRouteChange(bean);
125             break;
126         case AUDIO_PIPE_CHANGE:
127             setAppNameToEventVector("CLIENT_UID", bean);
128             mediaEventBaseWriter_.WriteAudioPipeChange(bean);
129             break;
130         case AUDIO_FOCUS_MIGRATE:
131             setAppNameToEventVector("CLIENT_UID", bean);
132             mediaEventBaseWriter_.WriteFocusMigrate(bean);
133             break;
134         case SET_FORCE_USE_AUDIO_DEVICE:
135             setAppNameToEventVector("CLIENT_UID", bean);
136             mediaEventBaseWriter_.WriteSetForceDevice(bean);
137             break;
138         case BACKGROUND_SILENT_PLAYBACK:
139             bundleInfo = GetBundleInfo(bean->GetIntValue("CLIENT_UID"));
140             bean->Add("APP_NAME", bundleInfo.appName);
141             bean->Add("APP_VERSION_CODE", bundleInfo.versionCode);
142             mediaEventBaseWriter_.WriteBGSilentPlayback(bean);
143             break;
144         case STREAM_STANDBY:
145             mediaEventBaseWriter_.WriteStreamStandby(bean);
146             break;
147         case AI_VOICE_NOISE_SUPPRESSION:
148             mediaEventBaseWriter_.WriteNoiseSuppression(bean);
149             break;
150         default:
151             WriteBehaviorEventExpansion(eventId, bean);
152             break;
153     }
154 }
155 
WriteBehaviorEventExpansion(EventId eventId,std::shared_ptr<EventBean> & bean)156 void MediaMonitorPolicy::WriteBehaviorEventExpansion(EventId eventId, std::shared_ptr<EventBean> &bean)
157 {
158     MEDIA_LOG_D("Write behavior event expansion");
159     BundleInfo bundleInfo = {};
160     switch (eventId) {
161         case VOLUME_SUBSCRIBE:
162             bundleInfo = GetBundleInfo(bean->GetIntValue("CLIENT_UID"));
163             bean->Add("SUBSCRIBE_KEY", bundleInfo.subscrbeKey);
164             bean->Add("SUBSCRIBE_RESULT", bundleInfo.subscrbeResult);
165             mediaEventBaseWriter_.WriteVolumeSubscribe(bean);
166             break;
167         case SMARTPA_STATUS:
168             mediaEventBaseWriter_.WriteSmartPAStatus(bean);
169             break;
170         case EXCLUDE_OUTPUT_DEVICE:
171             setAppNameToEventVector("CLIENT_UID", bean);
172             mediaEventBaseWriter_.WriteExcludeOutputDevice(bean);
173             break;
174         default:
175             break;
176     }
177 }
178 
setAppNameToEventVector(const std::string key,std::shared_ptr<EventBean> & bean)179 void MediaMonitorPolicy::setAppNameToEventVector(const std::string key, std::shared_ptr<EventBean> &bean)
180 {
181     BundleInfo bundleInfo = GetBundleInfo(bean->GetIntValue(key));
182     bean->Add("APP_NAME", bundleInfo.appName);
183 }
184 
GetBundleInfo(int32_t appUid)185 BundleInfo MediaMonitorPolicy::GetBundleInfo(int32_t appUid)
186 {
187     auto cachedBundleInfo = cachedBundleInfoMap_.find(appUid);
188     if (cachedBundleInfo != cachedBundleInfoMap_.end()) {
189         return cachedBundleInfo->second;
190     }
191     MediaMonitorWrapper mediaMonitorWrapper;
192     BundleInfo bundleInfo;
193     MediaMonitorErr err = mediaMonitorWrapper.GetBundleInfo(appUid, &bundleInfo);
194     if (err != MediaMonitorErr::SUCCESS) {
195         return bundleInfo;
196     }
197     cachedBundleInfoMap_.insert(std::make_pair(appUid, bundleInfo));
198     return bundleInfo;
199 }
200 
WriteFaultEvent(EventId eventId,std::shared_ptr<EventBean> & bean)201 void MediaMonitorPolicy::WriteFaultEvent(EventId eventId, std::shared_ptr<EventBean> &bean)
202 {
203     MEDIA_LOG_D("Write fault event");
204     switch (eventId) {
205         case LOAD_CONFIG_ERROR:
206             mediaEventBaseWriter_.WriteLoadConfigError(bean);
207             break;
208         case AUDIO_SERVICE_STARTUP_ERROR:
209             mediaEventBaseWriter_.WriteAudioStartupError(bean);
210             break;
211         case LOAD_EFFECT_ENGINE_ERROR:
212             mediaEventBaseWriter_.WriteLoadEffectEngineError(bean);
213             break;
214         case JANK_PLAYBACK:
215             mediaEventBaseWriter_.WriteJankPlaybackError(bean);
216             break;
217         default:
218             break;
219     }
220 }
221 
WriteAggregationEvent(EventId eventId,std::shared_ptr<EventBean> & bean)222 void MediaMonitorPolicy::WriteAggregationEvent(EventId eventId, std::shared_ptr<EventBean> &bean)
223 {
224     MEDIA_LOG_D("Write aggregation event");
225     BundleInfo bundleInfo = {};
226     switch (eventId) {
227         case AUDIO_STREAM_EXHAUSTED_STATS:
228             bundleInfo = GetBundleInfo(bean->GetIntValue("CLIENT_UID"));
229             bean->Add("DUBIOUS_APP", bundleInfo.appName);
230             mediaEventBaseWriter_.WriteStreamExhastedError(bean);
231             break;
232         case AUDIO_STREAM_CREATE_ERROR_STATS:
233             bundleInfo = GetBundleInfo(bean->GetIntValue("CLIENT_UID"));
234             bean->Add("APP_NAME", bundleInfo.appName);
235             bean->Add("APP_VERSION_CODE", bundleInfo.versionCode);
236             mediaEventBaseWriter_.WriteStreamCreateError(bean);
237             break;
238         case BACKGROUND_SILENT_PLAYBACK:
239             bundleInfo = GetBundleInfo(bean->GetIntValue("CLIENT_UID"));
240             bean->Add("APP_NAME", bundleInfo.appName);
241             bean->Add("APP_VERSION_CODE", bundleInfo.versionCode);
242             mediaEventBaseWriter_.WriteBackgoundSilentPlayback(bean);
243             break;
244         case STREAM_UTILIZATION_STATS:
245             mediaEventBaseWriter_.WriteStreamStatistic(bean);
246             break;
247         case STREAM_PROPERTY_STATS:
248             mediaEventBaseWriter_.WriteStreamPropertyStatistic(bean);
249             break;
250         case AUDIO_DEVICE_UTILIZATION_STATS:
251              mediaEventBaseWriter_.WriteDeviceStatistic(bean);
252             break;
253         case BT_UTILIZATION_STATS:
254             mediaEventBaseWriter_.WriteBtUsageStatistic(bean);
255             break;
256         case PERFORMANCE_UNDER_OVERRUN_STATS:
257             bundleInfo = GetBundleInfo(bean->GetIntValue("CLIENT_UID"));
258             bean->Add("APP_NAME", bundleInfo.appName);
259             mediaEventBaseWriter_.WriteUnderrunStatistic(bean);
260             break;
261         case PLAYBACK_VOLUME_STATS:
262             mediaEventBaseWriter_.WritePlaybackVolume(bean);
263             break;
264         case MUTED_CAPTURE_STATS:
265             mediaEventBaseWriter_.WriteMutedCapture(bean);
266             break;
267         default:
268             break;
269     }
270 }
271 
HandDeviceUsageToEventVector(std::shared_ptr<EventBean> & deviceUsage)272 void MediaMonitorPolicy::HandDeviceUsageToEventVector(std::shared_ptr<EventBean> &deviceUsage)
273 {
274     MEDIA_LOG_I("Handle device usage to event vector");
275     if (deviceUsage == nullptr) {
276         MEDIA_LOG_E("MediaMonitorPolicy HandDeviceUsageToEventVector deviceUsage is nullpr");
277         return;
278     }
279     bool isInEventMap = false;
280     auto isExist = [&deviceUsage](const std::shared_ptr<EventBean> &eventBean) {
281         if (eventBean->GetEventId() == AUDIO_DEVICE_UTILIZATION_STATS &&
282             deviceUsage->GetIntValue("IS_PLAYBACK") == eventBean->GetIntValue("IS_PLAYBACK") &&
283             deviceUsage->GetIntValue("STREAM_TYPE") == eventBean->GetIntValue("STREAM_TYPE") &&
284             deviceUsage->GetIntValue("DEVICE_TYPE") == eventBean->GetIntValue("DEVICE_TYPE")) {
285             MEDIA_LOG_D("Find the existing device usage");
286             return true;
287         }
288         return false;
289     };
290     std::lock_guard<std::mutex> lockEventVector(eventVectorMutex_);
291     auto it = std::find_if(eventVector_.begin(), eventVector_.end(), isExist);
292     if (it != eventVector_.end()) {
293         uint64_t duration = (*it)->GetUint64Value("DURATION") + deviceUsage->GetUint64Value("DURATION");
294         (*it)->UpdateUint64Map("DURATION", duration);
295         isInEventMap = true;
296     }
297 
298     if (!isInEventMap) {
299         std::shared_ptr<EventBean> eventBean = std::make_shared<EventBean>(ModuleId::AUDIO,
300             EventId::AUDIO_DEVICE_UTILIZATION_STATS, EventType::FREQUENCY_AGGREGATION_EVENT);
301         eventBean->Add("STREAM_TYPE", deviceUsage->GetIntValue("STREAM_TYPE"));
302         eventBean->Add("DEVICE_TYPE", deviceUsage->GetIntValue("DEVICE_TYPE"));
303         eventBean->Add("IS_PLAYBACK", deviceUsage->GetIntValue("IS_PLAYBACK"));
304         eventBean->Add("DURATION", deviceUsage->GetUint64Value("DURATION"));
305         AddToEventVector(eventBean);
306     }
307 }
308 
HandBtUsageToEventVector(std::shared_ptr<EventBean> & btUsage)309 void MediaMonitorPolicy::HandBtUsageToEventVector(std::shared_ptr<EventBean> &btUsage)
310 {
311     MEDIA_LOG_I("Handle bt usage to event vector");
312     if (btUsage == nullptr) {
313         MEDIA_LOG_I("MediaMonitorPolicy HandBtUsageToEventVector btUsage is nullpr");
314         return;
315     }
316     bool isInEventMap = false;
317     auto isExist = [&btUsage](const std::shared_ptr<EventBean> &eventBean) {
318         if (eventBean->GetEventId() == BT_UTILIZATION_STATS &&
319             btUsage->GetIntValue("BT_TYPE") == eventBean->GetIntValue("BT_TYPE") &&
320             btUsage->GetIntValue("STREAM_TYPE") == eventBean->GetIntValue("STREAM_TYPE") &&
321             btUsage->GetIntValue("IS_PLAYBACK") == eventBean->GetIntValue("IS_PLAYBACK")) {
322             MEDIA_LOG_I("Find the existing bt device usage");
323             return true;
324         }
325         return false;
326     };
327     std::lock_guard<std::mutex> lockEventVector(eventVectorMutex_);
328     auto it = std::find_if(eventVector_.begin(), eventVector_.end(), isExist);
329     if (it != eventVector_.end()) {
330         uint64_t duration = (*it)->GetUint64Value("DURATION") + btUsage->GetUint64Value("DURATION");
331         (*it)->UpdateUint64Map("DURATION", duration);
332         isInEventMap = true;
333     }
334 
335     if (!isInEventMap) {
336         std::shared_ptr<EventBean> eventBean = std::make_shared<EventBean>(ModuleId::AUDIO,
337             EventId::BT_UTILIZATION_STATS, EventType::FREQUENCY_AGGREGATION_EVENT);
338         eventBean->Add("BT_TYPE", btUsage->GetIntValue("BT_TYPE"));
339         eventBean->Add("IS_PLAYBACK", btUsage->GetIntValue("IS_PLAYBACK"));
340         eventBean->Add("STREAM_TYPE", btUsage->GetIntValue("STREAM_TYPE"));
341         eventBean->Add("DURATION", btUsage->GetUint64Value("DURATION"));
342         AddToEventVector(eventBean);
343     }
344 }
345 
HandStreamUsageToEventVector(std::shared_ptr<EventBean> & streamUsage)346 void MediaMonitorPolicy::HandStreamUsageToEventVector(std::shared_ptr<EventBean> &streamUsage)
347 {
348     MEDIA_LOG_I("Handle stream usage to event vector");
349     if (streamUsage == nullptr) {
350         MEDIA_LOG_E("MediaMonitorPolicy HandStreamUsageToEventVector streamUsage is nullpr");
351         return;
352     }
353     bool isInEventMap = false;
354     auto isExist = [&streamUsage](const std::shared_ptr<EventBean> &eventBean) {
355         if (eventBean->GetEventId() == STREAM_UTILIZATION_STATS &&
356             streamUsage->GetIntValue("PIPE_TYPE") == eventBean->GetIntValue("PIPE_TYPE") &&
357             streamUsage->GetIntValue("STREAM_TYPE") == eventBean->GetIntValue("STREAM_TYPE") &&
358             streamUsage->GetIntValue("IS_PLAYBACK") == eventBean->GetIntValue("IS_PLAYBACK") &&
359             streamUsage->GetStringValue("APP_NAME") == eventBean->GetStringValue("APP_NAME") &&
360             streamUsage->GetIntValue("SAMPLE_RATE") == eventBean->GetIntValue("SAMPLE_RATE")) {
361             MEDIA_LOG_I("Find the existing stream usage");
362             return true;
363         }
364         return false;
365     };
366     std::lock_guard<std::mutex> lockEventVector(eventVectorMutex_);
367     auto it = std::find_if(eventVector_.begin(), eventVector_.end(), isExist);
368     if (it != eventVector_.end()) {
369         uint64_t duration = (*it)->GetUint64Value("DURATION") + streamUsage->GetUint64Value("DURATION");
370         (*it)->UpdateUint64Map("DURATION", duration);
371         isInEventMap = true;
372     }
373     if (!isInEventMap) {
374         std::shared_ptr<EventBean> eventBean = std::make_shared<EventBean>(ModuleId::AUDIO,
375             EventId::STREAM_UTILIZATION_STATS, EventType::FREQUENCY_AGGREGATION_EVENT);
376         eventBean->Add("PIPE_TYPE", streamUsage->GetIntValue("PIPE_TYPE"));
377         eventBean->Add("IS_PLAYBACK", streamUsage->GetIntValue("IS_PLAYBACK"));
378         eventBean->Add("STREAM_TYPE", streamUsage->GetIntValue("STREAM_TYPE"));
379         eventBean->Add("SAMPLE_RATE", streamUsage->GetIntValue("SAMPLE_RATE"));
380         eventBean->Add("APP_NAME", streamUsage->GetStringValue("APP_NAME"));
381         eventBean->Add("EFFECT_CHAIN", streamUsage->GetIntValue("EFFECT_CHAIN"));
382         eventBean->Add("DURATION", streamUsage->GetUint64Value("DURATION"));
383         AddToEventVector(eventBean);
384     }
385 }
386 
HandStreamPropertyToEventVector(std::shared_ptr<EventBean> & streamProperty)387 void MediaMonitorPolicy::HandStreamPropertyToEventVector(std::shared_ptr<EventBean> &streamProperty)
388 {
389     MEDIA_LOG_I("Handle stream property to event vector");
390     if (streamProperty == nullptr) {
391         MEDIA_LOG_E("MediaMonitorPolicy HandStreamPropertyToEventVector streamProperty is nullptr");
392         return;
393     }
394     bool isInEventMap = false;
395     auto isExist = [&streamProperty](const std::shared_ptr<EventBean> &eventBean) {
396         if (eventBean->GetEventId() == STREAM_UTILIZATION_STATS &&
397             streamProperty->GetIntValue("UID") == eventBean->GetIntValue("UID") &&
398             streamProperty->GetIntValue("STREAM_TYPE") == eventBean->GetIntValue("STREAM_TYPE") &&
399             streamProperty->GetIntValue("IS_PLAYBACK") == eventBean->GetIntValue("IS_PLAYBACK") &&
400             streamProperty->GetStringValue("APP_NAME") == eventBean->GetStringValue("APP_NAME") &&
401             streamProperty->GetIntValue("CHANNEL_LAYOUT") ==
402             static_cast<int64_t>(eventBean->GetUint64Value("CHANNEL_LAYOUT")) &&
403             streamProperty->GetIntValue("ENCODING_TYPE") == eventBean->GetIntValue("ENCODING_TYPE")) {
404             MEDIA_LOG_I("Find the existing stream property");
405             return true;
406         }
407         return false;
408     };
409     std::lock_guard<std::mutex> lockEventVector(eventVectorMutex_);
410     auto it = std::find_if(eventVector_.begin(), eventVector_.end(), isExist);
411     if (it != eventVector_.end()) {
412         uint64_t duration = (*it)->GetUint64Value("DURATION") + streamProperty->GetUint64Value("DURATION");
413         (*it)->UpdateUint64Map("DURATION", duration);
414         isInEventMap = true;
415     }
416 
417     if (!isInEventMap) {
418         std::shared_ptr<EventBean> eventBean = std::make_shared<EventBean>(ModuleId::AUDIO,
419             EventId::STREAM_PROPERTY_STATS, EventType::DURATION_AGGREGATION_EVENT);
420 
421         eventBean->Add("IS_PLAYBACK", streamProperty->GetIntValue("IS_PLAYBACK"));
422         eventBean->Add("STREAM_TYPE", streamProperty->GetIntValue("STREAM_TYPE"));
423         eventBean->Add("APP_NAME", streamProperty->GetStringValue("APP_NAME"));
424         eventBean->Add("ENCODING_TYPE", streamProperty->GetIntValue("ENCODING_TYPE"));
425         eventBean->Add("CHANNEL_LAYOUT", streamProperty->GetUint64Value("CHANNEL_LAYOUT"));
426         eventBean->Add("DURATION", streamProperty->GetUint64Value("DURATION"));
427         AddToEventVector(eventBean);
428     }
429 }
430 
HandleVolumeToEventVector(std::shared_ptr<EventBean> & bean)431 void MediaMonitorPolicy::HandleVolumeToEventVector(std::shared_ptr<EventBean> &bean)
432 {
433     MEDIA_LOG_I("Handle volume to event vector");
434     if (bean == nullptr) {
435         MEDIA_LOG_E("MediaMonitorPolicy HandleVolumeToEventVector bean is nullpr");
436         return;
437     }
438     bool isInEventMap = false;
439     auto isExist = [&bean](const std::shared_ptr<EventBean> &eventBean) {
440         if (eventBean->GetEventId() == PLAYBACK_VOLUME_STATS &&
441             bean->GetIntValue("STREAM_TYPE") == eventBean->GetIntValue("STREAM_TYPE") &&
442             bean->GetIntValue("LEVEL") == eventBean->GetIntValue("LEVEL") &&
443             bean->GetIntValue("DEVICE_TYPE") == eventBean->GetIntValue("DEVICE_TYPE")) {
444             MEDIA_LOG_D("Find the existing volume usage");
445             return true;
446         }
447         return false;
448     };
449 
450     std::lock_guard<std::mutex> lockEventVector(eventVectorMutex_);
451     auto it = std::find_if(eventVector_.begin(), eventVector_.end(), isExist);
452     if (it != eventVector_.end()) {
453         uint64_t duration = (*it)->GetUint64Value("DURATION") + bean->GetUint64Value("DURATION");
454         (*it)->UpdateUint64Map("DURATION", duration);
455         isInEventMap = true;
456     }
457 
458     if (!isInEventMap) {
459         std::shared_ptr<EventBean> eventBean = std::make_shared<EventBean>(ModuleId::AUDIO,
460             EventId::PLAYBACK_VOLUME_STATS, EventType::FREQUENCY_AGGREGATION_EVENT);
461         eventBean->Add("STREAM_TYPE", bean->GetIntValue("STREAM_TYPE"));
462         eventBean->Add("LEVEL", bean->GetIntValue("LEVEL"));
463         eventBean->Add("DEVICE_TYPE", bean->GetIntValue("DEVICE_TYPE"));
464         eventBean->Add("DURATION", bean->GetUint64Value("DURATION"));
465         AddToEventVector(eventBean);
466     }
467 }
468 
HandleCaptureMutedToEventVector(std::shared_ptr<EventBean> & bean)469 void MediaMonitorPolicy::HandleCaptureMutedToEventVector(std::shared_ptr<EventBean> &bean)
470 {
471     MEDIA_LOG_I("Handle capture muted to event vector");
472     if (bean == nullptr) {
473         MEDIA_LOG_E("MediaMonitorPolicy HandleCaptureMutedToEventVector bean is nullpr");
474         return;
475     }
476     bool isInEventMap = false;
477     auto isExist = [&bean](const std::shared_ptr<EventBean> &eventBean) {
478         if (eventBean->GetEventId() == MUTED_CAPTURE_STATS &&
479             bean->GetIntValue("STREAM_TYPE") == eventBean->GetIntValue("STREAM_TYPE") &&
480             bean->GetIntValue("DEVICE_TYPE") == eventBean->GetIntValue("DEVICE_TYPE")) {
481             MEDIA_LOG_I("Find the existing capture muted");
482             return true;
483         }
484         return false;
485     };
486     std::lock_guard<std::mutex> lockEventVector(eventVectorMutex_);
487     auto it = std::find_if(eventVector_.begin(), eventVector_.end(), isExist);
488     if (it != eventVector_.end()) {
489         uint64_t duration = (*it)->GetUint64Value("DURATION") + bean->GetUint64Value("DURATION");
490         (*it)->UpdateUint64Map("DURATION", duration);
491         isInEventMap = true;
492     }
493 
494     if (!isInEventMap) {
495         std::shared_ptr<EventBean> eventBean = std::make_shared<EventBean>(ModuleId::AUDIO,
496             EventId::MUTED_CAPTURE_STATS, EventType::FREQUENCY_AGGREGATION_EVENT);
497         eventBean->Add("STREAM_TYPE", bean->GetIntValue("STREAM_TYPE"));
498         eventBean->Add("DEVICE_TYPE", bean->GetIntValue("DEVICE_TYPE"));
499         eventBean->Add("DURATION", bean->GetUint64Value("DURATION"));
500         AddToEventVector(eventBean);
501     }
502 }
503 
HandleExhaustedToEventVector(std::shared_ptr<EventBean> & bean)504 void MediaMonitorPolicy::HandleExhaustedToEventVector(std::shared_ptr<EventBean> &bean)
505 {
506     MEDIA_LOG_I("Handle exhausted to event map");
507     int32_t uid = bean->GetIntValue("CLIENT_UID");
508     int32_t appStreamNum = bean->GetIntValue("TIMES");
509     bool isInEventMap = false;
510     auto isExist = [&uid](const std::shared_ptr<EventBean> &eventBean) {
511         if (eventBean->GetEventId() == AUDIO_STREAM_EXHAUSTED_STATS &&
512             uid == eventBean->GetIntValue("CLIENT_UID")) {
513             MEDIA_LOG_I("Find the existing CLIENT_UID");
514             return true;
515         }
516         return false;
517     };
518     std::lock_guard<std::mutex> lockEventVector(eventVectorMutex_);
519     auto it = std::find_if(eventVector_.begin(), eventVector_.end(), isExist);
520     if (it != eventVector_.end()) {
521         int32_t streamNum = (*it)->GetIntValue("TIMES");
522         if (appStreamNum > streamNum) {
523             (*it)->UpdateIntMap("TIMES", appStreamNum);
524         }
525         isInEventMap = true;
526     }
527     if (!isInEventMap) {
528         std::shared_ptr<EventBean> eventBean = std::make_shared<EventBean>(ModuleId::AUDIO,
529             EventId::AUDIO_STREAM_EXHAUSTED_STATS, EventType::FREQUENCY_AGGREGATION_EVENT);
530         eventBean->Add("CLIENT_UID", uid);
531         eventBean->Add("TIMES", appStreamNum);
532         AddToEventVector(eventBean);
533     }
534 }
535 
HandleCreateErrorToEventVector(std::shared_ptr<EventBean> & bean)536 void MediaMonitorPolicy::HandleCreateErrorToEventVector(std::shared_ptr<EventBean> &bean)
537 {
538     MEDIA_LOG_I("Handle create error to event vector");
539     bool isInEventMap = false;
540     auto isExist = [&bean](const std::shared_ptr<EventBean> &eventBean) {
541         if (eventBean->GetEventId() == AUDIO_STREAM_CREATE_ERROR_STATS &&
542             bean->GetIntValue("CLIENT_UID") == eventBean->GetIntValue("CLIENT_UID") &&
543             bean->GetIntValue("IS_PLAYBACK") == eventBean->GetIntValue("IS_PLAYBACK") &&
544             bean->GetIntValue("STREAM_TYPE") == eventBean->GetIntValue("STREAM_TYPE") &&
545             bean->GetIntValue("ERROR_CODE") == eventBean->GetIntValue("ERROR_CODE")) {
546             MEDIA_LOG_I("Find the existing create error app");
547             return true;
548         }
549         return false;
550     };
551     std::lock_guard<std::mutex> lockEventVector(eventVectorMutex_);
552     auto it = std::find_if(eventVector_.begin(), eventVector_.end(), isExist);
553     if (it != eventVector_.end()) {
554         int32_t num = (*it)->GetIntValue("TIMES");
555         (*it)->UpdateIntMap("TIMES", ++num);
556         isInEventMap = true;
557     }
558 
559     if (!isInEventMap) {
560         std::shared_ptr<EventBean> eventBean = std::make_shared<EventBean>(ModuleId::AUDIO,
561             EventId::AUDIO_STREAM_CREATE_ERROR_STATS, EventType::FREQUENCY_AGGREGATION_EVENT);
562         eventBean->Add("CLIENT_UID", bean->GetIntValue("CLIENT_UID"));
563         eventBean->Add("IS_PLAYBACK", bean->GetIntValue("IS_PLAYBACK"));
564         eventBean->Add("STREAM_TYPE", bean->GetIntValue("STREAM_TYPE"));
565         eventBean->Add("ERROR_CODE", bean->GetIntValue("ERROR_CODE"));
566         eventBean->Add("TIMES", INITIAL_VALUE);
567         AddToEventVector(eventBean);
568     }
569 }
570 
HandleSilentPlaybackToEventVector(std::shared_ptr<EventBean> & bean)571 void MediaMonitorPolicy::HandleSilentPlaybackToEventVector(std::shared_ptr<EventBean> &bean)
572 {
573     MEDIA_LOG_I("Handle silent playback to event vector");
574     bool isInEventMap = false;
575     BundleInfo bundleInfo = GetBundleInfo(bean->GetIntValue("CLIENT_UID"));
576     auto isExist = [&bundleInfo](const std::shared_ptr<EventBean> &eventBean) {
577         if (eventBean->GetEventId() == BACKGROUND_SILENT_PLAYBACK &&
578             bundleInfo.appName == eventBean->GetStringValue("APP_NAME") &&
579             bundleInfo.versionCode == eventBean->GetIntValue("APP_VERSION_CODE")) {
580             MEDIA_LOG_I("Find the existing silent playback app");
581             return true;
582         }
583         return false;
584     };
585     std::lock_guard<std::mutex> lockEventVector(eventVectorMutex_);
586     auto it = std::find_if(eventVector_.begin(), eventVector_.end(), isExist);
587     if (it != eventVector_.end()) {
588         int32_t num = (*it)->GetIntValue("TIMES");
589         (*it)->UpdateIntMap("TIMES", ++num);
590         isInEventMap = true;
591     }
592     if (!isInEventMap) {
593         std::shared_ptr<EventBean> eventBean = std::make_shared<EventBean>(ModuleId::AUDIO,
594             EventId::BACKGROUND_SILENT_PLAYBACK, EventType::FREQUENCY_AGGREGATION_EVENT);
595         eventBean->Add("APP_NAME", bundleInfo.appName);
596         eventBean->Add("APP_VERSION_CODE", bundleInfo.versionCode);
597         eventBean->Add("TIMES", INITIAL_VALUE);
598         AddToEventVector(eventBean);
599     }
600 }
601 
HandleUnderrunToEventVector(std::shared_ptr<EventBean> & bean)602 void MediaMonitorPolicy::HandleUnderrunToEventVector(std::shared_ptr<EventBean> &bean)
603 {
604     MEDIA_LOG_I("Handle underrun to event vector");
605     bool isInEventMap = false;
606     auto isExist = [&bean](const std::shared_ptr<EventBean> &eventBean) {
607         if (eventBean->GetEventId() == PERFORMANCE_UNDER_OVERRUN_STATS &&
608             bean->GetIntValue("CLIENT_UID") == eventBean->GetIntValue("CLIENT_UID") &&
609             bean->GetIntValue("IS_PLAYBACK") == eventBean->GetIntValue("IS_PLAYBACK") &&
610             bean->GetIntValue("PIPE_TYPE") == eventBean->GetIntValue("PIPE_TYPE") &&
611             bean->GetIntValue("STREAM_TYPE") == eventBean->GetIntValue("STREAM_TYPE")) {
612             MEDIA_LOG_I("Find the existing underrun app");
613             return true;
614         }
615         return false;
616     };
617     std::lock_guard<std::mutex> lockEventVector(eventVectorMutex_);
618     auto it = std::find_if(eventVector_.begin(), eventVector_.end(), isExist);
619     if (it != eventVector_.end()) {
620         int32_t num = (*it)->GetIntValue("TIMES");
621         (*it)->UpdateIntMap("TIMES", ++num);
622         isInEventMap = true;
623     }
624     if (!isInEventMap) {
625         std::shared_ptr<EventBean> eventBean = std::make_shared<EventBean>(ModuleId::AUDIO,
626             EventId::PERFORMANCE_UNDER_OVERRUN_STATS, EventType::FREQUENCY_AGGREGATION_EVENT);
627         eventBean->Add("CLIENT_UID", bean->GetIntValue("CLIENT_UID"));
628         eventBean->Add("IS_PLAYBACK", bean->GetIntValue("IS_PLAYBACK"));
629         eventBean->Add("PIPE_TYPE", bean->GetIntValue("PIPE_TYPE"));
630         eventBean->Add("STREAM_TYPE", bean->GetIntValue("STREAM_TYPE"));
631         eventBean->Add("TIMES", INITIAL_VALUE);
632         AddToEventVector(eventBean);
633     }
634 }
635 
AddToEventVector(std::shared_ptr<EventBean> & bean)636 void MediaMonitorPolicy::AddToEventVector(std::shared_ptr<EventBean> &bean)
637 {
638     MEDIA_LOG_D("Add to event vector");
639     eventVector_.push_back(bean);
640 }
641 
WhetherToHiSysEvent()642 void MediaMonitorPolicy::WhetherToHiSysEvent()
643 {
644     MEDIA_LOG_D("eventVector size %{public}zu", eventVector_.size());
645     if (eventVector_.size() >= static_cast<uint32_t>(aggregationFrequency_)) {
646         std::unique_ptr<std::thread>  writeEventThread = std::make_unique<std::thread>(
647             &MediaMonitorPolicy::HandleToHiSysEvent, this);
648         pthread_setname_np(writeEventThread->native_handle(), "ToHiSysEvent");
649         writeEventThread->detach();
650     }
651 }
652 
HandleToHiSysEvent()653 void MediaMonitorPolicy::HandleToHiSysEvent()
654 {
655     MEDIA_LOG_D("Handle to hiSysEvent");
656     std::vector<std::shared_ptr<EventBean>> eventVector;
657     {
658         std::lock_guard<std::mutex> lockEventVector(eventVectorMutex_);
659         eventVector = eventVector_;
660         eventVector_.clear();
661     }
662     for (auto &desc : eventVector) {
663         if (desc == nullptr) {
664             continue;
665         }
666         WriteEvent(desc->GetEventId(), desc);
667     }
668 }
669 
WriteInfo(int32_t fd,std::string & dumpString)670 void MediaMonitorPolicy::WriteInfo(int32_t fd, std::string &dumpString)
671 {
672     if (fd != -1) {
673         int32_t remainderSize = aggregationFrequency_ - static_cast<int32_t>(eventVector_.size());
674         int64_t elapsedTime = static_cast<int64_t>((TimeUtils::GetCurSec() - curruntTime_)) / 60;
675         int64_t oneDay = aggregationTime_;
676         dumpString += "Counting of eventVector entries:";
677         dumpString += "\n";
678         dumpString += "    The current number of eventVector: " + std::to_string(eventVector_.size());
679         dumpString += "\n";
680         dumpString += "    Remaining number of starting refreshes: " + std::to_string(remainderSize);
681         dumpString += "\n";
682         dumpString += "\n";
683         dumpString += "Time Count:";
684         dumpString += "\n";
685         dumpString += "    Time elapsed:" + std::to_string(elapsedTime) + "min";
686         dumpString += "\n";
687         dumpString += "    Remaining refresh time:" + std::to_string(oneDay - elapsedTime) + "min";
688         dumpString += "\n";
689         dumpString += "\n";
690     }
691 }
692 
693 } // namespace MediaMonitor
694 } // namespace Media
695 } // namespace OHOS