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( "renderingTarget" );
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 mIsNativeImageTarget( true ),
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::OnInitialize()
102 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
103 mNativeImage = Dali::NativeImage::New( *nativeImageSourcePtr );
105 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
106 mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
109 void VideoView::SetUrl( const std::string& url )
111 if( mUrl != url || !mPropertyMap.Empty() )
114 mPropertyMap.Clear();
117 if( mIsNativeImageTarget )
119 Actor self( Self() );
120 InitializeVisual( self, mVisual, mNativeImage );
123 mVideoPlayer.SetUrl( mUrl );
126 void VideoView::SetPropertyMap( Property::Map map )
130 Actor self( Self() );
131 InitializeVisual( self, mVisual, mPropertyMap );
133 Property::Value* widthValue = mPropertyMap.Find( "width" );
137 if( widthValue->Get( width ) )
139 mVideoSize = ImageDimensions( width, mVideoSize.GetHeight() );
143 Property::Value* heightValue = mPropertyMap.Find( "height" );
147 if( heightValue->Get( height ) )
149 mVideoSize = ImageDimensions( mVideoSize.GetWidth(), height );
156 std::string VideoView::GetUrl()
161 void VideoView::SetLooping(bool looping)
163 mVideoPlayer.SetLooping( looping );
166 bool VideoView::IsLooping()
168 return mVideoPlayer.IsLooping();
171 void VideoView::Play()
178 void VideoView::Pause()
180 mVideoPlayer.Pause();
185 void VideoView::Stop()
192 void VideoView::Forward( int millisecond )
194 int curPos = mVideoPlayer.GetPlayPosition();
196 int nextPos = curPos + millisecond;
198 mVideoPlayer.SetPlayPosition( nextPos );
201 void VideoView::Backward( int millisecond )
203 int curPos = mVideoPlayer.GetPlayPosition();
205 int nextPos = curPos - millisecond;
206 nextPos = ( nextPos < 0 )? 0: nextPos;
208 mVideoPlayer.SetPlayPosition( nextPos );
211 void VideoView::SetMute( bool mute )
213 mVideoPlayer.SetMute( mute );
216 bool VideoView::IsMuted()
218 return mVideoPlayer.IsMuted();
221 void VideoView::SetVolume( float left, float right )
223 mVideoPlayer.SetVolume( left, right );
226 void VideoView::GetVolume( float& left, float& right )
228 mVideoPlayer.GetVolume( left, right );
231 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
233 return mFinishedSignal;
236 void VideoView::EmitSignalFinish()
238 if ( !mFinishedSignal.Empty() )
240 Dali::Toolkit::VideoView handle( GetOwner() );
241 mFinishedSignal.Emit( handle );
245 bool VideoView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
249 Dali::BaseHandle handle( object );
250 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
257 VideoView& impl = GetImpl( videoView );
259 if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PLAY ) == 0 )
264 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_PAUSE ) == 0 )
269 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_STOP ) == 0 )
274 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_FORWARD ) == 0 )
277 if( attributes["videoForward"].Get( millisecond ) )
279 impl.Forward( millisecond );
283 else if( strcmp( actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD ) == 0 )
286 if( attributes["videoBackward"].Get( millisecond ) )
288 impl.Backward( millisecond );
296 bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
298 Dali::BaseHandle handle( object );
300 bool connected( true );
301 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( handle );
303 if( 0 == strcmp( signalName.c_str(), FINISHED_SIGNAL ) )
305 videoView.FinishedSignal().Connect( tracker, functor );
309 // signalName does not match any signal
316 void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
318 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
322 VideoView& impl = GetImpl( videoView );
326 case Toolkit::VideoView::Property::VIDEO:
328 std::string videoUrl;
329 if( value.Get( videoUrl ) )
331 impl.SetUrl( videoUrl );
335 if( value.Get( map ) )
337 impl.SetPropertyMap( map );
339 Property::Value* target = map.Find( RENDERING_TARGET );
340 std::string targetType;
341 if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
343 impl.SetWindowSurfaceTarget();
345 else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
347 impl.SetNativeImageTarget();
352 case Toolkit::VideoView::Property::LOOPING:
355 if( value.Get( looping ) )
357 impl.SetLooping( looping );
361 case Toolkit::VideoView::Property::MUTED:
364 if( value.Get( mute ) )
366 impl.SetMute( mute );
370 case Toolkit::VideoView::Property::VOLUME:
374 if( value.Get( map ) )
376 Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
377 Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
378 if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
380 impl.SetVolume( left, right );
389 Property::Value VideoView::GetProperty( BaseObject* object, Property::Index propertyIndex )
391 Property::Value value;
392 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
396 VideoView& impl = GetImpl( videoView );
398 switch( propertyIndex )
400 case Toolkit::VideoView::Property::VIDEO:
402 if( !impl.mUrl.empty() )
406 else if( !impl.mPropertyMap.Empty() )
408 value = impl.mPropertyMap;
412 case Toolkit::VideoView::Property::LOOPING:
414 value = impl.IsLooping();
417 case Toolkit::VideoView::Property::MUTED:
419 value = impl.IsMuted();
422 case Toolkit::VideoView::Property::VOLUME:
427 impl.GetVolume( left, right );
428 map.Insert( VOLUME_LEFT, left );
429 map.Insert( VOLUME_RIGHT, right );
439 void VideoView::SetDepthIndex( int depthIndex )
443 mVisual.SetDepthIndex( depthIndex );
447 void VideoView::OnStageConnection( int depth )
449 Control::OnStageConnection( depth );
453 CustomActor self = Self();
454 mVisual.SetOnStage( self );
458 void VideoView::OnStageDisconnection()
462 CustomActor self = Self();
463 mVisual.SetOffStage( self );
466 Control::OnStageDisconnection();
469 Vector3 VideoView::GetNaturalSize()
472 size.x = mVideoSize.GetWidth();
473 size.y = mVideoSize.GetHeight();
475 if( size.x > 0 && size.y > 0 )
477 size.z = std::min( size.x, size.y );
482 return Control::GetNaturalSize();
486 float VideoView::GetHeightForWidth( float width )
488 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
490 return GetHeightForWidthBase( width );
494 return Control::GetHeightForWidthBase( width );
498 float VideoView::GetWidthForHeight( float height )
500 if( mVideoSize.GetWidth() > 0 && mVideoSize.GetHeight() > 0 )
502 return GetWidthForHeightBase( height );
506 return Control::GetWidthForHeightBase( height );
510 void VideoView::SetWindowSurfaceTarget()
513 int curPos = mVideoPlayer.GetPlayPosition();
517 Toolkit::GetImplementation(mVisual).SetOffStage(self);
521 mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle() );
522 mVideoPlayer.SetUrl( mUrl );
524 mIsNativeImageTarget = false;
533 mVideoPlayer.Pause();
535 mVideoPlayer.SetPlayPosition( curPos );
538 void VideoView::SetNativeImageTarget()
540 Actor self( Self() );
541 int curPos = mVideoPlayer.GetPlayPosition();
544 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
545 mNativeImage = Dali::NativeImage::New( *nativeImageSourcePtr );
547 mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
548 mVideoPlayer.SetUrl( mUrl );
550 InitializeVisual( self, mVisual, mNativeImage );
551 mIsNativeImageTarget = true;
561 mVideoPlayer.Pause();
563 mVideoPlayer.SetPlayPosition( curPos );
566 } // namespace Internal
568 } // namespace Toolkit