Skip to content

Commit

Permalink
feat: implement non-generic Packet for boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Dec 17, 2023
1 parent 169b004 commit 23e021f
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) 2023 homuler
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

using System;

namespace Mediapipe
{
public class Packet : MpResourceHandle
{
private Packet(IntPtr ptr, bool isOwner) : base(ptr, isOwner) { }

protected override void DeleteMpPtr()
{
UnsafeNativeMethods.mp_Packet__delete(ptr);
}

public long TimestampMicroseconds()
{
var value = SafeNativeMethods.mp_Packet__TimestampMicroseconds(mpPtr);
GC.KeepAlive(this);

return value;
}

internal static Packet CreateEmpty()
{
UnsafeNativeMethods.mp_Packet__(out var ptr).Assert();

return new Packet(ptr, true);
}

/// <summary>
/// Low-level API to reference the packet that <paramref name="ptr" /> points to.
/// </summary>
/// <remarks>
/// This method is to be used when you want to reference the packet whose lifetime is managed by native code.
/// </remarks>
/// <param name="ptr">
/// A pointer to a native Packet instance.
/// </param>
public static Packet CreateForReference(IntPtr ptr) => new Packet(ptr, false);

public static Packet CreateBool(bool value)
{
UnsafeNativeMethods.mp__MakeBoolPacket__b(value, out var ptr).Assert();

return new Packet(ptr, true);
}

public static Packet CreateBoolAt(bool value, long timestampMicrosec)
{
UnsafeNativeMethods.mp__MakeBoolPacket_At__b_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.
/// </remarks>
/// <exception cref="MediaPipeException">
/// If the <see cref="Packet"/> doesn't contain bool data.
/// </exception>
public bool GetBool()
{
UnsafeNativeMethods.mp_Packet__GetBool(mpPtr, out var value).Assert();

GC.KeepAlive(this);
return value;
}

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

GC.KeepAlive(this);
AssertStatusOk(statusPtr);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ internal static partial class SafeNativeMethods
[return: MarshalAs(UnmanagedType.I1)]
public static extern bool mp_Packet__IsEmpty(IntPtr packet);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern long mp_Packet__TimestampMicroseconds(IntPtr packet);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern void mp_PacketMap__clear(IntPtr packetMap);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ internal static partial class UnsafeNativeMethods
[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeBoolPacket_At__b_Rt([MarshalAs(UnmanagedType.I1)] bool value, IntPtr timestamp, out IntPtr packet);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeBoolPacket_At__b_ll([MarshalAs(UnmanagedType.I1)] bool value, long timestampMicrosec, out IntPtr packet);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__GetBool(IntPtr packet, [MarshalAs(UnmanagedType.I1)] out bool value);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) 2023 homuler
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

using NUnit.Framework;

namespace Mediapipe.Tests
{
public class PacketTest
{
#region Bool
[TestCase(true)]
[TestCase(false)]
public void CreateBool_ShouldReturnNewBoolPacket(bool value)
{
using var packet = Packet.CreateBool(value);

Assert.DoesNotThrow(packet.ValidateAsBool);
Assert.AreEqual(value, packet.GetBool());

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

[TestCase(true)]
[TestCase(false)]
public void CreateBoolAt_ShouldReturnNewBoolPacket(bool value)
{
var timestamp = 1;
using var packet = Packet.CreateBoolAt(value, timestamp);

Assert.DoesNotThrow(packet.ValidateAsBool);
Assert.AreEqual(value, packet.GetBool());
Assert.AreEqual(timestamp, packet.TimestampMicroseconds());
}
#endregion

#region #Validate
[Test]
public void ValidateAsBool_ShouldThrow_When_ValueIsNotSet()
{
using var packet = Packet.CreateEmpty();
_ = Assert.Throws<BadStatusException>(packet.ValidateAsBool);
}
#endregion
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions mediapipe_api/framework/packet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ MpReturnCode mp_Packet__Timestamp(mediapipe::Packet* packet, mediapipe::Timestam
CATCH_EXCEPTION
}

int64 mp_Packet__TimestampMicroseconds(mediapipe::Packet* packet) {
return packet->Timestamp().Microseconds();
}

MpReturnCode mp_Packet__DebugString(mediapipe::Packet* packet, const char** str_out) {
TRY
*str_out = strcpy_to_heap(packet->DebugString());
Expand Down Expand Up @@ -78,6 +82,13 @@ MpReturnCode mp__MakeBoolPacket_At__b_Rt(bool value, mediapipe::Timestamp* times
CATCH_EXCEPTION
}

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

MpReturnCode mp_Packet__GetBool(mediapipe::Packet* packet, bool* value_out) {
TRY_ALL
*value_out = packet->Get<bool>();
Expand Down
2 changes: 2 additions & 0 deletions mediapipe_api/framework/packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ MP_CAPI(MpReturnCode) mp_Packet__At__Rt(mediapipe::Packet* packet, mediapipe::Ti
MP_CAPI(bool) mp_Packet__IsEmpty(mediapipe::Packet* packet);
MP_CAPI(MpReturnCode) mp_Packet__ValidateAsProtoMessageLite(mediapipe::Packet* packet, absl::Status** status_out);
MP_CAPI(MpReturnCode) mp_Packet__Timestamp(mediapipe::Packet* packet, mediapipe::Timestamp** timestamp_out);
MP_CAPI(int64) mp_Packet__TimestampMicroseconds(mediapipe::Packet* packet);
MP_CAPI(MpReturnCode) mp_Packet__DebugString(mediapipe::Packet* packet, const char** str_out);
MP_CAPI(MpReturnCode) mp_Packet__RegisteredTypeName(mediapipe::Packet* packet, const char** str_out);
MP_CAPI(MpReturnCode) mp_Packet__DebugTypeName(mediapipe::Packet* packet, const char** str_out);

// bool
MP_CAPI(MpReturnCode) mp__MakeBoolPacket__b(bool value, mediapipe::Packet** packet_out);
MP_CAPI(MpReturnCode) mp__MakeBoolPacket_At__b_Rt(bool value, mediapipe::Timestamp* timestamp, mediapipe::Packet** packet_out);
MP_CAPI(MpReturnCode) mp__MakeBoolPacket_At__b_ll(bool value, int64 timestampMicrosec, mediapipe::Packet** packet_out);
MP_CAPI(MpReturnCode) mp_Packet__GetBool(mediapipe::Packet* packet, bool* value_out);
MP_CAPI(MpReturnCode) mp_Packet__ValidateAsBool(mediapipe::Packet* packet, absl::Status** status_out);

Expand Down

0 comments on commit 23e021f

Please sign in to comment.