-
Notifications
You must be signed in to change notification settings - Fork 22
/
rtmpMessage.js
62 lines (57 loc) · 1.98 KB
/
rtmpMessage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Created by delian on 3/11/14.
*
* This little module should provide interface that will allow processing of an RTMP message
*/
var chunk = require('./rtmpChunk.js');
var Log = require('./log.js');
/**
* Create RtmpMessageClass
* @param rSess rtmpSession object
* @returns {RtmpMessageClass}
*/
function RtmpMessageClass(opt) {
if (!(this instanceof RtmpMessageClass)) return new RtmpMessageClass(rSess);
this.opt = {};
this.Q = {};
this.sock = null;
if (opt.Q) { this.Q = opt.Q; this.sock = opt.Q.sock; }
if (opt.sock) this.sock = opt.sock;
if (opt) this.opt = opt;
this.log = Log(opt.debug,(this.sock?this.sock.remoteAddress:'')+':'+(this.sock?this.sock.remotePort:'')).log;
this.chunk = new chunk(opt);
this.getTs = this.chunk.getTs;
}
/**
* Define READ one message operation
* @param cb
* @param cbOpts an object defining specific callbacks per message
*/
RtmpMessageClass.prototype.read = function(cb,cbOpts) {
var me = this;
var rtmpMsgProc = function(chunkObj) {
if (chunkObj.chunk.msgComplete) {
if (cb) cb(chunkObj);
if ((typeof cbOpts == 'object')&&(typeof cbOpts[chunkObj.chunk.msgTypeText] == 'function')) cbOpts[chunkObj.chunk.msgTypeText](chunkObj); // Call the specific callback
if ((chunkObj.Q.recvBytes - chunkObj.Q.ackBytes)>=chunkObj.Q.chunkSize.rcvWinSize) chunkObj.sendAckMsg();
} else {
me.log('We received one chunk, but the message is incomplete',chunkObj.chunk);
return me.chunk.read(rtmpMsgProc);
}
};
return me.chunk.read(rtmpMsgProc);
};
/**
* Define LOOP between multiple reads operation
* @param cb
* @param cbOpts an object defining specific callbacks per message
*/
RtmpMessageClass.prototype.loop = function(cb,cbOpts) {
var me = this;
me.Q.defaultCb = function() {
me.log('Message Loop default callback!');
return me.read(cb,cbOpts);
};
me.read(cb,cbOpts);
};
module.exports = RtmpMessageClass;