2 * Copyright (c) 2018 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/public-api/common/stage.h>
26 #include <dali/devel-api/scripting/scripting.h>
27 #include <dali/public-api/adaptor-framework/native-image-source.h>
28 #include <dali/integration-api/adaptors/adaptor.h>
29 #include <dali/integration-api/debug.h>
30 #include <dali/public-api/animation/constraint.h>
31 #include <dali/devel-api/actors/actor-devel.h>
34 #include <dali-toolkit/public-api/controls/video-view/video-view.h>
35 #include <dali-toolkit/internal/visuals/visual-factory-cache.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 mediump mat4 uMvpMatrix;\n
91 uniform mediump 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 mediump mat4 uMvpMatrix;\n
113 uniform mediump 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()
135 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
136 mCurrentVideoPlayPosition( 0 ),
140 mVideoPlayer = Dali::VideoPlayer::New();
143 VideoView::~VideoView()
147 Toolkit::VideoView VideoView::New()
149 VideoView* impl = new VideoView();
150 Toolkit::VideoView handle = Toolkit::VideoView( *impl );
157 void VideoView::OnInitialize()
159 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
160 SetWindowSurfaceTarget();
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 mEffectPropertyMap = *( shaderValue->GetMap() );
201 if( mTextureRenderer && !mEffectPropertyMap.Empty() )
203 Dali::Shader shader = CreateShader();
204 mTextureRenderer.SetShader( shader );
210 std::string VideoView::GetUrl()
215 void VideoView::SetLooping( bool looping )
217 mVideoPlayer.SetLooping( looping );
220 bool VideoView::IsLooping()
222 return mVideoPlayer.IsLooping();
225 void VideoView::Play()
229 Self().AddRenderer( mOverlayRenderer );
236 void VideoView::Pause()
238 mVideoPlayer.Pause();
242 void VideoView::Stop()
248 void VideoView::Forward( int millisecond )
250 int curPos = mVideoPlayer.GetPlayPosition();
252 int nextPos = curPos + millisecond;
254 mVideoPlayer.SetPlayPosition( nextPos );
257 void VideoView::Backward( int millisecond )
259 int curPos = mVideoPlayer.GetPlayPosition();
261 int nextPos = curPos - millisecond;
262 nextPos = ( nextPos < 0 )? 0: nextPos;
264 mVideoPlayer.SetPlayPosition( nextPos );
267 void VideoView::SetMute( bool mute )
269 mVideoPlayer.SetMute( mute );
272 bool VideoView::IsMuted()
274 return mVideoPlayer.IsMuted();
277 void VideoView::SetVolume( float left, float right )
279 mVideoPlayer.SetVolume( left, right );
282 void VideoView::GetVolume( float& left, float& right )
284 mVideoPlayer.GetVolume( left, right );
287 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
289 return mFinishedSignal;
292 void VideoView::EmitSignalFinish()
296 Self().RemoveRenderer( mOverlayRenderer );
299 if ( !mFinishedSignal.Empty() )
301 Dali::Toolkit::VideoView handle( GetOwner() );
302 mFinishedSignal.Emit( handle );
306 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
310 Dali::BaseHandle handle( object );
311 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
318 VideoView& impl = GetImpl( videoView );
320 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
325 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
330 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
335 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
338 if( attributes["videoForward"].Get( millisecond ) )
340 impl.Forward( millisecond );
344 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
347 if( attributes["videoBackward"].Get( millisecond ) )
349 impl.Backward( millisecond );
357 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
359 Dali::BaseHandle handle( object );
361 bool connected( true );
362 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
364 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
366 videoView.FinishedSignal().Connect( tracker, functor );
370 // signalName does not match any signal
377 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
379 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
383 VideoView& impl = GetImpl( videoView );
387 case Toolkit::VideoView::Property::VIDEO:
389 std::string videoUrl;
392 if( value.Get( videoUrl ) )
394 impl.SetUrl( videoUrl );
396 else if( value.Get( map ) )
398 impl.SetPropertyMap( map );
402 case Toolkit::VideoView::Property::LOOPING:
405 if( value.Get( looping ) )
407 impl.SetLooping( looping );
411 case Toolkit::VideoView::Property::MUTED:
414 if( value.Get( mute ) )
416 impl.SetMute( mute );
420 case Toolkit::VideoView::Property::VOLUME:
424 if( value.Get( map ) )
426 Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
427 Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
428 if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
430 impl.SetVolume( left, right );
435 case Toolkit::VideoView::Property::UNDERLAY:
438 if( value.Get( underlay ) )
440 impl.SetUnderlay( underlay );
444 case Toolkit::VideoView::Property::PLAY_POSITION:
447 if( value.Get( pos ) )
449 impl.SetPlayPosition( pos );
453 case Toolkit::VideoView::Property::DISPLAY_MODE:
456 if( value.Get( mode ) )
458 impl.SetDisplayMode( mode );
466 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
468 Property::Value value;
469 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
473 VideoView& impl = GetImpl( videoView );
475 switch( propertyIndex )
477 case Toolkit::VideoView::Property::VIDEO:
479 if( !impl.mUrl.empty() )
483 else if( !impl.mPropertyMap.Empty() )
485 value = impl.mPropertyMap;
489 case Toolkit::VideoView::Property::LOOPING:
491 value = impl.IsLooping();
494 case Toolkit::VideoView::Property::MUTED:
496 value = impl.IsMuted();
499 case Toolkit::VideoView::Property::VOLUME:
504 impl.GetVolume( left, right );
505 map.Insert( VOLUME_LEFT, left );
506 map.Insert( VOLUME_RIGHT, right );
510 case Toolkit::VideoView::Property::UNDERLAY:
512 value = impl.IsUnderlay();
515 case Toolkit::VideoView::Property::PLAY_POSITION:
517 value = impl.GetPlayPosition();
520 case Toolkit::VideoView::Property::DISPLAY_MODE:
522 value = impl.GetDisplayMode();
531 void VideoView::SetDepthIndex( int depthIndex )
533 if( mTextureRenderer )
535 mTextureRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, depthIndex );
539 void VideoView::OnStageConnection( int depth )
541 Control::OnStageConnection( depth );
544 void VideoView::OnStageDisconnection()
546 Control::OnStageDisconnection();
549 Vector3 VideoView::GetNaturalSize()
552 size.x = mVideoSize.GetWidth();
553 size.y = mVideoSize.GetHeight();
555 if( size.x > 0 && size.y > 0 )
557 size.z = std::min( size.x, size.y );
562 return Control::GetNaturalSize();
566 float VideoView::GetHeightForWidth( float width )
568 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
570 return GetHeightForWidthBase( width );
574 return Control::GetHeightForWidthBase( width );
578 float VideoView::GetWidthForHeight( float height )
580 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
582 return GetWidthForHeightBase( height );
586 return Control::GetWidthForHeightBase( height );
590 void VideoView::SetWindowSurfaceTarget()
593 int curPos = mVideoPlayer.GetPlayPosition();
597 mVideoPlayer.Pause();
600 mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
601 mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
602 mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
603 mPositionUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
604 mSizeUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
605 mScaleUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
607 if( mTextureRenderer )
609 self.RemoveRenderer( mTextureRenderer );
612 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle() );
613 mVideoPlayer.SetUrl( mUrl );
615 if( !mOverlayRenderer )
617 // For underlay rendering mode, video display area have to be transparent.
618 Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
619 Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
620 mOverlayRenderer = Renderer::New( geometry, shader );
621 mOverlayRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::OFF );
631 mVideoPlayer.SetPlayPosition( curPos );
635 void VideoView::SetNativeImageTarget()
637 if( mVideoPlayer.IsVideoTextureSupported() == false )
639 DALI_LOG_ERROR( "Platform doesn't support decoded video frame images\n" );
646 mVideoPlayer.Pause();
649 Actor self( Self() );
651 if( mOverlayRenderer )
653 self.RemoveRenderer( mOverlayRenderer );
656 self.RemovePropertyNotification( mPositionUpdateNotification );
657 self.RemovePropertyNotification( mSizeUpdateNotification );
658 self.RemovePropertyNotification( mScaleUpdateNotification );
660 int curPos = mVideoPlayer.GetPlayPosition();
663 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
664 mNativeTexture = Dali::Texture::New( *nativeImageSourcePtr );
666 if( !mTextureRenderer )
668 Dali::Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
669 Dali::Shader shader = CreateShader();
670 Dali::TextureSet textureSet = Dali::TextureSet::New();
671 textureSet.SetTexture( 0u, mNativeTexture );
673 mTextureRenderer = Renderer::New( geometry, shader );
674 mTextureRenderer.SetTextures( textureSet );
678 Dali::TextureSet textureSet = mTextureRenderer.GetTextures();
679 textureSet.SetTexture( 0u, mNativeTexture );
681 Self().AddRenderer( mTextureRenderer );
683 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
684 mVideoPlayer.SetUrl( mUrl );
693 mVideoPlayer.SetPlayPosition( curPos );
697 void VideoView::UpdateDisplayArea( Dali::PropertyNotification& source )
704 Actor self( Self() );
706 bool positionUsesAnchorPoint = self.GetProperty( DevelActor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >();
707 Vector3 actorSize = self.GetCurrentSize() * self.GetCurrentScale();
708 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentAnchorPoint() : AnchorPoint::TOP_LEFT );
710 Vector2 screenPosition = self.GetProperty( DevelActor::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 Dali::Shader VideoView::CreateShader()
780 std::string fragmentShader = "#extension GL_OES_EGL_image_external:require\n";
781 std::string vertexShader;
782 std::string customFragmentShader;
783 bool checkShader = false;
785 if( !mEffectPropertyMap.Empty() )
787 Property::Value* vertexShaderValue = mEffectPropertyMap.Find( CUSTOM_VERTEX_SHADER );
788 if( vertexShaderValue )
790 checkShader = GetStringFromProperty( *vertexShaderValue, vertexShader );
793 if( !vertexShaderValue || !checkShader )
795 vertexShader = VERTEX_SHADER_TEXTURE;
798 Property::Value* fragmentShaderValue = mEffectPropertyMap.Find( CUSTOM_FRAGMENT_SHADER );
799 if( fragmentShaderValue )
801 checkShader = GetStringFromProperty( *fragmentShaderValue, customFragmentShader );
805 fragmentShader = customFragmentShader;
809 if( !fragmentShaderValue || !checkShader )
811 fragmentShader += FRAGMENT_SHADER_TEXTURE;
816 vertexShader = VERTEX_SHADER_TEXTURE;
817 fragmentShader += FRAGMENT_SHADER_TEXTURE;
820 return Dali::Shader::New( vertexShader, fragmentShader );
823 bool VideoView::GetStringFromProperty( const Dali::Property::Value& value, std::string& output )
825 bool extracted = false;
826 if( value.Get( output ) )
834 } // namespace Internal
836 } // namespace toolkit