Display RTSP H264 stream in browser using HTML5

Issue

I need to stream an H264 stream (MPEG-4 AVC, part 10) from an RTSP container to a browser without using plugins or transcode. I have read a lot of posts and articles about this, and I believe the best solution would be to parse the RTSP and extract the h264 stream, restructure the stream (convert it to fragmented MP4), and then use websocket to send the data to the browser. I have looked into solutions like Streamedian, but I would like to avoid GStreamer or ffmpeg for this project. I need help extracting the h264 stream from RTSP in java.

Answer

You can use the open-source library called “JCodec” to extract the H264 stream from RTSP in Java. Here is an example code snippet to get you started:

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;
import org.jcodec.codecs.h264.H264Utils;
import org.jcodec.common.NIOUtils;
import org.jcodec.common.SeekableByteChannel;
import org.jcodec.common.model.Packet;
import org.jcodec.containers.mp4.demuxer.MP4Demuxer;
import org.jcodec.containers.mp4.demuxer.MP4DemuxerTrack;

public class RTSPParser {
    public static void main(String[] args) throws IOException {
        String rtspUrl = "your_rtsp_url_here";
        SeekableByteChannel channel = NIOUtils.readableChannel(rtspUrl);
        MP4Demuxer demuxer = new MP4Demuxer(channel);
        List<MP4DemuxerTrack> tracks = demuxer.getTracks();
        for (MP4DemuxerTrack track : tracks) {
            if (track.getCodec().equals("avc1")) {
                ByteBuffer data;
                while ((data = track.nextFrame()) != null) {
                    List<Packet> packets = H264Utils.splitFrame(data);
                    for (Packet packet : packets) {
                        // do something with the H264 packet data
                        System.out.println("H264 packet: " + packet);
                    }
                }
            }
        }
        channel.close();
    }
}