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>
31 #include <dali-toolkit/public-api/controls/video-view/video-view.h>
32 #include <dali-toolkit/public-api/visuals/visual-properties.h>
33 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
34 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
35 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
36 #include <dali-toolkit/internal/visuals/visual-factory-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 )
62 DALI_SIGNAL_REGISTRATION( Toolkit, VideoView, "finished", FINISHED_SIGNAL )
64 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "play", ACTION_VIDEOVIEW_PLAY )
65 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "pause", ACTION_VIDEOVIEW_PAUSE )
66 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "stop", ACTION_VIDEOVIEW_STOP )
67 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "forward", ACTION_VIDEOVIEW_FORWARD )
68 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "backward", ACTION_VIDEOVIEW_BACKWARD )
70 DALI_TYPE_REGISTRATION_END()
72 const char* const VOLUME_LEFT( "volumeLeft" );
73 const char* const VOLUME_RIGHT( "volumeRight" );
74 const char* const RENDERING_TARGET( "renderingTarget" );
75 const char* const WINDOW_SURFACE_TARGET( "windowSurfaceTarget" );
76 const char* const NATIVE_IMAGE_TARGET( "nativeImageTarget" );
78 } // anonymous namepsace
80 VideoView::VideoView()
81 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
82 mCurrentVideoPlayPosition( 0 ),
83 mIsNativeImageTarget( true ),
87 mVideoPlayer = Dali::VideoPlayer::New();
90 VideoView::~VideoView()
94 Toolkit::VideoView VideoView::New()
96 VideoView* impl = new VideoView();
97 Toolkit::VideoView handle = Toolkit::VideoView( *impl );
104 void VideoView::OnInitialize()
107 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
108 mNativeImage = Dali::NativeImage::New( *nativeImageSourcePtr );
110 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
111 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
114 void VideoView::SetUrl( const std::string& url )
116 if( mUrl != url || !mPropertyMap.Empty() )
119 mPropertyMap.Clear();
122 if( mIsNativeImageTarget )
124 Actor self( Self() );
125 Internal::InitializeVisual( self, mVisual, mNativeImage );
128 mVideoPlayer.SetUrl( mUrl );
131 void VideoView::SetPropertyMap( Property::Map map )
135 Actor self( Self() );
136 Internal::InitializeVisual( self, mVisual, mPropertyMap );
138 Property::Value* widthValue = mPropertyMap.Find( "width" );
142 if( widthValue->Get( width ) )
144 mVideoSize = ImageDimensions( width, mVideoSize.GetHeight() );
148 Property::Value* heightValue = mPropertyMap.Find( "height" );
152 if( heightValue->Get( height ) )
154 mVideoSize = ImageDimensions( mVideoSize.GetWidth(), height );
158 Property::Value* target = map.Find( RENDERING_TARGET );
159 std::string targetType;
161 if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
163 this->SetWindowSurfaceTarget();
165 else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
167 this->SetNativeImageTarget();
173 std::string VideoView::GetUrl()
178 void VideoView::SetLooping(bool looping)
180 mVideoPlayer.SetLooping( looping );
183 bool VideoView::IsLooping()
185 return mVideoPlayer.IsLooping();
188 void VideoView::Play()
195 void VideoView::Pause()
197 mVideoPlayer.Pause();
202 void VideoView::Stop()
209 void VideoView::Forward( int millisecond )
211 int curPos = mVideoPlayer.GetPlayPosition();
213 int nextPos = curPos + millisecond;
215 mVideoPlayer.SetPlayPosition( nextPos );
218 void VideoView::Backward( int millisecond )
220 int curPos = mVideoPlayer.GetPlayPosition();
222 int nextPos = curPos - millisecond;
223 nextPos = ( nextPos < 0 )? 0: nextPos;
225 mVideoPlayer.SetPlayPosition( nextPos );
228 void VideoView::SetMute( bool mute )
230 mVideoPlayer.SetMute( mute );
233 bool VideoView::IsMuted()
235 return mVideoPlayer.IsMuted();
238 void VideoView::SetVolume( float left, float right )
240 mVideoPlayer.SetVolume( left, right );
243 void VideoView::GetVolume( float& left, float& right )
245 mVideoPlayer.GetVolume( left, right );
248 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
250 return mFinishedSignal;
253 void VideoView::EmitSignalFinish()
255 if ( !mFinishedSignal.Empty() )
257 Dali::Toolkit::VideoView handle( GetOwner() );
258 mFinishedSignal.Emit( handle );
262 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
266 Dali::BaseHandle handle( object );
267 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
274 VideoView& impl = GetImpl( videoView );
276 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
281 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
286 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
291 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
294 if( attributes["videoForward"].Get( millisecond ) )
296 impl.Forward( millisecond );
300 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
303 if( attributes["videoBackward"].Get( millisecond ) )
305 impl.Backward( millisecond );
313 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
315 Dali::BaseHandle handle( object );
317 bool connected( true );
318 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
320 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
322 videoView.FinishedSignal().Connect( tracker, functor );
326 // signalName does not match any signal
333 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
335 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
339 VideoView& impl = GetImpl( videoView );
343 case Toolkit::VideoView::Property::VIDEO:
345 std::string videoUrl;
348 if( value.Get( videoUrl ) )
350 impl.SetUrl( videoUrl );
352 else if( value.Get( map ) )
354 Property::Value* shaderValue = map.Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
356 if( map.Count() > 1u || !shaderValue )
358 impl.SetPropertyMap( map );
360 else if( impl.mVisual && map.Count() == 1u && shaderValue )
362 Property::Map shaderMap;
363 if( shaderValue->Get( shaderMap ) )
365 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
366 visual.SetCustomShader( shaderMap );
367 if( videoView.OnStage() )
369 visual.SetOffStage( videoView );
370 visual.SetOnStage( videoView );
377 case Toolkit::VideoView::Property::LOOPING:
380 if( value.Get( looping ) )
382 impl.SetLooping( looping );
386 case Toolkit::VideoView::Property::MUTED:
389 if( value.Get( mute ) )
391 impl.SetMute( mute );
395 case Toolkit::VideoView::Property::VOLUME:
399 if( value.Get( map ) )
401 Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
402 Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
403 if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
405 impl.SetVolume( left, right );
414 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
416 Property::Value value;
417 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
421 VideoView& impl = GetImpl( videoView );
423 switch( propertyIndex )
425 case Toolkit::VideoView::Property::VIDEO:
427 if( !impl.mUrl.empty() )
431 else if( !impl.mPropertyMap.Empty() )
433 value = impl.mPropertyMap;
437 case Toolkit::VideoView::Property::LOOPING:
439 value = impl.IsLooping();
442 case Toolkit::VideoView::Property::MUTED:
444 value = impl.IsMuted();
447 case Toolkit::VideoView::Property::VOLUME:
452 impl.GetVolume( left, right );
453 map.Insert( VOLUME_LEFT, left );
454 map.Insert( VOLUME_RIGHT, right );
464 void VideoView::SetDepthIndex( int depthIndex )
468 mVisual.SetDepthIndex( depthIndex );
472 void VideoView::OnStageConnection( int depth )
476 CustomActor self = Self();
477 Toolkit::GetImplementation(mVisual).SetOnStage( self );
480 Control::OnStageConnection( depth );
483 void VideoView::OnStageDisconnection()
487 CustomActor self = Self();
488 Toolkit::GetImplementation(mVisual).SetOffStage( self );
491 Control::OnStageDisconnection();
494 Vector3 VideoView::GetNaturalSize()
497 size.x = mVideoSize.GetWidth();
498 size.y = mVideoSize.GetHeight();
500 if( size.x > 0 && size.y > 0 )
502 size.z = std::min( size.x, size.y );
507 return Control::GetNaturalSize();
511 float VideoView::GetHeightForWidth( float width )
513 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
515 return GetHeightForWidthBase( width );
519 return Control::GetHeightForWidthBase( width );
523 float VideoView::GetWidthForHeight( float height )
525 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
527 return GetWidthForHeightBase( height );
531 return Control::GetWidthForHeightBase( height );
535 void VideoView::SetWindowSurfaceTarget()
538 int curPos = mVideoPlayer.GetPlayPosition();
542 Toolkit::GetImplementation(mVisual).SetOffStage(self);
546 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle() );
547 mVideoPlayer.SetUrl( mUrl );
549 mIsNativeImageTarget = false;
558 mVideoPlayer.Pause();
560 mVideoPlayer.SetPlayPosition( curPos );
563 void VideoView::SetNativeImageTarget()
565 Actor self( Self() );
566 int curPos = mVideoPlayer.GetPlayPosition();
569 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
570 mNativeImage = Dali::NativeImage::New( *nativeImageSourcePtr );
572 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
573 mVideoPlayer.SetUrl( mUrl );
575 Internal::InitializeVisual( self, mVisual, mNativeImage );
576 mIsNativeImageTarget = true;
586 mVideoPlayer.Pause();
588 mVideoPlayer.SetPlayPosition( curPos );
591 } // namespace Internal
593 } // namespace Toolkit