diff --git a/README.md b/README.md index e1203d76..72ddb190 100644 --- a/README.md +++ b/README.md @@ -279,7 +279,8 @@ If `opts` is specified, then the default options (shown below) will be overridde trickle: true, allowHalfTrickle: false, wrtc: {}, // RTCPeerConnection/RTCSessionDescription/RTCIceCandidate - objectMode: false + objectMode: false, + preferredCodecs: [], } ``` @@ -301,6 +302,7 @@ The options do the following: - [`RTCIceCandidate`](https://www.w3.org/TR/webrtc/#dom-rtcicecandidate) - `objectMode` - set to `true` to create the stream in [Object Mode](https://nodejs.org/api/stream.html#stream_object_mode). In this mode, incoming string data is not automatically converted to `Buffer` objects. +- `preferredCodecs` - set preferred codecs list ### `peer.signal(data)` diff --git a/index.js b/index.js index 0ee8051a..427b225b 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,9 @@ const MAX_BUFFERED_AMOUNT = 64 * 1024 const ICECOMPLETE_TIMEOUT = 5 * 1000 const CHANNEL_CLOSING_TIMEOUT = 5 * 1000 +const supportsSetCodecPreferences = window.RTCRtpTransceiver && + 'setCodecPreferences' in window.RTCRtpTransceiver.prototype + // HACK: Filter trickle lines when trickle is disabled #354 function filterTrickle (sdp) { return sdp.replace(/a=ice-options:trickle\s\n/g, '') @@ -51,6 +54,7 @@ class Peer extends stream.Duplex { this.trickle = opts.trickle !== undefined ? opts.trickle : true this.allowHalfTrickle = opts.allowHalfTrickle !== undefined ? opts.allowHalfTrickle : false this.iceCompleteTimeout = opts.iceCompleteTimeout || ICECOMPLETE_TIMEOUT + this.preferredCodecs = opts.preferredCodecs || []; this.destroyed = false this.destroying = false @@ -286,6 +290,14 @@ class Peer extends stream.Duplex { stream.getTracks().forEach(track => { this.addTrack(track, stream) }) + + if (supportsSetCodecPreferences && this.preferredCodecs.length > 0) { + const streamVideoTracks = stream.getVideoTracks() + const transceivers = this._pc.getTransceivers().filter(t => t.sender && streamVideoTracks.includes(t.sender.track)) + transceivers.forEach(transceiver => { + transceiver.setCodecPreferences(this.preferredCodecs) + }) + } } /**