@whereby.com/media 2.8.6 → 2.8.8

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.mjs CHANGED
@@ -2224,6 +2224,7 @@ class Session {
2224
2224
  this.ipv6HostCandidateTeredoSeen = false;
2225
2225
  this.ipv6HostCandidate6to4Seen = false;
2226
2226
  this.mdnsHostCandidateSeen = false;
2227
+ this.pendingReplaceTrackActions = [];
2227
2228
  this.peerConnectionConfig = peerConnectionConfig;
2228
2229
  this.clientId = clientId;
2229
2230
  this.pc = new RTCPeerConnection(this.peerConnectionConfig);
@@ -2300,22 +2301,16 @@ class Session {
2300
2301
  if (streamIndex !== -1) {
2301
2302
  this.streams.splice(streamIndex, 1);
2302
2303
  }
2303
- if (this.pc) {
2304
- if (this.pc.removeTrack) {
2305
- stream.getTracks().forEach((track) => {
2306
- const sender = this.pc.getSenders().find((sender) => sender.track === track);
2307
- if (sender) {
2308
- this.pc.removeTrack(sender);
2309
- }
2310
- });
2311
- }
2312
- else if (this.pc.removeStream) {
2313
- this.pc.removeStream(stream);
2314
- }
2304
+ if (this.pc.removeTrack) {
2305
+ stream.getTracks().forEach((track) => {
2306
+ const sender = this.pc.getSenders().find((sender) => sender.track === track);
2307
+ if (sender) {
2308
+ this.pc.removeTrack(sender);
2309
+ }
2310
+ });
2315
2311
  }
2316
- else {
2317
- rtcStats.sendEvent("P2PRemoveStreamNoPC", {});
2318
- this._incrementAnalyticMetric("P2PRemoveStreamNoPC");
2312
+ else if (this.pc.removeStream) {
2313
+ this.pc.removeStream(stream);
2319
2314
  }
2320
2315
  }
2321
2316
  _setRemoteDescription(desc) {
@@ -2409,86 +2404,77 @@ class Session {
2409
2404
  }
2410
2405
  }
2411
2406
  hasConnectedPeerConnection() {
2412
- return this.pc && this.pc.connectionState === "connected";
2407
+ return this.pc.connectionState === "connected";
2413
2408
  }
2414
2409
  replaceTrack(oldTrack, newTrack) {
2415
- logger$7.info("replacetrack() [oldTrackId: %s, newTrackId: %s]", oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.id, newTrack === null || newTrack === void 0 ? void 0 : newTrack.id);
2416
- if (!newTrack) {
2417
- rtcStats.sendEvent("P2PReplaceTrackNoNewTrack", {
2418
- oldTrackId: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.id,
2419
- oldTrackKind: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.kind,
2420
- oldTrackIsEffect: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.effectTrack,
2421
- });
2422
- this._incrementAnalyticMetric("P2PReplaceTrackNoNewTrack");
2423
- return false;
2424
- }
2425
- if (newTrack.readyState === "ended") {
2426
- rtcStats.sendEvent("P2PReplaceTrackNewTrackEnded", {
2427
- newTrackId: newTrack.id,
2428
- newTrackKind: newTrack.kind,
2429
- newTrackIsEffect: newTrack.effectTrack,
2410
+ return __awaiter(this, void 0, void 0, function* () {
2411
+ logger$7.info("replacetrack() [oldTrackId: %s, newTrackId: %s]", oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.id, newTrack.id);
2412
+ if (newTrack.readyState === "ended") {
2413
+ rtcStats.sendEvent("P2PReplaceTrackNewTrackEnded", {
2414
+ newTrackId: newTrack.id,
2415
+ newTrackKind: newTrack.kind,
2416
+ newTrackIsEffect: trackAnnotations(newTrack).isEffectTrack,
2417
+ });
2418
+ this._incrementAnalyticMetric("P2PReplaceTrackNewTrackEnded");
2419
+ throw new Error(`refusing to replace track trackId: ${newTrack.id} kind: ${newTrack.kind} with readyState: ${newTrack.readyState}`);
2420
+ }
2421
+ const pc = this.pc;
2422
+ if (oldTrack) {
2423
+ const sender = pc.getSenders().find((s) => { var _a; return ((_a = s.track) === null || _a === void 0 ? void 0 : _a.id) === oldTrack.id; });
2424
+ if (sender) {
2425
+ return yield sender.replaceTrack(newTrack);
2426
+ }
2427
+ }
2428
+ const sender = pc.getSenders().find((s) => {
2429
+ const track = s.track;
2430
+ return (track === null || track === void 0 ? void 0 : track.kind) === newTrack.kind && !trackAnnotations(track).fromGetDisplayMedia;
2430
2431
  });
2431
- this._incrementAnalyticMetric("P2PReplaceTrackNewTrackEnded");
2432
- return false;
2433
- }
2434
- const pc = this.pc;
2435
- if (oldTrack) {
2436
- const sender = pc.getSenders().find((s) => { var _a; return ((_a = s.track) === null || _a === void 0 ? void 0 : _a.id) === oldTrack.id; });
2437
2432
  if (sender) {
2438
- sender.replaceTrack(newTrack);
2439
- return Promise.resolve(newTrack);
2433
+ this._incrementAnalyticMetric("P2PReplaceTrackOldTrackNotFound");
2434
+ const track = sender.track;
2435
+ rtcStats.sendEvent("P2PReplaceTrackOldTrackNotFound", {
2436
+ targetTrackId: track === null || track === void 0 ? void 0 : track.id,
2437
+ targetTrackKind: track === null || track === void 0 ? void 0 : track.kind,
2438
+ targetTrackIsEffect: track && trackAnnotations(track).isEffectTrack,
2439
+ targetTrackReadyState: track === null || track === void 0 ? void 0 : track.readyState,
2440
+ newTrackId: newTrack.id,
2441
+ newTrackKind: newTrack.kind,
2442
+ newTrackIsEffect: trackAnnotations(newTrack).isEffectTrack,
2443
+ oldTrackId: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.id,
2444
+ oldTrackKind: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.kind,
2445
+ oldTrackIsEffect: oldTrack && trackAnnotations(oldTrack).isEffectTrack,
2446
+ });
2447
+ return yield sender.replaceTrack(newTrack);
2440
2448
  }
2441
- }
2442
- const sender = pc.getSenders().find((s) => {
2443
- const track = s.track;
2444
- return (track === null || track === void 0 ? void 0 : track.kind) === newTrack.kind && !trackAnnotations(track).fromGetDisplayMedia;
2445
- });
2446
- if (sender) {
2447
- this._incrementAnalyticMetric("P2PReplaceTrackOldTrackNotFound");
2448
- const track = sender.track;
2449
- rtcStats.sendEvent("P2PReplaceTrackOldTrackNotFound", {
2450
- targetTrackId: track === null || track === void 0 ? void 0 : track.id,
2451
- targetTrackKind: track === null || track === void 0 ? void 0 : track.kind,
2452
- targetTrackIsEffect: track === null || track === void 0 ? void 0 : track.effectTrack,
2453
- targetTrackReadyState: track === null || track === void 0 ? void 0 : track.readyState,
2454
- newTrackId: newTrack.id,
2455
- newTrackKind: newTrack.kind,
2456
- newTrackIsEffect: newTrack.effectTrack,
2457
- oldTrackId: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.id,
2458
- oldTrackKind: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.kind,
2459
- oldTrackIsEffect: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.effectTrack,
2460
- });
2461
- sender.replaceTrack(newTrack);
2462
- return Promise.resolve(newTrack);
2463
- }
2464
- let stream = this.streams.find((s) => s.getTracks().find((t) => t.id === newTrack.id));
2465
- if (!stream) {
2466
- rtcStats.sendEvent("P2PReplaceTrackNewTrackNotInStream", {
2449
+ let stream = this.streams.find((s) => s.getTracks().find((t) => t.id === newTrack.id));
2450
+ if (!stream) {
2451
+ rtcStats.sendEvent("P2PReplaceTrackNewTrackNotInStream", {
2452
+ oldTrackId: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.id,
2453
+ oldTrackKind: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.kind,
2454
+ oldTrackIsEffect: oldTrack && trackAnnotations(oldTrack).isEffectTrack,
2455
+ newTrackId: newTrack.id,
2456
+ newTrackKind: newTrack.kind,
2457
+ newTrackIsEffect: trackAnnotations(newTrack).isEffectTrack,
2458
+ });
2459
+ this._incrementAnalyticMetric("P2PReplaceTrackNewTrackNotInStream");
2460
+ }
2461
+ stream = this.streams[0];
2462
+ if (!stream) {
2463
+ rtcStats.sendEvent("P2PReplaceTrackNoStream", {});
2464
+ this._incrementAnalyticMetric("P2PReplaceTrackNoStream");
2465
+ throw new Error("replaceTrack: No stream?");
2466
+ }
2467
+ rtcStats.sendEvent("P2PReplaceTrackSourceKindNotFound", {
2467
2468
  oldTrackId: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.id,
2468
2469
  oldTrackKind: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.kind,
2469
- oldTrackIsEffect: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.effectTrack,
2470
+ oldTrackIsEffect: oldTrack && trackAnnotations(oldTrack).isEffectTrack,
2470
2471
  newTrackId: newTrack.id,
2471
2472
  newTrackKind: newTrack.kind,
2472
- newTrackIsEffect: newTrack.effectTrack,
2473
+ newTrackIsEffect: trackAnnotations(newTrack).isEffectTrack,
2473
2474
  });
2474
- this._incrementAnalyticMetric("P2PReplaceTrackNewTrackNotInStream");
2475
- }
2476
- stream = this.streams[0];
2477
- if (!stream) {
2478
- rtcStats.sendEvent("P2PReplaceTrackNoStream", {});
2479
- this._incrementAnalyticMetric("P2PReplaceTrackNoStream");
2480
- return Promise.reject(new Error("replaceTrack: No stream?"));
2481
- }
2482
- rtcStats.sendEvent("P2PReplaceTrackSourceKindNotFound", {
2483
- oldTrackId: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.id,
2484
- oldTrackKind: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.kind,
2485
- oldTrackIsEffect: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.effectTrack,
2486
- newTrackId: newTrack.id,
2487
- newTrackKind: newTrack.kind,
2488
- newTrackIsEffect: newTrack.effectTrack,
2475
+ this._incrementAnalyticMetric("P2PReplaceTrackSourceKindNotFound");
2476
+ pc.addTrack(newTrack, stream);
2489
2477
  });
2490
- this._incrementAnalyticMetric("P2PReplaceTrackSourceKindNotFound");
2491
- return pc.addTrack(newTrack, stream);
2492
2478
  }
2493
2479
  changeBandwidth(bandwidth) {
2494
2480
  var _a;
@@ -2525,39 +2511,6 @@ class Session {
2525
2511
 
2526
2512
  const MEDIA_JITTER_BUFFER_TARGET = 400;
2527
2513
 
2528
- var _a$3;
2529
- const adapter$3 = (_a$3 = adapterRaw.default) !== null && _a$3 !== void 0 ? _a$3 : adapterRaw;
2530
- function detectMicrophoneNotWorking(pc) {
2531
- var _a, _b;
2532
- if (((_a = adapter$3.browserDetails) === null || _a === void 0 ? void 0 : _a.browser) !== "chrome" ||
2533
- ((_b = adapter$3.browserDetails) === null || _b === void 0 ? void 0 : _b.browser) < 58 ||
2534
- pc.signalingState === "closed") {
2535
- return Promise.resolve(false);
2536
- }
2537
- const sendingAudio = pc.getSenders().some((sender) => sender.track && sender.track.kind === "audio");
2538
- const receivingAudio = pc.getReceivers().some((receiver) => receiver.track && receiver.track.kind === "audio");
2539
- return pc.getStats(null).then((result) => {
2540
- let microphoneFailed = false;
2541
- result.forEach((report) => {
2542
- if (report.type === "outbound-rtp" &&
2543
- (report.kind === "audio" || report.mediaType === "audio") &&
2544
- sendingAudio) {
2545
- if (report.bytesSent === 0) {
2546
- microphoneFailed = "outbound";
2547
- }
2548
- }
2549
- else if (report.type === "inbound-rtp" &&
2550
- (report.kind === "audio" || report.mediaType === "audio") &&
2551
- receivingAudio) {
2552
- if (report.bytesReceived === 0) {
2553
- microphoneFailed = "inbound";
2554
- }
2555
- }
2556
- });
2557
- return microphoneFailed;
2558
- });
2559
- }
2560
-
2561
2514
  const EVENTS = {
2562
2515
  CLIENT_CONNECTION_STATUS_CHANGED: "client_connection_status_changed",
2563
2516
  STREAM_ADDED: "stream_added",
@@ -2595,14 +2548,14 @@ const STREAM_TYPES = {
2595
2548
  screenshare: "screenshare",
2596
2549
  };
2597
2550
 
2598
- var _a$2, _b$1, _c;
2599
- const adapter$2 = (_a$2 = adapterRaw.default) !== null && _a$2 !== void 0 ? _a$2 : adapterRaw;
2551
+ var _a$3, _b$1, _c;
2552
+ const adapter$3 = (_a$3 = adapterRaw.default) !== null && _a$3 !== void 0 ? _a$3 : adapterRaw;
2600
2553
  const logger$6 = new Logger();
2601
2554
  const ICE_PUBLIC_IP_GATHERING_TIMEOUT = 3 * 1000;
2602
- const browserName$1 = (_b$1 = adapter$2.browserDetails) === null || _b$1 === void 0 ? void 0 : _b$1.browser;
2603
- const browserVersion = adapter$2.browserDetails.version;
2604
- if (browserName$1 === "firefox" && adapter$2.browserShim && "shimGetDisplayMedia" in adapter$2.browserShim) {
2605
- (_c = adapter$2.browserShim) === null || _c === void 0 ? void 0 : _c.shimGetDisplayMedia(window, "screen");
2555
+ const browserName$1 = (_b$1 = adapter$3.browserDetails) === null || _b$1 === void 0 ? void 0 : _b$1.browser;
2556
+ const browserVersion = adapter$3.browserDetails.version;
2557
+ if (browserName$1 === "firefox" && adapter$3.browserShim && "shimGetDisplayMedia" in adapter$3.browserShim) {
2558
+ (_c = adapter$3.browserShim) === null || _c === void 0 ? void 0 : _c.shimGetDisplayMedia(window, "screen");
2606
2559
  }
2607
2560
  let unloading$1 = false;
2608
2561
  if (browserName$1 === "chrome") {
@@ -2630,7 +2583,6 @@ class P2pRtcManager {
2630
2583
  this._closed = false;
2631
2584
  this.skipEmittingServerMessageCount = 0;
2632
2585
  this.offerOptions = { offerToReceiveAudio: true, offerToReceiveVideo: true };
2633
- this._pendingActionsForConnectedPeerConnections = [];
2634
2586
  this._audioTrackOnEnded = () => {
2635
2587
  rtcStats.sendEvent("audio_ended", { unloading: unloading$1 });
2636
2588
  this._emit(rtcManagerEvents.MICROPHONE_STOPPED_WORKING, {});
@@ -2663,10 +2615,8 @@ class P2pRtcManager {
2663
2615
  numPcOnAnswerFailure: 0,
2664
2616
  numPcOnOfferFailure: 0,
2665
2617
  P2PChangeBandwidthEmptySDPType: 0,
2666
- P2PNegotiationNeeded: 0,
2667
2618
  P2PSessionAlreadyCreated: 0,
2668
2619
  P2PReplaceTrackNoStream: 0,
2669
- P2PReplaceTrackNoNewTrack: 0,
2670
2620
  P2PReplaceTrackNewTrackEnded: 0,
2671
2621
  P2PReplaceTrackNewTrackNotInStream: 0,
2672
2622
  P2PReplaceTrackOldTrackNotFound: 0,
@@ -2700,7 +2650,7 @@ class P2pRtcManager {
2700
2650
  const audioTrack = stream.getAudioTracks()[0];
2701
2651
  const videoTrack = stream.getVideoTracks()[0];
2702
2652
  if (audioTrack) {
2703
- if (!audioTrack.effectTrack) {
2653
+ if (!trackAnnotations(audioTrack).isEffectTrack) {
2704
2654
  this._monitorAudioTrack(audioTrack);
2705
2655
  }
2706
2656
  const beforeEffectTrack = beforeEffectTracks.find((t) => t.kind === "audio");
@@ -2709,7 +2659,7 @@ class P2pRtcManager {
2709
2659
  }
2710
2660
  }
2711
2661
  if (videoTrack) {
2712
- if (!videoTrack.effectTrack) {
2662
+ if (!trackAnnotations(videoTrack).isEffectTrack) {
2713
2663
  this._monitorVideoTrack(videoTrack);
2714
2664
  }
2715
2665
  const beforeEffectTrack = beforeEffectTracks.find((t) => t.kind === "video");
@@ -2736,10 +2686,10 @@ class P2pRtcManager {
2736
2686
  return;
2737
2687
  }
2738
2688
  replaceTrack(oldTrack, newTrack) {
2739
- if (newTrack.kind === "audio" && !newTrack.effectTrack) {
2689
+ if (newTrack.kind === "audio" && !trackAnnotations(newTrack).isEffectTrack) {
2740
2690
  this._monitorAudioTrack(newTrack);
2741
2691
  }
2742
- if (newTrack.kind === "video" && !newTrack.effectTrack) {
2692
+ if (newTrack.kind === "video" && !trackAnnotations(newTrack).isEffectTrack) {
2743
2693
  this._monitorVideoTrack(newTrack);
2744
2694
  }
2745
2695
  return this._replaceTrackToPeerConnections(oldTrack, newTrack);
@@ -2830,25 +2780,6 @@ class P2pRtcManager {
2830
2780
  this.analytics.numPcOnAnswerFailure++;
2831
2781
  });
2832
2782
  }),
2833
- this._serverSocket.on(PROTOCOL_RESPONSES.ROOM_JOINED, (payload) => {
2834
- if ("error" in payload || !this._wasScreenSharing) {
2835
- return;
2836
- }
2837
- const screenShareStreamId = Object.keys(this.localStreams).find((id) => id !== CAMERA_STREAM_ID);
2838
- if (!screenShareStreamId) {
2839
- return;
2840
- }
2841
- const screenshareStream = this.localStreams[screenShareStreamId];
2842
- if (!screenshareStream) {
2843
- logger$6.warn("screenshare stream %s not found", screenShareStreamId);
2844
- return;
2845
- }
2846
- const hasAudioTrack = screenshareStream.getAudioTracks().length > 0;
2847
- this._emitServerEvent(PROTOCOL_REQUESTS.START_SCREENSHARE, {
2848
- streamId: screenShareStreamId,
2849
- hasAudioTrack,
2850
- });
2851
- }),
2852
2783
  this._serverSocket.on(PROTOCOL_RESPONSES.SCREENSHARE_STOPPED, (payload) => {
2853
2784
  const session = this._getSession(payload.clientId);
2854
2785
  if (session) {
@@ -2966,31 +2897,6 @@ class P2pRtcManager {
2966
2897
  }
2967
2898
  return this.peerConnections[peerConnectionId];
2968
2899
  }
2969
- _getOrCreateSession({ peerConnectionId, clientId, initialBandwidth, peerConnectionConfig, }) {
2970
- let session = this.peerConnections[peerConnectionId];
2971
- if (session === undefined) {
2972
- const deprioritizeH264Encoding = browserName$1 === "safari" &&
2973
- browserVersion &&
2974
- browserVersion >= 14 &&
2975
- this._features.deprioritizeH264OnSafari;
2976
- this.peerConnections[peerConnectionId] = session = new Session({
2977
- peerConnectionId,
2978
- clientId,
2979
- peerConnectionConfig,
2980
- bandwidth: initialBandwidth,
2981
- deprioritizeH264Encoding,
2982
- incrementAnalyticMetric: (metric) => this.analytics[metric]++,
2983
- });
2984
- }
2985
- else {
2986
- this.analytics.P2PSessionAlreadyCreated++;
2987
- rtcStats.sendEvent("P2PSessionAlreadyCreated", {
2988
- clientId,
2989
- peerConnectionId,
2990
- });
2991
- }
2992
- return session;
2993
- }
2994
2900
  _getLocalCameraStream() {
2995
2901
  return this.localStreams[CAMERA_STREAM_ID];
2996
2902
  }
@@ -3004,7 +2910,7 @@ class P2pRtcManager {
3004
2910
  const streamIds = this._getNonLocalCameraStreamIds();
3005
2911
  return streamIds.length === 0 ? null : this.localStreams[streamIds[0]];
3006
2912
  }
3007
- _createSession({ clientId, initialBandwidth, isOfferer, peerConnectionId, }) {
2913
+ _createSession({ clientId, initialBandwidth, isOfferer, peerConnectionId }) {
3008
2914
  if (!peerConnectionId) {
3009
2915
  throw new Error("peerConnectionId is missing");
3010
2916
  }
@@ -3014,26 +2920,22 @@ class P2pRtcManager {
3014
2920
  const peerConnectionConfig = {
3015
2921
  iceServers: this._features.turnServersOn ? this._turnServers : this._iceServers,
3016
2922
  };
3017
- const constraints = { optional: [] };
3018
- constraints.optional.push({ rtcStatsRoomSessionId: this._roomSessionId });
3019
- constraints.optional.push({ rtcStatsClientId: this._selfId });
3020
- constraints.optional.push({ rtcStatsPeerId: peerConnectionId });
3021
- constraints.optional.push({ rtcStatsConferenceId: this._roomName });
3022
- if (browserName$1 === "chrome") {
3023
- constraints.optional.push({
3024
- googCpuOveruseDetection: true,
3025
- });
3026
- peerConnectionConfig.sdpSemantics = "unified-plan";
3027
- }
3028
2923
  peerConnectionConfig.iceServers = turnServerOverride(peerConnectionConfig.iceServers, this._features.turnServerOverrideHost);
3029
2924
  external_stun_servers(peerConnectionConfig, this._features);
3030
2925
  maybeTurnOnly(peerConnectionConfig, this._features);
3031
- const session = this._getOrCreateSession({
2926
+ const deprioritizeH264Encoding = browserName$1 === "safari" &&
2927
+ browserVersion &&
2928
+ browserVersion >= 14 &&
2929
+ this._features.deprioritizeH264OnSafari;
2930
+ const session = new Session({
3032
2931
  peerConnectionId,
3033
2932
  clientId,
3034
- initialBandwidth,
3035
2933
  peerConnectionConfig,
2934
+ bandwidth: initialBandwidth,
2935
+ deprioritizeH264Encoding,
2936
+ incrementAnalyticMetric: (metric) => this.analytics[metric]++,
3036
2937
  });
2938
+ this.peerConnections[peerConnectionId] = session;
3037
2939
  setTimeout(() => this._emit(rtcManagerEvents.NEW_PC), 0);
3038
2940
  this.analytics.numNewPc++;
3039
2941
  const { pc } = session;
@@ -3056,6 +2958,7 @@ class P2pRtcManager {
3056
2958
  }
3057
2959
  };
3058
2960
  pc.oniceconnectionstatechange = () => {
2961
+ var _a;
3059
2962
  let newStatus;
3060
2963
  const currentStatus = session.connectionStatus;
3061
2964
  switch (pc.iceConnectionState) {
@@ -3065,14 +2968,10 @@ class P2pRtcManager {
3065
2968
  case "connected":
3066
2969
  case "completed":
3067
2970
  newStatus = TYPES.CONNECTION_SUCCESSFUL;
3068
- if (!session.wasEverConnected) {
3069
- this._pendingActionsForConnectedPeerConnections.forEach((action) => {
3070
- if (typeof action === "function") {
3071
- action();
3072
- }
3073
- });
3074
- this._pendingActionsForConnectedPeerConnections = [];
3075
- }
2971
+ session.pendingReplaceTrackActions.forEach((action) => __awaiter(this, void 0, void 0, function* () {
2972
+ yield action();
2973
+ }));
2974
+ session.pendingReplaceTrackActions = [];
3076
2975
  if (!session.wasEverConnected &&
3077
2976
  (pc.iceConnectionState.match(/connected|completed/) ||
3078
2977
  (browserName$1 === "chrome" && pc.localDescription && pc.localDescription.type === "answer"))) {
@@ -3081,7 +2980,7 @@ class P2pRtcManager {
3081
2980
  if (this._isAudioOnlyMode) {
3082
2981
  session.setAudioOnly(true, this._screenshareVideoTrackIds);
3083
2982
  }
3084
- session.registerConnected();
2983
+ (_a = session.registerConnected) === null || _a === void 0 ? void 0 : _a.call(session, {});
3085
2984
  break;
3086
2985
  case "disconnected":
3087
2986
  newStatus = TYPES.CONNECTION_DISCONNECTED;
@@ -3105,28 +3004,19 @@ class P2pRtcManager {
3105
3004
  }
3106
3005
  this._setConnectionStatus(session, newStatus, clientId);
3107
3006
  };
3108
- pc.onconnectionstate = () => {
3007
+ pc.onconnectionstatechange = () => {
3008
+ var _a;
3109
3009
  switch (pc.connectionState) {
3110
3010
  case "connected":
3111
- setTimeout(() => {
3112
- detectMicrophoneNotWorking(session.pc).then((failureDirection) => {
3113
- if (failureDirection !== false) {
3114
- this._emit(rtcManagerEvents.MICROPHONE_NOT_WORKING, {
3115
- failureDirection,
3116
- clientId,
3117
- });
3118
- }
3119
- });
3120
- }, 3000);
3121
- session.registerConnected();
3011
+ (_a = session.registerConnected) === null || _a === void 0 ? void 0 : _a.call(session, {});
3122
3012
  break;
3123
3013
  case "failed":
3124
3014
  const newStatus = TYPES.CONNECTION_FAILED;
3125
- if (session.relayCandidateSeen === null && !session.wasEverConnected) {
3126
- this._emit(rtcManagerEvents.CONNECTION_BLOCKED_BY_NETWORK);
3127
- }
3128
3015
  this._setConnectionStatus(session, newStatus, clientId);
3129
3016
  break;
3017
+ case "closed":
3018
+ this._cleanup(session.clientId);
3019
+ break;
3130
3020
  }
3131
3021
  };
3132
3022
  const localCameraStream = this.localStreams[CAMERA_STREAM_ID];
@@ -3182,43 +3072,34 @@ class P2pRtcManager {
3182
3072
  const promises = [];
3183
3073
  this._forEachPeerConnection((session) => {
3184
3074
  if (!session.hasConnectedPeerConnection()) {
3185
- rtcStats.sendEvent("P2PReplaceTrackWithoutPC", {});
3186
- this.analytics.P2PReplaceTrackWithoutPC++;
3187
- logger$6.info("Session doesn't have a connected PeerConnection, adding pending action!");
3188
- const pendingActions = this._pendingActionsForConnectedPeerConnections;
3189
- if (!pendingActions) {
3190
- rtcStats.sendEvent("P2PReplaceTrackToPCsPendingActionsNull", {});
3191
- this.analytics.P2PReplaceTrackToPCsPendingActionsNull++;
3192
- logger$6.warn(`No pending action is created to replace track, because the pending actions array is null`);
3075
+ if (session.pc.connectionState === "closed")
3193
3076
  return;
3194
- }
3077
+ logger$6.info("Session doesn't have a connected PeerConnection, adding pending action!");
3078
+ this.analytics.P2PReplaceTrackWithoutPC++;
3079
+ rtcStats.sendEvent("P2PReplaceTrackWithoutPC", {
3080
+ connectionState: session.pc.connectionState,
3081
+ iceConnectionState: session.pc.iceConnectionState,
3082
+ });
3195
3083
  const promise = new Promise((resolve, reject) => {
3196
- const action = () => {
3197
- const replacedTrackPromise = session.replaceTrack(oldTrack, newTrack);
3198
- if (!replacedTrackPromise) {
3199
- rtcStats.sendEvent("P2PReplaceTrackReturnedFalse", {});
3200
- this.analytics.P2PReplaceTrackReturnedFalse++;
3201
- logger$6.error("replaceTrack returned false!");
3202
- reject(`ReplaceTrack returned false`);
3203
- return;
3084
+ const action = () => __awaiter(this, void 0, void 0, function* () {
3085
+ try {
3086
+ yield session.replaceTrack(oldTrack, newTrack);
3087
+ resolve({});
3204
3088
  }
3205
- replacedTrackPromise.then((track) => resolve(track)).catch((error) => reject(error));
3206
- };
3207
- pendingActions.push(action);
3089
+ catch (error) {
3090
+ reject(error);
3091
+ }
3092
+ });
3093
+ session.pendingReplaceTrackActions.push(action);
3208
3094
  });
3209
3095
  promises.push(promise);
3210
3096
  return;
3211
3097
  }
3212
- const replacedTrackResult = session.replaceTrack(oldTrack, newTrack);
3213
- if (!replacedTrackResult) {
3214
- rtcStats.sendEvent("P2PReplaceTrackReturnedFalse", {});
3215
- this.analytics.P2PReplaceTrackReturnedFalse++;
3216
- logger$6.error("replaceTrack returned false!");
3217
- return;
3218
- }
3219
- promises.push(replacedTrackResult);
3098
+ promises.push(session.replaceTrack(oldTrack, newTrack));
3099
+ });
3100
+ return Promise.all(promises).catch((error) => {
3101
+ logger$6.error(String(error));
3220
3102
  });
3221
- return Promise.all(promises);
3222
3103
  }
3223
3104
  _removeStreamFromPeerConnections(stream) {
3224
3105
  this._forEachPeerConnection((session) => {
@@ -3290,7 +3171,7 @@ class P2pRtcManager {
3290
3171
  isOfferer: true,
3291
3172
  });
3292
3173
  this._negotiatePeerConnection(clientId, session);
3293
- return Promise.resolve(session);
3174
+ return session;
3294
3175
  }
3295
3176
  _maybeRestartIce(clientId, session) {
3296
3177
  const pc = session.pc;
@@ -3553,8 +3434,6 @@ class P2pRtcManager {
3553
3434
  }
3554
3435
  };
3555
3436
  pc.onnegotiationneeded = () => {
3556
- this.analytics.P2PNegotiationNeeded++;
3557
- rtcStats.sendEvent("P2PNegotiationNeeded", {});
3558
3437
  if (pc.iceConnectionState === "new" || !session.connectionStatus) {
3559
3438
  return;
3560
3439
  }
@@ -3569,7 +3448,6 @@ class P2pRtcManager {
3569
3448
  }
3570
3449
  let initialBandwidth = (session && session.bandwidth) || 0;
3571
3450
  if (session) {
3572
- logger$6.warn("Replacing peer session", clientId);
3573
3451
  this._cleanup(clientId);
3574
3452
  }
3575
3453
  else {
@@ -3621,9 +3499,7 @@ class P2pRtcManager {
3621
3499
  return;
3622
3500
  }
3623
3501
  if (enable === false) {
3624
- const stopCameraDelay = ((_a = localStream.getVideoTracks().find((t) => !t.enabled)) === null || _a === void 0 ? void 0 : _a.readyState) === "ended"
3625
- ? 0
3626
- : 5000;
3502
+ const stopCameraDelay = ((_a = localStream.getVideoTracks().find((t) => !t.enabled)) === null || _a === void 0 ? void 0 : _a.readyState) === "ended" ? 0 : 5000;
3627
3503
  setTimeout(() => {
3628
3504
  localStream.getVideoTracks().forEach((track) => {
3629
3505
  if (track.enabled === false) {
@@ -4511,10 +4387,10 @@ function createVegaConnectionManager(config) {
4511
4387
  return { connect, updateHostList, networkIsUp, networkIsPossiblyDown };
4512
4388
  }
4513
4389
 
4514
- var _a$1;
4515
- const adapter$1 = (_a$1 = adapterRaw.default) !== null && _a$1 !== void 0 ? _a$1 : adapterRaw;
4390
+ var _a$2;
4391
+ const adapter$2 = (_a$2 = adapterRaw.default) !== null && _a$2 !== void 0 ? _a$2 : adapterRaw;
4516
4392
  const logger$2 = new Logger();
4517
- const browserName = adapter$1.browserDetails.browser;
4393
+ const browserName = adapter$2.browserDetails.browser;
4518
4394
  let unloading = false;
4519
4395
  const RESTARTICE_ERROR_RETRY_THRESHOLD_IN_MS = 3500;
4520
4396
  const RESTARTICE_ERROR_MAX_RETRY_COUNT = 5;
@@ -5456,14 +5332,14 @@ class VegaRtcManager {
5456
5332
  }
5457
5333
  replaceTrack(oldTrack, track) {
5458
5334
  if (track.kind === "audio") {
5459
- if (!track.effectTrack) {
5335
+ if (!trackAnnotations(track).isEffectTrack) {
5460
5336
  this._monitorAudioTrack(track);
5461
5337
  }
5462
5338
  this._micTrack = track;
5463
5339
  this._replaceMicTrack();
5464
5340
  }
5465
5341
  if (track.kind === "video") {
5466
- if (!track.effectTrack) {
5342
+ if (!trackAnnotations(track).isEffectTrack) {
5467
5343
  this._monitorVideoTrack(track);
5468
5344
  }
5469
5345
  this._webcamTrack = track;
@@ -5496,7 +5372,7 @@ class VegaRtcManager {
5496
5372
  const audioTrack = stream.getAudioTracks()[0];
5497
5373
  if (videoTrack) {
5498
5374
  this._sendWebcam(videoTrack);
5499
- if (!videoTrack.effectTrack) {
5375
+ if (!trackAnnotations(videoTrack).isEffectTrack) {
5500
5376
  this._monitorVideoTrack(videoTrack);
5501
5377
  }
5502
5378
  const beforeEffectTrack = beforeEffectTracks.find((t) => t.kind === "video");
@@ -5507,7 +5383,7 @@ class VegaRtcManager {
5507
5383
  if (audioTrack) {
5508
5384
  this._sendMic(audioTrack);
5509
5385
  this._syncMicAnalyser();
5510
- if (!audioTrack.effectTrack) {
5386
+ if (!trackAnnotations(audioTrack).isEffectTrack) {
5511
5387
  this._monitorAudioTrack(audioTrack);
5512
5388
  }
5513
5389
  const beforeEffectTrack = beforeEffectTracks.find((t) => t.kind === "audio");
@@ -5581,9 +5457,7 @@ class VegaRtcManager {
5581
5457
  }
5582
5458
  if (!enable) {
5583
5459
  clearTimeout(this._stopCameraTimeout);
5584
- const stopCameraDelay = ((_a = localStream.getVideoTracks().find((t) => !t.enabled)) === null || _a === void 0 ? void 0 : _a.readyState) === "ended"
5585
- ? 0
5586
- : 5000;
5460
+ const stopCameraDelay = ((_a = localStream.getVideoTracks().find((t) => !t.enabled)) === null || _a === void 0 ? void 0 : _a.readyState) === "ended" ? 0 : 5000;
5587
5461
  this._stopCameraTimeout = setTimeout(() => {
5588
5462
  localStream.getVideoTracks().forEach((track) => {
5589
5463
  if (track.enabled === false) {
@@ -7127,6 +7001,39 @@ class BandwidthTester extends EventEmitter {
7127
7001
  }
7128
7002
  }
7129
7003
 
7004
+ var _a$1;
7005
+ const adapter$1 = (_a$1 = adapterRaw.default) !== null && _a$1 !== void 0 ? _a$1 : adapterRaw;
7006
+ function detectMicrophoneNotWorking(pc) {
7007
+ var _a, _b;
7008
+ if (((_a = adapter$1.browserDetails) === null || _a === void 0 ? void 0 : _a.browser) !== "chrome" ||
7009
+ ((_b = adapter$1.browserDetails) === null || _b === void 0 ? void 0 : _b.browser) < 58 ||
7010
+ pc.signalingState === "closed") {
7011
+ return Promise.resolve(false);
7012
+ }
7013
+ const sendingAudio = pc.getSenders().some((sender) => sender.track && sender.track.kind === "audio");
7014
+ const receivingAudio = pc.getReceivers().some((receiver) => receiver.track && receiver.track.kind === "audio");
7015
+ return pc.getStats(null).then((result) => {
7016
+ let microphoneFailed = false;
7017
+ result.forEach((report) => {
7018
+ if (report.type === "outbound-rtp" &&
7019
+ (report.kind === "audio" || report.mediaType === "audio") &&
7020
+ sendingAudio) {
7021
+ if (report.bytesSent === 0) {
7022
+ microphoneFailed = "outbound";
7023
+ }
7024
+ }
7025
+ else if (report.type === "inbound-rtp" &&
7026
+ (report.kind === "audio" || report.mediaType === "audio") &&
7027
+ receivingAudio) {
7028
+ if (report.bytesReceived === 0) {
7029
+ microphoneFailed = "inbound";
7030
+ }
7031
+ }
7032
+ });
7033
+ return microphoneFailed;
7034
+ });
7035
+ }
7036
+
7130
7037
  var _a, _b;
7131
7038
  const adapter = (_a = adapterRaw.default) !== null && _a !== void 0 ? _a : adapterRaw;
7132
7039
  const isSafari = ((_b = adapter.browserDetails) === null || _b === void 0 ? void 0 : _b.browser) === "safari";
@@ -7288,22 +7195,13 @@ function replaceTracksInStream(stream, newStream, only) {
7288
7195
  const replacedTracks = [];
7289
7196
  if (!only || only === "audio") {
7290
7197
  replacedTracks.push(...stream.getAudioTracks());
7291
- newStream.getAudioTracks().forEach((track) => {
7292
- track.replacement = true;
7293
- stream.addTrack(track);
7294
- });
7198
+ newStream.getAudioTracks().forEach((track) => stream.addTrack(track));
7295
7199
  }
7296
7200
  if (!only || only === "video") {
7297
7201
  replacedTracks.push(...stream.getVideoTracks());
7298
- newStream.getVideoTracks().forEach((track) => {
7299
- track.replacement = true;
7300
- stream.addTrack(track);
7301
- });
7202
+ newStream.getVideoTracks().forEach((track) => stream.addTrack(track));
7302
7203
  }
7303
- replacedTracks.forEach((track) => {
7304
- track.replaced = true;
7305
- stream.removeTrack(track);
7306
- });
7204
+ replacedTracks.forEach((track) => stream.removeTrack(track));
7307
7205
  return replacedTracks;
7308
7206
  }
7309
7207
  function getStream(constraintOpt_1) {