2 * Copyright (c) 2021 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);
121 DevelControl::SetAccessibilityConstructor(self, [](Dali::Actor actor) {
122 return std::make_unique<DevelControl::ControlAccessible>(actor, Dali::Accessibility::Role::VIDEO);
125 //Enable highightability
126 Self().SetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE, true);
128 //update self property
129 self.RegisterProperty(IS_VIDEO_VIEW_PROPERTY_NAME, true, Property::READ_WRITE);
132 void VideoView::SetUrl(const std::string& url)
135 mPropertyMap.Clear();
137 mVideoPlayer.SetUrl(mUrl);
140 void VideoView::SetPropertyMap(Property::Map map)
144 Property::Value* target = map.Find(RENDERING_TARGET);
145 std::string targetType;
147 if(target && target->Get(targetType) && targetType == WINDOW_SURFACE_TARGET)
150 SetWindowSurfaceTarget();
152 else if(target && target->Get(targetType) && targetType == NATIVE_IMAGE_TARGET)
155 SetNativeImageTarget();
159 Property::Value* shaderValue;
162 shaderValue = map.Find(CUSTOM_SHADER);
166 Property::Map* shaderMap = shaderValue->GetMap();
169 mEffectPropertyMap = *shaderMap;
174 if(mTextureRenderer && !mEffectPropertyMap.Empty())
176 Dali::Shader shader = CreateShader();
177 mTextureRenderer.SetShader(shader);
183 std::string VideoView::GetUrl()
188 void VideoView::SetLooping(bool looping)
190 mVideoPlayer.SetLooping(looping);
193 bool VideoView::IsLooping()
195 return mVideoPlayer.IsLooping();
198 void VideoView::Play()
204 void VideoView::Pause()
206 mVideoPlayer.Pause();
210 void VideoView::Stop()
216 void VideoView::Forward(int millisecond)
218 int curPos = mVideoPlayer.GetPlayPosition();
220 int nextPos = curPos + millisecond;
222 mVideoPlayer.SetPlayPosition(nextPos);
225 void VideoView::Backward(int millisecond)
227 int curPos = mVideoPlayer.GetPlayPosition();
229 int nextPos = curPos - millisecond;
230 nextPos = (nextPos < 0) ? 0 : nextPos;
232 mVideoPlayer.SetPlayPosition(nextPos);
235 void VideoView::SetMute(bool mute)
237 mVideoPlayer.SetMute(mute);
240 bool VideoView::IsMuted()
242 return mVideoPlayer.IsMuted();
245 void VideoView::SetVolume(float left, float right)
247 mVideoPlayer.SetVolume(left, right);
250 void VideoView::GetVolume(float& left, float& right)
252 mVideoPlayer.GetVolume(left, right);
255 Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
257 return mFinishedSignal;
260 void VideoView::EmitSignalFinish()
262 if(!mFinishedSignal.Empty())
264 Dali::Toolkit::VideoView handle(GetOwner());
265 mFinishedSignal.Emit(handle);
269 bool VideoView::DoAction(BaseObject* object, const std::string& actionName, const Property::Map& attributes)
273 Dali::BaseHandle handle(object);
274 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast(handle);
281 VideoView& impl = GetImpl(videoView);
283 if(strcmp(actionName.c_str(), ACTION_VIDEOVIEW_PLAY) == 0)
288 else if(strcmp(actionName.c_str(), ACTION_VIDEOVIEW_PAUSE) == 0)
293 else if(strcmp(actionName.c_str(), ACTION_VIDEOVIEW_STOP) == 0)
298 else if(strcmp(actionName.c_str(), ACTION_VIDEOVIEW_FORWARD) == 0)
301 if(attributes["videoForward"].Get(millisecond))
303 impl.Forward(millisecond);
307 else if(strcmp(actionName.c_str(), ACTION_VIDEOVIEW_BACKWARD) == 0)
310 if(attributes["videoBackward"].Get(millisecond))
312 impl.Backward(millisecond);
320 bool VideoView::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
322 Dali::BaseHandle handle(object);
324 bool connected(true);
325 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast(handle);
327 if(0 == strcmp(signalName.c_str(), FINISHED_SIGNAL))
329 videoView.FinishedSignal().Connect(tracker, functor);
333 // signalName does not match any signal
340 void VideoView::SetPropertyInternal(Property::Index index, const Property::Value& value)
344 case Toolkit::VideoView::Property::VIDEO:
346 std::string videoUrl;
349 if(value.Get(videoUrl))
353 else if(value.Get(map))
359 case Toolkit::VideoView::Property::LOOPING:
362 if(value.Get(looping))
368 case Toolkit::VideoView::Property::MUTED:
377 case Toolkit::VideoView::Property::VOLUME:
383 Property::Value* volumeLeft = map.Find(VOLUME_LEFT);
384 Property::Value* volumeRight = map.Find(VOLUME_RIGHT);
385 if(volumeLeft && volumeLeft->Get(left) && volumeRight && volumeRight->Get(right))
387 SetVolume(left, right);
392 case Toolkit::VideoView::Property::UNDERLAY:
395 if(value.Get(underlay))
397 SetUnderlay(underlay);
401 case Toolkit::VideoView::Property::PLAY_POSITION:
406 SetPlayPosition(pos);
410 case Toolkit::VideoView::Property::DISPLAY_MODE:
415 SetDisplayMode(mode);
422 void VideoView::SetProperty(BaseObject* object, Property::Index index, const Property::Value& value)
424 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast(Dali::BaseHandle(object));
428 VideoView& impl = GetImpl(videoView);
430 impl.SetPropertyInternal(index, value);
432 if(index != Toolkit::VideoView::Property::UNDERLAY)
435 // These values will be used when underlay mode is changed.
436 impl.mPropertyBackup[index] = value;
441 Property::Value VideoView::GetProperty(BaseObject* object, Property::Index propertyIndex)
443 Property::Value value;
444 Toolkit::VideoView videoView = Toolkit::VideoView::DownCast(Dali::BaseHandle(object));
448 VideoView& impl = GetImpl(videoView);
450 switch(propertyIndex)
452 case Toolkit::VideoView::Property::VIDEO:
454 if(!impl.mUrl.empty())
458 else if(!impl.mPropertyMap.Empty())
460 value = impl.mPropertyMap;
464 case Toolkit::VideoView::Property::LOOPING:
466 value = impl.IsLooping();
469 case Toolkit::VideoView::Property::MUTED:
471 value = impl.IsMuted();
474 case Toolkit::VideoView::Property::VOLUME:
479 impl.GetVolume(left, right);
480 map.Insert(VOLUME_LEFT, left);
481 map.Insert(VOLUME_RIGHT, right);
485 case Toolkit::VideoView::Property::UNDERLAY:
487 value = impl.IsUnderlay();
490 case Toolkit::VideoView::Property::PLAY_POSITION:
492 value = impl.GetPlayPosition();
495 case Toolkit::VideoView::Property::DISPLAY_MODE:
497 value = impl.GetDisplayMode();
506 void VideoView::SetDepthIndex(int depthIndex)
510 mTextureRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, depthIndex);
514 void VideoView::OnSceneConnection(int depth)
519 mSiblingOrder = self.GetProperty<int>(Dali::DevelActor::Property::SIBLING_ORDER);
520 DevelActor::ChildOrderChangedSignal(self.GetParent()).Connect(this, &VideoView::OnChildOrderChanged);
521 SetWindowSurfaceTarget();
524 Control::OnSceneConnection(depth);
527 void VideoView::OnSceneDisconnection()
529 Control::OnSceneDisconnection();
532 void VideoView::OnSizeSet(const Vector3& targetSize)
534 if(mIsUnderlay && mSyncMode == Dali::VideoSyncMode::ENABLED)
536 SetFrameRenderCallback();
537 mVideoPlayer.StartSynchronization();
539 Control::OnSizeSet(targetSize);
542 void VideoView::OnChildOrderChanged(Actor actor)
545 int currentSiblingOrder = self.GetProperty<int>(Dali::DevelActor::Property::SIBLING_ORDER);
546 if(currentSiblingOrder != mSiblingOrder)
548 Actor parent = self.GetParent();
553 int numChildren = static_cast<int>(parent.GetChildCount());
554 for(int i = 0; i < numChildren; i++)
556 child = parent.GetChildAt(i);
557 if(!IsVideoView(child))
567 if(i < currentSiblingOrder)
571 else if(i > currentSiblingOrder)
580 Toolkit::VideoView lowerView = Toolkit::VideoView::DownCast(lower);
581 mVideoPlayer.RaiseAbove(GetImpl(lowerView).GetVideoPlayer());
586 Toolkit::VideoView upperView = Toolkit::VideoView::DownCast(upper);
587 mVideoPlayer.LowerBelow(GetImpl(upperView).GetVideoPlayer());
589 mSiblingOrder = currentSiblingOrder;
593 Vector3 VideoView::GetNaturalSize()
596 size.x = mVideoSize.GetWidth();
597 size.y = mVideoSize.GetHeight();
599 if(size.x > 0 && size.y > 0)
601 size.z = std::min(size.x, size.y);
606 return Control::GetNaturalSize();
610 void VideoView::SetWindowSurfaceTarget()
614 if(!self.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
616 // When the control is off the stage, it does not have Window.
620 Dali::Window window = DevelWindow::Get(self);
621 window.ResizeSignal().Connect(this, &VideoView::OnWindowResized);
623 int curPos = mVideoPlayer.GetPlayPosition();
627 mVideoPlayer.Pause();
630 mPositionUpdateNotification = self.AddPropertyNotification(Actor::Property::WORLD_POSITION, StepCondition(1.0f, 1.0f));
631 mSizeUpdateNotification = self.AddPropertyNotification(Actor::Property::SIZE, StepCondition(1.0f, 1.0f));
632 mScaleUpdateNotification = self.AddPropertyNotification(Actor::Property::WORLD_SCALE, StepCondition(0.1f, 1.0f));
633 mPositionUpdateNotification.NotifySignal().Connect(this, &VideoView::UpdateDisplayArea);
634 mSizeUpdateNotification.NotifySignal().Connect(this, &VideoView::UpdateDisplayArea);
635 mScaleUpdateNotification.NotifySignal().Connect(this, &VideoView::UpdateDisplayArea);
639 self.RemoveRenderer(mTextureRenderer);
642 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
643 mVideoPlayer.SetRenderingTarget(Dali::Adaptor::Get().GetNativeWindowHandle(self));
645 ApplyBackupProperties();
647 if(!mOverlayRenderer)
649 // For underlay rendering mode, video display area have to be transparent.
650 Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
651 Shader shader = Shader::New(SHADER_VIDEO_VIEW_VERT, SHADER_VIDEO_VIEW_FRAG);
652 mOverlayRenderer = Renderer::New(geometry, shader);
653 mOverlayRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::OFF);
655 Self().AddRenderer(mOverlayRenderer);
664 mVideoPlayer.SetPlayPosition(curPos);
668 void VideoView::SetNativeImageTarget()
670 if(mVideoPlayer.IsVideoTextureSupported() == false)
672 DALI_LOG_ERROR("Platform doesn't support decoded video frame images\n");
679 mVideoPlayer.Pause();
686 self.RemoveRenderer(mOverlayRenderer);
688 mOverlayRenderer.Reset();
691 self.RemovePropertyNotification(mPositionUpdateNotification);
692 self.RemovePropertyNotification(mSizeUpdateNotification);
693 self.RemovePropertyNotification(mScaleUpdateNotification);
695 int curPos = mVideoPlayer.GetPlayPosition();
698 Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New(source);
699 mNativeTexture = Dali::Texture::New(*nativeImageSourcePtr);
701 if(!mTextureRenderer)
703 Dali::Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
704 Dali::Shader shader = CreateShader();
705 Dali::TextureSet textureSet = Dali::TextureSet::New();
706 textureSet.SetTexture(0u, mNativeTexture);
708 mTextureRenderer = Renderer::New(geometry, shader);
709 mTextureRenderer.SetTextures(textureSet);
713 Dali::TextureSet textureSet = mTextureRenderer.GetTextures();
714 textureSet.SetTexture(0u, mNativeTexture);
716 Self().AddRenderer(mTextureRenderer);
718 // Note VideoPlayer::SetRenderingTarget resets all the options. (e.g. url, mute, looping)
719 mVideoPlayer.SetRenderingTarget(nativeImageSourcePtr);
721 ApplyBackupProperties();
730 mVideoPlayer.SetPlayPosition(curPos);
734 void VideoView::UpdateDisplayArea(Dali::PropertyNotification& source)
736 // If mSyncMode is enabled, Video player's size and poistion is updated in Video player's constraint.
737 // Because video view and player should be work syncronization.
738 if(!mIsUnderlay || mSyncMode == Dali::VideoSyncMode::ENABLED)
745 bool positionUsesAnchorPoint = self.GetProperty(Actor::Property::POSITION_USES_ANCHOR_POINT).Get<bool>();
746 Vector3 actorSize = self.GetCurrentProperty<Vector3>(Actor::Property::SIZE) * self.GetCurrentProperty<Vector3>(Actor::Property::SCALE);
747 Vector3 anchorPointOffSet = actorSize * (positionUsesAnchorPoint ? self.GetCurrentProperty<Vector3>(Actor::Property::ANCHOR_POINT) : AnchorPoint::TOP_LEFT);
749 Vector2 screenPosition = self.GetProperty(Actor::Property::SCREEN_POSITION).Get<Vector2>();
751 mDisplayArea.x = screenPosition.x - anchorPointOffSet.x;
752 mDisplayArea.y = screenPosition.y - anchorPointOffSet.y;
753 mDisplayArea.width = actorSize.x;
754 mDisplayArea.height = actorSize.y;
756 mVideoPlayer.SetDisplayArea(mDisplayArea);
759 void VideoView::SetUnderlay(bool set)
761 if(set != mIsUnderlay)
767 SetWindowSurfaceTarget();
771 SetNativeImageTarget();
778 bool VideoView::IsUnderlay()
783 void VideoView::SetSWCodec(bool on)
785 // If setting SW or HW type is failed , video-view shows video by default codec type.
786 // The default codec type is selected by platform.
789 mVideoPlayer.SetCodecType(Dali::VideoPlayerPlugin::CodecType::SW);
793 mVideoPlayer.SetCodecType(Dali::VideoPlayerPlugin::CodecType::HW);
797 int VideoView::GetPlayPosition()
799 return mVideoPlayer.GetPlayPosition();
802 void VideoView::SetPlayPosition(int pos)
804 mVideoPlayer.SetPlayPosition(pos);
807 void VideoView::SetDisplayMode(int mode)
809 mVideoPlayer.SetDisplayMode(static_cast<Dali::VideoPlayerPlugin::DisplayMode::Type>(mode));
812 int VideoView::GetDisplayMode() const
814 return static_cast<int>(mVideoPlayer.GetDisplayMode());
817 Any VideoView::GetMediaPlayer()
819 return mVideoPlayer.GetMediaPlayer();
822 void VideoView::OnAnimationFinished(Animation& animation)
825 SetFrameRenderCallback();
828 void VideoView::OnWindowResized(Dali::Window winHandle, Dali::Window::WindowSize size)
830 Dali::VideoPlayerPlugin::DisplayRotation videoAngle = mVideoPlayer.GetDisplayRotation();
831 int windowAngle = (DevelWindow::GetPhysicalOrientation(winHandle) / 90);
833 if(windowAngle != videoAngle)
835 mVideoPlayer.SetDisplayRotation(static_cast<Dali::VideoPlayerPlugin::DisplayRotation>(windowAngle));
839 void VideoView::PlayAnimation(Dali::Animation animation)
841 if(mIsUnderlay && mSyncMode == Dali::VideoSyncMode::ENABLED)
843 mVideoPlayer.StartSynchronization();
844 animation.FinishedSignal().Connect(this, &VideoView::OnAnimationFinished);
849 Dali::Shader VideoView::CreateShader()
851 std::string fragmentShader;
852 std::string vertexShader;
853 std::string customFragmentShader;
854 bool checkShader = false;
856 if(!mEffectPropertyMap.Empty())
858 Property::Value* vertexShaderValue = mEffectPropertyMap.Find(CUSTOM_VERTEX_SHADER);
859 if(vertexShaderValue)
861 checkShader = GetStringFromProperty(*vertexShaderValue, vertexShader);
864 if(!vertexShaderValue || !checkShader)
866 vertexShader = SHADER_VIDEO_VIEW_TEXTURE_VERT.data();
869 Property::Value* fragmentShaderValue = mEffectPropertyMap.Find(CUSTOM_FRAGMENT_SHADER);
870 if(fragmentShaderValue)
872 checkShader = GetStringFromProperty(*fragmentShaderValue, customFragmentShader);
876 fragmentShader = customFragmentShader;
880 if(!fragmentShaderValue || !checkShader)
882 fragmentShader = SHADER_VIDEO_VIEW_TEXTURE_FRAG.data();
883 DevelTexture::ApplyNativeFragmentShader(mNativeTexture, fragmentShader);
888 vertexShader = SHADER_VIDEO_VIEW_TEXTURE_VERT.data();
889 fragmentShader = SHADER_VIDEO_VIEW_TEXTURE_FRAG.data();
890 DevelTexture::ApplyNativeFragmentShader(mNativeTexture, fragmentShader);
893 return Dali::Shader::New(vertexShader, fragmentShader);
896 bool VideoView::GetStringFromProperty(const Dali::Property::Value& value, std::string& output)
898 bool extracted = false;
899 if(value.Get(output))
907 void VideoView::ApplyBackupProperties()
909 Property::Map::SizeType pos = 0;
910 Property::Map::SizeType count = mPropertyBackup.Count();
912 for(; pos < count; pos++)
914 KeyValuePair property = mPropertyBackup.GetKeyValue(pos);
916 SetPropertyInternal(property.first.indexKey, property.second);
920 void VideoView::FrameRenderCallback(int frameID)
923 if(frameID == mFrameID)
925 mVideoPlayer.FinishSynchronization();
930 void VideoView::SetFrameRenderCallback()
933 DevelWindow::AddFrameRenderedCallback(DevelWindow::Get(Self()),
934 std::unique_ptr<CallbackBase>(MakeCallback(this, &VideoView::FrameRenderCallback)),
938 bool VideoView::IsVideoView(Actor actor) const
940 // Check whether the actor is a VideoView
941 bool isVideoView = false;
945 Property::Index propertyIsVideoView = actor.GetPropertyIndex(IS_VIDEO_VIEW_PROPERTY_NAME);
946 if(propertyIsVideoView != Property::INVALID_INDEX)
948 isVideoView = actor.GetProperty<bool>(propertyIsVideoView);
955 VideoPlayer VideoView::GetVideoPlayer()
960 } // namespace Internal
962 } // namespace Toolkit