@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.cjs CHANGED
@@ -2245,6 +2245,7 @@ class Session {
2245
2245
  this.ipv6HostCandidateTeredoSeen = false;
2246
2246
  this.ipv6HostCandidate6to4Seen = false;
2247
2247
  this.mdnsHostCandidateSeen = false;
2248
+ this.pendingReplaceTrackActions = [];
2248
2249
  this.peerConnectionConfig = peerConnectionConfig;
2249
2250
  this.clientId = clientId;
2250
2251
  this.pc = new RTCPeerConnection(this.peerConnectionConfig);
@@ -2321,22 +2322,16 @@ class Session {
2321
2322
  if (streamIndex !== -1) {
2322
2323
  this.streams.splice(streamIndex, 1);
2323
2324
  }
2324
- if (this.pc) {
2325
- if (this.pc.removeTrack) {
2326
- stream.getTracks().forEach((track) => {
2327
- const sender = this.pc.getSenders().find((sender) => sender.track === track);
2328
- if (sender) {
2329
- this.pc.removeTrack(sender);
2330
- }
2331
- });
2332
- }
2333
- else if (this.pc.removeStream) {
2334
- this.pc.removeStream(stream);
2335
- }
2325
+ if (this.pc.removeTrack) {
2326
+ stream.getTracks().forEach((track) => {
2327
+ const sender = this.pc.getSenders().find((sender) => sender.track === track);
2328
+ if (sender) {
2329
+ this.pc.removeTrack(sender);
2330
+ }
2331
+ });
2336
2332
  }
2337
- else {
2338
- rtcStats.sendEvent("P2PRemoveStreamNoPC", {});
2339
- this._incrementAnalyticMetric("P2PRemoveStreamNoPC");
2333
+ else if (this.pc.removeStream) {
2334
+ this.pc.removeStream(stream);
2340
2335
  }
2341
2336
  }
2342
2337
  _setRemoteDescription(desc) {
@@ -2430,86 +2425,77 @@ class Session {
2430
2425
  }
2431
2426
  }
2432
2427
  hasConnectedPeerConnection() {
2433
- return this.pc && this.pc.connectionState === "connected";
2428
+ return this.pc.connectionState === "connected";
2434
2429
  }
2435
2430
  replaceTrack(oldTrack, newTrack) {
2436
- 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);
2437
- if (!newTrack) {
2438
- rtcStats.sendEvent("P2PReplaceTrackNoNewTrack", {
2439
- oldTrackId: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.id,
2440
- oldTrackKind: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.kind,
2441
- oldTrackIsEffect: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.effectTrack,
2442
- });
2443
- this._incrementAnalyticMetric("P2PReplaceTrackNoNewTrack");
2444
- return false;
2445
- }
2446
- if (newTrack.readyState === "ended") {
2447
- rtcStats.sendEvent("P2PReplaceTrackNewTrackEnded", {
2448
- newTrackId: newTrack.id,
2449
- newTrackKind: newTrack.kind,
2450
- newTrackIsEffect: newTrack.effectTrack,
2431
+ return __awaiter(this, void 0, void 0, function* () {
2432
+ logger$7.info("replacetrack() [oldTrackId: %s, newTrackId: %s]", oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.id, newTrack.id);
2433
+ if (newTrack.readyState === "ended") {
2434
+ rtcStats.sendEvent("P2PReplaceTrackNewTrackEnded", {
2435
+ newTrackId: newTrack.id,
2436
+ newTrackKind: newTrack.kind,
2437
+ newTrackIsEffect: trackAnnotations(newTrack).isEffectTrack,
2438
+ });
2439
+ this._incrementAnalyticMetric("P2PReplaceTrackNewTrackEnded");
2440
+ throw new Error(`refusing to replace track trackId: ${newTrack.id} kind: ${newTrack.kind} with readyState: ${newTrack.readyState}`);
2441
+ }
2442
+ const pc = this.pc;
2443
+ if (oldTrack) {
2444
+ const sender = pc.getSenders().find((s) => { var _a; return ((_a = s.track) === null || _a === void 0 ? void 0 : _a.id) === oldTrack.id; });
2445
+ if (sender) {
2446
+ return yield sender.replaceTrack(newTrack);
2447
+ }
2448
+ }
2449
+ const sender = pc.getSenders().find((s) => {
2450
+ const track = s.track;
2451
+ return (track === null || track === void 0 ? void 0 : track.kind) === newTrack.kind && !trackAnnotations(track).fromGetDisplayMedia;
2451
2452
  });
2452
- this._incrementAnalyticMetric("P2PReplaceTrackNewTrackEnded");
2453
- return false;
2454
- }
2455
- const pc = this.pc;
2456
- if (oldTrack) {
2457
- const sender = pc.getSenders().find((s) => { var _a; return ((_a = s.track) === null || _a === void 0 ? void 0 : _a.id) === oldTrack.id; });
2458
2453
  if (sender) {
2459
- sender.replaceTrack(newTrack);
2460
- return Promise.resolve(newTrack);
2454
+ this._incrementAnalyticMetric("P2PReplaceTrackOldTrackNotFound");
2455
+ const track = sender.track;
2456
+ rtcStats.sendEvent("P2PReplaceTrackOldTrackNotFound", {
2457
+ targetTrackId: track === null || track === void 0 ? void 0 : track.id,
2458
+ targetTrackKind: track === null || track === void 0 ? void 0 : track.kind,
2459
+ targetTrackIsEffect: track && trackAnnotations(track).isEffectTrack,
2460
+ targetTrackReadyState: track === null || track === void 0 ? void 0 : track.readyState,
2461
+ newTrackId: newTrack.id,
2462
+ newTrackKind: newTrack.kind,
2463
+ newTrackIsEffect: trackAnnotations(newTrack).isEffectTrack,
2464
+ oldTrackId: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.id,
2465
+ oldTrackKind: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.kind,
2466
+ oldTrackIsEffect: oldTrack && trackAnnotations(oldTrack).isEffectTrack,
2467
+ });
2468
+ return yield sender.replaceTrack(newTrack);
2461
2469
  }
2462
- }
2463
- const sender = pc.getSenders().find((s) => {
2464
- const track = s.track;
2465
- return (track === null || track === void 0 ? void 0 : track.kind) === newTrack.kind && !trackAnnotations(track).fromGetDisplayMedia;
2466
- });
2467
- if (sender) {
2468
- this._incrementAnalyticMetric("P2PReplaceTrackOldTrackNotFound");
2469
- const track = sender.track;
2470
- rtcStats.sendEvent("P2PReplaceTrackOldTrackNotFound", {
2471
- targetTrackId: track === null || track === void 0 ? void 0 : track.id,
2472
- targetTrackKind: track === null || track === void 0 ? void 0 : track.kind,
2473
- targetTrackIsEffect: track === null || track === void 0 ? void 0 : track.effectTrack,
2474
- targetTrackReadyState: track === null || track === void 0 ? void 0 : track.readyState,
2475
- newTrackId: newTrack.id,
2476
- newTrackKind: newTrack.kind,
2477
- newTrackIsEffect: newTrack.effectTrack,
2478
- oldTrackId: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.id,
2479
- oldTrackKind: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.kind,
2480
- oldTrackIsEffect: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.effectTrack,
2481
- });
2482
- sender.replaceTrack(newTrack);
2483
- return Promise.resolve(newTrack);
2484
- }
2485
- let stream = this.streams.find((s) => s.getTracks().find((t) => t.id === newTrack.id));
2486
- if (!stream) {
2487
- rtcStats.sendEvent("P2PReplaceTrackNewTrackNotInStream", {
2470
+ let stream = this.streams.find((s) => s.getTracks().find((t) => t.id === newTrack.id));
2471
+ if (!stream) {
2472
+ rtcStats.sendEvent("P2PReplaceTrackNewTrackNotInStream", {
2473
+ oldTrackId: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.id,
2474
+ oldTrackKind: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.kind,
2475
+ oldTrackIsEffect: oldTrack && trackAnnotations(oldTrack).isEffectTrack,
2476
+ newTrackId: newTrack.id,
2477
+ newTrackKind: newTrack.kind,
2478
+ newTrackIsEffect: trackAnnotations(newTrack).isEffectTrack,
2479
+ });
2480
+ this._incrementAnalyticMetric("P2PReplaceTrackNewTrackNotInStream");
2481
+ }
2482
+ stream = this.streams[0];
2483
+ if (!stream) {
2484
+ rtcStats.sendEvent("P2PReplaceTrackNoStream", {});
2485
+ this._incrementAnalyticMetric("P2PReplaceTrackNoStream");
2486
+ throw new Error("replaceTrack: No stream?");
2487
+ }
2488
+ rtcStats.sendEvent("P2PReplaceTrackSourceKindNotFound", {
2488
2489
  oldTrackId: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.id,
2489
2490
  oldTrackKind: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.kind,
2490
- oldTrackIsEffect: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.effectTrack,
2491
+ oldTrackIsEffect: oldTrack && trackAnnotations(oldTrack).isEffectTrack,
2491
2492
  newTrackId: newTrack.id,
2492
2493
  newTrackKind: newTrack.kind,
2493
- newTrackIsEffect: newTrack.effectTrack,
2494
+ newTrackIsEffect: trackAnnotations(newTrack).isEffectTrack,
2494
2495
  });
2495
- this._incrementAnalyticMetric("P2PReplaceTrackNewTrackNotInStream");
2496
- }
2497
- stream = this.streams[0];
2498
- if (!stream) {
2499
- rtcStats.sendEvent("P2PReplaceTrackNoStream", {});
2500
- this._incrementAnalyticMetric("P2PReplaceTrackNoStream");
2501
- return Promise.reject(new Error("replaceTrack: No stream?"));
2502
- }
2503
- rtcStats.sendEvent("P2PReplaceTrackSourceKindNotFound", {
2504
- oldTrackId: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.id,
2505
- oldTrackKind: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.kind,
2506
- oldTrackIsEffect: oldTrack === null || oldTrack === void 0 ? void 0 : oldTrack.effectTrack,
2507
- newTrackId: newTrack.id,
2508
- newTrackKind: newTrack.kind,
2509
- newTrackIsEffect: newTrack.effectTrack,
2496
+ this._incrementAnalyticMetric("P2PReplaceTrackSourceKindNotFound");
2497
+ pc.addTrack(newTrack, stream);
2510
2498
  });
2511
- this._incrementAnalyticMetric("P2PReplaceTrackSourceKindNotFound");
2512
- return pc.addTrack(newTrack, stream);
2513
2499
  }
2514
2500
  changeBandwidth(bandwidth) {
2515
2501
  var _a;
@@ -2546,39 +2532,6 @@ class Session {
2546
2532
 
2547
2533
  const MEDIA_JITTER_BUFFER_TARGET = 400;
2548
2534
 
2549
- var _a$3;
2550
- const adapter$3 = (_a$3 = adapterRaw.default) !== null && _a$3 !== void 0 ? _a$3 : adapterRaw;
2551
- function detectMicrophoneNotWorking(pc) {
2552
- var _a, _b;
2553
- if (((_a = adapter$3.browserDetails) === null || _a === void 0 ? void 0 : _a.browser) !== "chrome" ||
2554
- ((_b = adapter$3.browserDetails) === null || _b === void 0 ? void 0 : _b.browser) < 58 ||
2555
- pc.signalingState === "closed") {
2556
- return Promise.resolve(false);
2557
- }
2558
- const sendingAudio = pc.getSenders().some((sender) => sender.track && sender.track.kind === "audio");
2559
- const receivingAudio = pc.getReceivers().some((receiver) => receiver.track && receiver.track.kind === "audio");
2560
- return pc.getStats(null).then((result) => {
2561
- let microphoneFailed = false;
2562
- result.forEach((report) => {
2563
- if (report.type === "outbound-rtp" &&
2564
- (report.kind === "audio" || report.mediaType === "audio") &&
2565
- sendingAudio) {
2566
- if (report.bytesSent === 0) {
2567
- microphoneFailed = "outbound";
2568
- }
2569
- }
2570
- else if (report.type === "inbound-rtp" &&
2571
- (report.kind === "audio" || report.mediaType === "audio") &&
2572
- receivingAudio) {
2573
- if (report.bytesReceived === 0) {
2574
- microphoneFailed = "inbound";
2575
- }
2576
- }
2577
- });
2578
- return microphoneFailed;
2579
- });
2580
- }
2581
-
2582
2535
  const EVENTS = {
2583
2536
  CLIENT_CONNECTION_STATUS_CHANGED: "client_connection_status_changed",
2584
2537
  STREAM_ADDED: "stream_added",
@@ -2616,14 +2569,14 @@ const STREAM_TYPES = {
2616
2569
  screenshare: "screenshare",
2617
2570
  };
2618
2571
 
2619
- var _a$2, _b$1, _c;
2620
- const adapter$2 = (_a$2 = adapterRaw.default) !== null && _a$2 !== void 0 ? _a$2 : adapterRaw;
2572
+ var _a$3, _b$1, _c;
2573
+ const adapter$3 = (_a$3 = adapterRaw.default) !== null && _a$3 !== void 0 ? _a$3 : adapterRaw;
2621
2574
  const logger$6 = new Logger();
2622
2575
  const ICE_PUBLIC_IP_GATHERING_TIMEOUT = 3 * 1000;
2623
- const browserName$1 = (_b$1 = adapter$2.browserDetails) === null || _b$1 === void 0 ? void 0 : _b$1.browser;
2624
- const browserVersion = adapter$2.browserDetails.version;
2625
- if (browserName$1 === "firefox" && adapter$2.browserShim && "shimGetDisplayMedia" in adapter$2.browserShim) {
2626
- (_c = adapter$2.browserShim) === null || _c === void 0 ? void 0 : _c.shimGetDisplayMedia(window, "screen");
2576
+ const browserName$1 = (_b$1 = adapter$3.browserDetails) === null || _b$1 === void 0 ? void 0 : _b$1.browser;
2577
+ const browserVersion = adapter$3.browserDetails.version;
2578
+ if (browserName$1 === "firefox" && adapter$3.browserShim && "shimGetDisplayMedia" in adapter$3.browserShim) {
2579
+ (_c = adapter$3.browserShim) === null || _c === void 0 ? void 0 : _c.shimGetDisplayMedia(window, "screen");
2627
2580
  }
2628
2581
  let unloading$1 = false;
2629
2582
  if (browserName$1 === "chrome") {
@@ -2651,7 +2604,6 @@ class P2pRtcManager {
2651
2604
  this._closed = false;
2652
2605
  this.skipEmittingServerMessageCount = 0;
2653
2606
  this.offerOptions = { offerToReceiveAudio: true, offerToReceiveVideo: true };
2654
- this._pendingActionsForConnectedPeerConnections = [];
2655
2607
  this._audioTrackOnEnded = () => {
2656
2608
  rtcStats.sendEvent("audio_ended", { unloading: unloading$1 });
2657
2609
  this._emit(rtcManagerEvents.MICROPHONE_STOPPED_WORKING, {});
@@ -2684,10 +2636,8 @@ class P2pRtcManager {
2684
2636
  numPcOnAnswerFailure: 0,
2685
2637
  numPcOnOfferFailure: 0,
2686
2638
  P2PChangeBandwidthEmptySDPType: 0,
2687
- P2PNegotiationNeeded: 0,
2688
2639
  P2PSessionAlreadyCreated: 0,
2689
2640
  P2PReplaceTrackNoStream: 0,
2690
- P2PReplaceTrackNoNewTrack: 0,
2691
2641
  P2PReplaceTrackNewTrackEnded: 0,
2692
2642
  P2PReplaceTrackNewTrackNotInStream: 0,
2693
2643
  P2PReplaceTrackOldTrackNotFound: 0,
@@ -2721,7 +2671,7 @@ class P2pRtcManager {
2721
2671
  const audioTrack = stream.getAudioTracks()[0];
2722
2672
  const videoTrack = stream.getVideoTracks()[0];
2723
2673
  if (audioTrack) {
2724
- if (!audioTrack.effectTrack) {
2674
+ if (!trackAnnotations(audioTrack).isEffectTrack) {
2725
2675
  this._monitorAudioTrack(audioTrack);
2726
2676
  }
2727
2677
  const beforeEffectTrack = beforeEffectTracks.find((t) => t.kind === "audio");
@@ -2730,7 +2680,7 @@ class P2pRtcManager {
2730
2680
  }
2731
2681
  }
2732
2682
  if (videoTrack) {
2733
- if (!videoTrack.effectTrack) {
2683
+ if (!trackAnnotations(videoTrack).isEffectTrack) {
2734
2684
  this._monitorVideoTrack(videoTrack);
2735
2685
  }
2736
2686
  const beforeEffectTrack = beforeEffectTracks.find((t) => t.kind === "video");
@@ -2757,10 +2707,10 @@ class P2pRtcManager {
2757
2707
  return;
2758
2708
  }
2759
2709
  replaceTrack(oldTrack, newTrack) {
2760
- if (newTrack.kind === "audio" && !newTrack.effectTrack) {
2710
+ if (newTrack.kind === "audio" && !trackAnnotations(newTrack).isEffectTrack) {
2761
2711
  this._monitorAudioTrack(newTrack);
2762
2712
  }
2763
- if (newTrack.kind === "video" && !newTrack.effectTrack) {
2713
+ if (newTrack.kind === "video" && !trackAnnotations(newTrack).isEffectTrack) {
2764
2714
  this._monitorVideoTrack(newTrack);
2765
2715
  }
2766
2716
  return this._replaceTrackToPeerConnections(oldTrack, newTrack);
@@ -2851,25 +2801,6 @@ class P2pRtcManager {
2851
2801
  this.analytics.numPcOnAnswerFailure++;
2852
2802
  });
2853
2803
  }),
2854
- this._serverSocket.on(PROTOCOL_RESPONSES.ROOM_JOINED, (payload) => {
2855
- if ("error" in payload || !this._wasScreenSharing) {
2856
- return;
2857
- }
2858
- const screenShareStreamId = Object.keys(this.localStreams).find((id) => id !== CAMERA_STREAM_ID);
2859
- if (!screenShareStreamId) {
2860
- return;
2861
- }
2862
- const screenshareStream = this.localStreams[screenShareStreamId];
2863
- if (!screenshareStream) {
2864
- logger$6.warn("screenshare stream %s not found", screenShareStreamId);
2865
- return;
2866
- }
2867
- const hasAudioTrack = screenshareStream.getAudioTracks().length > 0;
2868
- this._emitServerEvent(PROTOCOL_REQUESTS.START_SCREENSHARE, {
2869
- streamId: screenShareStreamId,
2870
- hasAudioTrack,
2871
- });
2872
- }),
2873
2804
  this._serverSocket.on(PROTOCOL_RESPONSES.SCREENSHARE_STOPPED, (payload) => {
2874
2805
  const session = this._getSession(payload.clientId);
2875
2806
  if (session) {
@@ -2987,31 +2918,6 @@ class P2pRtcManager {
2987
2918
  }
2988
2919
  return this.peerConnections[peerConnectionId];
2989
2920
  }
2990
- _getOrCreateSession({ peerConnectionId, clientId, initialBandwidth, peerConnectionConfig, }) {
2991
- let session = this.peerConnections[peerConnectionId];
2992
- if (session === undefined) {
2993
- const deprioritizeH264Encoding = browserName$1 === "safari" &&
2994
- browserVersion &&
2995
- browserVersion >= 14 &&
2996
- this._features.deprioritizeH264OnSafari;
2997
- this.peerConnections[peerConnectionId] = session = new Session({
2998
- peerConnectionId,
2999
- clientId,
3000
- peerConnectionConfig,
3001
- bandwidth: initialBandwidth,
3002
- deprioritizeH264Encoding,
3003
- incrementAnalyticMetric: (metric) => this.analytics[metric]++,
3004
- });
3005
- }
3006
- else {
3007
- this.analytics.P2PSessionAlreadyCreated++;
3008
- rtcStats.sendEvent("P2PSessionAlreadyCreated", {
3009
- clientId,
3010
- peerConnectionId,
3011
- });
3012
- }
3013
- return session;
3014
- }
3015
2921
  _getLocalCameraStream() {
3016
2922
  return this.localStreams[CAMERA_STREAM_ID];
3017
2923
  }
@@ -3025,7 +2931,7 @@ class P2pRtcManager {
3025
2931
  const streamIds = this._getNonLocalCameraStreamIds();
3026
2932
  return streamIds.length === 0 ? null : this.localStreams[streamIds[0]];
3027
2933
  }
3028
- _createSession({ clientId, initialBandwidth, isOfferer, peerConnectionId, }) {
2934
+ _createSession({ clientId, initialBandwidth, isOfferer, peerConnectionId }) {
3029
2935
  if (!peerConnectionId) {
3030
2936
  throw new Error("peerConnectionId is missing");
3031
2937
  }
@@ -3035,26 +2941,22 @@ class P2pRtcManager {
3035
2941
  const peerConnectionConfig = {
3036
2942
  iceServers: this._features.turnServersOn ? this._turnServers : this._iceServers,
3037
2943
  };
3038
- const constraints = { optional: [] };
3039
- constraints.optional.push({ rtcStatsRoomSessionId: this._roomSessionId });
3040
- constraints.optional.push({ rtcStatsClientId: this._selfId });
3041
- constraints.optional.push({ rtcStatsPeerId: peerConnectionId });
3042
- constraints.optional.push({ rtcStatsConferenceId: this._roomName });
3043
- if (browserName$1 === "chrome") {
3044
- constraints.optional.push({
3045
- googCpuOveruseDetection: true,
3046
- });
3047
- peerConnectionConfig.sdpSemantics = "unified-plan";
3048
- }
3049
2944
  peerConnectionConfig.iceServers = turnServerOverride(peerConnectionConfig.iceServers, this._features.turnServerOverrideHost);
3050
2945
  external_stun_servers(peerConnectionConfig, this._features);
3051
2946
  maybeTurnOnly(peerConnectionConfig, this._features);
3052
- const session = this._getOrCreateSession({
2947
+ const deprioritizeH264Encoding = browserName$1 === "safari" &&
2948
+ browserVersion &&
2949
+ browserVersion >= 14 &&
2950
+ this._features.deprioritizeH264OnSafari;
2951
+ const session = new Session({
3053
2952
  peerConnectionId,
3054
2953
  clientId,
3055
- initialBandwidth,
3056
2954
  peerConnectionConfig,
2955
+ bandwidth: initialBandwidth,
2956
+ deprioritizeH264Encoding,
2957
+ incrementAnalyticMetric: (metric) => this.analytics[metric]++,
3057
2958
  });
2959
+ this.peerConnections[peerConnectionId] = session;
3058
2960
  setTimeout(() => this._emit(rtcManagerEvents.NEW_PC), 0);
3059
2961
  this.analytics.numNewPc++;
3060
2962
  const { pc } = session;
@@ -3077,6 +2979,7 @@ class P2pRtcManager {
3077
2979
  }
3078
2980
  };
3079
2981
  pc.oniceconnectionstatechange = () => {
2982
+ var _a;
3080
2983
  let newStatus;
3081
2984
  const currentStatus = session.connectionStatus;
3082
2985
  switch (pc.iceConnectionState) {
@@ -3086,14 +2989,10 @@ class P2pRtcManager {
3086
2989
  case "connected":
3087
2990
  case "completed":
3088
2991
  newStatus = TYPES.CONNECTION_SUCCESSFUL;
3089
- if (!session.wasEverConnected) {
3090
- this._pendingActionsForConnectedPeerConnections.forEach((action) => {
3091
- if (typeof action === "function") {
3092
- action();
3093
- }
3094
- });
3095
- this._pendingActionsForConnectedPeerConnections = [];
3096
- }
2992
+ session.pendingReplaceTrackActions.forEach((action) => __awaiter(this, void 0, void 0, function* () {
2993
+ yield action();
2994
+ }));
2995
+ session.pendingReplaceTrackActions = [];
3097
2996
  if (!session.wasEverConnected &&
3098
2997
  (pc.iceConnectionState.match(/connected|completed/) ||
3099
2998
  (browserName$1 === "chrome" && pc.localDescription && pc.localDescription.type === "answer"))) {
@@ -3102,7 +3001,7 @@ class P2pRtcManager {
3102
3001
  if (this._isAudioOnlyMode) {
3103
3002
  session.setAudioOnly(true, this._screenshareVideoTrackIds);
3104
3003
  }
3105
- session.registerConnected();
3004
+ (_a = session.registerConnected) === null || _a === void 0 ? void 0 : _a.call(session, {});
3106
3005
  break;
3107
3006
  case "disconnected":
3108
3007
  newStatus = TYPES.CONNECTION_DISCONNECTED;
@@ -3126,28 +3025,19 @@ class P2pRtcManager {
3126
3025
  }
3127
3026
  this._setConnectionStatus(session, newStatus, clientId);
3128
3027
  };
3129
- pc.onconnectionstate = () => {
3028
+ pc.onconnectionstatechange = () => {
3029
+ var _a;
3130
3030
  switch (pc.connectionState) {
3131
3031
  case "connected":
3132
- setTimeout(() => {
3133
- detectMicrophoneNotWorking(session.pc).then((failureDirection) => {
3134
- if (failureDirection !== false) {
3135
- this._emit(rtcManagerEvents.MICROPHONE_NOT_WORKING, {
3136
- failureDirection,
3137
- clientId,
3138
- });
3139
- }
3140
- });
3141
- }, 3000);
3142
- session.registerConnected();
3032
+ (_a = session.registerConnected) === null || _a === void 0 ? void 0 : _a.call(session, {});
3143
3033
  break;
3144
3034
  case "failed":
3145
3035
  const newStatus = TYPES.CONNECTION_FAILED;
3146
- if (session.relayCandidateSeen === null && !session.wasEverConnected) {
3147
- this._emit(rtcManagerEvents.CONNECTION_BLOCKED_BY_NETWORK);
3148
- }
3149
3036
  this._setConnectionStatus(session, newStatus, clientId);
3150
3037
  break;
3038
+ case "closed":
3039
+ this._cleanup(session.clientId);
3040
+ break;
3151
3041
  }
3152
3042
  };
3153
3043
  const localCameraStream = this.localStreams[CAMERA_STREAM_ID];
@@ -3203,43 +3093,34 @@ class P2pRtcManager {
3203
3093
  const promises = [];
3204
3094
  this._forEachPeerConnection((session) => {
3205
3095
  if (!session.hasConnectedPeerConnection()) {
3206
- rtcStats.sendEvent("P2PReplaceTrackWithoutPC", {});
3207
- this.analytics.P2PReplaceTrackWithoutPC++;
3208
- logger$6.info("Session doesn't have a connected PeerConnection, adding pending action!");
3209
- const pendingActions = this._pendingActionsForConnectedPeerConnections;
3210
- if (!pendingActions) {
3211
- rtcStats.sendEvent("P2PReplaceTrackToPCsPendingActionsNull", {});
3212
- this.analytics.P2PReplaceTrackToPCsPendingActionsNull++;
3213
- logger$6.warn(`No pending action is created to replace track, because the pending actions array is null`);
3096
+ if (session.pc.connectionState === "closed")
3214
3097
  return;
3215
- }
3098
+ logger$6.info("Session doesn't have a connected PeerConnection, adding pending action!");
3099
+ this.analytics.P2PReplaceTrackWithoutPC++;
3100
+ rtcStats.sendEvent("P2PReplaceTrackWithoutPC", {
3101
+ connectionState: session.pc.connectionState,
3102
+ iceConnectionState: session.pc.iceConnectionState,
3103
+ });
3216
3104
  const promise = new Promise((resolve, reject) => {
3217
- const action = () => {
3218
- const replacedTrackPromise = session.replaceTrack(oldTrack, newTrack);
3219
- if (!replacedTrackPromise) {
3220
- rtcStats.sendEvent("P2PReplaceTrackReturnedFalse", {});
3221
- this.analytics.P2PReplaceTrackReturnedFalse++;
3222
- logger$6.error("replaceTrack returned false!");
3223
- reject(`ReplaceTrack returned false`);
3224
- return;
3105
+ const action = () => __awaiter(this, void 0, void 0, function* () {
3106
+ try {
3107
+ yield session.replaceTrack(oldTrack, newTrack);
3108
+ resolve({});
3225
3109
  }
3226
- replacedTrackPromise.then((track) => resolve(track)).catch((error) => reject(error));
3227
- };
3228
- pendingActions.push(action);
3110
+ catch (error) {
3111
+ reject(error);
3112
+ }
3113
+ });
3114
+ session.pendingReplaceTrackActions.push(action);
3229
3115
  });
3230
3116
  promises.push(promise);
3231
3117
  return;
3232
3118
  }
3233
- const replacedTrackResult = session.replaceTrack(oldTrack, newTrack);
3234
- if (!replacedTrackResult) {
3235
- rtcStats.sendEvent("P2PReplaceTrackReturnedFalse", {});
3236
- this.analytics.P2PReplaceTrackReturnedFalse++;
3237
- logger$6.error("replaceTrack returned false!");
3238
- return;
3239
- }
3240
- promises.push(replacedTrackResult);
3119
+ promises.push(session.replaceTrack(oldTrack, newTrack));
3120
+ });
3121
+ return Promise.all(promises).catch((error) => {
3122
+ logger$6.error(String(error));
3241
3123
  });
3242
- return Promise.all(promises);
3243
3124
  }
3244
3125
  _removeStreamFromPeerConnections(stream) {
3245
3126
  this._forEachPeerConnection((session) => {
@@ -3311,7 +3192,7 @@ class P2pRtcManager {
3311
3192
  isOfferer: true,
3312
3193
  });
3313
3194
  this._negotiatePeerConnection(clientId, session);
3314
- return Promise.resolve(session);
3195
+ return session;
3315
3196
  }
3316
3197
  _maybeRestartIce(clientId, session) {
3317
3198
  const pc = session.pc;
@@ -3574,8 +3455,6 @@ class P2pRtcManager {
3574
3455
  }
3575
3456
  };
3576
3457
  pc.onnegotiationneeded = () => {
3577
- this.analytics.P2PNegotiationNeeded++;
3578
- rtcStats.sendEvent("P2PNegotiationNeeded", {});
3579
3458
  if (pc.iceConnectionState === "new" || !session.connectionStatus) {
3580
3459
  return;
3581
3460
  }
@@ -3590,7 +3469,6 @@ class P2pRtcManager {
3590
3469
  }
3591
3470
  let initialBandwidth = (session && session.bandwidth) || 0;
3592
3471
  if (session) {
3593
- logger$6.warn("Replacing peer session", clientId);
3594
3472
  this._cleanup(clientId);
3595
3473
  }
3596
3474
  else {
@@ -3642,9 +3520,7 @@ class P2pRtcManager {
3642
3520
  return;
3643
3521
  }
3644
3522
  if (enable === false) {
3645
- const stopCameraDelay = ((_a = localStream.getVideoTracks().find((t) => !t.enabled)) === null || _a === void 0 ? void 0 : _a.readyState) === "ended"
3646
- ? 0
3647
- : 5000;
3523
+ const stopCameraDelay = ((_a = localStream.getVideoTracks().find((t) => !t.enabled)) === null || _a === void 0 ? void 0 : _a.readyState) === "ended" ? 0 : 5000;
3648
3524
  setTimeout(() => {
3649
3525
  localStream.getVideoTracks().forEach((track) => {
3650
3526
  if (track.enabled === false) {
@@ -4532,10 +4408,10 @@ function createVegaConnectionManager(config) {
4532
4408
  return { connect, updateHostList, networkIsUp, networkIsPossiblyDown };
4533
4409
  }
4534
4410
 
4535
- var _a$1;
4536
- const adapter$1 = (_a$1 = adapterRaw.default) !== null && _a$1 !== void 0 ? _a$1 : adapterRaw;
4411
+ var _a$2;
4412
+ const adapter$2 = (_a$2 = adapterRaw.default) !== null && _a$2 !== void 0 ? _a$2 : adapterRaw;
4537
4413
  const logger$2 = new Logger();
4538
- const browserName = adapter$1.browserDetails.browser;
4414
+ const browserName = adapter$2.browserDetails.browser;
4539
4415
  let unloading = false;
4540
4416
  const RESTARTICE_ERROR_RETRY_THRESHOLD_IN_MS = 3500;
4541
4417
  const RESTARTICE_ERROR_MAX_RETRY_COUNT = 5;
@@ -5477,14 +5353,14 @@ class VegaRtcManager {
5477
5353
  }
5478
5354
  replaceTrack(oldTrack, track) {
5479
5355
  if (track.kind === "audio") {
5480
- if (!track.effectTrack) {
5356
+ if (!trackAnnotations(track).isEffectTrack) {
5481
5357
  this._monitorAudioTrack(track);
5482
5358
  }
5483
5359
  this._micTrack = track;
5484
5360
  this._replaceMicTrack();
5485
5361
  }
5486
5362
  if (track.kind === "video") {
5487
- if (!track.effectTrack) {
5363
+ if (!trackAnnotations(track).isEffectTrack) {
5488
5364
  this._monitorVideoTrack(track);
5489
5365
  }
5490
5366
  this._webcamTrack = track;
@@ -5517,7 +5393,7 @@ class VegaRtcManager {
5517
5393
  const audioTrack = stream.getAudioTracks()[0];
5518
5394
  if (videoTrack) {
5519
5395
  this._sendWebcam(videoTrack);
5520
- if (!videoTrack.effectTrack) {
5396
+ if (!trackAnnotations(videoTrack).isEffectTrack) {
5521
5397
  this._monitorVideoTrack(videoTrack);
5522
5398
  }
5523
5399
  const beforeEffectTrack = beforeEffectTracks.find((t) => t.kind === "video");
@@ -5528,7 +5404,7 @@ class VegaRtcManager {
5528
5404
  if (audioTrack) {
5529
5405
  this._sendMic(audioTrack);
5530
5406
  this._syncMicAnalyser();
5531
- if (!audioTrack.effectTrack) {
5407
+ if (!trackAnnotations(audioTrack).isEffectTrack) {
5532
5408
  this._monitorAudioTrack(audioTrack);
5533
5409
  }
5534
5410
  const beforeEffectTrack = beforeEffectTracks.find((t) => t.kind === "audio");
@@ -5602,9 +5478,7 @@ class VegaRtcManager {
5602
5478
  }
5603
5479
  if (!enable) {
5604
5480
  clearTimeout(this._stopCameraTimeout);
5605
- const stopCameraDelay = ((_a = localStream.getVideoTracks().find((t) => !t.enabled)) === null || _a === void 0 ? void 0 : _a.readyState) === "ended"
5606
- ? 0
5607
- : 5000;
5481
+ const stopCameraDelay = ((_a = localStream.getVideoTracks().find((t) => !t.enabled)) === null || _a === void 0 ? void 0 : _a.readyState) === "ended" ? 0 : 5000;
5608
5482
  this._stopCameraTimeout = setTimeout(() => {
5609
5483
  localStream.getVideoTracks().forEach((track) => {
5610
5484
  if (track.enabled === false) {
@@ -7148,6 +7022,39 @@ class BandwidthTester extends EventEmitter {
7148
7022
  }
7149
7023
  }
7150
7024
 
7025
+ var _a$1;
7026
+ const adapter$1 = (_a$1 = adapterRaw.default) !== null && _a$1 !== void 0 ? _a$1 : adapterRaw;
7027
+ function detectMicrophoneNotWorking(pc) {
7028
+ var _a, _b;
7029
+ if (((_a = adapter$1.browserDetails) === null || _a === void 0 ? void 0 : _a.browser) !== "chrome" ||
7030
+ ((_b = adapter$1.browserDetails) === null || _b === void 0 ? void 0 : _b.browser) < 58 ||
7031
+ pc.signalingState === "closed") {
7032
+ return Promise.resolve(false);
7033
+ }
7034
+ const sendingAudio = pc.getSenders().some((sender) => sender.track && sender.track.kind === "audio");
7035
+ const receivingAudio = pc.getReceivers().some((receiver) => receiver.track && receiver.track.kind === "audio");
7036
+ return pc.getStats(null).then((result) => {
7037
+ let microphoneFailed = false;
7038
+ result.forEach((report) => {
7039
+ if (report.type === "outbound-rtp" &&
7040
+ (report.kind === "audio" || report.mediaType === "audio") &&
7041
+ sendingAudio) {
7042
+ if (report.bytesSent === 0) {
7043
+ microphoneFailed = "outbound";
7044
+ }
7045
+ }
7046
+ else if (report.type === "inbound-rtp" &&
7047
+ (report.kind === "audio" || report.mediaType === "audio") &&
7048
+ receivingAudio) {
7049
+ if (report.bytesReceived === 0) {
7050
+ microphoneFailed = "inbound";
7051
+ }
7052
+ }
7053
+ });
7054
+ return microphoneFailed;
7055
+ });
7056
+ }
7057
+
7151
7058
  var _a, _b;
7152
7059
  const adapter = (_a = adapterRaw.default) !== null && _a !== void 0 ? _a : adapterRaw;
7153
7060
  const isSafari = ((_b = adapter.browserDetails) === null || _b === void 0 ? void 0 : _b.browser) === "safari";
@@ -7309,22 +7216,13 @@ function replaceTracksInStream(stream, newStream, only) {
7309
7216
  const replacedTracks = [];
7310
7217
  if (!only || only === "audio") {
7311
7218
  replacedTracks.push(...stream.getAudioTracks());
7312
- newStream.getAudioTracks().forEach((track) => {
7313
- track.replacement = true;
7314
- stream.addTrack(track);
7315
- });
7219
+ newStream.getAudioTracks().forEach((track) => stream.addTrack(track));
7316
7220
  }
7317
7221
  if (!only || only === "video") {
7318
7222
  replacedTracks.push(...stream.getVideoTracks());
7319
- newStream.getVideoTracks().forEach((track) => {
7320
- track.replacement = true;
7321
- stream.addTrack(track);
7322
- });
7223
+ newStream.getVideoTracks().forEach((track) => stream.addTrack(track));
7323
7224
  }
7324
- replacedTracks.forEach((track) => {
7325
- track.replaced = true;
7326
- stream.removeTrack(track);
7327
- });
7225
+ replacedTracks.forEach((track) => stream.removeTrack(track));
7328
7226
  return replacedTracks;
7329
7227
  }
7330
7228
  function getStream(constraintOpt_1) {