hls

package module
v4.3.8 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2024 License: MIT Imports: 29 Imported by: 6

README

HLS插件

  • 该插件可用来拉取网络上的m3u8文件并解析后转换成其他协议
  • 可以直接访问http://localhost:8080/hls/live/user1.m3u8 进行播放,其中8080端口是全局HTTP配置,live/user1是streamPath,需要根据实际情况修改

插件地址

https://github.com/Monibuca/plugin-hls

插件引入

import (
    _ "m7s.live/plugin/hls/v4"
)

API

参数是可变的,下面的参数live/hls是作为例子,不是固定的

  • /hls/api/list 列出所有HLS流,是一个SSE,可以持续接受到列表数据,加上?json=1 可以返回json数据。
  • /hls/api/save?streamPath=live/hls 保存指定的流(例如live/hls)为HLS文件(m3u8和ts)当这个请求关闭时就结束保存(该API仅作用于远程拉流)
  • /hls/api/pull?streamPath=live/hls&target=http://localhost/abc.m3u8 将目标HLS流拉过来作为媒体源在monibuca内以live/hls流的形式存在
  • llhls地址形式http://localhost:8080/llhls/live/user1/index.m3u8 进行播放,其中8080端口是全局HTTP配置,live/user1是streamPath,需要根据实际情况修改

配置

  • 配置信息按照需要添加到配置文件中,无需复制全部默认配置信息
  • publish 和 subscribe 配置会覆盖全局配置
hls:
    publish: # 格式参考全局配置
    subscribe: # 格式参考全局配置
    pull: # 格式 https://m7s.live/guide/config.html#%E6%8F%92%E4%BB%B6%E9%85%8D%E7%BD%AE
    fragment: 10s # TS分片长度
    window: 2 # 实时流m3u8文件包含的TS文件数
    filter: "" # 正则表达式,用来过滤发布的流,只有匹配到的流才会写入
    path: "" # 远端拉流如果需要保存的话,存放的目录
    defaultts: "" # 默认切片用于无流时片头播放,如果留空则使用系统内置
    defaulttsduration: 3.88s # 默认切片的长度
    relaymode: 0 # 转发模式,0:转协议+不转发,1:不转协议+转发,2:转协议+转发
    preload: false # 是否预加载,预加载开启后HLS就变成内部订阅者无法按需关闭发布者了

转发模式

转发模式仅仅对从远端拉流的hls起作用。

relaymode 可以配置不同的转发模式

其中,转协议意味着hls可以拉流可以转换成其他协议格式,即需要对hls的数据进行解析, 转发意味着hls中的ts文件缓存在服务器,可以在从服务器拉流时直接读取ts文件。

例如,如果希望只做hls的纯转发,减少cpu消耗,可以配置

hls:
  relaymode: 1

HLS.js测试页面

访问 http://localhost:8080/hls/index.html

域名和端口根据实际情况修改

Documentation

Index

Constants

View Source
const (
	HLS_KEY_METHOD_AES_128 = "AES-128"
)

Variables

View Source
var HLSPlugin = InstallPlugin(hlsConfig, defaultYaml)
View Source
var LLHLSPlugin = InstallPlugin(llhlsConfig)

Functions

This section is empty.

Types

type AudioTrackReader added in v4.1.3

type AudioTrackReader struct {
	TrackReader
	*track.Audio
}

type HLSConfig

type HLSConfig struct {
	config.HTTP
	config.Publish
	config.Pull
	config.Subscribe
	Fragment          time.Duration `default:"2s" desc:"ts分片大小"`
	Window            int           `default:"3" desc:"m3u8窗口大小(包含ts的数量)"`
	Filter            config.Regexp `desc:"用于过滤的正则表达式"` // 过滤,正则表达式
	Path              string        `desc:"保存 ts 文件的路径"`
	DefaultTS         string        `desc:"默认的ts文件"`                                     // 默认的ts文件
	DefaultTSDuration time.Duration `desc:"默认的ts文件时长"`                                   // 默认的ts文件时长
	RelayMode         int           `desc:"转发模式(转协议会消耗资源)" enum:"0:只转协议,1:纯转发,2:转协议+转发"` // 转发模式,0:转协议+不转发,1:不转协议+转发,2:转协议+转发
	Preload           bool          `desc:"是否预加载,提高响应速度"`                                // 是否预加载,提高响应速度
}

func (*HLSConfig) API_List

func (config *HLSConfig) API_List(w http.ResponseWriter, r *http.Request)

func (*HLSConfig) API_Pull

func (config *HLSConfig) API_Pull(w http.ResponseWriter, r *http.Request)

func (*HLSConfig) API_Save

func (config *HLSConfig) API_Save(w http.ResponseWriter, r *http.Request)

处于拉流时,可以调用这个API将拉流的TS文件保存下来,这个http如果断开,则停止保存

func (*HLSConfig) OnEvent

func (c *HLSConfig) OnEvent(event any)

func (*HLSConfig) ServeHTTP

func (config *HLSConfig) ServeHTTP(w http.ResponseWriter, r *http.Request)

type HLSPuller

type HLSPuller struct {
	TSPublisher
	Puller
	Video       M3u8Info
	Audio       M3u8Info
	TsHead      http.Header     `json:"-" yaml:"-"` //用于提供cookie等特殊身份的http头
	SaveContext context.Context `json:"-" yaml:"-"` //用来保存ts文件到服务器
	// contains filtered or unexported fields
}

