File: test_dash.py

package info (click to toggle)
streamlink 8.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,564 kB
  • sloc: python: 51,188; sh: 184; makefile: 152
file content (108 lines) | stat: -rw-r--r-- 4,440 bytes parent folder | download | duplicates (2)
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
from __future__ import annotations

from typing import TYPE_CHECKING
from unittest.mock import Mock, call

import pytest

from streamlink.plugin.plugin import BIT_RATE_WEIGHT_RATIO, LOW_PRIORITY, NO_PRIORITY, NORMAL_PRIORITY
from streamlink.plugins.dash import MPEGDASH
from tests.plugins import PluginCanHandleUrl


if TYPE_CHECKING:
    from streamlink.session import Streamlink


class TestPluginCanHandleUrlMPEGDASH(PluginCanHandleUrl):
    __plugin__ = MPEGDASH

    should_match_groups = [
        # implicit DASH URLs
        ("example.com/foo.mpd", {"url": "example.com/foo.mpd"}),
        ("example.com/foo.MPD", {"url": "example.com/foo.MPD"}),
        ("example.com/foo.mpd?bar", {"url": "example.com/foo.mpd?bar"}),
        ("http://example.com/foo.mpd", {"url": "http://example.com/foo.mpd"}),
        ("http://example.com/foo.mpd?bar", {"url": "http://example.com/foo.mpd?bar"}),
        ("https://example.com/foo.mpd", {"url": "https://example.com/foo.mpd"}),
        ("https://example.com/foo.mpd?bar", {"url": "https://example.com/foo.mpd?bar"}),
        ("file://foo.mpd", {"url": "file://foo.mpd"}),
        ("file:///foo.mpd", {"url": "file:///foo.mpd"}),
        ("file://../foo.mpd", {"url": "file://../foo.mpd"}),
        # explicit DASH URLs with protocol prefix
        ("dash://example.com/foo?bar", {"url": "example.com/foo?bar"}),
        ("dash://http://example.com/foo?bar", {"url": "http://example.com/foo?bar"}),
        ("dash://https://example.com/foo?bar", {"url": "https://example.com/foo?bar"}),
        ("dash://file://foo", {"url": "file://foo"}),
        # optional parameters
        ("example.com/foo.mpd?bar abc=def", {"url": "example.com/foo.mpd?bar", "params": "abc=def"}),
        ("http://example.com/foo.mpd?bar abc=def", {"url": "http://example.com/foo.mpd?bar", "params": "abc=def"}),
        ("https://example.com/foo.mpd?bar abc=def", {"url": "https://example.com/foo.mpd?bar", "params": "abc=def"}),
        ("dash://https://example.com/foo?bar abc=def", {"url": "https://example.com/foo?bar", "params": "abc=def"}),
    ]

    should_not_match = [
        # implicit DASH URLs must have their path end with ".mpd"
        "example.mpd",
        "/example.mpd",
        "example.com/mpd",
        "example.com/MPD",
        "example.com/mpd abc=def",
        "example.com/foo.mpd,bar",
        "example.com/foo.mpd,bar abc=def",
        # missing parameters
        "example.com/foo.mpd ",
        "dash://example.com/foo ",
    ]


@pytest.mark.parametrize(
    ("url", "priority"),
    [
        ("http://example.com/foo.mpd", LOW_PRIORITY),
        ("http://example.com/foo.MPD", LOW_PRIORITY),
        ("dash://http://example.com/foo.mpd", NORMAL_PRIORITY),
        ("dash://http://example.com/bar", NORMAL_PRIORITY),
        ("http://example.com/bar", NO_PRIORITY),
    ],
)
def test_priority(url, priority):
    assert next((matcher.priority for matcher in MPEGDASH.matchers if matcher.pattern.match(url)), NO_PRIORITY) == priority


@pytest.mark.parametrize(
    ("url", "expected"),
    [
        ("example.com/foo.mpd", "https://example.com/foo.mpd"),
        ("http://example.com/foo.mpd", "http://example.com/foo.mpd"),
        ("https://example.com/foo.mpd", "https://example.com/foo.mpd"),
        ("file://foo.mpd", "file://foo.mpd"),
        ("file:///foo.mpd", "file:///foo.mpd"),
        ("file://../foo.mpd", "file://../foo.mpd"),
        ("dash://example.com/foo", "https://example.com/foo"),
        ("dash://http://example.com/foo", "http://example.com/foo"),
        ("dash://https://example.com/foo", "https://example.com/foo"),
    ],
)
def test_get_streams(monkeypatch: pytest.MonkeyPatch, session: Streamlink, url, expected):
    mock_parse_manifest = Mock(return_value={})
    monkeypatch.setattr("streamlink.stream.dash.DASHStream.parse_manifest", mock_parse_manifest)

    p = MPEGDASH(session, url)
    p.streams()

    assert mock_parse_manifest.call_args_list == [call(session, expected)]


@pytest.mark.parametrize(
    ("weight", "expected"),
    [
        ("720p", pytest.approx((720, "pixels"))),
        ("1080p", pytest.approx((1080, "pixels"))),
        ("720p+a128k", pytest.approx((720 + 128, "pixels"))),
        ("720p+a0k", pytest.approx((720, "pixels"))),
        ("a128k", pytest.approx((128 / BIT_RATE_WEIGHT_RATIO, "bitrate"))),
    ],
)
def test_stream_weight(weight: str, expected):
    assert MPEGDASH.stream_weight(weight) == expected