rtmp

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2023 License: MIT Imports: 12 Imported by: 0

README

usage

rtmp库只负责rtmp协议层面的解析,网络链接(一般为tcp)需要调用方自己去管理和收发数据。调用方只需要把从网络中收到的rtmp数据input到rtmp库,把rtmp库output出来的数据发送到网络即可

rtmp拉流客户端为例子

//step1 连接远端rtmp服务器
conn,err := net.Dial("tcp4", host)

//step2 创建rtmp客户端句柄,你可以指定chunk大小,握手模式(简单复杂)
client := rtmp.NewRtmpClient(rtmp.WithChunkSize(6000), rtmp.WithComplexHandshake())

//step 3 设置一些必须的回调函数,对于拉流客户端OnFrame 和 SetOutput是必须的
client.OnFrame(func(cid codec.CodecID, pts, dts uint32, frame []byte) {
    //接收到的音视频数据回调
})

client.SetOutput(func(b []byte) error {
    //在output回调函数里面,把数据发送给对端
    _, err := conn.Write(b)
    return err
})

//step 4 调用Start,参数是rtmp url 
client.Start(rtmpUrl)

//step 5 接收网络数据,送入到rtmp库中
for {
    n, err = c.Read(buf)
    if err != nil {
        break
    }
    err = client.Input(buf[:n])
    if err != nil {
        break
    }
}

Documentation

Index

Constants

View Source
const (
	CHUNK_CHANNEL_USE_CTRL   = 2
	CHUNK_CHANNEL_CMD        = 3
	CHUNK_CHANNEL_VIDEO      = 5
	CHUNK_CHANNEL_AUDIO      = 6
	CHUNK_CHANNEL_META       = 7
	CHUNK_CHANNEL_NET_STREAM = 8
)
View Source
const (
	FIX_CHUNK_SIZE     = 128
	DEFAULT_CHUNK_SIZE = 60000
	DEFAULT_ACK_SIZE   = 5000000
)
View Source
const (
	HANDSHAKE_SIZE           = 1536
	HANDSHAKE_FIX_SIZE       = 8
	HANDSHAKE_OFFSET_SIZE    = 4
	HANDSHAKE_DIGEST_SIZE    = 32
	HANDSHAKE_SCHEMA_SIZE    = 764
	HANDSHAKE_SCHEMA0_OFFSET = 776 // 8 + 764 + 4
	HANDSHAKE_SCHEMA1_OFFSET = 12  // 8 + 4
)
View Source
const (
	HANDSHAKE_COMPLEX_SCHEMA0 = 0
	HANDSHAKE_COMPLEX_SCHEMA1 = 1
)
View Source
const (
	PUBLISHING_LIVE   = "live"
	PUBLISHING_RECORD = "record"
	PUBLISHING_APPEND = "append"
)
View Source
const (
	LimitType_HARD    = 0
	LimitType_SOFT    = 1
	LimitType_DYNAMIC = 2
)
View Source
const (
	StreamBegin      = 0
	StreamEOF        = 1
	StreamDry        = 2
	SetBufferLength  = 3
	StreamIsRecorded = 4
	PingRequest      = 6
	PingResponse     = 7
)

Variables

View Source
var ChunkType [4]byte = [4]byte{11, 7, 3, 0}
View Source
var EndObj []byte = []byte{0, 0, byte(AMF0_OBJECT_END)}
View Source
var NullItem []byte = []byte{byte(AMF0_NULL)}

Functions

func WithAudioMuxer

func WithAudioMuxer(muxer flv.AVTagMuxer) func(*RtmpClient)

func WithChunkSize

func WithChunkSize(chunkSize uint32) func(*RtmpClient)

func WithComplexHandshake

func WithComplexHandshake() func(*RtmpClient)

func WithComplexHandshakeSchema

func WithComplexHandshakeSchema(schema int) func(*RtmpClient)

func WithEnablePublish

func WithEnablePublish() func(*RtmpClient)

func WithWndAckSize

func WithWndAckSize(ackSize uint32) func(*RtmpClient)

Types

type AMF0_DATA_TYPE

type AMF0_DATA_TYPE int
const (
	AMF0_NUMBER AMF0_DATA_TYPE = iota
	AMF0_BOOLEAN
	AMF0_STRING
	AMF0_OBJECT
	AMF0_MOVIECLIP
	AMF0_NULL
	AMF0_UNDEFINED
	AMF0_REFERENCE
	AMF0_ECMA_ARRAY
	AMF0_OBJECT_END
	AMF0_STRICT_ARRAY
	AMF0_DATE
	AMF0_LONG_STRING
	AMF0_UNSUPPORTED
	AMF0_RECORDSET
	AMF0_XML_DOCUMENT
	AMF0_TYPED_OBJECT
	AMF0_AVMPLUS_OBJECT
)

