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.ProgressEvent;
- import flash.events.SecurityErrorEvent;
- import flash.events.TimerEvent;
- import flash.filesystem.File;
- import flash.filesystem.FileMode;
- import flash.filesystem.FileStream;
- import flash.media.SoundTransform;
- 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;
- import flash.utils.Timer;
- public class StreamVideoPublisher
- {
- static private var _timer:Timer = new Timer(40);
- static private var _currentPoint:int;
- static private var _oldPoint:int = 0;
- static private var _ns:NetStream;
- static private var _video:Video;
- static private var _nc_broadcasting:NetConnection;
- static private var _ns_broadcasting:NetStream;
- static private var _groupSpec:GroupSpecifier;
- static private var _netGroup:NetGroup;
- static private var _metadata:Object;
- static private var _bytesLimit:int = 1024 * 1024 * 5;
- static private var _currentBytesLimit:int;
- static private var _bufferLimit:int = 10;
- static private var _filesize:int = 0;
- static private var _fs:FileStream;
- static private var _bytes:ByteArray;
- static private var _bytesLoaded:int = 0;
- static private var _bytesTotal:int = 0;
- static private var _isChunkLoaded:Boolean = false;
- static private var _chunkCounter:int = 0;
- static private var _isLoaded:Boolean = false;
- public function StreamVideoPublisher()
- {
- }
- static public function setClass():void
- {
- _timer.addEventListener(TimerEvent.TIMER, onTimer);
- }
- static public function start():void
- {
- buildBroadcasting();
- }
- static 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:");
- }
- static 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);
- }
- static private function buildBroadcastingStream():void
- {
- _ns_broadcasting = new NetStream(_nc_broadcasting, _groupSpec.groupspecWithoutAuthorizations());
- _ns_broadcasting.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
- _ns_broadcasting.addEventListener(NetStatusEvent.NET_STATUS, broadcastingStatusHandler);
- var broadcastClient:Object = new Object();
- broadcastClient.onPeerConnect = function(caller_ns:NetStream):Boolean
- {
- Cc.log("broadcast onPeerConnect: ", caller_ns.farID);
- return true;
- }
- _ns_broadcasting.client = broadcastClient;
- _ns_broadcasting.publish("video");
- }
- static 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();
- break;
- case "NetGroup.Neighbor.Connect":
- // _netGroup.post( { act: "videoData", metadata: _metadata } );
- _ns_broadcasting.send("onMetaData", _metadata);
- Cc.log("Send video metadata to neighbor client");
- break;
- case "NetStream.Connect.Success":
- startVideoStream();
- break;
- }
- }
- static private function loadData():void
- {
- var file:File = new File(Main.__config.video);
- if (!file.exists)
- {
- Cc.log("File or directory does not exist. ", Main.__config.video);
- return;
- }
- _bytes = new ByteArray;
- _bytesTotal = file.size;
- _fs = new FileStream();
- _fs.readAhead = _bytesLimit;
- _fs.addEventListener(ProgressEvent.PROGRESS, onBytesRead);
- var remainBytes:int = _bytesTotal - _bytesLoaded;
- _currentBytesLimit = remainBytes > _bytesLimit ? _bytesLimit : remainBytes;
- _fs.openAsync(file, FileMode.READ);
- _fs.position = _bytesLoaded;
- if (_bytesTotal == _bytesLoaded)
- {
- _isLoaded = true;
- }
- }
- static private function onBytesRead(e:ProgressEvent):void
- {
- _fs.readBytes(_bytes, _bytes.length, _fs.bytesAvailable);
- if (_bytes.length >= _currentBytesLimit)
- {
- chunkReady();
- }
- }
- static private function chunkReady():void
- {
- _fs.removeEventListener(ProgressEvent.PROGRESS, onBytesRead);
- _fs.close();
- _bytesLoaded += _bytes.length;
- if (_chunkCounter == 0)
- {
- // decrypt the first megabyte
- bytesDecode(_bytes);
- }
- _ns.appendBytes(_bytes);
- // _netGroup.post({act: "videoChunk", data: _bytes});
- _ns_broadcasting.send("onVideoBytes", _bytes);
- _bytes.clear();
- _isChunkLoaded = true;
- Cc.log("chunkReady", _currentBytesLimit);
- }
- static public function startVideoStream():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);
- _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];
- }
- _metadata = obj;
- Cc.log("Video metadata:");
- obj.o();
- Cc.log("Video duration: ", Math.ceil(metadata.duration), " sec");
- }
- var soundTransform:SoundTransform = new SoundTransform();
- soundTransform.volume = 0;
- _ns.soundTransform = soundTransform;
- /* _video = new Video();
- _video.width = 3840;
- _video.height = 1080;
- _video.opaqueBackground = 0x000000;
- Main.__stage1.addChild(_video);
- _video.attachNetStream(_ns);
- */
- _ns.play(null);
- _ns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
- _timer.start();
- Cc.log("Video stream started, waiting for bytes...");
- // then load data
- loadData();
- }
- static private function securityHandler(e:SecurityErrorEvent):void
- {
- Cc.log(e);
- }
- static private function asyncErrorHandler(e:AsyncErrorEvent):void
- {
- Cc.log(e.error);
- }
- static private function streamStatusHandler(e:NetStatusEvent):void
- {
- // End of video
- if (e.info.code == "NetStream.Buffer.Empty" && _isLoaded)
- {
- //Main.onAppClose();
- }
- Cc.log(e.info.code);
- }
- static private function connectionStatusHandler(e:NetStatusEvent):void
- {
- Cc.log(e.info.code);
- }
- static private function onTimer(e:TimerEvent):void
- {
- _currentPoint = Math.floor(_ns.time - 0.025);
- if (_currentPoint != _oldPoint)
- {
- if (_isChunkLoaded && _ns.bufferLength < _bufferLimit)
- {
- _isChunkLoaded = false;
- _chunkCounter++;
- loadData();
- }
- var fx:String = Main.__fx[_currentPoint];
- if (fx != null)
- {
- var str:String = "";
- str += "\n";
- str += "Video real time: " + _ns.time + " sec" + "\n";
- str += "Video current point: " + _currentPoint + " sec" + "\n";
- str += "FX data send to Cinema control panel" + "\n";
- //str += "FX data: " + fx + "\n";
- // Cc.log(str);
- }
- }
- _oldPoint = _currentPoint;
- }
- static private function bytesDecode(bytes:ByteArray):void
- {
- var i:int = 0;
- while (i++ < 1024 * 1024)
- {
- if (bytes[i] <= 127)
- {
- bytes[i] -= 128;
- }
- else
- {
- bytes[i] += 128;
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment