[AT-SPI] Squashed implementation
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / video-view / video-view-impl.cpp
index 3db94fa..9f6fc31 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <cstring>
 #include <dali/public-api/object/type-registry.h>
 #include <dali/public-api/object/type-registry-helper.h>
-#include <dali/public-api/common/stage.h>
 #include <dali/devel-api/scripting/scripting.h>
+#include <dali/devel-api/adaptor-framework/window-devel.h>
 #include <dali/public-api/adaptor-framework/native-image-source.h>
-#include <dali/integration-api/adaptors/adaptor.h>
 #include <dali/integration-api/debug.h>
 #include <dali/public-api/animation/constraint.h>
 #include <dali/devel-api/actors/actor-devel.h>
@@ -33,6 +32,8 @@
 // INTERNAL INCLUDES
 #include <dali-toolkit/public-api/controls/video-view/video-view.h>
 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
+#include <dali/integration-api/adaptor-framework/adaptor.h>
+#include <dali-toolkit/internal/controls/control/control-data-impl.h>
 
 namespace Dali
 {
@@ -87,8 +88,8 @@ const char* const CUSTOM_SAMPLER_TYPE_NAME( "samplerExternalOES" );
 
 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
   attribute mediump vec2 aPosition;\n
-  uniform mediump mat4 uMvpMatrix;\n
-  uniform mediump vec3 uSize;\n
+  uniform highp   mat4 uMvpMatrix;\n
+  uniform highp   vec3 uSize;\n
   \n
   void main()\n
   {\n
@@ -99,20 +100,18 @@ const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
 );
 
 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
-  uniform lowp vec4 uColor;\n
-  uniform lowp vec3 mixColor;\n
   \n
   void main()\n
   {\n
-    gl_FragColor = vec4(mixColor, 1.0)*uColor;\n
+    gl_FragColor = vec4(0.0);\n
   }\n
 );
 
 const char* VERTEX_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
   attribute mediump vec2 aPosition;\n
   varying mediump vec2 vTexCoord;\n
-  uniform mediump mat4 uMvpMatrix;\n
-  uniform mediump vec3 uSize;\n
+  uniform highp   mat4 uMvpMatrix;\n
+  uniform highp   vec3 uSize;\n
   varying mediump vec2 sTexCoordRect;\n
   void main()\n
   {\n
@@ -133,33 +132,39 @@ const char* FRAGMENT_SHADER_TEXTURE = DALI_COMPOSE_SHADER(
 
 } // anonymous namepsace
 
-VideoView::VideoView()
+VideoView::VideoView( Dali::VideoSyncMode syncMode )
 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
   mCurrentVideoPlayPosition( 0 ),
+  mFrameID( 0 ),
   mIsPlay( false ),
-  mIsUnderlay( true )
+  mIsUnderlay( true ),
+  mSyncMode( syncMode )
 {
-  mVideoPlayer = Dali::VideoPlayer::New();
+  DevelControl::SetAccessibilityConstructor( Self(), []( Dali::Actor actor )
+    {
+      return std::unique_ptr< Dali::Accessibility::Accessible >(
+        new Control::Impl::AccessibleImpl( actor, Dali::Accessibility::Role::VIDEO, true ) );
+    }
+  );
 }
 
 VideoView::~VideoView()
 {
 }
 
-Toolkit::VideoView VideoView::New()
+Toolkit::VideoView VideoView::New( VideoSyncMode syncMode )
 {
-  VideoView* impl = new VideoView();
+  VideoView* impl = new VideoView( syncMode );
   Toolkit::VideoView handle = Toolkit::VideoView( *impl );
 
+  impl->mVideoPlayer = Dali::VideoPlayer::New( impl->Self(), syncMode );
   impl->Initialize();
-
   return handle;
 }
 
 void VideoView::OnInitialize()
 {
   mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
-  SetWindowSurfaceTarget();
 }
 
 void VideoView::SetUrl( const std::string& url )
@@ -196,7 +201,11 @@ void VideoView::SetPropertyMap( Property::Map map )
 
     if( shaderValue )
     {
-      mEffectPropertyMap = *( shaderValue->GetMap() );
+      Property::Map* shaderMap = shaderValue->GetMap();
+      if( shaderMap )
+      {
+        mEffectPropertyMap = *shaderMap;
+      }
     }
   }
 
