99import java .net .URI ;
1010import java .net .URISyntaxException ;
1111import java .net .URL ;
12- import java .util .concurrent .Semaphore ;
1312
1413import javax .imageio .ImageIO ;
1514
4342/**
4443 * IP camera device.
4544 *
46- * @author Bartosz Firyn (SarXos )
45+ * @author Bartosz Firyn (sarxos )
4746 */
4847public class IpCamDevice implements WebcamDevice , FPSSource {
4948
@@ -58,22 +57,19 @@ private interface ImageReader extends FPSSource {
5857
5958 void halt ();
6059
61- void init ();
60+ void start ();
6261 }
6362
6463 private final class PushImageReader extends Thread implements ImageReader {
6564
6665 private final URI uri ;
6766 private volatile boolean running = true ;
68- private volatile WebcamException exception = null ;
6967 private volatile BufferedImage image = null ;
7068 private BufferedImage tmp ;
71- private final Semaphore semaphore = new Semaphore (1 );
7269 private volatile double fps = 0 ;
7370
7471 public PushImageReader (final URI uri ) {
7572 this .uri = uri ;
76- this .semaphore .drainPermits ();
7773 this .setDaemon (true );
7874 }
7975
@@ -97,7 +93,6 @@ public void run() {
9793 t1 = System .currentTimeMillis ();
9894 if ((tmp = stream .readFrame ()) != null ) {
9995 image = tmp ;
100- semaphore .release ();
10196 }
10297 t2 = System .currentTimeMillis ();
10398 fps = (double ) 1000 / (t2 - t1 + 1 );
@@ -106,33 +101,22 @@ public void run() {
106101 if (e instanceof EOFException ) { // EOF, ignore error and recreate stream
107102 continue ;
108103 }
109- exception = new WebcamException ("Cannot read MJPEG frame" , e );
104+ LOG . error ("Cannot read MJPEG frame" , e );
110105 }
111106 }
112107 }
113108
114109 @ Override
115110 public BufferedImage readImage () throws InterruptedException {
116- if (exception != null && failOnError ) {
117- throw exception ;
118- }
119- semaphore .acquire ();
120- try {
121- return image ;
122- } finally {
123- semaphore .release ();
111+ while (running && image == null ) {
112+ Thread .sleep (10 );
124113 }
114+ return image ;
125115 }
126116
127117 @ Override
128118 public void halt () {
129119 running = false ;
130- semaphore .release ();
131- }
132-
133- @ Override
134- public void init () {
135- start ();
136120 }
137121
138122 @ Override
@@ -181,7 +165,7 @@ public void halt() {
181165 }
182166
183167 @ Override
184- public void init () {
168+ public void start () {
185169 // do nothing, no need to start this one
186170 }
187171
@@ -195,9 +179,9 @@ public double getFPS() {
195179 private final URL url ;
196180 private final IpCamMode mode ;
197181 private final IpCamAuth auth ;
198- private boolean failOnError = false ;
199182
200183 private final HttpClient client ;
184+ private final HttpContext context ;
201185 private ImageReader reader ;
202186
203187 private boolean open = false ;
@@ -228,6 +212,8 @@ public IpCamDevice(String name, URL url, IpCamMode mode, IpCamAuth auth) {
228212 this .mode = mode ;
229213 this .auth = auth ;
230214 this .client = createClient ();
215+ this .context = createContext ();
216+
231217 }
232218
233219 protected static final URL toURL (String url ) {
@@ -268,7 +254,7 @@ private ImageReader createReader() {
268254 }
269255 }
270256
271- private HttpContext context () {
257+ private HttpContext createContext () {
272258
273259 final IpCamAuth auth = getAuth ();
274260
@@ -295,7 +281,7 @@ private HttpContext context() {
295281 private InputStream get (final URI uri , boolean withoutImageMime ) throws UnsupportedOperationException , IOException {
296282
297283 final HttpGet get = new HttpGet (uri );
298- final HttpResponse respone = client .execute (get , context () );
284+ final HttpResponse respone = client .execute (get , context );
299285 final HttpEntity entity = respone .getEntity ();
300286
301287 // normal jpeg return image/jpeg as opposite to mjpeg
@@ -366,14 +352,14 @@ public void setResolution(Dimension size) {
366352
367353 @ Override
368354 public synchronized BufferedImage getImage () {
369- if (open ) {
370- try {
371- return reader .readImage ();
372- } catch (InterruptedException e ) {
373- throw new WebcamException (e );
374- }
355+ if (!open ) {
356+ return null ;
357+ }
358+ try {
359+ return reader .readImage ();
360+ } catch (InterruptedException e ) {
361+ throw new WebcamException (e );
375362 }
376- return null ;
377363 }
378364
379365 /**
@@ -400,7 +386,7 @@ public void open() {
400386 if (!open ) {
401387
402388 reader = createReader ();
403- reader .init ();
389+ reader .start ();
404390
405391 try {
406392 reader .readImage ();
@@ -431,10 +417,6 @@ public IpCamAuth getAuth() {
431417 return auth ;
432418 }
433419
434- public void setFailOnError (boolean failOnError ) {
435- this .failOnError = failOnError ;
436- }
437-
438420 @ Override
439421 public void dispose () {
440422 // ignore
0 commit comments