mirror of
https://github.com/koush/scrypted.git
synced 2026-05-26 06:30:29 +01:00
@@ -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
|
||||
// ];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +58,6 @@ addSupportedType({
|
||||
audioReturn: dgram.Socket;
|
||||
demuxer?: RtpDemuxer;
|
||||
rtpSink?: HomeKitRtpSink;
|
||||
mtu: number;
|
||||
}
|
||||
const sessions = new Map<string, Session>();
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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')}`,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user