@@ -226,11 +235,6 @@ bool VideoView::IsLooping()
 
 void VideoView::Play()
 {
-  if( mIsUnderlay )
-  {
-    Self().AddRenderer( mOverlayRenderer );
-  }
-
   mVideoPlayer.Play();
   mIsPlay = true;
 }
@@ -293,11 +297,6 @@ Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
 
 void VideoView::EmitSignalFinish()
 {
-  if( mIsUnderlay )
-  {
-    Self().RemoveRenderer( mOverlayRenderer );
-  }
-
   if ( !mFinishedSignal.Empty() )
   {
     Dali::Toolkit::VideoView handle( GetOwner() );
@@ -376,91 +375,103 @@ bool VideoView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface*
   return connected;
 }
 
-void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
+void VideoView::SetPropertyInternal( Property::Index index, const Property::Value& value )
 {
-  Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
-
-  if( videoView )
+  switch( index )
   {
-    VideoView& impl = GetImpl( videoView );
-
-    switch( index )
+    case Toolkit::VideoView::Property::VIDEO:
     {
-      case Toolkit::VideoView::Property::VIDEO:
-      {
-        std::string videoUrl;
-        Property::Map map;
+      std::string videoUrl;
+      Property::Map map;
 
-        if( value.Get( videoUrl ) )
-        {
-          impl.SetUrl( videoUrl );
-        }
-        else if( value.Get( map ) )
-        {
-            impl.SetPropertyMap( map );
-          }
-        break;
+      if( value.Get( videoUrl ) )
+      {
+        SetUrl( videoUrl );
       }
-      case Toolkit::VideoView::Property::LOOPING:
+      else if( value.Get( map ) )
       {
-        bool looping;
-        if( value.Get( looping ) )
-        {
-          impl.SetLooping( looping );
-        }
-        break;
+        SetPropertyMap( map );
       }
-      case Toolkit::VideoView::Property::MUTED:
+      break;
+    }
+    case Toolkit::VideoView::Property::LOOPING:
+    {
+      bool looping;
+      if( value.Get( looping ) )
       {
-        bool mute;
-        if( value.Get( mute ) )
-        {
-          impl.SetMute( mute );
-        }
-        break;
+        SetLooping( looping );
       }
-      case Toolkit::VideoView::Property::VOLUME:
+      break;
+    }
+    case Toolkit::VideoView::Property::MUTED:
+    {
+      bool mute;
+      if( value.Get( mute ) )
       {
-        Property::Map map;
-        float left, right;
-        if( value.Get( map ) )
-        {
-          Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
-          Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
-          if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
-          {
-            impl.SetVolume( left, right );
-          }
-        }
-        break;
+        SetMute( mute );
       }
-      case Toolkit::VideoView::Property::UNDERLAY:
+      break;
+    }
+    case Toolkit::VideoView::Property::VOLUME:
+    {
+      Property::Map map;
+      float left, right;
+      if( value.Get( map ) )
       {
-        bool underlay;
-        if( value.Get( underlay ) )
+        Property::Value* volumeLeft = map.Find( VOLUME_LEFT );
+        Property::Value* volumeRight = map.Find( VOLUME_RIGHT );
+        if( volumeLeft && volumeLeft->Get( left ) && volumeRight && volumeRight->Get( right ) )
         {
-          impl.SetUnderlay( underlay );
+          SetVolume( left, right );
         }
-        break;
       }
-      case Toolkit::VideoView::Property::PLAY_POSITION:
+      break;
+    }
+    case Toolkit::VideoView::Property::UNDERLAY:
+    {
+      bool underlay;
+      if( value.Get( underlay ) )
       {
-        int pos;
-        if( value.Get( pos ) )
-        {
-          impl.SetPlayPosition( pos );
-        }
-        break;
+        SetUnderlay( underlay );
       }
-      case Toolkit::VideoView::Property::DISPLAY_MODE:
+      break;
+    }
+    case Toolkit::VideoView::Property::PLAY_POSITION:
+    {
+      int pos;
+      if( value.Get( pos ) )
       {
-        int mode;
-        if( value.Get( mode ) )
-        {
-          impl.SetDisplayMode( mode );
-        }
-        break;
+        SetPlayPosition( pos );
       }
+      break;
+    }
+    case Toolkit::VideoView::Property::DISPLAY_MODE:
+    {
+      int mode;
+      if( value.Get( mode ) )
+      {
+        SetDisplayMode( mode );
+      }
+      break;
+    }
+  }
+}
+
+void VideoView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
+{
+  Toolkit::VideoView videoView = Toolkit::VideoView::DownCast( Dali::BaseHandle( object ) );
+
+  if( videoView )
+  {
+    VideoView& impl = GetImpl( videoView );
+
+    impl.SetPropertyInternal( index, value );
+
+    if( index != Toolkit::VideoView::Property::UNDERLAY )
+    {
+      // Backup values.
+      // These values will be used when underlay mode is changed.
+      impl.mPropertyBackup[index] = value;
     }
   }
 }
@@ -538,14 +549,29 @@ void VideoView::SetDepthIndex( int depthIndex )
   }
 }
 
