Skip to content

Commit

Permalink
fix: use microseconds as a Timestamp value
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Jan 23, 2021
1 parent 43e0cba commit 8e00358
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using UnityEngine;

using Stopwatch = System.Diagnostics.Stopwatch;

public class OfficialDemoAndroid : DemoGraph {
const string outputStream = "output_video";

Expand All @@ -26,6 +28,7 @@ public class OfficialDemoAndroid : DemoGraph {
sinkNode.InputSidePacket.Add($"DESTINATION:{destinationBufferName}");

graph = new CalculatorGraph(calculatorGraphConfig);
stopwatch = new Stopwatch();
}

public override Status StartRun() {
Expand All @@ -45,6 +48,7 @@ public class OfficialDemoAndroid : DemoGraph {
gpuHelper.RunInGlContext(BuildDestination).AssertOk();
sidePacket.Emplace(destinationBufferName, outputPacket);

stopwatch.Start();
return graph.StartRun(sidePacket);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ public class OfficialDemoIOS : DemoGraph {
private SidePacket sidePacket;

public override Status StartRun() {
throw new NotSupportedException();
}

public override Status StartRun(Texture texture) {
Debug.Log("This graph is for testing official examples. You can customize the graph by editing `official_demo_dekstop_gpu.txt` (default is `hand_tracking_mobile.pbtxt`)");

outputStreamPoller = graph.AddOutputStreamPoller<ImageFrame>(outputStream).ConsumeValueOrDie();
Expand Down
33 changes: 27 additions & 6 deletions Assets/MediaPipe/Examples/Scripts/DemoGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
using System.Collections.Generic;
using UnityEngine;

using Stopwatch = System.Diagnostics.Stopwatch;

public abstract class DemoGraph : MonoBehaviour, IDemoGraph<TextureFrame> {
[SerializeField] protected TextAsset config = null;

protected const string inputStream = "input_video";
protected int timestampValue;
protected Stopwatch stopwatch;
protected static CalculatorGraph graph;
protected static GlCalculatorHelper gpuHelper;

Expand All @@ -20,6 +22,10 @@ public abstract class DemoGraph : MonoBehaviour, IDemoGraph<TextureFrame> {

protected virtual void OnDestroy() {
Stop();

if (stopwatch != null && stopwatch.IsRunning) {
stopwatch.Stop();
}
}

public virtual void Initialize() {
Expand All @@ -28,7 +34,7 @@ public abstract class DemoGraph : MonoBehaviour, IDemoGraph<TextureFrame> {
}

graph = new CalculatorGraph(config.text);
timestampValue = System.Environment.TickCount & System.Int32.MaxValue;
stopwatch = new Stopwatch();
}

public void Initialize(GpuResources gpuResources, GlCalculatorHelper gpuHelper) {
Expand All @@ -40,11 +46,12 @@ public abstract class DemoGraph : MonoBehaviour, IDemoGraph<TextureFrame> {

public abstract Status StartRun();
public virtual Status StartRun(Texture texture) {
stopwatch.Start();
return StartRun();
}

public Status PushInput(TextureFrame textureFrame) {
var timestamp = new Timestamp(++timestampValue);
var timestamp = GetCurrentTimestamp();

#if !UNITY_ANDROID
var imageFrame = new ImageFrame(
Expand Down Expand Up @@ -87,13 +94,18 @@ public abstract class DemoGraph : MonoBehaviour, IDemoGraph<TextureFrame> {
public abstract void RenderOutput(WebCamScreenController screenController, TextureFrame textureFrame);

public void Stop() {
if (graph != null) {
var status = graph.CloseAllPacketSources();
if (graph == null) { return; }

using (var status = graph.CloseAllPacketSources()) {
if (!status.ok) {
Debug.LogError(status.ToString());
}
}

using (var status = graph.WaitUntilDone()) {
if (!status.ok) {
Debug.LogError(status.ToString());
}
graph.WaitUntilDone().AssertOk();
}
}

Expand Down Expand Up @@ -130,4 +142,13 @@ public abstract class DemoGraph : MonoBehaviour, IDemoGraph<TextureFrame> {
protected bool IsGpuEnabled() {
return gpuHelper != null;
}

Timestamp GetCurrentTimestamp() {
if (stopwatch == null || !stopwatch.IsRunning) {
return Timestamp.Unset();
}

var microseconds = (stopwatch.ElapsedTicks) / (TimeSpan.TicksPerMillisecond / 1000);
return new Timestamp(microseconds);
}
}

0 comments on commit 8e00358

Please sign in to comment.