@observertc/observer-js 0.9.20-beta → 0.9.22-beta

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.
Files changed (212) hide show
  1. package/.eslintrc.json +143 -0
  2. package/lib/Evaluator.d.ts.map +1 -1
  3. package/lib/Evaluator.js +54 -47
  4. package/lib/Observer.d.ts +1408 -38
  5. package/lib/Observer.d.ts.map +1 -1
  6. package/lib/Observer.js +484 -180
  7. package/lib/common/Semaphore.d.ts.map +1 -1
  8. package/lib/common/callMetaReports.d.ts.map +1 -1
  9. package/lib/common/callMetaReports.js +1 -0
  10. package/lib/common/logger.d.ts.map +1 -1
  11. package/lib/common/logger.js +15 -15
  12. package/lib/common/utils.d.ts +1 -0
  13. package/lib/common/utils.d.ts.map +1 -1
  14. package/lib/common/utils.js +12 -1
  15. package/lib/entries/CallEntry.d.ts +611 -0
  16. package/lib/entries/CallEntry.d.ts.map +1 -0
  17. package/lib/entries/CallEntry.js +75 -0
  18. package/lib/entries/ClientEntry.d.ts +474 -0
  19. package/lib/entries/ClientEntry.d.ts.map +1 -0
  20. package/lib/entries/ClientEntry.js +101 -0
  21. package/lib/entries/InboundTrackEntry.d.ts +670 -0
  22. package/lib/entries/InboundTrackEntry.d.ts.map +1 -0
  23. package/lib/entries/InboundTrackEntry.js +76 -0
  24. package/lib/entries/OutboundTrackEntry.d.ts +670 -0
  25. package/lib/entries/OutboundTrackEntry.d.ts.map +1 -0
  26. package/lib/entries/OutboundTrackEntry.js +73 -0
  27. package/lib/entries/PeerConnectionEntry.d.ts +469 -0
  28. package/lib/entries/PeerConnectionEntry.d.ts.map +1 -0
  29. package/lib/entries/PeerConnectionEntry.js +144 -0
  30. package/lib/entries/SfuEntry.d.ts +150 -0
  31. package/lib/entries/SfuEntry.d.ts.map +1 -0
  32. package/lib/entries/SfuEntry.js +77 -0
  33. package/lib/entries/SfuInboundRtpPadEntry.d.ts +14 -0
  34. package/lib/entries/SfuInboundRtpPadEntry.d.ts.map +1 -0
  35. package/lib/entries/SfuInboundRtpPadEntry.js +49 -0
  36. package/lib/entries/SfuOutboundRtpPadEntry.d.ts +14 -0
  37. package/lib/entries/SfuOutboundRtpPadEntry.d.ts.map +1 -0
  38. package/lib/entries/SfuOutboundRtpPadEntry.js +47 -0
  39. package/lib/entries/SfuSctpChannelEntry.d.ts +12 -0
  40. package/lib/entries/SfuSctpChannelEntry.d.ts.map +1 -0
  41. package/lib/entries/SfuSctpChannelEntry.js +43 -0
  42. package/lib/entries/SfuTransportEntry.d.ts +82 -0
  43. package/lib/entries/SfuTransportEntry.d.ts.map +1 -0
  44. package/lib/entries/SfuTransportEntry.js +122 -0
  45. package/lib/index.d.ts +10 -0
  46. package/lib/index.d.ts.map +1 -1
  47. package/lib/middlewares/CallOperationsContext.d.ts.map +1 -1
  48. package/lib/middlewares/CallOperationsContext.js +2 -6
  49. package/lib/middlewares/CallProcessor.d.ts.map +1 -1
  50. package/lib/middlewares/CallProcessor.js +15 -6
  51. package/lib/middlewares/CommitTransactionMiddleware.d.ts.map +1 -1
  52. package/lib/middlewares/CommitTransactionMiddleware.js +22 -26
  53. package/lib/middlewares/DeleteOutdatedCallEntries.d.ts.map +1 -1
  54. package/lib/middlewares/DeleteOutdatedCallEntries.js +29 -20
  55. package/lib/middlewares/DeleteOutdatedSfuEntries.d.ts.map +1 -1
  56. package/lib/middlewares/DeleteOutdatedSfuEntries.js +27 -18
  57. package/lib/middlewares/Middleware.d.ts.map +1 -1
  58. package/lib/middlewares/Middleware.js +13 -4
  59. package/lib/middlewares/Processor.d.ts.map +1 -1
  60. package/lib/middlewares/Processor.js +18 -9
  61. package/lib/middlewares/TransactionContext.d.ts.map +1 -1
  62. package/lib/middlewares/TransactionContext.js +84 -73
  63. package/lib/middlewares/VisitObservedCallsMiddleware.d.ts.map +1 -1
  64. package/lib/middlewares/VisitObservedCallsMiddleware.js +16 -7
  65. package/lib/middlewares/VisitObservedSfusMiddleware.d.ts.map +1 -1
  66. package/lib/middlewares/VisitObservedSfusMiddleware.js +15 -6
  67. package/lib/middlewares/visitors/visitClient.d.ts.map +1 -1
  68. package/lib/middlewares/visitors/visitClient.js +6 -18
  69. package/lib/middlewares/visitors/visitInboundAudioTrack.d.ts.map +1 -1
  70. package/lib/middlewares/visitors/visitInboundAudioTrack.js +17 -20
  71. package/lib/middlewares/visitors/visitInboundVideoTrack.d.ts.map +1 -1
  72. package/lib/middlewares/visitors/visitInboundVideoTrack.js +16 -21
  73. package/lib/middlewares/visitors/visitOutboundAudioTrack.d.ts.map +1 -1
  74. package/lib/middlewares/visitors/visitOutboundAudioTrack.js +16 -16
  75. package/lib/middlewares/visitors/visitOutboundVideoTarck.d.ts.map +1 -1
  76. package/lib/middlewares/visitors/visitOutboundVideoTarck.js +16 -17
  77. package/lib/middlewares/visitors/visitPeerConnection.d.ts.map +1 -1
  78. package/lib/middlewares/visitors/visitPeerConnection.js +24 -40
  79. package/lib/middlewares/visitors/visitSfu.d.ts.map +1 -1
  80. package/lib/middlewares/visitors/visitSfu.js +6 -7
  81. package/lib/middlewares/visitors/visitSfuInboundRtpPad.d.ts.map +1 -1
  82. package/lib/middlewares/visitors/visitSfuInboundRtpPad.js +3 -7
  83. package/lib/middlewares/visitors/visitSfuOutboundRtpPad.d.ts.map +1 -1
  84. package/lib/middlewares/visitors/visitSfuOutboundRtpPad.js +3 -8
  85. package/lib/middlewares/visitors/visitSfuSctpChannel.d.ts.map +1 -1
  86. package/lib/middlewares/visitors/visitSfuSctpChannel.js +3 -6
  87. package/lib/middlewares/visitors/visitSfuTransport.d.ts.map +1 -1
  88. package/lib/middlewares/visitors/visitSfuTransport.js +3 -6
  89. package/lib/processes/closePeerConnectionProcess.d.ts.map +1 -1
  90. package/lib/processes/closePeerConnectionProcess.js +14 -5
  91. package/lib/processes/detachClientProcess.d.ts.map +1 -1
  92. package/lib/processes/detachClientProcess.js +20 -17
  93. package/lib/processes/findRemoteMatches.d.ts +2 -2
  94. package/lib/processes/findRemoteMatches.d.ts.map +1 -1
  95. package/lib/processes/findRemoteMatches.js +70 -56
  96. package/lib/processes/inboundTrackRemoved.d.ts.map +1 -1
  97. package/lib/processes/inboundTrackRemoved.js +12 -3
  98. package/lib/processes/joinClientProcess.d.ts.map +1 -1
  99. package/lib/processes/joinClientProcess.js +15 -6
  100. package/lib/processes/outboundTrackRemoved.d.ts.map +1 -1
  101. package/lib/processes/outboundTrackRemoved.js +12 -3
  102. package/lib/samples/ObservedCall.d.ts.map +1 -1
  103. package/lib/samples/ObservedCall.js +1 -5
  104. package/lib/samples/ObservedCalls.d.ts.map +1 -1
  105. package/lib/samples/ObservedClient.d.ts.map +1 -1
  106. package/lib/samples/ObservedClient.js +1 -9
  107. package/lib/samples/ObservedInboundAudioTrack.d.ts.map +1 -1
  108. package/lib/samples/ObservedInboundAudioTrack.js +1 -5
  109. package/lib/samples/ObservedInboundVideoTrack.d.ts.map +1 -1
  110. package/lib/samples/ObservedInboundVideoTrack.js +1 -5
  111. package/lib/samples/ObservedOutboundAudioTrack.d.ts.map +1 -1
  112. package/lib/samples/ObservedOutboundAudioTrack.js +1 -5
  113. package/lib/samples/ObservedOutboundVideoTrack.d.ts.map +1 -1
  114. package/lib/samples/ObservedOutboundVideoTrack.js +1 -5
  115. package/lib/samples/ObservedPeerConnection.d.ts.map +1 -1
  116. package/lib/samples/ObservedPeerConnection.js +1 -12
  117. package/lib/samples/ObservedSfu.d.ts.map +1 -1
  118. package/lib/samples/ObservedSfu.js +1 -8
  119. package/lib/samples/ObservedSfuInboundRtpPad.d.ts.map +1 -1
  120. package/lib/samples/ObservedSfuInboundRtpPad.js +1 -5
  121. package/lib/samples/ObservedSfuOutboundRtpPad.d.ts.map +1 -1
  122. package/lib/samples/ObservedSfuOutboundRtpPad.js +1 -5
  123. package/lib/samples/ObservedSfuSctpChannel.d.ts.map +1 -1
  124. package/lib/samples/ObservedSfuSctpChannel.js +1 -5
  125. package/lib/samples/ObservedSfuTransport.d.ts.map +1 -1
  126. package/lib/samples/ObservedSfuTransport.js +1 -8
  127. package/lib/samples/ObservedSfus.d.ts.map +1 -1
  128. package/lib/sinks/ObserverSink.d.ts.map +1 -1
  129. package/lib/sinks/ObserverSink.js +42 -31
  130. package/lib/sinks/callEventReports.js +1 -0
  131. package/lib/sources/ObservedCallSource.d.ts +5 -3
  132. package/lib/sources/ObservedCallSource.d.ts.map +1 -1
  133. package/lib/sources/ObservedClientSource.d.ts +3 -2
  134. package/lib/sources/ObservedClientSource.d.ts.map +1 -1
  135. package/lib/sources/ObservedSfuSource.d.ts +3 -2
  136. package/lib/sources/ObservedSfuSource.d.ts.map +1 -1
  137. package/lib/sources/Sources.d.ts +8 -4
  138. package/lib/sources/Sources.d.ts.map +1 -1
  139. package/lib/sources/Sources.js +71 -46
  140. package/lib/storages/ObserverStorage.d.ts.map +1 -1
  141. package/lib/storages/SimpleStorage.d.ts.map +1 -1
  142. package/lib/storages/SimpleStorage.js +40 -23
  143. package/package.json +2 -1
  144. package/src/Evaluator.ts +17 -9
  145. package/src/Observer.ts +239 -171
  146. package/src/common/Semaphore.ts +2 -0
  147. package/src/common/callMetaReports.ts +30 -28
  148. package/src/common/logger.ts +140 -132
  149. package/src/common/types.ts +1 -1
  150. package/src/common/utils.ts +12 -1
  151. package/src/entries/CallEntry.ts +62 -0
  152. package/src/entries/ClientEntry.ts +90 -0
  153. package/src/entries/InboundTrackEntry.ts +74 -0
  154. package/src/entries/OutboundTrackEntry.ts +72 -0
  155. package/src/entries/PeerConnectionEntry.ts +133 -0
  156. package/src/entries/SfuEntry.ts +62 -0
  157. package/src/entries/SfuInboundRtpPadEntry.ts +46 -0
  158. package/src/entries/SfuOutboundRtpPadEntry.ts +44 -0
  159. package/src/entries/SfuSctpChannelEntry.ts +39 -0
  160. package/src/entries/SfuTransportEntry.ts +109 -0
  161. package/src/index.ts +11 -0
  162. package/src/middlewares/CallOperationsContext.ts +9 -5
  163. package/src/middlewares/CallProcessor.ts +4 -2
  164. package/src/middlewares/CommitTransactionMiddleware.ts +8 -5
  165. package/src/middlewares/DeleteOutdatedCallEntries.ts +24 -16
  166. package/src/middlewares/DeleteOutdatedSfuEntries.ts +24 -15
  167. package/src/middlewares/Middleware.ts +1 -0
  168. package/src/middlewares/Processor.ts +10 -5
  169. package/src/middlewares/TransactionContext.ts +1 -2
  170. package/src/middlewares/VisitObservedCallsMiddleware.ts +5 -2
  171. package/src/middlewares/VisitObservedSfusMiddleware.ts +8 -7
  172. package/src/middlewares/visitors/visitClient.ts +15 -1
  173. package/src/middlewares/visitors/visitInboundAudioTrack.ts +4 -1
  174. package/src/middlewares/visitors/visitInboundVideoTrack.ts +5 -1
  175. package/src/middlewares/visitors/visitOutboundAudioTrack.ts +5 -1
  176. package/src/middlewares/visitors/visitOutboundVideoTarck.ts +5 -1
  177. package/src/middlewares/visitors/visitPeerConnection.ts +15 -2
  178. package/src/middlewares/visitors/visitSfu.ts +7 -1
  179. package/src/middlewares/visitors/visitSfuInboundRtpPad.ts +4 -2
  180. package/src/middlewares/visitors/visitSfuOutboundRtpPad.ts +3 -1
  181. package/src/middlewares/visitors/visitSfuSctpChannel.ts +23 -21
  182. package/src/middlewares/visitors/visitSfuTransport.ts +3 -1
  183. package/src/processes/closePeerConnectionProcess.ts +3 -0
  184. package/src/processes/detachClientProcess.ts +13 -4
  185. package/src/processes/findRemoteMatches.ts +32 -21
  186. package/src/processes/inboundTrackRemoved.ts +2 -0
  187. package/src/processes/joinClientProcess.ts +8 -2
  188. package/src/processes/outboundTrackRemoved.ts +2 -0
  189. package/src/samples/ObservedCall.ts +6 -0
  190. package/src/samples/ObservedCalls.ts +13 -7
  191. package/src/samples/ObservedClient.ts +24 -8
  192. package/src/samples/ObservedInboundAudioTrack.ts +3 -2
  193. package/src/samples/ObservedInboundVideoTrack.ts +3 -2
  194. package/src/samples/ObservedOutboundAudioTrack.ts +3 -2
  195. package/src/samples/ObservedOutboundVideoTrack.ts +3 -2
  196. package/src/samples/ObservedPeerConnection.ts +27 -11
  197. package/src/samples/ObservedSfu.ts +17 -10
  198. package/src/samples/ObservedSfuInboundRtpPad.ts +5 -4
  199. package/src/samples/ObservedSfuOutboundRtpPad.ts +5 -4
  200. package/src/samples/ObservedSfuSctpChannel.ts +13 -13
  201. package/src/samples/ObservedSfuTransport.ts +19 -7
  202. package/src/samples/ObservedSfus.ts +10 -5
  203. package/src/sinks/ObserverSink.ts +51 -44
  204. package/src/sinks/callEventReports.ts +1 -1
  205. package/src/sources/ObservedCallSource.ts +6 -5
  206. package/src/sources/ObservedClientSource.ts +3 -2
  207. package/src/sources/ObservedSfuSource.ts +4 -8
  208. package/src/sources/Sources.ts +96 -22
  209. package/src/storages/ObserverStorage.ts +1 -0
  210. package/src/storages/SimpleStorage.ts +44 -21
  211. package/tsconfig.json +7 -5
  212. package/.eslintrc.js +0 -27
