• 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_VIDEO_VIDEO_EVENT_HUB_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_VIDEO_VIDEO_EVENT_HUB_H
18 
19 #include <functional>
20 
21 #include "base/memory/ace_type.h"
22 #include "core/common/recorder/event_recorder.h"
23 #include "core/components_ng/base/frame_node.h"
24 #include "core/components_ng/event/event_hub.h"
25 #include "core/components_ng/event/gesture_event_hub.h"
26 
27 namespace OHOS::Ace::NG {
28 
29 using VideoEventCallback = std::function<void(const std::string&)>;
30 
31 class VideoEventHub : public EventHub {
32     DECLARE_ACE_TYPE(VideoEventHub, EventHub)
33 
34 public:
35     VideoEventHub() = default;
36     ~VideoEventHub() override = default;
37 
SetOnStart(VideoEventCallback && onStart)38     void SetOnStart(VideoEventCallback&& onStart)
39     {
40         onStart_ = std ::move(onStart);
41     }
FireStartEvent(const std::string & param)42     void FireStartEvent(const std::string& param)
43     {
44         if (onStart_) {
45             // onStart_ may be overwritten in its invoke so we copy it first
46             auto onStart = onStart_;
47             onStart(param);
48         }
49         RecorderOnEvent(Recorder::EventType::VIDEO_START, param);
50     }
51 
SetOnPause(VideoEventCallback && onPause)52     void SetOnPause(VideoEventCallback&& onPause)
53     {
54         onPause_ = std ::move(onPause);
55     }
FirePauseEvent(const std::string & param)56     void FirePauseEvent(const std::string& param)
57     {
58         if (onPause_) {
59             // onPause_ may be overwritten in its invoke so we copy it first
60             auto onPause = onPause_;
61             onPause(param);
62         }
63         RecorderOnEvent(Recorder::EventType::VIDEO_PAUSE, param);
64     }
65 
SetOnFinish(VideoEventCallback && onFinish)66     void SetOnFinish(VideoEventCallback&& onFinish)
67     {
68         onFinish_ = std ::move(onFinish);
69     }
FireFinishEvent(const std::string & param)70     void FireFinishEvent(const std::string& param)
71     {
72         if (onFinish_) {
73             // onFinish_ may be overwritten in its invoke so we copy it first
74             auto onFinish = onFinish_;
75             onFinish(param);
76         }
77         RecorderOnEvent(Recorder::EventType::VIDEO_FINISH, param);
78     }
79 
SetOnError(VideoEventCallback && onError)80     void SetOnError(VideoEventCallback&& onError)
81     {
82         onError_ = std ::move(onError);
83     }
FireErrorEvent(const std::string & param)84     void FireErrorEvent(const std::string& param)
85     {
86         if (onError_) {
87             // onError_ may be overwritten in its invoke so we copy it first
88             auto onError = onError_;
89             onError(param);
90         }
91         RecorderOnEvent(Recorder::EventType::VIDEO_ERROR, param);
92     }
93 
SetOnPrepared(VideoEventCallback && onPrepared)94     void SetOnPrepared(VideoEventCallback&& onPrepared)
95     {
96         onPrepared_ = std ::move(onPrepared);
97     }
FirePreparedEvent(const std::string & param)98     void FirePreparedEvent(const std::string& param)
99     {
100         if (onPrepared_) {
101             // onPrepared_ may be overwritten in its invoke so we copy it first
102             auto onPrepared = onPrepared_;
103             onPrepared(param);
104         }
105         RecorderOnEvent(Recorder::EventType::VIDEO_PREPARED, param);
106     }
107 
SetOnSeeking(VideoEventCallback && onSeeking)108     void SetOnSeeking(VideoEventCallback&& onSeeking)
109     {
110         onSeeking_ = std ::move(onSeeking);
111     }
FireSeekingEvent(const std::string & param)112     void FireSeekingEvent(const std::string& param)
113     {
114         if (onSeeking_) {
115             // onSeeking_ may be overwritten in its invoke so we copy it first
116             auto onSeeking = onSeeking_;
117             onSeeking(param);
118         }
119     }
120 
SetOnSeeked(VideoEventCallback && onSeeked)121     void SetOnSeeked(VideoEventCallback&& onSeeked)
122     {
123         onSeeked_ = std ::move(onSeeked);
124     }
FireSeekedEvent(const std::string & param)125     void FireSeekedEvent(const std::string& param)
126     {
127         if (onSeeked_) {
128             // onSeeked_ may be overwritten in its invoke so we copy it first
129             auto onSeeked = onSeeked_;
130             onSeeked(param);
131         }
132         RecorderOnEvent(Recorder::EventType::VIDEO_SEEKED, param);
133     }
134 
SetOnUpdate(VideoEventCallback && onUpdate)135     void SetOnUpdate(VideoEventCallback&& onUpdate)
136     {
137         onUpdate_ = std ::move(onUpdate);
138     }
FireUpdateEvent(const std::string & param)139     void FireUpdateEvent(const std::string& param)
140     {
141         if (onUpdate_) {
142             // onUpdate_ may be overwritten in its invoke so we copy it first
143             auto onUpdate = onUpdate_;
144             onUpdate(param);
145         }
146     }
147 
SetOnFullScreenChange(VideoEventCallback && onFullScreenChange)148     void SetOnFullScreenChange(VideoEventCallback&& onFullScreenChange)
149     {
150         onFullScreenChange_ = std ::move(onFullScreenChange);
151     }
FireFullScreenChangeEvent(const std::string & param)152     void FireFullScreenChangeEvent(const std::string& param)
153     {
154         if (onFullScreenChange_) {
155             // onFullScreenChange_ may be overwritten in its invoke so we copy it first
156             auto onFullScreenChange = onFullScreenChange_;
157             onFullScreenChange(param);
158         }
159         RecorderOnEvent(Recorder::EventType::VIDEO_SCREEN_CHANGE, param);
160     }
161 
SetInspectorId(const std::string & inspectorId)162     void SetInspectorId(const std::string& inspectorId)
163     {
164         inspectorId_ = inspectorId;
165     }
166 
167 private:
RecorderOnEvent(Recorder::EventType eventType,const std::string & param)168     void RecorderOnEvent(Recorder::EventType eventType, const std::string& param) const
169     {
170         if (!Recorder::EventRecorder::Get().IsComponentRecordEnable()) {
171             return;
172         }
173         Recorder::EventParamsBuilder builder;
174         builder.SetId(inspectorId_);
175         auto host = GetFrameNode();
176         if (host) {
177             builder.SetType(host->GetHostTag()).SetDescription(host->GetAutoEventParamValue(""));
178         }
179         builder.SetEventType(eventType).SetText(param);
180         Recorder::EventRecorder::Get().OnEvent(std::move(builder));
181     }
182 
183     VideoEventCallback onStart_;
184     VideoEventCallback onPause_;
185     VideoEventCallback onFinish_;
186     VideoEventCallback onError_;
187     VideoEventCallback onPrepared_;
188     VideoEventCallback onSeeking_;
189     VideoEventCallback onSeeked_;
190     VideoEventCallback onUpdate_;
191     VideoEventCallback onFullScreenChange_;
192     std::string inspectorId_;
193 };
194 
195 } // namespace OHOS::Ace::NG
196 
197 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_VIDEO_VIDEO_EVENT_HUB_H
198