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>
36 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
52 return Toolkit::VideoView::New();
55 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::VideoView, Toolkit::Control, Create );
57 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "video", MAP, VIDEO )
58 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "looping", BOOLEAN, LOOPING )
59 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "muted", BOOLEAN, MUTED )
60 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "volume", MAP, VOLUME )
61 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "underlay", BOOLEAN, UNDERLAY )
62 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "playPosition", INTEGER, PLAY_POSITION )
63 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "displayMode", INTEGER, DISPLAY_MODE )
65 DALI_SIGNAL_REGISTRATION( Toolkit, VideoView, "finished", FINISHED_SIGNAL )
67 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "play", ACTION_VIDEOVIEW_PLAY )
68 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "pause", ACTION_VIDEOVIEW_PAUSE )
69 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "stop", ACTION_VIDEOVIEW_STOP )
70 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "forward", ACTION_VIDEOVIEW_FORWARD )
71 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "backward", ACTION_VIDEOVIEW_BACKWARD )
73 DALI_TYPE_REGISTRATION_END()
75 const char* const VOLUME_LEFT( "volumeLeft" );
76 const char* const VOLUME_RIGHT( "volumeRight" );
78 // 3.0 TC uses RENDERING_TARGET. It should be removed in next release
79 const char* const RENDERING_TARGET( "renderingTarget" );
80 const char* const WINDOW_SURFACE_TARGET( "windowSurfaceTarget" );
81 const char* const NATIVE_IMAGE_TARGET( "nativeImageTarget" );
83 const char* const CUSTOM_SHADER( "shader" );
84 const char* const CUSTOM_VERTEX_SHADER( "vertexShader" );
85 const char* const CUSTOM_FRAGMENT_SHADER( "fragmentShader" );
86 const char* const DEFAULT_SAMPLER_TYPE_NAME( "sampler2D" );
87 const char* const CUSTOM_SAMPLER_TYPE_NAME( "samplerExternalOES" );
89 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
90 attribute mediump vec2 aPosition;\n
91 uniform highp mat4 uMvpMatrix;\n
92 uniform mediump vec3 uSize;\n
96 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
97 vertexPosition.xyz *= uSize;\n
98 gl_Position = uMvpMatrix * vertexPosition;\n
102 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
106 gl_FragColor = vec4(0.0);\n
110 const char* VERTEX_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
111 attribute mediump vec2 aPosition;\n
112 varying mediump vec2 vTexCoord;\n
113 uniform highp mat4 uMvpMatrix;\n
114 uniform mediump vec3 uSize;\n
115 varying mediump vec2 sTexCoordRect;\n
118 gl_Position = uMvpMatrix * vec4(aPosition * uSize.xy, 0.0, 1.0);\n
119 vTexCoord = aPosition + vec2(0.5);\n
123 const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
124 uniform lowp vec4 uColor;\n
125 varying mediump vec2 vTexCoord;\n
126 uniform samplerExternalOES sTexture;\n
129 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
133 } // anonymous namepsace
135 VideoView::VideoView()
136 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
137 mCurrentVideoPlayPosition( 0 ),
141 mVideoPlayer = Dali::VideoPlayer::New();
142 DevelControl::SetAccessibilityConstructor( Self(), []( Dali::Actor actor )
144 return std::unique_ptr< Dali::Accessibility::Accessible >(
145 new Control::Impl::AccessibleImpl( actor, Dali::Accessibility::Role::VIDEO, true ) );
150 VideoView::~VideoView()
154 Toolkit::VideoView VideoView::New()
156 VideoView* impl = new VideoView();
157 Toolkit::VideoView handle = Toolkit::VideoView( *impl );
164 void VideoView::OnInitialize()
166 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
169 void VideoView::SetUrl( const std::string& url )
172 mPropertyMap.Clear();
174 mVideoPlayer.SetUrl( mUrl );
177 void VideoView::SetPropertyMap( Property::Map map )
181 Property::Value* target = map.Find( RENDERING_TARGET );
182 std::string targetType;
184 if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
187 SetWindowSurfaceTarget();
189 else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
192 SetNativeImageTarget();
196 Property::Value* shaderValue;
199 shaderValue = map.Find( CUSTOM_SHADER );
203 Property::Map* shaderMap = shaderValue->GetMap();
206 mEffectPropertyMap = *shaderMap;
211 if( mTextureRenderer && !mEffectPropertyMap.Empty() )
213 Dali::Shader shader = CreateShader();
214 mTextureRenderer.SetShader( shader );
220 std::string VideoView::GetUrl()
225 void VideoView::SetLooping( bool looping )
227 mVideoPlayer.SetLooping( looping );
230 bool VideoView::IsLooping()
232 return mVideoPlayer.IsLooping();
235 void VideoView::Play()
237 if( mOverlayRenderer )
239 Self().AddRenderer( mOverlayRenderer );
246 void VideoView::Pause()
248 mVideoPlayer.Pause();
252 void VideoView::Stop()
258 void VideoView::Forward( int millisecond )
260 int curPos = mVideoPlayer.GetPlayPosition();
262 int nextPos = curPos + millisecond;
264 mVideoPlayer.SetPlayPosition( nextPos );
267 void VideoView::Backward( int millisecond )
269 int curPos = mVideoPlayer.GetPlayPosition();
271 int nextPos = curPos - millisecond;
272 nextPos = ( nextPos < 0 )? 0: nextPos;
274 mVideoPlayer.SetPlayPosition( nextPos );
277 void VideoView::SetMute( bool mute )
279 mVideoPlayer.SetMute( mute );
282 bool VideoView::IsMuted()
284 return mVideoPlayer.IsMuted();
287 void VideoView::SetVolume( float left, float right )
289 mVideoPlayer.SetVolume( left, right );
292 void VideoView::GetVolume( float& left, float& right )
294 mVideoPlayer.GetVolume( left, right );
297 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
299 return mFinishedSignal;
302 void VideoView::EmitSignalFinish()
304 if( mOverlayRenderer )
306 Self().RemoveRenderer( mOverlayRenderer );
309 if ( !mFinishedSignal.Empty() )
311 Dali::Toolkit::VideoView handle( GetOwner() );
312 mFinishedSignal.Emit( handle );
316 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
320 Dali::BaseHandle handle( object );
321 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
328 VideoView& impl = GetImpl( videoView );
330 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
335 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
340 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
345 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
348 if( attributes["videoForward"].Get( millisecond ) )
350 impl.Forward( millisecond );
354 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
357 if( attributes["videoBackward"].Get( millisecond ) )
359 impl.Backward( millisecond );
367 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
369 Dali::BaseHandle handle( object );
371 bool connected( true );
372 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
374 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
376 videoView.FinishedSignal().Connect( tracker, functor );
380 // signalName does not match any signal
387 void VideoView::SetPropertyInternal( Property::Index index, const Property::Value& value )
391 case Toolkit::VideoView::Property::VIDEO:
393 std::string videoUrl;
396 if( value.Get( videoUrl ) )
400 else if( value.Get( map ) )
402 SetPropertyMap( map );
406 case Toolkit::VideoView::Property::LOOPING:
409 if( value.Get( looping ) )
411 SetLooping( looping );
415 case Toolkit::VideoView::Property::MUTED:
418 if( value.Get( 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 SetVolume( left, right );
439 case Toolkit::VideoView::Property::UNDERLAY:
442 if( value.Get( underlay ) )
444 SetUnderlay( underlay );
448 case Toolkit::VideoView::Property::PLAY_POSITION:
451 if( value.Get( pos ) )
453 SetPlayPosition( pos );
457 case Toolkit::VideoView::Property::DISPLAY_MODE:
460 if( value.Get( mode ) )
462 SetDisplayMode( mode );
469 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
471 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
475 VideoView& impl = GetImpl( videoView );
477 impl.SetPropertyInternal( index, value );
479 if( index != Toolkit::VideoView::Property::UNDERLAY )
482 // These values will be used when underlay mode is changed.
483 impl.mPropertyBackup[index] = value;
488 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
490 Property::Value value;
491 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
495 VideoView& impl = GetImpl( videoView );
497 switch( propertyIndex )
499 case Toolkit::VideoView::Property::VIDEO:
501 if( !impl.mUrl.empty() )
505 else if( !impl.mPropertyMap.Empty() )
507 value = impl.mPropertyMap;
511 case Toolkit::VideoView::Property::LOOPING:
513 value = impl.IsLooping();
516 case Toolkit::VideoView::Property::MUTED:
518 value = impl.IsMuted();
521 case Toolkit::VideoView::Property::VOLUME:
526 impl.GetVolume( left, right );
527 map.Insert( VOLUME_LEFT, left );
528 map.Insert( VOLUME_RIGHT, right );
532 case Toolkit::VideoView::Property::UNDERLAY:
534 value = impl.IsUnderlay();
537 case Toolkit::VideoView::Property::PLAY_POSITION:
539 value = impl.GetPlayPosition();
542 case Toolkit::VideoView::Property::DISPLAY_MODE:
544 value = impl.GetDisplayMode();
553 void VideoView::SetDepthIndex( int depthIndex )
555 if( mTextureRenderer )
557 mTextureRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, depthIndex );
561 void VideoView::OnStageConnection( int depth )
563 Control::OnStageConnection( depth );
567 SetWindowSurfaceTarget();
571 void VideoView::OnStageDisconnection()
573 Control::OnStageDisconnection();
576 Vector3 VideoView::GetNaturalSize()
579 size.x = mVideoSize.GetWidth();
580 size.y = mVideoSize.GetHeight();
582 if( size.x > 0 && size.y > 0 )
584 size.z = std::min( size.x, size.y );
589 return Control::GetNaturalSize();
593 float VideoView::GetHeightForWidth( float width )
595 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
597 return GetHeightForWidthBase( width );
601 return Control::GetHeightForWidthBase( width );
605 float VideoView::GetWidthForHeight( float height )
607 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
609 return GetWidthForHeightBase( height );
613 return Control::GetWidthForHeightBase( height );
617 void VideoView::SetWindowSurfaceTarget()
621 if( !self.OnStage() )
623 // When the control is off the stage, it does not have Window.
627 int curPos = mVideoPlayer.GetPlayPosition();
631 mVideoPlayer.Pause();
634 mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
635 mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
636 mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
637 mPositionUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
638 mSizeUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
639 mScaleUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
641 if( mTextureRenderer )
643 self.RemoveRenderer( mTextureRenderer );
646 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
647 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle( self ) );
649 ApplyBackupProperties();
651 if( !mOverlayRenderer )
653 // For underlay rendering mode, video display area have to be transparent.
654 Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
655 Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
656 mOverlayRenderer = Renderer::New( geometry, shader );
657 mOverlayRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::OFF );
667 mVideoPlayer.SetPlayPosition( curPos );
671 void VideoView::SetNativeImageTarget()
673 if( mVideoPlayer.IsVideoTextureSupported() == false )
675 DALI_LOG_ERROR( "Platform doesn't support decoded video frame images\n" );
682 mVideoPlayer.Pause();
685 Actor self( Self() );
687 if( mOverlayRenderer )
689 self.RemoveRenderer( mOverlayRenderer );
691 mOverlayRenderer.Reset();
694 self.RemovePropertyNotification( mPositionUpdateNotification );
695 self.RemovePropertyNotification( mSizeUpdateNotification );
696 self.RemovePropertyNotification( mScaleUpdateNotification );
698 int curPos = mVideoPlayer.GetPlayPosition();
701 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
702 mNativeTexture = Dali::Texture::New( *nativeImageSourcePtr );
704 if( !mTextureRenderer )
706 Dali::Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
707 Dali::Shader shader = CreateShader();
708 Dali::TextureSet textureSet = Dali::TextureSet::New();
709 textureSet.SetTexture( 0u, mNativeTexture );
711 mTextureRenderer = Renderer::New( geometry, shader );
712 mTextureRenderer.SetTextures( textureSet );
716 Dali::TextureSet textureSet = mTextureRenderer.GetTextures();
717 textureSet.SetTexture( 0u, mNativeTexture );
719 Self().AddRenderer( mTextureRenderer );
721 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
722 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
724 ApplyBackupProperties();
733 mVideoPlayer.SetPlayPosition( curPos );
737 void VideoView::UpdateDisplayArea( Dali::PropertyNotification& source )
744 Actor self( Self() );
746 bool positionUsesAnchorPoint = self.GetProperty( DevelActor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >();
747 Vector3 actorSize = self.GetCurrentSize() * self.GetCurrentScale();
748 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentAnchorPoint() : AnchorPoint::TOP_LEFT );
750 Vector2 screenPosition = self.GetProperty( DevelActor::Property::SCREEN_POSITION ).Get< Vector2 >();
752 mDisplayArea.x = screenPosition.x - anchorPointOffSet.x;
753 mDisplayArea.y = screenPosition.y - anchorPointOffSet.y;
754 mDisplayArea.width = actorSize.x;
755 mDisplayArea.height = actorSize.y;
757 mVideoPlayer.SetDisplayArea( mDisplayArea );
760 void VideoView::SetUnderlay( bool set )
762 if( set != mIsUnderlay )
768 SetWindowSurfaceTarget();
772 SetNativeImageTarget();
779 bool VideoView::IsUnderlay()
784 void VideoView::SetSWCodec( bool on )
786 // If setting SW or HW type is failed , video-view shows video by default codec type.
787 // The default codec type is selected by platform.
790 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::SW );
794 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::HW );
798 int VideoView::GetPlayPosition()
800 return mVideoPlayer.GetPlayPosition();
803 void VideoView::SetPlayPosition( int pos )
805 mVideoPlayer.SetPlayPosition( pos );
808 void VideoView::SetDisplayMode( int mode )
810 mVideoPlayer.SetDisplayMode( static_cast< Dali::VideoPlayerPlugin::DisplayMode::Type >( mode ) );
813 int VideoView::GetDisplayMode() const
815 return static_cast< int >( mVideoPlayer.GetDisplayMode() );
818 Dali::Shader VideoView::CreateShader()
820 std::string fragmentShader = "#extension GL_OES_EGL_image_external:require\n";
821 std::string vertexShader;
822 std::string customFragmentShader;
823 bool checkShader = false;
825 if( !mEffectPropertyMap.Empty() )
827 Property::Value* vertexShaderValue = mEffectPropertyMap.Find( CUSTOM_VERTEX_SHADER );
828 if( vertexShaderValue )
830 checkShader = GetStringFromProperty( *vertexShaderValue, vertexShader );
833 if( !vertexShaderValue || !checkShader )
835 vertexShader = VERTEX_SHADER_TEXTURE;
838 Property::Value* fragmentShaderValue = mEffectPropertyMap.Find( CUSTOM_FRAGMENT_SHADER );
839 if( fragmentShaderValue )
841 checkShader = GetStringFromProperty( *fragmentShaderValue, customFragmentShader );
845 fragmentShader = customFragmentShader;
849 if( !fragmentShaderValue || !checkShader )
851 fragmentShader += FRAGMENT_SHADER_TEXTURE;
856 vertexShader = VERTEX_SHADER_TEXTURE;
857 fragmentShader += FRAGMENT_SHADER_TEXTURE;
860 return Dali::Shader::New( vertexShader, fragmentShader );
863 bool VideoView::GetStringFromProperty( const Dali::Property::Value& value, std::string& output )
865 bool extracted = false;
866 if( value.Get( output ) )
874 void VideoView::ApplyBackupProperties()
876 Property::Map::SizeType pos = 0;
877 Property::Map::SizeType count = mPropertyBackup.Count();
879 for( ; pos < count; pos++ )
881 KeyValuePair property = mPropertyBackup.GetKeyValue( pos );
883 SetPropertyInternal( property.first.indexKey, property.second );
887 } // namespace Internal
889 } // namespace toolkit