Package dpkt :: Module rtp
[hide private]
[frames] | no frames]

Source Code for Module dpkt.rtp

  1  # $Id: rtp.py 23 2006-11-08 15:45:33Z dugsong $ 
  2  # -*- coding: utf-8 -*- 
  3  """Real-Time Transport Protocol.""" 
  4  from __future__ import absolute_import 
  5   
  6  from .dpkt import Packet 
  7  from .decorators import deprecated 
  8   
  9  # version 1100 0000 0000 0000 ! 0xC000  14 
 10  # p       0010 0000 0000 0000 ! 0x2000  13 
 11  # x       0001 0000 0000 0000 ! 0x1000  12 
 12  # cc      0000 1111 0000 0000 ! 0x0F00   8 
 13  # m       0000 0000 1000 0000 ! 0x0080   7 
 14  # pt      0000 0000 0111 1111 ! 0x007F   0 
 15  # 
 16   
 17  _VERSION_MASK = 0xC000 
 18  _P_MASK = 0x2000 
 19  _X_MASK = 0x1000 
 20  _CC_MASK = 0x0F00 
 21  _M_MASK = 0x0080 
 22  _PT_MASK = 0x007F 
 23  _VERSION_SHIFT = 14 
 24  _P_SHIFT = 13 
 25  _X_SHIFT = 12 
 26  _CC_SHIFT = 8 
 27  _M_SHIFT = 7 
 28  _PT_SHIFT = 0 
 29   
 30  VERSION = 2 
