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.

17 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).

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