Skip to content

Commit

Permalink
SocketException: Broken pipe when using WebcamStreamer
Browse files Browse the repository at this point in the history
fixes #19
  • Loading branch information
sarxos committed Jan 11, 2013
1 parent 1583f66 commit 35e1f5b
Showing 1 changed file with 16 additions and 1 deletion.
Expand Up @@ -8,6 +8,7 @@
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
Expand Down Expand Up @@ -123,6 +124,13 @@ public void run() {

do {

if (socket.isInputShutdown()) {
break;
}
if (socket.isClosed()) {
break;
}

baos.reset();

ImageIO.write(getImage(), "JPG", baos);
Expand All @@ -136,7 +144,14 @@ public void run() {
bos.write(sb.toString().getBytes());
bos.write(baos.toByteArray());
bos.write(CRLF.getBytes());
bos.flush();

try {
bos.flush();
} catch (SocketException e) {
if (!socket.isClosed()) {
throw e;
}
}

Thread.sleep(getDelay());

Expand Down

4 comments on commit 35e1f5b

@He-Pin
Copy link

@He-Pin He-Pin commented on 35e1f5b Jan 15, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could use netty....and i don't think steam the original JPG data is that good.anyway,i some use case that's enough

@sarxos
Copy link
Owner Author

@sarxos sarxos commented on 35e1f5b Jan 15, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about Jetty, but didn't want to add more 3rd party dependencies. Agree that MJPEG stream is not that good, for sure not able to stream high FPS rate images, but as you said - in some cases it's enough. Maybe in 0.3.8 I will create additional interface for WebcamStreamer so one will just run it and register own encoder class which will stream buffered images in 3rd party format. But this is future, I didn't think about it yet.

@decebals
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a MjpegServlet? You can use it on any servlet container. Netty/Mina is a special case (async, good for the scenario with many clients) but I don't know about the complexity on development part. You can see in here [1] a snippet code for a mjpeg streamer with netty.

[1] http://blog.maurus.be/index.php/2012/05/netty-mjpeg-streamer/

@sarxos
Copy link
Owner Author

@sarxos sarxos commented on 35e1f5b Jan 15, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@decebals

This is also wonderful idea, but as I said before - I would rather avoid adding more dependencies to Webcam Capture API core because it's good to keep small output binaries size, I think (e.g. to use in your Raspberry Pi, or Arduino in future). However, if anyone create some good example of how to stream MJPEG with Netty / Jetty, I will be really happy to include it in examples. All good source codes are welcome :D

Please sign in to comment.