type HandShakeState

type HandShakeState int
const (
	CLIENT_S0 HandShakeState = iota
	CLIENT_S1
	CLIENT_S2

	SERVER_C0 HandShakeState = iota + 10
	SERVER_C1
	SERVER_C2

	HANDSHAKE_DONE HandShakeState = iota + 100
)

type MessageType

type MessageType int
const (
	//Protocol control messages
	SET_CHUNK_SIZE  MessageType = 1
	ABORT_MESSAGE   MessageType = 2
	ACKNOWLEDGEMENT MessageType = 3
	USER_CONTROL    MessageType = 4
	WND_ACK_SIZE    MessageType = 5
	SET_PEER_BW     MessageType = 6

	AUDIO             MessageType = 8
	VIDEO             MessageType = 9
	Command_AMF0      MessageType = 20
	Command_AMF3      MessageType = 17
	Metadata_AMF0     MessageType = 18
	Metadata_AMF3     MessageType = 15
	SharedObject_AMF0 MessageType = 19
	SharedObject_AMF3 MessageType = 16
	Aggregate         MessageType = 22
)

type NetStreamStatusCode

type NetStreamStatusCode string

type OnError

type OnError func(code, describe string)

type OnFrame

type OnFrame func(cid codec.CodecID, pts, dts uint32, frame []byte)

type OnPlay

type OnPlay func(app, streamName string, start, duration float64, reset bool) StatusCode

type OnPublish

type OnPublish func(app, streamName string) StatusCode

type OnReleaseStream

type OnReleaseStream func(app, streamName string)

type OnStateChange

type OnStateChange func(newState RtmpState)

type OnStatus

type OnStatus func(code, level, describe string)

type OutputCB

type OutputCB func([]byte) error

type ParserState

type ParserState int
const (
	S_BASIC_HEAD ParserState = iota
	S_MSG_HEAD
	S_EXTEND_TS
	S_PAYLOAD
)

type RtmpClient

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

func NewRtmpClient

func NewRtmpClient(options ...func(*RtmpClient)) *RtmpClient

func (*RtmpClient) GetState

func (cli *RtmpClient) GetState() RtmpState

func (*RtmpClient) Input

func (cli *RtmpClient) Input(data []byte) error

func (*RtmpClient) OnError

func (cli *RtmpClient) OnError(onerror OnError)

func (*RtmpClient) OnFrame

func (cli *RtmpClient) OnFrame(onframe OnFrame)

func (*RtmpClient) OnStateChange

func (cli *RtmpClient) OnStateChange(stateChange OnStateChange)

func (*RtmpClient) OnStatus

func (cli *RtmpClient) OnStatus(onstatus OnStatus)

func (*RtmpClient) SetOutput

func (cli *RtmpClient) SetOutput(output OutputCB)

func (*RtmpClient) Start

func (cli *RtmpClient) Start(url string)

url start with "rtmp://"

func (*RtmpClient) WriteAudio

func (cli *RtmpClient) WriteAudio(cid codec.CodecID, frame []byte, pts, dts uint32) error

func (*RtmpClient) WriteFrame

func (cli *RtmpClient) WriteFrame(cid codec.CodecID, frame []byte, pts, dts uint32) error

func (*RtmpClient) WriteVideo

func (cli *RtmpClient) WriteVideo(cid codec.CodecID, frame []byte, pts, dts uint32) error

type RtmpConnectCmd

type RtmpConnectCmd int
const (
	CONNECT RtmpConnectCmd = iota
	CLOSE
	CREATE_STREAM
	GET_STREAM_LENGTH
)

type RtmpParserState

type RtmpParserState int
const (
	HandShake RtmpParserState = iota
	ReadChunk
)

type RtmpServerHandle

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

func NewRtmpServerHandle

func NewRtmpServerHandle(options ...func(*RtmpServerHandle)) *RtmpServerHandle

func (*RtmpServerHandle) GetApp

func (server *RtmpServerHandle) GetApp() string

func (*RtmpServerHandle) GetState

func (server *RtmpServerHandle) GetState() RtmpState

func (*RtmpServerHandle) GetStreamName

func (server *RtmpServerHandle) GetStreamName() string

func (*RtmpServerHandle) Input

