2121import org .slf4j .LoggerFactory ;
2222
2323
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+ */
2431public class WebcamStreamer implements ThreadFactory , WebcamListener {
2532
2633 private static final Logger LOG = LoggerFactory .getLogger (WebcamStreamer .class );
@@ -33,12 +40,9 @@ private class Acceptor implements Runnable {
3340
3441 @ Override
3542 public void run () {
36- try {
37- ServerSocket server = new ServerSocket (port );
43+ try (ServerSocket server = new ServerSocket (port )) {
3844 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 ()));
4246 }
4347 } catch (Exception e ) {
4448 LOG .error ("Cannot accept socket connection" , e );
@@ -57,6 +61,8 @@ public Connection(Socket socket) {
5761 @ Override
5862 public void run () {
5963
64+ LOG .info ("New connection from {}" , socket .getRemoteSocketAddress ());
65+
6066 BufferedReader br = null ;
6167 BufferedOutputStream bos = null ;
6268 ByteArrayOutputStream baos = new ByteArrayOutputStream ();
@@ -135,9 +141,19 @@ public void run() {
135141 bos .write (CRLF .getBytes ());
136142 bos .flush ();
137143 } 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+
139152 br .close ();
140153 bos .close ();
154+
155+ LOG .debug ("Socket exception from " + socket .getRemoteSocketAddress (), e );
156+
141157 return ;
142158 }
143159
0 commit comments