Desktop Java

RTMP To RTSP re-stream using wowza and xuggler

Note: This is part of our “Xuggler Development Tutorials” series.

Hello to everyone!

    For the last three months we have been working on a teleconference project. We decided that a web based application, using technologies like Flex would be the best approach for such a demanding project. As the complexity of the software and the demands of telecommunication providers increased, the challenges we faced were noteworthy. One challenge was a request to feed live video from an agent that is utilizing our software to their RTSP server. The problem we faced is that Flex uses RTMP protocol in video and audio transmission and we needed something fast to do the re-streaming without re-inventing the wheel.

    In this article we will try to give enough information to deal with a complex problem, RTMP to RTSP re-streaming for a live stream. Our solution is based on wowza streaming server that supports both RTMP and RTSP and xuggler that brings the power of FFMPEG in java applications.

    Wowza supports RTMP to RTSP re-streaming for H.264 live streams but not for H.263. By default the video captured from ActionScript Camera object is H.263 encoded. We will provide the code based on xuggler that does the transcoding of H.263 to H.264 in real time and publish the H.264 stream to Wowza.

    First of all we need to set up xuggler. We need the latest version of FFMPEG and H.264 so it is necessary to checkout xuggler from it’s svn repository.

    svn checkout http://xuggle.googlecode.com/svn/trunk/java/xuggle-xuggler xuggle-xuggler
    
    

    Then we have to set up some environmental parameters.

    Linux

    export XUGGLE_HOME=/usr/local/xuggler
    export PATH=$XUGGLE_HOME/bin:$PATH
    export LD_LIBRARY_PATH=$XUGGLE_HOME/lib:$LD_LIBRARY_PATH
    

    MAC

    export XUGGLE_HOME=/usr/local/xuggler
    export PATH=$XUGGLE_HOME/bin:$PATH
    export DYLD_LIBRARY_PATH=$XUGGLE_HOME/lib:$DYLD_LIBRARY_PATH
    

    Windows

    • Set XUGGLE_HOME to a directory of your choice (make sure the directory exists on disk). When building, you must make sure that your XUGGLE_HOME is on your C: drive, and is in a path with no spaces in it. Sorry. This restriction does not apply if you use the installer.
    • Add %XUGGLE_HOME%\bin; to the start of your PATH environment variable.
    • Optionally, set %XUGGLE_REPO% to a directory where you want ant to install published ivy jars (if you don’t set it, it defaults to dist/ivy in your build directory).

    Then we have to build and install xuggler.

    ant run-tests
    sudo ant install

    Now you have xuggler installed at $XUGGLE_HOME

    After completing with the installation we have to write the code that does the trick.

    package com.javacodegeeks.xuggler;
    
    import org.apache.commons.cli.CommandLine;
    import org.apache.commons.cli.Options;
    import org.apache.commons.cli.ParseException;
    
    import com.xuggle.xuggler.Converter;
    
    public class Transcoder {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    
            String inputStream = "rtmp://wowzahost/live/streamLive3H.263";
            String outputStream = "rtmp://wowzahost/live/streamLive3H.264";
    
            String[] parameters = new String[] { "--acodec", "libfaac", "--vcodec",
                    "libx264", "--vpreset",
                    "/usr/local/xuggler/share/ffmpeg/libx264-ultrafast.ffpreset",
                    inputStream, outputStream };
    
            Converter converter = new Converter();
    
            Options options = converter.defineOptions();
    
            CommandLine cmdLine;
            try {
                cmdLine = converter.parseOptions(options, parameters);
                converter.run(cmdLine);
                System.out.println("Finish!!!");
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
    }
    

    Points to notice.

    • At the directory $XUGGLE_HOME/share/ffmpeg you will find all the avail ables presets for x264.Better quality means more delay. You will have to choose what is suitable for your needs.
    • You will not have to change acodec and vcodec
    • The latest version of FFMPEG can read and write RTMP Streams.

    The example project is available here.

    Thank you for your time. Hope you’ll find this article interesting.

    Best Regards
    ./pat

    Panagiotis Paterakis

    Panagiotis is an open source enthusiast. In the morning he works as a freelancer implementing a wide variety of applications/services for the telecom industry (teleconference, NGIN, SOA). As a night job he craves to contribute to several open source projects. His latest activity is a forthcoming open source software that will bring the non trivial applications closer to the cloud
    Subscribe
    Notify of
    guest

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    4 Comments
    Oldest
    Newest Most Voted
    Inline Feedbacks
    View all comments
    David Peterson
    David Peterson
    11 years ago

    I’ve installed Xuggler 3.4.   I have wowza publishing a rtmp stream (based upon an original ffmpeg stream).  I can open the RTMP stream with other programs, but xuggler hangs on the open call.  Any idea what could cause that?

    inputUrl: rtmp://localhost/live/4/1/2000_720.stream

    This call hangs:
    retval = mIContainer.open(inputURL, IContainer.Type.READ, iFmt);

    Tried changing it to this, but it also hangs.:

    retval = mIContainer.open(inputURL, IContainer.Type.READ, iFmt, true, false);

    David Peterson
    David Peterson
    11 years ago

    Sorry, but my post seemed to get lost so I am re-posting.  I am running Xuggler 3.4.  I can open a stream broadast from ffmpeg->Wowza 3 with other applications:  rtmp://localhost/live/4/1/2000_720.stream

    However, opening with Xuggler hangs on the IContainer.open.
    This:  mIContainer.open(inputURL, IContainer.Type.READ, iFmt, true, false);

    and this:  mIContainer.open(inputURL, IContainer.Type.READ, iFmt);

    Anyone have an idea as to why it would hang?

    Resolved. Ran into this defect: http://code.google.com/p/xuggle/issues/detail?id=284

    Ole
    Ole
    9 years ago

    cant you the exact same thing with command line ffmpeg, instead of using it in a java application?

    Sadh
    Sadh
    8 years ago

    Hi
    I am working on video streaming using webcamera. I am successfully getting the stream from camera using the xuggler library.
    How can i push this stream to the wowza server.

    Back to top button