Sunday, June 29, 2008

Simple Two-Way Audio/Video Chat Application With FMS3

In this post I will explain how to make a simple Two-Way A/V Chat Application with FMS3.

As we know that FMS3 application is the reference name we use in RTMP URL while connecting to FMS3 server. It's the folder name we create on server side. The chat application say "simplechat" will have two modules. An application can have several modules. Let's say we have two modules name Test1 and Test2. Both Test1 and Test2 will share the same FMS3 application and will connect to server. Both modules will receive the streams of each other when connect to server.

Below are the steps to start with chat application-

1: Create a new Flash File and name it to Test1.fla

2: In the Document Class text box in the Property inspector of flash document, write Test1

The simple interface of this application could be-

FMS3 Chat

3: Create an ActionScript 3.0 file and name it to Test1.as and write the following lines in it.

package
{
import flash.media.Camera;
import flash.media.Microphone;
import flash.media.Video;
import flash.events.NetStatusEvent;
import flash.display.Sprite;
import flash.net.NetConnection;
import flash.net.NetStream;

public class Test1 extends Sprite{

private var netConnection:NetConnection;
private var rtmpStr:String;
private var nsSubscribe:NetStream;
private var nsPublish:NetStream;
private var camera:Camera;
private var microphone:Microphone;
private var user1Video:Video;
private var user2Video:Video;

public function Test1 (){

rtmpStr="rtmp://localhost/simplechat"; //localhost can be replaced with your LAN IP or remote server IP points to FMS server.

attachCamera();
attachMicrophone();
attachVideoObjects();

netConnection=new NetConnection();
netConnection.addEventListener (NetStatusEvent.NET_STATUS,checkForConnection);
netConnection.connect(rtmpStr);

}

private function checkForConnection(event:NetStatusEvent):void{

event.info.code == "NetConnection.Connect.Success";

if (event.info.code){
nsPublish=new NetStream(netConnection);
nsPublish.attachAudio (microphone);
nsPublish.attachCamera (camera);
nsPublish.publish("user1","live");
nsSubscribe=new NetStream(netConnection);
nsSubscribe.play("user2");
user2Video.attachNetStream(nsSubscribe);
}
}

private function attachCamera(){

camera=Camera.getCamera();
camera.setKeyFrameInterval (9);
camera.setMode (240,180,15);
camera.setQuality (0,80);
}

private function attachMicrophone(){

microphone=Microphone.getMicrophone();
microphone.gain=80;
microphone.rate=12;
microphone.setSilenceLevel(15,2000);
}

private function attachVideoObjects(){

user1Video=new Video(camera.width,camera.height);
addChild(user1Video);
user1Video.x=25;
user1Video.y=35;
user1Video.attachCamera(camera);
user2Video=new Video(camera.width,camera.height);
addChild(user2Video);
user2Video.x=(user1Video.x+ camera.width +15);
user2Video.y=user1Video.y;
}
}
}

4: Save the Test1.as file and then publish Test1.fla to generate swf, html and JavaScript file.

5: Repeat the step 2, 3 and 4 with little change. In the Document Class text box in the Property inspector of flash document, write Test2
Now the .fla file will be named as Test2.fla and .as file will be named as Test2.as.

The changes in the Test2.as file will be-

public function Test2() in place of public function Test1()

nsPublisher.publish("user2","live"); in place of nsPublisher.publish("user1","live");

nsSubscriber.play("user1"); in place of nsSubscriber.play("user2");

6: Save the Test2.as file after these changes and publish the Test2.fla file to generate swf, html and JavaScript file.

Test the application with two users on two different computers with Mic and Camera. One user will open Test1.html file and other user will open Test2.html file and both will see each other's video as given in below picture.

Two Way Chat
This was the simplest two-way A/V chat application with FMS3. The following classes were used in this tutorial.

Camera
Microphone
Video
NetStatusEvent
Sprite
NetConnection
NetStream

You can read more about these API from the LiveDocs.

You can also extend this example to work with server side script as well.

33 comments:

Anonymous said...

nice tuto but where is de code of .asc file

Sunil Kumar Gupta said...

Do you really need that for this application? You can use .asc file for a better chat application that has full server side control. This is the minimal for a simple chat.

Anonymous said...

sorry i con not connect them each other if you can could you help me my e-mail mehmet_birlik@hotmail.com
if you can send me program thanks now

