繁体   English   中英

捕获 RTP 时间戳

[英]Capturing RTP Timestamps

我正在尝试一个小实验,以便使用 Python 中 Opencv 源代码中的 VideoCapture 类获取 RTP 数据包的时间戳,还必须修改 FFmpeg 以适应 Opencv 中的更改。

因为我阅读了RTP 数据包格式。想摆弄一下,看看我是否能设法找到一种方法来获取 NTP 时间戳。 无法找到任何可靠的帮助来尝试获取 RTP 时间戳。 所以尝试了这个小技巧。

感谢 ryantheseer 在 github 上修改后的代码。

FFmpeg 版本:3.2.3 Opencv 版本:3.2.0

在 Opencv 源代码中:

模块/videoio/include/opencv2/videoio.hpp:

为 RTP 时间戳添加了两个 getter:

.....   
    /** @brief Gets the upper bytes of the RTP time stamp in NTP format (seconds).
    */
    CV_WRAP virtual int64 getRTPTimeStampSeconds() const;
    
    /** @brief Gets the lower bytes of the RTP time stamp in NTP format (fraction of seconds).
    */
    CV_WRAP virtual int64 getRTPTimeStampFraction() const;
.....

模块/videoio/src/cap.cpp:

添加了一个导入并添加了时间戳获取器的实现:

....
#include <cstdint>
....
....
static inline uint64_t icvGetRTPTimeStamp(const CvCapture* capture)
{
  return capture ? capture->getRTPTimeStamp() : 0;
}
...

在 VideoCapture 类中添加了 C++ 时间戳获取器:

 ....
/**@brief Gets the upper bytes of the RTP time stamp in NTP format (seconds).
*/
int64 VideoCapture::getRTPTimeStampSeconds() const
{
    int64 seconds = 0;
    uint64_t timestamp = 0;
    //Get the time stamp from the capture object
    if (!icap.empty())
        timestamp = icap->getRTPTimeStamp();
    else
        timestamp = icvGetRTPTimeStamp(cap);
    //Take the top 32 bytes of the time stamp
    seconds = (int64)((timestamp & 0xFFFFFFFF00000000) / 0x100000000);
    return seconds;
}

/**@brief Gets the lower bytes of the RTP time stamp in NTP format (seconds).
*/
int64 VideoCapture::getRTPTimeStampFraction() const
{
    int64 fraction = 0;
    uint64_t timestamp = 0;
    //Get the time stamp from the capture object
    if (!icap.empty())
        timestamp = icap->getRTPTimeStamp();
    else
        timestamp = icvGetRTPTimeStamp(cap);
    //Take the bottom 32 bytes of the time stamp
    fraction = (int64)((timestamp & 0xFFFFFFFF));
    return fraction;
}
...

模块/videoio/src/cap_ffmpeg.cpp:

添加了一个导入:

...
#include <cstdint>
...

添加了方法参考定义:

...
static CvGetRTPTimeStamp_Plugin icvGetRTPTimeStamp_FFMPEG_p = 0;
...

将方法添加到模块初始化方法中:

...
if( icvFFOpenCV )
...
...
  icvGetRTPTimeStamp_FFMPEG_p =
                (CvGetRTPTimeStamp_Plugin)GetProcAddress(icvFFOpenCV, "cvGetRTPTimeStamp_FFMPEG");
...
...
icvWriteFrame_FFMPEG_p != 0 &&
icvGetRTPTimeStamp_FFMPEG_p !=0)
...

icvGetRTPTimeStamp_FFMPEG_p = (CvGetRTPTimeStamp_Plugin)cvGetRTPTimeStamp_FFMPEG;

实现了getter接口:

...
virtual uint64_t getRTPTimeStamp() const
    {
        return ffmpegCapture ? icvGetRTPTimeStamp_FFMPEG_p(ffmpegCapture) : 0;
    } 
...

在FFmpeg的源代码中:

libavcodec/avcodec.h:

向 AVPacket 结构添加了 NTP 时间戳定义:

typedef struct AVPacket {
...
...
uint64_t rtp_ntp_time_stamp;
}

libavformat/rtpdec.c:

将 ntp 时间戳存储在 finalize_packet 方法中的结构体中:

