Recording Audio and Video with Red5
What’s up people, these last days (and weeks) I’ve been working with Red5, and learning along the way because I had never used it beyond ‘Hello world’ type experiments, and it’s a shame I did not started to play with it earlier, because is so damn cool. Thanks a lot Red5 Team!
Anyway, I’ve seen some folks here and there asking on how to record a live webcam/mic stream using Red5, and some long ago when I searched for that I did not find a straightforward answer (AKA gimme the code :D), so for those fellas that are badass lazy as I am, and that their hopes are to find all the help (AKA gimme the code) on Google, here’s a little example that should get your ass going (if you were too lazy to try this suggestion which you should do if you want to really learn )
Ok, I’ll assume you have everything in order so we can go ahead and get our cameras and microphones:
1 2 3 | var myCam:Camera = Camera.getCamera(); var myMic:Microphone = Microphone.getMicrophone(); myMic.setLoopback(true); |
, then create our net stream and attach the live video/audio to it. Here you could add some validations to verify that you are getting your camera and mic feed as expected (e.g. not null), I will omit it however because my fingers are getting tired.
1 2 3 4 5 6 7 | // Create our net stream, by the way for the sake of brevity let's assume you have all your connection sh!t together var stream:NetStream = new NetStream(myNetConnection); // add a listener so we know when our stuff is ready for recording stream.addEventListener(NetStatusEvent.NET_STATUS, onRecStreamStatus); stream.attachCamera(myCam ); stream.attachAudio(myMic); stream.publish('myStream', 'live'); |
Now the stream status event handler, as you’ll see, when your stream has started to “stream” ha!
then you are ready to record,
1 2 3 4 5 6 7 | private function onRecStreamStatus(e:NetStatusEvent):void { if ( e.info.code == "NetStream.Publish.Start" ) { myConnection.call('startRecordingStream', null); } } |
This line myConnection.call('startRecordingStream', null); invokes the method ’startRecordingStream’ on our Red5 service which is responsible for handling our streams and recording/saving them accordingly… and it could look something like this…is Java by the way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | //Java public void startRecordingStream(IConnection conn) { String clientId = conn.getClient().getId(); IScope scope = conn.getScope(); String streamName = clientId + "_" + String.valueOf(System.currentTimeMillis()); try { ClientBroadcastStream stream = (ClientBroadcastStream) this.getBroadcastStream(conn.getScope(), "myStream" ); stream.saveAs(streamName, false); // You could, if you wish of course, notify the user that the recording has actually started // You just have to add the recordingStarted public function on your flash application ServiceUtils.invokeOnConnection(conn, "recordingStarted", new Object[]{clientId}); } catch(Exception e) { // You could notify the user that the recording failed for some stupid reason // sometimes things does not go well :) // You just have to add the recordingStarted public function on your flash application ServiceUtils.invokeOnConnection(conn, "recordingFailed", new Object[]{clientId}); } } |
And if you want to have a button to stop recording on your flash app., just throw a button and when is clicked invokes a method on your Red5 service for stopping the recording process. Something like this:
1 2 3 | //AS3 //assuming this is on a click handler or whatever myConnection.call('stopRecordingStream', null); |
1 2 3 4 5 6 7 8 | //Java public void stopRecordingStream(IConnection conn) { String clientId = conn.getClient().getId(); ClientBroadcastStream stream = (ClientBroadcastStream) this.getBroadcastStream(conn.getScope(), "myStream"); stream.stopRecording(); ServiceUtils.invokeOnConnection(conn, "recordingStopped", new Object[]{clientId}); } |
As Dominick pointed out on his blog post, you should get your recorded stream under red5/webapps/YouBadAssApplication/streams.
There are lot of things that have to be done in order to have video/voice application running smoothly, however I hope this helps you in realizing how simple Red5 makes things for us. Let me know if I missed something, if there are other workarounds you have found, or whatever you might wanna say
Cheers!