-
-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement PacketsCallbackTable (#971)
* refactor: TaskRunner passes callback_id to the callback * feat: implement PacketsCallbackTable * refactor: dispose PacketMap when Process / Send called * fix: packets_callback must be null in some cases
- Loading branch information
Showing
8 changed files
with
118 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Core/PacketsCallbackTable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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; | ||
using UnityEngine; | ||
|
||
namespace Mediapipe.Tasks.Core | ||
{ | ||
internal class PacketsCallbackTable | ||
{ | ||
private const int _MaxSize = 20; | ||
|
||
private static int _Counter = 0; | ||
private static readonly GlobalInstanceTable<int, TaskRunner.PacketsCallback> _Table = new GlobalInstanceTable<int, TaskRunner.PacketsCallback>(_MaxSize); | ||
|
||
public static (int, TaskRunner.NativePacketsCallback) Add(TaskRunner.PacketsCallback callback) | ||
{ | ||
if (callback == null) | ||
{ | ||
return (-1, null); | ||
} | ||
|
||
var callbackId = _Counter++; | ||
_Table.Add(callbackId, callback); | ||
return (callbackId, InvokeCallbackIfFound); | ||
} | ||
|
||
public static bool TryGetValue(int id, out TaskRunner.PacketsCallback callback) => _Table.TryGetValue(id, out callback); | ||
|
||
[AOT.MonoPInvokeCallback(typeof(TaskRunner.NativePacketsCallback))] | ||
private static void InvokeCallbackIfFound(int callbackId, IntPtr statusPtr, IntPtr packetMapPtr) | ||
{ | ||
// NOTE: if status is not OK, packetMap will be nullptr | ||
if (packetMapPtr == IntPtr.Zero) | ||
{ | ||
var status = new Status(statusPtr, false); | ||
Debug.LogError(status.ToString()); | ||
return; | ||
} | ||
|
||
if (TryGetValue(callbackId, out var callback)) | ||
{ | ||
callback(new PacketMap(packetMapPtr, false)); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Core/PacketsCallbackTable.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters