Revert "Modified video view impl by considering visual"
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / video-view / video-view-impl.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 // CLASS HEADER
19 #include "video-view-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <cstring>
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/public-api/object/type-registry-helper.h>
25 #include <dali/public-api/common/stage.h>
26 #include <dali/devel-api/scripting/scripting.h>
27 #include <dali/public-api/adaptor-framework/native-image-source.h>
28 #include <dali/integration-api/adaptors/adaptor.h>
29
30 // INTERNAL INCLUDES
31 #include <dali-toolkit/public-api/controls/video-view/video-view.h>
32 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
33
34 namespace Dali
35 {
36
37 namespace Toolkit
38 {
39
40 namespace Internal
41 {
42
43 namespace
44 {
45
46 BaseHandle Create()
47 {
48   return Toolkit::VideoView::New();
49 }
50
51 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::VideoView, Toolkit::Control, Create );
52
53 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "video", MAP, VIDEO )
54 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "looping", BOOLEAN, LOOPING )
55 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "muted", BOOLEAN, MUTED )
56 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "volume", MAP, VOLUME )
57
58 DALI_SIGNAL_REGISTRATION( Toolkit, VideoView, "finished", FINISHED_SIGNAL )
59
60 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "play", ACTION_VIDEOVIEW_PLAY )
61 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "pause", ACTION_VIDEOVIEW_PAUSE )
62 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "stop", ACTION_VIDEOVIEW_STOP )
63 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "forward", ACTION_VIDEOVIEW_FORWARD )
64 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "backward", ACTION_VIDEOVIEW_BACKWARD )
65
66 DALI_TYPE_REGISTRATION_END()
67
68 const char* const VOLUME_LEFT( "volumeLeft" );
69 const char* const VOLUME_RIGHT( "volumeRight" );
70 const char* const RENDERING_TARGET( "renderingTarget" );
71 const char* const WINDOW_SURFACE_TARGET( "windowSurfaceTarget" );
72 const char* const NATIVE_IMAGE_TARGET( "nativeImageTarget" );
73
74 } // anonymous namepsace
75
76 VideoView::VideoView()
77 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
78   mCurrentVideoPlayPosition( 0 ),
79   mIsNativeImageTarget( true ),
80   mIsPlay( false ),
81   mIsPause( false )
82 {
83   mVideoPlayer = Dali::VideoPlayer::New();
84 }
85
86 VideoView::~VideoView()
87 {
88 }
89
90 Toolkit::VideoView VideoView::New()
91 {
92   VideoView* impl = new VideoView();
93   Toolkit::VideoView handle = Toolkit::VideoView( *impl );
94
95   impl->Initialize();
96
97   return handle;
98 }
99
100 void VideoView::OnInitialize()
101 {
102   Any source;
103   Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
104   mNativeImage = Dali::NativeImage::New( *nativeImageSourcePtr );
105
106   mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
107   mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
108 }
109
110 void VideoView::SetUrl( const std::string& url )
111 {
112   if( mUrl != url || !mPropertyMap.Empty() )
113   {
114     mUrl = url;
115     mPropertyMap.Clear();
116   }
117
118   if( mIsNativeImageTarget )
119   {
120     Actor self( Self() );
121     Internal::InitializeVisual( self, mVisual, mNativeImage );
122   }
123
124   mVideoPlayer.SetUrl( mUrl );
125 }
126
127 void VideoView::SetPropertyMap( Property::Map map )
128 {
129   mPropertyMap = map;
130
131   Actor self( Self() );
132   Internal::InitializeVisual( self, mVisual, mPropertyMap );
133
134   Property::Value* widthValue = mPropertyMap.Find( "width" );
135   if( widthValue )
136   {
137     int width;
138     if( widthValue->Get( width ) )
139     {
140       mVideoSize = ImageDimensions( width, mVideoSize.GetHeight() );
141     }
142   }
143
144   Property::Value* heightValue = mPropertyMap.Find( "height" );
145   if( heightValue )
146   {
147     int height;
148     if( heightValue->Get( height ) )
149     {
150       mVideoSize = ImageDimensions( mVideoSize.GetWidth(), height );
151     }
152   }
153
154   RelayoutRequest();
155 }
156
157 std::string VideoView::GetUrl()
158 {
159   return mUrl;
160 }
161
162 void VideoView::SetLooping(bool looping)
163 {
164   mVideoPlayer.SetLooping( looping );
165 }
166
167 bool VideoView::IsLooping()
168 {
169   return mVideoPlayer.IsLooping();
170 }
171
172 void VideoView::Play()
173 {
174   mVideoPlayer.Play();
175   mIsPlay = true;
176   mIsPause = false;
177 }
178
179 void VideoView::Pause()
180 {
181   mVideoPlayer.Pause();
182   mIsPlay = false;
183   mIsPause = true;
184 }
185
186 void VideoView::Stop()
187 {
188   mVideoPlayer.Stop();
189   mIsPlay = false;
190   mIsPause = false;
191 }
192
193 void VideoView::Forward( int millisecond )
194 {
195   int curPos = mVideoPlayer.GetPlayPosition();
196
197   int nextPos = curPos + millisecond;
198
199   mVideoPlayer.SetPlayPosition( nextPos );
200 }
201
202 void VideoView::Backward( int millisecond )
203 {
204   int curPos = mVideoPlayer.GetPlayPosition();
205
206   int nextPos = curPos - millisecond;
207   nextPos = ( nextPos < 0 )? 0: nextPos;
208
209   mVideoPlayer.SetPlayPosition( nextPos );
210 }
211
212 void VideoView::SetMute( bool mute )
213 {
214   mVideoPlayer.SetMute( mute );
215 }
216
217 bool VideoView::IsMuted()
218 {
219   return mVideoPlayer.IsMuted();
220 }
221
222 void VideoView::SetVolume( float left, float right )
223 {
224   mVideoPlayer.SetVolume( left, right );
225 }
226
227 void VideoView::GetVolume( float& left, float& right )
228 {
229   mVideoPlayer.GetVolume( left, right );
230 }
231
232 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
233 {
234   return mFinishedSignal;
235 }
236
237 void VideoView::EmitSignalFinish()
238 {
239   if ( !mFinishedSignal.Empty() )
240   {
241     Dali::Toolkit::VideoView handle( GetOwner() );
242     mFinishedSignal.Emit( handle );
243   }
244 }
245
246 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
247 {
248   bool ret = false;
249
250   Dali::BaseHandle handle( object );
251   Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
252
253   if( !videoView )
254   {
255     return ret;
256   }
257
258   VideoView& impl = GetImpl( videoView );
259
260   if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
261   {
262     impl.Play();
263     ret = true;
264   }
265   else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
266   {
267     impl.Pause();
268     ret = true;
269   }
270   else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
271   {
272     impl.Stop();
273     ret = true;
274   }
275   else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
276   {
277     int millisecond = 0;
278     if( attributes["videoForward"].Get( millisecond ) )
279     {
280       impl.Forward( millisecond );
281       ret = true;
282     }
283   }
284   else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
285   {
286     int millisecond = 0;
287     if( attributes["videoBackward"].Get( millisecond ) )
288     {
289       impl.Backward( millisecond );
290       ret = true;
291     }
292   }
293
294   return ret;
295 }
296
297 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
298 {
299   Dali::BaseHandle handle( object );
300
301   bool connected( true );
302   Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
303
304   if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
305   {
306     videoView.FinishedSignal().Connect( tracker, functor );
307   }
308   else
309   {
310     // signalName does not match any signal
311     connected = false;
312   }
313
314   return connected;
315 }
316
317 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
318 {
319   Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
320
321   if( videoView )
322   {
323     VideoView& impl = GetImpl( videoView );
324
325     switch( index )
326     {
327       case Toolkit::VideoView::Property::VIDEO:
328       {
329         std::string videoUrl;
330         if( value.Get( videoUrl ) )
331         {
332           impl.SetUrl( videoUrl );
333         }
334
335         Property::Map map;
336         if( value.Get( map ) )
337         {
338           impl.SetPropertyMap( map );
339
340           Property::Value* target = map.Find( RENDERING_TARGET );
341           std::string targetType;
342           if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
343           {
344             impl.SetWindowSurfaceTarget();
345           }
346           else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
347           {
348             impl.SetNativeImageTarget();
349           }
350         }
351         break;
352       }
353       case Toolkit::VideoView::Property::LOOPING:
354       {
355         bool looping;
356         if( value.Get( looping ) )
357         {
358           impl.SetLooping( looping );
359         }
360         break;
361       }
362       case Toolkit::VideoView::Property::MUTED:
363       {
364         bool mute;
365         if( value.Get( mute ) )
366         {
367           impl.SetMute( mute );
368         }
369         break;
370       }
371       case Toolkit::VideoView::Property::VOLUME:
372       {
373         Property::Map map;
374         float left, right;
375         if( value.Get( map ) )
376         {
377           Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
378           Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
379           if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
380           {
381             impl.SetVolume( left, right );
382           }
383         }
384         break;
385       }
386     }
387   }
388 }
389
390 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
391 {
392   Property::Value value;
393   Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
394
395   if( videoView )
396   {
397     VideoView& impl = GetImpl( videoView );
398
399     switch( propertyIndex )
400     {
401       case Toolkit::VideoView::Property::VIDEO:
402       {
403         if( !impl.mUrl.empty() )
404         {
405           value = impl.mUrl;
406         }
407         else if( !impl.mPropertyMap.Empty() )
408         {
409           value = impl.mPropertyMap;
410         }
411         break;
412       }
413       case Toolkit::VideoView::Property::LOOPING:
414       {
415         value = impl.IsLooping();
416         break;
417       }
418       case Toolkit::VideoView::Property::MUTED:
419       {
420         value = impl.IsMuted();
421         break;
422       }
423       case Toolkit::VideoView::Property::VOLUME:
424       {
425         Property::Map map;
426         float left, right;
427
428         impl.GetVolume( left, right );
429         map.Insert( VOLUME_LEFT, left );
430         map.Insert( VOLUME_RIGHT, right );
431         value = map;
432         break;
433       }
434     }
435   }
436
437   return value;
438 }
439
440 void VideoView::SetDepthIndex( int depthIndex )
441 {
442   if( mVisual )
443   {
444     mVisual.SetDepthIndex( depthIndex );
445   }
446 }
447
448 void VideoView::OnStageConnection( int depth )
449 {
450   if( mVisual )
451   {
452     CustomActor self = Self();
453     Toolkit::GetImplementation(mVisual).SetOnStage( self );
454   }
455
456   Control::OnStageConnection( depth );
457 }
458
459 void VideoView::OnStageDisconnection()
460 {
461   if( mVisual )
462   {
463     CustomActor self = Self();
464     Toolkit::GetImplementation(mVisual).SetOffStage( self );
465   }
466
467   Control::OnStageDisconnection();
468 }
469
470 Vector3 VideoView::GetNaturalSize()
471 {
472   Vector3 size;
473   size.x = mVideoSize.GetWidth();
474   size.y = mVideoSize.GetHeight();
475
476   if( size.x > 0 && size.y > 0 )
477   {
478     size.z = std::min( size.x, size.y );
479     return size;
480   }
481   else
482   {
483     return Control::GetNaturalSize();
484   }
485 }
486
487 float VideoView::GetHeightForWidth( float width )
488 {
489   if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
490   {
491     return GetHeightForWidthBase( width );
492   }
493   else
494   {
495     return Control::GetHeightForWidthBase( width );
496   }
497 }
498
499 float VideoView::GetWidthForHeight( float height )
500 {
501   if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
502   {
503     return GetWidthForHeightBase( height );
504   }
505   else
506   {
507     return Control::GetWidthForHeightBase( height );
508   }
509 }
510
511 void VideoView::SetWindowSurfaceTarget()
512 {
513   Actor self = Self();
514   int curPos = mVideoPlayer.GetPlayPosition();
515
516   if( mVisual )
517   {
518     Toolkit::GetImplementation(mVisual).SetOffStage(self);
519     mVisual.Reset();
520   }
521
522   mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle() );
523   mVideoPlayer.SetUrl( mUrl );
524
525   mIsNativeImageTarget = false;
526
527   if( mIsPlay )
528   {
529     mVideoPlayer.Play();
530   }
531   if( mIsPause )
532   {
533     mVideoPlayer.Play();
534     mVideoPlayer.Pause();
535   }
536   mVideoPlayer.SetPlayPosition( curPos );
537 }
538
539 void VideoView::SetNativeImageTarget()
540 {
541   Actor self( Self() );
542   int curPos = mVideoPlayer.GetPlayPosition();
543
544   Any source;
545   Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
546   mNativeImage = Dali::NativeImage::New( *nativeImageSourcePtr );
547
548   mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
549   mVideoPlayer.SetUrl( mUrl );
550
551   Internal::InitializeVisual( self, mVisual, mNativeImage );
552   mIsNativeImageTarget = true;
553
554   if( mIsPlay )
555   {
556     mVideoPlayer.Play();
557   }
558
559   if( mIsPause )
560   {
561     mVideoPlayer.Play();
562     mVideoPlayer.Pause();
563   }
564   mVideoPlayer.SetPlayPosition( curPos );
565 }
566
567 } // namespace Internal
568
569 } // namespace Toolkit
570
571 } // namespace Dali