Merge pull request #3664 from element-hq/toger5/userMedia-alternative

Small refactor: User media alternative
This commit is contained in:
Timo
2026-01-13 17:51:36 +01:00
committed by GitHub
2 changed files with 30 additions and 57 deletions

View File

@@ -124,9 +124,9 @@ import {
} from "./remoteMembers/ConnectionManager.ts"; } from "./remoteMembers/ConnectionManager.ts";
import { import {
createMatrixLivekitMembers$, createMatrixLivekitMembers$,
type TaggedParticipant,
type LocalMatrixLivekitMember, type LocalMatrixLivekitMember,
type RemoteMatrixLivekitMember, type RemoteMatrixLivekitMember,
type MatrixLivekitMember,
} from "./remoteMembers/MatrixLivekitMembers.ts"; } from "./remoteMembers/MatrixLivekitMembers.ts";
import { import {
type AutoLeaveReason, type AutoLeaveReason,
@@ -717,65 +717,38 @@ export function createCallViewModel$(
matrixLivekitMembers, matrixLivekitMembers,
duplicateTiles, duplicateTiles,
]) { ]) {
let localUserMediaId: string | undefined = undefined; const computeMediaId = (m: MatrixLivekitMember): string =>
// add local member if available `${m.userId}:${m.membership$.value.deviceId}`;
if (localMatrixLivekitMember) {
const localUserMediaId = localMatrixLivekitMember
? computeMediaId(localMatrixLivekitMember)
: undefined;
const localAsArray = localMatrixLivekitMember
? [localMatrixLivekitMember]
: [];
const remoteWithoutLocal = matrixLivekitMembers.value.filter(
(m) => computeMediaId(m) !== localUserMediaId,
);
const allMatrixLivekitMembers = [
...localAsArray,
...remoteWithoutLocal,
];
for (const matrixLivekitMember of allMatrixLivekitMembers) {
const { userId, participant, connection$, membership$ } = const { userId, participant, connection$, membership$ } =
localMatrixLivekitMember; matrixLivekitMember;
const rtcId = membership$.value.rtcBackendIdentity; // rtcBackendIdentity
localUserMediaId = `${userId}:${membership$.value.deviceId}`; const mediaId = computeMediaId(matrixLivekitMember);
const rtcBackendIdentity = membership$.value.rtcBackendIdentity;
for (let dup = 0; dup < 1 + duplicateTiles; dup++) { for (let dup = 0; dup < 1 + duplicateTiles; dup++) {
yield { yield {
keys: [ keys: [dup, mediaId, userId, participant, connection$, rtcId],
dup,
localUserMediaId,
userId,
participant satisfies TaggedParticipant as TaggedParticipant, // Widen the type safely
connection$,
rtcBackendIdentity,
],
data: undefined,
};
}
}
// add remote members that are available
for (const {
userId,
participant,
connection$,
membership$,
} of matrixLivekitMembers.value) {
const userMediaId = `${userId}:${membership$.value.deviceId}`;
const rtcBackendIdentity = membership$.value.rtcBackendIdentity;
// skip local user as we added them manually before
if (userMediaId === localUserMediaId) continue;
for (let dup = 0; dup < 1 + duplicateTiles; dup++) {
yield {
keys: [
dup,
userMediaId,
userId,
participant,
connection$,
rtcBackendIdentity,
],
data: undefined, data: undefined,
}; };
} }
} }
}, },
( (scope, _, dup, mediaId, userId, participant, connection$, rtcId) => {
scope,
_data$,
dup,
userMediaId,
userId,
participant,
connection$,
rtcBackendIdentity,
) => {
const livekitRoom$ = scope.behavior( const livekitRoom$ = scope.behavior(
connection$.pipe(map((c) => c?.livekitRoom)), connection$.pipe(map((c) => c?.livekitRoom)),
); );
@@ -790,9 +763,9 @@ export function createCallViewModel$(
return new UserMedia( return new UserMedia(
scope, scope,
`${userMediaId}:${dup}`, `${mediaId}:${dup}`,
userId, userId,
rtcBackendIdentity, rtcId,
participant, participant,
options.encryptionSystem, options.encryptionSystem,
livekitRoom$, livekitRoom$,
@@ -801,8 +774,8 @@ export function createCallViewModel$(
localMembership.reconnecting$, localMembership.reconnecting$,
displayName$, displayName$,
matrixMemberMetadataStore.createAvatarUrlBehavior$(userId), matrixMemberMetadataStore.createAvatarUrlBehavior$(userId),
handsRaised$.pipe(map((v) => v[userMediaId]?.time ?? null)), handsRaised$.pipe(map((v) => v[mediaId]?.time ?? null)),
reactions$.pipe(map((v) => v[userMediaId] ?? undefined)), reactions$.pipe(map((v) => v[mediaId] ?? undefined)),
); );
}, },
), ),

View File

@@ -33,7 +33,7 @@ export type TaggedParticipant =
| LocalTaggedParticipant | LocalTaggedParticipant
| RemoteTaggedParticipant; | RemoteTaggedParticipant;
interface MatrixLivekitMember { export interface MatrixLivekitMember {
membership$: Behavior<CallMembership>; membership$: Behavior<CallMembership>;
connection$: Behavior<Connection | null>; connection$: Behavior<Connection | null>;
// participantId: string; We do not want a participantId here since it will be generated by the jwt // participantId: string; We do not want a participantId here since it will be generated by the jwt