| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace 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 | |
|
| 434 | 12 | | public Texture2D texture { private set; get; } |
| 59 | 13 | | public float volume { private set; get; } |
| 5 | 14 | | public bool playing => GetState() == VideoState.PLAYING; |
| 217 | 15 | | public bool isError => GetState() == VideoState.ERROR; |
| 66 | 16 | | public bool visible { get; set; } = true; |
| 84 | 17 | | 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; |
| 36 | 26 | | private float playStartTime = -1; |
| | 27 | |
|
| 36 | 28 | | private string lastError = ""; |
| | 29 | |
|
| 36 | 30 | | public WebVideoPlayer(string id, string url, bool useHls, IVideoPluginWrapper plugin) |
| | 31 | | { |
| 36 | 32 | | videoPlayerId = id; |
| 36 | 33 | | this.plugin = plugin; |
| 36 | 34 | | this.url = url; |
| 36 | 35 | | plugin.Create(id, url, useHls); |
| | 36 | |
|
| 36 | 37 | | SetAsLoading(); |
| 36 | 38 | | } |
| | 39 | |
|
| | 40 | | public void Update() |
| | 41 | | { |
| 111 | 42 | | switch (plugin.GetState(videoPlayerId)) |
| | 43 | | { |
| | 44 | | case VideoState.ERROR: |
| 2 | 45 | | string newError = plugin.GetError(videoPlayerId); |
| 2 | 46 | | if (newError == lastError) |
| | 47 | | break; |
| | 48 | |
|
| 2 | 49 | | SetAsError(); |
| 2 | 50 | | lastError = newError; |
| 2 | 51 | | Debug.LogError(lastError); |
| | 52 | |
|
| 2 | 53 | | break; |
| | 54 | | case VideoState.READY: |
| 51 | 55 | | if (!isReady) |
| | 56 | | { |
| 21 | 57 | | isReady = true; |
| 21 | 58 | | texture = plugin.PrepareTexture(videoPlayerId); |
| | 59 | | } |
| | 60 | |
|
| 51 | 61 | | if (playWhenReady) |
| | 62 | | { |
| 6 | 63 | | PlayInternal(); |
| 6 | 64 | | playWhenReady = false; |
| | 65 | | } |
| | 66 | |
|
| 6 | 67 | | break; |
| | 68 | | case VideoState.PLAYING: |
| 3 | 69 | | if (visible) |
| 3 | 70 | | plugin.TextureUpdate(videoPlayerId); |
| | 71 | |
|
| | 72 | | break; |
| | 73 | | } |
| 72 | 74 | | } |
| | 75 | |
|
| | 76 | | public void Play() |
| | 77 | | { |
| 13 | 78 | | if (isError) |
| 1 | 79 | | return; |
| | 80 | |
|
| 12 | 81 | | if (!isReady) |
| | 82 | | { |
| 8 | 83 | | playWhenReady = true; |
| 8 | 84 | | return; |
| | 85 | | } |
| | 86 | |
|
| 4 | 87 | | PlayInternal(); |
| 4 | 88 | | } |
| | 89 | |
|
| | 90 | | private void PlayInternal() |
| | 91 | | { |
| 10 | 92 | | plugin.Play(videoPlayerId, playStartTime); |
| 10 | 93 | | playStartTime = -1; |
| 10 | 94 | | } |
| | 95 | |
|
| | 96 | | public void Pause() |
| | 97 | | { |
| 18 | 98 | | if (isError) |
| 1 | 99 | | return; |
| | 100 | |
|
| 17 | 101 | | playStartTime = plugin.GetTime(videoPlayerId); |
| 17 | 102 | | plugin.Pause(videoPlayerId); |
| 17 | 103 | | playWhenReady = false; |
| 17 | 104 | | } |
| | 105 | |
|
| | 106 | | public void SetVolume(float volume) |
| | 107 | | { |
| 50 | 108 | | if (isError) |
| 1 | 109 | | return; |
| | 110 | |
|
| 49 | 111 | | plugin.SetVolume(videoPlayerId, volume); |
| 49 | 112 | | this.volume = volume; |
| 49 | 113 | | } |
| | 114 | |
|
| | 115 | | public void SetTime(float timeSecs) |
| | 116 | | { |
| 3 | 117 | | if (isError) |
| 1 | 118 | | return; |
| | 119 | |
|
| 2 | 120 | | playStartTime = timeSecs; |
| 2 | 121 | | plugin.SetTime(videoPlayerId, timeSecs); |
| 2 | 122 | | } |
| | 123 | |
|
| | 124 | | public void SetLoop(bool loop) |
| | 125 | | { |
| 18 | 126 | | if (isError) |
| 1 | 127 | | return; |
| | 128 | |
|
| 17 | 129 | | plugin.SetLoop(videoPlayerId, loop); |
| 17 | 130 | | } |
| | 131 | |
|
| | 132 | | public void SetPlaybackRate(float playbackRate) |
| | 133 | | { |
| 21 | 134 | | if (isError) |
| 1 | 135 | | return; |
| | 136 | |
|
| 20 | 137 | | plugin.SetPlaybackRate(videoPlayerId, playbackRate); |
| 20 | 138 | | } |
| | 139 | |
|
| | 140 | | public float GetTime() |
| | 141 | | { |
| 46 | 142 | | if (isError) |
| 1 | 143 | | return 0; |
| | 144 | |
|
| 45 | 145 | | return plugin.GetTime(videoPlayerId); |
| | 146 | | } |
| | 147 | |
|
| | 148 | | public float GetDuration() |
| | 149 | | { |
| 47 | 150 | | if (isError) |
| 1 | 151 | | return 0; |
| | 152 | |
|
| 46 | 153 | | float duration = plugin.GetDuration(videoPlayerId); |
| | 154 | |
|
| 46 | 155 | | if (float.IsNaN(duration)) |
| 1 | 156 | | duration = -1; |
| | 157 | |
|
| 46 | 158 | | return duration; |
| | 159 | | } |
| | 160 | |
|
| | 161 | | public VideoState GetState() |
| | 162 | | { |
| 401 | 163 | | return plugin.GetState(videoPlayerId); |
| | 164 | | } |
| | 165 | |
|
| | 166 | | public void SetAsLoading() |
| | 167 | | { |
| 36 | 168 | | UpdateTextureConservingAspectRatio(Resources.Load<Texture2D>(VIDEO_LOADING_PATH), true); |
| 36 | 169 | | } |
| | 170 | |
|
| | 171 | | public void SetAsError() |
| | 172 | | { |
| 2 | 173 | | UpdateTextureConservingAspectRatio(Resources.Load<Texture2D>(VIDEO_FAILED_PATH), true); |
| 2 | 174 | | } |
| | 175 | |
|
| | 176 | | public void Dispose() |
| | 177 | | { |
| 4 | 178 | | plugin.Remove(videoPlayerId); |
| 4 | 179 | | } |
| | 180 | |
|
| | 181 | |
|
| | 182 | | private void UpdateTextureConservingAspectRatio(Texture2D textureToCenter, bool fillBackground) |
| | 183 | | { |
| | 184 | | // Avoiding Memory leaks as textures are never destroyed by Unity |
| 38 | 185 | | if (texture != null) |
| | 186 | | { |
| 2 | 187 | | UnityEngine.Object.Destroy(texture); |
| 2 | 188 | | texture = null; |
| | 189 | | } |
| | 190 | |
|
| | 191 | | // Fix textureToCenter in the center of a 16:9 texture |
| 38 | 192 | | int xOffset = 0; |
| 38 | 193 | | int yOffset = 0; |
| 38 | 194 | | if ((float)textureToCenter.width / textureToCenter.height >= DEFAULT_ASPECT_RATIO) |
| | 195 | | { |
| 0 | 196 | | texture = new Texture2D(textureToCenter.width, Mathf.RoundToInt(textureToCenter.width / DEFAULT_ASPECT_R |
| 0 | 197 | | yOffset = (texture.height - textureToCenter.height) / 2; |
| | 198 | | } |
| | 199 | | else |
| | 200 | | { |
| 38 | 201 | | texture = new Texture2D(Mathf.RoundToInt(textureToCenter.height * DEFAULT_ASPECT_RATIO), textureToCenter |
| 38 | 202 | | xOffset = (texture.width - textureToCenter.width) / 2; |
| | 203 | | } |
| | 204 | |
|
| 38 | 205 | | if (fillBackground) |
| | 206 | | { |
| 38 | 207 | | Color32[] backgroundPixels = new Color32[texture.width * texture.height]; |
| 38 | 208 | | Color32 color = textureToCenter.GetPixel(0, 0); |
| 38 | 209 | | Array.Fill(backgroundPixels, color); |
| 38 | 210 | | texture.SetPixels32(backgroundPixels); |
| | 211 | | } |
| | 212 | |
|
| 38 | 213 | | Color32[] pixels = textureToCenter.GetPixels32(0); |
| 38 | 214 | | texture.SetPixels32(xOffset, yOffset, textureToCenter.width, textureToCenter.height, pixels); |
| 38 | 215 | | texture.Apply(); |
| 38 | 216 | | } |
| | 217 | | } |
| | 218 | | } |