RTCRtpSender: transform property
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Thetransform
property of theRTCRtpSender
object is used to insert a transform stream (TransformStream
) running in a worker thread into the sender pipeline.This allows stream transforms to be applied to encoded video and audio frames after they are output by a codec, and before they are sent.
The transform that is to be added is defined using anRTCRtpScriptTransform
and its associatedWorker
.If the transform is set synchronously immediately after creating theRTCRtpSender
it will receive the first full frame generated by the sender's encoder.
Value
ARTCRtpScriptTransform
, ornull
if the sender has no associated transform stream.
Example
This example shows how you might stream video from a user's webcam over WebRTC, adding a WebRTC encoded transform to modify the outgoing streams.Note that this is part of a larger example in the guide topicUsing WebRTC Encoded Transforms.
The code assumes that there is anRTCPeerConnection
calledpeerConnection
that is already connected to a remote peer.It first gets aMediaStreamTrack
, usinggetUserMedia()
to get a videoMediaStream
from a media device, and then theMediaStream.getTracks()
method to get the firstMediaStreamTrack
in the stream.
The track is added to the peer connection usingaddTrack()
.This returns a newRTCRtpSender
that will be used to send it.
const mediaStream = await navigator.mediaDevices.getUserMedia({ video: true });const [track] = mediaStream.getTracks();const videoSender = peerConnection.addTrack(track, mediaStream);
The code above sets up the connection and starts sending the track.To add a transform stream into the pipeline we need to construct anRTCRtpScriptTransform
and assign it to the sender'stransform
property.As the transform is constructed immediately after creation of theRTCRtpSender
, it will receive the first frame generated by the sender's encoder, before it is sent.
const worker = new Worker("worker.js");videoSender.transform = new RTCRtpScriptTransform(worker, { name: "senderTransform",});
Note that you can add the transform at any time.However by adding it immediately after callingaddTrack()
the transform will get the first encoded frame that is sent.
Specifications
Specification |
---|
WebRTC Encoded Transform # dom-rtcrtpsender-transform |