2 * Copyright (c) 2019 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/debug.h>
29 #include <dali/public-api/animation/constraint.h>
30 #include <dali/devel-api/actors/actor-devel.h>
33 #include <dali-toolkit/public-api/controls/video-view/video-view.h>
34 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
35 #include <dali/integration-api/adaptor-framework/adaptor.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 )
60 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "underlay", BOOLEAN, UNDERLAY )
61 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "playPosition", INTEGER, PLAY_POSITION )
62 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "displayMode", INTEGER, DISPLAY_MODE )
64 DALI_SIGNAL_REGISTRATION( Toolkit, VideoView, "finished", FINISHED_SIGNAL )
66 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "play", ACTION_VIDEOVIEW_PLAY )
67 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "pause", ACTION_VIDEOVIEW_PAUSE )
68 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "stop", ACTION_VIDEOVIEW_STOP )
69 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "forward", ACTION_VIDEOVIEW_FORWARD )
70 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "backward", ACTION_VIDEOVIEW_BACKWARD )
72 DALI_TYPE_REGISTRATION_END()
74 const char* const VOLUME_LEFT( "volumeLeft" );
75 const char* const VOLUME_RIGHT( "volumeRight" );
77 // 3.0 TC uses RENDERING_TARGET. It should be removed in next release
78 const char* const RENDERING_TARGET( "renderingTarget" );
79 const char* const WINDOW_SURFACE_TARGET( "windowSurfaceTarget" );
80 const char* const NATIVE_IMAGE_TARGET( "nativeImageTarget" );
82 const char* const CUSTOM_SHADER( "shader" );
83 const char* const CUSTOM_VERTEX_SHADER( "vertexShader" );
84 const char* const CUSTOM_FRAGMENT_SHADER( "fragmentShader" );
85 const char* const DEFAULT_SAMPLER_TYPE_NAME( "sampler2D" );
86 const char* const CUSTOM_SAMPLER_TYPE_NAME( "samplerExternalOES" );
88 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
89 attribute mediump vec2 aPosition;\n
90 uniform highp mat4 uMvpMatrix;\n
91 uniform mediump vec3 uSize;\n
95 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
96 vertexPosition.xyz *= uSize;\n
97 gl_Position = uMvpMatrix * vertexPosition;\n
101 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
105 gl_FragColor = vec4(0.0);\n
109 const char* VERTEX_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
110 attribute mediump vec2 aPosition;\n
111 varying mediump vec2 vTexCoord;\n
112 uniform highp mat4 uMvpMatrix;\n
113 uniform mediump vec3 uSize;\n
114 varying mediump vec2 sTexCoordRect;\n
117 gl_Position = uMvpMatrix * vec4(aPosition * uSize.xy, 0.0, 1.0);\n
118 vTexCoord = aPosition + vec2(0.5);\n
122 const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
123 uniform lowp vec4 uColor;\n
124 varying mediump vec2 vTexCoord;\n
125 uniform samplerExternalOES sTexture;\n
128 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
132 } // anonymous namepsace
134 VideoView::VideoView()
135 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
136 mCurrentVideoPlayPosition( 0 ),
140 mVideoPlayer = Dali::VideoPlayer::New();
143 VideoView::~VideoView()
147 Toolkit::VideoView VideoView::New()
149 VideoView* impl = new VideoView();
150 Toolkit::VideoView handle = Toolkit::VideoView( *impl );
157 void VideoView::OnInitialize()
159 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
162 void VideoView::SetUrl( const std::string& url )
165 mPropertyMap.Clear();
167 mVideoPlayer.SetUrl( mUrl );
170 void VideoView::SetPropertyMap( Property::Map map )
174 Property::Value* target = map.Find( RENDERING_TARGET );
175 std::string targetType;
177 if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
180 SetWindowSurfaceTarget();
182 else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
185 SetNativeImageTarget();
189 Property::Value* shaderValue;
192 shaderValue = map.Find( CUSTOM_SHADER );
196 Property::Map* shaderMap = shaderValue->GetMap();
199 mEffectPropertyMap = *shaderMap;
204 if( mTextureRenderer && !mEffectPropertyMap.Empty() )
206 Dali::Shader shader = CreateShader();
207 mTextureRenderer.SetShader( shader );
213 std::string VideoView::GetUrl()
218 void VideoView::SetLooping( bool looping )
220 mVideoPlayer.SetLooping( looping );
223 bool VideoView::IsLooping()
225 return mVideoPlayer.IsLooping();
228 void VideoView::Play()
230 if( mOverlayRenderer )
232 Self().AddRenderer( mOverlayRenderer );
239 void VideoView::Pause()
241 mVideoPlayer.Pause();
245 void VideoView::Stop()
251 void VideoView::Forward( int millisecond )
253 int curPos = mVideoPlayer.GetPlayPosition();
255 int nextPos = curPos + millisecond;
257 mVideoPlayer.SetPlayPosition( nextPos );
260 void VideoView::Backward( int millisecond )
262 int curPos = mVideoPlayer.GetPlayPosition();
264 int nextPos = curPos - millisecond;
265 nextPos = ( nextPos < 0 )? 0: nextPos;
267 mVideoPlayer.SetPlayPosition( nextPos );
270 void VideoView::SetMute( bool mute )
272 mVideoPlayer.SetMute( mute );
275 bool VideoView::IsMuted()
277 return mVideoPlayer.IsMuted();
280 void VideoView::SetVolume( float left, float right )
282 mVideoPlayer.SetVolume( left, right );
285 void VideoView::GetVolume( float& left, float& right )
287 mVideoPlayer.GetVolume( left, right );
290 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
292 return mFinishedSignal;
295 void VideoView::EmitSignalFinish()
297 if( mOverlayRenderer )
299 Self().RemoveRenderer( mOverlayRenderer );
302 if ( !mFinishedSignal.Empty() )
304 Dali::Toolkit::VideoView handle( GetOwner() );
305 mFinishedSignal.Emit( handle );
309 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
313 Dali::BaseHandle handle( object );
314 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
321 VideoView& impl = GetImpl( videoView );
323 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
328 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
333 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
338 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
341 if( attributes["videoForward"].Get( millisecond ) )
343 impl.Forward( millisecond );
347 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
350 if( attributes["videoBackward"].Get( millisecond ) )
352 impl.Backward( millisecond );
360 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
362 Dali::BaseHandle handle( object );
364 bool connected( true );
365 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
367 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
369 videoView.FinishedSignal().Connect( tracker, functor );
373 // signalName does not match any signal
380 void VideoView::SetPropertyInternal( Property::Index index, const Property::Value& value )
384 case Toolkit::VideoView::Property::VIDEO:
386 std::string videoUrl;
389 if( value.Get( videoUrl ) )
393 else if( value.Get( map ) )
395 SetPropertyMap( map );
399 case Toolkit::VideoView::Property::LOOPING:
402 if( value.Get( looping ) )
404 SetLooping( looping );
408 case Toolkit::VideoView::Property::MUTED:
411 if( value.Get( mute ) )
417 case Toolkit::VideoView::Property::VOLUME:
421 if( value.Get( map ) )
423 Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
424 Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
425 if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
427 SetVolume( left, right );
432 case Toolkit::VideoView::Property::UNDERLAY:
435 if( value.Get( underlay ) )
437 SetUnderlay( underlay );
441 case Toolkit::VideoView::Property::PLAY_POSITION:
444 if( value.Get( pos ) )
446 SetPlayPosition( pos );
450 case Toolkit::VideoView::Property::DISPLAY_MODE:
453 if( value.Get( mode ) )
455 SetDisplayMode( mode );
462 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
464 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
468 VideoView& impl = GetImpl( videoView );
470 impl.SetPropertyInternal( index, value );
472 if( index != Toolkit::VideoView::Property::UNDERLAY )
475 // These values will be used when underlay mode is changed.
476 impl.mPropertyBackup[index] = value;
481 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
483 Property::Value value;
484 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
488 VideoView& impl = GetImpl( videoView );
490 switch( propertyIndex )
492 case Toolkit::VideoView::Property::VIDEO:
494 if( !impl.mUrl.empty() )
498 else if( !impl.mPropertyMap.Empty() )
500 value = impl.mPropertyMap;
504 case Toolkit::VideoView::Property::LOOPING:
506 value = impl.IsLooping();
509 case Toolkit::VideoView::Property::MUTED:
511 value = impl.IsMuted();
514 case Toolkit::VideoView::Property::VOLUME:
519 impl.GetVolume( left, right );
520 map.Insert( VOLUME_LEFT, left );
521 map.Insert( VOLUME_RIGHT, right );
525 case Toolkit::VideoView::Property::UNDERLAY:
527 value = impl.IsUnderlay();
530 case Toolkit::VideoView::Property::PLAY_POSITION:
532 value = impl.GetPlayPosition();
535 case Toolkit::VideoView::Property::DISPLAY_MODE:
537 value = impl.GetDisplayMode();
546 void VideoView::SetDepthIndex( int depthIndex )
548 if( mTextureRenderer )
550 mTextureRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, depthIndex );
554 void VideoView::OnStageConnection( int depth )
556 Control::OnStageConnection( depth );
560 SetWindowSurfaceTarget();
564 void VideoView::OnStageDisconnection()
566 Control::OnStageDisconnection();
569 Vector3 VideoView::GetNaturalSize()
572 size.x = mVideoSize.GetWidth();
573 size.y = mVideoSize.GetHeight();
575 if( size.x > 0 && size.y > 0 )
577 size.z = std::min( size.x, size.y );
582 return Control::GetNaturalSize();
586 float VideoView::GetHeightForWidth( float width )
588 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
590 return GetHeightForWidthBase( width );
594 return Control::GetHeightForWidthBase( width );
598 float VideoView::GetWidthForHeight( float height )
600 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
602 return GetWidthForHeightBase( height );
606 return Control::GetWidthForHeightBase( height );
610 void VideoView::SetWindowSurfaceTarget()
614 if( !self.OnStage() )
616 // When the control is off the stage, it does not have Window.
620 int curPos = mVideoPlayer.GetPlayPosition();
624 mVideoPlayer.Pause();
627 mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
628 mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
629 mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
630 mPositionUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
631 mSizeUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
632 mScaleUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
634 if( mTextureRenderer )
636 self.RemoveRenderer( mTextureRenderer );
639 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
640 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle( self ) );
642 ApplyBackupProperties();
644 if( !mOverlayRenderer )
646 // For underlay rendering mode, video display area have to be transparent.
647 Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
648 Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
649 mOverlayRenderer = Renderer::New( geometry, shader );
650 mOverlayRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::OFF );
660 mVideoPlayer.SetPlayPosition( curPos );
664 void VideoView::SetNativeImageTarget()
666 if( mVideoPlayer.IsVideoTextureSupported() == false )
668 DALI_LOG_ERROR( "Platform doesn't support decoded video frame images\n" );
675 mVideoPlayer.Pause();
678 Actor self( Self() );
680 if( mOverlayRenderer )
682 self.RemoveRenderer( mOverlayRenderer );
684 mOverlayRenderer.Reset();
687 self.RemovePropertyNotification( mPositionUpdateNotification );
688 self.RemovePropertyNotification( mSizeUpdateNotification );
689 self.RemovePropertyNotification( mScaleUpdateNotification );
691 int curPos = mVideoPlayer.GetPlayPosition();
694 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
695 mNativeTexture = Dali::Texture::New( *nativeImageSourcePtr );
697 if( !mTextureRenderer )
699 Dali::Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
700 Dali::Shader shader = CreateShader();
701 Dali::TextureSet textureSet = Dali::TextureSet::New();
702 textureSet.SetTexture( 0u, mNativeTexture );
704 mTextureRenderer = Renderer::New( geometry, shader );
705 mTextureRenderer.SetTextures( textureSet );
709 Dali::TextureSet textureSet = mTextureRenderer.GetTextures();
710 textureSet.SetTexture( 0u, mNativeTexture );
712 Self().AddRenderer( mTextureRenderer );
714 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
715 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
717 ApplyBackupProperties();
726 mVideoPlayer.SetPlayPosition( curPos );
730 void VideoView::UpdateDisplayArea( Dali::PropertyNotification& source )
737 Actor self( Self() );
739 bool positionUsesAnchorPoint = self.GetProperty( DevelActor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >();
740 Vector3 actorSize = self.GetCurrentSize() * self.GetCurrentScale();
741 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentAnchorPoint() : AnchorPoint::TOP_LEFT );
743 Vector2 screenPosition = self.GetProperty( DevelActor::Property::SCREEN_POSITION ).Get< Vector2 >();
745 mDisplayArea.x = screenPosition.x - anchorPointOffSet.x;
746 mDisplayArea.y = screenPosition.y - anchorPointOffSet.y;
747 mDisplayArea.width = actorSize.x;
748 mDisplayArea.height = actorSize.y;
750 mVideoPlayer.SetDisplayArea( mDisplayArea );
753 void VideoView::SetUnderlay( bool set )
755 if( set != mIsUnderlay )
761 SetWindowSurfaceTarget();
765 SetNativeImageTarget();
772 bool VideoView::IsUnderlay()
777 void VideoView::SetSWCodec( bool on )
779 // If setting SW or HW type is failed , video-view shows video by default codec type.
780 // The default codec type is selected by platform.
783 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::SW );
787 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::HW );
791 int VideoView::GetPlayPosition()
793 return mVideoPlayer.GetPlayPosition();
796 void VideoView::SetPlayPosition( int pos )
798 mVideoPlayer.SetPlayPosition( pos );
801 void VideoView::SetDisplayMode( int mode )
803 mVideoPlayer.SetDisplayMode( static_cast< Dali::VideoPlayerPlugin::DisplayMode::Type >( mode ) );
806 int VideoView::GetDisplayMode() const
808 return static_cast< int >( mVideoPlayer.GetDisplayMode() );
811 Any VideoView::GetMediaPlayer()
813 return mVideoPlayer.GetMediaPlayer();
816 Dali::Shader VideoView::CreateShader()
818 std::string fragmentShader = "#extension GL_OES_EGL_image_external:require\n";
819 std::string vertexShader;
820 std::string customFragmentShader;
821 bool checkShader = false;
823 if( !mEffectPropertyMap.Empty() )
825 Property::Value* vertexShaderValue = mEffectPropertyMap.Find( CUSTOM_VERTEX_SHADER );
826 if( vertexShaderValue )
828 checkShader = GetStringFromProperty( *vertexShaderValue, vertexShader );
831 if( !vertexShaderValue || !checkShader )
833 vertexShader = VERTEX_SHADER_TEXTURE;
836 Property::Value* fragmentShaderValue = mEffectPropertyMap.Find( CUSTOM_FRAGMENT_SHADER );
837 if( fragmentShaderValue )
839 checkShader = GetStringFromProperty( *fragmentShaderValue, customFragmentShader );
843 fragmentShader = customFragmentShader;
847 if( !fragmentShaderValue || !checkShader )
849 fragmentShader += FRAGMENT_SHADER_TEXTURE;
854 vertexShader = VERTEX_SHADER_TEXTURE;
855 fragmentShader += FRAGMENT_SHADER_TEXTURE;
858 return Dali::Shader::New( vertexShader, fragmentShader );
861 bool VideoView::GetStringFromProperty( const Dali::Property::Value& value, std::string& output )
863 bool extracted = false;
864 if( value.Get( output ) )
872 void VideoView::ApplyBackupProperties()
874 Property::Map::SizeType pos = 0;
875 Property::Map::SizeType count = mPropertyBackup.Count();
877 for( ; pos < count; pos++ )
879 KeyValuePair property = mPropertyBackup.GetKeyValue( pos );
881 SetPropertyInternal( property.first.indexKey, property.second );
885 } // namespace Internal
887 } // namespace toolkit