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 #ifndef DFX_AGENT_H 17 #define DFX_AGENT_H 18 19 #include <unordered_set> 20 21 #include "osal/task/task.h" 22 #include "filter/filter.h" 23 #include "common/performance_utils.h" 24 25 namespace OHOS { 26 namespace Media { 27 28 enum class PlayerDfxSourceType : uint8_t { 29 DFX_SOURCE_TYPE_URL_FILE = 0, 30 DFX_SOURCE_TYPE_URL_FD = 1, 31 DFX_SOURCE_TYPE_URL_NETWORK = 2, 32 DFX_SOURCE_TYPE_DATASRC = 3, 33 DFX_SOURCE_TYPE_MEDIASOURCE_LOCAL = 4, 34 DFX_SOURCE_TYPE_MEDIASOURCE_NETWORK = 5, 35 DFX_SOURCE_TYPE_UNKNOWN = 127, 36 }; 37 38 class DfxAgent; 39 using DfxEventHandleFunc = std::function<void(std::weak_ptr<DfxAgent> ptr, const DfxEvent&)>; 40 41 class DfxAgent : public std::enable_shared_from_this<DfxAgent> { 42 public: 43 DfxAgent(const std::string& groupId, const std::string& appName); 44 ~DfxAgent(); 45 void SetSourceType(PlayerDfxSourceType type); 46 void OnDfxEvent(const DfxEvent &event); 47 void SetInstanceId(const std::string& instanceId); 48 void ResetAgent(); 49 private: 50 void ReportLagEvent(int64_t lagDuration, const std::string& eventMsg); 51 void ReportEosSeek0Event(int32_t appUid); 52 void UpdateDfxInfo(const DfxEvent &event); 53 std::string GetPerfStr(const bool needWaitAllData); 54 static void ProcessVideoLagEvent(std::weak_ptr<DfxAgent> ptr, const DfxEvent &event); 55 static void ProcessAudioLagEvent(std::weak_ptr<DfxAgent> ptr, const DfxEvent &event); 56 static void ProcessStreamLagEvent(std::weak_ptr<DfxAgent> ptr, const DfxEvent &event); 57 static void ProcessEosSeekEvent(std::weak_ptr<DfxAgent> ptr, const DfxEvent &event); 58 static void ProcessPerfInfoEvent(std::weak_ptr<DfxAgent> ptr, const DfxEvent &event); 59 std::string groupId_ {}; 60 std::string instanceId_ {}; 61 std::string appName_ {}; 62 PlayerDfxSourceType sourceType_ {PlayerDfxSourceType::DFX_SOURCE_TYPE_UNKNOWN}; 63 std::unique_ptr<Task> dfxTask_ {nullptr}; 64 bool hasReported_ {false}; 65 bool needPrintPerfLog_ { false }; 66 std::unordered_map<std::string, MainPerfData> perfDataMap_ {}; 67 68 static const std::map<DfxEventType, DfxEventHandleFunc> DFX_EVENT_HANDLERS_; 69 }; 70 71 class ConcurrentUidSet { 72 public: IsAppFirstEvent(int32_t uid)73 bool IsAppFirstEvent(int32_t uid) 74 { 75 std::lock_guard<std::mutex> lock(mutex_); 76 if (set_.find(uid) == set_.end()) { 77 set_.insert(uid); 78 return true; 79 } 80 return false; 81 } 82 83 private: 84 std::mutex mutex_{}; 85 std::unordered_set<int32_t> set_{}; 86 }; 87 88 } // namespace Media 89 } // namespace OHOS 90 #endif // DFX_AGENT_H