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 SetOnStop(VideoEventCallback && onStop)148 void SetOnStop(VideoEventCallback&& onStop) 149 { 150 onStop_ = std ::move(onStop); 151 } FireStopEvent(const std::string & param)152 void FireStopEvent(const std::string& param) 153 { 154 if (onStop_) { 155 // onStop_ may be overwritten in its invoke so we copy it first 156 auto onStop = onStop_; 157 onStop(param); 158 } 159 RecorderOnEvent(Recorder::EventType::VIDEO_STOP, param); 160 } 161 SetOnFullScreenChange(VideoEventCallback && onFullScreenChange)162 void SetOnFullScreenChange(VideoEventCallback&& onFullScreenChange) 163 { 164 onFullScreenChange_ = std ::move(onFullScreenChange); 165 } FireFullScreenChangeEvent(const std::string & param)166 void FireFullScreenChangeEvent(const std::string& param) 167 { 168 if (onFullScreenChange_) { 169 // onFullScreenChange_ may be overwritten in its invoke so we copy it first 170 auto onFullScreenChange = onFullScreenChange_; 171 onFullScreenChange(param); 172 } 173 RecorderOnEvent(Recorder::EventType::VIDEO_SCREEN_CHANGE, param); 174 } 175 SetInspectorId(const std::string & inspectorId)176 void SetInspectorId(const std::string& inspectorId) 177 { 178 inspectorId_ = inspectorId; 179 } 180 181 private: RecorderOnEvent(Recorder::EventType eventType,const std::string & param)182 void RecorderOnEvent(Recorder::EventType eventType, const std::string& param) const 183 { 184 if (!Recorder::EventRecorder::Get().IsComponentRecordEnable()) { 185 return; 186 } 187 Recorder::EventParamsBuilder builder; 188 builder.SetId(inspectorId_); 189 auto host = GetFrameNode(); 190 if (host) { 191 builder.SetType(host->GetHostTag()).SetDescription(host->GetAutoEventParamValue("")); 192 } 193 builder.SetEventType(eventType).SetText(param); 194 Recorder::EventRecorder::Get().OnEvent(std::move(builder)); 195 } 196 197 VideoEventCallback onStart_; 198 VideoEventCallback onPause_; 199 VideoEventCallback onFinish_; 200 VideoEventCallback onError_; 201 VideoEventCallback onPrepared_; 202 VideoEventCallback onSeeking_; 203 VideoEventCallback onSeeked_; 204 VideoEventCallback onUpdate_; 205 VideoEventCallback onStop_; 206 VideoEventCallback onFullScreenChange_; 207 std::string inspectorId_; 208 }; 209 210 } // namespace OHOS::Ace::NG 211 212 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_VIDEO_VIDEO_EVENT_HUB_H 213