Skip to content

Commit 01c5cc6

Browse files
应元东应元东
authored andcommitted
Merge branch 'wip'
2 parents f0660e0 + 7cdec4e commit 01c5cc6

11 files changed

Lines changed: 321 additions & 21 deletions

.idea/encodings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/web-rtmp.iml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.template.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<html>
2+
<head>
3+
<title>rtmp test</title>
4+
<script src="./node_modules/broadway-player/Player/Decoder.js"></script>
5+
<script src="./node_modules/broadway-player/Player/YUVCanvas.js"></script>
6+
<script src="./node_modules/broadway-player/Player/Player.js"></script>
7+
<script src="./node_modules/broadway-player/Player/stream.js"></script>
8+
<script src="./node_modules/broadway-player/Player/Player.js"></script>
9+
</head>
10+
11+
<body>
12+
build time: {{buildTime}}
13+
14+
<div id="vidCont" style="width:640px; height:360px;">
15+
</div>
16+
17+
<script type="text/javascript" src="build/bundle.js"></script>
18+
<script type="text/javascript">
19+
20+
</script>
21+
</body>
22+
23+
</html>

misc/do-rtmpsuck.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
# enable internal port forwarding:
4+
sudo sysctl -w net.inet.ip.forwarding=1
5+
6+
# apply the pf rules:
7+
echo '
8+
rdr pass log on lo0 proto tcp from en0 to any port 1935 -> 127.0.0.1
9+
pass out on en0 route-to lo0 inet proto tcp from en0 to any port 1935 keep state user != root
10+
' | sudo pfctl -ef -
11+
12+
# check the pf rules:
13+
# sudo pfctl -s all
14+
say "starting r-t-m-p-suck";
15+
16+
sudo rtmpsuck $@;
17+
18+
say "r-t-m-p-suck Stopped.";
19+
20+
# clear the pf rules:
21+
echo ''
22+
echo ''
23+
echo "====== restting pfctl rules ======="
24+
echo ''
25+
26+
sudo pfctl -F all -f /etc/pf.conf

misc/reloadChrome.scpt

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
on run {targetUrl}
2+
tell application "Google Chrome"
3+
activate
4+
5+
set theUrl to my remove_http(targetUrl)
6+
7+
if (count every window) = 0 then
8+
make new window
9+
end if
10+
11+
set found to false
12+
set theTabIndex to -1
13+
repeat with theWindow in every window
14+
set theTabIndex to 0
15+
16+
repeat with theTab in every tab of theWindow
17+
set theTabIndex to theTabIndex + 1
18+
set theTabUrl to my remove_http(theTab's URL as string)
19+
20+
if (theTabUrl contains theUrl) then
21+
set found to true
22+
exit repeat
23+
end if
24+
25+
end repeat
26+
27+
if found then
28+
exit repeat
29+
end if
30+
end repeat
31+
32+
if found then
33+
tell theTab to reload
34+
set theWindow's active tab index to theTabIndex
35+
set index of theWindow to 1
36+
else
37+
tell window 1 to make new tab with properties {URL:targetUrl}
38+
end if
39+
end tell
40+
end run
41+
42+
on remove_http(input_url)
43+
if (input_url contains "https://") then
44+
return trim_line(input_url, "https://")
45+
else
46+
return trim_line(input_url, "http://")
47+
end if
48+
return input_url
49+
end remove_http
50+
51+
-- Taken from: http://www.macosxautomation.com/applescript/sbrt/sbrt-06.html --
52+
on trim_line(this_text, trim_chars)
53+
set x to the length of the trim_chars
54+
-- TRIM BEGINNING
55+
repeat while this_text begins with the trim_chars
56+
try
57+
set this_text to characters (x + 1) thru -1 of this_text as string
58+
on error
59+
-- the text contains nothing but the trim characters
60+
return ""
61+
end try
62+
end repeat
63+
return this_text
64+
end trim_line

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"license": "ISC",
1111
"dependencies": {
1212
"babel-preset-es2015": "^6.16.0",
13+
"broadway-player": "^0.1.1",
1314
"buffer": "^5.0.0",
1415
"simple-websocket": "^4.1.0"
1516
},

test.js

Lines changed: 152 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,181 @@
11
var RTMP = require('./node-rtmpapi');
22
var SimpleWebsocket = require('simple-websocket');
3+
var Buffer = require('buffer').Buffer;
34

4-
var url = "ws://127.0.0.1:1999";
5+
const H264_SEP = new Buffer([0,0,0,1]);
6+
const FRAME_Q_SIZE = 15;
57

6-
var sock = new SimpleWebsocket(url);
8+
const url = {host: "ws://127.0.0.1:1999", app:"live", stream: "B012"};
79

8-
sock.on('close', function()
10+
var frameQ = [];
11+
var fps = 20;
12+
var lastRenderTime = 0;
13+
14+
var decoder = new Decoder();
15+
var player = new Player({ useWorker: false, webgl: true });
16+
player.canvas.style['height'] = '100%';
17+
document.getElementById("vidCont").appendChild(player.canvas);
18+
19+
var sock = new SimpleWebsocket(url.host);
20+
sock.setMaxListeners(100);
21+
22+
decoder.onPictureDecoded = function(buffer, width, height, infos)
923
{
10-
console.log("WTF... Socket Closed ");
11-
});
24+
if(frameQ.length === FRAME_Q_SIZE)
25+
{
26+
console.log("** drop oldest frame!");
27+
frameQ.shift(); //如果播放速度跟不上,扔掉最老那一帧
28+
}
29+
frameQ.push({data: Buffer.from(buffer), width: width, height: height, canvasObj: player.canvasObj});
30+
};
1231

