livekit-client 1.15.5 → 1.15.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. package/dist/livekit-client.e2ee.worker.js +1 -1
  2. package/dist/livekit-client.e2ee.worker.js.map +1 -1
  3. package/dist/livekit-client.e2ee.worker.mjs +14 -1
  4. package/dist/livekit-client.e2ee.worker.mjs.map +1 -1
  5. package/dist/livekit-client.esm.mjs +561 -328
  6. package/dist/livekit-client.esm.mjs.map +1 -1
  7. package/dist/livekit-client.umd.js +1 -1
  8. package/dist/livekit-client.umd.js.map +1 -1
  9. package/dist/src/api/SignalClient.d.ts +5 -1
  10. package/dist/src/api/SignalClient.d.ts.map +1 -1
  11. package/dist/src/index.d.ts +2 -2
  12. package/dist/src/index.d.ts.map +1 -1
  13. package/dist/src/logger.d.ts +19 -3
  14. package/dist/src/logger.d.ts.map +1 -1
  15. package/dist/src/options.d.ts +1 -0
  16. package/dist/src/options.d.ts.map +1 -1
  17. package/dist/src/room/PCTransport.d.ts +5 -1
  18. package/dist/src/room/PCTransport.d.ts.map +1 -1
  19. package/dist/src/room/PCTransportManager.d.ts +5 -1
  20. package/dist/src/room/PCTransportManager.d.ts.map +1 -1
  21. package/dist/src/room/RTCEngine.d.ts +8 -0
  22. package/dist/src/room/RTCEngine.d.ts.map +1 -1
  23. package/dist/src/room/Room.d.ts +2 -0
  24. package/dist/src/room/Room.d.ts.map +1 -1
  25. package/dist/src/room/participant/LocalParticipant.d.ts.map +1 -1
  26. package/dist/src/room/participant/Participant.d.ts +9 -1
  27. package/dist/src/room/participant/Participant.d.ts.map +1 -1
  28. package/dist/src/room/participant/RemoteParticipant.d.ts +2 -1
  29. package/dist/src/room/participant/RemoteParticipant.d.ts.map +1 -1
  30. package/dist/src/room/participant/publishUtils.d.ts +2 -1
  31. package/dist/src/room/participant/publishUtils.d.ts.map +1 -1
  32. package/dist/src/room/timers.d.ts +4 -5
  33. package/dist/src/room/timers.d.ts.map +1 -1
  34. package/dist/src/room/track/LocalAudioTrack.d.ts +2 -1
  35. package/dist/src/room/track/LocalAudioTrack.d.ts.map +1 -1
  36. package/dist/src/room/track/LocalTrack.d.ts +2 -1
  37. package/dist/src/room/track/LocalTrack.d.ts.map +1 -1
  38. package/dist/src/room/track/LocalTrackPublication.d.ts +2 -1
  39. package/dist/src/room/track/LocalTrackPublication.d.ts.map +1 -1
  40. package/dist/src/room/track/LocalVideoTrack.d.ts +2 -1
  41. package/dist/src/room/track/LocalVideoTrack.d.ts.map +1 -1
  42. package/dist/src/room/track/RemoteAudioTrack.d.ts +2 -1
  43. package/dist/src/room/track/RemoteAudioTrack.d.ts.map +1 -1
  44. package/dist/src/room/track/RemoteTrack.d.ts +2 -1
  45. package/dist/src/room/track/RemoteTrack.d.ts.map +1 -1
  46. package/dist/src/room/track/RemoteTrackPublication.d.ts +2 -1
  47. package/dist/src/room/track/RemoteTrackPublication.d.ts.map +1 -1
  48. package/dist/src/room/track/RemoteVideoTrack.d.ts +2 -1
  49. package/dist/src/room/track/RemoteVideoTrack.d.ts.map +1 -1
  50. package/dist/src/room/track/Track.d.ts +10 -1
  51. package/dist/src/room/track/Track.d.ts.map +1 -1
  52. package/dist/src/room/track/TrackPublication.d.ts +7 -1
  53. package/dist/src/room/track/TrackPublication.d.ts.map +1 -1
  54. package/dist/src/room/track/create.d.ts.map +1 -1
  55. package/dist/src/room/track/options.d.ts +8 -3
  56. package/dist/src/room/track/options.d.ts.map +1 -1
  57. package/dist/src/room/track/utils.d.ts +1 -0
  58. package/dist/src/room/track/utils.d.ts.map +1 -1
  59. package/dist/src/room/types.d.ts +4 -0
  60. package/dist/src/room/types.d.ts.map +1 -1
  61. package/dist/src/room/utils.d.ts +1 -0
  62. package/dist/src/room/utils.d.ts.map +1 -1
  63. package/dist/ts4.2/src/api/SignalClient.d.ts +5 -1
  64. package/dist/ts4.2/src/index.d.ts +2 -2
  65. package/dist/ts4.2/src/logger.d.ts +19 -3
  66. package/dist/ts4.2/src/options.d.ts +1 -0
  67. package/dist/ts4.2/src/room/PCTransport.d.ts +5 -1
  68. package/dist/ts4.2/src/room/PCTransportManager.d.ts +5 -1
  69. package/dist/ts4.2/src/room/RTCEngine.d.ts +8 -0
  70. package/dist/ts4.2/src/room/Room.d.ts +2 -0
  71. package/dist/ts4.2/src/room/participant/Participant.d.ts +9 -1
  72. package/dist/ts4.2/src/room/participant/RemoteParticipant.d.ts +2 -1
  73. package/dist/ts4.2/src/room/participant/publishUtils.d.ts +2 -1
  74. package/dist/ts4.2/src/room/timers.d.ts +4 -5
  75. package/dist/ts4.2/src/room/track/LocalAudioTrack.d.ts +2 -1
  76. package/dist/ts4.2/src/room/track/LocalTrack.d.ts +2 -1
  77. package/dist/ts4.2/src/room/track/LocalTrackPublication.d.ts +2 -1
  78. package/dist/ts4.2/src/room/track/LocalVideoTrack.d.ts +2 -1
  79. package/dist/ts4.2/src/room/track/RemoteAudioTrack.d.ts +2 -1
  80. package/dist/ts4.2/src/room/track/RemoteTrack.d.ts +2 -1
  81. package/dist/ts4.2/src/room/track/RemoteTrackPublication.d.ts +2 -1
  82. package/dist/ts4.2/src/room/track/RemoteVideoTrack.d.ts +2 -1
  83. package/dist/ts4.2/src/room/track/Track.d.ts +10 -1
  84. package/dist/ts4.2/src/room/track/TrackPublication.d.ts +7 -1
  85. package/dist/ts4.2/src/room/track/options.d.ts +8 -3
  86. package/dist/ts4.2/src/room/track/utils.d.ts +1 -0
  87. package/dist/ts4.2/src/room/types.d.ts +4 -0
  88. package/dist/ts4.2/src/room/utils.d.ts +1 -0
  89. package/package.json +15 -15
  90. package/src/api/SignalClient.ts +43 -21
  91. package/src/index.ts +2 -1
  92. package/src/logger.ts +32 -8
  93. package/src/options.ts +2 -0
  94. package/src/room/PCTransport.ts +29 -8
  95. package/src/room/PCTransportManager.ts +29 -9
  96. package/src/room/RTCEngine.ts +71 -34
  97. package/src/room/Room.ts +94 -30
  98. package/src/room/participant/LocalParticipant.ts +163 -47
  99. package/src/room/participant/Participant.ts +26 -3
  100. package/src/room/participant/RemoteParticipant.ts +23 -15
  101. package/src/room/participant/publishUtils.test.ts +2 -2
  102. package/src/room/participant/publishUtils.ts +7 -4
  103. package/src/room/track/LocalAudioTrack.ts +8 -7
  104. package/src/room/track/LocalTrack.ts +27 -20
  105. package/src/room/track/LocalTrackPublication.ts +3 -2
  106. package/src/room/track/LocalVideoTrack.ts +31 -13
  107. package/src/room/track/RemoteAudioTrack.ts +4 -3
  108. package/src/room/track/RemoteTrack.ts +4 -1
  109. package/src/room/track/RemoteTrackPublication.ts +21 -13
  110. package/src/room/track/RemoteVideoTrack.ts +5 -4
  111. package/src/room/track/Track.ts +32 -2
  112. package/src/room/track/TrackPublication.ts +18 -3
  113. package/src/room/track/create.ts +4 -3
  114. package/src/room/track/options.ts +12 -5
  115. package/src/room/track/utils.ts +23 -1
  116. package/src/room/types.ts +5 -0
  117. package/src/room/utils.ts +5 -0
