Skip to content

Commit

Permalink
Fix globocom#192 - correctly cascade base_path updates
Browse files Browse the repository at this point in the history
  • Loading branch information
davemevans authored and mauricioabreu committed Feb 18, 2020
1 parent 4d79a58 commit d30f907
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
31 changes: 28 additions & 3 deletions m3u8/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ def base_uri(self, new_base_uri):
self._base_uri = new_base_uri
self.media.base_uri = new_base_uri
self.playlists.base_uri = new_base_uri
self.iframe_playlists.base_uri = new_base_uri
self.segments.base_uri = new_base_uri
self.rendition_reports.base_uri = new_base_uri
for key in self.keys:
if key:
key.base_uri = new_base_uri
Expand Down Expand Up @@ -249,6 +251,8 @@ def _update_base_path(self):
self.media.base_path = self._base_path
self.segments.base_path = self._base_path
self.playlists.base_path = self._base_path
self.iframe_playlists.base_path = self._base_path
self.rendition_reports.base_path = self._base_path


def add_playlist(self, playlist):
Expand Down Expand Up @@ -411,7 +415,7 @@ def __init__(self, uri=None, base_uri=None, program_date_time=None, current_prog
self.uri = uri
self.duration = duration
self.title = title
self.base_uri = base_uri
self._base_uri = base_uri
self.byterange = byterange
self.program_date_time = program_date_time
self.current_program_date_time = current_program_date_time
Expand All @@ -422,9 +426,9 @@ def __init__(self, uri=None, base_uri=None, program_date_time=None, current_prog
self.scte35 = scte35
self.scte35_duration = scte35_duration
self.key = keyobject
self.parts = PartialSegmentList( [ PartialSegment(base_uri=self.base_uri, **partial) for partial in parts ] if parts else [] )
self.parts = PartialSegmentList( [ PartialSegment(base_uri=self._base_uri, **partial) for partial in parts ] if parts else [] )
if init_section is not None:
self.init_section = InitializationSection(self.base_uri, **init_section)
self.init_section = InitializationSection(self._base_uri, **init_section)
else:
self.init_section = None

Expand Down Expand Up @@ -492,6 +496,27 @@ def dumps(self, last_segment):
def __str__(self):
return self.dumps(None)

@property
def base_path(self):
return super(Segment, self).base_path

@base_path.setter
def base_path(self, newbase_path):
super(Segment, self.__class__).base_path.fset(self, newbase_path)
self.parts.base_path = newbase_path
if self.init_section is not None:
self.init_section.base_path = newbase_path

@property
def base_uri(self):
return self._base_uri

@base_uri.setter
def base_uri(self, newbase_uri):
self._base_uri = newbase_uri
self.parts.base_uri = newbase_uri
if self.init_section is not None:
self.init_section.base_uri = newbase_uri

class SegmentList(list, GroupedBasePathMixin):

Expand Down
40 changes: 37 additions & 3 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1099,15 +1099,17 @@ def test_add_rendition_report_to_playlist():

obj.add_rendition_report(
RenditionReport(
base_uri='',
base_uri=None,
uri='../1M/waitForMSN.php',
last_msn=273,
last_part=0
)
)

obj.base_path = 'http://localhost/test'

result = obj.dumps()
expected = '#EXT-X-RENDITION-REPORT:URI="../1M/waitForMSN.php",LAST-MSN=273,LAST-PART=0'
expected = '#EXT-X-RENDITION-REPORT:URI="http://localhost/test/waitForMSN.php",LAST-MSN=273,LAST-PART=0'

assert expected in result

Expand All @@ -1119,7 +1121,7 @@ def test_add_part_to_segment():

obj.add_part(
PartialSegment(
'',
None,
'filePart271.0.ts',
0.33334
)
Expand Down Expand Up @@ -1188,6 +1190,38 @@ def test_endswith_newline():

assert manifest.endswith('#EXT-X-ENDLIST\n')

def test_init_section_base_path_update():
obj = m3u8.M3U8(playlists.MULTIPLE_MAP_URI_PLAYLIST)

assert obj.segments[0].init_section.uri == 'init1.mp4'

obj.base_path = 'http://localhost/base_path'
obj.base_uri = 'http://localhost/base_uri'

assert obj.segments[0].init_section.uri == 'http://localhost/base_path/init1.mp4'
assert obj.segments[0].init_section.base_uri == 'http://localhost/base_uri'

def test_iframe_playlists_base_path_update():
obj = m3u8.M3U8(playlists.VARIANT_PLAYLIST_WITH_IFRAME_PLAYLISTS)

assert obj.iframe_playlists[0].uri == 'video-800k-iframes.m3u8'
assert obj.iframe_playlists[0].base_uri == None

obj.base_path = 'http://localhost/base_path'
obj.base_uri = 'http://localhost/base_uri'

assert obj.iframe_playlists[0].uri == 'http://localhost/base_path/video-800k-iframes.m3u8'
assert obj.iframe_playlists[0].base_uri == 'http://localhost/base_uri'

def test_partial_segment_base_path_update():
obj = m3u8.M3U8(playlists.LOW_LATENCY_DELTA_UPDATE_PLAYLIST)

obj.base_path = 'http://localhost/base_path'
obj.base_uri = 'http://localhost/base_uri'

assert obj.segments[2].parts[0].uri == 'http://localhost/base_path/filePart271.0.ts'
assert obj.segments[2].parts[0].base_uri == 'http://localhost/base_uri'

# custom asserts


Expand Down

0 comments on commit d30f907

Please sign in to comment.