-
Notifications
You must be signed in to change notification settings - Fork 22
/
videoStream.js
executable file
·139 lines (124 loc) · 4.17 KB
/
videoStream.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
var Mpeg1Muxer, STREAM_MAGIC_BYTES, VideoStream, events, util, ws
ws = require('ws')
util = require('util')
events = require('events')
Mpeg1Muxer = require('./mpeg1muxer')
STREAM_MAGIC_BYTES = "jsmp" // Must be 4 bytes
VideoStream = function (options) {
this.options = options
this.name = options.name
this.streamUrl = options.streamUrl
this.width = options.width
this.height = options.height
this.wsPort = options.wsPort
this.inputStreamStarted = false
this.stream = undefined
this.startMpeg1Stream()
this.pipeStreamToSocketServer()
return this
}
util.inherits(VideoStream, events.EventEmitter)
VideoStream.prototype.stop = function () {
this.wsServer.close()
this.stream.kill()
this.inputStreamStarted = false
return this
}
VideoStream.prototype.startMpeg1Stream = function () {
var gettingInputData, gettingOutputData, inputData, outputData
this.mpeg1Muxer = new Mpeg1Muxer({
ffmpegPreOptions: this.options.ffmpegPreOptions,
ffmpegOptions: this.options.ffmpegOptions,
url: this.streamUrl,
ffmpegPath: !this.options.ffmpegPath ? "ffmpeg" : this.options.ffmpegPath
});
this.stream = this.mpeg1Muxer.stream
if (this.inputStreamStarted) {
return
}
this.mpeg1Muxer.on('mpeg1data', (data) => {
return this.emit('camdata', data)
})
gettingInputData = false
inputData = []
gettingOutputData = false
outputData = []
this.mpeg1Muxer.on('ffmpegStderr', (data) => {
var size
data = data.toString()
if (data.indexOf('Input #') !== -1) {
gettingInputData = true
}
if (data.indexOf('Output #') !== -1) {
gettingInputData = false
gettingOutputData = true
}
if (data.indexOf('frame') === 0) {
gettingOutputData = false
}
if (gettingInputData) {
inputData.push(data.toString())
size = data.match(/\d+x\d+/)
if (size != null) {
size = size[0].split('x')
if (this.width == null) {
this.width = parseInt(size[0], 10)
}
if (this.height == null) {
return this.height = parseInt(size[1], 10)
}
}
}
})
this.mpeg1Muxer.on('ffmpegStderr', function (data) {
return global.process.stderr.write(data)
})
this.mpeg1Muxer.on('exitWithError', () => {
return this.emit('exitWithError')
})
this.mpeg1Muxer.on('exitWithoutError', () => {
return this.emit('exitWithoutError')
})
return this
}
VideoStream.prototype.pipeStreamToSocketServer = function () {
this.wsServer = new ws.Server({
port: this.wsPort
})
this.wsServer.on("connection", (socket, request) => {
return this.onSocketConnect(socket, request)
})
this.wsServer.broadcast = function (data, opts) {
var results
results = []
for (let client of this.clients) {
if (client.readyState === 1) {
results.push(client.send(data, opts))
} else {
results.push(console.log("Error: Client from remoteAddress " + client.remoteAddress + " not connected."))
}
}
return results
}
return this.on('camdata', (data) => {
return this.wsServer.broadcast(data)
})
}
VideoStream.prototype.onSocketConnect = function (socket, request) {
var streamHeader
// Send magic bytes and video size to the newly connected socket
// struct { char magic[4]; unsigned short width, height;}
streamHeader = new Buffer(8)
streamHeader.write(STREAM_MAGIC_BYTES)
streamHeader.writeUInt16BE(this.width, 4)
streamHeader.writeUInt16BE(this.height, 6)
socket.send(streamHeader, {
binary: true
})
console.log(`${this.name}: New WebSocket Connection (` + this.wsServer.clients.size + " total)")
socket.remoteAddress = request.connection.remoteAddress
return socket.on("close", (code, message) => {
return console.log(`${this.name}: Disconnected WebSocket (` + this.wsServer.clients.size + " total)")
})
}
module.exports = VideoStream