Skip to content

Commit

Permalink
feat: implement Packet.CreateFloat, Packet.GetFloat (#1070)
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Dec 17, 2023
1 parent 36ba644 commit 9a042b2
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,29 @@ public static Packet CreateDoubleAt(double value, long timestampMicrosec)
return new Packet(ptr, true);
}

/// <summary>
/// Create a float Packet.
/// </summary>
public static Packet CreateFloat(float value)
{
UnsafeNativeMethods.mp__MakeFloatPacket__f(value, out var ptr).Assert();

return new Packet(ptr, true);
}

/// <summary>
/// Create a float Packet.
/// </summary>
/// <param name="timestampMicrosec">
/// The timestamp of the packet.
/// </param>
public static Packet CreateFloatAt(float value, long timestampMicrosec)
{
UnsafeNativeMethods.mp__MakeFloatPacket_At__f_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>
Expand Down Expand Up @@ -179,6 +202,23 @@ public double GetDouble()
return value;
}

/// <summary>
/// Get the content of the <see cref="Packet"/> as a float.
/// </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 float data.
/// </exception>
public float GetFloat()
{
UnsafeNativeMethods.mp_Packet__GetFloat(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 @@ -220,5 +260,19 @@ public void ValidateAsDouble()
GC.KeepAlive(this);
AssertStatusOk(statusPtr);
}

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

GC.KeepAlive(this);
AssertStatusOk(statusPtr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ internal static partial class UnsafeNativeMethods
[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeFloatPacket_At__f_Rt(float value, IntPtr timestamp, out IntPtr packet);

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,35 @@ public void CreateDoubleAt_ShouldReturnNewDoublePacket(double value)
}
#endregion

#region Float
[TestCase(float.MaxValue)]
[TestCase(0f)]
[TestCase(float.MinValue)]
public void CreateFloat_ShouldReturnNewFloatPacket(float value)
{
using var packet = Packet.CreateFloat(value);

Assert.DoesNotThrow(packet.ValidateAsFloat);
Assert.AreEqual(value, packet.GetFloat());

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

[TestCase(float.MaxValue)]
[TestCase(0f)]
[TestCase(float.MinValue)]
public void CreateFloatAt_ShouldReturnNewFloatPacket(float value)
{
var timestamp = 1;
using var packet = Packet.CreateFloatAt(value, timestamp);

Assert.DoesNotThrow(packet.ValidateAsFloat);
Assert.AreEqual(value, packet.GetFloat());
Assert.AreEqual(timestamp, packet.TimestampMicroseconds());
}
#endregion

#region #Validate
[Test]
public void ValidateAsBool_ShouldThrow_When_ValueIsNotSet()
Expand All @@ -126,6 +155,14 @@ public void ValidateAsDouble_ShouldThrow_When_ValueIsNotSet()
using var packet = Packet.CreateEmpty();
_ = Assert.Throws<BadStatusException>(packet.ValidateAsDouble);
}


[Test]
public void ValidateAsFloat_ShouldThrow_When_ValueIsNotSet()
{
using var packet = Packet.CreateEmpty();
_ = Assert.Throws<BadStatusException>(packet.ValidateAsFloat);
}
#endregion
}
}
7 changes: 7 additions & 0 deletions mediapipe_api/framework/packet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ MpReturnCode mp__MakeFloatPacket_At__f_Rt(float value, mediapipe::Timestamp* tim
CATCH_EXCEPTION
}

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

MpReturnCode mp_Packet__GetFloat(mediapipe::Packet* packet, float* value_out) {
TRY_ALL
*value_out = packet->Get<float>();
Expand Down
1 change: 1 addition & 0 deletions mediapipe_api/framework/packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ MP_CAPI(MpReturnCode) mp_Packet__ValidateAsDouble(mediapipe::Packet* packet, abs
// 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);
MP_CAPI(MpReturnCode) mp__MakeFloatPacket_At__f_ll(float value, int64 timestampMicrosec, mediapipe::Packet** packet_out);
MP_CAPI(MpReturnCode) mp_Packet__GetFloat(mediapipe::Packet* packet, float* value_out);
MP_CAPI(MpReturnCode) mp_Packet__ValidateAsFloat(mediapipe::Packet* packet, absl::Status** status_out);

Expand Down

0 comments on commit 9a042b2

Please sign in to comment.