Skip to content

Commit

Permalink
feat: implement Packet.CreateDouble, Packet.GetDouble (#1069)
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Dec 17, 2023
1 parent 7441b9c commit 36ba644
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal static Packet CreateEmpty()
public static Packet CreateForReference(IntPtr ptr) => new Packet(ptr, false);

/// <summary>
/// Create a bool Packet from a boolean.
/// Create a bool Packet.
/// </summary>
public static Packet CreateBool(bool value)
{
Expand Down Expand Up @@ -90,11 +90,34 @@ public static Packet CreateBoolVectorAt(bool[] value, long timestampMicrosec)
return new Packet(ptr, true);
}

/// <summary>
/// Create a double Packet.
/// </summary>
public static Packet CreateDouble(double value)
{
UnsafeNativeMethods.mp__MakeDoublePacket__d(value, out var ptr).Assert();

return new Packet(ptr, true);
}

/// <summary>
/// Create a double Packet.
/// </summary>
/// <param name="timestampMicrosec">
/// The timestamp of the packet.
/// </param>
public static Packet CreateDoubleAt(double value, long timestampMicrosec)
{
UnsafeNativeMethods.mp__MakeDoublePacket_At__d_ll(value, timestampMicrosec, out var ptr).Assert();

return new Packet(ptr, true);
}

/// <summary>
/// Get the content of the <see cref="Packet"/> as a boolean.
/// </summary>
/// <remarks>
/// On some platforms (e.g. Windows), it will abort the process when <see cref="MediaPipeException"/> should be thrown.
/// On some platforms (e.g. Windows), it will abort the process when <see cref="MediaPipeException"/> should be thrown.
/// </remarks>
/// <exception cref="MediaPipeException">
/// If the <see cref="Packet"/> doesn't contain bool data.
Expand All @@ -121,9 +144,15 @@ public List<bool> GetBoolList()
/// <summary>
/// Get the content of a bool vector Packet as a <see cref="List{bool}"/>.
/// </summary>
/// <remarks>
/// On some platforms (e.g. Windows), it will abort the process when <see cref="MediaPipeException"/> should be thrown.
/// </remarks>
/// <param name="value">
/// The <see cref="List{bool}"/> to be filled with the content of the <see cref="Packet"/>.
/// </param>
/// <exception cref="MediaPipeException">
/// If the <see cref="Packet"/> doesn't contain std::vector&lt;bool&gt; data.
/// </exception>
public void GetBoolList(List<bool> value)
{
UnsafeNativeMethods.mp_Packet__GetBoolVector(mpPtr, out var structArray).Assert();
Expand All @@ -133,6 +162,23 @@ public void GetBoolList(List<bool> value)
structArray.Dispose();
}

/// <summary>
/// Get the content of the <see cref="Packet"/> as a double.
/// </summary>
/// <remarks>
/// On some platforms (e.g. Windows), it will abort the process when <see cref="MediaPipeException"/> should be thrown.
/// </remarks>
/// <exception cref="MediaPipeException">
/// If the <see cref="Packet"/> doesn't contain double data.
/// </exception>
public double GetDouble()
{
UnsafeNativeMethods.mp_Packet__GetDouble(mpPtr, out var value).Assert();

GC.KeepAlive(this);
return value;
}

/// <summary>
/// Validate if the content of the <see cref="Packet"/> is a boolean.
/// </summary>
Expand Down Expand Up @@ -160,5 +206,19 @@ public void ValidateAsBoolVector()
GC.KeepAlive(this);
AssertStatusOk(statusPtr);
}

