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 #ifndef MEDIA_MONITOR_POLICY_H
17 #define MEDIA_MONITOR_POLICY_H
18 
19 #include <thread>
20 #include <chrono>
21 #include <atomic>
22 #include <map>
23 #include <vector>
24 #include "event_bean.h"
25 #include "monitor_utils.h"
26 #include "media_event_base_writer.h"
27 
28 namespace OHOS {
29 namespace Media {
30 namespace MediaMonitor {
31 
32 class MediaMonitorPolicy {
33 public:
GetMediaMonitorPolicy()34     static MediaMonitorPolicy& GetMediaMonitorPolicy()
35     {
36         static MediaMonitorPolicy mediaMonitorPolicy;
37         return mediaMonitorPolicy;
38     }
39 
40     MediaMonitorPolicy();
41     ~MediaMonitorPolicy();
42 
43     void WriteEvent(EventId eventId, std::shared_ptr<EventBean> &bean);
44 
45     void WriteBehaviorEvent(EventId eventId, std::shared_ptr<EventBean> &bean);
46     void WriteFaultEvent(EventId eventId, std::shared_ptr<EventBean> &bean);
47     void WriteAggregationEvent(EventId eventId, std::shared_ptr<EventBean> &bean);
48 
49     void HandDeviceUsageToEventVector(std::shared_ptr<EventBean> &deviceUsage);
50     void HandStreamUsageToEventVector(std::shared_ptr<EventBean> &streamUsage);
51     void HandBtUsageToEventVector(std::shared_ptr<EventBean> &btUsage);
52     void AddToEventVector(std::shared_ptr<EventBean> &bean);
53     void HandleExhaustedToEventVector(std::shared_ptr<EventBean> &bean);
54     void HandleCreateErrorToEventVector(std::shared_ptr<EventBean> &bean);
55     void HandleSilentPlaybackToEventVector(std::shared_ptr<EventBean> &bean);
56     void HandleUnderrunToEventVector(std::shared_ptr<EventBean> &bean);
57     void HandleCaptureMutedToEventVector(std::shared_ptr<EventBean> &bean);
58     void HandleVolumeToEventVector(std::shared_ptr<EventBean> &bean);
59     void HandStreamPropertyToEventVector(std::shared_ptr<EventBean> &streamProperty);
60 
61     void WhetherToHiSysEvent();
62     void WriteInfo(int32_t fd, std::string &dumpString);
63 
64 private:
65     static constexpr int32_t DEFAULT_AGGREGATION_FREQUENCY = 1000;
66     static constexpr int32_t DEFAULT_AGGREGATION_TIME = 24 * 60;
67 
68     void ReadParameter();
69     void StartTimeThread();
70     void StopTimeThread();
71     void TimeFunc();
72     void HandleToHiSysEvent();
73 
74     uint64_t curruntTime_ = 0;
75     std::unique_ptr<std::thread> timeThread_ = nullptr;
76     std::atomic_bool startThread_ = true;
77 
78     MediaEventBaseWriter& mediaEventBaseWriter_;
79 
80     std::mutex eventVectorMutex_;
81     std::vector<std::shared_ptr<EventBean>> eventVector_;
82 
83     int32_t aggregationFrequency_ = DEFAULT_AGGREGATION_FREQUENCY;
84     int32_t aggregationTime_ = DEFAULT_AGGREGATION_TIME;
85 };
86 
87 } // namespace MediaMonitor
88 } // namespace Media
89 } // namespace OHOS
90 #endif // MEDIA_MONITOR_POLICY_H