Skip to content

Commit

Permalink
callbacks have a user parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
thecaptury committed Dec 17, 2024
1 parent a51d342 commit 1b26da4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
45 changes: 27 additions & 18 deletions RemoteCaptury.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,15 @@ static std::string lastStatusMessage = "disconnected";
static bool getLocalPoses = false;

static CapturyNewPoseCallback newPoseCallback = NULL;
static void* newPoseArg = NULL;
static CapturyNewAnglesCallback newAnglesCallback = NULL;
static void* newAnglesArg = NULL;
static CapturyActorChangedCallback actorChangedCallback = NULL;
static void* actorChangedArg = NULL;
static CapturyARTagCallback arTagCallback = NULL;
static void* arTagArg = NULL;
static CapturyImageCallback imageCallback = NULL;
static void* imageArg = NULL;

static bool handshakeFinished = false;
static bool isStreamThreadRunning = false;
Expand Down Expand Up @@ -980,7 +985,7 @@ static bool receive(SOCKET& sok)
actorsById[actor->id] = actor;
unlockMutex(&mutex);
if (actorChangedCallback)
actorChangedCallback(actor->id, actorData[actor->id].status);
actorChangedCallback(actor->id, actorData[actor->id].status, actorChangedArg);
} else {
lockMutex(&partialActorMutex);
partialActors[actor->id] = actor;
Expand Down Expand Up @@ -1052,7 +1057,7 @@ static bool receive(SOCKET& sok)
actorsById[actor->id] = actor;
unlockMutex(&mutex);
if (actorChangedCallback)
actorChangedCallback(actor->id, actorData[actor->id].status);
actorChangedCallback(actor->id, actorData[actor->id].status, actorChangedArg);
lockMutex(&partialActorMutex);
partialActors.erase(actor->id);
unlockMutex(&partialActorMutex);
Expand Down Expand Up @@ -1216,7 +1221,7 @@ static bool receive(SOCKET& sok)
case capturyActorModeChanged: {
CapturyActorModeChangedPacket* amc = (CapturyActorModeChangedPacket*)p;
if (actorChangedCallback != NULL)
actorChangedCallback(amc->actor, amc->mode);
actorChangedCallback(amc->actor, amc->mode, actorChangedArg);
lockMutex(&mutex);
if (actorData.count(amc->actor))
actorData[amc->actor].status = (CapturyActorStatus)amc->mode;
Expand Down Expand Up @@ -1274,7 +1279,7 @@ static void deleteActors()
unlockMutex(&mutex);

for (int id : deletedActorIds)
actorChangedCallback(id, ACTOR_DELETED);
actorChangedCallback(id, ACTOR_DELETED, actorChangedArg);
}

#ifdef WIN32
Expand Down Expand Up @@ -1350,7 +1355,7 @@ static void receivedPose(CapturyPose* pose, int actorId, ActorData* aData, uint6
aData->status = ACTOR_TRACKING;
if (actorChangedCallback) {
unlockMutex(&mutex);
actorChangedCallback(actorId, ACTOR_TRACKING);
actorChangedCallback(actorId, ACTOR_TRACKING, actorChangedArg);
lockMutex(&mutex);
}
}
Expand All @@ -1360,7 +1365,7 @@ static void receivedPose(CapturyPose* pose, int actorId, ActorData* aData, uint6
CapturyActor* actor = a.get();
returnedActors[actor] = a;
unlockMutex(&mutex);
newPoseCallback(actor, pose, aData->trackingQuality);
newPoseCallback(actor, pose, aData->trackingQuality, newPoseArg);
lockMutex(&mutex);
}

Expand All @@ -1380,7 +1385,7 @@ static void receivedPose(CapturyPose* pose, int actorId, ActorData* aData, uint6

unlockMutex(&mutex);
for (int id : stoppedActorIds)
actorChangedCallback(id, ACTOR_STOPPED);
actorChangedCallback(id, ACTOR_STOPPED, actorChangedArg);
}

static void decompressPose(CapturyPose* pose, uint8_t* v, CapturyActor* actor)
Expand Down Expand Up @@ -1697,7 +1702,7 @@ static void* streamLoop(void* arg)
unlockMutex(&mutex);

if (finished && imageCallback)
imageCallback(&currentImagesDone[cip->actor]);
imageCallback(&currentImagesDone[cip->actor], imageArg);

continue;
}
Expand All @@ -1713,14 +1718,14 @@ static void* streamLoop(void* arg)
// log(" id %d: orient % 4.1f,% 4.1f,% 4.1f\n", art->tags[i].id, art->tags[i].transform.rotation[0], art->tags[i].transform.rotation[1], art->tags[i].transform.rotation[2]);
unlockMutex(&mutex);
if (arTagCallback != NULL)
arTagCallback(art->numTags, &art->tags[0]);
arTagCallback(art->numTags, &art->tags[0], arTagArg);
continue;
}

