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

    Do you want to know how to develop your skillset to become a Java Rockstar?

    Subscribe to our newsletter to start Rocking right now!

    To get you started we give you two of our best selling eBooks for FREE!

    JPA Mini Book

    Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

    JVM Troubleshooting Guide

    The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

    Given email address is already subscribed, thank you!
    Oops. Something went wrong. Please try again later.
    Please provide a valid email address.
    Thank you, your sign-up request was successful! Please check your e-mail inbox.
    Please complete the CAPTCHA.
    Please fill in the required fields.

    3 Responses to "RTMP To RTSP re-stream using wowza and xuggler"

    1. David Peterson says:

      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);

    2. David Peterson says:

      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

    3. Ole says:

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

    Leave a Reply


    − 1 = three



    Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
    All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
    Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
    Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
    Do you want to know how to develop your skillset and become a ...
    Java Rockstar?

    Subscribe to our newsletter to start Rocking right now!

    To get you started we give you two of our best selling eBooks for FREE!

    Get ready to Rock!
    You can download the complementary eBooks using the links below:
    Close