live555时戳转换函数理解
u_int32_t RTPSink::convertToRTPTimestamp(struct timeval tv) {
// Begin by converting from "struct timeval" units to RTP timestamp units:
u_int32_t timestampIncrement = (fTimestampFrequency*tv.tv_sec);
timestampIncrement += (u_int32_t)((2.0*fTimestampFrequency*tv.tv_usec + 1000000.0)/2000000);
// note: rounding
// Then add this to our 'timestamp base':
if (fNextTimestampHasBeenPreset) {
// Make the returned timestamp the same as the current "fTimestampBase",
// so that timestamps begin with the value that was previously preset:
fTimestampBase -= timestampIncrement;
fNextTimestampHasBeenPreset = False;
}
u_int32_t const rtpTimestamp = fTimestampBase + timestampIncrement;
#ifdef DEBUG_TIMESTAMPS
fprintf(stderr, "fTimestampBase: 0x%08x, tv: %lu.%06ld\n\t=> RTP timestamp: 0x%08x\n",
fTimestampBase, tv.tv_sec, tv.tv_usec, rtpTimestamp);
fflush(stderr);
#endif
return rtpTimestamp;
}
u_int32_t RTPSink::presetNextTimestamp() {
struct timeval timeNow;
gettimeofday(&timeNow, NULL);
u_int32_t tsNow = convertToRTPTimestamp(timeNow);
fTimestampBase = tsNow;
fNextTimestampHasBeenPreset = True;
return tsNow;
}
要理解前面这个函数要结合后面这个函数及他们的调用来理解
分三部分
第一部分是转换时戳, 以频率为单位, 通常情况下该单位为90khz, 即90000
先转换秒, 再转换微妙(+1000000/2000000表示向上圆整, 5入)
第二部分
从代码看fNextTimestampHasBeenPreset如果值为真
则返回值为fTimestampBase及上一次得到的时戳
否则 则加上偏移
fNextTimestampHasBeenPreset 初始化时为真,
fTimestampBase 初始化时为一个随机值, 可见时戳不在乎绝对值, 而只在乎差值
从整体代码看presetNextTimestamp通常只会最初调用
这样整个代码就好理解了
第三部分简单加上偏移, 并返回
阅读(3247) | 评论(0) | 转发(0) |