Get rad!

For an upcoming project we needed to stream content from the user's webcam to be recorded on the server. In theory this sounded like a basic operation, having used FMS to this end before, but actually installing and setting up the Red5 server proved to be a little tricky. So I just wanted to outline the process I went through to get a Red5 Server running on ubuntu and how to setup your first recording stream.

Red5 is a powerful Java based video streaming and multi-user solution for the Flash Player. Red5 includes support for the latest multi-user API’s including NetConnection, NetStream and SharedObject’s while providing a powerful RTMP / Servlet implementation. It is basically an open source Flash Media Server.

We're going to go through two major steps here, firstly installing Red5 on Ubuntu and then building a simple application to record the webcam.

Installing Red5

For this installation we're going to use a stock Ubuntu Server installation (8.04 Hardy Heron). We installed LAMP as well for the project but it's not required for Red5. Firstly we need to install a few dependencies including the Java runtime environment and the Java development kit (and yes these are terminal commands so get it up and running):

sudo apt-get install java-package sun-java6-jdk sun-java6-jre
sudo apt-get install subversion ant

Next we need to grab the code base for Red5. We tried the trunk version first but had some issues with it (as you'd expect from a HEAD revision) so ended up using v0.9.1. You can try other versions of the server by browsing the repository and replacing the reference in the svn checkout below:

svn co http://red5.googlecode.com/svn/java/server/tags/0_9_1/ ~/red5

Now we build using Ant:

export JAVA_HOME=/usr/lib/jvm/java-6-sun
export ANT_HOME=/usr/share/ant/
cd ~/red5
/usr/share/ant/bin/ant

Make sure there were no errors during the build. If there were consult the red5 wiki for help. Install:

sudo mkdir /usr/share/red5
sudo cp -R ~/red5/dist/* /usr/share/red5/.
sudo chmod 755 /usr/share/red5/red5.sh

Start the Red5 server:

export RED5_HOME=/usr/share/red5
/usr/share/red5/red5.sh

Next we need to create the startup script. This script will allow you to get the server starting on reboot etc. Put the following text into the file /etc/init.d/red5 and make the file executable sudo chmod 755 /etc/init.d/red5.

  1. #!/bin/sh
  2. # init script for Red5
  3. # /etc/init.d/red5
  4.  
  5. RED5_USER=root
  6. RED5_HOME=/usr/share/red5
  7. RED5_PROG=red5
  8.  
  9. test -x $RED5_HOME/$RED5_PROG.sh || exit 5
  10.  
  11. case "$1" in
  12. start)
  13. echo -n "Starting Red5"
  14. echo -n " "
  15. cd $RED5_HOME
  16. su -s /bin/bash -c "$RED5_HOME/$RED5_PROG.sh &" $RED5_USER
  17. sleep 2
  18. ;;
  19. stop)
  20. echo -n "Shutting down Red5"
  21. echo -n " "
  22. su -s /bin/bash -c "killall -q -u $RED5_USER java" $RED5_USER
  23. sleep 2
  24. ;;
  25. restart)
  26. $0 stop
  27. $0 start
  28. ;;
  29. esac



Now you can start/stop/restart from the command line by using:
sudo /etc/init.d/red5 [start/stop/restart]

Lastly once you've got the server running, you can browse to the test page on your server http://localhost:5080 and you should see:

If you can't see this it and you've confirmed the Red5 server is running it could be a firewall issue. You may need to alter the IPTables on your machine but that's not going to be covered here.

Lastly we need to install the actual streaming application on our Red5 Server. Navigate to http://localhost:5080/installer/, select the oflaDemo and click Install.
To test it installed correctly, navigate to http://localhost:5080/demos/ofla_demo.html (correct the server name in the top right) and hit connect. You should see a list of available videos and a green light in the top right.

Congratulations you're all up and running with a Red5 Server!




Sample Application

The following class shows a very simple example of starting a streaming feed to the server and then streaming the same data back from the server into a Video object on the stage.

The fundamentals are very simple:

  • Create a NetConnection object and connect to the server
  • Create a NetStream when the connection succeeds and publish the camera feed to the stream as a "record" type
  1. package
  2. {
  3. import flash.display.Sprite;
  4.  
  5. import flash.net.NetConnection;
  6. import flash.net.NetStream;
  7. import flash.events.NetStatusEvent;
  8.  
  9. import flash.media.Camera;
  10. import flash.media.Video;
  11.  
  12. public class Red5Experiment extends Sprite
  13. {
  14. //
  15. // Constants
  16. public static const RED5_URL : String = "rtmp://SERVERNAME/oflaDemo/test/";
  17.  
  18. //
  19. // Variables
  20. private var _netConnection : NetConnection;
  21. private var _netStream : NetStream;
  22. private var _recvStream : NetStream;
  23.  
  24. private var _url : String = RED5_URL;
  25. private var _filename : String = "test-red5";
  26.  
  27. private var _video : Video;
  28.  
  29.  
  30. public function Red5Experiment()
  31. {
  32. _netConnection = new NetConnection();
  33. _netConnection.connect( _url );
  34. _netConnection.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler, false, 0, true );
  35.  
  36. _video = new Video();
  37. addChild( _video );
  38. }
  39.  
  40.  
  41. ////////////////////////////////////////////////////////
  42. // FUNCTIONALITY
  43. //
  44. private function start():void
  45. {
  46. //
  47. // Setup the camera
  48. var cam:Camera = Camera.getCamera();
  49. cam.setLoopback(true);
  50. cam.setMode(176,144,15);
  51. cam.setKeyFrameInterval(10);
  52. cam.setQuality(0,70);
  53.  
  54. //
  55. // Start publishing the video stream
  56. _netStream = new NetStream(_netConnection);
  57. _netStream.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler, false, 0, true );
  58. _netStream.attachCamera( cam );
  59. _netStream.publish(_filename, "record");
  60.  
  61. //
  62. // Grab live stream back from server
  63. _recvStream = new NetStream(_netConnection);
  64. _recvStream.play(_filename);
  65. _video.attachNetStream(_recvStream);
  66.  
  67. //
  68. // Alternatively, Use the local camera feed
  69. // _video.attachCamera(cam);
  70. }
  71.  
  72.  
  73. ////////////////////////////////////////////////////////
  74. // EVENT HANDLERS
  75. //
  76. private function netStatusHandler( event:NetStatusEvent ):void
  77. {
  78. trace( "Red5Experiment::netStatusHandler():"+event.info.code );
  79. switch ( event.info.code )
  80. {
  81. case "NetConnection.Connect.Success":
  82. start();
  83. break;
  84. }
  85. }
  86.  
  87. }
  88. }

The files are recorded to a directory within the webapps folder on your server. If you've followed the paths above you'll find the file /usr/share/red5/webapps/oflaDemo/streams/test/test-red5.flv on your server.

You now should have a complete streaming / recording media setup. All running off an open source server!


EDIT: I've created a zip of the sample application files: red5_sampleapplication


20 Responses to “Streaming Webcam To a Ubuntu Red5 Server”

  1. [...] Here is a good tutorial show you how to Stream Webcam To Ubuntu Red5 Server: For an upcoming project we needed to stream content from the user’s webcam to be recorded on the server. In theory this sounded like a basic operation, having used FMS to this end before, but actually installing and setting up the Red5 server proved to be a little tricky. So I just wanted to outline the process I went through to get a Red5 Server running on ubuntu and how to setup your first recording stream. [...]

    Reply

  2. [...] We installed a Red5 server on the Zoos website, a procedure I detailed in a previous post. [...]

    Reply

  3. My red5 server is running thanx to your tutorial but how do i install red5_sampleapplication?

    Thanx

    Reply

    • Frank,

      Did you get the demo applications that come with red5 installed? The sample application in the post runs in your web browser and talks to the server. It’s a flash application so you need to have a swf compiler to compile the sample code. You’ll just need to change the url location of the red5 server in the code to match your installation location.

      Let me know how you go!

      Reply

  4. Hello Michael,

    It took me a while to figure out how to compile a swf.
    I use flexbuilder for it and…. It works great, thanx a lot!
    This is realy cool stuff.

    Greetings from the Netherlands

    Frank,

    Reply

  5. Hi Micheal,

    Thanks for the great tut and sample code.

    I have imported the project into Flash Builder and compiled it after changing the URL to http://ip.ip.ip.ip:5080/oflaDemo/

    I have uploaded the script and I get a blank page which I’m not able to debug.

    Are you willing to help me out here ? oflaDemo is installed.

    Cheers,

    Matt

    Reply

  6. Oops, I actually have it working, the port should not be in the URL.

    Now I have a large webcam capture and no buttons… is this normal ?

    Reply

  7. Hi Matt,

    Yeah the sample application I’ve written above streams the camera to the Red5 server and displays a Video instance of what is being streamed back from the Red5 server.

    Interesting that you didn’t need the port in your url tho. The 5080 is the default install port of the server. Did you change that in the configuration?

    Reply

  8. hey, i wanna ask u bro,
    can i if i use red5 in the same server with web server, or it could be came new risk?

    Reply

    • Yeah of course,

      The Red5 server runs on a configurable port which defaults to the mentioned 5080. So you can install a web server, I usually run apache alongside on port 80 (or 443 with ssl) with no problems. The act of securing Red5 is something you’ll have to keep on top of in addition to your web server.

      Reply

  9. how would i use this with drupal??? does anybody have a clue?

    Reply

    • What sort of integration are you looking for with Drupal? Red5 is a separate streaming media server so you’ll have to have an integration concept in mind and develop some custom code for this.

      Cheers

      Reply

  10. [...] Red 5 Installation: Red5 Server, Build, Environment Variables, Startup Scripts [...]

    Reply

  11. I found this post useful. Red5 is a great media server, compared to Wowza its open source so no license fees and if done right installation is a breeze.

    Reply

  12. Hello Michael,

    I got the Red5 server running on my machine (Ubuntu 12.04 64-bit). But I’m not sure how to deploy the actionscript that you’ve given above. I’ve managed to compile the script, but when I open the .swf file in my browser, I see nothing at all.

    Can you help?

    Thanks

    Reply

    • Hi,

      You shouldn’t need to do anything in particular to deploy the swf. The only thing to be aware of is that you have a valid cross domain policy if you’re not on the same site as your red5 server.

      Are you getting any errors when you debug the application? Try using the flash debug player and connecting it to a debugger.

      Cheers,
      Michael

      Reply

  13. Hi,
    I am getting this error. Can you please help me about this error.

    Configuation root: /home/srinivas/red5/conf
    Exception in thread “main” java.lang.NumberFormatException: For input string: “1
    47j”
    at java.lang.NumberFormatException.forInputString(NumberFormatException.
    java:65)
    at java.lang.Integer.parseInt(Integer.java:481)
    at java.lang.Integer.valueOf(Integer.java:570)
    at org.red5.classloading.ClassLoaderBuilder.scrubURLList(ClassLoaderBuil
    der.java:505)
    at org.red5.classloading.ClassLoaderBuilder.build(ClassLoaderBuilder.jav
    a:179)
    at org.red5.classloading.ClassLoaderBuilder.build(ClassLoaderBuilder.jav
    a:97)
    at org.red5.server.Bootstrap.bootStrap(Bootstrap.java:126)
    at org.red5.server.Bootstrap.main(Bootstrap.java:50)

    Reply

  14. Hello Michael, I got a problem to follow this tutorial. Give me an advice, how can I compile that codes? Can you suggest me any compiler which can work on debian? Thank you :)

    Reply

    • The code can be compiled using the free as3 compiler in the AIR or Flex SDK from Adobe. I believe you can still find a linux version of it which will work on debian.

      Reply

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>