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