@webex/web-client-media-engine 3.11.0 → 3.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/index.js CHANGED
@@ -486,6 +486,7 @@ var WebrtcCoreErrorType;
486
486
  (function (WebrtcCoreErrorType) {
487
487
  WebrtcCoreErrorType["DEVICE_PERMISSION_DENIED"] = "DEVICE_PERMISSION_DENIED";
488
488
  WebrtcCoreErrorType["CREATE_STREAM_FAILED"] = "CREATE_STREAM_FAILED";
489
+ WebrtcCoreErrorType["ADD_EFFECT_FAILED"] = "ADD_EFFECT_FAILED";
489
490
  })(WebrtcCoreErrorType || (WebrtcCoreErrorType = {}));
490
491
  /**
491
492
  * Represents a WebRTC core error, which contains error type and error message.
@@ -1246,11 +1247,12 @@ class _Stream {
1246
1247
  _a$1$1 = StreamEventNames.MuteStateChange, _b$1 = StreamEventNames.Ended;
1247
1248
  const Stream = AddEvents(_Stream);
1248
1249
 
1249
- var _a$6, _b;
1250
+ var _a$6, _b, _c;
1250
1251
  var LocalStreamEventNames;
1251
1252
  (function (LocalStreamEventNames) {
1252
1253
  LocalStreamEventNames["ConstraintsChange"] = "constraints-change";
1253
1254
  LocalStreamEventNames["OutputTrackChange"] = "output-track-change";
1255
+ LocalStreamEventNames["EffectAdded"] = "effect-added";
1254
1256
  })(LocalStreamEventNames || (LocalStreamEventNames = {}));
1255
1257
  /**
1256
1258
  * A stream which originates on the local device.
@@ -1265,6 +1267,7 @@ class _LocalStream extends Stream {
1265
1267
  super(stream);
1266
1268
  this[_a$6] = new TypedEvent$1();
1267
1269
  this[_b] = new TypedEvent$1();
1270
+ this[_c] = new TypedEvent$1();
1268
1271
  this.effects = [];
1269
1272
  this.loadingEffects = new Map();
1270
1273
  this.inputStream = stream;
@@ -1356,55 +1359,110 @@ class _LocalStream extends Stream {
1356
1359
  /**
1357
1360
  * Adds an effect to a local stream.
1358
1361
  *
1359
- * @param name - The name of the effect.
1360
1362
  * @param effect - The effect to add.
1361
1363
  */
