Skip to content

Commit

Permalink
fix one crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Bvallon-sl committed Aug 16, 2024
1 parent 8285965 commit fbe11ef
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
55 changes: 33 additions & 22 deletions ZEDLiveLink/Source/ZEDLiveLink/Private/ZEDLiveLinkSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

FZEDLiveLinkSource::FZEDLiveLinkSource(const FZEDLiveLinkSettings& InSettings)
: Socket(nullptr)
, Stopping(false)
, bIsRunning(false)
, Thread(nullptr)
, WaitTime(FTimespan::FromMilliseconds(500))
{
Expand Down Expand Up @@ -101,7 +101,7 @@ void FZEDLiveLinkSource::ReceiveClient(ILiveLinkClient* InClient, FGuid InSource
bool FZEDLiveLinkSource::IsSourceStillValid() const
{
// Source is valid if we have a valid thread and socket
bool bIsSourceValid = !Stopping && Thread != nullptr && Socket != nullptr;
bool bIsSourceValid = bIsRunning && Thread != nullptr && Socket != nullptr;
return bIsSourceValid;
}

Expand All @@ -117,6 +117,8 @@ bool FZEDLiveLinkSource::RequestSourceShutdown()

void FZEDLiveLinkSource::Start()
{
bIsRunning = true;

ThreadName = "ZED UDP Receiver ";
ThreadName.AppendInt(FAsyncThreadIndex::GetNext());

Expand All @@ -125,13 +127,13 @@ void FZEDLiveLinkSource::Start()

void FZEDLiveLinkSource::Stop()
{
Stopping = true;
bIsRunning = false;
}

uint32 FZEDLiveLinkSource::Run()
{
TSharedRef<FInternetAddr> Sender = SocketSubsystem->CreateInternetAddr();
while (!Stopping)
while (bIsRunning)
{
if (Socket->Wait(ESocketWaitConditions::WaitForRead, WaitTime))
{
Expand All @@ -148,7 +150,7 @@ uint32 FZEDLiveLinkSource::Run()
{
if (Read > 0)
{
TSharedPtr<TArray<uint8>> ReceivedData = MakeShareable(new TArray<uint8>());
TSharedPtr<TArray<uint8>, ESPMode::ThreadSafe> ReceivedData = MakeShareable(new TArray<uint8>());
ReceivedData->SetNumUninitialized(Read);
memcpy(ReceivedData->GetData(), RecvBuffer.GetData(), Read);
AsyncTask(ENamedThreads::GameThread, [this, ReceivedData]() { ProcessReceivedData(ReceivedData); });
Expand Down Expand Up @@ -185,7 +187,7 @@ void FZEDLiveLinkSource::ProcessReceivedData(TSharedPtr<TArray<uint8>> ReceivedD
FLiveLinkFrameDataStruct FrameData;

FLiveLinkSubjectKey Key = FLiveLinkSubjectKey(SourceGuid, SubjectName);
if (Client)
if (Client && bIsRunning)
{
if (frameData.bIsValid)
{
Expand All @@ -200,27 +202,36 @@ void FZEDLiveLinkSource::ProcessReceivedData(TSharedPtr<TArray<uint8>> ReceivedD
}
else
{
if (!Subjects.Contains(SubjectName) && !Client->GetSubjects(true, false).Contains(Key))
TArray<FLiveLinkSubjectKey> SubjectsKey;
FScopeLock Lock(&SubjectsCriticalSection);
{
FLiveLinkSubjectPreset Preset;
Preset.Key = Key;
Preset.Role = frameData.SubjectRole;
//Preset.bEnabled = true;
SubjectsKey = Client->GetSubjects(true, false);
}

if (Client->GetSources().Num() > 0)
if (!SubjectsKey.Contains(Key))
{
if (!Subjects.Contains(SubjectName))
{
Client->CreateSubject(Preset);
Client->SetSubjectEnabled(Key, true);
FLiveLinkSubjectPreset Preset;
Preset.Key = Key;
Preset.Role = frameData.SubjectRole;
//Preset.bEnabled = true;

Subjects.Push(SubjectName);

if (frameData.SubjectRole == ULiveLinkCameraRole::StaticClass())
{
UpdateCameraStaticData(SubjectName, frameData.CameraTransform);
}
else if (frameData.SubjectRole == ULiveLinkAnimationRole::StaticClass())
if (Client->GetSources().Num() > 0)
{
UpdateAnimationStaticData(SubjectName, frameData.ParentsIdx, frameData.TargetBones);
Client->CreateSubject(Preset);
Client->SetSubjectEnabled(Key, true);

Subjects.Push(SubjectName);

if (frameData.SubjectRole == ULiveLinkCameraRole::StaticClass())
{
UpdateCameraStaticData(SubjectName, frameData.CameraTransform);
}
else if (frameData.SubjectRole == ULiveLinkAnimationRole::StaticClass())
{
UpdateAnimationStaticData(SubjectName, frameData.ParentsIdx, frameData.TargetBones);
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion ZEDLiveLink/Source/ZEDLiveLink/Public/ZEDLiveLinkSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ZEDLIVELINK_API FZEDLiveLinkSource : public ILiveLinkSource, public FRunna
ISocketSubsystem* SocketSubsystem;

// Threadsafe Bool for terminating the main thread loop
FThreadSafeBool Stopping;
FThreadSafeBool bIsRunning;

// Thread to run socket operations on
FRunnableThread* Thread;
Expand All @@ -87,6 +87,7 @@ class ZEDLIVELINK_API FZEDLiveLinkSource : public ILiveLinkSource, public FRunna
// Buffer to receive socket data into
TArray<uint8> RecvBuffer;

mutable FCriticalSection SubjectsCriticalSection;

bool FirstConnection = true;
// Check if static data is setup
Expand Down

0 comments on commit fbe11ef

Please sign in to comment.