-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
93 lines (71 loc) · 3 KB
/
server.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
const child_process = require('child_process');
const express = require('express');
const WebSocketServer = require('ws').Server;
const http = require('http');
const app = express();
const server = http.createServer(app).listen(3000, () => {
console.log('Listening...');
});
const wss = new WebSocketServer({
server: server
});
app.use((req, res, next) => {
console.log('HTTP Request: ' + req.method + ' ' + req.originalUrl);
return next();
});
app.use(express.static(__dirname + '/www'));
wss.on('connection', (ws, req) => {
const streamKey = "sk_us-west-**************************";
const rtmpUrl = 'rtmps://******.global-*******-video.net:443/app/'+streamKey;
console.log('Target RTMP URL:', rtmpUrl);
// Launch FFmpeg to handle all appropriate transcoding, muxing, and RTMP
const ffmpeg = child_process.spawn('ffmpeg', [
// Facebook requires an audio track, so we create a silent one here.
// Remove this line, as well as `-shortest`, if you send audio from the browser.
'-f', 'lavfi', '-i', 'anullsrc',
// FFmpeg will read input video from STDIN
'-i', '-',
// Because we're using a generated audio source which never ends,
// specify that we'll stop at end of other input. Remove this line if you
// send audio from the browser.
'-shortest',
// If we're encoding H.264 in-browser, we can set the video codec to 'copy'
// so that we don't waste any CPU and quality with unnecessary transcoding.
// If the browser doesn't support H.264, set the video codec to 'libx264'
// or similar to transcode it to H.264 here on the server.
'-vcodec', 'copy',
// AAC audio is required for Facebook Live. No browser currently supports
// encoding AAC, so we must transcode the audio to AAC here on the server.
'-acodec', 'aac',
// FLV is the container format used in conjunction with RTMP
'-f', 'flv',
// The output RTMP URL.
// For debugging, you could set this to a filename like 'test.flv', and play
// the resulting file with VLC.
rtmpUrl
]);
// If FFmpeg stops for any reason, close the WebSocket connection.
ffmpeg.on('close', (code, signal) => {
console.log('FFmpeg child process closed, code ' + code + ', signal ' + signal);
ws.terminate();
});
// Handle STDIN pipe errors by logging to the console.
// These errors most commonly occur when FFmpeg closes and there is still
// data to write. If left unhandled, the server will crash.
ffmpeg.stdin.on('error', (e) => {
console.log('FFmpeg STDIN Error', e);
});
// FFmpeg outputs all of its messages to STDERR. Let's log them to the console.
ffmpeg.stderr.on('data', (data) => {
console.log('FFmpeg STDERR:', data.toString());
});
// When data comes in from the WebSocket, write it to FFmpeg's STDIN.
ws.on('message', (msg) => {
console.log('DATA', msg);
ffmpeg.stdin.write(msg);
});
// If the client disconnects, stop FFmpeg.
ws.on('close', (e) => {
ffmpeg.kill('SIGINT');
});
});