7
7
import edu .wpi .grip .core .sockets .SocketHints ;
8
8
import edu .wpi .grip .core .util .Icon ;
9
9
10
+ import com .google .common .annotations .VisibleForTesting ;
10
11
import com .google .common .collect .ImmutableList ;
11
12
12
13
import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
17
18
import edu .wpi .first .wpilibj .networktables .NetworkTable ;
18
19
import edu .wpi .first .wpilibj .tables .ITable ;
19
20
20
- import org .apache .commons .lang .SystemUtils ;
21
21
import org .bytedeco .javacpp .opencv_core ;
22
22
import org .opencv .core .Mat ;
23
23
24
- import java .io .BufferedReader ;
25
- import java .io .IOException ;
26
- import java .io .InputStreamReader ;
27
24
import java .lang .reflect .Field ;
28
- import java .util .Arrays ;
25
+ import java .net .InetAddress ;
26
+ import java .net .UnknownHostException ;
29
27
import java .util .Deque ;
30
28
import java .util .LinkedList ;
31
29
import java .util .List ;
@@ -115,8 +113,16 @@ public PublishVideoOperation(InputSocket.Factory inputSocketFactory) {
115
113
server .setSource (serverSource );
116
114
117
115
ourTable = cameraPublisherTable .getSubTable ("GRIP-" + totalStepCount );
118
- ourTable .putStringArray ("streams" ,
119
- new String []{"mjpeg:http://" + getHostName () + ":" + ourPort + "/?action=stream" });
116
+ try {
117
+ InetAddress localHost = InetAddress .getLocalHost ();
118
+ ourTable .putStringArray ("streams" ,
119
+ new String []{
120
+ generateStreamUrl (localHost .getHostName (), ourPort ),
121
+ generateStreamUrl (localHost .getHostAddress (), ourPort )
122
+ });
123
+ } catch (UnknownHostException e ) {
124
+ ourTable .putStringArray ("streams" , new String [0 ]);
125
+ }
120
126
} else {
121
127
server = null ;
122
128
serverSource = null ;
@@ -177,6 +183,10 @@ public synchronized void cleanUp() {
177
183
}
178
184
}
179
185
186
+ private static String generateStreamUrl (String host , int port ) {
187
+ return String .format ("mjpeg:http://%s:%d/?action=stream" , host , port );
188
+ }
189
+
180
190
/**
181
191
* Copies the data from a JavaCV Mat wrapper object into an OpenCV Mat wrapper object so it's
182
192
* usable by the {@link CvSource} for this operation.
@@ -195,7 +205,8 @@ public synchronized void cleanUp() {
195
205
* @param openCvMat the OpenCV Mat wrapper object to copy into
196
206
* @throws RuntimeException if the OpenCV native pointer could not be set
197
207
*/
198
- private static void copyJavaCvToOpenCvMat (opencv_core .Mat javaCvMat , Mat openCvMat )
208
+ @ VisibleForTesting
209
+ static void copyJavaCvToOpenCvMat (opencv_core .Mat javaCvMat , Mat openCvMat )
199
210
throws RuntimeException {
200
211
// Make the OpenCV Mat object point to the same block of memory as the JavaCV object.
201
212
// This requires no data transfers or copies and is O(1) instead of O(n)
@@ -211,40 +222,4 @@ private static void copyJavaCvToOpenCvMat(opencv_core.Mat javaCvMat, Mat openCvM
211
222
}
212
223
}
213
224
214
- /**
215
- * Multi platform method for getting the hostname of the local computer. cscore's
216
- * {@link CameraServerJNI#getHostname() getHostName() function} only works on Linux, so we need to
217
- * implement the method for Windows and Mac ourselves.
218
- */
219
- private static String getHostName () {
220
- if (SystemUtils .IS_OS_WINDOWS ) {
221
- // Use the Windows `hostname` command-line utility
222
- // This will return a single line of text containing the hostname, no parsing required
223
- ProcessBuilder builder = new ProcessBuilder ("hostname" );
224
- Process hostname ;
225
- try {
226
- hostname = builder .start ();
227
- } catch (IOException e ) {
228
- logger .log (Level .WARNING , "Could not start hostname process" , e );
229
- return "" ;
230
- }
231
- try (BufferedReader in =
232
- new BufferedReader (new InputStreamReader (hostname .getInputStream ()))) {
233
- return in .readLine () + ".local" ;
234
- } catch (IOException e ) {
235
- logger .log (Level .WARNING , "Could not read the hostname process output" , e );
236
- return "" ;
237
- }
238
- } else if (SystemUtils .IS_OS_LINUX ) {
239
- // cscore already defines it for linux
240
- return CameraServerJNI .getHostname ();
241
- } else if (SystemUtils .IS_OS_MAC ) {
242
- // todo
243
- return "TODO-MAC" ;
244
- } else {
245
- throw new UnsupportedOperationException (
246
- "Unsupported operating system " + System .getProperty ("os.name" ));
247
- }
248
- }
249
-
250
225
}
0 commit comments