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