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 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()
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()
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::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
383 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
387 VideoView& impl = GetImpl( videoView );
391 case Toolkit::VideoView::Property::VIDEO:
393 std::string videoUrl;
396 if( value.Get( videoUrl ) )
398 impl.SetUrl( videoUrl );
400 else if( value.Get( map ) )
402 impl.SetPropertyMap( map );
406 case Toolkit::VideoView::Property::LOOPING:
409 if( value.Get( looping ) )
411 impl.SetLooping( looping );
415 case Toolkit::VideoView::Property::MUTED:
418 if( value.Get( mute ) )
420 impl.SetMute( mute );
424 case Toolkit::VideoView::Property::VOLUME:
428 if( value.Get( map ) )
430 Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
431 Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
432 if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
434 impl.SetVolume( left, right );
439 case Toolkit::VideoView::Property::UNDERLAY:
442 if( value.Get( underlay ) )
444 impl.SetUnderlay( underlay );
448 case Toolkit::VideoView::Property::PLAY_POSITION:
451 if( value.Get( pos ) )
453 impl.SetPlayPosition( pos );
457 case Toolkit::VideoView::Property::DISPLAY_MODE:
460 if( value.Get( mode ) )
462 impl.SetDisplayMode( mode );
470 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
472 Property::Value value;
473 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
477 VideoView& impl = GetImpl( videoView );
479 switch( propertyIndex )
481 case Toolkit::VideoView::Property::VIDEO:
483 if( !impl.mUrl.empty() )
487 else if( !impl.mPropertyMap.Empty() )
489 value = impl.mPropertyMap;
493 case Toolkit::VideoView::Property::LOOPING:
495 value = impl.IsLooping();
498 case Toolkit::VideoView::Property::MUTED:
500 value = impl.IsMuted();
503 case Toolkit::VideoView::Property::VOLUME:
508 impl.GetVolume( left, right );
509 map.Insert( VOLUME_LEFT, left );
510 map.Insert( VOLUME_RIGHT, right );
514 case Toolkit::VideoView::Property::UNDERLAY:
516 value = impl.IsUnderlay();
519 case Toolkit::VideoView::Property::PLAY_POSITION:
521 value = impl.GetPlayPosition();
524 case Toolkit::VideoView::Property::DISPLAY_MODE:
526 value = impl.GetDisplayMode();
535 void VideoView::SetDepthIndex( int depthIndex )
537 if( mTextureRenderer )
539 mTextureRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, depthIndex );
543 void VideoView::OnStageConnection( int depth )
545 Control::OnStageConnection( depth );
548 void VideoView::OnStageDisconnection()
550 Control::OnStageDisconnection();
553 Vector3 VideoView::GetNaturalSize()
556 size.x = mVideoSize.GetWidth();
557 size.y = mVideoSize.GetHeight();
559 if( size.x > 0 && size.y > 0 )
561 size.z = std::min( size.x, size.y );
566 return Control::GetNaturalSize();
570 float VideoView::GetHeightForWidth( float width )
572 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
574 return GetHeightForWidthBase( width );
578 return Control::GetHeightForWidthBase( width );
582 float VideoView::GetWidthForHeight( float height )
584 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
586 return GetWidthForHeightBase( height );
590 return Control::GetWidthForHeightBase( height );
594 void VideoView::SetWindowSurfaceTarget()
597 int curPos = mVideoPlayer.GetPlayPosition();
601 mVideoPlayer.Pause();
604 mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
605 mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
606 mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
607 mPositionUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
608 mSizeUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
609 mScaleUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
611 if( mTextureRenderer )
613 self.RemoveRenderer( mTextureRenderer );
616 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle() );
617 mVideoPlayer.SetUrl( mUrl );
619 if( !mOverlayRenderer )
621 // For underlay rendering mode, video display area have to be transparent.
622 Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
623 Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
624 mOverlayRenderer = Renderer::New( geometry, shader );
625 mOverlayRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::OFF );
635 mVideoPlayer.SetPlayPosition( curPos );
639 void VideoView::SetNativeImageTarget()
641 if( mVideoPlayer.IsVideoTextureSupported() == false )
643 DALI_LOG_ERROR( "Platform doesn't support decoded video frame images\n" );
650 mVideoPlayer.Pause();
653 Actor self( Self() );
655 if( mOverlayRenderer )
657 self.RemoveRenderer( mOverlayRenderer );
660 self.RemovePropertyNotification( mPositionUpdateNotification );
661 self.RemovePropertyNotification( mSizeUpdateNotification );
662 self.RemovePropertyNotification( mScaleUpdateNotification );
664 int curPos = mVideoPlayer.GetPlayPosition();
667 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
668 mNativeTexture = Dali::Texture::New( *nativeImageSourcePtr );
670 if( !mTextureRenderer )
672 Dali::Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
673 Dali::Shader shader = CreateShader();
674 Dali::TextureSet textureSet = Dali::TextureSet::New();
675 textureSet.SetTexture( 0u, mNativeTexture );
677 mTextureRenderer = Renderer::New( geometry, shader );
678 mTextureRenderer.SetTextures( textureSet );
682 Dali::TextureSet textureSet = mTextureRenderer.GetTextures();
683 textureSet.SetTexture( 0u, mNativeTexture );
685 Self().AddRenderer( mTextureRenderer );
687 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
688 mVideoPlayer.SetUrl( mUrl );
697 mVideoPlayer.SetPlayPosition( curPos );
701 void VideoView::UpdateDisplayArea( Dali::PropertyNotification& source )
708 Actor self( Self() );
710 bool positionUsesAnchorPoint = self.GetProperty( DevelActor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >();
711 Vector3 actorSize = self.GetCurrentSize() * self.GetCurrentScale();
712 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentAnchorPoint() : AnchorPoint::TOP_LEFT );
714 Vector2 screenPosition = self.GetProperty( DevelActor::Property::SCREEN_POSITION ).Get< Vector2 >();
716 mDisplayArea.x = screenPosition.x - anchorPointOffSet.x;
717 mDisplayArea.y = screenPosition.y - anchorPointOffSet.y;
718 mDisplayArea.width = actorSize.x;
719 mDisplayArea.height = actorSize.y;
721 mVideoPlayer.SetDisplayArea( mDisplayArea );
724 void VideoView::SetUnderlay( bool set )
726 if( set != mIsUnderlay )
732 SetWindowSurfaceTarget();
736 SetNativeImageTarget();
743 bool VideoView::IsUnderlay()
748 void VideoView::SetSWCodec( bool on )
750 // If setting SW or HW type is failed , video-view shows video by default codec type.
751 // The default codec type is selected by platform.
754 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::SW );
758 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::HW );
762 int VideoView::GetPlayPosition()
764 return mVideoPlayer.GetPlayPosition();
767 void VideoView::SetPlayPosition( int pos )
769 mVideoPlayer.SetPlayPosition( pos );
772 void VideoView::SetDisplayMode( int mode )
774 mVideoPlayer.SetDisplayMode( static_cast< Dali::VideoPlayerPlugin::DisplayMode::Type >( mode ) );
777 int VideoView::GetDisplayMode() const
779 return static_cast< int >( mVideoPlayer.GetDisplayMode() );
782 Dali::Shader VideoView::CreateShader()
784 std::string fragmentShader = "#extension GL_OES_EGL_image_external:require\n";
785 std::string vertexShader;
786 std::string customFragmentShader;
787 bool checkShader = false;
789 if( !mEffectPropertyMap.Empty() )
791 Property::Value* vertexShaderValue = mEffectPropertyMap.Find( CUSTOM_VERTEX_SHADER );
792 if( vertexShaderValue )
794 checkShader = GetStringFromProperty( *vertexShaderValue, vertexShader );
797 if( !vertexShaderValue || !checkShader )
799 vertexShader = VERTEX_SHADER_TEXTURE;
802 Property::Value* fragmentShaderValue = mEffectPropertyMap.Find( CUSTOM_FRAGMENT_SHADER );
803 if( fragmentShaderValue )
805 checkShader = GetStringFromProperty( *fragmentShaderValue, customFragmentShader );
809 fragmentShader = customFragmentShader;
813 if( !fragmentShaderValue || !checkShader )
815 fragmentShader += FRAGMENT_SHADER_TEXTURE;
820 vertexShader = VERTEX_SHADER_TEXTURE;
821 fragmentShader += FRAGMENT_SHADER_TEXTURE;
824 return Dali::Shader::New( vertexShader, fragmentShader );
827 bool VideoView::GetStringFromProperty( const Dali::Property::Value& value, std::string& output )
829 bool extracted = false;
830 if( value.Get( output ) )
838 } // namespace Internal
840 } // namespace toolkit