31 32 33 -class RTP(Packet):
34 """Real-Time Transport Protocol. 35 36 TODO: Longer class information.... 37 38 Attributes: 39 __hdr__: Header fields of RTP. 40 TODO. 41 """ 42 43 __hdr__ = ( 44 ('_type', 'H', 0x8000), 45 ('seq', 'H', 0), 46 ('ts', 'I', 0), 47 ('ssrc', 'I', 0), 48 ) 49 csrc = b'' 50 51 @property
52 - def version(self): return (self._type & _VERSION_MASK) >> _VERSION_SHIFT
53 54 @version.setter
55 - def version(self, ver):
56 self._type = (ver << _VERSION_SHIFT) | (self._type & ~_VERSION_MASK)
57 58 @property
59 - def p(self): return (self._type & _P_MASK) >> _P_SHIFT
60 61 @p.setter
62 - def p(self, p): self._type = (p << _P_SHIFT) | (self._type & ~_P_MASK)
63 64 @property
65 - def x(self): return (self._type & _X_MASK) >> _X_SHIFT
66 67 @x.setter
68 - def x(self, x): self._type = (x << _X_SHIFT) | (self._type & ~_X_MASK)
69 70 @property
71 - def cc(self): return (self._type & _CC_MASK) >> _CC_SHIFT
72 73 @cc.setter
74 - def cc(self, cc): self._type = (cc << _CC_SHIFT) | (self._type & ~_CC_MASK)
75 76 @property
77 - def m(self): return (self._type & _M_MASK) >> _M_SHIFT
78 79 @m.setter
80 - def m(self, m): self._type = (m << _M_SHIFT) | (self._type & ~_M_MASK)
81 82 @property
83 - def pt(self): return (self._type & _PT_MASK) >> _PT_SHIFT
84 85 @pt.setter
86 - def pt(self, m): self._type = (m << _PT_SHIFT) | (self._type & ~_PT_MASK)
87
88 - def __len__(self):
89 return self.__hdr_len__ + len(self.csrc) + len(self.data)
90
91 - def __bytes__(self):
92 return self.pack_hdr() + self.csrc + bytes(self.data)
93
94 - def unpack(self, buf):
95 super(RTP, self).unpack(buf) 96 self.csrc = buf[self.__hdr_len__:self.__hdr_len__ + self.cc * 4] 97 self.data = buf[self.__hdr_len__ + self.cc * 4:]
98
99 -def test_rtp():
100 rtp = RTP(b"\x80\x08\x4d\x01\x00\x01\x00\xe0\x34\x3f\xfa\x34\x53\x53\x53\x56\x53\x5d\x56\x57\xd5\xd6\xd1\xde\xdf\xd3\xd9\xda\xdf\xdc\xdf\xd8\xdd\xd4\xdd\xd9\xd1\xd6\xdc\xda\xde\xdd\xc7\xc1\xdf\xdf\xda\xdb\xdd\xdd\xc4\xd9\x55\x57\xd4\x50\x44\x44\x5b\x44\x4f\x4c\x47\x40\x4c\x47\x59\x5b\x58\x5d\x56\x56\x53\x56\xd5\xd5\x54\x55\xd6\xd6\xd4\xd1\xd1\xd0\xd1\xd5\xdd\xd6\x55\xd4\xd6\xd1\xd4\xd6\xd7\xd7\xd5\xd4\xd0\xd7\xd1\xd4\xd2\xdc\xd6\xdc\xdf\xdc\xdd\xd2\xde\xdc\xd0\xdd\xdc\xd0\xd6\xd6\xd6\x55\x54\x55\x57\x57\x56\x50\x50\x5c\x5c\x52\x5d\x5d\x5f\x5e\x5d\x5e\x52\x50\x52\x56\x54\x57\x55\x55\xd4\xd7\x55\xd5\x55\x55\x55\x55\x55\x54\x57\x54\x55\x55\xd5\xd5\xd7\xd6\xd7\xd1\xd1\xd3\xd2\xd3\xd2\xd2\xd3\xd3") 101 assert (rtp.version == 2) 102 assert (rtp.p == 0) 103 assert (rtp.x == 0) 104 assert (rtp.cc == 0) 105 assert (rtp.m == 0) 106 assert (rtp.pt == 8) 107 assert (rtp.seq == 19713) 108 assert (rtp.ts == 65760) 109 assert (rtp.ssrc == 0x343ffa34) 110 assert (len(rtp) == 172) 111 assert (bytes(rtp) == b"\x80\x08\x4d\x01\x00\x01\x00\xe0\x34\x3f\xfa\x34\x53\x53\x53\x56\x53\x5d\x56\x57\xd5\xd6\xd1\xde\xdf\xd3\xd9\xda\xdf\xdc\xdf\xd8\xdd\xd4\xdd\xd9\xd1\xd6\xdc\xda\xde\xdd\xc7\xc1\xdf\xdf\xda\xdb\xdd\xdd\xc4\xd9\x55\x57\xd4\x50\x44\x44\x5b\x44\x4f\x4c\x47\x40\x4c\x47\x59\x5b\x58\x5d\x56\x56\x53\x56\xd5\xd5\x54\x55\xd6\xd6\xd4\xd1\xd1\xd0\xd1\xd5\xdd\xd6\x55\xd4\xd6\xd1\xd4\xd6\xd7\xd7\xd5\xd4\xd0\xd7\xd1\xd4\xd2\xdc\xd6\xdc\xdf\xdc\xdd\xd2\xde\xdc\xd0\xdd\xdc\xd0\xd6\xd6\xd6\x55\x54\x55\x57\x57\x56\x50\x50\x5c\x5c\x52\x5d\x5d\x5f\x5e\x5d\x5e\x52\x50\x52\x56\x54\x57\x55\x55\xd4\xd7\x55\xd5\x55\x55\x55\x55\x55\x54\x57\x54\x55\x55\xd5\xd5\xd7\xd6\xd7\xd1\xd1\xd3\xd2\xd3\xd2\xd2\xd3\xd3") 112 113 # the following tests RTP header setters 114 rtp = RTP() 115 rtp.m = 1 116 rtp.pt = 3 117 rtp.seq = 1234 118 rtp.ts = 5678 119 rtp.ssrc = 0xabcdef01 120 assert (rtp.m == 1) 121 assert (rtp.pt == 3) 122 assert (rtp.seq == 1234) 123 assert (rtp.ts == 5678) 124 assert (rtp.ssrc == 0xabcdef01)
125