|
<?xml version="1.0" encoding="utf-8"?> |
|
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" |
|
xmlns:s="library://ns.adobe.com/flex/spark" |
|
xmlns:mx="library://ns.adobe.com/flex/mx" |
|
creationComplete="windowedapplication1_creationCompleteHandler(event)"> |
|
<fx:Declarations> |
|
<!-- Place non-visual elements (e.g., services, value objects) here --> |
|
</fx:Declarations> |
|
<fx:Script> |
|
|
|
<![CDATA[ |
|
import flash.display.MovieClip; |
|
import flash.events.NetStatusEvent; |
|
import flash.media.Camera; |
|
import flash.media.Microphone; |
|
import flash.media.Video; |
|
import flash.net.NetConnection; |
|
import flash.net.NetStream; |
|
|
|
import mx.charts.CategoryAxis; |
|
import mx.collections.ArrayCollection; |
|
import mx.controls.Alert; |
|
import mx.core.UIComponent; |
|
import mx.events.FlexEvent; |
|
var nc:NetConnection; |
|
var nc2:NetConnection; |
|
var ns:NetStream; |
|
var nsPlayer:NetStream; |
|
var vid:Video; |
|
var vidPlayer:Video; |
|
var cam:Camera; |
|
var mic:Microphone; |
|
var talk_so:SharedObject; |
|
var screen_w:int=320; |
|
var screen_h:int=240; |
|
var now_people:Number; |
|
[Bindable] |
|
var ready:Boolean; |
|
var media_server:Boolean; |
|
var shareObject_server:Boolean; |
|
|
|
protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void |
|
{ |
|
publish.enabled=false; |
|
send_shareobject.enabled=false; |
|
} |
|
public function onBWDone():void { |
|
trace("11"); |
|
} |
|
public function onBWDone2():void { |
|
trace("11"); |
|
} |
|
|
|
private function onNetStatus(event:NetStatusEvent):void{ |
|
trace(event.info.code); |
|
|
|
if(event.info.code == "NetConnection.Connect.Success"){ |
|
// |
|
|
|
publish.enabled=true; |
|
|
|
} |
|
else |
|
{ |
|
trace ("連接失敗"+event.info.code); |
|
|
|
} |
|
} |
|
|
|
private function netStatusHandler(evt:NetStatusEvent):void |
|
{ |
|
trace(evt.info.code); //调试代码用 |
|
|
|
|
|
if ( evt.info.code =="NetConnection.Connect.Success" ) |
|
{ |
|
// nc2.client ={ onBWDone: function():void{} }; |
|
|
|
// talk_so = SharedObject.getRemote("talk",nc2.uri,true); |
|
// |
|
// trace(nc2.uri) |
|
// talk_so.addEventListener(SyncEvent.SYNC,talkSoSyncHandler); |
|
// talk_so.connect(nc2); |
|
//talk_so.fps=0.1; |
|
talk_so = SharedObject.getRemote("userList",nc2.uri,false); |
|
talk_so.connect(nc2); |
|
|
|
talk_so.addEventListener(SyncEvent.SYNC,talkSoSyncHandler); |
|
|
|
send_shareobject.enabled=true; |
|
trace ("連接房間成功!"); |
|
} |
|
else |
|
{ |
|
trace ("連接不到房間!"); |
|
} |
|
} |
|
|
|
|
|
|
|
private function publishCamera( publish_name:String,play_type:String) |
|
{ |
|
|
|
//Cam |
|
try{ |
|
cam = Camera.getCamera(); |
|
cam.setMode(640, 480,60); |
|
|
|
/** |
|
* public function setKeyFrameInterval(keyFrameInterval:int):void |
|
* The number of video frames transmitted in full (called keyframes) instead of being interpolated by the video compression algorithm. |
|
* The default value is 15, which means that every 15th frame is a keyframe. A value of 1 means that every frame is a keyframe. |
|
* The allowed values are 1 through 300. |
|
*/ |
|
cam.setKeyFrameInterval(1); |
|
|
|
/** |
|
* public function setQuality(bandwidth:int, quality:int):void |
|
* bandwidth:int — Specifies the maximum amount of bandwidth that the current outgoing video feed can use, in bytes per second (bps). |
|
* To specify that the video can use as much bandwidth as needed to maintain the value of quality, pass 0 for bandwidth. |
|
* The default value is 16384. |
|
* quality:int — An integer that specifies the required level of picture quality, as determined by the amount of compression |
|
* being applied to each video frame. Acceptable values range from 1 (lowest quality, maximum compression) to 100 |
|
* (highest quality, no compression). To specify that picture quality can vary as needed to avoid exceeding bandwidth, |
|
* pass 0 for quality. |
|
*/ |
|
cam.setQuality(0,100); |
|
|
|
/** |
|
* public function setProfileLevel(profile:String, level:String):void |
|
* Set profile and level for video encoding. |
|
* Possible values for profile are H264Profile.BASELINE and H264Profile.MAIN. Default value is H264Profile.BASELINE. |
|
* Other values are ignored and results in an error. |
|
* Supported levels are 1, 1b, 1.1, 1.2, 1.3, 2, 2.1, 2.2, 3, 3.1, 3.2, 4, 4.1, 4.2, 5, and 5.1. |
|
* Level may be increased if required by resolution and frame rate. |
|
*/ |
|
//var h264setting:H264VideoStreamSettings = new H264VideoStreamSettings(); |
|
// h264setting.setProfileLevel(H264Profile.MAIN, 4); |
|
|
|
|
|
//Mic |
|
|
|
mic = Microphone.getMicrophone(); |
|
|
|
/* |
|
* The encoded speech quality when using the Speex codec. Possible values are from 0 to 10. The default value is 6. |
|
* Higher numbers represent higher quality but require more bandwidth, as shown in the following table. |
|
* The bit rate values that are listed represent net bit rates and do not include packetization overhead. |
|
* ------------------------------------------ |
|
* Quality value | Required bit rate (kbps) |
|
*------------------------------------------- |
|
* 0 | 3.95 |
|
* 1 | 5.75 |
|
* 2 | 7.75 |
|
* 3 | 9.80 |
|
* 4 | 12.8 |
|
* 5 | 16.8 |
|
* 6 | 20.6 |
|
* 7 | 23.8 |
|
* 8 | 27.8 |
|
* 9 | 34.2 |
|
* 10 | 42.2 |
|
*------------------------------------------- |
|
*/ |
|
mic.encodeQuality = 9; |
|
|
|
/* The rate at which the microphone is capturing sound, in kHz. Acceptable values are 5, 8, 11, 22, and 44. The default value is 8 kHz |
|
* if your sound capture device supports this value. Otherwise, the default value is the next available capture level above 8 kHz that |
|
* your sound capture device supports, usually 11 kHz. |
|
* |
|
*/ |
|
mic.rate = 44; |
|
|
|
|
|
ns = new NetStream(nc); |
|
|
|
//H.264 Setting |
|
//ns.videoStreamSettings = h264setting; |
|
ns.attachCamera(cam); |
|
ns.attachAudio(mic); |
|
ns.publish(publish_name, play_type); |
|
|
|
} |
|
catch(error:Error) |
|
{ |
|
cam=null; |
|
trace ("找不到視訊鏡頭"); |
|
|
|
return ; |
|
} |
|
|
|
/** |
|
* public function setMode(width:int, height:int, fps:Number, favorArea:Boolean = true):void |
|
* width:int — The requested capture width, in pixels. The default value is 160. |
|
* height:int — The requested capture height, in pixels. The default value is 120. |
|
* fps:Number — The requested capture frame rate, in frames per second. The default value is 15. |
|
*/ |
|
|
|
|
|
} |
|
|
|
|
|
private function displayPublishingVideo():void { |
|
trace ("開始撥放當前視訊鏡頭"); |
|
if(cam != null){ |
|
vid = new Video(screen_w, screen_h); |
|
// vid.x = 10; |
|
// vid.y = 10; |
|
vid.attachCamera(cam); |
|
|
|
var tmp:UIComponent = new UIComponent(); |
|
tmp.addChild(vid); |
|
canvas.addElement(tmp); |
|
} |
|
} |
|
|
|
|
|
private function displayPlaybackVideo(publish_name:String):void{ |
|
trace ("開始撥放返回數據流"); |
|
|
|
nsPlayer = new NetStream(nc); |
|
nsPlayer.bufferTime = 0.1; |
|
nsPlayer.play(publish_name); |
|
|
|
|
|
vidPlayer = new Video(screen_w, screen_h); |
|
// vidPlayer.x = screen_w + 20; |
|
// vidPlayer.y = 10; |
|
// |
|
vidPlayer.attachNetStream(nsPlayer); |
|
var tmp:UIComponent = new UIComponent(); |
|
tmp.addChild(vidPlayer); |
|
canvas2.addElement(tmp); |
|
|
|
} |
|
|
|
|
|
protected function publish_clickHandler(event:MouseEvent):void |
|
{ |
|
// TODO Auto-generated method stub |
|
|
|
trace ("開始推送數據流"); |
|
publishCamera(publish_name.text,publish_type.text); |
|
displayPlaybackVideo(getback_name.text); |
|
displayPublishingVideo(); |
|
|
|
|
|
} |
|
|
|
protected function connection_clickHandler(event:MouseEvent):void |
|
{ |
|
// TODO Auto-generated method stub |
|
|
|
|
|
// TODO Auto-generated method stub |
|
|
|
try{ |
|
nc = new NetConnection(); |
|
nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus); |
|
nc.connect(publish_address.text); |
|
nc.client ={ onBWDone: function():void{} }; |
|
|
|
// TODO Auto-generated method stub |
|
nc2 = new NetConnection(); |
|
nc2.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); |
|
nc2.connect(shareObject_address.text,shareObject_name.text); |
|
nc2.client ={ onBWDone: function():void{} }; |
|
nc2.client.showmsg = function (str:String):void |
|
{ |
|
|
|
msg.text=msg.text+str+"\n"; |
|
}; |
|
|
|
|
|
} |
|
catch(error:Error) |
|
{ |
|
trace (error.message); |
|
} |
|
|
|
|
|
} |
|
|
|
protected function disconnection(event:MouseEvent):void |
|
{ |
|
// TODO Auto-generated method stub |
|
nc.close(); |
|
nc2.close(); |
|
nc=null; |
|
nc=null; |
|
} |
|
|
|
protected function send_shareobject_clickHandler(event:MouseEvent):void |
|
{ |
|
// TODO Auto-generated method stub |
|
nc2.call("sendmsg",null,shareObject_msg.text); |
|
// var arr:ArrayCollection = new ArrayCollection(); |
|
// |
|
// |
|
// |
|
// if ( talk_so.data.msgList==null ) |
|
// { |
|
// arr = new ArrayCollection(); |
|
// } |
|
// else |
|
// { |
|
// convertArrayCollection(arr,talk_so.data.msgList as ArrayCollection); |
|
// } |
|
// |
|
// var obj:message = new message(); |
|
// obj.nickname="x213212"; |
|
// obj.msg=shareObject_msg.text; |
|
// obj.time = new Date(); |
|
// |
|
// arr.addItem(obj); |
|
// |
|
// talk_so.setProperty("msgList",arr); |
|
// |
|
} |
|
|
|
private function convertArrayCollection(arrNew:ArrayCollection,arrOld:ArrayCollection):void |
|
{ |
|
arrNew.removeAll(); |
|
|
|
for(var i:int=0;i<arrOld.length ;i++) |
|
{ |
|
arrNew.addItemAt(arrOld.getItemAt(i),i); |
|
} |
|
} |
|
|
|
private function talkSoSyncHandler(evt:SyncEvent):void |
|
{ |
|
var tmp:ArrayCollection = new ArrayCollection(); |
|
msg.text=""; |
|
|
|
if ( talk_so.data.msgList!=null ) |
|
{ |
|
convertArrayCollection(tmp,talk_so.data.msgList as ArrayCollection); |
|
|
|
for(var i:int=0;i<tmp.length ;i++) |
|
{ |
|
var msg:Object = tmp.getItemAt(i); |
|
|
|
var fullMsg:String=msg.nickname+"in"+msg.time.toTimeString()+"join:"+msg.msg; |
|
msg.text=msg.text+fullMsg+"\n"; |
|
trace (fullMsg); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
]]> |
|
</fx:Script> |
|
<s:VGroup> |
|
<s:HGroup> |
|
<s:VGroup width="33%"> |
|
<s:TextInput id="publish_address" text="rtmp://localhost/live"> |
|
|
|
</s:TextInput> |
|
<s:TextInput id="shareObject_address" text="rtmp://localhost/room"> |
|
|
|
</s:TextInput> |
|
<s:Button click="connection_clickHandler(event)" label="連接伺服器"> |
|
|
|
</s:Button > |
|
<s:Button click="disconnection(event)" label="斷開伺服器"> |
|
|
|
</s:Button > |
|
</s:VGroup> |
|
<s:VGroup width="33%"> |
|
<s:TextInput id="publish_name" text="myCamera"> |
|
|
|
</s:TextInput> |
|
<s:TextInput id="getback_name" text="myCamera2"> |
|
|
|
</s:TextInput> |
|
<s:TextInput id="publish_type" text="live"> |
|
|
|
</s:TextInput> |
|
<s:Button id="publish" click="publish_clickHandler(event)" label="推送數據流" > |
|
|
|
</s:Button > |
|
</s:VGroup> |
|
<s:VGroup width="33%"> |
|
<s:TextInput id="shareObject_name" text="msgList"> |
|
|
|
</s:TextInput> |
|
<s:TextInput id="shareObject_msg" text="test"> |
|
|
|
</s:TextInput> |
|
<s:Button id="send_shareobject" click="send_shareobject_clickHandler(event)" label="發送訊息" > |
|
|
|
</s:Button > |
|
|
|
</s:VGroup> |
|
|
|
</s:HGroup> |
|
<s:HGroup> |
|
<mx:Canvas id="canvas" height="{screen_h}" width="{screen_w}"> |
|
|
|
</mx:Canvas> |
|
|
|
<mx:Canvas id="canvas2" height="{screen_h}" width="{screen_w}"> |
|
|
|
</mx:Canvas> |
|
</s:HGroup> |
|
<s:HGroup> |
|
<s:TextInput id="msg" height="159" width="366"> |
|
|
|
</s:TextInput> |
|
</s:HGroup> |
|
|
|
</s:VGroup> |
|
|
|
</s:Application> |