Skip to content

🎥 Make videos programmatically with React

Update make-streamer.ts

Jonny Burger9373bc116171
diff --git a/packages/streaming/src/make-streamer.ts b/packages/streaming/src/make-streamer.ts
index 9184a56f39..e364953559 100644
--- a/packages/streaming/src/make-streamer.ts
+++ b/packages/streaming/src/make-streamer.ts
@@ -26,14 +26,32 @@ export const makeStreamer = (
dataMissing: number;
} = null;
+ const findSeparatorIndex = () => {
+ let searchIndex = 0;
+
+ // eslint-disable-next-line no-constant-condition
+ while (true) {
+ const separatorIndex = outputBuffer.indexOf(separator[0], searchIndex); // Start checking for the first byte of the separator
+ if (separatorIndex === -1) {
+ return -1;
+ }
+
+ if (
+ outputBuffer
+ .subarray(separatorIndex, separatorIndex + separator.length)
+ .toString() !== separator.toString()
+ ) {
+ searchIndex = separatorIndex + 1;
+ continue;
+ }
+
+ return separatorIndex;
+ }
+ };
+
const processInput = () => {
- let separatorIndex = outputBuffer.indexOf(separator[0]); // Start checking for the first byte of the separator
- if (
- separatorIndex === -1 ||
- outputBuffer
- .subarray(separatorIndex, separatorIndex + separator.length)
- .toString() !== separator.toString()
- ) {
+ let separatorIndex = findSeparatorIndex(); // Start checking for the first byte of the separator
+ if (separatorIndex === -1) {
return;
}
@@ -129,28 +147,31 @@ export const makeStreamer = (
const onData = (data: Uint8Array) => {
unprocessedBuffers.push(data);
- const separatorIndex = data.indexOf(separator[0]);
- if (separatorIndex === -1) {
- if (missingData) {
- missingData.dataMissing -= data.length;
- }
- if (!missingData || missingData.dataMissing > 0) {
- return;
- }
+ if (missingData) {
+ missingData.dataMissing -= data.length;
+ }
+
+ if (missingData && missingData.dataMissing > 0) {
+ return;
}
- unprocessedBuffers.unshift(outputBuffer);
- outputBuffer = new Uint8Array(
- unprocessedBuffers.reduce((acc, val) => acc + val.length, 0),
+ const newBuffer = new Uint8Array(
+ outputBuffer.length +
+ unprocessedBuffers.reduce((acc, val) => acc + val.length, 0),
);
- let offset = 0;
+ newBuffer.set(outputBuffer, 0);
+
+ let offset = outputBuffer.length;
for (const buf of unprocessedBuffers) {
- outputBuffer.set(buf, offset);
+ newBuffer.set(buf, offset);
offset += buf.length;
}
+ outputBuffer = newBuffer;
+
unprocessedBuffers = [];
+
processInput();
};