RTCRtpScriptTransform
Baseline 2025Newly available
Since October 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
TheRTCRtpScriptTransform interface of theWebRTC API is used to insert aWebRTC Encoded Transform (aTransformStream running in a worker thread) into the WebRTC sender and receiver pipelines.
In this article
Constructor
RTCRtpScriptTransform()Creates a new instance of the
RTCRtpScriptTransformobject.
Instance properties
None.
Instance methods
None.
Description
RTCRtpScriptTransform instances are constructed with aWorker, in which the transform stream code will run, along with an (optional)options object and array oftransferrable object that will be passed to the worker.They are then added into incoming and outgoing RTC pipelines by assigning them toRTCRtpReceiver.transform andRTCRtpSender.transform, respectively.
On construction of this object, and whenever an encoded frame arrives, thertctransform event is fired on the worker global object.The event'stransformer property is aRTCRtpScriptTransformer, the worker-side counterpart to the main-threadRTCRtpScriptTransform.This hasreadable (ReadableStream) andwritable (WritableStream) properties that have been shared from the main threadRTCRtpScriptTransform — where they are not public.If the correspondingRTCRtpScriptTransform is used with anRTCRtpReceiver, then thereadable queues incoming encoded audio or video frames from the packetizer.If it is used withRTCRtpSender thenreadable contains frames coming from a codec.
The worker threadrtctransform event handler defines apipe chain.This pipes encoded frames fromevent.transformer.readable, through aTransformStream which defines the transformation function, through toevent.transformer.writable.Theevent.transformer also has theoptions object passed from theRTCRtpScriptTransform constructor (if defined) that can be used to determine the source of the event, and hence the specificTransformStream to add to the chain.
Examples
Note that these examples show howRTCRtpScriptTransform is defined and used.Worker thread transform code is covered as part of the more complete example inUsing WebRTC Encoded Transforms.
Adding a transform for outgoing frames
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.The code assumes that there is anRTCPeerConnection calledpeerConnection that is already connected to a remote peer.
First we 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() and sent.TheaddTrack() method returns theRTCRtpSender that is being used to send the track.
// Get Video stream and MediaTrackconst stream = await navigator.mediaDevices.getUserMedia({ video: true });const [track] = stream.getTracks();const videoSender = peerConnection.addTrack(track, stream);AnRTCRtpScriptTransform is then constructed taking a worker script, which defines the transform, and an optional object that can be used to pass arbitrary messages to the worker (in this case we've used aname property with value "senderTransform" to tell the worker that this transform will be added to the outbound stream).We then add the transform to the sender by assigning it to theRTCRtpSender.transform property.
// Create a worker containing a TransformStreamconst 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.
Adding a transform for incoming frames
This example shows how you add a WebRTC encoded transform to modify an incoming stream.The code assumes that there is anRTCPeerConnection calledpeerConnection that is already connected to a remote peer.
First we add anRTCPeerConnectiontrack event handler to catch the event when a new track is streamed.Within the handler we construct anRTCRtpScriptTransform and add it toevent.receiver.transform (event.receiver is aRTCRtpReceiver).As in the previous example, the constructor takes an object withname property: but here we usereceiverTransform as the value to tell the worker that frames are incoming from the packetizer.
peerConnection.ontrack = (event) => { const worker = new Worker("worker.js"); event.receiver.transform = new RTCRtpScriptTransform(worker, { name: "receiverTransform", }); receivedVideo.srcObject = event.streams[0];};Note again that you can add the transform stream at any time.However by adding it in thetrack event handler ensures that the transform stream will get the first encoded frame for the track.
Specifications
| Specification |
|---|
| WebRTC Encoded Transform> # rtcrtpscripttransform> |