refactor to use destroy method
This commit is contained in:
@@ -527,12 +527,8 @@ export function createCallViewModel$(
|
|||||||
connectOptions$.value,
|
connectOptions$.value,
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
createPublisherFactory: (
|
createPublisherFactory: (connection: Connection) => {
|
||||||
scope: ObservableScope,
|
|
||||||
connection: Connection,
|
|
||||||
) => {
|
|
||||||
return new Publisher(
|
return new Publisher(
|
||||||
scope,
|
|
||||||
connection,
|
connection,
|
||||||
mediaDevices,
|
mediaDevices,
|
||||||
muteStates,
|
muteStates,
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ import { type CallMembershipIdentityParts } from "matrix-js-sdk/lib/matrixrtc/En
|
|||||||
|
|
||||||
import { type Behavior } from "../../Behavior.ts";
|
import { type Behavior } from "../../Behavior.ts";
|
||||||
import { type IConnectionManager } from "../remoteMembers/ConnectionManager.ts";
|
import { type IConnectionManager } from "../remoteMembers/ConnectionManager.ts";
|
||||||
import { ObservableScope } from "../../ObservableScope.ts";
|
import { type ObservableScope } from "../../ObservableScope.ts";
|
||||||
import { type Publisher } from "./Publisher.ts";
|
import { type Publisher } from "./Publisher.ts";
|
||||||
import { type MuteStates } from "../../MuteStates.ts";
|
import { type MuteStates } from "../../MuteStates.ts";
|
||||||
import {
|
import {
|
||||||
@@ -124,10 +124,7 @@ interface Props {
|
|||||||
scope: ObservableScope;
|
scope: ObservableScope;
|
||||||
muteStates: MuteStates;
|
muteStates: MuteStates;
|
||||||
connectionManager: IConnectionManager;
|
connectionManager: IConnectionManager;
|
||||||
createPublisherFactory: (
|
createPublisherFactory: (connection: Connection) => Publisher;
|
||||||
scope: ObservableScope,
|
|
||||||
connection: Connection,
|
|
||||||
) => Publisher;
|
|
||||||
joinMatrixRTC: (transport: LivekitTransport) => void;
|
joinMatrixRTC: (transport: LivekitTransport) => void;
|
||||||
homeserverConnected: HomeserverConnected;
|
homeserverConnected: HomeserverConnected;
|
||||||
localTransport$: Behavior<LocalTransportWithSFUConfig | null>;
|
localTransport$: Behavior<LocalTransportWithSFUConfig | null>;
|
||||||
@@ -318,13 +315,12 @@ export const createLocalMembership$ = ({
|
|||||||
connection?.transport.livekit_service_url,
|
connection?.transport.livekit_service_url,
|
||||||
);
|
);
|
||||||
if (connection !== null) {
|
if (connection !== null) {
|
||||||
const scope = new ObservableScope();
|
const publisher = createPublisherFactory(connection);
|
||||||
const publisher = createPublisherFactory(scope, connection);
|
|
||||||
publisher$.next(publisher);
|
publisher$.next(publisher);
|
||||||
// Clean-up callback
|
|
||||||
|
|
||||||
|
// Clean-up callback
|
||||||
return Promise.resolve(async (): Promise<void> => {
|
return Promise.resolve(async (): Promise<void> => {
|
||||||
scope.end();
|
await publisher.destroy();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ import { MatrixError, type MatrixClient } from "matrix-js-sdk";
|
|||||||
import {
|
import {
|
||||||
combineLatest,
|
combineLatest,
|
||||||
distinctUntilChanged,
|
distinctUntilChanged,
|
||||||
first,
|
|
||||||
from,
|
from,
|
||||||
map,
|
map,
|
||||||
of,
|
of,
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ import {
|
|||||||
import { getUrlParams } from "../../../UrlParams.ts";
|
import { getUrlParams } from "../../../UrlParams.ts";
|
||||||
import { observeTrackReference$ } from "../../MediaViewModel.ts";
|
import { observeTrackReference$ } from "../../MediaViewModel.ts";
|
||||||
import { type Connection } from "../remoteMembers/Connection.ts";
|
import { type Connection } from "../remoteMembers/Connection.ts";
|
||||||
import { type ObservableScope } from "../../ObservableScope.ts";
|
import { ObservableScope } from "../../ObservableScope.ts";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A wrapper for a Connection object.
|
* A wrapper for a Connection object.
|
||||||
@@ -47,9 +47,10 @@ export class Publisher {
|
|||||||
*/
|
*/
|
||||||
public shouldPublish = false;
|
public shouldPublish = false;
|
||||||
|
|
||||||
|
private readonly scope = new ObservableScope();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new Publisher.
|
* Creates a new Publisher.
|
||||||
* @param scope - The observable scope to use for managing the publisher.
|
|
||||||
* @param connection - The connection to use for publishing.
|
* @param connection - The connection to use for publishing.
|
||||||
* @param devices - The media devices to use for audio and video input.
|
* @param devices - The media devices to use for audio and video input.
|
||||||
* @param muteStates - The mute states for audio and video.
|
* @param muteStates - The mute states for audio and video.
|
||||||
@@ -57,7 +58,6 @@ export class Publisher {
|
|||||||
* @param logger - The logger to use for logging :D.
|
* @param logger - The logger to use for logging :D.
|
||||||
*/
|
*/
|
||||||
public constructor(
|
public constructor(
|
||||||
private scope: ObservableScope,
|
|
||||||
private connection: Pick<Connection, "livekitRoom" | "state$">, //setE2EEEnabled,
|
private connection: Pick<Connection, "livekitRoom" | "state$">, //setE2EEEnabled,
|
||||||
devices: MediaDevices,
|
devices: MediaDevices,
|
||||||
private readonly muteStates: MuteStates,
|
private readonly muteStates: MuteStates,
|
||||||
@@ -65,7 +65,6 @@ export class Publisher {
|
|||||||
private logger: Logger,
|
private logger: Logger,
|
||||||
) {
|
) {
|
||||||
const { controlledAudioDevices } = getUrlParams();
|
const { controlledAudioDevices } = getUrlParams();
|
||||||
|
|
||||||
const room = connection.livekitRoom;
|
const room = connection.livekitRoom;
|
||||||
|
|
||||||
room.setE2EEEnabled(room.options.e2ee !== undefined)?.catch((e: Error) => {
|
room.setE2EEEnabled(room.options.e2ee !== undefined)?.catch((e: Error) => {
|
||||||
@@ -73,27 +72,11 @@ export class Publisher {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Setup track processor syncing (blur)
|
// Setup track processor syncing (blur)
|
||||||
this.observeTrackProcessors(scope, room, trackerProcessorState$);
|
this.observeTrackProcessors(this.scope, room, trackerProcessorState$);
|
||||||
// Observe media device changes and update LiveKit active devices accordingly
|
// Observe media device changes and update LiveKit active devices accordingly
|
||||||
this.observeMediaDevices(scope, devices, controlledAudioDevices);
|
this.observeMediaDevices(this.scope, devices, controlledAudioDevices);
|
||||||
|
|
||||||
this.workaroundRestartAudioInputTrackChrome(devices, scope);
|
this.workaroundRestartAudioInputTrackChrome(devices, this.scope);
|
||||||
this.scope.onEnd(() => {
|
|
||||||
muteStates.audio.unsetHandler();
|
|
||||||
muteStates.video.unsetHandler();
|
|
||||||
this.logger.info(
|
|
||||||
"Scope ended -> unset handler + stop publishing all tracks",
|
|
||||||
);
|
|
||||||
|
|
||||||
const stopAllMedia = async () => {
|
|
||||||
logger.info("onEnd: start stopping all media");
|
|
||||||
await this.stopPublishing();
|
|
||||||
logger.info("onEnd: stopped publishing");
|
|
||||||
await this.stopTracks();
|
|
||||||
logger.info("onEnd: stopped tracks");
|
|
||||||
};
|
|
||||||
void stopAllMedia();
|
|
||||||
});
|
|
||||||
|
|
||||||
this.connection.livekitRoom.localParticipant.on(
|
this.connection.livekitRoom.localParticipant.on(
|
||||||
ParticipantEvent.LocalTrackPublished,
|
ParticipantEvent.LocalTrackPublished,
|
||||||
@@ -101,6 +84,21 @@ export class Publisher {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async destroy(): Promise<void> {
|
||||||
|
this.scope.end();
|
||||||
|
this.logger.info("Scope ended -> unset handler");
|
||||||
|
this.muteStates.audio.unsetHandler();
|
||||||
|
this.muteStates.video.unsetHandler();
|
||||||
|
|
||||||
|
this.logger.info(`Start to stop tracks`);
|
||||||
|
try {
|
||||||
|
await this.stopTracks();
|
||||||
|
this.logger.info(`Done to stop tracks`);
|
||||||
|
} catch (e) {
|
||||||
|
this.logger.error(`Failed to stop publishing: ${e}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// LiveKit will publish the tracks as soon as they are created
|
// LiveKit will publish the tracks as soon as they are created
|
||||||
// but we want to control when tracks are published.
|
// but we want to control when tracks are published.
|
||||||
// We cannot just mute the tracks, even if this will effectively stop the publishing,
|
// We cannot just mute the tracks, even if this will effectively stop the publishing,
|
||||||
|
|||||||
@@ -275,7 +275,7 @@ export class Connection {
|
|||||||
"[Connection " + opts.transport.livekit_service_url + "]",
|
"[Connection " + opts.transport.livekit_service_url + "]",
|
||||||
);
|
);
|
||||||
this.logger.info(
|
this.logger.info(
|
||||||
`constructor: ${opts.transport.livekit_service_url} alias: ${opts.transport.livekit_alias} withSfuConfig?: ${sfuConfig})`,
|
`constructor: ${opts.transport.livekit_service_url} alias: ${opts.transport.livekit_alias} withSfuConfig?: ${opts.existingSFUConfig ? JSON.stringify(opts.existingSFUConfig) : "undefined"}`,
|
||||||
);
|
);
|
||||||
const { transport, client, scope } = opts;
|
const { transport, client, scope } = opts;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user