google / google/closure-compiler

RTCRtpSender.addTrack() incorrectly requires a stream

Open
#4,071 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

The compiler's definition of this function requires one or more streams, and differs from [MDN](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/addTrack) and [W3C](https://www.w3.org/TR/webrtc/#ref-for-dom-rtcpeerconnection-addtrack-8) 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
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.