Real Time Messaging Protocol - Packet Structure

Packet Structure

Packets are sent over a TCP connection which are established first between client and server. They contain a header and a body which, in the case of connection and control commands, is encoded using the Action Message Format (AMF). The header is split into the Basic Header (shown as detached from the rest, in the diagramme) and Chunk Message Header. The Basic Header is the only constant part of the packet and is usually composed of a single composite byte, where the 2 most significant bits are the Chunk Type (fmt in the specification) and the rest form the Stream ID. Depending on the value of the former, some fields of the Message Header can be omitted and their value derived from previous packets while depending on the value of the latter, the Basic Header can be extended with 2 extra bytes (as in the case of the diagramme that has 3 bytes in total). The Chunk Message Header contains meta-data information such as the message size (measured in bytes), the Timestamp Delta and Message Type. This last value is a single byte and defines whether the packet is an audio, video, command or "low level" RTMP packet such as an RTMP Ping.

An example is shown below as captured when a flash client executes the following code:

var stream:NetStream = new NetStream(connectionObject);

this will generate the following Chunk:

Hex Code ASCII
03 00 0b 68 00 00 19 14 00 00 00 00 02 00 0C 63 72 65 61 74 65 53 74 72 65 61 6D 00 40 00 00 00 00 00 00 00 05 . . @ I . . . . . . . . . . . . c r e a t e S t r e a m . @ . . . . . . . .

The packet starts with a Basic Header of a single byte (0x03) where the 2 most significant bits (b00000011) define a chunk header type of 0 while the rest (b00000011) define a Chunk Stream ID of 3. The 4 possible values of the header type and their significance are:

  • b00 = 12 byte header (full header).
  • b01 = 8 bytes - like type b00. not including message ID (4 last bytes).
  • b10 = 4 bytes - Basic Header and timestamp (3 bytes) are included.
  • b11 = 1 byte - only the Basic Header is included.

The last type (b11) is always used in the case of aggregate messages where, in the example above, the second message will start with an id of 0xC3 (b11000011) and would mean that all Message Header fields should be derived from the message with a stream Id of 3 (which would be the message right above it). The 6 least significant bits that form the Stream ID can take values between 3 and 65599. Some values have special meaning like 1 that stands for an extended ID format, in which case there will be 2 bytes following that. A value of 2 is for low level messages such as Ping and Set Client Bandwidth.

The next bytes of the RTMP Header (including the values in the example packet above) are decoded as follows:

  • byte #1 (0x03) = Chunk Header Type.
  • byte #2-4 (0x000b68) = Timestamp delta.
  • byte #5-7 (0x000019) = Packet Length - in this case it is 0x000019 = 25 bytes.
  • byte #8 (0x14) = Message Type ID - 0x14 (20) defines an AMF0 encoded command message.
  • byte #9-12 (0x00000000) = Message Stream ID. This (strangely) is in little-endian order

The Message Type ID byte defines whether the packet contains audio/video data, a remote object or a command. Some possible values are for are:

  • 0x01 = Set Packet Size Message.
  • 0x04 = Ping Message.
  • 0x05 = Server Bandwidth
  • 0x06 = Client Bandwidth.
  • 0x08 = Audio Packet.
  • 0x09 = Video Packet.
  • 0x11 = An AMF3 type command.
  • 0x12 = Invoke (onMetaData info is sent as such).
  • 0x14 = An AMF0 type command.

Following the header, 0x02 denotes a string of size 0x000C and values 0x63 0x72 ... 0x6D ("createStream" command). Following that we have a 0x00 (number) which is the transaction id of value 2.0. The last byte is 0x05 (null) which means there are no arguments.

Read more about this topic:  Real Time Messaging Protocol

Famous quotes containing the words packet and/or structure:

    we know our end
    A packet of worm-seed, a garden of spent tissues.
    Allen Tate (1899–1979)

    The structure was designed by an old sea captain who believed that the world would end in a flood. He built a home in the traditional shape of the Ark, inverted, with the roof forming the hull of the proposed vessel. The builder expected that the deluge would cause the house to topple and then reverse itself, floating away on its roof until it should land on some new Ararat.
    —For the State of New Jersey, U.S. public relief program (1935-1943)