Skip to content

Commit

Permalink
feat(sdk): implement FloatArrayPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Feb 21, 2021
1 parent 20083ff commit 9a0780c
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Assets/MediaPipe/SDK/Scripts/Framework/Packet/FloatArrayPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;

namespace Mediapipe {
public class FloatArrayPacket : Packet<float[]> {
int _Length = -1;

public int Length {
get { return _Length; }
set {
if (_Length >= 0) {
throw new InvalidOperationException("Length is already set and cannot be changed");
}

_Length = value;
}
}

public FloatArrayPacket() : base() {}

public FloatArrayPacket(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) {}

public FloatArrayPacket(float[] value) : base() {
UnsafeNativeMethods.mp__MakeFloatArrayPacket__Pf_i(value, value.Length, out var ptr).Assert();
this.ptr = ptr;
Length = value.Length;
}

public FloatArrayPacket(float[] value, Timestamp timestamp) : base() {
UnsafeNativeMethods.mp__MakeFloatArrayPacket_At__Pf_i_Rtimestamp(value, value.Length, timestamp.mpPtr, out var ptr).Assert();
GC.KeepAlive(timestamp);
this.ptr = ptr;
Length = value.Length;
}

public override float[] Get() {
if (Length < 0) {
throw new InvalidOperationException("The array's length is unknown, set Length first");
}

var result = new float[Length];

unsafe {
float* src = (float*)GetArrayPtr();

for (var i = 0; i < result.Length; i++) {
result[i] = *src++;
}
}

return result;
}

public IntPtr GetArrayPtr() {
UnsafeNativeMethods.mp_Packet__GetFloatArray(mpPtr, out var value).Assert();
GC.KeepAlive(this);
return value;
}

public override StatusOr<float[]> Consume() {
throw new NotSupportedException();
}

public override Status ValidateAsType() {
UnsafeNativeMethods.mp_Packet__ValidateAsFloatArray(mpPtr, out var statusPtr).Assert();

GC.KeepAlive(this);
return new Status(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 @@ -71,6 +71,20 @@ internal static partial class UnsafeNativeMethods {
public static extern MpReturnCode mp_Packet__ValidateAsInt(IntPtr packet, out IntPtr status);
#endregion

#region FloatArray
[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeFloatArrayPacket__Pf_i(float[] value, int size, out IntPtr packet);

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

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

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

#region String
[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeStringPacket__PKc(string value, out IntPtr packet);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using Mediapipe;
using NUnit.Framework;
using System;

namespace Tests {
public class FloatArrayPacketTest {
#region Constructor
[Test, SignalAbort]
public void Ctor_ShouldInstantiatePacket_When_CalledWithNoArguments() {
var packet = new FloatArrayPacket();
packet.Length = 0;

Assert.AreEqual(packet.ValidateAsType().code, Status.StatusCode.Internal);
Assert.Throws<MediaPipeException>(() => { packet.Get(); });
Assert.AreEqual(packet.Timestamp(), Timestamp.Unset());
}

[Test]
public void Ctor_ShouldInstantiatePacket_When_CalledWithEmptyArray() {
float[] array = {};
var packet = new FloatArrayPacket(array);

Assert.True(packet.ValidateAsType().ok);
Assert.AreEqual(packet.Get(), array);
Assert.AreEqual(packet.Timestamp(), Timestamp.Unset());
}

[Test]
public void Ctor_ShouldInstantiatePacket_When_CalledWithArray() {
float[] array = { 0.01f };
var packet = new FloatArrayPacket(array);

Assert.True(packet.ValidateAsType().ok);
Assert.AreEqual(packet.Get(), array);
Assert.AreEqual(packet.Timestamp(), Timestamp.Unset());
}

[Test]
public void Ctor_ShouldInstantiatePacket_When_CalledWithValueAndTimestamp() {
var timestamp = new Timestamp(1);
float[] array = { 0.01f, 0.02f };
var packet = new FloatArrayPacket(array, timestamp);

Assert.True(packet.ValidateAsType().ok);
Assert.AreEqual(packet.Get(), array);
Assert.AreEqual(packet.Timestamp(), timestamp);
}
#endregion

#region #isDisposed
[Test]
public void isDisposed_ShouldReturnFalse_When_NotDisposedYet() {
var packet = new FloatArrayPacket();

Assert.False(packet.isDisposed);
}

[Test]
public void isDisposed_ShouldReturnTrue_When_AlreadyDisposed() {
var packet = new FloatArrayPacket();
packet.Dispose();

Assert.True(packet.isDisposed);
}
#endregion

#region #Consume
[Test]
public void Consume_ShouldThrowNotSupportedException() {
var packet = new FloatArrayPacket();

Assert.Throws<NotSupportedException>(() => { packet.Consume(); });
}
#endregion

#region #DebugTypeName
[Test]
public void DebugTypeName_ShouldReturnFloat_When_ValueIsSet() {
float[] array = { 0.01f };
var packet = new FloatArrayPacket(array);

Assert.AreEqual(packet.DebugTypeName(), "float []");
}
#endregion
}
}

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

36 changes: 36 additions & 0 deletions C/mediapipe_api/framework/packet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,42 @@ MpReturnCode mp_Packet__ValidateAsInt(mediapipe::Packet* packet, mediapipe::Stat
} CATCH_EXCEPTION
}

// FloatArrayPacket
MpReturnCode mp__MakeFloatArrayPacket__Pf_i(float* value, int size, mediapipe::Packet** packet_out) {
TRY {
float* array = new float[size];
std::memcpy(array, value, size * sizeof(float));
*packet_out = new mediapipe::Packet { mediapipe::Adopt(reinterpret_cast<float(*)[]>(array)) };
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

MpReturnCode mp__MakeFloatArrayPacket_At__Pf_i_Rtimestamp(float* value,
int size,
mediapipe::Timestamp* timestamp,
mediapipe::Packet** packet_out) {
TRY {
float* array = new float[size];
std::memcpy(array, value, size * sizeof(float));
*packet_out = new mediapipe::Packet { mediapipe::Adopt(reinterpret_cast<float(*)[]>(array)).At(*timestamp) };
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

MpReturnCode mp_Packet__GetFloatArray(mediapipe::Packet* packet, const float** value_out) {
TRY_ALL {
*value_out = packet->Get<float[]>();
RETURN_CODE(MpReturnCode::Success);
} CATCH_ALL
}

MpReturnCode mp_Packet__ValidateAsFloatArray(mediapipe::Packet* packet, mediapipe::Status** status_out) {
TRY {
*status_out = new mediapipe::Status { packet->ValidateAsType<float[]>() };
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

// StringPacket
MpReturnCode mp__MakeStringPacket__PKc(const char* str, mediapipe::Packet** packet_out) {
TRY {
Expand Down
9 changes: 9 additions & 0 deletions C/mediapipe_api/framework/packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ MP_CAPI(MpReturnCode) mp__MakeIntPacket_At__i_Rtimestamp(int value, mediapipe::T
MP_CAPI(MpReturnCode) mp_Packet__GetInt(mediapipe::Packet* packet, int* value_out);
MP_CAPI(MpReturnCode) mp_Packet__ValidateAsInt(mediapipe::Packet* packet, mediapipe::Status** status_out);

// Float Array
MP_CAPI(MpReturnCode) mp__MakeFloatArrayPacket__Pf_i(float* value, int size, mediapipe::Packet** packet_out);
MP_CAPI(MpReturnCode) mp__MakeFloatArrayPacket_At__Pf_i_Rtimestamp(float* value,
int size,
mediapipe::Timestamp* timestamp,
mediapipe::Packet** packet_out);
MP_CAPI(MpReturnCode) mp_Packet__GetFloatArray(mediapipe::Packet* packet, const float** value_out);
MP_CAPI(MpReturnCode) mp_Packet__ValidateAsFloatArray(mediapipe::Packet* packet, mediapipe::Status** status_out);

// String
MP_CAPI(MpReturnCode) mp__MakeStringPacket__PKc(const char* str, mediapipe::Packet** packet_out);
MP_CAPI(MpReturnCode) mp__MakeStringPacket_At__PKc_Rtimestamp(const char* str,
Expand Down

0 comments on commit 9a0780c

Please sign in to comment.