Kartal
Kartal

Reputation: 444

Accessing RTP Timestamps from WebRTC

I'm currently working on a WebRTC project. We are using Janus Gateway for WebRTC server side recording/transceiving etc.

For a canvas related synchronization, I need to access RTP timestamps of the received video track. I have been searching for a method extensively for a while now.

I've tried getStats function of peer connection, but the timestamps of the stats are the acquisition times. Not packet timestamps.

Also, in W3's standard it says specifically:

The timestamp of type DOMHighResTimeStamp [HIGHRES-TIME], indicating the most recent time of playout of an RTP packet containing the source. The timestamp is defined in [ HIGHRES-TIME] and corresponds to a local clock.

Which can be accessed via getContributingSources() function from RTCRtpReceiver object. But I've noticed, that function returns an empty array.

To reproduce:

https://webrtc.github.io/samples/src/content/peerconnection/pc1/

After you press Start and Call your camera feed and the peer connection should appear. After that open up the console and write:

const receivers = pc2.getReceivers();
receivers.forEach(receiver => {
    console.log(receiver.getContributingSources());
});

My question is, how can I access the received RTP packet's timestamp? Or the last received video RTP timestamp?

Thanks in advance.

Upvotes: 5

Views: 5446

Answers (2)

Christian Fritz
Christian Fritz

Reputation: 21354

As pointed out by @ksridhar in the comments, you can now access the timestamp through getSynchronizationSources of the receiver:

connection.getReceivers().forEach(receiver => {
  console.log(receiver.getParameters());
  receiver.getSynchronizationSources().forEach(ssrc =>
    console.log(ssrc.rtpTimestamp)); // compare to clockrate
});

Note that this is not really a (date-) time stamp. It seems to be samples of the clock rate. For instance, with a video payload that has a clock rate of 90,000, this rtpTimestamp will increase by 90,000 every second when everything is flowing smoothly (no lag). You can extract the clockRate from the codecs returned by receiver.getParameters().

For more details see https://w3c.github.io/webrtc-pc/#dom-rtcrtpcontributingsource-rtptimestamp and the RFC section it refers to.

Upvotes: 2

ceztko
ceztko

Reputation: 15207

EDIT: Chrome supports standardized stats since version 76.

You can't access individual frame timestamps (not in javascript, at least) but inbound-rtp stats seems defintely what you're searching for. The interface RTCInboundRtpStreamStats has lastPacketReceivedTimestamp which is defined as:

lastPacketReceivedTimestamp of type DOMHighResTimeStamp Represents the timestamp at which the last packet was received for this SSRC. This differs from timestamp, which represents the time at which the statistics were generated by the local endpoint.

All modern browsers (Chrome, Firefox, safari, Edge) seems to provide some support for inbound-rtp stats. Verify if they already provide meaningful values for lastPacketReceivedTimestamp. NOTE: the specification seems to be quite recent, your mileage may vary. Here is some code:

    rtcpeer.getReceivers()[0].getStats().then(function(stats)
    {
        for (let stat of stats.values())
        {
          if (stat.type === "inbound-rtp")
            console.log(stat.lastPacketReceivedTimestamp);
        }
    });

Upvotes: 4

Related Questions