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 )
68 DALI_SIGNAL_REGISTRATION( Toolkit, VideoView, "finished", FINISHED_SIGNAL )
70 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "play", ACTION_VIDEOVIEW_PLAY )
71 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "pause", ACTION_VIDEOVIEW_PAUSE )
72 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "stop", ACTION_VIDEOVIEW_STOP )
73 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "forward", ACTION_VIDEOVIEW_FORWARD )
74 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "backward", ACTION_VIDEOVIEW_BACKWARD )
76 DALI_TYPE_REGISTRATION_END()
78 const char* const VOLUME_LEFT( "volumeLeft" );
79 const char* const VOLUME_RIGHT( "volumeRight" );
81 // 3.0 TC uses RENDERING_TARGET. It should be removed in next release
82 const char* const RENDERING_TARGET( "renderingTarget" );
83 const char* const WINDOW_SURFACE_TARGET( "windowSurfaceTarget" );
84 const char* const NATIVE_IMAGE_TARGET( "nativeImageTarget" );
86 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
87 attribute mediump vec2 aPosition;\n
88 uniform mediump mat4 uMvpMatrix;\n
89 uniform mediump vec3 uSize;\n
93 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
94 vertexPosition.xyz *= uSize;\n
95 gl_Position = uMvpMatrix * vertexPosition;\n
99 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
100 uniform lowp vec4 uColor;\n
101 uniform lowp vec3 mixColor;\n
102 uniform lowp float opacity;\n
106 gl_FragColor = vec4(mixColor, opacity)*uColor;\n
110 } // anonymous namepsace
112 VideoView::VideoView()
113 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
114 mCurrentVideoPlayPosition( 0 ),
118 mVideoPlayer = Dali::VideoPlayer::New();
121 VideoView::~VideoView()
125 Toolkit::VideoView VideoView::New()
127 VideoView* impl = new VideoView();
128 Toolkit::VideoView handle = Toolkit::VideoView( *impl );
135 void VideoView::OnInitialize()
137 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
138 SetWindowSurfaceTarget();
141 void VideoView::SetUrl( const std::string& url )
143 if( mUrl != url || !mPropertyMap.Empty() )
146 mPropertyMap.Clear();
151 Actor self( Self() );
152 Internal::InitializeVisual( self, mVisual, mNativeImage );
155 mVideoPlayer.SetUrl( mUrl );
158 void VideoView::SetPropertyMap( Property::Map map )
162 Actor self( Self() );
163 Internal::InitializeVisual( self, mVisual, mPropertyMap );
165 Property::Value* widthValue = mPropertyMap.Find( "width" );
169 if( widthValue->Get( width ) )
171 mVideoSize = ImageDimensions( width, mVideoSize.GetHeight() );
175 Property::Value* heightValue = mPropertyMap.Find( "height" );
179 if( heightValue->Get( height ) )
181 mVideoSize = ImageDimensions( mVideoSize.GetWidth(), height );
185 Property::Value* target = map.Find( RENDERING_TARGET );
186 std::string targetType;
188 if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
191 SetWindowSurfaceTarget();
193 else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
196 SetNativeImageTarget();
202 std::string VideoView::GetUrl()
207 void VideoView::SetLooping(bool looping)
209 mVideoPlayer.SetLooping( looping );
212 bool VideoView::IsLooping()
214 return mVideoPlayer.IsLooping();
217 void VideoView::Play()
221 Self().AddRenderer( mRenderer );
228 void VideoView::Pause()
230 mVideoPlayer.Pause();
234 void VideoView::Stop()
240 void VideoView::Forward( int millisecond )
242 int curPos = mVideoPlayer.GetPlayPosition();
244 int nextPos = curPos + millisecond;
246 mVideoPlayer.SetPlayPosition( nextPos );
249 void VideoView::Backward( int millisecond )
251 int curPos = mVideoPlayer.GetPlayPosition();
253 int nextPos = curPos - millisecond;
254 nextPos = ( nextPos < 0 )? 0: nextPos;
256 mVideoPlayer.SetPlayPosition( nextPos );
259 void VideoView::SetMute( bool mute )
261 mVideoPlayer.SetMute( mute );
264 bool VideoView::IsMuted()
266 return mVideoPlayer.IsMuted();
269 void VideoView::SetVolume( float left, float right )
271 mVideoPlayer.SetVolume( left, right );
274 void VideoView::GetVolume( float& left, float& right )
276 mVideoPlayer.GetVolume( left, right );
279 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
281 return mFinishedSignal;
284 void VideoView::EmitSignalFinish()
288 Self().RemoveRenderer( mRenderer );
291 if ( !mFinishedSignal.Empty() )
293 Dali::Toolkit::VideoView handle( GetOwner() );
294 mFinishedSignal.Emit( handle );
298 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
302 Dali::BaseHandle handle( object );
303 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
310 VideoView& impl = GetImpl( videoView );
312 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
317 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
322 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
327 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
330 if( attributes["videoForward"].Get( millisecond ) )
332 impl.Forward( millisecond );
336 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
339 if( attributes["videoBackward"].Get( millisecond ) )
341 impl.Backward( millisecond );
349 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
351 Dali::BaseHandle handle( object );
353 bool connected( true );
354 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
356 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
358 videoView.FinishedSignal().Connect( tracker, functor );
362 // signalName does not match any signal
369 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
371 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
375 VideoView& impl = GetImpl( videoView );
379 case Toolkit::VideoView::Property::VIDEO:
381 std::string videoUrl;
384 if( value.Get( videoUrl ) )
386 impl.SetUrl( videoUrl );
388 else if( value.Get( map ) )
390 Property::Value* shaderValue = map.Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
392 if( map.Count() > 1u || !shaderValue )
394 impl.SetPropertyMap( map );
396 else if( impl.mVisual && map.Count() == 1u && shaderValue )
398 Property::Map shaderMap;
399 if( shaderValue->Get( shaderMap ) )
401 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
402 visual.SetCustomShader( shaderMap );
403 if( videoView.OnStage() )
405 visual.SetOffStage( videoView );
406 visual.SetOnStage( videoView );
413 case Toolkit::VideoView::Property::LOOPING:
416 if( value.Get( looping ) )
418 impl.SetLooping( looping );
422 case Toolkit::VideoView::Property::MUTED:
425 if( value.Get( mute ) )
427 impl.SetMute( mute );
431 case Toolkit::VideoView::Property::VOLUME:
435 if( value.Get( map ) )
437 Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
438 Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
439 if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
441 impl.SetVolume( left, right );
446 case Toolkit::VideoView::Property::UNDERLAY:
449 if( value.Get( underlay ) )
451 impl.SetUnderlay( underlay );
455 case Toolkit::VideoView::Property::PLAY_POSITION:
458 if( value.Get( pos ) )
460 impl.SetPlayPosition( pos );
468 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
470 Property::Value value;
471 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
475 VideoView& impl = GetImpl( videoView );
477 switch( propertyIndex )
479 case Toolkit::VideoView::Property::VIDEO:
481 if( !impl.mUrl.empty() )
485 else if( !impl.mPropertyMap.Empty() )
487 value = impl.mPropertyMap;
491 case Toolkit::VideoView::Property::LOOPING:
493 value = impl.IsLooping();
496 case Toolkit::VideoView::Property::MUTED:
498 value = impl.IsMuted();
501 case Toolkit::VideoView::Property::VOLUME:
506 impl.GetVolume( left, right );
507 map.Insert( VOLUME_LEFT, left );
508 map.Insert( VOLUME_RIGHT, right );
512 case Toolkit::VideoView::Property::UNDERLAY:
514 value = impl.IsUnderlay();
517 case Toolkit::VideoView::Property::PLAY_POSITION:
519 value = impl.GetPlayPosition();
528 void VideoView::SetDepthIndex( int depthIndex )
532 mVisual.SetDepthIndex( depthIndex );
536 void VideoView::OnStageConnection( int depth )
540 CustomActor self = Self();
541 Toolkit::GetImplementation(mVisual).SetOnStage( self );
544 Control::OnStageConnection( depth );
547 void VideoView::OnStageDisconnection()
551 CustomActor self = Self();
552 Toolkit::GetImplementation(mVisual).SetOffStage( self );
555 Control::OnStageDisconnection();
558 Vector3 VideoView::GetNaturalSize()
561 size.x = mVideoSize.GetWidth();
562 size.y = mVideoSize.GetHeight();
564 if( size.x > 0 && size.y > 0 )
566 size.z = std::min( size.x, size.y );
571 return Control::GetNaturalSize();
575 float VideoView::GetHeightForWidth( float width )
577 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
579 return GetHeightForWidthBase( width );
583 return Control::GetHeightForWidthBase( width );
587 float VideoView::GetWidthForHeight( float height )
589 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
591 return GetWidthForHeightBase( height );
595 return Control::GetWidthForHeightBase( height );
599 void VideoView::SetWindowSurfaceTarget()
602 int curPos = mVideoPlayer.GetPlayPosition();
606 Toolkit::GetImplementation(mVisual).SetOffStage(self);
612 mVideoPlayer.Pause();
615 mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
616 mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
617 mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
618 mPositionUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
619 mSizeUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
620 mScaleUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
622 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle() );
623 mVideoPlayer.SetUrl( mUrl );
627 // For underlay rendering mode, video display area have to be transparent.
628 Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
629 Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
630 mRenderer = Renderer::New( geometry, shader );
632 mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
633 mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_RGB, BlendFactor::ONE );
634 mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_RGB, BlendFactor::ZERO );
635 mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_ALPHA, BlendFactor::ONE );
636 mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_ALPHA, BlendFactor::ZERO );
646 mVideoPlayer.SetPlayPosition( curPos );
650 void VideoView::SetNativeImageTarget()
652 if( mVideoPlayer.IsVideoTextureSupported() == false )
654 DALI_LOG_ERROR( "Platform doesn't support decoded video frame images\n" );
661 mVideoPlayer.Pause();
664 Actor self( Self() );
665 self.RemoveRenderer( mRenderer );
666 Dali::Stage::GetCurrent().KeepRendering( 0.0f );
668 self.RemovePropertyNotification( mPositionUpdateNotification );
669 self.RemovePropertyNotification( mSizeUpdateNotification );
670 self.RemovePropertyNotification( mScaleUpdateNotification );
672 int curPos = mVideoPlayer.GetPlayPosition();
675 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
676 mNativeImage = Dali::NativeImage::New( *nativeImageSourcePtr );
678 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
679 mVideoPlayer.SetUrl( mUrl );
681 Internal::InitializeVisual( self, mVisual, mNativeImage );
682 Self().RemoveRenderer( mRenderer );
691 mVideoPlayer.SetPlayPosition( curPos );
695 void VideoView::UpdateDisplayArea( Dali::PropertyNotification& source )
702 Actor self( Self() );
704 bool positionUsesAnchorPoint = self.GetProperty( DevelActor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >();
705 Vector3 actorSize = self.GetCurrentSize() * self.GetCurrentScale();
706 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentAnchorPoint() : AnchorPoint::TOP_LEFT );
708 Vector2 screenPosition = self.GetProperty( DevelActor::Property::SCREEN_POSITION ).Get< Vector2 >();
710 mDisplayArea.x = screenPosition.x - anchorPointOffSet.x;
711 mDisplayArea.y = screenPosition.y - anchorPointOffSet.y;
712 mDisplayArea.width = actorSize.x;
713 mDisplayArea.height = actorSize.y;
715 mVideoPlayer.SetDisplayArea( mDisplayArea );
718 void VideoView::SetUnderlay( bool set )
720 if( set != mIsUnderlay )
726 SetWindowSurfaceTarget();
730 SetNativeImageTarget();
737 bool VideoView::IsUnderlay()
742 void VideoView::SetSWCodec( bool on )
744 // If setting SW or HW type is failed , video-view shows video by default codec type.
745 // The default codec type is selected by platform.
748 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::SW );
752 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::HW );
756 int VideoView::GetPlayPosition()
758 return mVideoPlayer.GetPlayPosition();
761 void VideoView::SetPlayPosition( int pos )
763 mVideoPlayer.SetPlayPosition( pos );
766 } // namespace Internal
768 } // namespace toolkit