• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <media_dfx.h>
17 #include <unistd.h>
18 #include "securec.h"
19 #include "hitrace_meter.h"
20 #include "hitrace/tracechain.h"
21 #include "ipc_skeleton.h"
22 #include "media_utils.h"
23 #include "hitrace/tracechain.h"
24 #include "common/log.h"
25 #include "common/media_core.h"
26 #include "meta/any.h"
27 #include <cstdint>
28 
29 namespace {
30     constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_SYSTEM_PLAYER, "MediaDFX" };
31 
32     constexpr uint32_t MAX_STRING_SIZE = 256;
33     constexpr int64_t HOURS_BETWEEN_REPORTS = 4;
34     constexpr int64_t MAX_MAP_SIZE = 100;
35 
36     std::mutex collectMut_;
37     std::mutex reportMut_;
38     std::map<OHOS::Media::CallType,
39         std::map<int32_t, std::list<std::pair<uint64_t, std::shared_ptr<OHOS::Media::Meta>>>>> mediaInfoMap_;
40     std::map<OHOS::Media::CallType,
41         std::map<int32_t, std::list<std::pair<uint64_t, std::shared_ptr<OHOS::Media::Meta>>>>> reportMediaInfoMap_;
42     std::map<uint64_t, std::pair<OHOS::Media::CallType, int32_t>> idMap_;
43     std::chrono::system_clock::time_point currentTime_ = std::chrono::system_clock::now();
44     bool g_reachMaxMapSize {false};
45 
CollectReportMediaInfo(uint64_t instanceId)46     bool CollectReportMediaInfo(uint64_t instanceId)
47     {
48         OHOS::Media::CallType ct;
49         int32_t uid;
50         std::pair<uint64_t, std::shared_ptr<OHOS::Media::Meta>> metaAppIdPair;
51         {
52             std::lock_guard<std::mutex> lock(collectMut_);
53             MEDIA_LOG_I("CollectReportMediaInfo, instanceId is %{public}" PRIu64, instanceId);
54             auto idMapIt = idMap_.find(instanceId);
55             if (idMapIt == idMap_.end()) {
56                 MEDIA_LOG_W("Not found instanceId in idMap, instanceId is : %{public}" PRIu64, instanceId);
57                 return false;
58             }
59             ct = idMapIt->second.first;
60             uid = idMapIt->second.second;
61             idMap_.erase(idMapIt);
62             auto ctUidToMediaInfo = mediaInfoMap_.find(ct);
63             if (ctUidToMediaInfo == mediaInfoMap_.end()) {
64                 MEDIA_LOG_W("Not found calltype, calltype is : %{public}d", static_cast<OHOS::Media::CallType>(ct));
65                 return false;
66             }
67             auto uidToMediaInfo = ctUidToMediaInfo->second.find(uid);
68             if (uidToMediaInfo == ctUidToMediaInfo->second.end()) {
69                 MEDIA_LOG_W("Not found uid in mediaInfoMap_, uid is : %{public}" PRId32, uid);
70                 return false;
71             }
72             auto& instanceList = uidToMediaInfo->second;
73             for (const auto& instancePair : instanceList) {
74                 if (instancePair.first == instanceId) {
75                     metaAppIdPair = instancePair;
76                     instanceList.remove(instancePair);
77                     break;
78                 }
79             }
80         }
81         std::lock_guard<std::mutex> lock(reportMut_);
82         auto reportCtUidToMediaInfo  = reportMediaInfoMap_.find(ct);
83         if (reportCtUidToMediaInfo != reportMediaInfoMap_.end()) {
84             auto it = reportCtUidToMediaInfo->second.find(uid);
85             if (it != reportCtUidToMediaInfo->second.end()) {
86                 it->second.push_back(metaAppIdPair);
87             } else {
88                 reportCtUidToMediaInfo->second[uid].push_back(metaAppIdPair);
89             }
90         } else {
91             reportMediaInfoMap_[ct][uid].push_back(metaAppIdPair);
92         }
93         g_reachMaxMapSize = (reportMediaInfoMap_[ct].size() >= MAX_MAP_SIZE);
94         return true;
95     }
96 
StatisticsEventReport()97     int32_t StatisticsEventReport()
98     {
99         MEDIA_LOG_I("StatisticsEventReport.");
100         if (reportMediaInfoMap_.empty()) {
101             MEDIA_LOG_I("reportMediaInfoMap_ is empty, can't report");
102             return OHOS::Media::MSERR_INVALID_OPERATION;
103         }
104         OHOS::Media::MediaEvent event;
105         for (const auto &it : reportMediaInfoMap_) {
106             event.CommonStatisicsEventWrite(it.first, OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC, it.second);
107         }
108         auto currentTime = std::chrono::system_clock::now();
109         currentTime_ = currentTime;
110         reportMediaInfoMap_.clear();
111         return OHOS::Media::MSERR_OK;
112     }
113 }
114 
115 namespace OHOS {
116 namespace Media {
117 using namespace OHOS::HiviewDFX;
CreateMsg(const char * format,...)118 bool MediaEvent::CreateMsg(const char *format, ...)
119 {
120     va_list args;
121     va_start(args, format);
122     char msg[MAX_STRING_SIZE] = {0};
123     auto ret = vsnprintf_s(msg, sizeof(msg), sizeof(msg) - 1, format, args);
124     va_end(args);
125     msg_ = msg;
126     return ret < 0 ? false : true;
127 }
128 
EventWrite(std::string eventName,OHOS::HiviewDFX::HiSysEvent::EventType type,std::string module)129 void MediaEvent::EventWrite(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
130     std::string module)
131 {
132     int32_t pid = getpid();
133     uint32_t uid = getuid();
134     HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA, eventName, type,
135         "PID", pid,
136         "UID", uid,
137         "MODULE", module,
138         "MSG", msg_);
139 }
140 
EventWriteWithAppInfo(std::string eventName,OHOS::HiviewDFX::HiSysEvent::EventType type,std::string module,std::string status,int32_t appUid,int32_t appPid)141 void MediaEvent::EventWriteWithAppInfo(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
142     std::string module, std::string status, int32_t appUid, int32_t appPid)
143 {
144     int32_t pid = getpid();
145     uint32_t uid = getuid();
146     HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA, eventName, type,
147         "PID", pid,
148         "UID", uid,
149         "MODULE", module,
150         "MSG", msg_,
151         "APP_PID", appPid,
152         "APP_UID", appUid,
153         "STATUS", status);
154 }
155 
EventWriteBundleName(std::string eventName,OHOS::HiviewDFX::HiSysEvent::EventType type,std::string module,std::string status,int32_t appUid,int32_t appPid,std::string bundleName)156 void MediaEvent::EventWriteBundleName(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
157     std::string module, std::string status, int32_t appUid, int32_t appPid, std::string bundleName)
158 {
159     int32_t pid = getpid();
160     uint32_t uid = getuid();
161     HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA, eventName, type,
162                     "PID", pid,
163                     "UID", uid,
164                     "MODULE", module,
165                     "MSG", msg_,
166                     "APP_PID", appPid,
167                     "APP_UID", appUid,
168                     "STATUS", status,
169                     "BUNDLE", bundleName);
170 }
171 
SourceEventWrite(const std::string & eventName,OHOS::HiviewDFX::HiSysEvent::EventType type,const std::string & appName,uint64_t instanceId,const std::string & callerType,int8_t sourceType,const std::string & sourceUrl,const std::string & errMsg)172 void MediaEvent::SourceEventWrite(const std::string& eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
173     const std::string& appName, uint64_t instanceId, const std::string& callerType, int8_t sourceType,
174     const std::string& sourceUrl, const std::string& errMsg)
175 {
176     std::string instanceIdStr = std::to_string(instanceId);
177     HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA, eventName, type,
178                     "APP_NAME", appName,
179                     "INSTANCE_ID", instanceIdStr,
180                     "CALLER_TYPE", callerType,
181                     "SOURCE_TYPE", sourceType,
182                     "SOURCE_URI", sourceUrl,
183                     "ERROR_MESG", errMsg);
184 }
185 
ScreenCaptureEventWrite(const std::string & eventName,OHOS::HiviewDFX::HiSysEvent::EventType type,const std::string & appName,uint64_t instanceId,int8_t captureMode,int8_t dataMode,int32_t errorCode,const std::string & errorMessage)186 void MediaEvent::ScreenCaptureEventWrite(const std::string& eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
187     const std::string& appName, uint64_t instanceId, int8_t captureMode, int8_t dataMode, int32_t errorCode,
188     const std::string& errorMessage)
189 {
190     std::string instanceIdStr = std::to_string(instanceId);
191     HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA, eventName, type,
192                     "APP_NAME", appName,
193                     "INSTANCE_ID", instanceIdStr,
194                     "CAPTURE_MODE", captureMode,
195                     "DATA_MODE", dataMode,
196                     "ERROR_CODE", errorCode,
197                     "ERROR_MESG", errorMessage);
198 }
199 
CommonStatisicsEventWrite(CallType callType,OHOS::HiviewDFX::HiSysEvent::EventType type,const std::map<int32_t,std::list<std::pair<uint64_t,std::shared_ptr<Meta>>>> & infoMap)200 void MediaEvent::CommonStatisicsEventWrite(CallType callType, OHOS::HiviewDFX::HiSysEvent::EventType type,
201     const std::map<int32_t, std::list<std::pair<uint64_t, std::shared_ptr<Meta>>>>& infoMap)
202 {
203     MEDIA_LOG_I("MediaEvent::CommonStatisicsEventWrite");
204     if (infoMap.empty()) {
205         MEDIA_LOG_I("Player infoMap is empty.");
206         return;
207     }
208     std::vector<std::string> infoArr;
209     for (const auto& kv : infoMap) {
210         json jsonArray;
211         json eventInfoJson;
212         json mediaEvents;
213         for (const auto& listPair : kv.second) {
214             json metaInfoJson;
215             ParseOneEvent(listPair, metaInfoJson);
216             mediaEvents.push_back(metaInfoJson);
217         }
218         eventInfoJson["appName"] = GetClientBundleName(kv.first);
219         eventInfoJson["mediaEvents"] = mediaEvents;
220         jsonArray.push_back(eventInfoJson);
221         infoArr.push_back(jsonArray.dump());
222     }
223 
224     StatisicsHiSysEventWrite(callType, type, infoArr);
225 }
226 
ParseOneEvent(const std::pair<uint64_t,std::shared_ptr<OHOS::Media::Meta>> & listPair,json & metaInfoJson)227 void MediaEvent::ParseOneEvent(const std::pair<uint64_t, std::shared_ptr<OHOS::Media::Meta>> &listPair,
228     json& metaInfoJson)
229 {
230     for (auto it = listPair.second->begin(); it != listPair.second->end(); ++it) {
231         Any valueType = OHOS::Media::GetDefaultAnyValue(it->first);
232         if (Any::IsSameTypeWith<int32_t>(valueType)) {
233             int32_t intVal;
234             if (listPair.second->GetData(it->first, intVal)) {
235                 metaInfoJson[it->first] = std::to_string(intVal);
236             }
237         } else if (Any::IsSameTypeWith<uint32_t>(valueType)) {
238             uint32_t uintVal;
239             if (listPair.second->GetData(it->first, uintVal)) {
240                 metaInfoJson[it->first] = std::to_string(uintVal);
241             }
242         } else if (Any::IsSameTypeWith<uint64_t>(valueType)) {
243             uint64_t uintVal;
244             if (listPair.second->GetData(it->first, uintVal)) {
245                 metaInfoJson[it->first] = std::to_string(uintVal);
246             }
247         } else if (Any::IsSameTypeWith<std::string>(valueType)) {
248             metaInfoJson[it->first] = AnyCast<std::string>(it->second);
249         } else if (Any::IsSameTypeWith<int8_t>(valueType)) {
250             int8_t intVal;
251             if (listPair.second->GetData(it->first, intVal)) {
252                 metaInfoJson[it->first] = std::to_string(intVal);
253             }
254         } else if (Any::IsSameTypeWith<bool>(valueType)) {
255             bool isTrue;
256             if (listPair.second->GetData(it->first, isTrue)) {
257                 metaInfoJson[it->first] = isTrue ? "true" : "false";
258             }
259         } else {
260             MEDIA_LOG_I("not found type matched with it->first: %{public}s", it->first.c_str());
261         }
262     }
263 }
264 
StatisicsHiSysEventWrite(CallType callType,OHOS::HiviewDFX::HiSysEvent::EventType type,const std::vector<std::string> & infoArr)265 void MediaEvent::StatisicsHiSysEventWrite(CallType callType, OHOS::HiviewDFX::HiSysEvent::EventType type,
266     const std::vector<std::string>& infoArr)
267 {
268     MEDIA_LOG_I("MediaEvent::StatisicsHiSysEventWrite");
269     std::string eventName;
270     switch (callType) {
271         case CallType::AVPLAYER:
272             eventName = "PLAYER_COMMON_STATISTICS";
273             break;
274         case CallType::AVRECORDER:
275             eventName = "RECORDER_STATISTICS";
276             break;
277         case CallType::SCREEN_CAPTRUER:
278             eventName = "SCREEN_CAPTURE_STATISTICS";
279             break;
280         case CallType::AVTRANSCODER:
281             eventName = "TRANSCODER_STATISTICS";
282             break;
283         default:
284             return;
285     }
286     HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA, eventName, type,
287                     "EVENTS", infoArr);
288 }
289 
BehaviorEventWrite(std::string status,std::string module)290 void BehaviorEventWrite(std::string status, std::string module)
291 {
292     MediaEvent event;
293     if (event.CreateMsg("%s, current state is: %s", "state change", status.c_str())) {
294         event.EventWrite("PLAYER_STATE", OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, module);
295     }
296 }
297 
BehaviorEventWriteForScreenCapture(std::string status,std::string module,int32_t appUid,int32_t appPid)298 void BehaviorEventWriteForScreenCapture(std::string status, std::string module, int32_t appUid, int32_t appPid)
299 {
300     MediaEvent event;
301     if (event.CreateMsg("%s, current state is: %s", "state change", status.c_str())) {
302         event.EventWriteWithAppInfo("PLAYER_STATE", OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
303             module, status, appUid, appPid);
304     }
305 }
306 
StatisticEventWriteBundleName(std::string status,std::string module)307 void StatisticEventWriteBundleName(std::string status, std::string module)
308 {
309     MediaEvent event;
310     int32_t appUid = IPCSkeleton::GetCallingUid();
311     int32_t appPid = IPCSkeleton::GetCallingPid();
312     std::string bundleName = GetClientBundleName(appUid);
313     if (event.CreateMsg("%s is invoke %s", bundleName.c_str(), module.c_str())) {
314         event.EventWriteBundleName("PLAYER_STATISTICS", OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,
315             module, status, appUid, appPid, bundleName);
316     }
317 }
318 
FaultEventWrite(std::string msg,std::string module)319 void FaultEventWrite(std::string msg, std::string module)
320 {
321     MediaEvent event;
322     if (event.CreateMsg("%s", msg.c_str())) {
323         event.EventWrite("PLAYER_ERR", OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, module);
324     }
325 }
326 
FaultSourceEventWrite(const std::string & appName,uint64_t instanceId,const std::string & callerType,int8_t sourceType,const std::string & sourceUrl,const std::string & errorMessage)327 void FaultSourceEventWrite(const std::string& appName, uint64_t instanceId, const std::string& callerType,
328     int8_t sourceType, const std::string& sourceUrl, const std::string& errorMessage)
329 {
330     MediaEvent event;
331     event.SourceEventWrite("SOURCE_FAILURE", OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, appName, instanceId,
332         callerType, sourceType, sourceUrl, errorMessage);
333 }
334 
FaultScreenCaptureEventWrite(const std::string & appName,uint64_t instanceId,int8_t captureMode,int8_t dataMode,int32_t errorCode,const std::string & errorMessage)335 void FaultScreenCaptureEventWrite(const std::string& appName, uint64_t instanceId, int8_t captureMode, int8_t dataMode,
336     int32_t errorCode, const std::string& errorMessage)
337 {
338     MediaEvent event;
339     event.ScreenCaptureEventWrite("SCREEN_CAPTURE_FAILURE", OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, appName,
340         instanceId, captureMode, dataMode, errorCode, errorMessage);
341 }
342 
CreateMediaInfo(CallType callType,int32_t uid,uint64_t instanceId)343 int32_t CreateMediaInfo(CallType callType, int32_t uid, uint64_t instanceId)
344 {
345     MEDIA_LOG_I("CreateMediaInfo uid is: %{public}" PRId32 " instanceId is: %{public}" PRIu64, uid, instanceId);
346     std::lock_guard<std::mutex> lock(collectMut_);
347     auto instanceIdMap = idMap_.find(instanceId);
348     if (instanceIdMap != idMap_.end()) {
349         MEDIA_LOG_I("instanceId already exists id idMap_");
350         return MSERR_INVALID_VAL;
351     } else {
352         MEDIA_LOG_I("CreateMediaInfo not found instanceId in idMap_, add the instanceId to idMap_");
353         std::pair<CallType, int32_t> insertToMapPair(callType, uid);
354         idMap_[instanceId] = insertToMapPair;
355     }
356     std::shared_ptr<Meta> meta = std::make_shared<Meta>();
357     std::pair<uint64_t, std::shared_ptr<Meta>> metaAppIdPair(instanceId, meta);
358     auto ctUidToMediaInfo = mediaInfoMap_.find(callType);
359     if (ctUidToMediaInfo != mediaInfoMap_.end()) {
360         auto it = ctUidToMediaInfo->second.find(uid);
361         if (it != ctUidToMediaInfo->second.end()) {
362             it->second.push_back(metaAppIdPair);
363             MEDIA_LOG_I("CreateMediaInfo: Successfully inserted metaAppIdPair for uid ");
364         } else {
365             ctUidToMediaInfo->second[uid].push_back(metaAppIdPair);
366             MEDIA_LOG_I("CreateMediaInfo: Successfully created new list for uid and inserted metaAppIdPair.");
367         }
368     } else {
369         mediaInfoMap_[callType][uid].push_back(metaAppIdPair);
370         MEDIA_LOG_I("CreateMediaInfo: Successfully created new list for callType and uid ");
371     }
372     return MSERR_OK;
373 }
374 
AppendMediaInfo(const std::shared_ptr<Meta> & meta,uint64_t instanceId)375 int32_t AppendMediaInfo(const std::shared_ptr<Meta>& meta, uint64_t instanceId)
376 {
377     MEDIA_LOG_I("AppendMediaInfo.");
378     if (meta == nullptr || meta->Empty()) {
379         MEDIA_LOG_I("Insert meta is empty.");
380         return MSERR_INVALID_OPERATION;
381     }
382     std::lock_guard<std::mutex> lock(collectMut_);
383     auto idMapIt = idMap_.find(instanceId);
384     if (idMapIt == idMap_.end()) {
385         MEDIA_LOG_I("Not found instanceId when append meta, instanceId is : %{public}" PRIu64, instanceId);
386         return MSERR_INVALID_VAL;
387     }
388     CallType ct = idMapIt->second.first;
389     int32_t uid = idMapIt->second.second;
390     auto ctUidToMediaInfo = mediaInfoMap_.find(ct);
391     if (ctUidToMediaInfo == mediaInfoMap_.end()) {
392         MEDIA_LOG_I("Not found calltype when append meta, calltype is : %{public}d", static_cast<CallType>(ct));
393         return MSERR_INVALID_OPERATION;
394     }
395     auto it = ctUidToMediaInfo->second.find(uid);
396     if (it == ctUidToMediaInfo->second.end()) {
397         MEDIA_LOG_I("Not found uid when append meta, uid is : %{public}" PRId32, uid);
398         return MSERR_INVALID_OPERATION;
399     }
400     auto& instanceList = it->second;
401     for (const auto& instancePair : instanceList) {
402         if (instancePair.first == instanceId) {
403             auto arg = meta->begin();
404             while (arg != meta->end()) {
405                 instancePair.second->SetData(arg->first, arg->second);
406                 ++arg;
407             }
408             break;
409         }
410     }
411     return MSERR_OK;
412 }
413 
ReportMediaInfo(uint64_t instanceId)414 int32_t ReportMediaInfo(uint64_t instanceId)
415 {
416     MEDIA_LOG_I("Report.");
417     MEDIA_LOG_I("Delete media info instanceId is: %{public}" PRIu64, instanceId);
418     if (!CollectReportMediaInfo(instanceId)) {
419         MEDIA_LOG_I("Collect media info fail.");
420         return MSERR_INVALID_OPERATION;
421     }
422     std::lock_guard<std::mutex> lock(reportMut_);
423     if (g_reachMaxMapSize) {
424         MEDIA_LOG_I("Event data size exceeds 100, report the event");
425         g_reachMaxMapSize = false;
426         return StatisticsEventReport();
427     }
428     auto currentTime = std::chrono::system_clock::now();
429     auto diff = currentTime - currentTime_;
430     auto hour = std::chrono::duration_cast<std::chrono::hours>(diff).count();
431     if (hour >= HOURS_BETWEEN_REPORTS) {
432         MEDIA_LOG_I("Over 4 hours, report the event");
433         return StatisticsEventReport();
434     }
435     return MSERR_OK;
436 }
437 
MediaTrace(const std::string & funcName)438 MediaTrace::MediaTrace(const std::string &funcName)
439 {
440     StartTrace(HITRACE_TAG_ZMEDIA, funcName);
441     isSync_ = true;
442 }
443 
TraceBegin(const std::string & funcName,int32_t taskId)444 void MediaTrace::TraceBegin(const std::string &funcName, int32_t taskId)
445 {
446     StartAsyncTrace(HITRACE_TAG_ZMEDIA, funcName, taskId);
447 }
448 
TraceEnd(const std::string & funcName,int32_t taskId)449 void MediaTrace::TraceEnd(const std::string &funcName, int32_t taskId)
450 {
451     FinishAsyncTrace(HITRACE_TAG_ZMEDIA, funcName, taskId);
452 }
453 
CounterTrace(const std::string & varName,int32_t val)454 void MediaTrace::CounterTrace(const std::string &varName, int32_t val)
455 {
456     CountTrace(HITRACE_TAG_ZMEDIA, varName, val);
457 }
458 
~MediaTrace()459 MediaTrace::~MediaTrace()
460 {
461     if (isSync_) {
462         FinishTrace(HITRACE_TAG_ZMEDIA);
463     }
464 }
465 } // namespace Media
466 } // namespace OHOS
467