package/src/Observer.ts CHANGED
@@ -1,23 +1,36 @@
1
1
  import { ObserverReportsEmitter, ObserverSinkProcess, SinkConfig, SinkImpl } from './sinks/ObserverSink';
2
2
  import { Sources, SourcesConfig } from './sources/Sources';
3
3
  import { createSimpleStorageProvider, StorageProvider } from './storages/StorageProvider';
4
- import * as Models from './models/Models';
5
4
  import { Evaluator, EvaluatorConfig, EvaluatorProcess } from './Evaluator';
6
5
  import { createSimpleSemaphoreProvider, SemaphoreProvider } from './common/Semaphore';
7
6
  import { ObservedCallSourceConfig, ObservedCallSource } from './sources/ObservedCallSource';
8
- import { ObservedClientSource, ObservedClientSourceConfig } from './sources/ObservedClientSource';
9
- import { PartialBy } from './common/utils';
7
+ import { PartialBy, asyncIteratorConverter } from './common/utils';
10
8
  import { createLogger, LogLevel } from './common/logger';
11
9
  import { ObservedSfuSource, ObservedSfuSourceConfig } from './sources/ObservedSfuSource';
12
- import EventEmitter from 'events';
10
+ import { EventEmitter } from 'events';
11
+ import { CallEntry, createCallEntry } from './entries/CallEntry';
12
+ import { ClientEntry, createClientEntry } from './entries/ClientEntry';
13
+ import { PeerConnectionEntry, createPeerConnectionEntry } from './entries/PeerConnectionEntry';
14
+ import { InboundTrackEntry, createInboundTrackEntry } from './entries/InboundTrackEntry';
15
+ import { OutboundTrackEntry, createOutboundTrackEntry } from './entries/OutboundTrackEntry';
16
+ import { SfuEntry, createSfuEntry } from './entries/SfuEntry';
17
+ import { SfuTransportEntry, createSfuTransportEntry } from './entries/SfuTransportEntry';
18
+ import { SfuInboundRtpPadEntry, createSfuInboundRtpPadEntry } from './entries/SfuInboundRtpPadEntry';
19
+ import { SfuOutboundRtpPadEntry, createSfuOutboundRtpPadEntry } from './entries/SfuOutboundRtpPadEntry';
20
+ import { SfuSctpChannelEntry, createSfuSctpChannelEntry } from './entries/SfuSctpChannelEntry';
13
21
 
