开发者

H.264 RTSP Absolute TIMESTAMP

Is it possible to read开发者_如何学编程 an absolute timestamp from an H.264 stream sent trough RTSP from an Axis camera?

It will be necessary to know when the frame has been taken by the camera.

Thanks Andrea


as Ralf already said - the RTP timestamps are relative to a random clock - they are only useful for computing the difference between two frames (or RTP-packets in general). For synchronizing these relative values to a wall clock you can use the RTCP sender - just have a look on the links Ralf provided.

For Axis-products using H.264 this works pretty good. In case you're also using MPEG4, the Axis firmware is buggy and the absolute timestamps in RTCP SR are not reliable - in this case you have to synchronize the relative RTP timestamps to your clients wall clock.


Assuming the cameras firmware works properly and it's synchronized with NTP regularly, you can extract the absolute timestamp from RTCP Sender Report. But this functionality is not available in FFMpeg library API, you have to use header libavformat/rtsp.h in order to access internal data structures. And then you have to calculate the ntp timestamp for every frame:

RTSPState* rtsp_state = (RTSPState*) pFormatCtx->priv_data;
RTSPStream* rtsp_stream = rtsp_state->rtsp_streams[0];
RTPDemuxContext* rtp_demux_context = (RTPDemuxContext*) rtsp_stream->transport_priv;

int32_t d_ts = rtp_demux_context->timestamp - rtp_demux_context->last_rtcp_timestamp;
uint64_t last_ntp_time = rtp_demux_context->last_rtcp_ntp_time;
uint32_t seconds = ((last_ntp_time >> 32) & 0xffffffff)-2208988800;
uint32_t fraction  = (last_ntp_time & 0xffffffff);
double useconds = ((double) fraction / 0xffffffff);
double base_time = seconds+useconds;

double frame_ntp_time = base_time+d_ts/90000.0;

Full example is here.


Timestamps are contained in the RTP stream. RTSP is a protocol that can be used to start/control an RTP media session. I'm assuming that RTP used and you can look at the RTP header here.


There is a program called openRTSP (livemedia-utils on Debian, live-media on Arch) that fetches a parameter o:

openRTSP -r rtsp://109.98.78.106
[...]
o=- 1613080009143448 1 IN IP4 109.98.78.106

Without having read the source code, I think it to be the NTS system time stamp from the Sender Report RTCP Packets Islam Sabyrgaliyev mentioned.

date -d@$( echo $(openRTSP -r rtsp://109.98.78.106 2>&1 | grep -Po '(?<=o=-\s)\d+' | head -n1 ) / 1000000 | bc )
Thu Feb 11 10:46:07 PM CET 2021
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