# Path: rtpmidi/protocols/rtp/jitter_buffer.py
# class JitterBuffer(object):
#     """Jitter buffer auto-resize by the jitter"""
# 
#     def __init__(self):
#         self.buffer = []
#         self.lock = Lock()
# 
#     def add(self, to_add):
#         """Adding a packet to the buffer (insertion sort)"""
#         place = 0
# 
#         self.lock.acquire()
# 
#         for i in range(len(self.buffer)):
#             #Order by seq number
#             if self.buffer[i][0].header.seq < to_add[0].header.seq:
#                 pass
#             else:
#                 place = i
#                 break
# 
#         if place != 0:
#             #Insert new element
#             self.buffer.insert(place, to_add)
#         else:
#             if len(self.buffer) >= 1:
#                 if self.buffer[i][0].header.seq > to_add[0].header.seq:
#                     self.buffer.insert(0, to_add)
#                 else:
#                     self.buffer.append(to_add)
#             else:
#                 self.buffer.append(to_add)
# 
#         self.lock.release()
# 
# 
#     def has_seq(self, seq):
#         """Looking for a special seqNum"""
#         res = False
#         for i in range(len(self.buffer)):
#             if self.buffer[i][0].header.seq == seq:
#                 res = True
#                 break
#         return res
# 
#     def get_packets(self, time):
#         """Getting all the packet with continus seq num from packet seq"""
#         to_send = []
#         i = 0
#         last_time = 0
#         self.lock.acquire()
#         while ( i < len(self.buffer)):
#             #if buffer time passed or
#             if self.buffer[i][1] <= time:
#                 to_send.append(self.buffer[i][0])
#                 del self.buffer[i]
#             else:
#                 break
# 
#         self.lock.release()
# 
#         return to_send
# 
# Path: rtpmidi/protocols/rtp/packets.py
# class RTPPacket:
#     """ Contains RTP data. """
#     class Header:
#         def __init__(self, ssrc, pt, ct, seq, ts, marker=0,
#                      xhdrtype=None, xhdrdata=''):
#             """
#             If xhdrtype is not None then it is required to be an
#             int >= 0 and < 2**16 and xhdrdata is required to be
#             a string whose length is a multiple of 4.
#             """
#             assert isinstance(ts, (int, long,)), "ts: %s :: %s" % (ts, type(ts))
#             assert isinstance(ssrc, (int, long,))
#             assert xhdrtype is None or isinstance(xhdrtype, int) \
#                     and xhdrtype >= 0 and xhdrtype < 2**16
#             # Sorry, RFC standard specifies that len is in 4-byte words,
#             # and I'm not going to do the padding and unpadding for you.
#             assert xhdrtype is None or (isinstance(xhdrdata, str) and \
#                     len(xhdrdata) % 4 == 0), \
#                     "xhdrtype: %s, len(xhdrdata): %s, xhdrdata: %s" % (
#                     xhdrtype, len(xhdrdata), `xhdrdata`,)
# 
#             (self.ssrc, self.pt, self.ct, self.seq, self.ts,
#                  self.marker, self.xhdrtype, self.xhdrdata) = (
#              ssrc,      pt,      ct,      seq,      ts,
#                  marker,      xhdrtype,      xhdrdata)
# 
#         def netbytes(self):
#             "Return network-formatted header."
#             assert isinstance(self.pt, int) and self.pt >= 0 and \
#                 self.pt < 2**8, \
#                 "pt is required to be a simple byte, suitable " + \
#                 "for stuffing into an RTP packet and sending. pt: %s" % self.pt
#             if self.xhdrtype is not None:
#                 firstbyte = 0x90
#                 xhdrnetbytes = struct.pack('!HH', self.xhdrtype,
#                                     len(self.xhdrdata)/4) + self.xhdrdata
#             else:
#                 firstbyte = 0x80
#                 xhdrnetbytes = ''
#             return struct.pack('!BBHII', firstbyte,
#                                         self.pt | self.marker << 7,
#                                         self.seq % 2**16,
#                                         self.ts, self.ssrc) + xhdrnetbytes
# 
#     def __init__(self, ssrc, seq, ts, data, pt=None, ct=None, marker=0,
#                  authtag='', xhdrtype=None, xhdrdata=''):
#         assert pt is None or isinstance(pt, int) and pt >= 0 and pt < 2**8, \
#             "pt is required to be a simple byte, suitable for stuffing " + \
#             "into an RTP packet and sending. pt: %s" % pt
#         self.header = RTPPacket.Header(ssrc, pt, ct, seq, ts, marker,
#                                        xhdrtype, xhdrdata)
#         self.data = data
#         # please leave this alone even if it appears unused --
#         # it is required for SRTP
#         self.authtag = authtag
# 
#     def __repr__(self):
#         if self.header.ct is not None:
#             ptrepr = "%r" % (self.header.ct,)
#         else:
#             ptrepr = "pt %s" % (self.header.pt,)
# 
#         if self.header.xhdrtype is not None:
#             return "<%s #%d (%s) %s [%s] at %x>"%(self.__class__.__name__,
#                                                   self.header.seq,
#                                                   self.header.xhdrtype,
#                                                   ptrepr,
#                                                   repr(self.header.xhdrdata),
#                                                   id(self))
#         else:
#             return "<%s #%d %s at %x>"%(self.__class__.__name__,
#                                         self.header.seq, ptrepr, id(self))
# 
#     def netbytes(self):
#         "Return network-formatted packet."
#         return self.header.netbytes() + self.data + self.authtag