-void VideoView::OnStageConnection( int depth )
+void VideoView::OnSceneConnection( int depth )
 {
-  Control::OnStageConnection( depth );
+  Control::OnSceneConnection( depth );
+
+  if( mIsUnderlay )
+  {
+    SetWindowSurfaceTarget();
+  }
+}
+
+void VideoView::OnSceneDisconnection()
+{
+  Control::OnSceneDisconnection();
 }
 
-void VideoView::OnStageDisconnection()
+void VideoView::OnSizeSet( const Vector3& targetSize )
 {
-  Control::OnStageDisconnection();
+  if( mIsUnderlay && mSyncMode == Dali::VideoSyncMode::ENABLED )
+  {
+    SetFrameRenderCallback();
+    mVideoPlayer.StartSynchronization();
+  }
+  Control::OnSizeSet( targetSize );
 }
 
 Vector3 VideoView::GetNaturalSize()
@@ -592,6 +618,13 @@ float VideoView::GetWidthForHeight( float height )
 void VideoView::SetWindowSurfaceTarget()
 {
   Actor self = Self();
+
+  if( !self.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
+  {
+    // When the control is off the stage, it does not have Window.
+    return;
+  }
+
   int curPos = mVideoPlayer.GetPlayPosition();
 
   if( mIsPlay )
@@ -611,8 +644,10 @@ void VideoView::SetWindowSurfaceTarget()
     self.RemoveRenderer( mTextureRenderer );
   }
 
-  mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle() );
-  mVideoPlayer.SetUrl( mUrl );
+  // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
+  mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle( self ) );
+
+  ApplyBackupProperties();
 
   if( !mOverlayRenderer )
   {
@@ -620,13 +655,9 @@ void VideoView::SetWindowSurfaceTarget()
     Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
     Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
     mOverlayRenderer = Renderer::New( geometry, shader );
-
-    mOverlayRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
-    mOverlayRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_RGB, BlendFactor::ONE );
-    mOverlayRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_RGB, BlendFactor::ZERO );
-    mOverlayRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_ALPHA, BlendFactor::ONE );
-    mOverlayRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_ALPHA, BlendFactor::ZERO );
+    mOverlayRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::OFF );
   }
+  Self().AddRenderer( mOverlayRenderer );
 
   if( mIsPlay )
   {
@@ -658,6 +689,8 @@ void VideoView::SetNativeImageTarget()
   if( mOverlayRenderer )
   {
     self.RemoveRenderer( mOverlayRenderer );
+
+    mOverlayRenderer.Reset();
   }
 
   self.RemovePropertyNotification( mPositionUpdateNotification );
@@ -687,8 +720,10 @@ void VideoView::SetNativeImageTarget()
   }
   Self().AddRenderer( mTextureRenderer );
 
