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

Add Segment.media_Sequence #310

Merged
merged 3 commits into from
Dec 27, 2022

Conversation

bbayles
Copy link
Contributor

@bbayles bbayles commented Dec 16, 2022

Re: Issue 308, this PR adds a media_sequence attribute to Segment objects.

From RFC 8216:

Each segment in a Media Playlist has a unique integer Media Sequence
   Number.  The Media Sequence Number of the first segment in the Media
   Playlist is either 0 or declared in the Playlist ([Section 4.4.3.2](https://datatracker.ietf.org/doc/html/draft-pantos-hls-rfc8216bis#section-4.4.3.2)).
   The Media Sequence Number of every other segment is equal to the
   Media Sequence Number of the segment that precedes it plus one.

It's useful to have access to the Media Sequence number on the Segment object because this value can be used for the initialization vector (section 5.2). It's also useful for tracking changes between requests of live playlists.

@@ -174,6 174,9 @@ def _initialize_attributes(self):
for attr, param in self.simple_attributes:
setattr(self, attr, self.data.get(param))

for i, segment in enumerate(self.segments, self.media_sequence or 0):
Copy link
Contributor

Choose a reason for hiding this comment

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

I learned something new here :) I didn't know enumerate would work with two sources :)

Just out of curiosity, why does it return first the number iterator and secondly the list iterator? (since they were passed in other order)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Two tricks here.

First, enumerate has a two argument form: iterable, start_number. For us, self.segments is the iterable, since the SegmentList type can be iterated. We want to start counting from the media sequence number, so we use it as the second argument.

Second, the self.media_sequence or 0 trick. I want to handle the case where media_sequence is None and treat that ias 0, as per the HLS spec. The or operator takes the left argument if it's truthy (None isn't) and the right operator if it's not.

None or 0  # Picks 0
0 or 0  # Picks the second 0
1 or 0  # Picks 1
2 or 0  # Picks 2
# etc

Copy link
Contributor

Choose a reason for hiding this comment

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

cool ty

Copy link
Contributor

@leandromoreira leandromoreira left a comment

Choose a reason for hiding this comment

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

Another amazing piece of work! ty man

@mauricioabreu
Copy link
Member

Thank you @bbayles amazing work!
Do you mind rebasing your PR? I'm ready to merge it.

@bbayles
Copy link
Contributor Author

bbayles commented Dec 27, 2022

OK, this is rebased. Thanks!

@mauricioabreu mauricioabreu merged commit 39443ca into globocom:master Dec 27, 2022
@mauricioabreu
Copy link
Member

Thank you, @bbayles merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants