RTCRtpScriptTransform
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used 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.
Constructor
RTCRtpScriptTransform()
Creates a new instance of the
RTCRtpScriptTransform
object.
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 anRTCPeerConnection
track
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", }); received_video.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 |