Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webrtc: fix reading Opus stereo tracks with Chrome (#2043) #2470

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/core/webrtc_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ var audioCodecs = []webrtc.RTPCodecParameters{
MimeType: webrtc.MimeTypeOpus,
ClockRate: 48000,
Channels: 2,
SDPFmtpLine: "minptime=10;useinbandfec=1",
SDPFmtpLine: "minptime=10;useinbandfec=1;stereo=1;sprop-stereo=1",
},
PayloadType: 111,
},
Expand Down
25 changes: 11 additions & 14 deletions internal/core/webrtc_publish_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@
};

const editAnswer = (answer, videoCodec, audioCodec, videoBitrate, audioBitrate, audioVoice) => {
const sections = answer.split('m=');
const sections = answer.sdp.split('m=');

for (let i = 0; i < sections.length; i++) {
const section = sections[i];
Expand All @@ -287,7 +287,7 @@
}
}

return sections.join('m=');
answer.sdp = sections.join('m=');
};

class Transmitter {
Expand Down Expand Up @@ -377,19 +377,16 @@
return;
}

answer = new RTCSessionDescription({
type: 'answer',
sdp: editAnswer(
answer.sdp,
document.getElementById('video_codec').value,
document.getElementById('audio_codec').value,
document.getElementById('video_bitrate').value,
document.getElementById('audio_bitrate').value,
document.getElementById('audio_voice').value,
),
});
editAnswer(
answer,
document.getElementById('video_codec').value,
document.getElementById('audio_codec').value,
document.getElementById('video_bitrate').value,
document.getElementById('audio_bitrate').value,
document.getElementById('audio_voice').value,
);

this.pc.setRemoteDescription(new RTCSessionDescription(answer));
this.pc.setRemoteDescription(answer);

if (this.queuedCandidates.length !== 0) {
this.sendLocalCandidates(this.queuedCandidates);
Expand Down
46 changes: 45 additions & 1 deletion internal/core/webrtc_read_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,48 @@
return ret;
};

const enableStereoOpus = (section) => {
let opusPayloadFormat = '';
let lines = section.split('\r\n');

for (let i = 0; i < lines.length; i++) {
if (lines[i].startsWith('a=rtpmap:') && lines[i].toLowerCase().includes('opus/')) {
opusPayloadFormat = lines[i].slice('a=rtpmap:'.length).split(' ')[0];
break;
}
}

if (opusPayloadFormat === '') {
return section;
}

for (let i = 0; i < lines.length; i++) {
if (lines[i].startsWith('a=fmtp:' + opusPayloadFormat + ' ')) {
if (!lines[i].includes('stereo')) {
lines[i] += ';stereo=1';
}
if (!lines[i].includes('sprop-stereo')) {
lines[i] += ';sprop-stereo=1';
}
}
}

return lines.join('\r\n');
};

const editOffer = (offer) => {
const sections = offer.sdp.split('m=');

for (let i = 0; i < sections.length; i++) {
const section = sections[i];
if (section.startsWith('audio')) {
sections[i] = enableStereoOpus(section);
}
}

offer.sdp = sections.join('m=');
};

const generateSdpFragment = (offerData, candidates) => {
const candidatesByMedia = {};
for (const candidate of candidates) {
Expand Down Expand Up @@ -139,6 +181,8 @@
}

onLocalOffer(offer) {
editOffer(offer);

this.offerData = parseOffer(offer.sdp);
this.pc.setLocalDescription(offer);

Expand Down Expand Up @@ -186,7 +230,7 @@
return;
}

this.pc.setRemoteDescription(new RTCSessionDescription(answer));
this.pc.setRemoteDescription(answer);

if (this.queuedCandidates.length !== 0) {
this.sendLocalCandidates(this.queuedCandidates);
Expand Down
Loading