1 /*
2  * Copyright (c) 2020-2021 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 #ifndef OHOS_ACELITE_VIDEO_VIEW_H
16 #define OHOS_ACELITE_VIDEO_VIEW_H
17 
18 #include "acelite_config.h"
19 #ifdef FEATURE_COMPONENT_VIDEO
20 #include <pthread.h>
21 #include <sys/prctl.h>
22 #include <unistd.h>
23 #include "ace_log.h"
24 #include "flex_layout.h"
25 #include "js_fwk_common.h"
26 #include "js_app_context.h"
27 #include "panel_view.h"
28 #include "player.h"
29 #include "ui_font.h"
30 #include "ui_image_view.h"
31 #include "ui_label.h"
32 #include "ui_slider.h"
33 #include "ui_surface_view.h"
34 #include "ui_view.h"
35 #include "ui_view_group.h"
36 #include "video_state_callback.h"
37 
38 namespace OHOS {
39 namespace ACELite {
40 class VideoView : public FlexLayout, public MemoryHeap {
41 public:
42     ACE_DISALLOW_COPY_AND_MOVE(VideoView);
43     VideoView();
44     ~VideoView();
45     bool CreateVideoView();
46     void SetVideoView();
47     void PanelRefreshLayout();
48 
49     /* player info */
50     int32_t Prepare();
51 
52     int32_t Play();
53 
54     int32_t Pause();
55 
56     int32_t Stop();
57 
58     int32_t SeekTo(int64_t currentTime);
59 
60     int32_t SetVolume(float volumeValue);
61 
62     int32_t GetCurrentPosition(int64_t &currentPosition);
63 
64     int32_t GetDuration(int64_t &duration);
65 
66     int32_t SetSource(const char * const videoSourceUrl);
67 
GetPlayer()68     const Media::Player* GetPlayer() const
69     {
70         return videoPlayer_;
71     }
72 
73     /* video pannel info */
GetPanelView()74     const PanelView* GetPanelView() const
75     {
76         return panelView_;
77     }
78 
GetMuted()79     const bool GetMuted() const
80     {
81         return mutedFlag_;
82     }
83 
setMuted(bool muted)84     void setMuted(bool muted)
85     {
86         mutedFlag_ = muted;
87     }
88 
SetVideoSrc(char * src)89     void SetVideoSrc(char* src)
90     {
91         videoSrc_ = src;
92     }
93 
GetAutoPlayFlag()94     const bool GetAutoPlayFlag() const
95     {
96         return autoPlayFlag_;
97     }
98 
SetAutoPlayFlag(bool autoPlayFlag)99     void SetAutoPlayFlag(bool autoPlayFlag)
100     {
101         autoPlayFlag_ = autoPlayFlag;
102     }
103 
104     void UpdateMutedStatus();
105 
106     bool HideVideoPanel();
107 
108     const Media::Player* GetVideoPlayer() const;
109 
110     void UpdatePanelProgress();
111 
SetPlayStateCallback(VideoStateCallback * callback)112     void SetPlayStateCallback(VideoStateCallback *callback)
113     {
114         playStateCallback_ = callback;
115     }
116 
117     void UpdatePlayState(VideoPlayState newState);
118 
119     bool WaitRebuildPlayerFinish();
120 
121     void PrepareRebuildPlayerThread();
122 
123     /* set video event sync call back functions */
SetPlayEventListener(const std::shared_ptr<Media::PlayerCallback> & cb)124     void SetPlayEventListener(const std::shared_ptr<Media::PlayerCallback> &cb)
125     {
126         playEventListener_ = cb;
127     }
128 
GetPlayEventListener()129     const std::shared_ptr<Media::PlayerCallback> GetPlayEventListener() const
130     {
131         return playEventListener_;
132     }
133 
GetPrepareSyncCBFunc()134     const jerry_value_t GetPrepareSyncCBFunc() const
135     {
136         return preparedSyncCallBackFunc_;
137     }
138 
GetStartSyncCBFunc()139     const jerry_value_t GetStartSyncCBFunc() const
140     {
141         return startSyncCallBackFunc_;
142     }
143 
GetPauseSyncCBFunc()144     const jerry_value_t GetPauseSyncCBFunc() const
145     {
146         return pauseSyncCallBackFunc_;
147     }
148 
GetTimeUpdateSyncCBFunc()149     const jerry_value_t GetTimeUpdateSyncCBFunc() const
150     {
151         return timeUpdateSyncCallBackFunc_;
152     }
153 
SetPrepareSyncCBFunc(jerry_value_t & callBackFunc)154     void SetPrepareSyncCBFunc(jerry_value_t& callBackFunc)
155     {
156         preparedSyncCallBackFunc_ = jerry_acquire_value(callBackFunc);
157     }
SetStartSyncCBFunc(jerry_value_t & callBackFunc)158     void SetStartSyncCBFunc(jerry_value_t& callBackFunc)
159     {
160         startSyncCallBackFunc_ = jerry_acquire_value(callBackFunc);
161     }
SetPauseSyncCBFunc(jerry_value_t & callBackFunc)162     void SetPauseSyncCBFunc(jerry_value_t& callBackFunc)
163     {
164         pauseSyncCallBackFunc_ = jerry_acquire_value(callBackFunc);
165     }
SetTimeUpdateSyncCBFunc(jerry_value_t & callBackFunc)166     void SetTimeUpdateSyncCBFunc(jerry_value_t& callBackFunc)
167     {
168         timeUpdateSyncCallBackFunc_ = jerry_acquire_value(callBackFunc);
169     }
170 
171     void FormatTime(uint32_t inSeconds, char* outBuffer, uint8_t bufferLength);
172     void UpdatePanelTimeText(bool currentTime = true);
173     void SetCurrentPlayTimeText(int32_t inSeconds);
174     void CallJSFunctionWithOnePara(const jerry_value_t &callBackFunc, const int64_t inputValue);
175 private:
176     void SetVideoPanel();
177     void SetSurfaceView();
178     bool ResetPlayer();
179     bool DestroyPlayer();
180     bool CreatePlayer();
181     static void *RebuildPlayerHandler(void *arg);
182     static pthread_mutex_t rebuildPlayerlock_;
183     static pthread_cond_t rebuildPlayerCondition_;
184 
185     PanelView* panelView_;
186     Media::Player* videoPlayer_;
187     UISurfaceView* surfaceView_;
188     std::shared_ptr<Media::PlayerCallback> playEventListener_;
189     VideoStateCallback* playStateCallback_;
190     jerry_value_t preparedSyncCallBackFunc_;
191     jerry_value_t startSyncCallBackFunc_;
192     jerry_value_t pauseSyncCallBackFunc_;
193     jerry_value_t timeUpdateSyncCallBackFunc_;
194     char* videoSrc_;
195     bool mutedFlag_;
196     bool autoPlayFlag_;
197 };
198 } // namespace ACELite
199 } // namespace OHOS
200 
201 #endif // FEATURE_COMPONENT_VIDEO
202 #endif // OHOS_ACELITE_VIDEO_VIEW_H
203