rebroadcast: fix bug where packet length was parsed as signed integer.

This commit is contained in:
Koushik Dutta
2022-03-02 10:33:33 -08:00
parent 2782d8cc4d
commit e217b315c2
6 changed files with 36 additions and 9 deletions

View File

@@ -109,7 +109,13 @@ export class RtspClient extends RtspBase {
async request(method: string, headers?: Headers, path?: string, body?: Buffer) {
headers = headers || {};
const line = `${method} ${this.url}${path || ''} RTSP/1.0`;
let fullUrl: string;
if (path)
fullUrl = new URL(path, this.url).toString();
else
fullUrl = this.url;
const line = `${method} ${fullUrl} RTSP/1.0`;
headers['CSeq'] = (this.cseq++).toString();
this.write(line, headers, body);

View File

@@ -15,3 +15,22 @@ export function parsePayloadTypes(sdp: string) {
videoPayloadTypes,
}
}
function getTrackId(track: string) {
if (!track)
return;
const lines = track.split('\n').map(line => line.trim());
const control = lines.find(line => line.startsWith('a=control:'));
return control?.split('a=control:')?.[1];
}
export function parseTrackIds(sdp: string) {
const tracks = sdp.split('m=');
const audioTrack = tracks.find(track => track.startsWith('audio'));
const videoTrack = tracks.find(track => track.startsWith('video'));
return {
audio: getTrackId(audioTrack),
video: getTrackId(videoTrack),
};
}

View File

@@ -1,12 +1,12 @@
{
"name": "@scrypted/prebuffer-mixin",
"version": "0.1.170",
"version": "0.1.171",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@scrypted/prebuffer-mixin",
"version": "0.1.170",
"version": "0.1.171",
"license": "Apache-2.0",
"dependencies": {
"@scrypted/common": "file:../../common",

View File

@@ -1,6 +1,6 @@
{
"name": "@scrypted/prebuffer-mixin",
"version": "0.1.170",
"version": "0.1.171",
"description": "Rebroadcast and Prebuffer for VideoCameras.",
"author": "Scrypted",
"license": "Apache-2.0",

View File

@@ -7,7 +7,7 @@ import { handleRebroadcasterClient, ParserOptions, ParserSession, startParserSes
import { createMpegTsParser, createFragmentedMp4Parser, StreamChunk, StreamParser } from '@scrypted/common/src/stream-parser';
import { AutoenableMixinProvider } from '@scrypted/common/src/autoenable-mixin-provider';
import { listenZeroSingleClient } from '@scrypted/common/src/listen-cluster';
import { parsePayloadTypes } from '@scrypted/common/src/sdp-utils';
import { parsePayloadTypes, parseTrackIds } from '@scrypted/common/src/sdp-utils';
import { createRtspParser, RtspClient, RtspServer } from '@scrypted/common/src/rtsp-server';
import { Duplex } from 'stream';
import net from 'net';
@@ -446,8 +446,10 @@ class PrebufferSession {
const sdpResponse = await rtspClient.describe();
const sdp = sdpResponse.body.toString().trim();
this.sdp = Promise.resolve(sdp);
await rtspClient.setup(0, '/audio');
await rtspClient.setup(2, '/video');
const { audio, video } = parseTrackIds(sdp);
// handle no audio?
await rtspClient.setup(0, audio);
await rtspClient.setup(2, video);
const socket = await rtspClient.play();
session = await startRFC4571Parser(socket, sdp, ffmpegInput.mediaStreamOptions, true, rbo);
}

View File

@@ -70,11 +70,11 @@ export async function startRFC4571Parser(socket: net.Socket, sdp: string, mediaS
let length: number;
if (hasRstpPrefix) {
header = await readLength(socket, 4);
length = header.readInt16BE(2);
length = header.readUInt16BE(2);
}
else {
header = await readLength(socket, 2);
length = header.readInt16BE(0);
length = header.readUInt16BE(0);
}
const data = await readLength(socket, length);