Server-side stream recording example updated

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.

Be Sociable, Share!

47 thoughts on “Server-side stream recording example updated

  1. Rock

    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. Carl Sziebert

    @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. Gerardo

    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. Sean Grove

    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. Gerardo

    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.

  6. Sean Grove

    @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!

  7. Jayden

    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

  8. Carl Sziebert

    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.

  9. Marius

    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

  10. Carl Sziebert

    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.

  11. Ian

    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?

  12. Mike

    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.

  13. eniraka

    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.

  14. mattias

    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.

  15. mattias

    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)

  16. mattias

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

  17. Michael Hylkema

    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?

  18. Nik Nieuwenhuijs

    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.

  19. Carl Sziebert

    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.

  20. Ralph

    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.

  21. Ralph

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

  22. eric

    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]

  23. saeed

    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,

  24. James

    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

  25. Pingback: record streaming - StartTags.com

  26. Zohaib

    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?

  27. Yëco

    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.

  28. Yëco

    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.

  29. Damon83

    @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…

  30. Greg

    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

  31. Bhagyesh

    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

  32. Carl Sziebert Post author

    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.

  33. Michael

    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

  34. Michael

    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.

  35. calmchess

    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.

  36. Nikos

    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!!

Comments are closed.