This is the current setup:
ACameraSensor::ACameraSensor() {
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
RenderTarget = CreateDefaultSubobject<UTextureRenderTarget2D>(TEXT("RenderTarget"));
RenderTarget->TargetGamma = 100.0f;
RenderTarget->RenderTargetFormat = ETextureRenderTargetFormat::RTF_RGBA8_SRGB;
RenderTarget->InitAutoFormat(1920, 1080);
SceneCapture = CreateDefaultSubobject<USceneCaptureComponent2D>(TEXT("SceneCapture"));
SceneCapture->SetupAttachment(RootComponent);
SceneCapture->TextureTarget = RenderTarget;
SetActorEnableCollision(false);
}
Here we initialise a camera with a render target and camera.
void ACameraSensor::StartStreaming(FString& InId) {
if (IPixelStreamingModule::IsAvailable() && !IsStreaming) {
Id = InId;
SceneCapture->Activate();
TSharedPtr<FPixelStreamingVideoInputRenderTarget> SceneVideo = FPixelStreamingVideoInputRenderTarget::Create(RenderTarget);
CameraStreamer = IPixelStreamingModule::Get().CreateStreamer(Id);
CameraStreamer->SetSignallingServerURL("REDACTED");
CameraStreamer->SetVideoInput(SceneVideo.ToSharedRef());
CameraStreamer->StartStreaming();
IsStreaming = true;
}
else {
UE_LOG(LogTemp, Error, TEXT("Failed to start streaming, either module not loaded or camera is already streaming"));
}
}
Here when we start streaming the camera, we pass in the name we want to stream it under, activate the SceneCapture and streamer. This works really well and produces the output.
However, the issue comes when I end the game. If I end the game, it stops but then a segmentation error comes from inside the PixelStreamingVideoInputRenderTarget, my guess is that at this point the resource has come null but it doesn’t check this.
This is how I stop the camera:
void ACameraSensor::StopStreaming() {
if (IsStreaming) {
CameraStreamer->StopStreaming();
SceneCapture->Deactivate();
IsStreaming = false;
}
}
void ACameraSensor::EndPlay(const EEndPlayReason::Type EndPlayReason) {
StopStreaming();
}
All the properties apart from the CameraStreamer are marked as UProperties so should not cause this. This is the exact line the segmentation fault occurs at.
if (FTexture2DRHIRef Texture = Target->GetResource()->GetTexture2DRHI())
Here are the logs:
[2024.01.29-14.09.47:123][619]LogWorld: BeginTearingDown for /Game/VehicleTemplate/Maps/UEDPIE_0_VehicleAdvExampleMap
[2024.01.29-14.09.47:123][619]LogPixelStreamingSS: Closing websocket to SS ws://REDCATED
[2024.01.29-14.09.47:123][619]LogHttpServerModule: Stopping all listeners...
[2024.01.29-14.09.47:123][619]LogHttpListener: HttListener stopping listening on Port 7080
[2024.01.29-14.09.47:123][619]LogHttpServerModule: All listeners stopped
[2024.01.29-14.09.47:131][619]LogTemp: Warning: Service deregistered successfully
[2024.01.29-14.09.47:131][619]LogService: Stopping service...
[2024.01.29-14.09.47:135][619]LogWorld: UWorld::CleanupWorld for VehicleAdvExampleMap, bSessionEnded=true, bCleanupResources=true
[2024.01.29-14.09.47:139][619]LogSlate: InvalidateAllWidgets triggered. All widgets were invalidated
[2024.01.29-14.09.47:139][619]LogContentBundle: [VehicleAdvExampleMap(Standalone)] Deleting container.
[2024.01.29-14.09.47:140][619]LogPlayLevel: Display: Shutting down PIE online subsystems
[2024.01.29-14.09.47:141][619]LogSlate: InvalidateAllWidgets triggered. All widgets were invalidated
[2024.01.29-14.09.47:529][619]LogKafka: Error: Error consuming message -185
[2024.01.29-14.09.47:529][619]LogKafka: Display: Exited consumer
[2024.01.29-14.09.47:532][619]LogSlate: Updating window title bar state: overlay mode, drag disabled, window buttons hidden, title bar hidden
[2024.01.29-14.09.47:532][619]LogAudioMixer: Deinitializing Audio Bus Subsystem for audio device with ID 2
[2024.01.29-14.09.47:579][619]LogUObjectHash: Compacting FUObjectHashTables data took 0.28ms
[2024.01.29-14.09.47:599][620]LogPlayLevel: Display: Destroying online subsystem :Context_1
Any guidance on how I could solve this is greatly appreciated as I am clueless to why this shouldn’t work. Many people have replicated this and it works fine in blueprints.