< Summary

Class:DCL.Components.Video.Plugin.WebVideoPlayer
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Video/Plugins/WebVideoPlayer.cs
Covered lines:75
Uncovered lines:7
Coverable lines:82
Total lines:174
Line coverage:91.4% (75 of 82)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WebVideoPlayer(...)0%110100%
UpdateWebVideoTexture()0%10.0110095.45%
Play()0%220100%
Pause()0%220100%
IsPaused()0%2100%
SetVolume(...)0%220100%
SetTime(...)0%220100%
SetLoop(...)0%220100%
SetPlaybackRate(...)0%220100%
GetTime()0%220100%
GetDuration()0%330100%
GetState()0%110100%
Dispose()0%110100%
CreateTexture(...)0%110100%

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        public event Action<Texture> OnTextureReady;
 09        public Texture2D texture { private set; get; }
 010        public float volume { private set; get; }
 011        public bool playing { get { return shouldBePlaying; } }
 012        public bool visible { get; set; }
 013        public bool isError { get; private set; }
 14
 115        private static bool isWebGL1 => SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.OpenGL
 16
 17        private string videoPlayerId;
 18        private readonly IWebVideoPlayerPlugin plugin;
 19        private IntPtr textureNativePtr;
 20        private bool initialized = false;
 21        private bool shouldBePlaying = false;
 3122        private float pausedAtTime = -1;
 23
 24
 3125        public WebVideoPlayer(string id, string url, bool useHls, IWebVideoPlayerPlugin plugin)
 26        {
 3127            videoPlayerId = id;
 3128            this.plugin = plugin;
 29
 3130            plugin.Create(id, url, useHls);
 3131        }
 32
 33        public void UpdateWebVideoTexture()
 34        {
 635            if (isError)
 36            {
 037                return;
 38            }
 39
 640            switch (plugin.GetState(videoPlayerId))
 41            {
 42                case (int)VideoState.ERROR:
 243                    Debug.LogError(plugin.GetError(videoPlayerId));
 244                    isError = true;
 245                    break;
 46                case (int)VideoState.READY:
 347                    if (!initialized)
 48                    {
 249                        initialized = true;
 250                        texture = CreateTexture(plugin.GetWidth(videoPlayerId), plugin.GetHeight(videoPlayerId));
 251                        textureNativePtr = texture.GetNativeTexturePtr();
 252                        OnTextureReady?.Invoke(texture);
 53                    }
 254                    break;
 55                case (int)VideoState.PLAYING:
 156                    if (shouldBePlaying && visible)
 57                    {
 158                        int width = plugin.GetWidth(videoPlayerId);
 159                        int height = plugin.GetHeight(videoPlayerId);
 160                        if (texture.width != width || texture.height != height)
 61                        {
 162                            if (texture.Resize(width, height))
 63                            {
 164                                texture.Apply();
 165                                textureNativePtr = texture.GetNativeTexturePtr();
 66                            }
 67                        }
 168                        if (texture.width > 0 && texture.height > 0)
 69                        {
 170                            plugin.TextureUpdate(videoPlayerId, textureNativePtr, isWebGL1);
 71                        }
 72                    }
 73                    break;
 74            }
 275        }
 76
 77        public void Play()
 78        {
 1079            if (isError)
 180                return;
 81
 982            plugin.Play(videoPlayerId, pausedAtTime);
 983            pausedAtTime = -1;
 84
 985            shouldBePlaying = true;
 986        }
 87
 88        public void Pause()
 89        {
 1690            if (isError)
 191                return;
 92
 1593            pausedAtTime = plugin.GetTime(videoPlayerId);
 1594            plugin.Pause(videoPlayerId);
 1595            shouldBePlaying = false;
 1596        }
 97
 098        public bool IsPaused() { return !shouldBePlaying; }
 99
 100        public void SetVolume(float volume)
 101        {
 39102            if (isError)
 1103                return;
 104
 38105            plugin.SetVolume(videoPlayerId, volume);
 38106            this.volume = volume;
 38107        }
 108
 109        public void SetTime(float timeSecs)
 110        {
 3111            if (isError)
 1112                return;
 113
 2114            pausedAtTime = timeSecs;
 2115            plugin.SetTime(videoPlayerId, timeSecs);
 2116        }
 117
 118        public void SetLoop(bool loop)
 119        {
 18120            if (isError)
 1121                return;
 122
 17123            plugin.SetLoop(videoPlayerId, loop);
 17124        }
 125
 126        public void SetPlaybackRate(float playbackRate)
 127        {
 17128            if (isError)
 1129                return;
 130
 16131            plugin.SetPlaybackRate(videoPlayerId, playbackRate);
 16132        }
 133
 134        public float GetTime()
 135        {
 32136            if (isError)
 1137                return 0;
 138
 31139            return plugin.GetTime(videoPlayerId);
 140        }
 141
 142        public float GetDuration()
 143        {
 33144            if (isError)
 1145                return 0;
 146
 32147            float duration = plugin.GetDuration(videoPlayerId);
 148
 32149            if (float.IsNaN(duration))
 1150                duration = -1;
 151
 32152            return duration;
 153        }
 154
 155        public VideoState GetState()
 156        {
 96157            return (VideoState)plugin.GetState(videoPlayerId);
 158        }
 159
 160        public void Dispose()
 161        {
 16162            plugin.Remove(videoPlayerId);
 16163            UnityEngine.Object.Destroy(texture);
 16164            texture = null;
 16165        }
 166
 167        private Texture2D CreateTexture(int width, int height)
 168        {
 2169            Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false);
 2170            tex.wrapMode = TextureWrapMode.Clamp;
 2171            return tex;
 172        }
 173    }
 174}