| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Components.Video.Plugin; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.ECS7.InternalComponents; |
| | 5 | | using DCL.ECSRuntime; |
| | 6 | | using DCL.Models; |
| | 7 | | using DCL.SettingsCommon; |
| | 8 | | using System; |
| | 9 | | using System.Collections.Generic; |
| | 10 | | using System.Linq; |
| | 11 | | using AudioSettings = DCL.SettingsCommon.AudioSettings; |
| | 12 | |
|
| | 13 | | namespace DCL.ECSComponents |
| | 14 | | { |
| | 15 | | public class VideoPlayerHandler : IECSComponentHandler<PBVideoPlayer> |
| | 16 | | { |
| 1 | 17 | | private static readonly string[] NO_STREAM_EXTENSIONS = new[] { ".mp4", ".ogg", ".mov", ".webm" }; |
| | 18 | |
|
| | 19 | | internal DataStore_LoadingScreen.DecoupledLoadingScreen loadingScreen; |
| | 20 | | private readonly ISettingsRepository<AudioSettings> audioSettings; |
| | 21 | | private readonly DataStore_VirtualAudioMixer audioMixerDataStore; |
| | 22 | | private readonly IntVariable currentPlayerSceneNumber; |
| | 23 | |
|
| | 24 | | internal PBVideoPlayer lastModel = null; |
| | 25 | | internal WebVideoPlayer videoPlayer; |
| | 26 | |
|
| | 27 | | // Flags to check if we can activate the video |
| | 28 | | internal bool isRendererActive = false; |
| | 29 | | internal bool hadUserInteraction = false; |
| | 30 | | internal bool isValidUrl = false; |
| | 31 | |
|
| | 32 | | private readonly IInternalECSComponent<InternalVideoPlayer> videoPlayerInternalComponent; |
| | 33 | | private IParcelScene currentScene; |
| 7 | 34 | | private bool canVideoBePlayed => isRendererActive && hadUserInteraction && isValidUrl; |
| | 35 | |
|
| 7 | 36 | | public VideoPlayerHandler( |
| | 37 | | IInternalECSComponent<InternalVideoPlayer> videoPlayerInternalComponent, |
| | 38 | | DataStore_LoadingScreen.DecoupledLoadingScreen loadingScreen, |
| | 39 | | ISettingsRepository<AudioSettings> audioSettings, |
| | 40 | | DataStore_VirtualAudioMixer audioMixerDataStore, |
| | 41 | | IntVariable currentPlayerSceneNumber) |
| | 42 | | { |
| 7 | 43 | | this.videoPlayerInternalComponent = videoPlayerInternalComponent; |
| 7 | 44 | | this.loadingScreen = loadingScreen; |
| 7 | 45 | | this.audioSettings = audioSettings; |
| 7 | 46 | | this.audioMixerDataStore = audioMixerDataStore; |
| 7 | 47 | | this.currentPlayerSceneNumber = currentPlayerSceneNumber; |
| 7 | 48 | | } |
| | 49 | |
|
| | 50 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) |
| | 51 | | { |
| 6 | 52 | | isRendererActive = !loadingScreen.visible.Get(); |
| | 53 | |
|
| | 54 | | // We need to check if the user interacted with the application before playing the video, |
| | 55 | | // otherwise browsers won't play the video, ending up in a fake 'playing' state. |
| 6 | 56 | | hadUserInteraction = Helpers.Utils.IsCursorLocked; |
| | 57 | |
|
| 6 | 58 | | if (!hadUserInteraction) |
| 2 | 59 | | Helpers.Utils.OnCursorLockChanged += OnCursorLockChanged; |
| 6 | 60 | | loadingScreen.visible.OnChange += OnLoadingScreenStateChanged; |
| 6 | 61 | | audioSettings.OnChanged += OnAudioSettingsChanged; |
| 6 | 62 | | audioMixerDataStore.sceneSFXVolume.OnChange += OnSceneSfxVolumeChanged; |
| 6 | 63 | | currentPlayerSceneNumber.OnChange += OnPlayerSceneChanged; |
| 6 | 64 | | currentScene = scene; |
| 6 | 65 | | } |
| | 66 | |
|
| | 67 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 68 | | { |
| 3 | 69 | | Helpers.Utils.OnCursorLockChanged -= OnCursorLockChanged; |
| 3 | 70 | | loadingScreen.visible.OnChange -= OnLoadingScreenStateChanged; |
| 3 | 71 | | audioSettings.OnChanged -= OnAudioSettingsChanged; |
| 3 | 72 | | audioMixerDataStore.sceneSFXVolume.OnChange -= OnSceneSfxVolumeChanged; |
| 3 | 73 | | currentPlayerSceneNumber.OnChange -= OnPlayerSceneChanged; |
| | 74 | |
|
| | 75 | | // ECSVideoPlayerSystem.Update() will run a video events check before the component is removed |
| 3 | 76 | | videoPlayerInternalComponent.RemoveFor(scene, entity, new InternalVideoPlayer() |
| | 77 | | { |
| | 78 | | removed = true |
| | 79 | | }); |
| | 80 | |
|
| 3 | 81 | | videoPlayer?.Dispose(); |
| 3 | 82 | | currentScene = null; |
| 3 | 83 | | } |
| | 84 | |
|
| | 85 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBVideoPlayer model) |
| | 86 | | { |
| | 87 | | // Setup video player |
| 8 | 88 | | if (lastModel == null || lastModel.Src != model.Src) |
| | 89 | | { |
| 8 | 90 | | videoPlayer?.Dispose(); |
| | 91 | |
|
| 8 | 92 | | var entityId = entity.entityId.ToString(); |
| 8 | 93 | | string id = scene.sceneData.sceneNumber > 0 ? scene.sceneData.sceneNumber + entityId : scene.GetHashCode |
| | 94 | |
|
| 8 | 95 | | VideoType videoType = VideoType.Common; |
| 8 | 96 | | if (model.Src.StartsWith("livekit-video://")) |
| 0 | 97 | | videoType = VideoType.LiveKit; |
| 16 | 98 | | else if (!NO_STREAM_EXTENSIONS.Any(x => model.Src.EndsWith(x))) |
| 0 | 99 | | videoType = VideoType.Hls; |
| | 100 | |
|
| 8 | 101 | | string videoUrl = videoType != VideoType.LiveKit |
| | 102 | | ? model.GetVideoUrl(scene.contentProvider, scene.sceneData.requiredPermissions, scene.sceneData.allo |
| | 103 | | : model.Src; |
| | 104 | |
|
| 8 | 105 | | isValidUrl = !string.IsNullOrEmpty(videoUrl); |
| 8 | 106 | | if (!isValidUrl) |
| 0 | 107 | | return; |
| | 108 | |
|
| 8 | 109 | | videoPlayer = new WebVideoPlayer(id, videoUrl, videoType, DCLVideoTexture.videoPluginWrapperBuilder.Invo |
| 8 | 110 | | videoPlayerInternalComponent.PutFor(scene, entity, new InternalVideoPlayer() |
| | 111 | | { |
| | 112 | | videoPlayer = videoPlayer, |
| | 113 | | assignedMaterials = new List<InternalVideoPlayer.MaterialAssigned>(), |
| | 114 | | }); |
| | 115 | | } |
| | 116 | |
|
| | 117 | | // Apply model values except 'Playing' |
| 8 | 118 | | float lastPosition = lastModel?.GetPosition() ?? 0.0f; |
| 8 | 119 | | if (Math.Abs(lastPosition - model.GetPosition()) > 0.01f) // 0.01s of tolerance |
| 0 | 120 | | videoPlayer.SetTime(model.GetPosition()); |
| | 121 | |
|
| 8 | 122 | | UpdateVolume(model, audioSettings.Data, audioMixerDataStore.sceneSFXVolume.Get(), currentPlayerSceneNumber); |
| | 123 | |
|
| 8 | 124 | | videoPlayer.SetPlaybackRate(model.GetPlaybackRate()); |
| 8 | 125 | | videoPlayer.SetLoop(model.GetLoop()); |
| | 126 | |
|
| 8 | 127 | | lastModel = model; |
| | 128 | |
|
| 8 | 129 | | ConditionsToPlayVideoChanged(); |
| 8 | 130 | | } |
| | 131 | |
|
| | 132 | | private void ConditionsToPlayVideoChanged() |
| | 133 | | { |
| 10 | 134 | | if (lastModel == null) return; |
| | 135 | |
|
| 10 | 136 | | bool shouldBePlaying = lastModel.IsPlaying() && canVideoBePlayed; |
| 10 | 137 | | if (shouldBePlaying != videoPlayer.playing) |
| | 138 | | { |
| 6 | 139 | | if (shouldBePlaying) |
| 5 | 140 | | videoPlayer.Play(); |
| | 141 | | else |
| 1 | 142 | | videoPlayer.Pause(); |
| | 143 | | } |
| 5 | 144 | | } |
| | 145 | |
|
| | 146 | | private void OnCursorLockChanged(bool isLocked) |
| | 147 | | { |
| 1 | 148 | | if (!isLocked) return; |
| | 149 | |
|
| 1 | 150 | | hadUserInteraction = true; |
| 1 | 151 | | Helpers.Utils.OnCursorLockChanged -= OnCursorLockChanged; |
| 1 | 152 | | ConditionsToPlayVideoChanged(); |
| 1 | 153 | | } |
| | 154 | |
|
| | 155 | | private void OnLoadingScreenStateChanged(bool isScreenEnabled, bool prevState) |
| | 156 | | { |
| 1 | 157 | | isRendererActive = !isScreenEnabled; |
| 1 | 158 | | ConditionsToPlayVideoChanged(); |
| 1 | 159 | | } |
| | 160 | |
|
| | 161 | | private void OnSceneSfxVolumeChanged(float current, float previous) => |
| 1 | 162 | | UpdateVolume(lastModel, audioSettings.Data, current, currentPlayerSceneNumber); |
| | 163 | |
|
| | 164 | | private void OnAudioSettingsChanged(AudioSettings settings) => |
| 3 | 165 | | UpdateVolume(lastModel, settings, audioMixerDataStore.sceneSFXVolume.Get(), currentPlayerSceneNumber); |
| | 166 | |
|
| | 167 | | private void OnPlayerSceneChanged(int current, int previous) => |
| 0 | 168 | | UpdateVolume(lastModel, audioSettings.Data, audioMixerDataStore.sceneSFXVolume.Get(), current); |
| | 169 | |
|
| | 170 | | private void UpdateVolume(PBVideoPlayer model, AudioSettings settings, float sceneMixerSfxVolume, int currentSce |
| | 171 | | { |
| 12 | 172 | | if (model == null) return; |
| 12 | 173 | | if (videoPlayer == null) return; |
| | 174 | |
|
| 12 | 175 | | float volume = 0; |
| | 176 | |
|
| 12 | 177 | | if (IsPlayerInSameSceneAsComponent(currentSceneNumber)) |
| | 178 | | { |
| 11 | 179 | | float sceneSFXSetting = settings.sceneSFXVolume; |
| 11 | 180 | | float masterSetting = settings.masterVolume; |
| 11 | 181 | | volume = model.GetVolume() * sceneMixerSfxVolume * sceneSFXSetting * masterSetting; |
| | 182 | | } |
| | 183 | |
|
| 12 | 184 | | videoPlayer.SetVolume(volume); |
| 12 | 185 | | } |
| | 186 | |
|
| | 187 | | private bool IsPlayerInSameSceneAsComponent(int currentSceneNumber) |
| | 188 | | { |
| 12 | 189 | | if (currentScene == null) |
| 1 | 190 | | return false; |
| | 191 | |
|
| 11 | 192 | | if (currentSceneNumber <= 0) |
| 0 | 193 | | return false; |
| | 194 | |
|
| 11 | 195 | | return currentScene.sceneData?.sceneNumber == currentSceneNumber || currentScene.isPersistent; |
| | 196 | | } |
| | 197 | | } |
| | 198 | | } |