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