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>
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 highp 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 highp 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( Dali::VideoSyncMode syncMode )
135 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
136 mCurrentVideoPlayPosition( 0 ),
140 mSyncMode( syncMode )
144 VideoView::~VideoView()
148 Toolkit::VideoView VideoView::New( VideoSyncMode syncMode )
150 VideoView* impl = new VideoView( syncMode );
151 Toolkit::VideoView handle = Toolkit::VideoView( *impl );
153 impl->mVideoPlayer = Dali::VideoPlayer::New( impl->Self(), syncMode );
158 void VideoView::OnInitialize()
160 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
163 void VideoView::SetUrl( const std::string& url )
166 mPropertyMap.Clear();
168 mVideoPlayer.SetUrl( mUrl );
171 void VideoView::SetPropertyMap( Property::Map map )
175 Property::Value* target = map.Find( RENDERING_TARGET );
176 std::string targetType;
178 if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
181 SetWindowSurfaceTarget();
183 else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
186 SetNativeImageTarget();
190 Property::Value* shaderValue;
193 shaderValue = map.Find( CUSTOM_SHADER );
197 Property::Map* shaderMap = shaderValue->GetMap();
200 mEffectPropertyMap = *shaderMap;
205 if( mTextureRenderer && !mEffectPropertyMap.Empty() )
207 Dali::Shader shader = CreateShader();
208 mTextureRenderer.SetShader( shader );
214 std::string VideoView::GetUrl()
219 void VideoView::SetLooping( bool looping )
221 mVideoPlayer.SetLooping( looping );
224 bool VideoView::IsLooping()
226 return mVideoPlayer.IsLooping();
229 void VideoView::Play()
231 if( mOverlayRenderer )
233 Self().AddRenderer( mOverlayRenderer );
240 void VideoView::Pause()
242 mVideoPlayer.Pause();
246 void VideoView::Stop()
252 void VideoView::Forward( int millisecond )
254 int curPos = mVideoPlayer.GetPlayPosition();
256 int nextPos = curPos + millisecond;
258 mVideoPlayer.SetPlayPosition( nextPos );
261 void VideoView::Backward( int millisecond )
263 int curPos = mVideoPlayer.GetPlayPosition();
265 int nextPos = curPos - millisecond;
266 nextPos = ( nextPos < 0 )? 0: nextPos;
268 mVideoPlayer.SetPlayPosition( nextPos );
271 void VideoView::SetMute( bool mute )
273 mVideoPlayer.SetMute( mute );
276 bool VideoView::IsMuted()
278 return mVideoPlayer.IsMuted();
281 void VideoView::SetVolume( float left, float right )
283 mVideoPlayer.SetVolume( left, right );
286 void VideoView::GetVolume( float& left, float& right )
288 mVideoPlayer.GetVolume( left, right );
291 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
293 return mFinishedSignal;
296 void VideoView::EmitSignalFinish()
298 if( mOverlayRenderer )
300 Self().RemoveRenderer( mOverlayRenderer );
303 if ( !mFinishedSignal.Empty() )
305 Dali::Toolkit::VideoView handle( GetOwner() );
306 mFinishedSignal.Emit( handle );
310 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
314 Dali::BaseHandle handle( object );
315 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
322 VideoView& impl = GetImpl( videoView );
324 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
329 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
334 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
339 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
342 if( attributes["videoForward"].Get( millisecond ) )
344 impl.Forward( millisecond );
348 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
351 if( attributes["videoBackward"].Get( millisecond ) )
353 impl.Backward( millisecond );
361 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
363 Dali::BaseHandle handle( object );
365 bool connected( true );
366 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
368 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
370 videoView.FinishedSignal().Connect( tracker, functor );
374 // signalName does not match any signal
381 void VideoView::SetPropertyInternal( Property::Index index, const Property::Value& value )
385 case Toolkit::VideoView::Property::VIDEO:
387 std::string videoUrl;
390 if( value.Get( videoUrl ) )
394 else if( value.Get( map ) )
396 SetPropertyMap( map );
400 case Toolkit::VideoView::Property::LOOPING:
403 if( value.Get( looping ) )
405 SetLooping( looping );
409 case Toolkit::VideoView::Property::MUTED:
412 if( value.Get( mute ) )
418 case Toolkit::VideoView::Property::VOLUME:
422 if( value.Get( map ) )
424 Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
425 Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
426 if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
428 SetVolume( left, right );
433 case Toolkit::VideoView::Property::UNDERLAY:
436 if( value.Get( underlay ) )
438 SetUnderlay( underlay );
442 case Toolkit::VideoView::Property::PLAY_POSITION:
445 if( value.Get( pos ) )
447 SetPlayPosition( pos );
451 case Toolkit::VideoView::Property::DISPLAY_MODE:
454 if( value.Get( mode ) )
456 SetDisplayMode( mode );
463 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
465 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
469 VideoView& impl = GetImpl( videoView );
471 impl.SetPropertyInternal( index, value );
473 if( index != Toolkit::VideoView::Property::UNDERLAY )
476 // These values will be used when underlay mode is changed.
477 impl.mPropertyBackup[index] = value;
482 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
484 Property::Value value;
485 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
489 VideoView& impl = GetImpl( videoView );
491 switch( propertyIndex )
493 case Toolkit::VideoView::Property::VIDEO:
495 if( !impl.mUrl.empty() )
499 else if( !impl.mPropertyMap.Empty() )
501 value = impl.mPropertyMap;
505 case Toolkit::VideoView::Property::LOOPING:
507 value = impl.IsLooping();
510 case Toolkit::VideoView::Property::MUTED:
512 value = impl.IsMuted();
515 case Toolkit::VideoView::Property::VOLUME:
520 impl.GetVolume( left, right );
521 map.Insert( VOLUME_LEFT, left );
522 map.Insert( VOLUME_RIGHT, right );
526 case Toolkit::VideoView::Property::UNDERLAY:
528 value = impl.IsUnderlay();
531 case Toolkit::VideoView::Property::PLAY_POSITION:
533 value = impl.GetPlayPosition();
536 case Toolkit::VideoView::Property::DISPLAY_MODE:
538 value = impl.GetDisplayMode();
547 void VideoView::SetDepthIndex( int depthIndex )
549 if( mTextureRenderer )
551 mTextureRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, depthIndex );
555 void VideoView::OnSceneConnection( int depth )
557 Control::OnSceneConnection( depth );
561 SetWindowSurfaceTarget();
565 void VideoView::OnSceneDisconnection()
567 Control::OnSceneDisconnection();
570 void VideoView::OnSizeSet( const Vector3& targetSize )
572 if( mIsUnderlay && mSyncMode == Dali::VideoSyncMode::ENABLED )
574 SetFrameRenderCallback();
575 mVideoPlayer.StartSynchronization();
577 Control::OnSizeSet( targetSize );
580 Vector3 VideoView::GetNaturalSize()
583 size.x = mVideoSize.GetWidth();
584 size.y = mVideoSize.GetHeight();
586 if( size.x > 0 && size.y > 0 )
588 size.z = std::min( size.x, size.y );
593 return Control::GetNaturalSize();
597 float VideoView::GetHeightForWidth( float width )
599 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
601 return GetHeightForWidthBase( width );
605 return Control::GetHeightForWidthBase( width );
609 float VideoView::GetWidthForHeight( float height )
611 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
613 return GetWidthForHeightBase( height );
617 return Control::GetWidthForHeightBase( height );
621 void VideoView::SetWindowSurfaceTarget()
625 if( !self.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
627 // When the control is off the stage, it does not have Window.
631 int curPos = mVideoPlayer.GetPlayPosition();
635 mVideoPlayer.Pause();
638 mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
639 mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
640 mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
641 mPositionUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
642 mSizeUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
643 mScaleUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
645 if( mTextureRenderer )
647 self.RemoveRenderer( mTextureRenderer );
650 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
651 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle( self ) );
653 ApplyBackupProperties();
655 if( !mOverlayRenderer )
657 // For underlay rendering mode, video display area have to be transparent.
658 Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
659 Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
660 mOverlayRenderer = Renderer::New( geometry, shader );
661 mOverlayRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::OFF );
671 mVideoPlayer.SetPlayPosition( curPos );
675 void VideoView::SetNativeImageTarget()
677 if( mVideoPlayer.IsVideoTextureSupported() == false )
679 DALI_LOG_ERROR( "Platform doesn't support decoded video frame images\n" );
686 mVideoPlayer.Pause();
689 Actor self( Self() );
691 if( mOverlayRenderer )
693 self.RemoveRenderer( mOverlayRenderer );
695 mOverlayRenderer.Reset();
698 self.RemovePropertyNotification( mPositionUpdateNotification );
699 self.RemovePropertyNotification( mSizeUpdateNotification );
700 self.RemovePropertyNotification( mScaleUpdateNotification );
702 int curPos = mVideoPlayer.GetPlayPosition();
705 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
706 mNativeTexture = Dali::Texture::New( *nativeImageSourcePtr );
708 if( !mTextureRenderer )
710 Dali::Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
711 Dali::Shader shader = CreateShader();
712 Dali::TextureSet textureSet = Dali::TextureSet::New();
713 textureSet.SetTexture( 0u, mNativeTexture );
715 mTextureRenderer = Renderer::New( geometry, shader );
716 mTextureRenderer.SetTextures( textureSet );
720 Dali::TextureSet textureSet = mTextureRenderer.GetTextures();
721 textureSet.SetTexture( 0u, mNativeTexture );
723 Self().AddRenderer( mTextureRenderer );
725 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
726 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
728 ApplyBackupProperties();
737 mVideoPlayer.SetPlayPosition( curPos );
741 void VideoView::UpdateDisplayArea( Dali::PropertyNotification& source )
743 // If mSyncMode is enabled, Video player's size and poistion is updated in Video player's constraint.
744 // Because video view and player should be work syncronization.
745 if( !mIsUnderlay || mSyncMode == Dali::VideoSyncMode::ENABLED )
750 Actor self( Self() );
752 bool positionUsesAnchorPoint = self.GetProperty( Actor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >();
753 Vector3 actorSize = self.GetCurrentProperty< Vector3 >( Actor::Property::SIZE ) * self.GetCurrentProperty< Vector3 >( Actor::Property::SCALE );
754 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentProperty< Vector3 >( Actor::Property::ANCHOR_POINT ) : AnchorPoint::TOP_LEFT );
756 Vector2 screenPosition = self.GetProperty( Actor::Property::SCREEN_POSITION ).Get< Vector2 >();
758 mDisplayArea.x = screenPosition.x - anchorPointOffSet.x;
759 mDisplayArea.y = screenPosition.y - anchorPointOffSet.y;
760 mDisplayArea.width = actorSize.x;
761 mDisplayArea.height = actorSize.y;
763 mVideoPlayer.SetDisplayArea( mDisplayArea );
766 void VideoView::SetUnderlay( bool set )
768 if( set != mIsUnderlay )
774 SetWindowSurfaceTarget();
778 SetNativeImageTarget();
785 bool VideoView::IsUnderlay()
790 void VideoView::SetSWCodec( bool on )
792 // If setting SW or HW type is failed , video-view shows video by default codec type.
793 // The default codec type is selected by platform.
796 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::SW );
800 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::HW );
804 int VideoView::GetPlayPosition()
806 return mVideoPlayer.GetPlayPosition();
809 void VideoView::SetPlayPosition( int pos )
811 mVideoPlayer.SetPlayPosition( pos );
814 void VideoView::SetDisplayMode( int mode )
816 mVideoPlayer.SetDisplayMode( static_cast< Dali::VideoPlayerPlugin::DisplayMode::Type >( mode ) );
819 int VideoView::GetDisplayMode() const
821 return static_cast< int >( mVideoPlayer.GetDisplayMode() );
824 Any VideoView::GetMediaPlayer()
826 return mVideoPlayer.GetMediaPlayer();
829 void VideoView::OnAnimationFinished( Animation& animation )
832 SetFrameRenderCallback();
835 void VideoView::PlayAnimation( Dali::Animation animation )
837 if( mIsUnderlay && mSyncMode == Dali::VideoSyncMode::ENABLED )
839 mVideoPlayer.StartSynchronization();
840 animation.FinishedSignal().Connect( this, &VideoView::OnAnimationFinished );
845 Dali::Shader VideoView::CreateShader()
847 std::string fragmentShader = "#extension GL_OES_EGL_image_external:require\n";
848 std::string vertexShader;
849 std::string customFragmentShader;
850 bool checkShader = false;
852 if( !mEffectPropertyMap.Empty() )
854 Property::Value* vertexShaderValue = mEffectPropertyMap.Find( CUSTOM_VERTEX_SHADER );
855 if( vertexShaderValue )
857 checkShader = GetStringFromProperty( *vertexShaderValue, vertexShader );
860 if( !vertexShaderValue || !checkShader )
862 vertexShader = VERTEX_SHADER_TEXTURE;
865 Property::Value* fragmentShaderValue = mEffectPropertyMap.Find( CUSTOM_FRAGMENT_SHADER );
866 if( fragmentShaderValue )
868 checkShader = GetStringFromProperty( *fragmentShaderValue, customFragmentShader );
872 fragmentShader = customFragmentShader;
876 if( !fragmentShaderValue || !checkShader )
878 fragmentShader += FRAGMENT_SHADER_TEXTURE;
883 vertexShader = VERTEX_SHADER_TEXTURE;
884 fragmentShader += FRAGMENT_SHADER_TEXTURE;
887 return Dali::Shader::New( vertexShader, fragmentShader );
890 bool VideoView::GetStringFromProperty( const Dali::Property::Value& value, std::string& output )
892 bool extracted = false;
893 if( value.Get( output ) )
901 void VideoView::ApplyBackupProperties()
903 Property::Map::SizeType pos = 0;
904 Property::Map::SizeType count = mPropertyBackup.Count();
906 for( ; pos < count; pos++ )
908 KeyValuePair property = mPropertyBackup.GetKeyValue( pos );
910 SetPropertyInternal( property.first.indexKey, property.second );
914 void VideoView::FrameRenderCallback( int frameID )
917 if( frameID == mFrameID )
919 mVideoPlayer.FinishSynchronization();
924 void VideoView::SetFrameRenderCallback()
927 DevelWindow::AddFrameRenderedCallback( DevelWindow::Get( Self() ),
928 std::unique_ptr< CallbackBase >( MakeCallback( this, &VideoView::FrameRenderCallback ) ), mFrameID );
931 } // namespace Internal
933 } // namespace toolkit