-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reworked the way the streaming backed works
- Loading branch information
Marco Keur
committed
Feb 24, 2016
1 parent
0b30824
commit 30a9fc2
Showing
7 changed files
with
284 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
(function() { | ||
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"; | ||
|
||
VideoStream = function(options) { | ||
this.name = options.name; | ||
this.streamUrl = options.streamUrl; | ||
this.width = options.width; | ||
this.height = options.height; | ||
this.wsPort = options.wsPort; | ||
|
||
this.stream = void 0; | ||
this.initMpeg1Muxer(); | ||
this.initWebsocketServer(); | ||
return this; | ||
}; | ||
|
||
util.inherits(VideoStream, events.EventEmitter); | ||
|
||
VideoStream.prototype.initMpeg1Muxer = function() { | ||
var gettingInputData, gettingOutputData, inputData, outputData, self; | ||
this.mpeg1Muxer = new Mpeg1Muxer({ | ||
url: this.streamUrl | ||
}); | ||
self = this; | ||
|
||
this.mpeg1Muxer.on('mpeg1data', function(data) { | ||
return self.emit('camdata', data); | ||
}); | ||
gettingInputData = false; | ||
inputData = []; | ||
gettingOutputData = false; | ||
outputData = []; | ||
this.mpeg1Muxer.on('ffmpegError', function(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 (self.width == null) { | ||
self.width = parseInt(size[0], 10); | ||
} | ||
if (self.height == null) { | ||
return self.height = parseInt(size[1], 10); | ||
} | ||
} | ||
} | ||
}); | ||
//this.mpeg1Muxer.on('ffmpegError', function(data) { | ||
// return global.process.stderr.write(data); | ||
//}); | ||
return this; | ||
}; | ||
|
||
VideoStream.prototype.initWebsocketServer = function() { | ||
var self; | ||
self = this; | ||
this.wsServer = new ws.Server({ | ||
port: this.wsPort | ||
}); | ||
this.wsServer.on("connection", function(socket) { | ||
return self.onSocketConnect(socket); | ||
}); | ||
this.wsServer.broadcast = function(data, opts) { | ||
var i, _results; | ||
_results = []; | ||
for (i in this.clients) { | ||
if (this.clients[i].readyState === 1) { | ||
_results.push(this.clients[i].send(data, opts)); | ||
} else { | ||
_results.push(console.log("Error: Client (" + i + ") not connected.")); | ||
} | ||
} | ||
return _results; | ||
}; | ||
return this.on('camdata', function(data) { | ||
return self.wsServer.broadcast(data); | ||
}); | ||
}; | ||
|
||
function sendHeader(socket, width, height) { | ||
streamHeader = new Buffer(8); | ||
streamHeader.write(STREAM_MAGIC_BYTES); | ||
streamHeader.writeUInt16BE(width, 4); | ||
streamHeader.writeUInt16BE(height, 6); | ||
socket.send(streamHeader, { | ||
binary: true | ||
}); | ||
} | ||
|
||
VideoStream.prototype.onSocketConnect = function(socket) { | ||
var self, streamHeader; | ||
self = this; | ||
|
||
self.mpeg1Muxer.startStream(); | ||
|
||
if(self.width == undefined){ | ||
setTimeout(function(){ | ||
sendHeader(socket, self.width, self.height); | ||
}, 2500); | ||
}else{ | ||
sendHeader(socket, self.width, self.height); | ||
} | ||
|
||
console.log(("" + this.name + ": New WebSocket Connection (") + this.wsServer.clients.length + " total)"); | ||
|
||
socket.on("close", function(code, message) { | ||
console.log(("" + self.name + ": Disconnected WebSocket (") + self.wsServer.clients.length + " total)"); | ||
|
||
if(self.wsServer.clients.length < 1){ | ||
self.mpeg1Muxer.stopStream(); | ||
console.log("Tearing down the mpeg1Muxer."); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = VideoStream; | ||
|
||
}).call(this); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta name="viewport" content="width=320, initial-scale=1"/> | ||
<title>jsmpeg streaming</title> | ||
<style type="text/css"> | ||
body { | ||
background: #333; | ||
text-align: center; | ||
margin-top: 10%; | ||
} | ||
#videoCanvas { | ||
/* Always stretch the canvas to 640x480, regardless of its | ||
internal size. */ | ||
width: 640px; | ||
height: 480px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<!-- The Canvas size specified here is the "initial" internal resolution. jsmpeg will | ||
change this internal resolution to whatever the source provides. The size the | ||
canvas is displayed on the website is dictated by the CSS style. | ||
--> | ||
<canvas id="videoCanvas" width="640" height="480"> | ||
<p> | ||
Please use a browser that supports the Canvas Element, like | ||
<a href="http://www.google.com/chrome">Chrome</a>, | ||
<a href="http://www.mozilla.com/firefox/">Firefox</a>, | ||
<a href="http://www.apple.com/safari/">Safari</a> or Internet Explorer 10 | ||
</p> | ||
</canvas> | ||
<script type="text/javascript" src="../../app/jsmpg.js"></script> | ||
<script type="text/javascript"> | ||
// Setup the WebSocket connection and start the player | ||
var client = new WebSocket( 'ws://localhost:9999/' ); | ||
|
||
var canvas = document.getElementById('videoCanvas'); | ||
var player = new jsmpeg(client, {canvas:canvas}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Stream = require('../../lib/videoStream.js'); | ||
stream = new Stream({ | ||
name: 'name', | ||
streamUrl: 'rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov', | ||
wsPort: 9999 | ||
}); |