-
Notifications
You must be signed in to change notification settings - Fork 52
/
WebRtmpPlayer.js
201 lines (176 loc) · 5.12 KB
/
WebRtmpPlayer.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
var RTMP = require('./node-rtmpapi');
var SimpleWebsocket = require('simple-websocket');
var Buffer = require('buffer').Buffer;
const H264_SEP = new Buffer([0,0,0,1]);
const FRAME_Q_SIZE = 15;
class WebRtmpPlayer
{
constructor(wsHost, app, streamName, tcUrl)
{
this._frameQ = [];
this._fps = NaN;
this._lastRenderTime = 0;
this._decoder = new Decoder();
this._player = new Player({ useWorker: false });
this._url = {host: wsHost, app: app, tcUrl: tcUrl, stream: streamName};
this._decoder.onPictureDecoded = this._onPictureDecoded.bind(this);
this._rtmpTransId = 0;
this._invokeChannel = null;
this._videoChannel = null;
this._rtmpSession = null;
this._sock = new SimpleWebsocket(this._url.host);
this._sock.setMaxListeners(100);
this._sock.on('connect', ()=>
{
new RTMP.rtmpSession(this._sock, true, this._onRtmpSessionCreated.bind(this));
})
}
get canvas() { return this._player.canvas; }
_onPictureDecoded(buffer, width, height, infos)
{
if(this._frameQ.length === FRAME_Q_SIZE)
{
console.log("** drop oldest frame!");
this._frameQ.shift(); //如果播放速度跟不上,扔掉最老那一帧
}
this._frameQ.push({data: Buffer.from(buffer), width: width, height: height, canvasObj: this._player.canvasObj});
}
_drawFrame()
{
var now = new Date();//如果播放速度跟不上网络速度,跳帧
var skipFrame = Math.floor(Math.abs(now - this._lastRenderTime) / (1000 / this._fps) ) - 1;
if(this._lastRenderTime && skipFrame > 0)
{
console.log("SkipFrmae = " + skipFrame);
//while(skipFrame-- > 0 && this._frameQ.length > 0) this._frameQ.shift();
}
var frame = this._frameQ.shift();
if(frame)
{
this._player.renderFrame(frame);
}
this._lastRenderTime = now;
}
_rtmpConnect()
{
this._rtmpSession.Q.Q(0,() =>
{
console.log("sending connect");
this._invokeChannel.sendAmf0EncCmdMsg({
cmd: 'connect',
transId:++this._rtmpTransId,
cmdObj:
{
app: this._url.app,
tcUrl: this._url.tcUrl,
fpad: false,
capabilities: 15.0, //note: 我不知道这些参数什么鬼,依据rtmpdump分析出来的
audioCodecs: 3191,
videoCodecs: 252,
videoFunction: 1.0
}
});
this._invokeChannel.invokedMethods[this._rtmpTransId] = 'connect';
});
}
_rtmpCreateStream()
{
this._rtmpSession.Q.Q(0, ()=>
{
console.log("sending createStream");
this._invokeChannel.sendAmf0EncCmdMsg({
cmd: 'createStream',
transId: ++this._rtmpTransId,
cmdObj: null
});
this._invokeChannel.invokedMethods[this._rtmpTransId] = 'createStream';
});
}
_rtmpSendPlay(msgStreamId)
{
this._rtmpSession.Q.Q(0, ()=>
{
this._videoChannel.chunk.msgStreamId = msgStreamId;
//send play ??
this._videoChannel.sendAmf0EncCmdMsg({
cmd: 'play',
transId: ++this._rtmpTransId,
cmdObj:null,
streamName: this._url.stream,
start:-2
},0);
this._invokeChannel.invokedMethods[this._rtmpTransId] = "play";
});
}
_onRtmpSessionCreated(session)
{
this._rtmpSession = session;
console.log("rtmpSession...cb...");
this._invokeChannel = new RTMP.rtmpChunk.RtmpChunkMsgClass({streamId:5}, {sock: this._sock, Q: session.Q, debug: false});
this._invokeChannel.invokedMethods = {}; //用来保存invoke的次数,以便收到消息的时候确认对应结果
this._videoChannel = new RTMP.rtmpChunk.RtmpChunkMsgClass({streamId:8}, {sock: this._sock, Q: session.Q, debug: false});
session.Q.Q(0,this._rtmpConnect.bind(this));
session.Q.Q(0, () =>
{
console.log("Begin LOOP");
session.msg.loop(this._handleRtmpMessage.bind(this));
});
}
_handleRtmpMessage(chunkMsg)
{
var chunk = chunkMsg.chunk;
var msg = chunk.msg;
console.log("GOT MESSAGE: " + chunk.msgTypeText);
//console.log("===========>\n" + JSON.stringify(msg));
if(chunk.msgTypeText == "amf0cmd")
{
if(msg.cmd == "_result")
{
var lastInvoke = this._invokeChannel.invokedMethods[msg.transId];
if(lastInvoke)
{
console.log("<--Got Invoke Result for: " + lastInvoke);
delete this._invokeChannel.invokedMethods[msg.transId];
}
switch (lastInvoke)
{
case 'connect':
return this._rtmpCreateStream();
case 'createStream':
return this._rtmpSendPlay(msg.info);
}
}
}
if(chunk.msgTypeText == "video")
{
//提取h264流
var chunkData = chunk.data;
if (chunkData.length > 4)
{
if (chunkData[1] === 1)
{
chunkData = Buffer.concat([H264_SEP, chunkData.slice(9)]);
}
else if (chunkData[1] === 0)
{
var spsSize = (chunkData[11] << 8) | chunkData[12];
var spsEnd = 13 + spsSize;
chunkData = Buffer.concat([H264_SEP, chunkData.slice(13, spsEnd), H264_SEP, chunkData.slice(spsEnd + 3)]);
}
this._decoder.decode(chunkData);
}
}
if(chunk.msgTypeText == "amf0meta" && msg.cmd == 'onMetaData')
{
console.log("onmetadata");
this._fps = chunk.msg['event']['framerate'];
console.log("fps = "+this._fps);
setInterval(this._drawFrame.bind(this), 1000.0/this._fps); //todo: clear
}
this._rtmpSession.Q.Q(0,()=>
{
this._rtmpSession.msg.loop(this._handleRtmpMessage.bind(this));
});
}
}
module.exports = WebRtmpPlayer;