-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathstream-camera.ts
More file actions
245 lines (204 loc) · 7.8 KB
/
stream-camera.ts
File metadata and controls
245 lines (204 loc) · 7.8 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import { ChildProcessWithoutNullStreams, spawn } from 'child_process';
import { EventEmitter } from 'events';
import * as stream from 'stream';
import { AwbMode, ExposureMode, Flip, Rotation } from '..';
import { getSharedArgs } from './shared-args';
export enum Codec {
H264 = 'H264',
MJPEG = 'MJPEG',
}
export enum SensorMode {
AutoSelect = 0,
Mode1 = 1,
Mode2 = 2,
Mode3 = 3,
Mode4 = 4,
Mode5 = 5,
Mode6 = 6,
Mode7 = 7,
}
export interface StreamOptions {
width?: number;
height?: number;
rotation?: Rotation;
flip?: Flip;
bitRate?: number;
fps?: number;
codec?: Codec;
sensorMode?: SensorMode;
shutter?: number;
sharpness?: number;
contrast?: number;
brightness?: number;
saturation?: number;
iso?: number;
exposureCompensation?: number;
exposureMode?: ExposureMode;
awbMode?: AwbMode;
analogGain?: number;
digitalGain?: number;
annotate?: (number | string)[];
}
declare interface StreamCamera {
on(event: 'frame', listener: (image: Buffer) => void): this;
once(event: 'frame', listener: (image: Buffer) => void): this;
}
class StreamCamera extends EventEmitter {
private readonly options: StreamOptions;
private childProcess?: ChildProcessWithoutNullStreams;
private streams: Array<stream.Readable> = [];
static readonly jpegSignature = Buffer.from([0xff, 0xd8, 0xff, 0xdb, 0x00, 0x84, 0x00]);
constructor(options: StreamOptions = {}) {
super();
this.options = {
rotation: Rotation.Rotate0,
flip: Flip.None,
bitRate: 17000000,
fps: 30,
codec: Codec.H264,
sensorMode: SensorMode.AutoSelect,
...options,
};
}
startCapture(): Promise<void> {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {
// TODO: refactor promise logic to be more ergonomic
// so that we don't need to try/catch here
try {
const args: Array<string> = [
/**
* Add the command-line arguments that are common to both `raspivid` and `raspistill`
*/
...getSharedArgs(this.options),
/**
* Bit rate
*/
...(this.options.bitRate ? ['--bitrate', this.options.bitRate.toString()] : []),
/**
* Frame rate
*/
...(this.options.fps ? ['--framerate', this.options.fps.toString()] : []),
/**
* Codec
*
* H264 or MJPEG
*
*/
...(this.options.codec ? ['--codec', this.options.codec.toString()] : []),
/**
* Sensor mode
*
* Camera version 1.x (OV5647):
*
* | Mode | Size | Aspect Ratio | Frame rates | FOV | Binning |
* |------|---------------------|--------------|-------------|---------|---------------|
* | 0 | automatic selection | | | | |
* | 1 | 1920x1080 | 16:9 | 1-30fps | Partial | None |
* | 2 | 2592x1944 | 4:3 | 1-15fps | Full | None |
* | 3 | 2592x1944 | 4:3 | 0.1666-1fps | Full | None |
* | 4 | 1296x972 | 4:3 | 1-42fps | Full | 2x2 |
* | 5 | 1296x730 | 16:9 | 1-49fps | Full | 2x2 |
* | 6 | 640x480 | 4:3 | 42.1-60fps | Full | 2x2 plus skip |
* | 7 | 640x480 | 4:3 | 60.1-90fps | Full | 2x2 plus skip |
*
*
* Camera version 2.x (IMX219):
*
* | Mode | Size | Aspect Ratio | Frame rates | FOV | Binning |
* |------|---------------------|--------------|-------------|---------|---------|
* | 0 | automatic selection | | | | |
* | 1 | 1920x1080 | 16:9 | 0.1-30fps | Partial | None |
* | 2 | 3280x2464 | 4:3 | 0.1-15fps | Full | None |
* | 3 | 3280x2464 | 4:3 | 0.1-15fps | Full | None |
* | 4 | 1640x1232 | 4:3 | 0.1-40fps | Full | 2x2 |
* | 5 | 1640x922 | 16:9 | 0.1-40fps | Full | 2x2 |
* | 6 | 1280x720 | 16:9 | 40-90fps | Partial | 2x2 |
* | 7 | 640x480 | 4:3 | 40-90fps | Partial | 2x2 |
*
*/
...(this.options.sensorMode ? ['--mode', this.options.sensorMode.toString()] : []),
/**
* Capture time (ms)
*
* Zero = forever
*
*/
'--timeout',
(0).toString(),
/**
* Do not display preview overlay on screen
*/
'--nopreview',
/**
* Output to stdout
*/
'--output',
'-',
];
// Spawn child process
this.childProcess = spawn('raspivid', args);
// Listen for error event to reject promise
this.childProcess.once('error', () =>
reject(
new Error(
"Could not start capture with StreamCamera. Are you running on a Raspberry Pi with 'raspivid' installed?",
),
),
);
// Wait for first data event to resolve promise
this.childProcess.stdout.once('data', () => resolve());
let stdoutBuffer = Buffer.alloc(0);
// Listen for image data events and parse MJPEG frames if codec is MJPEG
this.childProcess.stdout.on('data', (data: Buffer) => {
this.streams.forEach(stream => stream.push(data));
if (this.options.codec !== Codec.MJPEG) return;
stdoutBuffer = Buffer.concat([stdoutBuffer, data]);
// Extract all image frames from the current buffer
while (true) {
const signatureIndex = stdoutBuffer.indexOf(StreamCamera.jpegSignature, 0);
if (signatureIndex === -1) break;
// Make sure the signature starts at the beginning of the buffer
if (signatureIndex > 0) stdoutBuffer = stdoutBuffer.slice(signatureIndex);
const nextSignatureIndex = stdoutBuffer.indexOf(
StreamCamera.jpegSignature,
StreamCamera.jpegSignature.length,
);
if (nextSignatureIndex === -1) break;
this.emit('frame', stdoutBuffer.slice(0, nextSignatureIndex));
stdoutBuffer = stdoutBuffer.slice(nextSignatureIndex);
}
});
// Listen for error events
this.childProcess.stdout.on('error', err => this.emit('error', err));
this.childProcess.stderr.on('data', data => this.emit('error', new Error(data.toString())));
this.childProcess.stderr.on('error', err => this.emit('error', err));
// Listen for close events
this.childProcess.stdout.on('close', () => this.emit('close'));
} catch (err) {
return reject(err);
}
});
}
async stopCapture() {
if (this.childProcess) {
this.childProcess.kill();
}
// Push null to each stream to indicate EOF
// tslint:disable-next-line no-null-keyword
this.streams.forEach(stream => stream.push(null));
this.streams = [];
}
createStream() {
const newStream = new stream.Readable({
read: () => {},
});
this.streams.push(newStream);
return newStream;
}
takeImage() {
if (this.options.codec !== Codec.MJPEG) throw new Error("Codec must be 'MJPEG' to take image");
return new Promise<Buffer>(resolve => this.once('frame', data => resolve(data)));
}
}
export default StreamCamera;