21
21
import org .slf4j .LoggerFactory ;
22
22
23
23
24
+ /**
25
+ * This is very simple class which allows video from webcam to be exposed as MJPEG stream on a given
26
+ * port. The mapping between webcam and port is one-to-one, which means that a single port need to
27
+ * be allocated for every webcam you want to stream from.
28
+ *
29
+ * @author Bartoisz Firyn (sarxos)
30
+ */
24
31
public class WebcamStreamer implements ThreadFactory , WebcamListener {
25
32
26
33
private static final Logger LOG = LoggerFactory .getLogger (WebcamStreamer .class );
@@ -33,12 +40,9 @@ private class Acceptor implements Runnable {
33
40
34
41
@ Override
35
42
public void run () {
36
- try {
37
- ServerSocket server = new ServerSocket (port );
43
+ try (ServerSocket server = new ServerSocket (port )) {
38
44
while (started .get ()) {
39
- Socket socket = server .accept ();
40
- LOG .info ("New connection from {}" , socket .getRemoteSocketAddress ());
41
- executor .execute (new Connection (socket ));
45
+ executor .execute (new Connection (server .accept ()));
42
46
}
43
47
} catch (Exception e ) {
44
48
LOG .error ("Cannot accept socket connection" , e );
@@ -57,6 +61,8 @@ public Connection(Socket socket) {
57
61
@ Override
58
62
public void run () {
59
63
64
+ LOG .info ("New connection from {}" , socket .getRemoteSocketAddress ());
65
+
60
66
BufferedReader br = null ;
61
67
BufferedOutputStream bos = null ;
62
68
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
@@ -135,9 +141,19 @@ public void run() {
135
141
bos .write (CRLF .getBytes ());
136
142
bos .flush ();
137
143
} catch (SocketException e ) {
138
- LOG .error ("Socket exception from " + socket .getRemoteSocketAddress (), e );
144
+
145
+ if (!socket .isConnected ()) {
146
+ LOG .debug ("Connection to client has been lost" );
147
+ }
148
+ if (socket .isClosed ()) {
149
+ LOG .debug ("Connection to client is closed" );
150
+ }
151
+
139
152
br .close ();
140
153
bos .close ();
154
+
155
+ LOG .debug ("Socket exception from " + socket .getRemoteSocketAddress (), e );
156
+
141
157
return ;
142
158
}
143
159
0 commit comments