Friday, 28 May 2010

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



    12 comments:

    1. You could just stream from RTMP to RTSP using ffmpeg's ffserver. All you need in ffserver.conf is

      ####
      Port 8090
      RTSPPort 5454
      BindAddress 0.0.0.0
      MaxHTTPConnections 1000
      MaxClients 200
      MaxBandwidth 10000
      CustomLog -

      <Stream 1.mp4>
      File "rtmp://wowzahost/live/streamLive3H.263"
      Format rtp
      </Stream>
      ####

      And then anyone can play rtsp://server:5454/1.mp4

      ReplyDelete
    2. You are right but we wanted to control the streaming in application level. RTMP streams are dynamically created with random names not statically.

      ReplyDelete
    3. we have setup the xuggler in windows 2003 and convert the stream using command line in wowza api wich works. now we have moved the server to windows 2008 32 bit OS . the command line is not working in wowza module or from command prompt also .

      its not giving any erorr also just the blank screen. do you have any idea what could be the problem?

      ReplyDelete
    4. Hello chesponal.

      I don't know what causes the problem with 2008 installation. What i would suggest is to use wireshark at port 1935 and get a dump of the rtmp conversation.Also you could check wowza logs.You could send me the logs and the dump in order to try to understand what causes the problem.

      ReplyDelete
    5. Is the string in the output URL supposed to start with "rtmp://". Shouldn't it be "rtsp://" if the purpose is to transcode from an H.263/Sorenson file on RTMP to H.264 served over RTSP?

      ReplyDelete
    6. Hello Peter,

      the output is "rtmp://wowzahost/live/streamLive3H.264"
      Wowza has the ability to publish live rtmp streams with H.264 encoding ,as rtsp streams.We perform the trancoding with xuggler because wowza doesn't do the same re-streaming to rtsp for H.263 streams.

      In this example you would be able to open the same stream at the following urls.

      "rtmp://wowzahost/live/streamLive3H.264" as rtmp
      and
      "rtsp://wowzahost/live/streamLive3H.264.sdp" as rtsp

      thanks
      pat

      ReplyDelete
    7. Hello.

      We are student and we work on a project of video conference between ipad.
      Now we try to transform the flv stream into H264 to be accept by the ipad.

      But I have this error with your code :
      Can you help me?

      Exception in thread "main" java.lang.RuntimeException: could not open output url: rtmp://localhost/oflaDemo/leflux3
      at com.xuggle.xuggler.Converter.setupStreams(Converter.java:662)
      at com.xuggle.xuggler.Converter.run(Converter.java:1183)
      at com.javacodegeeks.xuggler.Transcoder.main(Transcoder.java:30)
      2011-01-06 13:02:40,441 [main] ERROR com.xuggle.xuggler - Could not find output format (../../../../../../../csrc/com/xuggle/xuggler/Container.cpp:339)

      Thanks, Flo.

      ReplyDelete
    8. Where the heck I download a win version of ffserver? I'm crazy of looking for it. any news to bergmp at gmail com

      ReplyDelete
    9. Hai Peter,
      Check this link for ffserver for windows

      http://www.filerepair.ca/downloads/f/ff/ffserver.exe-DLL-EXE-Download-ffserver.exe.html

      ReplyDelete
    10. hello, I followed the instructions above, when running ant run-tests, got this. am I missing anything -- can anyone help?

      [exec] checking for xlC... no
      [exec] checking for C++ compiler default output file name...
      [exec] make[2]: *** [/root/downloads/xuggle-xuggler/build/native/x86_64-unknown-linux-gnu/captive/libopencore-amr/csrc/Makefile] Error 1
      [exec] make[1]: *** [all-recursive] Error 1
      [exec] make: *** [all-recursive] Error 1
      [exec] Could not configure library: "/root/downloads/xuggle-xuggler/build/native/x86_64-unknown-linux-gnu/captive/../../../../captive/libopencore-amr"; you may want to try disabling it or installing your own version
      [exec] make[2]: Leaving directory `/root/downloads/xuggle-xuggler/build/native/x86_64-unknown-linux-gnu/captive/libopencore-amr'
      [exec] make[1]: Leaving directory `/root/downloads/xuggle-xuggler/build/native/x86_64-unknown-linux-gnu/captive'

      BUILD FAILED
      /root/downloads/xuggle-xuggler/mk/buildtools/buildhelper.xml:1146: exec returned: 2

      ReplyDelete
    11.  I've installed Xuggler 3.4.   I have wowza publishing a rtsp stream.  I can open the RTSP stream with other programs, but xuggler hangs on the open call.  Any idea what could cause that?

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

      ReplyDelete
    12. 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?

      ReplyDelete

    Related Posts Plugin for WordPress, Blogger...