File: test_python_io.py

package info (click to toggle)
python-av 14.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,664 kB
  • sloc: python: 4,712; sh: 175; ansic: 174; makefile: 123
file content (366 lines) | stat: -rw-r--r-- 10,907 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
from __future__ import annotations

import functools
import io
import types
from io import BytesIO
from re import escape
from typing import TYPE_CHECKING

import pytest

import av

from .common import TestCase, fate_png, fate_suite, has_pillow, run_in_sandbox
from .test_encode import assert_rgb_rotate, write_rgb_rotate

if TYPE_CHECKING:
    from collections.abc import Callable


class MethodLogger:
    def __init__(self, obj: object) -> None:
        self._obj = obj
        self._log: list[tuple[str, object]] = []

    def __getattr__(self, name: str) -> object:
        def _method(name: str, meth: Callable, *args, **kwargs) -> object:
            self._log.append((name, args))
            return meth(*args, **kwargs)

        value = getattr(self._obj, name)
        if isinstance(
            value,
            (
                types.MethodType,
                types.FunctionType,
                types.BuiltinFunctionType,
                types.BuiltinMethodType,
            ),
        ):
            return functools.partial(_method, name, value)
        else:
            self._log.append(("__getattr__", (name,)))
            return value

    def _filter(self, type_: str) -> list[tuple[str, object]]:
        return [log for log in self._log if log[0] == type_]


class BrokenBuffer(BytesIO):
    """
    Buffer which can be "broken" to simulate an I/O error.
    """

    broken = False

    def write(self, data):
        if self.broken:
            raise OSError("It's broken")
        else:
            return super().write(data)


class ReadOnlyBuffer:
    """
    Minimal buffer which *only* implements the read() method.
    """

    def __init__(self, data) -> None:
        self.data = data

    def read(self, n):
        data = self.data[0:n]
        self.data = self.data[n:]
        return data


class ReadOnlyPipe(BytesIO):
    """
    Buffer which behaves like a readable pipe.
    """

    @property
    def name(self) -> int:
        return 123

    def seekable(self) -> bool:
        return False

    def writable(self) -> bool:
        return False


class WriteOnlyPipe(BytesIO):
    """
    Buffer which behaves like a writable pipe.
    """

    @property
    def name(self) -> int:
        return 123

    def readable(self) -> bool:
        return False

    def seekable(self) -> bool:
        return False


def read(
    fh: io.BufferedReader | BytesIO | ReadOnlyBuffer, seekable: bool = True
) -> None:
    wrapped = MethodLogger(fh)

    with av.open(wrapped, "r") as container:
        assert container.format.name == "mpegts"
        assert container.format.long_name == "MPEG-TS (MPEG-2 Transport Stream)"
        assert len(container.streams) == 1
        if seekable:
            assert container.size == 800000
        assert container.metadata == {}

    # Check method calls.
    assert wrapped._filter("read")
    if seekable:
        assert wrapped._filter("seek")


def write(fh: io.BufferedWriter | BytesIO) -> None:
    wrapped = MethodLogger(fh)

    with av.open(wrapped, "w", "mp4") as container:
        write_rgb_rotate(container)

    # Check method calls.
    assert wrapped._filter("write")
    assert wrapped._filter("seek")


# Using a custom protocol will avoid the DASH muxer detecting or defaulting to a
# file: protocol and enabling the use of temporary files and renaming.
CUSTOM_IO_PROTOCOL = "pyavtest://"


class CustomIOLogger:
    """Log calls to open a file as well as method calls on the files"""

    def __init__(self) -> None:
        self._log: list[tuple[object, dict]] = []
        self._method_log: list[MethodLogger] = []

    def __call__(self, *args, **kwargs) -> MethodLogger:
        self._log.append((args, kwargs))
        self._method_log.append(self.io_open(*args, **kwargs))
        return self._method_log[-1]

    def io_open(self, url: str, flags, options: object) -> MethodLogger:
        # Remove the protocol prefix to reveal the local filename
        if CUSTOM_IO_PROTOCOL in url:
            url = url.split(CUSTOM_IO_PROTOCOL, 1)[1]

        if (flags & 3) == 3:
            mode = "r+b"
        elif (flags & 1) == 1:
            mode = "rb"
        elif (flags & 2) == 2:
            mode = "wb"
        else:
            raise RuntimeError(f"Unsupported io open mode {flags}")

        return MethodLogger(open(url, mode))


