Documentation ¶
Overview ¶
RTMP protocol golang implementation
Index ¶
- Constants
- Variables
- func CalcDHPos(buf []byte, offset uint32, mod_val uint32, add_val uint32) (digest_pos uint32)
- func CalcDigestPos(buf []byte, offset uint32, mod_val uint32, add_val uint32) (digest_pos uint32)
- func CheckError(err error, name string)
- func CopyNFromNetwork(dst Writer, src Reader, n int64) (written int64, err error)
- func CopyNToNetwork(dst Writer, src Reader, n int64) (written int64, err error)
- func CreateRandomBlock(size uint) []byte
- func FlushToNetwork(w *bufio.Writer) (err error)
- func GetTimestamp() uint32
- func HMACsha256(msgBytes []byte, key []byte) ([]byte, error)
- func Handshake(c net.Conn, br *bufio.Reader, bw *bufio.Writer, timeout time.Duration) (err error)
- func HandshakeSample(c net.Conn, br *bufio.Reader, bw *bufio.Writer, timeout time.Duration) (err error)
- func ImprintWithDigest(buf []byte, key []byte) uint32
- func ReadAtLeastFromNetwork(r Reader, buf []byte, min int) (n int, err error)
- func ReadBaseHeader(rbuf Reader) (n int, fmt uint8, csi uint32, err error)
- func ReadByteFromNetwork(r Reader) (b byte, err error)
- func SHandshake(c net.Conn, br *bufio.Reader, bw *bufio.Writer, timeout time.Duration) (err error)
- func SetLogger(l Logger)
- func ValidateDigest(buf []byte, offset uint32, key []byte) uint32
- func WriteToNetwork(w Writer, data []byte) (written int, err error)
- type AMFVersion
- type AudioEvent
- type ClientChunkStream
- type ClientConn
- type ClientPlayStream
- type ClientPublishStream
- type ClientStream
- type ClientStreamHandler
- type ClosedEvent
- type Command
- type CommandEvent
- type Conn
- type ConnHandler
- type ConnectionStatus
- type Header
- type InboundChunkStream
- type LogLevel
- type Logger
- type Message
- type MetadataEvent
- type RTMPEvent
- type Reader
- type RtmpURL
- type StatusEvent
- type StreamBegin
- type StreamCreatedEvent
- type StreamDry
- type StreamEOF
- type StreamIsRecorded
- type UnknownDataEvent
- type VideoEvent
- type Writer
Constants ¶
const ( ConnectionStatusClose ConnectionStatus = 0 ConnectionStatusHandshakeOK = 1 ConnectionStatusConnect = 2 ConnectionStatusConnectOK = 3 ConnectionStatusCreateStream = 4 ConnectionStatusCreateStreamOK = 5 )
const ( HEADER_FMT_FULL = 0x00 HEADER_FMT_SAME_STREAM = 0x01 HEADER_FMT_SAME_LENGTH_AND_STREAM = 0x02 HEADER_FMT_CONTINUATION = 0x03 )
Chunk Message Header - "fmt" field values
const ( RESULT_CONNECT_OK = "NetConnection.Connect.Success" RESULT_CONNECT_REJECTED = "NetConnection.Connect.Rejected" RESULT_CONNECT_OK_DESC = "Connection successed." RESULT_CONNECT_REJECTED_DESC = "[ AccessManager.Reject ] : [ code=400 ] : " NETSTREAM_PLAY_START = "NetStream.Play.Start" NETSTREAM_PLAY_RESET = "NetStream.Play.Reset" NETSTREAM_PUBLISH_START = "NetStream.Publish.Start" )
Result codes
const ( CS_ID_PROTOCOL_CONTROL = uint32(2) CS_ID_COMMAND = uint32(3) CS_ID_USER_CONTROL = uint32(4) )
Chunk stream ID
const ( // The value of the chunk size is carried as 4-byte message payload. A // default value exists for chunk size, but if the sender wants to // change this value it notifies the peer about it through this // protocol message. For example, a client wants to send 131 bytes of // data and the chunk size is at its default value of 128. So every // message from the client gets split into two chunks. The client can // choose to change the chunk size to 131 so that every message get // split into two chunks. The client MUST send this protocol message to // the server to notify that the chunk size is set to 131 bytes. // The maximum chunk size can be 65536 bytes. Chunk size is maintained // independently for server to client communication and client to server // communication. // // 0 1 2 3 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // | chunk size (4 bytes) | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // Figure 2 Pay load for the protocol message ‘Set Chunk Size’ // // chunk size: 32 bits // This field holds the new chunk size, which will be used for all // future chunks sent by this chunk stream. SET_CHUNK_SIZE = uint8(1) // Abort Message // // Protocol control message 2, Abort Message, is used to notify the peer // if it is waiting for chunks to complete a message, then to discard // the partially received message over a chunk stream and abort // processing of that message. The peer receives the chunk stream ID of // the message to be discarded as payload of this protocol message. This // message is sent when the sender has sent part of a message, but wants // to tell the receiver that the rest of the message will not be sent. // // 0 1 2 3 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // | chunk stream id (4 bytes) | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // Figure 3 Pay load for the protocol message ‘Abort Message’. // // // chunk stream ID: 32 bits // This field holds the chunk stream ID, whose message is to be // discarded. ABORT_MESSAGE = uint8(2) // Acknowledgement // // The client or the server sends the acknowledgment to the peer after // receiving bytes equal to the window size. The window size is the // maximum number of bytes that the sender sends without receiving // acknowledgment from the receiver. The server sends the window size to // the client after application connects. This message specifies the // sequence number, which is the number of the bytes received so far. // 0 1 2 3 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // | sequence number (4 bytes) | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // Figure 4 Pay load for the protocol message ‘Acknowledgement’. // // sequence number: 32 bits // This field holds the number of bytes received so far. ACKNOWLEDGEMENT = uint8(3) // User Control Message // // The client or the server sends this message to notify the peer about // the user control events. This message carries Event type and Event // data. // +------------------------------+------------------------- // | Event Type ( 2- bytes ) | Event Data // +------------------------------+------------------------- // Figure 5 Pay load for the ‘User Control Message’. // // // The first 2 bytes of the message data are used to identify the Event // type. Event type is followed by Event data. Size of Event data field // is variable. USER_CONTROL_MESSAGE = uint8(4) // Window Acknowledgement Size // // The client or the server sends this message to inform the peer which // window size to use when sending acknowledgment. For example, a server // expects acknowledgment from the client every time the server sends // bytes equivalent to the window size. The server updates the client // about its window size after successful processing of a connect // request from the client. // // 0 1 2 3 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // | Acknowledgement Window size (4 bytes) | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // Figure 6 Pay load for ‘Window Acknowledgement Size’. WINDOW_ACKNOWLEDGEMENT_SIZE = uint8(5) // Set Peer Bandwidth // // The client or the server sends this message to update the output // bandwidth of the peer. The output bandwidth value is the same as the // window size for the peer. The peer sends ‘Window Acknowledgement // Size’ back if its present window size is different from the one // received in the message. // 0 1 2 3 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // | Acknowledgement Window size | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // | Limit type | // +-+-+-+-+-+-+-+-+ // Figure 7 Pay load for ‘Set Peer Bandwidth’ // // The sender can mark this message hard (0), soft (1), or dynamic (2) // using the Limit type field. In a hard (0) request, the peer must send // the data in the provided bandwidth. In a soft (1) request, the // bandwidth is at the discretion of the peer and the sender can limit // the bandwidth. In a dynamic (2) request, the bandwidth can be hard or // soft. SET_PEER_BANDWIDTH = uint8(6) // Audio message // // The client or the server sends this message to send audio data to the // peer. The message type value of 8 is reserved for audio messages. AUDIO_TYPE = uint8(8) // Video message // // The client or the server sends this message to send video data to the // peer. The message type value of 9 is reserved for video messages. // These messages are large and can delay the sending of other type of // messages. To avoid such a situation, the video message is assigned // the lowest priority. VIDEO_TYPE = uint8(9) // Aggregate message // // An aggregate message is a single message that contains a list of sub- // messages. The message type value of 22 is reserved for aggregate // messages. AGGREGATE_MESSAGE_TYPE = uint8(22) // Shared object message // // A shared object is a Flash object (a collection of name value pairs) // that are in synchronization across multiple clients, instances, and // so on. The message types kMsgContainer=19 for AMF0 and // kMsgContainerEx=16 for AMF3 are reserved for shared object events. // Each message can contain multiple events. SHARED_OBJECT_AMF0 = uint8(19) SHARED_OBJECT_AMF3 = uint8(16) // Data message // // The client or the server sends this message to send Metadata or any // user data to the peer. Metadata includes details about the // data(audio, video etc.) like creation time, duration, theme and so // on. These messages have been assigned message type value of 18 for // AMF0 and message type value of 15 for AMF3. DATA_AMF0 = uint8(18) DATA_AMF3 = uint8(15) // Command message // // Command messages carry the AMF-encoded commands between the client // and the server. These messages have been assigned message type value // of 20 for AMF0 encoding and message type value of 17 for AMF3 // encoding. These messages are sent to perform some operations like // connect, createStream, publish, play, pause on the peer. Command // messages like onstatus, result etc. are used to inform the sender // about the status of the requested commands. A command message // consists of command name, transaction ID, and command object that // contains related parameters. A client or a server can request Remote // Procedure Calls (RPC) over streams that are communicated using the // command messages to the peer. COMMAND_AMF0 = uint8(20) COMMAND_AMF3 = uint8(17) // Keng-die!!! Just ignore one byte before AMF0. )
Message type
const ( EVENT_STREAM_BEGIN = uint16(0) EVENT_STREAM_EOF = uint16(1) EVENT_STREAM_DRY = uint16(2) EVENT_SET_BUFFER_LENGTH = uint16(3) EVENT_STREAM_IS_RECORDED = uint16(4) EVENT_PING_REQUEST = uint16(6) EVENT_PING_RESPONSE = uint16(7) EVENT_REQUEST_VERIFY = uint16(0x1a) EVENT_RESPOND_VERIFY = uint16(0x1b) EVENT_BUFFER_EMPTY = uint16(0x1f) EVENT_BUFFER_READY = uint16(0x20) )
const ( BINDWIDTH_LIMIT_HARD = uint8(0) BINDWIDTH_LIMIT_SOFT = uint8(1) BINDWIDTH_LIMIT_DYNAMIC = uint8(2) )
const ( MAX_TIMESTAMP = uint32(2000000000) AUTO_TIMESTAMP = uint32(0XFFFFFFFF) DEFAULT_HIGH_PRIORITY_BUFFER_SIZE = 2048 DEFAULT_MIDDLE_PRIORITY_BUFFER_SIZE = 128 DEFAULT_LOW_PRIORITY_BUFFER_SIZE = 64 DEFAULT_CHUNK_SIZE = uint32(128) DEFAULT_WINDOW_SIZE = 2500000 DEFAULT_CAPABILITIES = float64(15) DEFAULT_AUDIO_CODECS = float64(4071) DEFAULT_VIDEO_CODECS = float64(252) FMS_CAPBILITIES = uint32(255) FMS_MODE = uint32(2) SET_PEER_BANDWIDTH_HARD = byte(0) SET_PEER_BANDWIDTH_SOFT = byte(1) SET_PEER_BANDWIDTH_DYNAMIC = byte(2) )
const ( RTMP_SIG_SIZE = 1536 RTMP_LARGE_HEADER_SIZE = 12 SHA256_DIGEST_LENGTH = 32 RTMP_DEFAULT_CHUNKSIZE = 128 )
const (
RTMP_LOG_NAME = "rtmp"
)
Variables ¶
var ( FLASH_PLAYER_VERSION = []byte{0x09, 0x00, 0x7C, 0x02} FLASH_PLAYER_VERSION_STRING = "LNX 9,0,124,2" SWF_URL_STRING = "http://localhost/1.swf" PAGE_URL_STRING = "http://localhost/1.html" MIN_BUFFER_LENGTH = uint32(256) FMS_VERSION = []byte{0x04, 0x05, 0x00, 0x01} FMS_VERSION_STRING = "4,5,0,297" )
var ( GENUINE_FMS_KEY = []byte{ 0x47, 0x65, 0x6e, 0x75, 0x69, 0x6e, 0x65, 0x20, 0x41, 0x64, 0x6f, 0x62, 0x65, 0x20, 0x46, 0x6c, 0x61, 0x73, 0x68, 0x20, 0x4d, 0x65, 0x64, 0x69, 0x61, 0x20, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x30, 0x30, 0x31, 0xf0, 0xee, 0xc2, 0x4a, 0x80, 0x68, 0xbe, 0xe8, 0x2e, 0x00, 0xd0, 0xd1, 0x02, 0x9e, 0x7e, 0x57, 0x6e, 0xec, 0x5d, 0x2d, 0x29, 0x80, 0x6f, 0xab, 0x93, 0xb8, 0xe6, 0x36, 0xcf, 0xeb, 0x31, 0xae, } GENUINE_FP_KEY = []byte{ 0x47, 0x65, 0x6E, 0x75, 0x69, 0x6E, 0x65, 0x20, 0x41, 0x64, 0x6F, 0x62, 0x65, 0x20, 0x46, 0x6C, 0x61, 0x73, 0x68, 0x20, 0x50, 0x6C, 0x61, 0x79, 0x65, 0x72, 0x20, 0x30, 0x30, 0x31, 0xF0, 0xEE, 0xC2, 0x4A, 0x80, 0x68, 0xBE, 0xE8, 0x2E, 0x00, 0xD0, 0xD1, 0x02, 0x9E, 0x7E, 0x57, 0x6E, 0xEC, 0x5D, 0x2D, 0x29, 0x80, 0x6F, 0xAB, 0x93, 0xB8, 0xE6, 0x36, 0xCF, 0xEB, 0x31, 0xAE, } )
var DefaultObjectEncoding uint = amf.AMF0
Functions ¶
func CalcDigestPos ¶
func CopyNFromNetwork ¶
Copy bytes from network
func CopyNToNetwork ¶
Copy bytes to network
func CreateRandomBlock ¶
func FlushToNetwork ¶
func HandshakeSample ¶
func ImprintWithDigest ¶
func ReadAtLeastFromNetwork ¶
Read bytes from network
func ReadBaseHeader ¶
Read Base Header from io.Reader High level protocol can use chunk stream ID to query the previous header instance.
func ReadByteFromNetwork ¶
Read byte from network
func SHandshake ¶
Types ¶
type AudioEvent ¶
type AudioEvent struct {
Message *Message
}
type ClientChunkStream ¶
type ClientChunkStream struct { ID uint32 // contains filtered or unexported fields }
Chunk stream
A logical channel of communication that allows flow of chunks in a particular direction. The chunk stream can travel from the client to the server and reverse.
func NewClientChunkStream ¶
func NewClientChunkStream(id uint32) *ClientChunkStream
func (*ClientChunkStream) GetTimestamp ¶
func (chunkStream *ClientChunkStream) GetTimestamp() uint32
func (*ClientChunkStream) NewClientHeader ¶
func (chunkStream *ClientChunkStream) NewClientHeader(message *Message) *Header
type ClientConn ¶
type ClientConn interface { // Connect an appliction on FMS after handshake. Connect(extendedParameters ...interface{}) (err error) // Create a stream CreateStream() (err error) // Close a connection Close() // URL to connect URL() string // Connection status Status() (ConnectionStatus, error) // Send a message Send(message *Message) error // Calls a command or method on Flash Media Server // or on an application server running Flash Remoting. Call(name string, customParameters ...interface{}) (err error) // Get network connect instance Conn() Conn // Returns a channel of RTMPEvents Events() <-chan RTMPEvent }
func Dial ¶
func Dial(url string, maxChannelNumber int) (ClientConn, error)
Connect to FMS server, and finish handshake process using a default instance of net.Dialer
func DialWithDialer ¶
Connect to FMS server, and finish handshake process
func NewOutbounConn ¶
Connect to FMS server, and finish handshake process
type ClientPlayStream ¶
type ClientPlayStream interface { // Play Play(streamName string, start, duration *uint32, reset *bool) (err error) // Seeks the kerframe closedst to the specified location. Seek(offset uint32) }
A play stream
type ClientPublishStream ¶
type ClientPublishStream interface { // Publish Publish(name, t string) (err error) // Send audio data SendAudioData(data []byte) error // Send video data SendVideoData(data []byte) error }
A publish stream
type ClientStream ¶
type ClientStream interface { ClientPublishStream ClientPlayStream // ID ID() uint32 // Pause Pause() error // Resume Resume() error // Close Close() // Received messages Received(message *Message) (handlered bool) // Attach handler Attach(handler ClientStreamHandler) // Publish audio data PublishAudioData(data []byte, deltaTimestamp uint32) error // Publish video data PublishVideoData(data []byte, deltaTimestamp uint32) error // Publish data PublishData(dataType uint8, data []byte, deltaTimestamp uint32) error // Call Call(name string, customParameters ...interface{}) error }
A RTMP logical stream on connection.
type ClientStreamHandler ¶
type ClientStreamHandler interface { OnPlayStart(stream ClientStream) OnPublishStart(stream ClientStream) }
type ClosedEvent ¶
type ClosedEvent struct { }
type Command ¶
Command
Command messages carry the AMF encoded commands between the client and the server. A client or a server can request Remote Procedure Calls (RPC) over streams that are communicated using the command messages to the peer.
type CommandEvent ¶
type CommandEvent struct {
Command *Command
}
type Conn ¶
type Conn interface { Close() Send(message *Message) error CreateChunkStream(ID uint32) (*ClientChunkStream, error) CloseChunkStream(ID uint32) NewTransactionID() uint32 CreateMediaChunkStream() (*ClientChunkStream, error) CloseMediaChunkStream(id uint32) SetStreamBufferSize(streamId uint32, size uint32) ClientChunkStream(id uint32) (chunkStream *ClientChunkStream, found bool) InboundChunkStream(id uint32) (chunkStream *InboundChunkStream, found bool) SetWindowAcknowledgementSize() SetPeerBandwidth(peerBandwidth uint32, limitType byte) SetChunkSize(chunkSize uint32) SendUserControlMessage(eventId uint16) }
Conn
Common connection functions
type ConnHandler ¶
type ConnHandler interface { // Received message OnReceived(conn Conn, message *Message) // Received command OnReceivedRtmpCommand(conn Conn, command *Command) // Connection closed OnClosed(conn Conn) OnReceivedRtmpControl(eventType uint16, streamID uint32, message *Message) }
Connection handler
type ConnectionStatus ¶
type ConnectionStatus uint
func (ConnectionStatus) String ¶
func (s ConnectionStatus) String() string
type Header ¶
type Header struct { // Basic Header Fmt uint8 ChunkStreamID uint32 // Chunk Message Header Timestamp uint32 MessageLength uint32 MessageTypeID uint8 MessageStreamID uint32 // Extended Timestamp ExtendedTimestamp uint32 }
RTMP Chunk Header
The header is broken down into three parts:
| Basic header|Chunk Msg Header|Extended Time Stamp| Chunk Data |
Chunk basic header: 1 to 3 bytes
This field encodes the chunk stream ID and the chunk type. Chunk type determines the format of the encoded message header. The length depends entirely on the chunk stream ID, which is a variable-length field.
Chunk message header: 0, 3, 7, or 11 bytes
This field encodes information about the message being sent (whether in whole or in part). The length can be determined using the chunk type specified in the chunk header.
Extended timestamp: 0 or 4 bytes
This field MUST be sent when the normal timsestamp is set to 0xffffff, it MUST NOT be sent if the normal timestamp is set to anything else. So for values less than 0xffffff the normal timestamp field SHOULD be used in which case the extended timestamp MUST NOT be present. For values greater than or equal to 0xffffff the normal timestamp field MUST NOT be used and MUST be set to 0xffffff and the extended timestamp MUST be sent.
func (*Header) ReadHeader ¶
func (header *Header) ReadHeader(rbuf Reader, vfmt uint8, csi uint32, lastheader *Header) (n int, err error)
Read new chunk stream header from io.Reader
func (*Header) RealTimestamp ¶
type InboundChunkStream ¶
type InboundChunkStream struct { ID uint32 // contains filtered or unexported fields }
func NewInboundChunkStream ¶
func NewInboundChunkStream(id uint32) *InboundChunkStream
type Logger ¶
type Logger interface { ModulePrintf(level LogLevel, format string, v ...interface{}) ModulePrintln(level LogLevel, v ...interface{}) }
Logging interface that can be implemented by users of this package.
type Message ¶
type Message struct { ChunkStreamID uint32 Timestamp uint32 Size uint32 Type uint8 StreamID uint32 Buf *bytes.Buffer IsInbound bool AbsoluteTimestamp uint32 }
Message
The different types of messages that are exchanged between the server and the client include audio messages for sending the audio data, video messages for sending video data, data messages for sending any user data, shared object messages, and command messages.
type MetadataEvent ¶
type MetadataEvent struct { AMFVersion AMFVersion Message *Message }
type RtmpURL ¶
type RtmpURL struct {
// contains filtered or unexported fields
}
type StatusEvent ¶
type StatusEvent struct {
Status ConnectionStatus
}
type StreamBegin ¶
type StreamBegin struct {
StreamID uint32
}
type StreamCreatedEvent ¶
type StreamCreatedEvent struct {
Stream ClientStream
}
type StreamIsRecorded ¶
type StreamIsRecorded struct {
StreamID uint32
}
type UnknownDataEvent ¶
type UnknownDataEvent struct {
Message *Message
}
type VideoEvent ¶
type VideoEvent struct {
Message *Message
}