·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]RTMP协议的分析与研究(2)

时间:2012-03-15 09:26CuPlayer.com
RTMP 数据类型: 001 Chunk Size changes the chunk size for packets 002 Unknown anyone know this one? 003 Bytes Read send every x bytes read by both sides 004 Ping ping is a stream control message, has

RTMP 数据类型:

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
0x0A - 0×11 Unknown anyone know?
0×12 Notify an invoke which does not expect a reply
0×13 Shared Object has subtypes
0×14 Invoke like remoting call, used for stream actions too.

  3.3 协议的净核
  RTMP 的协议净核是用 AMF 格式来描述, AMF 格式本身的产生就是为了 RTMP 协议服务的,最初的 RTMP 采用 XML 的形式传输数据,但 XML 只是字符形式的值对的格式传输数据,而随着应用的普及这完全不能满足要求了,比如对象、结构、数组,甚至可以是数据集,配合 DataGrid 组件可以很方便地显示数据。
为了处理复杂数据类型,采用一种独有的方式使 Flash 与应用服务器间可以来回传送数据势在必行。于是 AMF 应运而生。

  AMF 是 Adobe 独家开发出来的通信协议,它采用二进制压缩,序列化、反序列化、传输数据,从而为 Flash 播放器与 Flash Remoting 网关通信提供了一种轻量级的、高效能的通信方式。如下图所示。

  AMF 最大的特色在于可直接将 Flash 内置对象,例如 Object, Array, Date, XML ,传回服务器端,并且在服务器端自动进行解析成适当的对象,这就减轻了开发人员繁复工作,同时也更省了开发时间。由于 AMF 采用二进制编码,这种方式可以高度压缩数据,因此非常适合用 来传递大量的资料。数据量越大, Flash Remoting 的传输效能就越高,远远超过 Web Service 。至于 XML, LoadVars 和 loadVariables() ,它们使用纯文本的传输方式,效能就更不能与 Flash Remoting 相提并论了。
注意: Flash Remoting 需要浏览器支持 Binary POST , Flash 播放器在 Netscape 6.x. 环境下运行 Flash Remoting 会不起作用( Flash Remoting 调用没有效果也不返回错误), Netscape 7 已经纠正了这个 bug 。对于早期 Safari 和 Chimera 版的苹果机也有这个问题。
同样是轻量级数据交换协议,同样是通过调用远程服务,同样是基于标准的 HTTP 和 HTTPS 协议, Flash Remoting 为什么选择了使用 AMF 而放弃了 SOAP 与 Flash 播放器通信呢 有如下原因:

  SOAP 将数据处理成 XML 格式,相对于二进制的 AFM 太冗长了;
  AMF 能更有效序列化数据;因为 AMF 的初衷只是为了支持 Flash ActionScript 的数据类型,而 SOAP 却致力于提供更广泛的用途;
  AMF 支持 Flash 播放器 6 只需要浏览器增加 4 KB 左右(压缩后)的大小,而 SOAP 就大多了;
  SOAP 的一些头部文件请求在 Flash 播放器 6 不支持。那 Flash 播放器 6 为什么能访问基于 SOAP 的 Web 服务呢?原来 Flash Remoting 网关将 SOAP 请求在服务器端与转换成 AFM 格式,然后利用 AFM 与 Flash 播放器通信。另外, AMF 包中包含 onResult 事件(比如说 response 事件)和 onStatus 事件(比如说 error 事件),这些事件对象在 Flash 中可以直接使用。
AMF 从 Flash MX 时代的 AMF0 发展到现在的 AMF3 。 AMF3 用作 Flash Playe 9 的 ActionScript 3.0 的默认序列化格式,而 AMF0 则用作旧版的 ActionScript 1.0 和 2.0 的序列化格式。 在网络传输数据方面, AMF3 比 AMF0 更有效率。 AMF3 能将 int 和 uint 对象作为整数( integer )传输,并且能序列化 ActionScript 3.0 才支持的数据类型 , 比如 ByteArray , XML 和 Iexternalizable 。

  AMF 很好的解决了内容的丰富性。(具体 AMF 格式参考附件格式文档)

  3.3.1 AMF中的数据类型Data Types
  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 many 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 many 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.

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器