< 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:24
Uncovered lines:68
Coverable lines:92
Total lines:213
Line coverage:26% (24 of 92)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WebVideoPlayer(...)0%110100%
WebVideoPlayerCreate(...)0%2100%
WebVideoPlayerRemove(...)0%2100%
WebVideoPlayerTextureUpdate(...)0%2100%
WebVideoPlayerPlay(...)0%2100%
WebVideoPlayerPause(...)0%2100%
WebVideoPlayerVolume(...)0%2100%
WebVideoPlayerGetHeight(...)0%2100%
WebVideoPlayerGetWidth(...)0%2100%
WebVideoPlayerGetTime(...)0%2100%
WebVideoPlayerGetDuration(...)0%2100%
WebVideoPlayerGetState(...)0%2100%
WebVideoPlayerGetError(...)0%2100%
WebVideoPlayerSetTime(...)0%2100%
WebVideoPlayerSetPlaybackRate(...)0%2100%
WebVideoPlayerSetLoop(...)0%2100%
UpdateWebVideoTexture()0%1101000%
Play()0%6200%
Pause()0%2.022083.33%
IsPaused()0%2100%
SetVolume(...)0%2.032080%
SetTime(...)0%6200%
SetLoop(...)0%2.062075%
SetPlaybackRate(...)0%2.062075%
GetTime()0%6200%
GetDuration()0%6200%
Dispose()0%110100%
CreateTexture(...)0%2100%

File(s)

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

