< Summary

Class:DCL.ECSComponents.VideoPlayerHandler
Assembly:DCL.ECSComponents.VideoPlayer
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/VideoPlayer/VideoPlayerHandler.cs
Covered lines:20
Uncovered lines:6
Coverable lines:26
Total lines:79
Line coverage:76.9% (20 of 26)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
VideoPlayerHandler()0%110100%
VideoPlayerHandler(...)0%110100%
OnComponentCreated(...)0%2100%
OnComponentRemoved(...)0%6200%
OnComponentModelUpdated(...)0%12.1712089.47%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/VideoPlayer/VideoPlayerHandler.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using Cysharp.Threading.Tasks;
 3using DCL.Components;
 4using DCL.Components.Video.Plugin;
 5using DCL.Controllers;
 6using DCL.ECS7.InternalComponents;
 7using DCL.ECSRuntime;
 8using DCL.Models;
 9using Decentraland.Common;
 10using System;
 11using System.Linq;
 12using UnityEngine;
 13
 14namespace DCL.ECSComponents
 15{
 16    public class VideoPlayerHandler : IECSComponentHandler<PBVideoPlayer>
 17    {
 118        private static readonly string[] NO_STREAM_EXTENSIONS = new[] { ".mp4", ".ogg", ".mov", ".webm" };
 19
 20        private PBVideoPlayer lastModel = null;
 21        internal WebVideoPlayer videoPlayer;
 22
 23        private readonly IInternalECSComponent<InternalVideoPlayer> videoPlayerInternalComponent;
 24
 325        public VideoPlayerHandler(IInternalECSComponent<InternalVideoPlayer> videoPlayerInternalComponent)
 26        {
 327            this.videoPlayerInternalComponent = videoPlayerInternalComponent;
 328        }
 29
 030        public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) { }
 31
 32        public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity)
 33        {
 034            videoPlayerInternalComponent.RemoveFor(scene, entity);
 035            videoPlayer?.Dispose();
 036        }
 37
 38        public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBVideoPlayer model)
 39        {
 440            if (lastModel != null && lastModel.Equals(model))
 041                return;
 42
 43            // detect change of the state
 444            if (lastModel == null || lastModel.Src != model.Src)
 45            {
 446                videoPlayer?.Dispose();
 47
 448                var id = entity.entityId.ToString();
 849                bool isStream = !NO_STREAM_EXTENSIONS.Any(x => model.Src.EndsWith(x));
 450                videoPlayer = new WebVideoPlayer(id, model.Src, isStream, DCLVideoTexture.videoPluginWrapperBuilder.Invo
 451                videoPlayerInternalComponent.PutFor(scene, entity, new InternalVideoPlayer()
 52                {
 53                    videoPlayer = videoPlayer,
 54                    assignedMaterials = new List<InternalVideoPlayer.MaterialAssigned>(),
 55                });
 56            }
 57
 58            // detect change of the state
 459            if (lastModel == null || lastModel.IsPlaying() != model.IsPlaying())
 60            {
 661                if (model.IsPlaying()) { videoPlayer.Play(); }
 262                else { videoPlayer.Pause(); }
 63            }
 64
 65            // detect change of the state
 466            float lastPosition = lastModel?.GetPosition() ?? 0.0f;
 467            if (Math.Abs(lastPosition - model.GetPosition()) > 0.01f) // 0.01s of tolerance
 68            {
 069                videoPlayer.SetTime(model.GetPosition());
 70            }
 71
 472            videoPlayer.SetVolume(model.GetVolume());
 73
 474            videoPlayer.SetPlaybackRate(model.GetPlaybackRate());
 75
 476            lastModel = model;
 477        }
 78    }
 79}