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