Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
fpv-wtf committed Nov 15, 2023
1 parent 082d23f commit 2239d20
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 57 deletions.
7 changes: 0 additions & 7 deletions src/osd-overlay/.eslintrc

This file was deleted.

4 changes: 1 addition & 3 deletions src/osd-overlay/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ export default class VideoWorkerManager {
}

case VideoWorkerShared.MessageType.PROGRESS_INIT: {
this.callbacks?.onProgressInit({
expectedFrames: message.expectedFrames,
});
this.callbacks?.onProgressInit({ expectedFrames: message.expectedFrames });
break;
}

Expand Down
5 changes: 4 additions & 1 deletion src/osd-overlay/mp4/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import {
} from "./types";
import { getMp4Time } from "./utils";
import { parseBox } from "./parsers";
import { writeBox, MdatBoxStreamWriter } from "./writers";
import {
writeBox,
MdatBoxStreamWriter,
} from "./writers";

export class MP4Parser {
private readonly stream: FileStreamReader;
Expand Down
2 changes: 1 addition & 1 deletion src/osd-overlay/mp4/io/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class FileStreamWriter {

async writeNextBytes(bytes: BufferSource | Blob): Promise<void> {
const stream = await this.getStream();
await stream.write(bytes)
await stream.write(bytes);

const byteLength = bytes instanceof Blob ? bytes.size : bytes.byteLength;
this._offset += byteLength;
Expand Down
2 changes: 1 addition & 1 deletion src/osd-overlay/mp4/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ class StblBoxParser extends SimpleBoxParser<StblBox> {
stss: childBoxes.stss![0] as StssBox,
stsz: childBoxes.stsz![0] as StszBox,
stts: childBoxes.stts![0] as SttsBox,
ctts: childBoxes.ctts ? childBoxes.ctts![0] as CttsBox : undefined
ctts: childBoxes.ctts ? childBoxes.ctts![0] as CttsBox : undefined,
};
}
}
Expand Down
24 changes: 13 additions & 11 deletions src/osd-overlay/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
import { StreamDataView } from "stream-data-view";

import VideoWorkerShared from "./shared";
import { MP4Parser, MP4Writer } from "./mp4";
import { Avc1Box, AvcCBox } from "./mp4/types";
import {
MP4Parser,
MP4Writer,
} from "./mp4";
import {
Avc1Box,
AvcCBox,
} from "./mp4/types";

const PROGRESS_UPDATE_INTERVAL = 100;

Expand Down Expand Up @@ -176,9 +182,7 @@ export class Processor {
this.expectedFrames = this.inMp4!.moov!.trak[0].mdia.minf.stbl.stsz.sampleCount;
this.decodedFrames = {};

this.progressInit({
expectedFrames: this.expectedFrames,
});
this.progressInit({ expectedFrames: this.expectedFrames });

this.progressUpdateIntervalHandle = self.setInterval(this.sendProgressUpdate, PROGRESS_UPDATE_INTERVAL);

Expand Down Expand Up @@ -245,7 +249,7 @@ export class Processor {
index: chunk.index,
image: undefined,
sync: chunk.sync,
}
};
}

// Enqueue samples for decoding.
Expand Down Expand Up @@ -286,9 +290,7 @@ export class Processor {
// Send first frame as preview. This needs to happen after constructing the frame otherwise
// it complains that "the image source is detached" which is completely ungooglable.
if (index === 0) {
this.progressUpdate({
preview: modifiedFrame
})
this.progressUpdate({ preview: modifiedFrame });
}

this.encoder!.encode(frame, { keyFrame: entry.sync });
Expand All @@ -304,7 +306,7 @@ export class Processor {
this.outMp4!.writeSample(frame.data, frame.sync);
}

lastSampleIndex += sampleChunks.length
lastSampleIndex += sampleChunks.length;
}

await this.outMp4!.close();
Expand Down Expand Up @@ -337,7 +339,7 @@ export class Processor {
}

