rtp.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """Real-Time Transport Protocol"""
  2. from pypacker import pypacker
  3. # version 1100 0000 0000 0000 ! 0xC000 14
  4. # p 0010 0000 0000 0000 ! 0x2000 13
  5. # x 0001 0000 0000 0000 ! 0x1000 12
  6. # cc 0000 1111 0000 0000 ! 0x0F00 8
  7. # m 0000 0000 1000 0000 ! 0x0080 7
  8. # pt 0000 0000 0111 1111 ! 0x007F 0
  9. #
  10. _VERSION_MASK = 0xC000
  11. _P_MASK = 0x2000
  12. _X_MASK = 0x1000
  13. _CC_MASK = 0x0F00
  14. _M_MASK = 0x0080
  15. _PT_MASK = 0x007F
  16. _VERSION_SHIFT = 14
  17. _P_SHIFT = 13
  18. _X_SHIFT = 12
  19. _CC_SHIFT = 8
  20. _M_SHIFT = 7
  21. _PT_SHIFT = 0
  22. VERSION = 2
  23. class RTP(pypacker.Packet):
  24. __hdr__ = (
  25. ("type", "H", 0x8000),
  26. ("seq", "H", 0),
  27. ("ts", "I", 0),
  28. ("ssrc", "I", 0)
  29. )
  30. def getversion(self):
  31. return (type & _VERSION_MASK) >> _VERSION_SHIFT
  32. def setversion(self, value):
  33. self.type = (value << _VERSION_SHIFT) | (self.type & ~_VERSION_MASK)
  34. version = property(getversion, setversion)
  35. def getp(self):
  36. return (self.type & _P_MASK) >> _P_SHIFT
  37. def setp(self, value):
  38. self.type = (value << _P_SHIFT) | (self.type & ~_P_MASK)
  39. p = property(getp, setp)
  40. def getx(self):
  41. return (self.type & _X_MASK) >> _X_SHIFT
  42. def setx(self, value):
  43. self.type = (value << _X_SHIFT) | (self.type & ~_X_MASK)
  44. x = property(getx, setx)
  45. def getcc(self):
  46. return (self.type & _CC_MASK) >> _CC_SHIFT
  47. def setcc(self, value):
  48. self.type = (value << _CC_SHIFT) | (self.type & ~_CC_MASK)
  49. cc = property(getcc, setcc)
  50. def getm(self):
  51. return (self.type & _M_MASK) >> _M_SHIFT
  52. def setm(self, value):
  53. self.type = (value << _M_SHIFT) | (self.type & ~_M_MASK)
  54. m = property(getm, setm)
  55. def getpt(self):
  56. return (self.type & _PT_MASK) >> _PT_SHIFT
  57. def setpt(self, value):
  58. self.type = (value << _PT_SHIFT) | (self.type & ~_PT_MASK)
  59. pt = property(getpt, setpt)
  60. """
  61. __m_switch_set = {"version":lambda type,version: (version << _VERSION_SHIFT) | (type & ~_VERSION_MASK),
  62. "p":lambda type,p: (p << _P_SHIFT) | (type & ~_P_MASK),
  63. "x":lambda type,x: (x << _X_SHIFT) | (type & ~_X_MASK),
  64. "cc":lambda type,cc: (cc << _CC_SHIFT) | (type & ~_CC_MASK),
  65. "m":lambda type,m: (m << _M_SHIFT) | (type & ~_M_MASK),
  66. "pt":lambda type,pt: (m << _PT_SHIFT) | (type & ~_PT_MASK)
  67. }
  68. _m_switch_get = {"version":lambda type: (type & _VERSION_MASK) >> _VERSION_SHIFT,
  69. "p":lambda type: (type & _P_MASK) >> _P_SHIFT,
  70. "x":lambda type: (type & _X_MASK) >> _X_SHIFT,
  71. "cc":lambda type: (type & _CC_MASK) >> _CC_SHIFT,
  72. "m":lambda type: (type & _M_MASK) >> _M_SHIFT,
  73. "pt":lambda type: (type & _PT_MASK) >> _PT_SHIFT
  74. }
  75. """