[Post] rtmp protocol Research

Source: Internet
Author: User
Tags float double

After reading the flex/flash related stuff for a week, I found that I was always dealing with the rtmp word. Today I finally made up my mind to learn more about this stuff and find a good one.Article, Reproduced with this, the original address: http://blog.csdn.net/chenyanxu/archive/2009/09/02/4511087.aspx

Body start:

Rtmp protocol Research

 

1 Protocol Research Overview
Protocol design and analysis have always been encountered at work. Here we will sum up that no one can leave the protocol in this network era. He exists in any corner of our lives, but we do not notice its existence at ordinary times. We can say that if there is no agreement, neither our life nor our daily work and production can be carried out. If you think about all the things you use in your life, the protocols are included. What is an agreement? Simply put, the two sides reach a consensus to facilitate better communication. What is the theoretical agreement? If anyone who has learned "signal and system" knows that there is a simple truth, that is, the information will not be lost when it passes through the symbol set of one pipeline to another.

All complex things have the simplest nature. The same is true for network protocols. In addition to the concept of the upper and lower layers, the Protocol only contains the rules of the entities of both parties.

Generally, protocols contain the most basic protocol header. Whether it is the physical layer, link layer, or network layer, this header constitutes the essence of the Protocol. Generally, the protocol header must contain the following basic information:

The unique identifier of the two parties. It is used to indicate the objects of both parties.
Type description or description of the net core, indicating the content of the net core.
The length of the Protocol's net core, which is used to extract the content of the net core.
Among them, the first two items are required. Without them, the interaction between the communication parties is not guaranteed. The third item can be removed from the inflexible communication, and the second item is available.

The richness of the Protocol is reflected by the diversity of net cores.

In addition to the above three terms, the protocol header can also add more information (such as control information and time information), depending on the specific application. When looking at the agreement, we can better grasp the subject of the agreement for analysis and design.

  

2 rtmp protocol Overview
The rtmp protocol is used by flash for object, video, and audio transmission. The Protocol is built on TCP or HTTP. rtmp is like a container for loading data packets. The data can be in AMF format, it can also be the video/audio data in FLV. A single connection can transmit multiple network streams through different channels. packages in these channels are transmitted according to fixed packages.

  

3 rtmp Protocol

3.1 protocol header
Struct rtmp_head

{

Char cchannelid: 6; // The last 6 digits of the first byte

Char ccheadsize; // The first two bytes

Char ctimer [3]; // time information represented by three bytes

Char clength [3]; // length represented by three bytes

Char cdatatype; // data type

Char sstreamid [4]; // Stream ID

};

There are three basic elements (unique identifier), (type), and (net core length): cchannelid, cdatatype, and clength.

 

3.2 Data Type
The data type determines what the upper layer of the Protocol can do, and the rules that must be followed by the user who uses the protocol.

At the same time, the data type shows the basic content of the net core.

Rtmp data type:

0 × 01, chunk size, changes the chunk size for packets
0 × 02, unknown, anyone know this one?
0 × 03, bytes read, send every X bytes read by both sides
0 × 04, ping, ping is a stream control message, has subtypes
0 × 05, server BW, the servers downstream BW
0 × 06, client BW, the clients upstream BW
0 × 07, unknown, anyone know this one?
0 × 08, audio data, packet containing audio
0 × 09, video data, packet containing video data
0 × 0a-0 × 11, unknown, anyone know?
0 × 12, 127y, an invoke which does not necessarily CT a reply
0 × 13, shared object, has subtypes
0 × 14, invoke, like remoting call, used for stream actions too.

3.3 net core of the Protocol
The net core of rtmp protocol is described in the AMF format. The AMF format is generated for the rtmp protocol service. The original rtmp uses XML to transmit data, however, XML only transmits data in the format of value pairs in character form. With the popularization of applications, this does not meet the requirements, such as objects, structures, arrays, and even datasets, with the DataGrid component, You can conveniently display data.
To process complex data types, it is imperative to use a unique method to transfer data between flash and the application server. So AMF came into being.

AMF is an exclusive communication protocol developed by Adobe. It adopts binary compression, serialization, deserialization, and data transmission, this provides a lightweight and efficient communication mode for the flash player to communicate with the flash remoting gateway.

