Add Kiwi Browser support (#11)

* Catch exception when trying to force desktop codec profile on Kiwi Browser

* Improve setCodecPreferences(), avoid crashing

* Improve codec status detection

* Fix codec detection
This commit is contained in:
redphx 2023-07-21 08:57:05 +07:00 committed by GitHub
parent 0c38b54c38
commit e7980c186d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -83,7 +83,7 @@ class Preferences {
{ {
'id': Preferences.USE_DESKTOP_CODEC, 'id': Preferences.USE_DESKTOP_CODEC,
'label': 'Force high quality codec', 'label': 'Force high quality codec (if possible)',
'default': false, 'default': false,
}, },
@ -1021,19 +1021,37 @@ function patchRtcCodecs() {
RTCRtpTransceiver.prototype.orgSetCodecPreferences = RTCRtpTransceiver.prototype.setCodecPreferences; RTCRtpTransceiver.prototype.orgSetCodecPreferences = RTCRtpTransceiver.prototype.setCodecPreferences;
RTCRtpTransceiver.prototype.setCodecPreferences = function(codecs) { RTCRtpTransceiver.prototype.setCodecPreferences = function(codecs) {
// Use the same codecs as desktop // Use the same codecs as desktop
codecs = [ let profileSetting;
{ for (let codec of codecs) {
'clockRate': 90000, if (codec.sdpFmtpLine.includes('profile-level-id=4d') || codec.sdpFmtpLine.includes('profile-level-id=42')) {
'mimeType': 'video/H264', profileSetting = codec.sdpFmtpLine.slice(-4); // get the last 4 characters
'sdpFmtpLine': 'level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=4d001f', break;
},
{
'clockRate': 90000,
'mimeType': 'video/H264',
'sdpFmtpLine': 'level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=4d001f',
} }
].concat(codecs); }
this.orgSetCodecPreferences(codecs);
let newCodecs;
if (profileSetting) {
newCodecs = [
{
'clockRate': 90000,
'mimeType': 'video/H264',
'sdpFmtpLine': 'level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=4d' + profileSetting,
},
{
'clockRate': 90000,
'mimeType': 'video/H264',
'sdpFmtpLine': 'level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=4d' + profileSetting,
}
].concat(codecs);
} else {
newCodecs = codecs;
}
try {
this.orgSetCodecPreferences(newCodecs);
} catch (e) {
this.orgSetCodecPreferences(codecs);
}
} }
} }
@ -1275,10 +1293,30 @@ if (document.readyState === 'complete' && !onLoadTriggered) {
RTCPeerConnection.prototype.orgSetRemoteDescription = RTCPeerConnection.prototype.setRemoteDescription; RTCPeerConnection.prototype.orgSetRemoteDescription = RTCPeerConnection.prototype.setRemoteDescription;
RTCPeerConnection.prototype.setRemoteDescription = function(...args) { RTCPeerConnection.prototype.setRemoteDescription = function(...args) {
StreamStatus.hqCodec = false;
const sdpDesc = args[0]; const sdpDesc = args[0];
if (sdpDesc.sdp) { if (sdpDesc.sdp) {
StreamStatus.hqCodec = sdpDesc.sdp.includes('profile-level-id=4d'); const sdp = sdpDesc.sdp;
let lineIndex = 0;
let endPos = 0;
let line;
while (lineIndex > -1) {
lineIndex = sdp.indexOf('a=fmtp:', endPos);
if (lineIndex === -1) {
break;
}
endPos = sdp.indexOf('\n', lineIndex);
line = sdp.substring(lineIndex, endPos);
if (line.includes('profile-level-id')) {
StreamStatus.hqCodec = line.includes('profile-level-id=4d');
break;
}
}
} }
return this.orgSetRemoteDescription.apply(this, args); return this.orgSetRemoteDescription.apply(this, args);
} }