跳到主要内容

9. SDP 语法 (SDP Grammar)

本节给出 SDP 的扩展巴科斯-诺尔范式 (Augmented Backus-Naur Form, ABNF) 语法. ABNF 定义见 [RFC5234].

下面的语法以 announcement 作为完整 SDP session description 的入口规则. 该入口规则按照 SDP 的固定字段顺序组合 protocol version, origin, session name, optional information, URI, email 和 phone 字段, connection 和 bandwidth 信息, timing 信息, key 字段, attributes 以及零个或多个 media descriptions. 代码块中的规则名, 字段字面量和十六进制字符值是协议定义的一部分, 必须保持原样.

语法还定义了各字段使用的通用结构, 包括 username, session id, network type, address type, unicast address, email address, phone number, bandwidth type, typed time, key data, attributes, media name, port, protocol 和 format 等. 这些规则约束 SDP 文本的可解析形式, 因此实现解析或生成 SDP 时应按这些规则处理空白, 换行, token 字符范围以及 IP address, FQDN 和 URI-reference 等底层类型.

; SDP Syntax

announcement = proto-version
origin-field
session-name-field
information-field
uri-field
email-fields
phone-fields
connection-field
bandwidth-fields
time-fields
key-field
attribute-fields
media-descriptions

proto-version = %x76 "=" 1*DIGIT CRLF
; Protocol version

origin-field = %x6f "=" username SP sess-id SP sess-version SP
nettype SP addrtype SP unicast-address CRLF

session-name-field = %x73 "=" text CRLF

information-field = [%x69 "=" text CRLF]

uri-field = [%x75 "=" uri CRLF]

email-fields = *(%x65 "=" email-address CRLF)

phone-fields = *(%x70 "=" phone-number CRLF)

connection-field = [%x63 "=" nettype SP addrtype SP
connection-address CRLF]

bandwidth-fields = *(%x62 "=" bwtype ":" bandwidth CRLF)

time-fields = 1*( %x74 "=" start-time SP stop-time
*(CRLF repeat-fields) CRLF)
[zone-adjustments CRLF]

repeat-fields = %x72 "=" repeat-interval SP typed-time
1*(SP typed-time)

zone-adjustments = %x7a "=" time SP ["-"] typed-time
*(SP time SP ["-"] typed-time)

key-field = [%x6b "=" key-data CRLF]

attribute-fields = *(%x61 "=" attribute CRLF)

media-descriptions = *( media-field
information-field
*connection-field
bandwidth-fields
key-field
attribute-fields )

media-field = %x6d "=" media SP port ["/" integer]
SP proto 1*(SP fmt) CRLF

; Generic rules

username = non-ws-string

sess-id = 1*DIGIT

sess-version = 1*DIGIT

nettype = token

addrtype = token

unicast-address = IP4-address / IP6-address / FQDN / extn-addr

IP4-address = b1 3("." decimal-uchar)

b1 = decimal-uchar

IP6-address = hexpart [ ":" IP4-address ]

FQDN = 4*(alpha-numeric / "-" / ".")

email-address = email / email "(" email-safe ")" /
email-safe "<" email ">"

phone-number = phone / phone "(" email-safe ")" /
email-safe "<" phone ">"

connection-address = multicast-address / unicast-address

bwtype = token

bandwidth = 1*DIGIT

start-time = time / "0"

stop-time = time / "0"

time = POS-DIGIT 9*DIGIT

repeat-interval = typed-time

typed-time = 1*DIGIT [fixed-len-time-unit]

fixed-len-time-unit = %x64 / %x68 / %x6d / %x73

key-data = key-type / key-type ":" key-material

key-type = %x63 %x6c %x65 %x61 %x72 /
%x62 %x61 %x73 %x65 "64" /
%x75 %x72 %x69 /
%x70 %x72 %x6f %x6d %x70 %x74

attribute = att-field ":" att-value / att-field

att-field = token

att-value = byte-string

media = token

port = 1*DIGIT

proto = token 0*("/" token)

fmt = token

; Generic primitive rules

token = 1*(token-char)

token-char = %x21 / %x23-27 / %x2A-2B / %x2D-2E / %x30-39
/ %x41-5A / %x5E-7E

byte-string = 1*(0x01..0x09 / 0x0B..0x0C / 0x0E..0xFF)

non-ws-string = 1*(VCHAR / 0x80..0xFF)

text = byte-string

email = email-safe "@" email-safe

email-safe = byte-string

phone = "+" POS-DIGIT 1*(SP / "-" / DIGIT)

uri = URI-reference

decimal-uchar = DIGIT
/ POS-DIGIT DIGIT
/ ("1" 2*(DIGIT))
/ ("2" ("0"/"1"/"2"/"3"/"4") DIGIT)
/ ("2" "5" ("0"/"1"/"2"/"3"/"4"/"5"))

alpha-numeric = ALPHA / DIGIT

POS-DIGIT = %x31-39

hexpart = hexseq / hexseq "::" [ hexseq ] / "::" [ hexseq ]

hexseq = hex4 *( ":" hex4)

hex4 = 1*4HEXDIG

extn-addr = non-ws-string

该语法定义了 SDP 会话描述的完整语法. 实现 MUST 符合此语法.

实现者需要注意, 此 ABNF 描述的是 SDP wire/text format 的语法约束, 不是面向用户的显示格式. 对 v=, o=, s=, i=, u=, e=, p=, c=, b=, t=, r=, z=, k=, a=m= 等字段标识符的解释仍由本文前面各节定义. 扩展字段和属性在不破坏此语法的前提下通过 token, attribute 和 extension address 等规则承载.