The biggest feature of AMF is that flash built-in objects such as object, array, date, and XML can be directly transmitted back to the server and automatically parsed to appropriate objects on the server, this reduces the complex work of developers and saves development time. Because AMF uses binary encoding, this method can compress data highly, so it is very suitable for transferring a large amount of data. The larger the data volume, the higher the transmission efficiency of Flash remoting, far exceeding the web service. For XML, loadvars, and loadvariables (), they use plain text transmission methods, and their performance cannot be compared with Flash remoting.

 
Note: Flash remoting requires the browser to support binary post, and the flash player is in Netscape 6. x. running flash remoting in the environment does not work (the flash remoting call has no effect and no error is returned). Netscape 7 has corrected this bug. This problem also exists for early Apple Safari and chimera versions.
It is also a lightweight Data Exchange Protocol, also by calling remote services, also based on standard HTTP and HTTPS protocols, why does flash remoting choose to use AMF and discard the communication between soap and flash players? The reasons are as follows:
 
Soap processes data in XML format, which is too lengthy compared with the binary file system;
AMF can more effectively serialize data, because AMF was originally designed to support the data types of Flash ActionScript, but soap is committed to providing a wider range of uses;
AMF supports Flash Player 6, which only requires a browser to increase the size of around 4 kb (after compression), while soap is mostly used;

Some SOAP header file requests are not supported in Flash Player 6. Why does Flash Player 6 access the soap-based Web service? The Flash remoting gateway used to convert the SOAP request on the server side to an atom file format, and then communicate with the flash player using an atom file.

 

In addition, the AMF package contains onresult events (such as response events) and onstatus events (such as error events). These event objects can be directly used in flash.
AMF has evolved from amf0 in the Flash MX era to amf3. Amf3 is used as the default serialization format for the event 3.0 of Flash playe 9, while amf0 is used as the serialization format for the old versions of event 1.0 and 2.0. In terms of network data transmission, amf3 is more efficient than amf0. Amf3 can transmit int and uint objects as integers, and serializes data types supported by ActionScript 3.0, such as bytearray, XML, and iexternalizable.

AMF solves the richness of content. (For details about the AMF format, refer to the appendix format document)

3.3.1 Data Types in AMF

Amf0 supports the following data types (with their type field values ):

 

Number = 0x00
Boolean = 0x01
String = 0x02
Object = 0x03
Movieclip = 0x04
Null_value = 0x05
Undefined = 0x06
Reference = 0x07
Ecma_array = 0x08
Object_end = 0x09
Strict_array = 0x0a
Date = 0x0b
Long_string = 0x0c
Unsupported = 0x0d
Record_set = 0x0e
Xml_object = 0x0f
Typed_object = 0x10

Binary format
AMF format for a value/object consists of a type byte (see above) followed by zero or more bytes. This section describes the bytes following the type byte for various types.

Number (type byte: 0x00)
Numbers are stored as 8 byte (big endian) float double. on x86 you can just byteswap a double to encode it correctly.

Boolean (type byte: 0x01)
A boolean is encoded in one byte. fixme: Is true sent as 0xff? 0x01?

String (type byte: 0x02)
A string is encoded as a 2 byte (big endian) count (number of bytes) followed by that contains bytes of text. Note: There is no null Terminator.

I think the text is assumed to be UTF-8. Can someone double check me on this?

Null_value (type byte: 0x05)
A null has zero bytes following the type byte

Undefined (type byte: 0x06)
A undefined has zero bytes following the type byte

Object (type byte: 0x08)
An object is encoded as a series of key/value pairs. The key is encoded as a string (above) with no type byte, and the value is any AMF value.

