Documentation
¶
Overview ¶
Package rtmp provides a complete implementation of the Real-Time Messaging Protocol (RTMP). This package offers both client and server implementations following the official RTMP specifications.
Index ¶
- Constants
- type Chunk
- type ChunkHeader
- type ChunkStreamer
- type Client
- func (c *Client) Close() error
- func (c *Client) Connect(ctx context.Context, address string) error
- func (c *Client) Publish(ctx context.Context, streamName string, streamType StreamType) error
- func (c *Client) ReadMessage() (*Message, error)
- func (c *Client) State() ClientState
- func (c *Client) Subscribe(ctx context.Context, streamName string) error
- func (c *Client) WriteMessage(msg *Message) error
- type ClientConfig
- type ClientState
- type ConnectionState
- type HandshakeState
- type Handshaker
- type Message
- type MessageType
- type Server
- type ServerConfig
- type ServerConnection
- func (sc *ServerConnection) BytesReceived() uint64
- func (sc *ServerConnection) BytesSent() uint64
- func (sc *ServerConnection) Close() error
- func (sc *ServerConnection) ConnectTime() time.Time
- func (sc *ServerConnection) ID() string
- func (sc *ServerConnection) LastActivity() time.Time
- func (sc *ServerConnection) ReadMessage() (*Message, error)
- func (sc *ServerConnection) RemoteAddr() net.Addr
- func (sc *ServerConnection) State() ConnectionState
- func (sc *ServerConnection) WriteMessage(msg *Message) error
- type Stream
- type StreamType
Constants ¶
const DefaultChunkSize = 128
DefaultChunkSize represents the default chunk size for RTMP messages.
const HandshakePacketSize = 1536
HandshakePacketSize represents the size of C1/S1 and C2/S2 packets.
const HandshakeVersion byte = 3
HandshakeVersion represents the RTMP version used in handshake.
const MaxChunkSize = 65536
MaxChunkSize represents the maximum allowed chunk size.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Chunk ¶
type Chunk struct {
// Header contains the chunk header.
Header *ChunkHeader
// Data contains the chunk data.
Data []byte
}
Chunk represents an RTMP chunk.
type ChunkHeader ¶
type ChunkHeader struct {
// Format specifies the chunk header format (0-3).
Format uint8
// ChunkStreamID identifies the chunk stream.
ChunkStreamID uint32
// Timestamp represents the chunk timestamp.
Timestamp uint32
// MessageLength represents the message length.
MessageLength uint32
// MessageTypeID represents the message type.
MessageTypeID uint8
// MessageStreamID identifies the message stream.
MessageStreamID uint32
// TimestampDelta represents the timestamp delta.
TimestampDelta uint32
// ExtendedTimestamp represents the extended timestamp.
ExtendedTimestamp uint32
}
ChunkHeader represents the header of an RTMP chunk.
type ChunkStreamer ¶
type ChunkStreamer struct {
// contains filtered or unexported fields
}
ChunkStreamer handles the RTMP chunk stream protocol. It manages reading and writing of chunked RTMP messages over a network connection.
func NewChunkStreamer ¶
func NewChunkStreamer(conn net.Conn) *ChunkStreamer
NewChunkStreamer creates a new chunk streamer for the given connection.
func (*ChunkStreamer) ReadMessage ¶
func (cs *ChunkStreamer) ReadMessage() (*Message, error)
ReadMessage reads a complete RTMP message from the connection.
func (*ChunkStreamer) SetReadChunkSize ¶
func (cs *ChunkStreamer) SetReadChunkSize(size uint32) error
SetReadChunkSize sets the chunk size for reading messages.
func (*ChunkStreamer) SetWriteChunkSize ¶
func (cs *ChunkStreamer) SetWriteChunkSize(size uint32) error
SetWriteChunkSize sets the chunk size for writing messages.
func (*ChunkStreamer) WriteMessage ¶
func (cs *ChunkStreamer) WriteMessage(msg *Message) error
WriteMessage writes a complete RTMP message to the connection.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents an RTMP client connection. It provides methods to connect to RTMP servers and publish/subscribe to streams.
func NewClient ¶
func NewClient(config *ClientConfig) *Client
NewClient creates a new RTMP client with the provided configuration.
func (*Client) Connect ¶
Connect establishes a connection to the RTMP server at the specified address.
func (*Client) ReadMessage ¶
ReadMessage reads an RTMP message from the server.
func (*Client) State ¶
func (c *Client) State() ClientState
State returns the current state of the client.
func (*Client) WriteMessage ¶
WriteMessage sends an RTMP message to the server.
type ClientConfig ¶
type ClientConfig struct {
// ConnectTimeout specifies the maximum time to wait for a connection establishment.
ConnectTimeout time.Duration
// ReadTimeout specifies the timeout for read operations.
ReadTimeout time.Duration
// WriteTimeout specifies the timeout for write operations.
WriteTimeout time.Duration
// ChunkSize specifies the chunk size for RTMP messages.
ChunkSize uint32
// WindowAckSize specifies the window acknowledgement size.
WindowAckSize uint32
// BandwidthLimitType specifies the bandwidth limit type.
BandwidthLimitType uint8
// AppName specifies the application name to connect to.
AppName string
// StreamName specifies the stream name for publishing/subscribing.
StreamName string
// Username for authentication (optional).
Username string
// Password for authentication (optional).
Password string
}
ClientConfig holds configuration options for the RTMP client.
type ClientState ¶
type ClientState int
ClientState represents the current state of the RTMP client.
const ( // ClientStateDisconnected indicates the client is not connected. ClientStateDisconnected ClientState = iota // ClientStateHandshaking indicates the client is performing the RTMP handshake. ClientStateHandshaking // ClientStateConnecting indicates the client is establishing the connection. ClientStateConnecting // ClientStateConnected indicates the client is successfully connected. ClientStateConnected // ClientStatePublishing indicates the client is publishing a stream. ClientStatePublishing // ClientStateSubscribing indicates the client is subscribing to a stream. ClientStateSubscribing // ClientStateError indicates the client encountered an error. ClientStateError )
func (ClientState) String ¶
func (s ClientState) String() string
String returns the string representation of the client state.
type ConnectionState ¶
type ConnectionState int
ConnectionState represents the state of a server connection.
const ( // ConnectionStateConnected indicates the connection is established. ConnectionStateConnected ConnectionState = iota // ConnectionStateHandshaking indicates the handshake is in progress. ConnectionStateHandshaking // ConnectionStateReady indicates the connection is ready for RTMP messages. ConnectionStateReady // ConnectionStatePublishing indicates the connection is publishing a stream. ConnectionStatePublishing // ConnectionStatePlaying indicates the connection is playing a stream. ConnectionStatePlaying // ConnectionStateClosing indicates the connection is being closed. ConnectionStateClosing // ConnectionStateClosed indicates the connection is closed. ConnectionStateClosed // ConnectionStateError indicates the connection encountered an error. ConnectionStateError )
func (ConnectionState) String ¶
func (cs ConnectionState) String() string
String returns the string representation of the connection state.
type HandshakeState ¶
type HandshakeState int
HandshakeState represents the state of the RTMP handshake.
const ( // HandshakeStateUninitialized indicates the handshake hasn't started. HandshakeStateUninitialized HandshakeState = iota // HandshakeStateVersionSent indicates C0/S0 has been sent. HandshakeStateVersionSent // HandshakeStateAckSent indicates C1/S1 has been sent. HandshakeStateAckSent // HandshakeStateHandshakeDone indicates the handshake is complete. HandshakeStateHandshakeDone // HandshakeStateError indicates an error occurred during handshake. HandshakeStateError )
func (HandshakeState) String ¶
func (hs HandshakeState) String() string
String returns the string representation of the handshake state.
type Handshaker ¶
type Handshaker struct {
// contains filtered or unexported fields
}
Handshaker handles the RTMP handshake process. The handshake consists of three parts from each peer: C0/S0: Version (1 byte) C1/S1: Time + Zero + Random (1536 bytes) C2/S2: Time + Time2 + Random echo (1536 bytes)
func NewHandshaker ¶
func NewHandshaker(conn net.Conn, isClient bool) *Handshaker
NewHandshaker creates a new handshaker for the given connection.
func (*Handshaker) DoHandshake ¶
func (h *Handshaker) DoHandshake() error
DoHandshake performs the complete RTMP handshake.
func (*Handshaker) State ¶
func (h *Handshaker) State() HandshakeState
State returns the current handshake state.
func (*Handshaker) WithTimeout ¶
func (h *Handshaker) WithTimeout(duration time.Duration) *Handshaker
type Message ¶
type Message struct {
// ChunkStreamID identifies the chunk stream.
ChunkStreamID uint32
// MessageStreamID identifies the message stream.
MessageStreamID uint32
// Timestamp represents the message timestamp.
Timestamp uint32
// Type represents the message type.
Type MessageType
// Length represents the message length.
Length uint32
// Data contains the message payload.
Data []byte
// AbsoluteTimestamp represents the absolute timestamp.
AbsoluteTimestamp time.Time
}
Message represents an RTMP message.
func NewMessage ¶
func NewMessage(chunkStreamID, messageStreamID uint32, msgType MessageType, data []byte) *Message
NewMessage creates a new RTMP message.
type MessageType ¶
type MessageType uint8
MessageType represents the type of RTMP message.
const ( // MessageTypeSetChunkSize sets the chunk size for subsequent chunks. MessageTypeSetChunkSize MessageType = 1 // MessageTypeAbortMessage aborts a message. MessageTypeAbortMessage MessageType = 2 // MessageTypeAcknowledgement sends an acknowledgement. MessageTypeAcknowledgement MessageType = 3 // MessageTypeUserControlMessage sends user control messages. MessageTypeUserControlMessage MessageType = 4 // MessageTypeWindowAckSize sets the window acknowledgement size. MessageTypeWindowAckSize MessageType = 5 // MessageTypeSetPeerBandwidth sets the peer bandwidth. MessageTypeSetPeerBandwidth MessageType = 6 // MessageTypeAudio sends audio data. MessageTypeAudio MessageType = 8 // MessageTypeVideo sends video data. MessageTypeVideo MessageType = 9 // MessageTypeDataMessageAMF3 sends data in AMF3 format. MessageTypeDataMessageAMF3 MessageType = 15 MessageTypeSharedObjectAMF3 MessageType = 16 // MessageTypeCommandMessageAMF3 sends a command in AMF3 format. MessageTypeCommandMessageAMF3 MessageType = 17 // MessageTypeDataMessageAMF0 sends data in AMF0 format. MessageTypeDataMessageAMF0 MessageType = 18 MessageTypeSharedObjectAMF0 MessageType = 19 // MessageTypeCommandMessageAMF0 sends a command in AMF0 format. MessageTypeCommandMessageAMF0 MessageType = 20 // MessageTypeAggregateMessage sends aggregate message. MessageTypeAggregateMessage MessageType = 22 )
func (MessageType) String ¶
func (mt MessageType) String() string
String returns the string representation of the message type.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server represents an RTMP server that can accept client connections. It handles multiple concurrent client connections and provides callbacks for various RTMP events.
func NewServer ¶
func NewServer(config *ServerConfig) *Server
NewServer creates a new RTMP server with the provided configuration.
func (*Server) GetConnectionCount ¶
GetConnectionCount returns the number of active connections.
func (*Server) GetConnections ¶
func (s *Server) GetConnections() []*ServerConnection
GetConnections returns a slice of all active connections.
type ServerConfig ¶
type ServerConfig struct {
// Addr specifies the network address to listen on.
Addr string
// ReadTimeout specifies the timeout for read operations.
ReadTimeout time.Duration
// WriteTimeout specifies the timeout for write operations.
WriteTimeout time.Duration
// HandshakeTimeout specifies the timeout for handshake completion.
HandshakeTimeout time.Duration
// ChunkSize specifies the default chunk size for new connections.
ChunkSize uint32
// WindowAckSize specifies the window acknowledgement size.
WindowAckSize uint32
// BandwidthLimitType specifies the bandwidth limit type.
BandwidthLimitType uint8
// MaxConnections specifies the maximum number of concurrent connections.
MaxConnections int
// OnConnect is called when a client connects.
OnConnect func(conn *ServerConnection) error
// OnDisconnect is called when a client disconnects.
OnDisconnect func(conn *ServerConnection)
// OnPublish is called when a client wants to publish a stream.
OnPublish func(conn *ServerConnection, streamName string, streamType StreamType) error
// OnPlay is called when a client wants to play a stream.
OnPlay func(conn *ServerConnection, streamName string) error
// OnMessage is called when a message is received from a client.
OnMessage func(conn *ServerConnection, msg *Message) error
}
ServerConfig holds configuration options for the RTMP server.
type ServerConnection ¶
type ServerConnection struct {
// contains filtered or unexported fields
}
ServerConnection represents a client connection to the server.
func (*ServerConnection) BytesReceived ¶
func (sc *ServerConnection) BytesReceived() uint64
BytesReceived returns the total number of bytes received.
func (*ServerConnection) BytesSent ¶
func (sc *ServerConnection) BytesSent() uint64
BytesSent returns the total number of bytes sent.
func (*ServerConnection) Close ¶
func (sc *ServerConnection) Close() error
Close closes the connection.
func (*ServerConnection) ConnectTime ¶
func (sc *ServerConnection) ConnectTime() time.Time
ConnectTime returns the time when the connection was established.
func (*ServerConnection) ID ¶
func (sc *ServerConnection) ID() string
ID returns the unique connection identifier.
func (*ServerConnection) LastActivity ¶
func (sc *ServerConnection) LastActivity() time.Time
LastActivity returns the time of the last activity on this connection.
func (*ServerConnection) ReadMessage ¶
func (sc *ServerConnection) ReadMessage() (*Message, error)
ReadMessage reads an RTMP message from the client.
func (*ServerConnection) RemoteAddr ¶
func (sc *ServerConnection) RemoteAddr() net.Addr
RemoteAddr returns the remote network address.
func (*ServerConnection) State ¶
func (sc *ServerConnection) State() ConnectionState
State returns the current connection state.
func (*ServerConnection) WriteMessage ¶
func (sc *ServerConnection) WriteMessage(msg *Message) error
WriteMessage sends an RTMP message to the client.
type Stream ¶
type Stream struct {
Name string
Type StreamType
Publisher *ServerConnection
Subscribers []*ServerConnection
StartTime time.Time
Active bool
Metadata map[string]interface{}
}
Stream represents a stream being published or played.
type StreamType ¶
type StreamType string
StreamType represents the type of stream for publishing.
const ( // StreamTypeLive indicates a live stream. StreamTypeLive StreamType = "live" // StreamTypeRecord indicates a recorded stream. StreamTypeRecord StreamType = "record" // StreamTypeAppend indicates appending to a recorded stream. StreamTypeAppend StreamType = "append" )