14
22
  const logger = createLogger('Observer');
15
23
 
16
24
  export type ObserverEvents = {
25
+ 'processing-started': undefined,
26
+ 'client-added': string,
27
+ 'client-removed': string,
28
+ 'processing-ended': undefined,
17
29
  'close': undefined,
18
30
  }
19
31
 
20
32
  export type ObserverConfig = {
33
+
21
34
  /**
22
35
  *
23
36
  * Sets the default serviceId for samples.
@@ -67,8 +80,8 @@ export class Observer {
67
80
  );
68
81
 
69
82
  const semaphores = providedConfig.semaphores ?? createSimpleSemaphoreProvider();
70
-
71
83
  const storages = providedConfig.storages ?? createSimpleStorageProvider();
84
+
72
85
  return new Observer(config, storages, semaphores);
73
86
  }
74
87
 
@@ -84,110 +97,93 @@ export class Observer {
84
97
  ) {
85
98
  this._sources = new Sources(config.sources);
86
99
  this._sink = new SinkImpl(config.sink);
87
- this._evaluator = new Evaluator(this.config.evaluator, this._semaphores.callSemaphore, this._storages, this._sink);
100
+ this._evaluator = new Evaluator(
101
+ this.config.evaluator,
102
+ this._semaphores.callSemaphore,
103
+ this._storages,
104
+ this._sink
105
+ );
88
106
 
89
107
  this._sources.on('observed-samples', (event) => {
108
+ this._emitter.emit('processing-started');
90
109
  this._evaluator.addObservedSamples(event);
91
110
  });
92
111
  this._sources.on('added-client-source', (event) => {
93
112
  this._evaluator.addCreatedClientSource(event);
113
+ this._emitter.emit('client-added', event.clientId);
94
114
  });
95
115
  this._sources.on('removed-client-source', (event) => {
96
116
  this._evaluator.addClosedClientSource(event);
117
+ this._emitter.emit('client-added', event.clientId);
97
118
  });
98
119
  this._evaluator.on('ready', () => {
99
120
  this._sink.emit();
121
+ this._emitter.emit('processing-ended');
100
122
  });
101
- logger.debug(`Observer is created with config`, this.config);
123
+ logger.debug('Observer is created with config', this.config);
124
+ }
125
+
126
+ public get sources(): Sources {
127
+ return this._sources;
102
128
  }
103
129
 
104
130
  public on<K extends keyof ObserverEvents>(event: K, listener: (arg: ObserverEvents[K]) => void): this {
105
131
  this._emitter.on(event, listener);
132
+
106
133
  return this;
107
134
  }
108
135
 
109
136
  public once<K extends keyof ObserverEvents>(event: K, listener: (arg: ObserverEvents[K]) => void): this {
110
137
  this._emitter.once(event, listener);
138
+
111
139
  return this;
112
140
  }
113
141
 
114
142
  public off<K extends keyof ObserverEvents>(event: K, listener: (arg: ObserverEvents[K]) => void): this {
115
143
  this._emitter.off(event, listener);
144
+
116
145
  return this;
117
146
  }
118
147
 
119
- public createCallSource(
120
- config: PartialBy<ObservedCallSourceConfig, 'serviceId' | 'mediaUnitId'>
121
- ): ObservedCallSource {
148
+ public createCallSource<T extends Record<string, unknown> = Record<string, unknown>>(
149
+ config: PartialBy<ObservedCallSourceConfig<T>, 'serviceId' | 'mediaUnitId' | 'appData'>
150
+ ): ObservedCallSource<T> {
122
151
  if (this._closed) {
123
- throw new Error(`Attempted to create a call source on a closed observer`);
152
+ throw new Error('Attempted to create a call source on a closed observer');
124
153
  }
125
- let closed = false;
126
- const serviceId = config.serviceId ?? this.config.defaultServiceId;
127
- const mediaUnitId = config.mediaUnitId ?? this.config.defaultMediaUnitId;
128
- const clientSources = new Map<string, ObservedClientSource>();
129
- const callSource: ObservedCallSource = {
130
- ...config,
131
- serviceId,
132
- mediaUnitId,
133
- createClientSource: (context) => {
134
- const clientSource = this._sources.createClientSource({
135
- ...config,
136
- ...context,
137
- serviceId,
138
- mediaUnitId,
139
- });
140
- const closeClientSource = clientSource.close;
141
- clientSource.close = () => {
142
- closeClientSource();
143
- clientSources.delete(context.clientId);
144
- };
145
- clientSources.set(context.clientId, clientSource);
146
- return clientSource;
147
- },
148
- close: () => {
149
- if (closed) {
150
- return;
151
- }
152
- for (const clientSource of clientSources.values()) {
153
- clientSource.close();
154
- }
155
- clientSources.clear();
156
- closed = true;
157
- },
158
- closed,
159
- };
160
- return callSource;
161
- }
162
154
 
163
- public createClientSource(
164
- config: PartialBy<ObservedClientSourceConfig, 'serviceId' | 'mediaUnitId' | 'joined'>
165
- ): ObservedClientSource {
166
- if (this._closed) {
167
- throw new Error(`Attempted to create a client source on a closed observer`);
168
- }
169
- const serviceId = config.serviceId ?? this.config.defaultServiceId;
170
- const mediaUnitId = config.mediaUnitId ?? this.config.defaultMediaUnitId;
171
- const joined = config.joined ?? Date.now();
172
- return this._sources.createClientSource({
173
- ...config,
174
- serviceId,
155
+ const {
156
+ serviceId = this.config.defaultServiceId,
157
+ mediaUnitId = this.config.defaultMediaUnitId,
158
+ appData = {} as T,
159
+ ...callConfig
160
+ } = config;
161
+
162
+ return this._sources.createCallSource({
163
+ ...callConfig,
164
+ appData,
175
165
  mediaUnitId,
176
- joined,
166
+ serviceId,
177
167
  });
178
168
  }
179
169
 
180
- public createSfuSource(
181
- config: PartialBy<ObservedSfuSourceConfig, 'serviceId' | 'mediaUnitId' | 'joined'>
170
+ public createSfuSource<T extends Record<string, unknown> = Record<string, unknown>>(
171
+ config: PartialBy<ObservedSfuSourceConfig<T>, 'serviceId' | 'mediaUnitId' | 'joined' | 'appData'>
182
172
  ): ObservedSfuSource {
183
173
  if (this._closed) {
184
- throw new Error(`Attempted to create a sfu source on a closed observer`);
174
+ throw new Error('Attempted to create a sfu source on a closed observer');
185
175
  }
186
- const serviceId = config.serviceId ?? this.config.defaultServiceId;
187
- const mediaUnitId = config.mediaUnitId ?? this.config.defaultMediaUnitId;
188
- const joined = config.joined ?? Date.now();
189
- return this._sources.createSfuSource({
190
- ...config,
176
+ const {
177
+ appData = {} as T,
178
+ serviceId = this.config.defaultServiceId,
179
+ mediaUnitId = this.config.defaultMediaUnitId,
180
+ joined = Date.now(),
181
+ ...sfuConfig
182
+ } = config;
183
+
184
+ return this._sources.createSfuSource<T>({
185
+ ...sfuConfig,
186
+ appData,
191
187
  serviceId,
192
188
  mediaUnitId,
193
189
  joined,
@@ -196,7 +192,7 @@ export class Observer {
196
192
 
197
193
  public async evaluate() {
198
194
  return new Promise<void>((resolve, reject) => {
199
- this._evaluator.once('ready', err => {
195
+ this._evaluator.once('ready', (err) => {
200
196
  if (err) reject(err);
201
197
  else resolve();
202
198
  });
@@ -210,7 +206,7 @@ export class Observer {
210
206
 
211
207
  public addEvaluators(...processes: EvaluatorProcess[]) {
212
208
  if (this._closed) {
213
- throw new Error(`Attempted to add an evaluator to a closed observer`);
209
+ throw new Error('Attempted to add an evaluator to a closed observer');
214
210
  }
215
211
  for (const process of processes) {
216
212
  this._evaluator.addProcess(process);
@@ -219,7 +215,7 @@ export class Observer {
219
215
 
220
216
  public removeEvaluators(...processes: EvaluatorProcess[]) {
221
217
  if (this._closed) {
222
- throw new Error(`Attempted to remove an evaluator from a closed observer`);
218
+ throw new Error('Attempted to remove an evaluator from a closed observer');
223
219
  }
224
220
  for (const process of processes) {
225
221
  this._evaluator.removeProcess(process);
@@ -228,7 +224,7 @@ export class Observer {
228
224
 
229
225
  public addSinks(...processes: ObserverSinkProcess[]) {
230
226
  if (this._closed) {
231
- throw new Error(`Attempted to add an evaluator to a closed observer`);
227
+ throw new Error('Attempted to add an evaluator to a closed observer');
232
228
  }
233
229
  for (const process of processes) {
234
230
  this._sink.addProcess(process);
@@ -237,194 +233,267 @@ export class Observer {
237
233
 
238
234
  public removeSinks(...processes: ObserverSinkProcess[]) {
239
235
  if (this._closed) {
240
- throw new Error(`Attempted to remove an evaluator from a closed observer`);
236
+ throw new Error('Attempted to remove an evaluator from a closed observer');
241
237
  }
242
238
  for (const process of processes) {
243
239
  this._sink.removeProcess(process);
244
240
  }
245
241
  }
246
242
 
247
- public calls(): AsyncIterableIterator<[string, Models.Call]> {
248
- const { callStorage } = this._storages;
249
- return callStorage[Symbol.asyncIterator]();
243
+ public calls(): AsyncIterableIterator<CallEntry> {
244
+ const storageProvider = this._storages;
245
+
246
+ return asyncIteratorConverter((async function *() {
247
+ for await (const [ , callModel ] of storageProvider.callStorage[Symbol.asyncIterator]()) {
248
+ if (!callModel) continue;
249
+ yield createCallEntry(storageProvider, callModel);
250
+ }
251
+ })());
250
252
  }
251
253
 
252
- public sfus(): AsyncIterableIterator<[string, Models.Sfu]> {
253
- const { sfuStorage } = this._storages;
254
- return sfuStorage[Symbol.asyncIterator]();
254
+ public sfus(): AsyncIterableIterator<SfuEntry> {
255
+ const storageProvider = this._storages;
256
+
257
+ return asyncIteratorConverter((async function *() {
258
+ for await (const [ , sfuModel ] of storageProvider.sfuStorage[Symbol.asyncIterator]()) {
259
+ if (!sfuModel) continue;
260
+ yield createSfuEntry(storageProvider, sfuModel);
261
+ }
262
+ })());
255
263
  }
256
264
 
257
- public sfuTransports(): AsyncIterableIterator<[string, Models.SfuTransport]> {
258
- const { sfuTransportStorage } = this._storages;
259
- return sfuTransportStorage[Symbol.asyncIterator]();
265
+ public sfuTransports(): AsyncIterableIterator<SfuTransportEntry> {
266
+ const storageProvider = this._storages;
267
+
268
+ return asyncIteratorConverter((async function *() {
269
+ for await (const [ , sfuTransportModel ] of storageProvider.sfuTransportStorage[Symbol.asyncIterator]()) {
270
+ if (!sfuTransportModel) continue;
271
+ yield createSfuTransportEntry(storageProvider, sfuTransportModel);
272
+ }
273
+ })());
260
274
  }
261
275
 
262
- public sfuInboundRtpPads(): AsyncIterableIterator<[string, Models.SfuInboundRtpPad]> {
263
- const { sfuInboundRtpPadStorage } = this._storages;
264
- return sfuInboundRtpPadStorage[Symbol.asyncIterator]();
276
+ public sfuInboundRtpPads(): AsyncIterableIterator<SfuInboundRtpPadEntry> {
277
+ const storageProvider = this._storages;
278
+
279
+ return asyncIteratorConverter((async function *() {
280
+ for await (const [ , sfuInboundRtpPadModel ] of storageProvider.sfuInboundRtpPadStorage[Symbol.asyncIterator]()) {
281
+ if (!sfuInboundRtpPadModel) continue;
282
+ yield createSfuInboundRtpPadEntry(storageProvider, sfuInboundRtpPadModel);
283
+ }
284
+ })());
265
285
  }
266
286
 
267
- public sfuOutboundRtpPads(): AsyncIterableIterator<[string, Models.SfuOutboundRtpPad]> {
268
- const { sfuOutboundRtpPadStorage } = this._storages;
269
- return sfuOutboundRtpPadStorage[Symbol.asyncIterator]();
287
+ public sfuOutboundRtpPads(): AsyncIterableIterator<SfuOutboundRtpPadEntry> {
288
+ const storageProvider = this._storages;
289
+
290
+ return asyncIteratorConverter((async function *() {
291
+ for await (const [ , sfuOutboundRtpPadModel ] of storageProvider.sfuOutboundRtpPadStorage[Symbol.asyncIterator]()) {
292
+ if (!sfuOutboundRtpPadModel) continue;
293
+ yield createSfuOutboundRtpPadEntry(storageProvider, sfuOutboundRtpPadModel);
294
+ }
295
+ })());
270
296
  }
271
297
 
272
- public clients(): AsyncIterableIterator<[string, Models.Client]> {
273
- const { clientStorage } = this._storages;
274
- return clientStorage[Symbol.asyncIterator]();
298
+ public clients(): AsyncIterableIterator<ClientEntry> {
299
+ const storageProvider = this._storages;
300
+
301
+ return asyncIteratorConverter((async function *() {
302
+ for await (const [ , callModel ] of storageProvider.clientStorage[Symbol.asyncIterator]()) {
303
+ if (!callModel) continue;
304
+ yield createClientEntry(storageProvider, callModel);
305
+ }
306
+ })());
275
307
  }
276
308
 
277
- public peerConnections(): AsyncIterableIterator<[string, Models.PeerConnection]> {
278
- const { peerConnectionStorage } = this._storages;
279
- return peerConnectionStorage[Symbol.asyncIterator]();
309
+ public peerConnections(): AsyncIterableIterator<PeerConnectionEntry> {
310
+ const storageProvider = this._storages;
311
+
312
+ return asyncIteratorConverter((async function *() {
313
+ for await (const [ , peerConnectionModel ] of storageProvider.peerConnectionStorage[Symbol.asyncIterator]()) {
314
+ if (!peerConnectionModel) continue;
315
+ yield createPeerConnectionEntry(storageProvider, peerConnectionModel);
316
+ }
317
+ })());
280
318
  }
281
319
 
282
320
  public async getTrack(trackId: string) {
283
321
  const { inboundTrackStorage, outboundTrackStorage } = this._storages;
284
- const [inbTrack, outbTrack] = await Promise.all([
322
+ const [ inbTrackModel, outbTrackModel ] = await Promise.all([
285
323
  inboundTrackStorage.get(trackId),
286
324
  outboundTrackStorage.get(trackId),
287
325
  ]);
288
- if (inbTrack) return inbTrack;
289
- else if (outbTrack) return outbTrack;
326
+
327
+ if (inbTrackModel) return createInboundTrackEntry(this._storages, inbTrackModel);
328
+ else if (outbTrackModel) return createOutboundTrackEntry(this._storages, outbTrackModel);
290
329
  }
291
330
 
292
331
  public async getAllTracks(trackIds: Iterable<string>) {
332
+ const storageProvider = this._storages;
293
333
  const { inboundTrackStorage, outboundTrackStorage } = this._storages;
294
- const [inbTracks, outbTracks] = await Promise.all([
334
+
335
+ const [ inbTrackModels, outbTrackModels ] = await Promise.all([
295
336
  inboundTrackStorage.getAll(trackIds),
296
337
  outboundTrackStorage.getAll(trackIds),
297
338
  ]);
298
- return new Map([...inbTracks, ...outbTracks]);
339
+
340
+ const result = new Map<string, InboundTrackEntry | OutboundTrackEntry>();
341
+
342
+ for (const [ trackId, model ] of inbTrackModels) {
343
+ result.set(trackId, createInboundTrackEntry(storageProvider, model));
344
+ }
345
+ for (const [ trackId, model ] of outbTrackModels) {
346
+ result.set(trackId, createOutboundTrackEntry(storageProvider, model));
347
+ }
348
+
349
+ return result;
299
350
  }
300
351
 
301
- public inboundTracks(): AsyncIterableIterator<[string, Models.InboundTrack]> {
302
- const { inboundTrackStorage } = this._storages;
303
- return inboundTrackStorage[Symbol.asyncIterator]();
352
+ public inboundTracks(): AsyncIterableIterator<InboundTrackEntry> {
353
+ const storageProvider = this._storages;
354
+
355
+ return asyncIteratorConverter((async function *() {
356
+ for await (const [ , inboundTrackModel ] of storageProvider.inboundTrackStorage[Symbol.asyncIterator]()) {
357
+ if (!inboundTrackModel) continue;
358
+ yield createInboundTrackEntry(storageProvider, inboundTrackModel);
359
+ }
360
+ })());
304
361
  }
305
362
 
306
- public outboundTracks(): AsyncIterableIterator<[string, Models.OutboundTrack]> {
307
- const { outboundTrackStorage } = this._storages;
308
- return outboundTrackStorage[Symbol.asyncIterator]();
363
+ public outboundTracks(): AsyncIterableIterator<OutboundTrackEntry> {
364
+ const storageProvider = this._storages;
365
+
366
+ return asyncIteratorConverter((async function *() {
367
+ for await (const [ , outboundTrackModel ] of storageProvider.outboundTrackStorage[Symbol.asyncIterator]()) {
368
+ if (!outboundTrackModel) continue;
369
+ yield createOutboundTrackEntry(storageProvider, outboundTrackModel);
370
+ }
371
+ })());
309
372
  }
310
373
 
311
- public sfuSctpChannels(): AsyncIterableIterator<[string, Models.SfuSctpChannel]> {
312
- const { sfuSctpChannelStorage } = this._storages;
313
- return sfuSctpChannelStorage[Symbol.asyncIterator]();
374
+ public sfuSctpChannels(): AsyncIterableIterator<SfuSctpChannelEntry> {
375
+ const storageProvider = this._storages;
376
+
377
+ return asyncIteratorConverter((async function *() {
378
+ for await (const [ , sfuSctpChannelModel ] of storageProvider.sfuSctpChannelStorage[Symbol.asyncIterator]()) {
379
+ if (!sfuSctpChannelModel) continue;
380
+ yield createSfuSctpChannelEntry(storageProvider, sfuSctpChannelModel);
381
+ }
382
+ })());
314
383
  }
315
384
 
316
- public async getCall(callId: string): Promise<Models.Call | undefined> {
317
- const { callStorage } = this._storages;
318
- return callStorage.get(callId);
385
+ public async getCall(callId: string): Promise<CallEntry> {
386
+ return createCallEntry(this._storages, await this._storages.callStorage.get(callId));
319
387
  }
320
388
 
321
- public async getAllCalls(callIds: Iterable<string>): Promise<ReadonlyMap<string, Models.Call>> {
322
- const { callStorage } = this._storages;
323
- return callStorage.getAll(callIds);
389
+ public async getAllCalls(callIds: Iterable<string>): Promise<ReadonlyMap<string, CallEntry>> {
390
+ const models = await this._storages.callStorage.getAll(callIds);
391
+
392
+ return new Map(Array.from(models).map(([ callId, model ]) => [ callId, createCallEntry(this._storages, model) ]));
324
393
  }
325
394
 
326
- public async getSfu(sfuId: string): Promise<Models.Sfu | undefined> {
327
- const { sfuStorage } = this._storages;
328
- return sfuStorage.get(sfuId);
395
+ public async getSfu(sfuId: string): Promise<SfuEntry> {
396
+ return createSfuEntry(this._storages, await this._storages.sfuStorage.get(sfuId));
329
397
  }
330
398
 
331
- public async getAllSfus(sfuIds: Iterable<string>): Promise<ReadonlyMap<string, Models.Sfu>> {
332
- const { sfuStorage } = this._storages;
333
- return sfuStorage.getAll(sfuIds);
399
+ public async getAllSfus(sfuIds: Iterable<string>): Promise<ReadonlyMap<string, SfuEntry>> {
400
+ const models = await this._storages.sfuStorage.getAll(sfuIds);
401
+
402
+ return new Map(Array.from(models).map(([ sfuId, model ]) => [ sfuId, createSfuEntry(this._storages, model) ]));
334
403
  }
335
404
 
336
- public async getSfuTransport(sfuTransportId: string): Promise<Models.SfuTransport | undefined> {
337
- const { sfuTransportStorage } = this._storages;
338
- return sfuTransportStorage.get(sfuTransportId);
405
+ public async getSfuTransport(sfuTransportId: string): Promise<SfuTransportEntry | undefined> {
406
+ return createSfuTransportEntry(this._storages, await this._storages.sfuTransportStorage.get(sfuTransportId));
339
407
  }
340
408
 
341
409
  public async getAllSfuTransports(
342
410
  sfuTransportIds: Iterable<string>
343
- ): Promise<ReadonlyMap<string, Models.SfuTransport>> {
344
- const { sfuTransportStorage } = this._storages;
345
- return sfuTransportStorage.getAll(sfuTransportIds);
411
+ ): Promise<ReadonlyMap<string, SfuTransportEntry>> {
412
+ const models = await this._storages.sfuTransportStorage.getAll(sfuTransportIds);
413
+
414
+ return new Map(Array.from(models).map(([ sfuTransportId, model ]) => [ sfuTransportId, createSfuTransportEntry(this._storages, model) ]));
346
415
  }
347
416
 
348
- public async getSfuInboundRtpPad(sfuInboundRtpPadId: string): Promise<Models.SfuInboundRtpPad | undefined> {
349
- const { sfuInboundRtpPadStorage } = this._storages;
350
- return sfuInboundRtpPadStorage.get(sfuInboundRtpPadId);
417
+ public async getSfuInboundRtpPad(sfuInboundRtpPadId: string): Promise<SfuInboundRtpPadEntry | undefined> {
418
+ return createSfuInboundRtpPadEntry(this._storages, await this._storages.sfuInboundRtpPadStorage.get(sfuInboundRtpPadId));
351
419
  }
352
420
 
353
421
  public async getAllSfuInboundRtpPads(
354
422
  sfuInboundRtpPadIds: Iterable<string>
355
- ): Promise<ReadonlyMap<string, Models.SfuInboundRtpPad>> {
356
- const { sfuInboundRtpPadStorage } = this._storages;
357
- return sfuInboundRtpPadStorage.getAll(sfuInboundRtpPadIds);
423
+ ): Promise<ReadonlyMap<string, SfuInboundRtpPadEntry>> {
424
+ const models = await this._storages.sfuInboundRtpPadStorage.getAll(sfuInboundRtpPadIds);
425
+
426
+ return new Map(Array.from(models).map(([ sfuInboundRtpPadId, model ]) => [ sfuInboundRtpPadId, createSfuInboundRtpPadEntry(this._storages, model) ]));
358
427
  }
359
428
 
360
- public async getSfuOutboundRtpPad(sfuOutboundRtpPadId: string): Promise<Models.SfuOutboundRtpPad | undefined> {
361
- const { sfuOutboundRtpPadStorage } = this._storages;
362
- return sfuOutboundRtpPadStorage.get(sfuOutboundRtpPadId);
429
+ public async getSfuOutboundRtpPad(sfuOutboundRtpPadId: string): Promise<SfuOutboundRtpPadEntry | undefined> {
430
+ return createSfuOutboundRtpPadEntry(this._storages, await this._storages.sfuOutboundRtpPadStorage.get(sfuOutboundRtpPadId));
363
431
  }
364
432
 
365
433
  public async getAllSfuOutboundRtpPads(
366
434
  sfuOutboundRtpPadIds: Iterable<string>
367
- ): Promise<ReadonlyMap<string, Models.SfuOutboundRtpPad>> {
368
- const { sfuOutboundRtpPadStorage } = this._storages;
369
- return sfuOutboundRtpPadStorage.getAll(sfuOutboundRtpPadIds);
435
+ ): Promise<ReadonlyMap<string, SfuOutboundRtpPadEntry>> {
436
+ const models = await this._storages.sfuOutboundRtpPadStorage.getAll(sfuOutboundRtpPadIds);
437
+
438
+ return new Map(Array.from(models).map(([ sfuOutboundRtpPadId, model ]) => [ sfuOutboundRtpPadId, createSfuOutboundRtpPadEntry(this._storages, model) ]));
370
439
  }
371
440
 
372
- public async getClient(clientId: string): Promise<Models.Client | undefined> {
373
- const { clientStorage } = this._storages;
374
- return clientStorage.get(clientId);
441
+ public async getClient(clientId: string): Promise<ClientEntry> {
442
+ return createClientEntry(this._storages, await this._storages.clientStorage.get(clientId));
375
443
  }
376
444
 
377
- public async getAllClient(clientIds: Iterable<string>): Promise<ReadonlyMap<string, Models.Client>> {
378
- const { clientStorage } = this._storages;
379
- return clientStorage.getAll(clientIds);
445
+ public async getAllClient(clientIds: Iterable<string>): Promise<ReadonlyMap<string, ClientEntry>> {
446
+ const models = await this._storages.clientStorage.getAll(clientIds);
447
+
448
+ return new Map(Array.from(models).map(([ clientId, model ]) => [ clientId, createClientEntry(this._storages, model) ]));
380
449
  }
381
450
 
382
- public async getPeerConnection(peerConnectionId: string): Promise<Models.PeerConnection | undefined> {
383
- const { peerConnectionStorage } = this._storages;
384
- return peerConnectionStorage.get(peerConnectionId);
451
+ public async getPeerConnection(peerConnectionId: string): Promise<PeerConnectionEntry> {
452
+ return createPeerConnectionEntry(this._storages, await this._storages.peerConnectionStorage.get(peerConnectionId));
385
453
  }
386
454
 
387
455
  public async getAllPeerConnections(
388
456
  peerConnectionIds: Iterable<string>
389
- ): Promise<ReadonlyMap<string, Models.PeerConnection>> {
390
- const { peerConnectionStorage } = this._storages;
391
- return peerConnectionStorage.getAll(peerConnectionIds);
457
+ ): Promise<ReadonlyMap<string, PeerConnectionEntry>> {
458
+ const models = await this._storages.peerConnectionStorage.getAll(peerConnectionIds);
459
+
460
+ return new Map(Array.from(models).map(([ peerConnectionId, model ]) => [ peerConnectionId, createPeerConnectionEntry(this._storages, model) ]));
392
461
  }
393
462
 
394
- public async getInboundTrack(inboundTrackId: string): Promise<Models.InboundTrack | undefined> {
395
- const { inboundTrackStorage } = this._storages;
396
- return inboundTrackStorage.get(inboundTrackId);
463
+ public async getInboundTrack(inboundTrackId: string): Promise<InboundTrackEntry> {
464
+ return createInboundTrackEntry(this._storages, await this._storages.inboundTrackStorage.get(inboundTrackId));
397
465
  }
398
466
 
399
467
  public async getAllInboundTracks(
400
468
  inboundTrackIds: Iterable<string>
401
- ): Promise<ReadonlyMap<string, Models.InboundTrack>> {
402
- const { inboundTrackStorage } = this._storages;
403
- return inboundTrackStorage.getAll(inboundTrackIds);
469
+ ): Promise<ReadonlyMap<string, InboundTrackEntry>> {
470
+ const models = await this._storages.inboundTrackStorage.getAll(inboundTrackIds);
471
+
472
+ return new Map(Array.from(models).map(([ inboundTrackId, model ]) => [ inboundTrackId, createInboundTrackEntry(this._storages, model) ]));
404
473
  }
405
474
 
406
- public async getOutboundTrack(outboundTrackId: string): Promise<Models.OutboundTrack | undefined> {
407
- const { outboundTrackStorage } = this._storages;
408
- return outboundTrackStorage.get(outboundTrackId);
475
+ public async getOutboundTrack(outboundTrackId: string): Promise<OutboundTrackEntry> {
476
+ return createOutboundTrackEntry(this._storages, await this._storages.outboundTrackStorage.get(outboundTrackId));
409
477
  }
410
478
 
411
479
  public async getAllOutboundTracks(
412
480
  outboundTrackIds: Iterable<string>
413
- ): Promise<ReadonlyMap<string, Models.OutboundTrack>> {
414
- const { outboundTrackStorage } = this._storages;
415
- return outboundTrackStorage.getAll(outboundTrackIds);
481
+ ): Promise<ReadonlyMap<string, OutboundTrackEntry>> {
482
+ const models = await this._storages.outboundTrackStorage.getAll(outboundTrackIds);
483
+
484
+ return new Map(Array.from(models).map(([ outboundTrackId, model ]) => [ outboundTrackId, createOutboundTrackEntry(this._storages, model) ]));
416
485
  }
417
486
 
418
- public async getSfuSctpChannel(sctpChannelId: string): Promise<Models.SfuSctpChannel | undefined> {
419
- const { sfuSctpChannelStorage } = this._storages;
420
- return sfuSctpChannelStorage.get(sctpChannelId);
487
+ public async getSfuSctpChannel(sctpChannelId: string): Promise<SfuSctpChannelEntry | undefined> {
488
+ return createSfuSctpChannelEntry(this._storages, await this._storages.sfuSctpChannelStorage.get(sctpChannelId));
421
489
  }
422
490
 
423
491
  public async getAllSfuSctpChannel(
424
492
  sctpChannelIds: Iterable<string>
425
- ): Promise<ReadonlyMap<string, Models.SfuSctpChannel>> {
426
- const { sfuSctpChannelStorage } = this._storages;
427
- return sfuSctpChannelStorage.getAll(sctpChannelIds);
493
+ ): Promise<ReadonlyMap<string, SfuSctpChannelEntry>> {
494
+ const models = await this._storages.sfuSctpChannelStorage.getAll(sctpChannelIds);
495
+
496
+ return new Map(Array.from(models).map(([ sctpChannelId, model ]) => [ sctpChannelId, createSfuSctpChannelEntry(this._storages, model) ]));
428
497
  }
429
498
 
430
499
  public get closed() {
@@ -433,8 +502,7 @@ export class Observer {
433
502
 
434
503
  public close() {
435
504
  if (this._closed) {
436
- logger.debug(`Attempted to close twice`);
437
- return;
505
+ return logger.debug('Attempted to close twice');
438
506
  }
439
507
  this._closed = true;
440
508
  this._sources.close();
@@ -3,6 +3,7 @@
3
3
  * Semaphores are used to ensure that only one process can access the shared resource at a time.
4
4
  */
5
5
  export interface Semaphore {
6
+
6
7
  /**
7
8
  * Acquires the semaphore, blocking until the semaphore is available.
8
9
  * When the semaphore is acquired, it prevents other processes from acquiring it.
@@ -29,6 +30,7 @@ export function createDummySemaphore(): Semaphore {
29
30
  * SemaphoreProviders are used to manage access to shared resources across multiple processes.
30
31
  */
31
32
  export interface SemaphoreProvider {
33
+
32
34
  /**
33
35
  * A Semaphore instance that manages access to the 'call' shared resource.
34
36
  * This semaphore ensures that only one process can access the 'call' resource at a time.