Skip to content

Commit

Permalink
Add few assertions in the code, minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Mar 20, 2013
1 parent 1dd0c96 commit a80b180
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 10 deletions.
@@ -0,0 +1,37 @@
package com.github.sarxos.webcam.example;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.log.WebcamLogConfigurator;


public class CalculateFPSExample {

public static void main(String[] args) {

WebcamLogConfigurator.configure("src/example/resources/logback.xml");

long t1 = 0;
long t2 = 0;

int p = 10;
int r = 5;

Webcam webcam = Webcam.getDefault();

for (int k = 0; k < p; k++) {

webcam.open();
webcam.getImage();

t1 = System.currentTimeMillis();
for (int i = 0; ++i <= r; webcam.getImage()) {
}
t2 = System.currentTimeMillis();

System.out.println("FPS " + k + ": " + (1000 * r / (t2 - t1 + 1)));

webcam.close();
}

}
}
2 changes: 1 addition & 1 deletion webcam-capture/src/example/resources/logback.xml
Expand Up @@ -4,7 +4,7 @@
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<root level="warn">
<root level="error">
<appender-ref ref="STDOUT" />
</root>
</configuration>
69 changes: 63 additions & 6 deletions webcam-capture/src/main/java/com/github/sarxos/webcam/Webcam.java
Expand Up @@ -165,6 +165,8 @@ public boolean open(boolean async) {

if (open.compareAndSet(false, true)) {

assert updater != null;

WebcamOpenTask task = new WebcamOpenTask(driver, device);
try {
task.open();
Expand Down Expand Up @@ -216,6 +218,8 @@ public boolean close() {

if (open.compareAndSet(true, false)) {

assert updater != null;

// close webcam

WebcamCloseTask task = new WebcamCloseTask(driver, device);
Expand Down Expand Up @@ -293,6 +297,7 @@ public Dimension[] getViewSizes() {
* @param sizes the array of custom resolutions to be supported by webcam
*/
public void setCustomViewSizes(Dimension[] sizes) {
assert customSizes != null;
if (sizes == null) {
customSizes.clear();
return;
Expand All @@ -301,6 +306,7 @@ public void setCustomViewSizes(Dimension[] sizes) {
}

public Dimension[] getCustomViewSizes() {
assert customSizes != null;
return customSizes.toArray(new Dimension[customSizes.size()]);
}

Expand Down Expand Up @@ -334,6 +340,9 @@ public void setViewSize(Dimension size) {
Dimension[] predefined = getViewSizes();
Dimension[] custom = getCustomViewSizes();

assert predefined != null;
assert custom != null;

boolean ok = false;
for (Dimension d : predefined) {
if (d.width == size.width && d.height == size.height) {
Expand Down Expand Up @@ -394,6 +403,8 @@ public BufferedImage getImage() {
long t1 = 0;
long t2 = 0;

assert updater != null;

if (asynchronous) {
return updater.getImage();
} else {
Expand Down Expand Up @@ -422,13 +433,15 @@ public BufferedImage getImage() {
}

public boolean isImageNew() {
assert updater != null;
if (asynchronous) {
return updater.isImageNew();
}
return true;
}

protected double getFPS() {
assert updater != null;
if (asynchronous) {
return updater.getFPS();
} else {
Expand Down Expand Up @@ -456,14 +469,22 @@ public ByteBuffer getImageBytes() {
return null;
}

assert driver != null;
assert device != null;

// some devices can support direct image buffers, and for those call
// processor task, and for those which does not support direct image
// buffers, just convert image to RGB byte array

if (device instanceof BufferAccess) {
return new WebcamReadBufferTask(driver, device).getBuffer();
} else {
return ByteBuffer.wrap(ImageUtils.toRawByteArray(getImage()));
BufferedImage image = getImage();
if (image != null) {
return ByteBuffer.wrap(ImageUtils.toRawByteArray(image));
} else {
return null;
}
}
}

Expand All @@ -474,6 +495,9 @@ public ByteBuffer getImageBytes() {
*/
private boolean isReady() {

assert disposed != null;
assert open != null;

if (disposed.get()) {
LOG.warn("Cannot get image, webcam has been already disposed");
return false;
Expand All @@ -495,7 +519,7 @@ private boolean isReady() {
* interval for webcam devices to be discovered. By default this time is set
* to 1 minute.
*
* @return List of webcams existing in the ssytem
* @return List of webcams existing in the system
* @throws WebcamException when something is wrong
* @see Webcam#getWebcams(long, TimeUnit)
*/
Expand All @@ -518,9 +542,13 @@ public static List<Webcam> getWebcams() throws WebcamException {
* @param timeout the time to wait for webcam devices to be discovered
* @return List of webcams existing in the ssytem
* @throws WebcamException when something is wrong
* @throws IllegalArgumentException when timeout is negative
* @see Webcam#getWebcams(long, TimeUnit)
*/
public static List<Webcam> getWebcams(long timeout) throws TimeoutException, WebcamException {
if (timeout < 0) {
throw new IllegalArgumentException(String.format("Timeout cannot be negative (%d)", timeout));
}
return getWebcams(timeout, TimeUnit.MILLISECONDS);
}

Expand All @@ -533,12 +561,22 @@ public static List<Webcam> getWebcams(long timeout) throws TimeoutException, Web
* @return List of webcams
* @throws TimeoutException when timeout has been exceeded
* @throws WebcamException when something is wrong
* @throws IllegalArgumentException when timeout is negative or tunit null
*/
public static synchronized List<Webcam> getWebcams(long timeout, TimeUnit tunit) throws TimeoutException, WebcamException {

if (timeout < 0) {
throw new IllegalArgumentException(String.format("Timeout cannot be negative (%d)", timeout));
}
if (tunit == null) {
throw new IllegalArgumentException("Time unit cannot be null!");
}

WebcamDiscoveryService discovery = getDiscoveryService();
List<Webcam> webcams = discovery.getWebcams(timeout, tunit);

assert discovery != null;

List<Webcam> webcams = discovery.getWebcams(timeout, tunit);
if (!discovery.isRunning()) {
discovery.start();
}
Expand Down Expand Up @@ -571,9 +609,13 @@ public static Webcam getDefault() throws WebcamException {
* @return Default webcam (first from the list)
* @throws TimeoutException when discovery timeout has been exceeded
* @throws WebcamException if something is really wrong
* @throws IllegalArgumentException when timeout is negative
* @see Webcam#getWebcams(long)
*/
public static Webcam getDefault(long timeout) throws TimeoutException, WebcamException {
if (timeout < 0) {
throw new IllegalArgumentException(String.format("Timeout cannot be negative (%d)", timeout));
}
return getDefault(timeout, TimeUnit.MILLISECONDS);
}

Expand All @@ -585,19 +627,22 @@ public static Webcam getDefault(long timeout) throws TimeoutException, WebcamExc
* @return Default webcam (first from the list)
* @throws TimeoutException when discovery timeout has been exceeded
* @throws WebcamException if something is really wrong
* @throws IllegalArgumentException when timeout is negative or tunit null
* @see Webcam#getWebcams(long, TimeUnit)
*/
public static Webcam getDefault(long timeout, TimeUnit tunit) throws TimeoutException, WebcamException {

if (timeout < 0) {
throw new IllegalArgumentException("Timeout cannot be negative");
throw new IllegalArgumentException(String.format("Timeout cannot be negative (%d)", timeout));
}
if (tunit == null) {
throw new IllegalArgumentException("Time unit cannot be null!");
}

List<Webcam> webcams = getWebcams(timeout, tunit);

assert webcams != null;

if (!webcams.isEmpty()) {
return webcams.get(0);
}
Expand All @@ -615,6 +660,7 @@ public static Webcam getDefault(long timeout, TimeUnit tunit) throws TimeoutExce
* @return Name
*/
public String getName() {
assert device != null;
return device.getName();
}

Expand All @@ -633,20 +679,23 @@ public boolean addWebcamListener(WebcamListener l) {
if (l == null) {
throw new IllegalArgumentException("Webcam listener cannot be null!");
}
assert listeners != null;
return listeners.add(l);
}

/**
* @return All webcam listeners
*/
public WebcamListener[] getWebcamListeners() {
assert listeners != null;
return listeners.toArray(new WebcamListener[listeners.size()]);
}

/**
* @return Number of webcam listeners
*/
public int getWebcamListenersCount() {
assert listeners != null;
return listeners.size();
}

Expand All @@ -657,6 +706,7 @@ public int getWebcamListenersCount() {
* @return True if listener has been removed, false otherwise
*/
public boolean removeWebcamListener(WebcamListener l) {
assert listeners != null;
return listeners.remove(l);
}

Expand Down Expand Up @@ -713,12 +763,12 @@ public static synchronized void setDriver(WebcamDriver driver) {
*/
public static synchronized void setDriver(Class<? extends WebcamDriver> driverClass) {

resetDriver();

if (driverClass == null) {
throw new IllegalArgumentException("Webcam driver class cannot be null!");
}

resetDriver();

try {
driver = driverClass.newInstance();
} catch (InstantiationException e) {
Expand Down Expand Up @@ -780,6 +830,7 @@ public static void registerDriver(String clazzName) {
* @return Underlying webcam device instance
*/
public WebcamDevice getDevice() {
assert device != null;
return device;
}

Expand All @@ -789,6 +840,12 @@ public WebcamDevice getDevice() {
*/
protected void dispose() {

assert disposed != null;
assert open != null;
assert driver != null;
assert device != null;
assert listeners != null;

if (!disposed.compareAndSet(false, true)) {
return;
}
Expand Down
@@ -1,5 +1,6 @@
package com.github.sarxos.webcam;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
Expand All @@ -18,11 +19,11 @@
import org.slf4j.LoggerFactory;


public class WebcamDiscoveryService implements Runnable {
public class WebcamDiscoveryService implements Runnable, UncaughtExceptionHandler {

private static final Logger LOG = LoggerFactory.getLogger(WebcamDiscoveryService.class);

private static final class WebcamsDiscovery implements Callable<List<Webcam>>, ThreadFactory {
private static final class WebcamsDiscovery implements Callable<List<Webcam>>, ThreadFactory, UncaughtExceptionHandler {

private final WebcamDriver driver;

Expand All @@ -39,8 +40,14 @@ public List<Webcam> call() throws Exception {
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "webcam-discovery-service");
t.setDaemon(true);
t.setUncaughtExceptionHandler(this);
return t;
}

@Override
public void uncaughtException(Thread t, Throwable e) {
LOG.error(String.format("Exception in thread %s", t.getName()), e);
}
}

private final WebcamDriver driver;
Expand All @@ -53,6 +60,11 @@ public Thread newThread(Runnable r) {
private Thread runner = null;

protected WebcamDiscoveryService(WebcamDriver driver) {

if (driver == null) {
throw new IllegalArgumentException("Driver cannot be null!");
}

this.driver = driver;
this.support = (WebcamDiscoverySupport) (driver instanceof WebcamDiscoverySupport ? driver : null);
}
Expand Down Expand Up @@ -320,6 +332,7 @@ public synchronized void start() {

runner = new Thread(this, "webcam-discovery-service");
runner.setDaemon(true);
runner.setUncaughtExceptionHandler(this);
runner.start();
}

Expand Down Expand Up @@ -353,4 +366,9 @@ protected synchronized void shutdown() {
WebcamDeallocator.unstore();
}
}

@Override
public void uncaughtException(Thread t, Throwable e) {
LOG.error(String.format("Exception in thread %s", t.getName()), e);
}
}
Expand Up @@ -66,6 +66,9 @@ public class DefaultPainter implements Painter {
@Override
public void paintPanel(WebcamPanel owner, Graphics2D g2) {

assert owner != null;
assert g2 != null;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setBackground(Color.BLACK);
g2.fillRect(0, 0, getWidth(), getHeight());
Expand Down

0 comments on commit a80b180

Please sign in to comment.