From b8873866493a3b75fbac6003889b812b17ab4d9a Mon Sep 17 00:00:00 2001 From: Kamil Stasiak Date: Thu, 27 Jun 2024 14:23:14 +0200 Subject: [PATCH] Extract `createSdpOfferEvent` to a separte file --- packages/ts-client/src/webrtc/sdpEvents.ts | 38 +++++++++++++++++++ .../ts-client/src/webrtc/webRTCEndpoint.ts | 38 ++++--------------- 2 files changed, 45 insertions(+), 31 deletions(-) create mode 100644 packages/ts-client/src/webrtc/sdpEvents.ts diff --git a/packages/ts-client/src/webrtc/sdpEvents.ts b/packages/ts-client/src/webrtc/sdpEvents.ts new file mode 100644 index 00000000..2ea4e091 --- /dev/null +++ b/packages/ts-client/src/webrtc/sdpEvents.ts @@ -0,0 +1,38 @@ +import { generateCustomEvent } from './mediaEvent'; +import { getTrackIdToTrackBitrates } from './bitrate'; +import { getMidToTrackId } from './transciever'; +import { RemoteTrackId, TrackContext } from './types'; +import { TrackContextImpl } from './internal'; + +export const createSdpOfferEvent = ( + offer: RTCSessionDescriptionInit, + connection: RTCPeerConnection | undefined, + localTrackIdToTrack: Map< + RemoteTrackId, + TrackContextImpl + >, + tracks: Map>, +) => + generateCustomEvent({ + type: 'sdpOffer', + data: { + sdpOffer: offer, + trackIdToTrackMetadata: getTrackIdToMetadata(tracks), + trackIdToTrackBitrates: getTrackIdToTrackBitrates( + connection, + localTrackIdToTrack, + tracks, + ), + midToTrackId: getMidToTrackId(connection, localTrackIdToTrack), + }, + }); + +const getTrackIdToMetadata = ( + tracks: Map>, +): Record => { + const trackIdToMetadata: Record = {}; + Array.from(tracks.entries()).forEach(([trackId, { metadata }]) => { + trackIdToMetadata[trackId] = metadata; + }); + return trackIdToMetadata; +}; diff --git a/packages/ts-client/src/webrtc/webRTCEndpoint.ts b/packages/ts-client/src/webrtc/webRTCEndpoint.ts index af62abde..52efccf2 100644 --- a/packages/ts-client/src/webrtc/webRTCEndpoint.ts +++ b/packages/ts-client/src/webrtc/webRTCEndpoint.ts @@ -34,12 +34,10 @@ import { applyBandwidthLimitation } from './bandwidth'; import { createTrackVariantBitratesEvent, getTrackBitrates, - getTrackIdToTrackBitrates, } from './bitrate'; import { addTrackToConnection, addTransceiversIfNeeded, - getMidToTrackId, setTransceiverDirection, setTransceiversToReadOnly, } from './transciever'; @@ -48,6 +46,7 @@ import { findSenderByTrack, isTrackInUse, } from './RTCPeerConnectionUtils'; +import { createSdpOfferEvent } from './sdpEvents'; /** * Main class that is responsible for connecting to the RTC Engine, sending and receiving media. @@ -1314,22 +1313,12 @@ export class WebRTCEndpoint< return; } - const mediaEvent = generateCustomEvent({ - type: 'sdpOffer', - data: { - sdpOffer: offer, - trackIdToTrackMetadata: this.getTrackIdToMetadata(), - trackIdToTrackBitrates: getTrackIdToTrackBitrates( - this.connection, - this.localTrackIdToTrack, - this.localEndpoint.tracks, - ), - midToTrackId: getMidToTrackId( - this.connection, - this.localTrackIdToTrack, - ), - }, - }); + const mediaEvent = createSdpOfferEvent( + offer, + this.connection, + this.localTrackIdToTrack, + this.localEndpoint.tracks, + ); this.sendMediaEvent(mediaEvent); for (const track of this.localTrackIdToTrack.values()) { @@ -1340,19 +1329,6 @@ export class WebRTCEndpoint< } } - private getTrackIdToMetadata = (): Record< - string, - TrackMetadata | undefined - > => { - const trackIdToMetadata: Record = {}; - Array.from(this.localEndpoint.tracks.entries()).forEach( - ([trackId, { metadata }]) => { - trackIdToMetadata[trackId] = metadata; - }, - ); - return trackIdToMetadata; - }; - private checkIfTrackBelongToEndpoint = ( trackId: string, endpoint: EndpointWithTrackContext,