github.com/cnotch/ipchub@v1.1.0/av/format/rtp/syncclock.go (about) 1 // Copyright (c) 2019,CAOHONGJU All rights reserved. 2 // Use of this source code is governed by a MIT-style 3 // license that can be found in the LICENSE file. 4 5 package rtp 6 7 import ( 8 "encoding/binary" 9 "time" 10 ) 11 12 const jan1970 = 0x83aa7e80 13 14 // SyncClock . 15 type SyncClock struct { 16 // NTP Timestamp(Network time protocol)SR包发送时的绝对时间值。 17 // NTP的作用是同步不同的RTP媒体流。 18 // NTP时间戳,它的前32位是从1900 年1 月1 日0 时开始到现在的以秒为单位的整数部, 19 // 后32 位是此时间的小数部,因此,它可以肯定的表示了数据发送出去的绝对时间。 20 NTPTime int64 // 此处转换成自 January 1, year 1 以来的纳秒数 21 // RTP Timestamp:与NTP时间戳对应, 22 // 与RTP数据包中的RTP时间戳具有相同的单位和随机初始值。 23 RTPTime uint32 24 RTPTimeUnit float64 // RTP时间单位,每个RTP时间的纳秒数 25 } 26 27 // LocalTime 本地时间 28 func (sc *SyncClock) LocalTime() time.Time { 29 return time.Unix(0, sc.NTPTime).In(time.Local) 30 } 31 32 // Decode . 33 func (sc *SyncClock) Decode(data []byte) (ok bool) { 34 if data[1] == 200 { 35 msw := binary.BigEndian.Uint32(data[8:]) 36 lsw := binary.BigEndian.Uint32(data[12:]) 37 sc.RTPTime = binary.BigEndian.Uint32(data[16:]) 38 sc.NTPTime = int64(msw-jan1970)*int64(time.Second) + (int64(lsw)*1000_000_000)>>32 39 ok = true 40 } 41 return 42 } 43 44 // Rtp2Ntp . 45 func (sc *SyncClock) Rtp2Ntp(rtptime uint32) int64 { 46 diff := int64(rtptime) - int64(sc.RTPTime) 47 return sc.NTPTime + int64(float64(diff)*sc.RTPTimeUnit) 48 }