rtmp

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package rtmp 提供一个 RTMP 采集(ingest)服务端,薄封装 github.com/yutopp/go-rtmp。 接受推流端(OBS / ffmpeg 等)的 publish,把每一路流的 metadata / audio / video 数据 交给业务实现的 Handler。

边界(和框架"薄机制"一致):本包**只负责收流**——不转码、不封装 HLS。收到的 audio/video 是 FLV tag body(含各自的编解码头),要落地成 HLS 分片需再接一层 remux (交给上层/ffmpeg,配合 pkg/hls 分发)。不重新发明 RTMP 协议,直接用成熟的纯 Go 实现。

Server 结构上满足 beauty.Service(Start/String)+ ReadyNotifier,可直接 beauty.WithService(srv) 挂进框架、随 app 优雅停机。默认监听 1935(RTMP 标准端口)。

用法:

srv := rtmp.NewServer(":1935", func(streamKey string) rtmp.Handler {
    // 按流名(推流地址 /app/<streamKey>)决定怎么处理,返回 nil 拒绝
    return &myHandler{key: streamKey}
})
app := beauty.New(beauty.WithService(srv))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConnectAuthFunc

type ConnectAuthFunc func(*ConnectInfo) error

ConnectAuthFunc 在 RTMP connect 阶段被调用(早于 publish)。返回非 nil error 则拒绝 连接——用于在建连时就按 app/tcUrl(签名、来源)做**连接级鉴权**,把非法推流挡在更早。

type ConnectInfo

type ConnectInfo struct {
	App   string // 推流地址里的应用名,如 rtmp://host/<app>/<streamKey> 的 <app>
	TCURL string // 完整 tcUrl,常含鉴权 query(如 ...?sign=xxx)
}

ConnectInfo 是 RTMP connect 命令携带的信息,用于连接级鉴权。

type Handler

type Handler interface {
	// OnMetaData 收到 onMetaData(如分辨率/码率/编解码信息)。payload 是原始 AMF 字节。可能不被调用。
	OnMetaData(payload []byte)
	// OnAudio 收到一帧音频(FLV audio tag body:首字节为音频编解码头,之后为数据)。
	OnAudio(timestamp uint32, data []byte) error
	// OnVideo 收到一帧视频(FLV video tag body:首字节含帧类型/编解码,之后为数据)。
	OnVideo(timestamp uint32, data []byte) error
	// OnClose 推流结束(任何原因)时调用一次,用于收尾。
	OnClose()
}

Handler 处理一路已开始的推流。方法在该连接的读 goroutine 内串行调用。

type Option

type Option func(*Server)

Option 配置 Server。

func WithConnectAuth

func WithConnectAuth(fn ConnectAuthFunc) Option

WithConnectAuth 设置连接级鉴权:在 RTMP connect 阶段按 app/tcUrl 校验,返回 error 拒绝连接。

func WithServiceName

func WithServiceName(name string) Option

WithServiceName 设置服务名(日志/标识用)。

type PublishFunc

type PublishFunc func(streamKey string) Handler

PublishFunc 在收到一路 publish 时被调用(streamKey 取自推流地址的流名,可含 ?token=…), 返回处理该路流的 Handler;返回 nil 表示拒绝这次推流。这是**推流级鉴权**点——按流名/ token 校验后决定接不接。

type Server

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

Server 是 RTMP 采集服务端。零值不可用,用 NewServer 构造。

func NewServer

func NewServer(addr string, onPublish PublishFunc, opts ...Option) *Server

NewServer 创建 RTMP 采集服务端。addr 形如 ":1935"。onPublish 决定如何处理每一路推流。

func (*Server) Addr

func (s *Server) Addr() net.Addr

Addr 返回实际监听地址(":0" 时取回系统分配端口);未启动返回 nil。

func (*Server) Ready

func (s *Server) Ready() <-chan struct{}

Ready 在开始监听后关闭——满足 beauty.ReadyNotifier。

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start 监听并处理 RTMP 连接,直到 ctx 取消——满足 beauty.Service。

func (*Server) String

func (s *Server) String() string

String 满足 beauty.Service。

Jump to

Keyboard shortcuts

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