简体   繁体   中英

Reading RTCP packets from an IP camera using FFMPEG

I am using the ffmpeg C library. I need to intercept RTCP packets from the camera in order to get the timestamp from the Sender Report. Is there any method or structure in the ffmpeg that gives me this information? I am completely stuck but I am not able to solve that problem.

Any help will be appreciated. Thanks in advance,

Finally I had to hack into the ffmpeg library like this:

// Patch for retrieving inner ffmpeg private data
RTSPState* rtsp_state = (RTSPState*) context->priv_data;
RTSPStream* rtsp_stream = rtsp_state->rtsp_streams[0];
RTPDemuxContext* rtp_demux_context = (RTPDemuxContext*) rtsp_stream->transport_priv;

// Decode the NTP time from the 64 bit structure
uint64_t ntp_time = rtp_demux_context->last_rtcp_reception_time;
uint32_t seconds = (uint32_t) ((ntp_time >> 32) & 0xffffffff);
uint32_t fraction  = (uint32_t) (ntp_time & 0xffffffff);
double useconds = ((double) fraction / 0xffffffff);

And I finally get timestamp information.

I have done some experiments on the ffmpeg (version 3.4.6).

AVFormatContext* ifmt_ctx = avformat_alloc_context();
AVStream * st = xx; // select stream
double timebase = av_q2d(st->time_base);
streamStartTime  = ifmt_ctx->start_time_realtime; // this is ntp time , i.e. stream build time 

then, add relative time to ntp time ,you can get the absolute time of each frame

streamStartTime + (1000000 * pkt->pts * time_base) // AVPacket * pkt

For anyone looking to extract this from FFmpeg=4.4.1, here is a nifty patch that I made: https://github.com/necla-ml/feedstocks/blob/main/recipes/ffmpeg/patches/add_rtp_ntp_timestamp_4.4.1.patch

This will add few new attributes to the AVPacket

uint32_t timestamp;
uint64_t last_rtcp_ntp_time;
uint32_t last_rtcp_timestamp;
uint16_t seq;
bool synced;

You can also just install fffmpeg from our conda channel: mamba install -c necla-ml ffmpeg=4.4.1=*ntp* and use https://github.com/LukasBommes/mv-extractor which does the job of calculating the absolute ntp timestamp while maintaining opencv like interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM