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