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>
37 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
53 return Toolkit::VideoView::New();
56 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::VideoView, Toolkit::Control, Create );
58 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "video", MAP, VIDEO )
59 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "looping", BOOLEAN, LOOPING )
60 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "muted", BOOLEAN, MUTED )
61 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "volume", MAP, VOLUME )
62 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "underlay", BOOLEAN, UNDERLAY )
63 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "playPosition", INTEGER, PLAY_POSITION )
64 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "displayMode", INTEGER, DISPLAY_MODE )
66 DALI_SIGNAL_REGISTRATION( Toolkit, VideoView, "finished", FINISHED_SIGNAL )
68 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "play", ACTION_VIDEOVIEW_PLAY )
69 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "pause", ACTION_VIDEOVIEW_PAUSE )
70 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "stop", ACTION_VIDEOVIEW_STOP )
71 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "forward", ACTION_VIDEOVIEW_FORWARD )
72 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "backward", ACTION_VIDEOVIEW_BACKWARD )
74 DALI_TYPE_REGISTRATION_END()
76 const char* const VOLUME_LEFT( "volumeLeft" );
77 const char* const VOLUME_RIGHT( "volumeRight" );
79 // 3.0 TC uses RENDERING_TARGET. It should be removed in next release
80 const char* const RENDERING_TARGET( "renderingTarget" );
81 const char* const WINDOW_SURFACE_TARGET( "windowSurfaceTarget" );
82 const char* const NATIVE_IMAGE_TARGET( "nativeImageTarget" );
84 const char* const CUSTOM_SHADER( "shader" );
85 const char* const CUSTOM_VERTEX_SHADER( "vertexShader" );
86 const char* const CUSTOM_FRAGMENT_SHADER( "fragmentShader" );
87 const char* const DEFAULT_SAMPLER_TYPE_NAME( "sampler2D" );
88 const char* const CUSTOM_SAMPLER_TYPE_NAME( "samplerExternalOES" );
90 } // anonymous namepsace
92 VideoView::VideoView( Dali::VideoSyncMode syncMode )
93 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
94 mCurrentVideoPlayPosition( 0 ),
102 VideoView::~VideoView()
106 Toolkit::VideoView VideoView::New( VideoSyncMode syncMode )
108 VideoView* impl = new VideoView( syncMode );
109 Toolkit::VideoView handle = Toolkit::VideoView( *impl );
111 impl->mVideoPlayer = Dali::VideoPlayer::New( impl->Self(), syncMode );
116 void VideoView::OnInitialize()
118 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
120 DevelControl::SetAccessibilityConstructor( Self(), []( Dali::Actor actor ) {
121 return std::unique_ptr< Dali::Accessibility::Accessible >(
122 new Control::Impl::AccessibleImpl( actor, Dali::Accessibility::Role::VIDEO ));
126 void VideoView::SetUrl( const std::string& url )
129 mPropertyMap.Clear();
131 mVideoPlayer.SetUrl( mUrl );
134 void VideoView::SetPropertyMap( Property::Map map )
138 Property::Value* target = map.Find( RENDERING_TARGET );
139 std::string targetType;
141 if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
144 SetWindowSurfaceTarget();
146 else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
149 SetNativeImageTarget();
153 Property::Value* shaderValue;
156 shaderValue = map.Find( CUSTOM_SHADER );
160 Property::Map* shaderMap = shaderValue->GetMap();
163 mEffectPropertyMap = *shaderMap;
168 if( mTextureRenderer && !mEffectPropertyMap.Empty() )
170 Dali::Shader shader = CreateShader();
171 mTextureRenderer.SetShader( shader );
177 std::string VideoView::GetUrl()
182 void VideoView::SetLooping( bool looping )
184 mVideoPlayer.SetLooping( looping );
187 bool VideoView::IsLooping()
189 return mVideoPlayer.IsLooping();
192 void VideoView::Play()
198 void VideoView::Pause()
200 mVideoPlayer.Pause();
204 void VideoView::Stop()
210 void VideoView::Forward( int millisecond )
212 int curPos = mVideoPlayer.GetPlayPosition();
214 int nextPos = curPos + millisecond;
216 mVideoPlayer.SetPlayPosition( nextPos );
219 void VideoView::Backward( int millisecond )
221 int curPos = mVideoPlayer.GetPlayPosition();
223 int nextPos = curPos - millisecond;
224 nextPos = ( nextPos < 0 )? 0: nextPos;
226 mVideoPlayer.SetPlayPosition( nextPos );
229 void VideoView::SetMute( bool mute )
231 mVideoPlayer.SetMute( mute );
234 bool VideoView::IsMuted()
236 return mVideoPlayer.IsMuted();
239 void VideoView::SetVolume( float left, float right )
241 mVideoPlayer.SetVolume( left, right );
244 void VideoView::GetVolume( float& left, float& right )
246 mVideoPlayer.GetVolume( left, right );
249 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
251 return mFinishedSignal;
254 void VideoView::EmitSignalFinish()
256 if ( !mFinishedSignal.Empty() )
258 Dali::Toolkit::VideoView handle( GetOwner() );
259 mFinishedSignal.Emit( handle );
263 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
267 Dali::BaseHandle handle( object );
268 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
275 VideoView& impl = GetImpl( videoView );
277 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
282 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
287 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
292 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
295 if( attributes["videoForward"].Get( millisecond ) )
297 impl.Forward( millisecond );
301 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
304 if( attributes["videoBackward"].Get( millisecond ) )
306 impl.Backward( millisecond );
314 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
316 Dali::BaseHandle handle( object );
318 bool connected( true );
319 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
321 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
323 videoView.FinishedSignal().Connect( tracker, functor );
327 // signalName does not match any signal
334 void VideoView::SetPropertyInternal( Property::Index index, const Property::Value& value )
338 case Toolkit::VideoView::Property::VIDEO:
340 std::string videoUrl;
343 if( value.Get( videoUrl ) )
347 else if( value.Get( map ) )
349 SetPropertyMap( map );
353 case Toolkit::VideoView::Property::LOOPING:
356 if( value.Get( looping ) )
358 SetLooping( looping );
362 case Toolkit::VideoView::Property::MUTED:
365 if( value.Get( mute ) )
371 case Toolkit::VideoView::Property::VOLUME:
375 if( value.Get( map ) )
377 Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
378 Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
379 if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
381 SetVolume( left, right );
386 case Toolkit::VideoView::Property::UNDERLAY:
389 if( value.Get( underlay ) )
391 SetUnderlay( underlay );
395 case Toolkit::VideoView::Property::PLAY_POSITION:
398 if( value.Get( pos ) )
400 SetPlayPosition( pos );
404 case Toolkit::VideoView::Property::DISPLAY_MODE:
407 if( value.Get( mode ) )
409 SetDisplayMode( mode );
416 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
418 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
422 VideoView& impl = GetImpl( videoView );
424 impl.SetPropertyInternal( index, value );
426 if( index != Toolkit::VideoView::Property::UNDERLAY )
429 // These values will be used when underlay mode is changed.
430 impl.mPropertyBackup[index] = value;
435 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
437 Property::Value value;
438 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
442 VideoView& impl = GetImpl( videoView );
444 switch( propertyIndex )
446 case Toolkit::VideoView::Property::VIDEO:
448 if( !impl.mUrl.empty() )
452 else if( !impl.mPropertyMap.Empty() )
454 value = impl.mPropertyMap;
458 case Toolkit::VideoView::Property::LOOPING:
460 value = impl.IsLooping();
463 case Toolkit::VideoView::Property::MUTED:
465 value = impl.IsMuted();
468 case Toolkit::VideoView::Property::VOLUME:
473 impl.GetVolume( left, right );
474 map.Insert( VOLUME_LEFT, left );
475 map.Insert( VOLUME_RIGHT, right );
479 case Toolkit::VideoView::Property::UNDERLAY:
481 value = impl.IsUnderlay();
484 case Toolkit::VideoView::Property::PLAY_POSITION:
486 value = impl.GetPlayPosition();
489 case Toolkit::VideoView::Property::DISPLAY_MODE:
491 value = impl.GetDisplayMode();
500 void VideoView::SetDepthIndex( int depthIndex )
502 if( mTextureRenderer )
504 mTextureRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, depthIndex );
508 void VideoView::OnSceneConnection( int depth )
510 Control::OnSceneConnection( depth );
514 SetWindowSurfaceTarget();
518 void VideoView::OnSceneDisconnection()
520 Control::OnSceneDisconnection();
523 void VideoView::OnSizeSet( const Vector3& targetSize )
525 if( mIsUnderlay && mSyncMode == Dali::VideoSyncMode::ENABLED )
527 SetFrameRenderCallback();
528 mVideoPlayer.StartSynchronization();
530 Control::OnSizeSet( targetSize );
533 Vector3 VideoView::GetNaturalSize()
536 size.x = mVideoSize.GetWidth();
537 size.y = mVideoSize.GetHeight();
539 if( size.x > 0 && size.y > 0 )
541 size.z = std::min( size.x, size.y );
546 return Control::GetNaturalSize();
550 float VideoView::GetHeightForWidth( float width )
552 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
554 return GetHeightForWidthBase( width );
558 return Control::GetHeightForWidthBase( width );
562 float VideoView::GetWidthForHeight( float height )
564 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
566 return GetWidthForHeightBase( height );
570 return Control::GetWidthForHeightBase( height );
574 void VideoView::SetWindowSurfaceTarget()
578 if( !self.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
580 // When the control is off the stage, it does not have Window.
584 int curPos = mVideoPlayer.GetPlayPosition();
588 mVideoPlayer.Pause();
591 mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
592 mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
593 mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
594 mPositionUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
595 mSizeUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
596 mScaleUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
598 if( mTextureRenderer )
600 self.RemoveRenderer( mTextureRenderer );
603 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
604 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle( self ) );
606 ApplyBackupProperties();
608 if( !mOverlayRenderer )
610 // For underlay rendering mode, video display area have to be transparent.
611 Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
612 Shader shader = Shader::New( SHADER_VIDEO_VIEW_VERT, SHADER_VIDEO_VIEW_FRAG );
613 mOverlayRenderer = Renderer::New( geometry, shader );
614 mOverlayRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::OFF );
616 Self().AddRenderer( mOverlayRenderer );
625 mVideoPlayer.SetPlayPosition( curPos );
629 void VideoView::SetNativeImageTarget()
631 if( mVideoPlayer.IsVideoTextureSupported() == false )
633 DALI_LOG_ERROR( "Platform doesn't support decoded video frame images\n" );
640 mVideoPlayer.Pause();
643 Actor self( Self() );
645 if( mOverlayRenderer )
647 self.RemoveRenderer( mOverlayRenderer );
649 mOverlayRenderer.Reset();
652 self.RemovePropertyNotification( mPositionUpdateNotification );
653 self.RemovePropertyNotification( mSizeUpdateNotification );
654 self.RemovePropertyNotification( mScaleUpdateNotification );
656 int curPos = mVideoPlayer.GetPlayPosition();
659 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
660 mNativeTexture = Dali::Texture::New( *nativeImageSourcePtr );
662 if( !mTextureRenderer )
664 Dali::Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
665 Dali::Shader shader = CreateShader();
666 Dali::TextureSet textureSet = Dali::TextureSet::New();
667 textureSet.SetTexture( 0u, mNativeTexture );
669 mTextureRenderer = Renderer::New( geometry, shader );
670 mTextureRenderer.SetTextures( textureSet );
674 Dali::TextureSet textureSet = mTextureRenderer.GetTextures();
675 textureSet.SetTexture( 0u, mNativeTexture );
677 Self().AddRenderer( mTextureRenderer );
679 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
680 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
682 ApplyBackupProperties();
691 mVideoPlayer.SetPlayPosition( curPos );
695 void VideoView::UpdateDisplayArea( Dali::PropertyNotification& source )
697 // If mSyncMode is enabled, Video player's size and poistion is updated in Video player's constraint.
698 // Because video view and player should be work syncronization.
699 if( !mIsUnderlay || mSyncMode == Dali::VideoSyncMode::ENABLED )
704 Actor self( Self() );
706 bool positionUsesAnchorPoint = self.GetProperty( Actor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >();
707 Vector3 actorSize = self.GetCurrentProperty< Vector3 >( Actor::Property::SIZE ) * self.GetCurrentProperty< Vector3 >( Actor::Property::SCALE );
708 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentProperty< Vector3 >( Actor::Property::ANCHOR_POINT ) : AnchorPoint::TOP_LEFT );
710 Vector2 screenPosition = self.GetProperty( Actor::Property::SCREEN_POSITION ).Get< Vector2 >();
712 mDisplayArea.x = screenPosition.x - anchorPointOffSet.x;
713 mDisplayArea.y = screenPosition.y - anchorPointOffSet.y;
714 mDisplayArea.width = actorSize.x;
715 mDisplayArea.height = actorSize.y;
717 mVideoPlayer.SetDisplayArea( mDisplayArea );
720 void VideoView::SetUnderlay( bool set )
722 if( set != mIsUnderlay )
728 SetWindowSurfaceTarget();
732 SetNativeImageTarget();
739 bool VideoView::IsUnderlay()
744 void VideoView::SetSWCodec( bool on )
746 // If setting SW or HW type is failed , video-view shows video by default codec type.
747 // The default codec type is selected by platform.
750 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::SW );
754 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::HW );
758 int VideoView::GetPlayPosition()
760 return mVideoPlayer.GetPlayPosition();
763 void VideoView::SetPlayPosition( int pos )
765 mVideoPlayer.SetPlayPosition( pos );
768 void VideoView::SetDisplayMode( int mode )
770 mVideoPlayer.SetDisplayMode( static_cast< Dali::VideoPlayerPlugin::DisplayMode::Type >( mode ) );
773 int VideoView::GetDisplayMode() const
775 return static_cast< int >( mVideoPlayer.GetDisplayMode() );
778 Any VideoView::GetMediaPlayer()
780 return mVideoPlayer.GetMediaPlayer();
783 void VideoView::OnAnimationFinished( Animation& animation )
786 SetFrameRenderCallback();
789 void VideoView::PlayAnimation( Dali::Animation animation )
791 if( mIsUnderlay && mSyncMode == Dali::VideoSyncMode::ENABLED )
793 mVideoPlayer.StartSynchronization();
794 animation.FinishedSignal().Connect( this, &VideoView::OnAnimationFinished );
799 Dali::Shader VideoView::CreateShader()
801 std::string fragmentShader = "#extension GL_OES_EGL_image_external:require\n";
802 std::string vertexShader;
803 std::string customFragmentShader;
804 bool checkShader = false;
806 if( !mEffectPropertyMap.Empty() )
808 Property::Value* vertexShaderValue = mEffectPropertyMap.Find( CUSTOM_VERTEX_SHADER );
809 if( vertexShaderValue )
811 checkShader = GetStringFromProperty( *vertexShaderValue, vertexShader );
814 if( !vertexShaderValue || !checkShader )
816 vertexShader = SHADER_VIDEO_VIEW_TEXTURE_VERT.data();
819 Property::Value* fragmentShaderValue = mEffectPropertyMap.Find( CUSTOM_FRAGMENT_SHADER );
820 if( fragmentShaderValue )
822 checkShader = GetStringFromProperty( *fragmentShaderValue, customFragmentShader );
826 fragmentShader = customFragmentShader;
830 if( !fragmentShaderValue || !checkShader )
832 fragmentShader += SHADER_VIDEO_VIEW_TEXTURE_FRAG.data();
837 vertexShader = SHADER_VIDEO_VIEW_TEXTURE_VERT.data();
838 fragmentShader += SHADER_VIDEO_VIEW_TEXTURE_FRAG.data();
841 return Dali::Shader::New( vertexShader, fragmentShader );
844 bool VideoView::GetStringFromProperty( const Dali::Property::Value& value, std::string& output )
846 bool extracted = false;
847 if( value.Get( output ) )
855 void VideoView::ApplyBackupProperties()
857 Property::Map::SizeType pos = 0;
858 Property::Map::SizeType count = mPropertyBackup.Count();
860 for( ; pos < count; pos++ )
862 KeyValuePair property = mPropertyBackup.GetKeyValue( pos );
864 SetPropertyInternal( property.first.indexKey, property.second );
868 void VideoView::FrameRenderCallback( int frameID )
871 if( frameID == mFrameID )
873 mVideoPlayer.FinishSynchronization();
878 void VideoView::SetFrameRenderCallback()
881 DevelWindow::AddFrameRenderedCallback( DevelWindow::Get( Self() ),
882 std::unique_ptr< CallbackBase >( MakeCallback( this, &VideoView::FrameRenderCallback ) ), mFrameID );
885 } // namespace Internal
887 } // namespace toolkit