from twisted.trial import unittest
from rtpmidi.protocols.rtp.jitter_buffer import JitterBuffer
from rtpmidi.protocols.rtp.packets import RTPPacket


class TestJitterBuffer(unittest.TestCase):
    """Testing function from JitterBuffer"""

    def setUp(self):
        self.jitter_buffer = JitterBuffer()

    def tearDown(self):
        pass

    def test_init(self):

# Path: rtpmidi/protocols/rtp/jitter_buffer.py
# class JitterBuffer(object):
#     """Jitter buffer auto-resize by the jitter"""
# 
#     def __init__(self):
#         self.buffer = []
#         self.lock = Lock()
# 
#     def add(self, to_add):
#         """Adding a packet to the buffer (insertion sort)"""
#         place = 0
# 
#         self.lock.acquire()
# 
#         for i in range(len(self.buffer)):
#             #Order by seq number
#             if self.buffer[i][0].header.seq < to_add[0].header.seq:
#                 pass
#             else:
#                 place = i
#                 break
# 
#         if place != 0:
#             #Insert new element
#             self.buffer.insert(place, to_add)
#         else:
#             if len(self.buffer) >= 1:
#                 if self.buffer[i][0].header.seq > to_add[0].header.seq:
#                     self.buffer.insert(0, to_add)
#                 else:
#                     self.buffer.append(to_add)
#             else:
#                 self.buffer.append(to_add)
# 
#         self.lock.release()
# 
# 
#     def has_seq(self, seq):
#         """Looking for a special seqNum"""
#         res = False
#         for i in range(len(self.buffer)):
#             if self.buffer[i][0].header.seq == seq:
#                 res = True
#                 break
#         return res
# 
#     def get_packets(self, time):
#         """Getting all the packet with continus seq num from packet seq"""
#         to_send = []
#         i = 0
#         last_time = 0
#         self.lock.acquire()
#         while ( i < len(self.buffer)):
#             #if buffer time passed or
#             if self.buffer[i][1] <= time:
#                 to_send.append(self.buffer[i][0])
#                 del self.buffer[i]
#             else:
#                 break
# 
#         self.lock.release()
# 
#         return to_send
# 
# Path: rtpmidi/protocols/rtp/packets.py
# class RTPPacket:
#     """ Contains RTP data. """
#     class Header:
#         def __init__(self, ssrc, pt, ct, seq, ts, marker=0,
#                      xhdrtype=None, xhdrdata=''):
#             """
#             If xhdrtype is not None then it is required to be an
#             int >= 0 and < 2**16 and xhdrdata is required to be
#             a string whose length is a multiple of 4.
#             """
#             assert isinstance(ts, (int, long,)), "ts: %s :: %s" % (ts, type(ts))
#             assert isinstance(ssrc, (int, long,))
#             assert xhdrtype is None or isinstance(xhdrtype, int) \
#                     and xhdrtype >= 0 and xhdrtype < 2**16
#             # Sorry, RFC standard specifies that len is in 4-byte words,
#             # and I'm not going to do the padding and unpadding for you.
#             assert xhdrtype is None or (isinstance(xhdrdata, str) and \
#                     len(xhdrdata) % 4 == 0), \
#                     "xhdrtype: %s, len(xhdrdata): %s, xhdrdata: %s" % (
#                     xhdrtype, len(xhdrdata), `xhdrdata`,)
# 
#             (self.ssrc, self.pt, self.ct, self.seq, self.ts,
#                  self.marker, self.xhdrtype, self.xhdrdata) = (
#              ssrc,      pt,      ct,      seq,      ts,
#                  marker,      xhdrtype,      xhdrdata)
# 
#         def netbytes(self):
#             "Return network-formatted header."
#             assert isinstance(self.pt, int) and self.pt >= 0 and \
#                 self.pt < 2**8, \
#                 "pt is required to be a simple byte, suitable " + \
#                 "for stuffing into an RTP packet and sending. pt: %s" % self.pt
#             if self.xhdrtype is not None:
#                 firstbyte = 0x90
#                 xhdrnetbytes = struct.pack('!HH', self.xhdrtype,
#                                     len(self.xhdrdata)/4) + self.xhdrdata
#             else:
#                 firstbyte = 0x80
#                 xhdrnetbytes = ''
#             return struct.pack('!BBHII', firstbyte,
#                                         self.pt | self.marker << 7,
#                                         self.seq % 2**16,
#                                         self.ts, self.ssrc) + xhdrnetbytes
# 
#     def __init__(self, ssrc, seq, ts, data, pt=None, ct=None, marker=0,
#                  authtag='', xhdrtype=None, xhdrdata=''):
#         assert pt is None or isinstance(pt, int) and pt >= 0 and pt < 2**8, \
#             "pt is required to be a simple byte, suitable for stuffing " + \
#             "into an RTP packet and sending. pt: %s" % pt
#         self.header = RTPPacket.Header(ssrc, pt, ct, seq, ts, marker,
#                                        xhdrtype, xhdrdata)
#         self.data = data
#         # please leave this alone even if it appears unused --
#         # it is required for SRTP
#         self.authtag = authtag
# 
#     def __repr__(self):
#         if self.header.ct is not None:
#             ptrepr = "%r" % (self.header.ct,)
#         else:
#             ptrepr = "pt %s" % (self.header.pt,)
# 
#         if self.header.xhdrtype is not None:
#             return "<%s #%d (%s) %s [%s] at %x>"%(self.__class__.__name__,
#                                                   self.header.seq,
#                                                   self.header.xhdrtype,
#                                                   ptrepr,
#                                                   repr(self.header.xhdrdata),
#                                                   id(self))
#         else:
#             return "<%s #%d %s at %x>"%(self.__class__.__name__,
#                                         self.header.seq, ptrepr, id(self))
# 
#     def netbytes(self):
#         "Return network-formatted packet."
#         return self.header.netbytes() + self.data + self.authtag


# Path: rtpmidi/test/test_jitter_buffer.py
from twisted.trial import unittest
from rtpmidi.protocols.rtp.jitter_buffer import JitterBuffer
from rtpmidi.protocols.rtp.packets import RTPPacket



class TestJitterBuffer(unittest.TestCase):
    """Testing function from JitterBuffer"""

    def setUp(self):
        self.jitter_buffer = JitterBuffer()

    def tearDown(self):
        pass

    def test_init(self):

        jitter_buffer = JitterBuffer()