/// <summary>
/// Validate if the content of the <see cref="Packet"/> is a double.
/// </summary>
/// <exception cref="BadStatusException">
/// If the <see cref="Packet"/> doesn't contain double data;.
/// </exception>
public void ValidateAsDouble()
{
UnsafeNativeMethods.mp_Packet__ValidateAsDouble(mpPtr, out var statusPtr).Assert();

GC.KeepAlive(this);
AssertStatusOk(statusPtr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ internal static partial class UnsafeNativeMethods
public static extern MpReturnCode mp_Packet__ValidateAsBoolVector(IntPtr packet, out IntPtr status);
#endregion

#region Double
[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeDoublePacket__d(double value, out IntPtr packet);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeDoublePacket_At__d_ll(double value, long timestampMicrosec, out IntPtr packet);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__GetDouble(IntPtr packet, out double value);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__ValidateAsDouble(IntPtr packet, out IntPtr status);
#endregion

#region Float
[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeFloatPacket__f(float value, out IntPtr packet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,35 @@ public void CreateBoolVectorAt_ShouldReturnNewBoolVectorPacket()
}
#endregion

#region Double
[TestCase(double.MaxValue)]
[TestCase(0d)]
[TestCase(double.MinValue)]
public void CreateDouble_ShouldReturnNewDoublePacket(double value)
{
using var packet = Packet.CreateDouble(value);

Assert.DoesNotThrow(packet.ValidateAsDouble);
Assert.AreEqual(value, packet.GetDouble());

using var unsetTimestamp = Timestamp.Unset();
Assert.AreEqual(unsetTimestamp.Microseconds(), packet.TimestampMicroseconds());
}

[TestCase(double.MaxValue)]
[TestCase(0d)]
[TestCase(double.MinValue)]
public void CreateDoubleAt_ShouldReturnNewDoublePacket(double value)
{
var timestamp = 1;
using var packet = Packet.CreateDoubleAt(value, timestamp);

Assert.DoesNotThrow(packet.ValidateAsDouble);
Assert.AreEqual(value, packet.GetDouble());
Assert.AreEqual(timestamp, packet.TimestampMicroseconds());
}
#endregion

#region #Validate
[Test]
public void ValidateAsBool_ShouldThrow_When_ValueIsNotSet()
Expand All @@ -90,6 +119,13 @@ public void ValidateAsBoolVector_ShouldThrow_When_ValueIsNotSet()
using var packet = Packet.CreateEmpty();
_ = Assert.Throws<BadStatusException>(packet.ValidateAsBoolVector);
}

[Test]
public void ValidateAsDouble_ShouldThrow_When_ValueIsNotSet()
{
using var packet = Packet.CreateEmpty();
_ = Assert.Throws<BadStatusException>(packet.ValidateAsDouble);
}
#endregion
}
}
29 changes: 29 additions & 0 deletions mediapipe_api/framework/packet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,35 @@ MpReturnCode mp_Packet__ValidateAsBoolVector(mediapipe::Packet* packet, absl::St
CATCH_EXCEPTION
}

// DoublePacket
MpReturnCode mp__MakeDoublePacket__d(double value, mediapipe::Packet** packet_out) {
TRY
*packet_out = new mediapipe::Packet{mediapipe::MakePacket<double>(value)};
RETURN_CODE(MpReturnCode::Success);
CATCH_EXCEPTION
}

MpReturnCode mp__MakeDoublePacket_At__d_ll(double value, int64 timestampMicrosec, mediapipe::Packet** packet_out) {
TRY
*packet_out = new mediapipe::Packet{mediapipe::MakePacket<double>(value).At(mediapipe::Timestamp(timestampMicrosec))};
RETURN_CODE(MpReturnCode::Success);
CATCH_EXCEPTION
}

MpReturnCode mp_Packet__GetDouble(mediapipe::Packet* packet, double* value_out) {
TRY_ALL
*value_out = packet->Get<double>();
RETURN_CODE(MpReturnCode::Success);
CATCH_ALL
}

MpReturnCode mp_Packet__ValidateAsDouble(mediapipe::Packet* packet, absl::Status** status_out) {
TRY
*status_out = new absl::Status{packet->ValidateAsType<double>()};
RETURN_CODE(MpReturnCode::Success);
CATCH_EXCEPTION
}

// FloatPacket
MpReturnCode mp__MakeFloatPacket__f(float value, mediapipe::Packet** packet_out) {
TRY
Expand Down
6 changes: 6 additions & 0 deletions mediapipe_api/framework/packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ MP_CAPI(MpReturnCode) mp__MakeBoolVectorPacket_At__Pb_i_ll(bool* value, int size
MP_CAPI(MpReturnCode) mp_Packet__GetBoolVector(mediapipe::Packet* packet, mp_api::StructArray<bool>* value_out);
MP_CAPI(MpReturnCode) mp_Packet__ValidateAsBoolVector(mediapipe::Packet* packet, absl::Status** status_out);

// double
MP_CAPI(MpReturnCode) mp__MakeDoublePacket__d(double value, mediapipe::Packet** packet_out);
MP_CAPI(MpReturnCode) mp__MakeDoublePacket_At__d_ll(double value, int64 timestampMicrosec, mediapipe::Packet** packet_out);
MP_CAPI(MpReturnCode) mp_Packet__GetDouble(mediapipe::Packet* packet, double* value_out);
MP_CAPI(MpReturnCode) mp_Packet__ValidateAsDouble(mediapipe::Packet* packet, absl::Status** status_out);

// float
MP_CAPI(MpReturnCode) mp__MakeFloatPacket__f(float value, mediapipe::Packet** packet_out);
MP_CAPI(MpReturnCode) mp__MakeFloatPacket_At__f_Rt(float value, mediapipe::Timestamp* timestamp, mediapipe::Packet** packet_out);
Expand Down

0 comments on commit 36ba644

Please sign in to comment.