[3.0] Remove custom shader from video view example and change video file sample
[platform/core/uifw/dali-demo.git] / examples / video-view / video-view-example.cpp
1 /*
2  * Copyright (c) 2015 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   {
52     // Connect to the Application's Init signal
53     mApplication.InitSignal().Connect( this, &VideoViewController::Create );
54   }
55
56   ~VideoViewController()
57   {
58     mVideoView.Stop();
59   }
60
61   void Create( Application& application )
62   {
63     mStageSize = Stage::GetCurrent().GetSize();
64
65     mVideoView = Toolkit::VideoView::New();
66     Stage::GetCurrent().Add( mVideoView );
67     mVideoView.SetParentOrigin( ParentOrigin::CENTER );
68     mVideoView.SetAnchorPoint( AnchorPoint::CENTER );
69     mVideoView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
70     mVideoView.SetSize( INIT_WIDTH, INIT_HEIGHT );
71     mVideoView.SetProperty( VideoView::Property::LOOPING, true );
72     mVideoView.SetProperty( VideoView::Property::MUTED, false );
73     mVideoView.SetProperty( VideoView::Property::VIDEO, PLAY_FILE );
74
75     mMenu = Layer::New();
76     mMenu.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
77     mMenu.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
78     mMenu.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
79     mMenu.SetSize( INIT_WIDTH, 120 );
80     mVideoView.Add( mMenu );
81
82     mPlayButton = PushButton::New();
83     mPlayButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
84     mPlayButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
85     mPlayButton.SetName( "Play" );
86     mPlayButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
87     mPlayButton.SetSize( BUTTON_SIZE, BUTTON_SIZE );
88     mPlayButton.SetPosition( 40, 10 );
89     mPlayButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
90
91     mPauseButton = PushButton::New();
92     mPauseButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
93     mPauseButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
94     mPauseButton.SetName( "Pause" );
95     mPauseButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
96     mPauseButton.SetSize( BUTTON_SIZE, BUTTON_SIZE );
97     mPauseButton.SetPosition( 40, 10 );
98     mPauseButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
99
100     mStopButton = PushButton::New();
101     mStopButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
102     mStopButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
103     mStopButton.SetName( "Stop" );
104     mStopButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
105     mStopButton.SetSize( BUTTON_SIZE, BUTTON_SIZE );
106     mStopButton.SetPosition( 140, 10 );
107     mStopButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
108
109     mResetButton = PushButton::New();
110     mResetButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
111     mResetButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
112     mResetButton.SetName( "Reset" );
113     mResetButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
114     mResetButton.SetSize( BUTTON_SIZE, BUTTON_SIZE );
115     mResetButton.SetPosition( 140, 10 );
116     mResetButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
117
118     mBackwardButton = PushButton::New();
119     mBackwardButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
120     mBackwardButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
121     mBackwardButton.SetName( "Backward" );
122     mBackwardButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
123     mBackwardButton.SetSize( BUTTON_SIZE, BUTTON_SIZE );
124     mBackwardButton.SetPosition( 240, 10 );
125     mBackwardButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
126
127     mForwardButton = PushButton::New();
128     mForwardButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
129     mForwardButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
130     mForwardButton.SetName( "Forward" );
131     mForwardButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
132     mForwardButton.SetSize( BUTTON_SIZE, BUTTON_SIZE );
133     mForwardButton.SetPosition( 340, 10 );
134     mForwardButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
135
136     mMenu.Add( mPlayButton );
137     mMenu.Add( mPauseButton );
138     mMenu.Add( mStopButton );
139     mMenu.Add( mResetButton );
140     mMenu.Add( mBackwardButton );
141     mMenu.Add( mForwardButton );
142
143     mPauseButton.SetVisible( false );
144     mPauseButton.SetDisabled( true );
145     mPlayButton.SetVisible( true );
146     mPlayButton.SetDisabled( false );
147     mStopButton.SetVisible( true );
148     mStopButton.SetDisabled( false );
149     mResetButton.SetVisible( false );
150     mResetButton.SetDisabled( true );
151
152     mPlayButton.SetUnselectedImage( PLAY_IMAGE );
153     mPlayButton.SetSelectedImage( PLAY_IMAGE );
154     mPauseButton.SetUnselectedImage( PAUSE_IMAGE );
155     mPauseButton.SetSelectedImage( PAUSE_IMAGE );
156
157     mStopButton.SetUnselectedImage( STOP_IMAGE );
158     mStopButton.SetSelectedImage( STOP_IMAGE );
159     mResetButton.SetUnselectedImage( RESET_IMAGE );
160     mResetButton.SetSelectedImage( RESET_IMAGE );
161
162     mBackwardButton.SetUnselectedImage( BACKWARD_IMAGE );
163     mBackwardButton.SetSelectedImage( BACKWARD_IMAGE );
164     mForwardButton.SetUnselectedImage( FORWARD_IMAGE );
165     mForwardButton.SetSelectedImage( FORWARD_IMAGE );
166
167     mPanGestureDetector = PanGestureDetector::New();
168     mPanGestureDetector.Attach( mVideoView );
169     mPanGestureDetector.DetectedSignal().Connect( this, &VideoViewController::OnPan );
170
171     mPinchGestureDetector = PinchGestureDetector::New();
172     mPinchGestureDetector.Attach( mVideoView );
173     mPinchGestureDetector.DetectedSignal().Connect( this, &VideoViewController::OnPinch );
174
175     mTapGestureDetector = TapGestureDetector::New();
176     mTapGestureDetector.Attach( mVideoView );
177     mTapGestureDetector.DetectedSignal().Connect( this, &VideoViewController::OnTap );
178
179     mRotationAnimation = Animation::New(2.f);
180     mRotationAnimation.AnimateBy( Property(mVideoView, Actor::Property::ORIENTATION), Quaternion(Degree(0.f), Degree(360.f), Degree(0.f)) );
181     mRotationAnimation.SetLooping(false);
182
183     Stage::GetCurrent().KeyEventSignal().Connect( this, &VideoViewController::OnKeyEvent );
184
185     mWindowSurfaceTarget.Insert( "RENDERING_TARGET", "windowSurfaceTarget" );
186     mNativeImageTarget.Insert( "RENDERING_TARGET", "nativeImageTarget" );
187
188     mVideoView.FinishedSignal().Connect( this, &VideoViewController::OnFinished );
189   }
190
191   bool OnButtonClicked( Button button )
192   {
193
194     if( mPauseButton.GetId() == button.GetId())
195     {
196        if( mIsPlay )
197       {
198         mPauseButton.SetVisible( false );
199         mPauseButton.SetDisabled( true );
200         mPlayButton.SetVisible( true );
201         mPlayButton.SetDisabled( false );
202
203         mIsPlay = false;
204         mVideoView.Pause();
205       }
206     }
207     else if( mPlayButton.GetId() == button.GetId())
208     {
209       if( mIsStop )
210       {
211         return false;
212       }
213
214       mPauseButton.SetVisible( true );
215       mPauseButton.SetDisabled( false );
216       mPlayButton.SetVisible( false );
217       mPlayButton.SetDisabled( true );
218
219       mIsPlay = true;
220       mVideoView.Play();
221     }
222     else if( mResetButton.GetId() == button.GetId())
223     {
224       if( mIsStop )
225       {
226         mPauseButton.SetVisible( true );
227         mPauseButton.SetDisabled( false );
228         mPlayButton.SetVisible( false );
229         mPlayButton.SetDisabled( true );
230         mResetButton.SetVisible( false );
231         mResetButton.SetDisabled( true );
232         mStopButton.SetVisible( true );
233         mStopButton.SetDisabled( false );
234
235         mIsStop = false;
236         mIsPlay = true;
237         mVideoView.SetProperty( VideoView::Property::VIDEO, PLAY_FILE );
238         mVideoView.Play();
239       }
240     }
241     else if( mStopButton.GetId() == button.GetId())
242     {
243       mPauseButton.SetVisible( false );
244       mPauseButton.SetDisabled( true );
245       mPlayButton.SetVisible( true );
246       mPlayButton.SetDisabled( false );
247       mResetButton.SetVisible( true );
248       mResetButton.SetDisabled( false );
249       mStopButton.SetVisible( false );
250       mStopButton.SetDisabled( true );
251
252       mIsStop = true;
253       mVideoView.Stop();
254     }
255     else if( mBackwardButton.GetId() == button.GetId())
256     {
257       mVideoView.Backward( SEEK_POS );
258     }
259     else if( mForwardButton.GetId() == button.GetId())
260     {
261       mVideoView.Forward( SEEK_POS );
262     }
263
264     return true;
265   }
266
267   void OnFinished( VideoView& view )
268   {
269     if( !mIsFullScreen )
270     {
271       mRotationAnimation.Play();
272       mIsStop = true;
273     }
274   }
275
276   void OnPan( Actor actor, const PanGesture& gesture )
277   {
278     if( !mIsFullScreen && gesture.state == Gesture::Continuing )
279     {
280       mVideoView.TranslateBy( Vector3( gesture.displacement ) );
281     }
282   }
283
284   void OnPinch( Actor actor, const PinchGesture& gesture )
285   {
286     if( gesture.state == Gesture::Started )
287     {
288       mPinchStartScale = mScale;
289     }
290
291     if( gesture.state == Gesture::Finished )
292     {
293       mScale = mPinchStartScale * gesture.scale;
294       if( mScale > 1.f )
295       {
296         mVideoView.SetSize( mStageSize.x, mStageSize.y );
297         mVideoView.SetProperty( VideoView::Property::VIDEO, mWindowSurfaceTarget );
298         mMenu.SetSize( mStageSize.x, 120 );
299         mIsFullScreen = true;
300       }
301       else
302       {
303         mVideoView.SetSize( INIT_WIDTH, INIT_HEIGHT );
304         mVideoView.SetProperty( VideoView::Property::VIDEO, mNativeImageTarget );
305         mMenu.SetSize( INIT_WIDTH, 120 );
306         mIsFullScreen = false;
307       }
308     }
309   }
310
311   void OnTap( Actor actor, const TapGesture& tapGesture )
312   {
313     if( !mIsFullScreen )
314     {
315       mRotationAnimation.Play();
316     }
317   }
318
319 private:
320
321   /**
322    * Main key event handler
323    */
324   void OnKeyEvent(const KeyEvent& event)
325   {
326     if(event.state == KeyEvent::Down)
327     {
328       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
329       {
330         mApplication.Quit();
331       }
332     }
333   }
334
335 private:
336   Application&  mApplication;
337   VideoView mVideoView;
338   Layer mMenu;
339   Vector2 mStageSize;
340
341   bool mIsPlay;
342   bool mIsStop;
343   bool mIsFullScreen;
344
345   PushButton mPlayButton;
346   PushButton mPauseButton;
347   PushButton mStopButton;
348   PushButton mResetButton;
349   PushButton mBackwardButton;
350   PushButton mForwardButton;
351
352   PanGestureDetector mPanGestureDetector;
353   PinchGestureDetector mPinchGestureDetector;
354   TapGestureDetector mTapGestureDetector;
355   float mScale;
356   float mPinchStartScale;
357
358   Animation mRotationAnimation;
359   Property::Map mWindowSurfaceTarget;
360   Property::Map mNativeImageTarget;
361 };
362
363 void RunTest( Application& application )
364 {
365   VideoViewController test( application );
366
367   application.MainLoop();
368 }
369
370 // Entry point for Linux & Tizen applications
371 //
372 int DALI_EXPORT_API main( int argc, char **argv )
373 {
374   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
375
376   RunTest( application );
377
378   return 0;
379 }