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

RTCRtpSender: setStreams() method

· 52CE 我爱测

The RTCRtpSender method **`setStreams()`** associates the sender's RTCRtpSender.track with the specified MediaStream objects.

WebRTC

The RTCRtpSender method setStreams() associates the sender's RTCRtpSender.track with the specified MediaStream objects.

Syntax

setStreams()
setStreams(mediaStream1)
setStreams(mediaStream1, mediaStream2)
setStreams(mediaStream1, mediaStream2, /* …, */ mediaStreamN)

Parameters

  • mediaStreamN
    • : An arbitrary number of MediaStream objects specified as arguments, that identify the streams to which the RTCRtpSender's RTCRtpSender.track belongs. If this parameter isn't specified, no new streams will be associated with the track.

Return value

None (undefined).

Exceptions

  • InvalidStateError DOMException
    • : Thrown if the sender's connection is closed.

Description

setStreams() is purely additive. It doesn't remove the track from any streams; it adds it to new ones. If you specify streams to which the track already belongs, that stream is unaffected.

Once the track has been added to all of the streams, renegotiation of the connection will be triggered by the RTCPeerConnection.negotiationneeded_event event being dispatched to the RTCPeerConnection to which the sender belongs.

Examples

This example adds all of an RTCPeerConnection's tracks to the specified stream.

function addTracksToStream(stream) {
  let senders = pc.getSenders();

  senders.forEach((sender) => {
    if (sender.track && sender.transport.state === connected) {
      sender.setStreams(stream);
    }
  });
}

After calling the RTCPeerConnection method RTCPeerConnection.getSenders to get the list of the connection's senders, the addTracksToStream() function iterates over the list. For each sender, if the sender's track is non-null and its transport's state is connected, we call setStreams() to add the track to the stream specified.

Specifications

Browser compatibility

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

资料来源与适用范围

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

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

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