The object encoding is terminated by 0x000009 (that is a zero length string key, followed by the object_end type byte described below.

Object_end (type byte: 0x09)
This is not really a value, but a marker for the end of an object. See above.

Strict_array (type byte: 0x0a)
This is the encoding for arrays such as ["foo", "bar", 1, 2, 3]. for a hash (a set of key/value pairs) you'll need to use object above.

An array is encoded as 4 byte (big endian) Integer Which is the number of elements in the array, followed by that parameter AMF values.

That's it. There's no Terminator of any kind.

Use in shared object files
While most AMF objects are just a value, there is a special variation used by shared object files for properties. rather than start with the type field, followed by the length, it starts with a byte count, then the name, and then the regular AMF type field, the length, and then the data.

  

3.4 client-server connection process

3.4.1 handshake between customers and servers
 

Flash Player uses the system time as the seedAlgorithmThe generated digital signature is the first handshake to the server in 1537 bytes. The server generates a 3073-byte verification package based on the client's digital signature and sends it to the client, after receiving a response from the server, the client sends a 1536-byte response.

Specific process:

Send handshark1
Receive the second handshake package handshark2
Handshark3, a three-way handshake packet sent
Both handshark1 and handshark2 are started with 0x03. The three handshakes are not the content of the rtmp protocol, so there is no rtmp header contained here. It is used by the server manufacturer's own products for verification. Strictly speaking, you must use Adobe's client and server to use my protocol.

3.4.2 communication between customers and servers
Specific connection and request video process

Send the rtmp_connect command
Send a local bandwidth message. The default value is 125000.
The server returns the server bandwidth information.
The server returns the local bandwidth information.
The server returns the connection success message "netconnection. Connect. Success"
The client sends the create stream request encodecreatestreampacket
The server returns the message indicating that the stream is successfully created.
The client sends the stream playback file message rtmp_play.
The server returns the type_chunk_size message.
The server returns the start playback message "netstream. Play. Start"
The server returns the video information (type_stream_metadata), including the size, width, height, and speed. The file length can be calculated here.
The rtmp net core determines the content service. Adobe's server uses the AMF string command to control video transmission and playback. The specific string command information is as follows:

(Note: the definition of the string is defined by the manufacturer (Adobe), as long as it meets the AMF format)

Netconnection. Call. Failed
Netconnection. Call. badversion
Netconnection. Connect. appshutdown
Netconnection. Connect. Closed
Netconnection. Connect. Rejected
Netconnection. Connect. Success
Netstream. Clear. Success
Netstream. Clear. Failed
Netstream. Publish. Start
Netstream. Publish. badname
Netstream. Failed
Netstream. unpublish. Success
Netstream. Record. Start
Netstream. Record. noaccess
Netstream. Record. Stop
Netstream. Record. Failed
Netstream. Play. insufficientbw
Netstream. Play. Start
Netstream. Play. streamnotfound
Netstream. Play. Stop
Netstream. Play. Failed
Netstream. Play. Reset
Netstream. Play. publishnotify
Netstream. Play. unpublishnotify
Netstream. Data. Start
Application. Script. Error
Application. Script. Warning
Application. Resource. lowmemory
Application. Shutdown
Application. GC
Play
Pause
Demoservice. getlistofavailableflvs
Getstreamlength
Connect
App
Flashver
Swfurl
Tcurl
Fpad
Capabilities
Audiocodecs
Audiocodecs
Videocodecs
Videofunction
Pageurl
Createstream
Deletestream
Duration
Framerate
Audiocodecid
Audiodatarate
Videocodecid
Videodatarate
Height
Width

3.4.2 Data Extraction
The message "netstream. play. after "start", the server will start to transmit data to the client. Generally, data extraction is performed by parsing the protocol header first, then, the data part can be taken out based on the data type and length of the protocol header. This is also true for the rtmp protocol.

Struct rtmp_head

{

Char cchannelid: 6; // The last 6 digits of the first byte

Char ccheadsize; // The first two bytes

Char ctimer [3]; // time information represented by three bytes

Char clength [3]; // length represented by three bytes

Char cdatatype; // data type

Char sstreamid [4]; // Stream ID

}

First, determine the type of cdatatype, and then extract the data Parts Based on Different Types for different processing. Obtain the video data to see if it is of the following type:

0 × 08 audio data packet containing audio
0 × 09 video data packet containing video data

 
The audio and video data in the memory is read based on the length of the core. The audio and video data here has a certain encoding format, which depends on the specific configuration of the application. Flash play uses the FLV format. To access this part of the data, we also need to do some work to shell the FLV video data and retrieve the data and save the file.

Bytes ------------------------------------------------------------------------------------

The following is a reposted on Baidu encyclopedia, which is easy to understand in comparison with the above:

Rtmp: routing table maintenance protocol (Route Selection table maintenance protocol)

In the appletalk protocol group, the routing table maintenance protocol (rtmp, routing table Protocol) is a transport layer protocol that establishes and maintains a routing table in the appletalk router. Rtmp is based on the route selection Information Protocol (RIP ). Like rip, rtmp uses Hops as the routing Metering Standard. When a data packet is sent from the source network to the target network, the number of routers or other intermediate media nodes that must pass is calculated as the number of hops.

Overview of real time messaging protocol

Real-Time Message Transfer Protocol is a proprietary protocol developed by Adobe Systems for audio, video, and data transmission between flash players and servers. It has three variants:

1) the plain text protocol working on TCP uses port 1935;

