@cloudflare/realtimekit-ui 2.0.3-staging.2 → 2.0.3-staging.4

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.
@@ -79,10 +79,7 @@ const RtkGrid = class {
79
79
  return;
80
80
  this.updateActiveParticipants();
81
81
  };
82
- this.onParticipantJoined = debounce.debounce(() => {
83
- this.updateActiveParticipants();
84
- }, MASS_ACTIONS_DEBOUNCE_TIMER);
85
- this.onParticipantLeft = debounce.debounce(() => {
82
+ this.refreshParticipants = debounce.debounce(() => {
86
83
  this.updateActiveParticipants();
87
84
  }, MASS_ACTIONS_DEBOUNCE_TIMER);
88
85
  this.stageStatusListener = () => {
@@ -149,6 +146,7 @@ const RtkGrid = class {
149
146
  var _a;
150
147
  if (!meeting)
151
148
  return;
149
+ this.refreshParticipants.cancel();
152
150
  this.participants = [];
153
151
  this.plugins = [];
154
152
  const { self, participants, plugins } = meeting;
@@ -161,8 +159,9 @@ const RtkGrid = class {
161
159
  plugins === null || plugins === void 0 ? void 0 : plugins.all.removeListener('stateUpdate', this.onPluginStateUpdate);
162
160
  (_a = meeting.stage) === null || _a === void 0 ? void 0 : _a.removeListener('stageStatusUpdate', this.stageStatusListener);
163
161
  participants.removeListener('viewModeChanged', this.onViewModeChanged);
164
- participants.active.removeListener('participantLeft', this.onParticipantLeft);
165
- participants.active.removeListener('participantJoined', this.onParticipantJoined);
162
+ participants.active.removeListener('participantLeft', this.refreshParticipants);
163
+ participants.active.removeListener('participantJoined', this.refreshParticipants);
164
+ participants.active.removeListener('participantsUpdate', this.refreshParticipants);
166
165
  participants.pinned.removeListener('participantJoined', this.onParticipantPinned);
167
166
  participants.pinned.removeListener('participantLeft', this.onParticipantUnpinned);
168
167
  participants.joined.removeListener('screenShareUpdate', this.onScreenShareUpdate);
@@ -217,10 +216,11 @@ const RtkGrid = class {
217
216
  stage === null || stage === void 0 ? void 0 : stage.addListener('stageStatusUpdate', this.stageStatusListener);
218
217
  plugins === null || plugins === void 0 ? void 0 : plugins.all.addListener('stateUpdate', this.onPluginStateUpdate);
219
218
  participants.addListener('viewModeChanged', this.onViewModeChanged);
220
- participants.active.addListener('participantLeft', this.onParticipantLeft);
219
+ participants.active.addListener('participantLeft', this.refreshParticipants);
221
220
  (_c = participants === null || participants === void 0 ? void 0 : participants.joined) === null || _c === void 0 ? void 0 : _c.on('stageStatusUpdate', this.peerStageStatusListener);
222
221
  participants.joined.addListener('screenShareUpdate', this.onScreenShareUpdate);
223
- participants.active.addListener('participantJoined', this.onParticipantJoined);
222
+ participants.active.addListener('participantJoined', this.refreshParticipants);
223
+ participants.active.addListener('participantsUpdate', this.refreshParticipants);
224
224
  participants.pinned.addListener('participantJoined', this.onParticipantPinned);
225
225
  participants.pinned.addListener('participantLeft', this.onParticipantUnpinned);
226
226
  if ((_d = meeting.stage) === null || _d === void 0 ? void 0 : _d.status) {
@@ -17,21 +17,64 @@ class RTKAudio {
17
17
  this.logger = meeting.__internals__.logger;
18
18
  this.audioStream = new MediaStream();
19
19
  this.audio.srcObject = this.audioStream;
20
- this.audioTracks = [];
20
+ this.trackIdByAudioIdMap = new Map();
21
+ this.trackOwners = new Map();
21
22
  }
22
- addTrack(id, track) {
23
- // Remove the track if it already exists, with the same id
24
- this.removeTrack(id);
25
- this.audioTracks.push({ id, track });
26
- this.audioStream.addTrack(track);
23
+ addTrack(audioId, track) {
24
+ /**
25
+ * NOTE(ravindra-cloudflare):
26
+ * audioId format: audio-${peerId} or screenshare-${peerId}.
27
+ *
28
+ * During reconnection, the old and new peer IDs of the same participant
29
+ * can temporarily co-own the same track.
30
+ *
31
+ * Before adding track, we should check if the track is already added with a different audio ID.
32
+ */
33
+ const currentTrackId = this.trackIdByAudioIdMap.get(audioId);
34
+ // Track already added, just play it
35
+ if (currentTrackId === track.id) {
36
+ this.play();
37
+ return;
38
+ }
39
+ // A logical audio ID owns one track. Release its previous track before replacing it.
40
+ if (currentTrackId)
41
+ this.removeTrack(audioId);
42
+ this.trackIdByAudioIdMap.set(audioId, track.id);
43
+ const owners = this.trackOwners.get(track.id);
44
+ if (owners) {
45
+ // Set membership is idempotent when reconnect events repeat or arrive out of order.
46
+ owners.audioIds.add(audioId);
47
+ }
48
+ else {
49
+ // If you are the first owner of this track, add it to the audio stream
50
+ this.trackOwners.set(track.id, { track, audioIds: new Set([audioId]) });
51
+ this.audioStream.addTrack(track);
52
+ }
27
53
  this.play();
28
54
  }
29
- removeTrack(id) {
30
- const track = this.audioTracks.find((a) => a.id === id);
31
- if (track != null) {
32
- this.audioStream.removeTrack(track.track);
33
- this.audioTracks = this.audioTracks.filter((a) => a.id !== id);
34
- }
55
+ removeTrack(audioId) {
56
+ /**
57
+ * NOTE(ravindra-cloudflare):
58
+ * audioId format: audio-${peerId} or screenshare-${peerId}.
59
+ *
60
+ * During reconnection, the old and new peer IDs of the same participant
61
+ * can temporarily co-own the same track.
62
+ *
63
+ * Before removing track, we should check if the track is still owned by other audio IDs.
64
+ */
65
+ const trackId = this.trackIdByAudioIdMap.get(audioId);
66
+ if (!trackId)
67
+ return;
68
+ this.trackIdByAudioIdMap.delete(audioId);
69
+ const owners = this.trackOwners.get(trackId);
70
+ if (!owners)
71
+ return;
72
+ owners.audioIds.delete(audioId);
73
+ if (owners.audioIds.size > 0)
74
+ return;
75
+ // Remove the physical track only after its final logical owner is gone.
76
+ this.audioStream.removeTrack(owners.track);
77
+ this.trackOwners.delete(trackId);
35
78
  }
36
79
  async play() {
37
80
  var _a;
@@ -76,10 +76,7 @@ export class RtkGrid {
76
76
  return;
77
77
  this.updateActiveParticipants();
78
78
  };
79
- this.onParticipantJoined = debounce(() => {
80
- this.updateActiveParticipants();
81
- }, MASS_ACTIONS_DEBOUNCE_TIMER);
82
- this.onParticipantLeft = debounce(() => {
79
+ this.refreshParticipants = debounce(() => {
83
80
  this.updateActiveParticipants();
84
81
  }, MASS_ACTIONS_DEBOUNCE_TIMER);
85
82
  this.stageStatusListener = () => {
@@ -146,6 +143,7 @@ export class RtkGrid {
146
143
  var _a;
147
144
  if (!meeting)
148
145
  return;
146
+ this.refreshParticipants.cancel();
149
147
  this.participants = [];
150
148
  this.plugins = [];
151
149
  const { self, participants, plugins } = meeting;
@@ -158,8 +156,9 @@ export class RtkGrid {
158
156
  plugins === null || plugins === void 0 ? void 0 : plugins.all.removeListener('stateUpdate', this.onPluginStateUpdate);
159
157
  (_a = meeting.stage) === null || _a === void 0 ? void 0 : _a.removeListener('stageStatusUpdate', this.stageStatusListener);
160
158
  participants.removeListener('viewModeChanged', this.onViewModeChanged);
161
- participants.active.removeListener('participantLeft', this.onParticipantLeft);
162
- participants.active.removeListener('participantJoined', this.onParticipantJoined);
159
+ participants.active.removeListener('participantLeft', this.refreshParticipants);
160
+ participants.active.removeListener('participantJoined', this.refreshParticipants);
161
+ participants.active.removeListener('participantsUpdate', this.refreshParticipants);
163
162
  participants.pinned.removeListener('participantJoined', this.onParticipantPinned);
164
163
  participants.pinned.removeListener('participantLeft', this.onParticipantUnpinned);
165
164
  participants.joined.removeListener('screenShareUpdate', this.onScreenShareUpdate);
@@ -214,10 +213,11 @@ export class RtkGrid {
214
213
  stage === null || stage === void 0 ? void 0 : stage.addListener('stageStatusUpdate', this.stageStatusListener);
215
214
  plugins === null || plugins === void 0 ? void 0 : plugins.all.addListener('stateUpdate', this.onPluginStateUpdate);
216
215
  participants.addListener('viewModeChanged', this.onViewModeChanged);
217
- participants.active.addListener('participantLeft', this.onParticipantLeft);
216
+ participants.active.addListener('participantLeft', this.refreshParticipants);
218
217
  (_c = participants === null || participants === void 0 ? void 0 : participants.joined) === null || _c === void 0 ? void 0 : _c.on('stageStatusUpdate', this.peerStageStatusListener);
219
218
  participants.joined.addListener('screenShareUpdate', this.onScreenShareUpdate);
220
- participants.active.addListener('participantJoined', this.onParticipantJoined);
219
+ participants.active.addListener('participantJoined', this.refreshParticipants);
220
+ participants.active.addListener('participantsUpdate', this.refreshParticipants);
221
221
  participants.pinned.addListener('participantJoined', this.onParticipantPinned);
222
222
  participants.pinned.addListener('participantLeft', this.onParticipantUnpinned);
223
223
  if ((_d = meeting.stage) === null || _d === void 0 ? void 0 : _d.status) {
@@ -10,21 +10,64 @@ export default class RTKAudio {
10
10
  this.logger = meeting.__internals__.logger;
11
11
  this.audioStream = new MediaStream();
12
12
  this.audio.srcObject = this.audioStream;
13
- this.audioTracks = [];
13
+ this.trackIdByAudioIdMap = new Map();
14
+ this.trackOwners = new Map();
14
15
  }
15
- addTrack(id, track) {
16
- // Remove the track if it already exists, with the same id
17
- this.removeTrack(id);
18
- this.audioTracks.push({ id, track });
19
- this.audioStream.addTrack(track);
16
+ addTrack(audioId, track) {
17
+ /**
18
+ * NOTE(ravindra-cloudflare):
19
+ * audioId format: audio-${peerId} or screenshare-${peerId}.
20
+ *
21
+ * During reconnection, the old and new peer IDs of the same participant
22
+ * can temporarily co-own the same track.
23
+ *
24
+ * Before adding track, we should check if the track is already added with a different audio ID.
25
+ */
26
+ const currentTrackId = this.trackIdByAudioIdMap.get(audioId);
27
+ // Track already added, just play it
28
+ if (currentTrackId === track.id) {
29
+ this.play();
30
+ return;
31
+ }
32
+ // A logical audio ID owns one track. Release its previous track before replacing it.
33
+ if (currentTrackId)
34
+ this.removeTrack(audioId);
35
+ this.trackIdByAudioIdMap.set(audioId, track.id);
36
+ const owners = this.trackOwners.get(track.id);
37
+ if (owners) {
38
+ // Set membership is idempotent when reconnect events repeat or arrive out of order.
39
+ owners.audioIds.add(audioId);
40
+ }
41
+ else {
42
+ // If you are the first owner of this track, add it to the audio stream
43
+ this.trackOwners.set(track.id, { track, audioIds: new Set([audioId]) });
44
+ this.audioStream.addTrack(track);
45
+ }
20
46
  this.play();
21
47
  }
22
- removeTrack(id) {
23
- const track = this.audioTracks.find((a) => a.id === id);
24
- if (track != null) {
25
- this.audioStream.removeTrack(track.track);
26
- this.audioTracks = this.audioTracks.filter((a) => a.id !== id);
27
- }
48
+ removeTrack(audioId) {
49
+ /**
50
+ * NOTE(ravindra-cloudflare):
51
+ * audioId format: audio-${peerId} or screenshare-${peerId}.
52
+ *
53
+ * During reconnection, the old and new peer IDs of the same participant
54
+ * can temporarily co-own the same track.
55
+ *
56
+ * Before removing track, we should check if the track is still owned by other audio IDs.
57
+ */
58
+ const trackId = this.trackIdByAudioIdMap.get(audioId);
59
+ if (!trackId)
60
+ return;
61
+ this.trackIdByAudioIdMap.delete(audioId);
62
+ const owners = this.trackOwners.get(trackId);
63
+ if (!owners)
64
+ return;
65
+ owners.audioIds.delete(audioId);
66
+ if (owners.audioIds.size > 0)
67
+ return;
68
+ // Remove the physical track only after its final logical owner is gone.
69
+ this.audioStream.removeTrack(owners.track);
70
+ this.trackOwners.delete(trackId);
28
71
  }
29
72
  async play() {
30
73
  var _a;
@@ -1 +1 @@
1
- import{p as t,H as i,c as s,h as e,d as r,t as a}from"./p-BBHQOrFK.js";import{c as n,e as o,i as h}from"./p-DhwysA2e.js";import{d as c}from"./p-t72veIFj.js";import{R as p}from"./p-CBJjerDR.js";import{a as l}from"./p-C0vp-Q7v.js";import{S as d}from"./p-bRRQr4xn.js";import{d as g}from"./p-CmmyVCoN.js";import{d as m}from"./p-D6vw6bT2.js";import{d as v}from"./p-CFBS-gbK.js";import{d as u}from"./p-CEK_XoGw.js";import{d as f}from"./p-BYIBbfJ8.js";import{d as k}from"./p-vqPkhXWC.js";import{d as x}from"./p-DhG2PF_5.js";import{d as b}from"./p-B0SC1Zj-.js";import{d as S}from"./p-DViKr-34.js";var y=function(t,i,s,e){var r,a=arguments.length,n=a<3?i:null===e?e=Object.getOwnPropertyDescriptor(i,s):e;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(t,i,s,e);else for(var o=t.length-1;o>=0;o--)(r=t[o])&&(n=(a<3?r(n):a>3?r(i,s,n):r(i,s))||n);return a>3&&n&&Object.defineProperty(i,s,n),n};const C=t(class extends i{constructor(t){super(),!1!==t&&this.__registerHost(),this.__attachShadow(),this.stateUpdate=s(this,"rtkStateUpdate",7),this.hideSelf=!1,this.participants=[],this.pinnedParticipants=[],this.screenShareParticipants=[],this.plugins=[],this.emptyStage=!1,this.showLiveStreamPlayer=!1,this.canCurrentPeerHost=!1,this.pipSupported=!1,this.pipEnabled=!1,this.hidden=!1,this.layout="row",this.aspectRatio="16:9",this.gap=8,this.config=n(),this.iconPack=o,this.t=h(),this.gridSize=g,this.overrides={},this.invalidRoomStates=["init","waitlisted","ended","kicked","rejected"],this.filterParticipants=t=>{var i;if(this.overrides&&(null===(i=this.overrides)||void 0===i?void 0:i.videoUnsubscribed)){const i=this.overrides.videoUnsubscribed.preset;i.length>0&&(t=t.filter((t=>!i.some((i=>void 0!==t.presetName&&t.presetName.match(i))))))}return t},this.onViewModeChanged=()=>{this.meeting&&this.updateActiveParticipants()},this.onParticipantJoined=c((()=>{this.updateActiveParticipants()}),50),this.onParticipantLeft=c((()=>{this.updateActiveParticipants()}),50),this.stageStatusListener=()=>{this.updateActiveParticipants(),this.showLiveStreamPlayer=l(this.meeting),"ON_STAGE"!==this.meeting.stage.status&&this.removeScreenShare(this.meeting.self)},this.peerStageStatusListener=t=>{this.updateActiveParticipants(),"ON_STAGE"!==this.meeting.stage.status&&(this.removePinned(t),this.removeScreenShare(t))},this.onScreenShareUpdate=t=>{t.screenShareEnabled?this.addScreenShare(t):this.removeScreenShare(t)},this.onSelfScreenShareUpdate=({screenShareEnabled:t})=>{t?this.addScreenShare(this.meeting.self):this.removeScreenShare(this.meeting.self)},this.toggleTileListener=({hidden:t})=>{this.hidden=t,this.updateActiveParticipants()},this.onPluginStateUpdate=(t,{active:i})=>{i?this.plugins.some((i=>i.id===t.id))||(this.plugins=[...this.plugins,t]):this.plugins=this.plugins.filter((i=>i.id!==t.id))},this.onParticipantPinned=()=>{this.updateActiveParticipants()},this.onParticipantUnpinned=()=>{this.updateActiveParticipants()},this.updateRoomStateListener=()=>{this.roomState=this.meeting.self.roomState}}connectedCallback(){this.meetingChanged(this.meeting)}disconnectedCallback(){this.disconnectMeeting(this.meeting)}disconnectMeeting(t){var i;if(!t)return;this.participants=[],this.plugins=[];const{self:s,participants:e,plugins:r}=t;s.removeListener("pinned",this.onParticipantPinned),s.removeListener("unpinned",this.onParticipantUnpinned),s.removeListener("roomLeft",this.updateRoomStateListener),s.removeListener("roomJoined",this.updateRoomStateListener),s.removeListener("screenShareUpdate",this.onSelfScreenShareUpdate),s.removeListener("toggleTile",this.toggleTileListener),null==r||r.all.removeListener("stateUpdate",this.onPluginStateUpdate),null===(i=t.stage)||void 0===i||i.removeListener("stageStatusUpdate",this.stageStatusListener),e.removeListener("viewModeChanged",this.onViewModeChanged),e.active.removeListener("participantLeft",this.onParticipantLeft),e.active.removeListener("participantJoined",this.onParticipantJoined),e.pinned.removeListener("participantJoined",this.onParticipantPinned),e.pinned.removeListener("participantLeft",this.onParticipantUnpinned),e.joined.removeListener("screenShareUpdate",this.onScreenShareUpdate),e.joined.removeListener("stageStatusUpdate",this.peerStageStatusListener)}meetingChanged(t,i){var s,e,r,a;if(null!==i&&this.disconnectMeeting(i),null!=t){const{self:i,participants:n,plugins:o,stage:h}=t;this.pipSupported=(null===(s=this.meeting.participants.pip)||void 0===s?void 0:s.isSupported())&&(null===(e=t.self.config)||void 0===e?void 0:e.pipMode),this.pipSupported&&this.meeting.participants.pip.init();const{permissions:c}=i;this.roomState=i.roomState,this.hideSelf="ON_STAGE"!==this.meeting.stage.status||(null==c?void 0:c.isRecorder)||c.hiddenParticipant,this.participants=this.filterParticipants([...n.active.toArray(),...i.isPinned||this.hideSelf?[]:[i]]),this.pinnedParticipants=[...n.pinned.toArray(),...i.isPinned&&!this.hideSelf?[i]:[]],this.screenShareParticipants=n.joined.toArray().filter((t=>t.screenShareEnabled)),this.plugins=(null==o?void 0:o.active.toArray())||[],(null==c?void 0:c.stageEnabled)&&(this.canCurrentPeerHost=c.acceptStageRequests||"ALLOWED"===c.canProduceAudio||"ALLOWED"===c.canProduceVideo||"ALLOWED"===c.canProduceScreenshare,this.updateStage()),this.hidden=t.self.hidden,i.addListener("roomLeft",this.updateRoomStateListener),i.addListener("roomJoined",this.updateRoomStateListener),i.addListener("screenShareUpdate",this.onSelfScreenShareUpdate),i.addListener("pinned",this.onParticipantPinned),i.addListener("unpinned",this.onParticipantUnpinned),i.addListener("toggleTile",this.toggleTileListener),null==h||h.addListener("stageStatusUpdate",this.stageStatusListener),null==o||o.all.addListener("stateUpdate",this.onPluginStateUpdate),n.addListener("viewModeChanged",this.onViewModeChanged),n.active.addListener("participantLeft",this.onParticipantLeft),null===(r=null==n?void 0:n.joined)||void 0===r||r.on("stageStatusUpdate",this.peerStageStatusListener),n.joined.addListener("screenShareUpdate",this.onScreenShareUpdate),n.active.addListener("participantJoined",this.onParticipantJoined),n.pinned.addListener("participantJoined",this.onParticipantPinned),n.pinned.addListener("participantLeft",this.onParticipantUnpinned),(null===(a=t.stage)||void 0===a?void 0:a.status)&&this.stageStatusListener()}}overridesChanged(t){this.updateActiveParticipants()}screenShareParticipantsChanged(t){const i=t.length>0;!!this.states.activeScreenShare!==i&&(this.stateUpdate.emit({activeScreenShare:i}),this.states.activeScreenShare=i)}pluginsChanged(t){const i=t.length>0;!!this.states.activePlugin!==i&&(this.stateUpdate.emit({activePlugin:i}),this.states.activePlugin=i)}pinnedParticipantsChanged(t){const i=t.length>0;!!this.states.activeSpotlight!==i&&(this.stateUpdate.emit({activeSpotlight:i}),this.states.activeSpotlight=i)}updateActiveParticipants(){var t;const{self:i,participants:s,stage:e}=this.meeting;this.hideSelf=this.hidden||"ON_STAGE"!==e.status||(null===(t=i.permissions)||void 0===t?void 0:t.isRecorder)||i.permissions.hiddenParticipant,this.participants=this.filterParticipants([...s.active.toArray().filter((t=>t.id!==i.id)),..."ACTIVE_GRID"!==s.viewMode||i.isPinned||this.hideSelf?[]:[i]]),this.pinnedParticipants=[...s.pinned.toArray().filter((t=>t.id!==i.id)),...i.isPinned&&!this.hideSelf?[i]:[]],this.screenShareParticipants=s.joined.toArray().filter((t=>t.screenShareEnabled)),i.screenShareEnabled&&(this.screenShareParticipants=this.screenShareParticipants.concat([i])),this.updateStage()}updateStage(){var t;const{self:i}=this.meeting;this.meeting&&(this.emptyStage=!!(null===(t=null==i?void 0:i.permissions)||void 0===t?void 0:t.stageEnabled)&&0===this.participants.length&&0===this.pinnedParticipants.length)}addScreenShare(t){this.screenShareParticipants.some((i=>i.id===t.id))||(this.screenShareParticipants=[...this.screenShareParticipants,t])}removeScreenShare(t){this.screenShareParticipants=this.screenShareParticipants.filter((i=>i.id!==t.id))}removePinned(t){this.pinnedParticipants=this.pinnedParticipants.filter((i=>i.id!==t.id))}render(){var t,i,s,a;const n={meeting:this.meeting,size:this.size,states:this.states,config:this.config,iconPack:this.iconPack,t:this.t};if(this.invalidRoomStates.includes(this.roomState))return e(r,null,e("div",{class:"offline-grid"},e("rtk-icon",{icon:this.iconPack.join_stage,size:"xl"}),e("h3",null,this.t("disconnected")),e("p",null,this.t("disconnected.description"))));if("disconnected"===this.roomState){const n=null===(i=null===(t=this.meeting)||void 0===t?void 0:t.meta)||void 0===i?void 0:i.mediaState;if("connected"!==(null===(s=null==n?void 0:n.send)||void 0===s?void 0:s.state)||"connected"!==(null===(a=null==n?void 0:n.recv)||void 0===a?void 0:a.state))return e(r,null,e("div",{class:"offline-grid"},e("rtk-icon",{icon:this.iconPack.disconnected,size:"xl"}),e("h3",null,this.t("offline")),e("p",null,this.t("offline.description"))))}return"failed"===this.roomState?e(r,null,e("div",{class:"offline-grid"},e("rtk-icon",{icon:this.iconPack.disconnected,size:"xl"}),e("h3",null,this.t("failed")),e("p",null,this.t("failed.description")))):this.showLiveStreamPlayer?e(r,null,e("rtk-livestream-player",{meeting:this.meeting,size:this.size}),e("rtk-livestream-indicator",{meeting:this.meeting,size:"sm",t:this.t}),e("rtk-viewer-count",{meeting:this.meeting,variant:"embedded",t:this.t})):this.emptyStage?e(r,null,e("div",{class:"webinar-stage"},e("div",{class:"center"},this.canCurrentPeerHost&&e("div",{class:"ctr",part:"container"},e("p",{class:"message",part:"message"},this.t("stage.empty_host")),e("span",{class:"description",part:"description"},this.t("stage.empty_host_summary"))),!this.canCurrentPeerHost&&e("div",{class:"ctr",part:"container"},e("p",{class:"message",part:"message"},this.t("stage.empty_viewer")))))):e(r,null,e(p,{element:"rtk-grid",defaults:n,childProps:{participants:this.participants,screenShareParticipants:this.screenShareParticipants,plugins:this.plugins,pinnedParticipants:this.pinnedParticipants,aspectRatio:this.aspectRatio,gap:this.gap,layout:this.layout,gridSize:this.gridSize},onlyChildren:!0}),e("rtk-livestream-indicator",{meeting:this.meeting,size:"sm",t:this.t}),e("rtk-viewer-count",{meeting:this.meeting,variant:"embedded",t:this.t}))}static get watchers(){return{meeting:[{meetingChanged:0}],overrides:[{overridesChanged:0}],screenShareParticipants:[{screenShareParticipantsChanged:0}],plugins:[{pluginsChanged:0}],pinnedParticipants:[{pinnedParticipantsChanged:0}]}}static get style(){return":host{line-height:initial;font-family:var(--rtk-font-family, sans-serif);font-feature-settings:normal;font-variation-settings:normal}p{margin:var(--rtk-space-0, 0px);padding:var(--rtk-space-0, 0px)}:host{display:block;height:100%;width:100%}.offline-grid{display:flex;height:100%;width:100%;flex-direction:column;align-items:center;justify-content:center}.offline-grid h3{margin-top:var(--rtk-space-2, 8px);margin-bottom:var(--rtk-space-2, 8px);margin-left:var(--rtk-space-0, 0px);margin-right:var(--rtk-space-0, 0px);font-size:20px;font-weight:500;color:rgb(var(--rtk-colors-text-900, 255 255 255 / 0.88))}.offline-grid p{margin-top:var(--rtk-space-1, 4px);margin-bottom:var(--rtk-space-1, 4px);margin-left:var(--rtk-space-0, 0px);margin-right:var(--rtk-space-0, 0px);text-align:center;font-size:16px;font-weight:400;color:rgb(var(--rtk-colors-text-700, 255 255 255 / 0.64))}.offline-grid rtk-icon{margin-bottom:var(--rtk-space-2, 8px)}.webinar-stage{box-sizing:border-box;height:100%;display:flex;align-content:center;align-items:center;justify-content:center;text-align:center}.webinar-stage h2{margin:var(--rtk-space-0, 0px);font-weight:normal}rtk-viewer-count{display:none}.ctr{display:flex;flex-direction:column;align-items:center}.message{font-size:16px;border-radius:var(--rtk-border-radius-lg, 12px);--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-background-800, 30 30 30) / var(--tw-bg-opacity));padding-left:var(--rtk-space-8, 32px);padding-right:var(--rtk-space-8, 32px);padding-top:var(--rtk-space-4, 16px);padding-bottom:var(--rtk-space-4, 16px);color:rgb(var(--rtk-colors-text-1000, 255 255 255))}.description{margin-top:var(--rtk-space-4, 16px);margin-bottom:var(--rtk-space-4, 16px);font-size:14px}rtk-livestream-indicator{display:none}@media only screen and (max-device-height: 480px) and (orientation: landscape){rtk-viewer-count[variant='embedded']{position:absolute;top:var(--rtk-space-0, 0px);right:var(--rtk-space-2, 8px);z-index:20;margin-top:var(--rtk-space-0, 0px);margin-left:var(--rtk-space-0, 0px);display:flex}rtk-livestream-indicator{position:absolute;top:var(--rtk-space-0, 0px);left:var(--rtk-space-2, 8px);z-index:20;margin-top:var(--rtk-space-0, 0px);margin-left:var(--rtk-space-0, 0px);display:flex}}"}},[1,"rtk-grid",{layout:[513],aspectRatio:[513,"aspect-ratio"],meeting:[16],gap:[514],size:[513],states:[16],config:[16],iconPack:[16],t:[16],gridSize:[16],overrides:[8],participants:[32],pinnedParticipants:[32],screenShareParticipants:[32],plugins:[32],emptyStage:[32],showLiveStreamPlayer:[32],canCurrentPeerHost:[32],pipSupported:[32],pipEnabled:[32],hidden:[32],roomState:[32]},void 0,{meeting:[{meetingChanged:0}],overrides:[{overridesChanged:0}],screenShareParticipants:[{screenShareParticipantsChanged:0}],plugins:[{pluginsChanged:0}],pinnedParticipants:[{pinnedParticipantsChanged:0}]}]);function w(){"undefined"!=typeof customElements&&["rtk-grid","rtk-button","rtk-controlbar-button","rtk-fullscreen-toggle","rtk-icon","rtk-livestream-indicator","rtk-livestream-player","rtk-spinner","rtk-viewer-count"].forEach((t=>{switch(t){case"rtk-grid":customElements.get(a(t))||customElements.define(a(t),C);break;case"rtk-button":customElements.get(a(t))||m();break;case"rtk-controlbar-button":customElements.get(a(t))||v();break;case"rtk-fullscreen-toggle":customElements.get(a(t))||u();break;case"rtk-icon":customElements.get(a(t))||f();break;case"rtk-livestream-indicator":customElements.get(a(t))||k();break;case"rtk-livestream-player":customElements.get(a(t))||x();break;case"rtk-spinner":customElements.get(a(t))||b();break;case"rtk-viewer-count":customElements.get(a(t))||S()}}))}y([d()],C.prototype,"meeting",void 0),y([d()],C.prototype,"states",void 0),y([d()],C.prototype,"config",void 0),y([d()],C.prototype,"iconPack",void 0),y([d()],C.prototype,"t",void 0),w();const P=C,j=w;export{P as RtkGrid,j as defineCustomElement}
1
+ import{p as t,H as i,c as s,h as e,d as r,t as a}from"./p-BBHQOrFK.js";import{c as n,e as o,i as h}from"./p-DhwysA2e.js";import{d as p}from"./p-t72veIFj.js";import{R as c}from"./p-CBJjerDR.js";import{a as l}from"./p-C0vp-Q7v.js";import{S as d}from"./p-bRRQr4xn.js";import{d as g}from"./p-CmmyVCoN.js";import{d as m}from"./p-D6vw6bT2.js";import{d as v}from"./p-CFBS-gbK.js";import{d as u}from"./p-CEK_XoGw.js";import{d as f}from"./p-BYIBbfJ8.js";import{d as k}from"./p-vqPkhXWC.js";import{d as x}from"./p-DhG2PF_5.js";import{d as b}from"./p-B0SC1Zj-.js";import{d as S}from"./p-DViKr-34.js";var y=function(t,i,s,e){var r,a=arguments.length,n=a<3?i:null===e?e=Object.getOwnPropertyDescriptor(i,s):e;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(t,i,s,e);else for(var o=t.length-1;o>=0;o--)(r=t[o])&&(n=(a<3?r(n):a>3?r(i,s,n):r(i,s))||n);return a>3&&n&&Object.defineProperty(i,s,n),n};const C=t(class extends i{constructor(t){super(),!1!==t&&this.__registerHost(),this.__attachShadow(),this.stateUpdate=s(this,"rtkStateUpdate",7),this.hideSelf=!1,this.participants=[],this.pinnedParticipants=[],this.screenShareParticipants=[],this.plugins=[],this.emptyStage=!1,this.showLiveStreamPlayer=!1,this.canCurrentPeerHost=!1,this.pipSupported=!1,this.pipEnabled=!1,this.hidden=!1,this.layout="row",this.aspectRatio="16:9",this.gap=8,this.config=n(),this.iconPack=o,this.t=h(),this.gridSize=g,this.overrides={},this.invalidRoomStates=["init","waitlisted","ended","kicked","rejected"],this.filterParticipants=t=>{var i;if(this.overrides&&(null===(i=this.overrides)||void 0===i?void 0:i.videoUnsubscribed)){const i=this.overrides.videoUnsubscribed.preset;i.length>0&&(t=t.filter((t=>!i.some((i=>void 0!==t.presetName&&t.presetName.match(i))))))}return t},this.onViewModeChanged=()=>{this.meeting&&this.updateActiveParticipants()},this.refreshParticipants=p((()=>{this.updateActiveParticipants()}),50),this.stageStatusListener=()=>{this.updateActiveParticipants(),this.showLiveStreamPlayer=l(this.meeting),"ON_STAGE"!==this.meeting.stage.status&&this.removeScreenShare(this.meeting.self)},this.peerStageStatusListener=t=>{this.updateActiveParticipants(),"ON_STAGE"!==this.meeting.stage.status&&(this.removePinned(t),this.removeScreenShare(t))},this.onScreenShareUpdate=t=>{t.screenShareEnabled?this.addScreenShare(t):this.removeScreenShare(t)},this.onSelfScreenShareUpdate=({screenShareEnabled:t})=>{t?this.addScreenShare(this.meeting.self):this.removeScreenShare(this.meeting.self)},this.toggleTileListener=({hidden:t})=>{this.hidden=t,this.updateActiveParticipants()},this.onPluginStateUpdate=(t,{active:i})=>{i?this.plugins.some((i=>i.id===t.id))||(this.plugins=[...this.plugins,t]):this.plugins=this.plugins.filter((i=>i.id!==t.id))},this.onParticipantPinned=()=>{this.updateActiveParticipants()},this.onParticipantUnpinned=()=>{this.updateActiveParticipants()},this.updateRoomStateListener=()=>{this.roomState=this.meeting.self.roomState}}connectedCallback(){this.meetingChanged(this.meeting)}disconnectedCallback(){this.disconnectMeeting(this.meeting)}disconnectMeeting(t){var i;if(!t)return;this.refreshParticipants.cancel(),this.participants=[],this.plugins=[];const{self:s,participants:e,plugins:r}=t;s.removeListener("pinned",this.onParticipantPinned),s.removeListener("unpinned",this.onParticipantUnpinned),s.removeListener("roomLeft",this.updateRoomStateListener),s.removeListener("roomJoined",this.updateRoomStateListener),s.removeListener("screenShareUpdate",this.onSelfScreenShareUpdate),s.removeListener("toggleTile",this.toggleTileListener),null==r||r.all.removeListener("stateUpdate",this.onPluginStateUpdate),null===(i=t.stage)||void 0===i||i.removeListener("stageStatusUpdate",this.stageStatusListener),e.removeListener("viewModeChanged",this.onViewModeChanged),e.active.removeListener("participantLeft",this.refreshParticipants),e.active.removeListener("participantJoined",this.refreshParticipants),e.active.removeListener("participantsUpdate",this.refreshParticipants),e.pinned.removeListener("participantJoined",this.onParticipantPinned),e.pinned.removeListener("participantLeft",this.onParticipantUnpinned),e.joined.removeListener("screenShareUpdate",this.onScreenShareUpdate),e.joined.removeListener("stageStatusUpdate",this.peerStageStatusListener)}meetingChanged(t,i){var s,e,r,a;if(null!==i&&this.disconnectMeeting(i),null!=t){const{self:i,participants:n,plugins:o,stage:h}=t;this.pipSupported=(null===(s=this.meeting.participants.pip)||void 0===s?void 0:s.isSupported())&&(null===(e=t.self.config)||void 0===e?void 0:e.pipMode),this.pipSupported&&this.meeting.participants.pip.init();const{permissions:p}=i;this.roomState=i.roomState,this.hideSelf="ON_STAGE"!==this.meeting.stage.status||(null==p?void 0:p.isRecorder)||p.hiddenParticipant,this.participants=this.filterParticipants([...n.active.toArray(),...i.isPinned||this.hideSelf?[]:[i]]),this.pinnedParticipants=[...n.pinned.toArray(),...i.isPinned&&!this.hideSelf?[i]:[]],this.screenShareParticipants=n.joined.toArray().filter((t=>t.screenShareEnabled)),this.plugins=(null==o?void 0:o.active.toArray())||[],(null==p?void 0:p.stageEnabled)&&(this.canCurrentPeerHost=p.acceptStageRequests||"ALLOWED"===p.canProduceAudio||"ALLOWED"===p.canProduceVideo||"ALLOWED"===p.canProduceScreenshare,this.updateStage()),this.hidden=t.self.hidden,i.addListener("roomLeft",this.updateRoomStateListener),i.addListener("roomJoined",this.updateRoomStateListener),i.addListener("screenShareUpdate",this.onSelfScreenShareUpdate),i.addListener("pinned",this.onParticipantPinned),i.addListener("unpinned",this.onParticipantUnpinned),i.addListener("toggleTile",this.toggleTileListener),null==h||h.addListener("stageStatusUpdate",this.stageStatusListener),null==o||o.all.addListener("stateUpdate",this.onPluginStateUpdate),n.addListener("viewModeChanged",this.onViewModeChanged),n.active.addListener("participantLeft",this.refreshParticipants),null===(r=null==n?void 0:n.joined)||void 0===r||r.on("stageStatusUpdate",this.peerStageStatusListener),n.joined.addListener("screenShareUpdate",this.onScreenShareUpdate),n.active.addListener("participantJoined",this.refreshParticipants),n.active.addListener("participantsUpdate",this.refreshParticipants),n.pinned.addListener("participantJoined",this.onParticipantPinned),n.pinned.addListener("participantLeft",this.onParticipantUnpinned),(null===(a=t.stage)||void 0===a?void 0:a.status)&&this.stageStatusListener()}}overridesChanged(t){this.updateActiveParticipants()}screenShareParticipantsChanged(t){const i=t.length>0;!!this.states.activeScreenShare!==i&&(this.stateUpdate.emit({activeScreenShare:i}),this.states.activeScreenShare=i)}pluginsChanged(t){const i=t.length>0;!!this.states.activePlugin!==i&&(this.stateUpdate.emit({activePlugin:i}),this.states.activePlugin=i)}pinnedParticipantsChanged(t){const i=t.length>0;!!this.states.activeSpotlight!==i&&(this.stateUpdate.emit({activeSpotlight:i}),this.states.activeSpotlight=i)}updateActiveParticipants(){var t;const{self:i,participants:s,stage:e}=this.meeting;this.hideSelf=this.hidden||"ON_STAGE"!==e.status||(null===(t=i.permissions)||void 0===t?void 0:t.isRecorder)||i.permissions.hiddenParticipant,this.participants=this.filterParticipants([...s.active.toArray().filter((t=>t.id!==i.id)),..."ACTIVE_GRID"!==s.viewMode||i.isPinned||this.hideSelf?[]:[i]]),this.pinnedParticipants=[...s.pinned.toArray().filter((t=>t.id!==i.id)),...i.isPinned&&!this.hideSelf?[i]:[]],this.screenShareParticipants=s.joined.toArray().filter((t=>t.screenShareEnabled)),i.screenShareEnabled&&(this.screenShareParticipants=this.screenShareParticipants.concat([i])),this.updateStage()}updateStage(){var t;const{self:i}=this.meeting;this.meeting&&(this.emptyStage=!!(null===(t=null==i?void 0:i.permissions)||void 0===t?void 0:t.stageEnabled)&&0===this.participants.length&&0===this.pinnedParticipants.length)}addScreenShare(t){this.screenShareParticipants.some((i=>i.id===t.id))||(this.screenShareParticipants=[...this.screenShareParticipants,t])}removeScreenShare(t){this.screenShareParticipants=this.screenShareParticipants.filter((i=>i.id!==t.id))}removePinned(t){this.pinnedParticipants=this.pinnedParticipants.filter((i=>i.id!==t.id))}render(){var t,i,s,a;const n={meeting:this.meeting,size:this.size,states:this.states,config:this.config,iconPack:this.iconPack,t:this.t};if(this.invalidRoomStates.includes(this.roomState))return e(r,null,e("div",{class:"offline-grid"},e("rtk-icon",{icon:this.iconPack.join_stage,size:"xl"}),e("h3",null,this.t("disconnected")),e("p",null,this.t("disconnected.description"))));if("disconnected"===this.roomState){const n=null===(i=null===(t=this.meeting)||void 0===t?void 0:t.meta)||void 0===i?void 0:i.mediaState;if("connected"!==(null===(s=null==n?void 0:n.send)||void 0===s?void 0:s.state)||"connected"!==(null===(a=null==n?void 0:n.recv)||void 0===a?void 0:a.state))return e(r,null,e("div",{class:"offline-grid"},e("rtk-icon",{icon:this.iconPack.disconnected,size:"xl"}),e("h3",null,this.t("offline")),e("p",null,this.t("offline.description"))))}return"failed"===this.roomState?e(r,null,e("div",{class:"offline-grid"},e("rtk-icon",{icon:this.iconPack.disconnected,size:"xl"}),e("h3",null,this.t("failed")),e("p",null,this.t("failed.description")))):this.showLiveStreamPlayer?e(r,null,e("rtk-livestream-player",{meeting:this.meeting,size:this.size}),e("rtk-livestream-indicator",{meeting:this.meeting,size:"sm",t:this.t}),e("rtk-viewer-count",{meeting:this.meeting,variant:"embedded",t:this.t})):this.emptyStage?e(r,null,e("div",{class:"webinar-stage"},e("div",{class:"center"},this.canCurrentPeerHost&&e("div",{class:"ctr",part:"container"},e("p",{class:"message",part:"message"},this.t("stage.empty_host")),e("span",{class:"description",part:"description"},this.t("stage.empty_host_summary"))),!this.canCurrentPeerHost&&e("div",{class:"ctr",part:"container"},e("p",{class:"message",part:"message"},this.t("stage.empty_viewer")))))):e(r,null,e(c,{element:"rtk-grid",defaults:n,childProps:{participants:this.participants,screenShareParticipants:this.screenShareParticipants,plugins:this.plugins,pinnedParticipants:this.pinnedParticipants,aspectRatio:this.aspectRatio,gap:this.gap,layout:this.layout,gridSize:this.gridSize},onlyChildren:!0}),e("rtk-livestream-indicator",{meeting:this.meeting,size:"sm",t:this.t}),e("rtk-viewer-count",{meeting:this.meeting,variant:"embedded",t:this.t}))}static get watchers(){return{meeting:[{meetingChanged:0}],overrides:[{overridesChanged:0}],screenShareParticipants:[{screenShareParticipantsChanged:0}],plugins:[{pluginsChanged:0}],pinnedParticipants:[{pinnedParticipantsChanged:0}]}}static get style(){return":host{line-height:initial;font-family:var(--rtk-font-family, sans-serif);font-feature-settings:normal;font-variation-settings:normal}p{margin:var(--rtk-space-0, 0px);padding:var(--rtk-space-0, 0px)}:host{display:block;height:100%;width:100%}.offline-grid{display:flex;height:100%;width:100%;flex-direction:column;align-items:center;justify-content:center}.offline-grid h3{margin-top:var(--rtk-space-2, 8px);margin-bottom:var(--rtk-space-2, 8px);margin-left:var(--rtk-space-0, 0px);margin-right:var(--rtk-space-0, 0px);font-size:20px;font-weight:500;color:rgb(var(--rtk-colors-text-900, 255 255 255 / 0.88))}.offline-grid p{margin-top:var(--rtk-space-1, 4px);margin-bottom:var(--rtk-space-1, 4px);margin-left:var(--rtk-space-0, 0px);margin-right:var(--rtk-space-0, 0px);text-align:center;font-size:16px;font-weight:400;color:rgb(var(--rtk-colors-text-700, 255 255 255 / 0.64))}.offline-grid rtk-icon{margin-bottom:var(--rtk-space-2, 8px)}.webinar-stage{box-sizing:border-box;height:100%;display:flex;align-content:center;align-items:center;justify-content:center;text-align:center}.webinar-stage h2{margin:var(--rtk-space-0, 0px);font-weight:normal}rtk-viewer-count{display:none}.ctr{display:flex;flex-direction:column;align-items:center}.message{font-size:16px;border-radius:var(--rtk-border-radius-lg, 12px);--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-background-800, 30 30 30) / var(--tw-bg-opacity));padding-left:var(--rtk-space-8, 32px);padding-right:var(--rtk-space-8, 32px);padding-top:var(--rtk-space-4, 16px);padding-bottom:var(--rtk-space-4, 16px);color:rgb(var(--rtk-colors-text-1000, 255 255 255))}.description{margin-top:var(--rtk-space-4, 16px);margin-bottom:var(--rtk-space-4, 16px);font-size:14px}rtk-livestream-indicator{display:none}@media only screen and (max-device-height: 480px) and (orientation: landscape){rtk-viewer-count[variant='embedded']{position:absolute;top:var(--rtk-space-0, 0px);right:var(--rtk-space-2, 8px);z-index:20;margin-top:var(--rtk-space-0, 0px);margin-left:var(--rtk-space-0, 0px);display:flex}rtk-livestream-indicator{position:absolute;top:var(--rtk-space-0, 0px);left:var(--rtk-space-2, 8px);z-index:20;margin-top:var(--rtk-space-0, 0px);margin-left:var(--rtk-space-0, 0px);display:flex}}"}},[1,"rtk-grid",{layout:[513],aspectRatio:[513,"aspect-ratio"],meeting:[16],gap:[514],size:[513],states:[16],config:[16],iconPack:[16],t:[16],gridSize:[16],overrides:[8],participants:[32],pinnedParticipants:[32],screenShareParticipants:[32],plugins:[32],emptyStage:[32],showLiveStreamPlayer:[32],canCurrentPeerHost:[32],pipSupported:[32],pipEnabled:[32],hidden:[32],roomState:[32]},void 0,{meeting:[{meetingChanged:0}],overrides:[{overridesChanged:0}],screenShareParticipants:[{screenShareParticipantsChanged:0}],plugins:[{pluginsChanged:0}],pinnedParticipants:[{pinnedParticipantsChanged:0}]}]);function w(){"undefined"!=typeof customElements&&["rtk-grid","rtk-button","rtk-controlbar-button","rtk-fullscreen-toggle","rtk-icon","rtk-livestream-indicator","rtk-livestream-player","rtk-spinner","rtk-viewer-count"].forEach((t=>{switch(t){case"rtk-grid":customElements.get(a(t))||customElements.define(a(t),C);break;case"rtk-button":customElements.get(a(t))||m();break;case"rtk-controlbar-button":customElements.get(a(t))||v();break;case"rtk-fullscreen-toggle":customElements.get(a(t))||u();break;case"rtk-icon":customElements.get(a(t))||f();break;case"rtk-livestream-indicator":customElements.get(a(t))||k();break;case"rtk-livestream-player":customElements.get(a(t))||x();break;case"rtk-spinner":customElements.get(a(t))||b();break;case"rtk-viewer-count":customElements.get(a(t))||S()}}))}y([d()],C.prototype,"meeting",void 0),y([d()],C.prototype,"states",void 0),y([d()],C.prototype,"config",void 0),y([d()],C.prototype,"iconPack",void 0),y([d()],C.prototype,"t",void 0),w();const P=C,j=w;export{P as RtkGrid,j as defineCustomElement}
@@ -1 +1 @@
1
- import{p as t,H as i,c as s,h as a,d as e,t as r}from"./p-BBHQOrFK.js";import{i as o}from"./p-CAqYddlg.js";import{e as n,i as d}from"./p-DhwysA2e.js";import{S as h}from"./p-bRRQr4xn.js";import{a as c}from"./p-C0vp-Q7v.js";import{d as l}from"./p-D6vw6bT2.js";import{d as p}from"./p-DSe3wsBa.js";import{d as u}from"./p-BYIBbfJ8.js";class k{constructor(t,i){this.meeting=t,this.audio=null!=i?i:document.createElement("audio"),this.audio.autoplay=!0,this.logger=t.__internals__.logger,this.audioStream=new MediaStream,this.audio.srcObject=this.audioStream,this.audioTracks=[]}addTrack(t,i){this.removeTrack(t),this.audioTracks.push({id:t,track:i}),this.audioStream.addTrack(i),this.play()}removeTrack(t){const i=this.audioTracks.find((i=>i.id===t));null!=i&&(this.audioStream.removeTrack(i.track),this.audioTracks=this.audioTracks.filter((i=>i.id!==t)))}async play(){var t;this.audio.srcObject=this.audioStream,await(null===(t=this.audio.play())||void 0===t?void 0:t.catch((t=>{"NotAllowedError"===t.name?null!=this._onError&&this._onError():"AbortError"!==t.name&&this.logger.error("[rtk-audio] play() error\n",t)})))}async setDevice(t){var i,s,a;o(this.meeting)||await(null===(a=null===(s=(i=this.audio).setSinkId)||void 0===s?void 0:s.call(i,t))||void 0===a?void 0:a.catch((t=>{this.logger.error("[rtk-audio] setSinkId() error\n",t)})))}onError(t){this._onError=t}}var m=function(t,i,s,a){var e,r=arguments.length,o=r<3?i:null===a?a=Object.getOwnPropertyDescriptor(i,s):a;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)o=Reflect.decorate(t,i,s,a);else for(var n=t.length-1;n>=0;n--)(e=t[n])&&(o=(r<3?e(o):r>3?e(i,s,o):e(i,s))||o);return r>3&&o&&Object.defineProperty(i,s,o),o};const b=t(class extends i{constructor(t){super(),!1!==t&&this.__registerHost(),this.__attachShadow(),this.dialogClose=s(this,"dialogClose",7),this.iconPack=n,this.t=d(),this.preloadedAudioElem=void 0,this.showPlayDialog=!1,this.onRtkDialogClose=()=>{this.showPlayDialog=!1,this.dialogClose.emit()}}componentDidLoad(){this.meetingChanged(this.meeting)}disconnectedCallback(){var t;this.meeting&&(this.audioUpdateListener&&this.meeting.participants.joined.removeListener("audioUpdate",this.audioUpdateListener),this.screenShareUpdateListener&&this.meeting.participants.joined.removeListener("screenShareUpdate",this.screenShareUpdateListener),this.participantLeftListener&&this.meeting.participants.joined.removeListener("participantLeft",this.participantLeftListener),this.deviceUpdateListener&&this.meeting.self.removeListener("deviceUpdate",this.deviceUpdateListener),this.stageStatusUpdateListener&&(null===(t=this.meeting.stage)||void 0===t||t.removeListener("stageStatusUpdate",this.stageStatusUpdateListener)))}async setupAudio(){this.audio=new k(this.meeting,this.preloadedAudioElem);const t=this.meeting.self.getCurrentDevices();null!=t.speaker&&await this.audio.setDevice(t.speaker.deviceId)}async handleAutoPlayError(){this.audio||await this.setupAudio(),this.audio.onError((()=>{this.showPlayDialog=!0})),this.audio.play()}async handleEvents(t){this.audioUpdateListener=({id:t,audioEnabled:i,audioTrack:s})=>{const a="audio-"+t;i&&null!=s?this.audio.addTrack(a,s):this.audio.removeTrack(a)};const i=t.participants.joined.toArray();for(const t of i)this.audioUpdateListener(t);this.participantLeftListener=({id:t})=>{this.audio.removeTrack("audio-"+t),this.audio.removeTrack("screenshare-"+t)},this.screenShareUpdateListener=({id:t,screenShareEnabled:i,screenShareTracks:s})=>{const a="screenshare-"+t;i&&null!=s.audio?this.audio.addTrack(a,s.audio):this.audio.removeTrack(a)},this.deviceUpdateListener=({device:t,preview:i})=>{i||"audiooutput"===t.kind&&this.audio.setDevice(t.deviceId)},t.participants.joined.addListener("audioUpdate",this.audioUpdateListener),t.participants.joined.addListener("screenShareUpdate",this.screenShareUpdateListener),t.participants.joined.addListener("participantLeft",this.participantLeftListener),t.self.addListener("deviceUpdate",this.deviceUpdateListener)}async meetingChanged(t){var i;t&&(this.setupAudio(),c(t)?(this.stageStatusUpdateListener=async t=>{"ON_STAGE"===t&&await this.handleAutoPlayError()},null===(i=t.stage)||void 0===i||i.on("stageStatusUpdate",this.stageStatusUpdateListener)):await this.handleAutoPlayError(),await this.handleEvents(t))}render(){return a(e,{key:"14ddae0562633e4433788c18fb89533248df6c2b"},this.showPlayDialog&&a("rtk-dialog",{key:"d0e33058fbcd77861009ce80d7d4e29b2b597475",open:!0,onRtkDialogClose:this.onRtkDialogClose,hideCloseButton:!0,disableEscapeKey:!0,iconPack:this.iconPack,t:this.t},a("div",{key:"e6a9a18c1eaf3e09d13e25373982183b3ad6f8ec",class:"modal"},a("h3",{key:"bc87cc408aad7117cc8d808e38142b3801e5ff53"},this.t("audio_playback.title")),a("p",{key:"b1927a5cd053c59e19a6c4c54044ca500691ee71"},this.t("audio_playback.description")),a("rtk-button",{key:"3c022bb0602295cbd5996ae2ee066e254b1af2fb",kind:"wide",onClick:()=>{this.audio.play(),this.onRtkDialogClose()},title:this.t("audio_playback")},this.t("audio_playback")))))}static get watchers(){return{meeting:[{meetingChanged:0}]}}static get style(){return":host{line-height:initial;font-family:var(--rtk-font-family, sans-serif);font-feature-settings:normal;font-variation-settings:normal}p{margin:var(--rtk-space-0, 0px);padding:var(--rtk-space-0, 0px)}:host{display:block;overflow-wrap:break-word;color:rgb(var(--rtk-colors-text-1000, 255 255 255));word-wrap:break-word}.modal{box-sizing:border-box;width:100%;max-width:var(--rtk-space-96, 384px);padding:var(--rtk-space-4, 16px);border-radius:var(--rtk-border-radius-md, 8px);--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-background-1000, 8 8 8) / var(--tw-bg-opacity))}.modal h3{margin:var(--rtk-space-0, 0px);padding:var(--rtk-space-0, 0px);font-size:24px;font-weight:500}.modal p{margin-top:var(--rtk-space-4, 16px);margin-bottom:var(--rtk-space-6, 24px)}.modal rtk-button{width:100%}"}},[1,"rtk-participants-audio",{meeting:[16],iconPack:[16],t:[16],preloadedAudioElem:[16],showPlayDialog:[32]},void 0,{meeting:[{meetingChanged:0}]}]);function v(){"undefined"!=typeof customElements&&["rtk-participants-audio","rtk-button","rtk-dialog","rtk-icon"].forEach((t=>{switch(t){case"rtk-participants-audio":customElements.get(r(t))||customElements.define(r(t),b);break;case"rtk-button":customElements.get(r(t))||l();break;case"rtk-dialog":customElements.get(r(t))||p();break;case"rtk-icon":customElements.get(r(t))||u()}}))}m([h()],b.prototype,"meeting",void 0),m([h()],b.prototype,"iconPack",void 0),m([h()],b.prototype,"t",void 0),v();const f=b,g=v;export{f as RtkParticipantsAudio,g as defineCustomElement}
1
+ import{p as t,H as i,c as s,h as a,d as e,t as r}from"./p-BBHQOrFK.js";import{i as o}from"./p-CAqYddlg.js";import{e as n,i as h}from"./p-DhwysA2e.js";import{S as d}from"./p-bRRQr4xn.js";import{a as c}from"./p-C0vp-Q7v.js";import{d as p}from"./p-D6vw6bT2.js";import{d as l}from"./p-DSe3wsBa.js";import{d as u}from"./p-BYIBbfJ8.js";class k{constructor(t,i){this.meeting=t,this.audio=null!=i?i:document.createElement("audio"),this.audio.autoplay=!0,this.logger=t.__internals__.logger,this.audioStream=new MediaStream,this.audio.srcObject=this.audioStream,this.trackIdByAudioIdMap=new Map,this.trackOwners=new Map}addTrack(t,i){const s=this.trackIdByAudioIdMap.get(t);if(s===i.id)return void this.play();s&&this.removeTrack(t),this.trackIdByAudioIdMap.set(t,i.id);const a=this.trackOwners.get(i.id);a?a.audioIds.add(t):(this.trackOwners.set(i.id,{track:i,audioIds:new Set([t])}),this.audioStream.addTrack(i)),this.play()}removeTrack(t){const i=this.trackIdByAudioIdMap.get(t);if(!i)return;this.trackIdByAudioIdMap.delete(t);const s=this.trackOwners.get(i);s&&(s.audioIds.delete(t),s.audioIds.size>0||(this.audioStream.removeTrack(s.track),this.trackOwners.delete(i)))}async play(){var t;this.audio.srcObject=this.audioStream,await(null===(t=this.audio.play())||void 0===t?void 0:t.catch((t=>{"NotAllowedError"===t.name?null!=this._onError&&this._onError():"AbortError"!==t.name&&this.logger.error("[rtk-audio] play() error\n",t)})))}async setDevice(t){var i,s,a;o(this.meeting)||await(null===(a=null===(s=(i=this.audio).setSinkId)||void 0===s?void 0:s.call(i,t))||void 0===a?void 0:a.catch((t=>{this.logger.error("[rtk-audio] setSinkId() error\n",t)})))}onError(t){this._onError=t}}var m=function(t,i,s,a){var e,r=arguments.length,o=r<3?i:null===a?a=Object.getOwnPropertyDescriptor(i,s):a;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)o=Reflect.decorate(t,i,s,a);else for(var n=t.length-1;n>=0;n--)(e=t[n])&&(o=(r<3?e(o):r>3?e(i,s,o):e(i,s))||o);return r>3&&o&&Object.defineProperty(i,s,o),o};const b=t(class extends i{constructor(t){super(),!1!==t&&this.__registerHost(),this.__attachShadow(),this.dialogClose=s(this,"dialogClose",7),this.iconPack=n,this.t=h(),this.preloadedAudioElem=void 0,this.showPlayDialog=!1,this.onRtkDialogClose=()=>{this.showPlayDialog=!1,this.dialogClose.emit()}}componentDidLoad(){this.meetingChanged(this.meeting)}disconnectedCallback(){var t;this.meeting&&(this.audioUpdateListener&&this.meeting.participants.joined.removeListener("audioUpdate",this.audioUpdateListener),this.screenShareUpdateListener&&this.meeting.participants.joined.removeListener("screenShareUpdate",this.screenShareUpdateListener),this.participantLeftListener&&this.meeting.participants.joined.removeListener("participantLeft",this.participantLeftListener),this.deviceUpdateListener&&this.meeting.self.removeListener("deviceUpdate",this.deviceUpdateListener),this.stageStatusUpdateListener&&(null===(t=this.meeting.stage)||void 0===t||t.removeListener("stageStatusUpdate",this.stageStatusUpdateListener)))}async setupAudio(){this.audio=new k(this.meeting,this.preloadedAudioElem);const t=this.meeting.self.getCurrentDevices();null!=t.speaker&&await this.audio.setDevice(t.speaker.deviceId)}async handleAutoPlayError(){this.audio||await this.setupAudio(),this.audio.onError((()=>{this.showPlayDialog=!0})),this.audio.play()}async handleEvents(t){this.audioUpdateListener=({id:t,audioEnabled:i,audioTrack:s})=>{const a="audio-"+t;i&&null!=s?this.audio.addTrack(a,s):this.audio.removeTrack(a)};const i=t.participants.joined.toArray();for(const t of i)this.audioUpdateListener(t);this.participantLeftListener=({id:t})=>{this.audio.removeTrack("audio-"+t),this.audio.removeTrack("screenshare-"+t)},this.screenShareUpdateListener=({id:t,screenShareEnabled:i,screenShareTracks:s})=>{const a="screenshare-"+t;i&&null!=s.audio?this.audio.addTrack(a,s.audio):this.audio.removeTrack(a)},this.deviceUpdateListener=({device:t,preview:i})=>{i||"audiooutput"===t.kind&&this.audio.setDevice(t.deviceId)},t.participants.joined.addListener("audioUpdate",this.audioUpdateListener),t.participants.joined.addListener("screenShareUpdate",this.screenShareUpdateListener),t.participants.joined.addListener("participantLeft",this.participantLeftListener),t.self.addListener("deviceUpdate",this.deviceUpdateListener)}async meetingChanged(t){var i;t&&(this.setupAudio(),c(t)?(this.stageStatusUpdateListener=async t=>{"ON_STAGE"===t&&await this.handleAutoPlayError()},null===(i=t.stage)||void 0===i||i.on("stageStatusUpdate",this.stageStatusUpdateListener)):await this.handleAutoPlayError(),await this.handleEvents(t))}render(){return a(e,{key:"14ddae0562633e4433788c18fb89533248df6c2b"},this.showPlayDialog&&a("rtk-dialog",{key:"d0e33058fbcd77861009ce80d7d4e29b2b597475",open:!0,onRtkDialogClose:this.onRtkDialogClose,hideCloseButton:!0,disableEscapeKey:!0,iconPack:this.iconPack,t:this.t},a("div",{key:"e6a9a18c1eaf3e09d13e25373982183b3ad6f8ec",class:"modal"},a("h3",{key:"bc87cc408aad7117cc8d808e38142b3801e5ff53"},this.t("audio_playback.title")),a("p",{key:"b1927a5cd053c59e19a6c4c54044ca500691ee71"},this.t("audio_playback.description")),a("rtk-button",{key:"3c022bb0602295cbd5996ae2ee066e254b1af2fb",kind:"wide",onClick:()=>{this.audio.play(),this.onRtkDialogClose()},title:this.t("audio_playback")},this.t("audio_playback")))))}static get watchers(){return{meeting:[{meetingChanged:0}]}}static get style(){return":host{line-height:initial;font-family:var(--rtk-font-family, sans-serif);font-feature-settings:normal;font-variation-settings:normal}p{margin:var(--rtk-space-0, 0px);padding:var(--rtk-space-0, 0px)}:host{display:block;overflow-wrap:break-word;color:rgb(var(--rtk-colors-text-1000, 255 255 255));word-wrap:break-word}.modal{box-sizing:border-box;width:100%;max-width:var(--rtk-space-96, 384px);padding:var(--rtk-space-4, 16px);border-radius:var(--rtk-border-radius-md, 8px);--tw-bg-opacity:1;background-color:rgba(var(--rtk-colors-background-1000, 8 8 8) / var(--tw-bg-opacity))}.modal h3{margin:var(--rtk-space-0, 0px);padding:var(--rtk-space-0, 0px);font-size:24px;font-weight:500}.modal p{margin-top:var(--rtk-space-4, 16px);margin-bottom:var(--rtk-space-6, 24px)}.modal rtk-button{width:100%}"}},[1,"rtk-participants-audio",{meeting:[16],iconPack:[16],t:[16],preloadedAudioElem:[16],showPlayDialog:[32]},void 0,{meeting:[{meetingChanged:0}]}]);function f(){"undefined"!=typeof customElements&&["rtk-participants-audio","rtk-button","rtk-dialog","rtk-icon"].forEach((t=>{switch(t){case"rtk-participants-audio":customElements.get(r(t))||customElements.define(r(t),b);break;case"rtk-button":customElements.get(r(t))||p();break;case"rtk-dialog":customElements.get(r(t))||l();break;case"rtk-icon":customElements.get(r(t))||u()}}))}m([d()],b.prototype,"meeting",void 0),m([d()],b.prototype,"iconPack",void 0),m([d()],b.prototype,"t",void 0),f();const v=b,g=f;export{v as RtkParticipantsAudio,g as defineCustomElement}
@@ -1,5 +1,5 @@
1
1
  {
2
- "timestamp": "2026-09-29T12:10:59",
2
+ "timestamp": "2026-10-02T12:55:12",
3
3
  "compiler": {
4
4
  "name": "@stencil/core",
5
5
  "version": "4.43.5",
@@ -19276,10 +19276,7 @@ const RtkGrid = class {
19276
19276
  return;
19277
19277
  this.updateActiveParticipants();
19278
19278
  };
19279
- this.onParticipantJoined = debounce(() => {
19280
- this.updateActiveParticipants();
19281
- }, MASS_ACTIONS_DEBOUNCE_TIMER$1);
19282
- this.onParticipantLeft = debounce(() => {
19279
+ this.refreshParticipants = debounce(() => {
19283
19280
  this.updateActiveParticipants();
19284
19281
  }, MASS_ACTIONS_DEBOUNCE_TIMER$1);
19285
19282
  this.stageStatusListener = () => {
@@ -19346,6 +19343,7 @@ const RtkGrid = class {
19346
19343
  var _a;
19347
19344
  if (!meeting)
19348
19345
  return;
19346
+ this.refreshParticipants.cancel();
19349
19347
  this.participants = [];
19350
19348
  this.plugins = [];
19351
19349
  const { self, participants, plugins } = meeting;
@@ -19358,8 +19356,9 @@ const RtkGrid = class {
19358
19356
  plugins === null || plugins === void 0 ? void 0 : plugins.all.removeListener('stateUpdate', this.onPluginStateUpdate);
19359
19357
  (_a = meeting.stage) === null || _a === void 0 ? void 0 : _a.removeListener('stageStatusUpdate', this.stageStatusListener);
19360
19358
  participants.removeListener('viewModeChanged', this.onViewModeChanged);
19361
- participants.active.removeListener('participantLeft', this.onParticipantLeft);
19362
- participants.active.removeListener('participantJoined', this.onParticipantJoined);
19359
+ participants.active.removeListener('participantLeft', this.refreshParticipants);
19360
+ participants.active.removeListener('participantJoined', this.refreshParticipants);
19361
+ participants.active.removeListener('participantsUpdate', this.refreshParticipants);
19363
19362
  participants.pinned.removeListener('participantJoined', this.onParticipantPinned);
19364
19363
  participants.pinned.removeListener('participantLeft', this.onParticipantUnpinned);
19365
19364
  participants.joined.removeListener('screenShareUpdate', this.onScreenShareUpdate);
@@ -19414,10 +19413,11 @@ const RtkGrid = class {
19414
19413
  stage === null || stage === void 0 ? void 0 : stage.addListener('stageStatusUpdate', this.stageStatusListener);
19415
19414
  plugins === null || plugins === void 0 ? void 0 : plugins.all.addListener('stateUpdate', this.onPluginStateUpdate);
19416
19415
  participants.addListener('viewModeChanged', this.onViewModeChanged);
19417
- participants.active.addListener('participantLeft', this.onParticipantLeft);
19416
+ participants.active.addListener('participantLeft', this.refreshParticipants);
19418
19417
  (_c = participants === null || participants === void 0 ? void 0 : participants.joined) === null || _c === void 0 ? void 0 : _c.on('stageStatusUpdate', this.peerStageStatusListener);
19419
19418
  participants.joined.addListener('screenShareUpdate', this.onScreenShareUpdate);
19420
- participants.active.addListener('participantJoined', this.onParticipantJoined);
19419
+ participants.active.addListener('participantJoined', this.refreshParticipants);
19420
+ participants.active.addListener('participantsUpdate', this.refreshParticipants);
19421
19421
  participants.pinned.addListener('participantJoined', this.onParticipantPinned);
19422
19422
  participants.pinned.addListener('participantLeft', this.onParticipantUnpinned);
19423
19423
  if ((_d = meeting.stage) === null || _d === void 0 ? void 0 : _d.status) {
@@ -22142,21 +22142,64 @@ class RTKAudio {
22142
22142
  this.logger = meeting.__internals__.logger;
22143
22143
  this.audioStream = new MediaStream();
22144
22144
  this.audio.srcObject = this.audioStream;
22145
- this.audioTracks = [];
22145
+ this.trackIdByAudioIdMap = new Map();
22146
+ this.trackOwners = new Map();
22146
22147
  }
22147
- addTrack(id, track) {
22148
- // Remove the track if it already exists, with the same id
22149
- this.removeTrack(id);
22150
- this.audioTracks.push({ id, track });
22151
- this.audioStream.addTrack(track);
22148
+ addTrack(audioId, track) {
22149
+ /**
22150
+ * NOTE(ravindra-cloudflare):
22151
+ * audioId format: audio-${peerId} or screenshare-${peerId}.
22152
+ *
22153
+ * During reconnection, the old and new peer IDs of the same participant
22154
+ * can temporarily co-own the same track.
22155
+ *
22156
+ * Before adding track, we should check if the track is already added with a different audio ID.
22157
+ */
22158
+ const currentTrackId = this.trackIdByAudioIdMap.get(audioId);
22159
+ // Track already added, just play it
22160
+ if (currentTrackId === track.id) {
22161
+ this.play();
22162
+ return;
22163
+ }
22164
+ // A logical audio ID owns one track. Release its previous track before replacing it.
22165
+ if (currentTrackId)
22166
+ this.removeTrack(audioId);
22167
+ this.trackIdByAudioIdMap.set(audioId, track.id);
22168
+ const owners = this.trackOwners.get(track.id);
22169
+ if (owners) {
22170
+ // Set membership is idempotent when reconnect events repeat or arrive out of order.
22171
+ owners.audioIds.add(audioId);
22172
+ }
22173
+ else {
22174
+ // If you are the first owner of this track, add it to the audio stream
22175
+ this.trackOwners.set(track.id, { track, audioIds: new Set([audioId]) });
22176
+ this.audioStream.addTrack(track);
22177
+ }
22152
22178
  this.play();
22153
22179
  }
22154
- removeTrack(id) {
22155
- const track = this.audioTracks.find((a) => a.id === id);
22156
- if (track != null) {
22157
- this.audioStream.removeTrack(track.track);
22158
- this.audioTracks = this.audioTracks.filter((a) => a.id !== id);
22159
- }
22180
+ removeTrack(audioId) {
22181
+ /**
22182
+ * NOTE(ravindra-cloudflare):
22183
+ * audioId format: audio-${peerId} or screenshare-${peerId}.
22184
+ *
22185
+ * During reconnection, the old and new peer IDs of the same participant
22186
+ * can temporarily co-own the same track.
22187
+ *
22188
+ * Before removing track, we should check if the track is still owned by other audio IDs.
22189
+ */
22190
+ const trackId = this.trackIdByAudioIdMap.get(audioId);
22191
+ if (!trackId)
22192
+ return;
22193
+ this.trackIdByAudioIdMap.delete(audioId);
22194
+ const owners = this.trackOwners.get(trackId);
22195
+ if (!owners)
22196
+ return;
22197
+ owners.audioIds.delete(audioId);
22198
+ if (owners.audioIds.size > 0)
22199
+ return;
22200
+ // Remove the physical track only after its final logical owner is gone.
22201
+ this.audioStream.removeTrack(owners.track);
22202
+ this.trackOwners.delete(trackId);
22160
22203
  }
22161
22204
  async play() {
22162
22205
  var _a;
@@ -77,10 +77,7 @@ const RtkGrid = class {
77
77
  return;
78
78
  this.updateActiveParticipants();
79
79
  };
80
- this.onParticipantJoined = debounce(() => {
81
- this.updateActiveParticipants();
82
- }, MASS_ACTIONS_DEBOUNCE_TIMER);
83
- this.onParticipantLeft = debounce(() => {
80
+ this.refreshParticipants = debounce(() => {
84
81
  this.updateActiveParticipants();
85
82
  }, MASS_ACTIONS_DEBOUNCE_TIMER);
86
83
  this.stageStatusListener = () => {
@@ -147,6 +144,7 @@ const RtkGrid = class {
147
144
  var _a;
148
145
  if (!meeting)
149
146
  return;
147
+ this.refreshParticipants.cancel();
150
148
  this.participants = [];
151
149
  this.plugins = [];
152
150
  const { self, participants, plugins } = meeting;
@@ -159,8 +157,9 @@ const RtkGrid = class {
159
157
  plugins === null || plugins === void 0 ? void 0 : plugins.all.removeListener('stateUpdate', this.onPluginStateUpdate);
160
158
  (_a = meeting.stage) === null || _a === void 0 ? void 0 : _a.removeListener('stageStatusUpdate', this.stageStatusListener);
161
159
  participants.removeListener('viewModeChanged', this.onViewModeChanged);
162
- participants.active.removeListener('participantLeft', this.onParticipantLeft);
163
- participants.active.removeListener('participantJoined', this.onParticipantJoined);
160
+ participants.active.removeListener('participantLeft', this.refreshParticipants);
161
+ participants.active.removeListener('participantJoined', this.refreshParticipants);
162
+ participants.active.removeListener('participantsUpdate', this.refreshParticipants);
164
163
  participants.pinned.removeListener('participantJoined', this.onParticipantPinned);
165
164
  participants.pinned.removeListener('participantLeft', this.onParticipantUnpinned);
166
165
  participants.joined.removeListener('screenShareUpdate', this.onScreenShareUpdate);
@@ -215,10 +214,11 @@ const RtkGrid = class {
215
214
  stage === null || stage === void 0 ? void 0 : stage.addListener('stageStatusUpdate', this.stageStatusListener);
216
215
  plugins === null || plugins === void 0 ? void 0 : plugins.all.addListener('stateUpdate', this.onPluginStateUpdate);
217
216
  participants.addListener('viewModeChanged', this.onViewModeChanged);
218
- participants.active.addListener('participantLeft', this.onParticipantLeft);
217
+ participants.active.addListener('participantLeft', this.refreshParticipants);
219
218
  (_c = participants === null || participants === void 0 ? void 0 : participants.joined) === null || _c === void 0 ? void 0 : _c.on('stageStatusUpdate', this.peerStageStatusListener);
220
219
  participants.joined.addListener('screenShareUpdate', this.onScreenShareUpdate);
221
- participants.active.addListener('participantJoined', this.onParticipantJoined);
220
+ participants.active.addListener('participantJoined', this.refreshParticipants);
221
+ participants.active.addListener('participantsUpdate', this.refreshParticipants);
222
222
  participants.pinned.addListener('participantJoined', this.onParticipantPinned);
223
223
  participants.pinned.addListener('participantLeft', this.onParticipantUnpinned);
224
224
  if ((_d = meeting.stage) === null || _d === void 0 ? void 0 : _d.status) {
@@ -15,21 +15,64 @@ class RTKAudio {
15
15
  this.logger = meeting.__internals__.logger;
16
16
  this.audioStream = new MediaStream();
17
17
  this.audio.srcObject = this.audioStream;
18
- this.audioTracks = [];
18
+ this.trackIdByAudioIdMap = new Map();
19
+ this.trackOwners = new Map();
19
20
  }
20
- addTrack(id, track) {
21
- // Remove the track if it already exists, with the same id
22
- this.removeTrack(id);
23
- this.audioTracks.push({ id, track });
24
- this.audioStream.addTrack(track);
21
+ addTrack(audioId, track) {
22
+ /**
23
+ * NOTE(ravindra-cloudflare):
24
+ * audioId format: audio-${peerId} or screenshare-${peerId}.
25
+ *
26
+ * During reconnection, the old and new peer IDs of the same participant
27
+ * can temporarily co-own the same track.
28
+ *
29
+ * Before adding track, we should check if the track is already added with a different audio ID.
30
+ */
31
+ const currentTrackId = this.trackIdByAudioIdMap.get(audioId);
32
+ // Track already added, just play it
33
+ if (currentTrackId === track.id) {
34
+ this.play();
35
+ return;
36
+ }
37
+ // A logical audio ID owns one track. Release its previous track before replacing it.
38
+ if (currentTrackId)
39
+ this.removeTrack(audioId);
40
+ this.trackIdByAudioIdMap.set(audioId, track.id);
41
+ const owners = this.trackOwners.get(track.id);
42
+ if (owners) {
43
+ // Set membership is idempotent when reconnect events repeat or arrive out of order.
44
+ owners.audioIds.add(audioId);
45
+ }
46
+ else {
47
+ // If you are the first owner of this track, add it to the audio stream
48
+ this.trackOwners.set(track.id, { track, audioIds: new Set([audioId]) });
49
+ this.audioStream.addTrack(track);
50
+ }
25
51
  this.play();
26
52
  }
27
- removeTrack(id) {
28
- const track = this.audioTracks.find((a) => a.id === id);
29
- if (track != null) {
30
- this.audioStream.removeTrack(track.track);
31
- this.audioTracks = this.audioTracks.filter((a) => a.id !== id);
32
- }
53
+ removeTrack(audioId) {
54
+ /**
55
+ * NOTE(ravindra-cloudflare):
56
+ * audioId format: audio-${peerId} or screenshare-${peerId}.
57
+ *
58
+ * During reconnection, the old and new peer IDs of the same participant
59
+ * can temporarily co-own the same track.
60
+ *
61
+ * Before removing track, we should check if the track is still owned by other audio IDs.
62
+ */
63
+ const trackId = this.trackIdByAudioIdMap.get(audioId);
64
+ if (!trackId)
65
+ return;
66
+ this.trackIdByAudioIdMap.delete(audioId);
67
+ const owners = this.trackOwners.get(trackId);
68
+ if (!owners)
69
+ return;
70
+ owners.audioIds.delete(audioId);
71
+ if (owners.audioIds.size > 0)
72
+ return;
73
+ // Remove the physical track only after its final logical owner is gone.
74
+ this.audioStream.removeTrack(owners.track);
75
+ this.trackOwners.delete(trackId);
33
76
  }
34
77
  async play() {
35
78
  var _a;