| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Models; |
| | 5 | | using UnityEngine; |
| | 6 | | using DCL.Components.Video.Plugin; |
| | 7 | | using DCL.Helpers; |
| | 8 | | using DCL.Interface; |
| | 9 | | using DCL.SettingsCommon; |
| | 10 | | using UnityEngine.Assertions; |
| | 11 | | using AudioSettings = DCL.SettingsCommon.AudioSettings; |
| | 12 | |
|
| | 13 | | namespace DCL.Components |
| | 14 | | { |
| | 15 | | public class DCLVideoTexture : DCLTexture |
| | 16 | | { |
| 1 | 17 | | public static bool VERBOSE = false; |
| 1 | 18 | | public static Logger logger = new Logger("DCLVideoTexture") {verboseEnabled = VERBOSE}; |
| | 19 | |
|
| | 20 | | private const float OUTOFSCENE_TEX_UPDATE_INTERVAL_IN_SECONDS = 1.5f; |
| | 21 | | private const float VIDEO_PROGRESS_UPDATE_INTERVAL_IN_SECONDS = 1f; |
| | 22 | |
|
| 1 | 23 | | public static System.Func<IVideoPluginWrapper> videoPluginWrapperBuilder = () => new VideoPluginWrapper_WebGL(); |
| | 24 | |
|
| | 25 | | [System.Serializable] |
| | 26 | | new public class Model : BaseModel |
| | 27 | | { |
| | 28 | | public string videoClipId; |
| | 29 | | public bool playing = false; |
| 45 | 30 | | public float volume = 1f; |
| 45 | 31 | | public float playbackRate = 1f; |
| | 32 | | public bool loop = false; |
| 45 | 33 | | public float seek = -1; |
| | 34 | | public BabylonWrapMode wrap = BabylonWrapMode.CLAMP; |
| 45 | 35 | | public FilterMode samplingMode = FilterMode.Bilinear; |
| | 36 | |
|
| 15 | 37 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 38 | | } |
| | 39 | |
|
| | 40 | | internal WebVideoPlayer texturePlayer; |
| | 41 | | private Coroutine texturePlayerUpdateRoutine; |
| | 42 | | private float baseVolume; |
| 15 | 43 | | private float distanceVolumeModifier = 1f; |
| | 44 | | private bool isPlayStateDirty = false; |
| | 45 | | internal bool isVisible = false; |
| | 46 | |
|
| | 47 | | private bool isInitialized = false; |
| 15 | 48 | | private bool isPlayerInScene = true; |
| 15 | 49 | | private float currUpdateIntervalTime = OUTOFSCENE_TEX_UPDATE_INTERVAL_IN_SECONDS; |
| | 50 | | private float lastVideoProgressReportTime; |
| | 51 | |
|
| 15 | 52 | | internal Dictionary<string, ISharedComponent> attachedMaterials = new Dictionary<string, ISharedComponent>(); |
| | 53 | | private string lastVideoClipID; |
| | 54 | | private VideoState previousVideoState; |
| | 55 | |
|
| 15 | 56 | | public DCLVideoTexture() |
| | 57 | | { |
| 15 | 58 | | model = new Model(); |
| | 59 | |
|
| 15 | 60 | | DataStore.i.virtualAudioMixer.sceneSFXVolume.OnChange += OnVirtualAudioMixerChangedValue; |
| 15 | 61 | | } |
| | 62 | |
|
| | 63 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 64 | | { |
| 30 | 65 | | yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get()); |
| | 66 | |
|
| | 67 | | //If the scene creates and destroy the component before our renderer has been turned on bad things happen! |
| | 68 | | //TODO: Analyze if we can catch this upstream and stop the IEnumerator |
| 15 | 69 | | if (isDisposed) |
| | 70 | | { |
| 0 | 71 | | yield break; |
| | 72 | | } |
| | 73 | |
|
| 15 | 74 | | var model = (Model) newModel; |
| | 75 | |
|
| 15 | 76 | | unitySamplingMode = model.samplingMode; |
| | 77 | |
|
| 15 | 78 | | switch (model.wrap) |
| | 79 | | { |
| | 80 | | case BabylonWrapMode.CLAMP: |
| 15 | 81 | | unityWrap = TextureWrapMode.Clamp; |
| 15 | 82 | | break; |
| | 83 | | case BabylonWrapMode.WRAP: |
| 0 | 84 | | unityWrap = TextureWrapMode.Repeat; |
| 0 | 85 | | break; |
| | 86 | | case BabylonWrapMode.MIRROR: |
| 0 | 87 | | unityWrap = TextureWrapMode.Mirror; |
| | 88 | | break; |
| | 89 | | } |
| | 90 | |
|
| 15 | 91 | | lastVideoClipID = model.videoClipId; |
| | 92 | |
|
| 15 | 93 | | if (texturePlayer == null) |
| | 94 | | { |
| 15 | 95 | | DCLVideoClip dclVideoClip = scene.componentsManagerLegacy.GetSceneSharedComponent(lastVideoClipID) as DC |
| | 96 | |
|
| 15 | 97 | | if (dclVideoClip == null) |
| | 98 | | { |
| 0 | 99 | | logger.Error("Wrong video clip type when playing VideoTexture!!"); |
| 0 | 100 | | yield break; |
| | 101 | | } |
| | 102 | |
|
| 15 | 103 | | Initialize(dclVideoClip); |
| | 104 | | } |
| | 105 | |
|
| 15 | 106 | | if (texture == null) |
| | 107 | | { |
| 434 | 108 | | yield return new WaitUntil(() => texturePlayer == null || ((texturePlayer.texture != null && texturePlay |
| | 109 | |
|
| 15 | 110 | | if (texturePlayer.isError) |
| | 111 | | { |
| 0 | 112 | | if (texturePlayerUpdateRoutine != null) |
| | 113 | | { |
| 0 | 114 | | CoroutineStarter.Stop(texturePlayerUpdateRoutine); |
| 0 | 115 | | texturePlayerUpdateRoutine = null; |
| | 116 | | } |
| | 117 | |
|
| 0 | 118 | | yield break; |
| | 119 | | } |
| | 120 | |
|
| 15 | 121 | | texture = texturePlayer.texture; |
| 15 | 122 | | SetPlayStateDirty(); |
| | 123 | | } |
| | 124 | |
|
| 15 | 125 | | if (texturePlayer != null) |
| | 126 | | { |
| 15 | 127 | | if (model.seek >= 0) |
| | 128 | | { |
| 1 | 129 | | texturePlayer.SetTime(model.seek); |
| 1 | 130 | | model.seek = -1; |
| | 131 | |
|
| | 132 | | // Applying seek is not immediate |
| 1 | 133 | | yield return null; |
| | 134 | | } |
| | 135 | |
|
| 15 | 136 | | if (model.playing) |
| | 137 | | { |
| 3 | 138 | | texturePlayer.Play(); |
| 3 | 139 | | } |
| | 140 | | else |
| | 141 | | { |
| 12 | 142 | | texturePlayer.Pause(); |
| | 143 | | } |
| | 144 | |
|
| 15 | 145 | | ReportVideoProgress(); |
| | 146 | |
|
| 15 | 147 | | if (baseVolume != model.volume) |
| | 148 | | { |
| 15 | 149 | | baseVolume = model.volume; |
| 15 | 150 | | UpdateVolume(); |
| | 151 | | } |
| | 152 | |
|
| 15 | 153 | | texturePlayer.SetPlaybackRate(model.playbackRate); |
| 15 | 154 | | texturePlayer.SetLoop(model.loop); |
| | 155 | | } |
| 15 | 156 | | } |
| | 157 | |
|
| | 158 | | private void Initialize(DCLVideoClip dclVideoClip) |
| | 159 | | { |
| 15 | 160 | | if (isInitialized) return; |
| 15 | 161 | | isInitialized = true; |
| | 162 | |
|
| 15 | 163 | | string videoId = (!string.IsNullOrEmpty(scene.sceneData.id)) ? scene.sceneData.id + id : scene.GetHashCode() |
| 15 | 164 | | texturePlayer = new WebVideoPlayer(videoId, dclVideoClip.GetUrl(), dclVideoClip.isStream, videoPluginWrapper |
| 15 | 165 | | texturePlayerUpdateRoutine = CoroutineStarter.Start(OnUpdate()); |
| | 166 | |
|
| 15 | 167 | | CommonScriptableObjects.playerCoords.OnChange += OnPlayerCoordsChanged; |
| 15 | 168 | | CommonScriptableObjects.sceneID.OnChange += OnSceneIDChanged; |
| 15 | 169 | | scene.OnEntityRemoved += SetPlayStateDirty; |
| 15 | 170 | | Settings.i.audioSettings.OnChanged += OnAudioSettingsChanged; |
| | 171 | |
|
| 15 | 172 | | OnSceneIDChanged(CommonScriptableObjects.sceneID.Get(), null); |
| 15 | 173 | | } |
| | 174 | |
|
| 0 | 175 | | public float GetVolume() { return ((Model) model).volume; } |
| | 176 | |
|
| | 177 | | private IEnumerator OnUpdate() |
| | 178 | | { |
| 499 | 179 | | while (true) |
| | 180 | | { |
| 514 | 181 | | UpdateDirtyState(); |
| 514 | 182 | | UpdateVideoTexture(); |
| 514 | 183 | | UpdateProgressReport(); |
| 514 | 184 | | yield return null; |
| | 185 | | } |
| | 186 | | } |
| | 187 | |
|
| | 188 | | private void UpdateDirtyState() |
| | 189 | | { |
| 514 | 190 | | if (!isPlayStateDirty) |
| 472 | 191 | | return; |
| | 192 | |
|
| 42 | 193 | | CalculateVideoVolumeAndPlayStatus(); |
| 42 | 194 | | isPlayStateDirty = false; |
| 42 | 195 | | } |
| | 196 | |
|
| | 197 | | private void UpdateVideoTexture() |
| | 198 | | { |
| 514 | 199 | | if (!isPlayerInScene && currUpdateIntervalTime < OUTOFSCENE_TEX_UPDATE_INTERVAL_IN_SECONDS) |
| | 200 | | { |
| 359 | 201 | | currUpdateIntervalTime += Time.unscaledDeltaTime; |
| 359 | 202 | | } |
| 155 | 203 | | else if (texturePlayer != null) |
| | 204 | | { |
| 155 | 205 | | currUpdateIntervalTime = 0; |
| 155 | 206 | | texturePlayer.Update(); |
| 155 | 207 | | texture = texturePlayer.texture; |
| | 208 | | } |
| 155 | 209 | | } |
| | 210 | |
|
| | 211 | | private void UpdateProgressReport() |
| | 212 | | { |
| 514 | 213 | | var currentState = texturePlayer.GetState(); |
| | 214 | |
|
| 514 | 215 | | if ( currentState == VideoState.PLAYING |
| | 216 | | && IsTimeToReportVideoProgress() |
| | 217 | | || previousVideoState != currentState) |
| | 218 | | { |
| 30 | 219 | | ReportVideoProgress(); |
| | 220 | | } |
| 514 | 221 | | } |
| | 222 | |
|
| | 223 | | private void ReportVideoProgress() |
| | 224 | | { |
| 45 | 225 | | lastVideoProgressReportTime = Time.unscaledTime; |
| 45 | 226 | | VideoState videoState = texturePlayer.GetState(); |
| 45 | 227 | | previousVideoState = videoState; |
| 45 | 228 | | var videoStatus = (int)videoState; |
| 45 | 229 | | var currentOffset = texturePlayer.GetTime(); |
| 45 | 230 | | var length = texturePlayer.GetDuration(); |
| 45 | 231 | | WebInterface.ReportVideoProgressEvent(id, scene.sceneData.id, lastVideoClipID, videoStatus, currentOffset, l |
| 45 | 232 | | } |
| | 233 | |
|
| | 234 | | private bool IsTimeToReportVideoProgress() |
| | 235 | | { |
| 14 | 236 | | return Time.unscaledTime - lastVideoProgressReportTime > VIDEO_PROGRESS_UPDATE_INTERVAL_IN_SECONDS; |
| | 237 | | } |
| | 238 | |
|
| | 239 | | private void CalculateVideoVolumeAndPlayStatus() |
| | 240 | | { |
| 42 | 241 | | isVisible = false; |
| 42 | 242 | | float minDistance = float.MaxValue; |
| 42 | 243 | | distanceVolumeModifier = 0; |
| | 244 | |
|
| 42 | 245 | | if (attachedMaterials.Count > 0) |
| | 246 | | { |
| 27 | 247 | | using (var iterator = attachedMaterials.GetEnumerator()) |
| | 248 | | { |
| 45 | 249 | | while (iterator.MoveNext()) |
| | 250 | | { |
| 27 | 251 | | var materialInfo = iterator.Current; |
| 27 | 252 | | bool isComponentVisible = DCLVideoTextureUtils.IsComponentVisible(materialInfo.Value); |
| | 253 | |
|
| 27 | 254 | | if (isComponentVisible) |
| | 255 | | { |
| 9 | 256 | | isVisible = true; |
| | 257 | |
|
| 9 | 258 | | var entityDist = DCLVideoTextureUtils.GetClosestDistanceSqr(materialInfo.Value, |
| | 259 | | CommonScriptableObjects.playerUnityPosition); |
| | 260 | |
|
| 9 | 261 | | if (entityDist < minDistance) |
| 9 | 262 | | minDistance = entityDist; |
| | 263 | |
|
| | 264 | | // NOTE: if current minDistance is enough for full volume then there is no need to keep iter |
| 9 | 265 | | if (minDistance <= DCL.Configuration.ParcelSettings.PARCEL_SIZE * DCL.Configuration.ParcelSe |
| 9 | 266 | | break; |
| | 267 | | } |
| | 268 | | } |
| 18 | 269 | | } |
| | 270 | | } |
| | 271 | |
|
| 42 | 272 | | if (isVisible) |
| | 273 | | { |
| | 274 | | const float maxDistanceBlockForSound = 12; |
| 9 | 275 | | float sqrParcelDistance = DCL.Configuration.ParcelSettings.PARCEL_SIZE * DCL.Configuration.ParcelSetting |
| 9 | 276 | | distanceVolumeModifier = 1 - Mathf.Clamp01(Mathf.FloorToInt(minDistance / sqrParcelDistance) / maxDistan |
| | 277 | | } |
| | 278 | |
|
| 42 | 279 | | if (texturePlayer != null) |
| | 280 | | { |
| 42 | 281 | | texturePlayer.visible = isVisible; |
| | 282 | | } |
| | 283 | |
|
| 42 | 284 | | UpdateVolume(); |
| 42 | 285 | | } |
| | 286 | |
|
| 0 | 287 | | private void OnVirtualAudioMixerChangedValue(float currentValue, float previousValue) { UpdateVolume(); } |
| | 288 | |
|
| | 289 | | private void UpdateVolume() |
| | 290 | | { |
| 59 | 291 | | if (texturePlayer == null) |
| 0 | 292 | | return; |
| | 293 | |
|
| 59 | 294 | | float targetVolume = 0f; |
| | 295 | |
|
| 59 | 296 | | if (CommonScriptableObjects.rendererState.Get() && IsPlayerInSameSceneAsComponent(CommonScriptableObjects.sc |
| | 297 | | { |
| 47 | 298 | | targetVolume = baseVolume * distanceVolumeModifier; |
| 47 | 299 | | float virtualMixerVolume = DataStore.i.virtualAudioMixer.sceneSFXVolume.Get(); |
| 47 | 300 | | float sceneSFXSetting = Settings.i.audioSettings.Data.sceneSFXVolume; |
| 47 | 301 | | float masterSetting = Settings.i.audioSettings.Data.masterVolume; |
| 47 | 302 | | targetVolume *= Utils.ToVolumeCurve(virtualMixerVolume * sceneSFXSetting * masterSetting); |
| | 303 | | } |
| | 304 | |
|
| 59 | 305 | | texturePlayer.SetVolume(targetVolume); |
| 59 | 306 | | } |
| | 307 | |
|
| | 308 | | private bool IsPlayerInSameSceneAsComponent(string currentSceneId) |
| | 309 | | { |
| 69 | 310 | | if (scene == null) |
| 0 | 311 | | return false; |
| 69 | 312 | | if (string.IsNullOrEmpty(currentSceneId)) |
| 0 | 313 | | return false; |
| | 314 | |
|
| 69 | 315 | | return (scene.sceneData.id == currentSceneId) || (scene.isPersistent); |
| | 316 | | } |
| | 317 | |
|
| 4 | 318 | | private void OnPlayerCoordsChanged(Vector2Int coords, Vector2Int prevCoords) => SetPlayStateDirty(); |
| | 319 | |
|
| 17 | 320 | | private void OnSceneIDChanged(string current, string previous) => isPlayerInScene = IsPlayerInSameSceneAsCompone |
| | 321 | |
|
| | 322 | | public override void AttachTo(ISharedComponent component) |
| | 323 | | { |
| 10 | 324 | | Assert.IsTrue(component != null, "Attachment must not be null!"); |
| | 325 | |
|
| 10 | 326 | | if (attachedMaterials.ContainsKey(component.id)) |
| 0 | 327 | | return; |
| | 328 | |
|
| 10 | 329 | | AddReference(component); |
| | 330 | |
|
| 10 | 331 | | SetPlayStateDirty(); |
| 10 | 332 | | attachedMaterials.Add(component.id, component); |
| | 333 | |
|
| 10 | 334 | | component.OnAttach += SetPlayStateDirty; |
| 10 | 335 | | component.OnDetach += SetPlayStateDirty; |
| 10 | 336 | | DCLVideoTextureUtils.SubscribeToEntityUpdates(component, SetPlayStateDirty); |
| 10 | 337 | | } |
| | 338 | |
|
| | 339 | | public override void DetachFrom(ISharedComponent component) |
| | 340 | | { |
| 1 | 341 | | Assert.IsTrue(component != null, "Component must not be null!"); |
| | 342 | |
|
| 1 | 343 | | if (!attachedMaterials.ContainsKey(component.id)) |
| 0 | 344 | | return; |
| | 345 | |
|
| 1 | 346 | | if (texturePlayer != null) |
| 1 | 347 | | texturePlayer.Pause(); |
| | 348 | |
|
| 1 | 349 | | attachedMaterials.Remove(component.id); |
| | 350 | |
|
| 1 | 351 | | component.OnAttach -= SetPlayStateDirty; |
| 1 | 352 | | component.OnDetach -= SetPlayStateDirty; |
| 1 | 353 | | DCLVideoTextureUtils.UnsubscribeToEntityShapeUpdate(component, SetPlayStateDirty); |
| | 354 | |
|
| 1 | 355 | | RemoveReference(component); |
| 1 | 356 | | SetPlayStateDirty(); |
| 1 | 357 | | } |
| | 358 | |
|
| 31 | 359 | | private void SetPlayStateDirty(IDCLEntity entity = null) => isPlayStateDirty = true; |
| | 360 | |
|
| 2 | 361 | | private void OnAudioSettingsChanged(AudioSettings settings) => UpdateVolume(); |
| | 362 | |
|
| | 363 | | public override void Dispose() |
| | 364 | | { |
| 1 | 365 | | DataStore.i.virtualAudioMixer.sceneSFXVolume.OnChange -= OnVirtualAudioMixerChangedValue; |
| 1 | 366 | | Settings.i.audioSettings.OnChanged -= OnAudioSettingsChanged; |
| 1 | 367 | | CommonScriptableObjects.playerCoords.OnChange -= OnPlayerCoordsChanged; |
| 1 | 368 | | CommonScriptableObjects.sceneID.OnChange -= OnSceneIDChanged; |
| | 369 | |
|
| 1 | 370 | | if (scene != null) |
| 1 | 371 | | scene.OnEntityRemoved -= SetPlayStateDirty; |
| | 372 | |
|
| 1 | 373 | | if (texturePlayerUpdateRoutine != null) |
| | 374 | | { |
| 1 | 375 | | CoroutineStarter.Stop(texturePlayerUpdateRoutine); |
| 1 | 376 | | texturePlayerUpdateRoutine = null; |
| | 377 | | } |
| | 378 | |
|
| 1 | 379 | | if (texturePlayer != null) |
| | 380 | | { |
| 1 | 381 | | texturePlayer.Dispose(); |
| 1 | 382 | | texturePlayer = null; |
| | 383 | | } |
| | 384 | |
|
| 1 | 385 | | Utils.SafeDestroy(texture); |
| 1 | 386 | | base.Dispose(); |
| 1 | 387 | | } |
| | 388 | | } |
| | 389 | | } |