Skip to content

Commit

Permalink
fix(mqtt): updated publish rate to 10 Hz for transform changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mwfarb committed Nov 1, 2022
1 parent 11aeb7c commit 5912fd6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 34 deletions.
33 changes: 16 additions & 17 deletions Runtime/ArenaCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ namespace ArenaUnity
[DisallowMultipleComponent]
public class ArenaCamera : PrettyObject
{
private const float avatarPublishIntervalSeconds = 1f;
private const float cameraKeepAliveInterval = 1f; // 1 second
private float publishInterval; // varies

private string messageType = "object";
private bool persist = false;
Expand All @@ -41,36 +42,34 @@ public void OnEnable()
void Start()
{
displayColor = ArenaUnity.ColorRandom();
StartCoroutine(CameraUpdater());
StartCoroutine(PublishTickThrottle());
}

IEnumerator CameraUpdater()
IEnumerator PublishTickThrottle()
{
while (true)
{
if (userid != null && camid != null)
{
// send more frequently when changed, otherwise minimum 1 second keep alive
if (transform.hasChanged && ArenaClientScene.Instance)
{
publishInterval = ((float)ArenaClientScene.Instance.camUpdateIntervalMs / 1000f);
transform.hasChanged = false;
}
else
{
publishInterval = cameraKeepAliveInterval;
}
PublishCreateUpdate();
}
yield return new WaitForSeconds(avatarPublishIntervalSeconds);
yield return new WaitForSeconds(publishInterval);
}
}

void Update()
{
// send only when changed, each publishInterval frames, or stop at 0 frames
if (!ArenaClientScene.Instance || ArenaClientScene.Instance.transformPublishInterval == 0 ||
Time.frameCount % ArenaClientScene.Instance.transformPublishInterval != 0)
return;
if (transform.hasChanged && userid != null && camid != null)
{
//TODO: prevent child objects of parent.transform.hasChanged = true from publishing unnecessarily

if (PublishCreateUpdate())
{
transform.hasChanged = false;
}
}
// let CameraTickThrottle handle publish frequency
}

public bool PublishCreateUpdate()
Expand Down
6 changes: 3 additions & 3 deletions Runtime/ArenaClientScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ protected override void Awake()
public bool logMqttEvents = false;
[Tooltip("Console log MQTT non-persist messages")]
public bool logMqttNonPersist = false;
[Tooltip("Publish per frames frequency to publish detected transform changes (0 to stop)")]
[Range(0, 60)]
public int transformPublishInterval = 30; // in publish per frames
[Tooltip("Publish interval frequency to publish detected transform changes (milliseconds)")]
[Range(100, 1000)]
public int camUpdateIntervalMs = 100;

/// <summary>
/// Browser URL for the scene.
Expand Down
36 changes: 22 additions & 14 deletions Runtime/ArenaObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class ArenaObject : PrettyObject

internal bool Created { get { return created; } set { created = value; } }

private float publishInterval; // varies
private bool created = false;
private string oldName; // test for rename
internal bool externalDelete = false;
Expand Down Expand Up @@ -68,8 +69,10 @@ void Start()
}

isJsonValidated = jsonData != null;
StartCoroutine(PublishTickThrottle());
}


public void SetTtlDeleteTimer(float seconds)
{
StartCoroutine(TtlUpdater(seconds));
Expand All @@ -82,26 +85,31 @@ IEnumerator TtlUpdater(float seconds)
Destroy(gameObject);
}

void Update()
IEnumerator PublishTickThrottle()
{
// send only when changed, each publishInterval frames, or stop at 0 frames
if (!ArenaClientScene.Instance || ArenaClientScene.Instance.transformPublishInterval == 0 ||
Time.frameCount % ArenaClientScene.Instance.transformPublishInterval != 0)
return;
//TODO: prevent child objects of parent.transform.hasChanged = true from publishing unnecessarily

HasPermissions = ArenaClientScene.Instance.sceneObjectRights;

if (transform.hasChanged || meshChanged)
while (true)
{
//TODO: prevent child objects of parent.transform.hasChanged = true from publishing unnecessarily

if (PublishCreateUpdate(true))
// send only when changed, each publishInterval
if ((transform.hasChanged || meshChanged) && ArenaClientScene.Instance)
{
transform.hasChanged = false;
meshChanged = false;
publishInterval = ((float)ArenaClientScene.Instance.camUpdateIntervalMs / 1000f);
if (PublishCreateUpdate(true))
{
transform.hasChanged = false;
meshChanged = false;
}
}
yield return new WaitForSeconds(publishInterval);
}
else if (oldName != null && name != oldName)
}

void Update()
{
HasPermissions = ArenaClientScene.Instance.sceneObjectRights;

if (oldName != null && name != oldName)
{
HandleRename();
}
Expand Down

0 comments on commit 5912fd6

Please sign in to comment.