大网数据广告活动
Web 标准与前端开发

RTCRtpSender: transform property

· 52CE 我爱测

The **`transform`** property of the RTCRtpSender 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

WebRTC

The transform property of the RTCRtpSender 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 an RTCRtpScriptTransform and its associated Worker. If the transform is set synchronously immediately after creating the RTCRtpSender it will receive the first full frame generated by the sender's encoder.

Value

A RTCRtpScriptTransform, or null 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 topic Using WebRTC Encoded Transforms.

The code assumes that there is an RTCPeerConnection called peerConnection that is already connected to a remote peer. It first gets a MediaStreamTrack, using MediaDevices/getUserMedia to get a video MediaStream from a media device, and then the MediaStream.getTracks() method to get the first MediaStreamTrack in the stream.

The track is added to the peer connection using RTCPeerConnection/addTrack(). This returns a new RTCRtpSender 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 an RTCRtpScriptTransform and assign it to the sender's transform property. As the transform is constructed immediately after creation of the RTCRtpSender, 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 calling addTrack() the transform will get the first encoded frame that is sent.

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。