@wireapp/avs 10.6.8 → 10.6.9

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/dist/avs_pc.js CHANGED
@@ -443,6 +443,148 @@ function setAudioJitterBufferMin(pc, receiver) {
443
443
  pc_log(LOG_LEVEL_WARN, "setAudioJitterBufferMin failed: " + err, err);
444
444
  }
445
445
  }
446
+ function ccallAudioAdaptMaxBps() {
447
+ return em_module.ccall("pc_audio_adapt_max_bps", "number", [], []);
448
+ }
449
+ function ccallAudioPtime() {
450
+ return em_module.ccall("pc_audio_ptime", "number", [], []);
451
+ }
452
+ var AUDIO_RISE_HOLD_MS = 3000;
453
+ var AUDIO_OPUS_MIN_BPS = 6000;
454
+ var AUDIO_RED_HDR_BYTES = 5; /* RED headers of a packet with one copy */
455
+ var AUDIO_SRTP_UDP_BYTES = 18; /* SRTP auth tag 10 + UDP 8 */
456
+ function perPacketBps(bytes, frameMs) {
457
+ return bytes * 8 * 1000 / frameMs;
458
+ }
459
+ /* The native clients fit RED into the bandwidth estimate by giving opus its
460
+ * share of it, and hold rises of the opus target for 3 s (peerflow
461
+ * AdaptiveRedEncoder, LossFloorEncoder). The browser can do neither: RED,
462
+ * when negotiated, is on for the whole call with its copy on top of the
463
+ * bitrate allocated to the audio, and with adaptivePtime (Chromium) that
464
+ * allocation doesn't go below 16 kbps, so on a slow link it sends twice the
465
+ * estimate or more.
466
+ *
467
+ * The lever left is encodings[0].maxBitrate, which in libwebrtc sets both
468
+ * ends of the audio allocation: the minimum to it when below 16 kbps, the
469
+ * maximum to it plus the headers at the shortest frame length. So on each
470
+ * stats tick (1 s) this sets maxBitrate so that opus gets no more than its
471
+ * share at the current frame length: the estimate less the headers, halved
472
+ * with RED. At long frames that would need less than opus's 6 kbps, so the
473
+ * share then holds only where the estimate leaves room for it; below, what
474
+ * it gains is the lower minimum (6 kbps instead of 16). A drop goes through
475
+ * at once, a rise after it has been wanted for 3 s, to the lowest value of
476
+ * that time. Needs the estimate (availableOutgoingBitrate, not in Firefox);
477
+ * off while sending video (which shares the estimate), with plain audio and
478
+ * with adaptive RED off (pc_audio_adapt_max_bps).
479
+ */
480
+ function adaptAudioBitrate(pc, stats) {
481
+ var rtc = pc.rtc;
482
+ var ar = pc.audioRate;
483
+ if (!rtc || ar.busy)
484
+ return;
485
+ var sender = rtc.getSenders().find(function (s) { return !!s.track && s.track.kind === 'audio'; });
486
+ if (!sender)
487
+ return;
488
+ var maxBps = ccallAudioAdaptMaxBps();
489
+ if (!maxBps || pc.sending_video) {
490
+ if (ar.maxBitrate)
491
+ setAudioMaxBitrate(pc, sender, 0, pc.sending_video ? 'video' : 'off');
492
+ ar.riseSince = 0;
493
+ return;
494
+ }
495
+ var out = null;
496
+ var pair = null;
497
+ stats.forEach(function (s) {
498
+ if (s.type === 'outbound-rtp' && s.kind === 'audio')
499
+ out = s;
500
+ else if (s.type === 'transport' && s.selectedCandidatePairId)
501
+ pair = stats.get(s.selectedCandidatePairId);
502
+ });
503
+ if (!out || !pair || !pair.availableOutgoingBitrate)
504
+ return;
505
+ var dt = out.timestamp - ar.ts;
506
+ var pkts = out.packetsSent - ar.pkts;
507
+ var hdrBytes = (out.headerBytesSent || 0) - ar.hdrBytes;
508
+ var first = ar.ts === 0;
509
+ ar.ts = out.timestamp;
510
+ ar.pkts = out.packetsSent;
511
+ ar.hdrBytes = out.headerBytesSent || 0;
512
+ if (first || dt <= 0 || pkts <= 0)
513
+ return;
514
+ /* Headers per packet as libwebrtc counts them for the allocation: RTP
515
+ * with its extensions, SRTP, UDP, IP, and TURN's on a relay.
516
+ */
517
+ var local = stats.get(pair.localCandidateId);
518
+ var ipv6 = !!(local && local.address && local.address.indexOf(':') >= 0);
519
+ var relay = 0;
520
+ if (local && local.candidateType === 'relay')
521
+ relay = local.relayProtocol === 'udp' ? 4 : 16;
522
+ var rtpHdr = hdrBytes > 0 ? hdrBytes / pkts : 24;
523
+ var hdr = rtpHdr + AUDIO_SRTP_UDP_BYTES + (ipv6 ? 40 : 20) + relay;
524
+ var codec = out.codecId ? stats.get(out.codecId) : null;
525
+ var red = !!(codec && /\/red$/i.test(codec.mimeType));
526
+ var enc = sender.getParameters().encodings[0];
527
+ var adaptive = !!(enc && enc.adaptivePtime);
528
+ var frameMs = Math.min(120, Math.max(10, Math.round(dt / pkts / 10) * 10));
529
+ var minFrameMs = adaptive ? Math.max(ccallAudioPtime(), 20) : frameMs;
530
+ var bwe = pair.availableOutgoingBitrate;
531
+ var share = bwe - perPacketBps(hdr + (red ? AUDIO_RED_HDR_BYTES : 0), frameMs);
532
+ if (red)
533
+ share /= 2;
534
+ /* The allocation's maximum has the headers at the shortest frame */
535
+ var want = share - (perPacketBps(hdr, minFrameMs) - perPacketBps(hdr, frameMs));
536
+ want = Math.max(Math.floor(want / 1000) * 1000, AUDIO_OPUS_MIN_BPS);
537
+ if (want >= maxBps)
538
+ want = 0;
539
+ var cur = ar.maxBitrate || Infinity;
540
+ var w = want || Infinity;
541
+ var why = "bwe ".concat((bwe / 1000).toFixed(1), " kbps, ").concat(frameMs, " ms frames, ")
542
+ + "".concat(Math.round(hdr), " B headers, red ").concat(red ? 'on' : 'off');
543
+ if (w < cur) {
544
+ ar.riseSince = 0;
545
+ setAudioMaxBitrate(pc, sender, want, why);
546
+ }
547
+ else if (w > cur) {
548
+ if (!ar.riseSince) {
549
+ ar.riseSince = out.timestamp;
550
+ ar.riseTo = w;
551
+ }
552
+ else {
553
+ ar.riseTo = Math.min(ar.riseTo, w);
554
+ }
555
+ if (out.timestamp - ar.riseSince >= AUDIO_RISE_HOLD_MS) {
556
+ ar.riseSince = 0;
557
+ setAudioMaxBitrate(pc, sender, ar.riseTo === Infinity ? 0 : ar.riseTo, why);
558
+ }
559
+ }
560
+ else {
561
+ ar.riseSince = 0;
562
+ }
563
+ }
564
+ function setAudioMaxBitrate(pc, sender, bps, why) {
565
+ var ar = pc.audioRate;
566
+ var params = sender.getParameters();
567
+ var enc = params.encodings && params.encodings[0];
568
+ var kbps = function (b) { return b ? "".concat(b / 1000, " kbps") : 'none'; };
569
+ if (!enc)
570
+ return;
571
+ if (bps)
572
+ enc.maxBitrate = bps;
573
+ else
574
+ delete enc.maxBitrate;
575
+ ar.busy = true;
576
+ sender.setParameters(params)
577
+ .then(function () {
578
+ pc_log(LOG_LEVEL_INFO, "adaptAudioBitrate: self=".concat(pc.self, " maxBitrate ")
579
+ + "".concat(kbps(bps), " (was ").concat(kbps(ar.maxBitrate), "); ").concat(why));
580
+ ar.maxBitrate = bps;
581
+ ar.busy = false;
582
+ }, function (e) {
583
+ /* Keep busy: don't try again on this connection */
584
+ pc_log(LOG_LEVEL_WARN, "adaptAudioBitrate: self=".concat(pc.self, " maxBitrate ")
585
+ + "".concat(kbps(bps), " failed: ").concat(e));
586
+ });
587
+ }
446
588
  function ccallSignallingHandler(pc, state) {
447
589
  em_module.ccall("pc_signalling_handler", null, ["number", "number"], [pc.self, state]);
448
590
  }
