diff --git a/hisysevent.yaml b/hisysevent.yaml index 1fd6703a95f3c6f60823e93e7a9186749e71ec55..fc71b3f3b8fef3ae438bd6d5c27894a7322d2f99 100644 --- a/hisysevent.yaml +++ b/hisysevent.yaml @@ -115,4 +115,19 @@ DRM_CERTIFICATE_DOWNLOAD_INFO: PROCESS_RESULT: {type: STRING, desc: License process result} CALL_SERVER_TIME: {type: UINT32, desc: Call time} SERVER_COST_DURATION: {type: UINT32, desc: Processing time of the server} - SERVER_RESULT: {type: STRING, desc: Result} \ No newline at end of file + SERVER_RESULT: {type: STRING, desc: Result} +PLAYER_COMMON_STATISTICS: + __BASE: {type: STATISTIC, level: MINOR, desc: Player all info statistics} + EVENTS: {type: STRING, arrsize: 100, desc: Group of player events} + +RECORDER_STATISTICS: + __BASE: {type: STATISTIC, level: MINOR, desc: Recorder statistics} + EVENTS: {type: STRING, arrsize: 100, desc: Group of recorder events} + +SCREEN_CAPTURE_STATISTICS: + __BASE: {type: STATISTIC, level: MINOR, desc: Screen capture statistics} + EVENTS: {type: STRING, arrsize: 100, desc: Group of screen capture events} + +DRM_STATISTICS: + __BASE: {type: STATISTIC, level: MINOR, desc: DRM common statistics} + EVENTS: {type: STRING, arrsize: 100, desc: Group of DRM common events} diff --git a/services/engine/histreamer/player/BUILD.gn b/services/engine/histreamer/player/BUILD.gn index 3aa631a4416dfafe161b7386fbd67a867d3853cb..ea46e81c5be4f62d4c19d2b540bb77674557ea90 100644 --- a/services/engine/histreamer/player/BUILD.gn +++ b/services/engine/histreamer/player/BUILD.gn @@ -77,6 +77,7 @@ ohos_static_library("media_engine_histreamer_player") { "graphic_surface:surface", "graphic_surface:sync_fence", "hilog:libhilog", + "hitrace:libhitracechain", "ipc:ipc_single", "media_foundation:media_foundation", "safwk:system_ability_fwk", diff --git a/services/services/screen_capture/server/screen_capture_server.cpp b/services/services/screen_capture/server/screen_capture_server.cpp index 42bc73c47c212e96f35e52344b21a68c31e8c308..fb61f05a96b838502c2eb3b5d3013893664b1dcc 100644 --- a/services/services/screen_capture/server/screen_capture_server.cpp +++ b/services/services/screen_capture/server/screen_capture_server.cpp @@ -215,10 +215,29 @@ int32_t ScreenCaptureServer::ReportAVScreenCaptureUserChoice(int32_t sessionId, return MSERR_UNKNOWN; } +void ScreenCaptureServer::SetMetaDataReport() +{ + std::shared_ptr meta = std::make_shared(); + meta->SetData(Tag::SCREEN_CAPTURE_ERR_CODE, statisticalEventInfo_.errCode); + meta->SetData(Tag::SCREEN_CAPTURE_ERR_MSG, statisticalEventInfo_.errMsg); + meta->SetData(Tag::SCREEN_CAPTURE_DURATION, statisticalEventInfo_.captureDuration); + meta->SetData(Tag::SCREEN_CAPTURE_AV_TYPE, avType_); + meta->SetData(Tag::SCREEN_CAPTURE_DATA_TYPE, dataMode_); + meta->SetData(Tag::SCREEN_CAPTURE_USER_AGREE, statisticalEventInfo_.userAgree); + meta->SetData(Tag::SCREEN_CAPTURE_REQURE_MIC, statisticalEventInfo_.requireMic); + meta->SetData(Tag::SCREEN_CAPTURE_ENABLE_MIC, statisticalEventInfo_.enableMic); + meta->SetData(Tag::SCREEN_CAPTURE_VIDEO_RESOLUTION, statisticalEventInfo_.videoResolution); + meta->SetData(Tag::SCREEN_CAPTURE_STOP_REASON, statisticalEventInfo_.stopReason); + meta->SetData(Tag::SCREEN_CAPTURE_START_LATENCY, statisticalEventInfo_.startLatency); + AppendMediaInfo(meta); + Report(); +} + ScreenCaptureServer::ScreenCaptureServer() { MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this)); InitAppInfo(); + CreateMediaInfo(SCREEN_CAPTRUER, IPCSkeleton::GetCallingUid()); } ScreenCaptureServer::~ScreenCaptureServer() @@ -692,6 +711,21 @@ void ScreenCaptureServer::InitAppInfo() MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR "InitAppInfo end.", FAKE_POINTER(this)); } +int64_t ScreenCaptureServer::GetCurrentMillisecond() +{ + std::chrono::system_clock::duration duration = std::chrono::system_clock::now().time_since_epoch(); + int64_t time = std::chrono::duration_cast(duration).count(); + return time; +} + +void ScreenCaptureServer::SetErrorInfo(int32_t errCode, std::string errMsg, StopReason stopReason, bool userAgree) +{ + statisticalEventInfo_.errCode = errCode; + statisticalEventInfo_.errMsg = errMsg; + statisticalEventInfo_.stopReason = stopReason; + statisticalEventInfo_.userAgree = userAgree; +} + int32_t ScreenCaptureServer::RequestUserPrivacyAuthority() { MediaTrace trace("ScreenCaptureServer::RequestUserPrivacyAuthority"); @@ -719,6 +753,8 @@ int32_t ScreenCaptureServer::OnReceiveUserPrivacyAuthority(bool isAllowed) if (screenCaptureCb_ == nullptr) { MEDIA_LOGE("OnReceiveUserPrivacyAuthority failed, screenCaptureCb is nullptr, state:%{public}d", captureState_); captureState_ = AVScreenCaptureState::STOPPED; + SetErrorInfo(MSERR_UNKNOWN, "OnReceiveUserPrivacyAuthority failed, screenCaptureCb is nullptr", + StopReason::RECEIVE_USER_PRIVACY_AUTHORITY_FAILED, IsUserPrivacyAuthorityNeeded()); return MSERR_UNKNOWN; } @@ -847,9 +883,12 @@ int32_t ScreenCaptureServer::OnStartScreenCapture() ret = StartScreenCaptureFile(); } if (ret == MSERR_OK) { + int64_t endTime = GetCurrentMillisecond(); + statisticalEventInfo_.startLatency = static_cast(endTime - startTime_); MEDIA_LOGI("OnStartScreenCapture start success, dataType:%{public}d", captureConfig_.dataType); } else { MEDIA_LOGE("OnStartScreenCapture start failed, dataType:%{public}d", captureConfig_.dataType); + statisticalEventInfo_.startLatency = -1; } return ret; } @@ -900,6 +939,8 @@ void ScreenCaptureServer::PostStartScreenCapture(bool isSuccess) isPrivacyAuthorityEnabled_ = false; isSurfaceMode_ = false; captureState_ = AVScreenCaptureState::STOPPED; + SetErrorInfo(MSERR_UNKNOWN, "PostStartScreenCapture handle failure", + StopReason::POST_START_SCREENCAPTURE_HANDLE_FAILURE, IsUserPrivacyAuthorityNeeded()); } MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR "PostStartScreenCapture end.", FAKE_POINTER(this)); } @@ -934,6 +975,9 @@ int32_t ScreenCaptureServer::InitAudioCap(AudioCaptureInfo audioInfo) if (audioInfo.audioSource == AudioCaptureSourceType::SOURCE_DEFAULT || audioInfo.audioSource == AudioCaptureSourceType::MIC) { captureConfig_.audioInfo.micCapInfo = audioInfo; + if (audioInfo.audioSource == AudioCaptureSourceType::MIC) { + statisticalEventInfo_.requireMic = true; + } } else if (audioInfo.audioSource == AudioCaptureSourceType::ALL_PLAYBACK || audioInfo.audioSource == AudioCaptureSourceType::APP_PLAYBACK) { captureConfig_.audioInfo.innerCapInfo = audioInfo; @@ -957,6 +1001,8 @@ int32_t ScreenCaptureServer::InitVideoCap(VideoCaptureInfo videoInfo) captureConfig_.videoInfo.videoCapInfo = videoInfo; avType_ = (avType_ == AVScreenCaptureAvType::AUDIO_TYPE) ? AVScreenCaptureAvType::AV_TYPE : AVScreenCaptureAvType::VIDEO_TYPE; + statisticalEventInfo_.videoResolution = std::to_string(videoInfo.videoFrameWidth) + " * " + + std::to_string(videoInfo.videoFrameHeight); MEDIA_LOGI("InitVideoCap success width:%{public}d, height:%{public}d, source:%{public}d, state:%{public}d", videoInfo.videoFrameWidth, videoInfo.videoFrameHeight, videoInfo.videoSource, videoInfo.state); return MSERR_OK; @@ -1099,6 +1145,8 @@ int32_t ScreenCaptureServer::StartScreenCaptureInner(bool isPrivacyAuthorityEnab ret = RequestUserPrivacyAuthority(); if (ret != MSERR_OK) { captureState_ = AVScreenCaptureState::STOPPED; + SetErrorInfo(ret, "StartScreenCaptureInner RequestUserPrivacyAuthority failed", + StopReason::REQUEST_USER_PRIVACY_AUTHORITY_FAILED, IsUserPrivacyAuthorityNeeded()); MEDIA_LOGE("StartScreenCaptureInner RequestUserPrivacyAuthority failed"); return ret; } @@ -1307,6 +1355,8 @@ int32_t ScreenCaptureServer::StartScreenCapture(bool isPrivacyAuthorityEnabled) { MediaTrace trace("ScreenCaptureServer::StartScreenCapture"); std::lock_guard lock(mutex_); + startTime_ = GetCurrentMillisecond(); + statisticalEventInfo_.enableMic = isMicrophoneOn_; MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR "StartScreenCapture start, " "isPrivacyAuthorityEnabled:%{public}s, captureState:%{public}d.", FAKE_POINTER(this), isPrivacyAuthorityEnabled ? "true" : "false", captureState_); @@ -1731,6 +1781,9 @@ int32_t ScreenCaptureServer::SetMicrophoneEnabled(bool isMicrophone) MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR "SetMicrophoneEnabled isMicrophoneOn_:%{public}d, " "new isMicrophone:%{public}d", FAKE_POINTER(this), isMicrophoneOn_, isMicrophone); isMicrophoneOn_ = isMicrophone; + if (isMicrophone) { + statisticalEventInfo_.enableMic = true; + } if (captureState_ == AVScreenCaptureState::STARTED) { if (micAudioCapture_ != nullptr) { micAudioCapture_->SetIsMuted(!isMicrophone); @@ -1851,6 +1904,7 @@ int32_t ScreenCaptureServer::StopScreenCaptureInner(AVScreenCaptureStateCode sta captureState_ = AVScreenCaptureState::STOPPED; isSurfaceMode_ = false; surface_ = nullptr; + SetErrorInfo(MSERR_OK, "normal stopped", StopReason::NORMAL_STOPPED, IsUserPrivacyAuthorityNeeded()); return MSERR_OK; } CHECK_AND_RETURN_RET(captureState_ != AVScreenCaptureState::STOPPED, MSERR_OK); @@ -1868,6 +1922,7 @@ int32_t ScreenCaptureServer::StopScreenCaptureInner(AVScreenCaptureStateCode sta } CHECK_AND_RETURN_RET_LOG(captureState_ == AVScreenCaptureState::STARTED, ret, "state:%{public}d", captureState_); captureState_ = AVScreenCaptureState::STOPPED; + SetErrorInfo(MSERR_OK, "normal stopped", StopReason::NORMAL_STOPPED, IsUserPrivacyAuthorityNeeded()); PostStopScreenCapture(stateCode); MEDIA_LOGI("ScreenCaptureServer: 0x%{public}06" PRIXPTR "StopScreenCaptureInner end.", FAKE_POINTER(this)); return ret; @@ -1913,6 +1968,9 @@ int32_t ScreenCaptureServer::StopScreenCapture() std::lock_guard lock(mutex_); int32_t ret = StopScreenCaptureInner(AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID); + int64_t endTime = GetCurrentMillisecond(); + statisticalEventInfo_.captureDuration = static_cast(endTime - startTime_ - + statisticalEventInfo_.startLatency); MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances StopScreenCapture E", FAKE_POINTER(this)); return ret; } @@ -1939,6 +1997,7 @@ void ScreenCaptureServer::ReleaseInner() std::lock_guard lock(mutexGlobal_); serverMap.erase(sessionId); } + SetMetaDataReport(); MEDIA_LOGI("0x%{public}06" PRIXPTR " Instances ReleaseInner E", FAKE_POINTER(this)); } diff --git a/services/services/screen_capture/server/screen_capture_server.h b/services/services/screen_capture/server/screen_capture_server.h index 4f3b1a50c7b92d8809ec5c7ae04278df0046a0a8..b4c2ca7408de3c2d5044f7d87c0b260f343aa840 100644 --- a/services/services/screen_capture/server/screen_capture_server.h +++ b/services/services/screen_capture/server/screen_capture_server.h @@ -24,6 +24,7 @@ #include #include #include +#include #include "audio_capturer_wrapper.h" #include "i_screen_capture_service.h" @@ -50,6 +51,7 @@ #include "notification_slot.h" #include "incall_observer.h" #include "media_data_source.h" +#include "meta/meta.h" namespace OHOS { namespace Media { @@ -101,6 +103,14 @@ enum AVScreenCaptureDataMode : int8_t { FILE_MODE = 2 }; +enum StopReason: int8_t { + NORMAL_STOPPED = 0, + RECEIVE_USER_PRIVACY_AUTHORITY_FAILED = 1, + POST_START_SCREENCAPTURE_HANDLE_FAILURE = 2, + REQUEST_USER_PRIVACY_AUTHORITY_FAILED = 3, + STOP_REASON_INVALID = 4 +}; + struct SurfaceBufferEntry { SurfaceBufferEntry(sptr buf, int32_t fence, int64_t timeStamp, OHOS::Rect& damage) : buffer(std::move(buf)), flushFence(fence), timeStamp(timeStamp), damageRect(damage) {} @@ -112,6 +122,18 @@ struct SurfaceBufferEntry { OHOS::Rect damageRect = {0, 0, 0, 0}; }; +struct StatisticalEventInfo { + int32_t errCode; + std::string errMsg; + int32_t captureDuration = -1; + bool userAgree = false; + bool requireMic = false; + bool enableMic = false; + std::string videoResolution; + StopReason stopReason = StopReason::STOP_REASON_INVALID; + int32_t startLatency = -1; +}; + class ScreenCapBufferConsumerListener : public IBufferConsumerListener { public: ScreenCapBufferConsumerListener( @@ -265,6 +287,9 @@ private: std::shared_ptr GetPixelMap(std::string path); std::shared_ptr GetPixelMapSvg(std::string path, int32_t width, int32_t height); void ResSchedReportData(int64_t value, std::unordered_map payload); + int64_t GetCurrentMillisecond(); + void SetMetaDataReport(); + void SetErrorInfo(int32_t errCode, std::string errMsg, StopReason stopReason, bool userAgree); private: std::mutex mutex_; @@ -290,6 +315,7 @@ private: AVScreenCaptureConfig captureConfig_; AVScreenCaptureAvType avType_ = AVScreenCaptureAvType::INVALID_TYPE; AVScreenCaptureDataMode dataMode_; + StatisticalEventInfo statisticalEventInfo_; sptr consumer_ = nullptr; bool isConsumerStart_ = false; ScreenId screenId_ = SCREEN_ID_INVALID; @@ -297,6 +323,7 @@ private: ScreenCaptureContentFilter contentFilter_; AVScreenCaptureState captureState_ = AVScreenCaptureState::CREATED; std::shared_ptr localLiveViewContent_; + int64_t startTime_ = 0; /* used for CAPTURE STREAM */ sptr surfaceCb_ = nullptr; diff --git a/services/utils/include/media_dfx.h b/services/utils/include/media_dfx.h index 08f7126de9c3b4bee9e493228eaacacb33e908a7..aa4f036cc00945daacd91d0c9e64bb9401e6d23f 100644 --- a/services/utils/include/media_dfx.h +++ b/services/utils/include/media_dfx.h @@ -16,13 +16,37 @@ #ifndef MEDIA_DFX_H #define MEDIA_DFX_H +#include +#include +#include +#include #include #include #include "nocopyable.h" #include "hisysevent.h" +#include "meta/meta.h" +#include "nlohmann/json.hpp" +#include +#include namespace OHOS { namespace Media { +using json = nlohmann::json; +enum CallType { + AVPLAYER, + AVRECORDER, + METADATA_RETRIEVER, + IMAGE_GENERATER, + AVDEMUXER, + AVMUXER, + VIDEO_DECODER, + VIDEO_ENCODER, + AUDIO_DECODER, + AUDIO_ENCODER, + SOUNDPOOL, + SCREEN_CAPTRUER, + AVTRANSCODER +}; class __attribute__((visibility("default"))) MediaEvent : public NoCopyable { public: MediaEvent() = default; @@ -41,10 +65,16 @@ public: const std::string& appName, int8_t sourceType, const std::string& errorMessage); void ScreenCaptureEventWrite(const std::string& eventName, OHOS::HiviewDFX::HiSysEvent::EventType type, const std::string& appName, int8_t captureMode, int8_t dataMode, int32_t errorCode, const std::string& errMsg); + void CommonStatisicsEventWrite(CallType callType, OHOS::HiviewDFX::HiSysEvent::EventType type, + const std::map>>>& infoMap); private: + void StatisicsHiSysEventWrite(CallType callType, OHOS::HiviewDFX::HiSysEvent::EventType type, + const std::vector& infoArr); + void ParseOneEvent(const std::pair> &listPair, json& metaInfoJson); std::string msg_; }; + __attribute__((visibility("default"))) void BehaviorEventWrite(std::string status, std::string module); __attribute__((visibility("default"))) void BehaviorEventWriteForScreenCapture(std::string status, std::string module, int32_t appUid, int32_t appPid); @@ -57,6 +87,9 @@ __attribute__((visibility("default"))) void FaultRecordAudioEventWrite(const std const std::string& errorMessage); __attribute__((visibility("default"))) void FaultScreenCaptureEventWrite(const std::string& appName, int8_t captureMode, int8_t dataMode, int32_t errorCode, const std::string& errorMessage); +__attribute__((visibility("default"))) int32_t CreateMediaInfo(CallType callType, int32_t uid); +__attribute__((visibility("default"))) int32_t AppendMediaInfo(const std::shared_ptr& meta); +__attribute__((visibility("default"))) int32_t Report(); class __attribute__((visibility("default"))) MediaTrace : public NoCopyable { public: diff --git a/services/utils/media_dfx.cpp b/services/utils/media_dfx.cpp index a86b89dc48e03a86dc36ea1b74ff6ff89b1d752e..ebcf3f430cd11610da065804b2ee97871b25b428 100644 --- a/services/utils/media_dfx.cpp +++ b/services/utils/media_dfx.cpp @@ -16,15 +16,93 @@ #include #include #include "securec.h" -#include "media_log.h" -#include "media_errors.h" #include "hitrace_meter.h" #include "hitrace/tracechain.h" #include "ipc_skeleton.h" #include "media_utils.h" +#include "hitrace/tracechain.h" +#include "common/log.h" +#include "common/media_core.h" +#include "meta/any.h" +#include namespace { constexpr uint32_t MAX_STRING_SIZE = 256; + constexpr int64_t HOURS_BETWEEN_REPORTS = 4; + constexpr int64_t MAX_MAP_SIZE = 100; + + std::mutex mux_; + std::map>>>> mediaInfoMap_; + std::map>>>> reportMediaInfoMap_; + std::map> idMap_; + std::chrono::system_clock::time_point currentTime_ = std::chrono::system_clock::now(); + + bool CollectReportMediaInfo(uint64_t instanceId) + { + std::lock_guard lock(mux_); + MEDIA_LOG_I("CollectReportMediaInfo."); + auto idMapIt = idMap_.find(instanceId); + if (idMapIt == idMap_.end()) { + MEDIA_LOG_I("Not found instanceId in idMap, instanceId is : %{public}" PRIu64, instanceId); + return false; + } + OHOS::Media::CallType ct = idMapIt->second.first; + int32_t uid = idMapIt->second.second; + idMap_.erase(idMapIt); + auto ctUidToMediaInfo = mediaInfoMap_.find(ct); + if (ctUidToMediaInfo == mediaInfoMap_.end()) { + MEDIA_LOG_I("Not found calltype, calltype is : %{public}d", static_cast(ct)); + return false; + } + auto uidToMediaInfo = ctUidToMediaInfo->second.find(uid); + if (uidToMediaInfo == ctUidToMediaInfo->second.end()) { + MEDIA_LOG_I("Not found uid in mediaInfoMap_, uid is : %{public}" PRId32, uid); + return false; + } + std::pair> metaAppIdPair; + auto& instanceList = uidToMediaInfo->second; + for (const auto& instancePair : instanceList) { + if (instancePair.first == instanceId) { + metaAppIdPair = instancePair; + instanceList.remove(instancePair); + break; + } + } + auto reportCtUidToMediaInfo = reportMediaInfoMap_.find(ct); + if (reportCtUidToMediaInfo != reportMediaInfoMap_.end()) { + auto it = reportCtUidToMediaInfo->second.find(uid); + if (it != reportCtUidToMediaInfo->second.end()) { + it->second.push_back(metaAppIdPair); + MEDIA_LOG_I("CollectReportMediaInfo: inserted metaAppIdPair for uid"); + } else { + reportCtUidToMediaInfo->second[uid].push_back(metaAppIdPair); + MEDIA_LOG_I("CollectReportMediaInfo: created new list for uid and inserted metaAppIdPair."); + } + } else { + reportMediaInfoMap_[ct][uid].push_back(metaAppIdPair); + MEDIA_LOG_I("CollectReportMediaInfo: created new list for callType and uid with metaAppIdPair."); + } + return true; + } + + int32_t StatisticsEventReport() + { + MEDIA_LOG_I("StatisticsEventReport."); + if (reportMediaInfoMap_.empty()) { + MEDIA_LOG_I("reportMediaInfoMap_ is empty, can't report"); + return OHOS::Media::MSERR_INVALID_OPERATION; + } + OHOS::Media::MediaEvent event; + for (const auto &it : reportMediaInfoMap_) { + event.CommonStatisicsEventWrite(it.first, OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC, it.second); + } + auto currentTime = std::chrono::system_clock::now(); + currentTime_ = currentTime; + reportMediaInfoMap_.clear(); + return OHOS::Media::MSERR_OK; + } } namespace OHOS { @@ -125,6 +203,88 @@ void MediaEvent::ScreenCaptureEventWrite(const std::string& eventName, OHOS::Hiv "ERROR_MESG", errorMessage); } +void MediaEvent::CommonStatisicsEventWrite(CallType callType, OHOS::HiviewDFX::HiSysEvent::EventType type, + const std::map>>>& infoMap) +{ + MEDIA_LOG_I("MediaEvent::CommonStatisicsEventWrite"); + if (infoMap.empty()) { + MEDIA_LOG_I("Player infoMap is empty."); + return; + } + std::vector infoArr; + for (const auto& kv : infoMap) { + json jsonArray; + json eventInfoJson; + json mediaEvents; + for (const auto& listPair : kv.second) { + json metaInfoJson; + ParseOneEvent(listPair, metaInfoJson); + mediaEvents.push_back(metaInfoJson); + } + eventInfoJson["appName"] = GetClientBundleName(kv.first); + eventInfoJson["mediaEvents"] = mediaEvents; + jsonArray.push_back(eventInfoJson); + infoArr.push_back(jsonArray.dump()); + } + + StatisicsHiSysEventWrite(callType, type, infoArr); +} + +void MediaEvent::ParseOneEvent(const std::pair> &listPair, + json& metaInfoJson) +{ + for (auto it = listPair.second->begin(); it != listPair.second->end(); ++it) { + Any valueType = OHOS::Media::GetDefaultAnyValue(it->first); + if (Any::IsSameTypeWith(valueType)) { + int32_t intVal; + if (listPair.second->GetData(it->first, intVal)) { + metaInfoJson[it->first] = std::to_string(intVal); + } + } else if (Any::IsSameTypeWith(valueType)) { + uint32_t uintVal; + if (listPair.second->GetData(it->first, uintVal)) { + metaInfoJson[it->first] = std::to_string(uintVal); + } + } else if (Any::IsSameTypeWith(valueType)) { + metaInfoJson[it->first] = AnyCast(it->second); + } else if (Any::IsSameTypeWith(valueType)) { + int8_t intVal; + if (listPair.second->GetData(it->first, intVal)) { + metaInfoJson[it->first] = std::to_string(intVal); + } + } else if (Any::IsSameTypeWith(valueType)) { + bool isTrue; + if (listPair.second->GetData(it->first, isTrue)) { + metaInfoJson[it->first] = isTrue ? "true" : "false"; + } + } else { + MEDIA_LOG_I("not found type matched with it->first: %{public}s", it->first.c_str()); + } + } +} + +void MediaEvent::StatisicsHiSysEventWrite(CallType callType, OHOS::HiviewDFX::HiSysEvent::EventType type, + const std::vector& infoArr) +{ + MEDIA_LOG_I("MediaEvent::StatisicsHiSysEventWrite"); + std::string eventName; + switch (callType) { + case CallType::AVPLAYER: + eventName = "PLAYER_COMMON_STATISTICS"; + break; + case CallType::AVRECORDER: + eventName = "RECORDER_STATISTICS"; + break; + case CallType::SCREEN_CAPTRUER: + eventName = "SCREEN_CAPTURE_STATISTICS"; + break; + default: + return; + } + HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::MULTI_MEDIA, eventName, type, + "EVENTS", infoArr); +} + void BehaviorEventWrite(std::string status, std::string module) { MediaEvent event; @@ -185,6 +345,108 @@ void FaultScreenCaptureEventWrite(const std::string& appName, int8_t captureMode captureMode, dataMode, errorCode, errorMessage); } +int32_t CreateMediaInfo(CallType callType, int32_t uid) +{ + std::lock_guard lock(mux_); + MEDIA_LOG_I("CreateMediaInfo."); + uint64_t instanceId = HiTraceChain::GetId().GetChainId(); + MEDIA_LOG_I("CreateMediaInfo uid is: %{public}" PRId32 " instanceId is: %{public}" PRIu64, uid, instanceId); + auto instanceIdMap = idMap_.find(instanceId); + if (instanceIdMap != idMap_.end()) { + MEDIA_LOG_I("instanceId already exists id idMap_"); + return MSERR_INVALID_VAL; + } else { + MEDIA_LOG_I("CreateMediaInfo not found instanceId in idMap_, add the instanceId to idMap_"); + std::pair insertToMapPair(callType, uid); + idMap_[instanceId] = insertToMapPair; + } + std::shared_ptr meta = std::make_shared(); + std::pair> metaAppIdPair(instanceId, meta); + auto ctUidToMediaInfo = mediaInfoMap_.find(callType); + if (ctUidToMediaInfo != mediaInfoMap_.end()) { + auto it = ctUidToMediaInfo->second.find(uid); + if (it != ctUidToMediaInfo->second.end()) { + it->second.push_back(metaAppIdPair); + MEDIA_LOG_I("CreateMediaInfo: Successfully inserted metaAppIdPair for uid "); + } else { + ctUidToMediaInfo->second[uid].push_back(metaAppIdPair); + MEDIA_LOG_I("CreateMediaInfo: Successfully created new list for uid and inserted metaAppIdPair."); + } + } else { + mediaInfoMap_[callType][uid].push_back(metaAppIdPair); + MEDIA_LOG_I("CreateMediaInfo: Successfully created new list for callType and uid "); + } + return MSERR_OK; +} + +int32_t AppendMediaInfo(const std::shared_ptr& meta) +{ + std::lock_guard lock(mux_); + MEDIA_LOG_I("AppendMediaInfo."); + if (meta == nullptr || meta->Empty()) { + MEDIA_LOG_I("Insert meta is empty."); + return MSERR_INVALID_OPERATION; + } + uint64_t instanceId = HiTraceChain::GetId().GetChainId(); + auto idMapIt = idMap_.find(instanceId); + if (idMapIt == idMap_.end()) { + MEDIA_LOG_I("Not found instanceId when append meta, instanceId is : %{public}" PRIu64, instanceId); + return MSERR_INVALID_VAL; + } + CallType ct = idMapIt->second.first; + int32_t uid = idMapIt->second.second; + auto ctUidToMediaInfo = mediaInfoMap_.find(ct); + if (ctUidToMediaInfo == mediaInfoMap_.end()) { + MEDIA_LOG_I("Not found calltype when append meta, calltype is : %{public}d", static_cast(ct)); + return MSERR_INVALID_OPERATION; + } + auto it = ctUidToMediaInfo->second.find(uid); + if (it == ctUidToMediaInfo->second.end()) { + MEDIA_LOG_I("Not found uid when append meta, uid is : %{public}" PRId32, uid); + return MSERR_INVALID_OPERATION; + } + auto& instanceList = it->second; + for (const auto& instancePair : instanceList) { + if (instancePair.first == instanceId) { + auto arg = meta->begin(); + while (arg != meta->end()) { + instancePair.second->SetData(arg->first, arg->second); + ++arg; + } + break; + } + } + return MSERR_OK; +} + +int32_t Report() +{ + MEDIA_LOG_I("Report."); + uint64_t instanceId = HiTraceChain::GetId().GetChainId(); + MEDIA_LOG_I("Delete media info instanceId is: %{public}" PRIu64, instanceId); + auto currentTime = std::chrono::system_clock::now(); + auto diff = currentTime - currentTime_; + auto hour = std::chrono::duration_cast(diff).count(); + if (!CollectReportMediaInfo(instanceId)) { + MEDIA_LOG_I("Collect media info fail."); + return MSERR_INVALID_OPERATION; + } + if (hour >= HOURS_BETWEEN_REPORTS) { + MEDIA_LOG_I("Over 4 hours, report the event"); + return StatisticsEventReport(); + } + if (reportMediaInfoMap_.empty()) { + return MSERR_INVALID_OPERATION; + } + for (const auto& mediaItem : reportMediaInfoMap_) { + if (mediaItem.second.size() >= MAX_MAP_SIZE) { + MEDIA_LOG_I("Event data size exceeds 100, report the event"); + return StatisticsEventReport(); + } + } + return MSERR_OK; +} + MediaTrace::MediaTrace(const std::string &funcName) { StartTrace(HITRACE_TAG_ZMEDIA, funcName);