chore: clean code + add options

This commit is contained in:
Elysia
2024-07-29 19:01:40 +07:00
parent 772739f980
commit 55df05d4d8
7 changed files with 42 additions and 38 deletions

View File

@@ -29,7 +29,6 @@ class AnnexBDispatcher extends VideoDispatcher {
codecCallback(frame) {
let accessUnit = frame;
const nalus = [];
let offset = 0;
// Extract NALUs from the access unit
@@ -37,25 +36,19 @@ class AnnexBDispatcher extends VideoDispatcher {
const naluSize = accessUnit.readUInt32BE(offset);
offset += 4;
const nalu = accessUnit.subarray(offset, offset + naluSize);
nalus.push(nalu);
offset += naluSize;
}
nalus.forEach((nalu, index) => {
const isLastNal = index === nalus.length - 1;
const isLastNal = offset + naluSize >= accessUnit.length;
if (nalu.length <= this.mtu) {
// If NALU size is within MTU, send it directly
this._playChunk(Buffer.concat([this.createHeaderExtension(), nalu]), index + 1 === nalus.length);
this._playChunk(Buffer.concat([this.createHeaderExtension(), nalu]), isLastNal);
} else {
// If NALU size exceeds MTU, fragment it
const [naluHeader, naluData] = this._nalFunctions.splitHeader(nalu);
const dataFragments = this.partitionVideoData(naluData);
dataFragments.forEach((data, fragmentIndex) => {
for (let fragmentIndex = 0; fragmentIndex < dataFragments.length; fragmentIndex++) {
const data = dataFragments[fragmentIndex];
const isFirstPacket = fragmentIndex === 0;
const isFinalPacket = fragmentIndex === dataFragments.length - 1;
const markerBit = isLastNal && isFinalPacket; // Is last packet ?
this._playChunk(
Buffer.concat([
@@ -63,11 +56,12 @@ class AnnexBDispatcher extends VideoDispatcher {
this.makeFragmentationUnitHeader(isFirstPacket, isFinalPacket, naluHeader),
data,
]),
markerBit,
isLastNal && isFinalPacket,
);
});
}
}
});
offset += naluSize;
}
}
}

View File

@@ -316,12 +316,12 @@ class BaseDispatcher extends Writable {
_createPacket(buffer, isLastPacket = false) {
// Header
const packetBuffer = Buffer.alloc(12);
packetBuffer[0] = (2 << 6) | ((this.extensionEnabled ? 1 : 0) << 4);
packetBuffer[0] = this.extensionEnabled ? 0x90 : 0x80; // 0b10000000 | ((this.extensionEnabled ? 1 : 0) << 4);
packetBuffer[1] = this.payloadType;
if (this.extensionEnabled) {
if (isLastPacket) {
packetBuffer[1] |= 0b10000000;
packetBuffer[1] |= 0x80;
}
}

View File

@@ -25,27 +25,24 @@ class VP8Dispatcher extends VideoDispatcher {
super(player, highWaterMark, streams, fps);
}
makeChunk(buffer, i) {
makeChunk(buffer, isFirstFrame) {
// Make frame
const headerExtensionBuf = this.createHeaderExtension();
// Vp8 payload descriptor
const payloadDescriptorBuf = Buffer.alloc(2);
payloadDescriptorBuf[0] = 0x80;
payloadDescriptorBuf[0] = isFirstFrame ? 0x90 : 0x80; // Mark S bit, indicates start of frame: payloadDescriptorBuf[0] |= 0b00010000;
payloadDescriptorBuf[1] = 0x80;
if (i == 0) {
payloadDescriptorBuf[0] |= 0b00010000; // Mark S bit, indicates start of frame
}
// Vp8 pictureid payload extension
const pictureIdBuf = Buffer.alloc(2);
pictureIdBuf.writeUIntBE(this.count, 0, 2);
pictureIdBuf[0] |= 0b10000000;
pictureIdBuf.writeUintBE(this.count, 0, 2);
pictureIdBuf[0] |= 0x80;
return Buffer.concat([headerExtensionBuf, payloadDescriptorBuf, pictureIdBuf, buffer]);
}
codecCallback(chunk) {
const chunkSplit = this.partitionVideoData(chunk);
for (let i = 0; i < chunkSplit.length; i++) {
this._playChunk(this.makeChunk(chunkSplit[i], i), i + 1 === chunkSplit.length);
this._playChunk(this.makeChunk(chunkSplit[i], i == 0), i + 1 === chunkSplit.length);
}
}
}