more video stream changes

This commit is contained in:
subnub
2024-12-25 01:10:09 -05:00
parent bf0dc36ea6
commit e54161464f
4 changed files with 7 additions and 83 deletions
@@ -385,8 +385,6 @@ class StorageService {
const chunksize = end - start + 1;
const IV = currentFile.metadata.IV;
console.log("chunksize", start, end, chunksize);
const head = {
"Content-Range": "bytes " + start + "-" + end + "/" + fileSize,
"Accept-Ranges": "bytes",
@@ -1,76 +0,0 @@
import { Response, Request } from "express";
const StreamSkip = require("stream-skip");
const awaitStreamVideo = (
start: number,
end: number,
differenceStart: number,
decipher: any,
res: Response,
req: Request,
streamsToErrorCatch: any[],
readStream: any
) => {
return new Promise<void>((resolve, reject) => {
req.on("close", () => {
// readStream.close();
decipher.destroy();
resolve();
});
req.on("end", () => {
streamsToErrorCatch.forEach((stream) => {
stream.destroy();
});
resolve();
});
streamsToErrorCatch.forEach((currentStream) => {
currentStream.on("error", (e: Error) => {
reject({
message: "Await Video Stream Input Error",
code: 500,
error: e,
});
});
});
readStream.on("close", () => {
decipher.destroy();
});
if (+start === 0 && +end === 1) {
// This is for Safari/iOS, for whatever reason they ask for the
// First byte, but if I actually try to return the first byte
// It will not work ???, but if I return the first 4 bytes it seems
// To work fine.
decipher.on("data", (data: Buffer | string) => {
const dataCoverted = data.toString("hex");
let neededData = dataCoverted.substring(0, 8);
const dataBack = Buffer.from(neededData, "hex");
res.write(dataBack);
resolve();
});
} else {
// This is where the differnce start comes into play, as I said before
// There will be an offset caused by the 16 block size of AES256.
// So if there was an offset we need to skip over those bytes
// To make sure it returns the exact position the browser is requesting.
// Most browsers will still work fine without this, such as Chrome and Firefox.
// But one browser needs to be pampered and fed the exact right bytes in
// Order to function correctly, can you guess what browser? If you
// Guessed Safari you would be correct.
const streamSkip = new StreamSkip({ skip: differenceStart });
decipher.pipe(streamSkip).pipe(res);
}
});
};
export default awaitStreamVideo;
@@ -42,7 +42,6 @@ const getFileAndRemoveActiveStream = async (
readStream.destroy();
// @ts-ignore
decipherStream.destroy();
console.log("Destroyed streams");
} catch (e) {
console.log("Error destroying streams", e);
}
@@ -140,16 +139,20 @@ const proccessData = (
let bytesSent = 0;
decipher.on("data", (data: Buffer) => {
if (bytesSent + data.length > range.chunksize) {
if (bytesSent === 0 && range.skip > 0) {
const neededData = data.slice(range.skip, data.length);
res.write(neededData);
bytesSent += neededData.length;
} else if (bytesSent + data.length > range.chunksize) {
const currentDataLength = bytesSent + data.length;
const difference = currentDataLength - range.chunksize;
const neededData = data.slice(0, data.length - difference);
res.write(neededData);
bytesSent += neededData.length;
} else {
res.write(data);
bytesSent += data.length;
}
bytesSent += data.length;
});
decipher.on("finish", () => {
-1
View File
@@ -91,7 +91,6 @@
"redux": "^5.0.1",
"regenerator-runtime": "^0.13.3",
"sharp": "^0.33.4",
"stream-skip": "^1.0.3",
"supertest-session": "^4.1.0",
"sweetalert2": "^11.6.13",
"temp": "^0.9.1",