| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL.Components.Video.Plugin |
| | 5 | | { |
| | 6 | | public class WebVideoPlayer : IDisposable |
| | 7 | | { |
| 0 | 8 | | public Texture2D texture { private set; get; } |
| 0 | 9 | | public float volume { private set; get; } |
| 1 | 10 | | public bool playing => GetState() == VideoState.PLAYING; |
| 186 | 11 | | public bool isError => GetState() == VideoState.ERROR; |
| 31 | 12 | | public bool visible { get; set; } = true; |
| | 13 | |
|
| | 14 | | public readonly string url; |
| | 15 | |
|
| | 16 | | private string videoPlayerId; |
| | 17 | |
|
| | 18 | | private readonly IVideoPluginWrapper plugin; |
| | 19 | |
|
| | 20 | | private bool isReady = false; |
| | 21 | | private bool playWhenReady = false; |
| 31 | 22 | | private float playStartTime = -1; |
| | 23 | |
|
| 31 | 24 | | public WebVideoPlayer(string id, string url, bool useHls, IVideoPluginWrapper plugin) |
| | 25 | | { |
| 31 | 26 | | videoPlayerId = id; |
| 31 | 27 | | this.plugin = plugin; |
| 31 | 28 | | this.url = url; |
| 31 | 29 | | texture = new Texture2D(1, 1); |
| 31 | 30 | | plugin.Create(id, url, useHls); |
| 31 | 31 | | } |
| | 32 | |
|
| | 33 | | public void Update() |
| | 34 | | { |
| 63 | 35 | | switch (plugin.GetState(videoPlayerId)) |
| | 36 | | { |
| | 37 | | case VideoState.ERROR: |
| 2 | 38 | | Debug.LogError(plugin.GetError(videoPlayerId)); |
| 2 | 39 | | break; |
| | 40 | | case VideoState.READY: |
| 20 | 41 | | if (!isReady) |
| | 42 | | { |
| 11 | 43 | | isReady = true; |
| 11 | 44 | | texture.UpdateExternalTexture((IntPtr)plugin.GetTexture(videoPlayerId)); |
| 11 | 45 | | texture.Apply(); |
| | 46 | | } |
| | 47 | |
|
| 20 | 48 | | if (playWhenReady) |
| | 49 | | { |
| 1 | 50 | | PlayInternal(); |
| 1 | 51 | | playWhenReady = false; |
| | 52 | | } |
| | 53 | |
|
| 1 | 54 | | break; |
| | 55 | | case VideoState.PLAYING: |
| 0 | 56 | | if (visible) |
| 0 | 57 | | plugin.TextureUpdate(videoPlayerId); |
| | 58 | |
|
| | 59 | | break; |
| | 60 | | } |
| 37 | 61 | | } |
| | 62 | |
|
| | 63 | | public void Play() |
| | 64 | | { |
| 11 | 65 | | if (isError) |
| 1 | 66 | | return; |
| | 67 | |
|
| 10 | 68 | | if (!isReady) |
| | 69 | | { |
| 6 | 70 | | playWhenReady = true; |
| 6 | 71 | | return; |
| | 72 | | } |
| | 73 | |
|
| 4 | 74 | | PlayInternal(); |
| 4 | 75 | | } |
| | 76 | |
|
| | 77 | | private void PlayInternal() |
| | 78 | | { |
| 5 | 79 | | plugin.Play(videoPlayerId, playStartTime); |
| 5 | 80 | | playStartTime = -1; |
| 5 | 81 | | } |
| | 82 | |
|
| | 83 | | public void Pause() |
| | 84 | | { |
| 16 | 85 | | if (isError) |
| 1 | 86 | | return; |
| | 87 | |
|
| 15 | 88 | | playStartTime = plugin.GetTime(videoPlayerId); |
| 15 | 89 | | plugin.Pause(videoPlayerId); |
| 15 | 90 | | playWhenReady = false; |
| 15 | 91 | | } |
| | 92 | |
|
| | 93 | | public void SetVolume(float volume) |
| | 94 | | { |
| 39 | 95 | | if (isError) |
| 1 | 96 | | return; |
| | 97 | |
|
| 38 | 98 | | plugin.SetVolume(videoPlayerId, volume); |
| 38 | 99 | | this.volume = volume; |
| 38 | 100 | | } |
| | 101 | |
|
| | 102 | | public void SetTime(float timeSecs) |
| | 103 | | { |
| 3 | 104 | | if (isError) |
| 1 | 105 | | return; |
| | 106 | |
|
| 2 | 107 | | playStartTime = timeSecs; |
| 2 | 108 | | plugin.SetTime(videoPlayerId, timeSecs); |
| 2 | 109 | | } |
| | 110 | |
|
| | 111 | | public void SetLoop(bool loop) |
| | 112 | | { |
| 18 | 113 | | if (isError) |
| 1 | 114 | | return; |
| | 115 | |
|
| 17 | 116 | | plugin.SetLoop(videoPlayerId, loop); |
| 17 | 117 | | } |
| | 118 | |
|
| | 119 | | public void SetPlaybackRate(float playbackRate) |
| | 120 | | { |
| 17 | 121 | | if (isError) |
| 1 | 122 | | return; |
| | 123 | |
|
| 16 | 124 | | plugin.SetPlaybackRate(videoPlayerId, playbackRate); |
| 16 | 125 | | } |
| | 126 | |
|
| | 127 | | public float GetTime() |
| | 128 | | { |
| 40 | 129 | | if (isError) |
| 1 | 130 | | return 0; |
| | 131 | |
|
| 39 | 132 | | return plugin.GetTime(videoPlayerId); |
| | 133 | | } |
| | 134 | |
|
| | 135 | | public float GetDuration() |
| | 136 | | { |
| 41 | 137 | | if (isError) |
| 1 | 138 | | return 0; |
| | 139 | |
|
| 40 | 140 | | float duration = plugin.GetDuration(videoPlayerId); |
| | 141 | |
|
| 40 | 142 | | if (float.IsNaN(duration)) |
| 1 | 143 | | duration = -1; |
| | 144 | |
|
| 40 | 145 | | return duration; |
| | 146 | | } |
| | 147 | |
|
| | 148 | | public VideoState GetState() |
| | 149 | | { |
| 291 | 150 | | return (VideoState)plugin.GetState(videoPlayerId); |
| | 151 | | } |
| | 152 | |
|
| | 153 | | public void Dispose() |
| | 154 | | { |
| 16 | 155 | | plugin.Remove(videoPlayerId); |
| 16 | 156 | | } |
| | 157 | | } |
| | 158 | | } |