-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(player): improve stream type detection
- Loading branch information
Showing
9 changed files
with
100 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,45 @@ | ||
import type { MediaStreamType } from '../core'; | ||
|
||
export function resolveStreamTypeFromHLSManifest( | ||
manifestSrc: string, | ||
requestInit?: RequestInit, | ||
): Promise<MediaStreamType> { | ||
return fetch(manifestSrc, requestInit) | ||
.then((res) => res.text()) | ||
.then((manifest) => { | ||
const renditionURI = resolveHLSRenditionURI(manifest); | ||
if (renditionURI) return resolveStreamTypeFromHLSManifest(renditionURI, requestInit); | ||
|
||
const streamType = /EXT-X-PLAYLIST-TYPE:\s*VOD/.test(manifest) ? 'on-demand' : 'live'; | ||
|
||
if ( | ||
streamType === 'live' && | ||
resolveTargetDuration(manifest) >= 10 && | ||
(/#EXT-X-DVR-ENABLED:\s*true/.test(manifest) || manifest.includes('#EXT-X-DISCONTINUITY')) | ||
) { | ||
return 'live:dvr'; | ||
} | ||
|
||
return streamType; | ||
}); | ||
} | ||
|
||
function resolveHLSRenditionURI(manifest: string) { | ||
const matches = manifest.match(/#EXT-X-STREAM-INF:[^\n]+(\n[^\n]+)*/g); | ||
return matches ? matches[0].split('\n')[1].trim() : null; | ||
} | ||
|
||
function resolveTargetDuration(manifest: string): number { | ||
const lines = manifest.split('\n'); | ||
|
||
for (const line of lines) { | ||
if (line.startsWith('#EXT-X-TARGETDURATION')) { | ||
const duration = parseFloat(line.split(':')[1]); | ||
if (!isNaN(duration)) { | ||
return duration; | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} |
This file contains 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 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