forked from kevinGodell/ffmpeg-streamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
144 lines (128 loc) · 4.14 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict'
let port = normalizePort(process.argv[2] || process.env.PORT || '8181')
const portRange = port + 10
const nodeEnv = process.argv[3] || process.env.NODE_ENV || 'production'
const express = require('express')
const app = express()
const http = require('http')
const server = http.createServer(app)
const io = require('socket.io').listen(server)
const path = require('path')
const logger = require('morgan')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const ejs = require('ejs')
const index = require('./routes/index')
const install = require('./routes/install')
const hls = require('./routes/hls')
const mp4 = require('./routes/mp4')
const mjpeg = require('./routes/mjpeg')
const progress = require('./routes/progress')
const assets = require('./routes/assets')
const info = require('./routes/info')
const jpegSocket = require('./sockets/jpeg')(app, io)
const mseSocket = require('./sockets/mse')(app, io)
const progressSocket = require('./sockets/progress')(app, io)
const m3u8Socket = require('./sockets/m3u8')(app, io)
const installSocket = require('./sockets/install')(app, io)
const stderrSocket = require('./sockets/stderr')(app, io)
const dirName = process.pkg && process.pkg.entrypoint ? path.dirname(process.execPath) : __dirname
const ffmpeg = require('./lib/findFfmpeg')(dirName)
const activity = require('./lib/activityLog')(dirName)
app.set('dirName', dirName)
app.set('ffmpegVersion', ffmpeg.version)
app.set('ffmpegPath', ffmpeg.path)
app.set('activity', activity)
app.set('env', nodeEnv)
app.set('port', port)
app.set('io', io)
app.set('jpegSocket', jpegSocket)
app.set('mseSocket', mseSocket)
app.set('progressSocket', progressSocket)
app.set('m3u8Socket', m3u8Socket)
app.set('installSocket', installSocket)
app.set('stderrSocket', stderrSocket)
app.set('ejs', ejs)
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
// uncomment after placing your favicon in /public
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
if (nodeEnv === 'development') {
// logs all requests to console
app.use(logger('dev'))
}
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.use('/info', info)
app.use('/assets', assets)
app.use('/', index)
app.use('/install', install)
app.use('/hls', hls)
app.use('/mp4', mp4)
app.use('/mjpeg', mjpeg)
app.use('/progress', progress)
app.use('/mdi', express.static(path.join(__dirname, 'node_modules/material-design-icons/iconfont')))
app.use('/mdl', express.static(path.join(__dirname, 'node_modules/material-design-lite/dist')))
app.use(express.static(path.join(__dirname, 'public')))
app.use(function (req, res, next) {
const err = new Error('Not Found')
console.error(`${req.url} not found.`)
err.status = 404
next(err)
})
app.use(function (err, req, res, next) {
res.status(err.status || 500)
res.render('error', {
message: err.message,
status: err.status,
stack: nodeEnv === 'development' ? err.stack : ''
})
if (nodeEnv === 'development') {
console.error(err)
}
// next(err);
})
if (nodeEnv === 'development') {
app.use(logger('dev'))
}
server.listen(port)
server.on('error', onError)
server.on('listening', onListening)
function normalizePort (val) {
const port = parseInt(val, 10)
if (isNaN(port)) {
return val
}
if (port >= 0) {
return port
}
return false
}
function onError (error) {
if (error.syscall !== 'listen') {
throw error
}
const bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges')
return process.exit(1)
case 'EADDRINUSE':
console.error(`${bind} is already in use.`)
if (typeof port === 'string' || port >= portRange) {
return process.exit(1)
}
console.log(`Incrementing to port ${++port} and trying again.`)
server.listen(port)
break
default:
throw error
}
}
function onListening () {
const addr = server.address()
const bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port
console.log(`Listening on ${bind}.`)
}
module.exports = app