Skip to content

RTCRtpSender.addTrack() incorrectly requires a stream #4071

Open
@hills

Description

@hills

The compiler's definition of this function requires one or more streams, and differs from MDN and W3C references which specify zero or more.

Code using addTrack(track) without a stream behaved as I expected, matching the docs in Chromium 113.0.5659.0.

I drafted a patch below, but this is untested as I don't have a build process or CLA. (For the avoidance of any doubt, I declare this patch in the public domain)

I was unable to find a way to override this in my project's @externs, the only workaround I came up with is to abandon all checking on this function entirely. I'd be interested if there's a better workaround:

pc['addTrack'](track);    /* workaround */

/** @type {!MediaStreamTrack} */
let track;

const pc = new RTCPeerConnection({iceServers: []});
pc.addTrack(track);
$ java -jar closure-compiler-v20230103.jar -O ADVANCED ms.js
ms.js:5:0: WARNING - [JSC_WRONG_ARGUMENT_COUNT] Function RTCPeerConnection.prototype.addTrack: called with 1 argument(s). Function requires at least 2 argument(s).
  5| pc.addTrack(track);
     ^^^^^^^^^^^^^^^^^^

0 error(s), 1 warning(s), 100.0% typed
(new RTCPeerConnection({iceServers:[]})).addTrack(void 0);
diff --git a/externs/browser/w3c_rtc.js b/externs/browser/w3c_rtc.js
index 4b0955cb4..9eff061ad 100644
--- a/externs/browser/w3c_rtc.js
+++ b/externs/browser/w3c_rtc.js
@@ -3440,11 +3440,10 @@ RTCPeerConnection.prototype.removeStream = function(stream) {};

 /**
  * @param {!MediaStreamTrack} track
- * @param {!MediaStream} stream
- * @param {...MediaStream} var_args Additional streams.
+ * @param {...MediaStream} var_args Optional streams.
  * @return {!RTCRtpSender}
  */
-RTCPeerConnection.prototype.addTrack = function(track, stream, var_args) {};
+RTCPeerConnection.prototype.addTrack = function(track, var_args) {};

 /**
  * @param {!MediaStreamTrack|string} trackOrKind

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions