Skip to content

Commit

Permalink
fix: Call GetStats multi times at the same time in StatsUI (#783)
Browse files Browse the repository at this point in the history
* wip

* fix

* dispose explicitly
  • Loading branch information
karasusan authored Oct 5, 2022
1 parent df423b6 commit 8d922b7
Showing 1 changed file with 75 additions and 42 deletions.
117 changes: 75 additions & 42 deletions com.unity.renderstreaming/Samples~/Example/Stats/ShowStatsUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,60 +103,93 @@ private IEnumerator CollectStats()
}
}

List<Coroutine> coroutines = new List<Coroutine>();

foreach (var sender in activeSenderList.Values.SelectMany(x => x))
{
var op = sender.GetStats();
yield return op;
if (op.IsError)
{
continue;
}

var report = op.Value;
if (lastSenderStats.TryGetValue(sender, out var statsDisplay))
{
statsDisplay.display.text = CreateDisplayString(report, statsDisplay.lastReport);
statsDisplay.lastReport = report;
}
else
{
var text = Instantiate(baseText, displayParent);
text.text = "";
text.gameObject.SetActive(true);
lastSenderStats[sender] = new StatsDisplay {display = text, lastReport = report};
}
var coroutine = StartCoroutine(UpdateStats(sender));
coroutines.Add(coroutine);
}

foreach (var receiver in activeReceiverList.Values.SelectMany(x => x))
{
var op = receiver.GetStats();
yield return op;

if (op.IsError)
{
continue;
}

var report = op.Value;
if (lastReceiverStats.TryGetValue(receiver, out var statsDisplay))
{
statsDisplay.display.text = CreateDisplayString(report, statsDisplay.lastReport);
statsDisplay.lastReport = report;
}
else
{
var text = Instantiate(baseText, displayParent);
text.text = "";
text.gameObject.SetActive(true);
lastReceiverStats[receiver] = new StatsDisplay {display = text, lastReport = report};
}
var coroutine = StartCoroutine(UpdateStats(receiver));
coroutines.Add(coroutine);
}
foreach(var coroutine in coroutines)
{
yield return coroutine;
}

var noStatsData = !lastSenderStats.Any() && !lastReceiverStats.Any();
baseText.gameObject.SetActive(noStatsData);
}
}

IEnumerator UpdateStats(RTCRtpReceiver receiver)
{
var op = receiver.GetStats();
yield return op;

if (op.IsError)
{
yield break;
}

var report = op.Value;
if (report == null)
{
yield break;
}

if (lastReceiverStats.TryGetValue(receiver, out var statsDisplay))
{
var lastReport = statsDisplay.lastReport;
statsDisplay.display.text = CreateDisplayString(report, lastReport);
statsDisplay.lastReport = report;
lastReport.Dispose();
}
else
{
var text = Instantiate(baseText, displayParent);
text.text = "";
text.gameObject.SetActive(true);
lastReceiverStats[receiver] = new StatsDisplay { display = text, lastReport = report };
}
}

IEnumerator UpdateStats(RTCRtpSender sender)
{
var op = sender.GetStats();
yield return op;


if (op.IsError)
{
yield break;
}

var report = op.Value;
if (report == null)
{
yield break;
}

if (lastSenderStats.TryGetValue(sender, out var statsDisplay))
{
var lastReport = statsDisplay.lastReport;
statsDisplay.display.text = CreateDisplayString(report, lastReport);
statsDisplay.lastReport = report;
lastReport.Dispose();
}
else
{
var text = Instantiate(baseText, displayParent);
text.text = "";
text.gameObject.SetActive(true);
lastSenderStats[sender] = new StatsDisplay { display = text, lastReport = report };
}
}

private void SetUpSenderBase(StreamSenderBase senderBase)
{
if (alreadySetupSenderList.Contains(senderBase))
Expand Down

0 comments on commit 8d922b7

Please sign in to comment.