2 * Copyright (c) 2016 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>
47 return Toolkit::VideoView::New();
50 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::VideoView, Toolkit::Control, Create );
52 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "video", MAP, VIDEO )
53 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "looping", BOOLEAN, LOOPING )
54 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "muted", BOOLEAN, MUTED )
55 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "volume", MAP, VOLUME )
57 DALI_SIGNAL_REGISTRATION( Toolkit, VideoView, "finished", FINISHED_SIGNAL )
59 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "play", ACTION_VIDEOVIEW_PLAY )
60 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "pause", ACTION_VIDEOVIEW_PAUSE )
61 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "stop", ACTION_VIDEOVIEW_STOP )
62 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "forward", ACTION_VIDEOVIEW_FORWARD )
63 DALI_ACTION_REGISTRATION( Toolkit, VideoView, "backward", ACTION_VIDEOVIEW_BACKWARD )
65 DALI_TYPE_REGISTRATION_END()
67 const char* const VOLUME_LEFT( "volumeLeft" );
68 const char* const VOLUME_RIGHT( "volumeRight" );
69 const char* const RENDERING_TARGET( "RENDERING_TARGET" );
70 const char* const WINDOW_SURFACE_TARGET( "windowSurfaceTarget" );
71 const char* const NATIVE_IMAGE_TARGET( "nativeImageTarget" );
73 } // anonymous namepsace
75 VideoView::VideoView()
76 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
77 mCurrentVideoPlayPosition( 0 ),
78 mSetRenderingTarget( false ),
82 mVideoPlayer = Dali::VideoPlayer::New();
85 VideoView::~VideoView()
89 Toolkit::VideoView VideoView::New()
91 VideoView* impl = new VideoView();
92 Toolkit::VideoView handle = Toolkit::VideoView( *impl );
99 void VideoView::SetUrl( const std::string& url )
101 if( mUrl != url || !mPropertyMap.Empty() )
103 mPropertyMap.Clear();
108 if( mSetRenderingTarget )
110 mVideoPlayer.SetUrl( mUrl );
114 SetNativeImageTarget();
118 void VideoView::SetPropertyMap( Property::Map map )
122 Actor self( Self() );
123 InitializeVisual( self, mVisual, mPropertyMap );
125 Property::Value* widthValue = mPropertyMap.Find( "width" );
129 if( widthValue->Get( width ) )
131 mVideoSize = ImageDimensions( width, mVideoSize.GetHeight() );
135 Property::Value* heightValue = mPropertyMap.Find( "height" );
139 if( heightValue->Get( height ) )
141 mVideoSize = ImageDimensions( mVideoSize.GetWidth(), height );
148 std::string VideoView::GetUrl()
153 void VideoView::SetLooping(bool looping)
155 mVideoPlayer.SetLooping( looping );
158 bool VideoView::IsLooping()
160 return mVideoPlayer.IsLooping();
163 void VideoView::Play()
170 void VideoView::Pause()
172 mVideoPlayer.Pause();
177 void VideoView::Stop()
184 void VideoView::Forward( int millisecond )
186 int curPos = mVideoPlayer.GetPlayPosition();
188 int nextPos = curPos + millisecond;
190 mVideoPlayer.SetPlayPosition( nextPos );
193 void VideoView::Backward( int millisecond )
195 int curPos = mVideoPlayer.GetPlayPosition();
197 int nextPos = curPos - millisecond;
198 nextPos = ( nextPos < 0 )? 0: nextPos;
200 mVideoPlayer.SetPlayPosition( nextPos );
203 void VideoView::SetMute( bool mute )
205 mVideoPlayer.SetMute( mute );
208 bool VideoView::IsMuted()
210 return mVideoPlayer.IsMuted();
213 void VideoView::SetVolume( float left, float right )
215 mVideoPlayer.SetVolume( left, right );
218 void VideoView::GetVolume( float& left, float& right )
220 mVideoPlayer.GetVolume( left, right );
223 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
225 return mFinishedSignal;
228 void VideoView::EmitSignalFinish()
230 if ( !mFinishedSignal.Empty() )
232 Dali::Toolkit::VideoView handle( GetOwner() );
233 mFinishedSignal.Emit( handle );
237 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
241 Dali::BaseHandle handle( object );
242 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
249 VideoView& impl = GetImpl( videoView );
251 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
256 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
261 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
266 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
269 if( attributes["videoForward"].Get( millisecond ) )
271 impl.Forward( millisecond );
275 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
278 if( attributes["videoBackward"].Get( millisecond ) )
280 impl.Backward( millisecond );
288 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
290 Dali::BaseHandle handle( object );
292 bool connected( true );
293 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
295 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
297 videoView.FinishedSignal().Connect( tracker, functor );
301 // signalName does not match any signal
308 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
310 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
314 VideoView& impl = GetImpl( videoView );
318 case Toolkit::VideoView::Property::VIDEO:
320 std::string videoUrl;
321 if( value.Get( videoUrl ) )
323 impl.SetUrl( videoUrl );
327 if( value.Get( map ) )
329 impl.SetPropertyMap( map );
331 Property::Value* target = map.Find( RENDERING_TARGET );
332 std::string targetType;
333 if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
335 impl.SetWindowSurfaceTarget();
337 else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
339 impl.SetNativeImageTarget();
344 case Toolkit::VideoView::Property::LOOPING:
347 if( value.Get( looping ) )
349 impl.SetLooping( looping );
353 case Toolkit::VideoView::Property::MUTED:
356 if( value.Get( mute ) )
358 impl.SetMute( mute );
362 case Toolkit::VideoView::Property::VOLUME:
366 if( value.Get( map ) )
368 Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
369 Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
370 if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
372 impl.SetVolume( left, right );
381 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
383 Property::Value value;
384 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
388 VideoView& impl = GetImpl( videoView );
390 switch( propertyIndex )
392 case Toolkit::VideoView::Property::VIDEO:
394 if( !impl.mUrl.empty() )
398 else if( !impl.mPropertyMap.Empty() )
400 value = impl.mPropertyMap;
404 case Toolkit::VideoView::Property::LOOPING:
406 value = impl.IsLooping();
409 case Toolkit::VideoView::Property::MUTED:
411 value = impl.IsMuted();
414 case Toolkit::VideoView::Property::VOLUME:
419 impl.GetVolume( left, right );
420 map.Insert( VOLUME_LEFT, left );
421 map.Insert( VOLUME_RIGHT, right );
431 void VideoView::SetDepthIndex( int depthIndex )
435 mVisual.SetDepthIndex( depthIndex );
439 void VideoView::OnStageConnection( int depth )
441 Control::OnStageConnection( depth );
445 CustomActor self = Self();
446 mVisual.SetOnStage( self );
450 void VideoView::OnStageDisconnection()
454 CustomActor self = Self();
455 mVisual.SetOffStage( self );
458 Control::OnStageDisconnection();
461 Vector3 VideoView::GetNaturalSize()
464 size.x = mVideoSize.GetWidth();
465 size.y = mVideoSize.GetHeight();
467 if( size.x > 0 && size.y > 0 )
469 size.z = std::min( size.x, size.y );
474 return Control::GetNaturalSize();
478 float VideoView::GetHeightForWidth( float width )
480 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
482 return GetHeightForWidthBase( width );
486 return Control::GetHeightForWidthBase( width );
490 float VideoView::GetWidthForHeight( float height )
492 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
494 return GetWidthForHeightBase( height );
498 return Control::GetWidthForHeightBase( height );
502 void VideoView::SetWindowSurfaceTarget()
505 int curPos = mVideoPlayer.GetPlayPosition();
507 mSetRenderingTarget = true;
508 mVisual.RemoveAndReset( self );
510 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle() );
511 mVideoPlayer.SetUrl( mUrl );
512 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
521 mVideoPlayer.Pause();
523 mVideoPlayer.SetPlayPosition( curPos );
526 void VideoView::SetNativeImageTarget()
528 Actor self( Self() );
529 int curPos = mVideoPlayer.GetPlayPosition();
531 mSetRenderingTarget = true;
534 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
535 mNativeImage = Dali::NativeImage::New( *nativeImageSourcePtr );
537 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
538 mVideoPlayer.SetUrl( mUrl );
539 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
541 InitializeVisual( self, mVisual, mNativeImage );
551 mVideoPlayer.Pause();
553 mVideoPlayer.SetPlayPosition( curPos );
556 } // namespace Internal
558 } // namespace Toolkit