Skip to content

Commit

Permalink
BoolPacket extends MpResourceHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Nov 3, 2020
1 parent f1d7b21 commit 32cab19
Show file tree
Hide file tree
Showing 23 changed files with 377 additions and 112 deletions.
21 changes: 15 additions & 6 deletions Assets/MediaPipe/SDK/Scripts/Framework/BoolPacket.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
using System;
using MpPacket = System.IntPtr;

namespace Mediapipe {
public class BoolPacket : Packet<bool> {
public BoolPacket() : base() {}

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

public BoolPacket(bool value) : base(UnsafeNativeMethods.MpMakeBoolPacket(value)) {}
public BoolPacket(bool value) : base() {
UnsafeNativeMethods.mp__MakeBoolPacket__b(value, out var ptr).Assert();
this.ptr = ptr;
}

public override bool GetValue() {
return UnsafeNativeMethods.MpPacketGetBool(ptr);
public override bool Get() {
return SafeNativeMethods.mp_Packet__GetBool(ptr);
}

public override bool ConsumeValue() {
public override bool Consume() {
throw new NotSupportedException();
}

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

GC.KeepAlive(this);
return new Status(statusPtr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Mediapipe {
public class ClassificationListPacket : Packet<ClassificationList> {
public ClassificationListPacket() : base() {}

public override ClassificationList GetValue() {
public override ClassificationList Get() {
var classificationListPtr = UnsafeNativeMethods.MpPacketGetClassificationList(ptr);
var rect = SerializedProto.FromPtr<ClassificationList>(classificationListPtr, ClassificationList.Parser);

Expand All @@ -13,7 +13,7 @@ public override ClassificationList GetValue() {
return rect;
}

public override ClassificationList ConsumeValue() {
public override ClassificationList Consume() {
throw new NotSupportedException();
}
}
Expand Down
4 changes: 2 additions & 2 deletions Assets/MediaPipe/SDK/Scripts/Framework/DetectionPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Mediapipe {
public class DetectionPacket : Packet<Detection> {
public DetectionPacket() : base() {}

public override Detection GetValue() {
public override Detection Get() {
var detectionPtr = UnsafeNativeMethods.MpPacketGetDetection(ptr);
var detection = SerializedProto.FromPtr<Detection>(detectionPtr, Detection.Parser);

Expand All @@ -14,7 +14,7 @@ public override Detection GetValue() {
return detection;
}

public override Detection ConsumeValue() {
public override Detection Consume() {
throw new NotSupportedException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Mediapipe {
public class DetectionVectorPacket : Packet<List<Detection>> {
public DetectionVectorPacket() : base() {}

public override List<Detection> GetValue() {
public override List<Detection> Get() {
var detectionVecPtr = UnsafeNativeMethods.MpPacketGetDetectionVector(ptr);
var detections = SerializedProtoVector.FromPtr<Detection>(detectionVecPtr, Detection.Parser);

Expand All @@ -14,7 +14,7 @@ public override List<Detection> GetValue() {
return detections;
}

public override List<Detection> ConsumeValue() {
public override List<Detection> Consume() {
throw new NotSupportedException();
}
}
Expand Down
11 changes: 5 additions & 6 deletions Assets/MediaPipe/SDK/Scripts/Framework/FloatPacket.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
using System;
using MpPacket = System.IntPtr;

namespace Mediapipe {
public class FloatPacket : Packet<float> {
public FloatPacket() : base() {}

public FloatPacket(MpPacket ptr) : base(ptr) {}

public FloatPacket(float value) : base(UnsafeNativeMethods.MpMakeFloatPacket(value)) {}
public FloatPacket(float value) {
// TODO: implement
}

public override float GetValue() {
public override float Get() {
return UnsafeNativeMethods.MpPacketGetFloat(ptr);
}

public override float ConsumeValue() {
public override float Consume() {
throw new NotSupportedException();
}
}
Expand Down
15 changes: 8 additions & 7 deletions Assets/MediaPipe/SDK/Scripts/Framework/GpuBufferPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ public class GpuBufferPacket : Packet<GpuBuffer> {
private GCHandle valueHandle;
public GpuBufferPacket() : base() {}

public GpuBufferPacket(GpuBuffer gpuBuffer, int timestamp) :
base(UnsafeNativeMethods.MpMakeGpuBufferPacketAt(gpuBuffer.GetPtr(), timestamp)) {
gpuBuffer.ReleaseOwnership();
valueHandle = GCHandle.Alloc(gpuBuffer);
public GpuBufferPacket(GpuBuffer gpuBuffer, int timestamp) {
// TODO: implement
// base(UnsafeNativeMethods.MpMakeGpuBufferPacketAt(gpuBuffer.GetPtr(), timestamp)) {
// gpuBuffer.ReleaseOwnership();
// valueHandle = GCHandle.Alloc(gpuBuffer);
}

protected override void Dispose(bool disposing) {
Expand All @@ -25,16 +26,16 @@ protected override void Dispose(bool disposing) {
_disposed = true;
}

public override GpuBuffer GetValue() {
public override GpuBuffer Get() {
return new GpuBuffer(UnsafeNativeMethods.MpPacketGetGpuBuffer(ptr), false);
}

public override GpuBuffer ConsumeValue() {
public override GpuBuffer Consume() {
if (!OwnsResource()) {
throw new InvalidOperationException("Not owns resouces to be consumed");
}

return new StatusOrGpuBuffer(UnsafeNativeMethods.MpPacketConsumeGpuBuffer(GetPtr())).ConsumeValue();
return new StatusOrGpuBuffer(UnsafeNativeMethods.MpPacketConsumeGpuBuffer(mpPtr)).ConsumeValue();
}
}
}
17 changes: 9 additions & 8 deletions Assets/MediaPipe/SDK/Scripts/Framework/ImageFramePacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ public class ImageFramePacket : Packet<ImageFrame> {
private GCHandle valueHandle;
public ImageFramePacket() : base() {}

public ImageFramePacket(ImageFrame imageFrame, int timestamp) :
base(UnsafeNativeMethods.MpMakeImageFramePacketAt(imageFrame.GetPtr(), timestamp)) {
imageFrame.ReleaseOwnership();
// to pin the pixelData
valueHandle = GCHandle.Alloc(imageFrame);
public ImageFramePacket(ImageFrame imageFrame, int timestamp) {
// TODO: implement
// base(UnsafeNativeMethods.MpMakeImageFramePacketAt(imageFrame.GetPtr(), timestamp)) {
// imageFrame.ReleaseOwnership();
// // to pin the pixelData
// valueHandle = GCHandle.Alloc(imageFrame);
}

protected override void Dispose(bool disposing) {
Expand All @@ -26,16 +27,16 @@ protected override void Dispose(bool disposing) {
_disposed = true;
}

public override ImageFrame GetValue() {
public override ImageFrame Get() {
return new ImageFrame(UnsafeNativeMethods.MpPacketGetImageFrame(ptr), false);
}

public override ImageFrame ConsumeValue() {
public override ImageFrame Consume() {
if (!OwnsResource()) {
throw new InvalidOperationException("Not owns resouces to be consumed");
}

return new StatusOrImageFrame(UnsafeNativeMethods.MpPacketConsumeImageFrame(GetPtr())).ConsumeValue();
return new StatusOrImageFrame(UnsafeNativeMethods.MpPacketConsumeImageFrame(mpPtr)).ConsumeValue();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Mediapipe {
public class NormalizedLandmarkListPacket : Packet<NormalizedLandmarkList> {
public NormalizedLandmarkListPacket() : base() {}

public override NormalizedLandmarkList GetValue() {
public override NormalizedLandmarkList Get() {
var landmarkListPtr = UnsafeNativeMethods.MpPacketGetNormalizedLandmarkList(ptr);
var rect = SerializedProto.FromPtr<NormalizedLandmarkList>(landmarkListPtr, NormalizedLandmarkList.Parser);

Expand All @@ -13,7 +13,7 @@ public override NormalizedLandmarkList GetValue() {
return rect;
}

public override NormalizedLandmarkList ConsumeValue() {
public override NormalizedLandmarkList Consume() {
throw new NotSupportedException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Mediapipe {
public class NormalizedLandmarkListVectorPacket : Packet<List<NormalizedLandmarkList>> {
public NormalizedLandmarkListVectorPacket() : base() {}

public override List<NormalizedLandmarkList> GetValue() {
public override List<NormalizedLandmarkList> Get() {
var landmarkListVecPtr = UnsafeNativeMethods.MpPacketGetNormalizedLandmarkListVector(ptr);
var rects = SerializedProtoVector.FromPtr<NormalizedLandmarkList>(landmarkListVecPtr, NormalizedLandmarkList.Parser);

Expand All @@ -14,7 +14,7 @@ public override List<NormalizedLandmarkList> GetValue() {
return rects;
}

public override List<NormalizedLandmarkList> ConsumeValue() {
public override List<NormalizedLandmarkList> Consume() {
throw new NotSupportedException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Mediapipe {
public class NormalizedRectPacket : Packet<NormalizedRect> {
public NormalizedRectPacket() : base() {}

public override NormalizedRect GetValue() {
public override NormalizedRect Get() {
var rectPtr = UnsafeNativeMethods.MpPacketGetNormalizedRect(ptr);
var rect = SerializedProto.FromPtr<NormalizedRect>(rectPtr, NormalizedRect.Parser);

Expand All @@ -13,7 +13,7 @@ public override NormalizedRect GetValue() {
return rect;
}

public override NormalizedRect ConsumeValue() {
public override NormalizedRect Consume() {
throw new NotSupportedException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Mediapipe {
public class NormalizedRectVectorPacket : Packet<List<NormalizedRect>> {
public NormalizedRectVectorPacket() : base() {}

public override List<NormalizedRect> GetValue() {
public override List<NormalizedRect> Get() {
var rectVecPtr = UnsafeNativeMethods.MpPacketGetNormalizedRectVector(ptr);
var rects = SerializedProtoVector.FromPtr<NormalizedRect>(rectVecPtr, NormalizedRect.Parser);

Expand All @@ -14,7 +14,7 @@ public override List<NormalizedRect> GetValue() {
return rects;
}

public override List<NormalizedRect> ConsumeValue() {
public override List<NormalizedRect> Consume() {
throw new NotSupportedException();
}
}
Expand Down
71 changes: 57 additions & 14 deletions Assets/MediaPipe/SDK/Scripts/Framework/Packet.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,71 @@
using System;

using MpPacket = System.IntPtr;

namespace Mediapipe {
public abstract class Packet<T> : ResourceHandle {
private bool _disposed = false;
public abstract class Packet<T> : MpResourceHandle {
public Packet() : base() {
UnsafeNativeMethods.mp_Packet__(out var ptr).Assert();
this.ptr = ptr;
}

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

public Packet() : base(UnsafeNativeMethods.MpPacketCreate(), true) {}
public abstract T Get();

public Packet(MpPacket ptr, bool isOwner = true) : base(ptr, isOwner) {}
[Obsolete("GetValue() is deprecated, use Get()")]
public T GetValue() { return Get(); }

public abstract T GetValue();
public abstract T Consume();

public abstract T ConsumeValue();
[Obsolete("ConsumeValue() is deprecated, use Consume()")]
public T ConsumeValue() { return Consume(); }

/// <remarks>To avoid copying the value, instantiate the packet with timestamp</remarks>
/// <returns>New packet with the given timestamp and the copied value</returns>
public Packet<T> At(Timestamp timestamp) {
UnsafeNativeMethods.mp_Packet__At__Rtimestamp(mpPtr, timestamp.mpPtr, out var packetPtr).Assert();

GC.KeepAlive(timestamp);
return (Packet<T>)Activator.CreateInstance(this.GetType(), packetPtr, true);
}

public Status ValidateAsProtoMessageLite() {
UnsafeNativeMethods.mp_Packet__ValidateAsProtoMessageLite(mpPtr, out var statusPtr).Assert();

GC.KeepAlive(this);
return new Status(statusPtr);
}

// TODO: declare as abstract
public virtual Status ValidateAsType() {
throw new NotImplementedException();
}

protected override void Dispose(bool disposing) {
if (_disposed) return;
public Timestamp Timestamp() {
UnsafeNativeMethods.mp_Packet__Timestamp(mpPtr, out var timestampPtr).Assert();

GC.KeepAlive(this);
return new Timestamp(timestampPtr);
}

public string DebugString() {
return MarshalStringFromNative(UnsafeNativeMethods.mp_Packet__DebugString);
}

public string RegisteredTypeName() {
var typeName = MarshalStringFromNative(UnsafeNativeMethods.mp_Packet__RegisteredTypeName);

return typeName == null ? "" : typeName;
}

public string DebugTypeName() {
return MarshalStringFromNative(UnsafeNativeMethods.mp_Packet__DebugTypeName);
}

protected override void DisposeUnmanaged() {
if (OwnsResource()) {
UnsafeNativeMethods.MpPacketDestroy(ptr);
UnsafeNativeMethods.mp_Packet__delete(ptr);
}

ptr = IntPtr.Zero;
_disposed = true;
base.DisposeUnmanaged();
}
}
}
4 changes: 2 additions & 2 deletions Assets/MediaPipe/SDK/Scripts/Framework/RectPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Mediapipe {
public class RectPacket : Packet<Rect> {
public RectPacket() : base() {}

public override Rect GetValue() {
public override Rect Get() {
var rectPtr = UnsafeNativeMethods.MpPacketGetRect(ptr);
var rect = SerializedProto.FromPtr<Rect>(rectPtr, Rect.Parser);

Expand All @@ -13,7 +13,7 @@ public override Rect GetValue() {
return rect;
}

public override Rect ConsumeValue() {
public override Rect Consume() {
throw new NotSupportedException();
}
}
Expand Down
4 changes: 2 additions & 2 deletions Assets/MediaPipe/SDK/Scripts/Framework/RectVectorPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Mediapipe {
public class RectVectorPacket : Packet<List<Rect>> {
public RectVectorPacket() : base() {}

public override List<Rect> GetValue() {
public override List<Rect> Get() {
var rectVecPtr = UnsafeNativeMethods.MpPacketGetRectVector(ptr);
var rects = SerializedProtoVector.FromPtr<Rect>(rectVecPtr, Rect.Parser);

Expand All @@ -14,7 +14,7 @@ public override List<Rect> GetValue() {
return rects;
}

public override List<Rect> ConsumeValue() {
public override List<Rect> Consume() {
throw new NotSupportedException();
}
}
Expand Down
Loading

0 comments on commit 32cab19

Please sign in to comment.