1 /*
2 * Copyright (C) 2023 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 <avcodec_sysevent.h>
17 #include <unistd.h>
18 #include "securec.h"
19 #include "avcodec_log.h"
20 #include "avcodec_errors.h"
21 #include "dump_usage.h"
22
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVCodecDFX"};
25 constexpr uint32_t MAX_STRING_SIZE = 256;
26 constexpr char HISYSEVENT_DOMAIN_AVCODEC[] = "AV_CODEC";
27 } // namespace
28
29 namespace OHOS {
30 namespace MediaAVCodec {
CreateMsg(const char * format,...)31 bool AVCodecEvent::CreateMsg(const char* format, ...)
32 {
33 va_list args;
34 va_start(args, format);
35 char msg[MAX_STRING_SIZE] = {0};
36 if (vsnprintf_s(msg, sizeof(msg), sizeof(msg) - 1, format, args) < 0) {
37 AVCODEC_LOGE("failed to call vsnprintf_s");
38 va_end(args);
39 return false;
40 }
41 va_end(args);
42 msg_ = msg;
43 return true;
44 }
45
FaultEventWrite(const std::string & eventName,OHOS::HiviewDFX::HiSysEvent::EventType type,FaultType faultType,const std::string & module)46 void AVCodecEvent::FaultEventWrite(const std::string& eventName,
47 OHOS::HiviewDFX::HiSysEvent::EventType type,
48 FaultType faultType,
49 const std::string& module)
50 {
51 std::string faultName;
52 switch (faultType) {
53 case FaultType::FAULT_TYPE_FREEZE:
54 faultName = "Freeze";
55 break;
56 case FaultType::FAULT_TYPE_CRASH:
57 faultName = "Crash";
58 break;
59 case FaultType::FAULT_TYPE_INNER_ERROR:
60 faultName = "Inner error";
61 break;
62 default:
63 AVCODEC_LOGE("Invalid fault type:%{public}d", faultType);
64 }
65 HiSysEventWrite(HISYSEVENT_DOMAIN_AVCODEC, eventName, type, "MODULE", module, "FAULTTYPE", faultName, "MSG", msg_);
66 }
67
FaultEventWrite(FaultType faultType,const std::string & msg,const std::string & module)68 void FaultEventWrite(FaultType faultType, const std::string& msg, const std::string& module)
69 {
70 AVCodecEvent event;
71 if (event.CreateMsg("%s", msg.c_str())) {
72 event.FaultEventWrite("FAULT", OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, faultType, module);
73 } else {
74 AVCODEC_LOGW("Failed to call CreateMsg");
75 }
76 }
77
ServiceStartEventWrite(uint32_t useTime,const std::string & module)78 void ServiceStartEventWrite(uint32_t useTime, const std::string& module)
79 {
80 OHOS::HiviewDFX::DumpUsage dumpUse;
81 uint64_t useMemory = dumpUse.GetPss(getpid());
82 HiSysEventWrite(HISYSEVENT_DOMAIN_AVCODEC, "SERVICE_START_INFO",
83 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "MODULE", module.c_str(), "TIME", useTime,
84 "MEMORY", useMemory);
85 }
86
CodecStartEventWrite(CodecDfxInfo & codecDfxInfo)87 void CodecStartEventWrite(CodecDfxInfo& codecDfxInfo)
88 {
89 HiSysEventWrite(HISYSEVENT_DOMAIN_AVCODEC, "CODEC_START_INFO",
90 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
91 "CLIENT_PID", codecDfxInfo.clientPid,
92 "CLIENT_UID", codecDfxInfo.clientUid,
93 "CODEC_INSTANCE_ID", codecDfxInfo.codecInstanceId,
94 "CODEC_NAME", codecDfxInfo.codecName,
95 "CODEC_IS_VENDOR", codecDfxInfo.codecIsVendor,
96 "CODEC_MODE", codecDfxInfo.codecMode,
97 "ENCODER_BITRATE", codecDfxInfo.encoderBitRate,
98 "VIDEO_WIDTH", codecDfxInfo.videoWidth,
99 "VIDEO_HEIGHT", codecDfxInfo.videoHeight,
100 "VIDEO_FRAMERATE", codecDfxInfo.videoFrameRate,
101 "VIDEO_PIXEL_FORMAT", codecDfxInfo.videoPixelFormat,
102 "AUDIO_CHANNEL_COUNT", codecDfxInfo.audioChannelCount,
103 "AUDIO_SAMPLE_RATE", codecDfxInfo.audioSampleRate);
104 }
105
CodecStopEventWrite(uint32_t clientPid,uint32_t clientUid,uint32_t codecInstanceId)106 void CodecStopEventWrite(uint32_t clientPid, uint32_t clientUid, uint32_t codecInstanceId)
107 {
108 HiSysEventWrite(HISYSEVENT_DOMAIN_AVCODEC, "CODEC_STOP_INFO",
109 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
110 "CLIENT_PID", clientPid, "CLIENT_UID", clientUid, "CODEC_INSTANCE_ID", codecInstanceId);
111 }
112 } // namespace MediaAVCodec
113 } // namespace OHOS
114