cb29b8d98773e48ad4b1e2ecb115cbd513541f00
[platform/core/uifw/dali-demo.git] / examples / video-view / video-view-example.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <dali/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
20
21 using namespace Dali;
22 using namespace Toolkit;
23
24 namespace
25 {
26   const int SEEK_POS( 5000 );
27   const int INIT_WIDTH( 600 );
28   const int INIT_HEIGHT( 400 );
29   const int BUTTON_SIZE( 80 );
30
31   const char* const PLAY_FILE = DEMO_VIDEO_DIR "demoVideo.mp4";
32   const char* const PLAY_IMAGE = DEMO_IMAGE_DIR "icon-play.png";
33   const char* const PAUSE_IMAGE = DEMO_IMAGE_DIR "Pause.png";
34   const char* const STOP_IMAGE = DEMO_IMAGE_DIR "icon-stop.png";
35   const char* const RESET_IMAGE = DEMO_IMAGE_DIR "icon-reset.png";
36   const char* const FORWARD_IMAGE = DEMO_IMAGE_DIR "Forward.png";
37   const char* const BACKWARD_IMAGE = DEMO_IMAGE_DIR "Backward.png";
38
39 }  // namespace
40
41 class VideoViewController: public ConnectionTracker
42 {
43  public:
44
45   VideoViewController( Application& application )
46     : mApplication( application ),
47       mIsPlay( false ),
48       mIsStop( false ),
49       mIsFullScreen( false ),
50       mScale( 1.f ),
51       mPinchStartScale( 1.0f )
52   {
53     // Connect to the Application's Init signal
54     mApplication.InitSignal().Connect( this, &VideoViewController::Create );
55   }
56
57   ~VideoViewController()
58   {
59     mVideoView.Stop();
60   }
61
62   void Create( Application& application )
63   {
64     mStageSize = Stage::GetCurrent().GetSize();
65
66     mVideoView = Toolkit::VideoView::New();
67     Stage::GetCurrent().Add( mVideoView );
68     mVideoView.SetParentOrigin( ParentOrigin::CENTER );
69     mVideoView.SetAnchorPoint( AnchorPoint::CENTER );
70     mVideoView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
71     mVideoView.SetSize( INIT_WIDTH, INIT_HEIGHT );
72     mVideoView.SetProperty( VideoView::Property::LOOPING, true );
73     mVideoView.SetProperty( VideoView::Property::MUTED, false );
74     mVideoView.SetProperty( VideoView::Property::VIDEO, PLAY_FILE );
75
76     mMenu = Layer::New();
77     mMenu.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
78     mMenu.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
79     mMenu.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
80     mMenu.SetSize( INIT_WIDTH, 120 );
81     mVideoView.Add( mMenu );
82
83     mPlayButton = PushButton::New();
84     mPlayButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
85     mPlayButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
86     mPlayButton.SetName( "Play" );
87     mPlayButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
88     mPlayButton.SetSize( BUTTON_SIZE, BUTTON_SIZE );
89     mPlayButton.SetPosition( 40, 10 );
90     mPlayButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
91
92     mPauseButton = PushButton::New();
93     mPauseButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
94     mPauseButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
95     mPauseButton.SetName( "Pause" );
96     mPauseButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
97     mPauseButton.SetSize( BUTTON_SIZE, BUTTON_SIZE );
98     mPauseButton.SetPosition( 40, 10 );
99     mPauseButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
100
101     mStopButton = PushButton::New();
102     mStopButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
103     mStopButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
104     mStopButton.SetName( "Stop" );
105     mStopButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
106     mStopButton.SetSize( BUTTON_SIZE, BUTTON_SIZE );
107     mStopButton.SetPosition( 140, 10 );
108     mStopButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
109
110     mResetButton = PushButton::New();
111     mResetButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
112     mResetButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
113     mResetButton.SetName( "Reset" );
114     mResetButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
115     mResetButton.SetSize( BUTTON_SIZE, BUTTON_SIZE );
116     mResetButton.SetPosition( 140, 10 );
117     mResetButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
118
119     mBackwardButton = PushButton::New();
120     mBackwardButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
121     mBackwardButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
122     mBackwardButton.SetName( "Backward" );
123     mBackwardButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
124     mBackwardButton.SetSize( BUTTON_SIZE, BUTTON_SIZE );
125     mBackwardButton.SetPosition( 240, 10 );
126     mBackwardButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
127
128     mForwardButton = PushButton::New();
129     mForwardButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
130     mForwardButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
131     mForwardButton.SetName( "Forward" );
132     mForwardButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
133     mForwardButton.SetSize( BUTTON_SIZE, BUTTON_SIZE );
134     mForwardButton.SetPosition( 340, 10 );
135     mForwardButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
136
137     mMenu.Add( mPlayButton );
138     mMenu.Add( mPauseButton );
139     mMenu.Add( mStopButton );
140     mMenu.Add( mResetButton );
141     mMenu.Add( mBackwardButton );
142     mMenu.Add( mForwardButton );
143
144     mPauseButton.SetVisible( false );
145     mPauseButton.SetDisabled( true );
146     mPlayButton.SetVisible( true );
147     mPlayButton.SetDisabled( false );
148     mStopButton.SetVisible( true );
149     mStopButton.SetDisabled( false );
150     mResetButton.SetVisible( false );
151     mResetButton.SetDisabled( true );
152
153     mPlayButton.SetUnselectedImage( PLAY_IMAGE );
154     mPlayButton.SetSelectedImage( PLAY_IMAGE );
155     mPauseButton.SetUnselectedImage( PAUSE_IMAGE );
156     mPauseButton.SetSelectedImage( PAUSE_IMAGE );
157
158     mStopButton.SetUnselectedImage( STOP_IMAGE );
159     mStopButton.SetSelectedImage( STOP_IMAGE );
160     mResetButton.SetUnselectedImage( RESET_IMAGE );
161     mResetButton.SetSelectedImage( RESET_IMAGE );
162
163     mBackwardButton.SetUnselectedImage( BACKWARD_IMAGE );
164     mBackwardButton.SetSelectedImage( BACKWARD_IMAGE );
165     mForwardButton.SetUnselectedImage( FORWARD_IMAGE );
166     mForwardButton.SetSelectedImage( FORWARD_IMAGE );
167
168     mPanGestureDetector = PanGestureDetector::New();
169     mPanGestureDetector.Attach( mVideoView );
170     mPanGestureDetector.DetectedSignal().Connect( this, &VideoViewController::OnPan );
171
172     mPinchGestureDetector = PinchGestureDetector::New();
173     mPinchGestureDetector.Attach( mVideoView );
174     mPinchGestureDetector.DetectedSignal().Connect( this, &VideoViewController::OnPinch );
175
176     mTapGestureDetector = TapGestureDetector::New();
177     mTapGestureDetector.Attach( mVideoView );
178     mTapGestureDetector.DetectedSignal().Connect( this, &VideoViewController::OnTap );
179
180     mRotationAnimation = Animation::New(2.f);
181     mRotationAnimation.AnimateBy( Property(mVideoView, Actor::Property::ORIENTATION), Quaternion(Degree(0.f), Degree(360.f), Degree(0.f)) );
182     mRotationAnimation.SetLooping(false);
183
184     Stage::GetCurrent().KeyEventSignal().Connect( this, &VideoViewController::OnKeyEvent );
185
186     mWindowSurfaceTarget.Insert( "RENDERING_TARGET", "windowSurfaceTarget" );
187     mNativeImageTarget.Insert( "RENDERING_TARGET", "nativeImageTarget" );
188
189     mVideoView.FinishedSignal().Connect( this, &VideoViewController::OnFinished );
190   }
191
192   bool OnButtonClicked( Button button )
193   {
194
195     if( mPauseButton.GetId() == button.GetId())
196     {
197        if( mIsPlay )
198       {
199         mPauseButton.SetVisible( false );
200         mPauseButton.SetDisabled( true );
201         mPlayButton.SetVisible( true );
202         mPlayButton.SetDisabled( false );
203
204         mIsPlay = false;
205         mVideoView.Pause();
206       }
207     }
208     else if( mPlayButton.GetId() == button.GetId())
209     {
210       if( mIsStop )
211       {
212         return false;
213       }
214
215       mPauseButton.SetVisible( true );
216       mPauseButton.SetDisabled( false );
217       mPlayButton.SetVisible( false );
218       mPlayButton.SetDisabled( true );
219
220       mIsPlay = true;
221       mVideoView.Play();
222     }
223     else if( mResetButton.GetId() == button.GetId())
224     {
225       if( mIsStop )
226       {
227         mPauseButton.SetVisible( true );
228         mPauseButton.SetDisabled( false );
229         mPlayButton.SetVisible( false );
230         mPlayButton.SetDisabled( true );
231         mResetButton.SetVisible( false );
232         mResetButton.SetDisabled( true );
233         mStopButton.SetVisible( true );
234         mStopButton.SetDisabled( false );
235
236         mIsStop = false;
237         mIsPlay = true;
238         mVideoView.SetProperty( VideoView::Property::VIDEO, PLAY_FILE );
239         mVideoView.Play();
240       }
241     }
242     else if( mStopButton.GetId() == button.GetId())
243     {
244       mPauseButton.SetVisible( false );
245       mPauseButton.SetDisabled( true );
246       mPlayButton.SetVisible( true );
247       mPlayButton.SetDisabled( false );
248       mResetButton.SetVisible( true );
249       mResetButton.SetDisabled( false );
250       mStopButton.SetVisible( false );
251       mStopButton.SetDisabled( true );
252
253       mIsStop = true;
254       mVideoView.Stop();
255     }
256     else if( mBackwardButton.GetId() == button.GetId())
257     {
258       mVideoView.Backward( SEEK_POS );
259     }
260     else if( mForwardButton.GetId() == button.GetId())
261     {
262       mVideoView.Forward( SEEK_POS );
263     }
264
265     return true;
266   }
267
268   void OnFinished( VideoView& view )
269   {
270     if( !mIsFullScreen )
271     {
272       mRotationAnimation.Play();
273       mIsStop = true;
274     }
275   }
276
277   void OnPan( Actor actor, const PanGesture& gesture )
278   {
279     if( !mIsFullScreen && gesture.state == Gesture::Continuing )
280     {
281       mVideoView.TranslateBy( Vector3( gesture.displacement ) );
282     }
283   }
284
285   void OnPinch( Actor actor, const PinchGesture& gesture )
286   {
287     if( gesture.state == Gesture::Started )
288     {
289       mPinchStartScale = mScale;
290     }
291
292     if( gesture.state == Gesture::Finished )
293     {
294       mScale = mPinchStartScale * gesture.scale;
295       if( mScale > 1.f )
296       {
297         mVideoView.SetSize( mStageSize.x, mStageSize.y );
298         mVideoView.SetProperty( VideoView::Property::VIDEO, mWindowSurfaceTarget );
299         mMenu.SetSize( mStageSize.x, 120 );
300         mIsFullScreen = true;
301       }
302       else
303       {
304         mVideoView.SetSize( INIT_WIDTH, INIT_HEIGHT );
305         mVideoView.SetProperty( VideoView::Property::VIDEO, mNativeImageTarget );
306         mMenu.SetSize( INIT_WIDTH, 120 );
307         mIsFullScreen = false;
308       }
309     }
310   }
311
312   void OnTap( Actor actor, const TapGesture& tapGesture )
313   {
314     if( !mIsFullScreen )
315     {
316       mRotationAnimation.Play();
317     }
318   }
319
320 private:
321
322   /**
323    * Main key event handler
324    */
325   void OnKeyEvent(const KeyEvent& event)
326   {
327     if(event.state == KeyEvent::Down)
328     {
329       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
330       {
331         mApplication.Quit();
332       }
333     }
334   }
335
336 private:
337   Application&  mApplication;
338   VideoView mVideoView;
339   Layer mMenu;
340   Vector2 mStageSize;
341
342   bool mIsPlay;
343   bool mIsStop;
344   bool mIsFullScreen;
345
346   PushButton mPlayButton;
347   PushButton mPauseButton;
348   PushButton mStopButton;
349   PushButton mResetButton;
350   PushButton mBackwardButton;
351   PushButton mForwardButton;
352
353   PanGestureDetector mPanGestureDetector;
354   PinchGestureDetector mPinchGestureDetector;
355   TapGestureDetector mTapGestureDetector;
356   float mScale;
357   float mPinchStartScale;
358
359   Animation mRotationAnimation;
360   Property::Map mWindowSurfaceTarget;
361   Property::Map mNativeImageTarget;
362 };
363
364 void RunTest( Application& application )
365 {
366   VideoViewController test( application );
367
368   application.MainLoop();
369 }
370
371 // Entry point for Linux & Tizen applications
372 //
373 int DALI_EXPORT_API main( int argc, char **argv )
374 {
375   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
376
377   RunTest( application );
378
379   return 0;
380 }