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/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>
51 return Toolkit::VideoView::New();
54 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::VideoView, Toolkit::Control, Create );
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 )
61 DALI_SIGNAL_REGISTRATION( Toolkit, VideoView, "finished", FINISHED_SIGNAL )
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 )
69 DALI_TYPE_REGISTRATION_END()
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" );
77 } // anonymous namepsace
79 VideoView::VideoView()
80 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
81 mCurrentVideoPlayPosition( 0 ),
82 mIsNativeImageTarget( true ),
86 mVideoPlayer = Dali::VideoPlayer::New();
89 VideoView::~VideoView()
93 Toolkit::VideoView VideoView::New()
95 VideoView* impl = new VideoView();
96 Toolkit::VideoView handle = Toolkit::VideoView( *impl );
103 void VideoView::OnInitialize()
106 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
107 mNativeImage = Dali::NativeImage::New( *nativeImageSourcePtr );
109 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
110 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
113 void VideoView::SetUrl( const std::string& url )
115 if( mUrl != url || !mPropertyMap.Empty() )
118 mPropertyMap.Clear();
121 if( mIsNativeImageTarget )
123 Actor self( Self() );
124 Internal::InitializeVisual( self, mVisual, mNativeImage );
127 mVideoPlayer.SetUrl( mUrl );
130 void VideoView::SetPropertyMap( Property::Map map )
134 Actor self( Self() );
135 Internal::InitializeVisual( self, mVisual, mPropertyMap );
137 Property::Value* widthValue = mPropertyMap.Find( "width" );
141 if( widthValue->Get( width ) )
143 mVideoSize = ImageDimensions( width, mVideoSize.GetHeight() );
147 Property::Value* heightValue = mPropertyMap.Find( "height" );
151 if( heightValue->Get( height ) )
153 mVideoSize = ImageDimensions( mVideoSize.GetWidth(), height );
157 Property::Value* target = map.Find( RENDERING_TARGET );
158 std::string targetType;
160 if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
162 this->SetWindowSurfaceTarget();
164 else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
166 this->SetNativeImageTarget();
172 std::string VideoView::GetUrl()
177 void VideoView::SetLooping(bool looping)
179 mVideoPlayer.SetLooping( looping );
182 bool VideoView::IsLooping()
184 return mVideoPlayer.IsLooping();
187 void VideoView::Play()
194 void VideoView::Pause()
196 mVideoPlayer.Pause();
201 void VideoView::Stop()
208 void VideoView::Forward( int millisecond )
210 int curPos = mVideoPlayer.GetPlayPosition();
212 int nextPos = curPos + millisecond;
214 mVideoPlayer.SetPlayPosition( nextPos );
217 void VideoView::Backward( int millisecond )
219 int curPos = mVideoPlayer.GetPlayPosition();
221 int nextPos = curPos - millisecond;
222 nextPos = ( nextPos < 0 )? 0: nextPos;
224 mVideoPlayer.SetPlayPosition( nextPos );
227 void VideoView::SetMute( bool mute )
229 mVideoPlayer.SetMute( mute );
232 bool VideoView::IsMuted()
234 return mVideoPlayer.IsMuted();
237 void VideoView::SetVolume( float left, float right )
239 mVideoPlayer.SetVolume( left, right );
242 void VideoView::GetVolume( float& left, float& right )
244 mVideoPlayer.GetVolume( left, right );
247 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
249 return mFinishedSignal;
252 void VideoView::EmitSignalFinish()
254 if ( !mFinishedSignal.Empty() )
256 Dali::Toolkit::VideoView handle( GetOwner() );
257 mFinishedSignal.Emit( handle );
261 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
265 Dali::BaseHandle handle( object );
266 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
273 VideoView& impl = GetImpl( videoView );
275 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
280 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
285 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
290 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
293 if( attributes["videoForward"].Get( millisecond ) )
295 impl.Forward( millisecond );
299 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
302 if( attributes["videoBackward"].Get( millisecond ) )
304 impl.Backward( millisecond );
312 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
314 Dali::BaseHandle handle( object );
316 bool connected( true );
317 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
319 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
321 videoView.FinishedSignal().Connect( tracker, functor );
325 // signalName does not match any signal
332 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
334 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
338 VideoView& impl = GetImpl( videoView );
342 case Toolkit::VideoView::Property::VIDEO:
344 std::string videoUrl;
347 if( value.Get( videoUrl ) )
349 impl.SetUrl( videoUrl );
351 else if( value.Get( map ) )
353 Property::Value* shaderValue = map.Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
355 if( map.Count() > 1u || !shaderValue )
357 impl.SetPropertyMap( map );
359 else if( impl.mVisual && map.Count() == 1u && shaderValue )
361 Property::Map shaderMap;
362 if( shaderValue->Get( shaderMap ) )
364 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
365 visual.SetCustomShader( shaderMap );
366 if( videoView.OnStage() )
368 visual.SetOffStage( videoView );
369 visual.SetOnStage( videoView );
376 case Toolkit::VideoView::Property::LOOPING:
379 if( value.Get( looping ) )
381 impl.SetLooping( looping );
385 case Toolkit::VideoView::Property::MUTED:
388 if( value.Get( mute ) )
390 impl.SetMute( mute );
394 case Toolkit::VideoView::Property::VOLUME:
398 if( value.Get( map ) )
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 ) )
404 impl.SetVolume( left, right );
413 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
415 Property::Value value;
416 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
420 VideoView& impl = GetImpl( videoView );
422 switch( propertyIndex )
424 case Toolkit::VideoView::Property::VIDEO:
426 if( !impl.mUrl.empty() )
430 else if( !impl.mPropertyMap.Empty() )
432 value = impl.mPropertyMap;
436 case Toolkit::VideoView::Property::LOOPING:
438 value = impl.IsLooping();
441 case Toolkit::VideoView::Property::MUTED:
443 value = impl.IsMuted();
446 case Toolkit::VideoView::Property::VOLUME:
451 impl.GetVolume( left, right );
452 map.Insert( VOLUME_LEFT, left );
453 map.Insert( VOLUME_RIGHT, right );
463 void VideoView::SetDepthIndex( int depthIndex )
467 mVisual.SetDepthIndex( depthIndex );
471 void VideoView::OnStageConnection( int depth )
475 CustomActor self = Self();
476 Toolkit::GetImplementation(mVisual).SetOnStage( self );
479 Control::OnStageConnection( depth );
482 void VideoView::OnStageDisconnection()
486 CustomActor self = Self();
487 Toolkit::GetImplementation(mVisual).SetOffStage( self );
490 Control::OnStageDisconnection();
493 Vector3 VideoView::GetNaturalSize()
496 size.x = mVideoSize.GetWidth();
497 size.y = mVideoSize.GetHeight();
499 if( size.x > 0 && size.y > 0 )
501 size.z = std::min( size.x, size.y );
506 return Control::GetNaturalSize();
510 float VideoView::GetHeightForWidth( float width )
512 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
514 return GetHeightForWidthBase( width );
518 return Control::GetHeightForWidthBase( width );
522 float VideoView::GetWidthForHeight( float height )
524 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
526 return GetWidthForHeightBase( height );
530 return Control::GetWidthForHeightBase( height );
534 void VideoView::SetWindowSurfaceTarget()
537 int curPos = mVideoPlayer.GetPlayPosition();
541 Toolkit::GetImplementation(mVisual).SetOffStage(self);
545 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle() );
546 mVideoPlayer.SetUrl( mUrl );
548 mIsNativeImageTarget = false;
557 mVideoPlayer.Pause();
559 mVideoPlayer.SetPlayPosition( curPos );
562 void VideoView::SetNativeImageTarget()
564 Actor self( Self() );
565 int curPos = mVideoPlayer.GetPlayPosition();
568 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
569 mNativeImage = Dali::NativeImage::New( *nativeImageSourcePtr );
571 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
572 mVideoPlayer.SetUrl( mUrl );
574 Internal::InitializeVisual( self, mVisual, mNativeImage );
575 mIsNativeImageTarget = true;
585 mVideoPlayer.Pause();
587 mVideoPlayer.SetPlayPosition( curPos );
590 } // namespace Internal
592 } // namespace Toolkit