@flashphoner/sfusdk-examples 2.0.244 → 2.0.248

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.
@@ -8,7 +8,7 @@ let playState;
8
8
  const PUBLISH = "publish";
9
9
  const PLAY = "play";
10
10
  const STOP = "stop";
11
- const PRELOADER_URL="../commons/media/silence.mp3";
11
+ const PRELOADER_URL = "../commons/media/silence.mp3";
12
12
 
13
13
 
14
14
  /**
@@ -19,7 +19,9 @@ const defaultConfig = {
19
19
  url: "wss://127.0.0.1:8888",
20
20
  name: "ROOM1",
21
21
  pin: "1234",
22
- nickName: "User1"
22
+ nickName: "User1",
23
+ failedProbesThreshold: 5,
24
+ pingInterval: 5000
23
25
  },
24
26
  media: {
25
27
  audio: {
@@ -31,18 +33,21 @@ const defaultConfig = {
31
33
  ]
32
34
  },
33
35
  video: {
34
- tracks: [
35
- {
36
- source: "camera",
37
- width: 640,
38
- height: 360,
39
- codec: "H264",
40
- encodings: [
41
- { rid: "360p", active: true, maxBitrate: 500000 },
42
- { rid: "180p", active: true, maxBitrate: 200000, scaleResolutionDownBy: 2 }
43
- ]
44
- }
45
- ]
36
+ tracks: Array(1).fill({
37
+ source: "camera",
38
+ width: 1280,
39
+ height: 720,
40
+ codec: "H264",
41
+ constraints: {
42
+ frameRate: 25
43
+ },
44
+ encodings: [
45
+ {rid: "180p", active: true, maxBitrate: 200000, scaleResolutionDownBy: 4},
46
+ {rid: "360p", active: true, maxBitrate: 500000, scaleResolutionDownBy: 2},
47
+ {rid: "720p", active: true, maxBitrate: 900000}
48
+ ],
49
+ type: "cam1"
50
+ })
46
51
  }
47
52
  }
48
53
  };
@@ -50,64 +55,74 @@ const defaultConfig = {
50
55
  /**
51
56
  * Current state object
52
57
  */
