Páginas

viernes, 5 de julio de 2013

Java RTMP Client

Let’s face it, sometimes web developers can lose track of how difficult some tasks are or, in other words, how browsers save us lots of troubles.

As an example, I bring today one of our latests requirements:

We had to develop two applications, one mobile app to publish video and audio in realtime and a java video player which plays that stream.

At first sight, it can’t seem too difficult; we do have strong knowledge in real time multimedia applications but as always, constraints made it way more difficult: It was mandatory to use RTMP protocol because the stream playback had to be real time

The android application used to send the video was already done (at least it’s core technology) so in order to use it, it was necessary to implement the RTMP protocol in java in order to receive the published streams, and once we had that, decode them (H264 video and SPEEX audio) for its playback.

So conceptually the problem for the java player was reduced to this modules:

To achieve that, we went through 2 different approaches, the first one was use RTMPDUMP and vlcj:

RTMPDUMP is a console-based application that implements RTMP protocol and is able to subscribe to one stream and write it in a file or pipeline it to another process.

VLCJ: is a powerful library that creates an instance of VLC player, which would play the recorded stream from RTMPDUMP.

Although the solution worked, there were many drawbacks, such as latency that made that solution not possible (VLCJ had no support for InputStreams (so no pipelining) and above this, there was a buffer problem: If vlc video buffer reached the end of the file (because it get filled quicker than RTMPDUMP file-write speed) the video will only play until that point.

When I found that library and saw it had built-in rtmp support, I knew there were lots of chances to work all our problems, and so it was.

Xuggler takes care of all, rtmp communication and decoding, making quite easy and fast (least than 250 lines) to have a stream decoded and played.

Finally, a full scheme with all the players related into the application and their functions:

As I started this post, I had never realized the amount of work there is under an html <video> tag; it is able to open a stream, find the correct codec, find its size, scale it to the selected size and play it for us. That’s great!