Modified video-view sample
[platform/core/uifw/dali-demo.git] / examples / video-view / video-view-example.cpp
1 /*
2  * Copyright (c) 2017 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 #include <dali-toolkit/devel-api/controls/buttons/button-devel.h>
21
22 using namespace Dali;
23 using namespace Toolkit;
24
25 namespace
26 {
27   const int SEEK_POS( 5000 );
28   const int INIT_WIDTH( 600 );
29   const int INIT_HEIGHT( 400 );
30   const int BUTTON_SIZE( 80 );
31
32   const char* const PLAY_FILE = DEMO_VIDEO_DIR "demoVideo.mp4";
33   const char* const PLAY_IMAGE = DEMO_IMAGE_DIR "icon-play.png";
34   const char* const PAUSE_IMAGE = DEMO_IMAGE_DIR "Pause.png";
35   const char* const CHANGE_IMAGE = DEMO_IMAGE_DIR "icon-change.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       mIsFullScreen( false ),
49       mScale( 1.f ),
50       mPinchStartScale( 1.0f )
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     application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
64
65     mStageSize = Stage::GetCurrent().GetSize();
66
67     mVideoView = Toolkit::VideoView::New();
68     Stage::GetCurrent().Add( mVideoView );
69     mVideoView.SetParentOrigin( ParentOrigin::CENTER );
70     mVideoView.SetAnchorPoint( AnchorPoint::CENTER );
71     mVideoView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
72     mVideoView.SetSize( INIT_WIDTH, INIT_HEIGHT );
73     mVideoView.SetProperty( VideoView::Property::LOOPING, true );
74     mVideoView.SetProperty( VideoView::Property::MUTED, false );
75     mVideoView.SetProperty( VideoView::Property::VIDEO, PLAY_FILE );
76
77     mMenu = Layer::New();
78     mMenu.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
79     mMenu.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
80     mMenu.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
81     mMenu.SetSize( INIT_WIDTH, 120 );
82     mVideoView.Add( mMenu );
83
84     mPlayButton = PushButton::New();
85     mPlayButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
86     mPlayButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
87     mPlayButton.SetName( "Play" );
88     mPlayButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
89     mPlayButton.SetSize( BUTTON_SIZE, BUTTON_SIZE );
90     mPlayButton.SetPosition( 40, 10 );
91     mPlayButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
92
93     mPauseButton = PushButton::New();
94     mPauseButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
95     mPauseButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
96     mPauseButton.SetName( "Pause" );
97     mPauseButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
98     mPauseButton.SetSize( BUTTON_SIZE, BUTTON_SIZE );
99     mPauseButton.SetPosition( 40, 10 );
100     mPauseButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
101
102     mChangeButton = PushButton::New();
103     mChangeButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
104     mChangeButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
105     mChangeButton.SetName( "Change" );
106     mChangeButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
107     mChangeButton.SetSize( BUTTON_SIZE, BUTTON_SIZE );
108     mChangeButton.SetPosition( 140, 10 );
109     mChangeButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
110
111     mBackwardButton = PushButton::New();
112     mBackwardButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
113     mBackwardButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
114     mBackwardButton.SetName( "Backward" );
115     mBackwardButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
116     mBackwardButton.SetSize( BUTTON_SIZE, BUTTON_SIZE );
117     mBackwardButton.SetPosition( 240, 10 );
118     mBackwardButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
119
120     mForwardButton = PushButton::New();
121     mForwardButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
122     mForwardButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
123     mForwardButton.SetName( "Forward" );
124     mForwardButton.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
125     mForwardButton.SetSize( BUTTON_SIZE, BUTTON_SIZE );
126     mForwardButton.SetPosition( 340, 10 );
127     mForwardButton.ClickedSignal().Connect( this, &VideoViewController::OnButtonClicked );
128
129     mMenu.Add( mPlayButton );
130     mMenu.Add( mPauseButton );
131     mMenu.Add( mChangeButton );
132     mMenu.Add( mBackwardButton );
133     mMenu.Add( mForwardButton );
134
135     mPauseButton.SetVisible( false );
136     mPauseButton.SetProperty( Button::Property::DISABLED, true );
137     mPlayButton.SetVisible( true );
138     mPlayButton.SetProperty( Button::Property::DISABLED, false );
139     mChangeButton.SetVisible( true );
140     mChangeButton.SetProperty( Button::Property::DISABLED, false );
141
142     mPlayButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, PLAY_IMAGE );
143     mPlayButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, PLAY_IMAGE );
144     mPauseButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, PAUSE_IMAGE );
145     mPauseButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, PAUSE_IMAGE );
146
147     mChangeButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, CHANGE_IMAGE );
148     mChangeButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, CHANGE_IMAGE );
149
150     mBackwardButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, BACKWARD_IMAGE );
151     mBackwardButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, BACKWARD_IMAGE );
152     mForwardButton.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, FORWARD_IMAGE );
153     mForwardButton.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, FORWARD_IMAGE );
154
155     mPanGestureDetector = PanGestureDetector::New();
156     mPanGestureDetector.Attach( mVideoView );
157     mPanGestureDetector.DetectedSignal().Connect( this, &VideoViewController::OnPan );
158
159     mPinchGestureDetector = PinchGestureDetector::New();
160     mPinchGestureDetector.Attach( mVideoView );
161     mPinchGestureDetector.DetectedSignal().Connect( this, &VideoViewController::OnPinch );
162
163     mTapGestureDetector = TapGestureDetector::New();
164     mTapGestureDetector.Attach( mVideoView );
165     mTapGestureDetector.DetectedSignal().Connect( this, &VideoViewController::OnTap );
166
167     mRotationAnimation = Animation::New(2.f);
168     mRotationAnimation.AnimateBy( Property(mVideoView, Actor::Property::ORIENTATION), Quaternion(Degree(0.f), Degree(360.f), Degree(0.f)) );
169     mRotationAnimation.SetLooping(false);
170
171     Stage::GetCurrent().KeyEventSignal().Connect( this, &VideoViewController::OnKeyEvent );
172
173   }
174
175   bool OnButtonClicked( Button button )
176   {
177     if( mPauseButton.GetId() == button.GetId())
178     {
179        if( mIsPlay )
180       {
181         mPauseButton.SetVisible( false );
182         mPauseButton.SetProperty( Button::Property::DISABLED, true );
183         mPlayButton.SetVisible( true );
184         mPlayButton.SetProperty( Button::Property::DISABLED, false );
185
186         mIsPlay = false;
187         mVideoView.Pause();
188       }
189     }
190     else if( mPlayButton.GetId() == button.GetId())
191     {
192       mPauseButton.SetVisible( true );
193       mPauseButton.SetProperty( Button::Property::DISABLED, false );
194       mPlayButton.SetVisible( false );
195       mPlayButton.SetProperty( Button::Property::DISABLED, true );
196
197       mIsPlay = true;
198       mVideoView.Play();
199     }
200     else if( mChangeButton.GetId() == button.GetId())
201     {
202       bool underlay = false;
203       underlay = mVideoView.GetProperty( Toolkit::VideoView::Property::UNDERLAY ).Get< bool >();
204       if( underlay )
205       {
206         mVideoView.SetProperty( Toolkit::VideoView::Property::UNDERLAY, false );
207       }
208       else
209       {
210         mVideoView.SetProperty( Toolkit::VideoView::Property::UNDERLAY, true );
211       }
212     }
213     else if( mBackwardButton.GetId() == button.GetId())
214     {
215       mVideoView.Backward( SEEK_POS );
216     }
217     else if( mForwardButton.GetId() == button.GetId())
218     {
219       mVideoView.Forward( SEEK_POS );
220     }
221
222     return true;
223   }
224
225   void OnPan( Actor actor, const PanGesture& gesture )
226   {
227     if( !mIsFullScreen && gesture.state == Gesture::Continuing )
228     {
229       mVideoView.TranslateBy( Vector3( gesture.displacement ) );
230     }
231   }
232
233   void OnPinch( Actor actor, const PinchGesture& gesture )
234   {
235     if( gesture.state == Gesture::Started )
236     {
237       mPinchStartScale = mScale;
238     }
239
240     if( gesture.state == Gesture::Finished )
241     {
242       mScale = mPinchStartScale * gesture.scale;
243       mVideoView.SetScale( mScale );
244     }
245   }
246
247   void OnTap( Actor actor, const TapGesture& tapGesture )
248   {
249     if( !mIsFullScreen )
250     {
251       mVideoView.SetSize( mStageSize.x, mStageSize.y );
252       mIsFullScreen = true;
253     }
254     else
255     {
256       mVideoView.SetSize( INIT_WIDTH, INIT_HEIGHT );
257       mIsFullScreen = false;
258     }
259   }
260
261 private:
262
263   /**
264    * Main key event handler
265    */
266   void OnKeyEvent(const KeyEvent& event)
267   {
268     if(event.state == KeyEvent::Down)
269     {
270       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
271       {
272         mApplication.Quit();
273       }
274     }
275   }
276
277 private:
278   Application&  mApplication;
279   VideoView mVideoView;
280   Layer mMenu;
281   Vector2 mStageSize;
282
283   bool mIsPlay;
284   bool mIsFullScreen;
285
286   PushButton mPlayButton;
287   PushButton mPauseButton;
288   PushButton mChangeButton;
289   PushButton mResetButton;
290   PushButton mBackwardButton;
291   PushButton mForwardButton;
292
293   PanGestureDetector mPanGestureDetector;
294   PinchGestureDetector mPinchGestureDetector;
295   TapGestureDetector mTapGestureDetector;
296   float mScale;
297   float mPinchStartScale;
298
299   Animation mRotationAnimation;
300 };
301
302 int DALI_EXPORT_API main( int argc, char **argv )
303 {
304   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
305   VideoViewController test( application );
306   application.MainLoop();
307   return 0;
308 }