Skip to content

Commit 1691326

Browse files
committed
Improve NetworkTable code
1 parent af7ebeb commit 1691326

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

core/src/main/java/edu/wpi/grip/core/operations/composite/PublishVideoOperation.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import edu.wpi.cscore.MjpegServer;
1616
import edu.wpi.cscore.VideoMode;
1717
import edu.wpi.first.wpilibj.networktables.NetworkTable;
18+
import edu.wpi.first.wpilibj.tables.ITable;
1819

1920
import org.bytedeco.javacpp.opencv_core;
2021
import org.opencv.core.Mat;
@@ -59,6 +60,8 @@ public class PublishVideoOperation implements Operation {
5960
.build();
6061
private static final int PORT = 1180;
6162

63+
@SuppressWarnings("PMD.AssignmentToNonFinalStatic")
64+
private static int totalStepCount;
6265
@SuppressWarnings("PMD.AssignmentToNonFinalStatic")
6366
private static int numSteps;
6467
private static final int MAX_STEP_COUNT = 10; // limit ports to 1180-1189
@@ -71,8 +74,11 @@ public class PublishVideoOperation implements Operation {
7174
private final InputSocket<Number> qualitySocket;
7275
private final MjpegServer server;
7376
private final CvSource serverSource;
74-
private static final NetworkTable cameraPublisherTable =
75-
NetworkTable.getTable("/CameraPublisher");
77+
78+
// Write to the /CameraPublisher table so the MJPEG streams are discoverable by other
79+
// applications connected to the same NetworkTable server (eg Shuffleboard)
80+
private static final ITable cameraPublisherTable = NetworkTable.getTable("/CameraPublisher");
81+
private final ITable ourTable;
7682
private final Mat publishMat = new Mat();
7783
private long lastFrame = -1;
7884

@@ -91,13 +97,17 @@ public PublishVideoOperation(InputSocket.Factory inputSocketFactory) {
9197

9298
int ourPort = availablePorts.removeFirst();
9399

94-
server = new MjpegServer("GRIP video publishing server " + ourPort, ourPort);
95-
serverSource = new CvSource("GRIP CvSource:" + ourPort, VideoMode.PixelFormat.kMJPEG, 0, 0, 0);
100+
server = new MjpegServer("GRIP video publishing server " + totalStepCount, ourPort);
101+
serverSource = new CvSource("GRIP CvSource " + totalStepCount,
102+
VideoMode.PixelFormat.kMJPEG, 0, 0, 0);
96103
server.setSource(serverSource);
97-
cameraPublisherTable.putStringArray("streams",
98-
new String[]{CameraServerJNI.getHostname() + ":" + ourPort});
104+
105+
ourTable = cameraPublisherTable.getSubTable("GRIP-" + totalStepCount);
106+
ourTable.putStringArray("streams",
107+
new String[]{CameraServerJNI.getHostname() + ":" + ourPort + "/?action=stream"});
99108

100109
numSteps++;
110+
totalStepCount++;
101111
}
102112

103113
@Override
@@ -115,7 +125,7 @@ public List<OutputSocket> getOutputSockets() {
115125

116126
@Override
117127
public void perform() {
118-
final long now = System.nanoTime();
128+
final long now = System.nanoTime(); // NOPMD
119129
opencv_core.Mat input = inputSocket.getValue().get();
120130
if (input.empty() || input.isNull()) {
121131
throw new IllegalArgumentException("Input image must not be empty");
@@ -136,6 +146,7 @@ public synchronized void cleanUp() {
136146
// Stop the video server if there are no Publish Video steps left
137147
numSteps--;
138148
availablePorts.addFirst(server.getPort());
149+
ourTable.getKeys().forEach(ourTable::delete);
139150
}
140151

141152
private void copyJavaCvToOpenCvMat(opencv_core.Mat javaCvMat, Mat openCvMat) {

0 commit comments

Comments
 (0)