@4players/odin-plugin-web 1.0.0-alpha.13 → 1.0.0-alpha.14

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.
@@ -87,7 +87,8 @@ export declare class AudioCapture implements Backend.AudioCapture {
87
87
  private _Vad;
88
88
  private _Apm;
89
89
  private _Volume;
90
- private _deviceParameters?;
90
+ private _DeviceParameters?;
91
+ private _DeviceCb;
91
92
  get vad(): Backend.VadConfig;
92
93
  get apm(): Backend.ApmConfig;
93
94
  get volume(): Backend.Volume;
@@ -339,7 +339,7 @@ export class AudioCapture {
339
339
  }
340
340
  get deviceId() {
341
341
  var _a, _b;
342
- return (_b = (_a = this._deviceParameters) === null || _a === void 0 ? void 0 : _a.device) === null || _b === void 0 ? void 0 : _b.device.deviceId;
342
+ return (_b = (_a = this._DeviceParameters) === null || _a === void 0 ? void 0 : _a.device) === null || _b === void 0 ? void 0 : _b.device.deviceId;
343
343
  }
344
344
  constructor(controller, parameters) {
345
345
  this.controller = controller;
@@ -347,6 +347,19 @@ export class AudioCapture {
347
347
  this._Vad = defaultVad;
348
348
  this._Apm = defaultApm;
349
349
  this._Volume = 1;
350
+ this._DeviceCb = () => __awaiter(this, void 0, void 0, function* () {
351
+ var _a, _b;
352
+ const devices = yield this.controller.enumerateDevices();
353
+ const isListed = devices.some((device) => {
354
+ var _a, _b;
355
+ return (device.device.deviceId ===
356
+ ((_b = (_a = this._DeviceParameters) === null || _a === void 0 ? void 0 : _a.device) === null || _b === void 0 ? void 0 : _b.device.deviceId));
357
+ });
358
+ if (!isListed) {
359
+ (_b = (_a = this._DeviceParameters) === null || _a === void 0 ? void 0 : _a.onStatusChanged) === null || _b === void 0 ? void 0 : _b.call(_a, 'stopped');
360
+ navigator.mediaDevices.removeEventListener('devicechange', this._DeviceCb);
361
+ }
362
+ });
350
363
  if (parameters.apm) {
351
364
  this.setApm(parameters.apm);
352
365
  }
@@ -397,25 +410,28 @@ export class AudioCapture {
397
410
  setApm(config) {
398
411
  return __awaiter(this, void 0, void 0, function* () {
399
412
  this._Apm = config;
400
- if (this._deviceParameters) {
401
- yield this.setDevice(this._deviceParameters);
413
+ if (this._DeviceParameters) {
414
+ yield this.setDevice(this._DeviceParameters);
402
415
  }
403
416
  });
404
417
  }
405
418
  setDevice(parameters) {
406
419
  var _a;
407
420
  return __awaiter(this, void 0, void 0, function* () {
408
- const rollbackParams = this._deviceParameters;
409
- this._deviceParameters = parameters;
421
+ const rollbackParams = this._DeviceParameters;
422
+ this._DeviceParameters = parameters;
410
423
  if (this._Volume === 'muted') {
411
424
  return;
412
425
  }
413
426
  const device = yield openCaptureDevice(this.controller, parameters, this._Apm);
414
427
  if (device.type !== 'Success') {
415
428
  console.error(`unable to open capture device; ${device.reason}`);
416
- this._deviceParameters = rollbackParams;
429
+ this._DeviceParameters = rollbackParams;
417
430
  return;
418
431
  }
432
+ if (this._DeviceParameters.onStatusChanged) {
433
+ navigator.mediaDevices.addEventListener('devicechange', this._DeviceCb);
434
+ }
419
435
  if (this._Capture !== undefined) {
420
436
  stopMediaStream(this._Capture.source.mediaStream);
421
437
  this._Capture.source.disconnect(this._Capture.worker.worklet);
@@ -445,8 +461,8 @@ export class AudioCapture {
445
461
  this.close();
446
462
  }
447
463
  else {
448
- if (!this._Capture && this._deviceParameters) {
449
- this.setDevice(this._deviceParameters);
464
+ if (!this._Capture && this._DeviceParameters) {
465
+ this.setDevice(this._DeviceParameters);
450
466
  }
451
467
  (_a = this._Capture) === null || _a === void 0 ? void 0 : _a.worker.setVolume(value);
452
468
  }
@@ -458,6 +474,7 @@ export class AudioCapture {
458
474
  this._Capture.source.disconnect(this._Capture.worker.worklet);
459
475
  this.controller.audioContexts.release(this._Capture.source.context);
460
476
  }
477
+ navigator.mediaDevices.removeEventListener('devicechange', this._DeviceCb);
461
478
  this._Capture = undefined;
462
479
  }
463
480
  _UpdateMedias() {
@@ -46,29 +46,31 @@ export class MainConnection {
46
46
  return this.connection.closed;
47
47
  }
48
48
  webRtcReady() {
49
- const isReady = () => {
50
- switch (this.webRtc.connectionState) {
51
- case 'connected':
52
- return true;
53
- case 'failed':
54
- case 'closed':
55
- return false;
56
- default:
57
- return undefined;
58
- }
59
- };
60
- const result = isReady();
61
- if (result !== undefined)
62
- return Promise.resolve(result);
63
- return new Promise((accept) => {
64
- const onConnectionStateChanged = () => {
65
- const result = isReady();
66
- if (result !== undefined) {
67
- this.webRtc.removeEventListener('connectionstatechange', onConnectionStateChanged);
68
- accept(result);
49
+ return __awaiter(this, void 0, void 0, function* () {
50
+ const isReady = () => {
51
+ switch (this.webRtc.connectionState) {
52
+ case 'connected':
53
+ return true;
54
+ case 'failed':
55
+ case 'closed':
56
+ return false;
57
+ default:
58
+ return undefined;
69
59
  }
70
60
  };
71
- this.webRtc.addEventListener('connectionstatechange', onConnectionStateChanged);
61
+ const result = isReady();
62
+ if (result !== undefined)
63
+ return result;
64
+ return new Promise((accept) => {
65
+ const onConnectionStateChanged = () => {
66
+ const result = isReady();
67
+ if (result !== undefined) {
68
+ this.webRtc.removeEventListener('connectionstatechange', onConnectionStateChanged);
69
+ accept(result);
70
+ }
71
+ };
72
+ this.webRtc.addEventListener('connectionstatechange', onConnectionStateChanged);
73
+ });
72
74
  });
73
75
  }
74
76
  constructor(id, url, onDatagram, connection, webRtc) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@4players/odin-plugin-web",
3
- "version": "1.0.0-alpha.13",
3
+ "version": "1.0.0-alpha.14",
4
4
  "description": "A backend plugin providing the custom WebAssembly audio processing module for ODIN web clients",
5
5
  "homepage": "https://www.4players.io",
6
6
  "repository": {