在使用Freeswitch会议融屏的时候,画面有时候会偏色,感觉编解码少了通道,整体会偏紫;怀疑是mp4的编解码可能有问题,后续改成优先VP8,感觉在使用的时候会减少类似情况;
找到暴露的位置
javascript
UA.on('newRTCSession', (res) => {
let { session, originator } = res;
session.on('sdp', (event) => {
// sdp一定要判断type是'offer'的时候去修改;
if (event.type === 'offer') {
let sdp = event.sdp;
// 修改SDP优先匹配VP8
sdp = SDPPrioritizeCodec(sdp, 'VP8');
event.sdp = sdp;
};
});
})
封装修改SDP方法
javascript
/* 调用 SDPPrioritizeCodec(sdp, 'VP8'); */
export function SDPPrioritizeCodec(sdp, preferredCodec) {
const codecRegex = /a=rtpmap:(\d+) (\w+)/g;
let codecPayloads = {};
let sdpLines = sdp.split('\r\n');
let mLineIndex = -1;
// 遍历SDP的每一行,找到codec的payload types
sdpLines.forEach((line, index) => {
if (line.startsWith('m=video')) {
mLineIndex = index;
}
let match = codecRegex.exec(line);
while (match) {
codecPayloads[match[2]] = match[1];
match = codecRegex.exec(line);
}
});
// 如果找到了m=video行
if (mLineIndex !== -1) {
let mLineParts = sdpLines[mLineIndex].split(' ');
let payloadList = mLineParts.slice(3); // 去掉m=video和端口号部分,只保留payload types
let preferredPayload = codecPayloads[preferredCodec];
let updatedPayloadList = payloadList.filter(p => p !== preferredPayload); // 删除优先编解码器,准备重新排序
if (preferredPayload) {
// 将优先的编解码器Payload放在最前面
updatedPayloadList.unshift(preferredPayload);
// 重新构建m=video行
mLineParts = mLineParts.slice(0, 3).concat(updatedPayloadList);
sdpLines[mLineIndex] = mLineParts.join(' ');
}
}
return sdpLines.join('\r\n');
}