hls

package
v0.0.0-...-928f415 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2022 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CleanupModeNever    = 0
	CleanupModeInTheEnd = 1
	CleanupModeAsap     = 2
)

Variables

Functions

func CalcM3u8Duration

func CalcM3u8Duration(content []byte) (durationSec float64, err error)

CalcM3u8Duration

@param content 传入m3u8文件内容

@return durationSec m3u8中所有ts的时间总和。注意,使用的是m3u8文件中描述的ts时间,而不是读取ts文件中实际音视频数据的时间。

func ReadFile

func ReadFile(filename string) ([]byte, error)

func RemoveAll

func RemoveAll(path string) error

func SetUseMemoryAsDiskFlag

func SetUseMemoryAsDiskFlag(flag bool)

func SplitFragment2TsPackets

func SplitFragment2TsPackets(content []byte) (ret [][]byte, err error)

Types

type DefaultPathStrategy

type DefaultPathStrategy struct {
}

DefaultPathStrategy 默认的路由,落盘策略

每个流在<rootPath>下以流名称生成一个子目录,目录下包含:

- playlist.m3u8 实时的HLS文件,定期刷新,写入当前最新的TS文件列表,淘汰过期的TS文件列表 - record.m3u8 录制回放的HLS文件,包含了从流开始至今的所有TS文件 - test110-1620540712084-0.ts TS分片文件,命名格式为{liveid}-{timestamp}-{index}.ts - test110-1620540716095-1.ts - ... 一系列的TS文件

假设 流名称="test110" rootPath="/tmp/lal/hls/"

http://127.0.0.1:8080/hls/test110/playlist.m3u8 -> /tmp/lal/hls/test110/playlist.m3u8 http://127.0.0.1:8080/hls/test110/record.m3u8 -> /tmp/lal/hls/test110/record.m3u8 http://127.0.0.1:8080/hls/test110/test110-1620540712084-0.ts -> /tmp/lal/hls/test110/test110-1620540712084-0.ts

http://127.0.0.1:8080/hls/test110.m3u8 -> /tmp/lal/hls/test110/playlist.m3u8 http://127.0.0.1:8080/hls/test110-1620540712084-0.ts -> /tmp/lal/hls/test110/test110-1620540712084-0.ts 最下面这两个做了特殊映射

func (*DefaultPathStrategy) GetLiveM3u8FileName

func (*DefaultPathStrategy) GetLiveM3u8FileName(outPath string, streamName string) string

func (*DefaultPathStrategy) GetMuxerOutPath

func (*DefaultPathStrategy) GetMuxerOutPath(rootOutPath string, streamName string) string

GetMuxerOutPath <rootOutPath>/<streamName>

func (*DefaultPathStrategy) GetRecordM3u8FileName

func (*DefaultPathStrategy) GetRecordM3u8FileName(outPath string, streamName string) string

func (*DefaultPathStrategy) GetRequestInfo

func (dps *DefaultPathStrategy) GetRequestInfo(urlCtx base.UrlContext, rootOutPath string) (ri RequestInfo)

GetRequestInfo

