-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat: support temporal media fragment URIs #7721
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
Open
christriants
wants to merge
6
commits into
video-dev:master
Choose a base branch
from
christriants:feat/temporal-media-fragments
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
81bca3e
Support temporal media fragment URIs
christriants 7ce13b6
Create media-fragment-controller for temporal support
christriants 7c466ce
Reset at start of onManifestLoading
christriants fe60b4b
Split without regex from parseNptTime
christriants 2011d51
Add percent-decoding support to MediaFragmentController
christriants b229267
Fix media fragment parser to handle last occurrence and npt prefix
christriants File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| import { Events } from '../events'; | ||
| import { Logger } from '../utils/logger'; | ||
| import { parseMediaFragment } from '../utils/media-fragment-parser'; | ||
| import type Hls from '../hls'; | ||
| import type { ComponentAPI } from '../types/component-api'; | ||
| import type { | ||
| ManifestLoadingData, | ||
| MediaAttachingData, | ||
| MediaDetachingData, | ||
| } from '../types/events'; | ||
|
|
||
| /** | ||
| * MediaFragmentController | ||
| * | ||
| * Handles W3C Media Fragments URI temporal dimension (#t=start,end). | ||
| * - Parses fragment from URL | ||
| * - Sets start position | ||
| * - Pauses at end time (one-time) | ||
| * - Removes listeners after pause | ||
| */ | ||
| export default class MediaFragmentController | ||
| extends Logger | ||
| implements ComponentAPI | ||
| { | ||
| private hls: Hls; | ||
| private media: HTMLMediaElement | null = null; | ||
| private fragmentEnd: number | null = null; | ||
| private endReached: boolean = false; | ||
| private _boundOnTimeUpdate: () => void; | ||
| private _boundOnSeeked: () => void; | ||
|
|
||
| constructor(hls: Hls) { | ||
| super('media-fragment', hls.logger); | ||
| this.hls = hls; | ||
| this._boundOnTimeUpdate = this.onTimeUpdate.bind(this); | ||
| this._boundOnSeeked = this.onSeeked.bind(this); | ||
| this.registerListeners(); | ||
| } | ||
|
|
||
| private registerListeners() { | ||
| const { hls } = this; | ||
| hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this); | ||
| hls.on(Events.MEDIA_ATTACHING, this.onMediaAttaching, this); | ||
| hls.on(Events.MEDIA_DETACHING, this.onMediaDetaching, this); | ||
| } | ||
|
|
||
| private unregisterListeners() { | ||
| const { hls } = this; | ||
| hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this); | ||
| hls.off(Events.MEDIA_ATTACHING, this.onMediaAttaching, this); | ||
| hls.off(Events.MEDIA_DETACHING, this.onMediaDetaching, this); | ||
| } | ||
|
|
||
| private onManifestLoading( | ||
| event: Events.MANIFEST_LOADING, | ||
| data: ManifestLoadingData, | ||
| ) { | ||
| // Reset state at top, before early return | ||
| this.fragmentEnd = null; | ||
| this.endReached = false; | ||
| this.detachMediaListeners(); | ||
| if (!data.url.includes('#')) { | ||
| return; | ||
| } | ||
| const hashIndex = data.url.indexOf('#'); | ||
| const fragment = data.url.substring(hashIndex + 1); | ||
| let decodedFragment: string; | ||
| try { | ||
| decodedFragment = decodeURIComponent(fragment); | ||
| } catch (e) { | ||
| return; | ||
| } | ||
| const { temporalFragment } = parseMediaFragment( | ||
| data.url.substring(0, hashIndex + 1) + decodedFragment, | ||
| ); | ||
| if (temporalFragment) { | ||
| if (temporalFragment.start !== undefined) { | ||
| this.hls.config.startPosition = temporalFragment.start; | ||
| } | ||
| if (temporalFragment.end !== undefined) { | ||
| this.fragmentEnd = temporalFragment.end; | ||
| } | ||
| this.hls.trigger(Events.MEDIA_FRAGMENT_PARSED, { | ||
| start: temporalFragment.start, | ||
| end: temporalFragment.end, | ||
| }); | ||
| if (this.media && this.fragmentEnd !== null) { | ||
| this.attachMediaListeners(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private onMediaAttaching( | ||
| event: Events.MEDIA_ATTACHING, | ||
| data: MediaAttachingData, | ||
| ) { | ||
| this.media = data.media; | ||
| if (this.fragmentEnd !== null && !this.endReached) { | ||
| this.attachMediaListeners(); | ||
| } | ||
| } | ||
|
|
||
| private onMediaDetaching( | ||
| event: Events.MEDIA_DETACHING, | ||
| data: MediaDetachingData, | ||
| ) { | ||
| this.detachMediaListeners(); | ||
| this.media = null; | ||
| } | ||
|
|
||
| private attachMediaListeners() { | ||
| if (!this.media) { | ||
| return; | ||
| } | ||
| this.media.addEventListener('timeupdate', this._boundOnTimeUpdate); | ||
| this.media.addEventListener('seeked', this._boundOnSeeked); | ||
| } | ||
|
|
||
| private detachMediaListeners() { | ||
| if (this.media) { | ||
| this.media.removeEventListener('timeupdate', this._boundOnTimeUpdate); | ||
| this.media.removeEventListener('seeked', this._boundOnSeeked); | ||
| } | ||
| } | ||
|
|
||
| private onTimeUpdate() { | ||
| this.checkFragmentEnd(); | ||
| } | ||
|
|
||
| private onSeeked() { | ||
| const { media } = this; | ||
| if (media) { | ||
| this.checkFragmentEnd(media.currentTime); | ||
|
robwalch marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| private checkFragmentEnd(seekTime?: number) { | ||
| const { media, fragmentEnd, endReached } = this; | ||
| if (!media || fragmentEnd === null || endReached) { | ||
| return; | ||
| } | ||
| const time = seekTime ?? media.currentTime; | ||
| if (time >= fragmentEnd && (!media.paused || seekTime !== undefined)) { | ||
| this.log( | ||
| `Reached media fragment end at ${time.toFixed(3)} (end: ${fragmentEnd.toFixed(3)})`, | ||
| ); | ||
| this.endReached = true; | ||
| media.pause(); | ||
| this.detachMediaListeners(); | ||
| this.hls.trigger(Events.MEDIA_FRAGMENT_END, {}); | ||
| } | ||
| } | ||
|
|
||
| destroy() { | ||
| this.unregisterListeners(); | ||
| this.detachMediaListeners(); | ||
| this.media = null; | ||
| // @ts-ignore | ||
| this.hls = null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.