Files
discord-decomp/lib/RTCMediaSinkWantsManager.tsx
T
2026-11-01 12:12:55 +02:00

524 lines
17 KiB
TypeScript

// Metro module 12862 (f12863).
import AuthenticationStore from "../stores/AuthenticationStore";
import { VideoToggleState } from "../Constants";
import { SimulcastOverrideQuality } from "../_bundle/Module4250";
import Durations from "../utils/Durations";
import { getFirefoxVersion } from "../utils/BrowserUtils";
import { getBrowserInvertedWantsConfig } from "../modules/media_engine/BrowserInvertedWantsExperiment";
import TypedEventEmitter from "../_bundle/TypedEventEmitter";
import { MediaSinkWantsLadder } from "../_bundle/MediaSinkWantsLadder";
import { DelayedCall, Timeout } from "../_bundle/Timeout";
import { WindowVisibilityVideoManager, WindowVisibilityEvent } from "./WindowVisibilityVideoManager";
import SnowflakeUtils from "../utils/SnowflakeUtils";
import { isDesktop } from "../utils/PlatformUtils";
import shallowEqual from "../_bundle/shallowEqual";
import Module12 from "../_bundle/Module12";
import { BaseConnectionEvent } from "../_bundle/Module4286";
let any, c8867_8, c8867_9, c8867_10, c8867_11, c8867_12;
const getDefaultWants = function getDefaultWants(wantsLevel) {
return getBrowserInvertedWantsConfig("RTCMediaSinkWantsManager.getDefaultWants").invertWants ? {
...c8867_8
} : {
any: wantsLevel
};
};
any = 100;
c8867_8 = {
any: 0
};
c8867_9 = 30 * Durations.Millis.SECOND;
c8867_10 = 120 * Durations.Millis.SECOND;
c8867_11 = getFirefoxVersion() !== -1;
c8867_12 = {
UserSSRCUpdate: "user-ssrc-update",
Update: "update"
};
class RTCMediaSinkWantsManager extends TypedEventEmitter {
constructor(a0, a1, a2, ladder = new MediaSinkWantsLadder()) {
super();
const _this = this;
this.connection = null;
this.audioSsrcs = {};
this.videoSsrcs = {};
this.remoteVideoSsrcs = {};
this.framesReceived = {};
this.streamIds = {};
this.offscreenUsers = {};
this.offscreenDisabledUsers = {};
this.streamPixelCounts = {};
this.latestWants = getBrowserInvertedWantsConfig("RTCMediaSinkWantsManager.getDefaultWants").invertWants ? {
...c8867_8
} : {
any
};
this.participants = new Set();
this.selectedParticipantId = null;
this.pipOpen = false;
this.simulcastDebugOverrides = new Map();
this.videoHealthManager = null;
this.otherUsers = new Set();
this.delayedUpdate = () => {
_this.delayedCall.delay();
};
this.addLru = (a0, a1, a2) => {
let length2;
a2.push(a0);
if (!(a2.length <= 3)) {
const length = a2.length;
let r1 = 0;
const r6 = 0 < length;
let r2 = -1;
let r10 = -1;
let r17 = -1;
if (r6) {
do {
const r21 = a1 - _this.offscreenUsers[a2[r1]];
const r3 = r1;
let r0 = r2;
let r22 = r10;
if (r21 > r22) {
r0 = r3;
r22 = r21;
}
r1 = r3 + 1;
length2 = a2.length;
r2 = r0;
r10 = r22;
r17 = r2;
} while (r1 < length2);
}
_this.offscreenDisabledUsers[a2[r17]] = true;
a2.splice(r17, 1);
}
};
this.updateOffscreenUsers = () => {
let r33;
const connection = _this.connection;
if (connection == null ? void 0 : connection.getActiveOutputSinkTrackingEnabled()) {
const r39 = Date.now();
const r8 = [];
let r47 = SnowflakeUtils.entries(_this.streamIds)[Symbol.iterator]();
const next = r47.next;
while (true) {
const r16 = r47 === void 0 ? void 0 : (r33 = next.call(r47)).done ? r47 = void 0 : r33.value;
if (r47 === void 0) {
if (!(r8.length > 0)) {
_this.offscreenTimeout.stop();
break;
}
let r102 = r39 + _this.getOffscreenTimeoutMs();
for (const item of r8) {
const r109 = Math;
const r111 = r102;
r102 = r109.min(r111, _this.offscreenUsers[item] + _this.getOffscreenTimeoutMs());
}
_this.offscreenTimeout.start(r102 - r39, _this.update);
break;
}
try {
const [r54, r55] = r16;
if (r55 != null) {
const connection2 = _this.connection;
const r18 = connection2 == null ? void 0 : connection2.getHasActiveVideoOutputSink(r55);
const offscreenUsers = _this.offscreenUsers;
if (r18) {
delete offscreenUsers[r54];
delete _this.offscreenDisabledUsers[r54];
} else if (offscreenUsers[r54] == null) {
const offscreenUsers2 = _this.offscreenUsers;
offscreenUsers2[r54] = r39;
_this.addLru(r54, r39, r8);
} else if (!_this.offscreenDisabledUsers[r54]) {
if (r39 - _this.offscreenUsers[r54] >= _this.getOffscreenTimeoutMs()) {
_this.offscreenDisabledUsers[r54] = true;
} else {
_this.addLru(r54, r39, r8);
}
}
}
} catch (r90) {
if (r47 !== void 0 && typeof r47.return === "function") {
try {
r47.return();
} catch {}
}
throw r90;
}
}
}
};
this.handleLocalVideoDisabled = () => {
_this.update();
};
this.handleLocalMute = () => {
_this.update();
};
this.update = function update(r32 = []) {
let r54;
const wantsLevel = _this.getWantsLevel();
let latestWants = getDefaultWants(wantsLevel);
const invertWants2 = getBrowserInvertedWantsConfig("RTCMediaSinkWantsManager.update").invertWants;
let r65 = latestWants;
if (invertWants2) {
_this.invertWants(latestWants, wantsLevel);
r65 = c8867_11 ? {
...latestWants
} : latestWants;
}
_this.updateOffscreenUsers();
const r18 = isDesktop() && _this.isOneToOneCall() && !_this.isStageChannel;
for (const item of SnowflakeUtils.entries(_this.videoSsrcs)) {
const [r35, r99] = item;
let r100 = [];
let r101 = false;
let r107 = _this.streamPixelCounts[_this.streamIds[r35]];
if (r107 == null) {
r107 = 0;
}
const wantsLevel2 = _this.getWantsLevel(r107);
let ssrc2 = r99[0].ssrc;
if (_this.shouldReceiveFromUser(r35)) {
const r132 = r35 === _this.selectedParticipantId && wantsLevel !== any && !_this.pipOpen;
if (r99.length > 1) {
let r143 = r99[Symbol.iterator]();
const next = r143.next;
while (true) {
const r146 = r143 === void 0 ? void 0 : (r54 = next.call(r143)).done ? r143 = void 0 : r54.value;
if (r143 === void 0) {
if (!_this.supportsSeamless) {
break;
}
if (_this.framesReceived[ssrc2]) {
break;
}
r101 = true;
r100 = [ssrc2];
for (const item3 of r99) {
if (item3.ssrc !== ssrc2 && _this.framesReceived[item3.ssrc]) {
if (item3.quality === any) {
latestWants[item3.ssrc] = any;
} else {
latestWants[item3.ssrc] = r18 ? wantsLevel2 : wantsLevel;
}
r100.push(item3.ssrc);
}
}
break;
}
try {
if (r146.quality === any) {
const ssrc = r146.ssrc;
if (r132) {
latestWants[ssrc] = any;
ssrc2 = r146.ssrc;
} else {
latestWants[ssrc] = 0;
}
} else if (r132) {
latestWants[r146.ssrc] = 0;
} else {
if (r18) {
latestWants[r146.ssrc] = wantsLevel2;
}
ssrc2 = r146.ssrc;
}
} catch (r162) {
if (r143 !== void 0 && typeof r143.return === "function") {
try {
r143.return();
} catch {}
}
throw r162;
}
}
} else if (r132) {
latestWants[ssrc2] = any;
} else if (r18) {
latestWants[ssrc2] = wantsLevel2;
}
} else {
for (const item2 of r99) {
latestWants[item2.ssrc] = 0;
}
}
const simulcastOverrideQuality = _this.getSimulcastOverrideQuality(r35);
if (simulcastOverrideQuality === SimulcastOverrideQuality.HIGH) {
latestWants[ssrc2] = any;
} else if (simulcastOverrideQuality === SimulcastOverrideQuality.LOW) {
latestWants[ssrc2] = 50;
}
if (!(_this.supportsSeamless && r101)) {
r100 = [ssrc2];
}
for (const item4 of r99) {
if (!r100.includes(item4.ssrc)) {
delete _this.framesReceived[item4.ssrc];
}
}
let r243 = r32.includes(r35);
if (!r243) {
r243 = _this.remoteVideoSsrcs[r35] !== void 0 && !shallowEqual(_this.remoteVideoSsrcs[r35], r100);
}
if (!r243) {
continue;
}
const remoteVideoSsrcs = _this.remoteVideoSsrcs;
const r264 = r100;
remoteVideoSsrcs[r35] = [...r264];
_this.emit(c8867_12.UserSSRCUpdate, r35, _this.audioSsrcs[r35], r264);
}
if (c8867_11) {
latestWants = r65;
}
for (const item5 of Object.entries(_this.audioSsrcs)) {
const [r293, r294] = item5;
const connection = _this.connection;
if (connection == null ? void 0 : connection.getLocalMute(r293)) {
latestWants[r294] = 0;
}
}
if (!(_this.connection == null || Module12.isEqual(_this.latestWants, latestWants))) {
_this.latestWants = latestWants;
_this.emit(c8867_12.Update, latestWants);
}
return latestWants;
};
this.incomingVideoEnabledChanged = () => {
_this.update();
};
this.userId = a0;
this.isStageChannel = a1;
this.supportsSeamless = a2;
this.ladder = ladder;
this.delayedCall = new DelayedCall(100, this.update);
this.offscreenTimeout = new Timeout();
WindowVisibilityVideoManager.on(WindowVisibilityEvent.IncomingVideoEnabledChanged, this.incomingVideoEnabledChanged);
}
getWantsLevel(r5 = 0) {
return this.ladder.getMaxSinkValue(this.getVideoParticipantCount(), r5);
}
userVideoDisabled(a0) {
return this.offscreenDisabledUsers[a0];
}
isOneToOneCall() {
return this.otherUsers.size === 1;
}
updateCallUserIds(a0) {
this.otherUsers = new Set(a0);
this.otherUsers.delete(AuthenticationStore.getId());
this.update();
}
shouldReceiveFromUser(a0) {
const connection = this.connection;
let r4 = connection == null ? void 0 : connection.getLocalVideoDisabled(a0);
if (!r4) {
let r12 = this.userVideoDisabled(a0);
if (r12) {
const videoHealthManager = this.videoHealthManager;
r12 = (videoHealthManager == null ? void 0 : videoHealthManager.getCurrentVideoToggleState(a0)) !== VideoToggleState.AUTO_PROBING;
}
r4 = r12;
}
return WindowVisibilityVideoManager.isIncomingVideoEnabled() && !r4;
}
invertWants(a0, a1) {
for (const item of Object.values(this.videoSsrcs)) {
for (const item2 of item) {
if (c8867_11) {
if (item2.quality !== any) {
a0[item2.ssrc] = any;
} else {
a0[item2.ssrc] = 0;
}
} else {
a0[item2.ssrc] = a1;
}
}
}
for (const item3 of Object.values(this.audioSsrcs)) {
a0[item3] = any;
}
}
getAudioSSRCs() {
return this.audioSsrcs;
}
setConnection(a0, r2 = true) {
const connection = this.connection;
if (connection != null) {
connection.removeListener(BaseConnectionEvent.LocalVideoDisabled, this.handleLocalVideoDisabled);
}
const connection2 = this.connection;
if (connection2 != null) {
connection2.removeListener(BaseConnectionEvent.LocalMute, this.handleLocalMute);
}
const connection3 = this.connection;
if (connection3 != null) {
connection3.removeListener(BaseConnectionEvent.ActiveSinksChange, this.delayedUpdate);
}
this.connection = a0;
const connection4 = this.connection;
if (connection4 != null) {
connection4.addListener(BaseConnectionEvent.LocalVideoDisabled, this.handleLocalVideoDisabled);
}
const connection5 = this.connection;
if (connection5 != null) {
connection5.addListener(BaseConnectionEvent.LocalMute, this.handleLocalMute);
}
const connection6 = this.connection;
if (connection6 != null) {
connection6.addListener(BaseConnectionEvent.ActiveSinksChange, this.delayedUpdate);
}
if (r2) {
this.update();
}
}
setAudioSSRC(a0, a1) {
if (a1 > 0) {
this.audioSsrcs[a0] = a1;
} else {
delete this.audioSsrcs[a0];
}
return this.update();
}
setVideoSSRCs(a0, a1) {
const r26 = a1.filter((item) => {
let active = item.active;
if (active) {
let ssrc = item.ssrc;
if (ssrc == null) {
ssrc = 0;
}
active = ssrc > 0;
}
return active;
}).map((item) => {
let quality = item.quality;
if (quality == null) {
quality = any;
}
return {
quality,
ssrc: item.ssrc
};
});
if (r26.length > 0) {
this.videoSsrcs[a0] = r26;
this.participants.add(a0);
} else {
if (this.videoSsrcs[a0] !== void 0) {
for (const item of this.videoSsrcs[a0]) {
delete this.framesReceived[item.ssrc];
}
}
delete this.remoteVideoSsrcs[a0];
delete this.videoSsrcs[a0];
this.participants.delete(a0);
this.emit(c8867_12.UserSSRCUpdate, a0, this.audioSsrcs[a0], []);
}
return this.update(Array.from(this.participants));
}
setFirstFrameReceived(a0) {
this.framesReceived[a0] = true;
return this.update();
}
setStreamId(a0, a1) {
if (a1 != null) {
this.streamIds[a0] = a1;
} else {
if (a0 in this.streamIds) {
delete this.streamPixelCounts[this.streamIds[a0]];
}
delete this.streamIds[a0];
}
return this.update();
}
destroyUser(a0) {
delete this.audioSsrcs[a0];
delete this.videoSsrcs[a0];
this.participants.delete(a0);
delete this.streamPixelCounts[this.streamIds[a0]];
delete this.streamIds[a0];
return this.update(Array.from(this.participants));
}
reset() {
this.setConnection(null, false);
this.audioSsrcs = {};
this.videoSsrcs = {};
this.remoteVideoSsrcs = {};
this.framesReceived = {};
this.streamIds = {};
this.streamPixelCounts = {};
this.latestWants = getBrowserInvertedWantsConfig("RTCMediaSinkWantsManager.getDefaultWants").invertWants ? {
...c8867_8
} : {
any
};
WindowVisibilityVideoManager.off(WindowVisibilityEvent.IncomingVideoEnabledChanged, this.incomingVideoEnabledChanged);
}
setSelectedParticipant(a0) {
if (a0 === this.selectedParticipantId) {
return this.latestWants;
}
const r0 = this.selectedParticipantId != null && this.selectedParticipantId !== this.userId;
const r11 = [];
if (r0) {
r11.push(this.selectedParticipantId);
}
if (a0 != null) {
if (this.participants.has(a0)) {
this.selectedParticipantId = a0;
r11.push(a0);
} else {
if (this.userId !== a0) {
return this.latestWants;
}
this.selectedParticipantId = a0;
}
} else {
this.selectedParticipantId = null;
}
return this.update(r11.filter((item) => this.videoSsrcs[item]?.length > 1));
}
setPipOpen(a0) {
this.pipOpen = a0;
return this.selectedParticipantId != null ? this.update([this.selectedParticipantId]) : this.latestWants;
}
getOffscreenDisabledUsers() {
return this.offscreenDisabledUsers;
}
setSimulcastDebugOverride(a0, a1) {
this.simulcastDebugOverrides.set(a0, a1);
this.update();
}
setVideoSize(a0, a1) {
if (a1 > 0) {
this.streamPixelCounts[a0] = a1;
} else {
delete this.streamPixelCounts[a0];
}
this.delayedUpdate();
}
getVideoParticipantCount() {
let r0 = 0;
for (const item of Object.keys(this.videoSsrcs)) {
const connection = this.connection;
if (!(connection == null ? void 0 : connection.getLocalVideoDisabled(item))) {
r0 = r0 + 1;
}
}
return r0;
}
getOffscreenTimeoutMs() {
return this.isStageChannel ? c8867_10 : c8867_9;
}
getSimulcastOverrideQuality(a0) {
return this.simulcastDebugOverrides.has(a0) ? this.simulcastDebugOverrides.get(a0) : SimulcastOverrideQuality.NO_OVERRIDE;
}
}
export default RTCMediaSinkWantsManager;
export const DEFAULT_WANTS_FULL = {
any: 100
};
export { c8867_8 as DEFAULT_WANTS_DISABLED };
export { c8867_12 as RTCMediaSinkWantsManagerEvent };