const newPosition = i + ctts.sampleOffsets[j - 1] / sampleDelta - initialOffset;
orderedFrames[newPosition] = Object.assign({}, this.decodedFrames[lastSampleIndex + i], {index: lastSampleIndex + newPosition});
orderedFrames[newPosition] = Object.assign({}, this.decodedFrames[lastSampleIndex + i], { index: lastSampleIndex + newPosition });
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/osd-overlay/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ namespace VideoWorkerShared {
| ProgressUpdateMessage
| StartMessage;



export class DecoderError extends Error {
constructor(message: string) {
super(message);
Expand Down
40 changes: 20 additions & 20 deletions src/osd-overlay/srt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,39 @@ export class SrtReader {
readonly frames: SrtFrame[] = [];

constructor(data: String) {
const blocks = data.split('\n\n').slice(0, -1);
const blocks = data.split("\n\n").slice(0, -1);

for (const block of blocks) {
const [ , timestamps, text ] = block.split('\n');
const [ , timestamps, text ] = block.split("\n");
const [ startHours, startMinutes, startSeconds, startMilliseconds ] = timestamps
.substring(0, 12)
.split(/\D/)
.map(it => parseInt(it));
.substring(0, 12)
.split(/\D/)
.map((it) => parseInt(it));
const [ endHours, endMinutes, endSeconds, endMilliseconds ] = timestamps
.substring(17)
.split(/\D/)
.map(it => parseInt(it));
.substring(17)
.split(/\D/)
.map((it) => parseInt(it));

const start = startHours * 1000 * 60 * 60 + startMinutes * 1000 * 60 + startSeconds * 1000 + startMilliseconds;
const end = endHours * 1000 * 60 * 60 + endMinutes * 1000 * 60 + endSeconds * 1000 + endMilliseconds;

const [ signalRaw, chRaw, flightTimeRaw, uavBatRaw, glsBatRaw, uavBatCellsRaw, glsBatCellsRaw, delayRaw, bitrateRaw, rcSignalRaw ] = text.split(' ');
const signal = signalRaw.split(':')[1];
const ch = chRaw.split(':')[1];
const flightTime = parseInt(flightTimeRaw.split(':')[1]);
const uavBat = uavBatRaw.split(':')[1];
const glsBat = glsBatRaw.split(':')[1];
const uavBatCells = uavBatCellsRaw.split(':')[1];
const glsBatCells = glsBatCellsRaw.split(':')[1];
const delay = delayRaw.split(':')[1];
const bitrate = bitrateRaw.split(':')[1];
const rcSignal = rcSignalRaw.split(':')[1];
const [ signalRaw, chRaw, flightTimeRaw, uavBatRaw, glsBatRaw, uavBatCellsRaw, glsBatCellsRaw, delayRaw, bitrateRaw, rcSignalRaw ] = text.split(" ");
const signal = signalRaw.split(":")[1];
const ch = chRaw.split(":")[1];
const flightTime = parseInt(flightTimeRaw.split(":")[1]);
const uavBat = uavBatRaw.split(":")[1];
const glsBat = glsBatRaw.split(":")[1];
const uavBatCells = uavBatCellsRaw.split(":")[1];
const glsBatCells = glsBatCellsRaw.split(":")[1];
const delay = delayRaw.split(":")[1];
const bitrate = bitrateRaw.split(":")[1];
const rcSignal = rcSignalRaw.split(":")[1];

const formatFlightTime = (totalSeconds: number) => {
const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, "0");
const seconds = String(totalSeconds % 60).padStart(2, "0");
return `${minutes}' ${seconds}"`;
}
};

this.frames.push({
start,
Expand Down
23 changes: 12 additions & 11 deletions src/osd-overlay/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export class VideoWorker {

this.fontPack = await Font.fromFiles(options.fontFiles);

const { width, height } = await this.processor.open(options.videoFile, options.outHandle);
const {
width,
height,
} = await this.processor.open(options.videoFile, options.outHandle);

if (width === 1280 && height === 720) {
this.wide = true;
Expand Down Expand Up @@ -109,9 +112,7 @@ export class VideoWorker {
height: outHeight,
});

this.postMessage({
type: VideoWorkerShared.MessageType.COMPLETE,
});
this.postMessage({ type: VideoWorkerShared.MessageType.COMPLETE });
} catch (e: any) {
this.postMessage({
type: VideoWorkerShared.MessageType.ERROR,
Expand All @@ -127,7 +128,7 @@ export class VideoWorker {
const osdCanvas = this.osdCanvas!;
const osdCtx = this.osdCtx!;

frameCtx.fillStyle = this.chromaKey ? this.chromaKeyColor : 'black';
frameCtx.fillStyle = this.chromaKey ? this.chromaKeyColor : "black";
frameCtx.fillRect(0, 0, frameCanvas.width, frameCanvas.height);
osdCtx.clearRect(0, 0, osdCanvas.width, osdCanvas.height);

Expand All @@ -152,17 +153,17 @@ export class VideoWorker {

if (this.srtReader) {
// If a srt file is supplied, render the DJI goggle osd elements
const drawText = (osdCtx: OffscreenCanvasRenderingContext2D, text: string, x: number, y: number, bigFont= false) => {
osdCtx.font = `${bigFont ? '30px' : '26px'} calibri`;
osdCtx.strokeStyle = '#333333';
const drawText = (osdCtx: OffscreenCanvasRenderingContext2D, text: string, x: number, y: number, bigFont = false) => {
osdCtx.font = `${bigFont ? "30px" : "26px"} calibri`;
osdCtx.strokeStyle = "#333333";
osdCtx.lineWidth = 4;
osdCtx.strokeText(text, x, y);
osdCtx.fillStyle = 'white';
osdCtx.fillStyle = "white";
osdCtx.fillText(text, x, y);
}
};

const currentFrameInMilliseconds = frameIndex * 1000 / 60;
let srtFrame = this.srtReader.frames.find(it => it.start <= currentFrameInMilliseconds && it.end > currentFrameInMilliseconds);
let srtFrame = this.srtReader.frames.find((it) => it.start <= currentFrameInMilliseconds && it.end > currentFrameInMilliseconds);

if (currentFrameInMilliseconds < this.srtReader.frames[0].start) {
// DJI subtitles don't start at 0 milliseconds
Expand Down

0 comments on commit 2239d20

Please sign in to comment.