jannes said...

nice tutorial... works fine! but the framerate of the streamed video is very low, even though i am running in a local area network

saanthu said...

Hi guys..

i tried this tutorial on Red5(0.6) version. the stream get freezes after few minutes :(. the same application runs fine on 0.7 release.

Matt said...

I am a newbie.

When you say "The simple interface of this application could be-
" I get lost..
If I follow instructions exactly..how does the flash movie know anything about the interface?

When I "complet" he tutorial.
I have the .as file and a canvas in my .swf with the image of the interface that copied and pasted from your tutorial.
I assume something has to be done to the interface. Do you mind covering that a little.

Sunil Kumar Gupta said...

Hi Matt,

I think you are confused because of words i used.
When I say-

The simple interface of this application could be

It means that you can have the User interface of the application as it is shown in image below. Just 2 video objects plus some labels. I think, it's not a hard thing to do. I hope it answers your question.

Thanks,
Sunil

Egidio said...

Hi, I have a question: I need to play in a flash website, the streaming video coming from 4 webcams; now I can see the output thanks to Zoneminder and Cambozola, but I wont to give the streaming video of the webacam to the flash website.
Can you help me please.
Thanks a lot.

E.C.

Egidio said...

I need to play the output of 4 webcam into a flash website; I installed correctly red5 but I do not know how to precede.
For now I can see the output of the webcam thanks to Zoneminder and Cambozola in the browser, but I want the streaming into the flash website .swf.

Can you help me?
Thank you in advance.

E.

Sunil Kumar Gupta said...

Hi Egidio,

If i am not wrong, you want to forward the output of four webcam to a flash website. You have to run a flash application that captures the webcam and send it to Red5 on all 4 machines. Now you have to create an application for your website that pull these videos from Red5. The whole workflow is quite simple and you can start with the video recorder and player applications given as examples in your red5 installtion.

Regards,
Sunil

Ken said...

Hey, this example seems to be pushing me in the right direction for a simple one on one video chat app. But I'm getting a strange error. I'm trying to use your tutorial with red5, but I get this error here..

ArgumentError: Error #2126: NetConnection object must be connected.

The app connects to red5 correctly, but its like it connects too quickly for

nsPublish=new NetStream(netConnection);
// this is on line 41

to initialize (it connects if I comment out the above line of code, and put a simple trace("connected") in.

I know this tutorial is for FMS3, so I might be doing something wrong with red5. Do you know what could be happening?

Sunil Kumar Gupta said...

Hi Ken,

Thanks for writing. I am not sure about the problem. It occurs when you use the NetConnection object before it actually connects to the server. Here you can try this.

netConnection.objectEncoding = flash.net.ObjectEncoding.AMF0;


try setting the encoding correctly after the following line

netConnection=new NetConnection();

Hope that helps.

Demas W W said...

sorry i'am new programmer. i have one question:
can i change the "user1" on "nsPublish.publish("user1","live");"
with some php variable.
please tell me how to do that.

sorry with my bad english.
my regard,
demas

Sunil Kumar Gupta said...

Hi Demas,

You can check about FlashVars( Pass Variables to Flash) from the following article of Adobe.

http://kb2.adobe.com/cps/164/tn_16417.htmlHope that helps.

Regards,
Sunil

Prashant said...

Sunil,

Great work with all the Red5 stuff you have on both your blogs.

Being a Java developer and a newbie for AS3 and Flash, I've been scratching my head for an app that I am working on for video recording and playing.

Is there a flv player with streaming that you recommend for Red5 both recording and playing videos? Or is it more like to each his own?

Also, I realize oflaDemo player generates metadata files. How is it done?

thanks a lot for your help.

Sunil Kumar Gupta said...

Hi Prashant,

Thanks for your comment.

I am not sure, if any player is there. But you can always make such application very easily. Doing stream recording and playback is very simple with Red5. Taking about metadata. Red5 automatically generates it, so that you have the essential information with you before you actually start playing the video.

Hynek Hanke said...

Hi, this example is great, but it didn't work for me due to an error at first:

This doesn't make any sense:

event.info.code == "NetConnection.Connect.Success";
if (event.info.code){

It piece needs to be

if (event.info.code == NetConnection.Connect.Success"){

Works perfectly on Red5 then with the oflaDemo module (change simplechat to oflaDemo in the rtmp line).

ransi1201 said...

I have confusion about point 5.
Do I have to change doc class in Test1.fla from Test1 to Test2?
I have done but no help.
Or I have to make another fla Test2.fla for Test2 class and Test2.as.co

Ransi1201 said...

When I stop my broadcasting to other end there still remain image of last frame captured of video.

How this can be removed ???

Sunil Kumar Gupta said...

Hi Ransi,

You are right, but did you check

clear () method of Video object

public function clear():void

Clears the image currently displayed in the Video object. This is useful when you want to display standby information without having to hide the Video object.

Hope that solves your problem.

Regards,
Sunil

ransi1201 said...

No it doesnt work.
It doesn't clear video last saved frame image.
I have tried with no use. I have also used seek(0) but that doesn't work.

Rajiv said...

Hi...
I want to show error exeption message when user is not connected to camera and headfone.
How this can be achieved.

Sunil Kumar Gupta said...

Hi Rajiv,

Camera class can help you in this.

If getCamera() returns null, either the camera is in use by another application, or there are no cameras installed on the system. To determine whether any cameras are installed, use the names.length property. To display the Flash Player Camera Settings panel, which lets the user choose the camera to be referenced by getCamera(), use Security.showSettings(SecurityPanel.CAMERA).

via Camera

and Microphone class

If Microphone.getMicrophone() returns null, either the microphone is in use by another application, or there are no microphones installed on the system. To determine whether any microphones are installed, use Microphones.names.length. To display the Flash Player Microphone Settings panel, which lets the user choose the microphone to be referenced by Microphone.getMicrophone, use Security.showSettings().

via Microphone

Thanks,
Sunil

Rajiv said...

I want to know whether camera is attached or not though it may be installed..
How can we find out that ??

Rajiv said...

In mine no micrphone is attached then also it is showing dialog box of security panel.
What is the reason ???

Sunil Kumar Gupta said...

Hi Rajiv,

Mic is attached or not, that's different. Please read this getMicrophone() Method. It explains why its like that.

Regards,
Sunil

Rajiv said...

Haan.
Its true.
But how to handle this.
Even though microphone is not attached though installed how I can show message that Microphone is not attached.
I have uninstalled audio through control panel. Now it is working fine.gatop

Mart said...

Hi Sunil,
I was wondering if you could help me with a query in relation to the rtmpStr. Do you put the path to each particular html file here. I have the files stored in red5 which is sitting on my PC (localhost webserver) only one is appearing on each side.What should the protocol or port be RTMP/HTTP ? 4080/1935?
Any help would really be appreciated
Thanks
Mart

Sunil Kumar Gupta said...

Hi Mart,

I am not sure if i understood your question correctly. rtmpStr is used for establishing connection between Flash and Red5. It's a RTMP URL which points to the application deployed on Red5.

Regards,
Sunil

mart said...

Hi Sunil,
Sorry I wasnt clear in my last post. I have the two html files sitting in a folder on my red 5 server. When I access the files from another machine the code is only picking up the mic and cam on that PC and not subscribing to the other stream . Here is what I have entered for rtmpStr

rtmpStr="http://xxx.xxx.xx.xxx:1935/~mart/webapps/root/flashtest/Test1.html";

Is this correct path I should have ?

Sunil Kumar Gupta said...

Hi Mart,

The rtmpURL points to the deployed application on FMS. Why are you giving http://xxx/

You must be having FMS application deployed on FMS, give that URL.

Please read the complete post carefully.

Regards,
Sunil

mart said...

Hi Sunil,
Sorry again the xx are just what the ip is for my webserver it is like 143.32.0.0 I have included the path down to the html file, but still it only picks up one camera and not the second.

Do you know if there is much configuration required in relation to red5 to work with this app.

All your help is much appreciated
Regards,
Mart

Sunil Kumar Gupta said...

Hi Mart, I am clear what is XX. I just wanted to tell you that rtmpStr points to your Red5 or FMS application. Local camera is working and not the remote one because it's not connecting to the deployed application.

Regards,
Sunil

Daily Technology Tips

Catch me at Mashable

PayPal

Sign up for PayPal and start accepting credit card payments instantly.

Blog Explosion


Bloglines

Blogrush


BlogsByCategory.com