Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import com.junkbyte.console.Cc;
- import flash.events.AsyncErrorEvent;
- import flash.events.NetStatusEvent;
- import flash.events.SecurityErrorEvent;
- import flash.media.Video;
- import flash.net.GroupSpecifier;
- import flash.net.NetConnection;
- import flash.net.NetGroup;
- import flash.net.NetStream;
- import flash.net.NetStreamAppendBytesAction;
- import flash.utils.ByteArray;
- public class PeerVideoPlayer
- {
- private var _nc_broadcasting:NetConnection;
- private var _groupSpec:GroupSpecifier;
- private var _netGroup:NetGroup;
- private var _video:Video;
- private var _ns:NetStream;
- private var _isMetaData:Boolean = false;
- public function PeerVideoPlayer()
- {
- }
- public function start():void
- {
- buildBroadcasting();
- }
- private function buildVideo():void
- {
- /* var nc:NetConnection = new NetConnection();
- nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityHandler);
- nc.addEventListener(NetStatusEvent.NET_STATUS, connectionStatusHandler);
- nc.connect(null);
- if (_ns != null)
- {
- _ns.close();
- }*/
- _ns = new NetStream(_nc_broadcasting, _groupSpec.groupspecWithoutAuthorizations());
- _ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
- _ns.addEventListener(NetStatusEvent.NET_STATUS, streamStatusHandler);
- _ns.client = {};
- _ns.client.onMetaData = function(metadata:Object):void
- {
- var obj:Object = {};
- for (var key:String in metadata)
- {
- obj[key] = metadata[key];
- }
- Cc.log("Video metadata:");
- obj.o();
- Cc.log("Video duration: ", Math.ceil(metadata.duration), " sec");
- }
- _ns.client.onVideoBytes = function(bytes:ByteArray):void
- {
- Cc.log("onVideoBytes: ", bytes.length);
- // _ns.appendBytes(bytes);
- }
- _video = new Video();
- _video.width = 3840;
- _video.height = 1080;
- _video.opaqueBackground = 0x000000;
- Main.__stage1.addChild(_video);
- _video.attachNetStream(_ns);
- _ns.play("video");
- Cc.log("NS PLAY: ", _ns.farID);
- // _ns.play(null);
- _ns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
- Cc.log("Video stream started, waiting for peer bytes...");
- }
- public function buildBroadcasting():void
- {
- _nc_broadcasting = new NetConnection();
- _nc_broadcasting.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityHandler);
- _nc_broadcasting.addEventListener(NetStatusEvent.NET_STATUS, broadcastingStatusHandler);
- _nc_broadcasting.connect("rtmfp:");
- }
- private function setupNetGroup():void
- {
- _groupSpec = new GroupSpecifier("videoGroup");
- _groupSpec.serverChannelEnabled = true;
- _groupSpec.postingEnabled = true;
- _groupSpec.multicastEnabled = true;
- _groupSpec.ipMulticastMemberUpdatesEnabled = true;
- _groupSpec.addIPMulticastAddress("225.225.0.1:30300");
- _netGroup = new NetGroup(_nc_broadcasting, _groupSpec.groupspecWithoutAuthorizations());
- _netGroup.addEventListener(NetStatusEvent.NET_STATUS, broadcastingStatusHandler);
- }
- private function broadcastingStatusHandler(e:NetStatusEvent):void
- {
- Cc.log("BROADCASTING STATUS: ", e.info.code);
- switch (e.info.code)
- {
- case "NetConnection.Connect.Success":
- setupNetGroup();
- break;
- case "NetGroup.Connect.Success":
- // buildBroadcastingStream();
- //startVideoStream();
- //buildVideo();
- break;
- case "NetGroup.MulticastStream.PublishNotify":
- buildVideo();
- break;
- case "NetGroup.Posting.Notify":
- switch (e.info.message.act)
- {
- case "videoData":
- Cc.log("Receive video metadata from stream publisher");
- //e.info.message.metadata.o();
- //_ns.send("@clearDataFrame", "onMetaData");
- //_ns.send("@setDataFrame", "onMetaData", e.info.message.metadata);
- //_ns.client = {};
- //
- //_ns.send("onMetaData", e.info.message.metadata);
- //_ns.send("@setDataFrame", "onMetaData", e.info.message.metadata);
- _isMetaData = true;
- // _ns.appendBytesAction(NetStreamAppendBytesAction.RESET_SEEK);
- break;
- default:
- /*var bytes:ByteArray = e.info.message.data;
- _ns.appendBytes(bytes);
- bytes.clear();*/
- }
- break;
- }
- }
- private function connectionStatusHandler(e:NetStatusEvent):void
- {
- Cc.log(e.info.code);
- }
- private function securityHandler(e:SecurityErrorEvent):void
- {
- Cc.log(e);
- }
- static private function streamStatusHandler(e:NetStatusEvent):void
- {
- // End of video
- Cc.log(e.info.code);
- if (e.info.code == "NetStream.Buffer.Empty")
- {
- //Main.onAppClose();
- }
- Cc.log(e.info.code);
- }
- private function asyncErrorHandler(e:AsyncErrorEvent):void
- {
- Cc.log(e.error);
- }
- public function get __video():Video
- {
- return _video;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement