2
2
3
3
import java .awt .Dimension ;
4
4
import java .awt .image .BufferedImage ;
5
+ import java .io .EOFException ;
5
6
import java .io .IOException ;
6
7
import java .io .InputStream ;
7
8
import java .net .MalformedURLException ;
@@ -50,13 +51,50 @@ private final class PushImageReader implements Runnable {
50
51
private BufferedImage image = null ;
51
52
private boolean running = true ;
52
53
private WebcamException exception = null ;
54
+ private HttpGet get = null ;
55
+ private URI uri = null ;
53
56
54
- public PushImageReader (InputStream is ) {
55
- stream = new IpCamMJPEGStream (is );
57
+ public PushImageReader (URI uri ) {
58
+ this .uri = uri ;
59
+ stream = new IpCamMJPEGStream (requestStream (uri ));
60
+ }
61
+
62
+ private InputStream requestStream (URI uri ) {
63
+
64
+ BasicHttpContext context = new BasicHttpContext ();
65
+
66
+ IpCamAuth auth = getAuth ();
67
+ if (auth != null ) {
68
+ AuthCache cache = new BasicAuthCache ();
69
+ cache .put (new HttpHost (uri .getHost ()), new BasicScheme ());
70
+ context .setAttribute (ClientContext .AUTH_CACHE , cache );
71
+ }
72
+
73
+ try {
74
+ get = new HttpGet (uri );
75
+
76
+ HttpResponse respone = client .execute (get , context );
77
+ HttpEntity entity = respone .getEntity ();
78
+
79
+ Header ct = entity .getContentType ();
80
+ if (ct == null ) {
81
+ throw new WebcamException ("Content Type header is missing" );
82
+ }
83
+
84
+ if (ct .getValue ().startsWith ("image/" )) {
85
+ throw new WebcamException ("Cannot read images in PUSH mode, change mode to PULL" );
86
+ }
87
+
88
+ return entity .getContent ();
89
+
90
+ } catch (Exception e ) {
91
+ throw new WebcamException ("Cannot download image" , e );
92
+ }
56
93
}
57
94
58
95
@ Override
59
96
public void run () {
97
+
60
98
while (running ) {
61
99
62
100
if (stream .isClosed ()) {
@@ -81,9 +119,27 @@ public void run() {
81
119
// case when someone manually closed stream, do not log
82
120
// exception, this is normal behavior
83
121
if (stream .isClosed ()) {
122
+ LOG .debug ("Stream already closed, returning" );
84
123
return ;
85
124
}
86
125
126
+ if (e instanceof EOFException ) {
127
+
128
+ LOG .debug ("EOF detected, recreating MJPEG stream" );
129
+
130
+ get .releaseConnection ();
131
+
132
+ try {
133
+ stream .close ();
134
+ } catch (IOException ioe ) {
135
+ throw new WebcamException (ioe );
136
+ }
137
+
138
+ stream = new IpCamMJPEGStream (requestStream (uri ));
139
+
140
+ continue ;
141
+ }
142
+
87
143
LOG .error ("Cannot read MJPEG frame" , e );
88
144
89
145
if (failOnError ) {
@@ -238,46 +294,14 @@ private BufferedImage getImagePushMode() {
238
294
239
295
synchronized (this ) {
240
296
241
- InputStream is = null ;
242
-
243
297
URI uri = null ;
244
-
245
298
try {
246
299
uri = getURL ().toURI ();
247
300
} catch (URISyntaxException e ) {
248
301
throw new WebcamException (String .format ("Incorrect URI syntax '%s'" , uri ), e );
249
302
}
250
303
251
- BasicHttpContext context = new BasicHttpContext ();
252
-
253
- IpCamAuth auth = getAuth ();
254
- if (auth != null ) {
255
- AuthCache cache = new BasicAuthCache ();
256
- cache .put (new HttpHost (uri .getHost ()), new BasicScheme ());
257
- context .setAttribute (ClientContext .AUTH_CACHE , cache );
258
- }
259
-
260
- try {
261
- HttpGet get = new HttpGet (uri );
262
- HttpResponse respone = client .execute (get , context );
263
- HttpEntity entity = respone .getEntity ();
264
-
265
- Header ct = entity .getContentType ();
266
- if (ct == null ) {
267
- throw new WebcamException ("Content Type header is missing" );
268
- }
269
-
270
- if (ct .getValue ().startsWith ("image/" )) {
271
- throw new WebcamException ("Cannot read images in PUSH mode, change mode to PULL" );
272
- }
273
-
274
- is = entity .getContent ();
275
-
276
- } catch (Exception e ) {
277
- throw new WebcamException ("Cannot download image" , e );
278
- }
279
-
280
- pushReader = new PushImageReader (is );
304
+ pushReader = new PushImageReader (uri );
281
305
282
306
// TODO: change to executor
283
307
0 commit comments