From 27aff626ae227f60f73701e65b01f5da0f985015 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Sun, 8 May 2022 22:01:07 -0700 Subject: [PATCH] common: rtsp server fixes --- common/src/rtsp-server.ts | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/common/src/rtsp-server.ts b/common/src/rtsp-server.ts index bedb39dbd..cd3281b21 100644 --- a/common/src/rtsp-server.ts +++ b/common/src/rtsp-server.ts @@ -91,25 +91,46 @@ export function createRtspParser(options?: StreamParserOptions): RtspStreamParse ], findSyncFrame(streamChunks: StreamChunk[]) { let foundIndex: number; + let nonVideo: { + [codec: string]: StreamChunk, + } = {}; + + const createSyncFrame = () => { + const ret = streamChunks.slice(foundIndex); + // for (const nv of Object.values(nonVideo)) { + // ret.unshift(nv); + // } + return ret; + } for (let prebufferIndex = 0; prebufferIndex < streamChunks.length; prebufferIndex++) { const streamChunk = streamChunks[prebufferIndex]; + if (streamChunk.type !== 'h264') { + nonVideo[streamChunk.type] = streamChunk; + continue; + } + if (findH264NaluType(streamChunk, H264_NAL_TYPE_SPS)) foundIndex = prebufferIndex; } if (foundIndex !== undefined) - return streamChunks.slice(foundIndex); + return createSyncFrame(); + nonVideo = {}; // some streams don't contain codec info, so find an idr frame instead. for (let prebufferIndex = 0; prebufferIndex < streamChunks.length; prebufferIndex++) { const streamChunk = streamChunks[prebufferIndex]; + if (streamChunk.type !== 'h264') { + nonVideo[streamChunk.type] = streamChunk; + continue; + } if (findH264NaluType(streamChunk, H264_NAL_TYPE_IDR)) foundIndex = prebufferIndex; } if (foundIndex !== undefined) - return streamChunks.slice(foundIndex); + return createSyncFrame(); // oh well! }, @@ -596,7 +617,7 @@ export class RtspServer { control: msection.control, protocol: 'udp', destination: parseInt(rtp), - codec: msection.codec, + codec: msection.codec || (msection.type === 'audio' ? 'pcm' : undefined), } } else if (transport.includes('TCP')) { @@ -608,7 +629,7 @@ export class RtspServer { control: msection.control, protocol: 'tcp', destination: low, - codec: msection.codec, + codec: msection.codec || (msection.type === 'audio' ? 'pcm' : undefined), } } }