Archive for actionscript

screen sharing flash, java

Here is a simple code for screen sharing( ofcourse, it just sends one image):Capture screen and send to red5:

import org.red5.server.net.rtmp.*;
import org.red5.server.api.service.*;
import java.io.*;

public class Red5Client {
static byte [] imgbyte = new byte[1000000];

public static void main(String args[]) throws Exception{
final CaptureScreen capture = new CaptureScreen();
final RTMPClient client = new RTMPClient();
client.connect(“localhost”, 1935, “echo”, new IPendingServiceCallback() {
public void resultReceived(IPendingServiceCall call) {
Object[] param = new Object[1];
try{
capture.capture();
}
catch(Exception e){

e.printStackTrace();
}
param[0]=Red5Client.imgbyte;

System.err.println(“Connected: ” + call.getResult());
client.invoke(“echo”, param,
new IPendingServiceCallback() {
public void resultReceived(IPendingServiceCall call) {
System.err.println(“Received: ” + call.getResult());
}
});
}
});

}

}
import java.net.URLConnection;
import java.net.URL;
import java.awt.image.BufferedImage;

import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;

import javax.imageio.*;
public class CaptureScreen {

private Date startDate;
public static final int interval = 10;
public CaptureScreen(){

}

public void capture() throws Exception {

// wait for a user-specified time
try {

Thread.sleep(interval);

} catch(NumberFormatException nfe) {

}
// determine current screen size
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
Rectangle screenRect = new Rectangle(screenSize);
// create screen shot
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRect);
ByteArrayOutputStream out = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam encpar = encoder.getDefaultJPEGEncodeParam(image);

encoder.setJPEGEncodeParam(encpar);
encoder.encode(image);
System.out.println(out.size());
Viewer.image = Toolkit.getDefaultToolkit().createImage(out.toByteArray());
Red5Client.imgbyte = out.toByteArray();
System.out.println(“Length of image”+out.toByteArray().length);

// use System.exit if the program hangs after writing the file;
// that’s an old bug which got fixed only recently
// System.exit(0);
}

}

Flash Client:

import flash.display.*;
var so:SharedObject = null;
var ldr:Loader = new Loader();
addChild(ldr);

// Current release of FMS only understands AMF0 so tell Flex to
// use AMF0 for all NetConnection objects.
NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
init();

function init():void{
// Create the NetConnection and listen for NetStatusEvent and SecurityErrorEvent events
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, netSecurityError);
nc.connect(“rtmp://127.0.0.1/echo”);
}

function shared_sync(event:SyncEvent) :void {
trace(“test”);
var list:Object=event.changeList;
for (var n in list) {
so_data=so.data[list[n].name];
var byteArr:ByteArray = new ByteArray();
for(i=0;i<so_data.length;i++){
byteArr[i]=so_data[i];

}
ldr.loadBytes(byteArr);

trace(“Length of shared Object”+so.data[list[n].name].length);
}

};

function netStatus(event:NetStatusEvent):void {
trace(“netStatus: ” + event);
var info:Object = event.info;
trace(info.code);
// lots more code here…
if (info.code == “NetConnection.Connect.Success”) {
trace(“Connect success”);
so = SharedObject.getRemote
(“test”, nc.uri, true);
so.addEventListener(SyncEvent.SYNC, shared_sync);

so.connect(nc);
}
else{
trace(“error”);
}

}

function netSecurityError(event:SecurityErrorEvent):void {
trace(“netSecurityError: ” + event);
}

//nc = new NetConnection();

//nc.onStatus = function(info) {

//}

//nc.connect(“rtmp://127.0.0.1/echo”);

/*
nc.onResult = function(obj) {
trace(“The result is ” + obj);
}
nc.call(“add”, nc, 1, 2);
*/

Comments (10)

Actionscript webcam player.

This is a simple actionscript3 web cam player.
The player do both subscribe and publish video.

You can compile the code using flexbuilder.

Enjoy! Let me know if you actionscript2 player

Main class :

