| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL.Components.Video.Plugin |
| | 5 | | { |
| | 6 | | public class WebVideoPlayer : IDisposable |
| | 7 | | { |
| | 8 | | public event Action<Texture> OnTextureReady; |
| 0 | 9 | | public Texture2D texture { private set; get; } |
| 0 | 10 | | public float volume { private set; get; } |
| 0 | 11 | | public bool playing { get { return shouldBePlaying; } } |
| 0 | 12 | | public bool visible { get; set; } |
| 0 | 13 | | public bool isError { get; private set; } |
| | 14 | |
|
| 1 | 15 | | 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; |
| 31 | 22 | | private float pausedAtTime = -1; |
| | 23 | |
|
| | 24 | |
|
| 31 | 25 | | public WebVideoPlayer(string id, string url, bool useHls, IWebVideoPlayerPlugin plugin) |
| | 26 | | { |
| 31 | 27 | | videoPlayerId = id; |
| 31 | 28 | | this.plugin = plugin; |
| | 29 | |
|
| 31 | 30 | | plugin.Create(id, url, useHls); |
| 31 | 31 | | } |
| | 32 | |
|
| | 33 | | public void UpdateWebVideoTexture() |
| | 34 | | { |
| 6 | 35 | | if (isError) |
| | 36 | | { |
| 0 | 37 | | return; |
| | 38 | | } |
| | 39 | |
|
| 6 | 40 | | switch (plugin.GetState(videoPlayerId)) |
| | 41 | | { |
| | 42 | | case (int)VideoState.ERROR: |
| 2 | 43 | | Debug.LogError(plugin.GetError(videoPlayerId)); |
| 2 | 44 | | isError = true; |
| 2 | 45 | | break; |
| | 46 | | case (int)VideoState.READY: |
| 3 | 47 | | if (!initialized) |
| | 48 | | { |
| 2 | 49 | | initialized = true; |
| 2 | 50 | | texture = CreateTexture(plugin.GetWidth(videoPlayerId), plugin.GetHeight(videoPlayerId)); |
| 2 | 51 | | textureNativePtr = texture.GetNativeTexturePtr(); |
| 2 | 52 | | OnTextureReady?.Invoke(texture); |
| | 53 | | } |
| 2 | 54 | | break; |
| | 55 | | case (int)VideoState.PLAYING: |
| 1 | 56 | | if (shouldBePlaying && visible) |
| | 57 | | { |
| 1 | 58 | | int width = plugin.GetWidth(videoPlayerId); |
| 1 | 59 | | int height = plugin.GetHeight(videoPlayerId); |
| 1 | 60 | | if (texture.width != width || texture.height != height) |
| | 61 | | { |
| 1 | 62 | | if (texture.Resize(width, height)) |
| | 63 | | { |
| 1 | 64 | | texture.Apply(); |
| 1 | 65 | | textureNativePtr = texture.GetNativeTexturePtr(); |
| | 66 | | } |
| | 67 | | } |
| 1 | 68 | | if (texture.width > 0 && texture.height > 0) |
| | 69 | | { |
| 1 | 70 | | plugin.TextureUpdate(videoPlayerId, textureNativePtr, isWebGL1); |
| | 71 | | } |
| | 72 | | } |
| | 73 | | break; |
| | 74 | | } |
| 2 | 75 | | } |
| | 76 | |
|
| | 77 | | public void Play() |
| | 78 | | { |
| 10 | 79 | | if (isError) |
| 1 | 80 | | return; |
| | 81 | |
|
| 9 | 82 | | plugin.Play(videoPlayerId, pausedAtTime); |
| 9 | 83 | | pausedAtTime = -1; |
| | 84 | |
|
| 9 | 85 | | shouldBePlaying = true; |
| 9 | 86 | | } |
| | 87 | |
|
| | 88 | | public void Pause() |
| | 89 | | { |
| 16 | 90 | | if (isError) |
| 1 | 91 | | return; |
| | 92 | |
|
| 15 | 93 | | pausedAtTime = plugin.GetTime(videoPlayerId); |
| 15 | 94 | | plugin.Pause(videoPlayerId); |
| 15 | 95 | | shouldBePlaying = false; |
| 15 | 96 | | } |
| | 97 | |
|
| 0 | 98 | | public bool IsPaused() { return !shouldBePlaying; } |
| | 99 | |
|
| | 100 | | public void SetVolume(float volume) |
| | 101 | | { |
| 39 | 102 | | if (isError) |
| 1 | 103 | | return; |
| | 104 | |
|
| 38 | 105 | | plugin.SetVolume(videoPlayerId, volume); |
| 38 | 106 | | this.volume = volume; |
| 38 | 107 | | } |
| | 108 | |
|
| | 109 | | public void SetTime(float timeSecs) |
| | 110 | | { |
| 3 | 111 | | if (isError) |
| 1 | 112 | | return; |
| | 113 | |
|
| 2 | 114 | | pausedAtTime = timeSecs; |
| 2 | 115 | | plugin.SetTime(videoPlayerId, timeSecs); |
| 2 | 116 | | } |
| | 117 | |
|
| | 118 | | public void SetLoop(bool loop) |
| | 119 | | { |
| 18 | 120 | | if (isError) |
| 1 | 121 | | return; |
| | 122 | |
|
| 17 | 123 | | plugin.SetLoop(videoPlayerId, loop); |
| 17 | 124 | | } |
| | 125 | |
|
| | 126 | | public void SetPlaybackRate(float playbackRate) |
| | 127 | | { |
| 17 | 128 | | if (isError) |
| 1 | 129 | | return; |
| | 130 | |
|
| 16 | 131 | | plugin.SetPlaybackRate(videoPlayerId, playbackRate); |
| 16 | 132 | | } |
| | 133 | |
|
| | 134 | | public float GetTime() |
| | 135 | | { |
| 32 | 136 | | if (isError) |
| 1 | 137 | | return 0; |
| | 138 | |
|
| 31 | 139 | | return plugin.GetTime(videoPlayerId); |
| | 140 | | } |
| | 141 | |
|
| | 142 | | public float GetDuration() |
| | 143 | | { |
| 33 | 144 | | if (isError) |
| 1 | 145 | | return 0; |
| | 146 | |
|
| 32 | 147 | | float duration = plugin.GetDuration(videoPlayerId); |
| | 148 | |
|
| 32 | 149 | | if (float.IsNaN(duration)) |
| 1 | 150 | | duration = -1; |
| | 151 | |
|
| 32 | 152 | | return duration; |
| | 153 | | } |
| | 154 | |
|
| | 155 | | public VideoState GetState() |
| | 156 | | { |
| 96 | 157 | | return (VideoState)plugin.GetState(videoPlayerId); |
| | 158 | | } |
| | 159 | |
|
| | 160 | | public void Dispose() |
| | 161 | | { |
| 16 | 162 | | plugin.Remove(videoPlayerId); |
| 16 | 163 | | UnityEngine.Object.Destroy(texture); |
| 16 | 164 | | texture = null; |
| 16 | 165 | | } |
| | 166 | |
|
| | 167 | | private Texture2D CreateTexture(int width, int height) |
| | 168 | | { |
| 2 | 169 | | Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false); |
| 2 | 170 | | tex.wrapMode = TextureWrapMode.Clamp; |
| 2 | 171 | | return tex; |
| | 172 | | } |
| | 173 | | } |
| | 174 | | } |