< 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:42
Uncovered lines:35
Coverable lines:77
Total lines:169
Line coverage:54.5% (42 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%30.6113052.94%
OnSceneChanged(...)0%2100%
OnRendererStateChanged(...)0%6200%
OnSettingsChanged(...)0%6200%
GetCalculatedSettingsVolume(...)0%110100%
SceneSFXVolume_OnChange(...)0%2100%
StopStreaming()0%110100%
StartStreaming()0%2100%
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;
 2using System.Collections;
 3using DCL.Controllers;
 4using DCL.Helpers;
 5using DCL.Models;
 6using DCL.SettingsCommon;
 7using UnityEngine;
 8using AudioSettings = DCL.SettingsCommon.AudioSettings;
 9
 10namespace 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;
 119            public float volume = 1;
 20
 121            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 22        }
 23
 224        private void Awake() { model = new Model(); }
 25
 26        private bool isPlaying = false;
 27        private float settingsVolume = 0;
 28        private bool isDestroyed = false;
 229        private Model prevModel = new Model();
 30
 031        new public Model GetModel() { return (Model) model; }
 32
 33        public override IEnumerator ApplyChanges(BaseModel newModel)
 34        {
 635            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
 339            if (isDestroyed)
 040                yield break;
 41
 342            Model model = (Model)newModel;
 343            bool forceUpdate = prevModel.volume != model.volume;
 344            settingsVolume = GetCalculatedSettingsVolume(Settings.i.audioSettings.Data);
 45
 346            UpdatePlayingState(forceUpdate);
 347            prevModel = model;
 348            yield return null;
 349        }
 50
 51        private void Start()
 52        {
 153            CommonScriptableObjects.sceneID.OnChange += OnSceneChanged;
 154            CommonScriptableObjects.rendererState.OnChange += OnRendererStateChanged;
 155            Settings.i.audioSettings.OnChanged += OnSettingsChanged;
 156            DataStore.i.virtualAudioMixer.sceneSFXVolume.OnChange += SceneSFXVolume_OnChange;
 157        }
 58
 59        private void OnDestroy()
 60        {
 161            isDestroyed = true;
 162            CommonScriptableObjects.sceneID.OnChange -= OnSceneChanged;
 163            CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChanged;
 164            Settings.i.audioSettings.OnChanged -= OnSettingsChanged;
 165            DataStore.i.virtualAudioMixer.sceneSFXVolume.OnChange -= SceneSFXVolume_OnChange;
 166            StopStreaming();
 167        }
 68
 69        private bool IsPlayerInSameSceneAsComponent(string currentSceneId)
 70        {
 371            if (scene == null)
 072                return false;
 373            if (string.IsNullOrEmpty(currentSceneId))
 374                return false;
 075            return (scene.sceneData.id == currentSceneId) || (scene is GlobalScene globalScene && globalScene.isPortable
 76        }
 77
 78        private void UpdatePlayingState(bool forceStateUpdate)
 79        {
 380            if (!gameObject.activeInHierarchy)
 81            {
 082                return;
 83            }
 84
 385            bool canPlayStream = IsPlayerInSameSceneAsComponent(CommonScriptableObjects.sceneID) && CommonScriptableObje
 86
 387            Model model = (Model) this.model;
 388            bool shouldStopStream = (isPlaying && !model.playing) || (isPlaying && !canPlayStream);
 389            bool shouldStartStream = !isPlaying && canPlayStream && model.playing;
 90
 391            if (shouldStopStream)
 92            {
 093                StopStreaming();
 094                return;
 95            }
 96
 397            if (shouldStartStream)
 98            {
 099                StartStreaming();
 0100                return;
 101            }
 102
 3103            if (forceStateUpdate)
 104            {
 0105                if (isPlaying)
 0106                    StartStreaming();
 107                else
 0108                    StopStreaming();
 109            }
 3110        }
 111
 0112        private void OnSceneChanged(string sceneId, string prevSceneId) { UpdatePlayingState(false); }
 113
 114        private void OnRendererStateChanged(bool isEnable, bool prevState)
 115        {
 0116            if (isEnable)
 117            {
 0118                UpdatePlayingState(false);
 119            }
 0120        }
 121
 122        private void OnSettingsChanged(AudioSettings settings)
 123        {
 0124            float newSettingsVolume = GetCalculatedSettingsVolume(settings);
 0125            if (Math.Abs(settingsVolume - newSettingsVolume) > Mathf.Epsilon)
 126            {
 0127                settingsVolume = newSettingsVolume;
 0128                UpdatePlayingState(true);
 129            }
 0130        }
 131
 3132        private float GetCalculatedSettingsVolume(AudioSettings audioSettings) { return Utils.ToVolumeCurve(DataStore.i.
 133
 0134        private void SceneSFXVolume_OnChange(float current, float previous) { OnSettingsChanged(Settings.i.audioSettings
 135
 136        private void StopStreaming()
 137        {
 1138            Model model = (Model) this.model;
 1139            isPlaying = false;
 1140            Interface.WebInterface.SendAudioStreamEvent(model.url, false, model.volume * settingsVolume);
 1141        }
 142
 143        private void StartStreaming()
 144        {
 0145            Model model = (Model) this.model;
 0146            isPlaying = true;
 0147            Interface.WebInterface.SendAudioStreamEvent(model.url, true, model.volume * settingsVolume);
 0148        }
 149
 150        public void UpdateOutOfBoundariesState(bool enable)
 151        {
 0152            if (!isPlaying)
 0153                return;
 154
 0155            if (enable)
 156            {
 0157                StartStreaming();
 0158            }
 159            else
 160            {
 0161                Model model = (Model) this.model;
 162                //Set volume to 0 (temporary solution until the refactor in #1421)
 0163                Interface.WebInterface.SendAudioStreamEvent(model.url, true, 0);
 164            }
 0165        }
 166
 0167        public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AUDIO_STREAM; }
 168    }
 169}