forked from pimterry/raspivid-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (66 loc) · 2.32 KB
/
index.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
const Splitter = require('stream-split');
const stream = require('stream');
const StreamConcat = require('stream-concat');
const raspivid = require('raspivid');
const NALseparator = new Buffer([0,0,0,1]);
const headerData = {
_waitingStream: new stream.PassThrough(),
_firstFrames: [],
_lastIdrFrame: null,
set idrFrame(frame) {
this._lastIdrFrame = frame;
if (this._waitingStream) {
const waitingStream = this._waitingStream;
this._waitingStream = null;
this.getStream().pipe(waitingStream);
}
},
addParameterFrame: function (frame) {
this._firstFrames.push(frame)
},
getStream: function () {
if (this._waitingStream) {
return this._waitingStream;
} else {
const headersStream = new stream.PassThrough();
this._firstFrames.forEach((frame) => headersStream.push(frame));
headersStream.push(this._lastIdrFrame);
headersStream.end();
return headersStream;
}
}
};
// This returns the live stream only, without the parameter chunks
function getLiveStream(options) {
return raspivid(Object.assign({
width: 960,
height: 540,
framerate: 20,
profile: 'baseline',
timeout: 0
}, options))
.pipe(new Splitter(NALseparator))
.pipe(new stream.Transform({ transform: function (chunk, encoding, callback) {
const chunkWithSeparator = Buffer.concat([NALseparator, chunk]);
const chunkType = chunk[0] & 0b11111;
// Capture the first SPS & PPS frames, so we can send stream parameters on connect.
if (chunkType === 7 || chunkType === 8) {
headerData.addParameterFrame(chunkWithSeparator);
} else {
// The live stream only includes the non-parameter chunks
this.push(chunkWithSeparator);
// Keep track of the latest IDR chunk, so we can start clients off with a near-current image
if (chunkType === 5) {
headerData.idrFrame = chunkWithSeparator;
}
}
callback();
}}));
}
var liveStream = null;
module.exports = function (options) {
if (!liveStream) {
liveStream = getLiveStream(options);
}
return new StreamConcat([headerData.getStream(), liveStream]);
}