Skip to content

Commit c4ed536

Browse files
committed
Test writing packets to data stream
1 parent 2367222 commit c4ed536

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

tests/test_packet.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,67 @@
1+
import fractions
2+
import io
3+
import json
14
import struct
25
from typing import get_args
36
from unittest import SkipTest
47

8+
import numpy
9+
510
import av
611

712
from .common import fate_suite, sandboxed
813

914

15+
class TestDataStreams:
16+
def generate_container_with_data_packets(self):
17+
file = io.BytesIO()
18+
packet_datas_expected = dict[fractions.Fraction, bytes]()
19+
20+
container = av.open(file, format="mp4", mode="w")
21+
stream = container.add_data_stream("bin_data")
22+
vstream = container.add_stream("h264")
23+
24+
for i in range(10):
25+
packet_data = json.dumps("This may not work", ensure_ascii=False).encode(
26+
"utf-8"
27+
)
28+
if i % 2 == 0:
29+
packet_data = b"This works fine"
30+
packet = av.Packet(packet_data)
31+
vframe = av.VideoFrame.from_ndarray(
32+
numpy.random.randint(0, 255, size=(120, 120, 3), dtype=numpy.uint8)
33+
)
34+
packet.pts = i
35+
packet.dts = i
36+
packet.duration = 1
37+
packet.stream = stream
38+
container.mux(packet)
39+
container.mux(vstream.encode(vframe))
40+
# NOTE test passes if this is used
41+
# packet_datas_expected[packet.pts * packet.time_base] = bytes(packet_data)
42+
43+
container.close()
44+
45+
container = av.open(file, format="mp4", mode="r")
46+
return container, packet_datas_expected
47+
48+
def test_data_packet_bytes(self):
49+
container, packet_datas_expected = self.generate_container_with_data_packets()
50+
51+
packet_datas = dict[fractions.Fraction, bytes]()
52+
for packet in container.demux(container.streams.data[0]):
53+
if packet.pts is None:
54+
continue
55+
assert bytes(packet) in (
56+
b"This works fine",
57+
json.dumps("This may not work", ensure_ascii=False).encode("utf-8"),
58+
), bytes(packet)
59+
# NOTE test passes if this is used
60+
# packet_datas[packet.pts * packet.time_base] = bytes(packet)
61+
62+
assert packet_datas == packet_datas_expected
63+
64+
1065
class TestProperties:
1166
def test_is_keyframe(self) -> None:
1267
with av.open(fate_suite("h264/interlaced_crop.mp4")) as container:

0 commit comments

Comments
 (0)