Files
player/src/utils/videoProtocol.ts
T
hibna 38295bdf9c Update MPEG-TS IPTV detection to use native playback
Changed protocol detection for .ts IPTV streams to prefer native playback instead of HLS.js, reflecting modern browser support for MPEG-TS. Updated related tests to match new behavior.
2025-11-04 06:09:26 +03:00

123 lines
3.2 KiB
TypeScript

/**
* Video protocol detection utility
* Detects the streaming protocol from a video URL
*/
export type VideoProtocol = 'native' | 'hls' | 'rtmp' | 'dash'
export interface ProtocolDetectionResult {
protocol: VideoProtocol
isLive: boolean
needsSpecialPlayer: boolean
}
/**
* Detects the video protocol from the source URL
* @param src - The video source URL
* @returns Protocol detection result
*/
export const detectVideoProtocol = (src: string): ProtocolDetectionResult => {
if (!src) {
return {
protocol: 'native',
isLive: false,
needsSpecialPlayer: false,
}
}
const lowerSrc = src.toLowerCase()
// RTMP protocol detection
// Supports: rtmp://, rtmps://, rtmpt://, rtmpe://
if (
lowerSrc.startsWith('rtmp://') ||
lowerSrc.startsWith('rtmps://') ||
lowerSrc.startsWith('rtmpt://') ||
lowerSrc.startsWith('rtmpe://')
) {
return {
protocol: 'rtmp',
isLive: true, // RTMP is typically used for live streaming
needsSpecialPlayer: true,
}
}
// HLS protocol detection
// Check for .m3u8 extension or HLS URL patterns
if (lowerSrc.includes('.m3u8')) {
return {
protocol: 'hls',
isLive: lowerSrc.includes('/live/') || lowerSrc.includes('live.m3u8'),
needsSpecialPlayer: true,
}
}
// DASH protocol detection
// Check for .mpd extension
if (lowerSrc.includes('.mpd')) {
return {
protocol: 'dash',
isLive: lowerSrc.includes('/live/') || lowerSrc.includes('live.mpd'),
needsSpecialPlayer: true,
}
}
// HTTP-FLV detection (alternative to RTMP)
if (lowerSrc.includes('.flv') || lowerSrc.includes('flv?')) {
return {
protocol: 'rtmp', // Use RTMP player for FLV files
isLive: lowerSrc.includes('/live/') || lowerSrc.includes('live.flv'),
needsSpecialPlayer: true,
}
}
// MPEG-TS (IPTV) detection
// Check for .ts extension (Transport Stream used in IPTV)
// Note: These are direct TS streams, not HLS playlists
// Try native playback first as modern browsers support MPEG-TS
if (lowerSrc.includes('.ts') || lowerSrc.match(/\.ts(\?|$)/)) {
return {
protocol: 'native', // Try native playback first
isLive: true, // IPTV streams are typically live
needsSpecialPlayer: false, // Modern browsers support MPEG-TS
}
}
// Native HTML5 video formats (MP4, WebM, OGG, etc.)
return {
protocol: 'native',
isLive: false,
needsSpecialPlayer: false,
}
}
/**
* Checks if the URL is an RTMP stream
* @param src - The video source URL
* @returns True if RTMP stream
*/
export const isRtmpStream = (src: string): boolean => {
const detection = detectVideoProtocol(src)
return detection.protocol === 'rtmp'
}
/**
* Checks if the URL is an HLS stream
* @param src - The video source URL
* @returns True if HLS stream
*/
export const isHlsStream = (src: string): boolean => {
const detection = detectVideoProtocol(src)
return detection.protocol === 'hls'
}
/**
* Checks if the stream is live
* @param src - The video source URL
* @returns True if live stream
*/
export const isLiveStream = (src: string): boolean => {
const detection = detectVideoProtocol(src)
return detection.isLive
}