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 #include "frameworks/bridge/declarative_frontend/jsview/js_video_controller.h"
17
18 #include "base/utils/linear_map.h"
19 #include "base/utils/utils.h"
20 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
21
22 namespace OHOS::Ace::Framework {
23 namespace {
24
25 const std::vector<SeekMode> SEEK_MODE = { SeekMode::SEEK_PREVIOUS_SYNC, SeekMode::SEEK_NEXT_SYNC,
26 SeekMode::SEEK_CLOSEST_SYNC, SeekMode::SEEK_CLOSEST };
27
28 } // namespace
29
JSBind(BindingTarget globalObj)30 void JSVideoController::JSBind(BindingTarget globalObj)
31 {
32 JSClass<JSVideoController>::Declare("VideoController");
33 JSClass<JSVideoController>::CustomMethod("start", &JSVideoController::Start);
34 JSClass<JSVideoController>::CustomMethod("pause", &JSVideoController::Pause);
35 JSClass<JSVideoController>::CustomMethod("stop", &JSVideoController::Stop);
36 JSClass<JSVideoController>::CustomMethod("setCurrentTime", &JSVideoController::SetCurrentTime);
37 JSClass<JSVideoController>::CustomMethod("requestFullscreen", &JSVideoController::RequestFullscreen);
38 JSClass<JSVideoController>::CustomMethod("exitFullscreen", &JSVideoController::ExitFullscreen);
39 JSClass<JSVideoController>::Bind(globalObj, JSVideoController::Constructor, JSVideoController::Destructor);
40 }
41
Constructor(const JSCallbackInfo & args)42 void JSVideoController::Constructor(const JSCallbackInfo& args)
43 {
44 auto videoController = Referenced::MakeRefPtr<JSVideoController>();
45 videoController->IncRefCount();
46 RefPtr<VideoControllerV2> controller = AceType::MakeRefPtr<VideoControllerV2>();
47 videoController->SetController(controller);
48 args.SetReturnValue(Referenced::RawPtr(videoController));
49 }
50
Destructor(JSVideoController * videoController)51 void JSVideoController::Destructor(JSVideoController* videoController)
52 {
53 if (videoController) {
54 const auto& controller = videoController->GetController();
55 videoController->DecRefCount();
56 if (controller) {
57 controller->Clear();
58 }
59 }
60 }
61
Start(const JSCallbackInfo & args)62 void JSVideoController::Start(const JSCallbackInfo& args)
63 {
64 if (videoController_) {
65 videoController_->Start();
66 }
67 }
68
Pause(const JSCallbackInfo & args)69 void JSVideoController::Pause(const JSCallbackInfo& args)
70 {
71 if (videoController_) {
72 videoController_->Pause();
73 }
74 }
75
Stop(const JSCallbackInfo & args)76 void JSVideoController::Stop(const JSCallbackInfo& args)
77 {
78 if (videoController_) {
79 videoController_->Stop();
80 }
81 }
82
SetCurrentTime(const JSCallbackInfo & args)83 void JSVideoController::SetCurrentTime(const JSCallbackInfo& args)
84 {
85 float value = 0;
86 if (args.Length() < 1 || !ConvertFromJSValue(args[0], value)) {
87 LOGE("JSVideoController::SetCurrentTime: Invalid params");
88 return;
89 }
90
91 SeekMode seekMode = SeekMode::SEEK_PREVIOUS_SYNC;
92 if (args.Length() > 1 && args[1]->IsNumber()) {
93 seekMode = SEEK_MODE[args[1]->ToNumber<int32_t>()];
94 }
95
96 if (videoController_) {
97 videoController_->SeekTo(value, seekMode);
98 }
99 }
100
RequestFullscreen(const JSCallbackInfo & args)101 void JSVideoController::RequestFullscreen(const JSCallbackInfo& args)
102 {
103 bool landscape = true;
104 if (args.Length() < 1 || !ConvertFromJSValue(args[0], landscape)) {
105 LOGE("JSVideoController::RequestFullscreen: Invalid params");
106 return;
107 }
108
109 if (videoController_) {
110 videoController_->RequestFullscreen(landscape);
111 }
112 }
113
ExitFullscreen(const JSCallbackInfo & args)114 void JSVideoController::ExitFullscreen(const JSCallbackInfo& args)
115 {
116 if (videoController_) {
117 videoController_->ExitFullscreen(false);
118 }
119 }
120
121 } // namespace OHOS::Ace::Framework
122