11import { ChildProcessWithoutNullStreams , spawn } from 'child_process' ;
22import { EventEmitter } from 'events' ;
33import * as stream from 'stream' ;
4- import * as si from 'systeminformation' ;
54import { AwbMode , ExposureMode , Flip , Rotation } from '..' ;
65import { getSharedArgs } from './shared-args' ;
76
@@ -53,6 +52,8 @@ class StreamCamera extends EventEmitter {
5352 private childProcess ?: ChildProcessWithoutNullStreams ;
5453 private streams : Array < stream . Readable > = [ ] ;
5554
55+ static readonly jpegSignature = Buffer . from ( [ 0xff , 0xd8 , 0xff , 0xdb , 0x00 , 0x84 , 0x00 ] ) ;
56+
5657 constructor ( options : StreamOptions = { } ) {
5758 super ( ) ;
5859
@@ -67,23 +68,6 @@ class StreamCamera extends EventEmitter {
6768 } ;
6869 }
6970
70- static async getJpegSignature ( ) {
71- const systemInfo = await si . system ( ) ;
72- switch ( systemInfo . model ) {
73- case 'BCM2711' :
74- case 'BCM2835 - Pi 3 Model B' :
75- case 'BCM2835 - Pi 3 Model B+' :
76- case 'BCM2835 - Pi 4 Model B' :
77- case 'BCM2835 - Pi Zero' :
78- case 'BCM2835 - Pi Zero W' :
79- return Buffer . from ( [ 0xff , 0xd8 , 0xff , 0xdb , 0x00 , 0x84 , 0x00 ] ) ;
80- default :
81- throw new Error (
82- `Could not determine JPEG signature. Unknown system model '${ systemInfo . model } '` ,
83- ) ;
84- }
85- }
86-
8771 startCapture ( ) : Promise < void > {
8872 // eslint-disable-next-line no-async-promise-executor
8973 return new Promise ( async ( resolve , reject ) => {
@@ -183,7 +167,6 @@ class StreamCamera extends EventEmitter {
183167 // Wait for first data event to resolve promise
184168 this . childProcess . stdout . once ( 'data' , ( ) => resolve ( ) ) ;
185169
186- const jpegSignature = await StreamCamera . getJpegSignature ( ) ;
187170 let stdoutBuffer = Buffer . alloc ( 0 ) ;
188171
189172 // Listen for image data events and parse MJPEG frames if codec is MJPEG
@@ -196,14 +179,17 @@ class StreamCamera extends EventEmitter {
196179
197180 // Extract all image frames from the current buffer
198181 while ( true ) {
199- const signatureIndex = stdoutBuffer . indexOf ( jpegSignature , 0 ) ;
182+ const signatureIndex = stdoutBuffer . indexOf ( StreamCamera . jpegSignature , 0 ) ;
200183
201184 if ( signatureIndex === - 1 ) break ;
202185
203186 // Make sure the signature starts at the beginning of the buffer
204187 if ( signatureIndex > 0 ) stdoutBuffer = stdoutBuffer . slice ( signatureIndex ) ;
205188
206- const nextSignatureIndex = stdoutBuffer . indexOf ( jpegSignature , jpegSignature . length ) ;
189+ const nextSignatureIndex = stdoutBuffer . indexOf (
190+ StreamCamera . jpegSignature ,
191+ StreamCamera . jpegSignature . length ,
192+ ) ;
207193
208194 if ( nextSignatureIndex === - 1 ) break ;
209195
0 commit comments