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( "renderingTarget" );
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 mIsNativeImageTarget( true ),
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::OnInitialize()
103 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
104 mNativeImage = Dali::NativeImage::New( *nativeImageSourcePtr );
106 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
107 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
110 void VideoView::SetUrl( const std::string& url )
112 if( mUrl != url || !mPropertyMap.Empty() )
115 mPropertyMap.Clear();
118 if( mIsNativeImageTarget )
120 Actor self( Self() );
121 Internal::InitializeVisual( self, mVisual, mNativeImage );
124 mVideoPlayer.SetUrl( mUrl );
127 void VideoView::SetPropertyMap( Property::Map map )
131 Actor self( Self() );
132 Internal::InitializeVisual( self, mVisual, mPropertyMap );
134 Property::Value* widthValue = mPropertyMap.Find( "width" );
138 if( widthValue->Get( width ) )
140 mVideoSize = ImageDimensions( width, mVideoSize.GetHeight() );
144 Property::Value* heightValue = mPropertyMap.Find( "height" );
148 if( heightValue->Get( height ) )
150 mVideoSize = ImageDimensions( mVideoSize.GetWidth(), height );
157 std::string VideoView::GetUrl()
162 void VideoView::SetLooping(bool looping)
164 mVideoPlayer.SetLooping( looping );
167 bool VideoView::IsLooping()
169 return mVideoPlayer.IsLooping();
172 void VideoView::Play()
179 void VideoView::Pause()
181 mVideoPlayer.Pause();
186 void VideoView::Stop()
193 void VideoView::Forward( int millisecond )
195 int curPos = mVideoPlayer.GetPlayPosition();
197 int nextPos = curPos + millisecond;
199 mVideoPlayer.SetPlayPosition( nextPos );
202 void VideoView::Backward( int millisecond )
204 int curPos = mVideoPlayer.GetPlayPosition();
206 int nextPos = curPos - millisecond;
207 nextPos = ( nextPos < 0 )? 0: nextPos;
209 mVideoPlayer.SetPlayPosition( nextPos );
212 void VideoView::SetMute( bool mute )
214 mVideoPlayer.SetMute( mute );
217 bool VideoView::IsMuted()
219 return mVideoPlayer.IsMuted();
222 void VideoView::SetVolume( float left, float right )
224 mVideoPlayer.SetVolume( left, right );
227 void VideoView::GetVolume( float& left, float& right )
229 mVideoPlayer.GetVolume( left, right );
232 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
234 return mFinishedSignal;
237 void VideoView::EmitSignalFinish()
239 if ( !mFinishedSignal.Empty() )
241 Dali::Toolkit::VideoView handle( GetOwner() );
242 mFinishedSignal.Emit( handle );
246 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
250 Dali::BaseHandle handle( object );
251 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
258 VideoView& impl = GetImpl( videoView );
260 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
265 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
270 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
275 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
278 if( attributes["videoForward"].Get( millisecond ) )
280 impl.Forward( millisecond );
284 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
287 if( attributes["videoBackward"].Get( millisecond ) )
289 impl.Backward( millisecond );
297 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
299 Dali::BaseHandle handle( object );
301 bool connected( true );
302 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
304 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
306 videoView.FinishedSignal().Connect( tracker, functor );
310 // signalName does not match any signal
317 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
319 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
323 VideoView& impl = GetImpl( videoView );
327 case Toolkit::VideoView::Property::VIDEO:
329 std::string videoUrl;
330 if( value.Get( videoUrl ) )
332 impl.SetUrl( videoUrl );
336 if( value.Get( map ) )
338 impl.SetPropertyMap( map );
340 Property::Value* target = map.Find( RENDERING_TARGET );
341 std::string targetType;
342 if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
344 impl.SetWindowSurfaceTarget();
346 else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
348 impl.SetNativeImageTarget();
353 case Toolkit::VideoView::Property::LOOPING:
356 if( value.Get( looping ) )
358 impl.SetLooping( looping );
362 case Toolkit::VideoView::Property::MUTED:
365 if( value.Get( mute ) )
367 impl.SetMute( mute );
371 case Toolkit::VideoView::Property::VOLUME:
375 if( value.Get( map ) )
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 ) )
381 impl.SetVolume( left, right );
390 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
392 Property::Value value;
393 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
397 VideoView& impl = GetImpl( videoView );
399 switch( propertyIndex )
401 case Toolkit::VideoView::Property::VIDEO:
403 if( !impl.mUrl.empty() )
407 else if( !impl.mPropertyMap.Empty() )
409 value = impl.mPropertyMap;
413 case Toolkit::VideoView::Property::LOOPING:
415 value = impl.IsLooping();
418 case Toolkit::VideoView::Property::MUTED:
420 value = impl.IsMuted();
423 case Toolkit::VideoView::Property::VOLUME:
428 impl.GetVolume( left, right );
429 map.Insert( VOLUME_LEFT, left );
430 map.Insert( VOLUME_RIGHT, right );
440 void VideoView::SetDepthIndex( int depthIndex )
444 mVisual.SetDepthIndex( depthIndex );
448 void VideoView::OnStageConnection( int depth )
452 CustomActor self = Self();
453 Toolkit::GetImplementation(mVisual).SetOnStage( self );
456 Control::OnStageConnection( depth );
459 void VideoView::OnStageDisconnection()
463 CustomActor self = Self();
464 Toolkit::GetImplementation(mVisual).SetOffStage( self );
467 Control::OnStageDisconnection();
470 Vector3 VideoView::GetNaturalSize()
473 size.x = mVideoSize.GetWidth();
474 size.y = mVideoSize.GetHeight();
476 if( size.x > 0 && size.y > 0 )
478 size.z = std::min( size.x, size.y );
483 return Control::GetNaturalSize();
487 float VideoView::GetHeightForWidth( float width )
489 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
491 return GetHeightForWidthBase( width );
495 return Control::GetHeightForWidthBase( width );
499 float VideoView::GetWidthForHeight( float height )
501 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
503 return GetWidthForHeightBase( height );
507 return Control::GetWidthForHeightBase( height );
511 void VideoView::SetWindowSurfaceTarget()
514 int curPos = mVideoPlayer.GetPlayPosition();
518 Toolkit::GetImplementation(mVisual).SetOffStage(self);
522 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle() );
523 mVideoPlayer.SetUrl( mUrl );
525 mIsNativeImageTarget = false;
534 mVideoPlayer.Pause();
536 mVideoPlayer.SetPlayPosition( curPos );
539 void VideoView::SetNativeImageTarget()
541 Actor self( Self() );
542 int curPos = mVideoPlayer.GetPlayPosition();
545 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
546 mNativeImage = Dali::NativeImage::New( *nativeImageSourcePtr );
548 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
549 mVideoPlayer.SetUrl( mUrl );
551 Internal::InitializeVisual( self, mVisual, mNativeImage );
552 mIsNativeImageTarget = true;
562 mVideoPlayer.Pause();
564 mVideoPlayer.SetPlayPosition( curPos );
567 } // namespace Internal
569 } // namespace Toolkit