Server-side stream recording example updated

Filed under ActionScript, Flash, Java, Red5, Software, Tutorials

With this being my most popular blog topic, I felt the time has come for a little update. (You can catch up on the original here.) Frankly, there’s not much too it. The server-side code didn’t change much, just an update to take advantage of the built-in logging support of Red5. Both clients have been completely rewritten using ActionScript 3. (I know, it’s about time.)

Recording the stream on the server takes one simple line as displayed here:

StreamManager.java

ClientBroadcastStream stream = (ClientBroadcastStream) app.getBroadcastStream(conn.getScope(), "hostStream");
try {
	// Save the stream to disk.
	stream.saveAs(streamName, false);
} catch (Exception e) {
	logger.error("Error while saving stream: {}", streamName);
}

The only button in the broadcast application starts and stops the recording session. The click handler does all the work:

Broadcast.as

private function onClick(event:MouseEvent):void {
	var button:Button = event.target as Button;
	// Record the stream by triggering a server event.
	if (button.label == "Record") {
		// Tell the remote server to start recording.
		runtime.conn.call("streamManager.recordShow", null);
		// Re-label the button.
		button.label = "Stop";
	// Stop recording the stream.
	} else if (button.label == "Stop") {
		// Tell the remote server to stop recording.
		runtime.conn.call("streamManager.stopRecordingShow", null);
		// Re-label the button.
		button.label = "Record";
	}
}

Like I said, not much to it. But there’s a lot of power wrapped up in this little piece of functionality.

Grab the source code for this tutorial here. The example code was updated to be compatible with Red5 0.9.1 on July 29, 2010.

Liked this post? Share it with others:
  • Digg
  • del.icio.us
  • Facebook
  • DZone
  • email
  • LinkedIn
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