53
- const CurrentState = function(prefix) {
58
+ const CurrentState = function (prefix) {
54
59
  let state = {
55
60
  prefix: prefix,
56
61
  pc: null,
57
62
  session: null,
58
63
  room: null,
64
+ display: null,
59
65
  roomEnded: false,
60
66
  starting: false,
61
- set: function(pc, session, room) {
67
+ set: function (pc, session, room) {
62
68
  state.pc = pc;
63
69
  state.session = session;
64
70
  state.room = room;
65
71
  state.roomEnded = false;
66
72
  },
67
- clear: function() {
73
+ clear: function () {
68
74
  state.room = null;
69
75
  state.session = null;
70
76
  state.pc = null;
71
77
  state.roomEnded = false;
72
78
  },
73
- setRoomEnded: function() {
79
+ setRoomEnded: function () {
74
80
  state.roomEnded = true;
75
81
  },
76
- buttonId: function() {
82
+ buttonId: function () {
77
83
  return state.prefix + "Btn";
78
84
  },
79
- buttonText: function() {
85
+ buttonText: function () {
80
86
  return (state.prefix.charAt(0).toUpperCase() + state.prefix.slice(1));
81
87
  },
82
- inputId: function() {
88
+ inputId: function () {
83
89
  return state.prefix + "Name";
84
90
  },
85
- statusId: function() {
91
+ statusId: function () {
86
92
  return state.prefix + "Status";
87
93
  },
88
- formId: function() {
94
+ formId: function () {
89
95
  return state.prefix + "Form";
90
96
  },
91
- errInfoId: function() {
97
+ errInfoId: function () {
92
98
  return state.prefix + "ErrorInfo";
93
99
  },
94
- is: function(value) {
100
+ is: function (value) {
95
101
  return (prefix === value);
96
102
  },
97
- isActive: function() {
103
+ isActive: function () {
98
104
  return (state.room && !state.roomEnded && state.pc);
99
105
  },
100
- isConnected: function() {
106
+ isConnected: function () {
101
107
  return (state.session && state.session.state() === constants.SFU_STATE.CONNECTED);
102
108
  },
103
- isRoomEnded: function() {
109
+ isRoomEnded: function () {
104
110
  return state.roomEnded;
105
111
  },
106
- setStarting: function(value) {
112
+ setStarting: function (value) {
107
113
  state.starting = value;
108
114
  },
109
- isStarting: function() {
115
+ isStarting: function () {
110
116
  return state.starting;
117
+ },
118
+ setDisplay: function (display) {
119
+ state.display = display;
120
+ },
121
+ disposeDisplay: function () {
122
+ if (state.display) {
123
+ state.display.stop();
124
+ state.display = null;
125
+ }
111
126
  }
112
127
  };
113
128
  return state;
@@ -116,7 +131,7 @@ const CurrentState = function(prefix) {
116
131
  /**
117
132
  * load config and set default values
118
133
  */
119
- const init = function() {
134
+ const init = function () {
120
135
  let configName = getUrlParam("config") || "./config.json";
121
136
  $("#publishBtn").prop('disabled', true);
122
137
  $("#playBtn").prop('disabled', true);
@@ -126,11 +141,11 @@ const init = function() {
126
141
  $("#playName").prop('disabled', true);
127
142
  publishState = CurrentState(PUBLISH);
128
143
  playState = CurrentState(PLAY);
129
- $.getJSON(configName, function(cfg){
144
+ $.getJSON(configName, function (cfg) {
130
145
  mainConfig = cfg;
131
146
  onDisconnected(publishState);
132
147
  onDisconnected(playState);
133
- }).fail(function(e){
148
+ }).fail(function (e) {
134
149
  //use default config
135
150
  console.error("Error reading configuration file " + configName + ": " + e.status + " " + e.statusText)
136
151
  console.log("Default config will be used");
@@ -139,15 +154,15 @@ const init = function() {
139
154
  onDisconnected(playState);
140
155
  });
141
156
  $("#url").val(setURL());
142
- $("#roomName").val("ROOM1-"+createUUID(4));
143
- $("#publishName").val("Publisher1-"+createUUID(4));
144
- $("#playName").val("Player1-"+createUUID(4));
157
+ $("#roomName").val("ROOM1-" + createUUID(4));
158
+ $("#publishName").val("Publisher1-" + createUUID(4));
159
+ $("#playName").val("Player1-" + createUUID(4));
145
160
  }
146
161
 
147
162
  /**
148
163
  * connect to server
149
164
  */
150
- const connect = async function(state) {
165
+ const connect = async function (state) {
151
166
  //create peer connection
152
167
  let pc = new RTCPeerConnection();
153
168
  //get config object for room creation
@@ -162,11 +177,11 @@ const connect = async function(state) {
162
177
  try {
163
178
  const session = await sfu.createRoom(roomConfig);
164
179
  // Set up session ending events
165
- session.on(constants.SFU_EVENT.DISCONNECTED, function() {
180
+ session.on(constants.SFU_EVENT.DISCONNECTED, function () {
166
181
  onStopClick(state);
167
182
  onDisconnected(state);
168
183
  setStatus(state.statusId(), "DISCONNECTED", "green");
169
- }).on(constants.SFU_EVENT.FAILED, function(e) {
184
+ }).on(constants.SFU_EVENT.FAILED, function (e) {
170
185
  onStopClick(state);
171
186
  onDisconnected(state);
172
187
  setStatus(state.statusId(), "FAILED", "red");
@@ -179,13 +194,14 @@ const connect = async function(state) {
179
194
  // Connected successfully
180
195
  onConnected(state, pc, session);
181
196
  setStatus(state.statusId(), "ESTABLISHED", "green");
182
- } catch(e) {
197
+ } catch (e) {
183
198
  onDisconnected(state);
184
199
  setStatus(state.statusId(), "FAILED", "red");
185
200
  setStatus(state.errInfoId(), e, "red");
186
- }}
201
+ }
202
+ }
187
203
 
188
- const onConnected = function(state, pc, session) {
204
+ const onConnected = function (state, pc, session) {
189
205
  state.set(pc, session, session.room());
190
206
  $("#" + state.buttonId()).text("Stop").off('click').click(function () {
191
207
  onStopClick(state);
@@ -194,25 +210,25 @@ const onConnected = function(state, pc, session) {
194
210
  $("#roomName").prop('disabled', true);
195
211
  $("#" + state.inputId()).prop('disabled', true);
196
212
  // Add errors displaying
197
- state.room.on(constants.SFU_ROOM_EVENT.FAILED, function(e) {
213
+ state.room.on(constants.SFU_ROOM_EVENT.FAILED, function (e) {
198
214
  setStatus(state.errInfoId(), e, "red");
199
215
  state.setRoomEnded();
200
216
  onStopClick(state);
201
217
  }).on(constants.SFU_ROOM_EVENT.OPERATION_FAILED, function (e) {
202
218
  onOperationFailed(state, e);
203
219
  }).on(constants.SFU_ROOM_EVENT.ENDED, function () {
204
- setStatus(state.errInfoId(), "Room "+state.room.name()+" has ended", "red");
220
+ setStatus(state.errInfoId(), "Room " + state.room.name() + " has ended", "red");
205
221
  state.setRoomEnded();
206
222
  onStopClick(state);
207
223
  }).on(constants.SFU_ROOM_EVENT.DROPPED, function () {
208
- setStatus(state.errInfoId(), "Dropped from the room "+state.room.name()+" due to network issues", "red");
224
+ setStatus(state.errInfoId(), "Dropped from the room " + state.room.name() + " due to network issues", "red");
209
225
  state.setRoomEnded();
210
226
  onStopClick(state);
211
227
  });
212
228
  startStreaming(state);
213
229
  }
214
230
 
215
- const onDisconnected = function(state) {
231
+ const onDisconnected = function (state) {
216
232
  state.clear();
217
233
  $("#" + state.buttonId()).text(state.buttonText()).off('click').click(function () {
218
234
  onStartClick(state);
@@ -228,10 +244,10 @@ const onDisconnected = function(state) {
228
244
  }
229
245
  }
230
246
 
231
- const onStartClick = function(state) {
247
+ const onStartClick = function (state) {
232
248
  if (validateForm("connectionForm", state.errInfoId())
233
- && validateForm(state.formId(), state.errInfoId())
234
- && validateName(state, state.errInfoId())) {
249
+ && validateForm(state.formId(), state.errInfoId())
250
+ && validateName(state, state.errInfoId())) {
235
251
  state.setStarting(true);
236
252
  let otherState = getOtherState(state);
237
253
  $("#" + state.buttonId()).prop('disabled', true);
@@ -249,7 +265,7 @@ const onStartClick = function(state) {
249
265
  }
250
266
  }
251
267
 
252
- const onOperationFailed = function(state, event) {
268
+ const onOperationFailed = function (state, event) {
253
269
  if (event.operation && event.error) {
254
270
  setStatus(state.errInfoId(), event.operation + " failed: " + event.error, "red");
255
271
  } else {
@@ -259,17 +275,20 @@ const onOperationFailed = function(state, event) {
259
275
  onStopClick(state);
260
276
  }
261
277
 
262
- const onStopClick = async function(state) {
278
+ const onStopClick = async function (state) {
263
279
  state.setStarting(false);
264
- stopStreaming(state);
280
+ disposeStateDisplay(state);
265
281
  if (state.isConnected()) {
266
282
  $("#" + state.buttonId()).prop('disabled', true);
267
283
  await state.session.disconnect();
268
284
  onDisconnected(state);
269
285
  }
270
286
  }
287
+ const disposeStateDisplay = function (state) {
288
+ state.disposeDisplay();
289
+ }
271
290
 
272
- const startStreaming = async function(state) {
291
+ const startStreaming = async function (state) {
273
292
  if (state.is(PUBLISH)) {
274
293
  await publishStreams(state);
275
294
  } else if (state.is(PLAY)) {
@@ -284,18 +303,11 @@ const startStreaming = async function(state) {
284
303
  }
285
304
  }
286
305
 
287
- const stopStreaming = function(state) {
288
- if (state.is(PUBLISH)) {
289
- unPublishStreams(state);
290
- } else if (state.is(PLAY)) {
291
- stopStreams(state);
292
- }
293
- }
294
-
295
- const publishStreams = async function(state) {
306
+ const publishStreams = async function (state) {
296
307
  if (state.isConnected()) {
297
308
  //create local display item to show local streams
298
- localDisplay = initLocalDisplay(document.getElementById("localVideo"));
309
+ const display = initLocalDisplay(document.getElementById("localVideo"));
310
+ state.setDisplay(display);
299
311
  try {
300
312
  //get configured local video streams
301
313
  let streams = await getVideoStreams(mainConfig);
@@ -308,7 +320,7 @@ const publishStreams = async function(state) {
308
320
  streams.forEach(function (s) {
309
321
  let contentType = s.type || s.source;
310
322
  //add local stream to local display
311
- localDisplay.add(s.stream.id, $("#" + state.inputId()).val(), s.stream, contentType);
323
+ display.add(s.stream.id, $("#" + state.inputId()).val(), s.stream, contentType);
312
324
  //add each track to PeerConnection
313
325
  s.stream.getTracks().forEach((track) => {
314
326
  config[track.id] = contentType;
@@ -319,7 +331,7 @@ const publishStreams = async function(state) {
319
331
  //start WebRTC negotiation
320
332
  await state.room.join(state.pc, null, config);
321
333
  }
322
- } catch(e) {
334
+ } catch (e) {
323
335
  if (e.type === constants.SFU_ROOM_EVENT.OPERATION_FAILED) {
324
336
  onOperationFailed(state, e);
325
337
  } else {
@@ -331,24 +343,15 @@ const publishStreams = async function(state) {
331
343
  }
332
344
  }
333
345
 
334
- const unPublishStreams = function(state) {
335
- if (localDisplay) {
336
- localDisplay.stop();
337
- }
338
- }
339
-
340
- const playStreams = async function(state) {
346
+ const playStreams = async function (state) {
341
347
  if (state.isConnected() && state.isActive()) {
342
348
  try {
343
349
  //create remote display item to show remote streams
344
- remoteDisplay = initRemoteDisplay({
345
- div: document.getElementById("remoteVideo"),
346
- room: state.room,
347
- peerConnection: state.pc
348
- });
350
+ const display = initDefaultRemoteDisplay(state.room, document.getElementById("remoteVideo"), null, null);
351
+ state.setDisplay(display);
349
352
  //start WebRTC negotiation
350
- await state.room.join(state.pc);
351
- } catch(e) {
353
+ await state.room.join(state.pc, null, null, 1);
354
+ } catch (e) {
352
355
  if (e.type === constants.SFU_ROOM_EVENT.OPERATION_FAILED) {
353
356
  onOperationFailed(state, e);
354
357
  } else {
@@ -360,14 +363,8 @@ const playStreams = async function(state) {
360
363
  }
361
364
  }
362
365
 
363
- const stopStreams = function(state) {
364
- if (remoteDisplay) {
365
- remoteDisplay.stop();
366
- }
367
- }
368
-
369
- const subscribeTrackToEndedEvent = function(room, track, pc) {
370
- track.addEventListener("ended", async function() {
366
+ const subscribeTrackToEndedEvent = function (room, track, pc) {
367
+ track.addEventListener("ended", async function () {
371
368
  //track ended, see if we need to cleanup
372
369
  let negotiate = false;
373
370
  for (const sender of pc.getSenders()) {
@@ -385,7 +382,7 @@ const subscribeTrackToEndedEvent = function(room, track, pc) {
385
382
  });
386
383
  };
387
384
 
388
- const addTrackToPeerConnection = function(pc, stream, track, encodings) {
385
+ const addTrackToPeerConnection = function (pc, stream, track, encodings) {
389
386
  pc.addTransceiver(track, {
390
387
  direction: "sendonly",
391
388
  streams: [stream],
@@ -444,7 +441,7 @@ const buttonText = function (string) {
444
441
  return string.charAt(0).toUpperCase() + string.slice(1);
445
442
  }
446
443
 
447
- const getOtherState = function(state) {
444
+ const getOtherState = function (state) {
448
445
  if (state.is(PUBLISH)) {
449
446
  return playState;
450
447
  } else if (state.is(PLAY)) {
@@ -13,7 +13,7 @@ const CurrentState = function() {
13
13
  pc: null,
14
14
  session: null,
15
15
  room: null,
16
- remoteDisplay: null,
16
+ display: null,
17
17
  roomEnded: false,
18
18
  set: function(pc, session, room) {
19
19
  state.pc = pc;
@@ -39,13 +39,13 @@ const CurrentState = function() {
39
39
  isActive: function() {
40
40
  return (state.room && !state.roomEnded && state.pc);
41
41
  },
42
- setDisplay: function(display) {
43
- state.remoteDisplay = display;
42
+ setDisplay: function (display) {
43
+ state.display = display;
44
44
  },
45
- disposeDisplay: function() {
46
- if (state.remoteDisplay) {
47
- state.remoteDisplay.stop();
48
- state.remoteDisplay = null;
45
+ disposeDisplay: function () {
46
+ if (state.display) {
47
+ state.display.stop();
48
+ state.display = null;
49
49
  }
50
50
  }
51
51
  };
@@ -192,24 +192,17 @@ const onOperationFailed = function(state, event) {
192
192
  onStopClick(state);
193
193
  }
194
194
 
195
- const playStreams = async function(state) {
195
+ const playStreams = async function (state) {
196
196
  try {
197
197
  // Create remote display item to show remote streams
198
- state.setDisplay(initRemoteDisplay({
199
- div: document.getElementById("remoteVideo"),
200
- room: state.room,
201
- peerConnection: state.pc,
202
- displayOptions: {
203
- publisher: false,
204
- quality: true,
205
- type: false,
206
- abr: true,
207
- abrKeepOnGoodQuality: 20000,
208
- abrTryForUpperQuality: 30000
209
- }
210
- }));
198
+ const display = initRemoteDisplay(state.room, document.getElementById("remoteVideo"), {quality:true, autoAbr: true}, {thresholds: [
199
+ {parameter: "nackCount", maxLeap: 10},
200
+ {parameter: "freezeCount", maxLeap: 10},
201
+ {parameter: "packetsLost", maxLeap: 10}
202
+ ], abrKeepOnGoodQuality: ABR_KEEP_ON_QUALITY, abrTryForUpperQuality: ABR_TRY_UPPER_QUALITY, interval: ABR_QUALITY_CHECK_PERIOD},createDefaultMeetingController, createDefaultMeetingModel, createDefaultMeetingView, oneToOneParticipantFactory(remoteTrackProvider(state.room)));
203
+ state.setDisplay(display);
211
204
  // Start WebRTC negotiation
212
- await state.room.join(state.pc);
205
+ await state.room.join(state.pc, null, null, 1);
213
206
  } catch(e) {
214
207
  if (e.type === constants.SFU_ROOM_EVENT.OPERATION_FAILED) {
215
208
  onOperationFailed(state, e);
@@ -222,7 +215,7 @@ const playStreams = async function(state) {
222
215
 
223
216
  }
224
217
 
225
- const stopStreams = function(state) {
218
+ const stopStreams = function (state) {
226
219
  state.disposeDisplay();
227
220
  }
228
221