views:

2899

answers:

2

I'm writing a Java client application to receive live M-JPEG video from an IP camera. The video is sent by the camera as an endless multipart HTTP message where each part is a single JPEG frame. I need to process each of these frames as they arrive, so I'm hoping there's a way to make an HTTP request that asynchronously triggers an event as each message part/video frame is received.

Is anyone aware of any libraries that can do this? All the examples I can find on Google won't work because they use blocking calls that only parse the response and break it up into parts after the entire response has finished being received (which obviously won't work for an endless response).

I realise I could manually break up the data into parts as it arrives by searching for the message boundary but it just feels like I would be reinventing the wheel.

+1  A: 

Try HttpClient from Apache Commons. The source code has a couple classes that show how to read in a multipart in a stream fashion.

Josh
I looked at the documentation for this library and it looks like it's only for parsing multipart HTTP requests, not responses. According to this thread it can't be used to parse multipart responses: http://markmail.org/message/l6t46xzmikkbxo2e. I'm looking into Mime4j which they mention in the link.
+1  A: 

This project: http://fmj-sf.net, does have a class to parse multipart/x-mixed-replace responses: http://fmj-sf.net/doc/fmj/net/sf/fmj/media/parser/MultipartMixedReplaceParser.html

Searching google code with: multipart/x-mixed-replace lang:java

I found some other examples like: http://www.google.com/codesearch?as_q=multipart%2Fx-mixed-replace&btnG=Search+Code&hl=en&as_lang=java&as_license_restrict=i&as_license=&as_package=&as_filename=&as_case=

http://www.google.com/codesearch/p?hl=en#FCmBlvKk1MA/cambozola-0.50/src/com/charliemouse/cambozola/shared/CamStream.java&q=multipart/x-mixed-replace%20lang:java

http://www.google.com/codesearch/p?hl=en#Xnnd-VJLMBY/src/Grabber.java&q=multipart/x-mixed-replace%20lang:java

Loki
Thanks- Cambozola looks to be exactly what I'm trying to do, I'll look some more at their code. I was hoping to use a proper HTTP library rather than custom code since it's all standard HTTP but looks like there's not much available for what I want to do.
Seems like HTTP push is only used in webcams and some experiments. The code is also not that complicated. I would only say make sure that connection failures are handled correctly and don't assume that all the information will arrive correctly. Any browser api should handle that correctly too.
Loki