• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 #define HST_LOG_TAG "DfxAgent"
17 
18 #include "dfx_agent.h"
19 #include "common/log.h"
20 #include "common/media_source.h"
21 #include "hisysevent.h"
22 
23 namespace OHOS {
24 namespace Media {
25 namespace {
26     constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_PLAYER, "DfxAgent" };
27     constexpr int64_t LAG_EVENT_THRESHOLD_MS = 500; // Lag threshold is 500 ms
28     ConcurrentUidSet g_appUidSet{};
29     const std::string SOURCE = "SRC";
30     const std::string DEMUXER = "DEMUX";
31     const std::string VIDEO_SINK = "VSINK";
32     const std::string AUDIO_SINK = "ASINK";
33     const std::string VIDEO_RENDERER = "VRNDR";
34 }
35 
36 const std::map<DfxEventType, DfxEventHandleFunc> DfxAgent::DFX_EVENT_HANDLERS_ = {
37     { DfxEventType::DFX_INFO_PLAYER_VIDEO_LAG, DfxAgent::ProcessVideoLagEvent },
38     { DfxEventType::DFX_INFO_PLAYER_AUDIO_LAG, DfxAgent::ProcessAudioLagEvent },
39     { DfxEventType::DFX_INFO_PLAYER_STREAM_LAG, DfxAgent::ProcessStreamLagEvent },
40     { DfxEventType::DFX_INFO_PLAYER_EOS_SEEK, DfxAgent::ProcessEosSeekEvent },
41     { DfxEventType::DFX_INFO_PERF_REPORT, DfxAgent::ProcessPerfInfoEvent },
42 };
43 
44 const std::unordered_map<std::string, bool> PERF_ITEM_NECESSITY = {
45     { SOURCE, false },
46     { DEMUXER, false },
47     { VIDEO_SINK, true },
48     { AUDIO_SINK, false },
49     { VIDEO_RENDERER, true },
50 };
51 
52 
DfxAgent(const std::string & groupId,const std::string & appName)53 DfxAgent::DfxAgent(const std::string& groupId, const std::string& appName) : groupId_(groupId), appName_(appName)
54 {
55     dfxTask_ = std::make_unique<Task>("OS_Ply_Dfx", groupId_, TaskType::GLOBAL, TaskPriority::NORMAL, false);
56     MEDIA_LOG_I("DfxAgent create for app " PUBLIC_LOG_S, appName_.c_str());
57 }
58 
~DfxAgent()59 DfxAgent::~DfxAgent()
60 {
61     dfxTask_.reset();
62 }
63 
SetSourceType(PlayerDfxSourceType type)64 void DfxAgent::SetSourceType(PlayerDfxSourceType type)
65 {
66     FALSE_RETURN(dfxTask_ != nullptr);
67     std::weak_ptr<DfxAgent> agent = shared_from_this();
68     dfxTask_->SubmitJobOnce([agent, type] {
69         auto ptr = agent.lock();
70         FALSE_RETURN_MSG(ptr != nullptr, "DfxAgent is released");
71         ptr->sourceType_ = type;
72     });
73 }
74 
SetInstanceId(const std::string & instanceId)75 void DfxAgent::SetInstanceId(const std::string& instanceId)
76 {
77     FALSE_RETURN(dfxTask_ != nullptr);
78     std::weak_ptr<DfxAgent> agent = shared_from_this();
79     dfxTask_->SubmitJobOnce([agent, instanceId] {
80         auto ptr = agent.lock();
81         FALSE_RETURN_MSG(ptr != nullptr, "DfxAgent is released");
82         ptr->instanceId_ = instanceId;
83     });
84 }
85 
OnDfxEvent(const DfxEvent & event)86 void DfxAgent::OnDfxEvent(const DfxEvent &event)
87 {
88     auto ret = DfxAgent::DFX_EVENT_HANDLERS_.find(event.type);
89     FALSE_RETURN(ret != DfxAgent::DFX_EVENT_HANDLERS_.end());
90     FALSE_RETURN(dfxTask_ != nullptr);
91     std::weak_ptr<DfxAgent> agent = shared_from_this();
92     dfxTask_->SubmitJobOnce([agent, event, handler = ret->second] {
93         auto ptr = agent.lock();
94         FALSE_RETURN_MSG(ptr != nullptr, "DfxAgent is released");
95         handler(ptr, event);
96     });
97 }
98 
ReportLagEvent(int64_t lagDuration,const std::string & eventMsg)99 void DfxAgent::ReportLagEvent(int64_t lagDuration, const std::string& eventMsg)
100 {
101     FALSE_RETURN(dfxTask_ != nullptr);
102     std::weak_ptr<DfxAgent> agent = shared_from_this();
103     dfxTask_->SubmitJobOnce([agent, lagDuration, eventMsg] {
104         auto ptr = agent.lock();
105         FALSE_RETURN_MSG(ptr != nullptr, "DfxAgent is released");
106         FALSE_RETURN(!(ptr->hasReported_));
107         std::string msg = eventMsg;
108         MEDIA_LOG_W("PLAYER_LAG event reported, lagDuration=" PUBLIC_LOG_D64 ", msg=" PUBLIC_LOG_S,
109             lagDuration, eventMsg.c_str());
110         HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA,
111             "PLAYER_LAG",
112             OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
113             "APP_NAME", ptr->appName_,
114             "INSTANCE_ID", ptr->instanceId_,
115             "SOURCE_TYPE", static_cast<uint8_t>(ptr->sourceType_),
116             "LAG_DURATION", static_cast<int32_t>(lagDuration),
117             "MSG", msg);
118         ptr->hasReported_ = true;
119     });
120 }
121 
ReportEosSeek0Event(int32_t appUid)122 void DfxAgent::ReportEosSeek0Event(int32_t appUid)
123 {
124     FALSE_RETURN(dfxTask_ != nullptr);
125     dfxTask_->SubmitJobOnce([appUid, appName = appName_] {
126         FALSE_RETURN(g_appUidSet.IsAppFirstEvent(appUid));
127         MEDIA_LOG_I("EOS_SEEK_0 event reported, appName = %{public}s appUid = %{public}d", appName.c_str(), appUid);
128         HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA,
129             "EOS_SEEK_0",
130             OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,
131             "APP_NAME", appName,
132             "APP_UID", appUid);
133     });
134 }
135 
ResetAgent()136 void DfxAgent::ResetAgent()
137 {
138     FALSE_RETURN(dfxTask_ != nullptr);
139     std::weak_ptr<DfxAgent> agent = shared_from_this();
140     dfxTask_->SubmitJobOnce([agent] {
141         auto ptr = agent.lock();
142         FALSE_RETURN_MSG(ptr != nullptr, "DfxAgent is released");
143         ptr->hasReported_ = false;
144     });
145 }
146 
ProcessVideoLagEvent(std::weak_ptr<DfxAgent> ptr,const DfxEvent & event)147 void DfxAgent::ProcessVideoLagEvent(std::weak_ptr<DfxAgent> ptr, const DfxEvent &event)
148 {
149     auto agent = ptr.lock();
150     FALSE_RETURN(agent != nullptr);
151     auto perfStr = agent->GetPerfStr(false);
152     MEDIA_LOG_W("%{public}s", perfStr.c_str());
153     agent->needPrintPerfLog_ = true;
154     int64_t lagDuration = AnyCast<int64_t>(event.param);
155     FALSE_RETURN(lagDuration >= LAG_EVENT_THRESHOLD_MS);
156     std::string msg = "lagEvent=Video ";
157     agent->ReportLagEvent(lagDuration, msg + perfStr);
158 }
159 
ProcessAudioLagEvent(std::weak_ptr<DfxAgent> ptr,const DfxEvent & event)160 void DfxAgent::ProcessAudioLagEvent(std::weak_ptr<DfxAgent> ptr, const DfxEvent &event)
161 {
162     auto agent = ptr.lock();
163     FALSE_RETURN(agent != nullptr);
164     int64_t lagDuration = AnyCast<int64_t>(event.param);
165     FALSE_RETURN(lagDuration >= LAG_EVENT_THRESHOLD_MS);
166     std::string msg = "lagEvent=Audio";
167     agent->ReportLagEvent(lagDuration, msg);
168 }
169 
ProcessStreamLagEvent(std::weak_ptr<DfxAgent> ptr,const DfxEvent & event)170 void DfxAgent::ProcessStreamLagEvent(std::weak_ptr<DfxAgent> ptr, const DfxEvent &event)
171 {
172     auto agent = ptr.lock();
173     FALSE_RETURN(agent != nullptr);
174     int64_t lagDuration = AnyCast<int64_t>(event.param);
175     FALSE_RETURN(lagDuration >= LAG_EVENT_THRESHOLD_MS);
176     std::string msg = "lagEvent=Stream";
177     agent->ReportLagEvent(lagDuration, msg);
178 }
179 
ProcessEosSeekEvent(std::weak_ptr<DfxAgent> ptr,const DfxEvent & event)180 void DfxAgent::ProcessEosSeekEvent(std::weak_ptr<DfxAgent> ptr, const DfxEvent &event)
181 {
182     auto agent = ptr.lock();
183     FALSE_RETURN(agent != nullptr);
184     int64_t appUid = AnyCast<int32_t>(event.param);
185     agent->ReportEosSeek0Event(appUid);
186 }
187 
ProcessPerfInfoEvent(std::weak_ptr<DfxAgent> ptr,const DfxEvent & event)188 void DfxAgent::ProcessPerfInfoEvent(std::weak_ptr<DfxAgent> ptr, const DfxEvent &event)
189 {
190     auto agent = ptr.lock();
191     FALSE_RETURN(agent != nullptr);
192     agent->UpdateDfxInfo(event);
193 }
194 
UpdateDfxInfo(const DfxEvent & event)195 void DfxAgent::UpdateDfxInfo(const DfxEvent &event)
196 {
197     auto data = AnyCast<MainPerfData>(event.param);
198     perfDataMap_.insert_or_assign(event.callerName, data);
199     FALSE_RETURN_NOLOG(needPrintPerfLog_);
200     MEDIA_LOG_W("%{public}s", GetPerfStr(true).c_str());
201 }
202 
GetPerfStr(const bool needWaitAllData)203 std::string DfxAgent::GetPerfStr(const bool needWaitAllData)
204 {
205     bool isAllDataReady = true;
206     std::string waitFor = "not all ready, wait for";
207     std::string perfStr = needPrintPerfLog_ ? "AfterLag\n" : "Lag\n";
208     for (auto it = PERF_ITEM_NECESSITY.begin(); it != PERF_ITEM_NECESSITY.end(); ++it) {
209         auto dataMapIt = perfDataMap_.find(it->first);
210         if (dataMapIt != perfDataMap_.end()) {
211             perfStr += "[" + it->first + " speed] max " + std::to_string(dataMapIt->second.max) + " min " +
212                 std::to_string(dataMapIt->second.min) + " avg " + std::to_string(dataMapIt->second.avg) + "\n";
213         } else if (!it->second) {
214             perfStr += "not enough data, but " + it->first + " is not bottleneck\n";
215         } else {
216             waitFor += " " + it->first;
217             isAllDataReady = false;
218         }
219     }
220     perfDataMap_.clear();
221     needPrintPerfLog_ = !isAllDataReady;
222     return (!isAllDataReady && needWaitAllData) ? waitFor : perfStr;
223 }
224 }  // namespace Media
225 }  // namespace OHOS