Skip to content

Commit

Permalink
fix(#35): graceful ffmpeg shutdown, update rtsp streams for the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
agsh committed Jul 1, 2024
1 parent 706ed5e commit b67482a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
21 changes: 12 additions & 9 deletions example/index-canvas.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,31 @@
if (divSocket) {
divSocket.disconnect();
}
if (this.value === '/') {
return;
}
console.log(this.value);
divSocket = io(location.origin + this.value);
divSocket.on('data', function(data) {

var bytes = new Uint8Array(data);

var blob = new Blob([bytes], {type: 'application/octet-binary'});

var url = URL.createObjectURL(blob);

var img = new Image;

var ctx = canvas.getContext("2d");

img.onload = function() {
URL.revokeObjectURL(url);
ctx.drawImage(img,100,100);

};

img.src = url;

//image.src = 'data:image/jpeg;base64,' + base64ArrayBuffer(bytes);
});
};
Expand Down Expand Up @@ -106,4 +109,4 @@
}
</script>
</body>
</html>
</html>
5 changes: 4 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
if (divSocket) {
divSocket.disconnect();
}
if (this.value === '/') {
return;
}
console.log(this.value);
divSocket = io(location.origin + this.value);
divSocket.on('data', function(data) {
Expand Down Expand Up @@ -103,4 +106,4 @@

</script>
</body>
</html>
</html>
7 changes: 4 additions & 3 deletions example/server-canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ server.listen(6147, function(){


var cams = [
'rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov'
, 'rtsp://freja.hiof.no:1935/rtplive/definst/hessdalen03.stream'
, 'udp://localhost:1234'
'http://webcam.mchcares.com/mjpg/video.mjpg?timestamp=1566232173730',
'http://77.222.181.11:8080/mjpg/video.mjpg',
'http://tamperehacklab.tunk.org:38001/nphMotionJpeg?Resolution=640x480&Quality=Clarity',
'udp://localhost:1234' // Your rtsp stream
].map(function(uri, i) {
var stream = new rtsp.FFMpeg({input: uri, resolution: '320x240', quality: 3});
stream.on('start', function() {
Expand Down
7 changes: 4 additions & 3 deletions example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ server.listen(6147, function(){


var cams = [
'rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov'
, 'rtsp://freja.hiof.no:1935/rtplive/definst/hessdalen03.stream'
, 'udp://localhost:1234'
'http://webcam.mchcares.com/mjpg/video.mjpg?timestamp=1566232173730',
'http://77.222.181.11:8080/mjpg/video.mjpg',
'http://tamperehacklab.tunk.org:38001/nphMotionJpeg?Resolution=640x480&Quality=Clarity',
'udp://localhost:1234', // Your rtsp stream
].map(function(uri, i) {
var stream = new rtsp.FFMpeg({input: uri, resolution: '320x240', quality: 3});
stream.on('start', function() {
Expand Down
14 changes: 9 additions & 5 deletions lib/rtsp-ffmpeg.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ FFMpeg.prototype.start = function() {
this.child.stdout.on('data', function(data){
//The image can be composed of one or multiple chunk when receiving stream data.
//Store all bytes into an array until we meet flag "FF D9" that mean it's the end of the image then we can send all data in order to display the full image.
if (data.length >1) {
if (data.length > 1) {
self.buffs.push(data);

offset = data[data.length-2].toString(16);
offset2 = data[data.length-1].toString(16);

if(offset == "ff" && offset2 == "d9") {
if(offset === 'ff' && offset2 === 'd9') {
self.emit('data', Buffer.concat(self.buffs));
self.buffs = [];
}
Expand All @@ -107,8 +107,10 @@ FFMpeg.prototype.start = function() {
throw new Error(data);
});
this.emit('start');
this.child.on('close', function(code) {
if (code === 0) {
this.child.on('exit', function(code) {
this.emit('exit', code);
if (code !== 0) {
// try to reconnect once if the connection was lost
setTimeout(FFMpeg.prototype.start.bind(this), 1000);
}
}.bind(this));
Expand All @@ -126,7 +128,9 @@ FFMpeg.prototype.start = function() {
*/
FFMpeg.prototype.stop = function() {
if (this.child) {
this.child.kill();
this.child.stdin.write('q\r\n');
this.child.stdin.end();
// this.child.kill();
}
delete this.child;
this.emit('stop');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "rtsp-ffmpeg",
"license": "MIT",
"version": "0.0.17",
"version": "0.0.18",
"author": "Andrew D.Laptev <[email protected]>",
"contributors": [
{
Expand Down

0 comments on commit b67482a

Please sign in to comment.