feat: Configure video tracks for high bitrate and prefer VP8/H264 codecs.

This commit is contained in:
2026-02-23 03:15:07 +01:00
parent 610ad6d661
commit 8840a429c7

View File

@@ -25,7 +25,18 @@ socket.on('viewer', id => {
peerConnections[id] = peerConnection;
activeStream.getTracks().forEach(track => {
peerConnection.addTrack(track, activeStream);
const sender = peerConnection.addTrack(track, activeStream);
// Force high bitrate for best possible 1080p60 quality
if (track.kind === 'video') {
const params = sender.getParameters();
if (!params.encodings) params.encodings = [{}];
// Set max bandwidth to 10 Mbps (10,000,000 bps)
params.encodings[0].maxBitrate = 10000000;
sender.setParameters(params).catch(e => console.error("Could not set high bitrate:", e));
}
});
peerConnection.onicecandidate = event => {
@@ -36,7 +47,32 @@ socket.on('viewer', id => {
peerConnection
.createOffer()
.then(sdp => peerConnection.setLocalDescription(sdp))
.then(sdp => {
// Force VP8/H264 on the SDP offer to maximize compatibility with Chromium
const RTCRtpSender = RTCRtpSender || window.RTCRtpSender;
if (RTCRtpSender && RTCRtpSender.getCapabilities) {
const capabilities = RTCRtpSender.getCapabilities('video');
if (capabilities && capabilities.codecs) {
const preferredCodecs = capabilities.codecs.filter(c =>
c.mimeType.toLowerCase() === 'video/vp8' ||
c.mimeType.toLowerCase() === 'video/h264'
);
if (preferredCodecs.length > 0) {
const transceivers = peerConnection.getTransceivers();
transceivers.forEach(transceiver => {
if (transceiver.receiver.track.kind === 'video') {
try {
transceiver.setCodecPreferences(preferredCodecs);
} catch (e) {
console.warn('Failed to set codec preferences:', e);
}
}
});
}
}
}
return peerConnection.setLocalDescription(sdp);
})
.then(() => {
socket.emit('offer', id, peerConnection.localDescription);
});