@livedigital/client 3.23.1 → 3.24.0

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.
@@ -41,7 +41,9 @@ class MediaSoupEventHandler {
41
41
  throw new Error('Socket connection not initialized');
42
42
  }
43
43
 
44
- connection.on(MEDIASOUP_EVENTS.producerClose, (producer: ProducerData) => {
44
+ this.engine.eventsQueue.subscribeToSocket(connection);
45
+
46
+ this.engine.eventsQueue.on(MEDIASOUP_EVENTS.producerClose, (producer: ProducerData) => {
45
47
  const peer = this.engine.peers.find((item) => item.id === producer.peerId);
46
48
  if (!peer) {
47
49
  return;
@@ -50,7 +52,7 @@ class MediaSoupEventHandler {
50
52
  peer.observer.safeEmit(MEDIASOUP_EVENTS.producerClose, producer);
51
53
  });
52
54
 
53
- connection.on(MEDIASOUP_EVENTS.newProducer, (producer: ProducerData) => {
55
+ this.engine.eventsQueue.on(MEDIASOUP_EVENTS.newProducer, (producer: ProducerData) => {
54
56
  const peer = this.engine.peers.find((item) => item.id === producer.peerId);
55
57
  if (!peer) {
56
58
  return;
@@ -59,7 +61,7 @@ class MediaSoupEventHandler {
59
61
  peer.observer.safeEmit(MEDIASOUP_EVENTS.newProducer, producer);
60
62
  });
61
63
 
62
- connection.on(MEDIASOUP_EVENTS.closeConsumer, (consumerId: string, peerId: string) => {
64
+ this.engine.eventsQueue.on(MEDIASOUP_EVENTS.closeConsumer, (consumerId: string, peerId: string) => {
63
65
  const peer = this.engine.peers.find((item) => item.id === peerId);
64
66
  if (!peer) {
65
67
  return;
@@ -68,7 +70,7 @@ class MediaSoupEventHandler {
68
70
  peer.observer.safeEmit(MEDIASOUP_EVENTS.closeConsumer, consumerId);
69
71
  });
70
72
 
71
- connection.on(MEDIASOUP_EVENTS.resumeConsumer, (consumerId: string, peerId: string) => {
73
+ this.engine.eventsQueue.on(MEDIASOUP_EVENTS.resumeConsumer, (consumerId: string, peerId: string) => {
72
74
  const peer = this.engine.peers.find((item) => item.id === peerId);
73
75
  if (!peer || peer.isMe) {
74
76
  return;
@@ -77,7 +79,7 @@ class MediaSoupEventHandler {
77
79
  peer.observer.safeEmit(MEDIASOUP_EVENTS.resumeConsumer, consumerId);
78
80
  });
79
81
 
80
- connection.on(MEDIASOUP_EVENTS.pauseConsumer, (consumerId: string, peerId: string) => {
82
+ this.engine.eventsQueue.on(MEDIASOUP_EVENTS.pauseConsumer, (consumerId: string, peerId: string) => {
81
83
  const peer = this.engine.peers.find((item) => item.id === peerId);
82
84
  if (!peer || peer.isMe) {
83
85
  return;
@@ -86,7 +88,7 @@ class MediaSoupEventHandler {
86
88
  peer.observer.safeEmit(MEDIASOUP_EVENTS.pauseConsumer, consumerId);
87
89
  });
88
90
 
89
- connection.on(MEDIASOUP_EVENTS.consumerChangePreferredLayers, ({
91
+ this.engine.eventsQueue.on(MEDIASOUP_EVENTS.consumerChangePreferredLayers, ({
90
92
  peerId, spatialLayer, temporalLayer, consumerId,
91
93
  }: ChangePreferredLayersParams) => {
92
94
  const peer = this.engine.peers.find((item) => item.id === peerId);
@@ -101,7 +103,7 @@ class MediaSoupEventHandler {
101
103
  });
102
104
  });
103
105
 
104
- connection.on(MEDIASOUP_EVENTS.consumerScoreChanged, (payload: ConsumerScoreChangedPayload) => {
106
+ this.engine.eventsQueue.on(MEDIASOUP_EVENTS.consumerScoreChanged, (payload: ConsumerScoreChangedPayload) => {
105
107
  const { consumerPeerId } = payload;
106
108
  const peer = this.engine.peers.find((item) => item.id === consumerPeerId);
107
109
  if (!peer || peer.isMe) {
@@ -116,7 +118,7 @@ class MediaSoupEventHandler {
116
118
  }
117
119
  });
118
120
 
119
- connection.on(MEDIASOUP_EVENTS.producerScoreChanged, (payload: ProducerScoreChangedPayload) => {
121
+ this.engine.eventsQueue.on(MEDIASOUP_EVENTS.producerScoreChanged, (payload: ProducerScoreChangedPayload) => {
120
122
  const peer = this.engine.peers.find((item) => item.isMe);
121
123
  if (!peer) {
122
124
  return;
@@ -125,7 +127,7 @@ class MediaSoupEventHandler {
125
127
  peer.observer.safeEmit(MEDIASOUP_EVENTS.producerScoreChanged, payload);
126
128
  });
127
129
 
128
- connection.on(MEDIASOUP_EVENTS.producerRequestMaxSpatialLayer, async ({
130
+ this.engine.eventsQueue.on(MEDIASOUP_EVENTS.producerRequestMaxSpatialLayer, async ({
129
131
  producerId,
130
132
  spatialLayer,
131
133
  }: ProducerRequestMaxSpatialLayer) => {
@@ -134,7 +136,7 @@ class MediaSoupEventHandler {
134
136
  }, MEDIASOUP_EVENTS.producerRequestMaxSpatialLayer);
135
137
  });
136
138
 
137
- connection.on(MEDIASOUP_EVENTS.producerSetMaxSpatialLayer, async ({
139
+ this.engine.eventsQueue.on(MEDIASOUP_EVENTS.producerSetMaxSpatialLayer, async ({
138
140
  peerId,
139
141
  producerId,
140
142
  spatialLayer,
@@ -147,7 +149,7 @@ class MediaSoupEventHandler {
147
149
  peer.observer.safeEmit(MEDIASOUP_EVENTS.producerSetMaxSpatialLayer, { peerId, producerId, spatialLayer });
148
150
  });
149
151
 
150
- connection.on(MEDIASOUP_EVENTS.transportConnectionTimeout, async ({
152
+ this.engine.eventsQueue.on(MEDIASOUP_EVENTS.transportConnectionTimeout, async ({
151
153
  reason,
152
154
  transportId,
153
155
  direction,
@@ -174,39 +176,41 @@ class MediaSoupEventHandler {
174
176
  });
175
177
  });
176
178
 
177
- connection.on(MEDIASOUP_EVENTS.producerPaused, ({ peerId, producerId }: ChangeProducerStatePayload) => {
178
- const peer = this.engine.peers.find((item) => item.id === peerId);
179
- const producer = peer?.producers.get(producerId);
180
- if (!peer || !producer) {
181
- return;
182
- }
183
-
184
- producer.paused = true;
185
- peer.observer.safeEmit(PEER_EVENTS.publisherPaused, {
186
- producerId: producer.id,
187
- kind: producer.kind,
188
- label: producer.label,
189
- paused: producer.paused,
179
+ this.engine.eventsQueue
180
+ .on(MEDIASOUP_EVENTS.producerPaused, ({ peerId, producerId }: ChangeProducerStatePayload) => {
181
+ const peer = this.engine.peers.find((item) => item.id === peerId);
182
+ const producer = peer?.producers.get(producerId);
183
+ if (!peer || !producer) {
184
+ return;
185
+ }
186
+
187
+ producer.paused = true;
188
+ peer.observer.safeEmit(PEER_EVENTS.publisherPaused, {
189
+ producerId: producer.id,
190
+ kind: producer.kind,
191
+ label: producer.label,
192
+ paused: producer.paused,
193
+ });
190
194
  });
191
- });
192
-
193
- connection.on(MEDIASOUP_EVENTS.producerResumed, ({ peerId, producerId }: ChangeProducerStatePayload) => {
194
- const peer = this.engine.peers.find((item) => item.id === peerId);
195
- const producer = peer?.producers.get(producerId);
196
- if (!peer || !producer) {
197
- return;
198
- }
199
195
 
200
- producer.paused = false;
201
- peer.observer.safeEmit(PEER_EVENTS.publisherResumed, {
202
- producerId: producer.id,
203
- kind: producer.kind,
204
- label: producer.label,
205
- paused: producer.paused,
196
+ this.engine.eventsQueue
197
+ .on(MEDIASOUP_EVENTS.producerResumed, ({ peerId, producerId }: ChangeProducerStatePayload) => {
198
+ const peer = this.engine.peers.find((item) => item.id === peerId);
199
+ const producer = peer?.producers.get(producerId);
200
+ if (!peer || !producer) {
201
+ return;
202
+ }
203
+
204
+ producer.paused = false;
205
+ peer.observer.safeEmit(PEER_EVENTS.publisherResumed, {
206
+ producerId: producer.id,
207
+ kind: producer.kind,
208
+ label: producer.label,
209
+ paused: producer.paused,
210
+ });
206
211
  });
207
- });
208
212
 
209
- connection.on(MEDIASOUP_EVENTS.producerForceClosed, async ({ label }: ForceCloseProducerPayload) => {
213
+ this.engine.eventsQueue.on(MEDIASOUP_EVENTS.producerForceClosed, async ({ label }: ForceCloseProducerPayload) => {
210
214
  const track = this.engine.media.getTrack(label);
211
215
  if (!track) {
212
216
  return;
@@ -220,9 +224,7 @@ class MediaSoupEventHandler {
220
224
  }
221
225
 
222
226
  private removeEventListeners() {
223
- const { connection } = this.engine.network.socket;
224
-
225
- if (!connection) {
227
+ if (!this.engine.eventsQueue) {
226
228
  return;
227
229
  }
228
230
 
@@ -243,7 +245,9 @@ class MediaSoupEventHandler {
243
245
  MEDIASOUP_EVENTS.producerForceClosed,
244
246
  ];
245
247
 
246
- eventNames.forEach((x) => connection.removeAllListeners(x));
248
+ const { connection } = this.engine.network.socket;
249
+
250
+ this.engine.eventsQueue.removeAllListeners(eventNames, connection);
247
251
  }
248
252
 
249
253
  public reset() {
@@ -57,6 +57,7 @@ import {
57
57
  import ChannelStateSyncEventHandler from './handlers/ChannelStateSyncEventHandler';
58
58
  import { getSupportedCodecsMimeTypes } from '../helpers/media';
59
59
  import ChannelStateSynchronizer from './ChannelStateSynchronizer';
60
+ import EventsQueue from './EventsQueue';
60
61
  import clientMetaProvider from '../ClientMetaProvider';
61
62
 
62
63
  type EngineParams = {
@@ -89,6 +90,8 @@ class Engine {
89
90
 
90
91
  public internalEventEmitter: EnhancedEventEmitter<InternalObserverEvents>;
91
92
 
93
+ public eventsQueue: EventsQueue;
94
+
92
95
  public logLevel: LogLevel;
93
96
 
94
97
  public readonly peersRepository: Map<string, Peer>;
@@ -183,6 +186,7 @@ class Engine {
183
186
  this.peersRepository = new Map<string, Peer>();
184
187
  this.clientEventEmitter = clientEventEmitter;
185
188
  this.internalEventEmitter = internalEventEmitter;
189
+ this.eventsQueue = new EventsQueue();
186
190
  this.channelEventsHandler = dependenciesFactory.createChannelEventHandler({
187
191
  engine: this,
188
192
  onLogMessage: params.onLogMessage,
@@ -310,6 +314,7 @@ class Engine {
310
314
  this.audioObserver?.close();
311
315
 
312
316
  this.generalDataConsumer?.removeListener('message', this.handleDataChannelEvent);
317
+ this.eventsQueue.removeListener('message');
313
318
  this.generalDataConsumer?.close();
314
319
 
315
320
  this.channelStateSyncDataConsumer?.removeListener('message', this.handleChannelStateSyncEvent);
@@ -852,7 +857,8 @@ class Engine {
852
857
  ordered: true,
853
858
  });
854
859
 
855
- this.generalDataConsumer.on('message', this.handleDataChannelEvent);
860
+ this.eventsQueue.subscribeToDataChannel(this.generalDataConsumer);
861
+ this.eventsQueue.on('message', this.handleDataChannelEvent);
856
862
  this.logger.debug('Successfully created general data consumer', {
857
863
  ...logCtx,
858
864
  producerId,
@@ -3,10 +3,17 @@ export interface ChannelStateProducer {
3
3
  paused: boolean;
4
4
  }
5
5
 
6
+ export interface ChannelStateConsumer {
7
+ id: string;
8
+ paused: boolean;
9
+ producerId: string;
10
+ }
11
+
6
12
  export interface ChannelStatePeer {
7
13
  id: string;
8
14
  appDataHash: string;
9
15
  producers: ChannelStateProducer[];
16
+ consumers?: ChannelStateConsumer[];
10
17
  }
11
18
 
12
19
  export interface ChannelState {