| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | | using System.Runtime.InteropServices; |
| | 4 | |
|
| | 5 | | namespace DCL.Components.Video.Plugin |
| | 6 | | { |
| | 7 | | public class WebVideoPlayer : IDisposable |
| | 8 | | { |
| | 9 | | public event Action<Texture> OnTextureReady; |
| 0 | 10 | | public Texture2D texture { private set; get; } |
| 0 | 11 | | public float volume { private set; get; } |
| 0 | 12 | | public bool playing { get { return shouldBePlaying; } } |
| 0 | 13 | | public bool visible { get; set; } |
| 0 | 14 | | public bool isError { get; private set; } |
| | 15 | |
|
| | 16 | | private enum VideoState { NONE = 0, ERROR = 1, LOADING = 2, READY = 3, PLAYING = 4, BUFFERING = 5 }; |
| | 17 | |
|
| 0 | 18 | | 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; |
| 11 | 24 | | 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 |
| 0 | 58 | | private static void WebVideoPlayerCreate(string id, string url, bool useHls) { } |
| 0 | 59 | | private static void WebVideoPlayerRemove(string id) { } |
| 0 | 60 | | private static void WebVideoPlayerTextureUpdate(string id, IntPtr texturePtr, bool isWebGL1) { } |
| 0 | 61 | | private static void WebVideoPlayerPlay(string id, float startTime) { } |
| 0 | 62 | | private static void WebVideoPlayerPause(string id) { } |
| 0 | 63 | | private static void WebVideoPlayerVolume(string id, float volume) { } |
| 0 | 64 | | private static int WebVideoPlayerGetHeight(string id) { return 0; } |
| 0 | 65 | | private static int WebVideoPlayerGetWidth(string id) { return 0; } |
| 0 | 66 | | private static float WebVideoPlayerGetTime(string id) { return 0; } |
| 0 | 67 | | private static float WebVideoPlayerGetDuration(string id) { return 0; } |
| 0 | 68 | | private static int WebVideoPlayerGetState(string id) { return (int)VideoState.ERROR; } |
| 0 | 69 | | private static string WebVideoPlayerGetError(string id) { return "WebVideoPlayer: Platform not supported"; } |
| 0 | 70 | | private static void WebVideoPlayerSetTime(string id, float second) { } |
| 0 | 71 | | private static void WebVideoPlayerSetPlaybackRate(string id, float playbackRate) { } |
| 0 | 72 | | private static void WebVideoPlayerSetLoop(string id, bool loop) { } |
| | 73 | | #endif |
| | 74 | |
|
| 11 | 75 | | public WebVideoPlayer(string id, string url, bool useHls) |
| | 76 | | { |
| 11 | 77 | | videoPlayerId = id; |
| | 78 | |
|
| 11 | 79 | | WebVideoPlayerCreate(id, url, useHls); |
| 11 | 80 | | } |
| | 81 | |
|
| | 82 | | public void UpdateWebVideoTexture() |
| | 83 | | { |
| 0 | 84 | | if (isError) |
| | 85 | | { |
| 0 | 86 | | return; |
| | 87 | | } |
| | 88 | |
|
| 0 | 89 | | switch (WebVideoPlayerGetState(videoPlayerId)) |
| | 90 | | { |
| | 91 | | case (int)VideoState.ERROR: |
| 0 | 92 | | Debug.LogError(WebVideoPlayerGetError(videoPlayerId)); |
| 0 | 93 | | isError = true; |
| 0 | 94 | | break; |
| | 95 | | case (int)VideoState.READY: |
| 0 | 96 | | if (!initialized) |
| | 97 | | { |
| 0 | 98 | | initialized = true; |
| 0 | 99 | | texture = CreateTexture(WebVideoPlayerGetWidth(videoPlayerId), WebVideoPlayerGetHeight(videoPlay |
| 0 | 100 | | textureNativePtr = texture.GetNativeTexturePtr(); |
| 0 | 101 | | OnTextureReady?.Invoke(texture); |
| | 102 | | } |
| 0 | 103 | | break; |
| | 104 | | case (int)VideoState.PLAYING: |
| 0 | 105 | | if (shouldBePlaying && visible) |
| | 106 | | { |
| 0 | 107 | | int width = WebVideoPlayerGetWidth(videoPlayerId); |
| 0 | 108 | | int height = WebVideoPlayerGetHeight(videoPlayerId); |
| 0 | 109 | | if (texture.width != width || texture.height != height) |
| | 110 | | { |
| 0 | 111 | | if (texture.Resize(width, height)) |
| | 112 | | { |
| 0 | 113 | | texture.Apply(); |
| 0 | 114 | | textureNativePtr = texture.GetNativeTexturePtr(); |
| | 115 | | } |
| | 116 | | } |
| 0 | 117 | | if (texture.width > 0 && texture.height > 0) |
| | 118 | | { |
| 0 | 119 | | WebVideoPlayerTextureUpdate(videoPlayerId, textureNativePtr, isWebGL1); |
| | 120 | | } |
| | 121 | | } |
| | 122 | | break; |
| | 123 | | } |
| 0 | 124 | | } |
| | 125 | |
|
| | 126 | | public void Play() |
| | 127 | | { |
| 0 | 128 | | if (isError) |
| 0 | 129 | | return; |
| | 130 | |
|
| 0 | 131 | | WebVideoPlayerPlay(videoPlayerId, pausedAtTime); |
| 0 | 132 | | pausedAtTime = -1; |
| | 133 | |
|
| 0 | 134 | | shouldBePlaying = true; |
| 0 | 135 | | } |
| | 136 | |
|
| | 137 | | public void Pause() |
| | 138 | | { |
| 12 | 139 | | if (isError) |
| 0 | 140 | | return; |
| | 141 | |
|
| 12 | 142 | | pausedAtTime = WebVideoPlayerGetTime(videoPlayerId); |
| 12 | 143 | | WebVideoPlayerPause(videoPlayerId); |
| 12 | 144 | | shouldBePlaying = false; |
| 12 | 145 | | } |
| | 146 | |
|
| 0 | 147 | | public bool IsPaused() { return !shouldBePlaying; } |
| | 148 | |
|
| | 149 | | public void SetVolume(float volume) |
| | 150 | | { |
| 31 | 151 | | if (isError) |
| 0 | 152 | | return; |
| | 153 | |
|
| 31 | 154 | | WebVideoPlayerVolume(videoPlayerId, volume); |
| 31 | 155 | | this.volume = volume; |
| 31 | 156 | | } |
| | 157 | |
|
| | 158 | | public void SetTime(float timeSecs) |
| | 159 | | { |
| 0 | 160 | | if (isError) |
| 0 | 161 | | return; |
| | 162 | |
|
| 0 | 163 | | pausedAtTime = timeSecs; |
| 0 | 164 | | WebVideoPlayerSetTime(videoPlayerId, timeSecs); |
| 0 | 165 | | } |
| | 166 | |
|
| | 167 | | public void SetLoop(bool loop) |
| | 168 | | { |
| 11 | 169 | | if (isError) |
| 0 | 170 | | return; |
| | 171 | |
|
| 11 | 172 | | WebVideoPlayerSetLoop(videoPlayerId, loop); |
| 11 | 173 | | } |
| | 174 | |
|
| | 175 | | public void SetPlaybackRate(float playbackRate) |
| | 176 | | { |
| 11 | 177 | | if (isError) |
| 0 | 178 | | return; |
| | 179 | |
|
| 11 | 180 | | WebVideoPlayerSetPlaybackRate(videoPlayerId, playbackRate); |
| 11 | 181 | | } |
| | 182 | |
|
| | 183 | | public float GetTime() |
| | 184 | | { |
| 0 | 185 | | if (isError) |
| 0 | 186 | | return 0; |
| | 187 | |
|
| 0 | 188 | | return WebVideoPlayerGetTime(videoPlayerId); |
| | 189 | | } |
| | 190 | |
|
| | 191 | | public float GetDuration() |
| | 192 | | { |
| 0 | 193 | | if (isError) |
| 0 | 194 | | return 0; |
| | 195 | |
|
| 0 | 196 | | return WebVideoPlayerGetDuration(videoPlayerId); |
| | 197 | | } |
| | 198 | |
|
| | 199 | | public void Dispose() |
| | 200 | | { |
| 11 | 201 | | WebVideoPlayerRemove(videoPlayerId); |
| 11 | 202 | | UnityEngine.Object.Destroy(texture); |
| 11 | 203 | | texture = null; |
| 11 | 204 | | } |
| | 205 | |
|
| | 206 | | private Texture2D CreateTexture(int width, int height) |
| | 207 | | { |
| 0 | 208 | | Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false); |
| 0 | 209 | | tex.wrapMode = TextureWrapMode.Clamp; |
| 0 | 210 | | return tex; |
| | 211 | | } |
| | 212 | | } |
| | 213 | | } |