Skip to content

Commit

Permalink
add optional abs-send-time support to Packetizer
Browse files Browse the repository at this point in the history
Relates to #15
  • Loading branch information
wdouglass committed Mar 26, 2019
1 parent d972bcc commit e92fe75
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ func (h *Header) MarshalTo(buf []byte) (n int, err error) {
h.PayloadOffset = n

if h.Extension {
if len(h.ExtensionPayload)%4 != 0 {
//the payload must be in 32-bit words.
return 0, io.ErrShortBuffer
}
extSize := uint16(len(h.ExtensionPayload) / 4)

binary.BigEndian.PutUint16(buf[n+0:n+2], h.ExtensionProfile)
Expand Down
35 changes: 35 additions & 0 deletions packetizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type packetizer struct {
Sequencer Sequencer
Timestamp uint32
ClockRate uint32
AbsSendTime int //if this is nonzero, then it's the extension number that's applied to http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time
}

// NewPacketizer returns a new instance of a Packetizer for a specific payloader
Expand All @@ -41,6 +42,19 @@ func NewPacketizer(mtu int, pt uint8, ssrc uint32, payloader Payloader, sequence
}
}

func toNtpTime(t time.Time) int64 {
var s int64
var f int64
u := t.UnixNano()
s = u / 1e9
f = u % 1e9
f <<= 32
f /= 1e9
s <<= 32

return s | f
}

// Packetize packetizes the payload of an RTP packet and returns one or more RTP packets
func (p *packetizer) Packetize(payload []byte, samples uint32) []*Packet {
// Guard against an empty payload
Expand Down Expand Up @@ -68,5 +82,26 @@ func (p *packetizer) Packetize(payload []byte, samples uint32) []*Packet {
}
p.Timestamp += samples

if p.AbsSendTime != 0 {
t := toNtpTime(time.Now()) >> 14
//apply http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time
packets[len(payloads)-1].ExtensionProfile = 0xBEDE
packets[len(payloads)-1].ExtensionPayload = []byte{
//the first byte is
// 0 1 2 3 4 5 6 7
//+-+-+-+-+-+-+-+-+
//| ID | len |
//+-+-+-+-+-+-+-+-+
//per RFC65285
//Len is the number of bytes in the extension - 1

byte((p.AbsSendTime << 4) | 2),
byte(t & 0xFF0000 >> 16),
byte(t & 0xFF00 >> 8),
byte(t & 0xFF),
}

}

return packets
}

0 comments on commit e92fe75

Please sign in to comment.