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/public-api/visuals/visual-properties.h>
36 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
37 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
38 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
39 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
40 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
56 return Toolkit::VideoView::New();
59 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::VideoView, Toolkit::Control, Create );
61 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "video", MAP, VIDEO )
62 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "looping", BOOLEAN, LOOPING )
63 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "muted", BOOLEAN, MUTED )
64 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "volume", MAP, VOLUME )
65 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "underlay", BOOLEAN, UNDERLAY )
66 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "playPosition", INTEGER, PLAY_POSITION )
67 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "displayMode", INTEGER, DISPLAY_MODE )
69 DALI_SIGNAL_REGISTRATION( Toolkit, VideoView, "finished", FINISHED_SIGNAL )
71 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "play", ACTION_VIDEOVIEW_PLAY )
72 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "pause", ACTION_VIDEOVIEW_PAUSE )
73 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "stop", ACTION_VIDEOVIEW_STOP )
74 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "forward", ACTION_VIDEOVIEW_FORWARD )
75 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "backward", ACTION_VIDEOVIEW_BACKWARD )
77 DALI_TYPE_REGISTRATION_END()
79 const char* const VOLUME_LEFT( "volumeLeft" );
80 const char* const VOLUME_RIGHT( "volumeRight" );
82 // 3.0 TC uses RENDERING_TARGET. It should be removed in next release
83 const char* const RENDERING_TARGET( "renderingTarget" );
84 const char* const WINDOW_SURFACE_TARGET( "windowSurfaceTarget" );
85 const char* const NATIVE_IMAGE_TARGET( "nativeImageTarget" );
87 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
88 attribute mediump vec2 aPosition;\n
89 uniform mediump 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(
101 uniform lowp vec4 uColor;\n
102 uniform lowp vec3 mixColor;\n
106 gl_FragColor = vec4(mixColor, 1.0)*uColor;\n
110 } // anonymous namepsace
112 VideoView::VideoView()
113 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
114 mCurrentVideoPlayPosition( 0 ),
118 mVideoPlayer = Dali::VideoPlayer::New();
121 VideoView::~VideoView()
125 Toolkit::VideoView VideoView::New()
127 VideoView* impl = new VideoView();
128 Toolkit::VideoView handle = Toolkit::VideoView( *impl );
135 void VideoView::OnInitialize()
137 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
138 SetWindowSurfaceTarget();
141 void VideoView::SetUrl( const std::string& url )
143 if( mUrl != url || !mPropertyMap.Empty() )
146 mPropertyMap.Clear();
151 Actor self( Self() );
152 Internal::InitializeVisual( self, mVisual, mNativeImage );
155 mVideoPlayer.SetUrl( mUrl );
158 void VideoView::SetPropertyMap( Property::Map map )
162 Actor self( Self() );
163 Internal::InitializeVisual( self, mVisual, mPropertyMap );
165 Property::Value* widthValue = mPropertyMap.Find( "width" );
169 if( widthValue->Get( width ) )
171 mVideoSize = ImageDimensions( width, mVideoSize.GetHeight() );
175 Property::Value* heightValue = mPropertyMap.Find( "height" );
179 if( heightValue->Get( height ) )
181 mVideoSize = ImageDimensions( mVideoSize.GetWidth(), height );
185 Property::Value* target = map.Find( RENDERING_TARGET );
186 std::string targetType;
188 if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
191 SetWindowSurfaceTarget();
193 else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
196 SetNativeImageTarget();
202 std::string VideoView::GetUrl()
207 void VideoView::SetLooping(bool looping)
209 mVideoPlayer.SetLooping( looping );
212 bool VideoView::IsLooping()
214 return mVideoPlayer.IsLooping();
217 void VideoView::Play()
221 Self().AddRenderer( mRenderer );
228 void VideoView::Pause()
230 mVideoPlayer.Pause();
234 void VideoView::Stop()
240 void VideoView::Forward( int millisecond )
242 int curPos = mVideoPlayer.GetPlayPosition();
244 int nextPos = curPos + millisecond;
246 mVideoPlayer.SetPlayPosition( nextPos );
249 void VideoView::Backward( int millisecond )
251 int curPos = mVideoPlayer.GetPlayPosition();
253 int nextPos = curPos - millisecond;
254 nextPos = ( nextPos < 0 )? 0: nextPos;
256 mVideoPlayer.SetPlayPosition( nextPos );
259 void VideoView::SetMute( bool mute )
261 mVideoPlayer.SetMute( mute );
264 bool VideoView::IsMuted()
266 return mVideoPlayer.IsMuted();
269 void VideoView::SetVolume( float left, float right )
271 mVideoPlayer.SetVolume( left, right );
274 void VideoView::GetVolume( float& left, float& right )
276 mVideoPlayer.GetVolume( left, right );
279 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
281 return mFinishedSignal;
284 void VideoView::EmitSignalFinish()
288 Self().RemoveRenderer( mRenderer );
291 if ( !mFinishedSignal.Empty() )
293 Dali::Toolkit::VideoView handle( GetOwner() );
294 mFinishedSignal.Emit( handle );
298 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
302 Dali::BaseHandle handle( object );
303 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
310 VideoView& impl = GetImpl( videoView );
312 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
317 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
322 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
327 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
330 if( attributes["videoForward"].Get( millisecond ) )
332 impl.Forward( millisecond );
336 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
339 if( attributes["videoBackward"].Get( millisecond ) )
341 impl.Backward( millisecond );
349 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
351 Dali::BaseHandle handle( object );
353 bool connected( true );
354 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
356 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
358 videoView.FinishedSignal().Connect( tracker, functor );
362 // signalName does not match any signal
369 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
371 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
375 VideoView& impl = GetImpl( videoView );
379 case Toolkit::VideoView::Property::VIDEO:
381 std::string videoUrl;
384 if( value.Get( videoUrl ) )
386 impl.SetUrl( videoUrl );
388 else if( value.Get( map ) )
390 Property::Value* shaderValue = map.Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
392 if( map.Count() > 1u || !shaderValue )
394 impl.SetPropertyMap( map );
396 else if( impl.mVisual && map.Count() == 1u && shaderValue )
398 Property::Map shaderMap;
399 if( shaderValue->Get( shaderMap ) )
401 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
402 visual.SetCustomShader( shaderMap );
403 if( videoView.OnStage() )
405 visual.SetOffStage( videoView );
406 visual.SetOnStage( videoView );
413 case Toolkit::VideoView::Property::LOOPING:
416 if( value.Get( looping ) )
418 impl.SetLooping( looping );
422 case Toolkit::VideoView::Property::MUTED:
425 if( value.Get( mute ) )
427 impl.SetMute( mute );
431 case Toolkit::VideoView::Property::VOLUME:
435 if( value.Get( map ) )
437 Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
438 Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
439 if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
441 impl.SetVolume( left, right );
446 case Toolkit::VideoView::Property::UNDERLAY:
449 if( value.Get( underlay ) )
451 impl.SetUnderlay( underlay );
455 case Toolkit::VideoView::Property::PLAY_POSITION:
458 if( value.Get( pos ) )
460 impl.SetPlayPosition( pos );
464 case Toolkit::VideoView::Property::DISPLAY_MODE:
467 if( value.Get( mode ) )
469 impl.SetDisplayMode( mode );
477 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
479 Property::Value value;
480 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
484 VideoView& impl = GetImpl( videoView );
486 switch( propertyIndex )
488 case Toolkit::VideoView::Property::VIDEO:
490 if( !impl.mUrl.empty() )
494 else if( !impl.mPropertyMap.Empty() )
496 value = impl.mPropertyMap;
500 case Toolkit::VideoView::Property::LOOPING:
502 value = impl.IsLooping();
505 case Toolkit::VideoView::Property::MUTED:
507 value = impl.IsMuted();
510 case Toolkit::VideoView::Property::VOLUME:
515 impl.GetVolume( left, right );
516 map.Insert( VOLUME_LEFT, left );
517 map.Insert( VOLUME_RIGHT, right );
521 case Toolkit::VideoView::Property::UNDERLAY:
523 value = impl.IsUnderlay();
526 case Toolkit::VideoView::Property::PLAY_POSITION:
528 value = impl.GetPlayPosition();
531 case Toolkit::VideoView::Property::DISPLAY_MODE:
533 value = impl.GetDisplayMode();
542 void VideoView::SetDepthIndex( int depthIndex )
546 mVisual.SetDepthIndex( depthIndex );
550 void VideoView::OnStageConnection( int depth )
554 CustomActor self = Self();
555 Toolkit::GetImplementation(mVisual).SetOnStage( self );
558 Control::OnStageConnection( depth );
561 void VideoView::OnStageDisconnection()
565 CustomActor self = Self();
566 Toolkit::GetImplementation(mVisual).SetOffStage( self );
569 Control::OnStageDisconnection();
572 Vector3 VideoView::GetNaturalSize()
575 size.x = mVideoSize.GetWidth();
576 size.y = mVideoSize.GetHeight();
578 if( size.x > 0 && size.y > 0 )
580 size.z = std::min( size.x, size.y );
585 return Control::GetNaturalSize();
589 float VideoView::GetHeightForWidth( float width )
591 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
593 return GetHeightForWidthBase( width );
597 return Control::GetHeightForWidthBase( width );
601 float VideoView::GetWidthForHeight( float height )
603 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
605 return GetWidthForHeightBase( height );
609 return Control::GetWidthForHeightBase( height );
613 void VideoView::SetWindowSurfaceTarget()
616 int curPos = mVideoPlayer.GetPlayPosition();
620 Toolkit::GetImplementation(mVisual).SetOffStage(self);
626 mVideoPlayer.Pause();
629 mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
630 mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
631 mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
632 mPositionUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
633 mSizeUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
634 mScaleUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
636 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle() );
637 mVideoPlayer.SetUrl( mUrl );
641 // For underlay rendering mode, video display area have to be transparent.
642 Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
643 Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
644 mRenderer = Renderer::New( geometry, shader );
646 mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
647 mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_RGB, BlendFactor::ONE );
648 mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_RGB, BlendFactor::ZERO );
649 mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_ALPHA, BlendFactor::ONE );
650 mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_ALPHA, BlendFactor::ZERO );
660 mVideoPlayer.SetPlayPosition( curPos );
664 void VideoView::SetNativeImageTarget()
666 if( mVideoPlayer.IsVideoTextureSupported() == false )
668 DALI_LOG_ERROR( "Platform doesn't support decoded video frame images\n" );
675 mVideoPlayer.Pause();
678 Actor self( Self() );
679 self.RemoveRenderer( mRenderer );
680 Dali::Stage::GetCurrent().KeepRendering( 0.0f );
682 self.RemovePropertyNotification( mPositionUpdateNotification );
683 self.RemovePropertyNotification( mSizeUpdateNotification );
684 self.RemovePropertyNotification( mScaleUpdateNotification );
686 int curPos = mVideoPlayer.GetPlayPosition();
689 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
690 mNativeImage = Dali::NativeImage::New( *nativeImageSourcePtr );
692 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
693 mVideoPlayer.SetUrl( mUrl );
695 Internal::InitializeVisual( self, mVisual, mNativeImage );
696 Self().RemoveRenderer( mRenderer );
705 mVideoPlayer.SetPlayPosition( curPos );
709 void VideoView::UpdateDisplayArea( Dali::PropertyNotification& source )
716 Actor self( Self() );
718 bool positionUsesAnchorPoint = self.GetProperty( DevelActor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >();
719 Vector3 actorSize = self.GetCurrentSize() * self.GetCurrentScale();
720 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentAnchorPoint() : AnchorPoint::TOP_LEFT );
722 Vector2 screenPosition = self.GetProperty( DevelActor::Property::SCREEN_POSITION ).Get< Vector2 >();
724 mDisplayArea.x = screenPosition.x - anchorPointOffSet.x;
725 mDisplayArea.y = screenPosition.y - anchorPointOffSet.y;
726 mDisplayArea.width = actorSize.x;
727 mDisplayArea.height = actorSize.y;
729 mVideoPlayer.SetDisplayArea( mDisplayArea );
732 void VideoView::SetUnderlay( bool set )
734 if( set != mIsUnderlay )
740 SetWindowSurfaceTarget();
744 SetNativeImageTarget();
751 bool VideoView::IsUnderlay()
756 void VideoView::SetSWCodec( bool on )
758 // If setting SW or HW type is failed , video-view shows video by default codec type.
759 // The default codec type is selected by platform.
762 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::SW );
766 mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::HW );
770 int VideoView::GetPlayPosition()
772 return mVideoPlayer.GetPlayPosition();
775 void VideoView::SetPlayPosition( int pos )
777 mVideoPlayer.SetPlayPosition( pos );
780 void VideoView::SetDisplayMode( int mode )
782 mVideoPlayer.SetDisplayMode( static_cast< Dali::VideoPlayerPlugin::DisplayMode::Type >( mode ) );
785 int VideoView::GetDisplayMode() const
787 return static_cast< int >( mVideoPlayer.GetDisplayMode() );
790 } // namespace Internal
792 } // namespace toolkit