Skip to content

Commit

Permalink
Extract createSdpOfferEvent to a separte file
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-stasiak committed Jun 27, 2024
1 parent 7d53c21 commit b887386
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 31 deletions.
38 changes: 38 additions & 0 deletions packages/ts-client/src/webrtc/sdpEvents.ts
Original file line number Diff line number Diff line change
@@ -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 = <EndpointMetadata, TrackMetadata>(
offer: RTCSessionDescriptionInit,
connection: RTCPeerConnection | undefined,
localTrackIdToTrack: Map<
RemoteTrackId,
TrackContextImpl<EndpointMetadata, TrackMetadata>
>,
tracks: Map<string, TrackContext<EndpointMetadata, TrackMetadata>>,
) =>
generateCustomEvent({
type: 'sdpOffer',
data: {
sdpOffer: offer,
trackIdToTrackMetadata: getTrackIdToMetadata(tracks),
trackIdToTrackBitrates: getTrackIdToTrackBitrates(
connection,
localTrackIdToTrack,
tracks,
),
midToTrackId: getMidToTrackId(connection, localTrackIdToTrack),
},
});

const getTrackIdToMetadata = <EndpointMetadata, TrackMetadata>(
tracks: Map<string, TrackContext<EndpointMetadata, TrackMetadata>>,
): Record<string, TrackMetadata | undefined> => {
const trackIdToMetadata: Record<string, TrackMetadata | undefined> = {};
Array.from(tracks.entries()).forEach(([trackId, { metadata }]) => {
trackIdToMetadata[trackId] = metadata;
});
return trackIdToMetadata;
};
38 changes: 7 additions & 31 deletions packages/ts-client/src/webrtc/webRTCEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ import { applyBandwidthLimitation } from './bandwidth';
import {
createTrackVariantBitratesEvent,
getTrackBitrates,
getTrackIdToTrackBitrates,
} from './bitrate';
import {
addTrackToConnection,
addTransceiversIfNeeded,
getMidToTrackId,
setTransceiverDirection,
setTransceiversToReadOnly,
} from './transciever';
Expand All @@ -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.
Expand Down Expand Up @@ -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()) {
Expand All @@ -1340,19 +1329,6 @@ export class WebRTCEndpoint<
}
}

private getTrackIdToMetadata = (): Record<
string,
TrackMetadata | undefined
> => {
const trackIdToMetadata: Record<string, TrackMetadata | undefined> = {};
Array.from(this.localEndpoint.tracks.entries()).forEach(
([trackId, { metadata }]) => {
trackIdToMetadata[trackId] = metadata;
},
);
return trackIdToMetadata;
};

private checkIfTrackBelongToEndpoint = (
trackId: string,
endpoint: EndpointWithTrackContext<EndpointMetadata, TrackMetadata>,
Expand Down

0 comments on commit b887386

Please sign in to comment.