1 /*
2  * Copyright (c) 2022-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 #include "bridge/declarative_frontend/jsview/models/video_model_impl.h"
17 
18 #include "bridge/declarative_frontend/view_stack_processor.h"
19 #include "core/components/video/video_component_v2.h"
20 
21 namespace OHOS::Ace::Framework {
22 
Create(const RefPtr<VideoControllerV2> & videoController)23 void VideoModelImpl::Create(const RefPtr<VideoControllerV2>& videoController)
24 {
25     auto* stack = ViewStackProcessor::GetInstance();
26     auto videoComponent = AceType::MakeRefPtr<OHOS::Ace::VideoComponentV2>();
27     videoComponent->SetSaveComponentEvent([videoComponent](std::unordered_map<std::string, RefPtr<Component>> map) {
28         videoComponent->SetGestureComponentMap(std::move(map));
29     });
30     if (AceApplicationInfo::GetInstance().IsRightToLeft()) {
31         videoComponent->SetTextDirection(TextDirection::RTL);
32     }
33 
34     ViewStackProcessor::GetInstance()->ClaimElementId(videoComponent);
35     ViewStackProcessor::GetInstance()->Push(videoComponent);
36 
37     if (videoController) {
38         videoComponent->SetVideoControllerV2(videoController);
39     }
40     stack->GetFlexItemComponent();
41 }
42 
SetSrc(const std::string & src,const std::string &,const std::string &)43 void VideoModelImpl::SetSrc(const std::string& src, const std::string& /* bundleName */,
44     const std::string& /*moduleName */)
45 {
46     auto videoComponent = AceType::DynamicCast<VideoComponentV2>(ViewStackProcessor::GetInstance()->GetMainComponent());
47     CHECK_NULL_VOID(videoComponent);
48     videoComponent->SetSrc(src);
49 }
50 
SetProgressRate(double progressRate)51 void VideoModelImpl::SetProgressRate(double progressRate)
52 {
53     auto videoComponent = AceType::DynamicCast<VideoComponentV2>(ViewStackProcessor::GetInstance()->GetMainComponent());
54     CHECK_NULL_VOID(videoComponent);
55     videoComponent->SetSpeed(static_cast<float>(progressRate));
56 }
57 
SetPosterSourceInfo(const std::string & posterUrl,const std::string & bundleName,const std::string & moduleName)58 void VideoModelImpl::SetPosterSourceInfo(const std::string& posterUrl, const std::string &bundleName,
59     const std::string &moduleName)
60 {
61     auto videoComponent = AceType::DynamicCast<VideoComponentV2>(ViewStackProcessor::GetInstance()->GetMainComponent());
62     CHECK_NULL_VOID(videoComponent);
63     videoComponent->SetPoster(posterUrl);
64 }
65 
SetPosterSourceByPixelMap(RefPtr<PixelMap> & pixMap)66 void VideoModelImpl::SetPosterSourceByPixelMap(RefPtr<PixelMap>& pixMap)
67 {
68     LOGW("Pixelmap is not supported.");
69 }
70 
SetMuted(bool muted)71 void VideoModelImpl::SetMuted(bool muted)
72 {
73     auto videoComponent = AceType::DynamicCast<VideoComponentV2>(ViewStackProcessor::GetInstance()->GetMainComponent());
74     CHECK_NULL_VOID(videoComponent);
75     videoComponent->SetMute(muted);
76 }
77 
SetAutoPlay(bool autoPlay)78 void VideoModelImpl::SetAutoPlay(bool autoPlay)
79 {
80     auto videoComponent = AceType::DynamicCast<VideoComponentV2>(ViewStackProcessor::GetInstance()->GetMainComponent());
81     CHECK_NULL_VOID(videoComponent);
82     videoComponent->SetAutoPlay(autoPlay);
83 }
84 
SetControls(bool controls)85 void VideoModelImpl::SetControls(bool controls)
86 {
87     auto videoComponent = AceType::DynamicCast<VideoComponentV2>(ViewStackProcessor::GetInstance()->GetMainComponent());
88     CHECK_NULL_VOID(videoComponent);
89     videoComponent->SetNeedControls(controls);
90 }
91 
SetObjectFit(ImageFit objectFit)92 void VideoModelImpl::SetObjectFit(ImageFit objectFit)
93 {
94     auto videoComponent = AceType::DynamicCast<VideoComponentV2>(ViewStackProcessor::GetInstance()->GetMainComponent());
95     CHECK_NULL_VOID(videoComponent);
96     videoComponent->SetFit(objectFit);
97 }
98 
SetLoop(bool loop)99 void VideoModelImpl::SetLoop(bool loop)
100 {
101     auto videoComponent = AceType::DynamicCast<VideoComponentV2>(ViewStackProcessor::GetInstance()->GetMainComponent());
102     CHECK_NULL_VOID(videoComponent);
103     videoComponent->SetLoop(loop);
104 }
105 
SetOnStart(VideoEventFunc && onStart)106 void VideoModelImpl::SetOnStart(VideoEventFunc&& onStart)
107 {
108     auto videoComponent = AceType::DynamicCast<VideoComponentV2>(ViewStackProcessor::GetInstance()->GetMainComponent());
109     CHECK_NULL_VOID(videoComponent);
110     videoComponent->SetStartEventId(
111         EventMarker([func = std::move(onStart)](const std::string& param) { func(param); }));
112 }
113 
SetOnPause(VideoEventFunc && onPause)114 void VideoModelImpl::SetOnPause(VideoEventFunc&& onPause)
115 {
116     auto videoComponent = AceType::DynamicCast<VideoComponentV2>(ViewStackProcessor::GetInstance()->GetMainComponent());
117     CHECK_NULL_VOID(videoComponent);
118     videoComponent->SetPauseEventId(
119         EventMarker([func = std::move(onPause)](const std::string& param) { func(param); }));
120 }
121 
SetOnFinish(VideoEventFunc && onFinish)122 void VideoModelImpl::SetOnFinish(VideoEventFunc&& onFinish)
123 {
124     auto videoComponent = AceType::DynamicCast<VideoComponentV2>(ViewStackProcessor::GetInstance()->GetMainComponent());
125     CHECK_NULL_VOID(videoComponent);
126     videoComponent->SetFinishEventId(
127         EventMarker([func = std::move(onFinish)](const std::string& param) { func(param); }));
128 }
129 
SetOnError(VideoEventFunc && onError)130 void VideoModelImpl::SetOnError(VideoEventFunc&& onError)
131 {
132     auto videoComponent = AceType::DynamicCast<VideoComponentV2>(ViewStackProcessor::GetInstance()->GetMainComponent());
133     CHECK_NULL_VOID(videoComponent);
134     videoComponent->SetErrorEventId(
135         EventMarker([func = std::move(onError)](const std::string& param) { func(param); }));
136 }
137 
SetOnPrepared(VideoEventFunc && onPrepared)138 void VideoModelImpl::SetOnPrepared(VideoEventFunc&& onPrepared)
139 {
140     auto videoComponent = AceType::DynamicCast<VideoComponentV2>(ViewStackProcessor::GetInstance()->GetMainComponent());
141     CHECK_NULL_VOID(videoComponent);
142     videoComponent->SetPreparedEventId(
143         EventMarker([func = std::move(onPrepared)](const std::string& param) { func(param); }));
144 }
145 
SetOnSeeking(VideoEventFunc && onSeeking)146 void VideoModelImpl::SetOnSeeking(VideoEventFunc&& onSeeking)
147 {
148     auto videoComponent = AceType::DynamicCast<VideoComponentV2>(ViewStackProcessor::GetInstance()->GetMainComponent());
149     CHECK_NULL_VOID(videoComponent);
150     videoComponent->SetSeekingEventId(
151         EventMarker([func = std::move(onSeeking)](const std::string& param) { func(param); }));
152 }
153 
SetOnSeeked(VideoEventFunc && onSeeked)154 void VideoModelImpl::SetOnSeeked(VideoEventFunc&& onSeeked)
155 {
156     auto videoComponent = AceType::DynamicCast<VideoComponentV2>(ViewStackProcessor::GetInstance()->GetMainComponent());
157     CHECK_NULL_VOID(videoComponent);
158     videoComponent->SetSeekedEventId(
159         EventMarker([func = std::move(onSeeked)](const std::string& param) { func(param); }));
160 }
161 
SetOnUpdate(VideoEventFunc && onUpdate)162 void VideoModelImpl::SetOnUpdate(VideoEventFunc&& onUpdate)
163 {
164     auto videoComponent = AceType::DynamicCast<VideoComponentV2>(ViewStackProcessor::GetInstance()->GetMainComponent());
165     CHECK_NULL_VOID(videoComponent);
166     videoComponent->SetTimeUpdateEventId(
167         EventMarker([func = std::move(onUpdate)](const std::string& param) { func(param); }));
168 }
169 
SetOnFullScreenChange(VideoEventFunc && onFullScreenChange)170 void VideoModelImpl::SetOnFullScreenChange(VideoEventFunc&& onFullScreenChange)
171 {
172     auto videoComponent = AceType::DynamicCast<VideoComponentV2>(ViewStackProcessor::GetInstance()->GetMainComponent());
173     CHECK_NULL_VOID(videoComponent);
174     videoComponent->SetFullscreenChangeEventId(
175         EventMarker([func = std::move(onFullScreenChange)](const std::string& param) { func(param); }));
176 }
177 } // namespace OHOS::Ace::Framework
178