live555中进行RTP打包时,生成RTP header时,需要计算该RTP的时间戳.
RTP header
可以看出
RTP
的时间戳在RTP header
的第5
字节到第8
字节,共4
个字节,即32bit
.
timeval
/*
* Structure used in select() call, taken from the BSD file sys/time.h.
*/
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* and microseconds */
};
- tv_sec
秒. - tv_usec
u秒, 即1000000
分之一秒.
调用堆栈
以视频为H.264为例:
RTPSink::convertToRTPTimestamp**(timeval tv) 行 97 C++
MultiFramedRTPSink::setTimestamp(timeval framePresentationTime) 行 113 + 0x10 字节 C++
H264or5VideoRTPSink::doSpecialFrameHandling(unsigned int __formal, unsigned int __formal, unsigned int __formal, timeval framePresentationTime, unsigned int __formal) 行 152 C++
MultiFramedRTPSink::afterGettingFrame1(unsigned int frameSize, unsigned int numTruncatedBytes, timeval presentationTime, unsigned int durationInMicroseconds) 行 319 + 0x27 字节 C++
MultiFramedRTPSink::afterGettingFrame(void * clientData, unsigned int numBytesRead, unsigned int numTruncatedBytes, timeval presentationTime, unsigned int durationInMicroseconds) 行 235 C++
FramedSource::afterGetting(FramedSource * source) 行 91 + 0x31 字节 C++
核心函数
- setTimestamp
void MultiFramedRTPSink::setTimestamp(struct timeval framePresentationTime) {
// First, convert the presentation time to a 32-bit RTP timestamp:
fCurrentTimestamp = convertToRTPTimestamp(framePresentationTime);
// Then, insert it into the RTP packet:
fOutBuf->insertWord(fCurrentTimestamp, fTimestampPosition);
}
-
convertToRTPTimestamp
生成32
bit的RTP时间戳.
u_int32_t RTPSink::convertToRTPTimestamp(struct timeval tv) {
// 将 "struct timeval" 单元转换为RTP 时间戳单元:
u_int32_t timestampIncrement = (fTimestampFrequency*tv.tv_sec);
// 加0.5是为了实现四舍五入
timestampIncrement += (u_int32_t)(fTimestampFrequency*(tv.tv_usec/1000000.0) + 0.5); // 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;
}
// 生成32位的时间戳
// 时间戳 = 基准时间戳 + 时间戳增量
u_int32_t const rtpTimestamp = fTimestampBase + timestampIncrement;
return rtpTimestamp;
}
fTimestampFrequency
对H.264来说一般为90000
.