From 68c8bde3d8f8e97f917ca538a9237d58d1cd992d Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Fri, 31 Dec 2021 23:19:09 -0800 Subject: [PATCH] Revert "plugins: experimental" This reverts commit 2301ff2fc0b6c8529fd8c39642627f5ab6488f60. --- common/src/stream-parser.ts | 103 +++++++--------------------- plugins/homekit/src/types/camera.ts | 6 +- plugins/prebuffer-mixin/src/main.ts | 32 ++++----- 3 files changed, 42 insertions(+), 99 deletions(-) diff --git a/common/src/stream-parser.ts b/common/src/stream-parser.ts index 07d4df476..86a660a58 100644 --- a/common/src/stream-parser.ts +++ b/common/src/stream-parser.ts @@ -92,11 +92,6 @@ export function createPCMParser(): StreamParser { export function createMpegTsParser(options?: StreamParserOptions): StreamParser { let pat: Buffer; let pmt: Buffer; - let audioCodecInfo: Buffer; - let videoCodecInfo: Buffer; - let seventeen: Buffer; - let startStream: Buffer[] = []; - return { container: 'mpegts', outputArguments: [ @@ -109,54 +104,21 @@ export function createMpegTsParser(options?: StreamParserOptions): StreamParser throw new Error('Invalid sync byte in mpeg-ts packet. Terminating stream.') } - // if (pat && pmt && audioCodecInfo && seventeen && videoCodecInfo) - // return; + if (pat && pmt) + return; - // let offset = 0; - // while (offset + 188 < concat.length) { - // const pkt = concat.subarray(offset, offset + 188); - // const pid = ((concat[1] & 0x1F) << 8) | concat[2]; - // if (!pat && pid === 0) { - // pat = pkt; - // startStream.push(pkt); - // } - // else if (!videoCodecInfo && pid === 256) { - // videoCodecInfo = pkt; - // startStream.push(pkt); - // } - // else if (!audioCodecInfo && pid === 257) { - // audioCodecInfo = pkt; - // startStream.push(pkt); - // } - // else if (!pmt && pid === 0x1000) { - // pmt = pkt; - // startStream.push(pkt); - // } - // else if (!seventeen && pid === 17) { - // seventeen = pkt; - // startStream.push(pkt); - // } - - // switch (pid) { - // case 0: - // case 17: - // case 0x1000: - // case 256: - // case 257: - // break; - // default: - // console.log('what pid', pid); - // } - - - // offset += 188; - // } + const pid = ((concat[1] & 0x1F) << 8) | concat[2]; + if (pid === 0) { + const tableId = concat[5]; + if (tableId === 0) { + pat = concat.slice(0, 188); + } + else if (tableId === 2) { + pmt = concat.slice(0, 188); + } + } }), findSyncFrame(streamChunks): StreamChunk[] { - // pmt pid: 0x1000 - // pat pid: 0x0000 - // 17 aka 0x11: unknown - for (let prebufferIndex = 0; prebufferIndex < streamChunks.length; prebufferIndex++) { const streamChunk = streamChunks[prebufferIndex]; @@ -172,34 +134,21 @@ export function createMpegTsParser(options?: StreamParserOptions): StreamParser if ((pkt[3] & 0x20) && (pkt[4] > 0)) { // have AF if (pkt[5] & 0x40) { - console.log('found sync', pid); - if (true) { - // we found the sync frame, but also need to send the pat and pmt - // which might be at the start of this chunk before the keyframe. - // yolo! - const ret: StreamChunk = { - chunks: [], - startStream: startStream.length ? Buffer.concat(startStream) : undefined, - }; - const remainingChunks = streamChunks.slice(prebufferIndex); - return [ - ret, - ...remainingChunks, - ] - } + // we found the sync frame, but also need to send the pat and pmt + // which might be at the start of this chunk before the keyframe. + // yolo! + return streamChunks.slice(prebufferIndex); + // const chunks = streamChunk.chunks.slice(chunkIndex + 1); + // const take = chunk.subarray(offset); + // chunks.unshift(take); - const chunks = streamChunk.chunks.slice(chunkIndex + 1); - const take = chunk.subarray(offset); - chunks.unshift(take); - - const remainingChunks = streamChunks.slice(prebufferIndex + 1); - const ret = Object.assign({}, streamChunk); - ret.chunks = chunks; - ret.startStream = startStream.length ? Buffer.concat(startStream) : undefined; - return [ - ret, - ...remainingChunks - ]; + // const remainingChunks = streamChunks.slice(prebufferIndex + 1); + // const ret = Object.assign({}, streamChunk); + // ret.chunks = chunks; + // return [ + // ret, + // ...remainingChunks + // ]; } } } diff --git a/plugins/homekit/src/types/camera.ts b/plugins/homekit/src/types/camera.ts index 84f52c1af..cc8bed312 100644 --- a/plugins/homekit/src/types/camera.ts +++ b/plugins/homekit/src/types/camera.ts @@ -58,7 +58,6 @@ addSupportedType({ audioReturn: dgram.Socket; demuxer?: RtpDemuxer; rtpSink?: HomeKitRtpSink; - mtu: number; } const sessions = new Map(); @@ -99,7 +98,6 @@ addSupportedType({ cp: null, videoReturn, audioReturn, - mtu: socketType === 'udp4' ? 1378 : 1228, } sessions.set(request.sessionID, session); @@ -223,8 +221,8 @@ addSupportedType({ }, 60000)); - const videomtu = 188 * 3;//request.video.mtu || session.mtu; - const audiomtu = 188;//request.video.mtu || session.mtu; + const videomtu = 188 * 3; + const audiomtu = 188 * 1; try { console.log('fetching video stream'); diff --git a/plugins/prebuffer-mixin/src/main.ts b/plugins/prebuffer-mixin/src/main.ts index 5f8e4bb28..c88649100 100644 --- a/plugins/prebuffer-mixin/src/main.ts +++ b/plugins/prebuffer-mixin/src/main.ts @@ -376,23 +376,19 @@ class PrebufferSession { this.events.on(eventName, safeWriteData); session.events.once('killed', cleanup); - if (true) { - for (const prebuffer of prebufferContainer) { - if (prebuffer.time < now - requestedPrebuffer) - continue; - - safeWriteData(prebuffer.chunk); - } - } - else { - // for some reason this doesn't work as well as simply guessing and dumping. - const parser = this.parsers[container]; - const availablePrebuffers = parser.findSyncFrame(prebufferContainer.filter(pb => pb.time >= now - requestedPrebuffer).map(pb => pb.chunk)); - for (const prebuffer of availablePrebuffers) { - safeWriteData(prebuffer); - } - } + // for (const prebuffer of prebufferContainer) { + // if (prebuffer.time < now - requestedPrebuffer) + // continue; + // safeWriteData(prebuffer.chunk); + // } + + // for some reason this doesn't work as well as simply guessing and dumping. + const parser = this.parsers[container]; + const availablePrebuffers = parser.findSyncFrame(prebufferContainer.filter(pb => pb.time >= now - requestedPrebuffer).map(pb => pb.chunk)); + for (const prebuffer of availablePrebuffers) { + safeWriteData(prebuffer); + } return cleanup; } }) @@ -440,7 +436,7 @@ class PrebufferSession { url, container, inputArguments: [ - '-analyzeduration', '10000000',// '-probesize', '500000', + // '-analyzeduration', '0', '-probesize', '500000', '-f', container, '-i', url, ], @@ -449,7 +445,7 @@ class PrebufferSession { if (pcmAudio) { ffmpegInput.inputArguments.push( - '-analyzeduration', '10000000',// '-probesize', '500000', + // '-analyzeduration', '0', '-probesize', '500000', '-f', 's16le', '-i', `tcp://127.0.0.1:${await createContainerServer('s16le')}`, )