├── test.js ├── rtmpApi.js ├── .gitignore ├── log.js ├── package.json ├── rtmpServer.js ├── rtmpMessage.js ├── readQueue.js ├── README.md ├── rtmpSession.js ├── LICENSE └── rtmpChunk.js /test.js: -------------------------------------------------------------------------------- 1 | var rtmp = require('./rtmpApi.js'); 2 | 3 | console.log(rtmp.rtmpServer); 4 | -------------------------------------------------------------------------------- /rtmpApi.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | log: require('./log.js'), 3 | rtmpServer: require('./rtmpServer.js'), 4 | rtmpSession: require('./rtmpSession.js'), 5 | rtmpQueue: require('./readQueue.js'), 6 | rtmpChunk: require('./rtmpChunk.js'), 7 | rtmpMessage: require('./rtmpMessage.js') 8 | } 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Deployed apps should consider commenting this line out: 24 | # see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git 25 | node_modules 26 | -------------------------------------------------------------------------------- /log.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by delian on 3/11/14. 3 | * Easy implementation of Logging. Always print Data and some prefix in front of it 4 | */ 5 | 6 | module.exports = function(sDebug,sRaddr) { 7 | var debug = sDebug; 8 | var raddr = sRaddr||'none'; 9 | 10 | return { 11 | log: function() { 12 | if (debug) { 13 | for (var z = [], k = arguments.length-1; k>=0; k--) z[k]=arguments[k]; 14 | console.log.apply(this,[new Date,raddr,'>'].concat(z)); 15 | } 16 | }, 17 | debug: function(sDebug) { 18 | debug = sDebug; 19 | }, 20 | raddr: function(sRaddr) { 21 | raddr = sRaddr; 22 | } 23 | }; 24 | } 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-rtmpapi", 3 | "version": "0.0.1", 4 | "description": "Node.JS RTMP api providing an easy way of creating RTMP servers or clients", 5 | "main": "rtmpApi.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/delian/node-rtmpapi.git" 12 | }, 13 | "keywords": [ 14 | "adobe", 15 | "rtmp", 16 | "flash", 17 | "server" 18 | ], 19 | "author": "Delian Delchev (http://delian.blogspot.com/)", 20 | "license": "GPLv2", 21 | "bugs": { 22 | "url": "https://github.com/delian/node-rtmpapi/issues" 23 | }, 24 | "homepage": "https://github.com/delian/node-rtmpapi", 25 | "dependencies": { 26 | "node-amfutils": "0.0.1" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /rtmpServer.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by delian on 3/11/14. 3 | * 4 | * Simple implmenetation of an RTMP Server 5 | * 6 | */ 7 | 8 | var net = require('net'); 9 | var Log = require('./log.js'); 10 | var rtmpSession = require('./rtmpSession.js'); 11 | var debug = 0; 12 | 13 | module.exports = function() { 14 | Log(debug).log('Create RTMP Server Object'); 15 | var ret = {}; 16 | ret.createServer = function(cb) { 17 | Log(debug).log('Create RTMP Server'); 18 | return net.createServer(function(sock) { 19 | sock.setMaxListeners(100); // Warning related to the amount of CS id's 20 | rtmpSession(sock,0,function() { 21 | Log(debug).log('Callback the Server callback!'); 22 | var me = this; 23 | cb.apply(me,arguments); // Call the CallBack preserving the object 24 | }); 25 | }); 26 | }; 27 | return ret; 28 | }; -------------------------------------------------------------------------------- /rtmpMessage.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by delian on 3/11/14. 3 | * 4 | * This little module should provide interface that will allow processing of an RTMP message 5 | */ 6 | 7 | var chunk = require('./rtmpChunk.js'); 8 | var Log = require('./log.js'); 9 | 10 | /** 11 | * Create RtmpMessageClass 12 | * @param rSess rtmpSession object 13 | * @returns {RtmpMessageClass} 14 | */ 15 | function RtmpMessageClass(opt) { 16 | if (!(this instanceof RtmpMessageClass)) return new RtmpMessageClass(rSess); 17 | this.opt = {}; 18 | this.Q = {}; 19 | this.sock = null; 20 | if (opt.Q) { this.Q = opt.Q; this.sock = opt.Q.sock; } 21 | if (opt.sock) this.sock = opt.sock; 22 | if (opt) this.opt = opt; 23 | this.log = Log(opt.debug,(this.sock?this.sock.remoteAddress:'')+':'+(this.sock?this.sock.remotePort:'')).log; 24 | this.chunk = new chunk(opt); 25 | this.getTs = this.chunk.getTs; 26 | } 27 | 28 | /** 29 | * Define READ one message operation 30 | * @param cb 31 | * @param cbOpts an object defining specific callbacks per message 32 | */ 33 | RtmpMessageClass.prototype.read = function(cb,cbOpts) { 34 | var me = this; 35 | var rtmpMsgProc = function(chunkObj) { 36 | if (chunkObj.chunk.msgComplete) { 37 | if (cb) cb(chunkObj); 38 | if ((typeof cbOpts == 'object')&&(typeof cbOpts[chunkObj.chunk.msgTypeText] == 'function')) cbOpts[chunkObj.chunk.msgTypeText](chunkObj); // Call the specific callback 39 | if ((chunkObj.Q.recvBytes - chunkObj.Q.ackBytes)>=chunkObj.Q.chunkSize.rcvWinSize) chunkObj.sendAckMsg(); 40 | } else { 41 | me.log('We received one chunk, but the message is incomplete',chunkObj.chunk); 42 | return me.chunk.read(rtmpMsgProc); 43 | } 44 | }; 45 | return me.chunk.read(rtmpMsgProc); 46 | }; 47 | 48 | /** 49 | * Define LOOP between multiple reads operation 50 | * @param cb 51 | * @param cbOpts an object defining specific callbacks per message 52 | */ 53 | RtmpMessageClass.prototype.loop = function(cb,cbOpts) { 54 | var me = this; 55 | me.Q.defaultCb = function() { 56 | me.log('Message Loop default callback!'); 57 | return me.read(cb,cbOpts); 58 | }; 59 | me.read(cb,cbOpts); 60 | }; 61 | 62 | module.exports = RtmpMessageClass; 63 | -------------------------------------------------------------------------------- /readQueue.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by delian on 3/11/14. 3 | * This file implements a queue for read certain amount of bytes from the input stream 4 | */ 5 | 6 | var Log = require('./log.js'); 7 | var debug = 0; 8 | var queueLength = 10000000; 9 | 10 | /** 11 | * Read Queue implementation 12 | * @param sock 13 | * @returns {QueueClass} 14 | * @constructor 15 | */ 16 | function QueueClass(sock) { 17 | if (!(this instanceof QueueClass)) return new QueueClass(sock); 18 | 19 | var me = this; 20 | 21 | me.readQueue = []; 22 | me.sock = sock; 23 | 24 | me.lock = false; 25 | 26 | me.recvBytes = 0; 27 | me.ackBytes = 0; 28 | 29 | me.buffer = new Buffer(queueLength); 30 | me.readIndex = 0; 31 | me.writeIndex = 0; 32 | me.bufferEnd = me.buffer.length; 33 | 34 | me.defaultCb = null; 35 | 36 | me.log = Log(debug, sock.remoteAddress + ':' + sock.remotePort).log; 37 | me.log('QUEUE: Open Read Queue'); 38 | 39 | sock.on('data',function(data) { 40 | var freespace = 0; 41 | 42 | function writeBuf() { 43 | data.copy(me.buffer,me.writeIndex); 44 | me.writeIndex+=data.length; 45 | me.bufferEnd=Math.max(me.writeIndex,me.bufferEnd); 46 | me.log('QUEUE: Readable event has been received'); 47 | if (me.readQueue.length==0) { 48 | if (typeof me.defaultCb == 'function') { 49 | me.log('QUEUE: The queue is empty. Call the default callback if any'); 50 | me.defaultCb(); 51 | } 52 | } 53 | return me.tryRead(); 54 | } 55 | 56 | if (me.writeIndexdata.length) return writeBuf(); 60 | 61 | // In case we have no enough freespace 62 | if (me.writeIndex>=me.readIndex) { 63 | me.bufferEnd=me.writeIndex; 64 | me.writeIndex=0; 65 | } 66 | 67 | freespace = me.readIndex-me.writeIndex; 68 | if (freespace>data.length) return writeBuf(); 69 | 70 | // No enough data 71 | throw new Error('No enough data space, slow reading!'); 72 | 73 | }); 74 | 75 | sock.on('close', function() { 76 | me.log('QUEUE: Socket has been closed, remove the queue tasks!'); 77 | me.readQueue = []; 78 | me.defaultCb = null; 79 | me.sock = null; 80 | }) 81 | } 82 | 83 | /** 84 | * Add an element to the Queue and check for data 85 | * @param len 86 | * @param cb 87 | * @returns {undefined} 88 | * @constructor 89 | */ 90 | QueueClass.prototype.Q = function (len, cb) { 91 | var me = this; 92 | me.readQueue.push({ len: len, cb: cb }); 93 | return me.tryRead(); // Immediate try, allowing us to keep the order 94 | }; 95 | 96 | /** 97 | * Return how many bytes we can read 98 | * @returns {number} 99 | */ 100 | QueueClass.prototype.getBufLen = function() { 101 | if (this.writeIndexthis.getBufLen()) return null; 108 | if (this.readIndex=bytes) { 109 | buf = this.buffer.slice(this.readIndex,this.readIndex+bytes); 110 | this.readIndex+=bytes; 111 | return buf; 112 | } 113 | 114 | // A special case, where the chunk is on the border 115 | 116 | console.log('Border copy, bytes',bytes,'readIndex',this.readIndex,'bufferEnd',this.bufferEnd,this.bufferEnd-this.readIndex,bytes-(this.bufferEnd-this.readIndex)); 117 | buf = new Buffer(bytes); 118 | this.buffer.copy(buf,0,this.readIndex,this.bufferEnd); 119 | this.buffer.copy(buf,this.bufferEnd-this.readIndex, 0, bytes-(this.bufferEnd-this.readIndex)); 120 | this.readIndex = bytes-(this.bufferEnd-this.readIndex); 121 | return buf; 122 | }; 123 | 124 | /** 125 | * This function try to read all the tasks set in to the read queue, as long as there are enough data in the receive buffer 126 | * @returns {undefined} 127 | */ 128 | QueueClass.prototype.tryRead = function() { 129 | var data; 130 | var readQueue = this.readQueue; 131 | var sock = this.sock; 132 | // var log = this.log; 133 | 134 | if (this.lock || readQueue.length==0) return; 135 | if (readQueue[0].len>this.getBufLen()) return; 136 | 137 | this.lock = true; 138 | while (1) { 139 | var rq = readQueue.shift(); 140 | if (!rq) { 141 | this.lock = false; 142 | return; // Nothing to do for now 143 | } 144 | 145 | if (rq.len == 0) { 146 | rq.cb(); 147 | } else if (rq.len < 0) { 148 | if (this.getBufLen()<=0) { 149 | this.lock = false; 150 | return readQueue.unshift(rq); 151 | } 152 | rq.cb(); 153 | } else { 154 | data = this.read(rq.len); 155 | if (!data) { 156 | this.lock = false; 157 | return readQueue.unshift(rq); 158 | } // Not enough data yet, push back the request and quit the queue 159 | this.recvBytes += data.length; // Increase the counter 160 | // log('Received enough data', data); 161 | rq.cb(data); 162 | } 163 | } 164 | }; 165 | 166 | module.exports = QueueClass; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | node-rtmpapi 2 | ============ 3 | 4 | A Node.JS module that provides easy RTMP API for implementation of RTMP Server or client. 5 | 6 | This API provides both separate as well as hidden apis that provides sequencing, execution, encoding, decoding of chunk and messages of the RTMP protocol. 7 | It is a "low level" protocol API, it does not implement the high-level operations of an RTMP server. It just provides tools you can use to implement your own RTMP Server (or Client). 8 | 9 | The API has been tested against MISTServer, avconf and Orban Encoders. 10 | 11 | Please keep in mind - the RTMP protocol is badly defined and has a lot of incorrect implementations. 12 | For example some RTMP libraries (including the Adobe's one) crashes if you sequence the commands in unexpected order, even though this is allowed by the RTMP protocol specification! 13 | Also the old versions of the Adobe's RTMP library has incorrect implementation of the SetWindow command. 14 | 15 | You have to be careful and do a lot of tests to implement a stable server. 16 | 17 | Another thing, the RTMP protocol consist of 3 distinct layers: 18 | 19 | - Chunk layer (chunk frames and commands that just exchange data in chunk fragments) 20 | - Transport layer (transport related commands and streams who uses the chunk layer for data exchange, but adds on top of it some control commands, encoding schemes and a mechanism for exchange of control commands) 21 | - Application layer - the layer that uses the transport layer to exchange messages, real data and triger remote procedures 22 | 23 | The specs I've been using from Adobe covers only the chunk and the transport layer. There is no clear specification on the application layer that I have seen. But the application layer is the one that essentially implement the client-server comminication flow. 24 | All the RTMP libraries implement mainly the Chunk and the Transport layer, as the rest is not clearly specified and should be reverse engineered. This library implement Chunk and Transport layer as well. It does not focus on the application layer. So all the provided examples may not be very correct and may not follow very correctly the communication flow between client and server. 25 | For you to implement a RTMP client or a RTMP server, you need to have already working client/server, listen to their communication with Wireshark and then replicate it using eventually this library. 26 | 27 | Usage 28 | == 29 | 30 | To use this library is simple. 31 | To implement a server you just have to do something like: 32 | 33 | 34 | var rtmpApi = require('node-rtmpapi'); 35 | var rtmpServer = rtmpApi.rtmpServer(); 36 | 37 | var rtmpServer.createServer(function(rtmpSession) { 38 | rtmpSession.msg.loop(null,{ 39 | "amf0cmd": function(chunk) { 40 | var msg = chunk.chunk.msg; 41 | switch(msg.cmd) { 42 | "connect": 43 | chunk.sendSetChunkSize(4096); 44 | chunk.sendSetWindowSize(10000000); 45 | chunk.sendSetPeerBw(10000000,1); 46 | chunk.sendAmf0EncCmdMsg({ 47 | cmd: "_result", 48 | transId: msg.transId, 49 | cmdObj: { 50 | fmsVer: "FMS/3,5,5,2004", 51 | capabilities: 31, 52 | mode: 1 53 | }, 54 | info: { 55 | level: "status", 56 | code: "NetConnection.Connect.Success", 57 | description: "Connection succeeded.", 58 | clientId: 1337, 59 | objectEncoding: 0 60 | } 61 | }); 62 | chunk.sendUserControlMsg(0,1); 63 | break; 64 | "FCPublish": 65 | .... 66 | break; 67 | "createStream": 68 | .... 69 | break; 70 | "publish": 71 | .... 72 | break; 73 | "releaseStream": 74 | .... 75 | break; 76 | default: 77 | } 78 | }, 79 | "audio": function(chunk) { 80 | .... 81 | }, 82 | "video": function(chunk) { 83 | .... 84 | } 85 | }); 86 | }).listen(1935); 87 | 88 | In the example provided above, I am copying the application flow control of a server as it has been implemented in Mist Server, version 1.1. 89 | 90 | This flow control essentially expect RTMP client to connect to this RTMP server and to PUBLISH a stream to the server (not the server to publish a stream to the client). 91 | 92 | Basically, I expect to receive first transport amf0 encoded command with "connect" and I set the Chunk Size to 4k, the Ack window Size to 10MB, Peer Bandwidth Control to Loose 10MB, and reply with _result Connect.Success and send UserControl to 1 (saying to the client to start playing the stream). 93 | 94 | Then I expect FCPublish, or createStream, or publish, or releaseStream commands, where you would get some parameters saying something about the streams (if you are expecting to receive more streams you have to check this values and store it in a dictionary and then associate the audio/video chunks with the correct stream). 95 | 96 | Then I am expecting to receive "audio"/"video" chunks containing the stream data. 97 | 98 | In the next example (To Be Done) I am writing the RTMP Client part - assuming this is the RTMP client who is publishing stream data to the RTMP Server: 99 | 100 | TBD 101 | 102 | 103 | rtmpChunk 104 | ==== 105 | 106 | To use it do: 107 | 108 | var rtmpChunk = require('node-rtmpapi').rtmpChunk(); 109 | 110 | 111 | rtmpServer 112 | ==== 113 | 114 | To use it do: 115 | 116 | var rtmpServer = require('node-rtmpapi').rtmpServer(); 117 | 118 | 119 | -------------------------------------------------------------------------------- /rtmpSession.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by delian on 3/11/14. 3 | * This module implements basic RTMP Session handling 4 | */ 5 | 6 | var Log = require('./log.js'); 7 | var RQ = require('./readQueue.js'); 8 | var rmsg = require('./rtmpMessage.js'); 9 | var debug = 0; 10 | 11 | /** 12 | * Class constructor 13 | * @param sock 14 | * @param isClient 15 | * @param cb 16 | * @returns {RtmpStream} 17 | * @constructor 18 | */ 19 | var RtmpStream = function(sock,isClient,cb) { 20 | 21 | if (!(this instanceof RtmpStream)) return new RtmpStream(sock,isClient,cb); 22 | 23 | var me = this; 24 | me.Q = new RQ(sock); // Create new Read Queue 25 | me.isClient = isClient; 26 | me.sock = sock; 27 | me.log = Log(debug,sock.remoteAddress+':'+sock.remotePort).log; 28 | me.log('RSESS: Create RTMP Session'); 29 | me.msg = new rmsg({ debug: debug, Q: this.Q }); // Provide interface to the messages 30 | me.getTs = me.msg.getTs; 31 | 32 | if (isClient) { 33 | me.log('RSESS: Client Handshake Start'); 34 | me.rtmpSendHandshakeC0(); 35 | me.rtmpSendHandshakeC1(); 36 | me.rtmpReadHandshakeS0(); 37 | me.rtmpReadHandshakeS1(); 38 | me.rtmpSendHandshakeC2(); 39 | me.rtmpReadHandshakeS2(); 40 | } else { 41 | me.log('RSESS: Server Handshake Start'); 42 | me.rtmpReadHandshakeC0(); 43 | // this.rtmpSendHandshakeS0(); 44 | me.rtmpReadHandshakeC1(); 45 | me.rtmpSendHandshakeS01(); // Send 0 and 1 together 46 | me.rtmpReadHandshakeC2(); 47 | me.rtmpSendHandshakeS2(); 48 | } 49 | 50 | this.sock.on('close',function() { 51 | me.log('RSESS: Destroy the RTMP session'); 52 | delete(me.log); 53 | delete(me.msg); 54 | delete(me.getTs); 55 | delete(me.sock); 56 | delete(me.Q); 57 | }); 58 | 59 | if (typeof cb == 'function') cb.call(this,this); // Send the object as an argument 60 | }; 61 | 62 | /** 63 | * Send C0 header 64 | * @returns {*} 65 | */ 66 | RtmpStream.prototype.rtmpSendHandshakeC0 = function() { 67 | var me = this; 68 | this.Q.Q(0,function() { 69 | var buffer = new Buffer(1); 70 | buffer.writeUInt8(3,0); // RTMP Version 71 | me.sock.write(buffer); 72 | me.log('RSESS: Sent C0 Handshake',data); 73 | }); 74 | }; 75 | 76 | /** 77 | * Receive C0 header 78 | */ 79 | RtmpStream.prototype.rtmpReadHandshakeC0 = function() { 80 | var me = this; 81 | me.Q.Q(1,function(data) { 82 | me.log('RSESS: Received C0 Handshake',data); 83 | }); 84 | }; 85 | 86 | /** 87 | * Send C1 header 88 | */ 89 | RtmpStream.prototype.rtmpSendHandshakeC1 = function() { 90 | var me = this; 91 | me.Q.Q(0,function() { 92 | var buffer = new Buffer(1536); 93 | buffer.writeUInt32BE(me.getTs(),0); // Set the timestamp 94 | buffer.writeUInt32BE(0,4); // zero 95 | me.sock.write(buffer); 96 | me.log('RSESS: Sent C1 Handshake',buffer); 97 | }); 98 | }; 99 | 100 | /** 101 | * Receive C1 header 102 | */ 103 | RtmpStream.prototype.rtmpReadHandshakeC1 = function() { 104 | var me = this; 105 | me.Q.Q(1536,function(data) { 106 | me.C1 = data; 107 | me.c1s1ts = me.getTs(); 108 | me.log('RSESS: Received C1 Handshake',data); 109 | }); 110 | }; 111 | 112 | /** 113 | * Send S0 header 114 | */ 115 | RtmpStream.prototype.rtmpSendHandshakeS0 = function() { 116 | var me = this; 117 | me.Q.Q(0,function() { 118 | var buffer = new Buffer(1); 119 | buffer.writeUInt8(3,0); // RTMP Version 120 | me.sock.write(buffer); 121 | me.log('RSESS: Sent S0 Handshake',buffer); 122 | }); 123 | }; 124 | 125 | /** 126 | * Read S0 header 127 | */ 128 | RtmpStream.prototype.rtmpReadHandshakeS0 = function() { 129 | var me = this; 130 | me.Q.Q(1,function(data) { 131 | me.log('RSESS: Received S0 Handshake',data); 132 | }); 133 | }; 134 | 135 | /** 136 | * Send S1 header 137 | */ 138 | RtmpStream.prototype.rtmpSendHandshakeS1 = function() { 139 | var me = this; 140 | me.Q.Q(0,function() { 141 | var buffer = new Buffer(1536); 142 | buffer.writeUInt32BE(me.getTs(),0); // Set the timestamp 143 | buffer.writeUInt32BE(0,4); // zero 144 | me.sock.write(buffer); 145 | me.log('RSESS: Sent S1 Handshake',buffer); 146 | }); 147 | }; 148 | 149 | /** 150 | * Send S0 and S1 at once, to avoid a potential bug with the librtmp 151 | */ 152 | RtmpStream.prototype.rtmpSendHandshakeS01 = function() { 153 | var me = this; 154 | me.Q.Q(0,function() { 155 | var buffer = new Buffer(1537); 156 | buffer.writeUInt8(3,0); 157 | buffer.writeUInt32BE(me.getTs(),1); // Set the timestamp 158 | buffer.writeUInt32BE(0,5); // zero 159 | me.sock.write(buffer); 160 | me.log('RSESS: Sent S0+1 Handshake',buffer); 161 | }); 162 | }; 163 | 164 | /** 165 | * Read S1 Header 166 | */ 167 | RtmpStream.prototype.rtmpReadHandshakeS1 = function() { 168 | var me = this; 169 | me.Q.Q(1536,function(data) { 170 | me.S1 = data; 171 | me.c1s1ts = me.getTs(); 172 | me.log('RSESS: Received S1 Handshake',data); 173 | }); 174 | }; 175 | 176 | /** 177 | * Send C2 Header 178 | */ 179 | RtmpStream.prototype.rtmpSendHandshakeC2 = function() { 180 | var me = this; 181 | me.Q.Q(0,function() { 182 | var buffer = new Buffer(1536); 183 | if (me.S1) me.S1.copy(buffer); 184 | buffer.writeUInt32BE(me.c1s1ts,4); 185 | me.sock.write(buffer); 186 | me.log('RSESS: Sent C2 Handshake',buffer); 187 | }); 188 | }; 189 | 190 | /** 191 | * Read C2 Header 192 | */ 193 | RtmpStream.prototype.rtmpReadHandshakeC2 = function() { 194 | var me = this; 195 | me.Q.Q(1536,function(data) { 196 | me.log('RSESS: Received C2 Handshake',data); 197 | }); 198 | }; 199 | 200 | /** 201 | * Send S2 Header 202 | */ 203 | RtmpStream.prototype.rtmpSendHandshakeS2 = function() { 204 | var me = this; 205 | me.Q.Q(0,function() { 206 | var buffer = new Buffer(1536); 207 | if (me.C1) me.C1.copy(buffer); 208 | buffer.writeUInt32BE(me.c1s1ts,4); 209 | me.sock.write(buffer); 210 | me.log('RSESS: Sent S2 Handshake',buffer); 211 | }); 212 | }; 213 | 214 | /** 215 | * Read S2 Header 216 | */ 217 | RtmpStream.prototype.rtmpReadHandshakeS2 = function() { 218 | var me = this; 219 | me.Q.Q(1536,function(data) { 220 | me.log('RSESS: Received S2 Handshake',data); 221 | }); 222 | }; 223 | 224 | module.exports = RtmpStream; 225 | 226 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. -------------------------------------------------------------------------------- /rtmpChunk.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by delian on 3/11/14. 3 | * 4 | * This little module provides interface to processing chunks on a higher level 5 | */ 6 | 7 | var amf = require('node-amfutils'); 8 | var Log = require('./log.js'); 9 | 10 | /** 11 | * Contains rules for chunk processing 12 | */ 13 | 14 | function getTs() { 15 | var ts = new Date().getTime(); 16 | return ts % 0x7FFFFF; 17 | } 18 | 19 | /** 20 | * Create a chunk class 21 | * @param chunk {object} 22 | * @param opt 23 | * @returns {RtmpChunkMsgClass} 24 | */ 25 | function RtmpChunkMsgClass(chunk, opt) { 26 | var k; 27 | if (!(this instanceof RtmpChunkMsgClass)) return new RtmpChunkMsgClass(chunk, opt); 28 | 29 | var me = this; 30 | 31 | me.chunk = { 32 | data: new Buffer(0), 33 | sendData: new Buffer(0), // The data parameter we use to transmit 34 | timestamp: 0, 35 | msgLen: 0, 36 | msgLeft: 0, 37 | msgType: 0, 38 | msgTypeText: "", 39 | fmt: 0, 40 | msgComplete: 1, 41 | streamId: 0, 42 | recvBytes: 0, 43 | chunkSize: {}, // Reference to a chunk size, initialized later 44 | msgStreamId: 0 45 | }; 46 | me.oldChunk = { 47 | data: new Buffer(0), 48 | sendData: new Buffer(0), // The data parameter we use to transmit 49 | timestamp: 0, 50 | msgLen: 0, 51 | msgLeft: 0, 52 | msgType: 0, 53 | msgTypeText: "", 54 | fmt: 0, 55 | msgComplete: 1, 56 | streamId: 0, 57 | recvBytes: 0, 58 | chunkSize: {}, // Reference to a chunk size, initialized later 59 | msgStreamId: 0 60 | }; 61 | me.Q = opt.Q; // Inherit Q 62 | if (opt.Q) me.sock = opt.Q.sock; 63 | if (opt.sock) me.sock = opt.sock; // Inherit socket 64 | me.log = Log(opt.debug, (me.sock ? me.sock.remoteAddress : '') + ':' + (me.sock ? me.sock.remotePort : '')).log; // Implement Logging 65 | if (me.Q && typeof me.Q.chunkSize == 'undefined') { 66 | me.Q.chunkSize = { rcv: 128, snd: 128, rcvWinSize: 4096, sndWinSize: 4096 }; 67 | me.log('CHUNK: The session never had chunkSize before, lets set it'); 68 | } 69 | for (k in chunk) me.chunk[k] = chunk[k]; // Get the properties 70 | me.chunk.chunkSize = me.Q.chunkSize; 71 | me.oldChunk.chunkSize = me.chunk.chunkSize; 72 | if (me.sock) { 73 | me.sock.on('close', function () { 74 | me.log('CHUNK: MSG class destroyed', me.streamId); 75 | me.log = null; 76 | me.Q = null; 77 | me.sock = null; 78 | me.chunk = null; 79 | }); 80 | } 81 | } 82 | 83 | /** 84 | * Static link to the local getTs function 85 | * @returns {*} 86 | */ 87 | RtmpChunkMsgClass.prototype.getTs = function () { 88 | return getTs(); 89 | }; 90 | 91 | /** 92 | * Called when we receive Set Chunk Size message 93 | */ 94 | RtmpChunkMsgClass.prototype.rcvSetChunkSize = function () { 95 | var c = this.chunk; 96 | c.chunkSize.rcv = c.data.readUInt32BE(0); 97 | c.msgTypeText = "setChunkSize"; 98 | c.msg = { setChunkSize: c.chunkSize.rcv }; 99 | this.log('CHUNK: Received Set Chunk Size', c.chunkSize.rcv, this.chunk); 100 | }; 101 | 102 | /** 103 | * Called when we want to set the Chunk Size 104 | * @param size 105 | */ 106 | RtmpChunkMsgClass.prototype.sendSetChunkSize = function (size) { // Chunk Size is it one direction only or both? 107 | this.log('CHUNK: Send Set Chunk Size', size); 108 | this.chunk.chunkSize.snd = size; // Set the chunk size of the object 109 | var msg = { 110 | msgType: 1, 111 | data: new Buffer(4), msgStreamId: 0, streamId: 2 112 | }; 113 | msg.data.writeUInt32BE(size, 0); 114 | return this.rtmpMsgSend(msg); 115 | }; 116 | 117 | /** 118 | * Called when we receive Abort Message 119 | */ 120 | RtmpChunkMsgClass.prototype.rcvAbortMsg = function () { 121 | var sid = this.chunk.data.readUInt32BE(0); 122 | this.chunk.msgTypeText = "abort"; 123 | this.chunk.msg = { msgStreamId: sid }; 124 | this.log('CHUNK: Received Abort message for Stream Id', sid); 125 | }; 126 | 127 | /** 128 | * Called when we want to send Abort message for a certain stream 129 | * @param msgStreamId 130 | * @returns {*} 131 | */ 132 | RtmpChunkMsgClass.prototype.sendAbortMsg = function (msgStreamId) { 133 | this.log('CHUNK: Send Abort Message for', msgStreamId); 134 | var data = new Buffer(4); 135 | data.writeUInt32BE(msgStreamId, 0); 136 | return this.rtmpMsgSend({ msgType: 2, data: data, msgStreamId: 0, streamId: 2 }); 137 | }; 138 | 139 | /** 140 | * Called when we receive Ack Message 141 | */ 142 | RtmpChunkMsgClass.prototype.rcvAckMsg = function () { 143 | var snum = this.chunk.data.readUInt32BE(0); 144 | this.log('CHUNK: Received ACK with seq num', snum); 145 | this.chunk.msgTypeText = "ack"; 146 | this.chunk.msg = { seq: snum }; 147 | }; 148 | 149 | /** 150 | * Called when we want to send Ack Message 151 | * @returns {*} 152 | */ 153 | RtmpChunkMsgClass.prototype.sendAckMsg = function () { 154 | var data = new Buffer(4); 155 | var Q = this.Q; 156 | data.writeUInt32BE(Q.recvBytes, 0); 157 | this.log('CHUNK: Send ACK with seq num', Q.recvBytes, Q.recvBytes - Q.ackBytes); 158 | Q.ackBytes = Q.recvBytes; 159 | return this.rtmpMsgSend({ msgType: 3, data: data, msgStreamId: 0, streamId: 2 }); 160 | }; 161 | 162 | /** 163 | * Called when we receive Set Window Size message 164 | */ 165 | RtmpChunkMsgClass.prototype.rcvSetWindowSize = function () { 166 | var snum = this.chunk.data.readUInt32BE(0); 167 | this.log('CHUNK: Received Set Window Size', snum); 168 | this.chunk.windowSize = snum; 169 | this.chunk.msgTypeText = "setWindowSize"; 170 | this.chunk.msg = { windowSize: snum }; 171 | this.chunk.chunkSize.sndWinSize = snum; // We do set this, but we do not follow it strictly 172 | }; 173 | 174 | /** 175 | * Send request to set Window Size 176 | * @param snum the window size number 177 | * @returns {*} 178 | */ 179 | RtmpChunkMsgClass.prototype.sendSetWindowSize = function (snum) { 180 | this.log('CHUNK: Send Set Window Size', snum); 181 | var data = new Buffer(4); 182 | data.writeUInt32BE(snum, 0); 183 | this.chunk.chunkSize.rcvWinSize = snum; 184 | return this.rtmpMsgSend({ msgType: 5, data: data, msgStreamId: 0, streamId: 2 }); 185 | }; 186 | 187 | /** 188 | * Called when we receive setPeerBw message 189 | */ 190 | RtmpChunkMsgClass.prototype.rcvSetPeerBw = function () { 191 | var snum = this.chunk.data.readUInt32BE(0); 192 | var ltype = 0; 193 | if (this.chunk.data.length > 4) this.chunk.data.readUInt8(4); 194 | this.log('CHUNK: Received Set Peer Bw', snum, ltype); 195 | this.chunk.peerBw = snum; 196 | this.chunk.lType = ltype; 197 | this.chunk.msgTypeText = "setPeerBw"; 198 | this.chunk.msg = { peerBw: snum, lType: ltype }; 199 | // this.chunk.chunkSize.rcvWinSize = snum; 200 | this.sendSetWindowSize(snum); // We do a reply and automatically set our ack 201 | }; 202 | 203 | /** 204 | * Send request to set Peer Bandwidth 205 | * @param peerBw 206 | * @param lType 207 | * @returns {*} 208 | */ 209 | RtmpChunkMsgClass.prototype.sendSetPeerBw = function (peerBw, lType) { // Broke the lType the way MIST does!!! 210 | this.log('CHUNK: Send Set Peer Bw', peerBw, lType); 211 | var data = new Buffer(4); 212 | data.writeUInt32BE(peerBw, 0); 213 | if (typeof lType != 'undefined') { 214 | var lTypeData = new Buffer(1); 215 | lTypeData.writeInt8(lType, 0); 216 | data = Buffer.concat([data, lTypeData]); 217 | } 218 | return this.rtmpMsgSend({ msgType: 6, data: data, msgStreamId: 0, streamId: 2 }); 219 | }; 220 | 221 | /** 222 | * Receive User Control Message 223 | */ 224 | RtmpChunkMsgClass.prototype.rcvUserControlMsg = function () { 225 | this.log('CHUNK: Received User Control Message', this.chunk); 226 | this.chunk.msgTypeText = "userControl"; 227 | var eventType = this.chunk.data.readUInt16BE(0); 228 | this.chunk.msg = { 229 | evenType: eventType 230 | }; 231 | switch (eventType) { 232 | case 0: // Stream begin 233 | this.chunk.msg.streamId = this.chunk.data.readUInt32BE(2); 234 | break; 235 | case 1: // Stream EOF 236 | this.chunk.msg.streamId = this.chunk.data.readUInt32BE(2); 237 | break; 238 | case 2: // Stream Dry 239 | this.chunk.msg.streamId = this.chunk.data.readUInt32BE(2); 240 | break; 241 | case 3: // Set Buffer Length 242 | this.chunk.msg.streamId = this.chunk.data.readUInt32BE(2); 243 | this.chunk.msg.buffLenMs = this.chunk.data.readUInt32BE(6); 244 | break; 245 | case 4: // Stream is recorded 246 | this.chunk.msg.streamId = this.chunk.data.readUInt32BE(2); 247 | break; 248 | case 5: // Ping Request 249 | this.chunk.msg.timestamp = this.chunk.data.readUInt32BE(2); 250 | this.sendUserControlMsg(6); // Respond immediately with ping response 251 | break; 252 | case 6: // Ping response 253 | this.chunk.msg.timestamp = this.chunk.data.readUInt32BE(2); 254 | break; 255 | default: 256 | this.log('CHUNK: UNKNOWN User Control Message Event Type', eventType, this.chunk); 257 | } 258 | }; 259 | 260 | /** 261 | * Receive User Control Message 262 | */ 263 | RtmpChunkMsgClass.prototype.sendUserControlMsg = function (eventType, streamId, buffLenMs) { 264 | this.log('CHUNK: Sending User Control Message', eventType, streamId, buffLenMs); 265 | var data = new Buffer(6); 266 | data.writeUInt16BE(eventType, 0); 267 | data.writeUInt32BE(streamId || 0, 2); 268 | this.chunk.msgTypeText = "userControl"; 269 | var eventType = this.chunk.data.readUInt16BE(0); 270 | if (eventType == 3) 271 | data = Buffer.concat([data, (new Buffer(4)).writeUInt32BE(buffLenMs, 6)]); 272 | return this.rtmpMsgSend({ msgType: 4, data: data, msgStreamId: 0, streamId: 2 }); // User control messages has always CS2 and S0 273 | }; 274 | 275 | /** 276 | * Receive Audio Message 277 | */ 278 | RtmpChunkMsgClass.prototype.rcvAudioMsg = function () { 279 | this.log('CHUNK: Received Audio Message', this.chunk); 280 | this.chunk.msgTypeText = "audio"; 281 | this.chunk.msg = {}; 282 | // TODO: Implement Audio Message 283 | }; 284 | 285 | /** 286 | * Receive Video Message 287 | */ 288 | RtmpChunkMsgClass.prototype.rcvVideoMsg = function () { 289 | this.log('CHUNK: Received Video Message', this.chunk); 290 | this.chunk.msgTypeText = "video"; 291 | this.chunk.msg = {}; 292 | // TODO: Implement Video Message 293 | }; 294 | 295 | /** 296 | * Receive AMF3 Meta Message 297 | */ 298 | RtmpChunkMsgClass.prototype.rcvAmf3MetaMsg = function () { 299 | this.log('CHUNK: Received Meta Message AMF3', this.chunk); 300 | this.chunk.msgTypeText = "amf3meta"; 301 | this.chunk.msg = {}; 302 | // TODO: Implement Meta Message AMF3 303 | }; 304 | 305 | /** 306 | * Receive AMF0 Meta Message 307 | */ 308 | RtmpChunkMsgClass.prototype.rcvAmf0MetaMsg = function () { 309 | this.log('CHUNK: Received Meta Message AMF0', this.chunk); 310 | var c = this.chunk; 311 | c.msgTypeText = "amf0meta"; 312 | var data = c.data; 313 | var dec = amf.amf0Decode(data); 314 | c.msg = { 315 | cmd: dec[0], 316 | event: dec[1], 317 | parms: dec[2], 318 | dec: dec 319 | } 320 | }; 321 | 322 | /** 323 | * Receive AM3 SObjMessage 324 | */ 325 | RtmpChunkMsgClass.prototype.rcvAmf3SObjMsg = function () { 326 | this.log('CHUNK: Received Shared Object Message AMF3', this.chunk); 327 | this.chunk.msgTypeText = "amf3sobject"; 328 | this.chunk.msg = {}; 329 | // TODO: Implement SOBJ Message AMF3 330 | }; 331 | 332 | /** 333 | * 334 | */ 335 | RtmpChunkMsgClass.prototype.rcvAmf0SObjMsg = function () { 336 | this.log('CHUNK: Received Shared Object Message AMF0', this.chunk); 337 | this.chunk.msgTypeText = "amf0sobject"; 338 | this.chunk.msg = {}; 339 | // TODO: Implement SOBJ Message AMF0 340 | }; 341 | 342 | /** 343 | * 344 | */ 345 | RtmpChunkMsgClass.prototype.rcvAmf3EncCmdMsg = function () { 346 | this.log('CHUNK: Received CMD Message AMF3', this.chunk); 347 | this.chunk.msgTypeText = "amf3cmd"; 348 | this.chunk.msg = amf.decodeAmf3Cmd(this.chunk.data); 349 | }; 350 | 351 | /** 352 | * 353 | */ 354 | RtmpChunkMsgClass.prototype.rcvAmf0EncCmdMsg = function () { 355 | this.log('CHUNK: Received CMD Message AMF0', this.chunk); 356 | this.chunk.msgTypeText = "amf0cmd"; 357 | this.chunk.msg = amf.decodeAmf0Cmd(this.chunk.data); 358 | }; 359 | 360 | /** 361 | * Send AMF0 encoded command message 362 | * @param s 363 | * @returns {*} 364 | */ 365 | RtmpChunkMsgClass.prototype.sendAmf0EncCmdMsg = function (s) { 366 | var data = amf.encodeAmf0Cmd(s); // TODO: Check the CSid and Sid 367 | this.log('CHUNK: Send encoded AMF0 cmd', s, data); 368 | return this.rtmpMsgSend({ msgType: 20, data: data }); // Force the CMD message into CS2, although it is not required by the standard 369 | }; 370 | 371 | /** 372 | * 373 | */ 374 | RtmpChunkMsgClass.prototype.rcvAggMsg = function () { 375 | this.log('CHUNK: Received Aggreg Message', this.chunk); 376 | this.chunk.msg = {}; 377 | this.chunk.msgTypeText = "agg"; 378 | // TODO: Implement Aggreg Message 379 | }; 380 | 381 | /** 382 | * Auto decide which type of chunk header type to use 383 | * @param msg 384 | */ 385 | RtmpChunkMsgClass.prototype.rtmpMsgSend = function (msg) { 386 | var me = this; 387 | var c = this.chunk; 388 | var Q = me.Q; 389 | 390 | // We will avoid sending data trough the read queue in general to avoid event based blocking 391 | 392 | msg.sendData = msg.data.slice(0, c.chunkSize.snd); // The lower layer uses different transport variable - sendData instead data 393 | me.rtmpMsg0Send(msg); 394 | 395 | for (var i = c.chunkSize.snd; msg.data.length - i > 0; i += c.chunkSize.snd) { 396 | msg.sendData = msg.data.slice(i, i + c.chunkSize.snd); 397 | me.rtmpMsg2Send(msg); 398 | } 399 | }; 400 | 401 | /** 402 | * Format chunk.data into Type 0 RTMP message 403 | * @param msg 404 | * @param ts 405 | */ 406 | RtmpChunkMsgClass.prototype.rtmpMsg0Send = function (msg, ts) { 407 | var me = this; 408 | var c = me.chunk; 409 | if (!ts) ts = me.getTs(); 410 | var eTs = (ts > 0xFFFFFF) ? 4 : 0; 411 | var buffer = new Buffer(11 + eTs); 412 | if (eTs) { 413 | buffer.writeUInt32BE(ts, 11); // Extended Timestamp at Byte 11 414 | ts = 0x7FFFFF; 415 | } 416 | buffer.writeUInt16BE(ts >> 8, 0); 417 | buffer.writeUInt8(ts & 0xFF, 2); // Write the Timestamp 418 | buffer.writeUInt16BE(msg.sendData.length >> 8, 3); 419 | buffer.writeUInt8(msg.sendData.length & 0xFF, 5); // Write the message length 420 | buffer.writeUInt8(msg.msgType || c.msgType, 6); // Write the message type 421 | buffer.writeUInt32LE(msg.msgStreamId || c.msgStreamId, 7); // Write the message stream id 422 | return me.rtmpChunkSend(0, msg.streamId || c.streamId, Buffer.concat([buffer, msg.sendData])); // Send the concatenated header 423 | }; 424 | 425 | /** 426 | * Format the chunk.data into Type 1 RTMP message 427 | * @param msg 428 | * @param ts 429 | */ 430 | RtmpChunkMsgClass.prototype.rtmpMsg1Send = function (msg, ts) { 431 | var me = this; 432 | var c = me.chunk; 433 | if (!ts) ts = me.getTs(); 434 | var eTs = (ts > 0x7FFFFF) ? 4 : 0; 435 | var buffer = new Buffer(7 + eTs); 436 | if (eTs) { 437 | buffer.writeUInt32BE(ts, 7); // Extended Timestamp at Byte 7 438 | ts = 0xFFFFFF; 439 | } 440 | buffer.writeUInt16BE(ts >> 8, 0); 441 | buffer.writeUInt8(ts & 0xFF, 2); // Write the Timestamp 442 | buffer.writeUInt16BE(msg.sendData.length >> 8, 3); 443 | buffer.writeUInt8(msg.sendData.length & 0xFF, 5); // Write the message length 444 | buffer.writeUInt8(msg.msgType || c.msgType, 6); // Write the message type 445 | return me.rtmpChunkSend(1, msg.streamId || c.streamId, Buffer.concat([buffer, msg.sendData])); // Send the concatenated header 446 | }; 447 | 448 | /** 449 | * Format the chunk into Type 2 RTMP message 450 | * @param msg 451 | * @param ts 452 | */ 453 | RtmpChunkMsgClass.prototype.rtmpMsg2Send = function (msg, ts) { 454 | var me = this; 455 | var c = me.chunk; 456 | if (!ts) ts = me.getTs(); 457 | var eTs = (ts > 0x7FFFFF) ? 4 : 0; 458 | var buffer = new Buffer(3 + eTs); 459 | if (eTs) { 460 | buffer.writeUInt32BE(ts, 3); // Extended Timestamp at Byte 3 461 | ts = 0x7FFFFF; 462 | } 463 | buffer.writeUInt16BE(ts >> 8, 0); 464 | buffer.writeUInt8(ts & 0xFF, 2); // Write the Timestamp 465 | return me.rtmpChunkSend(2, msg.streamId || c.streamId, Buffer.concat([buffer, msg.sendData])); // Send the concatenated header 466 | }; 467 | 468 | /** 469 | * Send the chunk into Type 3 RTMP message. If eh is set, then it sets extended timestamp, if not, no header 470 | * @param msg 471 | * @param ts 472 | */ 473 | RtmpChunkMsgClass.prototype.rtmpMsg3Send = function (msg, ts) { 474 | var me = this; 475 | var c = me.chunk; 476 | if (!ts) ts = me.getTs(); 477 | var eTs = (ts > 0x7FFFFF) ? 4 : 0; 478 | var buffer = new Buffer(eTs); 479 | if (eTs) buffer.writeUInt32BE(ts, 0); // Extended Timestamp at Byte 0 480 | return me.rtmpChunkSend(3, msg.streamId || c.streamId, Buffer.concat([buffer, msg.sendData])); // Send the concatenated header 481 | }; 482 | 483 | // Send RTMP message 484 | /** 485 | * Generates chunk basic header and add it to the data 486 | * @param type chunk message type 487 | * @param streamId chunk streamId 488 | * @param data chunk data 489 | * @returns {*} 490 | */ 491 | RtmpChunkMsgClass.prototype.rtmpChunkSend = function (type, streamId, data) { 492 | var bhLen = 1; 493 | if (streamId > 63 && streamId < 320) bhLen = 2; 494 | if (streamId > 319) bhLen = 3; 495 | var bHdr = new Buffer(bhLen); 496 | switch (bhLen) { 497 | case 1: 498 | bHdr.writeUInt8(streamId, 0); 499 | break; 500 | case 2: 501 | bHdr.writeUInt16BE(streamId, 0); 502 | break; 503 | case 3: 504 | bHdr.writeUInt16BE(streamId >> 8, 0); 505 | bHdr.writeUInt8(streamId & 0xFF, 2); 506 | break; 507 | default: 508 | } 509 | bHdr.writeUInt8(bHdr.readUInt8(0) | (type << 6), 0); 510 | return this.sock.write(Buffer.concat([bHdr, data])); // Send it immediately over the socket 511 | }; 512 | 513 | /** 514 | * Create RtmpChunkClass 515 | * @param opt 516 | * @returns {RtmpChunkClass} 517 | */ 518 | function RtmpChunkClass(opt) { 519 | if (!(this instanceof RtmpChunkClass)) return new RtmpChunkClass(opt); 520 | var me = this; 521 | me.opt = {}; 522 | me.sock = null; 523 | me.Q = function () { 524 | }; // Empty queue 525 | if (typeof opt == 'object') me.opt = opt; 526 | if (opt.Q) { 527 | me.Q = opt.Q; 528 | me.sock = opt.Q.sock; 529 | } 530 | if (opt.sock) me.sock = opt.sock; 531 | me.log = Log(opt.debug, (me.sock ? me.sock.remoteAddress : '') + ':' + (me.sock ? me.sock.remotePort : '')).log; 532 | me.cStreams = {}; 533 | 534 | if (me.sock) { 535 | me.sock.on('close', function () { 536 | me.log('CHUNK: ChunkClass destroyed!'); 537 | me.opt = null; 538 | me.sock = null; 539 | me.Q = null; 540 | me.log = null; 541 | me.cStreams = null; 542 | }); 543 | } 544 | } 545 | 546 | /** 547 | * Read the Chunk Basic Header 548 | * @param cb 549 | */ 550 | RtmpChunkClass.prototype.rtmpReadChunkBasicHdr = function (cb) { 551 | var me = this; 552 | 553 | function chunkClass(chunk) { 554 | if (!me.cStreams[chunk.streamId]) me.cStreams[chunk.streamId] = new RtmpChunkMsgClass(chunk, me.opt); 555 | me.cStreams[chunk.streamId].chunk.fmt = chunk.fmt; // Lets not forget the fmt! 556 | me.cStreams[chunk.streamId].chunk.hdrData = chunk.hdrData; // Debug purposes 557 | return me.cStreams[chunk.streamId]; 558 | } 559 | 560 | me.Q.Q(1, function (data) { 561 | var chunk = {}; 562 | var b = data.readUInt8(0); 563 | chunk.fmt = b >> 6; // The first bytes 564 | chunk.streamId = b & 0x3F; 565 | chunk.hdrData = data; 566 | me.log('CHUNK: First byte of the chunk', chunk.fmt, chunk.streamId); 567 | if (chunk.streamId == 0 || chunk.streamId == 1) { 568 | me.Q.Q(1 + chunk.streamId, function (data2) { 569 | me.log('CHUNK: Large Chunk StreamId', data2.length, data2); 570 | chunk.streamId = 64 + (data2.length == 1) ? data2.readUInt8(0) : data2.readUInt16LE(0); // the 2 byte version is in LE 571 | chunk.hdrData = Buffer.concat([chunk.hdrData, data2]); 572 | return cb(chunkClass(chunk)); 573 | }); 574 | } else return cb(chunkClass(chunk)); 575 | }); 576 | }; 577 | 578 | /** 579 | * does one rtmpChunk read and execute a callback 580 | * @param cb 581 | */ 582 | RtmpChunkClass.prototype.read = function (cb) { 583 | var me = this; 584 | var log = me.log; 585 | me.rtmpReadChunkBasicHdr(function (chunk) { 586 | me.rtmpReadChunkMessageHdr(chunk, function (chunk) { 587 | me.rtmpReadChunkMessagePCM(chunk, function (chunk) { 588 | log('CHUNK: Call back the callback'); 589 | if (cb) cb(chunk); 590 | }); 591 | }); 592 | }); 593 | }; 594 | 595 | RtmpChunkClass.prototype.getTs = function () { 596 | return getTs(); 597 | }; 598 | 599 | 600 | /** 601 | * Read the Chunk Message Header if present 602 | * @param chunk 603 | * @param cb 604 | */ 605 | 606 | RtmpChunkClass.prototype.rtmpReadChunkMessageHdr = function (chunk, cb) { 607 | var me = this; 608 | //var log = me.log; 609 | var Q = me.Q; 610 | var c = chunk.chunk; 611 | var oc = chunk.oldChunk; 612 | 613 | function copyChunk(n, o) { 614 | n.fmt = o.fmt; 615 | } 616 | 617 | if (c.fmt == 0) 618 | Q.Q(3, function (data) { // Get the timestamp, fmt 0 only 619 | c.tmpTimestamp = data.readUInt16BE(0) * 256 + data.readUInt8(2); 620 | if (c.tmpTimestamp != 0xFFFFFF) c.timestamp = c.tmpTimestamp; 621 | c.hdrData = Buffer.concat([c.hdrData, data]); 622 | }); 623 | 624 | if (c.fmt == 1 || c.fmt == 2) 625 | Q.Q(3, function (data) { // Set the timestampDelta, fmt 1, 2 626 | c.tmpTimestamp = data.readUInt16BE(0) * 256 + data.readUInt8(2); 627 | if (c.tmpTimestamp != 0xFFFFFF) c.timestamp += c.tmpTimestamp; 628 | c.hdrData = Buffer.concat([c.hdrData, data]); 629 | }); 630 | 631 | if (c.fmt == 0 || c.fmt == 1) 632 | Q.Q(4, function (data) { // Set msgLen, fmt 0, 1 633 | c.msgLen = data.readUInt16BE(0) * 256 + data.readUInt8(2); 634 | c.msgLeft = c.msgLen; // We still have to receive so much 635 | c.msgType = data.readUInt8(3); 636 | c.data = new Buffer(0); // It was a test to avoid a certain bug 637 | c.hdrData = Buffer.concat([c.hdrData, data]); 638 | }); 639 | 640 | if (c.fmt == 0) Q.Q(4, function (data) { // streamId 641 | c.msgStreamId = data.readUInt32LE(0); 642 | }); 643 | 644 | Q.Q(0, function () { // Lets read the extended timestamp 645 | if (c.tmpTimestamp == 0xFFFFFF) { 646 | Q.Q(4, function (data) { 647 | var ts = data.readUInt32BE(0); 648 | if (oc.fmt == 0) c.timestamp = ts; 649 | else c.timestamp += ts; 650 | c.hdrData = Buffer.concat([c.hdrData, data]); 651 | if (c.msgLeft == 0) c.msgLeft = c.msgLen; // We still have to receive so much. This is in order for buggy handling of the T3 header by Orban coders 652 | copyChunk(chunk.oldChunk, chunk.chunk); 653 | // console.log('Got chunk fmt:', c.fmt, 'timestamp:', c.timestamp, 'type:', c.msgType, 'CS:', c.streamId, 'streamId:', c.msgStreamId, 'Len:', c.msgLen, 'Left:', c.msgLeft, 'HDR:', c.hdrData.toString('hex')); 654 | return cb(chunk); // Try to keep the stack small 655 | }); 656 | } else { 657 | if (c.fmt == 3) { 658 | // console.log('fmt is 3, adjust timestamp if msgLeft is 0. FMT:', c.fmt, 'oFMT:', oc.fmt, 'TS Delta:', c.tmpTimestamp, 'msgLeft', c.msgLeft); 659 | if (c.msgLeft == 0 && oc.fmt != 0) c.timestamp += c.tmpTimestamp; // we need to update the ts, in case we have type3 header 660 | } 661 | if (c.msgLeft == 0) c.msgLeft = c.msgLen; // We still have to receive so much. This is in order for buggy handling of the T3 header by Orban coders 662 | copyChunk(chunk.oldChunk, chunk.chunk); 663 | // console.log('Got chunk fmt:', c.fmt, 'timestamp:', c.timestamp, 'type:', c.msgType, 'CS:', c.streamId, 'streamId:', c.msgStreamId, 'Len:', c.msgLen, 'Left:', c.msgLeft, 'HDR:', c.hdrData.toString('hex')); 664 | return cb(chunk); // Try to keep the stack small 665 | } 666 | }); 667 | }; 668 | 669 | /** 670 | * This function implements the receiving of the message body 671 | * @param chunk 672 | * @param cb 673 | */ 674 | RtmpChunkClass.prototype.rtmpReadChunkMessagePCM = function (chunk, cb) { 675 | var me = this; 676 | var log = me.log; 677 | var Q = me.Q; 678 | var c = chunk.chunk; // Chunk from the chunk class 679 | var size = Math.min(c.chunkSize.rcv, c.msgLeft); 680 | 681 | if (size == 0) { 682 | log('CHUNK: Somethins is wrong', size, c.chunkSize.rcv, c.msgLeft, c); 683 | console.error(size, c.chunkSize.rcv, c.msgLeft, c); 684 | console.trace(); 685 | throw new Error('Wrong, wrong!'); 686 | } 687 | 688 | //log('CHUNK: We need to receive',size); 689 | 690 | Q.Q(size, function (data) { 691 | if (size == 0) data = new Buffer(0); 692 | c.msgLeft -= data.length; 693 | c.msgComplete = (c.msgLeft <= 0) ? 1 : 0; 694 | c.data = Buffer.concat([c.data, data]); // Add the data we just received 695 | log('CHUNK: Got the chunk data', c); 696 | if (c.msgComplete) { 697 | log('CHUNK: We have received complete message to process'); 698 | switch (c.msgType) { 699 | case 1: 700 | chunk.rcvSetChunkSize(); 701 | break; 702 | case 2: 703 | chunk.rcvAbortMsg(); 704 | break; 705 | case 3: 706 | chunk.rcvAckMsg(); 707 | break; 708 | case 4: 709 | chunk.rcvUserControlMsg(); 710 | break; 711 | case 5: 712 | chunk.rcvSetWindowSize(); 713 | break; 714 | case 6: 715 | chunk.rcvSetPeerBw(); 716 | break; 717 | case 8: 718 | chunk.rcvAudioMsg(); 719 | break; 720 | case 9: 721 | chunk.rcvVideoMsg(); 722 | break; 723 | case 15: 724 | chunk.rcvAmf3MetaMsg(); 725 | break; 726 | case 16: 727 | chunk.rcvAmf3SObjMsg(); 728 | break; 729 | case 17: 730 | chunk.rcvAmf3EncCmdMsg(); 731 | break; 732 | case 18: 733 | chunk.rcvAmf0MetaMsg(); 734 | break; 735 | case 19: 736 | chunk.rcvAmf0SObjMsg(); 737 | break; 738 | case 20: 739 | chunk.rcvAmf0EncCmdMsg(); 740 | break; 741 | case 22: 742 | chunk.rcvAggMsg(); 743 | break; 744 | default: 745 | log('CHUNK: ERROR! Unknown msg type!', c); 746 | } 747 | } 748 | return cb(chunk); // Implement the callback 749 | }); 750 | }; 751 | 752 | module.exports = RtmpChunkClass; --------------------------------------------------------------------------------