Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 34 additions & 21 deletions packages/media-signaling/src/lib/services/webrtc/Processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { MediaStreamManager } from '../../media/MediaStreamManager';
import { getExternalWaiter, type PromiseWaiterData } from '../../utils/getExternalWaiter';

const DATA_CHANNEL_LABEL = 'rocket.chat';
type P2PCommand = 'mute' | 'unmute' | 'end' | 'screen-share.start' | 'screen-share.stop';
type P2PCommand = 'mute' | 'unmute' | 'end';

export class MediaCallWebRTCProcessor implements IWebRTCProcessor {
public readonly emitter: Emitter<WebRTCProcessorEvents>;
Expand Down Expand Up @@ -94,8 +94,6 @@ export class MediaCallWebRTCProcessor implements IWebRTCProcessor {

this.screenVideoTrack = newVideoTrack;
await this.loadScreenVideoTrack();

this.updateDirectionForVideoTrackChanged();
}

public async createOffer({ iceRestart }: { iceRestart?: boolean }): Promise<RTCSessionDescriptionInit> {
Expand Down Expand Up @@ -477,7 +475,7 @@ export class MediaCallWebRTCProcessor implements IWebRTCProcessor {

channel.onopen = (_event) => {
this.config.logger?.debug('Data Channel Open', channel.label);
if (!this._dataChannel || this._dataChannel.readyState !== 'open') {
if (this._dataChannel?.readyState !== 'open') {
this._dataChannel = channel;
}

Expand Down Expand Up @@ -528,7 +526,7 @@ export class MediaCallWebRTCProcessor implements IWebRTCProcessor {
}

private isValidCommand(command: string): command is P2PCommand {
return ['mute', 'unmute', 'end', 'screen-share.start', 'screen-share.stop'].includes(command);
return ['mute', 'unmute', 'end'].includes(command);
}

private getCommandFromDataChannelMessage(message: string): P2PCommand | null {
Expand Down Expand Up @@ -556,12 +554,6 @@ export class MediaCallWebRTCProcessor implements IWebRTCProcessor {
case 'end':
this._dataChannelEnded = true;
break;
case 'screen-share.start':
this.streams.screenShareRemote.setActive(true);
break;
case 'screen-share.stop':
this.streams.screenShareRemote.setActive(false);
break;
}
}

Expand Down Expand Up @@ -598,14 +590,11 @@ export class MediaCallWebRTCProcessor implements IWebRTCProcessor {
this.updateAudioDirectionAfterNegotiation();
this.updateVideoDirectionAfterNegotiation();
this.updateRemoteHeld();
this.updateRemoteScreenShare();
Comment thread
pierre-lehnen-rc marked this conversation as resolved.
}

private updateRemoteHeld(): void {
if (this.stopped) {
return;
}

if (['closed', 'failed', 'new'].includes(this.peer.connectionState)) {
if (!this.isActiveConnection()) {
return;
}

Expand All @@ -628,6 +617,30 @@ export class MediaCallWebRTCProcessor implements IWebRTCProcessor {
this.setRemoteHeld(anyTransceiverNotSending);
}

private updateRemoteScreenShare(): void {
if (!this.isActiveConnection()) {
return;
}

const transceivers = this.getTransceivers('video');
for (const transceiver of transceivers) {
if (!transceiver.currentDirection || transceiver.currentDirection === 'stopped') {
continue;
}

if (transceiver.currentDirection.includes('recv')) {
this.config.logger?.debug(`Video Transceiver is receiving; enabling screen-share`);
this.streams.screenShareRemote.setActive(true);
return;
}
}

if (this.streams.screenShareRemote.active) {
this.config.logger?.debug(`No video Transceiver is receiving, disabling screen-share`);
this.streams.screenShareRemote.setActive(false);
}
}

private registerPeerEvents() {
const { peer } = this;

Expand Down Expand Up @@ -670,6 +683,10 @@ export class MediaCallWebRTCProcessor implements IWebRTCProcessor {
return !this.stopped && this.peer.signalingState === 'stable';
}

private isActiveConnection(): boolean {
return !this.stopped && !['new', 'closed', 'failed'].includes(this.peer.connectionState);
}

private onIceCandidate(event: RTCPeerConnectionIceEvent) {
if (this.stopped) {
return;
Expand Down Expand Up @@ -760,11 +777,7 @@ export class MediaCallWebRTCProcessor implements IWebRTCProcessor {

this.streams.screenShareLocal.setActive(Boolean(this.screenVideoTrack));

if (this.screenVideoTrack) {
this.sendP2PCommand('screen-share.start');
} else {
this.sendP2PCommand('screen-share.stop');
}
this.updateDirectionForVideoTrackChanged();
Comment thread
pierre-lehnen-rc marked this conversation as resolved.
}

private onIceGatheringComplete() {
Expand Down
Loading