Skip to content

Commit

Permalink
Fix safari crash on iOS 15.1
Browse files Browse the repository at this point in the history
safari crashes when h264 is used while negotiation.
  • Loading branch information
SangwonOh committed Oct 29, 2021
1 parent 10b6d77 commit 2d64b13
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion src/OvenLiveKit.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,52 @@ function findIp(string) {
return result;
}

function checkIOSVersion() {
var agent = window.navigator.userAgent,
start = agent.indexOf('OS ');
if ((agent.indexOf('iPhone') > -1 || agent.indexOf('iPad') > -1) && start > -1) {
return window.Number(agent.substr(start + 3, 3).replace('_', '.'));
}
return 0;
}

function getFormatNumber(sdp, format) {

const lines = sdp.split('\n');
let formatNumber = -1;

for (let i = 0; i < lines.length - 1; i++) {

lines[i] = lines[i].toLowerCase();

if (lines[i].indexOf('a=rtpmap') > -1 && lines[i].indexOf(format.toLowerCase()) > -1) {
// parsing "a=rtpmap:100 H264/90000" line
formatNumber = lines[i].split(' ')[0].split(':')[1];
break;
}
}

return formatNumber;
}

function removeFormat(sdp, formatNumber) {
let newLines = [];
let lines = sdp.split('\n');

for (let i = 0; i < lines.length; i++) {

if (lines[i].indexOf('m=video') === 0) {
newLines.push(lines[i].replace(' ' + formatNumber + '', ''));
} else if (lines[i].indexOf(formatNumber + '') > -1) {

} else {
newLines.push(lines[i]);
}
}

return newLines.join('\n')
}

async function getStreamForDeviceCheck() {

// High resolution video constraints makes browser to get maximum resolution of video device.
Expand Down Expand Up @@ -486,6 +532,15 @@ function addMethod(instance) {
peerConnection.addTrack(track, instance.stream);
});


if (checkIOSVersion() >= 15) {
const formatNumber = getFormatNumber(offer.sdp, 'H264');

if (formatNumber > 0) {
offer.sdp = removeFormat(offer.sdp, formatNumber);
}
}

if (instance.connectionConfig.maxVideoBitrate) {

// if bandwith limit is set. modify sdp from ome to limit acceptable bandwidth of ome
Expand All @@ -503,6 +558,16 @@ function addMethod(instance) {
peerConnection.createAnswer()
.then(function (answer) {

if (checkIOSVersion() >= 15) {

const formatNumber = getFormatNumber(answer.sdp, 'H264');

if (formatNumber > 0) {

answer.sdp = removeFormat(answer.sdp, formatNumber);
}
}

if (instance.connectionConfig.sdp && instance.connectionConfig.sdp.appendFmtp) {

answer.sdp = appendFmtp(answer.sdp);
Expand Down Expand Up @@ -687,7 +752,7 @@ function addMethod(instance) {
// static methods
OvenLiveKit.create = function (options) {

console.info(logEventHeader, 'Create WebRTC Input v1.0.1');
console.info(logEventHeader, 'Create WebRTC Input v1.0.2');

let instance = {};

Expand Down

0 comments on commit 2d64b13

Please sign in to comment.