1 /*
2  * Copyright (c) 2021-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_VIDEO_VIDEO_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_VIDEO_VIDEO_COMPONENT_H
18 
19 #include <string>
20 
21 #include "base/utils/utils.h"
22 #include "core/components/image/image_component.h"
23 #include "core/components/video/resource/player.h"
24 #include "core/components/video/resource/texture.h"
25 #include "core/components/video/texture_component.h"
26 #include "core/components/video/video_controller_v2.h"
27 #include "core/components/video/video_utils.h"
28 #include "core/event/ace_event_helper.h"
29 #include "core/pipeline/base/element.h"
30 
31 namespace OHOS::Ace {
32 // A component can show Video.
33 class ACE_EXPORT VideoComponent : public TextureComponent {
34     DECLARE_ACE_TYPE(VideoComponent, TextureComponent);
35 
36 public:
37     using FullscreenEvent = std::function<RefPtr<Component>(bool, const WeakPtr<Player>&, const WeakPtr<Texture>&)>;
38     using MediaExitFullscreenEvent = std::function<void(bool, bool, int32_t)>;
39     using MediaFullscreenEvent = std::function<RefPtr<Component>(bool, bool, const WeakPtr<Texture>&)>;
40 
VideoComponent()41     explicit VideoComponent()
42     {
43         videoController_ = AceType::MakeRefPtr<VideoController>();
44     };
45 
46     ~VideoComponent() override = default;
47 
48     RefPtr<RenderNode> CreateRenderNode() override;
49     RefPtr<Element> CreateElement() override;
50 
GetSrc()51     const std::string& GetSrc() const
52     {
53         return src_;
54     }
55 
SetSrc(const std::string & src)56     void SetSrc(const std::string& src)
57     {
58         src_ = src;
59     }
60 
GetPoster()61     const std::string& GetPoster() const
62     {
63         return poster_;
64     }
65 
SetPoster(const std::string & poster)66     void SetPoster(const std::string& poster)
67     {
68         poster_ = poster;
69     }
70 
GetPosterImage()71     const RefPtr<ImageComponent>& GetPosterImage() const
72     {
73         return posterImage_;
74     }
75 
SetPosterImage(const RefPtr<ImageComponent> & posterImage)76     void SetPosterImage(const RefPtr<ImageComponent>& posterImage)
77     {
78         posterImage_ = posterImage;
79     }
80 
NeedControls()81     bool NeedControls() const
82     {
83         return needControls_;
84     }
85 
SetNeedControls(bool needControls)86     void SetNeedControls(bool needControls)
87     {
88         needControls_ = needControls;
89     }
90 
IsMute()91     bool IsMute() const
92     {
93         return isMute_;
94     }
95 
SetMute(bool mute)96     void SetMute(bool mute)
97     {
98         isMute_ = mute;
99     }
100 
IsLoop()101     bool IsLoop() const
102     {
103         return isLoop_;
104     }
105 
SetLoop(bool loop)106     void SetLoop(bool loop)
107     {
108         isLoop_ = loop;
109     }
110 
SetSpeed(float speed)111     void SetSpeed(float speed)
112     {
113         if (GreatOrEqual(speed, 0.1) && LessOrEqual(speed, 20.0)) {
114             speed_ = speed;
115         }
116     }
117 
GetSpeed()118     float GetSpeed() const
119     {
120         return speed_;
121     }
122 
SetDirection(const std::string & direction)123     void SetDirection(const std::string& direction)
124     {
125         if (direction == "vertical" || direction == "horizontal" || direction == "adapt" || direction == "auto") {
126             direction_ = direction;
127         }
128     }
129 
GetDirection()130     const std::string& GetDirection() const
131     {
132         return direction_;
133     }
134 
GetStartTime()135     int32_t GetStartTime() const
136     {
137         return startTime_;
138     }
139 
SetStartTime(int32_t startTime)140     void SetStartTime(int32_t startTime)
141     {
142         startTime_ = startTime;
143     }
144 
IsAutoPlay()145     bool IsAutoPlay() const
146     {
147         return isAutoPlay_;
148     }
149 
SetAutoPlay(bool isAutoPlay)150     void SetAutoPlay(bool isAutoPlay)
151     {
152         isAutoPlay_ = isAutoPlay;
153     }
154 
GetPreparedEventId()155     const EventMarker& GetPreparedEventId() const
156     {
157         return preparedEventId_;
158     }
159 
SetPreparedEventId(const EventMarker & eventId)160     void SetPreparedEventId(const EventMarker& eventId)
161     {
162         preparedEventId_ = eventId;
163     }
164 
GetStartEventId()165     const EventMarker& GetStartEventId() const
166     {
167         return startEventId_;
168     }
169 
SetStartEventId(const EventMarker & eventId)170     void SetStartEventId(const EventMarker& eventId)
171     {
172         startEventId_ = eventId;
173     }
174 
GetPauseEventId()175     const EventMarker& GetPauseEventId() const
176     {
177         return pauseEventId_;
178     }
179 
SetPauseEventId(const EventMarker & eventId)180     void SetPauseEventId(const EventMarker& eventId)
181     {
182         pauseEventId_ = eventId;
183     }
184 
GetStopEventId()185     const EventMarker& GetStopEventId() const
186     {
187         return stopEventId_;
188     }
189 
SetStopEventId(const EventMarker & eventId)190     void SetStopEventId(const EventMarker& eventId)
191     {
192         stopEventId_ = eventId;
193     }
194 
GetFinishEventId()195     const EventMarker& GetFinishEventId() const
196     {
197         return finishEventId_;
198     }
199 
SetFinishEventId(const EventMarker & eventId)200     void SetFinishEventId(const EventMarker& eventId)
201     {
202         finishEventId_ = eventId;
203     }
204 
GetErrorEventId()205     const EventMarker& GetErrorEventId() const
206     {
207         return errorEventId_;
208     }
209 
SetErrorEventId(const EventMarker & eventId)210     void SetErrorEventId(const EventMarker& eventId)
211     {
212         errorEventId_ = eventId;
213     }
214 
GetSeekingEventId()215     const EventMarker& GetSeekingEventId() const
216     {
217         return seekingEventId_;
218     }
219 
SetSeekingEventId(const EventMarker & eventId)220     void SetSeekingEventId(const EventMarker& eventId)
221     {
222         seekingEventId_ = eventId;
223     }
224 
GetSeekedEventId()225     const EventMarker& GetSeekedEventId() const
226     {
227         return seekedEventId_;
228     }
229 
SetSeekedEventId(const EventMarker & eventId)230     void SetSeekedEventId(const EventMarker& eventId)
231     {
232         seekedEventId_ = eventId;
233     }
234 
GetTimeUpdateEventId()235     const EventMarker& GetTimeUpdateEventId() const
236     {
237         return timeUpdateEventId_;
238     }
239 
SetTimeUpdateEventId(const EventMarker & eventId)240     void SetTimeUpdateEventId(const EventMarker& eventId)
241     {
242         timeUpdateEventId_ = eventId;
243     }
244 
GetFullscreenChangeEventId()245     const EventMarker& GetFullscreenChangeEventId() const
246     {
247         return fullscreenChangeEventId_;
248     }
249 
SetFullscreenChangeEventId(const EventMarker & eventId)250     void SetFullscreenChangeEventId(const EventMarker& eventId)
251     {
252         fullscreenChangeEventId_ = eventId;
253     }
254 
GetVideoController()255     RefPtr<VideoController> GetVideoController() const
256     {
257         return videoController_;
258     }
259 
SetFullscreenEvent(FullscreenEvent && fullscreenEvent)260     void SetFullscreenEvent(FullscreenEvent&& fullscreenEvent)
261     {
262         fullscreenEvent_ = std::move(fullscreenEvent);
263     }
264 
GetFullscreenEvent()265     const FullscreenEvent& GetFullscreenEvent() const
266     {
267         return fullscreenEvent_;
268     }
269 
SetMediaFullscreenEvent(MediaFullscreenEvent && mediaFullscreenEvent)270     void SetMediaFullscreenEvent(MediaFullscreenEvent&& mediaFullscreenEvent)
271     {
272         mediaFullscreenEvent_ = std::move(mediaFullscreenEvent);
273     }
274 
GetMediaFullscreenEvent()275     const MediaFullscreenEvent& GetMediaFullscreenEvent() const
276     {
277         return mediaFullscreenEvent_;
278     }
279 
SetMediaExitFullscreenEvent(MediaExitFullscreenEvent && mediaExitFullscreenEvent)280     void SetMediaExitFullscreenEvent(MediaExitFullscreenEvent&& mediaExitFullscreenEvent)
281     {
282         mediaExitFullscreenEvent_ = mediaExitFullscreenEvent;
283     }
284 
GetMediaExitFullscreenEvent()285     const MediaExitFullscreenEvent& GetMediaExitFullscreenEvent() const
286     {
287         return mediaExitFullscreenEvent_;
288     }
289 
IsFullscreen()290     bool IsFullscreen() const
291     {
292         return isFullscreen_;
293     }
294 
SetFullscreen(bool isFullscreen)295     void SetFullscreen(bool isFullscreen)
296     {
297         isFullscreen_ = isFullscreen;
298     }
299 
SetPlayer(const WeakPtr<Player> & player)300     void SetPlayer(const WeakPtr<Player>& player)
301     {
302         player_ = player;
303     }
304 
GetPlayer()305     WeakPtr<Player> GetPlayer() const
306     {
307         return player_;
308     }
309 
SetTexture(const WeakPtr<Texture> & texture)310     void SetTexture(const WeakPtr<Texture>& texture)
311     {
312         texture_ = texture;
313     }
314 
GetTexture()315     WeakPtr<Texture> GetTexture() const
316     {
317         return texture_;
318     }
319 
SetVideoController(const RefPtr<VideoController> & videoController)320     virtual void SetVideoController(const RefPtr<VideoController>& videoController)
321     {
322         if (videoController) {
323             videoController_ = videoController;
324         }
325     }
326 
SetPastPlayingStatus(bool pastPlayingStatus)327     void SetPastPlayingStatus(bool pastPlayingStatus)
328     {
329         pastPlayingStatus_ = pastPlayingStatus;
330     }
331 
GetPastPlayingStatus()332     bool GetPastPlayingStatus() const
333     {
334         return pastPlayingStatus_;
335     }
336 
SetMediaPlayerFullStatus(bool isMediaPlayerFullStatus)337     void SetMediaPlayerFullStatus(bool isMediaPlayerFullStatus)
338     {
339         isMediaPlayerFullStatus_ = isMediaPlayerFullStatus;
340     }
341 
GetMediaPlayerFullStatus()342     bool GetMediaPlayerFullStatus() const
343     {
344         return isMediaPlayerFullStatus_;
345     }
346 
347 private:
348     std::string src_;
349     std::string poster_;
350     bool isAutoPlay_ = false;
351     bool needControls_ = true;
352     bool isMute_ = false;
353     bool isFullscreen_ = false;
354     bool isLoop_ = false;
355     bool pastPlayingStatus_ = false;
356     bool isMediaPlayerFullStatus_ = false;
357     int32_t startTime_ = 0;
358     float speed_ = 1.0f;
359     std::string direction_ = "auto";
360 
361     EventMarker preparedEventId_;
362     EventMarker startEventId_;
363     EventMarker pauseEventId_;
364     EventMarker stopEventId_;
365     EventMarker finishEventId_;
366     EventMarker errorEventId_;
367     EventMarker seekingEventId_;
368     EventMarker seekedEventId_;
369     EventMarker timeUpdateEventId_;
370     EventMarker fullscreenChangeEventId_;
371     FullscreenEvent fullscreenEvent_;
372     MediaExitFullscreenEvent mediaExitFullscreenEvent_;
373     MediaFullscreenEvent mediaFullscreenEvent_;
374 
375     RefPtr<ImageComponent> posterImage_;
376     RefPtr<VideoController> videoController_;
377     WeakPtr<Player> player_;
378     WeakPtr<Texture> texture_;
379 };
380 
381 } // namespace OHOS::Ace
382 
383 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_VIDEO_VIDEO_COMPONENT_H
384