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 )
66 DALI_SIGNAL_REGISTRATION( Toolkit, VideoView, "finished", FINISHED_SIGNAL )
68 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "play", ACTION_VIDEOVIEW_PLAY )
69 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "pause", ACTION_VIDEOVIEW_PAUSE )
70 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "stop", ACTION_VIDEOVIEW_STOP )
71 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "forward", ACTION_VIDEOVIEW_FORWARD )
72 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "backward", ACTION_VIDEOVIEW_BACKWARD )
74 DALI_TYPE_REGISTRATION_END()
76 const char* const VOLUME_LEFT( "volumeLeft" );
77 const char* const VOLUME_RIGHT( "volumeRight" );
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* VERTEX_SHADER = DALI_COMPOSE_SHADER(
83 attribute mediump vec2 aPosition;\n
84 uniform mediump mat4 uMvpMatrix;\n
85 uniform mediump vec3 uSize;\n
89 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
90 vertexPosition.xyz *= uSize;\n
91 gl_Position = uMvpMatrix * vertexPosition;\n
95 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
96 uniform lowp vec4 uColor;\n
97 uniform lowp vec3 mixColor;\n
98 uniform lowp float opacity;\n
102 gl_FragColor = vec4(mixColor, opacity)*uColor;\n
106 struct TriggerFunctor
108 TriggerFunctor( TriggerEventInterface* notification )
109 : mNotification( notification )
113 void operator()( bool& current, const PropertyInputContainer& inputs )
115 if( mNotification != NULL )
117 mNotification->Trigger();
121 TriggerEventInterface* mNotification;
124 } // anonymous namepsace
126 VideoView::VideoView()
127 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
128 mUpdateTriggerPropertyIndex( Property::INVALID_INDEX),
129 mNotification( NULL ),
130 mCurrentVideoPlayPosition( 0 ),
131 mIsNativeImageTarget( true ),
135 mVideoPlayer = Dali::VideoPlayer::New();
137 TriggerEventFactory triggerEventFactory;
138 mNotification = triggerEventFactory.CreateTriggerEvent( MakeCallback(this, &VideoView::UpdateDisplayArea ),
139 TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER);
142 VideoView::~VideoView()
144 if( mNotification != NULL )
146 delete mNotification;
150 Toolkit::VideoView VideoView::New()
152 VideoView* impl = new VideoView();
153 Toolkit::VideoView handle = Toolkit::VideoView( *impl );
160 void VideoView::OnInitialize()
163 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
164 mNativeImage = Dali::NativeImage::New( *nativeImageSourcePtr );
166 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
167 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
169 mUpdateTriggerPropertyIndex = Self().RegisterProperty( "updateTrigger", true );
172 void VideoView::SetUrl( const std::string& url )
174 if( mUrl != url || !mPropertyMap.Empty() )
177 mPropertyMap.Clear();
180 if( mIsNativeImageTarget )
182 Actor self( Self() );
183 Internal::InitializeVisual( self, mVisual, mNativeImage );
186 mVideoPlayer.SetUrl( mUrl );
189 void VideoView::SetPropertyMap( Property::Map map )
193 Actor self( Self() );
194 Internal::InitializeVisual( self, mVisual, mPropertyMap );
196 Property::Value* widthValue = mPropertyMap.Find( "width" );
200 if( widthValue->Get( width ) )
202 mVideoSize = ImageDimensions( width, mVideoSize.GetHeight() );
206 Property::Value* heightValue = mPropertyMap.Find( "height" );
210 if( heightValue->Get( height ) )
212 mVideoSize = ImageDimensions( mVideoSize.GetWidth(), height );
216 Property::Value* target = map.Find( RENDERING_TARGET );
217 std::string targetType;
219 if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
221 this->SetWindowSurfaceTarget();
223 else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
225 this->SetNativeImageTarget();
231 std::string VideoView::GetUrl()
236 void VideoView::SetLooping(bool looping)
238 mVideoPlayer.SetLooping( looping );
241 bool VideoView::IsLooping()
243 return mVideoPlayer.IsLooping();
246 void VideoView::Play()
253 void VideoView::Pause()
255 mVideoPlayer.Pause();
260 void VideoView::Stop()
267 void VideoView::Forward( int millisecond )
269 int curPos = mVideoPlayer.GetPlayPosition();
271 int nextPos = curPos + millisecond;
273 mVideoPlayer.SetPlayPosition( nextPos );
276 void VideoView::Backward( int millisecond )
278 int curPos = mVideoPlayer.GetPlayPosition();
280 int nextPos = curPos - millisecond;
281 nextPos = ( nextPos < 0 )? 0: nextPos;
283 mVideoPlayer.SetPlayPosition( nextPos );
286 void VideoView::SetMute( bool mute )
288 mVideoPlayer.SetMute( mute );
291 bool VideoView::IsMuted()
293 return mVideoPlayer.IsMuted();
296 void VideoView::SetVolume( float left, float right )
298 mVideoPlayer.SetVolume( left, right );
301 void VideoView::GetVolume( float& left, float& right )
303 mVideoPlayer.GetVolume( left, right );
306 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
308 return mFinishedSignal;
311 void VideoView::EmitSignalFinish()
313 if ( !mFinishedSignal.Empty() )
315 Dali::Toolkit::VideoView handle( GetOwner() );
316 mFinishedSignal.Emit( handle );
320 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
324 Dali::BaseHandle handle( object );
325 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
332 VideoView& impl = GetImpl( videoView );
334 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
339 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
344 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
349 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
352 if( attributes["videoForward"].Get( millisecond ) )
354 impl.Forward( millisecond );
358 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
361 if( attributes["videoBackward"].Get( millisecond ) )
363 impl.Backward( millisecond );
371 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
373 Dali::BaseHandle handle( object );
375 bool connected( true );
376 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
378 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
380 videoView.FinishedSignal().Connect( tracker, functor );
384 // signalName does not match any signal
391 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
393 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
397 VideoView& impl = GetImpl( videoView );
401 case Toolkit::VideoView::Property::VIDEO:
403 std::string videoUrl;
406 if( value.Get( videoUrl ) )
408 impl.SetUrl( videoUrl );
410 else if( value.Get( map ) )
412 Property::Value* shaderValue = map.Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
414 if( map.Count() > 1u || !shaderValue )
416 impl.SetPropertyMap( map );
418 else if( impl.mVisual && map.Count() == 1u && shaderValue )
420 Property::Map shaderMap;
421 if( shaderValue->Get( shaderMap ) )
423 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
424 visual.SetCustomShader( shaderMap );
425 if( videoView.OnStage() )
427 visual.SetOffStage( videoView );
428 visual.SetOnStage( videoView );
435 case Toolkit::VideoView::Property::LOOPING:
438 if( value.Get( looping ) )
440 impl.SetLooping( looping );
444 case Toolkit::VideoView::Property::MUTED:
447 if( value.Get( mute ) )
449 impl.SetMute( mute );
453 case Toolkit::VideoView::Property::VOLUME:
457 if( value.Get( map ) )
459 Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
460 Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
461 if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
463 impl.SetVolume( left, right );
472 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
474 Property::Value value;
475 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
479 VideoView& impl = GetImpl( videoView );
481 switch( propertyIndex )
483 case Toolkit::VideoView::Property::VIDEO:
485 if( !impl.mUrl.empty() )
489 else if( !impl.mPropertyMap.Empty() )
491 value = impl.mPropertyMap;
495 case Toolkit::VideoView::Property::LOOPING:
497 value = impl.IsLooping();
500 case Toolkit::VideoView::Property::MUTED:
502 value = impl.IsMuted();
505 case Toolkit::VideoView::Property::VOLUME:
510 impl.GetVolume( left, right );
511 map.Insert( VOLUME_LEFT, left );
512 map.Insert( VOLUME_RIGHT, right );
522 void VideoView::SetDepthIndex( int depthIndex )
526 mVisual.SetDepthIndex( depthIndex );
530 void VideoView::OnStageConnection( int depth )
534 CustomActor self = Self();
535 Toolkit::GetImplementation(mVisual).SetOnStage( self );
538 Control::OnStageConnection( depth );
541 void VideoView::OnStageDisconnection()
545 CustomActor self = Self();
546 Toolkit::GetImplementation(mVisual).SetOffStage( self );
549 Control::OnStageDisconnection();
552 Vector3 VideoView::GetNaturalSize()
555 size.x = mVideoSize.GetWidth();
556 size.y = mVideoSize.GetHeight();
558 if( size.x > 0 && size.y > 0 )
560 size.z = std::min( size.x, size.y );
565 return Control::GetNaturalSize();
569 float VideoView::GetHeightForWidth( float width )
571 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
573 return GetHeightForWidthBase( width );
577 return Control::GetHeightForWidthBase( width );
581 float VideoView::GetWidthForHeight( float height )
583 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
585 return GetWidthForHeightBase( height );
589 return Control::GetWidthForHeightBase( height );
593 void VideoView::SetWindowSurfaceTarget()
596 int curPos = mVideoPlayer.GetPlayPosition();
600 Toolkit::GetImplementation(mVisual).SetOffStage(self);
604 Constraint constraint = Constraint::New<bool>( self, mUpdateTriggerPropertyIndex, TriggerFunctor( mNotification ) );
605 constraint.AddSource( Source( self, Actor::Property::POSITION ) );
606 constraint.AddSource( Source( self, Actor::Property::SIZE ) );
609 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle() );
610 mVideoPlayer.SetUrl( mUrl );
612 mIsNativeImageTarget = false;
621 mVideoPlayer.Pause();
626 mVideoPlayer.SetPlayPosition( curPos );
629 // For underlay rendering mode, video display area have to be transparent.
630 Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
631 Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
632 Renderer renderer = Renderer::New( geometry, shader );
634 renderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
635 renderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_RGB, BlendFactor::ONE );
636 renderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_RGB, BlendFactor::ZERO );
637 renderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_ALPHA, BlendFactor::ONE );
638 renderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_ALPHA, BlendFactor::ZERO );
639 self.AddRenderer( renderer );
642 void VideoView::SetNativeImageTarget()
644 Actor self( Self() );
645 int curPos = mVideoPlayer.GetPlayPosition();
648 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
649 mNativeImage = Dali::NativeImage::New( *nativeImageSourcePtr );
651 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
652 mVideoPlayer.SetUrl( mUrl );
654 Internal::InitializeVisual( self, mVisual, mNativeImage );
655 mIsNativeImageTarget = true;
665 mVideoPlayer.Pause();
669 mVideoPlayer.SetPlayPosition( curPos );
673 void VideoView::UpdateDisplayArea()
675 Actor self( Self() );
677 bool positionUsesAnchorPoint = self.GetProperty( DevelActor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >();
678 Vector3 actorSize = self.GetCurrentSize() * self.GetCurrentScale();
679 Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentAnchorPoint() : AnchorPoint::TOP_LEFT );
681 Vector2 screenPosition = self.GetProperty( DevelActor::Property::SCREEN_POSITION ).Get< Vector2 >();
683 mDisplayArea.x = screenPosition.x - anchorPointOffSet.x;
684 mDisplayArea.y = screenPosition.y - anchorPointOffSet.y;
685 mDisplayArea.width = actorSize.x;
686 mDisplayArea.height = actorSize.y;
688 mVideoPlayer.SetDisplayArea( mDisplayArea );
691 } // namespace Internal
693 } // namespace toolkit