class TestPythonIO(TestCase):
    def test_basic_errors(self) -> None:
        self.assertRaises(Exception, av.open, None)
        self.assertRaises(Exception, av.open, None, "w")

    def test_reading_from_buffer(self) -> None:
        with open(fate_suite("mpeg2/mpeg2_field_encoding.ts"), "rb") as fh:
            buf = BytesIO(fh.read())
        read(buf, seekable=True)

    def test_reading_from_buffer_no_seek(self) -> None:
        with open(fate_suite("mpeg2/mpeg2_field_encoding.ts"), "rb") as fh:
            buf = ReadOnlyBuffer(fh.read())
        read(buf, seekable=False)

    def test_reading_from_file(self) -> None:
        with open(fate_suite("mpeg2/mpeg2_field_encoding.ts"), "rb") as fh:
            read(fh, seekable=True)

    def test_reading_from_pipe_readonly(self) -> None:
        with open(fate_suite("mpeg2/mpeg2_field_encoding.ts"), "rb") as fh:
            buf = ReadOnlyPipe(fh.read())
        read(buf, seekable=False)

    def test_reading_from_write_readonly(self) -> None:
        with open(fate_suite("mpeg2/mpeg2_field_encoding.ts"), "rb") as fh:
            buf = WriteOnlyPipe(fh.read())

        msg = escape("File object has no read() method, or readable() returned False.")
        with pytest.raises(ValueError, match=msg):
            read(buf, seekable=False)

    def test_writing_to_buffer(self) -> None:
        buf = BytesIO()

        write(buf)

        # Check contents.
        assert buf.tell()
        buf.seek(0)
        with av.open(buf, "r") as container:
            assert_rgb_rotate(self, container)

    def test_writing_to_buffer_broken(self) -> None:
        buf = BrokenBuffer()

        with pytest.raises(OSError):
            with av.open(buf, "w", "mp4") as container:
                write_rgb_rotate(container)

                # break I/O
                buf.broken = True

    def test_writing_to_buffer_broken_with_close(self) -> None:
        buf = BrokenBuffer()

        with av.open(buf, "w", "mp4") as container:
            write_rgb_rotate(container)

            # break I/O
            buf.broken = True

            # try to close file
            with pytest.raises(OSError):
                container.close()

    @run_in_sandbox
    def test_writing_to_custom_io_dash(self) -> None:
        # Custom I/O that opens file and logs calls
        wrapped_custom_io = CustomIOLogger()

        output_filename = "custom_io_output.mpd"

        # Write a DASH package using the custom IO. Prefix the name with CUSTOM_IO_PROTOCOL to
        # avoid temporary file and renaming.
        with av.open(
            CUSTOM_IO_PROTOCOL + output_filename, "w", io_open=wrapped_custom_io
        ) as container:
            write_rgb_rotate(container)

        # Check that at least 3 files were opened using the custom IO:
        #   "output_filename", init-stream0.m4s and chunk-stream-0x.m4s
        assert len(wrapped_custom_io._log) >= 3
        assert len(wrapped_custom_io._method_log) >= 3

        # Check that all files were written to
        all_write = all(
            method_log._filter("write") for method_log in wrapped_custom_io._method_log
        )
        assert all_write

        # Check that all files were closed
        all_closed = all(
            method_log._filter("close") for method_log in wrapped_custom_io._method_log
        )
        assert all_closed

        # Check contents.
        # Note that the dash demuxer doesn't support custom I/O.
        with av.open(output_filename, "r") as container:
            assert_rgb_rotate(self, container, is_dash=True)

    def test_writing_to_custom_io_image2(self) -> None:
        if not has_pillow:
            pytest.skip()

        import PIL.Image as Image

        # Custom I/O that opens file and logs calls
        wrapped_custom_io = CustomIOLogger()

        image = Image.open(fate_png())
        input_frame = av.VideoFrame.from_image(image)

        frame_count = 10
        sequence_filename = self.sandboxed("test%d.png")
        width = 160
        height = 90

        # Write a PNG image sequence using the custom IO
        with av.open(
            sequence_filename, "w", "image2", io_open=wrapped_custom_io
        ) as output:
            stream = output.add_stream("png")
            stream.width = width
            stream.height = height
            stream.pix_fmt = "rgb24"

            for _ in range(frame_count):
                for packet in stream.encode(input_frame):
                    output.mux(packet)

        # Check that "frame_count" files were opened using the custom IO
        assert len(wrapped_custom_io._log) == frame_count
        assert len(wrapped_custom_io._method_log) == frame_count

        # Check that all files were written to
        all_write = all(
            method_log._filter("write") for method_log in wrapped_custom_io._method_log
        )
        assert all_write

        # Check that all files were closed
        all_closed = all(
            method_log._filter("close") for method_log in wrapped_custom_io._method_log
        )
        assert all_closed

        # Check contents.
        with av.open(sequence_filename, "r", "image2") as container:
            assert len(container.streams) == 1
            assert isinstance(container.streams[0], av.video.stream.VideoStream)

            stream = container.streams[0]
            assert stream.duration == frame_count
            assert stream.type == "video"

            # codec context properties
            assert stream.codec.name == "png"
            assert stream.format.name == "rgb24"
            assert stream.format.width == width
            assert stream.format.height == height

    def test_writing_to_file(self) -> None:
        path = self.sandboxed("writing.mp4")

        with open(path, "wb") as fh:
            write(fh)

        # Check contents.
        with av.open(path) as container:
            assert_rgb_rotate(self, container)

    def test_writing_to_pipe_readonly(self) -> None:
        buf = ReadOnlyPipe()
        with pytest.raises(
            ValueError,
            match=escape(
                "File object has no write() method, or writable() returned False."
            ),
        ) as cm:
            write(buf)

    def test_writing_to_pipe_writeonly(self) -> None:
        av.logging.set_level(av.logging.VERBOSE)

        buf = WriteOnlyPipe()
        with pytest.raises(
            ValueError, match=escape("[mp4] muxer does not support non seekable output")
        ):
            write(buf)

        av.logging.set_level(None)