@subiz/wsclient 4.0.10 → 4.0.12

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 (2) hide show
  1. package/package.json +1 -1
  2. package/webrtc.js +29 -17
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@subiz/wsclient",
3
- "version": "4.0.10",
3
+ "version": "4.0.12",
4
4
  "main": "realtime.js",
5
5
  "scripts": {
6
6
  "test": "node realtime.test.js"
package/webrtc.js CHANGED
@@ -222,7 +222,17 @@ function WebRTCConn(options) {
222
222
  readyToSendCandidateResolve()
223
223
  if (pendingStream && pendingStream != currentStream) {
224
224
  currentStream = pendingStream
225
- currentStream.getTracks().forEach((track) => peer.addTrack(track, currentStream))
225
+ currentStream.getTracks().forEach((track) => {
226
+ let replaced = false
227
+ peer.getSenders().forEach((sender) => {
228
+ if (!sender.track) return
229
+ if (sender.track.kind != track.kind) return
230
+ sender.replaceTrack(track)
231
+ replaced = true
232
+ })
233
+
234
+ if (!replaced) peer.addTrack(track, currentStream)
235
+ })
226
236
  }
227
237
  pendingStream = undefined
228
238
  return peer.createAnswer()
@@ -257,7 +267,7 @@ function WebRTCConn(options) {
257
267
  }
258
268
  })
259
269
 
260
- this.makeCall = (number, fromnumber, stream, callid) => {
270
+ this.makeCall = (number, fromnumber, streamPm, callid) => {
261
271
  if (!callid) callid = 'webcall-' + randomString(30)
262
272
  let now = env.Date.now()
263
273
  let call = {
@@ -273,26 +283,28 @@ function WebRTCConn(options) {
273
283
  dialingRequest[callid] = call
274
284
  publish({type: 'call_ringing', data: {call_info: {call_id: callid}}}) // fake dialing
275
285
  return new env.Promise((rs) => {
276
- this.joinCall(callid, stream, (err) => {
277
- if (err) {
278
- delete dialingRequest[callid]
279
- endRequest[callid] = env.Date.now()
280
- publish({type: 'call_ended', data: {call_info: {call_id: callid}}})
281
- return rs({error: err})
282
- }
283
-
284
- let url = `${apiUrl}call?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&number=${number}&from_number=${fromnumber}`
285
- callAPI('post', url, undefined, (body, code) => {
286
- if (code != 200) {
286
+ streamPm.then(stream => {
287
+ this.joinCall(callid, stream, (err) => {
288
+ if (err) {
287
289
  delete dialingRequest[callid]
288
290
  endRequest[callid] = env.Date.now()
289
291
  publish({type: 'call_ended', data: {call_info: {call_id: callid}}})
290
292
  return rs({error: err})
291
293
  }
292
- body = parseJSON(body)
293
- if (body) activeCalls[callid] = body
294
- delete dialingRequest[callid]
295
- rs({body})
294
+
295
+ let url = `${apiUrl}call?x-access-token=${access_token}&connection_id=${connId}&call_id=${callid}&number=${number}&from_number=${fromnumber}`
296
+ callAPI('post', url, undefined, (body, code) => {
297
+ if (code != 200) {
298
+ delete dialingRequest[callid]
299
+ endRequest[callid] = env.Date.now()
300
+ publish({type: 'call_ended', data: {call_info: {call_id: callid}}})
301
+ return rs({error: err})
302
+ }
303
+ body = parseJSON(body)
304
+ if (body) activeCalls[callid] = body
305
+ delete dialingRequest[callid]
306
+ rs({body})
307
+ })
296
308
  })
297
309
  })
298
310
  })