#LineLine coverage
 1using System;
 2using UnityEngine;
 3using System.Runtime.InteropServices;
 4
 5namespace DCL.Components.Video.Plugin
 6{
 7    public class WebVideoPlayer : IDisposable
 8    {
 9        public event Action<Texture> OnTextureReady;
 010        public Texture2D texture { private set; get; }
 011        public float volume { private set; get; }
 012        public bool playing { get { return shouldBePlaying; } }
 013        public bool visible { get; set; }
 014        public bool isError { get; private set; }
 15
 16        private enum VideoState { NONE = 0, ERROR = 1, LOADING = 2, READY = 3, PLAYING = 4, BUFFERING = 5 };
 17
 018        private static bool isWebGL1 => SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.OpenGL
 19
 20        private string videoPlayerId;
 21        private IntPtr textureNativePtr;
 22        private bool initialized = false;
 23        private bool shouldBePlaying = false;
 1124        private float pausedAtTime = -1;
 25
 26#if UNITY_WEBGL && !UNITY_EDITOR
 27    [DllImport("__Internal")]
 28    private static extern void WebVideoPlayerCreate(string id, string url, bool useHls);
 29    [DllImport("__Internal")]
 30    private static extern void WebVideoPlayerRemove(string id);
 31    [DllImport("__Internal")]
 32    private static extern void WebVideoPlayerTextureUpdate(string id, IntPtr texturePtr, bool isWebGL1);
 33    [DllImport("__Internal")]
 34    private static extern void WebVideoPlayerPlay(string id, float startTime);
 35    [DllImport("__Internal")]
 36    private static extern void WebVideoPlayerPause(string id);
 37    [DllImport("__Internal")]
 38    private static extern void WebVideoPlayerVolume(string id, float volume);
 39    [DllImport("__Internal")]
 40    private static extern int WebVideoPlayerGetHeight(string id);
 41    [DllImport("__Internal")]
 42    private static extern int WebVideoPlayerGetWidth(string id);
 43    [DllImport("__Internal")]
 44    private static extern float WebVideoPlayerGetTime(string id);
 45    [DllImport("__Internal")]
 46    private static extern float WebVideoPlayerGetDuration(string id);
 47    [DllImport("__Internal")]
 48    private static extern int WebVideoPlayerGetState(string id);
 49    [DllImport("__Internal")]
 50    private static extern string WebVideoPlayerGetError(string id);
 51    [DllImport("__Internal")]
 52    private static extern void WebVideoPlayerSetTime(string id, float second);
 53    [DllImport("__Internal")]
 54    private static extern void WebVideoPlayerSetPlaybackRate(string id, float playbackRate);
 55    [DllImport("__Internal")]
 56    private static extern void WebVideoPlayerSetLoop(string id, bool loop);
 57#else
 058        private static void WebVideoPlayerCreate(string id, string url, bool useHls) { }
 059        private static void WebVideoPlayerRemove(string id) { }
 060        private static void WebVideoPlayerTextureUpdate(string id, IntPtr texturePtr, bool isWebGL1) { }
 061        private static void WebVideoPlayerPlay(string id, float startTime) { }
 062        private static void WebVideoPlayerPause(string id) { }
 063        private static void WebVideoPlayerVolume(string id, float volume) { }
 064        private static int WebVideoPlayerGetHeight(string id) { return 0; }
 065        private static int WebVideoPlayerGetWidth(string id) { return 0; }
 066        private static float WebVideoPlayerGetTime(string id) { return 0; }
 067        private static float WebVideoPlayerGetDuration(string id) { return 0; }
 068        private static int WebVideoPlayerGetState(string id) { return (int)VideoState.ERROR; }
 069        private static string WebVideoPlayerGetError(string id) { return "WebVideoPlayer: Platform not supported"; }
 070        private static void WebVideoPlayerSetTime(string id, float second) { }
 071        private static void WebVideoPlayerSetPlaybackRate(string id, float playbackRate) { }
 072        private static void WebVideoPlayerSetLoop(string id, bool loop) { }
 73#endif
 74
 1175        public WebVideoPlayer(string id, string url, bool useHls)
 76        {
 1177            videoPlayerId = id;
 78
 1179            WebVideoPlayerCreate(id, url, useHls);
 1180        }
 81
 82        public void UpdateWebVideoTexture()
 83        {
 084            if (isError)
 85            {
 086                return;
 87            }
 88
 089            switch (WebVideoPlayerGetState(videoPlayerId))
 90            {
 91                case (int)VideoState.ERROR:
 092                    Debug.LogError(WebVideoPlayerGetError(videoPlayerId));
 093                    isError = true;
 094                    break;
 95                case (int)VideoState.READY:
 096                    if (!initialized)
 97                    {
 098                        initialized = true;
 099                        texture = CreateTexture(WebVideoPlayerGetWidth(videoPlayerId), WebVideoPlayerGetHeight(videoPlay
 0100                        textureNativePtr = texture.GetNativeTexturePtr();
 0101                        OnTextureReady?.Invoke(texture);
 102                    }
 0103                    break;
 104                case (int)VideoState.PLAYING:
 0105                    if (shouldBePlaying && visible)
 106                    {
 0107                        int width = WebVideoPlayerGetWidth(videoPlayerId);
 0108                        int height = WebVideoPlayerGetHeight(videoPlayerId);
 0109                        if (texture.width != width || texture.height != height)
 110                        {
 0111                            if (texture.Resize(width, height))
 112                            {
 0113                                texture.Apply();
 0114                                textureNativePtr = texture.GetNativeTexturePtr();
 115                            }
 116                        }
 0117                        if (texture.width > 0 && texture.height > 0)
 118                        {
 0119                            WebVideoPlayerTextureUpdate(videoPlayerId, textureNativePtr, isWebGL1);
 120                        }
 121                    }
 122                    break;
 123            }
 0124        }
 125
 126        public void Play()
 127        {
 0128            if (isError)
 0129                return;
 130
 0131            WebVideoPlayerPlay(videoPlayerId, pausedAtTime);
 0132            pausedAtTime = -1;
 133
 0134            shouldBePlaying = true;
 0135        }
 136
 137        public void Pause()
 138        {
 12139            if (isError)
 0140                return;
 141
 12142            pausedAtTime = WebVideoPlayerGetTime(videoPlayerId);
 12143            WebVideoPlayerPause(videoPlayerId);
 12144            shouldBePlaying = false;
 12145        }
 146
 0147        public bool IsPaused() { return !shouldBePlaying; }
 148
 149        public void SetVolume(float volume)
 150        {
 31151            if (isError)
 0152                return;
 153
 31154            WebVideoPlayerVolume(videoPlayerId, volume);
 31155            this.volume = volume;
 31156        }
 157
 158        public void SetTime(float timeSecs)
 159        {
 0160            if (isError)
 0161                return;
 162
 0163            pausedAtTime = timeSecs;
 0164            WebVideoPlayerSetTime(videoPlayerId, timeSecs);
 0165        }
 166
 167        public void SetLoop(bool loop)
 168        {
 11169            if (isError)
 0170                return;
 171
 11172            WebVideoPlayerSetLoop(videoPlayerId, loop);
 11173        }
 174
 175        public void SetPlaybackRate(float playbackRate)
 176        {
 11177            if (isError)
 0178                return;
 179
 11180            WebVideoPlayerSetPlaybackRate(videoPlayerId, playbackRate);
 11181        }
 182
 183        public float GetTime()
 184        {
 0185            if (isError)
 0186                return 0;
 187
 0188            return WebVideoPlayerGetTime(videoPlayerId);
 189        }
 190
 191        public float GetDuration()
 192        {
 0193            if (isError)
 0194                return 0;
 195
 0196            return WebVideoPlayerGetDuration(videoPlayerId);
 197        }
 198
 199        public void Dispose()
 200        {
 11201            WebVideoPlayerRemove(videoPlayerId);
 11202            UnityEngine.Object.Destroy(texture);
 11203            texture = null;
 11204        }
 205
 206        private Texture2D CreateTexture(int width, int height)
 207        {
 0208            Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false);
 0209            tex.wrapMode = TextureWrapMode.Clamp;
 0210            return tex;
 211        }
 212    }
 213}