This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The RTCPeerConnection.ontrack property is an EventHandler which specifies a function to be called when the track event occurs on an RTCPeerConnection interface. 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.


Specification Status Comment
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'RTCPeerConnection.ontrack' in that specification.
Candidate Recommendation Initial specification.

Browser compatibility

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support No support 46 (46)[1] No support ? ?
Feature Android Webview Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support No support No support 46 (46)[1] No support ? ?

[1] Introduced in Firefox 45 as onaddtrack; renamed to ontrack in Firefox 46

See also

Document Tags and Contributors

 Contributors to this page: matsub, jpmedley, Sheppy
 Last updated by: matsub,