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>::CustomMethod("reset", &JSVideoController::Reset);
43     JSClass<JSVideoController>::Bind(globalObj, JSVideoController::Constructor, JSVideoController::Destructor);
44 }
45 
Constructor(const JSCallbackInfo & args)46 void JSVideoController::Constructor(const JSCallbackInfo& args)
47 {
48     auto videoController = Referenced::MakeRefPtr<JSVideoController>();
49     videoController->IncRefCount();
50     RefPtr<VideoControllerV2> controller = AceType::MakeRefPtr<VideoControllerV2>();
51     videoController->SetController(controller);
52     args.SetReturnValue(Referenced::RawPtr(videoController));
53 }
54 
Destructor(JSVideoController * videoController)55 void JSVideoController::Destructor(JSVideoController* videoController)
56 {
57     if (videoController) {
58         const auto& controller = videoController->GetController();
59         videoController->DecRefCount();
60         if (controller) {
61             controller->Clear();
62         }
63     }
64 }
65 
Reset(const JSCallbackInfo & args)66 void JSVideoController::Reset(const JSCallbackInfo& args)
67 {
68     if (videoController_) {
69         videoController_->Reset();
70     }
71 }
72 
Start(const JSCallbackInfo & args)73 void JSVideoController::Start(const JSCallbackInfo& args)
74 {
75     ContainerScope scope(instanceId_);
76     if (videoController_) {
77 #ifdef SUPPORT_JSSTACK
78         HiviewDFX::ReportXPowerJsStackSysEvent(args.GetVm(), "STREAM_CHANGE", "SRC=Video");
79 #endif
80         videoController_->Start();
81     }
82 }
83 
Pause(const JSCallbackInfo & args)84 void JSVideoController::Pause(const JSCallbackInfo& args)
85 {
86     ContainerScope scope(instanceId_);
87     if (videoController_) {
88         videoController_->Pause();
89     }
90 }
91 
Stop(const JSCallbackInfo & args)92 void JSVideoController::Stop(const JSCallbackInfo& args)
93 {
94     ContainerScope scope(instanceId_);
95     if (videoController_) {
96         videoController_->Stop();
97     }
98 }
99 
SetCurrentTime(const JSCallbackInfo & args)100 void JSVideoController::SetCurrentTime(const JSCallbackInfo& args)
101 {
102     ContainerScope scope(instanceId_);
103     float value = 0;
104     if (args.Length() < 1 || !ConvertFromJSValue(args[0], value)) {
105         TAG_LOGW(AceLogTag::ACE_VIDEO, "JSVideoController set current time with invalid params");
106         return;
107     }
108 
109     SeekMode seekMode = SeekMode::SEEK_PREVIOUS_SYNC;
110     if (args.Length() > 1 && args[1]->IsNumber() && args[1]->ToNumber<uint32_t>() < SEEK_MODE.size()) {
111         seekMode = SEEK_MODE[args[1]->ToNumber<int32_t>()];
112     }
113 
114     if (videoController_) {
115         videoController_->SeekTo(value, seekMode);
116     }
117 }
118 
RequestFullscreen(const JSCallbackInfo & args)119 void JSVideoController::RequestFullscreen(const JSCallbackInfo& args)
120 {
121     ContainerScope scope(instanceId_);
122     bool landscape = true;
123     if (args.Length() < 1 || !ConvertFromJSValue(args[0], landscape)) {
124         TAG_LOGW(AceLogTag::ACE_VIDEO, "JSVideoController request full screen with invalid params");
125         return;
126     }
127 
128     if (videoController_) {
129         videoController_->RequestFullscreen(landscape);
130     }
131 }
132 
ExitFullscreen(const JSCallbackInfo & args)133 void JSVideoController::ExitFullscreen(const JSCallbackInfo& args)
134 {
135     ContainerScope scope(instanceId_);
136     if (videoController_) {
137         videoController_->ExitFullscreen(false);
138     }
139 }
140 
141 } // namespace OHOS::Ace::Framework
142