| | 1 | | using System; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.ECSRuntime; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Interface; |
| | 6 | | using DCL.Models; |
| | 7 | | using DCL.SettingsCommon; |
| | 8 | | using UnityEngine; |
| | 9 | | using AudioSettings = DCL.SettingsCommon.AudioSettings; |
| | 10 | |
|
| | 11 | | namespace DCL.ECSComponents |
| | 12 | | { |
| | 13 | | public class ECSAudioStreamComponentHandler : IECSComponentHandler<PBAudioStream> |
| | 14 | | { |
| | 15 | | private float settingsVolume = 0; |
| | 16 | |
|
| 0 | 17 | | internal float currentVolume = -1; |
| | 18 | | internal bool isPlaying = false; |
| | 19 | | internal PBAudioStream model; |
| | 20 | | internal IParcelScene scene; |
| | 21 | |
|
| | 22 | | // Flags to check if we can activate the AudioStream |
| | 23 | | internal bool isInsideScene = false; |
| | 24 | | internal bool isRendererActive = false; |
| | 25 | | internal bool wasCursorLocked = false; |
| | 26 | |
|
| | 27 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) |
| | 28 | | { |
| 6 | 29 | | this.scene = scene; |
| | 30 | | // If it is a smart wearable, we don't look up to see if the scene has change since the scene is global |
| 6 | 31 | | if (!scene.isPersistent) |
| | 32 | | { |
| 6 | 33 | | CommonScriptableObjects.sceneID.OnChange += OnSceneChanged; |
| | 34 | | } |
| | 35 | |
|
| 6 | 36 | | CommonScriptableObjects.rendererState.OnChange += OnRendererStateChanged; |
| 6 | 37 | | Settings.i.audioSettings.OnChanged += OnSettingsChanged; |
| 6 | 38 | | DataStore.i.virtualAudioMixer.sceneSFXVolume.OnChange += SceneSFXVolume_OnChange; |
| 6 | 39 | | settingsVolume = GetCalculatedSettingsVolume(Settings.i.audioSettings.Data); |
| | 40 | |
|
| 6 | 41 | | isRendererActive = CommonScriptableObjects.rendererState.Get(); |
| 6 | 42 | | isInsideScene = scene.isPersistent || scene.sceneData.id == CommonScriptableObjects.sceneID.Get(); |
| | 43 | |
|
| 6 | 44 | | wasCursorLocked = Utils.IsCursorLocked; |
| 6 | 45 | | if (!wasCursorLocked) |
| | 46 | | { |
| 6 | 47 | | Utils.OnCursorLockChanged += OnCursorLockChanged; |
| | 48 | | } |
| 6 | 49 | | } |
| | 50 | |
|
| | 51 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 52 | | { |
| 6 | 53 | | Utils.OnCursorLockChanged -= OnCursorLockChanged; |
| 6 | 54 | | Dispose(); |
| 6 | 55 | | } |
| | 56 | |
|
| | 57 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBAudioStream model) |
| | 58 | | { |
| | 59 | | // Nothing has change so we do an early return |
| 9 | 60 | | if (!StateHasChange(model)) |
| 0 | 61 | | return; |
| | 62 | |
|
| | 63 | | // We update the model and the volume |
| 9 | 64 | | UpdateModel(model); |
| | 65 | |
|
| | 66 | | // In case that the audio stream can't be played we do an early return |
| 9 | 67 | | if (!CanAudioStreamBePlayed()) |
| 6 | 68 | | return; |
| | 69 | |
|
| | 70 | | // If everything went ok, we update the state |
| 3 | 71 | | SendUpdateAudioStreamEvent(model.Playing); |
| 3 | 72 | | } |
| | 73 | |
|
| | 74 | | private void Dispose() |
| | 75 | | { |
| 6 | 76 | | if (!scene.isPersistent) |
| | 77 | | { |
| 6 | 78 | | CommonScriptableObjects.sceneID.OnChange -= OnSceneChanged; |
| | 79 | | } |
| 6 | 80 | | CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChanged; |
| 6 | 81 | | Settings.i.audioSettings.OnChanged -= OnSettingsChanged; |
| 6 | 82 | | DataStore.i.virtualAudioMixer.sceneSFXVolume.OnChange -= SceneSFXVolume_OnChange; |
| | 83 | |
|
| 6 | 84 | | StopStreaming(); |
| 6 | 85 | | } |
| | 86 | |
|
| | 87 | | private void UpdateModel(PBAudioStream model) |
| | 88 | | { |
| 9 | 89 | | this.model = model; |
| 9 | 90 | | currentVolume = model.GetVolume() * settingsVolume; |
| 9 | 91 | | } |
| | 92 | |
|
| | 93 | | private bool StateHasChange(PBAudioStream model) |
| | 94 | | { |
| | 95 | | // First time that the model come so the state has change |
| 9 | 96 | | if (this.model == null) |
| 6 | 97 | | return true; |
| | 98 | |
|
| 3 | 99 | | bool shouldChangeState = isPlaying && !model.Playing; |
| 3 | 100 | | bool shouldUpdateVolume = Mathf.Approximately(currentVolume, model.GetVolume()); |
| 3 | 101 | | bool shouldUpdateUrl = this.model.Url == model.Url; |
| | 102 | |
|
| 3 | 103 | | return shouldChangeState || shouldUpdateVolume || shouldUpdateUrl; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | private void ConditionsToPlayChanged() |
| | 107 | | { |
| 0 | 108 | | bool canBePlayed = CanAudioStreamBePlayed(); |
| | 109 | |
|
| 0 | 110 | | if (isPlaying && !canBePlayed) |
| 0 | 111 | | StopStreaming(); |
| 0 | 112 | | if (!isPlaying && canBePlayed && model.Playing) |
| 0 | 113 | | StartStreaming(); |
| 0 | 114 | | } |
| | 115 | |
|
| | 116 | | private bool CanAudioStreamBePlayed() |
| | 117 | | { |
| 9 | 118 | | return isInsideScene && isRendererActive && wasCursorLocked; |
| | 119 | | } |
| | 120 | |
|
| | 121 | | private void OnSceneChanged(string sceneId, string prevSceneId) |
| | 122 | | { |
| 0 | 123 | | isInsideScene = sceneId == scene.sceneData.id; |
| 0 | 124 | | ConditionsToPlayChanged(); |
| 0 | 125 | | } |
| | 126 | |
|
| | 127 | | private void OnRendererStateChanged(bool isEnable, bool prevState) |
| | 128 | | { |
| 0 | 129 | | isRendererActive = isEnable; |
| 0 | 130 | | ConditionsToPlayChanged(); |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | private void OnSettingsChanged(AudioSettings settings) |
| | 134 | | { |
| 0 | 135 | | float newSettingsVolume = GetCalculatedSettingsVolume(settings); |
| 0 | 136 | | if (Math.Abs(settingsVolume - newSettingsVolume) > Mathf.Epsilon) |
| | 137 | | { |
| 0 | 138 | | settingsVolume = newSettingsVolume; |
| 0 | 139 | | SendUpdateAudioStreamEvent(isPlaying); |
| | 140 | | } |
| 0 | 141 | | } |
| | 142 | |
|
| | 143 | | private float GetCalculatedSettingsVolume(AudioSettings audioSettings) |
| | 144 | | { |
| 6 | 145 | | return Utils.ToVolumeCurve(DataStore.i.virtualAudioMixer.sceneSFXVolume.Get() * audioSettings.sceneSFXVolume |
| | 146 | | } |
| | 147 | |
|
| | 148 | | private void SceneSFXVolume_OnChange(float current, float previous) |
| | 149 | | { |
| 0 | 150 | | OnSettingsChanged(Settings.i.audioSettings.Data); |
| 0 | 151 | | } |
| | 152 | |
|
| | 153 | | private void StopStreaming() |
| | 154 | | { |
| 6 | 155 | | SendUpdateAudioStreamEvent(false); |
| 6 | 156 | | } |
| | 157 | |
|
| | 158 | | private void StartStreaming() |
| | 159 | | { |
| 0 | 160 | | SendUpdateAudioStreamEvent(true); |
| 0 | 161 | | } |
| | 162 | |
|
| | 163 | | private void SendUpdateAudioStreamEvent(bool play) |
| | 164 | | { |
| 9 | 165 | | isPlaying = play; |
| 9 | 166 | | WebInterface.SendAudioStreamEvent(model.Url, isPlaying, currentVolume); |
| 9 | 167 | | } |
| | 168 | |
|
| | 169 | | private void OnCursorLockChanged(bool isLocked) |
| | 170 | | { |
| 0 | 171 | | if (isLocked) |
| | 172 | | { |
| 0 | 173 | | wasCursorLocked = true; |
| 0 | 174 | | ConditionsToPlayChanged(); |
| | 175 | | } |
| 0 | 176 | | } |
| | 177 | | } |
| | 178 | | } |