Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
Wrap access to VideoSinks with a scope lock
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Audykowicz committed Sep 9, 2023
1 parent 0bc62ec commit 9727e6a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
66 changes: 43 additions & 23 deletions DolbyIO/Source/Private/DolbyIOSubsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ void UDolbyIOSubsystem::Initialize(FSubsystemCollectionBase& Collection)

ConferenceStatus = conference_status::destroyed;

VideoSinks.Emplace(LocalCameraTrackID, std::make_shared<FVideoSink>(LocalCameraTrackID));
VideoSinks.Emplace(LocalScreenshareTrackID, std::make_shared<FVideoSink>(LocalScreenshareTrackID));
LocalCameraFrameHandler = std::make_shared<FVideoFrameHandler>(VideoSinks[LocalCameraTrackID]);
LocalScreenshareFrameHandler = std::make_shared<FVideoFrameHandler>(VideoSinks[LocalScreenshareTrackID]);
{
FScopeLock Lock{&VideoSinksLock};
VideoSinks.Emplace(LocalCameraTrackID, std::make_shared<FVideoSink>(LocalCameraTrackID));
VideoSinks.Emplace(LocalScreenshareTrackID, std::make_shared<FVideoSink>(LocalScreenshareTrackID));
LocalCameraFrameHandler = std::make_shared<FVideoFrameHandler>(VideoSinks[LocalCameraTrackID]);
LocalScreenshareFrameHandler = std::make_shared<FVideoFrameHandler>(VideoSinks[LocalScreenshareTrackID]);
}

FTimerManager& TimerManager = GetGameInstance()->GetTimerManager();
TimerManager.SetTimer(LocationTimerHandle, this, &UDolbyIOSubsystem::SetLocationUsingFirstPlayer, 0.1, true);
Expand Down Expand Up @@ -186,13 +189,14 @@ void UDolbyIOSubsystem::Initialize(const FString& Token)
{
const FDolbyIOVideoTrack VideoTrack = ToFDolbyIOVideoTrack(Event.track);

FScopeLock Lock1{&VideoSinksLock};
VideoSinks.Emplace(VideoTrack.TrackID, std::make_shared<FVideoSink>(VideoTrack.TrackID));
Sdk->video()
.remote()
.set_video_sink(Event.track, VideoSinks[VideoTrack.TrackID])
.on_error(MAKE_DLB_ERROR_HANDLER);

FScopeLock Lock{&RemoteParticipantsLock};
FScopeLock Lock2{&RemoteParticipantsLock};
if (RemoteParticipants.Contains(VideoTrack.ParticipantID))
{
VideoSinks[VideoTrack.TrackID]->OnTextureCreated([this, VideoTrack]
Expand All @@ -216,8 +220,17 @@ void UDolbyIOSubsystem::Initialize(const FString& Token)
DLB_UE_LOG("Video track removed: TrackID=%s ParticipantID=%s", *VideoTrack.TrackID,
*VideoTrack.ParticipantID);

VideoSinks[VideoTrack.TrackID]->UnbindAllMaterials();
VideoSinks.Remove(VideoTrack.TrackID);
FScopeLock Lock{&VideoSinksLock};
if (std::shared_ptr<DolbyIO::FVideoSink>* Sink = VideoSinks.Find(VideoTrack.TrackID))
{
(*Sink)->UnbindAllMaterials();
VideoSinks.Remove(VideoTrack.TrackID);
}
else
{
DLB_UE_WARN("Non-existent video track removed");
}

BroadcastEvent(OnVideoTrackRemoved, VideoTrack);
});
})
Expand Down Expand Up @@ -632,38 +645,43 @@ void UDolbyIOSubsystem::ProcessBufferedVideoTracks(const FString& ParticipantID)
{
if (TArray<FDolbyIOVideoTrack>* AddedTracks = BufferedAddedVideoTracks.Find(ParticipantID))
{
FScopeLock Lock{&VideoSinksLock};
for (const FDolbyIOVideoTrack& AddedTrack : *AddedTracks)
{
VideoSinks[AddedTrack.TrackID]->OnTextureCreated(
[=]
{
BroadcastVideoTrackAdded(AddedTrack);

if (TArray<FDolbyIOVideoTrack>* EnabledTracks = BufferedEnabledVideoTracks.Find(ParticipantID))
if (std::shared_ptr<DolbyIO::FVideoSink>* Sink = VideoSinks.Find(AddedTrack.TrackID))
{
(*Sink)->OnTextureCreated(
[=]
{
TArray<FDolbyIOVideoTrack>& EnabledTracksRef = *EnabledTracks;
for (int i = 0; i < EnabledTracksRef.Num(); ++i)
BroadcastVideoTrackAdded(AddedTrack);

if (TArray<FDolbyIOVideoTrack>* EnabledTracks = BufferedEnabledVideoTracks.Find(ParticipantID))
{
if (EnabledTracksRef[i].TrackID == AddedTrack.TrackID)
TArray<FDolbyIOVideoTrack>& EnabledTracksRef = *EnabledTracks;
for (int i = 0; i < EnabledTracksRef.Num(); ++i)
{
BroadcastVideoTrackEnabled(EnabledTracksRef[i]);
EnabledTracksRef.RemoveAt(i);
if (!EnabledTracksRef.Num())
if (EnabledTracksRef[i].TrackID == AddedTrack.TrackID)
{
BufferedEnabledVideoTracks.Remove(ParticipantID);
BroadcastVideoTrackEnabled(EnabledTracksRef[i]);
EnabledTracksRef.RemoveAt(i);
if (!EnabledTracksRef.Num())
{
BufferedEnabledVideoTracks.Remove(ParticipantID);
}
return;
}
return;
}
}
}
});
});
}
}
BufferedAddedVideoTracks.Remove(ParticipantID);
}
}

