Skip to content

Commit

Permalink
FCE-376 Fix cleanup in disconnect event handler (#60)
Browse files Browse the repository at this point in the history
Co-authored-by: Adrian Czerwiec <[email protected]>
  • Loading branch information
kamil-stasiak and czerwiukk authored Aug 19, 2024
1 parent d1b94d0 commit a53f785
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const DeviceControls = ({
className="btn btn-error btn-sm"
disabled={!device?.stream}
onClick={() => {
device?.cleanup();
device?.stop();
}}
>
Stop {type} device
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ export const MainControls = () => {
}}
defaultOptionText="Select video device"
stop={() => {
video.cleanup();
video.stop();
}}
/>

Expand All @@ -389,7 +389,7 @@ export const MainControls = () => {
}}
defaultOptionText="Select audio device"
stop={() => {
audio.cleanup();
audio.stop();
}}
/>

Expand Down
6 changes: 4 additions & 2 deletions packages/react-client/src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,10 @@ export class Client<PeerMetadata, TrackMetadata> extends (EventEmitter as {

this.tsClient.on("disconnected", () => {
this.status = null;
this.videoTrackManager.stopStreaming();
this.audioTrackManager.stopStreaming();

this.videoTrackManager.cleanup();
this.audioTrackManager.cleanup();

this.stateToSnapshot();

this.emit("disconnected", this);
Expand Down
1 change: 1 addition & 0 deletions packages/react-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type {
UseSetupMediaResult,
UseSetupMediaConfig,
CreateFishjamClient,
ScreenshareApi,
UseConnect,
GenericTrackManager,
} from "./types";
Expand Down
10 changes: 7 additions & 3 deletions packages/react-client/src/trackManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { Track } from "./state.types";
import { getRemoteOrLocalTrack } from "./utils/track";

export class TrackManager<PeerMetadata, TrackMetadata> implements GenericTrackManager<TrackMetadata> {
private mediaManager: GenericMediaManager;
private tsClient: FishjamClient<PeerMetadata, TrackMetadata>;
private readonly mediaManager: GenericMediaManager;
private readonly tsClient: FishjamClient<PeerMetadata, TrackMetadata>;
private currentTrackId: string | null = null;

constructor(tsClient: FishjamClient<PeerMetadata, TrackMetadata>, deviceManager: GenericMediaManager) {
Expand All @@ -31,10 +31,14 @@ export class TrackManager<PeerMetadata, TrackMetadata> implements GenericTrackMa
this.mediaManager?.start(deviceId ?? true);
};

public cleanup = async () => {
public stop = async () => {
this?.mediaManager?.stop();
};

public cleanup = () => {
this.currentTrackId = null;
};

public startStreaming = async (
trackMetadata?: TrackMetadata,
simulcastConfig?: SimulcastConfig,
Expand Down
4 changes: 2 additions & 2 deletions packages/react-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export interface GenericMediaManager {

export interface GenericTrackManager<TrackMetadata> {
initialize: (deviceId?: string) => Promise<void>;
cleanup: () => Promise<void>;
stop: () => Promise<void>;
startStreaming: (
trackMetadata?: TrackMetadata,
simulcastConfig?: SimulcastConfig,
Expand All @@ -181,7 +181,7 @@ export type UserMediaAPI<TrackMetadata> = {
};

export type ScreenshareApi<TrackMetadata> = {
startStreaming: () => Promise<void>;
startStreaming: (props?: { metadata?: TrackMetadata; requestAudio?: boolean }) => Promise<void>;
stopStreaming: () => Promise<void>;
stream: MediaStream | null;
videoTrack: MediaStreamTrack | null;
Expand Down

0 comments on commit a53f785

Please sign in to comment.