@flashphoner/sfusdk-examples 2.0.193 → 2.0.196

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flashphoner/sfusdk-examples",
3
- "version": "2.0.193",
3
+ "version": "2.0.196",
4
4
  "description": "Official Flashphoner WebCallServer SFU SDK usage examples",
5
5
  "main": "dist/sfu.js",
6
6
  "types": "src/sfu.ts",
@@ -27,6 +27,6 @@
27
27
  "webpack-cli": "^4.9.2"
28
28
  },
29
29
  "dependencies": {
30
- "@flashphoner/sfusdk": "^2.0.130"
30
+ "@flashphoner/sfusdk": "^2.0.193"
31
31
  }
32
32
  }
@@ -163,12 +163,19 @@ const createControls = function(config) {
163
163
  }
164
164
 
165
165
  const roomConfig = function() {
166
- return {
166
+ let roomConfig = {
167
167
  url: controls.entrance.url.value,
168
168
  roomName: controls.entrance.roomName.value,
169
169
  pin: controls.entrance.roomPin.value,
170
170
  nickname: controls.entrance.nickName.value
171
+ };
172
+ if (config.room.failedProbesThreshold !== undefined) {
173
+ roomConfig.failedProbesThreshold = config.room.failedProbesThreshold;
171
174
  }
175
+ if (config.room.pingInterval !== undefined) {
176
+ roomConfig.pingInterval = config.room.pingInterval;
177
+ }
178
+ return roomConfig;
172
179
  }
173
180
 
174
181
  const getVideoStreams = function() {
@@ -301,17 +308,28 @@ const getMedia = async function(tracks) {
301
308
  if (track.source === "mic") {
302
309
  //audio
303
310
  constraints.audio = {};
311
+ if (track.constraints) {
312
+ constraints.audio = track.constraints;
313
+ }
304
314
  constraints.audio.stereo = track.channels !== 1
315
+ if (track.channels && track.channels === 2) {
316
+ constraints.audio.echoCancellation = false;
317
+ constraints.audio.googEchoCancellation = false;
318
+ }
305
319
  } else if (track.source === "camera") {
306
- constraints.video = {
307
- width: track.width,
308
- height: track.height
309
- };
320
+ constraints.video = {};
321
+ if (track.constraints) {
322
+ constraints.video = track.constraints;
323
+ }
324
+ constraints.video.width = track.width;
325
+ constraints.video.height = track.height;
310
326
  } else if (track.source === "screen") {
311
- constraints.video = {
312
- width: track.width,
313
- height: track.height
314
- };
327
+ constraints.video = {};
328
+ if (track.constraints) {
329
+ constraints.video = track.constraints;
330
+ }
331
+ constraints.video.width = track.width;
332
+ constraints.video.height = track.height;
315
333
  screen = true;
316
334
  }
317
335
  });
@@ -68,7 +68,13 @@ async function connect() {
68
68
  const session = await sfu.createRoom(roomConfig);
69
69
  // Now we connected to the server (if no exception was thrown)
70
70
  session.on(constants.SFU_EVENT.FAILED, function(e) {
71
- displayError("CONNECTION FAILED: "+ e +". Refresh the page to enter the room again");
71
+ if (e.status && e.statusText) {
72
+ displayError("CONNECTION FAILED: " + e.status + " " + e.statusText);
73
+ } else if (e.type && e.info) {
74
+ displayError("CONNECTION FAILED: " + e.info);
75
+ } else {
76
+ displayError("CONNECTION FAILED: " + e);
77
+ }
72
78
  }).on(constants.SFU_EVENT.DISCONNECTED, function(e) {
73
79
  displayError("DISCONNECTED. Refresh the page to enter the room again");
74
80
  });
@@ -1,10 +1,16 @@
1
1
  const getRoomConfig = function(config) {
2
2
  let roomConfig = {
3
- url: config.url || "ws://127.0.0.1:8080",
4
- roomName: config.name || "ROOM1",
5
- pin: config.pin || "1234",
6
- nickname: config.nickName || "User1"
3
+ url: config.room.url || "ws://127.0.0.1:8080",
4
+ roomName: config.room.name || "ROOM1",
5
+ pin: config.room.pin || "1234",
6
+ nickname: config.room.nickName || "User1"
7
7
  };
8
+ if (config.room.failedProbesThreshold !== undefined) {
9
+ roomConfig.failedProbesThreshold = config.room.failedProbesThreshold;
10
+ }
11
+ if (config.room.pingInterval !== undefined) {
12
+ roomConfig.pingInterval = config.room.pingInterval;
13
+ }
8
14
  return roomConfig;
9
15
  }
10
16
 
@@ -50,6 +56,7 @@ const getMedia = async function(track) {
50
56
  if (track.constraints) {
51
57
  constraints.audio = track.constraints;
52
58
  }
59
+ constraints.audio.stereo = track.channels !== 1
53
60
  if (track.channels && track.channels === 2) {
54
61
  constraints.audio.echoCancellation = false;
55
62
  constraints.audio.googEchoCancellation = false;
@@ -171,20 +171,28 @@ const connect = async function(state) {
171
171
  const session = await sfu.createRoom(roomConfig);
172
172
  // Set up session ending events
173
173
  session.on(constants.SFU_EVENT.DISCONNECTED, function() {
174
+ onStopClick(state);
174
175
  state.clear();
175
176
  onDisconnected(state);
176
177
  setStatus(state.statusId(), "DISCONNECTED", "green");
177
178
  }).on(constants.SFU_EVENT.FAILED, function(e) {
179
+ onStopClick(state);
178
180
  state.clear();
179
181
  onDisconnected(state);
180
182
  setStatus(state.statusId(), "FAILED", "red");
181
- setStatus(state.errInfoId(), e.status + " " + e.statusText, "red");
183
+ if (e.status && e.statusText) {
184
+ setStatus(state.errInfoId(), e.status + " " + e.statusText, "red");
185
+ } else if (e.type && e.info) {
186
+ setStatus(state.errInfoId(), e.type + ": " + e.info, "red");
187
+ }
182
188
  });
183
189
  // Connected successfully
184
190
  state.set(pc, session, session.room());
185
191
  onConnected(state);
186
192
  setStatus(state.statusId(), "ESTABLISHED", "green");
187
193
  } catch(e) {
194
+ state.clear();
195
+ onDisconnected(state);
188
196
  setStatus(state.statusId(), "FAILED", "red");
189
197
  setStatus(state.errInfoId(), e, "red");
190
198
  }
package/src/sfu.ts CHANGED
@@ -1,22 +1,35 @@
1
- import {Sfu, RoomEvent, SfuEvent, State} from "@flashphoner/sfusdk";
1
+ import {
2
+ Sfu,
3
+ RoomEvent,
4
+ SfuEvent,
5
+ State
6
+ } from "@flashphoner/sfusdk";
2
7
 
3
8
  export async function createRoom(options: {
4
9
  url: string,
5
10
  roomName: string,
6
11
  pin: string,
7
- nickname: string
12
+ nickname: string,
13
+ failedProbesThreshold?: number,
14
+ pingInterval?: number,
8
15
  }) {
9
- const sfu = new Sfu();
10
- await sfu.connect({
11
- url: options.url,
12
- nickname: options.nickname,
13
- logGroup: options.roomName
14
- });
15
- sfu.createRoom({
16
- name: options.roomName,
17
- pin: options.pin
18
- });
19
- return sfu;
16
+ try {
17
+ const sfu = new Sfu();
18
+ await sfu.connect({
19
+ url: options.url,
20
+ nickname: options.nickname,
21
+ logGroup: options.roomName,
22
+ failedProbesThreshold: options.failedProbesThreshold,
23
+ pingInterval: options.pingInterval
24
+ });
25
+ sfu.createRoom({
26
+ name: options.roomName,
27
+ pin: options.pin
28
+ });
29
+ return sfu;
30
+ } catch (e) {
31
+ throw new Error("Can't connect to websocket URL " + options.url);
32
+ }
20
33
  }
21
34
 
22
35
  export const constants = {
@@ -3,7 +3,9 @@
3
3
  "url": "ws://127.0.0.1:8080",
4
4
  "name": "ROOM1",
5
5
  "pin": "1234",
6
- "nickName": "User1"
6
+ "nickName": "User1",
7
+ "failedProbesThreshold": 5,
8
+ "pingInterval": 5000
7
9
  },
8
10
  "media": {
9
11
  "audio": {
@@ -211,24 +211,31 @@ const connect = async function(state) {
211
211
  const session = await sfu.createRoom(roomConfig);
212
212
  // Set up session ending events
213
213
  session.on(constants.SFU_EVENT.DISCONNECTED, function() {
214
+ onStopClick(state);
214
215
  state.clear();
215
216
  onDisconnected(state);
216
217
  setStatus(state.statusId(), "DISCONNECTED", "green");
217
218
  }).on(constants.SFU_EVENT.FAILED, function(e) {
219
+ onStopClick(state);
218
220
  state.clear();
219
221
  onDisconnected(state);
220
222
  setStatus(state.statusId(), "FAILED", "red");
221
- setStatus(state.errInfoId(), e.status + " " + e.statusText, "red");
223
+ if (e.status && e.statusText) {
224
+ setStatus(state.errInfoId(), e.status + " " + e.statusText, "red");
225
+ } else if (e.type && e.info) {
226
+ setStatus(state.errInfoId(), e.type + ": " + e.info, "red");
227
+ }
222
228
  });
223
229
  // Connected successfully
224
230
  state.set(pc, session, session.room());
225
231
  onConnected(state);
226
232
  setStatus(state.statusId(), "ESTABLISHED", "green");
227
233
  } catch(e) {
234
+ state.clear();
235
+ onDisconnected(state);
228
236
  setStatus(state.statusId(), "FAILED", "red");
229
237
  setStatus(state.errInfoId(), e, "red");
230
- }
231
- }
238
+ }}
232
239
 
233
240
  const onConnected = function(state) {
234
241
  $("#" + state.buttonId()).text("Stop").off('click').click(function () {
@@ -351,7 +358,7 @@ const publishStreams = async function(state) {
351
358
  s.stream.getTracks().forEach((track) => {
352
359
  config[track.id] = contentType;
353
360
  addTrackToPeerConnection(state.pc, s.stream, track, s.encodings);
354
- subscribeTrackToEndedEvent(state, track);
361
+ subscribeTrackToEndedEvent(state.room, track, state.pc);
355
362
  });
356
363
  });
357
364
  //start WebRTC negotiation
@@ -396,10 +403,8 @@ const stopStreams = function(state) {
396
403
  }
397
404
  }
398
405
 
399
- const subscribeTrackToEndedEvent = function(state, track) {
400
- let room = state.room;
401
- let pc = state.pc;
402
- track.addEventListener("ended", async function() {
406
+ const subscribeTrackToEndedEvent = function(room, track, pc) {
407
+ track.addEventListener("ended", function() {
403
408
  //track ended, see if we need to cleanup
404
409
  let negotiate = false;
405
410
  for (const sender of pc.getSenders()) {
@@ -412,7 +417,7 @@ const subscribeTrackToEndedEvent = function(state, track) {
412
417
  }
413
418
  if (negotiate) {
414
419
  //kickoff renegotiation
415
- state.waitFor(room.updateState(), MAX_AWAIT_MS);
420
+ room.updateState();
416
421
  }
417
422
  });
418
423
  };
@@ -115,7 +115,7 @@ const init = function() {
115
115
  */
116
116
  const connect = async function(state) {
117
117
  // Create peer connection
118
- const pc = new RTCPeerConnection();
118
+ let pc = new RTCPeerConnection();
119
119
  // Create a config to connect to SFU room
120
120
  const roomConfig = {
121
121
  // Server websocket URL
@@ -135,20 +135,28 @@ const connect = async function(state) {
135
135
  const session = await sfu.createRoom(roomConfig);
136
136
  // Set up session ending events
137
137
  session.on(constants.SFU_EVENT.DISCONNECTED, function() {
138
+ onStopClick(state);
138
139
  state.clear();
139
140
  onDisconnected(state);
140
141
  setStatus("playStatus", "DISCONNECTED", "green");
141
142
  }).on(constants.SFU_EVENT.FAILED, function(e) {
143
+ onStopClick(state);
142
144
  state.clear();
143
145
  onDisconnected(state);
144
146
  setStatus("playStatus", "FAILED", "red");
145
- setStatus("playErrorInfo", e.status + " " + e.statusText, "red");
147
+ if (e.status && e.statusText) {
148
+ setStatus("playErrorInfo", e.status + " " + e.statusText, "red");
149
+ } else if (e.type && e.info) {
150
+ setStatus("playErrorInfo", e.type + ": " + e.info, "red");
151
+ }
146
152
  });
147
153
  // Connected successfully
148
154
  state.set(pc, session, session.room());
149
155
  onConnected(state);
150
156
  setStatus("playStatus", "CONNECTING...", "black");
151
157
  } catch(e) {
158
+ state.clear();
159
+ onDisconnected(state);
152
160
  setStatus("playStatus", "FAILED", "red");
153
161
  setStatus("playErrorInfo", e, "red");
154
162
  }