13-
sock.on('error', function(e)
32+
function drawFrame()
1433
{
15-
console.log("WTF... Socket Error: " + e);
16-
});
34+
var now = new Date();//如果播放速度跟不上网络速度,跳帧
35+
var skipFrame = Math.floor(Math.abs(now - lastRenderTime) / (1000 / fps) ) - 1;
36+
if(lastRenderTime && skipFrame > 0)
37+
{
38+
console.log("SkipFrmae = " + skipFrame);
39+
while(skipFrame-- > 0 && frameQ.length > 0) frameQ.shift();
40+
}
41+
42+
var frame = frameQ.shift();
43+
if(frame)
44+
{
45+
player.renderFrameWebGL(frame);
46+
}
47+
lastRenderTime = now;
48+
}
1749

1850
sock.on('connect', function()
1951
{
20-
var stream = new RTMP.rtmpSession(sock, true, function(me)
52+
var transId = 0;
53+
var session = new RTMP.rtmpSession(sock, true, function(me)
2154
{
22-
console.log("rtmpSession...cb..ss.");
55+
console.log("rtmpSession...cb...");
56+
var invokeChannel = new RTMP.rtmpChunk.RtmpChunkMsgClass({streamId:5}, {sock: sock, Q: me.Q, debug: false});
57+
invokeChannel.invokedMethods = {}; //用来保存invoke的次数,以便收到消息的时候确认对应结果
58+
59+
var videoChannel = new RTMP.rtmpChunk.RtmpChunkMsgClass({streamId:8}, {sock: sock, Q: me.Q, debug: false});
60+
2361
var msger = me.msg;
2462
me.Q.Q(0,function()
2563
{
2664
console.log("sending connect");
27-
var chunk = new RTMP.rtmpChunk.RtmpChunkMsgClass({streamId:3}, {sock: sock, Q: me.Q, debug: true});
28-
chunk.sendAmf0EncCmdMsg({
65+
66+
//todo: 先确定可行,再重构
67+
invokeChannel.sendAmf0EncCmdMsg({
2968
cmd: 'connect',
30-
transId:1,
69+
transId:++transId,
3170
cmdObj:
3271
{
33-
app:"live",
72+
app: url.app,
3473
tcUrl: "rtmp://video.7uan7uan.com/live",
3574
fpad: false,
3675
capabilities: 15.0,
3776
audioCodecs: 3191,
3877
videoCodecs: 252,
3978
videoFunction: 1.0
40-
}
41-
/*args:''*/});
79+
}
80+
});
81+
invokeChannel.invokedMethods[transId] = 'connect';
82+
});
4283

43-
//read---
44-
//msger.loop();
84+
me.Q.Q(0, function()
85+
{
86+
console.log("Begin LOOP");
87+
msger.loop(handleMessage);
4588
});
89+
90+
function handleMessage(chunkMsg)
91+
{
92+
var chunk = chunkMsg.chunk;
93+
var msg = chunk.msg;
94+
95+
console.log("GOT MESSAGE: " + chunk.msgTypeText);
96+
console.log("===========>\n" + JSON.stringify(msg));
97+
98+
if(chunk.msgTypeText == "amf0cmd")
99+
{
100+
if(msg.cmd == "_result")
101+
{
102+
var lastInvoke = invokeChannel.invokedMethods[msg.transId];
103+
if(lastInvoke)
104+
{
105+
console.log("<--Got Invoke Result for: " + lastInvoke);
106+
delete invokeChannel.invokedMethods[msg.transId];
107+
}
108+
109+
if(lastInvoke == "connect") //确认是connect的结果
110+
{
111+
console.log("sending createStream");
112+
invokeChannel.sendAmf0EncCmdMsg({
113+
cmd: 'createStream',
114+
transId: ++transId,
115+
cmdObj: null
116+
});
117+
invokeChannel.invokedMethods[transId] = 'createStream';
118+
}
119+
else if(lastInvoke == "createStream") //确认是createStream的结果
120+
{
121+
videoChannel.chunk.msgStreamId = msg.info;
122+
//send play ??
123+
videoChannel.sendAmf0EncCmdMsg({
124+
cmd: 'play',
125+
transId: ++transId,
126+
cmdObj:null,
127+
streamName: url.stream,
128+
start:-2
129+
130+
},0);
131+
invokeChannel.invokedMethods[transId] = "play";
132+
}
133+
}
134+
else if(msg.cmd == 'onBWDone')
135+
{
136+
console.log("onBWDone");
137+
//send checkBW
138+
invokeChannel.sendAmf0EncCmdMsg({
139+
cmd: '_checkbw',
140+
transId: ++transId,
141+
cmdObj:null
142+
},0);
143+
invokeChannel.invokedMethods[transId] = "_checkbw";
144+
}
145+
}
146+
147+
if(chunk.msgTypeText == "video")
148+
{
149+
var chunkData = chunk.data;
150+
if (chunkData.length > 4)
151+
{
152+
if (chunkData[1] === 1)
153+
{
154+
chunkData = Buffer.concat([H264_SEP, chunkData.slice(9)]);
155+
}
156+
else if (chunkData[1] === 0)
157+
{
158+
var spsSize = (chunkData[11] << 8) | chunkData[12];
159+
var spsEnd = 13 + spsSize;
160+
chunkData = Buffer.concat([H264_SEP, chunkData.slice(13, spsEnd), H264_SEP, chunkData.slice(spsEnd + 3)]);
161+
}
162+
decoder.decode(chunkData);
163+
//player.decode(chunkData);
164+
}
165+
}
166+
167+
if(chunk.msgTypeText == "amf0meta" && msg.cmd == 'onMetaData')
168+
{
169+
console.log("onmetadata");
170+
fps = chunk.msg['event']['framerate'];
171+
console.log("fps = "+fps);
172+
setInterval(drawFrame, 1000.0/fps);
173+
}
174+
175+
me.Q.Q(0,function()
176+
{
177+
msger.loop(handleMessage);
178+
});
179+
}
46180
});
47181
});

0 commit comments

Comments
 (0)