Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add version_matching module #341

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ local/
.cache
.python-version
.idea/
.vscode/
venv/
pyvenv.cfg
8 changes: 6 additions & 2 deletions m3u8/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
pass


from m3u8 import protocol
from m3u8 import protocol, version_matching

"""
http://tools.ietf.org/html/draft-pantos-http-live-streaming-08#section-3.2
Expand Down Expand Up @@ -75,8 +75,12 @@ def parse(content, strict=False, custom_tags_parser=None):
"current_segment_map": None,
}

lines = string_to_lines(content)
if strict:
davigsousa marked this conversation as resolved.
Show resolved Hide resolved
version_matching.validate(lines)

lineno = 0
for line in string_to_lines(content):
for line in lines:
lineno += 1
line = line.strip()

Expand Down
43 changes: 43 additions & 0 deletions m3u8/version_matching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from typing import Callable, List

from m3u8 import protocol
from m3u8.version_matching_rules import *


class VersionMatchingError(Exception):
def __init__(self, lineno, line):
self.lineno = lineno
self.line = line

def __str__(self):
return f"Version matching error at line {self.lineno}: {self.line}"


def get_version(file_lines: List[str]):
for line in file_lines:
if line.startswith(protocol.ext_x_version):
version = line.split(":")[1]
return float(version)

return None


def valid_in_all_rules(line_number: int, line: str, version: float):
rules: List[Callable[[str, float], bool]] = [
valid_iv_in_EXT_X_KEY,
valid_floating_point_EXTINF,
valid_EXT_X_BYTERANGE_or_EXT_X_I_FRAMES_ONLY,
]

for rule in rules:
if not rule(line, version):
raise VersionMatchingError(line_number, line)
davigsousa marked this conversation as resolved.
Show resolved Hide resolved


def validate(file_lines: List[str]):
found_version = get_version(file_lines)
if found_version is None:
return

for number, line in enumerate(file_lines):
valid_in_all_rules(number, line, found_version)
44 changes: 44 additions & 0 deletions m3u8/version_matching_rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from m3u8 import protocol


# You must use at least protocol version 2 if you have IV in EXT-X-KEY.
def valid_iv_in_EXT_X_KEY(line: str, version: float):
if not protocol.ext_x_key in line:
return True

if "IV" in line:
return version >= 2

return True


# You must use at least protocol version 3 if you have floating point EXTINF duration values.
davigsousa marked this conversation as resolved.
Show resolved Hide resolved
def valid_floating_point_EXTINF(line: str, version: float):
if not protocol.extinf in line:
return True

chunks = line.replace(protocol.extinf + ":", "").split(",", 1)
duration = chunks[0]

def is_number(value: str):
try:
float(value)
return True
except:
return False

def is_floating_number(value: str):
return is_number(value) and "." in value

if is_floating_number(duration):
return version >= 3

return is_number(duration)


# You must use at least protocol version 4 if you have EXT-X-BYTERANGE or EXT-X-IFRAME-ONLY.
def valid_EXT_X_BYTERANGE_or_EXT_X_I_FRAMES_ONLY(line: str, version: float):
if not protocol.ext_x_byterange in line and not protocol.ext_i_frames_only in line:
return True

return version >= 4
28 changes: 28 additions & 0 deletions tests/invalid_versioned_playlists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Should have at least version 2 if you have IV in EXT-X-KEY.
M3U8_RULE_IV = """
#EXTM3U
#EXT-X-VERSION: 1
#EXT-X-KEY: METHOD=AES-128, IV=0x123456789ABCDEF0123456789ABCDEF0, URI="https://example.com/key.bin"
#EXT-X-TARGETDURATION: 10
#EXTINF: 10.0,
https://example.com/segment1.ts
"""

# Should have at least version 3 if you have floating point EXTINF duration values.
M3U8_RULE_FLOATING_POINT = """
#EXTM3U
#EXT-X-VERSION: 2
#EXT-X-TARGETDURATION: 10
#EXTINF: 10.5,
https://example.com/segment1.ts
"""

# Should have at least version 4 if you have EXT-X-BYTERANGE or EXT-X-IFRAME-ONLY.
M3U8_RULE_BYTE_RANGE = """
#EXTM3U
#EXT-X-VERSION: 3
#EXT-X-BYTERANGE: 200000@1000
#EXT-X-TARGETDURATION: 10
#EXTINF: 10.0,
https://example.com/segment1.ts
"""
24 changes: 24 additions & 0 deletions tests/test_invalid_versioned_playlists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# coding: utf-8
# Copyright 2014 Globo.com Player authors. All rights reserved.
# Use of this source code is governed by a MIT License
# license that can be found in the LICENSE file.

import invalid_versioned_playlists
import pytest

import m3u8


@pytest.mark.xfail
def test_should_fail_if_iv_in_EXT_X_KEY_and_version_less_than_2():
m3u8.parse(invalid_versioned_playlists.M3U8_RULE_IV)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe we should assert the exception or at least partially the error message/help

def test_raises():
    with pytest.raises(Exception) as exc_info:   
        raise Exception('some info')
    # these asserts are identical; you can use either one   
    assert exc_info.value.args[0] == 'some info'
    assert str(exc_info.value) == 'some info'

ref https://stackoverflow.com/a/23514853

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



@pytest.mark.xfail
def test_should_fail_if_floating_point_EXTINF_and_version_less_than_3():
m3u8.parse(invalid_versioned_playlists.M3U8_RULE_FLOATING_POINT)


@pytest.mark.xfail
def test_should_fail_if_EXT_X_BYTERANGE_or_EXT_X_I_FRAMES_ONLY_and_version_less_than_4():
m3u8.parse(invalid_versioned_playlists.M3U8_RULE_BYTE_RANGE)
5 changes: 4 additions & 1 deletion tests/test_strict_validations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
# Use of this source code is governed by a MIT License
# license that can be found in the LICENSE file.

import invalid_versioned_playlists
import pytest

import m3u8


@pytest.mark.xfail
def test_should_fail_if_first_line_not_EXTM3U():
Expand Down Expand Up @@ -33,7 +36,7 @@ def test_should_fail_if_EXT_X_MEDIA_SEQUENCE_is_not_a_number():

@pytest.mark.xfail
def test_should_validate_supported_EXT_X_VERSION():
assert 0
m3u8.parse(invalid_versioned_playlists.M3U8_RULE_IV)


@pytest.mark.xfail
Expand Down
70 changes: 70 additions & 0 deletions tests/test_version_matching_rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from m3u8.version_matching_rules import (
valid_EXT_X_BYTERANGE_or_EXT_X_I_FRAMES_ONLY,
valid_floating_point_EXTINF,
valid_iv_in_EXT_X_KEY,
)


def test_invalid_iv_in_EXT_X_KEY():
result = valid_iv_in_EXT_X_KEY(
"#EXT-X-KEY: METHOD=AES-128, IV=0x123456789ABCDEF0123456789ABCDEF0, URI=https://example.com/key.bin",
1,
)
assert result == False


def test_valid_iv_in_EXT_X_KEY():
result = valid_iv_in_EXT_X_KEY(
"#EXT-X-KEY: METHOD=AES-128, URI=https://example.com/key.bin", 1
)
assert result == True
result = valid_iv_in_EXT_X_KEY(
"#EXT-X-KEY: METHOD=AES-128, IV=0x123456789ABCDEF0123456789ABCDEF0, URI=https://example.com/key.bin",
2,
)
assert result == True
result = valid_iv_in_EXT_X_KEY(
"#EXT-X-KEY: METHOD=AES-128, IV=0x123456789ABCDEF0123456789ABCDEF0, URI=https://example.com/key.bin",
3,
)
assert result == True


def test_invalid_floating_point_EXTINF():
result = valid_floating_point_EXTINF("#EXTINF: 10.5,", 2)
assert result == False
result = valid_floating_point_EXTINF("#EXTINF: A,", 3)
assert result == False


def test_valid_floating_point_EXTINF():
result = valid_floating_point_EXTINF("#EXTINF: 10,", 2)
assert result == True
result = valid_floating_point_EXTINF("#EXTINF: 10.5,", 3)
assert result == True
result = valid_floating_point_EXTINF("#EXTINF: 10.5,", 4)
assert result == True


def test_invalid_EXT_X_BYTERANGE_or_EXT_X_I_FRAMES_ONLY():
result = valid_EXT_X_BYTERANGE_or_EXT_X_I_FRAMES_ONLY(
"#EXT-X-BYTERANGE: 200000@1000", 3
)
assert result == False
result = valid_EXT_X_BYTERANGE_or_EXT_X_I_FRAMES_ONLY("#EXT-X-I-FRAMES-ONLY", 3)
assert result == False


def test_valid_EXT_X_BYTERANGE_or_EXT_X_I_FRAMES_ONLY():
result = valid_EXT_X_BYTERANGE_or_EXT_X_I_FRAMES_ONLY(
"#EXT-X-BYTERANGE: 200000@1000", 4
)
assert result == True
result = valid_EXT_X_BYTERANGE_or_EXT_X_I_FRAMES_ONLY("#EXT-X-I-FRAMES-ONLY", 4)
assert result == True
result = valid_EXT_X_BYTERANGE_or_EXT_X_I_FRAMES_ONLY(
"#EXT-X-BYTERANGE: 200000@1000", 5
)
assert result == True
result = valid_EXT_X_BYTERANGE_or_EXT_X_I_FRAMES_ONLY("#EXT-X-I-FRAMES-ONLY", 5)
assert result == True