def test_userDataWords_encodings(self, data): doc, encoding, bom = data payload = RTPPayload_TTML(userDataWords=doc, encoding=encoding, bom=bom) self.assertEqual(payload.userDataWords, doc) self.assertEqual(payload._userDataWords, utfEncode(doc, encoding, bom))
def test_packetiseDoc(self, data): doc, encoding, bom, time, marker = data thisTransmitter = TTMLTransmitter("", 0, encoding=encoding, bom=bom) expectedSeqNum = thisTransmitter._nextSeqNum packets = thisTransmitter._packetiseDoc(doc, time) for x in range(len(packets)): payload = RTPPayload_TTML( encoding=encoding, bom=bom).fromBytearray(packets[x].payload) self.assertEqual(packets[x].timestamp, thisTransmitter._datetimeToRTPTs(time)) self.assertEqual(packets[x].sequenceNumber, expectedSeqNum + x) self.assertIn(payload.userDataWords, doc) self.assertLess(len(utfEncode(payload.userDataWords)), 2**16) thisBom = BOMS[encoding] if bom and (x == 0): self.assertTrue(payload._userDataWords.startswith(thisBom)) else: self.assertFalse(payload._userDataWords.startswith(thisBom)) if x == (len(packets) - 1): self.assertTrue(packets[x].marker) else: self.assertFalse(packets[x].marker) self.assertEqual(thisTransmitter.nextSeqNum, expectedSeqNum + len(packets))
def test_init(self, data): doc, encoding, bom = data reservedBits = bytearray(b'\x00\x00') newP = RTPPayload_TTML(reservedBits, doc, encoding, bom) self.assertEqual(newP.reserved, reservedBits) self.assertEqual(newP.userDataWords, doc) self.assertEqual(newP._encoding, encoding) self.assertEqual(newP._bom, bom)
def test_fromBytearray(self, doc): expected = RTPPayload_TTML(userDataWords=doc) bDoc = utfEncode(doc) bArray = bytearray(2) bArray += int(len(bDoc)).to_bytes(2, byteorder='big') bArray += bDoc self.thisP.fromBytearray(bArray) self.assertEqual(expected, self.thisP)
def _processPacket(self, packet: RTP) -> None: # New TS means a new document if self._curTimestamp != packet.timestamp: # If we haven't processed by now, document is incomplete # so we discard it self._fragments.clear() # Assume this packet is the first in doc. If we're wrong, the doc # won't be valid TTML when decoded anyway self._curTimestamp = packet.timestamp seqNumber = packet.sequenceNumber if len(self._fragments) > 0: seqNumber = self._unloopSeqNum( max(self._fragments), packet.sequenceNumber) payload = RTPPayload_TTML( encoding=self._encoding, bom=self._bom) payload.fromBytearray(packet.payload) self._fragments[seqNumber] = payload.userDataWords
def test_generateRTPPacket(self, data): doc, encoding, bom, time, isFirst, marker = data thisTransmitter = TTMLTransmitter("", 0, encoding=encoding, bom=bom) expectedSeqNum = thisTransmitter._nextSeqNum packet = thisTransmitter._generateRTPPacket(doc, time, isFirst, marker) payload = RTPPayload_TTML(encoding=encoding, bom=bom).fromBytearray(packet.payload) self.assertEqual(packet.timestamp, time) self.assertEqual(packet.sequenceNumber, expectedSeqNum) self.assertEqual(packet.marker, marker) self.assertEqual(payload.userDataWords, doc) self.assertEqual(thisTransmitter._nextSeqNum, expectedSeqNum + 1)
def _generateRTPPacket(self, doc: str, time: int, isFirst: bool, marker: bool) -> RTP: # Only include bom in first packet for doc thisBOM = (isFirst and self._bom) packet = RTP(timestamp=time, sequenceNumber=self._nextSeqNum, payload=RTPPayload_TTML(userDataWords=doc, encoding=self._encoding, bom=thisBOM).toBytearray(), marker=marker, payloadType=self._payloadType) self._nextSeqNum += 1 return packet
def test_processData(self, data): doc, encoding, bom = data payload = RTPPayload_TTML(userDataWords=doc, encoding=encoding, bom=bom).toBytearray() packet = RTP(payload=payload, marker=True) packetBytes = packet.toBytes() with mock.patch("rtpTTML.ttmlReceiver.RTPPayload_TTML") as mockTTML: thisReceiver = TTMLReceiver(0, self.callback, encoding=encoding, bom=bom) thisReceiver._processData(packetBytes) mockTTML.assert_called_once_with(encoding=encoding, bom=bom)
def test_bytearray_default(self): expected = bytearray(4) self.assertEqual(bytes(self.thisP), expected) newP = RTPPayload_TTML().fromBytearray(expected) self.assertEqual(newP, self.thisP)
def test_eq(self): reservedBits = bytearray(b'\x00\x00') newP = RTPPayload_TTML(reservedBits, "") self.assertEqual(newP, self.thisP)
def test_init_invalidEnc(self, doc, enc, bom): reservedBits = bytearray(b'\x00\x00') with self.assertRaises(AttributeError): RTPPayload_TTML(reservedBits, doc, enc, bom)
def setUp(self): self.thisP = RTPPayload_TTML()
class TestExtension(TestCase): def setUp(self): self.thisP = RTPPayload_TTML() @given( st.tuples(st.text(), st.sampled_from(SUPPORTED_ENCODINGS), st.booleans()).filter( lambda x: len(utfEncode(x[0], x[1], x[2])) < 2**16)) def test_init(self, data): doc, encoding, bom = data reservedBits = bytearray(b'\x00\x00') newP = RTPPayload_TTML(reservedBits, doc, encoding, bom) self.assertEqual(newP.reserved, reservedBits) self.assertEqual(newP.userDataWords, doc) self.assertEqual(newP._encoding, encoding) self.assertEqual(newP._bom, bom) @given(st.text(), st.text().filter(lambda x: x not in SUPPORTED_ENCODINGS), st.booleans()) def test_init_invalidEnc(self, doc, enc, bom): reservedBits = bytearray(b'\x00\x00') with self.assertRaises(AttributeError): RTPPayload_TTML(reservedBits, doc, enc, bom) def test_reserved_default(self): self.assertEqual(self.thisP.reserved, bytearray(b'\x00\x00')) def test_reserved_notBytes(self): with self.assertRaises(AttributeError): self.thisP.reserved = "" @given(st.binary().filter(lambda x: x != bytearray(b'\x00\x00'))) def test_reserved_invalid(self, value): with self.assertRaises(ValueError): self.thisP.reserved = bytearray(value) def test_userDataWords_default(self): self.assertEqual(self.thisP.userDataWords, "") @given(st.text().filter(lambda x: len(utfEncode(x, "UTF-8")) < 2**16)) def test_userDataWords(self, doc): self.thisP.userDataWords = doc self.assertEqual(self.thisP.userDataWords, doc) def test_userDataWords_invalidType(self): with self.assertRaises(AttributeError): self.thisP.userDataWords = 0 def test_userDataWords_tooLong(self): doc = "" for x in range(2**16): doc += "a" with self.assertRaises(LengthError): self.thisP.userDataWords = doc @given( st.tuples(st.text(), st.sampled_from(SUPPORTED_ENCODINGS), st.booleans()).filter( lambda x: len(utfEncode(x[0], x[1], x[2])) < 2**16)) def test_userDataWords_encodings(self, data): doc, encoding, bom = data payload = RTPPayload_TTML(userDataWords=doc, encoding=encoding, bom=bom) self.assertEqual(payload.userDataWords, doc) self.assertEqual(payload._userDataWords, utfEncode(doc, encoding, bom)) def test_eq(self): reservedBits = bytearray(b'\x00\x00') newP = RTPPayload_TTML(reservedBits, "") self.assertEqual(newP, self.thisP) def test_bytearray_default(self): expected = bytearray(4) self.assertEqual(bytes(self.thisP), expected) newP = RTPPayload_TTML().fromBytearray(expected) self.assertEqual(newP, self.thisP) @given( st.binary(min_size=2, max_size=2).filter(lambda x: x != b'\x00\x00')) def test_fromBytearray_invalidLen(self, length): bArray = bytearray(4) bArray[2:4] = length with self.assertRaises(LengthError): RTPPayload_TTML().fromBytearray(bArray) @given(st.text()) def test_toBytearray(self, doc): self.thisP.userDataWords = doc bDoc = utfEncode(doc) expected = bytearray(2) expected += int(len(bDoc)).to_bytes(2, byteorder='big') expected += bDoc self.assertEqual(expected, self.thisP.toBytearray()) @given(st.text()) def test_fromBytearray(self, doc): expected = RTPPayload_TTML(userDataWords=doc) bDoc = utfEncode(doc) bArray = bytearray(2) bArray += int(len(bDoc)).to_bytes(2, byteorder='big') bArray += bDoc self.thisP.fromBytearray(bArray) self.assertEqual(expected, self.thisP)
def test_fromBytearray_invalidLen(self, length): bArray = bytearray(4) bArray[2:4] = length with self.assertRaises(LengthError): RTPPayload_TTML().fromBytearray(bArray)