aruntj / mjpeg-readable-stream

Code to read an Mjpeg stream using the Readable stream api.
MIT License
53 stars 13 forks source link

What if there are no headers? #6

Open edwinallan opened 1 year ago

edwinallan commented 1 year ago

I have a mjpeg stream from a Canon camera (live view). The problem is that it does not provide any headers (appart from application/octet-stream)

How can you script be adapted to function? I tried listening for JPEG EOI, but i get a corrupted image:

const EOI = new Uint8Array(2);
EOI[0] = 0xFF;
EOI[1] = 0xD9;

reader.read().then(({ done, value }) => {
    if (done) {
        this.close();
        return;
    }

    for (let index = 0; index < value.length; index++) {
        if (value[index] == SOI[0] && value[index + 1] == SOI[1]) {
            console.log('start of jpeg');

            // Reset the buffer!
            imageBuffer = new Array();
            jpegStarted = true;
        }

        // add value to jpeg bytes
        imageBuffer[bytesRead++] = value[index];

        if (value[index - 1] == EOI[0] && value[index] == EOI[1] && jpegStarted) {
            // end of jpeg
            console.log('end of jpeg');

            unitarray = new Uint8Array(imageBuffer);

            let frame = URL.createObjectURL(new Blob([imageBuffer], { type: TYPE_JPEG }))
            image.src = frame;
            URL.revokeObjectURL(frame)

            bytesRead = 0;

            imageBuffer = new Array();
        }
    }

    read();
}