17
17
18
18
package org .photonvision .vision .frame .consumer ;
19
19
20
+ import edu .wpi .first .cameraserver .CameraServer ;
20
21
import edu .wpi .first .cscore .*;
21
- import edu .wpi .first .networktables .NetworkTable ;
22
- import edu .wpi .first .networktables .NetworkTableInstance ;
23
22
import edu .wpi .first .util .PixelFormat ;
24
- import java .util .ArrayList ;
25
23
import org .photonvision .common .util .math .MathUtils ;
26
24
import org .photonvision .vision .frame .StaticFrames ;
27
25
import org .photonvision .vision .opencv .CVMat ;
@@ -34,59 +32,13 @@ public class MJPGFrameConsumer implements AutoCloseable {
34
32
private CvSource cvSource ;
35
33
private MjpegServer mjpegServer ;
36
34
37
- private VideoListener listener ;
38
-
39
- private final NetworkTable table ;
40
-
41
35
public MJPGFrameConsumer (String sourceName , int width , int height , int port ) {
42
36
this .cvSource = new CvSource (sourceName , PixelFormat .kMJPEG , width , height , 30 );
43
- this .table =
44
- NetworkTableInstance .getDefault ().getTable ("/CameraPublisher" ).getSubTable (sourceName );
45
37
46
38
this .mjpegServer = new MjpegServer ("serve_" + cvSource .getName (), port );
47
39
mjpegServer .setSource (cvSource );
48
40
mjpegServer .setCompression (75 );
49
-
50
- listener =
51
- new VideoListener (
52
- event -> {
53
- if (event .kind == VideoEvent .Kind .kNetworkInterfacesChanged ) {
54
- table .getEntry ("source" ).setString ("cv:" );
55
- table .getEntry ("streams" );
56
- table .getEntry ("connected" ).setBoolean (true );
57
- table .getEntry ("mode" ).setString (videoModeToString (cvSource .getVideoMode ()));
58
- table .getEntry ("modes" ).setStringArray (getSourceModeValues (cvSource .getHandle ()));
59
- updateStreamValues ();
60
- }
61
- },
62
- 0x4fff ,
63
- true );
64
- }
65
-
66
- private synchronized void updateStreamValues () {
67
- // Get port
68
- int port = mjpegServer .getPort ();
69
-
70
- // Generate values
71
- var addresses = CameraServerJNI .getNetworkInterfaces ();
72
- ArrayList <String > values = new ArrayList <>(addresses .length + 1 );
73
- String listenAddress = CameraServerJNI .getMjpegServerListenAddress (mjpegServer .getHandle ());
74
- if (!listenAddress .isEmpty ()) {
75
- // If a listen address is specified, only use that
76
- values .add (makeStreamValue (listenAddress , port ));
77
- } else {
78
- // Otherwise generate for hostname and all interface addresses
79
- values .add (makeStreamValue (CameraServerJNI .getHostname () + ".local" , port ));
80
- for (String addr : addresses ) {
81
- if ("127.0.0.1" .equals (addr )) {
82
- continue ; // ignore localhost
83
- }
84
- values .add (makeStreamValue (addr , port ));
85
- }
86
- }
87
-
88
- String [] streamAddresses = values .toArray (new String [0 ]);
89
- table .getEntry ("streams" ).setStringArray (streamAddresses );
41
+ CameraServer .addServer (mjpegServer );
90
42
}
91
43
92
44
public MJPGFrameConsumer (String name , int port ) {
@@ -106,53 +58,12 @@ public void accept(CVMat image) {
106
58
}
107
59
}
108
60
109
- public int getCurrentStreamPort () {
110
- return mjpegServer .getPort ();
111
- }
112
-
113
- private static String makeStreamValue (String address , int port ) {
114
- return "mjpg:http://" + address + ":" + port + "/?action=stream" ;
115
- }
116
-
117
- private static String [] getSourceModeValues (int sourceHandle ) {
118
- VideoMode [] modes = CameraServerJNI .enumerateSourceVideoModes (sourceHandle );
119
- String [] modeStrings = new String [modes .length ];
120
- for (int i = 0 ; i < modes .length ; i ++) {
121
- modeStrings [i ] = videoModeToString (modes [i ]);
122
- }
123
- return modeStrings ;
124
- }
125
-
126
- private static String videoModeToString (VideoMode mode ) {
127
- return mode .width
128
- + "x"
129
- + mode .height
130
- + " "
131
- + pixelFormatToString (mode .pixelFormat )
132
- + " "
133
- + mode .fps
134
- + " fps" ;
135
- }
136
-
137
- private static String pixelFormatToString (PixelFormat pixelFormat ) {
138
- return switch (pixelFormat ) {
139
- case kMJPEG -> "MJPEG" ;
140
- case kYUYV -> "YUYV" ;
141
- case kRGB565 -> "RGB565" ;
142
- case kBGR -> "BGR" ;
143
- case kGray -> "Gray" ;
144
- case kUYVY , kUnknown , kY16 , kBGRA -> "Unknown" ;
145
- };
146
- }
147
-
148
61
@ Override
149
62
public void close () {
150
- table . getEntry ( "connected" ). setBoolean ( false );
63
+ CameraServer . removeServer ( mjpegServer . getName () );
151
64
mjpegServer .close ();
152
65
cvSource .close ();
153
- listener .close ();
154
66
mjpegServer = null ;
155
67
cvSource = null ;
156
- listener = null ;
157
68
}
158
69
}
0 commit comments