2 * Copyright (c) 2024 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"
22 #include <dali/devel-api/actors/actor-devel.h>
23 #include <dali/devel-api/adaptor-framework/window-devel.h>
24 #include <dali/devel-api/rendering/texture-devel.h>
25 #include <dali/devel-api/scripting/scripting.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/public-api/adaptor-framework/native-image-source.h>
28 #include <dali/public-api/animation/constraint.h>
29 #include <dali/public-api/object/type-registry-helper.h>
30 #include <dali/public-api/object/type-registry.h>
34 #include <dali-toolkit/devel-api/controls/control-devel.h>
35 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
36 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
37 #include <dali-toolkit/public-api/controls/video-view/video-view.h>
38 #include <dali/integration-api/adaptor-framework/adaptor.h>
50 return Toolkit::VideoView::New();
53 DALI_TYPE_REGISTRATION_BEGIN(Toolkit::VideoView, Toolkit::Control, Create);
55 DALI_PROPERTY_REGISTRATION(Toolkit, VideoView, "video", MAP, VIDEO)
56 DALI_PROPERTY_REGISTRATION(Toolkit, VideoView, "looping", BOOLEAN, LOOPING)
57 DALI_PROPERTY_REGISTRATION(Toolkit, VideoView, "muted", BOOLEAN, MUTED)
58 DALI_PROPERTY_REGISTRATION(Toolkit, VideoView, "volume", MAP, VOLUME)
59 DALI_PROPERTY_REGISTRATION(Toolkit, VideoView, "underlay", BOOLEAN, UNDERLAY)
60 DALI_PROPERTY_REGISTRATION(Toolkit, VideoView, "playPosition", INTEGER, PLAY_POSITION)
61 DALI_PROPERTY_REGISTRATION(Toolkit, VideoView, "displayMode", INTEGER, DISPLAY_MODE)
63 DALI_SIGNAL_REGISTRATION(Toolkit, VideoView, "finished", FINISHED_SIGNAL)
65 DALI_ACTION_REGISTRATION(Toolkit, VideoView, "play", ACTION_VIDEOVIEW_PLAY)
66 DALI_ACTION_REGISTRATION(Toolkit, VideoView, "pause", ACTION_VIDEOVIEW_PAUSE)
67 DALI_ACTION_REGISTRATION(Toolkit, VideoView, "stop", ACTION_VIDEOVIEW_STOP)
68 DALI_ACTION_REGISTRATION(Toolkit, VideoView, "forward", ACTION_VIDEOVIEW_FORWARD)
69 DALI_ACTION_REGISTRATION(Toolkit, VideoView, "backward", ACTION_VIDEOVIEW_BACKWARD)
71 DALI_TYPE_REGISTRATION_END()
73 const char* const VOLUME_LEFT("volumeLeft");
74 const char* const VOLUME_RIGHT("volumeRight");
76 // 3.0 TC uses RENDERING_TARGET. It should be removed in next release
77 const char* const RENDERING_TARGET("renderingTarget");
78 const char* const WINDOW_SURFACE_TARGET("windowSurfaceTarget");
79 const char* const NATIVE_IMAGE_TARGET("nativeImageTarget");
81 const char* const CUSTOM_SHADER("shader");
82 const char* const CUSTOM_VERTEX_SHADER("vertexShader");
83 const char* const CUSTOM_FRAGMENT_SHADER("fragmentShader");
84 const char* const DEFAULT_SAMPLER_TYPE_NAME("sampler2D");
85 const char* const CUSTOM_SAMPLER_TYPE_NAME("samplerExternalOES");
87 const char* const IS_VIDEO_VIEW_PROPERTY_NAME = "isVideoView";
91 VideoView::VideoView(Dali::VideoSyncMode syncMode)
92 : Control(ControlBehaviour(ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS)),
93 mCurrentVideoPlayPosition(0),
102 VideoView::~VideoView()
106 Toolkit::VideoView VideoView::New(VideoSyncMode syncMode)
108 VideoView* impl = new VideoView(syncMode);
109 Toolkit::VideoView handle = Toolkit::VideoView(*impl);
111 impl->mVideoPlayer = Dali::VideoPlayer::New(impl->Self(), syncMode);
116 void VideoView::OnInitialize()
119 mVideoPlayer.FinishedSignal().Connect(this, &VideoView::EmitSignalFinish);
122 self.SetProperty(DevelControl::Property::ACCESSIBILITY_ROLE, Dali::Accessibility::Role::VIDEO);
123 self.SetProperty(DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE, true);
125 //update self property
126 self.RegisterProperty(IS_VIDEO_VIEW_PROPERTY_NAME, true, Property::READ_WRITE);
129 void VideoView::SetUrl(const std::string& url)
132 mPropertyMap.Clear();
134 mVideoPlayer.SetUrl(mUrl);
137 void VideoView::SetPropertyMap(Property::Map map)
141 Property::Value* target = map.Find(RENDERING_TARGET);
142 std::string targetType;
144 if(target && target->Get(targetType) && targetType == WINDOW_SURFACE_TARGET)
147 SetWindowSurfaceTarget();
149 else if(target && target->Get(targetType) && targetType == NATIVE_IMAGE_TARGET)
152 SetNativeImageTarget();
156 Property::Value* shaderValue;
159 shaderValue = map.Find(CUSTOM_SHADER);
163 Property::Map* shaderMap = shaderValue->GetMap();
166 mEffectPropertyMap = *shaderMap;
171 if(mTextureRenderer && !mEffectPropertyMap.Empty())
173 Dali::Shader shader = CreateShader();
174 mTextureRenderer.SetShader(shader);
180 std::string VideoView::GetUrl()
185 void VideoView::SetLooping(bool looping)
187 mVideoPlayer.SetLooping(looping);
190 bool VideoView::IsLooping()
192 return mVideoPlayer.IsLooping();
195 void VideoView::Play()
201 void VideoView::Pause()
203 mVideoPlayer.Pause();
207 void VideoView::Stop()
213 void VideoView::Forward(int millisecond)
215 int curPos = mVideoPlayer.GetPlayPosition();
217 int nextPos = curPos + millisecond;
219 mVideoPlayer.SetPlayPosition(nextPos);
222 void VideoView::Backward(int millisecond)
224 int curPos = mVideoPlayer.GetPlayPosition();
226 int nextPos = curPos - millisecond;
227 nextPos = (nextPos < 0) ? 0 : nextPos;
229 mVideoPlayer.SetPlayPosition(nextPos);
232 void VideoView::SetMute(bool mute)
234 mVideoPlayer.SetMute(mute);
237 bool VideoView::IsMuted()
239 return mVideoPlayer.IsMuted();
242 void VideoView::SetVolume(float left, float right)
244 mVideoPlayer.SetVolume(left, right);
247 void VideoView::GetVolume(float& left, float& right)
249 mVideoPlayer.GetVolume(left, right);
252 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
254 return mFinishedSignal;
257 void VideoView::EmitSignalFinish()
259 if(!mFinishedSignal.Empty())
261 Dali::Toolkit::VideoView handle(GetOwner());
262 mFinishedSignal.Emit(handle);
266 bool VideoView::DoAction(BaseObject* object, const std::string& actionName, const Property::Map& attributes)
270 Dali::BaseHandle handle(object);
271 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast(handle);
278 VideoView& impl = GetImpl(videoView);
280 if(strcmp(actionName.c_str(), ACTION_VIDEOVIEW_PLAY) == 0)
285 else if(strcmp(actionName.c_str(), ACTION_VIDEOVIEW_PAUSE) == 0)
290 else if(strcmp(actionName.c_str(), ACTION_VIDEOVIEW_STOP) == 0)
295 else if(strcmp(actionName.c_str(), ACTION_VIDEOVIEW_FORWARD) == 0)
298 if(attributes["videoForward"].Get(millisecond))
300 impl.Forward(millisecond);
304 else if(strcmp(actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD) == 0)
307 if(attributes["videoBackward"].Get(millisecond))
309 impl.Backward(millisecond);
317 bool VideoView::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
319 Dali::BaseHandle handle(object);
321 bool connected(true);
322 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast(handle);
324 if(0 == strcmp(signalName.c_str(), FINISHED_SIGNAL))
326 videoView.FinishedSignal().Connect(tracker, functor);
330 // signalName does not match any signal
337 void VideoView::SetPropertyInternal(Property::Index index, const Property::Value& value)
341 case Toolkit::VideoView::Property::VIDEO:
343 std::string videoUrl;
346 if(value.Get(videoUrl))
350 else if(value.Get(map))
356 case Toolkit::VideoView::Property::LOOPING:
359 if(value.Get(looping))
365 case Toolkit::VideoView::Property::MUTED:
374 case Toolkit::VideoView::Property::VOLUME:
380 Property::Value* volumeLeft = map.Find(VOLUME_LEFT);
381 Property::Value* volumeRight = map.Find(VOLUME_RIGHT);
382 if(volumeLeft && volumeLeft->Get(left) && volumeRight && volumeRight->Get(right))
384 SetVolume(left, right);
389 case Toolkit::VideoView::Property::UNDERLAY:
392 if(value.Get(underlay))
394 SetUnderlay(underlay);
398 case Toolkit::VideoView::Property::PLAY_POSITION:
403 SetPlayPosition(pos);
407 case Toolkit::VideoView::Property::DISPLAY_MODE:
412 SetDisplayMode(mode);
419 void VideoView::SetProperty(BaseObject* object, Property::Index index, const Property::Value& value)
421 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast(Dali::BaseHandle(object));
425 VideoView& impl = GetImpl(videoView);
427 impl.SetPropertyInternal(index, value);
429 if(index != Toolkit::VideoView::Property::UNDERLAY)
432 // These values will be used when underlay mode is changed.
433 impl.mPropertyBackup[index] = value;
438 Property::Value VideoView::GetProperty(BaseObject* object, Property::Index propertyIndex)
440 Property::Value value;
441 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast(Dali::BaseHandle(object));
445 VideoView& impl = GetImpl(videoView);
447 switch(propertyIndex)
449 case Toolkit::VideoView::Property::VIDEO:
451 if(!impl.mUrl.empty())
455 else if(!impl.mPropertyMap.Empty())
457 value = impl.mPropertyMap;
461 case Toolkit::VideoView::Property::LOOPING:
463 value = impl.IsLooping();
466 case Toolkit::VideoView::Property::MUTED:
468 value = impl.IsMuted();
471 case Toolkit::VideoView::Property::VOLUME:
476 impl.GetVolume(left, right);
477 map.Insert(VOLUME_LEFT, left);
478 map.Insert(VOLUME_RIGHT, right);
482 case Toolkit::VideoView::Property::UNDERLAY:
484 value = impl.IsUnderlay();
487 case Toolkit::VideoView::Property::PLAY_POSITION:
489 value = impl.GetPlayPosition();
492 case Toolkit::VideoView::Property::DISPLAY_MODE:
494 value = impl.GetDisplayMode();
503 void VideoView::SetDepthIndex(int depthIndex)
507 mTextureRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, depthIndex);
511 void VideoView::OnSceneConnection(int depth)
516 mSiblingOrder = self.GetProperty<int>(Dali::DevelActor::Property::SIBLING_ORDER);
517 DevelActor::ChildOrderChangedSignal(self.GetParent()).Connect(this, &VideoView::OnChildOrderChanged);
518 SetWindowSurfaceTarget();
521 Control::OnSceneConnection(depth);
524 void VideoView::OnSceneDisconnection()
526 Control::OnSceneDisconnection();
529 void VideoView::OnSizeSet(const Vector3& targetSize)
531 if(mIsUnderlay && mSyncMode == Dali::VideoSyncMode::ENABLED)
533 SetFrameRenderCallback();
534 mVideoPlayer.StartSynchronization();
536 Control::OnSizeSet(targetSize);
539 void VideoView::OnChildOrderChanged(Actor actor)
542 int currentSiblingOrder = self.GetProperty<int>(Dali::DevelActor::Property::SIBLING_ORDER);
543 if(currentSiblingOrder != mSiblingOrder)
545 Actor parent = self.GetParent();
550 int numChildren = static_cast<int>(parent.GetChildCount());
551 for(int i = 0; i < numChildren; i++)
553 child = parent.GetChildAt(i);
554 if(!IsVideoView(child))
564 if(i < currentSiblingOrder)
568 else if(i > currentSiblingOrder)
577 Toolkit::VideoView lowerView = Toolkit::VideoView::DownCast(lower);
578 mVideoPlayer.RaiseAbove(GetImpl(lowerView).GetVideoPlayer());
583 Toolkit::VideoView upperView = Toolkit::VideoView::DownCast(upper);
584 mVideoPlayer.LowerBelow(GetImpl(upperView).GetVideoPlayer());
586 mSiblingOrder = currentSiblingOrder;
590 Vector3 VideoView::GetNaturalSize()
593 size.x = mVideoSize.GetWidth();
594 size.y = mVideoSize.GetHeight();
596 if(size.x > 0 && size.y > 0)
598 size.z = std::min(size.x, size.y);
603 return Control::GetNaturalSize();
607 void VideoView::SetWindowSurfaceTarget()
611 if(!self.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
613 // When the control is off the stage, it does not have Window.
617 Dali::Window window = DevelWindow::Get(self);
618 window.ResizeSignal().Connect(this, &VideoView::OnWindowResized);
620 int curPos = mVideoPlayer.GetPlayPosition();
624 mVideoPlayer.Pause();
627 mPositionUpdateNotification = self.AddPropertyNotification(Actor::Property::WORLD_POSITION, StepCondition(1.0f, 1.0f));
628 mSizeUpdateNotification = self.AddPropertyNotification(Actor::Property::SIZE, StepCondition(1.0f, 1.0f));
629 mScaleUpdateNotification = self.AddPropertyNotification(Actor::Property::WORLD_SCALE, StepCondition(0.1f, 1.0f));
630 mPositionUpdateNotification.NotifySignal().Connect(this, &VideoView::UpdateDisplayArea);
631 mSizeUpdateNotification.NotifySignal().Connect(this, &VideoView::UpdateDisplayArea);
632 mScaleUpdateNotification.NotifySignal().Connect(this, &VideoView::UpdateDisplayArea);
636 self.RemoveRenderer(mTextureRenderer);
639 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
640 mVideoPlayer.SetRenderingTarget(Dali::Adaptor::Get().GetNativeWindowHandle(self));
642 ApplyBackupProperties();
644 if(!mOverlayRenderer)
646 // For underlay rendering mode, video display area have to be transparent.
647 Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
648 Shader shader = Shader::New(SHADER_VIDEO_VIEW_VERT, SHADER_VIDEO_VIEW_FRAG, Shader::Hint::NONE, "VIDEO_VIEW_OVERLAY");
649 mOverlayRenderer = Renderer::New(geometry, shader);
650 mOverlayRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::OFF);
652 Self().AddRenderer(mOverlayRenderer);
661 mVideoPlayer.SetPlayPosition(curPos);
665 void VideoView::SetNativeImageTarget()
667 if(mVideoPlayer.IsVideoTextureSupported() == false)
669 DALI_LOG_ERROR("Platform doesn't support decoded video frame images\n");
676 mVideoPlayer.Pause();
683 self.RemoveRenderer(mOverlayRenderer);
685 mOverlayRenderer.Reset();
688 self.RemovePropertyNotification(mPositionUpdateNotification);
689 self.RemovePropertyNotification(mSizeUpdateNotification);
690 self.RemovePropertyNotification(mScaleUpdateNotification);
692 int curPos = mVideoPlayer.GetPlayPosition();
695 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New(source);
696 mNativeTexture = Dali::Texture::New(*nativeImageSourcePtr);
698 if(!mTextureRenderer)
700 Dali::Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
701 Dali::Shader shader = CreateShader();
702 Dali::TextureSet textureSet = Dali::TextureSet::New();
703 textureSet.SetTexture(0u, mNativeTexture);
705 mTextureRenderer = Renderer::New(geometry, shader);
706 mTextureRenderer.SetTextures(textureSet);
710 Dali::TextureSet textureSet = mTextureRenderer.GetTextures();
711 textureSet.SetTexture(0u, mNativeTexture);
713 Self().AddRenderer(mTextureRenderer);
715 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
716 mVideoPlayer.SetRenderingTarget(nativeImageSourcePtr);
718 ApplyBackupProperties();
727 mVideoPlayer.SetPlayPosition(curPos);
731 void VideoView::UpdateDisplayArea(Dali::PropertyNotification& source)
733 // If mSyncMode is enabled, Video player's size and poistion is updated in Video player's constraint.
734 // Because video view and player should be work syncronization.
735 if(!mIsUnderlay || mSyncMode == Dali::VideoSyncMode::ENABLED)
742 bool positionUsesAnchorPoint = self.GetProperty(Actor::Property::POSITION_USES_ANCHOR_POINT).Get<bool>();
743 Vector3 actorSize = self.GetCurrentProperty<Vector3>(Actor::Property::SIZE) * self.GetCurrentProperty<Vector3>(Actor::Property::SCALE);
744 Vector3 anchorPointOffSet = actorSize * (positionUsesAnchorPoint ? self.GetCurrentProperty<Vector3>(Actor::Property::ANCHOR_POINT) : AnchorPoint::TOP_LEFT);
746 Vector2 screenPosition = self.GetProperty(Actor::Property::SCREEN_POSITION).Get<Vector2>();
748 mDisplayArea.x = screenPosition.x - anchorPointOffSet.x;
749 mDisplayArea.y = screenPosition.y - anchorPointOffSet.y;
750 mDisplayArea.width = actorSize.x;
751 mDisplayArea.height = actorSize.y;
753 mVideoPlayer.SetDisplayArea(mDisplayArea);
756 void VideoView::SetUnderlay(bool set)
758 if(set != mIsUnderlay)
764 SetWindowSurfaceTarget();
768 SetNativeImageTarget();
775 bool VideoView::IsUnderlay()
780 void VideoView::SetSWCodec(bool on)
782 // If setting SW or HW type is failed , video-view shows video by default codec type.
783 // The default codec type is selected by platform.
786 mVideoPlayer.SetCodecType(Dali::VideoPlayerPlugin::CodecType::SW);
790 mVideoPlayer.SetCodecType(Dali::VideoPlayerPlugin::CodecType::HW);
794 int VideoView::GetPlayPosition()
796 return mVideoPlayer.GetPlayPosition();
799 void VideoView::SetPlayPosition(int pos)
801 mVideoPlayer.SetPlayPosition(pos);
804 void VideoView::SetDisplayMode(int mode)
806 mVideoPlayer.SetDisplayMode(static_cast<Dali::VideoPlayerPlugin::DisplayMode::Type>(mode));
809 int VideoView::GetDisplayMode() const
811 return static_cast<int>(mVideoPlayer.GetDisplayMode());
814 Any VideoView::GetMediaPlayer()
816 return mVideoPlayer.GetMediaPlayer();
819 void VideoView::OnAnimationFinished(Animation& animation)
822 SetFrameRenderCallback();
825 void VideoView::OnWindowResized(Dali::Window winHandle, Dali::Window::WindowSize size)
827 Dali::VideoPlayerPlugin::DisplayRotation videoAngle = mVideoPlayer.GetDisplayRotation();
828 int windowAngle = (DevelWindow::GetPhysicalOrientation(winHandle) / 90);
830 if(windowAngle != videoAngle)
832 mVideoPlayer.SetDisplayRotation(static_cast<Dali::VideoPlayerPlugin::DisplayRotation>(windowAngle));
836 void VideoView::PlayAnimation(Dali::Animation animation)
838 if(mIsUnderlay && mSyncMode == Dali::VideoSyncMode::ENABLED)
840 mVideoPlayer.StartSynchronization();
841 animation.FinishedSignal().Connect(this, &VideoView::OnAnimationFinished);
846 Dali::Shader VideoView::CreateShader()
848 std::string fragmentShader;
849 std::string vertexShader;
850 std::string customFragmentShader;
851 bool checkShader = false;
853 if(!mEffectPropertyMap.Empty())
855 Property::Value* vertexShaderValue = mEffectPropertyMap.Find(CUSTOM_VERTEX_SHADER);
856 if(vertexShaderValue)
858 checkShader = GetStringFromProperty(*vertexShaderValue, vertexShader);
861 if(!vertexShaderValue || !checkShader)
863 vertexShader = SHADER_VIDEO_VIEW_TEXTURE_VERT.data();
866 Property::Value* fragmentShaderValue = mEffectPropertyMap.Find(CUSTOM_FRAGMENT_SHADER);
867 if(fragmentShaderValue)
869 checkShader = GetStringFromProperty(*fragmentShaderValue, customFragmentShader);
873 fragmentShader = customFragmentShader;
877 if(!fragmentShaderValue || !checkShader)
879 fragmentShader = SHADER_VIDEO_VIEW_TEXTURE_FRAG.data();
880 DevelTexture::ApplyNativeFragmentShader(mNativeTexture, fragmentShader);
885 vertexShader = SHADER_VIDEO_VIEW_TEXTURE_VERT.data();
886 fragmentShader = SHADER_VIDEO_VIEW_TEXTURE_FRAG.data();
887 DevelTexture::ApplyNativeFragmentShader(mNativeTexture, fragmentShader);
890 return Dali::Shader::New(vertexShader, fragmentShader, Shader::Hint::NONE, "VIDEO_VIEW");
893 bool VideoView::GetStringFromProperty(const Dali::Property::Value& value, std::string& output)
895 bool extracted = false;
896 if(value.Get(output))
904 void VideoView::ApplyBackupProperties()
906 Property::Map::SizeType pos = 0;
907 Property::Map::SizeType count = mPropertyBackup.Count();
909 for(; pos < count; pos++)
911 KeyValuePair property = mPropertyBackup.GetKeyValue(pos);
913 SetPropertyInternal(property.first.indexKey, property.second);
917 void VideoView::FrameRenderCallback(int frameID)
920 if(frameID == mFrameID)
922 mVideoPlayer.FinishSynchronization();
927 void VideoView::SetFrameRenderCallback()
930 DevelWindow::AddFrameRenderedCallback(DevelWindow::Get(Self()),
931 std::unique_ptr<CallbackBase>(MakeCallback(this, &VideoView::FrameRenderCallback)),
935 bool VideoView::IsVideoView(Actor actor) const
937 // Check whether the actor is a VideoView
938 bool isVideoView = false;
942 Property::Index propertyIsVideoView = actor.GetPropertyIndex(IS_VIDEO_VIEW_PROPERTY_NAME);
943 if(propertyIsVideoView != Property::INVALID_INDEX)
945 isVideoView = actor.GetProperty<bool>(propertyIsVideoView);
952 VideoPlayer VideoView::GetVideoPlayer()
957 } // namespace Internal
959 } // namespace Toolkit