| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Models; |
| | 6 | | using DCL.SettingsCommon; |
| | 7 | | using UnityEngine; |
| | 8 | | using AudioSettings = DCL.SettingsCommon.AudioSettings; |
| | 9 | |
|
| | 10 | | namespace DCL.Components |
| | 11 | | { |
| | 12 | | public class DCLAudioStream : BaseComponent, IOutOfSceneBoundariesHandler |
| | 13 | | { |
| | 14 | | [System.Serializable] |
| | 15 | | public class Model : BaseModel |
| | 16 | | { |
| | 17 | | public string url; |
| | 18 | | public bool playing = false; |
| 1 | 19 | | public float volume = 1; |
| | 20 | |
|
| 1 | 21 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 22 | | } |
| | 23 | |
|
| 2 | 24 | | private void Awake() { model = new Model(); } |
| | 25 | |
|
| | 26 | | private bool isPlaying = false; |
| | 27 | | private float settingsVolume = 0; |
| | 28 | | private bool isDestroyed = false; |
| 2 | 29 | | private Model prevModel = new Model(); |
| | 30 | |
|
| 0 | 31 | | new public Model GetModel() { return (Model) model; } |
| | 32 | |
|
| | 33 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 34 | | { |
| 6 | 35 | | yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get()); |
| | 36 | |
|
| | 37 | | //If the scene creates and destroy the component before our renderer has been turned on bad things happen! |
| | 38 | | //TODO: Analyze if we can catch this upstream and stop the IEnumerator |
| 3 | 39 | | if (isDestroyed) |
| 0 | 40 | | yield break; |
| | 41 | |
|
| 3 | 42 | | Model model = (Model)newModel; |
| 3 | 43 | | bool forceUpdate = prevModel.volume != model.volume; |
| 3 | 44 | | settingsVolume = GetCalculatedSettingsVolume(Settings.i.audioSettings.Data); |
| | 45 | |
|
| 3 | 46 | | UpdatePlayingState(forceUpdate); |
| 3 | 47 | | prevModel = model; |
| 3 | 48 | | yield return null; |
| 3 | 49 | | } |
| | 50 | |
|
| | 51 | | private void Start() |
| | 52 | | { |
| 1 | 53 | | CommonScriptableObjects.sceneID.OnChange += OnSceneChanged; |
| 1 | 54 | | CommonScriptableObjects.rendererState.OnChange += OnRendererStateChanged; |
| 1 | 55 | | Settings.i.audioSettings.OnChanged += OnSettingsChanged; |
| 1 | 56 | | DataStore.i.virtualAudioMixer.sceneSFXVolume.OnChange += SceneSFXVolume_OnChange; |
| 1 | 57 | | } |
| | 58 | |
|
| | 59 | | private void OnDestroy() |
| | 60 | | { |
| 1 | 61 | | isDestroyed = true; |
| 1 | 62 | | CommonScriptableObjects.sceneID.OnChange -= OnSceneChanged; |
| 1 | 63 | | CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChanged; |
| 1 | 64 | | Settings.i.audioSettings.OnChanged -= OnSettingsChanged; |
| 1 | 65 | | DataStore.i.virtualAudioMixer.sceneSFXVolume.OnChange -= SceneSFXVolume_OnChange; |
| 1 | 66 | | StopStreaming(); |
| 1 | 67 | | } |
| | 68 | |
|
| | 69 | | private bool IsPlayerInSameSceneAsComponent(string currentSceneId) |
| | 70 | | { |
| 3 | 71 | | if (scene == null) |
| 0 | 72 | | return false; |
| 3 | 73 | | if (string.IsNullOrEmpty(currentSceneId)) |
| 3 | 74 | | return false; |
| 0 | 75 | | return (scene.sceneData.id == currentSceneId) || (scene is GlobalScene globalScene && globalScene.isPortable |
| | 76 | | } |
| | 77 | |
|
| | 78 | | private void UpdatePlayingState(bool forceStateUpdate) |
| | 79 | | { |
| 3 | 80 | | if (!gameObject.activeInHierarchy) |
| | 81 | | { |
| 0 | 82 | | return; |
| | 83 | | } |
| | 84 | |
|
| 3 | 85 | | bool canPlayStream = IsPlayerInSameSceneAsComponent(CommonScriptableObjects.sceneID) && CommonScriptableObje |
| | 86 | |
|
| 3 | 87 | | Model model = (Model) this.model; |
| 3 | 88 | | bool shouldStopStream = (isPlaying && !model.playing) || (isPlaying && !canPlayStream); |
| 3 | 89 | | bool shouldStartStream = !isPlaying && canPlayStream && model.playing; |
| | 90 | |
|
| 3 | 91 | | if (shouldStopStream) |
| | 92 | | { |
| 0 | 93 | | StopStreaming(); |
| 0 | 94 | | return; |
| | 95 | | } |
| | 96 | |
|
| 3 | 97 | | if (shouldStartStream) |
| | 98 | | { |
| 0 | 99 | | StartStreaming(); |
| 0 | 100 | | return; |
| | 101 | | } |
| | 102 | |
|
| 3 | 103 | | if (forceStateUpdate) |
| | 104 | | { |
| 0 | 105 | | if (isPlaying) |
| 0 | 106 | | StartStreaming(); |
| | 107 | | else |
| 0 | 108 | | StopStreaming(); |
| | 109 | | } |
| 3 | 110 | | } |
| | 111 | |
|
| 0 | 112 | | private void OnSceneChanged(string sceneId, string prevSceneId) { UpdatePlayingState(false); } |
| | 113 | |
|
| | 114 | | private void OnRendererStateChanged(bool isEnable, bool prevState) |
| | 115 | | { |
| 0 | 116 | | if (isEnable) |
| | 117 | | { |
| 0 | 118 | | UpdatePlayingState(false); |
| | 119 | | } |
| 0 | 120 | | } |
| | 121 | |
|
| | 122 | | private void OnSettingsChanged(AudioSettings settings) |
| | 123 | | { |
| 0 | 124 | | float newSettingsVolume = GetCalculatedSettingsVolume(settings); |
| 0 | 125 | | if (Math.Abs(settingsVolume - newSettingsVolume) > Mathf.Epsilon) |
| | 126 | | { |
| 0 | 127 | | settingsVolume = newSettingsVolume; |
| 0 | 128 | | UpdatePlayingState(true); |
| | 129 | | } |
| 0 | 130 | | } |
| | 131 | |
|
| 3 | 132 | | private float GetCalculatedSettingsVolume(AudioSettings audioSettings) { return Utils.ToVolumeCurve(DataStore.i. |
| | 133 | |
|
| 0 | 134 | | private void SceneSFXVolume_OnChange(float current, float previous) { OnSettingsChanged(Settings.i.audioSettings |
| | 135 | |
|
| | 136 | | private void StopStreaming() |
| | 137 | | { |
| 1 | 138 | | Model model = (Model) this.model; |
| 1 | 139 | | isPlaying = false; |
| 1 | 140 | | Interface.WebInterface.SendAudioStreamEvent(model.url, false, model.volume * settingsVolume); |
| 1 | 141 | | } |
| | 142 | |
|
| | 143 | | private void StartStreaming() |
| | 144 | | { |
| 0 | 145 | | Model model = (Model) this.model; |
| 0 | 146 | | isPlaying = true; |
| 0 | 147 | | Interface.WebInterface.SendAudioStreamEvent(model.url, true, model.volume * settingsVolume); |
| 0 | 148 | | } |
| | 149 | |
|
| | 150 | | public void UpdateOutOfBoundariesState(bool enable) |
| | 151 | | { |
| 0 | 152 | | if (!isPlaying) |
| 0 | 153 | | return; |
| | 154 | |
|
| 0 | 155 | | if (enable) |
| | 156 | | { |
| 0 | 157 | | StartStreaming(); |
| 0 | 158 | | } |
| | 159 | | else |
| | 160 | | { |
| 0 | 161 | | Model model = (Model) this.model; |
| | 162 | | //Set volume to 0 (temporary solution until the refactor in #1421) |
| 0 | 163 | | Interface.WebInterface.SendAudioStreamEvent(model.url, true, 0); |
| | 164 | | } |
| 0 | 165 | | } |
| | 166 | |
|
| 0 | 167 | | public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AUDIO_STREAM; } |
| | 168 | | } |
| | 169 | | } |