1362
- addEffect(name, effect) {
1364
+ addEffect(effect) {
1363
1365
  return __awaiter$2(this, void 0, void 0, function* () {
1364
- // Load the effect
1365
- this.loadingEffects.set(name, effect);
1366
- const outputTrack = yield effect.load(this.outputTrack);
1367
- // Check that the loaded effect is the latest one and dispose if not
1368
- if (effect !== this.loadingEffects.get(name)) {
1366
+ // Check if the effect has already been added.
1367
+ if (this.effects.some((e) => e.id === effect.id)) {
1368
+ return;
1369
+ }
1370
+ // Load the effect. Because loading is asynchronous, keep track of the loading effects.
1371
+ this.loadingEffects.set(effect.kind, effect);
1372
+ yield effect.load(this.outputTrack);
1373
+ // After loading, check whether or not we still want to use this effect. If another effect of
1374
+ // the same kind was added while this effect was loading, we only want to use the latest effect,
1375
+ // so dispose this one. If the effects list was cleared while this effect was loading, also
1376
+ // dispose it.
1377
+ if (effect !== this.loadingEffects.get(effect.kind)) {
1369
1378
  yield effect.dispose();
1370
- throw new Error(`Effect "${name}" not required after loading`);
1379
+ throw new WebrtcCoreError(WebrtcCoreErrorType.ADD_EFFECT_FAILED, `Another effect with kind ${effect.kind} was added while effect ${effect.id} was loading, or the effects list was cleared.`);
1371
1380
  }
1372
- // Use the effect
1373
- this.loadingEffects.delete(name);
1374
- this.effects.push({ name, effect });
1375
- this.changeOutputTrack(outputTrack);
1376
- // When the effect's track is updated, update the next effect or output stream.
1377
- // TODO: using EffectEvent.TrackUpdated will cause the entire web-media-effects lib to be built
1378
- // and makes the size of the webrtc-core build much larger, so we use type assertion here as a
1379
- // temporary workaround.
1380
- effect.on('track-updated', (track) => {
1381
- var _c;
1382
- const effectIndex = this.effects.findIndex((e) => e.name === name);
1381
+ this.loadingEffects.delete(effect.kind);
1382
+ /**
1383
+ * Handle when the effect's output track has been changed. This will update the input of the
1384
+ * next effect in the effects list of the output of the stream.
1385
+ *
1386
+ * @param track - The new output track of the effect.
1387
+ */
1388
+ const handleEffectTrackUpdated = (track) => {
1389
+ var _d;
1390
+ const effectIndex = this.effects.findIndex((e) => e.id === effect.id);
1383
1391
  if (effectIndex === this.effects.length - 1) {
1384
1392
  this.changeOutputTrack(track);
1385
1393
  }
1394
+ else if (effectIndex >= 0) {
1395
+ (_d = this.effects[effectIndex + 1]) === null || _d === void 0 ? void 0 : _d.replaceInputTrack(track);
1396
+ }
1386
1397
  else {
1387
- (_c = this.effects[effectIndex + 1]) === null || _c === void 0 ? void 0 : _c.effect.replaceInputTrack(track);
1398
+ logger$3.error(`Effect with ID ${effect.id} not found in effects list.`);
1388
1399
  }
1389
- });
1400
+ };
1401
+ /**
1402
+ * Handle when the effect has been disposed. This will remove all event listeners from the
1403
+ * effect.
1404
+ */
1405
+ const handleEffectDisposed = () => {
1406
+ effect.off('track-updated', handleEffectTrackUpdated);
1407
+ effect.off('disposed', handleEffectDisposed);
1408
+ };
1409
+ // TODO: using EffectEvent.TrackUpdated or EffectEvent.Disposed will cause the entire
1410
+ // web-media-effects lib to be rebuilt and inflates the size of the webrtc-core build, so
1411
+ // we use type assertion here as a temporary workaround.
1412
+ effect.on('track-updated', handleEffectTrackUpdated);
1413
+ effect.on('disposed', handleEffectDisposed);
1414
+ // Add the effect to the effects list. If an effect of the same kind has already been added,
1415
+ // dispose the existing effect and replace it with the new effect. If the existing effect was
1416
+ // enabled, also enable the new effect.
1417
+ const existingEffectIndex = this.effects.findIndex((e) => e.kind === effect.kind);
1418
+ if (existingEffectIndex >= 0) {
1419
+ const [existingEffect] = this.effects.splice(existingEffectIndex, 1, effect);
1420
+ if (existingEffect.isEnabled) {
1421
+ // If the existing effect is not the first effect in the effects list, then the input of the
1422
+ // new effect should be the output of the previous effect in the effects list. We know the
1423
+ // output track of the previous effect must exist because it must have been loaded (and all
1424
+ // loaded effects have an output track).
1425
+ const inputTrack = existingEffectIndex === 0
1426
+ ? this.inputTrack
1427
+ : this.effects[existingEffectIndex - 1].getOutputTrack();
1428
+ yield effect.replaceInputTrack(inputTrack);
1429
+ // Enabling the new effect will trigger the track-updated event, which will handle the new
1430
+ // effect's updated output track.
1431
+ yield effect.enable();
1432
+ }
1433
+ yield existingEffect.dispose();
1434
+ }
1435
+ else {
1436
+ this.effects.push(effect);
1437
+ }
1438
+ // Emit an event with the effect so others can listen to the effect events.
1439
+ this[LocalStreamEventNames.EffectAdded].emit(effect);
1390
1440
  });
1391
1441
  }
1392
1442
  /**
1393
- * Get an effect from the effects list.
1443
+ * Get an effect from the effects list by ID.
1444
+ *
1445
+ * @param id - The id of the effect you want to get.
1446
+ * @returns The effect or undefined.
1447
+ */
1448
+ getEffectById(id) {
1449
+ return this.effects.find((effect) => effect.id === id);
1450
+ }
1451
+ /**
1452
+ * Get an effect from the effects list by kind.
1394
1453
  *
1395
- * @param name - The name of the effect you want to get.
1454
+ * @param kind - The kind of the effect you want to get.
1396
1455
  * @returns The effect or undefined.
1397
1456
  */
1398
- getEffect(name) {
1399
- var _c;
1400
- return (_c = this.effects.find((e) => e.name === name)) === null || _c === void 0 ? void 0 : _c.effect;
1457
+ getEffectByKind(kind) {
1458
+ return this.effects.find((effect) => effect.kind === kind);
1401
1459
  }
1402
1460
  /**
1403
1461
  * Get all the effects from the effects list.
1404
1462
  *
1405
- * @returns A list of effect items, each containing the name and the effect itself.
1463
+ * @returns A list of effects.
1406
1464
  */
1407
- getAllEffects() {
1465
+ getEffects() {
1408
1466
  return this.effects;
1409
1467
  }
1410
1468
  /**
@@ -1416,13 +1474,13 @@ class _LocalStream extends Stream {
1416
1474
  // Dispose of any effects currently in use
1417
1475
  if (this.effects.length > 0) {
1418
1476
  this.changeOutputTrack(this.inputTrack);
1419
- yield Promise.all(this.effects.map((item) => item.effect.dispose()));
1477
+ yield Promise.all(this.effects.map((effect) => effect.dispose()));
1420
1478
  this.effects = [];
1421
1479
  }
1422
1480
  });
1423
1481
  }
1424
1482
  }
1425
- _a$6 = LocalStreamEventNames.ConstraintsChange, _b = LocalStreamEventNames.OutputTrackChange;
1483
+ _a$6 = LocalStreamEventNames.ConstraintsChange, _b = LocalStreamEventNames.OutputTrackChange, _c = LocalStreamEventNames.EffectAdded;
1426
1484
  const LocalStream = AddEvents(_LocalStream);
1427
1485
 
1428
1486
  /**