HLSPuller HLS拉流者

func (*HLSPuller) Connect

func (p *HLSPuller) Connect() (err error)

func (*HLSPuller) Disconnect added in v4.3.3

func (p *HLSPuller) Disconnect()

func (*HLSPuller) GetTs added in v4.3.4

func (p *HLSPuller) GetTs(key string) util.Recyclable

func (*HLSPuller) OnEvent added in v4.2.1

func (p *HLSPuller) OnEvent(event any)

func (*HLSPuller) Pull

func (p *HLSPuller) Pull() error

type HLSWriter

type HLSWriter struct {
	Subscriber
	// contains filtered or unexported fields
}

func (*HLSWriter) GetTs added in v4.3.4

func (hls *HLSWriter) GetTs(key string) util.Recyclable

func (*HLSWriter) OnEvent

func (hls *HLSWriter) OnEvent(event any)

func (*HLSWriter) ReadTrack added in v4.1.3

func (hls *HLSWriter) ReadTrack()

func (*HLSWriter) Start added in v4.1.0

func (hls *HLSWriter) Start(streamPath string)

type LLAudioTrack added in v4.2.9

type LLAudioTrack struct {
	*track.AVRingReader
	*track.Audio
}

type LLHLSConfig added in v4.2.9

type LLHLSConfig struct {
	DefaultYaml
	config.HTTP
	config.Publish
	// config.Pull
	config.Subscribe
	Filter string // 过滤,正则表达式
	Path   string
}

func (*LLHLSConfig) OnEvent added in v4.2.9

func (c *LLHLSConfig) OnEvent(event any)

func (*LLHLSConfig) ServeHTTP added in v4.2.9

func (c *LLHLSConfig) ServeHTTP(w http.ResponseWriter, r *http.Request)

type LLMuxer added in v4.2.9

type LLMuxer struct {
	*gohlslib.Muxer
	Subscriber
	// contains filtered or unexported fields
}

func (*LLMuxer) OnEvent added in v4.2.9

func (ll *LLMuxer) OnEvent(event any)

func (*LLMuxer) Start added in v4.2.9

func (ll *LLMuxer) Start(s *Stream)

type LLVideoTrack added in v4.2.9

type LLVideoTrack struct {
	*track.AVRingReader
	*track.Video
}

type M3u8Info

type M3u8Info struct {
	Req       *http.Request `json:"-" yaml:"-"`
	M3U8Count int           //一共拉取的m3u8文件数量
	TSCount   int           //一共拉取的ts文件数量
	LastM3u8  string        //最后一个m3u8文件内容
}

M3u8Info m3u8文件的信息,用于拉取m3u8文件,和提供查询

type Playlist

type Playlist struct {
	io.Writer
	ExtM3U         string      // indicates that the file is an Extended M3U [M3U] Playlist file. (4.3.3.1) -- 每个M3U文件第一行必须是这个tag.
	Version        int         // indicates the compatibility version of the Playlist file. (4.3.1.2) -- 协议版本号.
	Sequence       int         // indicates the Media Sequence Number of the first Media Segment that appears in a Playlist file. (4.3.3.2) -- 第一个媒体段的序列号.
	Targetduration int         // specifies the maximum Media Segment duration. (4.3.3.1) -- 每个视频分段最大的时长(单位秒).
	PlaylistType   int         // rovides mutability information about the Media Playlist file. (4.3.3.5) -- 提供关于PlayList的可变性的信息.
	Discontinuity  int         // indicates a discontinuity between theMedia Segment that follows it and the one that preceded it. (4.3.2.3) -- 该标签后边的媒体文件和之前的媒体文件之间的编码不连贯(即发生改变)(场景用于插播广告等等).
	Key            PlaylistKey // specifies how to decrypt them. (4.3.2.4) -- 解密媒体文件的必要信息(表示怎么对media segments进行解码).
	EndList        string      // indicates that no more Media Segments will be added to the Media Playlist file. (4.3.3.4) -- 标示没有更多媒体文件将会加入到播放列表中,它可能会出现在播放列表文件的任何地方,但是不能出现两次或以上.
	Inf            PlaylistInf // specifies the duration of a Media Segment. (4.3.2.1) -- 指定每个媒体段(ts)的持续时间.
	// contains filtered or unexported fields
}

以”#EXT“开头的表示一个”tag“,否则表示注释,直接忽略

func (*Playlist) Init

func (pl *Playlist) Init() (err error)

func (*Playlist) WriteInf

func (pl *Playlist) WriteInf(inf PlaylistInf) (err error)

type PlaylistInf

type PlaylistInf struct {
	Duration float64
	Title    string
	FilePath string
}

type PlaylistKey

type PlaylistKey struct {
	Method string // specifies the encryption method. (4.3.2.4)
	Uri    string // key url. (4.3.2.4)
	IV     string // key iv. (4.3.2.4)
}

type TSDownloader added in v4.3.7

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

func (*TSDownloader) Start added in v4.3.7

func (p *TSDownloader) Start()

type TrackReader added in v4.1.3

type TrackReader struct {
	sync.RWMutex
	M3u8 util.Buffer

	*track.AVRingReader
	// contains filtered or unexported fields
}

type VideoTrackReader added in v4.1.3

type VideoTrackReader struct {
	TrackReader
	*track.Video
}

Jump to

Keyboard shortcuts

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