static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
{
    uint64_t offsetTime = 0;
    uint64_t rtp_ntp_time_stamp = timestamp;
...
...
/*RM: Sets the RTP time stamp in the AVPacket */
    if (!s->last_rtcp_ntp_time || !s->last_rtcp_timestamp)
        offsetTime = 0;
    else
        offsetTime = s->last_rtcp_ntp_time - ((uint64_t)(s->last_rtcp_timestamp) * 65536);
    rtp_ntp_time_stamp = ((uint64_t)(timestamp) * 65536) + offsetTime;
    pkt->rtp_ntp_time_stamp = rtp_ntp_time_stamp;

libavformat/utils.c:

在 read_frame_internal 方法中将数据包中的 ntp 时间戳复制到帧中:

static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
{
    ...
    uint64_t rtp_ntp_time_stamp = 0;
...
    while (!got_packet && !s->internal->parse_queue) {
          ...
          //COPY OVER the RTP time stamp TODO: just create a local copy
          rtp_ntp_time_stamp = cur_pkt.rtp_ntp_time_stamp;


          ...


  #if FF_API_LAVF_AVCTX
    update_stream_avctx(s);
  #endif

  if (s->debug & FF_FDEBUG_TS)
      av_log(s, AV_LOG_DEBUG,
           "read_frame_internal stream=%d, pts=%s, dts=%s, "
           "size=%d, duration=%"PRId64", flags=%d\n",
           pkt->stream_index,
           av_ts2str(pkt->pts),
           av_ts2str(pkt->dts),
           pkt->size, pkt->duration, pkt->flags);
pkt->rtp_ntp_time_stamp = rtp_ntp_time_stamp; #Just added this line in the if statement.
return ret;

我使用这些更改的 python 代码:

import cv2

uri = 'rtsp://admin:password@192.168.1.67:554'
cap = cv2.VideoCapture(uri)

while True:
    frame_exists, curr_frame = cap.read()
    # if frame_exists:
    k = cap.getRTPTimeStampSeconds()
    l = cap.getRTPTimeStampFraction()
    time_shift = 0x100000000
    #because in the getRTPTimeStampSeconds() 
    #function, seconds was multiplied by 0x10000000 
    seconds = time_shift * k
    m = (time_shift * k) + l
    print("Imagetimestamp: %i" % m)
cap.release()

我得到的作为我的输出:

    Imagetimestamp: 0
    Imagetimestamp: 212041451700224
    Imagetimestamp: 212041687629824
    Imagetimestamp: 212041923559424
    Imagetimestamp: 212042159489024
    Imagetimestamp: 212042395418624
    Imagetimestamp: 212042631348224
    ...

最让我震惊的是,当我关闭网络摄像机电源并重新打开时,时间戳将从 0 开始然后快速增加。 我读的NTP时间格式是相对于1900年1月1日00:00。 即使当我尝试计算偏移量,并从现在到 01-01-1900 之间进行核算时,我最终还是得到了一个疯狂的高数字。

不知道是不是我算错了。 我有一种感觉,它非常糟糕,或者我得到的不是时间戳。

如我所见,您会收到一个 uint64 类型的时间戳,其中包含高位和低位中的 uint32 值。 我在您使用的部分代码中看到:

seconds = (int64)((timestamp & 0xFFFFFFFF00000000) / 0x100000000);

这基本上删除了低位并将高位移到低位。 然后将其转换为 int64。 在这里我只考虑它首先应该是无符号的,因为它在任何情况下都不应该是负数(因为纪元总是正数的秒数)并且它应该是 uint32,因为它保证它不会更大(你只需要 32位)。 此外,这可以通过像这样的位移来实现(可能更快):

auto seconds = static_cast<uint32>(timestamp >> 32);

我发现的另一个错误是在这部分:

time_shift = 0x100000000
seconds = time_shift * k
m = (time_shift * k) + l

在这里,您基本上是在重建 64 位时间戳,而不是创建可在其他上下文中使用的时间戳。 这意味着,您将在几秒钟内将低位移动到高位并将小数部分添加为低位......这将以一个非常大的数字结束,这可能并不总是有用。 您仍然可以使用它进行比较,但是不需要在 C++ 部分中完成的所有转换。 我认为一个更正常的时间戳,你可以使用 python datetime 是这样的:

timestamp = float(str(k) + "." + str(l)) # don't know if there is a better way
date = datetime.fromtimestamp(timestamp)

如果你不关心小数部分,你可以直接使用秒。

另一件要考虑的事情是,RTP 协议的时间戳取决于相机/服务器......他们可能使用时钟时间戳或只是其他一些时钟,如系统启动流的开始。 所以它可能来自也可能不是来自纪元。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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