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