1049e185fSopenharmony_ci/*
2049e185fSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3049e185fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4049e185fSopenharmony_ci * you may not use this file except in compliance with the License.
5049e185fSopenharmony_ci * You may obtain a copy of the License at
6049e185fSopenharmony_ci *
7049e185fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8049e185fSopenharmony_ci *
9049e185fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10049e185fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11049e185fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12049e185fSopenharmony_ci * See the License for the specific language governing permissions and
13049e185fSopenharmony_ci * limitations under the License.
14049e185fSopenharmony_ci */
15049e185fSopenharmony_ci
16049e185fSopenharmony_ci#include <media_dfx.h>
17049e185fSopenharmony_ci#include <unistd.h>
18049e185fSopenharmony_ci#include "securec.h"
19049e185fSopenharmony_ci#include "hitrace_meter.h"
20049e185fSopenharmony_ci#include "hitrace/tracechain.h"
21049e185fSopenharmony_ci#include "ipc_skeleton.h"
22049e185fSopenharmony_ci#include "media_utils.h"
23049e185fSopenharmony_ci#include "hitrace/tracechain.h"
24049e185fSopenharmony_ci#include "common/log.h"
25049e185fSopenharmony_ci#include "common/media_core.h"
26049e185fSopenharmony_ci#include "meta/any.h"
27049e185fSopenharmony_ci#include <cstdint>
28049e185fSopenharmony_ci
29049e185fSopenharmony_cinamespace {
30049e185fSopenharmony_ci    constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_SYSTEM_PLAYER, "MediaDFX" };
31049e185fSopenharmony_ci
32049e185fSopenharmony_ci    constexpr uint32_t MAX_STRING_SIZE = 256;
33049e185fSopenharmony_ci    constexpr int64_t HOURS_BETWEEN_REPORTS = 4;
34049e185fSopenharmony_ci    constexpr int64_t MAX_MAP_SIZE = 100;
35049e185fSopenharmony_ci
36049e185fSopenharmony_ci    std::mutex collectMut_;
37049e185fSopenharmony_ci    std::mutex reportMut_;
38049e185fSopenharmony_ci    std::map<OHOS::Media::CallType,
39049e185fSopenharmony_ci        std::map<int32_t, std::list<std::pair<uint64_t, std::shared_ptr<OHOS::Media::Meta>>>>> mediaInfoMap_;
40049e185fSopenharmony_ci    std::map<OHOS::Media::CallType,
41049e185fSopenharmony_ci        std::map<int32_t, std::list<std::pair<uint64_t, std::shared_ptr<OHOS::Media::Meta>>>>> reportMediaInfoMap_;
42049e185fSopenharmony_ci    std::map<uint64_t, std::pair<OHOS::Media::CallType, int32_t>> idMap_;
43049e185fSopenharmony_ci    std::chrono::system_clock::time_point currentTime_ = std::chrono::system_clock::now();
44049e185fSopenharmony_ci    bool g_reachMaxMapSize {false};
45049e185fSopenharmony_ci
46049e185fSopenharmony_ci    bool CollectReportMediaInfo(uint64_t instanceId)
47049e185fSopenharmony_ci    {
48049e185fSopenharmony_ci        OHOS::Media::CallType ct;
49049e185fSopenharmony_ci        int32_t uid;
50049e185fSopenharmony_ci        std::pair<uint64_t, std::shared_ptr<OHOS::Media::Meta>> metaAppIdPair;
51049e185fSopenharmony_ci        {
52049e185fSopenharmony_ci            std::lock_guard<std::mutex> lock(collectMut_);
53049e185fSopenharmony_ci            MEDIA_LOG_I("CollectReportMediaInfo, instanceId is : %{public}" PRIu64, instanceId);
54049e185fSopenharmony_ci            auto idMapIt = idMap_.find(instanceId);
55049e185fSopenharmony_ci            if (idMapIt == idMap_.end()) {
56049e185fSopenharmony_ci                MEDIA_LOG_W("Not found instanceId in idMap, instanceId is : %{public}" PRIu64, instanceId);
57049e185fSopenharmony_ci                return false;
58049e185fSopenharmony_ci            }
59049e185fSopenharmony_ci            ct = idMapIt->second.first;
60049e185fSopenharmony_ci            uid = idMapIt->second.second;
61049e185fSopenharmony_ci            idMap_.erase(idMapIt);
62049e185fSopenharmony_ci            auto ctUidToMediaInfo = mediaInfoMap_.find(ct);
63049e185fSopenharmony_ci            if (ctUidToMediaInfo == mediaInfoMap_.end()) {
64049e185fSopenharmony_ci                MEDIA_LOG_W("Not found calltype, calltype is : %{public}d", static_cast<OHOS::Media::CallType>(ct));
65049e185fSopenharmony_ci                return false;
66049e185fSopenharmony_ci            }
67049e185fSopenharmony_ci            auto uidToMediaInfo = ctUidToMediaInfo->second.find(uid);
68049e185fSopenharmony_ci            if (uidToMediaInfo == ctUidToMediaInfo->second.end()) {
69049e185fSopenharmony_ci                MEDIA_LOG_W("Not found uid in mediaInfoMap_, uid is : %{public}" PRId32, uid);
70049e185fSopenharmony_ci                return false;
71049e185fSopenharmony_ci            }
72049e185fSopenharmony_ci            auto& instanceList = uidToMediaInfo->second;
73049e185fSopenharmony_ci            for (const auto& instancePair : instanceList) {
74049e185fSopenharmony_ci                if (instancePair.first == instanceId) {
75049e185fSopenharmony_ci                    metaAppIdPair = instancePair;
76049e185fSopenharmony_ci                    instanceList.remove(instancePair);
77049e185fSopenharmony_ci                    break;
78049e185fSopenharmony_ci                }
79049e185fSopenharmony_ci            }
80049e185fSopenharmony_ci        }
81049e185fSopenharmony_ci        std::lock_guard<std::mutex> lock(reportMut_);
82049e185fSopenharmony_ci        auto reportCtUidToMediaInfo  = reportMediaInfoMap_.find(ct);
83049e185fSopenharmony_ci        if (reportCtUidToMediaInfo != reportMediaInfoMap_.end()) {
84049e185fSopenharmony_ci            auto it = reportCtUidToMediaInfo->second.find(uid);
85049e185fSopenharmony_ci            if (it != reportCtUidToMediaInfo->second.end()) {
86049e185fSopenharmony_ci                it->second.push_back(metaAppIdPair);
87049e185fSopenharmony_ci            } else {
88049e185fSopenharmony_ci                reportCtUidToMediaInfo->second[uid].push_back(metaAppIdPair);
89049e185fSopenharmony_ci            }
90049e185fSopenharmony_ci        } else {
91049e185fSopenharmony_ci            reportMediaInfoMap_[ct][uid].push_back(metaAppIdPair);
92049e185fSopenharmony_ci        }
93049e185fSopenharmony_ci        g_reachMaxMapSize = (reportMediaInfoMap_[ct].size() >= MAX_MAP_SIZE);
94049e185fSopenharmony_ci        return true;
95049e185fSopenharmony_ci    }
96049e185fSopenharmony_ci
97049e185fSopenharmony_ci    int32_t StatisticsEventReport()
98049e185fSopenharmony_ci    {
99049e185fSopenharmony_ci        MEDIA_LOG_I("StatisticsEventReport.");
100049e185fSopenharmony_ci        if (reportMediaInfoMap_.empty()) {
101049e185fSopenharmony_ci            MEDIA_LOG_I("reportMediaInfoMap_ is empty, can't report");
102049e185fSopenharmony_ci            return OHOS::Media::MSERR_INVALID_OPERATION;
103049e185fSopenharmony_ci        }
104049e185fSopenharmony_ci        OHOS::Media::MediaEvent event;
105049e185fSopenharmony_ci        for (const auto &it : reportMediaInfoMap_) {
106049e185fSopenharmony_ci            event.CommonStatisicsEventWrite(it.first, OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC, it.second);
107049e185fSopenharmony_ci        }
108049e185fSopenharmony_ci        auto currentTime = std::chrono::system_clock::now();
109049e185fSopenharmony_ci        currentTime_ = currentTime;
110049e185fSopenharmony_ci        reportMediaInfoMap_.clear();
111049e185fSopenharmony_ci        return OHOS::Media::MSERR_OK;
112049e185fSopenharmony_ci    }
113049e185fSopenharmony_ci}
114049e185fSopenharmony_ci
115049e185fSopenharmony_cinamespace OHOS {
116049e185fSopenharmony_cinamespace Media {
117049e185fSopenharmony_ciusing namespace OHOS::HiviewDFX;
118049e185fSopenharmony_cibool MediaEvent::CreateMsg(const char *format, ...)
119049e185fSopenharmony_ci{
120049e185fSopenharmony_ci    va_list args;
121049e185fSopenharmony_ci    va_start(args, format);
122049e185fSopenharmony_ci    char msg[MAX_STRING_SIZE] = {0};
123049e185fSopenharmony_ci    auto ret = vsnprintf_s(msg, sizeof(msg), sizeof(msg) - 1, format, args);
124049e185fSopenharmony_ci    va_end(args);
125049e185fSopenharmony_ci    msg_ = msg;
126049e185fSopenharmony_ci    return ret < 0 ? false : true;
127049e185fSopenharmony_ci}
128049e185fSopenharmony_ci
129049e185fSopenharmony_civoid MediaEvent::EventWrite(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
130049e185fSopenharmony_ci    std::string module)
131049e185fSopenharmony_ci{
132049e185fSopenharmony_ci    int32_t pid = getpid();
133049e185fSopenharmony_ci    uint32_t uid = getuid();
134049e185fSopenharmony_ci    HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA, eventName, type,
135049e185fSopenharmony_ci        "PID", pid,
136049e185fSopenharmony_ci        "UID", uid,
137049e185fSopenharmony_ci        "MODULE", module,
138049e185fSopenharmony_ci        "MSG", msg_);
139049e185fSopenharmony_ci}
140049e185fSopenharmony_ci
141049e185fSopenharmony_civoid MediaEvent::EventWriteWithAppInfo(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
142049e185fSopenharmony_ci    std::string module, std::string status, int32_t appUid, int32_t appPid)
143049e185fSopenharmony_ci{
144049e185fSopenharmony_ci    int32_t pid = getpid();
145049e185fSopenharmony_ci    uint32_t uid = getuid();
146049e185fSopenharmony_ci    HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA, eventName, type,
147049e185fSopenharmony_ci        "PID", pid,
148049e185fSopenharmony_ci        "UID", uid,
149049e185fSopenharmony_ci        "MODULE", module,
150049e185fSopenharmony_ci        "MSG", msg_,
151049e185fSopenharmony_ci        "APP_PID", appPid,
152049e185fSopenharmony_ci        "APP_UID", appUid,
153049e185fSopenharmony_ci        "STATUS", status);
154049e185fSopenharmony_ci}
155049e185fSopenharmony_ci
156049e185fSopenharmony_civoid MediaEvent::EventWriteBundleName(std::string eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
157049e185fSopenharmony_ci    std::string module, std::string status, int32_t appUid, int32_t appPid, std::string bundleName)
158049e185fSopenharmony_ci{
159049e185fSopenharmony_ci    int32_t pid = getpid();
160049e185fSopenharmony_ci    uint32_t uid = getuid();
161049e185fSopenharmony_ci    HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA, eventName, type,
162049e185fSopenharmony_ci                    "PID", pid,
163049e185fSopenharmony_ci                    "UID", uid,
164049e185fSopenharmony_ci                    "MODULE", module,
165049e185fSopenharmony_ci                    "MSG", msg_,
166049e185fSopenharmony_ci                    "APP_PID", appPid,
167049e185fSopenharmony_ci                    "APP_UID", appUid,
168049e185fSopenharmony_ci                    "STATUS", status,
169049e185fSopenharmony_ci                    "BUNDLE", bundleName);
170049e185fSopenharmony_ci}
171049e185fSopenharmony_ci
172049e185fSopenharmony_civoid MediaEvent::SourceEventWrite(const std::string& eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
173049e185fSopenharmony_ci    const std::string& appName, uint64_t instanceId, const std::string& callerType, int8_t sourceType,
174049e185fSopenharmony_ci    const std::string& sourceUrl, const std::string& errMsg)
175049e185fSopenharmony_ci{
176049e185fSopenharmony_ci    std::string instanceIdStr = std::to_string(instanceId);
177049e185fSopenharmony_ci    HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA, eventName, type,
178049e185fSopenharmony_ci                    "APP_NAME", appName,
179049e185fSopenharmony_ci                    "INSTANCE_ID", instanceIdStr,
180049e185fSopenharmony_ci                    "CALLER_TYPE", callerType,
181049e185fSopenharmony_ci                    "SOURCE_TYPE", sourceType,
182049e185fSopenharmony_ci                    "SOURCE_URI", sourceUrl,
183049e185fSopenharmony_ci                    "ERROR_MESG", errMsg);
184049e185fSopenharmony_ci}
185049e185fSopenharmony_ci
186049e185fSopenharmony_civoid MediaEvent::ScreenCaptureEventWrite(const std::string& eventName, OHOS::HiviewDFX::HiSysEvent::EventType type,
187049e185fSopenharmony_ci    const std::string& appName, uint64_t instanceId, int8_t captureMode, int8_t dataMode, int32_t errorCode,
188049e185fSopenharmony_ci    const std::string& errorMessage)
189049e185fSopenharmony_ci{
190049e185fSopenharmony_ci    std::string instanceIdStr = std::to_string(instanceId);
191049e185fSopenharmony_ci    HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA, eventName, type,
192049e185fSopenharmony_ci                    "APP_NAME", appName,
193049e185fSopenharmony_ci                    "INSTANCE_ID", instanceIdStr,
194049e185fSopenharmony_ci                    "CAPTURE_MODE", captureMode,
195049e185fSopenharmony_ci                    "DATA_MODE", dataMode,
196049e185fSopenharmony_ci                    "ERROR_CODE", errorCode,
197049e185fSopenharmony_ci                    "ERROR_MESG", errorMessage);
198049e185fSopenharmony_ci}
199049e185fSopenharmony_ci
200049e185fSopenharmony_civoid MediaEvent::CommonStatisicsEventWrite(CallType callType, OHOS::HiviewDFX::HiSysEvent::EventType type,
201049e185fSopenharmony_ci    const std::map<int32_t, std::list<std::pair<uint64_t, std::shared_ptr<Meta>>>>& infoMap)
202049e185fSopenharmony_ci{
203049e185fSopenharmony_ci    MEDIA_LOG_I("MediaEvent::CommonStatisicsEventWrite");
204049e185fSopenharmony_ci    if (infoMap.empty()) {
205049e185fSopenharmony_ci        MEDIA_LOG_I("Player infoMap is empty.");
206049e185fSopenharmony_ci        return;
207049e185fSopenharmony_ci    }
208049e185fSopenharmony_ci    std::vector<std::string> infoArr;
209049e185fSopenharmony_ci#ifdef SUPPORT_JSON
210049e185fSopenharmony_ci    for (const auto& kv : infoMap) {
211049e185fSopenharmony_ci        json jsonArray;
212049e185fSopenharmony_ci        json eventInfoJson;
213049e185fSopenharmony_ci        json mediaEvents;
214049e185fSopenharmony_ci        for (const auto& listPair : kv.second) {
215049e185fSopenharmony_ci            json metaInfoJson;
216049e185fSopenharmony_ci            ParseOneEvent(listPair, metaInfoJson);
217049e185fSopenharmony_ci            mediaEvents.push_back(metaInfoJson);
218049e185fSopenharmony_ci        }
219049e185fSopenharmony_ci        eventInfoJson["appName"] = GetClientBundleName(kv.first);
220049e185fSopenharmony_ci        eventInfoJson["mediaEvents"] = mediaEvents;
221049e185fSopenharmony_ci        jsonArray.push_back(eventInfoJson);
222049e185fSopenharmony_ci        infoArr.push_back(jsonArray.dump());
223049e185fSopenharmony_ci    }
224049e185fSopenharmony_ci#endif
225049e185fSopenharmony_ci    StatisicsHiSysEventWrite(callType, type, infoArr);
226049e185fSopenharmony_ci}
227049e185fSopenharmony_ci
228049e185fSopenharmony_ci
229049e185fSopenharmony_ci#ifdef SUPPORT_JSON
230049e185fSopenharmony_civoid MediaEvent::ParseOneEvent(const std::pair<uint64_t, std::shared_ptr<OHOS::Media::Meta>> &listPair,
231049e185fSopenharmony_ci    json& metaInfoJson)
232049e185fSopenharmony_ci{
233049e185fSopenharmony_ci    for (auto it = listPair.second->begin(); it != listPair.second->end(); ++it) {
234049e185fSopenharmony_ci        Any valueType = OHOS::Media::GetDefaultAnyValue(it->first);
235049e185fSopenharmony_ci        if (Any::IsSameTypeWith<int32_t>(valueType)) {
236049e185fSopenharmony_ci            int32_t intVal;
237049e185fSopenharmony_ci            if (listPair.second->GetData(it->first, intVal)) {
238049e185fSopenharmony_ci                metaInfoJson[it->first] = std::to_string(intVal);
239049e185fSopenharmony_ci            }
240049e185fSopenharmony_ci        } else if (Any::IsSameTypeWith<uint32_t>(valueType)) {
241049e185fSopenharmony_ci            uint32_t uintVal;
242049e185fSopenharmony_ci            if (listPair.second->GetData(it->first, uintVal)) {
243049e185fSopenharmony_ci                metaInfoJson[it->first] = std::to_string(uintVal);
244049e185fSopenharmony_ci            }
245049e185fSopenharmony_ci        } else if (Any::IsSameTypeWith<uint64_t>(valueType)) {
246049e185fSopenharmony_ci            uint64_t uintVal;
247049e185fSopenharmony_ci            if (listPair.second->GetData(it->first, uintVal)) {
248049e185fSopenharmony_ci                metaInfoJson[it->first] = std::to_string(uintVal);
249049e185fSopenharmony_ci            }
250049e185fSopenharmony_ci        } else if (Any::IsSameTypeWith<std::string>(valueType)) {
251049e185fSopenharmony_ci            metaInfoJson[it->first] = AnyCast<std::string>(it->second);
252049e185fSopenharmony_ci        } else if (Any::IsSameTypeWith<int8_t>(valueType)) {
253049e185fSopenharmony_ci            int8_t intVal;
254049e185fSopenharmony_ci            if (listPair.second->GetData(it->first, intVal)) {
255049e185fSopenharmony_ci                metaInfoJson[it->first] = std::to_string(intVal);
256049e185fSopenharmony_ci            }
257049e185fSopenharmony_ci        } else if (Any::IsSameTypeWith<bool>(valueType)) {
258049e185fSopenharmony_ci            bool isTrue;
259049e185fSopenharmony_ci            if (listPair.second->GetData(it->first, isTrue)) {
260049e185fSopenharmony_ci                metaInfoJson[it->first] = isTrue ? "true" : "false";
261049e185fSopenharmony_ci            }
262049e185fSopenharmony_ci        } else {
263049e185fSopenharmony_ci            MEDIA_LOG_I("not found type matched with it->first: %{public}s", it->first.c_str());
264049e185fSopenharmony_ci        }
265049e185fSopenharmony_ci    }
266049e185fSopenharmony_ci}
267049e185fSopenharmony_ci#endif
268049e185fSopenharmony_ci
269049e185fSopenharmony_civoid MediaEvent::StatisicsHiSysEventWrite(CallType callType, OHOS::HiviewDFX::HiSysEvent::EventType type,
270049e185fSopenharmony_ci    const std::vector<std::string>& infoArr)
271049e185fSopenharmony_ci{
272049e185fSopenharmony_ci    MEDIA_LOG_I("MediaEvent::StatisicsHiSysEventWrite");
273049e185fSopenharmony_ci    std::string eventName;
274049e185fSopenharmony_ci    switch (callType) {
275049e185fSopenharmony_ci        case CallType::AVPLAYER:
276049e185fSopenharmony_ci            eventName = "PLAYER_COMMON_STATISTICS";
277049e185fSopenharmony_ci            break;
278049e185fSopenharmony_ci        case CallType::AVRECORDER:
279049e185fSopenharmony_ci            eventName = "RECORDER_STATISTICS";
280049e185fSopenharmony_ci            break;
281049e185fSopenharmony_ci        case CallType::SCREEN_CAPTRUER:
282049e185fSopenharmony_ci            eventName = "SCREEN_CAPTURE_STATISTICS";
283049e185fSopenharmony_ci            break;
284049e185fSopenharmony_ci        case CallType::AVTRANSCODER:
285049e185fSopenharmony_ci            eventName = "TRANSCODER_STATISTICS";
286049e185fSopenharmony_ci            break;
287049e185fSopenharmony_ci        default:
288049e185fSopenharmony_ci            return;
289049e185fSopenharmony_ci    }
290049e185fSopenharmony_ci    HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA, eventName, type,
291049e185fSopenharmony_ci                    "EVENTS", infoArr);
292049e185fSopenharmony_ci}
293049e185fSopenharmony_ci
294049e185fSopenharmony_civoid BehaviorEventWrite(std::string status, std::string module)
295049e185fSopenharmony_ci{
296049e185fSopenharmony_ci    MediaEvent event;
297049e185fSopenharmony_ci    if (event.CreateMsg("%s, current state is: %s", "state change", status.c_str())) {
298049e185fSopenharmony_ci        event.EventWrite("PLAYER_STATE", OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, module);
299049e185fSopenharmony_ci    }
300049e185fSopenharmony_ci}
301049e185fSopenharmony_ci
302049e185fSopenharmony_civoid BehaviorEventWriteForScreenCapture(std::string status, std::string module, int32_t appUid, int32_t appPid)
303049e185fSopenharmony_ci{
304049e185fSopenharmony_ci    MediaEvent event;
305049e185fSopenharmony_ci    if (event.CreateMsg("%s, current state is: %s", "state change", status.c_str())) {
306049e185fSopenharmony_ci        event.EventWriteWithAppInfo("PLAYER_STATE", OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
307049e185fSopenharmony_ci            module, status, appUid, appPid);
308049e185fSopenharmony_ci    }
309049e185fSopenharmony_ci}
310049e185fSopenharmony_ci
311049e185fSopenharmony_civoid StatisticEventWriteBundleName(std::string status, std::string module)
312049e185fSopenharmony_ci{
313049e185fSopenharmony_ci    MediaEvent event;
314049e185fSopenharmony_ci    int32_t appUid = IPCSkeleton::GetCallingUid();
315049e185fSopenharmony_ci    int32_t appPid = IPCSkeleton::GetCallingPid();
316049e185fSopenharmony_ci    std::string bundleName = GetClientBundleName(appUid);
317049e185fSopenharmony_ci    if (event.CreateMsg("%s is invoke %s", bundleName.c_str(), module.c_str())) {
318049e185fSopenharmony_ci        event.EventWriteBundleName("PLAYER_STATISTICS", OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,
319049e185fSopenharmony_ci            module, status, appUid, appPid, bundleName);
320049e185fSopenharmony_ci    }
321049e185fSopenharmony_ci}
322049e185fSopenharmony_ci
323049e185fSopenharmony_civoid FaultEventWrite(std::string msg, std::string module)
324049e185fSopenharmony_ci{
325049e185fSopenharmony_ci    MediaEvent event;
326049e185fSopenharmony_ci    if (event.CreateMsg("%s", msg.c_str())) {
327049e185fSopenharmony_ci        event.EventWrite("PLAYER_ERR", OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, module);
328049e185fSopenharmony_ci    }
329049e185fSopenharmony_ci}
330049e185fSopenharmony_ci
331049e185fSopenharmony_civoid FaultSourceEventWrite(const std::string& appName, uint64_t instanceId, const std::string& callerType,
332049e185fSopenharmony_ci    int8_t sourceType, const std::string& sourceUrl, const std::string& errorMessage)
333049e185fSopenharmony_ci{
334049e185fSopenharmony_ci    MediaEvent event;
335049e185fSopenharmony_ci    event.SourceEventWrite("SOURCE_FAILURE", OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, appName, instanceId,
336049e185fSopenharmony_ci        callerType, sourceType, sourceUrl, errorMessage);
337049e185fSopenharmony_ci}
338049e185fSopenharmony_ci
339049e185fSopenharmony_civoid FaultScreenCaptureEventWrite(const std::string& appName, uint64_t instanceId, int8_t captureMode, int8_t dataMode,
340049e185fSopenharmony_ci    int32_t errorCode, const std::string& errorMessage)
341049e185fSopenharmony_ci{
342049e185fSopenharmony_ci    MediaEvent event;
343049e185fSopenharmony_ci    event.ScreenCaptureEventWrite("SCREEN_CAPTURE_FAILURE", OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, appName,
344049e185fSopenharmony_ci        instanceId, captureMode, dataMode, errorCode, errorMessage);
345049e185fSopenharmony_ci}
346049e185fSopenharmony_ci
347049e185fSopenharmony_ciint32_t CreateMediaInfo(CallType callType, int32_t uid, uint64_t instanceId)
348049e185fSopenharmony_ci{
349049e185fSopenharmony_ci    MEDIA_LOG_I("CreateMediaInfo uid is: %{public}" PRId32 " instanceId is: %{public}" PRIu64, uid, instanceId);
350049e185fSopenharmony_ci    std::lock_guard<std::mutex> lock(collectMut_);
351049e185fSopenharmony_ci    auto instanceIdMap = idMap_.find(instanceId);
352049e185fSopenharmony_ci    if (instanceIdMap != idMap_.end()) {
353049e185fSopenharmony_ci        MEDIA_LOG_I("instanceId already exists id idMap_");
354049e185fSopenharmony_ci        return MSERR_INVALID_VAL;
355049e185fSopenharmony_ci    } else {
356049e185fSopenharmony_ci        MEDIA_LOG_I("CreateMediaInfo not found instanceId in idMap_, add the instanceId to idMap_");
357049e185fSopenharmony_ci        std::pair<CallType, int32_t> insertToMapPair(callType, uid);
358049e185fSopenharmony_ci        idMap_[instanceId] = insertToMapPair;
359049e185fSopenharmony_ci    }
360049e185fSopenharmony_ci    std::shared_ptr<Meta> meta = std::make_shared<Meta>();
361049e185fSopenharmony_ci    std::pair<uint64_t, std::shared_ptr<Meta>> metaAppIdPair(instanceId, meta);
362049e185fSopenharmony_ci    auto ctUidToMediaInfo = mediaInfoMap_.find(callType);
363049e185fSopenharmony_ci    if (ctUidToMediaInfo != mediaInfoMap_.end()) {
364049e185fSopenharmony_ci        auto it = ctUidToMediaInfo->second.find(uid);
365049e185fSopenharmony_ci        if (it != ctUidToMediaInfo->second.end()) {
366049e185fSopenharmony_ci            it->second.push_back(metaAppIdPair);
367049e185fSopenharmony_ci            MEDIA_LOG_I("CreateMediaInfo: Successfully inserted metaAppIdPair for uid ");
368049e185fSopenharmony_ci        } else {
369049e185fSopenharmony_ci            ctUidToMediaInfo->second[uid].push_back(metaAppIdPair);
370049e185fSopenharmony_ci            MEDIA_LOG_I("CreateMediaInfo: Successfully created new list for uid and inserted metaAppIdPair.");
371049e185fSopenharmony_ci        }
372049e185fSopenharmony_ci    } else {
373049e185fSopenharmony_ci        mediaInfoMap_[callType][uid].push_back(metaAppIdPair);
374049e185fSopenharmony_ci        MEDIA_LOG_I("CreateMediaInfo: Successfully created new list for callType and uid ");
375049e185fSopenharmony_ci    }
376049e185fSopenharmony_ci    return MSERR_OK;
377049e185fSopenharmony_ci}
378049e185fSopenharmony_ci
379049e185fSopenharmony_ciint32_t AppendMediaInfo(const std::shared_ptr<Meta>& meta, uint64_t instanceId)
380049e185fSopenharmony_ci{
381049e185fSopenharmony_ci    MEDIA_LOG_I("AppendMediaInfo.");
382049e185fSopenharmony_ci    if (meta == nullptr || meta->Empty()) {
383049e185fSopenharmony_ci        MEDIA_LOG_I("Insert meta is empty.");
384049e185fSopenharmony_ci        return MSERR_INVALID_OPERATION;
385049e185fSopenharmony_ci    }
386049e185fSopenharmony_ci    std::lock_guard<std::mutex> lock(collectMut_);
387049e185fSopenharmony_ci    auto idMapIt = idMap_.find(instanceId);
388049e185fSopenharmony_ci    if (idMapIt == idMap_.end()) {
389049e185fSopenharmony_ci        MEDIA_LOG_I("Not found instanceId when append meta, instanceId is : %{public}" PRIu64, instanceId);
390049e185fSopenharmony_ci        return MSERR_INVALID_VAL;
391049e185fSopenharmony_ci    }
392049e185fSopenharmony_ci    CallType ct = idMapIt->second.first;
393049e185fSopenharmony_ci    int32_t uid = idMapIt->second.second;
394049e185fSopenharmony_ci    auto ctUidToMediaInfo = mediaInfoMap_.find(ct);
395049e185fSopenharmony_ci    if (ctUidToMediaInfo == mediaInfoMap_.end()) {
396049e185fSopenharmony_ci        MEDIA_LOG_I("Not found calltype when append meta, calltype is : %{public}d", static_cast<CallType>(ct));
397049e185fSopenharmony_ci        return MSERR_INVALID_OPERATION;
398049e185fSopenharmony_ci    }
399049e185fSopenharmony_ci    auto it = ctUidToMediaInfo->second.find(uid);
400049e185fSopenharmony_ci    if (it == ctUidToMediaInfo->second.end()) {
401049e185fSopenharmony_ci        MEDIA_LOG_I("Not found uid when append meta, uid is : %{public}" PRId32, uid);
402049e185fSopenharmony_ci        return MSERR_INVALID_OPERATION;
403049e185fSopenharmony_ci    }
404049e185fSopenharmony_ci    auto& instanceList = it->second;
405049e185fSopenharmony_ci    for (const auto& instancePair : instanceList) {
406049e185fSopenharmony_ci        if (instancePair.first == instanceId) {
407049e185fSopenharmony_ci            auto arg = meta->begin();
408049e185fSopenharmony_ci            while (arg != meta->end()) {
409049e185fSopenharmony_ci                instancePair.second->SetData(arg->first, arg->second);
410049e185fSopenharmony_ci                ++arg;
411049e185fSopenharmony_ci            }
412049e185fSopenharmony_ci            break;
413049e185fSopenharmony_ci        }
414049e185fSopenharmony_ci    }
415049e185fSopenharmony_ci    return MSERR_OK;
416049e185fSopenharmony_ci}
417049e185fSopenharmony_ci
418049e185fSopenharmony_ciint32_t ReportMediaInfo(uint64_t instanceId)
419049e185fSopenharmony_ci{
420049e185fSopenharmony_ci    MEDIA_LOG_I("Report.");
421049e185fSopenharmony_ci    MEDIA_LOG_I("Delete media info instanceId is: %{public}" PRIu64, instanceId);
422049e185fSopenharmony_ci    if (!CollectReportMediaInfo(instanceId)) {
423049e185fSopenharmony_ci        MEDIA_LOG_I("Collect media info fail.");
424049e185fSopenharmony_ci        return MSERR_INVALID_OPERATION;
425049e185fSopenharmony_ci    }
426049e185fSopenharmony_ci    std::lock_guard<std::mutex> lock(reportMut_);
427049e185fSopenharmony_ci    if (g_reachMaxMapSize) {
428049e185fSopenharmony_ci        MEDIA_LOG_I("Event data size exceeds 100, report the event");
429049e185fSopenharmony_ci        g_reachMaxMapSize = false;
430049e185fSopenharmony_ci        return StatisticsEventReport();
431049e185fSopenharmony_ci    }
432049e185fSopenharmony_ci    auto currentTime = std::chrono::system_clock::now();
433049e185fSopenharmony_ci    auto diff = currentTime - currentTime_;
434049e185fSopenharmony_ci    auto hour = std::chrono::duration_cast<std::chrono::hours>(diff).count();
435049e185fSopenharmony_ci    if (hour >= HOURS_BETWEEN_REPORTS) {
436049e185fSopenharmony_ci        MEDIA_LOG_I("Over 4 hours, report the event");
437049e185fSopenharmony_ci        return StatisticsEventReport();
438049e185fSopenharmony_ci    }
439049e185fSopenharmony_ci    return MSERR_OK;
440049e185fSopenharmony_ci}
441049e185fSopenharmony_ci
442049e185fSopenharmony_ciMediaTrace::MediaTrace(const std::string &funcName)
443049e185fSopenharmony_ci{
444049e185fSopenharmony_ci    StartTrace(HITRACE_TAG_ZMEDIA, funcName);
445049e185fSopenharmony_ci    isSync_ = true;
446049e185fSopenharmony_ci}
447049e185fSopenharmony_ci
448049e185fSopenharmony_civoid MediaTrace::TraceBegin(const std::string &funcName, int32_t taskId)
449049e185fSopenharmony_ci{
450049e185fSopenharmony_ci    StartAsyncTrace(HITRACE_TAG_ZMEDIA, funcName, taskId);
451049e185fSopenharmony_ci}
452049e185fSopenharmony_ci
453049e185fSopenharmony_civoid MediaTrace::TraceEnd(const std::string &funcName, int32_t taskId)
454049e185fSopenharmony_ci{
455049e185fSopenharmony_ci    FinishAsyncTrace(HITRACE_TAG_ZMEDIA, funcName, taskId);
456049e185fSopenharmony_ci}
457049e185fSopenharmony_ci
458049e185fSopenharmony_civoid MediaTrace::CounterTrace(const std::string &varName, int32_t val)
459049e185fSopenharmony_ci{
460049e185fSopenharmony_ci    CountTrace(HITRACE_TAG_ZMEDIA, varName, val);
461049e185fSopenharmony_ci}
462049e185fSopenharmony_ci
463049e185fSopenharmony_ciMediaTrace::~MediaTrace()
464049e185fSopenharmony_ci{
465049e185fSopenharmony_ci    if (isSync_) {
466049e185fSopenharmony_ci        FinishTrace(HITRACE_TAG_ZMEDIA);
467049e185fSopenharmony_ci    }
468049e185fSopenharmony_ci}
469049e185fSopenharmony_ci} // namespace Media
470049e185fSopenharmony_ci} // namespace OHOS
471