46 Comments

  1. Rock
    Posted April 18, 2009 at 1:32 pm | Permalink

    Hi Carl,
    Firstly, thanks much for the tutorial and for your time. I installed Red5 on my localhost (PHP) and placed the updated source code (recorder) folder in the webapps directory of red5. Then launched broadcast.html file. The browser accessed my webcam & I assigned the security permissions as suggested in one of your earlier posts. Then I revisited the same URL on my local host but however much I try and click on the Record button, it does not work.

    I did not do anything apart from what I mentioned above. Can you please help me? Thank you.

  2. Posted April 20, 2009 at 10:04 pm | Permalink

    @Rock:

    Did you compile the example code? The download doesn’t contain the necessary dependencies either. I’ve set it up to use Ivy for that. Compile the app via ant clean compile, then copy it over.

  3. Posted April 21, 2009 at 2:26 pm | Permalink

    Hi Carl,

    First of all, thanks for your help.
    I´m working with red5 and flex 3 to make an application wich lets a user share it’s screen computer with others. I think the best option is to broadcast that screen as a simulated webcam video, but i can´t find how to choose as video source the broadcaster user’s screen.

    Is this possible?

  4. Posted April 21, 2009 at 8:20 pm | Permalink

    Hey Carl,
    Just wanted to say thanks for the heads up with red5 video recording. I appreciate you taking the time to flesh things like this out for us newcomers. I’ll definitely give this a try later tonight!

    @Gerardo, I believe you need to take an image snapshot of the whole screen, serialize the bytes, and then send them over to the server. Set it up to do it at regular intervals and some crude compression, and you have a variable-quality screen broadcaster. I don’t know if that’s the canonical way of doing it in flex (I’m new to the Flex/flash world), but it’s one option anyway.

  5. Posted April 21, 2009 at 9:47 pm | Permalink

    @Gerardo:

    Give this post a look and let me know how it works out for you: http://mainichipress.wordpress.com/2008/04/03/screen-sharing-flash-java/

  6. Posted April 21, 2009 at 9:49 pm | Permalink

    @Sean:

    Always happy to help. If there is a particular topic that you’d like to see covered here, let me know.

  7. Posted April 22, 2009 at 2:26 pm | Permalink

    Thank you,
    I think this is a great idea to start. I have no idea how to make take this screen shooter and make a codec to transform single images into video, but will keep searching.

  8. Posted April 22, 2009 at 5:10 pm | Permalink

    @Carl,

    Actually, I sat down to work on this a bit last night, but it seems that your example code was created with flash rather than flex.

    Any chance we might get a barebones working project example written in flex (preferably for the current stable 0.7 red5)? I know it’s a bit much so I understand either way!

  9. Posted April 29, 2009 at 6:15 am | Permalink

    Great post Carl. I am a total noob when it comes to Red5, but quite an advanced AS2 developer.

    I desperately need to learn how to:

    a) Install Red5 locally (WAMP/PHP);
    b) Learn how to code for Red5 so that I can record and save flv content with audio (recorded live through the user’s webcam & mic;

    I can’t wait to learn, but when starting from scratch it seems the learning curve is steep and the tutorials are few! Do you have any recommended links that give relatively simple steps for achieving a) and b) ?

    Many thanks!

    Jayden

  10. Posted April 29, 2009 at 8:44 am | Permalink

    Jayden:

    There are several sets of instructions for installing Red5. I’d suggest that you start with the documentation at their site. If you already know Java, then learning the Red5 APIs is relatively straight forward. If you don’t know Java, then I would suggest that you start with Bruce Eckel’s Thinking in Java.

  11. Posted April 29, 2009 at 3:17 pm | Permalink

    Fantastic – thanks so much!

  12. Marius
    Posted June 2, 2009 at 5:54 am | Permalink

    I installed the Demo on my brandnew RED5 Server. Everything seems to work so far.
    Loding the Demo-swf’s faild with actionscript error:

    TypeError: Error #1009: Der Zugriff auf eine Eigenschaft oder eine Methode eines null-Objektverweises ist nicht möglich.
    at net.sziebert.tutorials.broadcast::Broadcast/::createChildren()
    at net.sziebert.tutorials.broadcast::Broadcast$iinit()
    at flash.display::Sprite/flash.display:Sprite::constructChildren()
    at flash.display::Sprite$iinit()
    at flash.display::MovieClip$iinit()

    I never worked with flash and at this time I haven’t Flex. You know, what’s the Problem here?

    Thanks Marius

  13. Posted June 3, 2009 at 3:51 pm | Permalink

    Marius:

    How are you loading the SWF? It looks like it can’t find the necessary classes, so it could be that the classpath entry is incorrect.

  14. Matthew
    Posted July 1, 2009 at 10:57 am | Permalink

    Is there a way to change the location of where the files will be saved?

  15. Posted July 1, 2009 at 2:45 pm | Permalink

    Yes, Matthew there is an easy way to change the location of the saved files. Give this tutorial a look: http://www.joachim-bauch.de/tutorials/red5/HOWTO-StreamCustomDirectories.txt

  16. Ian
    Posted July 27, 2009 at 11:30 am | Permalink

    I’m using this to record up to 20 second clips. The problem I am having is the first few seconds of the recording tend to be bad data. It will show black for the first few seconds and then resume the recording already in progress. If I restart it it will show whatever frame was last shown until it starts playing the good data. Any thoughts?

  17. Posted August 6, 2009 at 7:59 pm | Permalink

    Thank you Carl for the link to Joachim’s tutorial.
    I find the standard RED 5 manuals are lacking some basic information. You helped me.

  18. eniraka
    Posted August 7, 2009 at 1:42 am | Permalink

    Hello,

    I deployed the Application and StreamManager to Red5 but red5 can’t load them. I have no idea why.
    I’ve compiled your project (with ant clean compile). I created a new directory called “recorder”/WEB-INF in the webapps folder in Red5. And I put in there the folder classes, lib and red5 configuration files (red5-web.properties, red5-web.xml, web.xml) that i modified so that they are correct. When I call the application localhost/recorder, I get a 503 error. It seems like it can’t load this class: “net.sziebert.tutorials.Application”. Because when I change this web handler to the red file Application Adapter org.red5.server.adapter.ApplicationAdapter I can reach the application.

    Have you got any idea why it can’t find this class? net.sziebert.tutorials.Application
    It’s in the folder: “red5directory/webapps/recorder/WEB-INF/classes/net/sziebert/tutorials/Application.class

    Thank you for your help.

  19. mattias
    Posted September 7, 2009 at 3:31 pm | Permalink

    Hi.

    When i try to run “ant clean compile” i get the following errors:

    BUILD FAILED
    /root/Recorder/build.xml:38: Problem: failed to create task or type antlib:org.apache.ivy.ant:settings
    Cause: The name is undefined.
    Action: Check the spelling.
    Action: Check that any custom tasks/types have been declared.
    Action: Check that any / declarations have taken place.

  20. mattias
    Posted September 7, 2009 at 3:42 pm | Permalink

    Figured Ivy out… now i get this error instead:

    compile:
    [javac] Compiling 2 source files to /root/Recorder/web/WEB-INF/classes
    [javac] ———-
    [javac] 1. ERROR in /root/Recorder/src/java/net/sziebert/tutorials/Application.java (at line 26)
    [javac] @Override
    [javac] ^^^^^^^^^
    [javac] Syntax error, annotations are only available if source level is 5.0
    [javac] ———-
    [javac] 2. ERROR in /root/Recorder/src/java/net/sziebert/tutorials/Application.java (at line 38)
    [javac] @Override
    [javac] ^^^^^^^^^
    [javac] Syntax error, annotations are only available if source level is 5.0
    [javac] ———-
    [javac] 2 problems (2 errors)

  21. Posted September 8, 2009 at 9:32 pm | Permalink

    Mattias:

    You need to compile the sample code with JDK 1.5 or greater.

  22. mattias
    Posted September 8, 2009 at 11:56 pm | Permalink

    Sorry but i only use official debian packages, and the package java-gcj-dev is not an official Java JDK.
    The compiler is GCJ 4.3 though.
    Will try to locate some other projekct like this instead, thanks anyway :-)

  23. Michael Hylkema
    Posted September 22, 2009 at 9:43 am | Permalink

    I’m getting the same build error:

    BUILD FAILED
    /root/Recorder/build.xml:38: Problem: failed to create task or type antlib:org.apache.ivy.ant:settings

    Mattias: How did you solve this?

  24. Posted September 23, 2009 at 12:52 pm | Permalink

    Michael:

    You just need to install Ivy (http://ant.apache.org/ivy/) into $ANT_HOME/lib.

  25. Nik Nieuwenhuijs
    Posted October 13, 2009 at 11:24 am | Permalink

    Hi Carl,

    Thanks for your tutorial. It’s really helpfull.
    Currently i’m testing your tutorial. When i nc.call(“streamManager.recordShow”, null);
    It gives me an Java NullException error on line 17 of StreamManager.java. Basicly that’s the following line:
    ClientBroadcastStream stream = (ClientBroadcastStream) app.getBroadcastStream(conn.getScope(), “hostStream”);

    Hopefully you can help me.
    Thanks for sharing and your help!

    Cheers.

  26. Nik Nieuwenhuijs
    Posted October 13, 2009 at 12:22 pm | Permalink

    Carl found the issue. Sorry!

  27. Posted October 13, 2009 at 1:33 pm | Permalink

    Hey, Nik.

    Happy to hear that you’ve fixed your problem. Could you provide some details so that I can update my example where necessary? Others may run into your issue as well, so your solution would be handy to document.

    Thanks.

  28. Ralph
    Posted November 30, 2009 at 8:53 am | Permalink

    Carl,

    I want to be able to pass parameters from AS to Java and I am having difficulties doing so.
    I have easily managed to pass a parameter to the roomConnect method, however I need the parameter available in recordShow.
    I saw that you can pass params also via the NetConnection’s call method and I think I have the right code on the AS side.
    Here it is:

    runtime.conn.call(“streamManager.recordShow”, new Responder(onComplete) , fileName);

    Things are not working and my guess is I am not declaring the right type on the Java side.
    Here is my Java code:

    public void recordShow(IConnection conn,java.io.Serializable asObject,Object[] params) {…

    I chose to use Serializable because I read it here http://livedocs.adobe.com/flashremoting/mx/Using_Flash_Remoting_MX/UseASData4.htm but I tried also many other options including Object,Map etc.

    I’ll appreciate if you could direct me on that.
    Thanks.

  29. Ralph
    Posted December 1, 2009 at 5:37 am | Permalink

    ok, problem solved. All I needed it not trying to receive the Responder on the Java side.

    Client site looks like this now:

    runtime.conn.call(“streamManager.recordShow”, null , fileName);

    Server side

    public void recordShow(IConnection conn, String fileName) {…

    and it works :)

  30. eric
    Posted December 1, 2009 at 2:56 pm | Permalink

    It works at the fist time. Then close netconnection, then tried to create netconnection and netstream. it works. finally I clicked to record button. I got the following error.

    [ERROR] [NioProcessor-2] org.red5.server.service.ServiceInvoker – Service invocation error
    java.lang.reflect.InvocationTargetException

    Caused by: java.lang.NullPointerException at org.red5.server.adapter.MultiThreadedApplicationAdapter.getBroadcastStream(MultiThreadedApplicationAdapter.java:805) [red5.jar:na]

  31. eric
    Posted December 3, 2009 at 4:57 pm | Permalink

    The problem was resolved. It was my flash document issue.
    Thank Carl!

  32. ashton
    Posted December 15, 2009 at 5:26 pm | Permalink

    Trying to install the source code in the tutorial.
    When I run ant in the Recorder directory, ivy tried to download red5.jar from several places (including http://garagetech.googlecode.com/svn/trunk/repository/red5.jar) and then fails.

    [ivy:retrieve] :: garagetech#red5;: not found

    but I already have red5.jar. How do I make it known?

  33. saeed
    Posted December 23, 2009 at 10:19 am | Permalink

    Thanks,
    is this possible to deploy server to server netconnection in Red5? in the way full RTMP functionallity – such as stream.send(data) in conjuction with audio and video – would be avaiable!
    I notice that it isn’t supported in wowza media.
    in the other word: is it possible to deploy an array of Red5 server that work in an fully integrated manner?

    thanks again,

  34. James
    Posted December 28, 2009 at 11:28 am | Permalink

    Hi Carl!
    Thank you for the app but I can’t make it work. I deploy the Recorder folder to my webapps and then compile it using ants. I get no errors and a successful build, but when I go to http://localhost:8080/recorder/web/broadcast.html a empty box appears and nothing happens.
    I’ve noticed that the web.xml points to /recorder but even creating a folder and then moving the web folder content (including WEB-INF and some html and swf) I get the same result.

    What am I doing wrong??

    Thanks in advance,

    James

  35. Zohaib
    Posted February 4, 2010 at 8:34 pm | Permalink

    Carl,

    We got our video recording to work properly but when we try to play them back on oflaDemo there seems to be a lag. Are you aware of any issues relating to the lag?

  36. Posted March 21, 2010 at 11:08 pm | Permalink

    This must be a little late, but just in case you need it.

    For those who can’t retrieve red5.jar ([ivy:retrieve] :: garagetech#red5;: not found) from Carl’s svn… just add latest as revision in the avy settings:

    *”/>

    That should make the trick.

  37. Posted March 21, 2010 at 11:10 pm | Permalink

    This must be a little late, but just in case you need it.
    For those who can’t retrieve red5.jar ([ivy:retrieve] :: garagetech#red5;: not found) from Carl’s svn… just add latest as revision in the avy settings:

    <dependency name="red5" rev="latest" changing="true" conf="compile->*"/>

    That should make the trick.

  38. Damon83
    Posted April 14, 2010 at 4:19 pm | Permalink

    @Yëco
    I’ve got the same problem ([ivy:retrieve] :: garagetech#red5;: not found) and to add “latest” on that line not worked for me…

  39. Posted May 20, 2010 at 7:41 pm | Permalink

    The problem was resolved. It was my flash document issue.
    Thank Carl!

  40. Posted June 7, 2010 at 7:43 am | Permalink

    Hi Carl!
    Thank you for the app but I can’t make it work. I deploy the Recorder folder to my webapps and then compile it using ants. I get no errors and a successful build, but when I go to http://localhost/recorder/web/broadcast.html I got message: “Your flash player appears to be out of date. Please upgrade it.” I made upgrade Flash Player on my PC but got the same message.
    I use ColdFusion9 on my local PC. May be that is the reason problems?
    What am I doing wrong??

    Thanks in advance,

    James

  41. Bhagyesh
    Posted July 13, 2010 at 6:46 am | Permalink

    Hello Carl,

    I am really in trouble. I have LINUX centos v 5.3 server and I am trying to install red5 on my server since last two weeks. Do you provide any service to install red5 on my server? I am happy to pay for this work. Please email me on bhagyesh_patel_2006@yahoo.com

  42. Posted July 29, 2010 at 1:22 pm | Permalink

    I’ve updated the source to be compatible with the latest version of Red5 and have also made some changes to the Ivy configuration files. Grab the latest by clicking the download link above and let me know if you run into any issues.

  43. Michael
    Posted October 26, 2010 at 8:37 am | Permalink

    Carl,

    Are you still active on this project? I have everything set up fine but there is a terrible lag and playback is essentially unusable. I was wondering if you had any insights.

    I’m using your latest code and Red5 v0.9.1.

    Testing so far only on my local network when I click playback maybe a few seconds of the video plays then video stops, audio continues a little longer then it complete stops.

    Any ideas?

    Thanks.
    Mike

  44. Michael
    Posted October 26, 2010 at 11:53 am | Permalink

    VIDEO AUDIO LAG ISSUE FIXED

    After much research it turns out there are issues with recording video using Red5 0.9.1. A downgrade to 0.8.0 using the Recorder documented here and you should be set.

    Worked for me.

  45. Posted October 29, 2010 at 8:50 am | Permalink

    I finnally got this working Applcation app = this; is the key to makeing the above code work with eclipse took the better part of a day to realize it.

  46. Nikos
    Posted April 19, 2011 at 1:00 pm | Permalink

    Hi Carl,

    First of all thanks for your clear tutorial. It’s a lot of help.
    Thanks for your last update to the source for Red5 0.9.1.
    I’ve tested it on the last Red 5 release 1.0 but i encounter some problems. Can you maybe do a quick test as well?

    Thanks!!

One Trackback

  1. By record streaming - StartTags.com on January 24, 2010 at 3:47 pm

    [...] sure there are many more like me. So I ask ye… who out there recorded Lilys, Lightning Bolt, BrianCarl Sziebert Server-side stream recording example updatedRecording the stream on the server takes one simple line as displayed here: … The only button in [...]