From cfea14e8e5172aef532bc9da3074e9e72bb84cab Mon Sep 17 00:00:00 2001 From: Alessandro Ros Date: Wed, 17 Apr 2024 23:41:45 +0200 Subject: [PATCH] webrtc: detect stereo tracks published with the web page (#2902) (#3263) --- internal/servers/webrtc/publish_index.html | 42 ++++++++++++++-------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/internal/servers/webrtc/publish_index.html b/internal/servers/webrtc/publish_index.html index 63537ee2af2..8298117502d 100644 --- a/internal/servers/webrtc/publish_index.html +++ b/internal/servers/webrtc/publish_index.html @@ -307,9 +307,13 @@ } const lines3 = []; + let firstLine = true; for (const line of lines2) { - if (line.startsWith('a=fmtp:')) { + if (firstLine) { + firstLine = false; + lines3.push(line.split(' ').slice(0, 3).concat(payloadFormats).join(' ')); + } else if (line.startsWith('a=fmtp:')) { if (payloadFormats.includes(line.slice('a=fmtp:'.length).split(' ')[0])) { lines3.push(line); } @@ -368,15 +372,28 @@ return lines.join('\r\n'); }; -const editAnswer = (sdp, videoCodec, audioCodec, videoBitrate, audioBitrate, audioVoice) => { +const editOffer = (sdp) => { const sections = sdp.split('m='); for (let i = 0; i < sections.length; i++) { const section = sections[i]; if (section.startsWith('video')) { - sections[i] = setVideoBitrate(setCodec(section, videoCodec), videoBitrate); + sections[i] = setCodec(section, videoForm.codec.value); } else if (section.startsWith('audio')) { - sections[i] = setAudioBitrate(setCodec(section, audioCodec), audioBitrate, audioVoice); + sections[i] = setAudioBitrate(setCodec(section, audioForm.codec.value), audioForm.bitrate.value, audioForm.voice.checked); + } + } + + return sections.join('m='); +}; + +const editAnswer = (sdp) => { + const sections = sdp.split('m='); + + for (let i = 0; i < sections.length; i++) { + const section = sections[i]; + if (section.startsWith('video')) { + sections[i] = setVideoBitrate(section, videoForm.bitrate.value); } } @@ -421,14 +438,7 @@ return; } - sdp = editAnswer( - sdp, - videoForm.codec.value, - audioForm.codec.value, - videoForm.bitrate.value, - audioForm.bitrate.value, - audioForm.voice.checked, - ); + sdp = editAnswer(sdp); pc.setRemoteDescription(new RTCSessionDescription({ type: 'answer', @@ -442,12 +452,14 @@ }; const sendOffer = (offer) => { + offer = editOffer(offer); + fetch(new URL('whip', window.location.href) + window.location.search, { method: 'POST', headers: { 'Content-Type': 'application/sdp', }, - body: offer.sdp, + body: offer, }) .then((res) => { if (res.status !== 201) { @@ -456,7 +468,7 @@ sessionUrl = new URL(res.headers.get('location'), window.location.href).toString(); return res.text(); }) - .then((sdp) => onRemoteAnswer(sdp)) + .then((answer) => onRemoteAnswer(answer)) .catch((err) => { onError(err.toString(), true); }); @@ -467,7 +479,7 @@ .then((offer) => { offerData = parseOffer(offer.sdp); pc.setLocalDescription(offer); - sendOffer(offer); + sendOffer(offer.sdp); }); };