func (server *RtmpServerHandle) Input(data []byte) error

func (*RtmpServerHandle) OnFrame

func (server *RtmpServerHandle) OnFrame(onframe OnFrame)

func (*RtmpServerHandle) OnPlay

func (server *RtmpServerHandle) OnPlay(onPlay OnPlay)

func (*RtmpServerHandle) OnPublish

func (server *RtmpServerHandle) OnPublish(onPub OnPublish)

func (*RtmpServerHandle) OnRelease

func (server *RtmpServerHandle) OnRelease(onRelease OnReleaseStream)

func (*RtmpServerHandle) OnStateChange

func (server *RtmpServerHandle) OnStateChange(stateChange OnStateChange)

状态变更,回调函数, 服务端在STATE_RTMP_PLAY_START状态下,开始发流 客户端在STATE_RTMP_PUBLISH_START状态,开始推流

func (*RtmpServerHandle) SetOutput

func (server *RtmpServerHandle) SetOutput(output OutputCB)

func (*RtmpServerHandle) WriteAudio

func (server *RtmpServerHandle) WriteAudio(cid codec.CodecID, frame []byte, pts, dts uint32) error

func (*RtmpServerHandle) WriteFrame

func (server *RtmpServerHandle) WriteFrame(cid codec.CodecID, frame []byte, pts, dts uint32) error

func (*RtmpServerHandle) WriteVideo

func (server *RtmpServerHandle) WriteVideo(cid codec.CodecID, frame []byte, pts, dts uint32) error

type RtmpState

type RtmpState int
const (
	STATE_HANDSHAKEING RtmpState = iota
	STATE_HANDSHAKE_DONE
	STATE_RTMP_CONNECTING
	STATE_RTMP_PLAY_START
	STATE_RTMP_PLAY_FAILED
	STATE_RTMP_PUBLISH_START
	STATE_RTMP_PUBLISH_FAILED
)

type StatusCode

type StatusCode string
const (
	NETSTREAM_PUBLISH_START     StatusCode = "NetStream.Publish.Start"
	NETSTREAM_PLAY_START        StatusCode = "NetStream.Play.Start"
	NETSTREAM_PLAY_STOP         StatusCode = "NetStream.Play.Stop"
	NETSTREAM_PLAY_FAILED       StatusCode = "NetStream.Play.Failed"
	NETSTREAM_PLAY_NOTFOUND     StatusCode = "NetStream.Play.StreamNotFound"
	NETSTREAM_PLAY_RESET        StatusCode = "NetStream.Play.Reset"
	NETSTREAM_PAUSE_NOTIFY      StatusCode = "NetStream.Pause.Notify"
	NETSTREAM_UNPAUSE_NOTIFY    StatusCode = "NetStream.Unpause.Notify"
	NETSTREAM_RECORD_START      StatusCode = "NetStream.Record.Start"
	NETSTREAM_RECORD_STOP       StatusCode = "NetStream.Record.Stop"
	NETSTREAM_RECORD_FAILED     StatusCode = "NetStream.Record.Failed"
	NETSTREAM_SEEK_FAILED       StatusCode = "NetStream.Seek.Failed"
	NETSTREAM_SEEK_NOTIFY       StatusCode = "NetStream.Seek.Notify"
	NETCONNECT_CONNECT_CLOSED   StatusCode = "NetConnection.Connect.Closed"
	NETCONNECT_CONNECT_FAILED   StatusCode = "NetConnection.Connect.Failed"
	NETCONNECT_CONNECT_SUCCESS  StatusCode = "NetConnection.Connect.Success"
	NETCONNECT_CONNECT_REJECTED StatusCode = "NetConnection.Connect.Rejected"
	NETSTREAM_CONNECT_CLOSED    StatusCode = "NetStream.Connect.Closed"
	NETSTREAM_CONNECT_FAILED    StatusCode = "NetStream.Connect.Failed"
	NETSTREAM_CONNECT_SUCCESSS  StatusCode = "NetStream.Connect.Success"
	NETSTREAM_CONNECT_REJECTED  StatusCode = "NetStream.Connect.Rejected"
)

func (StatusCode) Description

func (c StatusCode) Description() StatusLevel

func (StatusCode) Level

func (c StatusCode) Level() StatusLevel

type StatusLevel

type StatusLevel string
const (
	LEVEL_STATUS StatusLevel = "status"
	LEVEL_ERROR  StatusLevel = "error"
	LEVEL_WARN   StatusLevel = "warning"
)

type UserEvent

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

Jump to

Keyboard shortcuts

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