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>
29 #include <dali/integration-api/debug.h>
30 #include <dali/public-api/animation/constraint.h>
31 #include <dali/devel-api/actors/actor-devel.h>
34 #include <dali-toolkit/public-api/controls/video-view/video-view.h>
35 #include <dali-toolkit/public-api/visuals/visual-properties.h>
36 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
37 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
38 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
39 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
40 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
56 return Toolkit::VideoView::New();
59 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::VideoView, Toolkit::Control, Create );
61 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "video", MAP, VIDEO )
62 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "looping", BOOLEAN, LOOPING )
63 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "muted", BOOLEAN, MUTED )
64 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "volume", MAP, VOLUME )
65 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "underlay", BOOLEAN, UNDERLAY )
66 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "playPosition", INTEGER, PLAY_POSITION )
67 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "displayMode", INTEGER, DISPLAY_MODE )
69 DALI_SIGNAL_REGISTRATION( Toolkit, VideoView, "finished", FINISHED_SIGNAL )
71 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "play", ACTION_VIDEOVIEW_PLAY )
72 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "pause", ACTION_VIDEOVIEW_PAUSE )
73 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "stop", ACTION_VIDEOVIEW_STOP )
74 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "forward", ACTION_VIDEOVIEW_FORWARD )
75 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "backward", ACTION_VIDEOVIEW_BACKWARD )
77 DALI_TYPE_REGISTRATION_END()
79 const char* const VOLUME_LEFT( "volumeLeft" );
80 const char* const VOLUME_RIGHT( "volumeRight" );
82 // 3.0 TC uses RENDERING_TARGET. It should be removed in next release
83 const char* const RENDERING_TARGET( "renderingTarget" );
84 const char* const WINDOW_SURFACE_TARGET( "windowSurfaceTarget" );
85 const char* const NATIVE_IMAGE_TARGET( "nativeImageTarget" );
87 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
88 attribute mediump vec2 aPosition;\n
89 uniform mediump mat4 uMvpMatrix;\n
90 uniform mediump vec3 uSize;\n
94 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
95 vertexPosition.xyz *= uSize;\n
96 gl_Position = uMvpMatrix * vertexPosition;\n
100 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
101 uniform lowp vec4 uColor;\n
102 uniform lowp vec3 mixColor;\n
103 uniform lowp float opacity;\n
107 gl_FragColor = vec4(mixColor, opacity)*uColor;\n
111 } // anonymous namepsace
113 VideoView::VideoView()
114 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
115 mCurrentVideoPlayPosition( 0 ),
119 mVideoPlayer = Dali::VideoPlayer::New();
122 VideoView::~VideoView()
126 Toolkit::VideoView VideoView::New()
128 VideoView* impl = new VideoView();
129 Toolkit::VideoView handle = Toolkit::VideoView( *impl );
136 void VideoView::OnInitialize()
138 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
139 SetWindowSurfaceTarget();
142 void VideoView::SetUrl( const std::string& url )
144 if( mUrl != url || !mPropertyMap.Empty() )
147 mPropertyMap.Clear();
152 Actor self( Self() );
153 Internal::InitializeVisual( self, mVisual, mNativeImage );
156 mVideoPlayer.SetUrl( mUrl );
159 void VideoView::SetPropertyMap( Property::Map map )
163 Actor self( Self() );
164 Internal::InitializeVisual( self, mVisual, mPropertyMap );
166 Property::Value* widthValue = mPropertyMap.Find( "width" );
170 if( widthValue->Get( width ) )
172 mVideoSize = ImageDimensions( width, mVideoSize.GetHeight() );
176 Property::Value* heightValue = mPropertyMap.Find( "height" );
180 if( heightValue->Get( height ) )
182 mVideoSize = ImageDimensions( mVideoSize.GetWidth(), height );
186 Property::Value* target = map.Find( RENDERING_TARGET );
187 std::string targetType;
189 if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
192 SetWindowSurfaceTarget();
194 else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
197 SetNativeImageTarget();
203 std::string VideoView::GetUrl()
208 void VideoView::SetLooping(bool looping)
210 mVideoPlayer.SetLooping( looping );
213 bool VideoView::IsLooping()
215 return mVideoPlayer.IsLooping();
218 void VideoView::Play()
222 Self().AddRenderer( mRenderer );
229 void VideoView::Pause()
231 mVideoPlayer.Pause();
235 void VideoView::Stop()
241 void VideoView::Forward( int millisecond )
243 int curPos = mVideoPlayer.GetPlayPosition();
245 int nextPos = curPos + millisecond;
247 mVideoPlayer.SetPlayPosition( nextPos );
250 void VideoView::Backward( int millisecond )
252 int curPos = mVideoPlayer.GetPlayPosition();
254 int nextPos = curPos - millisecond;
255 nextPos = ( nextPos < 0 )? 0: nextPos;
257 mVideoPlayer.SetPlayPosition( nextPos );
260 void VideoView::SetMute( bool mute )
262 mVideoPlayer.SetMute( mute );
265 bool VideoView::IsMuted()
267 return mVideoPlayer.IsMuted();
270 void VideoView::SetVolume( float left, float right )
272 mVideoPlayer.SetVolume( left, right );
275 void VideoView::GetVolume( float& left, float& right )
277 mVideoPlayer.GetVolume( left, right );
280 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
282 return mFinishedSignal;
285 void VideoView::EmitSignalFinish()
289 Self().RemoveRenderer( mRenderer );
292 if ( !mFinishedSignal.Empty() )
294 Dali::Toolkit::VideoView handle( GetOwner() );
295 mFinishedSignal.Emit( handle );
299 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
303 Dali::BaseHandle handle( object );
304 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
311 VideoView& impl = GetImpl( videoView );
313 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
318 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
323 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
328 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
331 if( attributes["videoForward"].Get( millisecond ) )
333 impl.Forward( millisecond );
337 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
340 if( attributes["videoBackward"].Get( millisecond ) )
342 impl.Backward( millisecond );
350 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
352 Dali::BaseHandle handle( object );
354 bool connected( true );
355 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
357 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
359 videoView.FinishedSignal().Connect( tracker, functor );
363 // signalName does not match any signal
370 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
372 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
376 VideoView& impl = GetImpl( videoView );
380 case Toolkit::VideoView::Property::VIDEO:
382 std::string videoUrl;
385 if( value.Get( videoUrl ) )
387 impl.SetUrl( videoUrl );
389 else if( value.Get( map ) )
391 Property::Value* shaderValue = map.Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
393 if( map.Count() > 1u || !shaderValue )
395 impl.SetPropertyMap( map );
397 else if( impl.mVisual && map.Count() == 1u && shaderValue )
399 Property::Map shaderMap;
400 if( shaderValue->Get( shaderMap ) )
402 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
403 visual.SetCustomShader( shaderMap );
404 if( videoView.OnStage() )
406 visual.SetOffStage( videoView );
407 visual.SetOnStage( videoView );
414 case Toolkit::VideoView::Property::LOOPING:
417 if( value.Get( looping ) )
419 impl.SetLooping( looping );
423 case Toolkit::VideoView::Property::MUTED:
426 if( value.Get( mute ) )
428 impl.SetMute( mute );
432 case Toolkit::VideoView::Property::VOLUME:
436 if( value.Get( map ) )
438 Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
439 Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
440 if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
442 impl.SetVolume( left, right );
447 case Toolkit::VideoView::Property::UNDERLAY:
450 if( value.Get( underlay ) )
452 impl.SetUnderlay( underlay );
456 case Toolkit::VideoView::Property::PLAY_POSITION:
459 if( value.Get( pos ) )
461 impl.SetPlayPosition( pos );
465 case Toolkit::VideoView::Property::DISPLAY_MODE:
468 if( value.Get( mode ) )
470 impl.SetDisplayMode( mode );
478 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
480 Property::Value value;
481 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
485 VideoView& impl = GetImpl( videoView );
487 switch( propertyIndex )
489 case Toolkit::VideoView::Property::VIDEO:
491 if( !impl.mUrl.empty() )
495 else if( !impl.mPropertyMap.Empty() )
497 value = impl.mPropertyMap;
501 case Toolkit::VideoView::Property::LOOPING:
503 value = impl.IsLooping();
506 case Toolkit::VideoView::Property::MUTED:
508 value = impl.IsMuted();
511 case Toolkit::VideoView::Property::VOLUME:
516 impl.GetVolume( left, right );
517 map.Insert( VOLUME_LEFT, left );
518 map.Insert( VOLUME_RIGHT, right );
522 case Toolkit::VideoView::Property::UNDERLAY:
524 value = impl.IsUnderlay();
527 case Toolkit::VideoView::Property::PLAY_POSITION:
529 value = impl.GetPlayPosition();
532 case Toolkit::VideoView::Property::DISPLAY_MODE:
534 value = impl.GetDisplayMode();
543 void VideoView::SetDepthIndex( int depthIndex )
547 mVisual.SetDepthIndex( depthIndex );
551 void VideoView::OnStageConnection( int depth )
555 CustomActor self = Self();
556 Toolkit::GetImplementation(mVisual).SetOnStage( self );
559 Control::OnStageConnection( depth );
562 void VideoView::OnStageDisconnection()
566 CustomActor self = Self();
567 Toolkit::GetImplementation(mVisual).SetOffStage( self );
570 Control::OnStageDisconnection();
573 Vector3 VideoView::GetNaturalSize()
576 size.x = mVideoSize.GetWidth();
577 size.y = mVideoSize.GetHeight();
579 if( size.x > 0 && size.y > 0 )
581 size.z = std::min( size.x, size.y );
586 return Control::GetNaturalSize();
590 float VideoView::GetHeightForWidth( float width )
592 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
594 return GetHeightForWidthBase( width );
598 return Control::GetHeightForWidthBase( width );
602 float VideoView::GetWidthForHeight( float height )
604 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
606 return GetWidthForHeightBase( height );
610 return Control::GetWidthForHeightBase( height );
614 void VideoView::SetWindowSurfaceTarget()
617 int curPos = mVideoPlayer.GetPlayPosition();
621 Toolkit::GetImplementation(mVisual).SetOffStage(self);
627 mVideoPlayer.Pause();
630 mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
631 mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
632 mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
633 mPositionUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
634 mSizeUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
635 mScaleUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
637 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle() );
638 mVideoPlayer.SetUrl( mUrl );
642 // For underlay rendering mode, video display area have to be transparent.
643 Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
644 Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
645 mRenderer = Renderer::New( geometry, shader );
647 mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
648 mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_RGB, BlendFactor::ONE );
649 mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_RGB, BlendFactor::ZERO );
650 mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_ALPHA, BlendFactor::ONE );
651 mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_ALPHA, BlendFactor::ZERO );
661 mVideoPlayer.SetPlayPosition( curPos );
665 void VideoView::SetNativeImageTarget()
667 if( mVideoPlayer.IsVideoTextureSupported() == false )
669 DALI_LOG_ERROR( "Platform doesn't support decoded video frame images\n" );
676 mVideoPlayer.Pause();
679 Actor self( Self() );
680 self.RemoveRenderer( mRenderer );
681 Dali::Stage::GetCurrent().KeepRendering( 0.0f );
683 self.RemovePropertyNotification( mPositionUpdateNotification );
684 self.RemovePropertyNotification( mSizeUpdateNotification );
685 self.RemovePropertyNotification( mScaleUpdateNotification );
687 int curPos = mVideoPlayer.GetPlayPosition();
690 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
691 mNativeImage = Dali::NativeImage::New( *nativeImageSourcePtr );
693 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
694 mVideoPlayer.SetUrl( mUrl );
696 Internal::InitializeVisual( self, mVisual, mNativeImage );
697 Self().RemoveRenderer( mRenderer );
706 mVideoPlayer.SetPlayPosition( curPos );
710 void VideoView::UpdateDisplayArea( Dali::PropertyNotification& source )
717 Actor self( Self() );
719 bool positionUsesAnchorPoint = self.GetProperty( DevelActor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >();
720 Vector3 actorSize = self.GetCurrentSize() * self.GetCurrentScale();
721 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentAnchorPoint() : AnchorPoint::TOP_LEFT );
723 Vector2 screenPosition = self.GetProperty( DevelActor::Property::SCREEN_POSITION ).Get< Vector2 >();
725 mDisplayArea.x = screenPosition.x - anchorPointOffSet.x;
726 mDisplayArea.y = screenPosition.y - anchorPointOffSet.y;
727 mDisplayArea.width = actorSize.x;
728 mDisplayArea.height = actorSize.y;
730 mVideoPlayer.SetDisplayArea( mDisplayArea );
733 void VideoView::SetUnderlay( bool set )
735 if( set != mIsUnderlay )
741 SetWindowSurfaceTarget();
745 SetNativeImageTarget();
752 bool VideoView::IsUnderlay()
757 void VideoView::SetSWCodec( bool on )
759 // If setting SW or HW type is failed , video-view shows video by default codec type.
760 // The default codec type is selected by platform.
763 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::SW );
767 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::HW );
771 int VideoView::GetPlayPosition()
773 return mVideoPlayer.GetPlayPosition();
776 void VideoView::SetPlayPosition( int pos )
778 mVideoPlayer.SetPlayPosition( pos );
781 void VideoView::SetDisplayMode( int mode )
783 mVideoPlayer.SetDisplayMode( static_cast< Dali::VideoPlayerPlugin::DisplayMode::Type >( mode ) );
786 int VideoView::GetDisplayMode() const
788 return static_cast< int >( mVideoPlayer.GetDisplayMode() );
791 } // namespace Internal
793 } // namespace toolkit