You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+57-18Lines changed: 57 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ There are many NPM modules for connecting to the Raspberry Pi camera, why use th
11
11
12
12
-**Speed:** JPEG images can be captured in ~33ms using a built in MJPEG parser
13
13
-**Efficient:** Pictures and video streams are piped directly into Node as a `Buffer`, keeping all data in memory and eliminating disk I/O
14
-
-**Usable:** Video streams are available as `stream.Readable` objects that can be piped or listened to
14
+
-**Usable:** Video streams are available as [`stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable) objects that can be piped or listened to
15
15
-**Tested:** Contains automated tests using Jest
16
16
-**Modern:** Uses the latest ESNext features and up to date development practices
17
17
-**Structure**: Ships with TypeScript definition files
@@ -159,7 +165,7 @@ Capturing a video stream is easy. There are currently 2 codecs supported: `H264`
159
165
The GPU on the Raspberry Pi comes with a hardware-accelerated H264 encoder and JPEGencoder. To capture videos in real time, using these hardware encoders are required.
160
166
161
167
### Stream
162
-
A standard NodeJS [readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable) is available after calling `startCapture()`. As with any readable stream, it can be piped or listened to.
168
+
A standard NodeJS [readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable) is available after calling `createStream()`. As with any readable stream, it can be piped or listened to.
163
169
164
170
```javascript
165
171
import { StreamCamera, Codec } from "pi-camera-connect";
Begins the camera stream. Returns a `Promise`with a readable stream of the video data that is resolved when the capture has started.
275
+
### `startCapture(): Promise<void>`
276
+
Begins the camera stream. Returns a `Promise` that is resolved when the capture has started.
266
277
267
278
### `stopCapture(): Promise<void>`
268
279
Ends the camera stream. Returns a `Promise` that is resolved when the capture has stopped.
269
280
281
+
### `createStream(): stream.Readable`
282
+
Creates a [`readable stream`](https://nodejs.org/api/stream.html#stream_class_stream_readable) of video data. There is no limit to the number of streams you can create.
283
+
284
+
Be aware that, as with any readable stream, data will buffer in memory until it is read. If you create a video stream but do not read its data, your program will quickly run out of memory.
285
+
286
+
Ways to read data so that it does not remain buffered in memory include:
287
+
- Switching the stream to 'flowing' mode by calling either `resume()`, `pipe()`, or attaching a listener to the `'data'`event
288
+
- Calling `read()` when the stream is in'paused' mode
289
+
290
+
See the [readable stream documentation](https://nodejs.org/api/stream.html#stream_two_modes) for more information on flowing/paused modes.
291
+
292
+
```javascript
293
+
const streamCamera = new StreamCamera({
294
+
codec: Codec.H264
295
+
});
296
+
297
+
const videoStream = streamCamera.createStream();
298
+
299
+
await streamCamera.startCapture();
300
+
301
+
videoStream.on("data", data => console.log("New video data", data));
302
+
303
+
// Wait 5 seconds
304
+
await new Promise(resolve => setTimeout(() => resolve(), 5000));
305
+
306
+
await streamCamera.stopCapture();
307
+
```
308
+
270
309
### `takeImage(): Promise<Buffer>`
271
310
272
-
Takes a JPEG image from an MJPEG camera stream, resulting in very fast image captures. Returns a `Promise`with a `Buffer` containing the image bytes.
311
+
Takes a JPEG image frame from an MJPEG camera stream, resulting in very fast image captures. Returns a `Promise`with a `Buffer` containing the image bytes.
273
312
274
313
*Note:`StreamOptions.codec` must be set to `Codec.MJPEG`, otherwise `takeImage()`withthrow an error.*
Copy file name to clipboardExpand all lines: src/lib/stream-camera.ts
+18-14Lines changed: 18 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -41,7 +41,7 @@ class StreamCamera extends EventEmitter {
41
41
42
42
privateoptions: StreamOptions;
43
43
privatechildProcess?: ChildProcess;
44
-
privatestream?: stream.Readable;
44
+
privatestreams: Array<stream.Readable>=[];
45
45
46
46
constructor(options: StreamOptions={}){
47
47
@@ -58,7 +58,7 @@ class StreamCamera extends EventEmitter {
58
58
};
59
59
}
60
60
61
-
startCapture(): Promise<stream.Readable>{
61
+
startCapture(): Promise<void>{
62
62
63
63
returnnewPromise(async(resolve,reject)=>{
64
64
@@ -161,25 +161,21 @@ class StreamCamera extends EventEmitter {
161
161
"--output","-"
162
162
];
163
163
164
-
this.stream=newstream.Readable({
165
-
read: ()=>{}
166
-
});
167
-
168
164
// Spawn child process
169
165
this.childProcess=spawn("raspivid",args);
170
166
171
167
// Listen for error event to reject promise
172
168
this.childProcess.once("error",err=>reject(newError("Could not start capture with StreamCamera. Are you running on a Raspberry Pi with 'raspivid' installed?")));
0 commit comments