UTexture2D* UDolbyIOSubsystem::GetTexture(const FString& VideoTrackID)
{
FScopeLock Lock{&VideoSinksLock};
if (const std::shared_ptr<FVideoSink>* Sink = VideoSinks.Find(VideoTrackID))
{
return (*Sink)->GetTexture();
Expand Down Expand Up @@ -753,6 +771,7 @@ void UDolbyIOSubsystem::ChangeScreenshareParameters(EDolbyIOScreenshareEncoderHi

void UDolbyIOSubsystem::BindMaterial(UMaterialInstanceDynamic* Material, const FString& VideoTrackID)
{
FScopeLock Lock{&VideoSinksLock};
for (auto& Sink : VideoSinks)
{
if (Sink.Key != VideoTrackID)
Expand All @@ -769,6 +788,7 @@ void UDolbyIOSubsystem::BindMaterial(UMaterialInstanceDynamic* Material, const F

void UDolbyIOSubsystem::UnbindMaterial(UMaterialInstanceDynamic* Material, const FString& VideoTrackID)
{
FScopeLock Lock{&VideoSinksLock};
if (const std::shared_ptr<DolbyIO::FVideoSink>* Sink = VideoSinks.Find(VideoTrackID))
{
(*Sink)->UnbindMaterial(Material);
Expand Down
3 changes: 3 additions & 0 deletions DolbyIO/Source/Public/DolbyIOSubsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,13 @@ class DOLBYIO_API UDolbyIOSubsystem : public UGameInstanceSubsystem
EDolbyIOSpatialAudioStyle SpatialAudioStyle;
TMap<FString, TArray<FDolbyIOVideoTrack>> BufferedAddedVideoTracks;
TMap<FString, TArray<FDolbyIOVideoTrack>> BufferedEnabledVideoTracks;

TMap<FString, FDolbyIOParticipantInfo> RemoteParticipants;
FCriticalSection RemoteParticipantsLock;

TMap<FString, std::shared_ptr<DolbyIO::FVideoSink>> VideoSinks;
FCriticalSection VideoSinksLock;

std::shared_ptr<DolbyIO::FVideoFrameHandler> LocalCameraFrameHandler;
std::shared_ptr<DolbyIO::FVideoFrameHandler> LocalScreenshareFrameHandler;
TSharedPtr<dolbyio::comms::sdk> Sdk;
Expand Down

0 comments on commit 9727e6a

Please sign in to comment.