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