15
15
import edu .wpi .cscore .MjpegServer ;
16
16
import edu .wpi .cscore .VideoMode ;
17
17
import edu .wpi .first .wpilibj .networktables .NetworkTable ;
18
+ import edu .wpi .first .wpilibj .tables .ITable ;
18
19
19
20
import org .bytedeco .javacpp .opencv_core ;
20
21
import org .opencv .core .Mat ;
@@ -59,6 +60,8 @@ public class PublishVideoOperation implements Operation {
59
60
.build ();
60
61
private static final int PORT = 1180 ;
61
62
63
+ @ SuppressWarnings ("PMD.AssignmentToNonFinalStatic" )
64
+ private static int totalStepCount ;
62
65
@ SuppressWarnings ("PMD.AssignmentToNonFinalStatic" )
63
66
private static int numSteps ;
64
67
private static final int MAX_STEP_COUNT = 10 ; // limit ports to 1180-1189
@@ -71,8 +74,11 @@ public class PublishVideoOperation implements Operation {
71
74
private final InputSocket <Number > qualitySocket ;
72
75
private final MjpegServer server ;
73
76
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 ;
76
82
private final Mat publishMat = new Mat ();
77
83
private long lastFrame = -1 ;
78
84
@@ -91,13 +97,17 @@ public PublishVideoOperation(InputSocket.Factory inputSocketFactory) {
91
97
92
98
int ourPort = availablePorts .removeFirst ();
93
99
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 );
96
103
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" });
99
108
100
109
numSteps ++;
110
+ totalStepCount ++;
101
111
}
102
112
103
113
@ Override
@@ -115,7 +125,7 @@ public List<OutputSocket> getOutputSockets() {
115
125
116
126
@ Override
117
127
public void perform () {
118
- final long now = System .nanoTime ();
128
+ final long now = System .nanoTime (); // NOPMD
119
129
opencv_core .Mat input = inputSocket .getValue ().get ();
120
130
if (input .empty () || input .isNull ()) {
121
131
throw new IllegalArgumentException ("Input image must not be empty" );
@@ -136,6 +146,7 @@ public synchronized void cleanUp() {
136
146
// Stop the video server if there are no Publish Video steps left
137
147
numSteps --;
138
148
availablePorts .addFirst (server .getPort ());
149
+ ourTable .getKeys ().forEach (ourTable ::delete );
139
150
}
140
151
141
152
private void copyJavaCvToOpenCvMat (opencv_core .Mat javaCvMat , Mat openCvMat ) {
0 commit comments