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()
235 void VideoView::Pause()
237 mVideoPlayer.Pause();
241 void VideoView::Stop()
247 void VideoView::Forward( int millisecond )
249 int curPos = mVideoPlayer.GetPlayPosition();
251 int nextPos = curPos + millisecond;
253 mVideoPlayer.SetPlayPosition( nextPos );
256 void VideoView::Backward( int millisecond )
258 int curPos = mVideoPlayer.GetPlayPosition();
260 int nextPos = curPos - millisecond;
261 nextPos = ( nextPos < 0 )? 0: nextPos;
263 mVideoPlayer.SetPlayPosition( nextPos );
266 void VideoView::SetMute( bool mute )
268 mVideoPlayer.SetMute( mute );
271 bool VideoView::IsMuted()
273 return mVideoPlayer.IsMuted();
276 void VideoView::SetVolume( float left, float right )
278 mVideoPlayer.SetVolume( left, right );
281 void VideoView::GetVolume( float& left, float& right )
283 mVideoPlayer.GetVolume( left, right );
286 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
288 return mFinishedSignal;
291 void VideoView::EmitSignalFinish()
293 if ( !mFinishedSignal.Empty() )
295 Dali::Toolkit::VideoView handle( GetOwner() );
296 mFinishedSignal.Emit( handle );
300 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
304 Dali::BaseHandle handle( object );
305 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
312 VideoView& impl = GetImpl( videoView );
314 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
319 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
324 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
329 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
332 if( attributes["videoForward"].Get( millisecond ) )
334 impl.Forward( millisecond );
338 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
341 if( attributes["videoBackward"].Get( millisecond ) )
343 impl.Backward( millisecond );
351 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
353 Dali::BaseHandle handle( object );
355 bool connected( true );
356 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
358 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
360 videoView.FinishedSignal().Connect( tracker, functor );
364 // signalName does not match any signal
371 void VideoView::SetPropertyInternal( Property::Index index, const Property::Value& value )
375 case Toolkit::VideoView::Property::VIDEO:
377 std::string videoUrl;
380 if( value.Get( videoUrl ) )
384 else if( value.Get( map ) )
386 SetPropertyMap( map );
390 case Toolkit::VideoView::Property::LOOPING:
393 if( value.Get( looping ) )
395 SetLooping( looping );
399 case Toolkit::VideoView::Property::MUTED:
402 if( value.Get( mute ) )
408 case Toolkit::VideoView::Property::VOLUME:
412 if( value.Get( map ) )
414 Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
415 Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
416 if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
418 SetVolume( left, right );
423 case Toolkit::VideoView::Property::UNDERLAY:
426 if( value.Get( underlay ) )
428 SetUnderlay( underlay );
432 case Toolkit::VideoView::Property::PLAY_POSITION:
435 if( value.Get( pos ) )
437 SetPlayPosition( pos );
441 case Toolkit::VideoView::Property::DISPLAY_MODE:
444 if( value.Get( mode ) )
446 SetDisplayMode( mode );
453 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
455 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
459 VideoView& impl = GetImpl( videoView );
461 impl.SetPropertyInternal( index, value );
463 if( index != Toolkit::VideoView::Property::UNDERLAY )
466 // These values will be used when underlay mode is changed.
467 impl.mPropertyBackup[index] = value;
472 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
474 Property::Value value;
475 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
479 VideoView& impl = GetImpl( videoView );
481 switch( propertyIndex )
483 case Toolkit::VideoView::Property::VIDEO:
485 if( !impl.mUrl.empty() )
489 else if( !impl.mPropertyMap.Empty() )
491 value = impl.mPropertyMap;
495 case Toolkit::VideoView::Property::LOOPING:
497 value = impl.IsLooping();
500 case Toolkit::VideoView::Property::MUTED:
502 value = impl.IsMuted();
505 case Toolkit::VideoView::Property::VOLUME:
510 impl.GetVolume( left, right );
511 map.Insert( VOLUME_LEFT, left );
512 map.Insert( VOLUME_RIGHT, right );
516 case Toolkit::VideoView::Property::UNDERLAY:
518 value = impl.IsUnderlay();
521 case Toolkit::VideoView::Property::PLAY_POSITION:
523 value = impl.GetPlayPosition();
526 case Toolkit::VideoView::Property::DISPLAY_MODE:
528 value = impl.GetDisplayMode();
537 void VideoView::SetDepthIndex( int depthIndex )
539 if( mTextureRenderer )
541 mTextureRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, depthIndex );
545 void VideoView::OnSceneConnection( int depth )
547 Control::OnSceneConnection( depth );
551 SetWindowSurfaceTarget();
555 void VideoView::OnSceneDisconnection()
557 Control::OnSceneDisconnection();
560 void VideoView::OnSizeSet( const Vector3& targetSize )
562 if( mIsUnderlay && mSyncMode == Dali::VideoSyncMode::ENABLED )
564 SetFrameRenderCallback();
565 mVideoPlayer.StartSynchronization();
567 Control::OnSizeSet( targetSize );
570 Vector3 VideoView::GetNaturalSize()
573 size.x = mVideoSize.GetWidth();
574 size.y = mVideoSize.GetHeight();
576 if( size.x > 0 && size.y > 0 )
578 size.z = std::min( size.x, size.y );
583 return Control::GetNaturalSize();
587 float VideoView::GetHeightForWidth( float width )
589 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
591 return GetHeightForWidthBase( width );
595 return Control::GetHeightForWidthBase( width );
599 float VideoView::GetWidthForHeight( float height )
601 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
603 return GetWidthForHeightBase( height );
607 return Control::GetWidthForHeightBase( height );
611 void VideoView::SetWindowSurfaceTarget()
615 if( !self.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
617 // When the control is off the stage, it does not have Window.
621 int curPos = mVideoPlayer.GetPlayPosition();
625 mVideoPlayer.Pause();
628 mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
629 mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
630 mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
631 mPositionUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
632 mSizeUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
633 mScaleUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
635 if( mTextureRenderer )
637 self.RemoveRenderer( mTextureRenderer );
640 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
641 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle( self ) );
643 ApplyBackupProperties();
645 if( !mOverlayRenderer )
647 // For underlay rendering mode, video display area have to be transparent.
648 Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
649 Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
650 mOverlayRenderer = Renderer::New( geometry, shader );
651 mOverlayRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::OFF );
653 Self().AddRenderer( mOverlayRenderer );
662 mVideoPlayer.SetPlayPosition( curPos );
666 void VideoView::SetNativeImageTarget()
668 if( mVideoPlayer.IsVideoTextureSupported() == false )
670 DALI_LOG_ERROR( "Platform doesn't support decoded video frame images\n" );
677 mVideoPlayer.Pause();
680 Actor self( Self() );
682 if( mOverlayRenderer )
684 self.RemoveRenderer( mOverlayRenderer );
686 mOverlayRenderer.Reset();
689 self.RemovePropertyNotification( mPositionUpdateNotification );
690 self.RemovePropertyNotification( mSizeUpdateNotification );
691 self.RemovePropertyNotification( mScaleUpdateNotification );
693 int curPos = mVideoPlayer.GetPlayPosition();
696 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
697 mNativeTexture = Dali::Texture::New( *nativeImageSourcePtr );
699 if( !mTextureRenderer )
701 Dali::Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
702 Dali::Shader shader = CreateShader();
703 Dali::TextureSet textureSet = Dali::TextureSet::New();
704 textureSet.SetTexture( 0u, mNativeTexture );
706 mTextureRenderer = Renderer::New( geometry, shader );
707 mTextureRenderer.SetTextures( textureSet );
711 Dali::TextureSet textureSet = mTextureRenderer.GetTextures();
712 textureSet.SetTexture( 0u, mNativeTexture );
714 Self().AddRenderer( mTextureRenderer );
716 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
717 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
719 ApplyBackupProperties();
728 mVideoPlayer.SetPlayPosition( curPos );
732 void VideoView::UpdateDisplayArea( Dali::PropertyNotification& source )
734 // If mSyncMode is enabled, Video player's size and poistion is updated in Video player's constraint.
735 // Because video view and player should be work syncronization.
736 if( !mIsUnderlay || mSyncMode == Dali::VideoSyncMode::ENABLED )
741 Actor self( Self() );
743 bool positionUsesAnchorPoint = self.GetProperty( Actor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >();
744 Vector3 actorSize = self.GetCurrentProperty< Vector3 >( Actor::Property::SIZE ) * self.GetCurrentProperty< Vector3 >( Actor::Property::SCALE );
745 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentProperty< Vector3 >( Actor::Property::ANCHOR_POINT ) : AnchorPoint::TOP_LEFT );
747 Vector2 screenPosition = self.GetProperty( Actor::Property::SCREEN_POSITION ).Get< Vector2 >();
749 mDisplayArea.x = screenPosition.x - anchorPointOffSet.x;
750 mDisplayArea.y = screenPosition.y - anchorPointOffSet.y;
751 mDisplayArea.width = actorSize.x;
752 mDisplayArea.height = actorSize.y;
754 mVideoPlayer.SetDisplayArea( mDisplayArea );
757 void VideoView::SetUnderlay( bool set )
759 if( set != mIsUnderlay )
765 SetWindowSurfaceTarget();
769 SetNativeImageTarget();
776 bool VideoView::IsUnderlay()
781 void VideoView::SetSWCodec( bool on )
783 // If setting SW or HW type is failed , video-view shows video by default codec type.
784 // The default codec type is selected by platform.
787 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::SW );
791 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::HW );
795 int VideoView::GetPlayPosition()
797 return mVideoPlayer.GetPlayPosition();
800 void VideoView::SetPlayPosition( int pos )
802 mVideoPlayer.SetPlayPosition( pos );
805 void VideoView::SetDisplayMode( int mode )
807 mVideoPlayer.SetDisplayMode( static_cast< Dali::VideoPlayerPlugin::DisplayMode::Type >( mode ) );
810 int VideoView::GetDisplayMode() const
812 return static_cast< int >( mVideoPlayer.GetDisplayMode() );
815 Any VideoView::GetMediaPlayer()
817 return mVideoPlayer.GetMediaPlayer();
820 void VideoView::OnAnimationFinished( Animation& animation )
823 SetFrameRenderCallback();
826 void VideoView::PlayAnimation( Dali::Animation animation )
828 if( mIsUnderlay && mSyncMode == Dali::VideoSyncMode::ENABLED )
830 mVideoPlayer.StartSynchronization();
831 animation.FinishedSignal().Connect( this, &VideoView::OnAnimationFinished );
836 Dali::Shader VideoView::CreateShader()
838 std::string fragmentShader = "#extension GL_OES_EGL_image_external:require\n";
839 std::string vertexShader;
840 std::string customFragmentShader;
841 bool checkShader = false;
843 if( !mEffectPropertyMap.Empty() )
845 Property::Value* vertexShaderValue = mEffectPropertyMap.Find( CUSTOM_VERTEX_SHADER );
846 if( vertexShaderValue )
848 checkShader = GetStringFromProperty( *vertexShaderValue, vertexShader );
851 if( !vertexShaderValue || !checkShader )
853 vertexShader = VERTEX_SHADER_TEXTURE;
856 Property::Value* fragmentShaderValue = mEffectPropertyMap.Find( CUSTOM_FRAGMENT_SHADER );
857 if( fragmentShaderValue )
859 checkShader = GetStringFromProperty( *fragmentShaderValue, customFragmentShader );
863 fragmentShader = customFragmentShader;
867 if( !fragmentShaderValue || !checkShader )
869 fragmentShader += FRAGMENT_SHADER_TEXTURE;
874 vertexShader = VERTEX_SHADER_TEXTURE;
875 fragmentShader += FRAGMENT_SHADER_TEXTURE;
878 return Dali::Shader::New( vertexShader, fragmentShader );
881 bool VideoView::GetStringFromProperty( const Dali::Property::Value& value, std::string& output )
883 bool extracted = false;
884 if( value.Get( output ) )
892 void VideoView::ApplyBackupProperties()
894 Property::Map::SizeType pos = 0;
895 Property::Map::SizeType count = mPropertyBackup.Count();
897 for( ; pos < count; pos++ )
899 KeyValuePair property = mPropertyBackup.GetKeyValue( pos );
901 SetPropertyInternal( property.first.indexKey, property.second );
905 void VideoView::FrameRenderCallback( int frameID )
908 if( frameID == mFrameID )
910 mVideoPlayer.FinishSynchronization();
915 void VideoView::SetFrameRenderCallback()
918 DevelWindow::AddFrameRenderedCallback( DevelWindow::Get( Self() ),
919 std::unique_ptr< CallbackBase >( MakeCallback( this, &VideoView::FrameRenderCallback ) ), mFrameID );
922 } // namespace Internal
924 } // namespace toolkit