diff --git a/Assets/MediaPipe/SDK/Scripts/External/Protobuf.cs b/Assets/MediaPipe/SDK/Scripts/External/Protobuf.cs
index ca050a881..0baaf6a02 100644
--- a/Assets/MediaPipe/SDK/Scripts/External/Protobuf.cs
+++ b/Assets/MediaPipe/SDK/Scripts/External/Protobuf.cs
@@ -11,18 +11,6 @@ static Protobuf() {
UnsafeNativeMethods.google_protobuf__SetLogHandler__PF(protobufLogHandler).Assert();
}
- /// Thrown when an error occured in unmanaged code
- /// Thrown when failed to parse
- public static CalculatorGraphConfig ParseFromStringAsCalculatorGraphConfig(string configText) {
- UnsafeNativeMethods.google_protobuf_TextFormat__ParseFromStringAsCalculatorGraphConfig__PKc(configText, out var configPtr).Assert();
-
- if (configPtr == IntPtr.Zero) {
- throw new FormatException("Failed to parse the text as CalculatorGraphConfig");
- }
-
- return new CalculatorGraphConfig(configPtr);
- }
-
public static T DeserializeProto(IntPtr ptr, pb::MessageParser parser) where T : pb::IMessage {
var serializedProto = Marshal.PtrToStructure(ptr);
var bytes = new byte[serializedProto.length];
diff --git a/Assets/MediaPipe/SDK/Scripts/Framework/CalculatorGraph.cs b/Assets/MediaPipe/SDK/Scripts/Framework/CalculatorGraph.cs
index adb4a7a8f..0b2145baa 100644
--- a/Assets/MediaPipe/SDK/Scripts/Framework/CalculatorGraph.cs
+++ b/Assets/MediaPipe/SDK/Scripts/Framework/CalculatorGraph.cs
@@ -1,6 +1,8 @@
using System;
using System.Runtime.InteropServices;
+using Google.Protobuf;
+
namespace Mediapipe {
public class CalculatorGraph : MpResourceHandle {
public delegate IntPtr NativePacketCallback(IntPtr packetPtr);
@@ -11,10 +13,18 @@ public CalculatorGraph() : base() {
this.ptr = ptr;
}
- public CalculatorGraph(string configText) : base() {
- var config = CalculatorGraphConfig.ParseFromString(configText);
- UnsafeNativeMethods.mp_CalculatorGraph__Rconfig(config.mpPtr, out var ptr).Assert();
- GC.KeepAlive(config);
+ /// text-formatted config
+ ///
+ /// By calling this constructor, the config text is parsed and serialized to binary string in C++,
+ /// parsed and serialized again in C#, and finally the serialized config is passed to C++ (so it is not efficient).
+ /// If possible, it would be better to build CalculatorGraphConfig from JSON in C# and pass it to another constructor.
+ ///
+ public CalculatorGraph(string textFormatConfig) : this(CalculatorGraphConfig.Parser.ParseFromTextFormat(textFormatConfig)) {}
+
+ public CalculatorGraph(CalculatorGraphConfig config) : base() {
+ var bytes = config.ToByteArray();
+
+ UnsafeNativeMethods.mp_CalculatorGraph__Rcgc(bytes, bytes.Length, out var ptr).Assert();
this.ptr = ptr;
}
@@ -23,27 +33,30 @@ protected override void DeleteMpPtr() {
}
public Status Initialize(CalculatorGraphConfig config) {
- UnsafeNativeMethods.mp_CalculatorGraph__Initialize__Rconfig(mpPtr, config.mpPtr, out var statusPtr).Assert();
+ var bytes = config.ToByteArray();
+ UnsafeNativeMethods.mp_CalculatorGraph__Initialize__Rcgc(mpPtr, bytes, bytes.Length, out var statusPtr).Assert();
GC.KeepAlive(this);
return new Status(statusPtr);
}
public Status Initialize(CalculatorGraphConfig config, SidePacket sidePacket) {
- UnsafeNativeMethods.mp_CalculatorGraph__Initialize__Rconfig_Rsp(mpPtr, config.mpPtr, sidePacket.mpPtr, out var statusPtr).Assert();
+ var bytes = config.ToByteArray();
+ UnsafeNativeMethods.mp_CalculatorGraph__Initialize__Rcgc_Rsp(mpPtr, bytes, bytes.Length, sidePacket.mpPtr, out var statusPtr).Assert();
GC.KeepAlive(this);
return new Status(statusPtr);
}
/// Crashes if config is not set
- public CalculatorGraphConfig config {
- get {
- UnsafeNativeMethods.mp_CalculatorGraph__Config(mpPtr, out var configPtr).Assert();
+ public CalculatorGraphConfig Config() {
+ UnsafeNativeMethods.mp_CalculatorGraph__Config(mpPtr, out var serializedProtoPtr).Assert();
+ GC.KeepAlive(this);
+
+ var config = Protobuf.DeserializeProto(serializedProtoPtr, CalculatorGraphConfig.Parser);
+ UnsafeNativeMethods.mp_api_SerializedProto__delete(serializedProtoPtr);
- GC.KeepAlive(this);
- return new CalculatorGraphConfig(configPtr);
- }
+ return config;
}
public Status ObserveOutputStream(string streamName, PacketCallback packetCallback, out GCHandle callbackHandle) where T : Packet {
diff --git a/Assets/MediaPipe/SDK/Scripts/Framework/CalculatorGraphConfig.cs b/Assets/MediaPipe/SDK/Scripts/Framework/CalculatorGraphConfig.cs
index dc5b3d0b7..d8d26780c 100644
--- a/Assets/MediaPipe/SDK/Scripts/Framework/CalculatorGraphConfig.cs
+++ b/Assets/MediaPipe/SDK/Scripts/Framework/CalculatorGraphConfig.cs
@@ -1,34 +1,15 @@
-using System;
-
+using pb = global::Google.Protobuf;
namespace Mediapipe {
- public class CalculatorGraphConfig : MpResourceHandle {
- public CalculatorGraphConfig(IntPtr ptr) : base(ptr) {}
-
- public static CalculatorGraphConfig ParseFromString(string configText) {
- return Protobuf.ParseFromStringAsCalculatorGraphConfig(configText);
- }
-
- protected override void DeleteMpPtr() {
- UnsafeNativeMethods.mp_CalculatorGraphConfig__delete(ptr);
- }
+ public static class CalculatorGraphConfigExtension {
+ ///
+ ///
+ public static CalculatorGraphConfig ParseFromTextFormat(this pb::MessageParser parser, string configText) {
+ UnsafeNativeMethods.mp_api__ConvertFromCalculatorGraphConfigTextFormat(configText, out var serializedProtoPtr).Assert();
- public int byteSizeLong {
- get { return UnsafeNativeMethods.mp_CalculatorGraphConfig__ByteSizeLong(mpPtr); }
- }
-
- /// Thrown when some required fields are not set
- public string SerializeAsString() {
- var str = MarshalStringFromNative(UnsafeNativeMethods.mp_CalculatorGraphConfig__SerializeAsString);
-
- if (str == null) {
- throw new InvalidOperationException("All the required fields must be set");
- }
-
- return str;
- }
+ var config = Protobuf.DeserializeProto(serializedProtoPtr, CalculatorGraphConfig.Parser);
+ UnsafeNativeMethods.mp_api_SerializedProto__delete(serializedProtoPtr);
- public string DebugString() {
- return MarshalStringFromNative(UnsafeNativeMethods.mp_CalculatorGraphConfig__DebugString);
+ return config;
}
}
}
diff --git a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Framework/CalculatorGraphConfig_Unsafe.cs b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Framework/CalculatorGraphConfig_Unsafe.cs
deleted file mode 100644
index 31886b7d8..000000000
--- a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Framework/CalculatorGraphConfig_Unsafe.cs
+++ /dev/null
@@ -1,21 +0,0 @@
-using System;
-using System.Runtime.InteropServices;
-
-namespace Mediapipe {
- internal static partial class UnsafeNativeMethods {
- [DllImport (MediaPipeLibrary, ExactSpelling = true)]
- public static extern MpReturnCode google_protobuf_TextFormat__ParseFromStringAsCalculatorGraphConfig__PKc(string configText, out IntPtr config);
-
- [DllImport (MediaPipeLibrary, ExactSpelling = true)]
- public static extern void mp_CalculatorGraphConfig__delete(IntPtr config);
-
- [DllImport (MediaPipeLibrary, ExactSpelling = true)]
- public static extern int mp_CalculatorGraphConfig__ByteSizeLong(IntPtr config);
-
- [DllImport (MediaPipeLibrary, ExactSpelling = true)]
- public static extern MpReturnCode mp_CalculatorGraphConfig__SerializeAsString(IntPtr config, out IntPtr str);
-
- [DllImport (MediaPipeLibrary, ExactSpelling = true)]
- public static extern MpReturnCode mp_CalculatorGraphConfig__DebugString(IntPtr config, out IntPtr str);
- }
-}
diff --git a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Framework/CalculatorGraph_Unsafe.cs b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Framework/CalculatorGraph_Unsafe.cs
index 3630b306f..a1ab3171b 100644
--- a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Framework/CalculatorGraph_Unsafe.cs
+++ b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Framework/CalculatorGraph_Unsafe.cs
@@ -7,20 +7,20 @@ internal static partial class UnsafeNativeMethods {
public static extern MpReturnCode mp_CalculatorGraph__(out IntPtr graph);
[DllImport (MediaPipeLibrary, ExactSpelling = true)]
- public static extern MpReturnCode mp_CalculatorGraph__Rconfig(IntPtr config, out IntPtr graph);
+ public static extern MpReturnCode mp_CalculatorGraph__Rcgc(byte[] serializedConfig, int size, out IntPtr graph);
[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern void mp_CalculatorGraph__delete(IntPtr graph);
[DllImport (MediaPipeLibrary, ExactSpelling = true)]
- public static extern MpReturnCode mp_CalculatorGraph__Initialize__Rconfig(IntPtr graph, IntPtr config, out IntPtr status);
+ public static extern MpReturnCode mp_CalculatorGraph__Initialize__Rcgc(IntPtr graph, byte[] serializedConfig, int size, out IntPtr status);
[DllImport (MediaPipeLibrary, ExactSpelling = true)]
- public static extern MpReturnCode mp_CalculatorGraph__Initialize__Rconfig_Rsp(
- IntPtr graph, IntPtr config, IntPtr sidePackets, out IntPtr status);
+ public static extern MpReturnCode mp_CalculatorGraph__Initialize__Rcgc_Rsp(
+ IntPtr graph, byte[] serializedConfig, int size, IntPtr sidePackets, out IntPtr status);
[DllImport (MediaPipeLibrary, ExactSpelling = true)]
- public static extern MpReturnCode mp_CalculatorGraph__Config(IntPtr graph, out IntPtr config);
+ public static extern MpReturnCode mp_CalculatorGraph__Config(IntPtr graph, out IntPtr serializedProto);
[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_CalculatorGraph__ObserveOutputStream__PKc_PF(IntPtr graph, string streamName,
diff --git a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Framework/Calculator_Unsafe.cs b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Framework/Calculator_Unsafe.cs
new file mode 100644
index 000000000..e8f7b5567
--- /dev/null
+++ b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Framework/Calculator_Unsafe.cs
@@ -0,0 +1,9 @@
+using System;
+using System.Runtime.InteropServices;
+
+namespace Mediapipe {
+ internal static partial class UnsafeNativeMethods {
+ [DllImport (MediaPipeLibrary, ExactSpelling = true)]
+ public static extern MpReturnCode mp_api__ConvertFromCalculatorGraphConfigTextFormat(string configText, out IntPtr serializedProto);
+ }
+}
diff --git a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Framework/CalculatorGraphConfig_Unsafe.cs.meta b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Framework/Calculator_Unsafe.cs.meta
similarity index 83%
rename from Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Framework/CalculatorGraphConfig_Unsafe.cs.meta
rename to Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Framework/Calculator_Unsafe.cs.meta
index af7ff2cbf..4e6fecd92 100644
--- a/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Framework/CalculatorGraphConfig_Unsafe.cs.meta
+++ b/Assets/MediaPipe/SDK/Scripts/PInvoke/NativeMethods/Framework/Calculator_Unsafe.cs.meta
@@ -1,5 +1,5 @@
fileFormatVersion: 2
-guid: d5e6779da314ccdffa48a6faeae6c930
+guid: 3c7f419d9297b69f7bd4259432b3a846
MonoImporter:
externalObjects: {}
serializedVersion: 2
diff --git a/Assets/MediaPipe/SDK/Tests/EditMode/Framework/CalculatorGraphConfigTest.cs.meta b/Assets/MediaPipe/SDK/Scripts/Protobuf/Calculator.cs.meta
similarity index 83%
rename from Assets/MediaPipe/SDK/Tests/EditMode/Framework/CalculatorGraphConfigTest.cs.meta
rename to Assets/MediaPipe/SDK/Scripts/Protobuf/Calculator.cs.meta
index 9883771a0..b4a8484a9 100644
--- a/Assets/MediaPipe/SDK/Tests/EditMode/Framework/CalculatorGraphConfigTest.cs.meta
+++ b/Assets/MediaPipe/SDK/Scripts/Protobuf/Calculator.cs.meta
@@ -1,5 +1,5 @@
fileFormatVersion: 2
-guid: df84d70ff3ca111b78113acb388bc3f2
+guid: edc92de05b2aeaa7fb02a1f48950921b
MonoImporter:
externalObjects: {}
serializedVersion: 2
diff --git a/Assets/MediaPipe/SDK/Scripts/Protobuf/CalculatorOptions.cs.meta b/Assets/MediaPipe/SDK/Scripts/Protobuf/CalculatorOptions.cs.meta
new file mode 100644
index 000000000..13af0df72
--- /dev/null
+++ b/Assets/MediaPipe/SDK/Scripts/Protobuf/CalculatorOptions.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 241902159e9673a34bb1eddf1bac3f14
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Assets/MediaPipe/SDK/Scripts/Protobuf/MediapipeOptions.cs.meta b/Assets/MediaPipe/SDK/Scripts/Protobuf/MediapipeOptions.cs.meta
new file mode 100644
index 000000000..0c08d4c4b
--- /dev/null
+++ b/Assets/MediaPipe/SDK/Scripts/Protobuf/MediapipeOptions.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 337271b467f7a3f7c83e86296b50d26c
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Assets/MediaPipe/SDK/Scripts/Protobuf/PacketFactory.cs.meta b/Assets/MediaPipe/SDK/Scripts/Protobuf/PacketFactory.cs.meta
new file mode 100644
index 000000000..da61fef8d
--- /dev/null
+++ b/Assets/MediaPipe/SDK/Scripts/Protobuf/PacketFactory.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: c49ec041502b5f594bc1ff898dbe6874
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Assets/MediaPipe/SDK/Scripts/Protobuf/PacketGenerator.cs.meta b/Assets/MediaPipe/SDK/Scripts/Protobuf/PacketGenerator.cs.meta
new file mode 100644
index 000000000..5f31df3cd
--- /dev/null
+++ b/Assets/MediaPipe/SDK/Scripts/Protobuf/PacketGenerator.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 7505da3ce11608a6093315b4b9cb58fb
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Assets/MediaPipe/SDK/Scripts/Protobuf/StatusHandler.cs.meta b/Assets/MediaPipe/SDK/Scripts/Protobuf/StatusHandler.cs.meta
new file mode 100644
index 000000000..62c1db9c4
--- /dev/null
+++ b/Assets/MediaPipe/SDK/Scripts/Protobuf/StatusHandler.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: c39644006f7358a459ade6e7a45161b7
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Assets/MediaPipe/SDK/Scripts/Protobuf/StreamHandler.cs.meta b/Assets/MediaPipe/SDK/Scripts/Protobuf/StreamHandler.cs.meta
new file mode 100644
index 000000000..9b865f83d
--- /dev/null
+++ b/Assets/MediaPipe/SDK/Scripts/Protobuf/StreamHandler.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: cd1234b5f11206ad7b425fce6ad1643c
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Assets/MediaPipe/SDK/Tests/EditMode/Framework/CalculatorGraphConfigTest.cs b/Assets/MediaPipe/SDK/Tests/EditMode/Framework/CalculatorGraphConfigTest.cs
deleted file mode 100644
index 3bf92dde5..000000000
--- a/Assets/MediaPipe/SDK/Tests/EditMode/Framework/CalculatorGraphConfigTest.cs
+++ /dev/null
@@ -1,61 +0,0 @@
-using Mediapipe;
-using NUnit.Framework;
-
-namespace Tests {
- public class CalculatorGraphConfigTest {
- private static string validConfigText = @"node {
- calculator: ""PassThroughCalculator""
- input_stream: ""in""
- output_stream: ""out1""
-}
-node {
- calculator: ""PassThroughCalculator""
- input_stream: ""out1""
- output_stream: ""out""
-}
-input_stream: ""in""
-output_stream: ""out""
-";
-
- private static string invalidConfigText = "Invalid";
-
- #region .ParseFromString
- [Test]
- public void ParseFromString_ShouldReturnCalculatorGraphConfig_When_ConfigIsValid() {
- var config = CalculatorGraphConfig.ParseFromString(validConfigText);
-
- Assert.AreEqual(config.DebugString(), validConfigText);
- }
-
- public void ParseFromString_ShouldThrowFormatException_When_ConfigIsInvalid() {
- Assert.Throws(() => { CalculatorGraphConfig.ParseFromString(invalidConfigText); });
- }
- #endregion
-
- #region #isDisposed
- [Test]
- public void isDisposed_ShouldReturnFalse_When_NotDisposedYet() {
- var config = CalculatorGraphConfig.ParseFromString(validConfigText);
-
- Assert.False(config.isDisposed);
- }
-
- [Test]
- public void isDisposed_ShouldReturnTrue_When_AlreadyDisposed() {
- var config = CalculatorGraphConfig.ParseFromString(validConfigText);
- config.Dispose();
-
- Assert.True(config.isDisposed);
- }
- #endregion
-
- #region #SerializeAsString
- [Test]
- public void SerializeAsString_ShouldReturnString() {
- var config = CalculatorGraphConfig.ParseFromString(validConfigText);
-
- Assert.AreEqual(config.SerializeAsString().Length, config.byteSizeLong);
- }
- #endregion
- }
-}
diff --git a/Assets/MediaPipe/SDK/Tests/EditMode/Framework/CalculatorGraphTest.cs b/Assets/MediaPipe/SDK/Tests/EditMode/Framework/CalculatorGraphTest.cs
index f21252d26..1c68e0ed8 100644
--- a/Assets/MediaPipe/SDK/Tests/EditMode/Framework/CalculatorGraphTest.cs
+++ b/Assets/MediaPipe/SDK/Tests/EditMode/Framework/CalculatorGraphTest.cs
@@ -26,9 +26,10 @@ public void Ctor_ShouldInstantiateCalculatorGraph_When_CalledWithNoArguments() {
[Test]
public void Ctor_ShouldInstantiateCalculatorGraph_When_CalledWithConfigText() {
var graph = new CalculatorGraph(validConfigText);
- var config = graph.config;
+ var config = graph.Config();
- Assert.Greater(config.byteSizeLong, 0);
+ Assert.AreEqual(config.InputStream[0], "in");
+ Assert.AreEqual(config.OutputStream[0], "out");
}
#endregion
@@ -53,18 +54,18 @@ public void isDisposed_ShouldReturnTrue_When_AlreadyDisposed() {
[Test]
public void Initialize_ShouldReturnOk_When_CalledWithConfig_And_ConfigIsNotSet() {
var graph = new CalculatorGraph();
- var config = CalculatorGraphConfig.ParseFromString(validConfigText);
- var status = graph.Initialize(config);
-
+ var status = graph.Initialize(CalculatorGraphConfig.Parser.ParseFromTextFormat(validConfigText));
Assert.True(status.ok);
- Assert.Greater(graph.config.byteSizeLong, 0);
+
+ var config = graph.Config();
+ Assert.AreEqual(config.InputStream[0], "in");
+ Assert.AreEqual(config.OutputStream[0], "out");
}
[Test]
public void Initialize_ShouldReturnInternalError_When_CalledWithConfig_And_ConfigIsSet() {
var graph = new CalculatorGraph(validConfigText);
- var config = CalculatorGraphConfig.ParseFromString(validConfigText);
- var status = graph.Initialize(config);
+ var status = graph.Initialize(CalculatorGraphConfig.Parser.ParseFromTextFormat(validConfigText));
Assert.AreEqual(status.code, Status.StatusCode.Internal);
}
@@ -72,7 +73,7 @@ public void Initialize_ShouldReturnInternalError_When_CalledWithConfig_And_Confi
[Test]
public void Initialize_ShouldReturnOk_When_CalledWithConfigAndSidePacket_And_ConfigIsNotSet() {
var graph = new CalculatorGraph();
- var config = CalculatorGraphConfig.ParseFromString(validConfigText);
+ var config = CalculatorGraphConfig.Parser.ParseFromTextFormat(validConfigText);
var sidePacket = new SidePacket();
sidePacket.Emplace("flag", new BoolPacket(true));
var status = graph.Initialize(config, sidePacket);
@@ -84,7 +85,7 @@ public void Initialize_ShouldReturnOk_When_CalledWithConfigAndSidePacket_And_Con
[Test]
public void Initialize_ShouldReturnInternalError_When_CalledWithConfigAndSidePacket_And_ConfigIsSet() {
var graph = new CalculatorGraph(validConfigText);
- var config = CalculatorGraphConfig.ParseFromString(validConfigText);
+ var config = CalculatorGraphConfig.Parser.ParseFromTextFormat(validConfigText);
var sidePacket = new SidePacket();
sidePacket.Emplace("flag", new BoolPacket(true));
var status = graph.Initialize(config, sidePacket);
diff --git a/Assets/MediaPipe/SDK/Tests/EditMode/Tests.asmdef b/Assets/MediaPipe/SDK/Tests/EditMode/Tests.asmdef
index eb32aeab9..eb93729b9 100644
--- a/Assets/MediaPipe/SDK/Tests/EditMode/Tests.asmdef
+++ b/Assets/MediaPipe/SDK/Tests/EditMode/Tests.asmdef
@@ -12,7 +12,8 @@
"allowUnsafeCode": true,
"overrideReferences": true,
"precompiledReferences": [
- "nunit.framework.dll"
+ "nunit.framework.dll",
+ "Google.Protobuf.dll"
],
"autoReferenced": false,
"defineConstraints": [
diff --git a/C/mediapipe_api/BUILD b/C/mediapipe_api/BUILD
index f85facd97..f661ac655 100644
--- a/C/mediapipe_api/BUILD
+++ b/C/mediapipe_api/BUILD
@@ -120,6 +120,13 @@ pkg_model(
pkg_zip(
name = "mediapipe_proto_srcs",
srcs = [
+ "//mediapipe_api/framework:calculator_cs",
+ "//mediapipe_api/framework:calculator_options_cs",
+ "//mediapipe_api/framework:mediapipe_options_cs",
+ "//mediapipe_api/framework:packet_factory_cs",
+ "//mediapipe_api/framework:packet_generator_cs",
+ "//mediapipe_api/framework:status_handler_cs",
+ "//mediapipe_api/framework:stream_handler_cs",
"//mediapipe_api/framework/formats:classification_cs",
"//mediapipe_api/framework/formats:detection_cs",
"//mediapipe_api/framework/formats:landmark_cs",
diff --git a/C/mediapipe_api/external/BUILD b/C/mediapipe_api/external/BUILD
index e850a06cd..5f2e61f62 100644
--- a/C/mediapipe_api/external/BUILD
+++ b/C/mediapipe_api/external/BUILD
@@ -16,6 +16,7 @@ cc_library(
deps = [
"//mediapipe_api:common",
"@com_google_protobuf//:protobuf",
+ "@com_google_mediapipe//mediapipe/framework/port:parse_text_proto",
],
visibility = ["//visibility:public"],
alwayslink = True,
diff --git a/C/mediapipe_api/external/protobuf.cc b/C/mediapipe_api/external/protobuf.cc
index 136db47d4..fc19be471 100644
--- a/C/mediapipe_api/external/protobuf.cc
+++ b/C/mediapipe_api/external/protobuf.cc
@@ -1,5 +1,7 @@
#include
#include
+
+#include "google/protobuf/stubs/logging.h"
#include "mediapipe_api/external/protobuf.h"
using google::protobuf::LogLevel;
diff --git a/C/mediapipe_api/external/protobuf.h b/C/mediapipe_api/external/protobuf.h
index 701e85a51..615510684 100644
--- a/C/mediapipe_api/external/protobuf.h
+++ b/C/mediapipe_api/external/protobuf.h
@@ -3,7 +3,7 @@
#include
#include "mediapipe_api/common.h"
-#include "google/protobuf/stubs/logging.h"
+#include "mediapipe/framework/port/parse_text_proto.h"
namespace mp_api {
@@ -41,7 +41,6 @@ inline struct mp_api::SerializedProto* SerializeProto(const T& proto) {
return new mp_api::SerializedProto { bytes, static_cast(length) };
}
-
template
inline struct mp_api::SerializedProtoVector* SerializeProtoVector(const std::vector& proto_vec) {
auto size = proto_vec.size();
@@ -54,6 +53,14 @@ inline struct mp_api::SerializedProtoVector* SerializeProtoVector(const std::vec
return new mp_api::SerializedProtoVector { data, static_cast(size) };
}
+template
+inline struct mp_api::SerializedProto* ConvertFromTextFormat(const char* str) {
+ T proto;
+ auto result = google::protobuf::TextFormat::ParseFromString(str, &proto);
+
+ return result ? SerializeProto(proto) : nullptr;
+}
+
extern "C" {
typedef void LogHandler(int level, const char* filename, int line, const char* message);
diff --git a/C/mediapipe_api/framework/BUILD b/C/mediapipe_api/framework/BUILD
index 780952346..73cb89f74 100644
--- a/C/mediapipe_api/framework/BUILD
+++ b/C/mediapipe_api/framework/BUILD
@@ -2,21 +2,17 @@ package(
default_visibility = ["//visibility:public"],
)
+load("//mediapipe_api:csharp_proto_src.bzl", "csharp_proto_src")
+
cc_library(
name = "calculator",
srcs = ["calculator.cc"],
hdrs = ["calculator.h"],
deps = [
"//mediapipe_api:common",
+ "//mediapipe_api/external:protobuf",
"@com_google_mediapipe//mediapipe/framework:calculator_framework",
- "@com_google_mediapipe//mediapipe/framework/port:parse_text_proto",
- ] + select({
- "@com_google_mediapipe//mediapipe/gpu:disable_gpu": [],
- "//conditions:default": [
- "//mediapipe_api/gpu:gl_calculator_helper",
- "//mediapipe_api/gpu:gpu_shared_data_internal",
- ],
- }),
+ ],
alwayslink = True,
)
@@ -74,3 +70,59 @@ cc_library(
],
alwayslink = True,
)
+
+csharp_proto_src(
+ name = "calculator_cs",
+ proto_src = "mediapipe/framework/calculator.proto",
+ deps = [
+ "@com_google_mediapipe//mediapipe/framework:protos_src",
+ ],
+)
+
+csharp_proto_src(
+ name = "calculator_options_cs",
+ proto_src = "mediapipe/framework/calculator_options.proto",
+ deps = [
+ "@com_google_mediapipe//mediapipe/framework:protos_src",
+ ],
+)
+
+csharp_proto_src(
+ name = "mediapipe_options_cs",
+ proto_src = "mediapipe/framework/mediapipe_options.proto",
+ deps = [
+ "@com_google_mediapipe//mediapipe/framework:protos_src",
+ ],
+)
+
+csharp_proto_src(
+ name = "packet_factory_cs",
+ proto_src = "mediapipe/framework/packet_factory.proto",
+ deps = [
+ "@com_google_mediapipe//mediapipe/framework:protos_src",
+ ],
+)
+
+csharp_proto_src(
+ name = "packet_generator_cs",
+ proto_src = "mediapipe/framework/packet_generator.proto",
+ deps = [
+ "@com_google_mediapipe//mediapipe/framework:protos_src",
+ ],
+)
+
+csharp_proto_src(
+ name = "status_handler_cs",
+ proto_src = "mediapipe/framework/status_handler.proto",
+ deps = [
+ "@com_google_mediapipe//mediapipe/framework:protos_src",
+ ],
+)
+
+csharp_proto_src(
+ name = "stream_handler_cs",
+ proto_src = "mediapipe/framework/stream_handler.proto",
+ deps = [
+ "@com_google_mediapipe//mediapipe/framework:protos_src",
+ ],
+)
diff --git a/C/mediapipe_api/framework/calculator.cc b/C/mediapipe_api/framework/calculator.cc
index 5c721c1dd..566ca0e53 100644
--- a/C/mediapipe_api/framework/calculator.cc
+++ b/C/mediapipe_api/framework/calculator.cc
@@ -1,42 +1,9 @@
#include "mediapipe_api/framework/calculator.h"
-#include "mediapipe/framework/port/parse_text_proto.h"
+#include "mediapipe/framework/calculator.pb.h"
-MpReturnCode google_protobuf_TextFormat__ParseFromStringAsCalculatorGraphConfig__PKc(const char* input,
- mediapipe::CalculatorGraphConfig** config_out) {
+MpReturnCode mp_api__ConvertFromCalculatorGraphConfigTextFormat(const char* config_text, mp_api::SerializedProto** value_out) {
TRY {
- auto config = new mediapipe::CalculatorGraphConfig {};
- auto result = google::protobuf::TextFormat::ParseFromString(input, config);
-
- *config_out = result ? config : nullptr;
- RETURN_CODE(MpReturnCode::Success);
- } CATCH_EXCEPTION
-}
-
-void mp_CalculatorGraphConfig__delete(mediapipe::CalculatorGraphConfig* config) {
- delete config;
-}
-
-int mp_CalculatorGraphConfig__ByteSizeLong(mediapipe::CalculatorGraphConfig* config) {
- return config->ByteSizeLong();
-}
-
-MpReturnCode mp_CalculatorGraphConfig__SerializeAsString(mediapipe::CalculatorGraphConfig* config, const char** str_out) {
- TRY {
- if (config->IsInitialized()) {
- auto size = config->ByteSizeLong();
- auto buffer = new char[size + 1];
- config->SerializeToArray(buffer, size);
- *str_out = buffer;
- } else {
- *str_out = nullptr;
- }
- RETURN_CODE(MpReturnCode::Success);
- } CATCH_EXCEPTION
-}
-
-MpReturnCode mp_CalculatorGraphConfig__DebugString(mediapipe::CalculatorGraphConfig* config, const char** str_out) {
- TRY {
- *str_out = strcpy_to_heap(config->DebugString());
+ *value_out = ConvertFromTextFormat(config_text);
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}
diff --git a/C/mediapipe_api/framework/calculator.h b/C/mediapipe_api/framework/calculator.h
index 4b5246cba..0ac99ece9 100644
--- a/C/mediapipe_api/framework/calculator.h
+++ b/C/mediapipe_api/framework/calculator.h
@@ -3,20 +3,12 @@
#include
#include
-#include "mediapipe/framework/calculator.pb.h"
#include "mediapipe_api/common.h"
+#include "mediapipe_api/external/protobuf.h"
extern "C" {
-MP_CAPI(MpReturnCode) google_protobuf_TextFormat__ParseFromStringAsCalculatorGraphConfig__PKc(
- const char* input,
- mediapipe::CalculatorGraphConfig** config_out);
-
-MP_CAPI(void) mp_CalculatorGraphConfig__delete(mediapipe::CalculatorGraphConfig* config);
-
-MP_CAPI(int) mp_CalculatorGraphConfig__ByteSizeLong(mediapipe::CalculatorGraphConfig* config);
-MP_CAPI(MpReturnCode) mp_CalculatorGraphConfig__SerializeAsString(mediapipe::CalculatorGraphConfig* config, const char** str_out);
-MP_CAPI(MpReturnCode) mp_CalculatorGraphConfig__DebugString(mediapipe::CalculatorGraphConfig* config, const char** str_out);
+MP_CAPI(MpReturnCode) mp_api__ConvertFromCalculatorGraphConfigTextFormat(const char* config_text, mp_api::SerializedProto** value_out);
} // extern "C"
diff --git a/C/mediapipe_api/framework/calculator_graph.cc b/C/mediapipe_api/framework/calculator_graph.cc
index 678139bff..0a16480fa 100644
--- a/C/mediapipe_api/framework/calculator_graph.cc
+++ b/C/mediapipe_api/framework/calculator_graph.cc
@@ -1,6 +1,13 @@
#include
#include "mediapipe_api/framework/calculator_graph.h"
+inline mediapipe::CalculatorGraphConfig ParseFromStringAsCalculatorGraphConfig(const char* serialized_config, int size) {
+ mediapipe::CalculatorGraphConfig config;
+ CHECK(config.ParseFromString(std::string(serialized_config, size)));
+
+ return config;
+}
+
MpReturnCode mp_CalculatorGraph__(mediapipe::CalculatorGraph** graph_out) {
TRY {
*graph_out = new mediapipe::CalculatorGraph();
@@ -12,36 +19,41 @@ void mp_CalculatorGraph__delete(mediapipe::CalculatorGraph* graph) {
delete graph;
}
-MpReturnCode mp_CalculatorGraph__Rconfig(mediapipe::CalculatorGraphConfig* config, mediapipe::CalculatorGraph** graph_out) {
+MpReturnCode mp_CalculatorGraph__Rcgc(const char* serialized_config, int size, mediapipe::CalculatorGraph** graph_out) {
TRY_ALL {
- *graph_out = new mediapipe::CalculatorGraph(*config);
+ LOG(INFO) << serialized_config;
+ auto config = ParseFromStringAsCalculatorGraphConfig(serialized_config, size);
+ *graph_out = new mediapipe::CalculatorGraph(config);
RETURN_CODE(MpReturnCode::Success);
} CATCH_ALL
}
-MpReturnCode mp_CalculatorGraph__Initialize__Rconfig(mediapipe::CalculatorGraph* graph,
- mediapipe::CalculatorGraphConfig* config,
- mediapipe::Status** status_out) {
+MpReturnCode mp_CalculatorGraph__Initialize__Rcgc(mediapipe::CalculatorGraph* graph,
+ const char* serialized_config,
+ int size,
+ mediapipe::Status** status_out) {
TRY_ALL {
- *status_out = new mediapipe::Status { graph->Initialize(*config) };
+ auto config = ParseFromStringAsCalculatorGraphConfig(serialized_config, size);
+ *status_out = new mediapipe::Status { graph->Initialize(config) };
RETURN_CODE(MpReturnCode::Success);
} CATCH_ALL
}
-MpReturnCode mp_CalculatorGraph__Initialize__Rconfig_Rsp(
- mediapipe::CalculatorGraph* graph,
- mediapipe::CalculatorGraphConfig* config,
- SidePackets* side_packets,
- mediapipe::Status** status_out) {
+MpReturnCode mp_CalculatorGraph__Initialize__Rcgc_Rsp(mediapipe::CalculatorGraph* graph,
+ const char* serialized_config,
+ int size,
+ SidePackets* side_packets,
+ mediapipe::Status** status_out) {
TRY_ALL {
- *status_out = new mediapipe::Status { graph->Initialize(*config, *side_packets) };
+ auto config = ParseFromStringAsCalculatorGraphConfig(serialized_config, size);
+ *status_out = new mediapipe::Status { graph->Initialize(config, *side_packets) };
RETURN_CODE(MpReturnCode::Success);
} CATCH_ALL
}
-MpReturnCode mp_CalculatorGraph__Config(mediapipe::CalculatorGraph* graph, mediapipe::CalculatorGraphConfig** config_out) {
+MpReturnCode mp_CalculatorGraph__Config(mediapipe::CalculatorGraph* graph, mp_api::SerializedProto** config_out) {
TRY_ALL {
- *config_out = new mediapipe::CalculatorGraphConfig { graph->Config() }; // Crashes if graph has no config
+ *config_out = SerializeProto(graph->Config());
RETURN_CODE(MpReturnCode::Success);
} CATCH_ALL
}
diff --git a/C/mediapipe_api/framework/calculator_graph.h b/C/mediapipe_api/framework/calculator_graph.h
index 3283ab4f2..7fac85558 100644
--- a/C/mediapipe_api/framework/calculator_graph.h
+++ b/C/mediapipe_api/framework/calculator_graph.h
@@ -4,6 +4,8 @@
#include