+  // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
   mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
-  mVideoPlayer.SetUrl( mUrl );
+
+  ApplyBackupProperties();
 
   if( mIsPlay )
   {
@@ -703,18 +738,20 @@ void VideoView::SetNativeImageTarget()
 
 void VideoView::UpdateDisplayArea( Dali::PropertyNotification& source )
 {
-  if( !mIsUnderlay )
+  // If mSyncMode is enabled, Video player's size and poistion is updated in Video player's constraint.
+  // Because video view and player should be work syncronization.
+  if( !mIsUnderlay || mSyncMode == Dali::VideoSyncMode::ENABLED )
   {
     return;
   }
 
   Actor self( Self() );
 
-  bool positionUsesAnchorPoint = self.GetProperty( DevelActor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >();
-  Vector3 actorSize = self.GetCurrentSize() * self.GetCurrentScale();
-  Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentAnchorPoint() : AnchorPoint::TOP_LEFT );
+  bool positionUsesAnchorPoint = self.GetProperty( Actor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >();
+  Vector3 actorSize = self.GetCurrentProperty< Vector3 >( Actor::Property::SIZE ) * self.GetCurrentProperty< Vector3 >( Actor::Property::SCALE );
+  Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentProperty< Vector3 >( Actor::Property::ANCHOR_POINT ) : AnchorPoint::TOP_LEFT );
 
-  Vector2 screenPosition = self.GetProperty( DevelActor::Property::SCREEN_POSITION ).Get< Vector2 >();
+  Vector2 screenPosition = self.GetProperty( Actor::Property::SCREEN_POSITION ).Get< Vector2 >();
 
   mDisplayArea.x = screenPosition.x - anchorPointOffSet.x;
   mDisplayArea.y = screenPosition.y - anchorPointOffSet.y;
@@ -782,6 +819,27 @@ int VideoView::GetDisplayMode() const
   return static_cast< int >( mVideoPlayer.GetDisplayMode() );
 }
 
+Any VideoView::GetMediaPlayer()
+{
+  return mVideoPlayer.GetMediaPlayer();
+}
+
+void VideoView::OnAnimationFinished( Animation& animation )
+{
+  // send desync
+  SetFrameRenderCallback();
+}
+
+void VideoView::PlayAnimation( Dali::Animation animation )
+{
+  if( mIsUnderlay && mSyncMode == Dali::VideoSyncMode::ENABLED )
+  {
+    mVideoPlayer.StartSynchronization();
+    animation.FinishedSignal().Connect( this, &VideoView::OnAnimationFinished );
+  }
+  animation.Play();
+}
+
 Dali::Shader VideoView::CreateShader()
 {
   std::string fragmentShader = "#extension GL_OES_EGL_image_external:require\n";
@@ -838,6 +896,36 @@ bool VideoView::GetStringFromProperty( const Dali::Property::Value& value, std::
   return extracted;
 }
 
+void VideoView::ApplyBackupProperties()
+{
+  Property::Map::SizeType pos = 0;
+  Property::Map::SizeType count = mPropertyBackup.Count();
+
+  for( ; pos < count; pos++ )
+  {
+    KeyValuePair property = mPropertyBackup.GetKeyValue( pos );
+
+    SetPropertyInternal( property.first.indexKey, property.second );
+  }
+}
+
+void VideoView::FrameRenderCallback( int frameID )
+{
+  // send desync
+  if( frameID == mFrameID )
+  {
+    mVideoPlayer.FinishSynchronization();
+    mFrameID = 0;
+  }
+}
+
+void VideoView::SetFrameRenderCallback()
+{
+  mFrameID++;
+  DevelWindow::AddFrameRenderedCallback( DevelWindow::Get( Self() ),
+                                         std::unique_ptr< CallbackBase >( MakeCallback( this, &VideoView::FrameRenderCallback ) ), mFrameID );
+}
+
 } // namespace Internal
 
 } // namespace toolkit