webrtc: prevent payload type conflict in read page (#4456)

This commit is contained in:
Alessandro Ros
2025-04-21 15:24:01 +02:00
committed by GitHub
parent 0c501e2507
commit 77c77c7f2f
+12 -1
View File
@@ -53,16 +53,27 @@ class MediaMTXWebRTCReader {
static #supportsNonAdvertisedCodec(codec, fmtp) {
return new Promise((resolve) => {
const payloadType = 118; // TODO: dynamic
const pc = new RTCPeerConnection({ iceServers: [] });
const mediaType = 'audio';
let payloadType = '';
pc.addTransceiver(mediaType, { direction: 'recvonly' });
pc.createOffer()
.then((offer) => {
if (offer.sdp === undefined) {
throw new Error('SDP not present');
}
if (offer.sdp.includes(` ${codec}`)) { // codec is advertised, there's no need to add it manually
throw new Error('already present');
}
const sections = offer.sdp.split(`m=${mediaType}`);
const payloadTypes = sections.slice(1)
.map((s) => s.split('\r\n')[0].split(' ').slice(3))
.reduce((prev, cur) => [...prev, ...cur], []);
payloadType = this.#reservePayloadType(payloadTypes);
const lines = sections[1].split('\r\n');
lines[0] += ` ${payloadType}`;
lines.splice(lines.length - 1, 0, `a=rtpmap:${payloadType} ${codec}`);