Skip to content

Commit

Permalink
Get rid of nullability of AudioClip promise as it was not communicate…
Browse files Browse the repository at this point in the history
…d back to ECS (#257)
  • Loading branch information
mikhail-dcl authored Jan 11, 2024
1 parent bac0543 commit f12c930
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DCL.ECSComponents;
using DCL.Optimization.Pools;
using System;
using UnityEngine;
using Promise = ECS.StreamableLoading.Common.AssetPromise<UnityEngine.AudioClip, ECS.StreamableLoading.AudioClips.GetAudioClipIntention>;
Expand All @@ -10,16 +9,16 @@ public struct AudioSourceComponent : IDisposable
{
public readonly PBAudioSource PBAudioSource;

public Promise? ClipPromise;
public Promise ClipPromise;

/// <summary>
/// The final audio source ready for consumption
/// </summary>
public AudioSource Result;

public AudioSourceComponent(PBAudioSource pbAudioSource)
public AudioSourceComponent(PBAudioSource pbAudioSource, Promise promise)
{
ClipPromise = null;
ClipPromise = promise;
PBAudioSource = pbAudioSource;
Result = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ namespace DCL.SDKComponents.AudioSources
[UpdateInGroup(typeof(CleanUpGroup))]
[LogCategory(ReportCategory.AUDIO_SOURCES)]
[ThrottlingEnabled]
public partial class CleanUpAudioSourceSystem: BaseUnityLoopSystem, IFinalizeWorldSystem
public partial class CleanUpAudioSourceSystem : BaseUnityLoopSystem, IFinalizeWorldSystem
{
private ReleaseAudioSourceComponent releaseAudioSourceComponent;

private CleanUpAudioSourceSystem(World world, AudioClipsCache cache,IComponentPoolsRegistry poolsRegistry) : base(world)
private CleanUpAudioSourceSystem(World world, AudioClipsCache cache, IComponentPoolsRegistry poolsRegistry) : base(world)
{
releaseAudioSourceComponent = new ReleaseAudioSourceComponent(world, cache, poolsRegistry);
}
Expand Down Expand Up @@ -59,7 +59,7 @@ public void FinalizeComponents(in Query query)
private readonly AudioClipsCache cache;
private readonly IComponentPool componentPool;

public ReleaseAudioSourceComponent(World world, AudioClipsCache cache,IComponentPoolsRegistry poolsRegistry)
public ReleaseAudioSourceComponent(World world, AudioClipsCache cache, IComponentPoolsRegistry poolsRegistry)
{
this.world = world;
this.cache = cache;
Expand All @@ -69,18 +69,15 @@ public ReleaseAudioSourceComponent(World world, AudioClipsCache cache,IComponent

public void Update(ref AudioSourceComponent component)
{
if (component.ClipPromise == null) return; // loading not started
GetAudioClipIntention clipIntent = component.ClipPromise.LoadingIntention;

component.ClipPromise.ForgetLoading(world);

if (component.Result == null) // loading in progress
{
component.ClipPromise.Value.ForgetLoading(world);
component.ClipPromise = null;
return;
}

cache.Dereference(component.ClipPromise!.Value.LoadingIntention, component.Result.clip);
cache.Dereference(clipIntent, component.Result.clip);
componentPool.Release(component.Result);

component.Dispose();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ private void CreateAudioSourceComponentWithPromise(in Entity entity, ref PBAudio
if (!frameTimeBudgetProvider.TrySpendBudget()) return;
if (!sceneData.TryGetContentUrl(sdkAudioSource.AudioClipUrl, out URLAddress audioClipUrl)) return;

var audioSourceComponent = new AudioSourceComponent(sdkAudioSource);

audioSourceComponent.ClipPromise = Promise.Create(World, new GetAudioClipIntention
var audioSourceComponent = new AudioSourceComponent(sdkAudioSource, Promise.Create(World, new GetAudioClipIntention
{
CommonArguments = new CommonLoadingArguments(audioClipUrl),
AudioType = sdkAudioSource.AudioClipUrl.ToAudioType(),
}, partitionComponent);
}, partitionComponent));

World.Add(entity, audioSourceComponent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ protected override void Update(float t)
{
CreateAudioSourceQuery(World);
UpdateAudioSourceQuery(World);

// TODO: Handle Volume updates - refer to ECSAudioSourceComponentHandler.cs in unity-renderer and check UpdateAudioSourceVolume() method and its usages
}

[Query]
[All(typeof(PBAudioSource), typeof(AudioSourceComponent))]
private void UpdateAudioSource(ref PBAudioSource sdkAudioSource, ref AudioSourceComponent audioSourceComponent)
{
if (sdkAudioSource.IsDirty)
if (sdkAudioSource.IsDirty && audioSourceComponent.Result != null)
{
audioSourceComponent.Result.ApplyPBAudioSource(sdkAudioSource);
sdkAudioSource.IsDirty = false;
Expand All @@ -51,16 +52,21 @@ private void UpdateAudioSource(ref PBAudioSource sdkAudioSource, ref AudioSource
}

[Query]
private void CreateAudioSource(ref AudioSourceComponent audioSourceComponent, ref TransformComponent entityTransform)
private void CreateAudioSource(ref PBAudioSource sdkAudioSource, ref AudioSourceComponent audioSourceComponent, ref TransformComponent entityTransform)
{
if (NoBudget() || audioSourceComponent.ClipPromise == null || !audioSourceComponent.ClipPromise.Value.TryConsume(World, out StreamableLoadingResult<AudioClip> promiseResult))
if (NoBudget()
|| audioSourceComponent.ClipPromise.IsConsumed
|| !audioSourceComponent.ClipPromise.TryConsume(World, out StreamableLoadingResult<AudioClip> promiseResult))
return;

if (audioSourceComponent.Result == null)
audioSourceComponent.Result ??= audioSourcesPool.Get();

audioSourceComponent.Result.FromPBAudioSource(promiseResult.Asset, audioSourceComponent.PBAudioSource);

// Reset isDirty as we just applied the PBAudioSource to the AudioSource
sdkAudioSource.IsDirty = false;

Transform transform = audioSourceComponent.Result.transform;
transform.SetParent(entityTransform.Transform, false);
transform.ResetLocalTRS();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ public async Task ShouldCreateAudioSource_WithDownloadedAudioClip_WhenPBAudioSou
startLoadingSystem.Update(0);

world.TryGet(entity, out AudioSourceComponent audioSourceComponent);
world.Get<StreamableLoadingState>(audioSourceComponent.ClipPromise!.Value.Entity).SetAllowed(Substitute.For<IAcquiredBudget>());
world.Get<StreamableLoadingState>(audioSourceComponent.ClipPromise.Entity).SetAllowed(Substitute.For<IAcquiredBudget>());

loadAudioClipSystem.Update(1);
await UniTask.WaitUntil(() => audioSourceComponent.ClipPromise.Value.TryGetResult(world, out _));
await UniTask.WaitUntil(() => audioSourceComponent.ClipPromise.TryGetResult(world, out _));

updateAudioSourceSystem.Update(2);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Arch.Core;
using DCL.ECSComponents;
using DCL.Optimization.PerformanceBudgeting;
using DCL.Optimization.Pools;
using ECS.Prioritization.Components;
Expand Down Expand Up @@ -28,13 +29,12 @@ public void SetUp()

void CreateComponent()
{
component = new AudioSourceComponent(CreatePBAudioSource());
component.ClipPromise = AssetPromise<AudioClip, GetAudioClipIntention>.Create(world, new GetAudioClipIntention(), PartitionComponent.TOP_PRIORITY);
component = new AudioSourceComponent(CreatePBAudioSource(), AssetPromise<AudioClip, GetAudioClipIntention>.Create(world, new GetAudioClipIntention(), PartitionComponent.TOP_PRIORITY));
}

void CreateEntity()
{
entity = world.Create(component);
entity = world.Create(component, new PBAudioSource());
AddTransformToEntity(entity);
}
}
Expand Down Expand Up @@ -75,7 +75,7 @@ public void NotCreateAudioSourceIfClipNotFinishedLoading()
public void CreateAudioSourceFromResolvedPromise()
{
// Arrange
world.Add(component.ClipPromise!.Value.Entity, new StreamableLoadingResult<AudioClip>(TestAudioClip));
world.Add(component.ClipPromise.Entity, new StreamableLoadingResult<AudioClip>(TestAudioClip));

// Act
system.Update(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public void CreateAudioSourceComponentForPBAudioSource()
Assert.That(audioSourceComponent.Result, Is.Null);

// Assert promise
Assert.That(audioSourceComponent.ClipPromise!.Value, Is.Not.Null);
AssetPromise<AudioClip, GetAudioClipIntention> promiseValue = audioSourceComponent.ClipPromise.Value;
Assert.That(audioSourceComponent.ClipPromise, Is.Not.EqualTo(AssetPromise<AudioClip, GetAudioClipIntention>.NULL));
AssetPromise<AudioClip, GetAudioClipIntention> promiseValue = audioSourceComponent.ClipPromise;
Assert.That(world.TryGet(promiseValue.Entity, out GetAudioClipIntention intention), Is.True);
Assert.That(intention.CommonArguments.URL, Is.EqualTo(pbAudioSource.AudioClipUrl));
Assert.That(intention.AudioType, Is.EqualTo(pbAudioSource.AudioClipUrl.ToAudioType()));
Expand Down

0 comments on commit f12c930

Please sign in to comment.