sip协议解析
文档第一部分介绍sip标准gb28181内容,第二部分介绍sip标准db33内容
SIP: SIP(Session Initiation Protocol)是一个应用层的信令控制协议。用于创建、修改和释放一个或多个参与者的会话。这些会话可以是Internet多媒体会议[3] 、IP电话或多媒体分发。会话的参与者可以通过组播(multicast)、网状单播(unicast)或两者的混合体进行通信。
Gb28181
Sip通信协议结构:
会话通道 |
媒体通道 |
|||||||
|
SDP |
MANSCDP |
MANSRTSP |
|
|
MPEG-4/H.264/SVAC |
G.711/G.72 3.1/G.729 |
|
|
SIP |
|
|
RTP/RTCP |
|
|||
TCP/UDP |
TCP/UDP |
|||||||
IP |
SIP消息应支持基于UDP和TCP的传输,视频流传输使用偶数端口,端口+1默认为为RTCP使用端口;
基本注册流程示意图:
注册流程描述如下:
1、 SIP代理向SIP服务器发送REGISTER请求;
2、 SIP服务器向SIP代理发送响应401,并在响应的消息头WWW-Authenticate字段中给出适合SIP代理的认证体制和参数;
3、 SIP代理重新向SIP服务器发送REGISTER请求,在请求的Authenticate字段给出信任书,包含认证信息;
4、 SIP服务器对请求进行验证,如果检查出SIP代理身份合法,向SIP代理发送成功响应200 OK,如果身份不合法则发送拒绝服务应答。
注册信令消息示范:
Register sip:SIP服务器编码@目的域名或者IP地址端口 SIP/2.0
Via:SIP/2.0/UDP 源域名或者IP地址端口
From:<sip:SIP设备编码@源域名>;tag=185328220
To: <sip:SIP设备编码@源域名>
Call-ID:ms1214-322164-681262131542511620107-0@172.18.16.3
CSeq:1 Register
Contact: <sip:SIP设备编码@源IP地址端口>
Max-Forwords:70
Expires: 7200
Content-Length: 0
SIP/2.0 401 Unauthorized
To: sip:SIP设备编码@源域名
Content-Length: 0
CSeq:1 Register
Call-ID:ms1214-322164-681262131542511620107-0@172.18.16.3
From:<sip:SIP设备编码@源域名>;tag=185328220
Via:SIP/2.0/UDP 源域名或者IP地址端口
WWW-Authenticate: Digest realm=”64010000”,nonce=”6fe9ba44a76be22a”
Register sip:SIP服务器编码@目的域名或者IP地址端口 SIP/2.0
Via:SIP/2.0/UDP 源域名或者IP地址端口
From:<sip:SIP设备编码@源域名>;tag=185328220
To: <sip:SIP设备编码@源域名>
Call-ID:ms1214-322164-681262131542511620107-0@172.18.16.3
CSeq:2 Register
Contact: <sip:SIP设备编码@源IP地址端口>
Authorization: Digest username=”64010000002020000001”,realm=”64010000”, nonce=”6fe9ba44a76be22a”,uri=”sip:64010000002020000001@172.18.16.5:5060”,response=”9625d92d1bddea7a911926e0db054968”,algorithm=”MD5”
Max-Forwords:70
Expires: 7200
Content-Length: 0
SIP/2.0 200 OK
To: <sip:SIP设备编码@源域名>;tag=69113a2a
Contact: sip:SIP设备编码@源IP地址端口
Content-Length: 0
CSeq:2 Register
Call-ID:ms1214-322164-681262131542511620107-0@172.18.16.3
From:<sip:SIP设备编码@源域名>;tag=185328220
Via:SIP/2.0/UDP 源域名或者IP地址端口
Date:2010-11-02T15:01:26.115
Expires: 7200
客户端主动发起的实时视音频点播流程示意图:
客户端发起的实时点播消息示范:(请求视频信令与断开视频信息 和 回播基本无差别)
1、请求视频流
INVITE sip:00000000001310018021@192.168.40.66:7100 SIP/2.0
Via: SIP/2.0/UDP 192.168.40.55:7100;rport;branch=z9hG4bK2480933505
From: <sip:120105110228023020@192.168.40.55:7100>;tag=2249831759
To: <sip:00000000001310018021@192.168.40.66:7100>
Call-ID: 821763613 // Call-ID:使用该字段标识一路视频
CSeq: 20 INVITE
Contact: <sip:120105110228023020@192.168.40.55:7100>
Content-Type: Application/SDP
Max-Forwards: 70
User-Agent: NCG V2.6.0.299938
Subject: 00000000001310018021:0,120105110228023020:0
Content-Length: 239
v=0
o=00000000001310018021 0 0 IN IP4 192.168.40.55
s=Play //Play标识为点播请求 Playback标识回播请求
c=IN IP4 192.168.40.55 //192.168.40.55:音视频流目的地址
t=0 0 //t行第一参数为视频开始时间 第二个参数为结束时间 t = 0 0表示实时视音频点播
m=video 5552 RTP/AVP 96 97 98 //video:表示请求视频流 audio:表示请求音频流 5522:音视频流目的端口 RTP/AVP:视频流使用协议 96 97 98:客户端支持码流格式
a=rtpmap:96 PS/90000
a=rtpmap:97 MPEG4/90000
a=rtpmap:98 H264/90000
a=recvonly
a=streamMode:MAIN
y=0999999999
SIP/2.0 100 Trying
Via: SIP/2.0/UDP 192.168.40.55:7100;rport=7100;branch=z9hG4bK2480933505
From: <sip:120105110228023020@192.168.40.55:7100>;tag=2249831759
To: <sip:00000000001310018021@192.168.40.66:7100>
Call-ID: 821763613
CSeq: 20 INVITE
User-Agent: NCG V2.6.0.299938
Content-Length: 0
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.40.55:7100;rport=7100;branch=z9hG4bK2480933505
From: <sip:120105110228023020@192.168.40.55:7100>;tag=2249831759
To: <sip:00000000001310018021@192.168.40.66:7100>;tag=2885333649
Call-ID: 821763613
CSeq: 20 INVITE
Contact: <sip:161128022553273720@192.168.40.66:7100>
Content-Type: Application/SDP
User-Agent: NCG V2.6.0.299938
Content-Length: 165
v=0
o=00000000001310018021 0 0 IN IP4 192.168.40.55
s=Play //Play标识为点播请求 Playback标识回播请求
c=IN IP4 192.168.40.66 //192.168.40.66:音视频流源地址 注:视音频源与目的地址不局限于级联服务器本身
t=0 0
m=video 5268 RTP/AVP 96 // video:表示请求视频流 audio:表示请求音频流 5268:音视频流源端口 RTP/AVP:视频流使用协议 96:服务端所选择的码流格式 音视频所使用端口统一使用偶数端口 port+1为rtcp端口
a=rtpmap:96 PS/90000
a=sendonly
y=0100005268
视频流截包:
2、断开视频流
BYE sip:161128022553273720@192.168.40.66:7100 SIP/2.0
Via: SIP/2.0/UDP 192.168.40.55:7100;rport;branch=z9hG4bK1981844232
From: <sip:120105110228023020@192.168.40.55:7100>;tag=2249831759
To: <sip:00000000001310018021@192.168.40.66:7100>;tag=2885333649
Call-ID: 821763613 // Call-ID:该字段判断请求端口哪一路视频
CSeq: 21 BYE
Contact: <sip:120105110228023020@192.168.40.55:7100>
Max-Forwards: 70
User-Agent: NCG V2.6.0.299938
Content-Length: 0
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.40.55:7100;rport=7100;branch=z9hG4bK1981844232
From: <sip:120105110228023020@192.168.40.55:7100>;tag=2249831759
To: <sip:00000000001310018021@192.168.40.66:7100>;tag=2885333649
Call-ID: 821763613
CSeq: 21 BYE
User-Agent: NCG V2.6.0.299938
Content-Length: 0
客户端主动发起的实时视音频回播流程示意图:
客户端发起的回播控制消息示范:(请求视频信令与断开视频信息 和 回播基本无差别 Play/Playback)
Gb28181标准中,明确说明历史视音频的回复控制命令应采用监控报警联网系统实时流协议(MANSRTSP),并采用SIP消息INFO消息体携带传输。(GBT 28181-2016 4.3.5)
INFO sip:161128022553273720@192.168.40.66:7100 SIP/2.0
Via: SIP/2.0/UDP 192.168.40.55:7100;rport;branch=z9hG4bK1764044774
From: <sip:120105110228023020@192.168.40.55:7100>;tag=4105413651
To: <sip:00000000001310018021@192.168.40.66:7100>;tag=4232579586
Call-ID: 2259139548
CSeq: 21 INFO
Contact: <sip:120105110228023020@192.168.40.55:7100>
Content-Type: Application/MANSRTSP
Max-Forwards: 70
User-Agent: NCG V2.6.0.299938
Content-Length: 49
PLAY MANSRTSP/1.0
CSeq: 59367
Scale: 0.000000 // Scale:回播速度控制 例:Scale: 8.000000 快进x8
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.40.55:7100;rport=7100;branch=z9hG4bK1764044774
From: <sip:120105110228023020@192.168.40.55:7100>;tag=4105413651
To: <sip:00000000001310018021@192.168.40.66:7100>;tag=4232579586
Call-ID: 2259139548
CSeq: 21 INFO
Contact: <sip:161128022553273720@192.168.40.66:7100>
User-Agent: NCG V2.6.0.299938
Content-Length: 0
控制消息示例并说明:
SIP消息使用MESSAGE消息体携带控制命令;message消息头Content-Type头域为Content-Type: Application/MANSCDP+xml。
设备控制命令采用MANSCDP协议格式定义,设备在收到message消息后,应立即返回应答,应答均无消息体。
MESSAGE sip:120105110228023020@192.168.40.55:7100 SIP/2.0
Via: SIP/2.0/UDP 192.168.40.66:7100;rport;branch=z9hG4bK3098087079
From: <sip:161128022553273720@192.168.40.66:7100>;tag=10660660
To: <sip:120105110228023020@192.168.40.55:7100>
Call-ID: 3949551065
CSeq: 20 MESSAGE
Content-Type: Application/MANSCDP+xml
Max-Forwards: 70
User-Agent: NCG V2.6.0.299938
Content-Length: 151
<?xml version="1.0"?>
<Notify>
<CmdType>Keepalive</CmdType>
<SN>14885</SN>
<DeviceID>161128022553273720</DeviceID>
<Status>OK</Status>
</Notify>
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.40.66:7100;rport=7100;branch=z9hG4bK3098087079
From: <sip:161128022553273720@192.168.40.66:7100>;tag=10660660
To: <sip:120105110228023020@192.168.40.55:7100>;tag=1450434041
Call-ID: 3949551065
CSeq: 20 MESSAGE
User-Agent: NCG V2.6.0.299938
Content-Length: 0
命令类型整理:(基础示例)
命令类型(CmdType)
命令序列号(SN)
设备编码(DeviceID)、
执行结果(Result)、
经度(Longitude)
纬度(Latitude)
厂家信息(Manufacturer)
设备型号(Model)
固件版本(Firm-ware)
最大支持摄像机个数(Channel)
Db33
实时监控图像获取示意图:
请求视频流:
INVITE sip:00000000001310018021@192.168.40.66:7100 SIP/2.0
Via: SIP/2.0/UDP 192.168.40.55:7100;rport;branch=z9hG4bK178329191
From: <sip:120105110228023020@192.168.40.55:7100>;tag=2091251200
To: <sip:00000000001310018021@192.168.40.66:7100>
Call-ID: 4130842379
CSeq: 20 INVITE
Contact: <sip:120105110228023020@192.168.40.55:7100>
Content-Type: Application/DDCP
Max-Forwards: 70
User-Agent: NCG V2.6.0.299938
Subject: 00000000001310018021:0,120105110228023020:0
Content-Length: 297
<?xml version="1.0"?>
<Action>
<Variable>RealMedia</Variable>
<Privilege/>
<Format>1080 720 D1 HALFD1 CIF 2CIF 4CIF QCIF</Format> //码流格式
<Video>MPEG-2 MPEG-4 M-JPEG H.264</Video> //视频编码类型
<Audio>G.711A G.711U G.722</Audio> //音频编码类型
<MaxBitrate>20480</MaxBitrate> //传输码率
<Socket>192.168.40.55 UDP 5560</Socket> //表示使用设备192.168.40.55的UDP端口5560接收视频流
</Action>
SIP/2.0 100 Trying
Via: SIP/2.0/UDP 192.168.40.55:7100;rport=7100;branch=z9hG4bK178329191
From: <sip:120105110228023020@192.168.40.55:7100>;tag=2091251200
To: <sip:00000000001310018021@192.168.40.66:7100>
Call-ID: 4130842379
CSeq: 20 INVITE
User-Agent: NCG V2.6.0.299938
Content-Length: 0
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.40.55:7100;rport=7100;branch=z9hG4bK178329191
From: <sip:120105110228023020@192.168.40.55:7100>;tag=2091251200
To: <sip:00000000001310018021@192.168.40.66:7100>;tag=2781666162
Call-ID: 4130842379
CSeq: 20 INVITE
Contact: <sip:161128022553273720@192.168.40.66:7100>
Content-Type: Application/DDCP
User-Agent: NCG V2.6.0.299938
Content-Length: 247
<?xml version="1.0"?>
<Response>
<Variable>RealMedia</Variable>
<Format>CIF</Format>
<Video>H.264</Video>
<Audio>G.711U</Audio>
<Bitrate>512</Bitrate> //传输码率
<Socket>192.168.40.66 UDP 5272</Socket> //表示使用设备192.168.40.66的UDP端口5272发送视频流
<DecoderTag>hikvision</DecoderTag>
</Response>
视频流截包:
断开视频流:
BYE sip:161128022553273720@192.168.40.66:7100 SIP/2.0
Via: SIP/2.0/UDP 192.168.40.55:7100;rport;branch=z9hG4bK2697700230
From: <sip:120105110228023020@192.168.40.55:7100>;tag=2091251200
To: <sip:00000000001310018021@192.168.40.66:7100>;tag=2781666162
Call-ID: 4130842379
CSeq: 21 BYE
Contact: <sip:120105110228023020@192.168.40.55:7100>
Max-Forwards: 70
User-Agent: NCG V2.6.0.299938
Content-Length: 0
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.40.55:7100;rport=7100;branch=z9hG4bK2697700230
From: <sip:120105110228023020@192.168.40.55:7100>;tag=2091251200
To: <sip:00000000001310018021@192.168.40.66:7100>;tag=2781666162
Call-ID: 4130842379
CSeq: 21 BYE
User-Agent: NCG V2.6.0.299938
Content-Length: 0
历史图像获取示意图:
请求视频流:
DO sip:00000000001310018021@192.168.40.66:7100 SIP/2.0
Via: SIP/2.0/UDP 192.168.40.55:7100;rport;branch=z9hG4bK2258689616
From: <sip:120105110228023020@192.168.40.55:7100>;tag=382563044
To: <sip:00000000001310018021@192.168.40.66:7100>
Call-ID: 2435803271
CSeq: 20 DO
Content-Type: Application/DDCP
Max-Forwards: 70
User-Agent: NCG V2.6.0.299938
Content-Length: 295
<?xml version="1.0"?>
<Action>
<Query>
<Variable>FileList</Variable>
<Privilege></Privilege>
<FileType>23</FileType>
<FromIndex>1</FromIndex>
<ToIndex>2</ToIndex>
<BeginTime>20170621T000000Z</BeginTime>
<EndTime>20170621T235959Z</EndTime>
<RecordPos>1</RecordPos>
</Query>
</Action>
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.40.55:7100;rport=7100;branch=z9hG4bK2258689616
From: <sip:120105110228023020@192.168.40.55:7100>;tag=382563044
To: <sip:00000000001310018021@192.168.40.66:7100>;tag=762901480
Call-ID: 2435803271
CSeq: 20 DO
Content-Type: Application/DDCP
User-Agent: NCG V2.6.0.299938
Content-Length: 875
<?xml version="1.0"?>
<Response>
<QueryResponse>
<Variable>FileList</Variable>
<Result>0</Result>
<RealFileNum>2</RealFileNum>
<FromIndex>1</FromIndex>
<ToIndex>2</ToIndex>
<FileInfolist>
<Item>
<Name>0_1_1497973658_0.h264</Name>
<BeginTime>20170621T000000Z</BeginTime>
<EndTime>20170621T022702Z</EndTime>
<FileSize>4439165188</FileSize>
<RecordType>1</RecordType>
<VodUrl>rtsp://192.168.40.66:6304/file/vag_plug://192.168.40.66:6300/0/sessions/28e8deaef7a64db88010fee38da0ba31</VodUrl>
</Item>
<Item>
<Name>0_1_1497983224_0.h264</Name>
<BeginTime>20170621T022704Z</BeginTime>
<EndTime>20170621T155926Z</EndTime>
<FileSize>24528928360</FileSize>
<RecordType>1</RecordType>
<VodUrl>rtsp://192.168.40.66:6304/file/vag_plug://192.168.40.66:6300/0/sessions/28e8deaef7a64db88010fee38da0ba31</VodUrl>
</Item>
</FileInfolist>
</QueryResponse>
</Response>
DO sip:00000000001310018021@192.168.40.66:7100 SIP/2.0
Via: SIP/2.0/UDP 192.168.40.55:7100;rport;branch=z9hG4bK929522417
From: <sip:120105110228023020@192.168.40.55:7100>;tag=3721737815
To: <sip:00000000001310018021@192.168.40.66:7100>
Call-ID: 55014328
CSeq: 20 DO
Content-Type: Application/DDCP
Max-Forwards: 70
User-Agent: NCG V2.6.0.299938
Content-Length: 308
<?xml version="1.0"?>
<Action>
<Variable>VODByRTSP</Variable>
<Privilege></Privilege>
<FileType>23</FileType>
<Name>0_1_1497973658_0.h264</Name>
<BeginTime>20170621T000000Z</BeginTime>
<EndTime>20170621T155926Z</EndTime>
<MaxBitrate>1024</MaxBitrate>
<RecordPos>1</RecordPos>
<TV>2</TV>
</Action>
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.40.55:7100;rport=7100;branch=z9hG4bK929522417
From: <sip:120105110228023020@192.168.40.55:7100>;tag=3721737815
To: <sip:00000000001310018021@192.168.40.66:7100>;tag=3556564953
Call-ID: 55014328
CSeq: 20 DO
Content-Type: Application/DDCP
User-Agent: NCG V2.6.0.299938
Content-Length: 494
<?xml version="1.0"?>
<Response>
<Variable>VODByRTSP</Variable>
<Result>0</Result>
<Bitrate>0</Bitrate>
<Playurl>rtsp://192.168.40.66:7087/ncg00/rtsp://192.168.40.66:6304/file/vag_plug://192.168.40.66:6300/0/sessions/28e8deaef7a64db88010fee38da0ba31?ncgMode=1&devType=10001&ks=1497974400&js=1498031966&usrName=admin&passWord=jdwa2003</Playurl>
<UsrName>admin</UsrName>
<PassWord>jdwa2003</PassWord>
<TV>2</TV>
<CamIndex>00000000001310018021</CamIndex>
</Response>
Record-Route说明:
From: 如果一个SIP消息中没有Contact或者Record-Route头域,那么callee就会根据From头域产生后续的Request。比如:如果 Alice打一个电话给Bob,From头域的内容是 From:Alice<sip:alice@example.org>。那么Bob打给Alice时就会使用 sip:alice@example.org作为To头域和Request-URI头域的内容。
Contact: 后续Request将根据Contact头域的内容决定目的地的地址,同时将Contact头域的内容放到Request-URI中。它还可以用来指示没 有在Record-Route头域中记录的Proxies的地址。同时它还可以被用在Redirect servers和REGISTER requests 和responses。
Route:Route和Via字段正好相反,它被用来作为Request消息的路由。当一个Proxy Server收到一个Request消息时,会检查Route字段的第一个地址是否等于自己,如果是,它将从Route字段中删去自己的地址信息,并将消 息转发到Route字段中指定的下个地址;如果Route字段为空,则转发到Request URI指定的地址。
Record-Route/Route: Record-Route头域一般是被proxies插入到request中的,这样后续的Request如何有着和前面一样的call-id就会被路由 到这些proxies。它也会被User Agent作为发送后续request的依据。这套机制很像source-route,Record-Route头域的信息被复制到Route头域中。并 且Request-URI头域会被设置为第一个Route头域的内容。
Record-Route字段实际上用于帮助UA建立Route Set,当UA发送Request时会用Route Set来设置上面提到的Route字段。当一个Request消息经过Proxy Server时,如果该Proxy Server希望通知UA相关的后续消息都能通过它来转发,此时它就会在消息中添加Record-Route字段,内容为自己的地址信息。当UAS发送 Resposne消息时它将复制Request中的Record-Route字段,而UAC在Response消息中检测到Record-Route字段 时,它就会用该字段的路由信息更新自己的Route Set。
Via: Via头域是被服务器插入request中,用来检查路由环的,并且可以使response根据via找到返回的路。它不会对未来的request 或者是response造成影响。
当UAC发起一个SIP Request消息时,消息经过的每一跳(包含发起方)都会在SIP消息中增加一个Via字段,内容为自己的地址信息,表示此消息通过此地址发往下一跳。为什么要增加Via字段来记录Request消息经过的地址呢?实际上这个地址信息将被作为Request消息的Response消息的路由,Response将根据Via字段中记录的地址逐级返回,直到回到Request的发起方。
总的来说,如果有Route,request就应该根据Route发送,如果没有就根据Contact头域发送,如果连Contact都没有,就根据From头域发送。