2) rtmpt is encapsulated in HTTP requests and can pass through the firewall;

3) rtmps is similar to rtmpt, but uses HTTPS connections;

Introduction:

The rtmp protocol is used by flash for object, video, and audio transmission. The Protocol is established over TCP or HTTP.

The rtmp protocol is like a container for loading data packets. The data can be AMF data or FLV video/audio data.

A single connection can transmit multiple network streams through different channels. packages in these channels are transmitted according to fixed packages.

Connection)

Simple connection and playback of a stream using an ActionScriptCode:

Copy the content to the clipboard code:

VaR videoinstance: Video = your_video_instance;

VaR NC: netconnection = new netconnection ();

VaR connected: Boolean = nC. Connect ("rtmp:/localhost/MyApp ");

VaR ns: netstream = new netstream (NC );

Videoinstance. attachvideo (NS );

NS. Play ("flvname ");

The default port is 1935.

Handshake

Client → server: Send a handshake request to the server. this is not part of the protocol package. The first byte of the handshake request is (0 × 03), followed by 1536 bytes. management seems that this part of content is not crucial for the rtmp protocol, but it cannot be taken at will.

Server → client: the server responds to the handshake request from the client. this part of the data is still not part of the rtmp protocol. in fact, the response bytes are still (0x03), but the back is followed by a mass with a length of 1536 bytes (a total of 3072. the first 1536 pieces seem to be any content, or even null. the second code block of 1536 is the content of the handshake request sent from the client to the server in the previous step.

Client → server: The second 1536-byte data block that the server responded to the client from the previous step.

Now the handshake between the client and the server ends, and the rtmp Package content will be sent below.

Client → server: Send a connection package to the server.

Server → client: Server Response.

... And so on ......

Rtmp Data Type

0 × 01 chunk size changes the chunk size for packets

0 × 02 unknown anyone know this one?

0 × 03 bytes read send every X bytes read by both sides

0 × 04 Ping is a stream control message, has subtypes

0 × 05 server BW the servers downstream BW

0 × 06 client BW the clients upstream BW

0 × 07 unknown anyone know this one?

0 × 08 audio data packet containing audio

0 × 09 video data packet containing video data

0x0a-0 × 11 unknown anyone know?

0 × 12 running y an invoke which does not found CT a reply

0 × 13 shared object has subtypes

0 × 14 invoke like remoting call, used for stream actions too.

Shared Object Data Type

0 × 01 connect

0 × 02 disconnect

0 × 03 set attribute

0 × 04 update data

0 × 05 Update attribute

0 × 06 send message

0 × 07 status

0 × 08 clear data

0 × 09 delete data

0x0a Delete attribute

0x0b

Initial Data

Rtmp Package Structure

The rtmp package contains a fixed-length header and a package body up to 128 bytes. the header can be any of the following four lengths: 12, 8, 4, or 1 byte (s ).

The first two bits of the first byte are very important. It determines the header length. it can use the mask 0xc0 for "and" calculation. the following table lists the possible headers: bits header length.

00 12 bytes

01 8 bytes

10 4 bytes

11 1 byte

We will discuss the structure of the rtmp package here. we will discuss about AMF in the future (please wait...: loveliness :). In fact, the rtmp package structure uses the AMF format.

Stream

About stream operations we need to further study in the forum of http://www.openred5.com/bbs/viewthread.php? Tid = 175 & extra = Page % 3d1 this article is a good study. You can refer to it. However, the following describes the process of sending a stream from the client to the server:

Client → server: send a request to create a stream.

Server → client: returns an index number indicating the stream.

Client → server: Start sending.

Client → server: sends audio and video data packets (these packets are in the same channel and uniquely identified by the stream index number ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.