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/public-api/adaptor-framework/native-image-source.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/public-api/animation/constraint.h>
29 #include <dali/devel-api/actors/actor-devel.h>
32 #include <dali-toolkit/public-api/controls/video-view/video-view.h>
33 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
34 #include <dali/integration-api/adaptor-framework/adaptor.h>
50 return Toolkit::VideoView::New();
53 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::VideoView, Toolkit::Control, Create );
55 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "video", MAP, VIDEO )
56 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "looping", BOOLEAN, LOOPING )
57 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "muted", BOOLEAN, MUTED )
58 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "volume", MAP, VOLUME )
59 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "underlay", BOOLEAN, UNDERLAY )
60 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "playPosition", INTEGER, PLAY_POSITION )
61 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "displayMode", INTEGER, DISPLAY_MODE )
63 DALI_SIGNAL_REGISTRATION( Toolkit, VideoView, "finished", FINISHED_SIGNAL )
65 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "play", ACTION_VIDEOVIEW_PLAY )
66 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "pause", ACTION_VIDEOVIEW_PAUSE )
67 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "stop", ACTION_VIDEOVIEW_STOP )
68 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "forward", ACTION_VIDEOVIEW_FORWARD )
69 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "backward", ACTION_VIDEOVIEW_BACKWARD )
71 DALI_TYPE_REGISTRATION_END()
73 const char* const VOLUME_LEFT( "volumeLeft" );
74 const char* const VOLUME_RIGHT( "volumeRight" );
76 // 3.0 TC uses RENDERING_TARGET. It should be removed in next release
77 const char* const RENDERING_TARGET( "renderingTarget" );
78 const char* const WINDOW_SURFACE_TARGET( "windowSurfaceTarget" );
79 const char* const NATIVE_IMAGE_TARGET( "nativeImageTarget" );
81 const char* const CUSTOM_SHADER( "shader" );
82 const char* const CUSTOM_VERTEX_SHADER( "vertexShader" );
83 const char* const CUSTOM_FRAGMENT_SHADER( "fragmentShader" );
84 const char* const DEFAULT_SAMPLER_TYPE_NAME( "sampler2D" );
85 const char* const CUSTOM_SAMPLER_TYPE_NAME( "samplerExternalOES" );
87 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
88 attribute mediump vec2 aPosition;\n
89 uniform highp mat4 uMvpMatrix;\n
90 uniform mediump vec3 uSize;\n
94 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
95 vertexPosition.xyz *= uSize;\n
96 gl_Position = uMvpMatrix * vertexPosition;\n
100 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
104 gl_FragColor = vec4(0.0);\n
108 const char* VERTEX_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
109 attribute mediump vec2 aPosition;\n
110 varying mediump vec2 vTexCoord;\n
111 uniform highp mat4 uMvpMatrix;\n
112 uniform mediump vec3 uSize;\n
113 varying mediump vec2 sTexCoordRect;\n
116 gl_Position = uMvpMatrix * vec4(aPosition * uSize.xy, 0.0, 1.0);\n
117 vTexCoord = aPosition + vec2(0.5);\n
121 const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
122 uniform lowp vec4 uColor;\n
123 varying mediump vec2 vTexCoord;\n
124 uniform samplerExternalOES sTexture;\n
127 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
131 } // anonymous namepsace
133 VideoView::VideoView()
134 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
135 mCurrentVideoPlayPosition( 0 ),
139 mVideoPlayer = Dali::VideoPlayer::New();
142 VideoView::~VideoView()
146 Toolkit::VideoView VideoView::New()
148 VideoView* impl = new VideoView();
149 Toolkit::VideoView handle = Toolkit::VideoView( *impl );
156 void VideoView::OnInitialize()
158 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
161 void VideoView::SetUrl( const std::string& url )
164 mPropertyMap.Clear();
166 mVideoPlayer.SetUrl( mUrl );
169 void VideoView::SetPropertyMap( Property::Map map )
173 Property::Value* target = map.Find( RENDERING_TARGET );
174 std::string targetType;
176 if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
179 SetWindowSurfaceTarget();
181 else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
184 SetNativeImageTarget();
188 Property::Value* shaderValue;
191 shaderValue = map.Find( CUSTOM_SHADER );
195 Property::Map* shaderMap = shaderValue->GetMap();
198 mEffectPropertyMap = *shaderMap;
203 if( mTextureRenderer && !mEffectPropertyMap.Empty() )
205 Dali::Shader shader = CreateShader();
206 mTextureRenderer.SetShader( shader );
212 std::string VideoView::GetUrl()
217 void VideoView::SetLooping( bool looping )
219 mVideoPlayer.SetLooping( looping );
222 bool VideoView::IsLooping()
224 return mVideoPlayer.IsLooping();
227 void VideoView::Play()
229 if( mOverlayRenderer )
231 Self().AddRenderer( mOverlayRenderer );
238 void VideoView::Pause()
240 mVideoPlayer.Pause();
244 void VideoView::Stop()
250 void VideoView::Forward( int millisecond )
252 int curPos = mVideoPlayer.GetPlayPosition();
254 int nextPos = curPos + millisecond;
256 mVideoPlayer.SetPlayPosition( nextPos );
259 void VideoView::Backward( int millisecond )
261 int curPos = mVideoPlayer.GetPlayPosition();
263 int nextPos = curPos - millisecond;
264 nextPos = ( nextPos < 0 )? 0: nextPos;
266 mVideoPlayer.SetPlayPosition( nextPos );
269 void VideoView::SetMute( bool mute )
271 mVideoPlayer.SetMute( mute );
274 bool VideoView::IsMuted()
276 return mVideoPlayer.IsMuted();
279 void VideoView::SetVolume( float left, float right )
281 mVideoPlayer.SetVolume( left, right );
284 void VideoView::GetVolume( float& left, float& right )
286 mVideoPlayer.GetVolume( left, right );
289 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
291 return mFinishedSignal;
294 void VideoView::EmitSignalFinish()
296 if( mOverlayRenderer )
298 Self().RemoveRenderer( mOverlayRenderer );
301 if ( !mFinishedSignal.Empty() )
303 Dali::Toolkit::VideoView handle( GetOwner() );
304 mFinishedSignal.Emit( handle );
308 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
312 Dali::BaseHandle handle( object );
313 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
320 VideoView& impl = GetImpl( videoView );
322 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
327 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
332 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
337 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
340 if( attributes["videoForward"].Get( millisecond ) )
342 impl.Forward( millisecond );
346 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
349 if( attributes["videoBackward"].Get( millisecond ) )
351 impl.Backward( millisecond );
359 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
361 Dali::BaseHandle handle( object );
363 bool connected( true );
364 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
366 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
368 videoView.FinishedSignal().Connect( tracker, functor );
372 // signalName does not match any signal
379 void VideoView::SetPropertyInternal( Property::Index index, const Property::Value& value )
383 case Toolkit::VideoView::Property::VIDEO:
385 std::string videoUrl;
388 if( value.Get( videoUrl ) )
392 else if( value.Get( map ) )
394 SetPropertyMap( map );
398 case Toolkit::VideoView::Property::LOOPING:
401 if( value.Get( looping ) )
403 SetLooping( looping );
407 case Toolkit::VideoView::Property::MUTED:
410 if( value.Get( mute ) )
416 case Toolkit::VideoView::Property::VOLUME:
420 if( value.Get( map ) )
422 Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
423 Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
424 if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
426 SetVolume( left, right );
431 case Toolkit::VideoView::Property::UNDERLAY:
434 if( value.Get( underlay ) )
436 SetUnderlay( underlay );
440 case Toolkit::VideoView::Property::PLAY_POSITION:
443 if( value.Get( pos ) )
445 SetPlayPosition( pos );
449 case Toolkit::VideoView::Property::DISPLAY_MODE:
452 if( value.Get( mode ) )
454 SetDisplayMode( mode );
461 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
463 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
467 VideoView& impl = GetImpl( videoView );
469 impl.SetPropertyInternal( index, value );
471 if( index != Toolkit::VideoView::Property::UNDERLAY )
474 // These values will be used when underlay mode is changed.
475 impl.mPropertyBackup[index] = value;
480 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
482 Property::Value value;
483 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
487 VideoView& impl = GetImpl( videoView );
489 switch( propertyIndex )
491 case Toolkit::VideoView::Property::VIDEO:
493 if( !impl.mUrl.empty() )
497 else if( !impl.mPropertyMap.Empty() )
499 value = impl.mPropertyMap;
503 case Toolkit::VideoView::Property::LOOPING:
505 value = impl.IsLooping();
508 case Toolkit::VideoView::Property::MUTED:
510 value = impl.IsMuted();
513 case Toolkit::VideoView::Property::VOLUME:
518 impl.GetVolume( left, right );
519 map.Insert( VOLUME_LEFT, left );
520 map.Insert( VOLUME_RIGHT, right );
524 case Toolkit::VideoView::Property::UNDERLAY:
526 value = impl.IsUnderlay();
529 case Toolkit::VideoView::Property::PLAY_POSITION:
531 value = impl.GetPlayPosition();
534 case Toolkit::VideoView::Property::DISPLAY_MODE:
536 value = impl.GetDisplayMode();
545 void VideoView::SetDepthIndex( int depthIndex )
547 if( mTextureRenderer )
549 mTextureRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, depthIndex );
553 void VideoView::OnStageConnection( int depth )
555 Control::OnStageConnection( depth );
559 SetWindowSurfaceTarget();
563 void VideoView::OnStageDisconnection()
565 Control::OnStageDisconnection();
568 Vector3 VideoView::GetNaturalSize()
571 size.x = mVideoSize.GetWidth();
572 size.y = mVideoSize.GetHeight();
574 if( size.x > 0 && size.y > 0 )
576 size.z = std::min( size.x, size.y );
581 return Control::GetNaturalSize();
585 float VideoView::GetHeightForWidth( float width )
587 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
589 return GetHeightForWidthBase( width );
593 return Control::GetHeightForWidthBase( width );
597 float VideoView::GetWidthForHeight( float height )
599 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
601 return GetWidthForHeightBase( height );
605 return Control::GetWidthForHeightBase( height );
609 void VideoView::SetWindowSurfaceTarget()
613 if( !self.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
615 // When the control is off the stage, it does not have Window.
619 int curPos = mVideoPlayer.GetPlayPosition();
623 mVideoPlayer.Pause();
626 mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
627 mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
628 mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
629 mPositionUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
630 mSizeUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
631 mScaleUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
633 if( mTextureRenderer )
635 self.RemoveRenderer( mTextureRenderer );
638 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
639 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle( self ) );
641 ApplyBackupProperties();
643 if( !mOverlayRenderer )
645 // For underlay rendering mode, video display area have to be transparent.
646 Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
647 Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
648 mOverlayRenderer = Renderer::New( geometry, shader );
649 mOverlayRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::OFF );
659 mVideoPlayer.SetPlayPosition( curPos );
663 void VideoView::SetNativeImageTarget()
665 if( mVideoPlayer.IsVideoTextureSupported() == false )
667 DALI_LOG_ERROR( "Platform doesn't support decoded video frame images\n" );
674 mVideoPlayer.Pause();
677 Actor self( Self() );
679 if( mOverlayRenderer )
681 self.RemoveRenderer( mOverlayRenderer );
683 mOverlayRenderer.Reset();
686 self.RemovePropertyNotification( mPositionUpdateNotification );
687 self.RemovePropertyNotification( mSizeUpdateNotification );
688 self.RemovePropertyNotification( mScaleUpdateNotification );
690 int curPos = mVideoPlayer.GetPlayPosition();
693 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
694 mNativeTexture = Dali::Texture::New( *nativeImageSourcePtr );
696 if( !mTextureRenderer )
698 Dali::Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
699 Dali::Shader shader = CreateShader();
700 Dali::TextureSet textureSet = Dali::TextureSet::New();
701 textureSet.SetTexture( 0u, mNativeTexture );
703 mTextureRenderer = Renderer::New( geometry, shader );
704 mTextureRenderer.SetTextures( textureSet );
708 Dali::TextureSet textureSet = mTextureRenderer.GetTextures();
709 textureSet.SetTexture( 0u, mNativeTexture );
711 Self().AddRenderer( mTextureRenderer );
713 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
714 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
716 ApplyBackupProperties();
725 mVideoPlayer.SetPlayPosition( curPos );
729 void VideoView::UpdateDisplayArea( Dali::PropertyNotification& source )
736 Actor self( Self() );
738 bool positionUsesAnchorPoint = self.GetProperty( Actor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >();
739 Vector3 actorSize = self.GetCurrentProperty< Vector3 >( Actor::Property::SIZE ) * self.GetCurrentProperty< Vector3 >( Actor::Property::SCALE );
740 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentProperty< Vector3 >( Actor::Property::ANCHOR_POINT ) : AnchorPoint::TOP_LEFT );
742 Vector2 screenPosition = self.GetProperty( Actor::Property::SCREEN_POSITION ).Get< Vector2 >();
744 mDisplayArea.x = screenPosition.x - anchorPointOffSet.x;
745 mDisplayArea.y = screenPosition.y - anchorPointOffSet.y;
746 mDisplayArea.width = actorSize.x;
747 mDisplayArea.height = actorSize.y;
749 mVideoPlayer.SetDisplayArea( mDisplayArea );
752 void VideoView::SetUnderlay( bool set )
754 if( set != mIsUnderlay )
760 SetWindowSurfaceTarget();
764 SetNativeImageTarget();
771 bool VideoView::IsUnderlay()
776 void VideoView::SetSWCodec( bool on )
778 // If setting SW or HW type is failed , video-view shows video by default codec type.
779 // The default codec type is selected by platform.
782 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::SW );
786 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::HW );
790 int VideoView::GetPlayPosition()
792 return mVideoPlayer.GetPlayPosition();
795 void VideoView::SetPlayPosition( int pos )
797 mVideoPlayer.SetPlayPosition( pos );
800 void VideoView::SetDisplayMode( int mode )
802 mVideoPlayer.SetDisplayMode( static_cast< Dali::VideoPlayerPlugin::DisplayMode::Type >( mode ) );
805 int VideoView::GetDisplayMode() const
807 return static_cast< int >( mVideoPlayer.GetDisplayMode() );
810 Any VideoView::GetMediaPlayer()
812 return mVideoPlayer.GetMediaPlayer();
815 Dali::Shader VideoView::CreateShader()
817 std::string fragmentShader = "#extension GL_OES_EGL_image_external:require\n";
818 std::string vertexShader;
819 std::string customFragmentShader;
820 bool checkShader = false;
822 if( !mEffectPropertyMap.Empty() )
824 Property::Value* vertexShaderValue = mEffectPropertyMap.Find( CUSTOM_VERTEX_SHADER );
825 if( vertexShaderValue )
827 checkShader = GetStringFromProperty( *vertexShaderValue, vertexShader );
830 if( !vertexShaderValue || !checkShader )
832 vertexShader = VERTEX_SHADER_TEXTURE;
835 Property::Value* fragmentShaderValue = mEffectPropertyMap.Find( CUSTOM_FRAGMENT_SHADER );
836 if( fragmentShaderValue )
838 checkShader = GetStringFromProperty( *fragmentShaderValue, customFragmentShader );
842 fragmentShader = customFragmentShader;
846 if( !fragmentShaderValue || !checkShader )
848 fragmentShader += FRAGMENT_SHADER_TEXTURE;
853 vertexShader = VERTEX_SHADER_TEXTURE;
854 fragmentShader += FRAGMENT_SHADER_TEXTURE;
857 return Dali::Shader::New( vertexShader, fragmentShader );
860 bool VideoView::GetStringFromProperty( const Dali::Property::Value& value, std::string& output )
862 bool extracted = false;
863 if( value.Get( output ) )
871 void VideoView::ApplyBackupProperties()
873 Property::Map::SizeType pos = 0;
874 Property::Map::SizeType count = mPropertyBackup.Count();
876 for( ; pos < count; pos++ )
878 KeyValuePair property = mPropertyBackup.GetKeyValue( pos );
880 SetPropertyInternal( property.first.indexKey, property.second );
884 } // namespace Internal
886 } // namespace toolkit