2 * Copyright (c) 2017 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 )
67 DALI_SIGNAL_REGISTRATION( Toolkit, VideoView, "finished", FINISHED_SIGNAL )
69 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "play", ACTION_VIDEOVIEW_PLAY )
70 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "pause", ACTION_VIDEOVIEW_PAUSE )
71 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "stop", ACTION_VIDEOVIEW_STOP )
72 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "forward", ACTION_VIDEOVIEW_FORWARD )
73 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "backward", ACTION_VIDEOVIEW_BACKWARD )
75 DALI_TYPE_REGISTRATION_END()
77 const char* const VOLUME_LEFT( "volumeLeft" );
78 const char* const VOLUME_RIGHT( "volumeRight" );
80 // 3.0 TC uses RENDERING_TARGET. It should be removed in next release
81 const char* const RENDERING_TARGET( "renderingTarget" );
82 const char* const WINDOW_SURFACE_TARGET( "windowSurfaceTarget" );
83 const char* const NATIVE_IMAGE_TARGET( "nativeImageTarget" );
85 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
86 attribute mediump vec2 aPosition;\n
87 uniform mediump mat4 uMvpMatrix;\n
88 uniform mediump vec3 uSize;\n
92 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
93 vertexPosition.xyz *= uSize;\n
94 gl_Position = uMvpMatrix * vertexPosition;\n
98 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
99 uniform lowp vec4 uColor;\n
100 uniform lowp vec3 mixColor;\n
101 uniform lowp float opacity;\n
105 gl_FragColor = vec4(mixColor, opacity)*uColor;\n
109 } // anonymous namepsace
111 VideoView::VideoView()
112 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
113 mCurrentVideoPlayPosition( 0 ),
117 mVideoPlayer = Dali::VideoPlayer::New();
120 VideoView::~VideoView()
124 Toolkit::VideoView VideoView::New()
126 VideoView* impl = new VideoView();
127 Toolkit::VideoView handle = Toolkit::VideoView( *impl );
134 void VideoView::OnInitialize()
136 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
137 SetWindowSurfaceTarget();
140 void VideoView::SetUrl( const std::string& url )
142 if( mUrl != url || !mPropertyMap.Empty() )
145 mPropertyMap.Clear();
150 Actor self( Self() );
151 Internal::InitializeVisual( self, mVisual, mNativeImage );
154 mVideoPlayer.SetUrl( mUrl );
157 void VideoView::SetPropertyMap( Property::Map map )
161 Actor self( Self() );
162 Internal::InitializeVisual( self, mVisual, mPropertyMap );
164 Property::Value* widthValue = mPropertyMap.Find( "width" );
168 if( widthValue->Get( width ) )
170 mVideoSize = ImageDimensions( width, mVideoSize.GetHeight() );
174 Property::Value* heightValue = mPropertyMap.Find( "height" );
178 if( heightValue->Get( height ) )
180 mVideoSize = ImageDimensions( mVideoSize.GetWidth(), height );
184 Property::Value* target = map.Find( RENDERING_TARGET );
185 std::string targetType;
187 if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
190 SetWindowSurfaceTarget();
192 else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
195 SetNativeImageTarget();
201 std::string VideoView::GetUrl()
206 void VideoView::SetLooping(bool looping)
208 mVideoPlayer.SetLooping( looping );
211 bool VideoView::IsLooping()
213 return mVideoPlayer.IsLooping();
216 void VideoView::Play()
220 Self().AddRenderer( mRenderer );
227 void VideoView::Pause()
229 mVideoPlayer.Pause();
233 void VideoView::Stop()
239 void VideoView::Forward( int millisecond )
241 int curPos = mVideoPlayer.GetPlayPosition();
243 int nextPos = curPos + millisecond;
245 mVideoPlayer.SetPlayPosition( nextPos );
248 void VideoView::Backward( int millisecond )
250 int curPos = mVideoPlayer.GetPlayPosition();
252 int nextPos = curPos - millisecond;
253 nextPos = ( nextPos < 0 )? 0: nextPos;
255 mVideoPlayer.SetPlayPosition( nextPos );
258 void VideoView::SetMute( bool mute )
260 mVideoPlayer.SetMute( mute );
263 bool VideoView::IsMuted()
265 return mVideoPlayer.IsMuted();
268 void VideoView::SetVolume( float left, float right )
270 mVideoPlayer.SetVolume( left, right );
273 void VideoView::GetVolume( float& left, float& right )
275 mVideoPlayer.GetVolume( left, right );
278 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
280 return mFinishedSignal;
283 void VideoView::EmitSignalFinish()
287 Self().RemoveRenderer( mRenderer );
290 if ( !mFinishedSignal.Empty() )
292 Dali::Toolkit::VideoView handle( GetOwner() );
293 mFinishedSignal.Emit( handle );
297 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
301 Dali::BaseHandle handle( object );
302 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
309 VideoView& impl = GetImpl( videoView );
311 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
316 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
321 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
326 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
329 if( attributes["videoForward"].Get( millisecond ) )
331 impl.Forward( millisecond );
335 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
338 if( attributes["videoBackward"].Get( millisecond ) )
340 impl.Backward( millisecond );
348 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
350 Dali::BaseHandle handle( object );
352 bool connected( true );
353 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
355 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
357 videoView.FinishedSignal().Connect( tracker, functor );
361 // signalName does not match any signal
368 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
370 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
374 VideoView& impl = GetImpl( videoView );
378 case Toolkit::VideoView::Property::VIDEO:
380 std::string videoUrl;
383 if( value.Get( videoUrl ) )
385 impl.SetUrl( videoUrl );
387 else if( value.Get( map ) )
389 Property::Value* shaderValue = map.Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
391 if( map.Count() > 1u || !shaderValue )
393 impl.SetPropertyMap( map );
395 else if( impl.mVisual && map.Count() == 1u && shaderValue )
397 Property::Map shaderMap;
398 if( shaderValue->Get( shaderMap ) )
400 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
401 visual.SetCustomShader( shaderMap );
402 if( videoView.OnStage() )
404 visual.SetOffStage( videoView );
405 visual.SetOnStage( videoView );
412 case Toolkit::VideoView::Property::LOOPING:
415 if( value.Get( looping ) )
417 impl.SetLooping( looping );
421 case Toolkit::VideoView::Property::MUTED:
424 if( value.Get( mute ) )
426 impl.SetMute( mute );
430 case Toolkit::VideoView::Property::VOLUME:
434 if( value.Get( map ) )
436 Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
437 Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
438 if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
440 impl.SetVolume( left, right );
445 case Toolkit::VideoView::Property::UNDERLAY:
448 if( value.Get( underlay ) )
450 impl.SetUnderlay( underlay );
458 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
460 Property::Value value;
461 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
465 VideoView& impl = GetImpl( videoView );
467 switch( propertyIndex )
469 case Toolkit::VideoView::Property::VIDEO:
471 if( !impl.mUrl.empty() )
475 else if( !impl.mPropertyMap.Empty() )
477 value = impl.mPropertyMap;
481 case Toolkit::VideoView::Property::LOOPING:
483 value = impl.IsLooping();
486 case Toolkit::VideoView::Property::MUTED:
488 value = impl.IsMuted();
491 case Toolkit::VideoView::Property::VOLUME:
496 impl.GetVolume( left, right );
497 map.Insert( VOLUME_LEFT, left );
498 map.Insert( VOLUME_RIGHT, right );
502 case Toolkit::VideoView::Property::UNDERLAY:
504 value = impl.IsUnderlay();
513 void VideoView::SetDepthIndex( int depthIndex )
517 mVisual.SetDepthIndex( depthIndex );
521 void VideoView::OnStageConnection( int depth )
525 CustomActor self = Self();
526 Toolkit::GetImplementation(mVisual).SetOnStage( self );
529 Control::OnStageConnection( depth );
532 void VideoView::OnStageDisconnection()
536 CustomActor self = Self();
537 Toolkit::GetImplementation(mVisual).SetOffStage( self );
540 Control::OnStageDisconnection();
543 Vector3 VideoView::GetNaturalSize()
546 size.x = mVideoSize.GetWidth();
547 size.y = mVideoSize.GetHeight();
549 if( size.x > 0 && size.y > 0 )
551 size.z = std::min( size.x, size.y );
556 return Control::GetNaturalSize();
560 float VideoView::GetHeightForWidth( float width )
562 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
564 return GetHeightForWidthBase( width );
568 return Control::GetHeightForWidthBase( width );
572 float VideoView::GetWidthForHeight( float height )
574 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
576 return GetWidthForHeightBase( height );
580 return Control::GetWidthForHeightBase( height );
584 void VideoView::SetWindowSurfaceTarget()
587 int curPos = mVideoPlayer.GetPlayPosition();
591 Toolkit::GetImplementation(mVisual).SetOffStage(self);
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 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle() );
608 mVideoPlayer.SetUrl( mUrl );
612 // For underlay rendering mode, video display area have to be transparent.
613 Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
614 Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
615 mRenderer = Renderer::New( geometry, shader );
617 mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
618 mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_RGB, BlendFactor::ONE );
619 mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_RGB, BlendFactor::ZERO );
620 mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_ALPHA, BlendFactor::ONE );
621 mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_ALPHA, BlendFactor::ZERO );
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() );
650 self.RemoveRenderer( mRenderer );
651 Dali::Stage::GetCurrent().KeepRendering( 0.0f );
653 self.RemovePropertyNotification( mPositionUpdateNotification );
654 self.RemovePropertyNotification( mSizeUpdateNotification );
655 self.RemovePropertyNotification( mScaleUpdateNotification );
657 int curPos = mVideoPlayer.GetPlayPosition();
660 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
661 mNativeImage = Dali::NativeImage::New( *nativeImageSourcePtr );
663 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
664 mVideoPlayer.SetUrl( mUrl );
666 Internal::InitializeVisual( self, mVisual, mNativeImage );
667 Self().RemoveRenderer( mRenderer );
676 mVideoPlayer.SetPlayPosition( curPos );
680 void VideoView::UpdateDisplayArea( Dali::PropertyNotification& source )
687 Actor self( Self() );
689 bool positionUsesAnchorPoint = self.GetProperty( DevelActor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >();
690 Vector3 actorSize = self.GetCurrentSize() * self.GetCurrentScale();
691 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentAnchorPoint() : AnchorPoint::TOP_LEFT );
693 Vector2 screenPosition = self.GetProperty( DevelActor::Property::SCREEN_POSITION ).Get< Vector2 >();
695 mDisplayArea.x = screenPosition.x - anchorPointOffSet.x;
696 mDisplayArea.y = screenPosition.y - anchorPointOffSet.y;
697 mDisplayArea.width = actorSize.x;
698 mDisplayArea.height = actorSize.y;
700 mVideoPlayer.SetDisplayArea( mDisplayArea );
703 void VideoView::SetUnderlay( bool set )
705 if( set != mIsUnderlay )
711 SetWindowSurfaceTarget();
715 SetNativeImageTarget();
722 bool VideoView::IsUnderlay()
727 } // namespace Internal
729 } // namespace toolkit