< Summary

Class:DCL.ECSComponents.ECSAudioStreamComponentHandler
Assembly:DCL.ECSComponents.AudioStream
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/AudioStream/Handler/ECSAudioStreamComponentHandler.cs
Covered lines:44
Uncovered lines:23
Coverable lines:67
Total lines:159
Line coverage:65.6% (44 of 67)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ECSAudioStreamComponentHandler()0%2100%
OnComponentCreated(...)0%330100%
OnComponentRemoved(...)0%110100%
OnComponentModelUpdated(...)0%3.033085.71%
Dispose()0%330100%
UpdateModel(...)0%110100%
StateHasChange(...)0%330100%
ConditionsToPlayChanged()0%30500%
CanAudioStreamBePlayed()0%6200%
OnSceneChanged(...)0%2100%
OnRendererStateChanged(...)0%2100%
OnSettingsChanged(...)0%6200%
GetCalculatedSettingsVolume(...)0%110100%
SceneSFXVolume_OnChange(...)0%2100%
StopStreaming()0%110100%
StartStreaming()0%2100%
SendUpdateAudioStreamEvent(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/AudioStream/Handler/ECSAudioStreamComponentHandler.cs

#LineLine coverage
 1using System;
 2using DCL.Controllers;
 3using DCL.ECSRuntime;
 4using DCL.Helpers;
 5using DCL.Interface;
 6using DCL.Models;
 7using DCL.SettingsCommon;
 8using UnityEngine;
 9using AudioSettings = DCL.SettingsCommon.AudioSettings;
 10
 11namespace DCL.ECSComponents
 12{
 13    public class ECSAudioStreamComponentHandler : IECSComponentHandler<PBAudioStream>
 14    {
 15        private float settingsVolume = 0;
 16
 017        internal float currentVolume = -1;
 18        internal bool isPlaying = false;
 19        internal AudioSource audioSource;
 20        internal PBAudioStream model;
 21        internal IParcelScene scene;
 22
 23        // Flags to check if we can activate the AudioStream
 24        internal bool isInsideScene = false;
 25        internal bool isRendererActive = false;
 26
 27        public void OnComponentCreated(IParcelScene scene, IDCLEntity entity)
 28        {
 729            this.scene = scene;
 730            audioSource = entity.gameObject.AddComponent<AudioSource>();
 31
 32            // If it is a smart wearable, we don't look up to see if the scene has change since the scene is global
 733            if(!scene.isPersistent)
 734                CommonScriptableObjects.sceneID.OnChange += OnSceneChanged;
 735            CommonScriptableObjects.rendererState.OnChange += OnRendererStateChanged;
 736            Settings.i.audioSettings.OnChanged += OnSettingsChanged;
 737            DataStore.i.virtualAudioMixer.sceneSFXVolume.OnChange += SceneSFXVolume_OnChange;
 738            settingsVolume = GetCalculatedSettingsVolume(Settings.i.audioSettings.Data);
 39
 740            isRendererActive = CommonScriptableObjects.rendererState.Get();
 741            isInsideScene = scene.isPersistent || scene.sceneData.id == CommonScriptableObjects.sceneID.Get();
 742        }
 43
 44        public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity)
 45        {
 846            Dispose();
 847        }
 48
 49        public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBAudioStream model)
 50        {
 51            // Nothing has change so we do an early return
 1052            if(!StateHasChange(model))
 053                return;
 54
 55            // We update the model and the volume
 1056            UpdateModel(model);
 57
 58            // In case that the audio stream can't be played we do an early return
 1059            if (!CanAudioStreamBePlayed())
 260                return;
 61
 62            // If everything went ok, we update the state
 863            SendUpdateAudioStreamEvent(model.Playing);
 864        }
 65
 66        private void Dispose()
 67        {
 868            if(!scene.isPersistent)
 869                CommonScriptableObjects.sceneID.OnChange -= OnSceneChanged;
 870            CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChanged;
 871            Settings.i.audioSettings.OnChanged -= OnSettingsChanged;
 872            DataStore.i.virtualAudioMixer.sceneSFXVolume.OnChange -= SceneSFXVolume_OnChange;
 73
 874            StopStreaming();
 75
 876            if (audioSource != null)
 77            {
 778                GameObject.Destroy(audioSource);
 779                audioSource = null;
 80            }
 881        }
 82
 83        private void UpdateModel(PBAudioStream model)
 84        {
 1085            this.model = model;
 1086            currentVolume = model.Volume * settingsVolume;
 1087        }
 88
 89        private bool StateHasChange(PBAudioStream model)
 90        {
 91            // First time that the model come so the state has change
 1092            if (this.model == null)
 793                return true;
 94
 395            bool shouldChangeState = isPlaying && !model.Playing;
 396            bool shouldUpdateVolume = Mathf.Approximately( currentVolume, model.Volume);
 397            bool shouldUpdateUrl = this.model.Url == model.Url;
 98
 399            return shouldChangeState || shouldUpdateVolume || shouldUpdateUrl;
 100        }
 101
 102        private void ConditionsToPlayChanged()
 103        {
 0104            bool canBePlayed = CanAudioStreamBePlayed();
 105
 0106            if(isPlaying && !canBePlayed)
 0107                StopStreaming();
 0108            if(!isPlaying && canBePlayed && model.Playing)
 0109                StartStreaming();
 0110        }
 111
 112        private bool CanAudioStreamBePlayed()
 113        {
 0114            return isInsideScene && isRendererActive;
 115        }
 116
 117        private void OnSceneChanged(string sceneId, string prevSceneId)
 118        {
 0119            isInsideScene = sceneId == scene.sceneData.id;
 0120            ConditionsToPlayChanged();
 0121        }
 122
 123        private void OnRendererStateChanged(bool isEnable, bool prevState)
 124        {
 0125            isRendererActive = isEnable;
 0126            ConditionsToPlayChanged();
 0127        }
 128
 129        private void OnSettingsChanged(AudioSettings settings)
 130        {
 0131            float newSettingsVolume = GetCalculatedSettingsVolume(settings);
 0132            if (Math.Abs(settingsVolume - newSettingsVolume) > Mathf.Epsilon)
 133            {
 0134                settingsVolume = newSettingsVolume;
 0135                SendUpdateAudioStreamEvent(isPlaying);
 136            }
 0137        }
 138
 7139        private float GetCalculatedSettingsVolume(AudioSettings audioSettings) { return Utils.ToVolumeCurve(DataStore.i.
 140
 0141        private void SceneSFXVolume_OnChange(float current, float previous) { OnSettingsChanged(Settings.i.audioSettings
 142
 143        private void StopStreaming()
 144        {
 8145            SendUpdateAudioStreamEvent(false);
 8146        }
 147
 148        private void StartStreaming()
 149        {
 0150            SendUpdateAudioStreamEvent(true);
 0151        }
 152
 153        private void SendUpdateAudioStreamEvent(bool play)
 154        {
 16155            isPlaying = play;
 16156            WebInterface.SendAudioStreamEvent(model.Url, isPlaying, currentVolume);
 16157        }
 158    }
 159}