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 )
143 DevelControl::SetAccessibilityConstructor( Self(), []( Dali::Actor actor )
145 return std::unique_ptr< Dali::Accessibility::Accessible >(
146 new Control::Impl::AccessibleImpl( actor, Dali::Accessibility::Role::VIDEO, true ) );
151 VideoView::~VideoView()
155 Toolkit::VideoView VideoView::New( VideoSyncMode syncMode )
157 VideoView* impl = new VideoView( syncMode );
158 Toolkit::VideoView handle = Toolkit::VideoView( *impl );
160 impl->mVideoPlayer = Dali::VideoPlayer::New( impl->Self(), syncMode );
165 void VideoView::OnInitialize()
167 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
170 void VideoView::SetUrl( const std::string& url )
173 mPropertyMap.Clear();
175 mVideoPlayer.SetUrl( mUrl );
178 void VideoView::SetPropertyMap( Property::Map map )
182 Property::Value* target = map.Find( RENDERING_TARGET );
183 std::string targetType;
185 if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
188 SetWindowSurfaceTarget();
190 else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
193 SetNativeImageTarget();
197 Property::Value* shaderValue;
200 shaderValue = map.Find( CUSTOM_SHADER );
204 Property::Map* shaderMap = shaderValue->GetMap();
207 mEffectPropertyMap = *shaderMap;
212 if( mTextureRenderer && !mEffectPropertyMap.Empty() )
214 Dali::Shader shader = CreateShader();
215 mTextureRenderer.SetShader( shader );
221 std::string VideoView::GetUrl()
226 void VideoView::SetLooping( bool looping )
228 mVideoPlayer.SetLooping( looping );
231 bool VideoView::IsLooping()
233 return mVideoPlayer.IsLooping();
236 void VideoView::Play()
242 void VideoView::Pause()
244 mVideoPlayer.Pause();
248 void VideoView::Stop()
254 void VideoView::Forward( int millisecond )
256 int curPos = mVideoPlayer.GetPlayPosition();
258 int nextPos = curPos + millisecond;
260 mVideoPlayer.SetPlayPosition( nextPos );
263 void VideoView::Backward( int millisecond )
265 int curPos = mVideoPlayer.GetPlayPosition();
267 int nextPos = curPos - millisecond;
268 nextPos = ( nextPos < 0 )? 0: nextPos;
270 mVideoPlayer.SetPlayPosition( nextPos );
273 void VideoView::SetMute( bool mute )
275 mVideoPlayer.SetMute( mute );
278 bool VideoView::IsMuted()
280 return mVideoPlayer.IsMuted();
283 void VideoView::SetVolume( float left, float right )
285 mVideoPlayer.SetVolume( left, right );
288 void VideoView::GetVolume( float& left, float& right )
290 mVideoPlayer.GetVolume( left, right );
293 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
295 return mFinishedSignal;
298 void VideoView::EmitSignalFinish()
300 if ( !mFinishedSignal.Empty() )
302 Dali::Toolkit::VideoView handle( GetOwner() );
303 mFinishedSignal.Emit( handle );
307 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
311 Dali::BaseHandle handle( object );
312 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
319 VideoView& impl = GetImpl( videoView );
321 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
326 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
331 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
336 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
339 if( attributes["videoForward"].Get( millisecond ) )
341 impl.Forward( millisecond );
345 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
348 if( attributes["videoBackward"].Get( millisecond ) )
350 impl.Backward( millisecond );
358 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
360 Dali::BaseHandle handle( object );
362 bool connected( true );
363 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
365 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
367 videoView.FinishedSignal().Connect( tracker, functor );
371 // signalName does not match any signal
378 void VideoView::SetPropertyInternal( Property::Index index, const Property::Value& value )
382 case Toolkit::VideoView::Property::VIDEO:
384 std::string videoUrl;
387 if( value.Get( videoUrl ) )
391 else if( value.Get( map ) )
393 SetPropertyMap( map );
397 case Toolkit::VideoView::Property::LOOPING:
400 if( value.Get( looping ) )
402 SetLooping( looping );
406 case Toolkit::VideoView::Property::MUTED:
409 if( value.Get( mute ) )
415 case Toolkit::VideoView::Property::VOLUME:
419 if( value.Get( map ) )
421 Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
422 Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
423 if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
425 SetVolume( left, right );
430 case Toolkit::VideoView::Property::UNDERLAY:
433 if( value.Get( underlay ) )
435 SetUnderlay( underlay );
439 case Toolkit::VideoView::Property::PLAY_POSITION:
442 if( value.Get( pos ) )
444 SetPlayPosition( pos );
448 case Toolkit::VideoView::Property::DISPLAY_MODE:
451 if( value.Get( mode ) )
453 SetDisplayMode( mode );
460 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
462 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
466 VideoView& impl = GetImpl( videoView );
468 impl.SetPropertyInternal( index, value );
470 if( index != Toolkit::VideoView::Property::UNDERLAY )
473 // These values will be used when underlay mode is changed.
474 impl.mPropertyBackup[index] = value;
479 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
481 Property::Value value;
482 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
486 VideoView& impl = GetImpl( videoView );
488 switch( propertyIndex )
490 case Toolkit::VideoView::Property::VIDEO:
492 if( !impl.mUrl.empty() )
496 else if( !impl.mPropertyMap.Empty() )
498 value = impl.mPropertyMap;
502 case Toolkit::VideoView::Property::LOOPING:
504 value = impl.IsLooping();
507 case Toolkit::VideoView::Property::MUTED:
509 value = impl.IsMuted();
512 case Toolkit::VideoView::Property::VOLUME:
517 impl.GetVolume( left, right );
518 map.Insert( VOLUME_LEFT, left );
519 map.Insert( VOLUME_RIGHT, right );
523 case Toolkit::VideoView::Property::UNDERLAY:
525 value = impl.IsUnderlay();
528 case Toolkit::VideoView::Property::PLAY_POSITION:
530 value = impl.GetPlayPosition();
533 case Toolkit::VideoView::Property::DISPLAY_MODE:
535 value = impl.GetDisplayMode();
544 void VideoView::SetDepthIndex( int depthIndex )
546 if( mTextureRenderer )
548 mTextureRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, depthIndex );
552 void VideoView::OnSceneConnection( int depth )
554 Control::OnSceneConnection( depth );
558 SetWindowSurfaceTarget();
562 void VideoView::OnSceneDisconnection()
564 Control::OnSceneDisconnection();
567 void VideoView::OnSizeSet( const Vector3& targetSize )
569 if( mIsUnderlay && mSyncMode == Dali::VideoSyncMode::ENABLED )
571 SetFrameRenderCallback();
572 mVideoPlayer.StartSynchronization();
574 Control::OnSizeSet( targetSize );
577 Vector3 VideoView::GetNaturalSize()
580 size.x = mVideoSize.GetWidth();
581 size.y = mVideoSize.GetHeight();
583 if( size.x > 0 && size.y > 0 )
585 size.z = std::min( size.x, size.y );
590 return Control::GetNaturalSize();
594 float VideoView::GetHeightForWidth( float width )
596 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
598 return GetHeightForWidthBase( width );
602 return Control::GetHeightForWidthBase( width );
606 float VideoView::GetWidthForHeight( float height )
608 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
610 return GetWidthForHeightBase( height );
614 return Control::GetWidthForHeightBase( height );
618 void VideoView::SetWindowSurfaceTarget()
622 if( !self.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
624 // When the control is off the stage, it does not have Window.
628 int curPos = mVideoPlayer.GetPlayPosition();
632 mVideoPlayer.Pause();
635 mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
636 mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
637 mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
638 mPositionUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
639 mSizeUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
640 mScaleUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
642 if( mTextureRenderer )
644 self.RemoveRenderer( mTextureRenderer );
647 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
648 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle( self ) );
650 ApplyBackupProperties();
652 if( !mOverlayRenderer )
654 // For underlay rendering mode, video display area have to be transparent.
655 Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
656 Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
657 mOverlayRenderer = Renderer::New( geometry, shader );
658 mOverlayRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::OFF );
660 Self().AddRenderer( mOverlayRenderer );
669 mVideoPlayer.SetPlayPosition( curPos );
673 void VideoView::SetNativeImageTarget()
675 if( mVideoPlayer.IsVideoTextureSupported() == false )
677 DALI_LOG_ERROR( "Platform doesn't support decoded video frame images\n" );
684 mVideoPlayer.Pause();
687 Actor self( Self() );
689 if( mOverlayRenderer )
691 self.RemoveRenderer( mOverlayRenderer );
693 mOverlayRenderer.Reset();
696 self.RemovePropertyNotification( mPositionUpdateNotification );
697 self.RemovePropertyNotification( mSizeUpdateNotification );
698 self.RemovePropertyNotification( mScaleUpdateNotification );
700 int curPos = mVideoPlayer.GetPlayPosition();
703 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
704 mNativeTexture = Dali::Texture::New( *nativeImageSourcePtr );
706 if( !mTextureRenderer )
708 Dali::Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
709 Dali::Shader shader = CreateShader();
710 Dali::TextureSet textureSet = Dali::TextureSet::New();
711 textureSet.SetTexture( 0u, mNativeTexture );
713 mTextureRenderer = Renderer::New( geometry, shader );
714 mTextureRenderer.SetTextures( textureSet );
718 Dali::TextureSet textureSet = mTextureRenderer.GetTextures();
719 textureSet.SetTexture( 0u, mNativeTexture );
721 Self().AddRenderer( mTextureRenderer );
723 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
724 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
726 ApplyBackupProperties();
735 mVideoPlayer.SetPlayPosition( curPos );
739 void VideoView::UpdateDisplayArea( Dali::PropertyNotification& source )
741 // If mSyncMode is enabled, Video player's size and poistion is updated in Video player's constraint.
742 // Because video view and player should be work syncronization.
743 if( !mIsUnderlay || mSyncMode == Dali::VideoSyncMode::ENABLED )
748 Actor self( Self() );
750 bool positionUsesAnchorPoint = self.GetProperty( Actor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >();
751 Vector3 actorSize = self.GetCurrentProperty< Vector3 >( Actor::Property::SIZE ) * self.GetCurrentProperty< Vector3 >( Actor::Property::SCALE );
752 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentProperty< Vector3 >( Actor::Property::ANCHOR_POINT ) : AnchorPoint::TOP_LEFT );
754 Vector2 screenPosition = self.GetProperty( Actor::Property::SCREEN_POSITION ).Get< Vector2 >();
756 mDisplayArea.x = screenPosition.x - anchorPointOffSet.x;
757 mDisplayArea.y = screenPosition.y - anchorPointOffSet.y;
758 mDisplayArea.width = actorSize.x;
759 mDisplayArea.height = actorSize.y;
761 mVideoPlayer.SetDisplayArea( mDisplayArea );
764 void VideoView::SetUnderlay( bool set )
766 if( set != mIsUnderlay )
772 SetWindowSurfaceTarget();
776 SetNativeImageTarget();
783 bool VideoView::IsUnderlay()
788 void VideoView::SetSWCodec( bool on )
790 // If setting SW or HW type is failed , video-view shows video by default codec type.
791 // The default codec type is selected by platform.
794 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::SW );
798 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::HW );
802 int VideoView::GetPlayPosition()
804 return mVideoPlayer.GetPlayPosition();
807 void VideoView::SetPlayPosition( int pos )
809 mVideoPlayer.SetPlayPosition( pos );
812 void VideoView::SetDisplayMode( int mode )
814 mVideoPlayer.SetDisplayMode( static_cast< Dali::VideoPlayerPlugin::DisplayMode::Type >( mode ) );
817 int VideoView::GetDisplayMode() const
819 return static_cast< int >( mVideoPlayer.GetDisplayMode() );
822 Any VideoView::GetMediaPlayer()
824 return mVideoPlayer.GetMediaPlayer();
827 void VideoView::OnAnimationFinished( Animation& animation )
830 SetFrameRenderCallback();
833 void VideoView::PlayAnimation( Dali::Animation animation )
835 if( mIsUnderlay && mSyncMode == Dali::VideoSyncMode::ENABLED )
837 mVideoPlayer.StartSynchronization();
838 animation.FinishedSignal().Connect( this, &VideoView::OnAnimationFinished );
843 Dali::Shader VideoView::CreateShader()
845 std::string fragmentShader = "#extension GL_OES_EGL_image_external:require\n";
846 std::string vertexShader;
847 std::string customFragmentShader;
848 bool checkShader = false;
850 if( !mEffectPropertyMap.Empty() )
852 Property::Value* vertexShaderValue = mEffectPropertyMap.Find( CUSTOM_VERTEX_SHADER );
853 if( vertexShaderValue )
855 checkShader = GetStringFromProperty( *vertexShaderValue, vertexShader );
858 if( !vertexShaderValue || !checkShader )
860 vertexShader = VERTEX_SHADER_TEXTURE;
863 Property::Value* fragmentShaderValue = mEffectPropertyMap.Find( CUSTOM_FRAGMENT_SHADER );
864 if( fragmentShaderValue )
866 checkShader = GetStringFromProperty( *fragmentShaderValue, customFragmentShader );
870 fragmentShader = customFragmentShader;
874 if( !fragmentShaderValue || !checkShader )
876 fragmentShader += FRAGMENT_SHADER_TEXTURE;
881 vertexShader = VERTEX_SHADER_TEXTURE;
882 fragmentShader += FRAGMENT_SHADER_TEXTURE;
885 return Dali::Shader::New( vertexShader, fragmentShader );
888 bool VideoView::GetStringFromProperty( const Dali::Property::Value& value, std::string& output )
890 bool extracted = false;
891 if( value.Get( output ) )
899 void VideoView::ApplyBackupProperties()
901 Property::Map::SizeType pos = 0;
902 Property::Map::SizeType count = mPropertyBackup.Count();
904 for( ; pos < count; pos++ )
906 KeyValuePair property = mPropertyBackup.GetKeyValue( pos );
908 SetPropertyInternal( property.first.indexKey, property.second );
912 void VideoView::FrameRenderCallback( int frameID )
915 if( frameID == mFrameID )
917 mVideoPlayer.FinishSynchronization();
922 void VideoView::SetFrameRenderCallback()
925 DevelWindow::AddFrameRenderedCallback( DevelWindow::Get( Self() ),
926 std::unique_ptr< CallbackBase >( MakeCallback( this, &VideoView::FrameRenderCallback ) ), mFrameID );
929 } // namespace Internal
931 } // namespace toolkit