Skip to content

Commit

Permalink
Create http context only once instead on every get
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Nov 12, 2017
1 parent 13b0b61 commit de05c76
Showing 1 changed file with 20 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.concurrent.Semaphore;

import javax.imageio.ImageIO;

Expand Down Expand Up @@ -43,7 +42,7 @@
/**
* IP camera device.
*
* @author Bartosz Firyn (SarXos)
* @author Bartosz Firyn (sarxos)
*/
public class IpCamDevice implements WebcamDevice, FPSSource {

Expand All @@ -58,22 +57,19 @@ private interface ImageReader extends FPSSource {

void halt();

void init();
void start();
}

private final class PushImageReader extends Thread implements ImageReader {

private final URI uri;
private volatile boolean running = true;
private volatile WebcamException exception = null;
private volatile BufferedImage image = null;
private BufferedImage tmp;
private final Semaphore semaphore = new Semaphore(1);
private volatile double fps = 0;

public PushImageReader(final URI uri) {
this.uri = uri;
this.semaphore.drainPermits();
this.setDaemon(true);
}

Expand All @@ -97,7 +93,6 @@ public void run() {
t1 = System.currentTimeMillis();
if ((tmp = stream.readFrame()) != null) {
image = tmp;
semaphore.release();
}
t2 = System.currentTimeMillis();
fps = (double) 1000 / (t2 - t1 + 1);
Expand All @@ -106,33 +101,22 @@ public void run() {
if (e instanceof EOFException) { // EOF, ignore error and recreate stream
continue;
}
exception = new WebcamException("Cannot read MJPEG frame", e);
LOG.error("Cannot read MJPEG frame", e);
}
}
}

@Override
public BufferedImage readImage() throws InterruptedException {
if (exception != null && failOnError) {
throw exception;
}
semaphore.acquire();
try {
return image;
} finally {
semaphore.release();
while (running && image == null) {
Thread.sleep(10);
}
return image;
}

@Override
public void halt() {
running = false;
semaphore.release();
}

@Override
public void init() {
start();
}

@Override
Expand Down Expand Up @@ -181,7 +165,7 @@ public void halt() {
}

@Override
public void init() {
public void start() {
// do nothing, no need to start this one
}

Expand All @@ -195,9 +179,9 @@ public double getFPS() {
private final URL url;
private final IpCamMode mode;
private final IpCamAuth auth;
private boolean failOnError = false;

private final HttpClient client;
private final HttpContext context;
private ImageReader reader;

private boolean open = false;
Expand Down Expand Up @@ -228,6 +212,8 @@ public IpCamDevice(String name, URL url, IpCamMode mode, IpCamAuth auth) {
this.mode = mode;
this.auth = auth;
this.client = createClient();
this.context = createContext();

}

protected static final URL toURL(String url) {
Expand Down Expand Up @@ -268,7 +254,7 @@ private ImageReader createReader() {
}
}

private HttpContext context() {
private HttpContext createContext() {

final IpCamAuth auth = getAuth();

Expand All @@ -295,7 +281,7 @@ private HttpContext context() {
private InputStream get(final URI uri, boolean withoutImageMime) throws UnsupportedOperationException, IOException {

final HttpGet get = new HttpGet(uri);
final HttpResponse respone = client.execute(get, context());
final HttpResponse respone = client.execute(get, context);
final HttpEntity entity = respone.getEntity();

// normal jpeg return image/jpeg as opposite to mjpeg
Expand Down Expand Up @@ -366,14 +352,14 @@ public void setResolution(Dimension size) {

@Override
public synchronized BufferedImage getImage() {
if (open) {
try {
return reader.readImage();
} catch (InterruptedException e) {
throw new WebcamException(e);
}
if (!open) {
return null;
}
try {
return reader.readImage();
} catch (InterruptedException e) {
throw new WebcamException(e);
}
return null;
}

/**
Expand All @@ -400,7 +386,7 @@ public void open() {
if (!open) {

reader = createReader();
reader.init();
reader.start();

try {
reader.readImage();
Expand Down Expand Up @@ -431,10 +417,6 @@ public IpCamAuth getAuth() {
return auth;
}

public void setFailOnError(boolean failOnError) {
this.failOnError = failOnError;
}

@Override
public void dispose() {
// ignore
Expand Down

0 comments on commit de05c76

Please sign in to comment.