2 * Copyright (c) 2020 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/devel-api/scripting/scripting.h>
26 #include <dali/devel-api/adaptor-framework/window-devel.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>
36 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
52 return Toolkit::VideoView::New();
55 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::VideoView, Toolkit::Control, Create );
57 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "video", MAP, VIDEO )
58 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "looping", BOOLEAN, LOOPING )
59 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "muted", BOOLEAN, MUTED )
60 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "volume", MAP, VOLUME )
61 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "underlay", BOOLEAN, UNDERLAY )
62 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "playPosition", INTEGER, PLAY_POSITION )
63 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "displayMode", INTEGER, DISPLAY_MODE )
65 DALI_SIGNAL_REGISTRATION( Toolkit, VideoView, "finished", FINISHED_SIGNAL )
67 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "play", ACTION_VIDEOVIEW_PLAY )
68 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "pause", ACTION_VIDEOVIEW_PAUSE )
69 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "stop", ACTION_VIDEOVIEW_STOP )
70 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "forward", ACTION_VIDEOVIEW_FORWARD )
71 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "backward", ACTION_VIDEOVIEW_BACKWARD )
73 DALI_TYPE_REGISTRATION_END()
75 const char* const VOLUME_LEFT( "volumeLeft" );
76 const char* const VOLUME_RIGHT( "volumeRight" );
78 // 3.0 TC uses RENDERING_TARGET. It should be removed in next release
79 const char* const RENDERING_TARGET( "renderingTarget" );
80 const char* const WINDOW_SURFACE_TARGET( "windowSurfaceTarget" );
81 const char* const NATIVE_IMAGE_TARGET( "nativeImageTarget" );
83 const char* const CUSTOM_SHADER( "shader" );
84 const char* const CUSTOM_VERTEX_SHADER( "vertexShader" );
85 const char* const CUSTOM_FRAGMENT_SHADER( "fragmentShader" );
86 const char* const DEFAULT_SAMPLER_TYPE_NAME( "sampler2D" );
87 const char* const CUSTOM_SAMPLER_TYPE_NAME( "samplerExternalOES" );
89 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
90 attribute mediump vec2 aPosition;\n
91 uniform highp mat4 uMvpMatrix;\n
92 uniform highp vec3 uSize;\n
96 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
97 vertexPosition.xyz *= uSize;\n
98 gl_Position = uMvpMatrix * vertexPosition;\n
102 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
106 gl_FragColor = vec4(0.0);\n
110 const char* VERTEX_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
111 attribute mediump vec2 aPosition;\n
112 varying mediump vec2 vTexCoord;\n
113 uniform highp mat4 uMvpMatrix;\n
114 uniform highp vec3 uSize;\n
115 varying mediump vec2 sTexCoordRect;\n
118 gl_Position = uMvpMatrix * vec4(aPosition * uSize.xy, 0.0, 1.0);\n
119 vTexCoord = aPosition + vec2(0.5);\n
123 const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
124 uniform lowp vec4 uColor;\n
125 varying mediump vec2 vTexCoord;\n
126 uniform samplerExternalOES sTexture;\n
129 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
133 } // anonymous namepsace
135 VideoView::VideoView( Dali::VideoSyncMode syncMode )
136 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
137 mCurrentVideoPlayPosition( 0 ),
141 mSyncMode( syncMode )
145 VideoView::~VideoView()
149 Toolkit::VideoView VideoView::New( VideoSyncMode syncMode )
151 VideoView* impl = new VideoView( syncMode );
152 Toolkit::VideoView handle = Toolkit::VideoView( *impl );
154 impl->mVideoPlayer = Dali::VideoPlayer::New( impl->Self(), syncMode );
159 void VideoView::OnInitialize()
161 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
163 DevelControl::SetAccessibilityConstructor( Self(), []( Dali::Actor actor ) {
164 return std::unique_ptr< Dali::Accessibility::Accessible >(
165 new Control::Impl::AccessibleImpl( actor, Dali::Accessibility::Role::VIDEO ));
169 void VideoView::SetUrl( const std::string& url )
172 mPropertyMap.Clear();
174 mVideoPlayer.SetUrl( mUrl );
177 void VideoView::SetPropertyMap( Property::Map map )
181 Property::Value* target = map.Find( RENDERING_TARGET );
182 std::string targetType;
184 if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
187 SetWindowSurfaceTarget();
189 else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
192 SetNativeImageTarget();
196 Property::Value* shaderValue;
199 shaderValue = map.Find( CUSTOM_SHADER );
203 Property::Map* shaderMap = shaderValue->GetMap();
206 mEffectPropertyMap = *shaderMap;
211 if( mTextureRenderer && !mEffectPropertyMap.Empty() )
213 Dali::Shader shader = CreateShader();
214 mTextureRenderer.SetShader( shader );
220 std::string VideoView::GetUrl()
225 void VideoView::SetLooping( bool looping )
227 mVideoPlayer.SetLooping( looping );
230 bool VideoView::IsLooping()
232 return mVideoPlayer.IsLooping();
235 void VideoView::Play()
241 void VideoView::Pause()
243 mVideoPlayer.Pause();
247 void VideoView::Stop()
253 void VideoView::Forward( int millisecond )
255 int curPos = mVideoPlayer.GetPlayPosition();
257 int nextPos = curPos + millisecond;
259 mVideoPlayer.SetPlayPosition( nextPos );
262 void VideoView::Backward( int millisecond )
264 int curPos = mVideoPlayer.GetPlayPosition();
266 int nextPos = curPos - millisecond;
267 nextPos = ( nextPos < 0 )? 0: nextPos;
269 mVideoPlayer.SetPlayPosition( nextPos );
272 void VideoView::SetMute( bool mute )
274 mVideoPlayer.SetMute( mute );
277 bool VideoView::IsMuted()
279 return mVideoPlayer.IsMuted();
282 void VideoView::SetVolume( float left, float right )
284 mVideoPlayer.SetVolume( left, right );
287 void VideoView::GetVolume( float& left, float& right )
289 mVideoPlayer.GetVolume( left, right );
292 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
294 return mFinishedSignal;
297 void VideoView::EmitSignalFinish()
299 if ( !mFinishedSignal.Empty() )
301 Dali::Toolkit::VideoView handle( GetOwner() );
302 mFinishedSignal.Emit( handle );
306 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
310 Dali::BaseHandle handle( object );
311 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
318 VideoView& impl = GetImpl( videoView );
320 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
325 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
330 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
335 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
338 if( attributes["videoForward"].Get( millisecond ) )
340 impl.Forward( millisecond );
344 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
347 if( attributes["videoBackward"].Get( millisecond ) )
349 impl.Backward( millisecond );
357 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
359 Dali::BaseHandle handle( object );
361 bool connected( true );
362 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
364 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
366 videoView.FinishedSignal().Connect( tracker, functor );
370 // signalName does not match any signal
377 void VideoView::SetPropertyInternal( Property::Index index, const Property::Value& value )
381 case Toolkit::VideoView::Property::VIDEO:
383 std::string videoUrl;
386 if( value.Get( videoUrl ) )
390 else if( value.Get( map ) )
392 SetPropertyMap( map );
396 case Toolkit::VideoView::Property::LOOPING:
399 if( value.Get( looping ) )
401 SetLooping( looping );
405 case Toolkit::VideoView::Property::MUTED:
408 if( value.Get( mute ) )
414 case Toolkit::VideoView::Property::VOLUME:
418 if( value.Get( map ) )
420 Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
421 Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
422 if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
424 SetVolume( left, right );
429 case Toolkit::VideoView::Property::UNDERLAY:
432 if( value.Get( underlay ) )
434 SetUnderlay( underlay );
438 case Toolkit::VideoView::Property::PLAY_POSITION:
441 if( value.Get( pos ) )
443 SetPlayPosition( pos );
447 case Toolkit::VideoView::Property::DISPLAY_MODE:
450 if( value.Get( mode ) )
452 SetDisplayMode( mode );
459 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
461 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
465 VideoView& impl = GetImpl( videoView );
467 impl.SetPropertyInternal( index, value );
469 if( index != Toolkit::VideoView::Property::UNDERLAY )
472 // These values will be used when underlay mode is changed.
473 impl.mPropertyBackup[index] = value;
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 )
545 if( mTextureRenderer )
547 mTextureRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, depthIndex );
551 void VideoView::OnSceneConnection( int depth )
553 Control::OnSceneConnection( depth );
557 SetWindowSurfaceTarget();
561 void VideoView::OnSceneDisconnection()
563 Control::OnSceneDisconnection();
566 void VideoView::OnSizeSet( const Vector3& targetSize )
568 if( mIsUnderlay && mSyncMode == Dali::VideoSyncMode::ENABLED )
570 SetFrameRenderCallback();
571 mVideoPlayer.StartSynchronization();
573 Control::OnSizeSet( targetSize );
576 Vector3 VideoView::GetNaturalSize()
579 size.x = mVideoSize.GetWidth();
580 size.y = mVideoSize.GetHeight();
582 if( size.x > 0 && size.y > 0 )
584 size.z = std::min( size.x, size.y );
589 return Control::GetNaturalSize();
593 float VideoView::GetHeightForWidth( float width )
595 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
597 return GetHeightForWidthBase( width );
601 return Control::GetHeightForWidthBase( width );
605 float VideoView::GetWidthForHeight( float height )
607 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
609 return GetWidthForHeightBase( height );
613 return Control::GetWidthForHeightBase( height );
617 void VideoView::SetWindowSurfaceTarget()
621 if( !self.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
623 // When the control is off the stage, it does not have Window.
627 int curPos = mVideoPlayer.GetPlayPosition();
631 mVideoPlayer.Pause();
634 mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
635 mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
636 mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
637 mPositionUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
638 mSizeUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
639 mScaleUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
641 if( mTextureRenderer )
643 self.RemoveRenderer( mTextureRenderer );
646 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
647 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle( self ) );
649 ApplyBackupProperties();
651 if( !mOverlayRenderer )
653 // For underlay rendering mode, video display area have to be transparent.
654 Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
655 Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
656 mOverlayRenderer = Renderer::New( geometry, shader );
657 mOverlayRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::OFF );
659 Self().AddRenderer( mOverlayRenderer );
668 mVideoPlayer.SetPlayPosition( curPos );
672 void VideoView::SetNativeImageTarget()
674 if( mVideoPlayer.IsVideoTextureSupported() == false )
676 DALI_LOG_ERROR( "Platform doesn't support decoded video frame images\n" );
683 mVideoPlayer.Pause();
686 Actor self( Self() );
688 if( mOverlayRenderer )
690 self.RemoveRenderer( mOverlayRenderer );
692 mOverlayRenderer.Reset();
695 self.RemovePropertyNotification( mPositionUpdateNotification );
696 self.RemovePropertyNotification( mSizeUpdateNotification );
697 self.RemovePropertyNotification( mScaleUpdateNotification );
699 int curPos = mVideoPlayer.GetPlayPosition();
702 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
703 mNativeTexture = Dali::Texture::New( *nativeImageSourcePtr );
705 if( !mTextureRenderer )
707 Dali::Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
708 Dali::Shader shader = CreateShader();
709 Dali::TextureSet textureSet = Dali::TextureSet::New();
710 textureSet.SetTexture( 0u, mNativeTexture );
712 mTextureRenderer = Renderer::New( geometry, shader );
713 mTextureRenderer.SetTextures( textureSet );
717 Dali::TextureSet textureSet = mTextureRenderer.GetTextures();
718 textureSet.SetTexture( 0u, mNativeTexture );
720 Self().AddRenderer( mTextureRenderer );
722 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
723 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
725 ApplyBackupProperties();
734 mVideoPlayer.SetPlayPosition( curPos );
738 void VideoView::UpdateDisplayArea( Dali::PropertyNotification& source )
740 // If mSyncMode is enabled, Video player's size and poistion is updated in Video player's constraint.
741 // Because video view and player should be work syncronization.
742 if( !mIsUnderlay || mSyncMode == Dali::VideoSyncMode::ENABLED )
747 Actor self( Self() );
749 bool positionUsesAnchorPoint = self.GetProperty( Actor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >();
750 Vector3 actorSize = self.GetCurrentProperty< Vector3 >( Actor::Property::SIZE ) * self.GetCurrentProperty< Vector3 >( Actor::Property::SCALE );
751 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentProperty< Vector3 >( Actor::Property::ANCHOR_POINT ) : AnchorPoint::TOP_LEFT );
753 Vector2 screenPosition = self.GetProperty( Actor::Property::SCREEN_POSITION ).Get< Vector2 >();
755 mDisplayArea.x = screenPosition.x - anchorPointOffSet.x;
756 mDisplayArea.y = screenPosition.y - anchorPointOffSet.y;
757 mDisplayArea.width = actorSize.x;
758 mDisplayArea.height = actorSize.y;
760 mVideoPlayer.SetDisplayArea( mDisplayArea );
763 void VideoView::SetUnderlay( bool set )
765 if( set != mIsUnderlay )
771 SetWindowSurfaceTarget();
775 SetNativeImageTarget();
782 bool VideoView::IsUnderlay()
787 void VideoView::SetSWCodec( bool on )
789 // If setting SW or HW type is failed , video-view shows video by default codec type.
790 // The default codec type is selected by platform.
793 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::SW );
797 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::HW );
801 int VideoView::GetPlayPosition()
803 return mVideoPlayer.GetPlayPosition();
806 void VideoView::SetPlayPosition( int pos )
808 mVideoPlayer.SetPlayPosition( pos );
811 void VideoView::SetDisplayMode( int mode )
813 mVideoPlayer.SetDisplayMode( static_cast< Dali::VideoPlayerPlugin::DisplayMode::Type >( mode ) );
816 int VideoView::GetDisplayMode() const
818 return static_cast< int >( mVideoPlayer.GetDisplayMode() );
821 Any VideoView::GetMediaPlayer()
823 return mVideoPlayer.GetMediaPlayer();
826 void VideoView::OnAnimationFinished( Animation& animation )
829 SetFrameRenderCallback();
832 void VideoView::PlayAnimation( Dali::Animation animation )
834 if( mIsUnderlay && mSyncMode == Dali::VideoSyncMode::ENABLED )
836 mVideoPlayer.StartSynchronization();
837 animation.FinishedSignal().Connect( this, &VideoView::OnAnimationFinished );
842 Dali::Shader VideoView::CreateShader()
844 std::string fragmentShader = "#extension GL_OES_EGL_image_external:require\n";
845 std::string vertexShader;
846 std::string customFragmentShader;
847 bool checkShader = false;
849 if( !mEffectPropertyMap.Empty() )
851 Property::Value* vertexShaderValue = mEffectPropertyMap.Find( CUSTOM_VERTEX_SHADER );
852 if( vertexShaderValue )
854 checkShader = GetStringFromProperty( *vertexShaderValue, vertexShader );
857 if( !vertexShaderValue || !checkShader )
859 vertexShader = VERTEX_SHADER_TEXTURE;
862 Property::Value* fragmentShaderValue = mEffectPropertyMap.Find( CUSTOM_FRAGMENT_SHADER );
863 if( fragmentShaderValue )
865 checkShader = GetStringFromProperty( *fragmentShaderValue, customFragmentShader );
869 fragmentShader = customFragmentShader;
873 if( !fragmentShaderValue || !checkShader )
875 fragmentShader += FRAGMENT_SHADER_TEXTURE;
880 vertexShader = VERTEX_SHADER_TEXTURE;
881 fragmentShader += FRAGMENT_SHADER_TEXTURE;
884 return Dali::Shader::New( vertexShader, fragmentShader );
887 bool VideoView::GetStringFromProperty( const Dali::Property::Value& value, std::string& output )
889 bool extracted = false;
890 if( value.Get( output ) )
898 void VideoView::ApplyBackupProperties()
900 Property::Map::SizeType pos = 0;
901 Property::Map::SizeType count = mPropertyBackup.Count();
903 for( ; pos < count; pos++ )
905 KeyValuePair property = mPropertyBackup.GetKeyValue( pos );
907 SetPropertyInternal( property.first.indexKey, property.second );
911 void VideoView::FrameRenderCallback( int frameID )
914 if( frameID == mFrameID )
916 mVideoPlayer.FinishSynchronization();
921 void VideoView::SetFrameRenderCallback()
924 DevelWindow::AddFrameRenderedCallback( DevelWindow::Get( Self() ),
925 std::unique_ptr< CallbackBase >( MakeCallback( this, &VideoView::FrameRenderCallback ) ), mFrameID );
928 } // namespace Internal
930 } // namespace toolkit