-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
121 lines (91 loc) · 2.91 KB
/
app.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
var express = require('express.io'),
spawn = require('child_process').spawn,
fs = require('fs');
out = fs.openSync('./out.log', 'a');
var request = require('request');
var app = express();
app.http().io();
var tvheadendHost = 'http://192.168.1.50:9981';
var childPid = null;
var killWorker = function(callback) {
try {
if (childPid > 0) {
console.log('Killing process with pid '+childPid);
process.kill(childPid, 'SIGKILL');
app.io.broadcast('statusUpdate', {'msg': 'Stream gestoppt.'});
}
}
catch (e) {
console.log('The process was killed before...');
app.io.broadcast('statusUpdate', {'msg': 'Stream ist abgestürzt.'});
}
callback();
};
process.on( 'SIGINT', function() {
console.log( "\n...gracefully shutting down from SIGINT (Crtl-C)" )
// some other closing procedures go here
process.kill(childPid,'SIGKILL');
app.io.broadcast('statusUpdate', { 'msg' : 'Node.js Backend wurde beendet!' });
process.exit( )
})
/* Socket.IO Controlling */
app.io.sockets.on('connection', function (socket) {
socket.emit('say-hello', { 'connected': true });
});
/* Express controllers */
app.use(express.static(__dirname));
app.get('/channels', function(req, res) {
request(tvheadendHost + '/channels?op=list', function(error, response, body) {
res.send(200, body);
});
});
app.get('/channelTags', function(req, res) {
request(tvheadendHost + '/channeltags?op=listTags', function(error, response, body) {
res.send(200, body);
});
});
app.get('/epg', function(req, res) {
request(tvheadendHost + '/epg?limit=500&start=0', function(error, response, body) {
res.send(200, body);
});
});
app.get('/', function(req, res) {
res.sendfile( __dirname+ '/index.html');
});
app.io.route('switchToChannel', function(req) {
req.io.join(req.data);
// make sure you set the correct path to your video file storage
var pathToMovie = tvheadendHost + '/stream/channelid/' +req.data.id;
var cmd = 'avconv';
var args =
['-re', '-i' , pathToMovie,
'-acodec', 'libmp3lame',
'-b:a', '128k',
'-ar', '44100',
'-vcodec', 'libx264',
'-filter:v', 'yadif',
'-threads', '4',
//'-preset', 'veryslow',
'-profile:v', 'baseline',
'-b:v', '5000k',
'-f', 'flv',
'-s', '964x544',
'-metadata', 'streamName=myStreamName',
'tcp://127.0.0.1:6666'];
killWorker(function() {
console.log('Starting stream for: '+ pathToMovie);
console.log('Command is: '+ cmd + args.join(' '));
child = spawn(cmd, args, {
detached: true,
stdio: [ 'ignore', out, null ]
});
console.log('Spawned child pid: ' + child.pid);
var response = {
'msg' : 'Wiedergabe wird gestartet...',
'startPlayback': true
};
childPid = child.pid;
req.io.emit('statusUpdate', response);
});
});
app.listen(4000);