sarxos / webcam-capture

The goal of this project is to allow integrated or USB-connected webcams to be accessed directly from Java. Using provided libraries users are able to read camera images and detect motion. Main project consist of several sub projects - the root one, which contains required classes, build-in webcam driver compatible with Windows, Linux and Mac OS, which can stream images as fast as your camera can serve them (up to 50 FPS). Main project can be used standalone, but user is able to replace build-in driver with different one - such as OpenIMAJ, GStreamer, V4L4j, JMF, LTI-CIVIL, FMJ, etc.
http://webcam-capture.sarxos.pl
MIT License
2.28k stars 1.11k forks source link

How to handle exceptions in IpCamDevice threads #922

Open lipinskikuba opened 10 months ago

lipinskikuba commented 10 months ago

Hi, first of all I would like to say that I am very impressed with your work. This library is very powerful, yet simple and clean, with state of art examples.

I'm using it capture images from webcam. I have problem handling following exception in PushImageReader. It there is problem with connection to camera then after Webcam.open() the following uncaught exception is thrown:

com.github.sarxos.webcam.WebcamException: Cannot download image. Connect to 192.168.0.101:4747 [/192.168.0.101] failed: Połączenie odrzucone
    at com.github.sarxos.webcam.ds.ipcam.IpCamDevice$PushImageReader.request(IpCamDevice.java:83)
    at com.github.sarxos.webcam.ds.ipcam.IpCamDevice$PushImageReader.run(IpCamDevice.java:94)
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to 192.168.0.101:4747 [/192.168.0.101] failed: Połączenie odrzucone
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:158)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
    at com.github.sarxos.webcam.ds.ipcam.IpCamDevice.get(IpCamDevice.java:287)
    at com.github.sarxos.webcam.ds.ipcam.IpCamDevice.access$0(IpCamDevice.java:284)
    at com.github.sarxos.webcam.ds.ipcam.IpCamDevice$PushImageReader.request(IpCamDevice.java:81)
    ... 1 more
Caused by: java.net.ConnectException: Połączenie odrzucone
    at java.base/sun.nio.ch.Net.connect0(Native Method)
    at java.base/sun.nio.ch.Net.connect(Net.java:579)
    at java.base/sun.nio.ch.Net.connect(Net.java:568)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:576)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
    at java.base/java.net.Socket.connect(Socket.java:666)
    at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:75)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
    ... 13 more

and the whole thread is locked indefinitely on AtomicProcessor.process() line 70 outbound.take()

Is there a build in way to properly handle this kind of exceptions?