@@ -633,7 +775,16 @@ function pc_New(self, convidPtr, audioIvPtr, videoIvPtr, ivlen) {
633
775
  rtt: 0
634
776
  },
635
777
  streams: {},
636
- gatherTimer: null
778
+ gatherTimer: null,
779
+ audioRate: {
780
+ ts: 0,
781
+ pkts: 0,
782
+ hdrBytes: 0,
783
+ maxBitrate: 0,
784
+ riseSince: 0,
785
+ riseTo: 0,
786
+ busy: false
787
+ }
637
788
  };
638
789
  worker.postMessage({ op: 'create', self: pc.self, iva: iva8, ivv: ivv8 });
639
790
  var hnd = connectionsStore.storePeerConnection(pc);
@@ -1545,6 +1696,12 @@ function pc_GetLocalStats(hnd) {
1545
1696
  });
1546
1697
  rtc.getStats()
1547
1698
  .then(function (stats) {
1699
+ try {
1700
+ adaptAudioBitrate(pc, stats);
1701
+ }
1702
+ catch (err) {
1703
+ pc_log(LOG_LEVEL_WARN, "adaptAudioBitrate: self=".concat(pc.self, " failed: ").concat(err), err);
1704
+ }
1548
1705
  // !This does not work --> let report_json = JSON.stringify(stats);
1549
1706
  // We need to create json from individual items
1550
1707
  var report = '[';
@@ -181,6 +181,7 @@ export declare class Wcall {
181
181
  setProxy(host: string, port: number): number;
182
182
  setAudioParams(ptime: number, max_bitrate: number): void;
183
183
  setLossParams(red: number, fec_loss_pct: number, max_bitrate: number, jb_min_ms: number): void;
184
+ setAdaptiveRed(enable: number): void;
184
185
  setStatsLog(enable: number): void;
185
186
  setReqClientsHandler(wuser: number, reqch: WcallReqClientsHandler): void;
186
187
  setClientsForConv(wuser: number, convid: string, json: string): number;
package/dist/avs_wcall.js CHANGED
@@ -643,6 +643,9 @@ var Wcall = /** @class */ (function () {
643
643
  Wcall.prototype.setLossParams = function (red, fec_loss_pct, max_bitrate, jb_min_ms) {
644
644
  this.em_module.ccall("wcall_set_loss_params", "null", ["number", "number", "number", "number"], [red, fec_loss_pct, max_bitrate, jb_min_ms]);
645
645
  };
646
+ Wcall.prototype.setAdaptiveRed = function (enable) {
647
+ this.em_module.ccall("wcall_set_adaptive_red", "null", ["number"], [enable]);
648
+ };
646
649
  Wcall.prototype.setStatsLog = function (enable) {
647
650
  this.em_module.ccall("wcall_set_stats_log", "null", ["number"], [enable]);
648
651
  };
package/package.json CHANGED
@@ -27,5 +27,5 @@
27
27
  "prepack": "prettier -w src/avs_wcall.ts && tsc && cp src/*.js src/*.d.ts dist/",
28
28
  "test": "karma start"
29
29
  },
30
- "version": "10.6.8"
30
+ "version": "10.6.9"
31
31
  }