Skip to content

Add remote connectivity constructor to ZEDImageSensor #791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public class ZEDImageSensor extends ImageSensor
private final ZEDModelData zedModel;
private final int slInputType;
private final int slDepthMode;
private final int streamingPort = nextStreamingPort++;
private String remoteStreamingAddress;
private int remoteStreamingPort;
private final int localStreamingPort = nextStreamingPort++;

private final RawImage[] grabbedImages = new RawImage[OUTPUT_IMAGE_COUNT];
private final Pointer[] slMatPointers = new Pointer[OUTPUT_IMAGE_COUNT];
Expand Down Expand Up @@ -98,6 +100,17 @@ public ZEDImageSensor(int cameraID, ZEDModelData zedModel, int slInputType, int
zedRuntimeParameters.enable_fill_mode(false);
}

/**
* Constructor to connect to a remote ZED SDK instance
*/
public ZEDImageSensor(int cameraID, ZEDModelData zedModel, int slDepthMode, String remoteStreamingAddress, int remoteStreamingPort)
{
this(cameraID, zedModel, SL_INPUT_TYPE_STREAM, slDepthMode);

this.remoteStreamingAddress = remoteStreamingAddress;
this.remoteStreamingPort = remoteStreamingPort;
}

private void updateReferenceFrames()
{
if (leftSensorFrame != null)
Expand Down Expand Up @@ -140,13 +153,21 @@ protected boolean startSensor()
sl_enable_positional_tracking(cameraID, positionalTrackingParameters, "");
}

sl_enable_streaming(cameraID, SL_STREAMING_CODEC_H264, 8000, (short) streamingPort, -1, 0, 16084, CAMERA_FPS);

// Get camera intrinsics
SL_CalibrationParameters sensorIntrinsics = sl_get_calibration_parameters(cameraID, false);
imageWidth = sl_get_width(cameraID);
imageHeight = sl_get_height(cameraID);

if (slInputType != SL_INPUT_TYPE_STREAM)
sl_enable_streaming(cameraID,
SL_STREAMING_CODEC_H264,
calculateBitrate(imageWidth, imageHeight, CAMERA_FPS),
(short) localStreamingPort,
-1,
0,
16084,
CAMERA_FPS);

leftSensorIntrinsics.setWidth(imageWidth);
leftSensorIntrinsics.setHeight(imageHeight);
leftSensorIntrinsics.setFx(sensorIntrinsics.left_cam().fx());
Expand Down Expand Up @@ -174,6 +195,14 @@ protected boolean startSensor()
catch (ZEDException exception)
{
LogTools.error(exception);

if (slInputType == SL_INPUT_TYPE_STREAM)
{
// Do not retry if stream, it can cause a native crash. TODO: Look into this
LogTools.info("Connection to remote ZED SDK failed, not retrying.");
close();
}

return false;
}

Expand Down Expand Up @@ -208,7 +237,10 @@ protected void setInitParameters(SL_InitParameters parametersToSet)

protected int openCamera()
{
return sl_open_camera(cameraID, zedInitParameters, 0, "", "", 0, "", "", "");
if (slInputType == SL_INPUT_TYPE_STREAM)
return sl_open_camera(cameraID, zedInitParameters, 0, "", remoteStreamingAddress, remoteStreamingPort, "", "", "");
else
return sl_open_camera(cameraID, zedInitParameters, 0, "", "", 0, "", "", "");
}

@Override
Expand Down Expand Up @@ -372,7 +404,7 @@ public int getCameraID()

public int getStreamingPort()
{
return streamingPort;
return localStreamingPort;
}

public void enablePositionalTracking(boolean enable)
Expand Down Expand Up @@ -470,4 +502,10 @@ private String getZEDErrorName(int errorCode)
default -> "UNKNOWN";
};
}

private static int calculateBitrate(int width, int height, int fps)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returns ~8000 for 1920x1080@30

{
double bpp = 0.128; // Derived from 8000 kbps for 1080p30
return (int) ((width * height * fps * bpp) / 1000);
}
}
Loading