RequestURI example: uri -> FileName StreamName FileType FileNameWithPath /hls/test110.m3u8 -> test110.m3u8 test110 m3u8 {rootOutPath}/test110/playlist.m3u8 /hls/test110/playlist.m3u8 -> playlist.m3u8 test110 m3u8 {rootOutPath}/test110/playlist.m3u8 /hls/test110/record.m3u8 -> record.m3u8 test110 m3u8 {rootOutPath}/test110/record.m3u8 /hls/test110/test110-1620540712084-.ts -> test110-1620540712084-.ts test110 ts {rootOutPath/test110/test110-1620540712084-.ts /hls/test110-1620540712084-.ts -> test110-1620540712084-.ts test110 ts {rootOutPath/test110/test110-1620540712084-.ts

func (*DefaultPathStrategy) GetTsFileName

func (*DefaultPathStrategy) GetTsFileName(streamName string, index int, timestamp int) string

func (*DefaultPathStrategy) GetTsFileNameWithPath

func (*DefaultPathStrategy) GetTsFileNameWithPath(outPath string, fileName string) string

type Fragment

type Fragment struct {
	// contains filtered or unexported fields
}

func (*Fragment) CloseFile

func (f *Fragment) CloseFile() error

func (*Fragment) OpenFile

func (f *Fragment) OpenFile(filename string) (err error)

func (*Fragment) WriteFile

func (f *Fragment) WriteFile(b []byte) (err error)

type IMuxerObserver

type IMuxerObserver interface {
	OnHlsMakeTs(info base.HlsMakeTsInfo)

	// OnFragmentOpen
	//
	// 内部决定开启新的fragment切片,将该事件通知给上层
	//
	// TODO(chef): [refactor] 考虑用OnHlsMakeTs替代OnFragmentOpen 202206
	//
	OnFragmentOpen()
}

type IPathRequestStrategy

type IPathRequestStrategy interface {
	// GetRequestInfo
	//
	// 解析HTTP请求,得到流名称、文件所在路径
	//
	GetRequestInfo(urlCtx base.UrlContext, rootOutPath string) RequestInfo
}

IPathRequestStrategy

路由策略 接到HTTP请求时,对应文件路径的映射逻辑

type IPathStrategy

type IPathStrategy interface {
	IPathRequestStrategy
	IPathWriteStrategy
}

type IPathWriteStrategy

type IPathWriteStrategy interface {
	// GetMuxerOutPath 获取单个流对应的文件根路径
	GetMuxerOutPath(rootOutPath string, streamName string) string

	// GetLiveM3u8FileName 获取单个流对应的m3u8文件路径
	//
	// @param outPath: func GetMuxerOutPath的结果
	GetLiveM3u8FileName(outPath string, streamName string) string

	// GetRecordM3u8FileName 获取单个流对应的record类型的m3u8文件路径
	//
	// live m3u8和record m3u8的区别:
	// live记录的是当前最近的可播放内容,record记录的是从流开始时的可播放内容
	//
	// @param outPath: func GetMuxerOutPath的结果
	GetRecordM3u8FileName(outPath string, streamName string) string

	// GetTsFileNameWithPath 获取单个流对应的ts文件路径
	//
	// @param outPath: func GetMuxerOutPath的结果
	GetTsFileNameWithPath(outPath string, fileName string) string

	// GetTsFileName ts文件名的生成策略
	GetTsFileName(streamName string, index int, timestamp int) string
}

IPathWriteStrategy 落盘策略

type Muxer

type Muxer struct {
	UniqueKey string
	// contains filtered or unexported fields
}

Muxer

输入mpegts流,输出hls(m3u8+ts)至文件中

func NewMuxer

func NewMuxer(streamName string, config *MuxerConfig, observer IMuxerObserver) *Muxer

NewMuxer

@param observer 可以为nil,如果不为nil,TS流将回调给上层

func (*Muxer) Dispose

func (m *Muxer) Dispose()

func (*Muxer) FeedMPEGts

func (m *Muxer) FeedMPEGts(packets []byte, frame *mpegts.Frame, boundary bool)

func (*Muxer) FeedPatPmt

func (m *Muxer) FeedPatPmt(b []byte)

func (*Muxer) OnPatPmt

func (m *Muxer) OnPatPmt(b []byte)

OnPatPmt OnTsPackets

实现 remux.IRtmp2MpegtsRemuxerObserver,方便直接将 remux.Rtmp2MpegtsRemuxer 的数据喂入 hls.Muxer

func (*Muxer) OnTsPackets

func (m *Muxer) OnTsPackets(packets []byte, frame *mpegts.Frame, boundary bool)

func (*Muxer) OutPath

func (m *Muxer) OutPath() string

func (*Muxer) Start

func (m *Muxer) Start()

type MuxerConfig

type MuxerConfig struct {
	OutPath            string `json:"out_path"`
	FragmentDurationMs int    `json:"fragment_duration_ms"`
	FragmentNum        int    `json:"fragment_num"`
	DeleteThreshold    int    `json:"delete_threshold"`
	CleanupMode        int    `json:"cleanup_mode"` // TODO chef: lalserver的模式1的逻辑是在上层做的,应该重构到hls模块中
}

MuxerConfig

各字段含义见文档: https://pengrl.com/lal/#/ConfigBrief

type RequestInfo

type RequestInfo struct {
	StreamName       string // uri结合策略
	FileNameWithPath string // uri结合策略, 从磁盘打开文件时使用
}

type ServerHandler

type ServerHandler struct {
	// contains filtered or unexported fields
}

func NewServerHandler

func NewServerHandler(outPath string) *ServerHandler

func (*ServerHandler) ServeHTTP

func (s *ServerHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request)

func (*ServerHandler) ServeHTTPWithUrlCtx

func (s *ServerHandler) ServeHTTPWithUrlCtx(resp http.ResponseWriter, urlCtx base.UrlContext)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL