@whereby.com/media 8.0.0 → 8.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -2217,8 +2217,7 @@ var _a$4;
2217
2217
  const adapter$4 = (_a$4 = adapterRaw.default) !== null && _a$4 !== void 0 ? _a$4 : adapterRaw;
2218
2218
  const logger$7 = new Logger();
2219
2219
  class Session {
2220
- constructor({ peerConnectionId, clientId, bandwidth, peerConnectionConfig, deprioritizeH264Encoding, incrementAnalyticMetric, }) {
2221
- this.peerConnectionId = peerConnectionId;
2220
+ constructor({ clientId, bandwidth, peerConnectionConfig, deprioritizeH264Encoding, incrementAnalyticMetric, }) {
2222
2221
  this.relayCandidateSeen = false;
2223
2222
  this.serverReflexiveCandidateSeen = false;
2224
2223
  this.publicHostCandidateSeen = false;
@@ -2755,8 +2754,8 @@ class P2pRtcManager {
2755
2754
  }
2756
2755
  disconnectAll() {
2757
2756
  logger$6.info("disconnectAll()");
2758
- Object.keys(this.peerConnections).forEach((peerConnectionId) => {
2759
- this.disconnect(peerConnectionId);
2757
+ Object.keys(this.peerConnections).forEach((clientId) => {
2758
+ this.disconnect(clientId);
2760
2759
  });
2761
2760
  this.peerConnections = {};
2762
2761
  this._socketListenerDeregisterFunctions.forEach((func) => {
@@ -2955,16 +2954,14 @@ class P2pRtcManager {
2955
2954
  _emit(eventName, data) {
2956
2955
  this._emitter.emit(eventName, data);
2957
2956
  }
2958
- _getSession(peerConnectionId) {
2959
- if (!(peerConnectionId in this.peerConnections)) {
2957
+ _getSession(clientId) {
2958
+ if (!(clientId in this.peerConnections)) {
2960
2959
  return null;
2961
2960
  }
2962
- return this.peerConnections[peerConnectionId];
2961
+ return this.peerConnections[clientId];
2963
2962
  }
2964
- _createSession({ clientId, initialBandwidth, isOfferer, peerConnectionId }) {
2965
- if (!peerConnectionId) {
2966
- throw new Error("peerConnectionId is missing");
2967
- }
2963
+ _createSession({ clientId, initialBandwidth, isOfferer }) {
2964
+ var _a, _b;
2968
2965
  if (!clientId) {
2969
2966
  throw new Error("clientId is missing");
2970
2967
  }
@@ -2979,17 +2976,121 @@ class P2pRtcManager {
2979
2976
  browserVersion >= 14 &&
2980
2977
  this._features.deprioritizeH264OnSafari;
2981
2978
  const session = new Session({
2982
- peerConnectionId,
2983
2979
  clientId,
2984
2980
  peerConnectionConfig,
2985
2981
  bandwidth: initialBandwidth,
2986
2982
  deprioritizeH264Encoding,
2987
2983
  incrementAnalyticMetric: (metric) => this.analytics[metric]++,
2988
2984
  });
2989
- this.peerConnections[peerConnectionId] = session;
2985
+ this.peerConnections[clientId] = session;
2990
2986
  setTimeout(() => this._emit(rtcManagerEvents.NEW_PC), 0);
2991
2987
  this.analytics.numNewPc++;
2992
2988
  const { pc } = session;
2989
+ pc.onicegatheringstatechange = (event) => {
2990
+ const connection = event.target;
2991
+ switch (connection.iceGatheringState) {
2992
+ case "gathering":
2993
+ if (this._icePublicIPGatheringTimeoutID)
2994
+ clearTimeout(this._icePublicIPGatheringTimeoutID);
2995
+ this._icePublicIPGatheringTimeoutID = setTimeout(() => {
2996
+ if (!session.publicHostCandidateSeen &&
2997
+ !session.relayCandidateSeen &&
2998
+ !session.serverReflexiveCandidateSeen) {
2999
+ if (pc.iceConnectionState !== "connected" && pc.iceConnectionState !== "completed")
3000
+ this.analytics.numIceNoPublicIpGatheredIn3sec++;
3001
+ }
3002
+ }, ICE_PUBLIC_IP_GATHERING_TIMEOUT);
3003
+ break;
3004
+ case "complete":
3005
+ if (this._icePublicIPGatheringTimeoutID)
3006
+ clearTimeout(this._icePublicIPGatheringTimeoutID);
3007
+ this._icePublicIPGatheringTimeoutID = null;
3008
+ break;
3009
+ }
3010
+ };
3011
+ pc.onicecandidate = (event) => {
3012
+ if (event.candidate) {
3013
+ if (event.candidate.type === "relayed") {
3014
+ this.analytics.P2PRelayedIceCandidate++;
3015
+ }
3016
+ switch (event.candidate.type) {
3017
+ case "host":
3018
+ const address = event.candidate.address;
3019
+ if (!address) {
3020
+ break;
3021
+ }
3022
+ try {
3023
+ if (ipRegex.v4({ exact: true }).test(address)) {
3024
+ const ipv4 = checkIp(address);
3025
+ if (ipv4.isPublicIp)
3026
+ session.publicHostCandidateSeen = true;
3027
+ }
3028
+ else if (ipRegex.v6({ exact: true }).test(address.replace(/^\[(.*)\]/, "$1"))) {
3029
+ const ipv6 = new ipAddress.Address6(address.replace(/^\[(.*)\]/, "$1"));
3030
+ session.ipv6HostCandidateSeen = true;
3031
+ if (ipv6.getScope() === "Global") {
3032
+ session.publicHostCandidateSeen = true;
3033
+ }
3034
+ if (ipv6.isTeredo()) {
3035
+ session.ipv6HostCandidateTeredoSeen = true;
3036
+ }
3037
+ if (ipv6.is6to4()) {
3038
+ session.ipv6HostCandidate6to4Seen = true;
3039
+ }
3040
+ }
3041
+ else {
3042
+ const uuidv4 = address.replace(/.local/, "");
3043
+ if (uuidv4 && validate(uuidv4, 4)) {
3044
+ session.mdnsHostCandidateSeen = true;
3045
+ }
3046
+ }
3047
+ }
3048
+ catch (error) {
3049
+ logger$6.info("Error during parsing candidates! Error: ", { error });
3050
+ }
3051
+ break;
3052
+ case "srflx":
3053
+ session.serverReflexiveCandidateSeen = true;
3054
+ break;
3055
+ case "relayed":
3056
+ case "relay":
3057
+ session.relayCandidateSeen = true;
3058
+ break;
3059
+ }
3060
+ this._emitServerEvent(RELAY_MESSAGES.ICE_CANDIDATE, {
3061
+ receiverId: clientId,
3062
+ message: event.candidate,
3063
+ });
3064
+ }
3065
+ else {
3066
+ this._emitServerEvent(RELAY_MESSAGES.ICE_END_OF_CANDIDATES, {
3067
+ receiverId: clientId,
3068
+ });
3069
+ if (!session.publicHostCandidateSeen &&
3070
+ !session.relayCandidateSeen &&
3071
+ !session.serverReflexiveCandidateSeen &&
3072
+ pc.iceConnectionState !== "connected" &&
3073
+ pc.iceConnectionState !== "completed") {
3074
+ this.analytics.numIceNoPublicIpGathered++;
3075
+ }
3076
+ if (session.ipv6HostCandidateSeen) {
3077
+ this.analytics.numIceIpv6Seen++;
3078
+ if (session.ipv6HostCandidate6to4Seen)
3079
+ this.analytics.numIceIpv6SixToFour++;
3080
+ if (session.ipv6HostCandidateTeredoSeen)
3081
+ this.analytics.numIceIpv6TeredoSeen++;
3082
+ }
3083
+ if (session.mdnsHostCandidateSeen)
3084
+ this.analytics.numIceMdnsSeen++;
3085
+ }
3086
+ };
3087
+ pc.onnegotiationneeded = () => {
3088
+ if (pc.iceConnectionState === "new" || !session.connectionStatus) {
3089
+ return;
3090
+ }
3091
+ logger$6.info(`onnegotiationneeded client ${clientId}`);
3092
+ this._negotiatePeerConnection({ clientId, session });
3093
+ };
2993
3094
  pc.ontrack = (event) => {
2994
3095
  const stream = event.streams[0];
2995
3096
  if (!stream) {
@@ -3121,16 +3222,22 @@ class P2pRtcManager {
3121
3222
  });
3122
3223
  }
3123
3224
  }
3225
+ if (this._features.increaseIncomingMediaBufferOn) {
3226
+ this._setJitterBufferTarget(pc);
3227
+ }
3228
+ if (((_b = (_a = this._localCameraStream) === null || _a === void 0 ? void 0 : _a.getVideoTracks()) === null || _b === void 0 ? void 0 : _b.length) && this._stoppedVideoTrack) {
3229
+ pc.addTrack(this._stoppedVideoTrack, this._localCameraStream);
3230
+ }
3124
3231
  return session;
3125
3232
  }
3126
- _cleanup(peerConnectionId) {
3127
- const session = this._getSession(peerConnectionId);
3233
+ _cleanup(clientId) {
3234
+ const session = this._getSession(clientId);
3128
3235
  if (!session) {
3129
- logger$6.warn("No RTCPeerConnection in RTCManager.disconnect()", peerConnectionId);
3236
+ logger$6.warn("No RTCPeerConnection in RTCManager.disconnect()", clientId);
3130
3237
  return;
3131
3238
  }
3132
3239
  session.close();
3133
- delete this.peerConnections[peerConnectionId];
3240
+ delete this.peerConnections[clientId];
3134
3241
  }
3135
3242
  _forEachPeerConnection(func) {
3136
3243
  Object.values(this.peerConnections).forEach((peerConnection) => {
@@ -3231,7 +3338,7 @@ class P2pRtcManager {
3231
3338
  else {
3232
3339
  initialBandwidth = this._changeBandwidthForAllClients(true);
3233
3340
  }
3234
- session = this._createP2pSession({
3341
+ session = this._createSession({
3235
3342
  clientId,
3236
3343
  initialBandwidth,
3237
3344
  isOfferer: true,
@@ -3395,128 +3502,6 @@ class P2pRtcManager {
3395
3502
  });
3396
3503
  return bandwidth;
3397
3504
  }
3398
- _createP2pSession({ clientId, initialBandwidth, isOfferer = false, }) {
3399
- var _a, _b;
3400
- const session = this._createSession({
3401
- peerConnectionId: clientId,
3402
- clientId,
3403
- initialBandwidth,
3404
- isOfferer,
3405
- });
3406
- const pc = session.pc;
3407
- if (this._features.increaseIncomingMediaBufferOn) {
3408
- this._setJitterBufferTarget(pc);
3409
- }
3410
- if (((_b = (_a = this._localCameraStream) === null || _a === void 0 ? void 0 : _a.getVideoTracks()) === null || _b === void 0 ? void 0 : _b.length) && this._stoppedVideoTrack) {
3411
- pc.addTrack(this._stoppedVideoTrack, this._localCameraStream);
3412
- }
3413
- pc.onicegatheringstatechange = (event) => {
3414
- const connection = event.target;
3415
- switch (connection.iceGatheringState) {
3416
- case "gathering":
3417
- if (this._icePublicIPGatheringTimeoutID)
3418
- clearTimeout(this._icePublicIPGatheringTimeoutID);
3419
- this._icePublicIPGatheringTimeoutID = setTimeout(() => {
3420
- if (!session.publicHostCandidateSeen &&
3421
- !session.relayCandidateSeen &&
3422
- !session.serverReflexiveCandidateSeen) {
3423
- if (pc.iceConnectionState !== "connected" && pc.iceConnectionState !== "completed")
3424
- this.analytics.numIceNoPublicIpGatheredIn3sec++;
3425
- }
3426
- }, ICE_PUBLIC_IP_GATHERING_TIMEOUT);
3427
- break;
3428
- case "complete":
3429
- if (this._icePublicIPGatheringTimeoutID)
3430
- clearTimeout(this._icePublicIPGatheringTimeoutID);
3431
- this._icePublicIPGatheringTimeoutID = null;
3432
- break;
3433
- }
3434
- };
3435
- pc.onicecandidate = (event) => {
3436
- if (event.candidate) {
3437
- if (event.candidate.type === "relayed") {
3438
- this.analytics.P2PRelayedIceCandidate++;
3439
- }
3440
- switch (event.candidate.type) {
3441
- case "host":
3442
- const address = event.candidate.address;
3443
- if (!address) {
3444
- break;
3445
- }
3446
- try {
3447
- if (ipRegex.v4({ exact: true }).test(address)) {
3448
- const ipv4 = checkIp(address);
3449
- if (ipv4.isPublicIp)
3450
- session.publicHostCandidateSeen = true;
3451
- }
3452
- else if (ipRegex.v6({ exact: true }).test(address.replace(/^\[(.*)\]/, "$1"))) {
3453
- const ipv6 = new ipAddress.Address6(address.replace(/^\[(.*)\]/, "$1"));
3454
- session.ipv6HostCandidateSeen = true;
3455
- if (ipv6.getScope() === "Global") {
3456
- session.publicHostCandidateSeen = true;
3457
- }
3458
- if (ipv6.isTeredo()) {
3459
- session.ipv6HostCandidateTeredoSeen = true;
3460
- }
3461
- if (ipv6.is6to4()) {
3462
- session.ipv6HostCandidate6to4Seen = true;
3463
- }
3464
- }
3465
- else {
3466
- const uuidv4 = address.replace(/.local/, "");
3467
- if (uuidv4 && validate(uuidv4, 4)) {
3468
- session.mdnsHostCandidateSeen = true;
3469
- }
3470
- }
3471
- }
3472
- catch (error) {
3473
- logger$6.info("Error during parsing candidates! Error: ", { error });
3474
- }
3475
- break;
3476
- case "srflx":
3477
- session.serverReflexiveCandidateSeen = true;
3478
- break;
3479
- case "relayed":
3480
- case "relay":
3481
- session.relayCandidateSeen = true;
3482
- break;
3483
- }
3484
- this._emitServerEvent(RELAY_MESSAGES.ICE_CANDIDATE, {
3485
- receiverId: clientId,
3486
- message: event.candidate,
3487
- });
3488
- }
3489
- else {
3490
- this._emitServerEvent(RELAY_MESSAGES.ICE_END_OF_CANDIDATES, {
3491
- receiverId: clientId,
3492
- });
3493
- if (!session.publicHostCandidateSeen &&
3494
- !session.relayCandidateSeen &&
3495
- !session.serverReflexiveCandidateSeen &&
3496
- pc.iceConnectionState !== "connected" &&
3497
- pc.iceConnectionState !== "completed") {
3498
- this.analytics.numIceNoPublicIpGathered++;
3499
- }
3500
- if (session.ipv6HostCandidateSeen) {
3501
- this.analytics.numIceIpv6Seen++;
3502
- if (session.ipv6HostCandidate6to4Seen)
3503
- this.analytics.numIceIpv6SixToFour++;
3504
- if (session.ipv6HostCandidateTeredoSeen)
3505
- this.analytics.numIceIpv6TeredoSeen++;
3506
- }
3507
- if (session.mdnsHostCandidateSeen)
3508
- this.analytics.numIceMdnsSeen++;
3509
- }
3510
- };
3511
- pc.onnegotiationneeded = () => {
3512
- if (pc.iceConnectionState === "new" || !session.connectionStatus) {
3513
- return;
3514
- }
3515
- logger$6.info(`onnegotiationneeded client ${clientId}`);
3516
- this._negotiatePeerConnection({ clientId, session });
3517
- };
3518
- return session;
3519
- }
3520
3505
  acceptNewStream({ streamId, clientId }) {
3521
3506
  logger$6.info("acceptNewStream() [streamId: %s}, clientId: %s]", streamId, clientId);
3522
3507
  let session = this._getSession(clientId);
@@ -3530,7 +3515,7 @@ class P2pRtcManager {
3530
3515
  else {
3531
3516
  initialBandwidth = this._changeBandwidthForAllClients(true);
3532
3517
  }
3533
- session = this._createP2pSession({
3518
+ session = this._createSession({
3534
3519
  clientId,
3535
3520
  initialBandwidth,
3536
3521
  isOfferer: false,
package/dist/index.d.cts CHANGED
@@ -1188,7 +1188,6 @@ declare function getUpdatedDevices({ oldDevices, newDevices, currentAudioId, cur
1188
1188
  }): GetUpdatedDevicesResult;
1189
1189
 
1190
1190
  interface P2PSessionOptions {
1191
- peerConnectionId: string;
1192
1191
  clientId: string;
1193
1192
  bandwidth: number;
1194
1193
  peerConnectionConfig: RTCConfiguration;
@@ -1196,7 +1195,6 @@ interface P2PSessionOptions {
1196
1195
  incrementAnalyticMetric: P2PIncrementAnalyticMetric;
1197
1196
  }
1198
1197
  declare class Session {
1199
- peerConnectionId: any;
1200
1198
  relayCandidateSeen: boolean;
1201
1199
  serverReflexiveCandidateSeen: boolean;
1202
1200
  publicHostCandidateSeen: boolean;
@@ -1222,7 +1220,7 @@ declare class Session {
1222
1220
  srdComplete: any;
1223
1221
  _incrementAnalyticMetric: P2PIncrementAnalyticMetric;
1224
1222
  pendingReplaceTrackActions: (() => Promise<void>)[];
1225
- constructor({ peerConnectionId, clientId, bandwidth, peerConnectionConfig, deprioritizeH264Encoding, incrementAnalyticMetric, }: P2PSessionOptions);
1223
+ constructor({ clientId, bandwidth, peerConnectionConfig, deprioritizeH264Encoding, incrementAnalyticMetric, }: P2PSessionOptions);
1226
1224
  addStream(stream: MediaStream): void;
1227
1225
  addTrack(track: MediaStreamTrack, stream?: MediaStream): void;
1228
1226
  removeTrack(track: MediaStreamTrack): void;
@@ -1240,7 +1238,6 @@ declare class Session {
1240
1238
  }
1241
1239
 
1242
1240
  interface CreateSessionOptions {
1243
- peerConnectionId: string;
1244
1241
  clientId: string;
1245
1242
  initialBandwidth: number;
1246
1243
  isOfferer: boolean;
@@ -1342,9 +1339,9 @@ declare class P2pRtcManager implements RtcManager {
1342
1339
  _setJitterBufferTarget(pc: RTCPeerConnection): void;
1343
1340
  _emitServerEvent(eventName: string, data?: any): void;
1344
1341
  _emit(eventName: string, data?: any): void;
1345
- _getSession(peerConnectionId: string): Session | null;
1346
- _createSession({ clientId, initialBandwidth, isOfferer, peerConnectionId }: CreateSessionOptions): Session;
1347
- _cleanup(peerConnectionId: string): void;
1342
+ _getSession(clientId: string): Session | null;
1343
+ _createSession({ clientId, initialBandwidth, isOfferer }: CreateSessionOptions): Session;
1344
+ _cleanup(clientId: string): void;
1348
1345
  _forEachPeerConnection(func: any): void;
1349
1346
  _addStreamToPeerConnections(stream: MediaStream): void;
1350
1347
  _addTrackToPeerConnections(track: MediaStreamTrack, stream?: MediaStream): void;
@@ -1361,11 +1358,6 @@ declare class P2pRtcManager implements RtcManager {
1361
1358
  _negotiatePeerConnection({ clientId, session, constraints, isInitialOffer, }: NegotiatePeerConnectionOptions): void;
1362
1359
  _withForcedRenegotiation(session: Session, action: any): void;
1363
1360
  _changeBandwidthForAllClients(isJoining: boolean): number;
1364
- _createP2pSession({ clientId, initialBandwidth, isOfferer, }: {
1365
- clientId: string;
1366
- initialBandwidth: number;
1367
- isOfferer: boolean;
1368
- }): Session;
1369
1361
  acceptNewStream({ streamId, clientId }: {
1370
1362
  streamId: string;
1371
1363
  clientId: string;
package/dist/index.d.mts CHANGED
@@ -1188,7 +1188,6 @@ declare function getUpdatedDevices({ oldDevices, newDevices, currentAudioId, cur
1188
1188
  }): GetUpdatedDevicesResult;
1189
1189
 
1190
1190
  interface P2PSessionOptions {
1191
- peerConnectionId: string;
1192
1191
  clientId: string;
1193
1192
  bandwidth: number;
1194
1193
  peerConnectionConfig: RTCConfiguration;
@@ -1196,7 +1195,6 @@ interface P2PSessionOptions {
1196
1195
  incrementAnalyticMetric: P2PIncrementAnalyticMetric;
1197
1196
  }
1198
1197
  declare class Session {
1199
- peerConnectionId: any;
1200
1198
  relayCandidateSeen: boolean;
1201
1199
  serverReflexiveCandidateSeen: boolean;
1202
1200
  publicHostCandidateSeen: boolean;
@@ -1222,7 +1220,7 @@ declare class Session {
1222
1220
  srdComplete: any;
1223
1221
  _incrementAnalyticMetric: P2PIncrementAnalyticMetric;
1224
1222
  pendingReplaceTrackActions: (() => Promise<void>)[];
1225
- constructor({ peerConnectionId, clientId, bandwidth, peerConnectionConfig, deprioritizeH264Encoding, incrementAnalyticMetric, }: P2PSessionOptions);
1223
+ constructor({ clientId, bandwidth, peerConnectionConfig, deprioritizeH264Encoding, incrementAnalyticMetric, }: P2PSessionOptions);
1226
1224
  addStream(stream: MediaStream): void;
1227
1225
  addTrack(track: MediaStreamTrack, stream?: MediaStream): void;
1228
1226
  removeTrack(track: MediaStreamTrack): void;
@@ -1240,7 +1238,6 @@ declare class Session {
1240
1238
  }
1241
1239
 
1242
1240
  interface CreateSessionOptions {
1243
- peerConnectionId: string;
1244
1241
  clientId: string;
1245
1242
  initialBandwidth: number;
1246
1243
  isOfferer: boolean;
@@ -1342,9 +1339,9 @@ declare class P2pRtcManager implements RtcManager {
1342
1339
  _setJitterBufferTarget(pc: RTCPeerConnection): void;
1343
1340
  _emitServerEvent(eventName: string, data?: any): void;
1344
1341
  _emit(eventName: string, data?: any): void;
1345
- _getSession(peerConnectionId: string): Session | null;
1346
- _createSession({ clientId, initialBandwidth, isOfferer, peerConnectionId }: CreateSessionOptions): Session;
1347
- _cleanup(peerConnectionId: string): void;
1342
+ _getSession(clientId: string): Session | null;
1343
+ _createSession({ clientId, initialBandwidth, isOfferer }: CreateSessionOptions): Session;
1344
+ _cleanup(clientId: string): void;
1348
1345
  _forEachPeerConnection(func: any): void;
1349
1346
  _addStreamToPeerConnections(stream: MediaStream): void;
1350
1347
  _addTrackToPeerConnections(track: MediaStreamTrack, stream?: MediaStream): void;
@@ -1361,11 +1358,6 @@ declare class P2pRtcManager implements RtcManager {
1361
1358
  _negotiatePeerConnection({ clientId, session, constraints, isInitialOffer, }: NegotiatePeerConnectionOptions): void;
1362
1359
  _withForcedRenegotiation(session: Session, action: any): void;
1363
1360
  _changeBandwidthForAllClients(isJoining: boolean): number;
1364
- _createP2pSession({ clientId, initialBandwidth, isOfferer, }: {
1365
- clientId: string;
1366
- initialBandwidth: number;
1367
- isOfferer: boolean;
1368
- }): Session;
1369
1361
  acceptNewStream({ streamId, clientId }: {
1370
1362
  streamId: string;
1371
1363
  clientId: string;
package/dist/index.d.ts CHANGED
@@ -1188,7 +1188,6 @@ declare function getUpdatedDevices({ oldDevices, newDevices, currentAudioId, cur
1188
1188
  }): GetUpdatedDevicesResult;
1189
1189
 
1190
1190
  interface P2PSessionOptions {
1191
- peerConnectionId: string;
1192
1191
  clientId: string;
1193
1192
  bandwidth: number;
1194
1193
  peerConnectionConfig: RTCConfiguration;
@@ -1196,7 +1195,6 @@ interface P2PSessionOptions {
1196
1195
  incrementAnalyticMetric: P2PIncrementAnalyticMetric;
1197
1196
  }
1198
1197
  declare class Session {
1199
- peerConnectionId: any;
1200
1198
  relayCandidateSeen: boolean;
1201
1199
  serverReflexiveCandidateSeen: boolean;
1202
1200
  publicHostCandidateSeen: boolean;
@@ -1222,7 +1220,7 @@ declare class Session {
1222
1220
  srdComplete: any;
1223
1221
  _incrementAnalyticMetric: P2PIncrementAnalyticMetric;
1224
1222
  pendingReplaceTrackActions: (() => Promise<void>)[];
1225
- constructor({ peerConnectionId, clientId, bandwidth, peerConnectionConfig, deprioritizeH264Encoding, incrementAnalyticMetric, }: P2PSessionOptions);
1223
+ constructor({ clientId, bandwidth, peerConnectionConfig, deprioritizeH264Encoding, incrementAnalyticMetric, }: P2PSessionOptions);
1226
1224
  addStream(stream: MediaStream): void;
1227
1225
  addTrack(track: MediaStreamTrack, stream?: MediaStream): void;
1228
1226
  removeTrack(track: MediaStreamTrack): void;
@@ -1240,7 +1238,6 @@ declare class Session {
1240
1238
  }
1241
1239
 
1242
1240
  interface CreateSessionOptions {
1243
- peerConnectionId: string;
1244
1241
  clientId: string;
1245
1242
  initialBandwidth: number;
1246
1243
  isOfferer: boolean;
@@ -1342,9 +1339,9 @@ declare class P2pRtcManager implements RtcManager {
1342
1339
  _setJitterBufferTarget(pc: RTCPeerConnection): void;
1343
1340
  _emitServerEvent(eventName: string, data?: any): void;
1344
1341
  _emit(eventName: string, data?: any): void;
1345
- _getSession(peerConnectionId: string): Session | null;
1346
- _createSession({ clientId, initialBandwidth, isOfferer, peerConnectionId }: CreateSessionOptions): Session;
1347
- _cleanup(peerConnectionId: string): void;
1342
+ _getSession(clientId: string): Session | null;
1343
+ _createSession({ clientId, initialBandwidth, isOfferer }: CreateSessionOptions): Session;
1344
+ _cleanup(clientId: string): void;
1348
1345
  _forEachPeerConnection(func: any): void;
1349
1346
  _addStreamToPeerConnections(stream: MediaStream): void;
1350
1347
  _addTrackToPeerConnections(track: MediaStreamTrack, stream?: MediaStream): void;
@@ -1361,11 +1358,6 @@ declare class P2pRtcManager implements RtcManager {
1361
1358
  _negotiatePeerConnection({ clientId, session, constraints, isInitialOffer, }: NegotiatePeerConnectionOptions): void;
1362
1359
  _withForcedRenegotiation(session: Session, action: any): void;
1363
1360
  _changeBandwidthForAllClients(isJoining: boolean): number;
1364
- _createP2pSession({ clientId, initialBandwidth, isOfferer, }: {
1365
- clientId: string;
1366
- initialBandwidth: number;
1367
- isOfferer: boolean;
1368
- }): Session;
1369
1361
  acceptNewStream({ streamId, clientId }: {
1370
1362
  streamId: string;
1371
1363
  clientId: string;
package/dist/index.mjs CHANGED
@@ -2196,8 +2196,7 @@ var _a$4;
2196
2196
  const adapter$4 = (_a$4 = adapterRaw.default) !== null && _a$4 !== void 0 ? _a$4 : adapterRaw;
2197
2197
  const logger$7 = new Logger();
2198
2198
  class Session {
2199
- constructor({ peerConnectionId, clientId, bandwidth, peerConnectionConfig, deprioritizeH264Encoding, incrementAnalyticMetric, }) {
2200
- this.peerConnectionId = peerConnectionId;
2199
+ constructor({ clientId, bandwidth, peerConnectionConfig, deprioritizeH264Encoding, incrementAnalyticMetric, }) {
2201
2200
  this.relayCandidateSeen = false;
2202
2201
  this.serverReflexiveCandidateSeen = false;
2203
2202
  this.publicHostCandidateSeen = false;
@@ -2734,8 +2733,8 @@ class P2pRtcManager {
2734
2733
  }
2735
2734
  disconnectAll() {
2736
2735
  logger$6.info("disconnectAll()");
2737
- Object.keys(this.peerConnections).forEach((peerConnectionId) => {
2738
- this.disconnect(peerConnectionId);
2736
+ Object.keys(this.peerConnections).forEach((clientId) => {
2737
+ this.disconnect(clientId);
2739
2738
  });
2740
2739
  this.peerConnections = {};
2741
2740
  this._socketListenerDeregisterFunctions.forEach((func) => {
@@ -2934,16 +2933,14 @@ class P2pRtcManager {
2934
2933
  _emit(eventName, data) {
2935
2934
  this._emitter.emit(eventName, data);
2936
2935
  }
2937
- _getSession(peerConnectionId) {
2938
- if (!(peerConnectionId in this.peerConnections)) {
2936
+ _getSession(clientId) {
2937
+ if (!(clientId in this.peerConnections)) {
2939
2938
  return null;
2940
2939
  }
2941
- return this.peerConnections[peerConnectionId];
2940
+ return this.peerConnections[clientId];
2942
2941
  }
2943
- _createSession({ clientId, initialBandwidth, isOfferer, peerConnectionId }) {
2944
- if (!peerConnectionId) {
2945
- throw new Error("peerConnectionId is missing");
2946
- }
2942
+ _createSession({ clientId, initialBandwidth, isOfferer }) {
2943
+ var _a, _b;
2947
2944
  if (!clientId) {
2948
2945
  throw new Error("clientId is missing");
2949
2946
  }
@@ -2958,17 +2955,121 @@ class P2pRtcManager {
2958
2955
  browserVersion >= 14 &&
2959
2956
  this._features.deprioritizeH264OnSafari;
2960
2957
  const session = new Session({
2961
- peerConnectionId,
2962
2958
  clientId,
2963
2959
  peerConnectionConfig,
2964
2960
  bandwidth: initialBandwidth,
2965
2961
  deprioritizeH264Encoding,
2966
2962
  incrementAnalyticMetric: (metric) => this.analytics[metric]++,
2967
2963
  });
2968
- this.peerConnections[peerConnectionId] = session;
2964
+ this.peerConnections[clientId] = session;
2969
2965
  setTimeout(() => this._emit(rtcManagerEvents.NEW_PC), 0);
2970
2966
  this.analytics.numNewPc++;
2971
2967
  const { pc } = session;
2968
+ pc.onicegatheringstatechange = (event) => {
2969
+ const connection = event.target;
2970
+ switch (connection.iceGatheringState) {
2971
+ case "gathering":
2972
+ if (this._icePublicIPGatheringTimeoutID)
2973
+ clearTimeout(this._icePublicIPGatheringTimeoutID);
2974
+ this._icePublicIPGatheringTimeoutID = setTimeout(() => {
2975
+ if (!session.publicHostCandidateSeen &&
2976
+ !session.relayCandidateSeen &&
2977
+ !session.serverReflexiveCandidateSeen) {
2978
+ if (pc.iceConnectionState !== "connected" && pc.iceConnectionState !== "completed")
2979
+ this.analytics.numIceNoPublicIpGatheredIn3sec++;
2980
+ }
2981
+ }, ICE_PUBLIC_IP_GATHERING_TIMEOUT);
2982
+ break;
2983
+ case "complete":
2984
+ if (this._icePublicIPGatheringTimeoutID)
2985
+ clearTimeout(this._icePublicIPGatheringTimeoutID);
2986
+ this._icePublicIPGatheringTimeoutID = null;
2987
+ break;
2988
+ }
2989
+ };
2990
+ pc.onicecandidate = (event) => {
2991
+ if (event.candidate) {
2992
+ if (event.candidate.type === "relayed") {
2993
+ this.analytics.P2PRelayedIceCandidate++;
2994
+ }
2995
+ switch (event.candidate.type) {
2996
+ case "host":
2997
+ const address = event.candidate.address;
2998
+ if (!address) {
2999
+ break;
3000
+ }
3001
+ try {
3002
+ if (ipRegex.v4({ exact: true }).test(address)) {
3003
+ const ipv4 = checkIp(address);
3004
+ if (ipv4.isPublicIp)
3005
+ session.publicHostCandidateSeen = true;
3006
+ }
3007
+ else if (ipRegex.v6({ exact: true }).test(address.replace(/^\[(.*)\]/, "$1"))) {
3008
+ const ipv6 = new Address6(address.replace(/^\[(.*)\]/, "$1"));
3009
+ session.ipv6HostCandidateSeen = true;
3010
+ if (ipv6.getScope() === "Global") {
3011
+ session.publicHostCandidateSeen = true;
3012
+ }
3013
+ if (ipv6.isTeredo()) {
3014
+ session.ipv6HostCandidateTeredoSeen = true;
3015
+ }
3016
+ if (ipv6.is6to4()) {
3017
+ session.ipv6HostCandidate6to4Seen = true;
3018
+ }
3019
+ }
3020
+ else {
3021
+ const uuidv4 = address.replace(/.local/, "");
3022
+ if (uuidv4 && validate(uuidv4, 4)) {
3023
+ session.mdnsHostCandidateSeen = true;
3024
+ }
3025
+ }
3026
+ }
3027
+ catch (error) {
3028
+ logger$6.info("Error during parsing candidates! Error: ", { error });
3029
+ }
3030
+ break;
3031
+ case "srflx":
3032
+ session.serverReflexiveCandidateSeen = true;
3033
+ break;
3034
+ case "relayed":
3035
+ case "relay":
3036
+ session.relayCandidateSeen = true;
3037
+ break;
3038
+ }
3039
+ this._emitServerEvent(RELAY_MESSAGES.ICE_CANDIDATE, {
3040
+ receiverId: clientId,
3041
+ message: event.candidate,
3042
+ });
3043
+ }
3044
+ else {
3045
+ this._emitServerEvent(RELAY_MESSAGES.ICE_END_OF_CANDIDATES, {
3046
+ receiverId: clientId,
3047
+ });
3048
+ if (!session.publicHostCandidateSeen &&
3049
+ !session.relayCandidateSeen &&
3050
+ !session.serverReflexiveCandidateSeen &&
3051
+ pc.iceConnectionState !== "connected" &&
3052
+ pc.iceConnectionState !== "completed") {
3053
+ this.analytics.numIceNoPublicIpGathered++;
3054
+ }
3055
+ if (session.ipv6HostCandidateSeen) {
3056
+ this.analytics.numIceIpv6Seen++;
3057
+ if (session.ipv6HostCandidate6to4Seen)
3058
+ this.analytics.numIceIpv6SixToFour++;
3059
+ if (session.ipv6HostCandidateTeredoSeen)
3060
+ this.analytics.numIceIpv6TeredoSeen++;
3061
+ }
3062
+ if (session.mdnsHostCandidateSeen)
3063
+ this.analytics.numIceMdnsSeen++;
3064
+ }
3065
+ };
3066
+ pc.onnegotiationneeded = () => {
3067
+ if (pc.iceConnectionState === "new" || !session.connectionStatus) {
3068
+ return;
3069
+ }
3070
+ logger$6.info(`onnegotiationneeded client ${clientId}`);
3071
+ this._negotiatePeerConnection({ clientId, session });
3072
+ };
2972
3073
  pc.ontrack = (event) => {
2973
3074
  const stream = event.streams[0];
2974
3075
  if (!stream) {
@@ -3100,16 +3201,22 @@ class P2pRtcManager {
3100
3201
  });
3101
3202
  }
3102
3203
  }
3204
+ if (this._features.increaseIncomingMediaBufferOn) {
3205
+ this._setJitterBufferTarget(pc);
3206
+ }
3207
+ if (((_b = (_a = this._localCameraStream) === null || _a === void 0 ? void 0 : _a.getVideoTracks()) === null || _b === void 0 ? void 0 : _b.length) && this._stoppedVideoTrack) {
3208
+ pc.addTrack(this._stoppedVideoTrack, this._localCameraStream);
3209
+ }
3103
3210
  return session;
3104
3211
  }
3105
- _cleanup(peerConnectionId) {
3106
- const session = this._getSession(peerConnectionId);
3212
+ _cleanup(clientId) {
3213
+ const session = this._getSession(clientId);
3107
3214
  if (!session) {
3108
- logger$6.warn("No RTCPeerConnection in RTCManager.disconnect()", peerConnectionId);
3215
+ logger$6.warn("No RTCPeerConnection in RTCManager.disconnect()", clientId);
3109
3216
  return;
3110
3217
  }
3111
3218
  session.close();
3112
- delete this.peerConnections[peerConnectionId];
3219
+ delete this.peerConnections[clientId];
3113
3220
  }
3114
3221
  _forEachPeerConnection(func) {
3115
3222
  Object.values(this.peerConnections).forEach((peerConnection) => {
@@ -3210,7 +3317,7 @@ class P2pRtcManager {
3210
3317
  else {
3211
3318
  initialBandwidth = this._changeBandwidthForAllClients(true);
3212
3319
  }
3213
- session = this._createP2pSession({
3320
+ session = this._createSession({
3214
3321
  clientId,
3215
3322
  initialBandwidth,
3216
3323
  isOfferer: true,
@@ -3374,128 +3481,6 @@ class P2pRtcManager {
3374
3481
  });
3375
3482
  return bandwidth;
3376
3483
  }
3377
- _createP2pSession({ clientId, initialBandwidth, isOfferer = false, }) {
3378
- var _a, _b;
3379
- const session = this._createSession({
3380
- peerConnectionId: clientId,
3381
- clientId,
3382
- initialBandwidth,
3383
- isOfferer,
3384
- });
3385
- const pc = session.pc;
3386
- if (this._features.increaseIncomingMediaBufferOn) {
3387
- this._setJitterBufferTarget(pc);
3388
- }
3389
- if (((_b = (_a = this._localCameraStream) === null || _a === void 0 ? void 0 : _a.getVideoTracks()) === null || _b === void 0 ? void 0 : _b.length) && this._stoppedVideoTrack) {
3390
- pc.addTrack(this._stoppedVideoTrack, this._localCameraStream);
3391
- }
3392
- pc.onicegatheringstatechange = (event) => {
3393
- const connection = event.target;
3394
- switch (connection.iceGatheringState) {
3395
- case "gathering":
3396
- if (this._icePublicIPGatheringTimeoutID)
3397
- clearTimeout(this._icePublicIPGatheringTimeoutID);
3398
- this._icePublicIPGatheringTimeoutID = setTimeout(() => {
3399
- if (!session.publicHostCandidateSeen &&
3400
- !session.relayCandidateSeen &&
3401
- !session.serverReflexiveCandidateSeen) {
3402
- if (pc.iceConnectionState !== "connected" && pc.iceConnectionState !== "completed")
3403
- this.analytics.numIceNoPublicIpGatheredIn3sec++;
3404
- }
3405
- }, ICE_PUBLIC_IP_GATHERING_TIMEOUT);
3406
- break;
3407
- case "complete":
3408
- if (this._icePublicIPGatheringTimeoutID)
3409
- clearTimeout(this._icePublicIPGatheringTimeoutID);
3410
- this._icePublicIPGatheringTimeoutID = null;
3411
- break;
3412
- }
3413
- };
3414
- pc.onicecandidate = (event) => {
3415
- if (event.candidate) {
3416
- if (event.candidate.type === "relayed") {
3417
- this.analytics.P2PRelayedIceCandidate++;
3418
- }
3419
- switch (event.candidate.type) {
3420
- case "host":
3421
- const address = event.candidate.address;
3422
- if (!address) {
3423
- break;
3424
- }
3425
- try {
3426
- if (ipRegex.v4({ exact: true }).test(address)) {
3427
- const ipv4 = checkIp(address);
3428
- if (ipv4.isPublicIp)
3429
- session.publicHostCandidateSeen = true;
3430
- }
3431
- else if (ipRegex.v6({ exact: true }).test(address.replace(/^\[(.*)\]/, "$1"))) {
3432
- const ipv6 = new Address6(address.replace(/^\[(.*)\]/, "$1"));
3433
- session.ipv6HostCandidateSeen = true;
3434
- if (ipv6.getScope() === "Global") {
3435
- session.publicHostCandidateSeen = true;
3436
- }
3437
- if (ipv6.isTeredo()) {
3438
- session.ipv6HostCandidateTeredoSeen = true;
3439
- }
3440
- if (ipv6.is6to4()) {
3441
- session.ipv6HostCandidate6to4Seen = true;
3442
- }
3443
- }
3444
- else {
3445
- const uuidv4 = address.replace(/.local/, "");
3446
- if (uuidv4 && validate(uuidv4, 4)) {
3447
- session.mdnsHostCandidateSeen = true;
3448
- }
3449
- }
3450
- }
3451
- catch (error) {
3452
- logger$6.info("Error during parsing candidates! Error: ", { error });
3453
- }
3454
- break;
3455
- case "srflx":
3456
- session.serverReflexiveCandidateSeen = true;
3457
- break;
3458
- case "relayed":
3459
- case "relay":
3460
- session.relayCandidateSeen = true;
3461
- break;
3462
- }
3463
- this._emitServerEvent(RELAY_MESSAGES.ICE_CANDIDATE, {
3464
- receiverId: clientId,
3465
- message: event.candidate,
3466
- });
3467
- }
3468
- else {
3469
- this._emitServerEvent(RELAY_MESSAGES.ICE_END_OF_CANDIDATES, {
3470
- receiverId: clientId,
3471
- });
3472
- if (!session.publicHostCandidateSeen &&
3473
- !session.relayCandidateSeen &&
3474
- !session.serverReflexiveCandidateSeen &&
3475
- pc.iceConnectionState !== "connected" &&
3476
- pc.iceConnectionState !== "completed") {
3477
- this.analytics.numIceNoPublicIpGathered++;
3478
- }
3479
- if (session.ipv6HostCandidateSeen) {
3480
- this.analytics.numIceIpv6Seen++;
3481
- if (session.ipv6HostCandidate6to4Seen)
3482
- this.analytics.numIceIpv6SixToFour++;
3483
- if (session.ipv6HostCandidateTeredoSeen)
3484
- this.analytics.numIceIpv6TeredoSeen++;
3485
- }
3486
- if (session.mdnsHostCandidateSeen)
3487
- this.analytics.numIceMdnsSeen++;
3488
- }
3489
- };
3490
- pc.onnegotiationneeded = () => {
3491
- if (pc.iceConnectionState === "new" || !session.connectionStatus) {
3492
- return;
3493
- }
3494
- logger$6.info(`onnegotiationneeded client ${clientId}`);
3495
- this._negotiatePeerConnection({ clientId, session });
3496
- };
3497
- return session;
3498
- }
3499
3484
  acceptNewStream({ streamId, clientId }) {
3500
3485
  logger$6.info("acceptNewStream() [streamId: %s}, clientId: %s]", streamId, clientId);
3501
3486
  let session = this._getSession(clientId);
@@ -3509,7 +3494,7 @@ class P2pRtcManager {
3509
3494
  else {
3510
3495
  initialBandwidth = this._changeBandwidthForAllClients(true);
3511
3496
  }
3512
- session = this._createP2pSession({
3497
+ session = this._createSession({
3513
3498
  clientId,
3514
3499
  initialBandwidth,
3515
3500
  isOfferer: false,
@@ -2196,8 +2196,7 @@ var _a$4;
2196
2196
  const adapter$4 = (_a$4 = adapterRaw.default) !== null && _a$4 !== void 0 ? _a$4 : adapterRaw;
2197
2197
  const logger$7 = new Logger();
2198
2198
  class Session {
2199
- constructor({ peerConnectionId, clientId, bandwidth, peerConnectionConfig, deprioritizeH264Encoding, incrementAnalyticMetric, }) {
2200
- this.peerConnectionId = peerConnectionId;
2199
+ constructor({ clientId, bandwidth, peerConnectionConfig, deprioritizeH264Encoding, incrementAnalyticMetric, }) {
2201
2200
  this.relayCandidateSeen = false;
2202
2201
  this.serverReflexiveCandidateSeen = false;
2203
2202
  this.publicHostCandidateSeen = false;
@@ -2734,8 +2733,8 @@ class P2pRtcManager {
2734
2733
  }
2735
2734
  disconnectAll() {
2736
2735
  logger$6.info("disconnectAll()");
2737
- Object.keys(this.peerConnections).forEach((peerConnectionId) => {
2738
- this.disconnect(peerConnectionId);
2736
+ Object.keys(this.peerConnections).forEach((clientId) => {
2737
+ this.disconnect(clientId);
2739
2738
  });
2740
2739
  this.peerConnections = {};
2741
2740
  this._socketListenerDeregisterFunctions.forEach((func) => {
@@ -2934,16 +2933,14 @@ class P2pRtcManager {
2934
2933
  _emit(eventName, data) {
2935
2934
  this._emitter.emit(eventName, data);
2936
2935
  }
2937
- _getSession(peerConnectionId) {
2938
- if (!(peerConnectionId in this.peerConnections)) {
2936
+ _getSession(clientId) {
2937
+ if (!(clientId in this.peerConnections)) {
2939
2938
  return null;
2940
2939
  }
2941
- return this.peerConnections[peerConnectionId];
2940
+ return this.peerConnections[clientId];
2942
2941
  }
2943
- _createSession({ clientId, initialBandwidth, isOfferer, peerConnectionId }) {
2944
- if (!peerConnectionId) {
2945
- throw new Error("peerConnectionId is missing");
2946
- }
2942
+ _createSession({ clientId, initialBandwidth, isOfferer }) {
2943
+ var _a, _b;
2947
2944
  if (!clientId) {
2948
2945
  throw new Error("clientId is missing");
2949
2946
  }
@@ -2958,17 +2955,121 @@ class P2pRtcManager {
2958
2955
  browserVersion >= 14 &&
2959
2956
  this._features.deprioritizeH264OnSafari;
2960
2957
  const session = new Session({
2961
- peerConnectionId,
2962
2958
  clientId,
2963
2959
  peerConnectionConfig,
2964
2960
  bandwidth: initialBandwidth,
2965
2961
  deprioritizeH264Encoding,
2966
2962
  incrementAnalyticMetric: (metric) => this.analytics[metric]++,
2967
2963
  });
2968
- this.peerConnections[peerConnectionId] = session;
2964
+ this.peerConnections[clientId] = session;
2969
2965
  setTimeout(() => this._emit(rtcManagerEvents.NEW_PC), 0);
2970
2966
  this.analytics.numNewPc++;
2971
2967
  const { pc } = session;
2968
+ pc.onicegatheringstatechange = (event) => {
2969
+ const connection = event.target;
2970
+ switch (connection.iceGatheringState) {
2971
+ case "gathering":
2972
+ if (this._icePublicIPGatheringTimeoutID)
2973
+ clearTimeout(this._icePublicIPGatheringTimeoutID);
2974
+ this._icePublicIPGatheringTimeoutID = setTimeout(() => {
2975
+ if (!session.publicHostCandidateSeen &&
2976
+ !session.relayCandidateSeen &&
2977
+ !session.serverReflexiveCandidateSeen) {
2978
+ if (pc.iceConnectionState !== "connected" && pc.iceConnectionState !== "completed")
2979
+ this.analytics.numIceNoPublicIpGatheredIn3sec++;
2980
+ }
2981
+ }, ICE_PUBLIC_IP_GATHERING_TIMEOUT);
2982
+ break;
2983
+ case "complete":
2984
+ if (this._icePublicIPGatheringTimeoutID)
2985
+ clearTimeout(this._icePublicIPGatheringTimeoutID);
2986
+ this._icePublicIPGatheringTimeoutID = null;
2987
+ break;
2988
+ }
2989
+ };
2990
+ pc.onicecandidate = (event) => {
2991
+ if (event.candidate) {
2992
+ if (event.candidate.type === "relayed") {
2993
+ this.analytics.P2PRelayedIceCandidate++;
2994
+ }
2995
+ switch (event.candidate.type) {
2996
+ case "host":
2997
+ const address = event.candidate.address;
2998
+ if (!address) {
2999
+ break;
3000
+ }
3001
+ try {
3002
+ if (ipRegex.v4({ exact: true }).test(address)) {
3003
+ const ipv4 = checkIp(address);
3004
+ if (ipv4.isPublicIp)
3005
+ session.publicHostCandidateSeen = true;
3006
+ }
3007
+ else if (ipRegex.v6({ exact: true }).test(address.replace(/^\[(.*)\]/, "$1"))) {
3008
+ const ipv6 = new Address6(address.replace(/^\[(.*)\]/, "$1"));
3009
+ session.ipv6HostCandidateSeen = true;
3010
+ if (ipv6.getScope() === "Global") {
3011
+ session.publicHostCandidateSeen = true;
3012
+ }
3013
+ if (ipv6.isTeredo()) {
3014
+ session.ipv6HostCandidateTeredoSeen = true;
3015
+ }
3016
+ if (ipv6.is6to4()) {
3017
+ session.ipv6HostCandidate6to4Seen = true;
3018
+ }
3019
+ }
3020
+ else {
3021
+ const uuidv4 = address.replace(/.local/, "");
3022
+ if (uuidv4 && validate(uuidv4, 4)) {
3023
+ session.mdnsHostCandidateSeen = true;
3024
+ }
3025
+ }
3026
+ }
3027
+ catch (error) {
3028
+ logger$6.info("Error during parsing candidates! Error: ", { error });
3029
+ }
3030
+ break;
3031
+ case "srflx":
3032
+ session.serverReflexiveCandidateSeen = true;
3033
+ break;
3034
+ case "relayed":
3035
+ case "relay":
3036
+ session.relayCandidateSeen = true;
3037
+ break;
3038
+ }
3039
+ this._emitServerEvent(RELAY_MESSAGES.ICE_CANDIDATE, {
3040
+ receiverId: clientId,
3041
+ message: event.candidate,
3042
+ });
3043
+ }
3044
+ else {
3045
+ this._emitServerEvent(RELAY_MESSAGES.ICE_END_OF_CANDIDATES, {
3046
+ receiverId: clientId,
3047
+ });
3048
+ if (!session.publicHostCandidateSeen &&
3049
+ !session.relayCandidateSeen &&
3050
+ !session.serverReflexiveCandidateSeen &&
3051
+ pc.iceConnectionState !== "connected" &&
3052
+ pc.iceConnectionState !== "completed") {
3053
+ this.analytics.numIceNoPublicIpGathered++;
3054
+ }
3055
+ if (session.ipv6HostCandidateSeen) {
3056
+ this.analytics.numIceIpv6Seen++;
3057
+ if (session.ipv6HostCandidate6to4Seen)
3058
+ this.analytics.numIceIpv6SixToFour++;
3059
+ if (session.ipv6HostCandidateTeredoSeen)
3060
+ this.analytics.numIceIpv6TeredoSeen++;
3061
+ }
3062
+ if (session.mdnsHostCandidateSeen)
3063
+ this.analytics.numIceMdnsSeen++;
3064
+ }
3065
+ };
3066
+ pc.onnegotiationneeded = () => {
3067
+ if (pc.iceConnectionState === "new" || !session.connectionStatus) {
3068
+ return;
3069
+ }
3070
+ logger$6.info(`onnegotiationneeded client ${clientId}`);
3071
+ this._negotiatePeerConnection({ clientId, session });
3072
+ };
2972
3073
  pc.ontrack = (event) => {
2973
3074
  const stream = event.streams[0];
2974
3075
  if (!stream) {
@@ -3100,16 +3201,22 @@ class P2pRtcManager {
3100
3201
  });
3101
3202
  }
3102
3203
  }
3204
+ if (this._features.increaseIncomingMediaBufferOn) {
3205
+ this._setJitterBufferTarget(pc);
3206
+ }
3207
+ if (((_b = (_a = this._localCameraStream) === null || _a === void 0 ? void 0 : _a.getVideoTracks()) === null || _b === void 0 ? void 0 : _b.length) && this._stoppedVideoTrack) {
3208
+ pc.addTrack(this._stoppedVideoTrack, this._localCameraStream);
3209
+ }
3103
3210
  return session;
3104
3211
  }
3105
- _cleanup(peerConnectionId) {
3106
- const session = this._getSession(peerConnectionId);
3212
+ _cleanup(clientId) {
3213
+ const session = this._getSession(clientId);
3107
3214
  if (!session) {
3108
- logger$6.warn("No RTCPeerConnection in RTCManager.disconnect()", peerConnectionId);
3215
+ logger$6.warn("No RTCPeerConnection in RTCManager.disconnect()", clientId);
3109
3216
  return;
3110
3217
  }
3111
3218
  session.close();
3112
- delete this.peerConnections[peerConnectionId];
3219
+ delete this.peerConnections[clientId];
3113
3220
  }
3114
3221
  _forEachPeerConnection(func) {
3115
3222
  Object.values(this.peerConnections).forEach((peerConnection) => {
@@ -3210,7 +3317,7 @@ class P2pRtcManager {
3210
3317
  else {
3211
3318
  initialBandwidth = this._changeBandwidthForAllClients(true);
3212
3319
  }
3213
- session = this._createP2pSession({
3320
+ session = this._createSession({
3214
3321
  clientId,
3215
3322
  initialBandwidth,
3216
3323
  isOfferer: true,
@@ -3374,128 +3481,6 @@ class P2pRtcManager {
3374
3481
  });
3375
3482
  return bandwidth;
3376
3483
  }
3377
- _createP2pSession({ clientId, initialBandwidth, isOfferer = false, }) {
3378
- var _a, _b;
3379
- const session = this._createSession({
3380
- peerConnectionId: clientId,
3381
- clientId,
3382
- initialBandwidth,
3383
- isOfferer,
3384
- });
3385
- const pc = session.pc;
3386
- if (this._features.increaseIncomingMediaBufferOn) {
3387
- this._setJitterBufferTarget(pc);
3388
- }
3389
- if (((_b = (_a = this._localCameraStream) === null || _a === void 0 ? void 0 : _a.getVideoTracks()) === null || _b === void 0 ? void 0 : _b.length) && this._stoppedVideoTrack) {
3390
- pc.addTrack(this._stoppedVideoTrack, this._localCameraStream);
3391
- }
3392
- pc.onicegatheringstatechange = (event) => {
3393
- const connection = event.target;
3394
- switch (connection.iceGatheringState) {
3395
- case "gathering":
3396
- if (this._icePublicIPGatheringTimeoutID)
3397
- clearTimeout(this._icePublicIPGatheringTimeoutID);
3398
- this._icePublicIPGatheringTimeoutID = setTimeout(() => {
3399
- if (!session.publicHostCandidateSeen &&
3400
- !session.relayCandidateSeen &&
3401
- !session.serverReflexiveCandidateSeen) {
3402
- if (pc.iceConnectionState !== "connected" && pc.iceConnectionState !== "completed")
3403
- this.analytics.numIceNoPublicIpGatheredIn3sec++;
3404
- }
3405
- }, ICE_PUBLIC_IP_GATHERING_TIMEOUT);
3406
- break;
3407
- case "complete":
3408
- if (this._icePublicIPGatheringTimeoutID)
3409
- clearTimeout(this._icePublicIPGatheringTimeoutID);
3410
- this._icePublicIPGatheringTimeoutID = null;
3411
- break;
3412
- }
3413
- };
3414
- pc.onicecandidate = (event) => {
3415
- if (event.candidate) {
3416
- if (event.candidate.type === "relayed") {
3417
- this.analytics.P2PRelayedIceCandidate++;
3418
- }
3419
- switch (event.candidate.type) {
3420
- case "host":
3421
- const address = event.candidate.address;
3422
- if (!address) {
3423
- break;
3424
- }
3425
- try {
3426
- if (ipRegex.v4({ exact: true }).test(address)) {
3427
- const ipv4 = checkIp(address);
3428
- if (ipv4.isPublicIp)
3429
- session.publicHostCandidateSeen = true;
3430
- }
3431
- else if (ipRegex.v6({ exact: true }).test(address.replace(/^\[(.*)\]/, "$1"))) {
3432
- const ipv6 = new Address6(address.replace(/^\[(.*)\]/, "$1"));
3433
- session.ipv6HostCandidateSeen = true;
3434
- if (ipv6.getScope() === "Global") {
3435
- session.publicHostCandidateSeen = true;
3436
- }
3437
- if (ipv6.isTeredo()) {
3438
- session.ipv6HostCandidateTeredoSeen = true;
3439
- }
3440
- if (ipv6.is6to4()) {
3441
- session.ipv6HostCandidate6to4Seen = true;
3442
- }
3443
- }
3444
- else {
3445
- const uuidv4 = address.replace(/.local/, "");
3446
- if (uuidv4 && validate(uuidv4, 4)) {
3447
- session.mdnsHostCandidateSeen = true;
3448
- }
3449
- }
3450
- }
3451
- catch (error) {
3452
- logger$6.info("Error during parsing candidates! Error: ", { error });
3453
- }
3454
- break;
3455
- case "srflx":
3456
- session.serverReflexiveCandidateSeen = true;
3457
- break;
3458
- case "relayed":
3459
- case "relay":
3460
- session.relayCandidateSeen = true;
3461
- break;
3462
- }
3463
- this._emitServerEvent(RELAY_MESSAGES.ICE_CANDIDATE, {
3464
- receiverId: clientId,
3465
- message: event.candidate,
3466
- });
3467
- }
3468
- else {
3469
- this._emitServerEvent(RELAY_MESSAGES.ICE_END_OF_CANDIDATES, {
3470
- receiverId: clientId,
3471
- });
3472
- if (!session.publicHostCandidateSeen &&
3473
- !session.relayCandidateSeen &&
3474
- !session.serverReflexiveCandidateSeen &&
3475
- pc.iceConnectionState !== "connected" &&
3476
- pc.iceConnectionState !== "completed") {
3477
- this.analytics.numIceNoPublicIpGathered++;
3478
- }
3479
- if (session.ipv6HostCandidateSeen) {
3480
- this.analytics.numIceIpv6Seen++;
3481
- if (session.ipv6HostCandidate6to4Seen)
3482
- this.analytics.numIceIpv6SixToFour++;
3483
- if (session.ipv6HostCandidateTeredoSeen)
3484
- this.analytics.numIceIpv6TeredoSeen++;
3485
- }
3486
- if (session.mdnsHostCandidateSeen)
3487
- this.analytics.numIceMdnsSeen++;
3488
- }
3489
- };
3490
- pc.onnegotiationneeded = () => {
3491
- if (pc.iceConnectionState === "new" || !session.connectionStatus) {
3492
- return;
3493
- }
3494
- logger$6.info(`onnegotiationneeded client ${clientId}`);
3495
- this._negotiatePeerConnection({ clientId, session });
3496
- };
3497
- return session;
3498
- }
3499
3484
  acceptNewStream({ streamId, clientId }) {
3500
3485
  logger$6.info("acceptNewStream() [streamId: %s}, clientId: %s]", streamId, clientId);
3501
3486
  let session = this._getSession(clientId);
@@ -3509,7 +3494,7 @@ class P2pRtcManager {
3509
3494
  else {
3510
3495
  initialBandwidth = this._changeBandwidthForAllClients(true);
3511
3496
  }
3512
- session = this._createP2pSession({
3497
+ session = this._createSession({
3513
3498
  clientId,
3514
3499
  initialBandwidth,
3515
3500
  isOfferer: false,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@whereby.com/media",
3
3
  "description": "Media library for Whereby",
4
- "version": "8.0.0",
4
+ "version": "8.0.1",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/whereby/sdk",
7
7
  "repository": {
@@ -61,10 +61,10 @@
61
61
  "prettier": "^3.5.3",
62
62
  "process": "^0.11.10",
63
63
  "typescript": "^5.8.3",
64
+ "@whereby.com/eslint-config": "0.1.0",
64
65
  "@whereby.com/jest-config": "0.1.0",
65
66
  "@whereby.com/prettier-config": "0.1.0",
66
67
  "@whereby.com/rollup-config": "0.1.1",
67
- "@whereby.com/eslint-config": "0.1.0",
68
68
  "@whereby.com/tsconfig": "0.1.0"
69
69
  },
70
70
  "engines": {