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