if (cpp->type == capturyAngles) {
CapturyAnglesPacket* ang = (CapturyAnglesPacket*)buffer.data();
if (newAnglesCallback != NULL)
newAnglesCallback(Captury_getActor(ang->actor), ang->numAngles, ang->angles);
newAnglesCallback(Captury_getActor(ang->actor), ang->numAngles, ang->angles, newAnglesArg);
lockMutex(&mutex);
currentAngles[ang->actor].resize(ang->numAngles);
for (int i = 0; i < ang->numAngles; ++i)
Expand All @@ -1733,7 +1738,7 @@ static void* streamLoop(void* arg)
CapturyActorModeChangedPacket* amc = (CapturyActorModeChangedPacket*)buffer.data();
log("received actorModeChanged packet %x %d\n", amc->actor, amc->mode);
if (actorChangedCallback != NULL)
actorChangedCallback(amc->actor, amc->mode);
actorChangedCallback(amc->actor, amc->mode, actorChangedArg);
lockMutex(&mutex);
if (actorData.count(amc->actor))
actorData[amc->actor].status = (CapturyActorStatus)amc->mode;
Expand Down Expand Up @@ -2253,7 +2258,7 @@ extern "C" CapturyPose* Captury_getCurrentPoseAndTrackingConsistencyForActor(int

unlockMutex(&mutex);
for (int id : stoppedActorIds)
actorChangedCallback(id, ACTOR_STOPPED);
actorChangedCallback(id, ACTOR_STOPPED, actorChangedArg);
lockMutex(&mutex);

if (!stillCurrent) {
Expand Down Expand Up @@ -2848,7 +2853,7 @@ int Captury_sendCustomPacket(char* pluginName, int size, void* data)
}


int Captury_registerNewPoseCallback(CapturyNewPoseCallback callback)
int Captury_registerNewPoseCallback(CapturyNewPoseCallback callback, void* userArg)
{
if (newPoseCallback != NULL) { // callback already exists
if (callback == NULL) { // remove callback
Expand All @@ -2862,11 +2867,12 @@ int Captury_registerNewPoseCallback(CapturyNewPoseCallback callback)
return 0;

newPoseCallback = callback;
newPoseArg = userArg;
return 1;
}


int Captury_registerNewAnglesCallback(CapturyNewAnglesCallback callback)
int Captury_registerNewAnglesCallback(CapturyNewAnglesCallback callback, void* userArg)
{
if (newAnglesCallback != NULL) { // callback already exists
if (callback == NULL) { // remove callback
Expand All @@ -2880,11 +2886,12 @@ int Captury_registerNewAnglesCallback(CapturyNewAnglesCallback callback)
return 0;

newAnglesCallback = callback;
newAnglesArg = userArg;
return 1;
}


int Captury_registerActorChangedCallback(CapturyActorChangedCallback callback)
int Captury_registerActorChangedCallback(CapturyActorChangedCallback callback, void* userArg)
{
if (actorChangedCallback != NULL) { // callback already exists
if (callback == NULL) { // remove callback
Expand All @@ -2898,11 +2905,12 @@ int Captury_registerActorChangedCallback(CapturyActorChangedCallback callback)
return 0;

actorChangedCallback = callback;
actorChangedArg = userArg;
return 1;
}


int Captury_registerARTagCallback(CapturyARTagCallback callback)
int Captury_registerARTagCallback(CapturyARTagCallback callback, void* userArg)
{
if (arTagCallback != NULL) { // callback already exists
if (callback == NULL) { // remove callback
Expand All @@ -2916,11 +2924,12 @@ int Captury_registerARTagCallback(CapturyARTagCallback callback)
return 0;

arTagCallback = callback;
arTagArg = userArg;
return 1;
}


int Captury_registerImageStreamingCallback(CapturyImageCallback callback)
int Captury_registerImageStreamingCallback(CapturyImageCallback callback, void* userArg)
{
if (imageCallback != NULL) {
if (callback == NULL) { // callback already exists
Expand All @@ -2932,7 +2941,7 @@ int Captury_registerImageStreamingCallback(CapturyImageCallback callback)
return 0;

imageCallback = callback;

imageArg = userArg;
return 1;
}

Expand Down
16 changes: 8 additions & 8 deletions RemoteCaptury.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,25 +158,25 @@ CAPTURY_DLL_EXPORT CapturyAngleData* Captury_getCurrentAngles(int actorId, int*
// simple function for releasing memory of a pose
CAPTURY_DLL_EXPORT void Captury_freePose(CapturyPose* pose);

typedef void (*CapturyNewPoseCallback)(CapturyActor*, CapturyPose*, int trackingQuality);
typedef void (*CapturyNewPoseCallback)(CapturyActor*, CapturyPose*, int trackingQuality, void* userArg);

// register callback that will be called when a new pose is received
// the callback will be run in a different thread than the main application
// try to be quick in the callback
// returns 1 if successful otherwise 0
CAPTURY_DLL_EXPORT int Captury_registerNewPoseCallback(CapturyNewPoseCallback callback);
CAPTURY_DLL_EXPORT int Captury_registerNewPoseCallback(CapturyNewPoseCallback callback, void* userArg);

typedef void (*CapturyNewAnglesCallback)(const CapturyActor*, int numAngles, struct CapturyAngleData* values);
typedef void (*CapturyNewAnglesCallback)(const CapturyActor*, int numAngles, struct CapturyAngleData* values, void* userArg);

// register callback that will be called when new physiological angle data is received
// the callback will be run in a different thread than the main application
// try to be quick in the callback
// returns 1 if successful otherwise 0
CAPTURY_DLL_EXPORT int Captury_registerNewAnglesCallback(CapturyNewAnglesCallback callback);
CAPTURY_DLL_EXPORT int Captury_registerNewAnglesCallback(CapturyNewAnglesCallback callback, void* userArg);

typedef enum { ACTOR_SCALING = 0, ACTOR_TRACKING = 1, ACTOR_STOPPED = 2, ACTOR_DELETED = 3, ACTOR_UNKNOWN = 4 } CapturyActorStatus;
extern const char* CapturyActorStatusString[];
typedef void (*CapturyActorChangedCallback)(int actorId, int mode);
typedef void (*CapturyActorChangedCallback)(int actorId, int mode, void* userArg);
// returns CapturyActorStatus if the actorId is not known returns ACTOR_UNKNOWN
// this retrieves the local status. it causes no network traffic and should be fast.
CAPTURY_DLL_EXPORT int Captury_getActorStatus(int actorId);
Expand All @@ -185,9 +185,9 @@ CAPTURY_DLL_EXPORT int Captury_getActorStatus(int actorId);
// the status of an existing actor changes
// status can be one of CapturyActorStatus
// returns 1 if successful otherwise 0
CAPTURY_DLL_EXPORT int Captury_registerActorChangedCallback(CapturyActorChangedCallback callback);
CAPTURY_DLL_EXPORT int Captury_registerActorChangedCallback(CapturyActorChangedCallback callback, void* userArg);

typedef void (*CapturyARTagCallback)(int num, CapturyARTag*);
typedef void (*CapturyARTagCallback)(int num, CapturyARTag*, void* userArg);

// register callback that will be called when an artag is detected
// pass NULL if you want to deregister the callback
Expand All @@ -201,7 +201,7 @@ CAPTURY_DLL_EXPORT CapturyARTag* Captury_getCurrentARTags();
CAPTURY_DLL_EXPORT void Captury_freeARTags(CapturyARTag* artags);

// do NOT free the image
typedef void (*CapturyImageCallback)(const CapturyImage* img);
typedef void (*CapturyImageCallback)(const CapturyImage* img, void* userArg);

// register callback that will be called when a new frame was streamed from this particular camera
// pass NULL to deregister
Expand Down

0 comments on commit 1b26da4

Please sign in to comment.