< Summary

Class:DCL.Components.Video.Plugin.WebVideoPlayer
Assembly:DCL.Components.Video
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Video/Plugins/WebVideoPlayer.cs
Covered lines:99
Uncovered lines:2
Coverable lines:101
Total lines:218
Line coverage:98% (99 of 101)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WebVideoPlayer(...)0%110100%
Update()0%660100%
Play()0%330100%
PlayInternal()0%110100%
Pause()0%220100%
SetVolume(...)0%220100%
SetTime(...)0%220100%
SetLoop(...)0%220100%
SetPlaybackRate(...)0%220100%
GetTime()0%220100%
GetDuration()0%330100%
GetState()0%110100%
SetAsLoading()0%110100%
SetAsError()0%110100%
Dispose()0%110100%
UpdateTextureConservingAspectRatio(...)0%4.024089.47%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Video/Plugins/WebVideoPlayer.cs

#LineLine coverage
 1using System;
 2using UnityEngine;
 3
 4namespace DCL.Components.Video.Plugin
 5{
 6    public class WebVideoPlayer : IDisposable
 7    {
 8        private const string VIDEO_LOADING_PATH = "Textures/VideoLoading";
 9        private const string VIDEO_FAILED_PATH = "Textures/VideoFailed";
 10        private const float DEFAULT_ASPECT_RATIO = 4f / 3f;
 11
 43412        public Texture2D texture { private set; get; }
 5913        public float volume { private set; get; }
 514        public bool playing => GetState() == VideoState.PLAYING;
 21715        public bool isError => GetState() == VideoState.ERROR;
 6616        public bool visible { get; set; } = true;
 8417        public bool isReady { get; private set; } = false;
 18
 19        public readonly string url;
 20
 21        private string videoPlayerId;
 22
 23        private readonly IVideoPluginWrapper plugin;
 24
 25        private bool playWhenReady = false;
 3626        private float playStartTime = -1;
 27
 3628        private string lastError = "";
 29
 3630        public WebVideoPlayer(string id, string url, bool useHls, IVideoPluginWrapper plugin)
 31        {
 3632            videoPlayerId = id;
 3633            this.plugin = plugin;
 3634            this.url = url;
 3635            plugin.Create(id, url, useHls);
 36
 3637            SetAsLoading();
 3638        }
 39
 40        public void Update()
 41        {
 11142            switch (plugin.GetState(videoPlayerId))
 43            {
 44                case VideoState.ERROR:
 245                    string newError = plugin.GetError(videoPlayerId);
 246                    if (newError == lastError)
 47                        break;
 48
 249                    SetAsError();
 250                    lastError = newError;
 251                    Debug.LogError(lastError);
 52
 253                    break;
 54                case VideoState.READY:
 5155                    if (!isReady)
 56                    {
 2157                        isReady = true;
 2158                        texture = plugin.PrepareTexture(videoPlayerId);
 59                    }
 60
 5161                    if (playWhenReady)
 62                    {
 663                        PlayInternal();
 664                        playWhenReady = false;
 65                    }
 66
 667                    break;
 68                case VideoState.PLAYING:
 369                    if (visible)
 370                        plugin.TextureUpdate(videoPlayerId);
 71
 72                    break;
 73            }
 7274        }
 75
 76        public void Play()
 77        {
 1378            if (isError)
 179                return;
 80
 1281            if (!isReady)
 82            {
 883                playWhenReady = true;
 884                return;
 85            }
 86
 487            PlayInternal();
 488        }
 89
 90        private void PlayInternal()
 91        {
 1092            plugin.Play(videoPlayerId, playStartTime);
 1093            playStartTime = -1;
 1094        }
 95
 96        public void Pause()
 97        {
 1898            if (isError)
 199                return;
 100
 17101            playStartTime = plugin.GetTime(videoPlayerId);
 17102            plugin.Pause(videoPlayerId);
 17103            playWhenReady = false;
 17104        }
 105
 106        public void SetVolume(float volume)
 107        {
 50108            if (isError)
 1109                return;
 110
 49111            plugin.SetVolume(videoPlayerId, volume);
 49112            this.volume = volume;
 49113        }
 114
 115        public void SetTime(float timeSecs)
 116        {
 3117            if (isError)
 1118                return;
 119
 2120            playStartTime = timeSecs;
 2121            plugin.SetTime(videoPlayerId, timeSecs);
 2122        }
 123
 124        public void SetLoop(bool loop)
 125        {
 18126            if (isError)
 1127                return;
 128
 17129            plugin.SetLoop(videoPlayerId, loop);
 17130        }
 131
 132        public void SetPlaybackRate(float playbackRate)
 133        {
 21134            if (isError)
 1135                return;
 136
 20137            plugin.SetPlaybackRate(videoPlayerId, playbackRate);
 20138        }
 139
 140        public float GetTime()
 141        {
 46142            if (isError)
 1143                return 0;
 144
 45145            return plugin.GetTime(videoPlayerId);
 146        }
 147
 148        public float GetDuration()
 149        {
 47150            if (isError)
 1151                return 0;
 152
 46153            float duration = plugin.GetDuration(videoPlayerId);
 154
 46155            if (float.IsNaN(duration))
 1156                duration = -1;
 157
 46158            return duration;
 159        }
 160
 161        public VideoState GetState()
 162        {
 401163            return plugin.GetState(videoPlayerId);
 164        }
 165
 166        public void SetAsLoading()
 167        {
 36168            UpdateTextureConservingAspectRatio(Resources.Load<Texture2D>(VIDEO_LOADING_PATH), true);
 36169        }
 170
 171        public void SetAsError()
 172        {
 2173            UpdateTextureConservingAspectRatio(Resources.Load<Texture2D>(VIDEO_FAILED_PATH), true);
 2174        }
 175
 176        public void Dispose()
 177        {
 4178            plugin.Remove(videoPlayerId);
 4179        }
 180
 181
 182        private void UpdateTextureConservingAspectRatio(Texture2D textureToCenter, bool fillBackground)
 183        {
 184            // Avoiding Memory leaks as textures are never destroyed by Unity
 38185            if (texture != null)
 186            {
 2187                UnityEngine.Object.Destroy(texture);
 2188                texture = null;
 189            }
 190
 191            // Fix textureToCenter in the center of a 16:9 texture
 38192            int xOffset = 0;
 38193            int yOffset = 0;
 38194            if ((float)textureToCenter.width / textureToCenter.height >= DEFAULT_ASPECT_RATIO)
 195            {
 0196                texture = new Texture2D(textureToCenter.width, Mathf.RoundToInt(textureToCenter.width / DEFAULT_ASPECT_R
 0197                yOffset = (texture.height - textureToCenter.height) / 2;
 198            }
 199            else
 200            {
 38201                texture = new Texture2D(Mathf.RoundToInt(textureToCenter.height * DEFAULT_ASPECT_RATIO), textureToCenter
 38202                xOffset = (texture.width - textureToCenter.width) / 2;
 203            }
 204
 38205            if (fillBackground)
 206            {
 38207                Color32[] backgroundPixels = new Color32[texture.width * texture.height];
 38208                Color32 color = textureToCenter.GetPixel(0, 0);
 38209                Array.Fill(backgroundPixels, color);
 38210                texture.SetPixels32(backgroundPixels);
 211            }
 212
 38213            Color32[] pixels = textureToCenter.GetPixels32(0);
 38214            texture.SetPixels32(xOffset, yOffset, textureToCenter.width, textureToCenter.height, pixels);
 38215            texture.Apply();
 38216        }
 217    }
 218}