< Summary

Class:DCL.Components.DCLAudioStream
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Audio/DCLAudioStream.cs
Covered lines:50
Uncovered lines:27
Coverable lines:77
Total lines:166
Line coverage:64.9% (50 of 77)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Model()0%110100%
GetDataFromJSON(...)0%110100%
Awake()0%110100%
DCLAudioStream()0%110100%
GetModel()0%2100%
ApplyChanges()0%6.046090%
Start()0%110100%
OnDestroy()0%110100%
IsPlayerInSameSceneAsComponent(...)0%6.65060%
UpdatePlayingState(...)0%15.213076.47%
OnSceneChanged(...)0%2100%
OnRendererStateChanged(...)0%6200%
OnSettingsChanged(...)0%6200%
GetCalculatedSettingsVolume(...)0%110100%
SceneSFXVolume_OnChange(...)0%2100%
StopStreaming()0%110100%
StartStreaming()0%110100%
UpdateOutOfBoundariesState(...)0%12300%
GetClassId()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Audio/DCLAudioStream.cs

#LineLine coverage
 1using System.Collections;
 2using DCL.Controllers;
 3using DCL.Helpers;
 4using DCL.Models;
 5using DCL.SettingsCommon;
 6
 7namespace 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;
 116            public float volume = 1;
 17
 118            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 19        }
 20
 221        private void Awake() { model = new Model(); }
 22
 23        private bool isPlaying = false;
 24        private float settingsVolume = 0;
 25        private bool isDestroyed = false;
 226        private Model prevModel = new Model();
 27
 028        new public Model GetModel() { return (Model) model; }
 29
 30        public override IEnumerator ApplyChanges(BaseModel newModel)
 31        {
 632            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
 336            if (isDestroyed)
 037                yield break;
 38
 339            Model model = (Model)newModel;
 340            bool forceUpdate = prevModel.volume != model.volume;
 341            settingsVolume = GetCalculatedSettingsVolume(Settings.i.audioSettings.Data);
 42
 343            UpdatePlayingState(forceUpdate);
 344            prevModel = model;
 345            yield return null;
 346        }
 47
 48        private void Start()
 49        {
 150            CommonScriptableObjects.sceneID.OnChange += OnSceneChanged;
 151            CommonScriptableObjects.rendererState.OnChange += OnRendererStateChanged;
 152            Settings.i.audioSettings.OnChanged += OnSettingsChanged;
 153            DataStore.i.virtualAudioMixer.sceneSFXVolume.OnChange += SceneSFXVolume_OnChange;
 154        }
 55
 56        private void OnDestroy()
 57        {
 158            isDestroyed = true;
 159            CommonScriptableObjects.sceneID.OnChange -= OnSceneChanged;
 160            CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChanged;
 161            Settings.i.audioSettings.OnChanged -= OnSettingsChanged;
 162            DataStore.i.virtualAudioMixer.sceneSFXVolume.OnChange -= SceneSFXVolume_OnChange;
 163            StopStreaming();
 164        }
 65
 66        private bool IsPlayerInSameSceneAsComponent(string currentSceneId)
 67        {
 368            if (scene == null)
 069                return false;
 370            if (string.IsNullOrEmpty(currentSceneId))
 071                return false;
 372            return (scene.sceneData.id == currentSceneId) || (scene is GlobalScene globalScene && globalScene.isPortable
 73        }
 74
 75        private void UpdatePlayingState(bool forceStateUpdate)
 76        {
 377            if (!gameObject.activeInHierarchy)
 78            {
 079                return;
 80            }
 81
 382            bool canPlayStream = IsPlayerInSameSceneAsComponent(CommonScriptableObjects.sceneID) && CommonScriptableObje
 83
 384            Model model = (Model) this.model;
 385            bool shouldStopStream = (isPlaying && !model.playing) || (isPlaying && !canPlayStream);
 386            bool shouldStartStream = !isPlaying && canPlayStream && model.playing;
 87
 388            if (shouldStopStream)
 89            {
 190                StopStreaming();
 191                return;
 92            }
 93
 294            if (shouldStartStream)
 95            {
 196                StartStreaming();
 197                return;
 98            }
 99
 1100            if (forceStateUpdate)
 101            {
 0102                if (isPlaying)
 0103                    StartStreaming();
 104                else
 0105                    StopStreaming();
 106            }
 1107        }
 108
 0109        private void OnSceneChanged(string sceneId, string prevSceneId) { UpdatePlayingState(false); }
 110
 111        private void OnRendererStateChanged(bool isEnable, bool prevState)
 112        {
 0113            if (isEnable)
 114            {
 0115                UpdatePlayingState(false);
 116            }
 0117        }
 118
 119        private void OnSettingsChanged(AudioSettings settings)
 120        {
 0121            float newSettingsVolume = GetCalculatedSettingsVolume(settings);
 0122            if (settingsVolume != newSettingsVolume)
 123            {
 0124                settingsVolume = newSettingsVolume;
 0125                UpdatePlayingState(true);
 126            }
 0127        }
 128
 3129        private float GetCalculatedSettingsVolume(AudioSettings audioSettings) { return Utils.ToVolumeCurve(DataStore.i.
 130
 0131        private void SceneSFXVolume_OnChange(float current, float previous) { OnSettingsChanged(Settings.i.audioSettings
 132
 133        private void StopStreaming()
 134        {
 2135            Model model = (Model) this.model;
 2136            isPlaying = false;
 2137            Interface.WebInterface.SendAudioStreamEvent(model.url, false, model.volume * settingsVolume);
 2138        }
 139
 140        private void StartStreaming()
 141        {
 1142            Model model = (Model) this.model;
 1143            isPlaying = true;
 1144            Interface.WebInterface.SendAudioStreamEvent(model.url, true, model.volume * settingsVolume);
 1145        }
 146
 147        public void UpdateOutOfBoundariesState(bool enable)
 148        {
 0149            if (!isPlaying)
 0150                return;
 151
 0152            if (enable)
 153            {
 0154                StartStreaming();
 0155            }
 156            else
 157            {
 0158                Model model = (Model) this.model;
 159                //Set volume to 0 (temporary solution until the refactor in #1421)
 0160                Interface.WebInterface.SendAudioStreamEvent(model.url, true, 0);
 161            }
 0162        }
 163
 0164        public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AUDIO_STREAM; }
 165    }
 166}