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.
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:
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:
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.
- # init script for Red5
- # /etc/init.d/red5
- test -x $RED5_HOME/$RED5_PROG.sh || exit 5
- case "$1" in
- echo -n "Starting Red5"
- echo -n " "
- cd $RED5_HOME
- su -s /bin/bash -c "$RED5_HOME/$RED5_PROG.sh &" $RED5_USER
- sleep 2
- echo -n "Shutting down Red5"
- echo -n " "
- su -s /bin/bash -c "killall -q -u $RED5_USER java" $RED5_USER
- sleep 2
- $0 stop
- $0 start
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!
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
- import flash.display.Sprite;
- import flash.net.NetConnection;
- import flash.net.NetStream;
- import flash.events.NetStatusEvent;
- import flash.media.Camera;
- import flash.media.Video;
- public class Red5Experiment extends Sprite
- // Constants
- public static const RED5_URL : String = "rtmp://SERVERNAME/oflaDemo/test/";
- // Variables
- private var _netConnection : NetConnection;
- private var _netStream : NetStream;
- private var _recvStream : NetStream;
- private var _url : String = RED5_URL;
- private var _filename : String = "test-red5";
- private var _video : Video;
- public function Red5Experiment()
- _netConnection = new NetConnection();
- _netConnection.connect( _url );
- _netConnection.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler, false, 0, true );
- _video = new Video();
- addChild( _video );
- // FUNCTIONALITY
- private function start():void
- // Setup the camera
- var cam:Camera = Camera.getCamera();
- // Start publishing the video stream
- _netStream = new NetStream(_netConnection);
- _netStream.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler, false, 0, true );
- _netStream.attachCamera( cam );
- _netStream.publish(_filename, "record");
- // Grab live stream back from server
- _recvStream = new NetStream(_netConnection);
- // Alternatively, Use the local camera feed
- // _video.attachCamera(cam);
- // EVENT HANDLERS
- private function netStatusHandler( event:NetStatusEvent ):void
- trace( "Red5Experiment::netStatusHandler():"+event.info.code );
- switch ( event.info.code )
- case "NetConnection.Connect.Success":
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