Documentation
¶
Overview ¶
Package live 提供 SRS 直播流媒体对接层:
- 回调接收(SRS → gbx):6 类 HTTP 回调,解析后转发给业务注入的裁决函数
- API 客户端(gbx → SRS):查流/踢流/踢客户端
模块只做"对接",不内置业务规则:鉴权裁决由业务注入(OnPublish 等)。
Index ¶
- func Clients() map[string]*Client
- func Provide(app *iris.Application, config Config)
- func SetGlobal(client *Client)
- func SetNamed(name string, client *Client)
- type Client
- func (c *Client) KickClient(ctx context.Context, cid string) error
- func (c *Client) KickStream(ctx context.Context, streamName string) error
- func (c *Client) ListClients(ctx context.Context) ([]*ClientInfo, error)
- func (c *Client) ListStreams(ctx context.Context) ([]*Stream, error)
- func (c *Client) Version(ctx context.Context) (*Version, error)
- type ClientInfo
- type Config
- type ConnectInfo
- type DvrInfo
- type Handlers
- type HlsInfo
- type PlayInfo
- type PublishInfo
- type Stream
- type StreamAudio
- type StreamVideo
- type UnpublishInfo
- type Version
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Provide ¶
func Provide(app *iris.Application, config Config)
Provide 装配 live 模块(builder 回调中调用,业务一行启用):
builder.EnableWeb(appbox.TimeFormat, ":8080", "info", func(app *iris.Application) {
live.Provide(app, live.Config{
APIBase: "http://127.0.0.1:1985",
CallbackMount: "/api/live",
Handlers: &live.Handlers{
OnPublish: func(ctx iris.Context, info *live.PublishInfo) error {
// 业务鉴权:密钥校验/封禁名单;返回 error 即拒流
if info.Param != "?key=secret" {
return errors.New("invalid stream key")
}
return nil
},
},
})
})
注册 *live.Client 全局便捷入口(SetGlobal);挂载 6 类回调路由。
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client SRS HTTP API 客户端。
func NewClient ¶
NewClient 创建 SRS API 客户端。 baseURL 如 http://127.0.0.1:1985;timeout 默认 5s。
func (*Client) KickClient ¶
KickClient 踢客户端(DELETE /api/v1/clients/{cid},不带尾部斜杠)。
func (*Client) KickStream ¶
KickStream 踢流(两步:①查流名取 publish_cid ②DELETE 客户端)。 流不存在返回明确错误;cid 缺失返回提示(流刚断/无活跃推流)。
func (*Client) ListClients ¶
func (c *Client) ListClients(ctx context.Context) ([]*ClientInfo, error)
ListClients 查询客户端连接(GET /api/v1/clients/,带尾部斜杠)。
func (*Client) ListStreams ¶
ListStreams 查询在线流(GET /api/v1/streams/,必须带尾部斜杠)。
type ClientInfo ¶
type ClientInfo struct {
ID string `json:"id"`
Vhost string `json:"vhost"`
Stream string `json:"stream"`
App string `json:"app"`
IP string `json:"ip"`
PageURL string `json:"page_url"`
Type string `json:"type"` // publisher / player
Alive string `json:"alive"` // 秒
Publish bool `json:"publish"`
}
ClientInfo 客户端连接信息。
type Config ¶
type Config struct {
// APIBase SRS HTTP API 地址(如 http://127.0.0.1:1985)。
APIBase string
// CallbackMount 回调挂载前缀(需与 SRS http_hooks 配置一致,如 /api/live)。
CallbackMount string
// Handlers 业务注入的回调裁决函数。
Handlers *Handlers
// APITimeout SRS API 超时(默认 5s)。
APITimeout int
}
Config live 模块配置。
type ConnectInfo ¶
type ConnectInfo struct {
Action string `json:"action"`
ClientID string `json:"client_id"`
IP string `json:"ip"`
Vhost string `json:"vhost"`
App string `json:"app"`
Stream string `json:"stream"`
Param string `json:"param"`
TCURL string `json:"tcUrl"`
PageURL string `json:"pageUrl"`
}
ConnectInfo on_connect 回调(连接建立,可选,默认放行)。
type DvrInfo ¶
type DvrInfo struct {
Action string `json:"action"`
ClientID string `json:"client_id"`
IP string `json:"ip"`
Vhost string `json:"vhost"`
App string `json:"app"`
Stream string `json:"stream"`
Param string `json:"param"`
Cwd string `json:"cwd"` // 录制文件目录
File string `json:"file"` // 录制文件相对路径
}
DvrInfo on_dvr 回调(录制完成:回放入库)。
type Handlers ¶
type Handlers struct {
// OnPublish 推流鉴权(最关键):返回 nil 放行,返回 error 拒绝(403,SRS 断流)。
OnPublish func(ctx iris.Context, info *PublishInfo) error
// OnPlay 播放鉴权:返回 nil 放行,返回 error 拒绝。
OnPlay func(ctx iris.Context, info *PlayInfo) error
// OnConnect 连接鉴权(可选):返回 nil 放行,返回 error 拒绝;未注入默认放行。
OnConnect func(ctx iris.Context, info *ConnectInfo) error
// OnUnpublish 下播通知(无裁决)。
OnUnpublish func(ctx iris.Context, info *UnpublishInfo)
// OnDvr 录制完成通知。
OnDvr func(ctx iris.Context, info *DvrInfo)
// OnHls 切片生成通知。
OnHls func(ctx iris.Context, info *HlsInfo)
}
Handlers 业务注入的回调裁决函数。 返回 error 的(on_publish/on_play/on_connect)决定放行或拒绝; 其余为通知型,返回值忽略(仅日志)。
type HlsInfo ¶
type HlsInfo struct {
Action string `json:"action"`
ClientID string `json:"client_id"`
IP string `json:"ip"`
Vhost string `json:"vhost"`
App string `json:"app"`
Stream string `json:"stream"`
Param string `json:"param"`
Duration float64 `json:"duration"` // 切片时长(秒)
CWD string `json:"cwd"` // m3u8 目录
File string `json:"file"` // 切片文件
URL string `json:"url"` // m3u8 URL
M3U8 string `json:"m3u8"`
SeqNo int `json:"seq_no"`
}
HlsInfo on_hls 回调(切片生成,后续回放用)。
type PlayInfo ¶
type PlayInfo struct {
Action string `json:"action"`
ClientID string `json:"client_id"`
IP string `json:"ip"`
Vhost string `json:"vhost"`
App string `json:"app"`
Stream string `json:"stream"`
Param string `json:"param"`
PageURL string `json:"pageUrl"`
}
PlayInfo on_play 回调(播放鉴权:会员/付费/封禁)。
type PublishInfo ¶
type PublishInfo struct {
Action string `json:"action"` // "on_publish"
ClientID string `json:"client_id"` // 连接 ID(踢流用)
IP string `json:"ip"`
Vhost string `json:"vhost"`
App string `json:"app"`
Stream string `json:"stream"` // 纯流名,不含 query
Param string `json:"param"` // "?key=xxx"(含 ? 前缀)
}
PublishInfo on_publish 回调(推流鉴权,最关键)。 推流 URL 的 query(如 ?key=xxx)在 param 字段(带 ? 前缀),不在 stream 字段。
type Stream ¶
type Stream struct {
ID string `json:"id"`
Name string `json:"name"`
App string `json:"app"`
Vhost string `json:"vhost"`
URL string `json:"url"`
PublishCID string `json:"publish_cid"` // 推流连接 ID(踢流关键)
VideoCodec string `json:"video_codec"` // 如 H264(平铺兼容字段)
AudioCodec string `json:"audio_codec"` // 如 AAC(平铺兼容字段)
Width int `json:"width"` // 视频宽(平铺兼容字段)
Height int `json:"height"` // 视频高(平铺兼容字段)
// Video 视频编码/分辨率详情(SRS 5.x 顶层 video 对象;无视频时为 nil)。
Video *StreamVideo `json:"video,omitempty"`
// Audio 音频编码详情(SRS 5.x 顶层 audio 对象;无音频时为 nil)。
Audio *StreamAudio `json:"audio,omitempty"`
}
Stream 在线流信息(字段名直观化)。 SRS 5.x 的 video/audio 在流对象顶层,同时兼容旧版 publish.video/publish.audio。
type StreamAudio ¶ added in v1.46.0
type StreamAudio struct {
Codec string `json:"codec"` // 如 AAC
SampleRate int `json:"sample_rate"` // 如 44100
Channel int `json:"channel"` // 如 2
Profile string `json:"profile"` // 如 LC
}
StreamAudio 音频编码信息(json tag 对齐 SRS streams API)。
type StreamVideo ¶ added in v1.46.0
type StreamVideo struct {
Codec string `json:"codec"` // 如 H264
Profile string `json:"profile"` // 如 High
Level string `json:"level"` // 如 3.1
Width int `json:"width"` // 如 1280
Height int `json:"height"` // 如 720
}
StreamVideo 视频编码信息(json tag 对齐 SRS streams API)。