5.1 RTCPeerConnection Interface Extensions
The RTP media API extends the RTCPeerConnection
interface as described below.
- WebIDL partial interface
RTCPeerConnection
{- sequence<
RTCRtpSender
>getSenders
();- sequence<
RTCRtpReceiver
>getReceivers
();- sequence<
RTCRtpTransceiver
>getTransceivers
();RTCRtpSender
addTrack
(MediaStreamTrack track, MediaStream... streams);- undefined
removeTrack
(RTCRtpSender
sender);RTCRtpTransceiver
addTransceiver
((MediaStreamTrack or DOMString) trackOrKind,- optional
RTCRtpTransceiverInit
init = {});- attribute EventHandler
ontrack
;- };
Attributes
ontrack
of type EventHandler
The event type of this event handler is track
.
Methods
getSenders
Returns a sequence of RTCRtpSender
objects representing the RTP senders that belong to non-stopped RTCRtpTransceiver
objects currently attached to this RTCPeerConnection
object.
When the getSenders
method is invoked, the user agent MUST return the result of executing the CollectSenders
algorithm.
We define the CollectSenders algorithm as follows:
- Let transceivers be the result of executing the
CollectTransceivers
algorithm. - Let senders be a new empty sequence.
- For each transceiver in transceivers,
- If transceiver.[[Stopped]] is
false
, add transceiver.[[Sender]] to senders.
- If transceiver.[[Stopped]] is
- Return senders.
getReceivers
Returns a sequence of RTCRtpReceiver
objects representing the RTP receivers that belong to non-stopped RTCRtpTransceiver
objects currently attached to this RTCPeerConnection
object.
When the getReceivers
method is invoked, the user agent MUST run the following steps:
- Let transceivers be the result of executing the
CollectTransceivers
algorithm. - Let receivers be a new empty sequence.
- For each transceiver in transceivers,
- If transceiver.[[Stopped]] is
false
, add transceiver.[[Receiver]] to receivers.
- If transceiver.[[Stopped]] is
- Return receivers.
getTransceivers
Returns a sequence of RTCRtpTransceiver
objects representing the RTP transceivers that are currently attached to this RTCPeerConnection
object.
The getTransceivers
method MUST return the result of executing the CollectTransceivers
algorithm.
We define the CollectTransceivers algorithm as follows:
- Let transceivers be a new sequence consisting of all
RTCRtpTransceiver
objects in thisRTCPeerConnection
object’s set of transceivers, in insertion order. - Return transceivers.
addTrack
Adds a new track to the RTCPeerConnection
, and indicates that it is contained in the specified MediaStream
s.
When the addTrack
method is invoked, the user agent MUST run the following steps:
Let connection be the
RTCPeerConnection
object on which this method was invoked.Let track be the
MediaStreamTrack
object indicated by the method’s first argument.Let kind be track.kind.
Let streams be a list of
MediaStream
objects constructed from the method’s remaining arguments, or an empty list if the method was called with a single argument.If connection.[[IsClosed]] is
true
, throw anInvalidStateError
.Let senders be the result of executing the
CollectSenders
algorithm. If anRTCRtpSender
for track already exists in senders, throw anInvalidAccessError
.The steps below describe how to determine if an existing sender can be reused. Doing so will cause future calls to
createOffer
andcreateAnswer
to mark the corresponding media description assendrecv
orsendonly
and add the MSID of the sender’s streams, as defined in [RFC8829] (section 5.2.2. and section 5.3.2.).If any
RTCRtpSender
object in senders matches all the following criteria, let sender be that object, ornull
otherwise:The sender’s track is null.
The transceiver kind of the
RTCRtpTransceiver
, associated with the sender, matches kind.The [[Stopping]] slot of the
RTCRtpTransceiver
associated with the sender isfalse
.The sender has never been used to send. More precisely, the [[CurrentDirection]] slot of the
RTCRtpTransceiver
associated with the sender has never had a value of “sendrecv
“ or “sendonly
“.
If sender is not
null
, run the following steps to use that sender:Set sender.[[SenderTrack]] to track.
Set sender.[[AssociatedMediaStreamIds]] to an empty set.
For each stream in streams, add stream.id to [[AssociatedMediaStreamIds]] if it’s not already there.
Let transceiver be the
RTCRtpTransceiver
associated with sender.If transceiver.[[Direction]] is “
recvonly
“, set transceiver.[[Direction]] to “sendrecv
“.If transceiver.[[Direction]] is “
inactive
“, set transceiver.[[Direction]] to “sendonly
“.
If sender is
null
, run the following steps:Create an RTCRtpSender with track, kind and streams, and let sender be the result.
Create an RTCRtpReceiver with kind, and let receiver be the result.
Create an RTCRtpTransceiver with sender, receiver and an
RTCRtpTransceiverDirection
value of “sendrecv
“, and let transceiver be the result.Add transceiver to connection’s set of transceivers.
A track could have contents that are inaccessible to the application. This can be due to anything that would make a track CORS cross-origin. These tracks can be supplied to the
addTrack
()
method, and have anRTCRtpSender
created for them, but content MUST NOT be transmitted. Silence (audio), black frames (video) or equivalently absent content is sent in place of track content.Note that this property can change over time.
Update the negotiation-needed flag for connection.
Return sender.
removeTrack
Stops sending media from sender. The RTCRtpSender
will still appear in getSenders
. Doing so will cause future calls to createOffer
to mark the media description for the corresponding transceiver as “recvonly
“ or “inactive
“, as defined in [RFC8829] (section 5.2.2.).
When the other peer stops sending a track in this manner, the track is removed from any remote MediaStream
s that were initially revealed in the track
event, and if the MediaStreamTrack
is not already muted, a mute
event is fired at the track.
Note
The same effect as removeTrack
()
can be achieved by setting the RTCRtpTransceiver
.direction
attribute of the corresponding transceiver and invoking RTCRtpSender
.replaceTrack
(null) on the sender. One minor difference is that replaceTrack
()
is asynchronous and removeTrack
()
is synchronous.
When the removeTrack
method is invoked, the user agent MUST run the following steps:
Let sender be the argument to
removeTrack
.Let connection be the
RTCPeerConnection
object on which the method was invoked.If connection.[[IsClosed]] is
true
, throw anInvalidStateError
.If sender was not created by connection, throw an
InvalidAccessError
.Let senders be the result of executing the
CollectSenders
algorithm.If sender is not in senders (which indicates its transceiver was stopped or removed due to setting a session description of
type
“rollback
“), then abort these steps.If sender.[[SenderTrack]] is null, abort these steps.
Set sender.[[SenderTrack]] to null.
Let transceiver be the
RTCRtpTransceiver
object corresponding to sender.If transceiver.[[Direction]] is “
sendrecv
“, set transceiver.[[Direction]] to “recvonly
“.If transceiver.[[Direction]] is “
sendonly
“, set transceiver.[[Direction]] to “inactive
“.Update the negotiation-needed flag for connection.
addTransceiver
Create a new RTCRtpTransceiver
and add it to the set of transceivers.
Adding a transceiver will cause future calls to createOffer
to add a media description for the corresponding transceiver, as defined in [RFC8829] (section 5.2.2.).
The initial value of mid
is null. Setting a session description may later change it to a non-null value.
The sendEncodings
argument can be used to specify the number of offered simulcast encodings, and optionally their RIDs and encoding parameters.
When this method is invoked, the user agent MUST run the following steps:
Let init be the second argument.
Let streams be init.
streams
.Let sendEncodings be init.
sendEncodings
.Let direction be init.
direction
.If the first argument is a string, let it be kind and run the following steps:
If kind is not a legal
MediaStreamTrack
kind
, throw aTypeError
.Let track be
null
.
If the first argument is a
MediaStreamTrack
, let it be track and let kind be track.kind.If connection.[[IsClosed]] is
true
, throw anInvalidStateError
.Validate sendEncodings by running the following steps:
Verify that each
rid
value in sendEncodings conforms to the grammar specified in Section 10 of [RFC8851]. If one of the RIDs does not meet these requirements, throw aTypeError
.If any
RTCRtpEncodingParameters
dictionary in sendEncodings contains a read-only parameter other thanrid
, throw anInvalidAccessError
.Verify that the value of each
scaleResolutionDownBy
member in sendEncodings that is defined is greater than or equal to 1.0. If one of thescaleResolutionDownBy
values does not meet this requirement, throw aRangeError
.Let maxN be the maximum number of total simultaneous encodings the user agent may support for this kind, at minimum
1
.This should be an optimistic number since the codec to be used is not known yet.If sendEncodings contains any encoding whose
scaleResolutionDownBy
attribute is defined, set any undefinedscaleResolutionDownBy
of the other encodings to 1.0.If the number of
RTCRtpEncodingParameters
stored in sendEncodings exceeds maxN, then trim sendEncodings from the tail until its length is maxN.If the
scaleResolutionDownBy
attribues of sendEncodings are still undefined, initialize each encoding’sscaleResolutionDownBy
to2^(length of sendEncodings - encoding index - 1)
. This results in smaller-to-larger resolutions where the last encoding has no scaling applied to it, e.g. 4:2:1 if the length is 3.If the number of
RTCRtpEncodingParameters
now stored in sendEncodings is1
, then remove anyrid
member from the lone entry.Note
Providing a single, default
RTCRtpEncodingParameters
in sendEncodings allows the application to subsequently set encoding parameters usingsetParameters
, even when simulcast isn’t used.
Create an RTCRtpSender with track, kind, streams and sendEncodings and let sender be the result.
If sendEncodings is set, then subsequent calls to
createOffer
will be configured to send multiple RTP encodings as defined in [RFC8829] (section 5.2.2. and section 5.2.1.). WhensetRemoteDescription
is called with a corresponding remote description that is able to receive multiple RTP encodings as defined in [RFC8829] (section 3.7.), theRTCRtpSender
may send multiple RTP encodings and the parameters retrieved via the transceiver’ssender
.getParameters
()
will reflect the encodings negotiated.Create an RTCRtpReceiver with kind and let receiver be the result.
Create an RTCRtpTransceiver with sender, receiver and direction, and let transceiver be the result.
Add transceiver to connection’s set of transceivers.
Update the negotiation-needed flag for connection.
Return transceiver.
- WebIDLdictionary
RTCRtpTransceiverInit
{RTCRtpTransceiverDirection
direction
= "sendrecv";- sequence<MediaStream>
streams
= [];- sequence<
RTCRtpEncodingParameters
>sendEncodings
= [];- };
Dictionary RTCRtpTransceiverInit
Members
direction
of type RTCRtpTransceiverDirection
, defaulting to “sendrecv
“
The direction of the RTCRtpTransceiver
.
streams
of type sequence<MediaStream
>
When the remote PeerConnection’s track event fires corresponding to the RTCRtpReceiver
being added, these are the streams that will be put in the event.
sendEncodings
of type sequence<RTCRtpEncodingParameters
>
A sequence containing parameters for sending RTP encodings of media.
RTCRtpTransceiverDirection Enumeration description | |
---|---|
sendrecv | The ‘s sender will offer to send RTP, and will send RTP if the remote peer accepts and sender.
() . [i]. is true for any value of i. The ‘s will offer to receive RTP, and will receive RTP if the remote peer accepts. |
sendonly | The ‘s sender will offer to send RTP, and will send RTP if the remote peer accepts and sender.
() . [i]. is true for any value of i. The ‘s will not offer to receive RTP, and will not receive RTP. |
recvonly | The ‘s will not offer to send RTP, and will not send RTP. The ‘s will offer to receive RTP, and will receive RTP if the remote peer accepts. |
inactive | The ‘s will not offer to send RTP, and will not send RTP. The ‘s will not offer to receive RTP, and will not receive RTP. |
stopped | The will neither send nor receive RTP. It will generate a zero port in the offer. In answers, its will not offer to send RTP, and its will not offer to receive RTP. This is a terminal state. |
5.1.1 Processing Remote MediaStreamTracks
An application can reject incoming media descriptions by setting the transceiver’s direction to either “inactive
“ to turn off both directions temporarily, or to “sendonly
“ to reject only the incoming side. To permanently reject an m-line in a manner that makes it available for reuse, the application would need to call RTCRtpTransceiver
.stop
()
and subsequently initiate negotiation from its end.
To process remote tracks given an RTCRtpTransceiver
transceiver, direction, msids, addList, removeList, and trackEventInits, run the following steps:
Set the associated remote streams with transceiver.[[Receiver]], msids, addList, and removeList.
If direction is “
sendrecv
“ or “recvonly
“ and transceiver.[[FiredDirection]] is neither “sendrecv
“ nor “recvonly
“, or the previous step increased the length of addList, process the addition of a remote track with transceiver and trackEventInits.If direction is “
sendonly
“ or “inactive
“, set transceiver.[[Receptive]] tofalse
.If direction is “
sendonly
“ or “inactive
“, and transceiver.[[FiredDirection]] is either “sendrecv
“ or “recvonly
“, process the removal of a remote track for the media description, with transceiver and muteTracks.Set transceiver.[[FiredDirection]] to direction.
To process the addition of a remote track given an RTCRtpTransceiver
transceiver and trackEventInits, run the following steps:
Let receiver be transceiver.[[Receiver]].
Let track be receiver.[[ReceiverTrack]].
Let streams be receiver.[[AssociatedRemoteMediaStreams]].
Create a new
RTCTrackEventInit
dictionary with receiver, track, streams and transceiver as members and add it to trackEventInits.
To process the removal of a remote track with an RTCRtpTransceiver
transceiver and muteTracks, run the following steps:
Let receiver be transceiver.[[Receiver]].
Let track be receiver.[[ReceiverTrack]].
If track.muted is
false
, add track to muteTracks.
To set the associated remote streams given RTCRtpReceiver
receiver, msids, addList, and removeList, run the following steps:
Let connection be the
RTCPeerConnection
object associated with receiver.For each MSID in msids, unless a
MediaStream
object has previously been created with thatid
for this connection, create aMediaStream
object with thatid
.Let streams be a list of the
MediaStream
objects created for this connection with theid
s corresponding to msids.Let track be receiver.[[ReceiverTrack]].
For each stream in receiver.[[AssociatedRemoteMediaStreams]] that is not present in streams, add stream and track as a pair to removeList.
For each stream in streams that is not present in receiver.[[AssociatedRemoteMediaStreams]], add stream and track as a pair to addList.
Set receiver.[[AssociatedRemoteMediaStreams]] to streams.