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