package liveplayer {
import flash.display.Sprite;
import flash.media.Video;
import lib.StreamPlayer;

public class liveplayer extends Sprite
{

public function liveplayer()
{
// TODO: move this to UI class
var vid:Video = new Video(320,240);
this.addChild (vid);

var stPlayer:StreamPlayer = new StreamPlayer(vid);
stPlayer.initialize_pub();
stPlayer.initialize_subscribe();

}
}
}

package lib{
// Player class

import flash.media.Camera;
import flash.media.Microphone;
import flash.media.Video;
import flash.net.*;
import flash.events.*;

import lib.Config;
public class StreamPlayer{

private var play_nc:NetConnection;
private var play_ns:NetStream;
private var pub_nc:NetConnection;
private var pub_ns:NetStream;

private var stat:Number;
private var myCamera:Camera = new Camera();
private var myMic:Microphone = new Microphone();
private var vidObj:Video;
public function StreamPlayer(vid:Video){

this.vidObj=vid;

}

public function initialize_subscribe():void{
// Get the camera
//myMic=Microphone.get();
// Connect to oflaDemo demo application in red5 server
play_nc = new NetConnection();
// Should be configurable
trace(Config.streamingURL);
play_nc.addEventListener(NetStatusEvent.NET_STATUS, sub_netStatus);
play_nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, netSecurityError);
play_nc.connect(Config.streamingURL);

}

function sub_netStatus(event:NetStatusEvent):void {
trace(“netStatus: ” + event);
var info:Object = event.info;
trace(info.code);

// lots more code here…
if (info.code == “NetConnection.Connect.Success”) {

// Publish video from the camera to the red5 server
play_ns = new NetStream(play_nc);
this.subscribe();
}

else{
trace(“error”);
}

}

private function netSecurityError(event:SecurityErrorEvent):void {
trace(“netSecurityError: ” + event);
}

public function initialize_pub():void{
// Get the camera
myCamera = Camera.getCamera();
//vidObj.attachCamera(myCamera);
myMic=Microphone.getMicrophone();

// Connect to oflaDemo demo application in red5 server
pub_nc = new NetConnection();
NetConnection.prototype.onBWDone = function(p_bw) {
trace(“onBWDone: “);
}
pub_nc.addEventListener(NetStatusEvent.NET_STATUS, pub_netStatus);
pub_nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, netSecurityError);

// Should be configurable
pub_nc.connect(Config.streamingURL);

trace(Config.streamingURL);

}

// publish connection status callback
function pub_netStatus(event:NetStatusEvent):void {
trace(“netStatus: ” + event);
var info:Object = event.info;
trace(info.code);

// lots more code here…
if (info.code == “NetConnection.Connect.Success”) {
// Publish video from the camera to the red5 server
pub_ns = new NetStream(pub_nc);
this.publish();
}

else{
trace(“error”);
}

}

public function publish():void{
pub_ns.attachAudio(myMic);
pub_ns.attachCamera(myCamera);
pub_ns.publish(Config.stPubPoint, “live”);

}

public function subscribe():void{
play_ns.play(Config.stPubPoint);
vidObj.attachNetStream(play_ns);

}

public function muteAudio():void{
//myMic.muted=true;
}

public function startAudio():void{

}

public function start_stop_subscribe():void{
play_ns.pause();

}

public function start_stop_pub():void{
pub_ns.pause();

}

}// End of class

}

package lib {
import lib.QueryString;

public class Config{
//public function Config(){
// Default value;
/* streamingURL= _root.streamingURL ? _root.streamingURL : “rtmp://127.0.0.1/oflaDemo”;
messHost=_root.messHost ? _root.messHost : “127.0.0.1″;
messPort=_root.messPort ? _root.messPort : 8081;
stPubPoint=_root.stPubPoint ? _root.stPubPoint : “example”;
pubID=_root.pubID ? _root.pubID : “himu”;
pageURL=_root.pageURL ? _root.pageURL : “http://test.com”;
*/
// This should be intialized from the URL
public static var qs:QueryString= new QueryString;
//public static var streamingURL:String=qs.parameters(’streamingURL’);
//public static var stPubPoint:String=qs.parameters(’stPubPoint’);
public static var streamingURL:String=”rtmp://yourip/oflaDemo”;
public static var stPubPoint:String=”example”;

}

}

http://www.chakribazaar.com

, a good job site

http://www.mappedia.net

Comments (2)