2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "video-view-impl.h"
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>
31 #include <dali-toolkit/public-api/controls/video-view/video-view.h>
32 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
48 return Toolkit::VideoView::New();
51 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::VideoView, Toolkit::Control, Create );
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 )
58 DALI_SIGNAL_REGISTRATION( Toolkit, VideoView, "finished", FINISHED_SIGNAL )
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 )
66 DALI_TYPE_REGISTRATION_END()
68 const char* const VOLUME_LEFT( "volumeLeft" );
69 const char* const VOLUME_RIGHT( "volumeRight" );
70 const char* const RENDERING_TARGET( "RENDERING_TARGET" );
71 const char* const WINDOW_SURFACE_TARGET( "windowSurfaceTarget" );
72 const char* const NATIVE_IMAGE_TARGET( "nativeImageTarget" );
74 } // anonymous namepsace
76 VideoView::VideoView()
77 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
78 mCurrentVideoPlayPosition( 0 ),
79 mSetRenderingTarget( false ),
83 mVideoPlayer = Dali::VideoPlayer::New();
86 VideoView::~VideoView()
90 Toolkit::VideoView VideoView::New()
92 VideoView* impl = new VideoView();
93 Toolkit::VideoView handle = Toolkit::VideoView( *impl );
100 void VideoView::SetUrl( const std::string& url )
102 if( mUrl != url || !mPropertyMap.Empty() )
104 mPropertyMap.Clear();
109 if( mSetRenderingTarget )
111 mVideoPlayer.SetUrl( mUrl );
115 SetNativeImageTarget();
119 void VideoView::SetPropertyMap( Property::Map map )
123 Actor self( Self() );
124 Internal::InitializeVisual( self, mVisual, mPropertyMap );
126 Property::Value* widthValue = mPropertyMap.Find( "width" );
130 if( widthValue->Get( width ) )
132 mVideoSize = ImageDimensions( width, mVideoSize.GetHeight() );
136 Property::Value* heightValue = mPropertyMap.Find( "height" );
140 if( heightValue->Get( height ) )
142 mVideoSize = ImageDimensions( mVideoSize.GetWidth(), height );
149 std::string VideoView::GetUrl()
154 void VideoView::SetLooping(bool looping)
156 mVideoPlayer.SetLooping( looping );
159 bool VideoView::IsLooping()
161 return mVideoPlayer.IsLooping();
164 void VideoView::Play()
171 void VideoView::Pause()
173 mVideoPlayer.Pause();
178 void VideoView::Stop()
185 void VideoView::Forward( int millisecond )
187 int curPos = mVideoPlayer.GetPlayPosition();
189 int nextPos = curPos + millisecond;
191 mVideoPlayer.SetPlayPosition( nextPos );
194 void VideoView::Backward( int millisecond )
196 int curPos = mVideoPlayer.GetPlayPosition();
198 int nextPos = curPos - millisecond;
199 nextPos = ( nextPos < 0 )? 0: nextPos;
201 mVideoPlayer.SetPlayPosition( nextPos );
204 void VideoView::SetMute( bool mute )
206 mVideoPlayer.SetMute( mute );
209 bool VideoView::IsMuted()
211 return mVideoPlayer.IsMuted();
214 void VideoView::SetVolume( float left, float right )
216 mVideoPlayer.SetVolume( left, right );
219 void VideoView::GetVolume( float& left, float& right )
221 mVideoPlayer.GetVolume( left, right );
224 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
226 return mFinishedSignal;
229 void VideoView::EmitSignalFinish()
231 if ( !mFinishedSignal.Empty() )
233 Dali::Toolkit::VideoView handle( GetOwner() );
234 mFinishedSignal.Emit( handle );
238 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
242 Dali::BaseHandle handle( object );
243 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
250 VideoView& impl = GetImpl( videoView );
252 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
257 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
262 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
267 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
270 if( attributes["videoForward"].Get( millisecond ) )
272 impl.Forward( millisecond );
276 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
279 if( attributes["videoBackward"].Get( millisecond ) )
281 impl.Backward( millisecond );
289 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
291 Dali::BaseHandle handle( object );
293 bool connected( true );
294 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
296 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
298 videoView.FinishedSignal().Connect( tracker, functor );
302 // signalName does not match any signal
309 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
311 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
315 VideoView& impl = GetImpl( videoView );
319 case Toolkit::VideoView::Property::VIDEO:
321 std::string videoUrl;
322 if( value.Get( videoUrl ) )
324 impl.SetUrl( videoUrl );
328 if( value.Get( map ) )
330 impl.SetPropertyMap( map );
332 Property::Value* target = map.Find( RENDERING_TARGET );
333 std::string targetType;
334 if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
336 impl.SetWindowSurfaceTarget();
338 else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
340 impl.SetNativeImageTarget();
345 case Toolkit::VideoView::Property::LOOPING:
348 if( value.Get( looping ) )
350 impl.SetLooping( looping );
354 case Toolkit::VideoView::Property::MUTED:
357 if( value.Get( mute ) )
359 impl.SetMute( mute );
363 case Toolkit::VideoView::Property::VOLUME:
367 if( value.Get( map ) )
369 Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
370 Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
371 if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
373 impl.SetVolume( left, right );
382 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
384 Property::Value value;
385 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
389 VideoView& impl = GetImpl( videoView );
391 switch( propertyIndex )
393 case Toolkit::VideoView::Property::VIDEO:
395 if( !impl.mUrl.empty() )
399 else if( !impl.mPropertyMap.Empty() )
401 value = impl.mPropertyMap;
405 case Toolkit::VideoView::Property::LOOPING:
407 value = impl.IsLooping();
410 case Toolkit::VideoView::Property::MUTED:
412 value = impl.IsMuted();
415 case Toolkit::VideoView::Property::VOLUME:
420 impl.GetVolume( left, right );
421 map.Insert( VOLUME_LEFT, left );
422 map.Insert( VOLUME_RIGHT, right );
432 void VideoView::SetDepthIndex( int depthIndex )
436 mVisual.SetDepthIndex( depthIndex );
440 void VideoView::OnStageConnection( int depth )
444 CustomActor self = Self();
445 Toolkit::GetImplementation(mVisual).SetOnStage( self );
448 Control::OnStageConnection( depth );
451 void VideoView::OnStageDisconnection()
455 CustomActor self = Self();
456 Toolkit::GetImplementation(mVisual).SetOffStage( self );
459 Control::OnStageDisconnection();
462 Vector3 VideoView::GetNaturalSize()
465 size.x = mVideoSize.GetWidth();
466 size.y = mVideoSize.GetHeight();
468 if( size.x > 0 && size.y > 0 )
470 size.z = std::min( size.x, size.y );
475 return Control::GetNaturalSize();
479 float VideoView::GetHeightForWidth( float width )
481 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
483 return GetHeightForWidthBase( width );
487 return Control::GetHeightForWidthBase( width );
491 float VideoView::GetWidthForHeight( float height )
493 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
495 return GetWidthForHeightBase( height );
499 return Control::GetWidthForHeightBase( height );
503 void VideoView::SetWindowSurfaceTarget()
506 int curPos = mVideoPlayer.GetPlayPosition();
508 mSetRenderingTarget = true;
512 Toolkit::GetImplementation(mVisual).SetOffStage(self);
516 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle() );
517 mVideoPlayer.SetUrl( mUrl );
518 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
527 mVideoPlayer.Pause();
529 mVideoPlayer.SetPlayPosition( curPos );
532 void VideoView::SetNativeImageTarget()
534 Actor self( Self() );
535 int curPos = mVideoPlayer.GetPlayPosition();
537 mSetRenderingTarget = true;
540 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
541 mNativeImage = Dali::NativeImage::New( *nativeImageSourcePtr );
543 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
544 mVideoPlayer.SetUrl( mUrl );
545 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
547 Internal::InitializeVisual( self, mVisual, mNativeImage );
557 mVideoPlayer.Pause();
559 mVideoPlayer.SetPlayPosition( curPos );
562 } // namespace Internal
564 } // namespace Toolkit