Play H.264 Encoded Stream in JavaFX


Play H.264 Encoded Stream in JavaFX



I have a Raspberry Pi getting video data from raspivid and piping that to netcat, which sends it to my PC where I can read each frame like this:


raspivid


netcat


ServerSocketChannel serverChannel = ServerSocketChannel.open();
serverChannel.socket().bind(new
InetSocketAddress(Main.config.getInt("VIDEO_STREAM_PORT")));
serverChannel.configureBlocking(true);
SocketChannel channel = serverChannel.accept();
InetSocketAddress remoteAddress = (InetSocketAddress)
channel.getRemoteAddress();
int BUFFER_SIZE = 2 << 16 - 1;
ByteBuffer buff = ByteBuffer.allocate(BUFFER_SIZE);
while (channel.read(buff) != -1) {
buff.flip();
if (buff.hasRemaining()) {
buff.compact();
} else {
buff.clear();
}
}



What is the optimal way of playing these frames as video in JavaFX? Is there any way to do it besides decoding each frame as a BufferedImage and rendering that? Or is there another approach altogether that I'm missing?



On Linux you could do what I'm looking for just by using netcat | mplayer but I'd like to do this in a JavaFX app.


netcat | mplayer





As I know Swing supports neither video nor sound capabilities. But JavaFX has a native support for this. See this tutorial. You can embed a JavaFX element into your Swing application using JFXPanel.
– Sergiy Medvynskyy
Jul 1 at 7:10






@SergiyMedvynskyy I actually am using JavaFX, but it's late at night and somehow I got the two confused. My apologies.
– htmlhigh5
Jul 1 at 7:32





JavaFX has media support in the javafx.scene.media package. The Javadoc of the package indicates that H.264 encoding is supported. You'll want to use a Media. Unfortunately, Media doesn't support arbitrary input. It takes a String of a URI that points to the media source and creates its own connection (based on protocol). If you're tied to using a SocketChannel then I'm not sure how you'd stream it (if it's even possible).
– Slaw
Jul 1 at 23:01



javafx.scene.media


Media


Media


String


SocketChannel









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API