1 /*
2  * Copyright (c) 2022 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_DFX_H
17 #define MEDIA_DFX_H
18 
19 #include <cstring>
20 #include <list>
21 #include <memory>
22 #include <map>
23 #include <string>
24 #include <refbase.h>
25 #include "nocopyable.h"
26 #include "hisysevent.h"
27 #include "meta/meta.h"
28 #ifndef CROSS_PLATFORM
29 #include "nlohmann/json.hpp"
30 #endif
31 #include <chrono>
32 #include <mutex>
33 
34 namespace OHOS {
35 namespace Media {
36 #ifndef CROSS_PLATFORM
37 using json = nlohmann::json;
38 #endif
39 enum CallType {
40     AVPLAYER,
41     AVRECORDER,
42     METADATA_RETRIEVER,
43     IMAGE_GENERATER,
44     AVDEMUXER,
45     AVMUXER,
46     VIDEO_DECODER,
47     VIDEO_ENCODER,
48     AUDIO_DECODER,
49     AUDIO_ENCODER,
50     SOUNDPOOL,
51     SCREEN_CAPTRUER,
52     AVTRANSCODER
53 };
54 class __attribute__((visibility("default"))) MediaEvent : public NoCopyable {
55 public:
56     MediaEvent() = default;
57     ~MediaEvent() = default;
58     bool CreateMsg(const char *format, ...) __attribute__((__format__(printf, 2, 3)));
59     void EventWrite(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
60         std::string module);
61     void EventWriteWithAppInfo(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
62         std::string module, std::string status, int32_t appUid, int32_t appPid);
63     void EventWriteBundleName(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
64         std::string module, std::string status, int32_t appUid, int32_t appPid, std::string bundleName);
65     void SourceEventWrite(const std::string& eventName, OHOS::HiviewDFX::HiSysEvent::EventType type, const std::string&
66         appName, uint64_t instanceId, const std::string& callerType, int8_t sourceType, const std::string& sourceUrl,
67         const std::string& errMsg);
68     void ScreenCaptureEventWrite(const std::string& eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
69         const std::string& appName, uint64_t instanceId, int8_t captureMode, int8_t dataMode, int32_t errorCode,
70         const std::string& errMsg);
71     void CommonStatisicsEventWrite(CallType callType, OHOS::HiviewDFX::HiSysEvent::EventType type,
72         const std::map<int32_t, std::list<std::pair<uint64_t, std::shared_ptr<Meta>>>>& infoMap);
73 private:
74     void StatisicsHiSysEventWrite(CallType callType, OHOS::HiviewDFX::HiSysEvent::EventType type,
75         const std::vector<std::string>& infoArr);
76 #ifndef CROSS_PLATFORM
77     void ParseOneEvent(const std::pair<uint64_t, std::shared_ptr<OHOS::Media::Meta>> &listPair, json& metaInfoJson);
78 #endif
79     std::string msg_;
80 };
81 
82 
83 __attribute__((visibility("default"))) void BehaviorEventWrite(std::string status, std::string module);
84 __attribute__((visibility("default"))) void BehaviorEventWriteForScreenCapture(std::string status,
85     std::string module, int32_t appUid, int32_t appPid);
86 __attribute__((visibility("default"))) void StatisticEventWriteBundleName(std::string status,
87     std::string module);
88 __attribute__((visibility("default"))) void FaultEventWrite(std::string msg, std::string module);
89 __attribute__((visibility("default"))) void FaultSourceEventWrite(const std::string& appName, uint64_t instanceId,
90     const std::string& callerType, int8_t sourceType, const std::string& sourceUrl, const std::string& errorMessage);
91 __attribute__((visibility("default"))) void FaultScreenCaptureEventWrite(const std::string& appName,
92     uint64_t instanceId, int8_t captureMode, int8_t dataMode, int32_t errorCode, const std::string& errorMessage);
93 __attribute__((visibility("default"))) int32_t CreateMediaInfo(CallType callType, int32_t uid, uint64_t instanceId);
94 __attribute__((visibility("default"))) int32_t AppendMediaInfo(const std::shared_ptr<Meta>& meta, uint64_t instanceId);
95 __attribute__((visibility("default"))) int32_t ReportMediaInfo(uint64_t instanceId);
96 
97 class __attribute__((visibility("default"))) MediaTrace : public NoCopyable {
98 public:
99     explicit MediaTrace(const std::string &funcName);
100     static void TraceBegin(const std::string &funcName, int32_t taskId);
101     static void TraceEnd(const std::string &funcName, int32_t taskId);
102     static void CounterTrace(const std::string &varName, int32_t val);
103     ~MediaTrace();
104 private:
105     bool isSync_ = false;
106 };
107 } // namespace Media
108 } // namespace OHOS
109 #endif // MEDIA_DFX_H