RTCPeerConnection
property
ontrack
是
EventHandler
which specifies a function to be called when the
track
event occurs, indicating that a track has been added to the
RTCPeerConnection
.
The function receives as input the event object, of type
RTCTrackEvent
; this event is sent when a new incoming
MediaStreamTrack
has been created and associated with an
RTCRtpReceiver
object which has been added to the set of receivers on connection.
RTCPeerConnection.ontrack = eventHandler;
Set
ontrack
to be a function you provide that accepts as input a
RTCTrackEvent
object describing the new track and how it's being used. This information includes the
MediaStreamTrack
object representing the new track, the
RTCRtpReceiver
and
RTCRtpTransceiver
, and a list of
MediaStream
objects which indicates which stream or streams the track is part of..
This example, taken from the code for the article
Signaling and video calling
, connects the incoming track to the
<video>
element which will be used to display the incoming video.
pc.ontrack = function(event) {
document.getElementById("received_video").srcObject = event.streams[0];
document.getElementById("hangup-button").disabled = false;
};
The first line of our
ontrack
event handler takes the first stream in the incoming track and sets the
srcobject
attribute to that. This connects that stream of video to the element so that it begins to be presented to the user. The second line of code simply enables a "hang up" button, which the user can use to disconnect from the call.
| 规范 | 状态 | 注释 |
|---|---|---|
|
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'RTCPeerConnection.ontrack' in that specification. |
候选推荐 | 最初的规范。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
ontrack
|
Chrome 64 | Edge ≤18 | Firefox 22 | IE No |
Opera
43
|
Safari 11 | WebView Android 64 | Chrome Android 64 | Firefox Android 44 |
Opera Android
43
|
Safari iOS Yes | Samsung Internet Android 6.0 |
完整支持
不支持
见实现注意事项。
track
event and its type,
RTCTrackEvent
.
RTCPeerConnection
canTrickleIceCandidates
connectionState
currentLocalDescription
currentRemoteDescription
getDefaultIceServers()
iceConnectionState
iceGatheringState
localDescription
onaddstream
onconnectionstatechange
ondatachannel
onicecandidate
oniceconnectionstatechange
onicegatheringstatechange
onidentityresult
onidpassertionerror
onidpvalidationerror
onnegotiationneeded
onpeeridentity
onremovestream
onsignalingstatechange
ontrack
peerIdentity
pendingLocalDescription
pendingRemoteDescription
remoteDescription
sctp
signalingState
addIceCandidate()
addStream()
addTrack()
close()
createAnswer()
createDataChannel()
createOffer()
generateCertificate()
getConfiguration()
getIdentityAssertion()
getReceivers()
getSenders()
getStats()
getStreamById()
getTransceivers()
removeStream()
removeTrack()
restartIce()
setConfiguration()
setIdentityProvider()
setLocalDescription()
setRemoteDescription()
MediaDevices.getUserMedia()
Navigator.mediaDevices
RTCCertificate
RTCDTMFSender
RTCDTMFToneChangeEvent
RTCDataChannel
RTCDataChannelEvent
RTCDtlsTransport
RTCErrorEvent
RTCIceCandidate
RTCIceTransport
RTCPeerConnectionIceErrorEvent
RTCPeerConnectionIceEvent
RTCRtpReceiver
RTCRtpSender
RTCRtpTransceiver
RTCSctpTransport
RTCSessionDescription
RTCStatsEvent
RTCStatsReport
RTCTrackEvent