diff --git a/Assets/MediaPipe/Examples/Graphs/OfficialDemo/Scripts/OfficialDemoAndroid.cs b/Assets/MediaPipe/Examples/Graphs/OfficialDemo/Scripts/OfficialDemoAndroid.cs index c5c01fce6..14ca88d9c 100644 --- a/Assets/MediaPipe/Examples/Graphs/OfficialDemo/Scripts/OfficialDemoAndroid.cs +++ b/Assets/MediaPipe/Examples/Graphs/OfficialDemo/Scripts/OfficialDemoAndroid.cs @@ -3,6 +3,8 @@ using System.Linq; using UnityEngine; +using Stopwatch = System.Diagnostics.Stopwatch; + public class OfficialDemoAndroid : DemoGraph { const string outputStream = "output_video"; @@ -26,6 +28,7 @@ public override void Initialize() { sinkNode.InputSidePacket.Add($"DESTINATION:{destinationBufferName}"); graph = new CalculatorGraph(calculatorGraphConfig); + stopwatch = new Stopwatch(); } public override Status StartRun() { @@ -45,6 +48,7 @@ public override Status StartRun(Texture texture) { gpuHelper.RunInGlContext(BuildDestination).AssertOk(); sidePacket.Emplace(destinationBufferName, outputPacket); + stopwatch.Start(); return graph.StartRun(sidePacket); } diff --git a/Assets/MediaPipe/Examples/Graphs/OfficialDemo/Scripts/OfficialDemoIOS.cs b/Assets/MediaPipe/Examples/Graphs/OfficialDemo/Scripts/OfficialDemoIOS.cs index 3d43806ef..715fdd034 100644 --- a/Assets/MediaPipe/Examples/Graphs/OfficialDemo/Scripts/OfficialDemoIOS.cs +++ b/Assets/MediaPipe/Examples/Graphs/OfficialDemo/Scripts/OfficialDemoIOS.cs @@ -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(outputStream).ConsumeValueOrDie(); diff --git a/Assets/MediaPipe/Examples/Scripts/DemoGraph.cs b/Assets/MediaPipe/Examples/Scripts/DemoGraph.cs index a9337de5a..af8664671 100644 --- a/Assets/MediaPipe/Examples/Scripts/DemoGraph.cs +++ b/Assets/MediaPipe/Examples/Scripts/DemoGraph.cs @@ -3,11 +3,13 @@ using System.Collections.Generic; using UnityEngine; +using Stopwatch = System.Diagnostics.Stopwatch; + public abstract class DemoGraph : MonoBehaviour, IDemoGraph { [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; @@ -20,6 +22,10 @@ public abstract class DemoGraph : MonoBehaviour, IDemoGraph { protected virtual void OnDestroy() { Stop(); + + if (stopwatch != null && stopwatch.IsRunning) { + stopwatch.Stop(); + } } public virtual void Initialize() { @@ -28,7 +34,7 @@ public virtual void Initialize() { } graph = new CalculatorGraph(config.text); - timestampValue = System.Environment.TickCount & System.Int32.MaxValue; + stopwatch = new Stopwatch(); } public void Initialize(GpuResources gpuResources, GlCalculatorHelper gpuHelper) { @@ -40,11 +46,12 @@ public void Initialize(GpuResources gpuResources, GlCalculatorHelper gpuHelper) 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( @@ -87,13 +94,18 @@ static IntPtr PushInputInGlContext() { 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(); } } @@ -130,4 +142,13 @@ public List FetchNextVector(OutputStreamPoller> poller, Packet