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