< Summary

Class:DCL.Components.DCLAudioStream
Assembly:DCL.Components.Audio
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Audio/DCLAudioStream.cs
Covered lines:58
Uncovered lines:27
Coverable lines:85
Total lines:185
Line coverage:68.2% (58 of 85)
Covered branches:0
Total branches:0
Covered methods:18
Total methods:23
Method coverage:78.2% (18 of 23)

Metrics

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

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;
 9using Decentraland.Sdk.Ecs6;
 10
 11namespace DCL.Components
 12{
 13    public class DCLAudioStream : BaseComponent, IOutOfSceneBoundariesHandler
 14    {
 15        [Serializable]
 16        public class Model : BaseModel
 17        {
 18            public string url;
 19            public bool playing;
 1820            public float volume = 1;
 21
 22            public override BaseModel GetDataFromJSON(string json) =>
 423                Utils.SafeFromJson<Model>(json);
 24
 25            public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel)
 26            {
 027                if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.AudioStream)
 028                    return Utils.SafeUnimplemented<DCLAudioStream, Model>(expected: ComponentBodyPayload.PayloadOneofCas
 29
 030                var pb = new Model();
 031                if (pbModel.AudioStream.HasPlaying) pb.playing = pbModel.AudioStream.Playing;
 032                if (pbModel.AudioStream.HasUrl) pb.url = pbModel.AudioStream.Url;
 033                if (pbModel.AudioStream.HasVolume) pb.volume = pbModel.AudioStream.Volume;
 34
 035                return pb;
 36            }
 37        }
 38
 839        private void Awake() { model = new Model(); }
 40
 41        public override void Initialize(IParcelScene scene, IDCLEntity entity)
 42        {
 443            base.Initialize(scene, entity);
 444            DataStore.i.sceneBoundariesChecker.Add(entity,this);
 445        }
 46
 4747        public bool isPlaying { get; private set; } = false;
 48        private float settingsVolume = 0;
 49        private bool isDestroyed = false;
 750        private Model prevModel = new Model();
 51
 552        public override string componentName => "AudioStream";
 53
 054        new public Model GetModel() { return (Model) model; }
 55
 56        public override IEnumerator ApplyChanges(BaseModel newModel)
 57        {
 1258            yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get());
 59
 60            //If the scene creates and destroy the component before our renderer has been turned on bad things happen!
 61            //TODO: Analyze if we can catch this upstream and stop the IEnumerator
 662            if (isDestroyed)
 063                yield break;
 64
 665            Model model = (Model)newModel;
 666            bool forceUpdate = prevModel.volume != model.volume;
 667            settingsVolume = GetCalculatedSettingsVolume(Settings.i.audioSettings.Data);
 68
 669            UpdatePlayingState(forceUpdate);
 670            prevModel = model;
 671            yield return null;
 572        }
 73
 74        private void Start()
 75        {
 476            CommonScriptableObjects.sceneNumber.OnChange += OnSceneChanged;
 477            CommonScriptableObjects.rendererState.OnChange += OnRendererStateChanged;
 478            Settings.i.audioSettings.OnChanged += OnSettingsChanged;
 479            DataStore.i.virtualAudioMixer.sceneSFXVolume.OnChange += SceneSFXVolume_OnChange;
 480        }
 81
 82        private void OnDestroy()
 83        {
 484            isDestroyed = true;
 485            CommonScriptableObjects.sceneNumber.OnChange -= OnSceneChanged;
 486            CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChanged;
 487            Settings.i.audioSettings.OnChanged -= OnSettingsChanged;
 488            DataStore.i.virtualAudioMixer.sceneSFXVolume.OnChange -= SceneSFXVolume_OnChange;
 489            StopStreaming();
 490            DataStore.i.sceneBoundariesChecker.Remove(entity,this);
 491        }
 92
 93        private void UpdatePlayingState(bool forceStateUpdate)
 94        {
 1095            if (!gameObject.activeInHierarchy)
 96            {
 097                return;
 98            }
 99
 10100            bool canPlayStream = scene.isPersistent || scene.sceneData.sceneNumber == CommonScriptableObjects.sceneNumbe
 10101            canPlayStream &= CommonScriptableObjects.rendererState;
 102
 10103            Model model = (Model) this.model;
 10104            bool shouldStopStream = (isPlaying && !model.playing) || (isPlaying && !canPlayStream);
 10105            bool shouldStartStream = !isPlaying && canPlayStream && model.playing;
 106
 10107            if (shouldStopStream)
 108            {
 3109                StopStreaming();
 3110                return;
 111            }
 112
 7113            if (shouldStartStream)
 114            {
 4115                StartStreaming();
 4116                return;
 117            }
 118
 3119            if (forceStateUpdate)
 120            {
 0121                if (isPlaying)
 0122                    StartStreaming();
 123                else
 0124                    StopStreaming();
 125            }
 3126        }
 127
 8128        private void OnSceneChanged(int currentSceneNumber, int previousSceneNumber) { UpdatePlayingState(false); }
 129
 130        private void OnRendererStateChanged(bool isEnable, bool prevState)
 131        {
 3132            if (isEnable)
 133            {
 0134                UpdatePlayingState(false);
 135            }
 3136        }
 137
 138        private void OnSettingsChanged(AudioSettings settings)
 139        {
 0140            float newSettingsVolume = GetCalculatedSettingsVolume(settings);
 0141            if (Math.Abs(settingsVolume - newSettingsVolume) > Mathf.Epsilon)
 142            {
 0143                settingsVolume = newSettingsVolume;
 0144                UpdatePlayingState(true);
 145            }
 0146        }
 147
 6148        private float GetCalculatedSettingsVolume(AudioSettings audioSettings) { return Utils.ToVolumeCurve(DataStore.i.
 149
 0150        private void SceneSFXVolume_OnChange(float current, float previous) { OnSettingsChanged(Settings.i.audioSettings
 151
 152        private void StopStreaming()
 153        {
 7154            Model model = (Model) this.model;
 7155            isPlaying = false;
 7156            Interface.WebInterface.SendAudioStreamEvent(model.url, false, model.volume * settingsVolume);
 7157        }
 158
 159        private void StartStreaming()
 160        {
 4161            Model model = (Model) this.model;
 4162            isPlaying = true;
 4163            Interface.WebInterface.SendAudioStreamEvent(model.url, true, model.volume * settingsVolume);
 4164        }
 165
 166        public void UpdateOutOfBoundariesState(bool isInsideBoundaries)
 167        {
 0168            if (!isPlaying)
 0169                return;
 170
 0171            if (isInsideBoundaries)
 172            {
 0173                StartStreaming();
 174            }
 175            else
 176            {
 0177                Model model = (Model) this.model;
 178                //Set volume to 0 (temporary solution until the refactor in #1421)
 0179                Interface.WebInterface.SendAudioStreamEvent(model.url, true, 0);
 180            }
 0181        }
 182
 1183        public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AUDIO_STREAM; }
 184    }
 185}