packetizer.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package rtp
  2. import (
  3. "time"
  4. )
  5. // Payloader payloads a byte array for use as rtp.Packet payloads
  6. type Payloader interface {
  7. Payload(mtu uint16, payload []byte) [][]byte
  8. }
  9. // Packetizer packetizes a payload
  10. type Packetizer interface {
  11. Packetize(payload []byte, samples uint32) []*Packet
  12. EnableAbsSendTime(value int)
  13. SkipSamples(skippedSamples uint32)
  14. }
  15. type packetizer struct {
  16. MTU uint16
  17. PayloadType uint8
  18. SSRC uint32
  19. Payloader Payloader
  20. Sequencer Sequencer
  21. Timestamp uint32
  22. ClockRate uint32
  23. extensionNumbers struct { // put extension numbers in here. If they're 0, the extension is disabled (0 is not a legal extension number)
  24. AbsSendTime int // http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time
  25. }
  26. timegen func() time.Time
  27. }
  28. // NewPacketizer returns a new instance of a Packetizer for a specific payloader
  29. func NewPacketizer(mtu uint16, pt uint8, ssrc uint32, payloader Payloader, sequencer Sequencer, clockRate uint32) Packetizer {
  30. return &packetizer{
  31. MTU: mtu,
  32. PayloadType: pt,
  33. SSRC: ssrc,
  34. Payloader: payloader,
  35. Sequencer: sequencer,
  36. Timestamp: globalMathRandomGenerator.Uint32(),
  37. ClockRate: clockRate,
  38. timegen: time.Now,
  39. }
  40. }
  41. func (p *packetizer) EnableAbsSendTime(value int) {
  42. p.extensionNumbers.AbsSendTime = value
  43. }
  44. // Packetize packetizes the payload of an RTP packet and returns one or more RTP packets
  45. func (p *packetizer) Packetize(payload []byte, samples uint32) []*Packet {
  46. // Guard against an empty payload
  47. if len(payload) == 0 {
  48. return nil
  49. }
  50. payloads := p.Payloader.Payload(p.MTU-12, payload)
  51. packets := make([]*Packet, len(payloads))
  52. for i, pp := range payloads {
  53. packets[i] = &Packet{
  54. Header: Header{
  55. Version: 2,
  56. Padding: false,
  57. Extension: false,
  58. Marker: i == len(payloads)-1,
  59. PayloadType: p.PayloadType,
  60. SequenceNumber: p.Sequencer.NextSequenceNumber(),
  61. Timestamp: p.Timestamp, // Figure out how to do timestamps
  62. SSRC: p.SSRC,
  63. },
  64. Payload: pp,
  65. }
  66. }
  67. p.Timestamp += samples
  68. if len(packets) != 0 && p.extensionNumbers.AbsSendTime != 0 {
  69. sendTime := NewAbsSendTimeExtension(p.timegen())
  70. // apply http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time
  71. b, err := sendTime.Marshal()
  72. if err != nil {
  73. return nil // never happens
  74. }
  75. err = packets[len(packets)-1].SetExtension(uint8(p.extensionNumbers.AbsSendTime), b)
  76. if err != nil {
  77. return nil // never happens
  78. }
  79. }
  80. return packets
  81. }
  82. // SkipSamples causes a gap in sample count between Packetize requests so the
  83. // RTP payloads produced have a gap in timestamps
  84. func (p *packetizer) SkipSamples(skippedSamples uint32) {
  85. p.Timestamp += skippedSamples
  86. }