• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     ContainerScope scope(instanceId_);
68     if (videoController_) {
69 #ifdef SUPPORT_JSSTACK
70         HiviewDFX::ReportXPowerJsStackSysEvent(args.GetVm(), "STREAM_CHANGE", "SRC=Video");
71 #endif
72         videoController_->Start();
73     }
74 }
75 
Pause(const JSCallbackInfo & args)76 void JSVideoController::Pause(const JSCallbackInfo& args)
77 {
78     ContainerScope scope(instanceId_);
79     if (videoController_) {
80         videoController_->Pause();
81     }
82 }
83 
Stop(const JSCallbackInfo & args)84 void JSVideoController::Stop(const JSCallbackInfo& args)
85 {
86     ContainerScope scope(instanceId_);
87     if (videoController_) {
88         videoController_->Stop();
89     }
90 }
91 
SetCurrentTime(const JSCallbackInfo & args)92 void JSVideoController::SetCurrentTime(const JSCallbackInfo& args)
93 {
94     ContainerScope scope(instanceId_);
95     float value = 0;
96     if (args.Length() < 1 || !ConvertFromJSValue(args[0], value)) {
97         TAG_LOGW(AceLogTag::ACE_VIDEO, "JSVideoController set current time with invalid params");
98         return;
99     }
100 
101     SeekMode seekMode = SeekMode::SEEK_PREVIOUS_SYNC;
102     if (args.Length() > 1 && args[1]->IsNumber() && args[1]->ToNumber<uint32_t>() < SEEK_MODE.size()) {
103         seekMode = SEEK_MODE[args[1]->ToNumber<int32_t>()];
104     }
105 
106     if (videoController_) {
107         videoController_->SeekTo(value, seekMode);
108     }
109 }
110 
RequestFullscreen(const JSCallbackInfo & args)111 void JSVideoController::RequestFullscreen(const JSCallbackInfo& args)
112 {
113     ContainerScope scope(instanceId_);
114     bool landscape = true;
115     if (args.Length() < 1 || !ConvertFromJSValue(args[0], landscape)) {
116         TAG_LOGW(AceLogTag::ACE_VIDEO, "JSVideoController request full screen with invalid params");
117         return;
118     }
119 
120     if (videoController_) {
121         videoController_->RequestFullscreen(landscape);
122     }
123 }
124 
ExitFullscreen(const JSCallbackInfo & args)125 void JSVideoController::ExitFullscreen(const JSCallbackInfo& args)
126 {
127     ContainerScope scope(instanceId_);
128     if (videoController_) {
129         videoController_->ExitFullscreen(false);
130     }
131 }
132 
133 } // namespace OHOS::Ace::Framework
134