@@ -2,8 +2,9 @@ import { EventEmitter } from 'events';
2
2
  import type { MediaDescription } from 'sdp-transform';
3
3
  import { parse, write } from 'sdp-transform';
4
4
  import { debounce } from 'ts-debounce';
5
- import log from '../logger';
5
+ import log, { LoggerNames, getLogger } from '../logger';
6
6
  import { NegotiationError, UnexpectedConnectionState } from './errors';
7
+ import type { LoggerOptions } from './types';
7
8
  import { ddExtensionURI, isChromiumBased, isSVCCodec } from './utils';
8
9
 
9
10
  /** @internal */
@@ -43,6 +44,10 @@ export default class PCTransport extends EventEmitter {
43
44
 
44
45
  private mediaConstraints: Record<string, unknown>;
45
46
 
47
+ private log = log;
48
+
49
+ private loggerOptions: LoggerOptions;
50
+
46
51
  pendingCandidates: RTCIceCandidateInit[] = [];
47
52
 
48
53
  restartingIce: boolean = false;
@@ -71,8 +76,14 @@ export default class PCTransport extends EventEmitter {
71
76
 
72
77
  onTrack?: (ev: RTCTrackEvent) => void;
73
78
 
74
- constructor(config?: RTCConfiguration, mediaConstraints: Record<string, unknown> = {}) {
79
+ constructor(
80
+ config?: RTCConfiguration,
81
+ mediaConstraints: Record<string, unknown> = {},
82
+ loggerOptions: LoggerOptions = {},
83
+ ) {
75
84
  super();
85
+ this.log = getLogger(loggerOptions.loggerName ?? LoggerNames.PCTransport);
86
+ this.loggerOptions = loggerOptions;
76
87
  this.config = config;
77
88
  this.mediaConstraints = mediaConstraints;
78
89
  this._pc = this.createPC();
@@ -112,6 +123,12 @@ export default class PCTransport extends EventEmitter {
112
123
  return pc;
113
124
  }
114
125
 
126
+ private get logContext() {
127
+ return {
128
+ ...this.loggerOptions.loggerContextCb?.(),
129
+ };
130
+ }
131
+
115
132
  get isICEConnected(): boolean {
116
133
  return (
117
134
  this._pc !== null &&
@@ -229,7 +246,7 @@ export default class PCTransport extends EventEmitter {
229
246
  }
230
247
 
231
248
  if (options?.iceRestart) {
232
- log.debug('restarting ICE');
249
+ this.log.debug('restarting ICE', this.logContext);
233
250
  this.restartingIce = true;
234
251
  }
235
252
 
@@ -246,12 +263,12 @@ export default class PCTransport extends EventEmitter {
246
263
  return;
247
264
  }
248
265
  } else if (!this._pc || this._pc.signalingState === 'closed') {
249
- log.warn('could not createOffer with closed peer connection');
266
+ this.log.warn('could not createOffer with closed peer connection', this.logContext);
250
267
  return;
251
268
  }
252
269
 
253
270
  // actually negotiate
254
- log.debug('starting to negotiate');
271
+ this.log.debug('starting to negotiate', this.logContext);
255
272
  const offer = await this.pc.createOffer(options);
256
273
 
257
274
  const sdpParsed = parse(offer.sdp ?? '');
@@ -452,7 +469,10 @@ export default class PCTransport extends EventEmitter {
452
469
  const originalSdp = sd.sdp;
453
470
  sd.sdp = munged;
454
471
  try {
455
- log.debug(`setting munged ${remote ? 'remote' : 'local'} description`);
472
+ this.log.debug(
473
+ `setting munged ${remote ? 'remote' : 'local'} description`,
474
+ this.logContext,
475
+ );
456
476
  if (remote) {
457
477
  await this.pc.setRemoteDescription(sd);
458
478
  } else {
@@ -460,7 +480,8 @@ export default class PCTransport extends EventEmitter {
460
480
  }
461
481
  return;
462
482
  } catch (e) {
463
- log.warn(`not able to set ${sd.type}, falling back to unmodified sdp`, {
483
+ this.log.warn(`not able to set ${sd.type}, falling back to unmodified sdp`, {
484
+ ...this.logContext,
464
485
  error: e,
465
486
  sdp: munged,
466
487
  });
@@ -491,7 +512,7 @@ export default class PCTransport extends EventEmitter {
491
512
  if (!remote && this.pc.remoteDescription) {
492
513
  fields.remoteSdp = this.pc.remoteDescription;
493
514
  }
494
- log.error(`unable to set ${sd.type}`, fields);
515
+ this.log.error(`unable to set ${sd.type}`, { ...this.logContext, fields });
495
516
  throw new NegotiationError(msg);
496
517
  }
497
518
  }
@@ -1,9 +1,10 @@
1
- import log from '../logger';
1
+ import log, { LoggerNames, getLogger } from '../logger';
2
2
  import { SignalTarget } from '../proto/livekit_rtc_pb';
3
3
  import PCTransport, { PCEvents } from './PCTransport';
4
4
  import { roomConnectOptionDefaults } from './defaults';
5
5
  import { ConnectionError, ConnectionErrorReason } from './errors';
6
6
  import CriticalTimers from './timers';
7
+ import type { LoggerOptions } from './types';
7
8
  import { Mutex, sleep } from './utils';
8
9
 
9
10
  export enum PCTransportState {
@@ -56,12 +57,23 @@ export class PCTransportManager {
56
57
 
57
58
  private connectionLock: Mutex;
58
59
 
59
- constructor(rtcConfig: RTCConfiguration, subscriberPrimary: boolean) {
60
+ private log = log;
61
+
62
+ private loggerOptions: LoggerOptions;
63
+
64
+ constructor(
65
+ rtcConfig: RTCConfiguration,
66
+ subscriberPrimary: boolean,
67
+ loggerOptions: LoggerOptions,
68
+ ) {
69
+ this.log = getLogger(loggerOptions.loggerName ?? LoggerNames.PCManager);
70
+ this.loggerOptions = loggerOptions;
71
+
60
72
  this.isPublisherConnectionRequired = !subscriberPrimary;
61
73
  this.isSubscriberConnectionRequired = subscriberPrimary;
62
74
  const googConstraints = { optional: [{ googDscp: true }] };
63
- this.publisher = new PCTransport(rtcConfig, googConstraints);
64
- this.subscriber = new PCTransport(rtcConfig);
75
+ this.publisher = new PCTransport(rtcConfig, googConstraints, loggerOptions);
76
+ this.subscriber = new PCTransport(rtcConfig, loggerOptions);
65
77
 
66
78
  this.publisher.onConnectionStateChange = this.updateState;
67
79
  this.subscriber.onConnectionStateChange = this.updateState;
@@ -91,6 +103,12 @@ export class PCTransportManager {
91
103
  this.connectionLock = new Mutex();
92
104
  }
93
105
 
106
+ private get logContext() {
107
+ return {
108
+ ...this.loggerOptions.loggerContextCb?.(),
109
+ };
110
+ }
111
+
94
112
  requirePublisher(require = true) {
95
113
  this.isPublisherConnectionRequired = require;
96
114
  this.updateState();
@@ -123,7 +141,7 @@ export class PCTransportManager {
123
141
  publisher.removeTrack(sender);
124
142
  }
125
143
  } catch (e) {
126
- log.warn('could not removeTrack', { error: e });
144
+ this.log.warn('could not removeTrack', { ...this.logContext, error: e });
127
145
  }
128
146
  }
129
147
  }
@@ -148,7 +166,8 @@ export class PCTransportManager {
148
166
  }
149
167
 
150
168
  async createSubscriberAnswerFromOffer(sd: RTCSessionDescriptionInit) {
151
- log.debug('received server offer', {
169
+ this.log.debug('received server offer', {
170
+ ...this.logContext,
152
171
  RTCSdpType: sd.type,
153
172
  signalingState: this.subscriber.getSignallingState().toString(),
154
173
  });
@@ -175,7 +194,7 @@ export class PCTransportManager {
175
194
  this.publisher.getConnectionState() !== 'connected' &&
176
195
  this.publisher.getConnectionState() !== 'connecting'
177
196
  ) {
178
- log.debug('negotiation required, start negotiating');
197
+ this.log.debug('negotiation required, start negotiating', this.logContext);
179
198
  this.publisher.negotiate();
180
199
  }
181
200
  await Promise.all(
@@ -271,10 +290,11 @@ export class PCTransportManager {
271
290
  }
272
291
 
273
292
  if (previousState !== this.state) {
274
- log.debug(
293
+ this.log.debug(
275
294
  `pc state change: from ${PCTransportState[previousState]} to ${
276
295
  PCTransportState[this.state]
277
296
  }`,
297
+ this.logContext,
278
298
  );
279
299
  this.onStateChange?.(
280
300
  this.state,
@@ -296,7 +316,7 @@ export class PCTransportManager {
296
316
 
297
317
  return new Promise<void>(async (resolve, reject) => {
298
318
  const abortHandler = () => {
299
- log.warn('abort transport connection');
319
+ this.log.warn('abort transport connection', this.logContext);
300
320
  CriticalTimers.clearTimeout(connectTimeout);
301
321
 
302
322
  reject(
@@ -7,7 +7,7 @@ import {
7
7
  SignalConnectionState,
8
8
  toProtoSessionDescription,
9
9
  } from '../api/SignalClient';
10
- import log from '../logger';
10
+ import log, { LoggerNames, getLogger } from '../logger';
11
11
  import type { InternalRoomOptions } from '../options';
12
12
  import {
13
13
  ClientConfigSetting,
@@ -61,6 +61,7 @@ import type RemoteTrackPublication from './track/RemoteTrackPublication';
61
61
  import { Track } from './track/Track';
62
62
  import type { TrackPublishOptions, VideoCodec } from './track/options';
63
63
  import { getTrackPublicationInfo } from './track/utils';
64
+ import type { LoggerOptions } from './types';
64
65
  import {
65
66
  Mutex,
66
67
  isVideoCodec,
@@ -162,9 +163,18 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
162
163
 
163
164
  private regionUrlProvider?: RegionUrlProvider;
164
165
 
166
+ private log = log;
167
+
168
+ private loggerOptions: LoggerOptions;
169
+
165
170
  constructor(private options: InternalRoomOptions) {
166
171
  super();
167
- this.client = new SignalClient();
172
+ this.log = getLogger(options.loggerName ?? LoggerNames.Engine);
173
+ this.loggerOptions = {
174
+ loggerName: options.loggerName,
175
+ loggerContextCb: () => this.logContext,
176
+ };
177
+ this.client = new SignalClient(undefined, this.loggerOptions);
168
178
  this.client.signalLatency = this.options.expSignalLatency;
169
179
  this.reconnectPolicy = this.options.reconnectPolicy;
170
180
  this.registerOnLineListener();
@@ -187,6 +197,15 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
187
197
  this.client.onStreamStateUpdate = (update) => this.emit(EngineEvent.StreamStateChanged, update);
188
198
  }
189
199
 
200
+ /** @internal */
201
+ get logContext() {
202
+ return {
203
+ room: this.latestJoinResponse?.room?.name,
204
+ roomSid: this.latestJoinResponse?.room?.sid,
205
+ identity: this.latestJoinResponse?.participant?.identity,
206
+ };
207
+ }
208
+
190
209
  async join(
191
210
  url: string,
192
211
  token: string,
@@ -220,8 +239,9 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
220
239
  } catch (e) {
221
240
  if (e instanceof ConnectionError) {
222
241
  if (e.reason === ConnectionErrorReason.ServerUnreachable) {
223
- log.warn(
242
+ this.log.warn(
224
243
  `Couldn't connect to server, attempt ${this.joinAttempts} of ${this.maxJoinAttempts}`,
244
+ this.logContext,
225
245
  );
226
246
  if (this.joinAttempts < this.maxJoinAttempts) {
227
247
  return this.join(url, token, opts, abortSignal);
@@ -324,7 +344,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
324
344
  this.pcManager!.removeTrack(sender);
325
345
  return true;
326
346
  } catch (e: unknown) {
327
- log.warn('failed to remove track', { error: e, method: 'removeTrack' });
347
+ this.log.warn('failed to remove track', { ...this.logContext, error: e });
328
348
  }
329
349
  return false;
330
350
  }
@@ -356,7 +376,11 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
356
376
 
357
377
  const rtcConfig = this.makeRTCConfiguration(joinResponse);
358
378
 
359
- this.pcManager = new PCTransportManager(rtcConfig, joinResponse.subscriberPrimary);
379
+ this.pcManager = new PCTransportManager(
380
+ rtcConfig,
381
+ joinResponse.subscriberPrimary,
382
+ this.loggerOptions,
383
+ );
360
384
 
361
385
  this.emit(EngineEvent.TransportsCreated, this.pcManager.publisher, this.pcManager.subscriber);
362
386
 
@@ -370,7 +394,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
370
394
 
371
395
  this.pcManager.onDataChannel = this.handleDataChannel;
372
396
  this.pcManager.onStateChange = async (connectionState, publisherState, subscriberState) => {
373
- log.debug(`primary PC state changed ${connectionState}`);
397
+ this.log.debug(`primary PC state changed ${connectionState}`, this.logContext);
374
398
  if (connectionState === PCTransportState.CONNECTED) {
375
399
  const shouldEmit = this.pcState === PCState.New;
376
400
  this.pcState = PCState.Connected;
@@ -404,9 +428,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
404
428
  if (!this.pcManager) {
405
429
  return;
406
430
  }
407
- log.debug('received server answer', {
408
- RTCSdpType: sd.type,
409
- });
431
+ this.log.debug('received server answer', { ...this.logContext, RTCSdpType: sd.type });
410
432
  await this.pcManager.setPublisherAnswer(sd);
411
433
  };
412
434
 
@@ -415,7 +437,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
415
437
  if (!this.pcManager) {
416
438
  return;
417
439
  }
418
- log.trace('got ICE candidate from peer', { candidate, target });
440
+ this.log.trace('got ICE candidate from peer', { ...this.logContext, candidate, target });
419
441
  this.pcManager.addIceCandidate(candidate, target);
420
442
  };
421
443
 
@@ -429,9 +451,16 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
429
451
  };
430
452
 
431
453
  this.client.onLocalTrackPublished = (res: TrackPublishedResponse) => {
432
- log.debug('received trackPublishedResponse', res);
454
+ this.log.debug('received trackPublishedResponse', {
455
+ ...this.logContext,
456
+ cid: res.cid,
457
+ track: res.track?.sid,
458
+ });
433
459
  if (!this.pendingTrackResolvers[res.cid]) {
434
- log.error(`missing track resolver for ${res.cid}`);
460
+ this.log.error(`missing track resolver for ${res.cid}`, {
461
+ ...this.logContext,
462
+ cid: res.cid,
463
+ });
435
464
  return;
436
465
  }
437
466
  const { resolve } = this.pendingTrackResolvers[res.cid];
@@ -464,7 +493,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
464
493
  this.emit(EngineEvent.Disconnected, leave?.reason);
465
494
  this.close();
466
495
  }
467
- log.trace('leave request', { leave });
496
+ this.log.debug('client leave request', { ...this.logContext, reason: leave?.reason });
468
497
  };
469
498
  }
470
499
 
@@ -472,7 +501,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
472
501
  const rtcConfig = { ...this.rtcConfig };
473
502
 
474
503
  if (this.signalOpts?.e2eeEnabled) {
475
- log.debug('E2EE - setting up transports with insertable streams');
504
+ this.log.debug('E2EE - setting up transports with insertable streams', this.logContext);
476
505
  // this makes sure that no data is sent before the transforms are ready
477
506
  // @ts-ignore
478
507
  rtcConfig.encodedInsertableStreams = true;
@@ -562,7 +591,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
562
591
  } else {
563
592
  return;
564
593
  }
565
- log.debug(`on data channel ${channel.id}, ${channel.label}`);
594
+ this.log.debug(`on data channel ${channel.id}, ${channel.label}`, this.logContext);
566
595
  channel.onmessage = this.handleDataMessage;
567
596
  };
568
597
 
@@ -577,7 +606,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
577
606
  } else if (message.data instanceof Blob) {
578
607
  buffer = await message.data.arrayBuffer();
579
608
  } else {
580
- log.error('unsupported data type', message.data);
609
+ this.log.error('unsupported data type', { ...this.logContext, data: message.data });
581
610
  return;
582
611
  }
583
612
  const dp = DataPacket.fromBinary(new Uint8Array(buffer));
@@ -598,9 +627,12 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
598
627
 
599
628
  if (event instanceof ErrorEvent && event.error) {
600
629
  const { error } = event.error;
601
- log.error(`DataChannel error on ${channelKind}: ${event.message}`, error);
630
+ this.log.error(`DataChannel error on ${channelKind}: ${event.message}`, {
631
+ ...this.logContext,
632
+ error,
633
+ });
602
634
  } else {
603
- log.error(`Unknown DataChannel error on ${channelKind}`, event);
635
+ this.log.error(`Unknown DataChannel error on ${channelKind}`, { ...this.logContext, event });
604
636
  }
605
637
  };
606
638
 
@@ -622,7 +654,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
622
654
  }
623
655
  const cap = RTCRtpSender.getCapabilities(kind);
624
656
  if (!cap) return;
625
- log.debug('get capabilities', cap);
657
+ this.log.debug('get sender capabilities', { ...this.logContext, cap });
626
658
  const matched: RTCRtpCodecCapability[] = [];
627
659
  const partialMatched: RTCRtpCodecCapability[] = [];
628
660
  const unmatched: RTCRtpCodecCapability[] = [];
@@ -666,7 +698,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
666
698
  return sender;
667
699
  }
668
700
  if (supportsAddTrack()) {
669
- log.warn('using add-track fallback');
701
+ this.log.warn('using add-track fallback', this.logContext);
670
702
  const sender = await this.createRTCRtpSender(track.mediaStreamTrack);
671
703
  return sender;
672
704
  }
@@ -684,7 +716,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
684
716
  return this.createSimulcastTransceiverSender(track, simulcastTrack, opts, encodings);
685
717
  }
686
718
  if (supportsAddTrack()) {
687
- log.debug('using add-track fallback');
719
+ this.log.debug('using add-track fallback', this.logContext);
688
720
  return this.createRTCRtpSender(track.mediaStreamTrack);
689
721
  }
690
722
 
@@ -764,15 +796,16 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
764
796
  return;
765
797
  }
766
798
 
767
- log.warn(`${connection} disconnected`);
799
+ this.log.warn(`${connection} disconnected`, this.logContext);
768
800
  if (this.reconnectAttempts === 0) {
769
801
  // only reset start time on the first try
770
802
  this.reconnectStart = Date.now();
771
803
  }
772
804
 
773
805
  const disconnect = (duration: number) => {
774
- log.warn(
806
+ this.log.warn(
775
807
  `could not recover connection after ${this.reconnectAttempts} attempts, ${duration}ms. giving up`,
808
+ this.logContext,
776
809
  );
777
810
  this.emit(EngineEvent.Disconnected);
778
811
  this.close();
@@ -792,7 +825,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
792
825
  delay = 0;
793
826
  }
794
827
 
795
- log.debug(`reconnecting in ${delay}ms`);
828
+ this.log.debug(`reconnecting in ${delay}ms`, this.logContext);
796
829
 
797
830
  this.clearReconnectTimeout();
798
831
  if (this.token && this.regionUrlProvider) {
@@ -836,7 +869,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
836
869
  this.reconnectAttempts += 1;
837
870
  let recoverable = true;
838
871
  if (e instanceof UnexpectedConnectionState) {
839
- log.debug('received unrecoverable error', { error: e });
872
+ this.log.debug('received unrecoverable error', { ...this.logContext, error: e });
840
873
  // unrecoverable
841
874
  recoverable = false;
842
875
  } else if (!(e instanceof SignalReconnectError)) {
@@ -847,10 +880,11 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
847
880
  if (recoverable) {
848
881
  this.handleDisconnect('reconnect', ReconnectReason.RR_UNKNOWN);
849
882
  } else {
850
- log.info(
883
+ this.log.info(
851
884
  `could not recover connection after ${this.reconnectAttempts} attempts, ${
852
885
  Date.now() - this.reconnectStart
853
886
  }ms. giving up`,
887
+ this.logContext,
854
888
  );
855
889
  this.emit(EngineEvent.Disconnected);
856
890
  await this.close();
@@ -864,7 +898,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
864
898
  try {
865
899
  return this.reconnectPolicy.nextRetryDelayInMs(context);
866
900
  } catch (e) {
867
- log.warn('encountered error in reconnect policy', { error: e });
901
+ this.log.warn('encountered error in reconnect policy', { ...this.logContext, error: e });
868
902
  }
869
903
 
870
904
  // error in user code with provided reconnect policy, stop reconnecting
@@ -878,7 +912,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
878
912
  throw new UnexpectedConnectionState('could not reconnect, url or token not saved');
879
913
  }
880
914
 
881
- log.info(`reconnecting, attempt: ${this.reconnectAttempts}`);
915
+ this.log.info(`reconnecting, attempt: ${this.reconnectAttempts}`, this.logContext);
882
916
  this.emit(EngineEvent.Restarting);
883
917
 
884
918
  if (!this.client.isDisconnected) {
@@ -890,7 +924,10 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
890
924
  let joinResponse: JoinResponse;
891
925
  try {
892
926
  if (!this.signalOpts) {
893
- log.warn('attempted connection restart, without signal options present');
927
+ this.log.warn(
928
+ 'attempted connection restart, without signal options present',
929
+ this.logContext,
930
+ );
894
931
  throw new SignalReconnectError();
895
932
  }
896
933
  // in case a regionUrl is passed, the region URL takes precedence
@@ -937,7 +974,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
937
974
  throw new UnexpectedConnectionState('publisher and subscriber connections unset');
938
975
  }
939
976
 
940
- log.info(`resuming signal connection, attempt ${this.reconnectAttempts}`);
977
+ this.log.info(`resuming signal connection, attempt ${this.reconnectAttempts}`, this.logContext);
941
978
  this.emit(EngineEvent.Resuming);
942
979
 
943
980
  try {
@@ -951,7 +988,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
951
988
  let message = '';
952
989
  if (e instanceof Error) {
953
990
  message = e.message;
954
- log.error(e.message);
991
+ this.log.error(e.message, this.logContext);
955
992
  }
956
993
  if (e instanceof ConnectionError && e.reason === ConnectionErrorReason.NotAllowed) {
957
994
  throw new UnexpectedConnectionState('could not reconnect, token might be expired');
@@ -990,7 +1027,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
990
1027
  private async waitForPCReconnected() {
991
1028
  this.pcState = PCState.Reconnecting;
992
1029
 
993
- log.debug('waiting for peer connection to reconnect');
1030
+ this.log.debug('waiting for peer connection to reconnect', this.logContext);
994
1031
  try {
995
1032
  await sleep(minReconnectWait); // FIXME setTimeout again not ideal for a connection critical path
996
1033
  if (!this.pcManager) {
@@ -1136,7 +1173,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
1136
1173
 
1137
1174
  const handleClosed = () => {
1138
1175
  abortController.abort();
1139
- log.debug('engine disconnected while negotiation was ongoing');
1176
+ this.log.debug('engine disconnected while negotiation was ongoing', this.logContext);
1140
1177
  resolve();
1141
1178
  return;
1142
1179
  };
@@ -1196,7 +1233,7 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
1196
1233
  /** @internal */
1197
1234
  sendSyncState(remoteTracks: RemoteTrackPublication[], localTracks: LocalTrackPublication[]) {
1198
1235
  if (!this.pcManager) {
1199
- log.warn('sync state cannot be sent without peer connection setup');
1236
+ this.log.warn('sync state cannot be sent without peer connection setup', this.logContext);
1200
1237
  return;
1201
1238
  }
1202
1239
  const previousAnswer = this.pcManager.subscriber.getLocalDescription();