Skip to content

Commit

Permalink
Handle track ended in media sender
Browse files Browse the repository at this point in the history
  • Loading branch information
havfo committed Mar 12, 2024
1 parent 22d0ae1 commit c64061a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
18 changes: 18 additions & 0 deletions src/store/middlewares/mediaMiddleware.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ const createMediaMiddleware = ({
});

mediaService.on('mediaClosed', (source) => {
logger.debug('mediaClosed() [source:%s]', source);

if (source === 'webcam') {
dispatch(meActions.setLostVideo(true));
dispatch(meActions.setVideoMuted(true));
Expand All @@ -120,6 +122,22 @@ const createMediaMiddleware = ({
dispatch(meActions.setAudioMuted(true));
dispatch(meActions.setMicEnabled(false));
}

if (source === 'screen') {
dispatch(meActions.setScreenEnabled(false));
}

if (source === 'screenaudio') {
dispatch(meActions.setScreenAudioEnabled(false));
}

if (source === 'extravideo') {
dispatch(meActions.setExtraVideoEnabled(false));
}

if (source === 'extraaudio') {
dispatch(meActions.setExtraAudioEnabled(false));
}
});

mediaService.on('lostMediaServer', () => {
Expand Down
18 changes: 12 additions & 6 deletions src/utils/mediaSender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export class MediaSender extends EventEmitter {
public running = false;
public paused = false;

public track: MediaStreamTrack | null = null;
private producerOptions: ProducerOptions = {};
private codec?: ProducerCodec;
public producer?: Producer;
Expand Down Expand Up @@ -54,6 +53,10 @@ export class MediaSender extends EventEmitter {
this.handleSignaling();
}

get track(): MediaStreamTrack | null {
return this.producerOptions.track ?? null;
}

private handleSignaling(): void {
this.signalingService.on('notification', (notification) => {
switch (notification.method) {
Expand Down Expand Up @@ -87,6 +90,7 @@ export class MediaSender extends EventEmitter {
this.running = true;
this.producerOptions = producerOptions;
this.codec = codec;
this.handleTrack();

const promises: Promise<Producer | PeerProducer>[] = [ this.sfuProduce() ];

Expand All @@ -104,8 +108,6 @@ export class MediaSender extends EventEmitter {
sfuResult.value.pause();
}

this.track = producerOptions.track ?? null;

this.maybeAddHark();
}

Expand Down Expand Up @@ -136,7 +138,6 @@ export class MediaSender extends EventEmitter {
}

this.track?.stop();
this.track = null;
this.producerOptions = {};
this.codec = undefined;

Expand Down Expand Up @@ -269,9 +270,8 @@ export class MediaSender extends EventEmitter {

const oldTrack = this.track;

this.track = track;
this.producerOptions.track = track;

this.handleTrack();
this.maybeAddHark();

await this.producer?.replaceTrack({ track });
Expand Down Expand Up @@ -380,4 +380,10 @@ export class MediaSender extends EventEmitter {

return peerProducingPromise;
}

private handleTrack(): void {
this.track?.addEventListener('ended', () => {
this.stop(false);
}, { once: true });
}
}

0 comments on commit c64061a

Please sign in to comment.