GB28181协议指的是国家标准GB/T 28181—2016《公共安全视频监控联网系统信息传输、交换、控制技术要求》1,该标准规定了公共安全视频监控联网系统的互联结构, 传输、交换、控制的基本要求和安全性要求, 以及控制、传输流程和协议接口等技术要求,是视频监控领域的国家标准。GB28181协议信令层面使用的是SIP(Session Initiation Protocol)协议2,流媒体传输层面使用的是实时传输协议(Real-time Transport Protocol,RTP)协议3,因此可以理解为GB28181是在国际通用标准的基础之上进行了私有化定制以满足视频监控联网系统互联传输的标准化需求。本文旨在说明在FFmpeg中增加对GB28181协议的支持,使其可以与支持GB28181协议的设备进行通信与控制,实现设备的注册、保活以及流媒体的传输。
GB28181协议会话通道实际上使用的是SIP协议,并且在SIP协议的基础之上做了些私有化处理。SIP是一个由IETF MMUSIC工作组开发的协议,作为标准被提议用于创建,修改和终止包括视频,语音,即时通信,在线游戏和虚拟现实等多种多媒体元素在内的交互式用户会话。SIP中一个比较重要的概念是用户代理(User Agent),指的是一个SIP逻辑网络端点,用于创建、发送、接收SIP消息并管理一个SIP会话。SIP用户代理又可分为用户代理客户端UAC(User Agent Client)和用户代理服务端UAS(User Agent Server)。UAC创建并发送SIP请求,UAS接收处理SIP请求,发送SIP响应。SIP协议会与许多其它的协议协同工作,如SIP报文内容发送会话描述协议(Session Description Protocol,SDP)4,SDP协议描述了会话所使用流媒体细节,如:使用哪个IP端口,采用哪种编解码器等等。SIP的一个典型用途是:SIP会话传输一些简单的经过报文的实时传输协议流,RTP本身才是语音或视频的载体。在GB28181协议中,联网系统在进行视音频传输及控制时应建立两个传输通道: 会话通道和媒体流通道。会话通道用于在设备之间建立会话并传输系统控制命令; 媒体流通道用于传输视音频数据, 经过压缩编码的视音频流采用流媒体协议RTP/RTCP传输
图例:
GB28181文档地址:
http://openstd.samr.gov.cn/bzgk/gb/newGbInfo?hcno=469659DC56B9B8187671FF08748CEC89
GB28181相关项目:
https://gitee.com/pan648540858/wvp-GB28181-pro
https://gitee.com/qinqi/JNVS
https://github.com/GB28181/GB28181.Solution
https://gitee.com/chatop2020/AKStream?_from=gitee_search
https://github.com/ZLMediaKit/ZLMediaKit
https://gitee.com/kkkkk5G/MediaServerUI
关于sip协议,这里使用sip通信报文配上注释来说明会更加详细。
1.注册文本摄像头 Sip
//摄像头发起的第一次注册信息
REGISTER sip:34020000002000000002@192.168.6.14:5060 SIP/2.0
Via: SIP/2.0/UDP 192.168.9.200:5060;rport;branch=z9hG4bK1945009904
Route: <sip:34020000001110000011@192.168.6.14:5060;lr>
From: <sip:34020000001110000011@192.168.9.200:5060>;tag=321443940 //在这里我们可以拿到设备的id
To: <sip:34020000001110000011@192.168.9.200:5060>
Call-ID: 214776626@192.168.9.200 //这个是会话的唯一标识,回复时一定要带这个,要不然报错
CSeq: 1 REGISTER //这里代表第一次注册,按照国际惯例是先回复401请求校验信息,回复信息也要携带
Contact: <sip:34020000001110000011@192.168.9.200:5060>
Max-Forwards: 70
User-Agent: Dahua SIP UAS V1.0 //厂商信息
Expires: 3600 //注册有效时间,不为代表注册,为0时代表注销
Content-Length: 0 //请求体长度为0,说明下面没有数据了,没有携带消息体
//服务器回复401校验
SIP/2.0 401 Unauthorized
CSeq: 1 REGISTER //回复第一次注册
Call-ID: 214776626@192.168.9.200 //会话id
From: <sip:34020000001110000011@192.168.9.200:5060>;tag=793568880
To: <sip:34020000001110000011@192.168.9.200:5060>
Via: SIP/2.0/UDP 192.168.9.200:5060;rport;branch=z9hG4bK45766126 //这个字段直接拷贝上面的来回复
WWW-Authenticate: Digest realm="3402000000",nonce="962535b552b6e29883ff988c0065ddc2" //这里表明验证参数,nonce这里是Call-ID+设备id的md5 ,这个值只是提供校验的随机数,对于怎么进行校验,下面代码详解
Content-Length: 0
//摄像头第二次注册
REGISTER sip:34020000002000000002@192.168.6.14:5060 SIP/2.0 //留意该文本,按照空格切割,uri为第二个字符串,第一个是方法,第三个是请求版本号,类似HTTP/1.1
Via: SIP/2.0/UDP 192.168.9.200:5060;rport;branch=z9hG4bK1647127273
Route: <sip:34020000001110000011@192.168.6.14:5060;lr>
From: <sip:34020000001110000011@192.168.9.200:5060>;tag=793568880
To: <sip:34020000001110000011@192.168.9.200:5060>
Call-ID: 214776626@192.168.9.200
CSeq: 2 REGISTER //第二次注册
Contact: <sip:34020000001110000011@192.168.9.200:5060>
Authorization: Digest username="34020000001110000011", realm="3402000000", nonce="962535b552b6e29883ff988c0065ddc2", uri="sip:34020000002000000002@192.168.6.14:5060", response="77ed0f6d00ab1e0c1a871dd5e03edb65", algorithm=MD5 //加密算法 以及检验的参数
Max-Forwards: 70
User-Agent: Dahua SIP UAS V1.0
Expires: 3600
Content-Length: 0
//第二次校验成功的话直接响应200 之后摄像头会发起心跳保活
SIP/2.0 200 OK
CSeq: 2 REGISTER //回复第二次注册
Call-ID: 214776626@192.168.9.200 //唯一会话id
From: <sip:34020000001110000011@192.168.9.200:5060>;tag=793568880
To: <sip:34020000001110000011@192.168.9.200:5060>
Via: SIP/2.0/UDP 192.168.9.200:5060;rport;branch=z9hG4bK1647127273
Expires: 3600
Date: Fri, 10 Apr 2021 08:24:49 GMT
Content-Length: 0
//摄像头回复注销信息 这里注意Expires字段,为0代表注销 服务器收到后直接做业务操作回复200
REGISTER sip:34020000002000000002@192.168.6.14:5060 SIP/2.0
Via: SIP/2.0/UDP 192.168.9.200:5060;rport;branch=z9hG4bK399301364
Route: <sip:34020000001110000011@192.168.6.14:5060;lr>
From: <sip:34020000001110000011@192.168.9.200:5060>;tag=793568880
To: <sip:34020000001110000011@192.168.9.200:5060>
Call-ID: 214776626@192.168.9.200
CSeq: 3 REGISTER //第三次注册
Contact: <sip:34020000001110000011@192.168.9.200:5060>
Max-Forwards: 70
User-Agent: Dahua SIP UAS V1.0
Expires: 0 //注销
Content-Length: 0
注: 平台之间注册不需要鉴权,直接回复ok
2.心跳文本//摄像头发送过来的Keepalive保活信息
MESSAGE sip:34020000002000000002@192.168.6.14:5060 SIP/2.0 //MESSAGE 方法名,类似http的get/post方法
Via: SIP/2.0/UDP 192.168.1.8:5060;rport;branch=z9hG4bK700933079 //这个不用理会,但需要拷贝该字段,回复200要用
From: <sip:34020000001110000011@192.168.1.8:5060>;tag=1896094222 //同上
To: <sip:34020000002000000002@192.168.6.14:5060> //同上
Call-ID: 78119256@192.168.1.8 //同上
CSeq: 20 MESSAGE //唯一标识
Max-Forwards: 70
User-Agent: Dahua SIP UAS V1.0
Content-Type: Application/MANSCDP+xml //消息体格式
Content-Length: 178 //消息体长度,不带消息头,下面会空一行才会读到消息体,解析时注意下读到空行
<?xml version="1.0" encoding="GB2312" ?> //编码
<Notify>
<CmdType>Keepalive</CmdType> //注意这里的消息体,大华发送的消息体有空格的,海康的消息体不带空格,需要做下处理,或者使用xml工具类,这里为了方便直接解析字符串
<SN>0</SN>
<DeviceID>34020000001110000011</DeviceID> //设备编号
<Status>OK</Status>
</Notify>
//服务器需要对其进行200回复,超过不回复次数就会断流
SIP/2.0 200 OK //回复200
CSeq: 20 MESSAGE //拷贝上面心跳信息
Call-ID: 78119256@192.168.1.8 //同上
From: <sip:34020000001110000011@192.168.1.8:5060>;tag=1896094222 //同上
To: <sip:34020000002000000002@192.168.6.14:5060> //同上
Via: SIP/2.0/UDP 192.168.1.8:5060;rport;branch=z9hG4bK700933079 //同上
Content-Length: 0 //没有消息体,直接写0
请求
客户端向服务端发送示例:
sip协议
Via: SIP/2.0/UDP 27.10.24.1:5061;branch=z9hG4bK8210143023101430fa101430b
Call-ID: 9ae8ca0e3be8ca0ee2e8ca0ea0e8ca0e16e8c@27.10.24.1
From: <sip:42010000012005000001@27.10.24.1:5061;transport=udp>;tag=63a161ccc2a161cc1ba161cc59a161cc
To: <sip:45510000012005000001@27.10.30.98;transport=udp>
CSeq: 134349445 MESSAGE
Max-Forwards: 70
Expires: 90
User-Agent: IMOS/V3
Contact: <sip:42010000012005000001@27.10.24.1:5061>
Content-Type: application/MANSCDP+xml
Content-Length: 1288
<?xml version="1.0" encoding="GB2312"?>
<Response>
<CmdType>Catalog</CmdType>
<SN>859327</SN>
<DeviceID>4201010303</DeviceID> //设备编号,实际是上级组织编号
<SumNum>72</SumNum>
<DeviceList Num="1">
<Item> //核心数据
<DeviceID>42010000011315000808</DeviceID> //真正的设备编号
<Name>A ��ɽ�������·��-C79</Name> //名称
<Manufacturer>uniview</Manufacturer>
<Model>h3c</Model> //型号
<Owner>h3c</Owner> //所属厂商
<CivilCode>4201010303</CivilCode>
<Block></Block>
<Address>27.10.24.1</Address> //来源地址
<Parental>0</Parental>
<ParentID>4201010303</ParentID> //上级编号
<SafetyWay>0</SafetyWay>
<RegisterWay>1</RegisterWay>
<CertNum>1</CertNum>
<Certifiable>0</Certifiable>
<ErrCode>400</ErrCode>
<EndTime>2011-11-11T19:46:17</EndTime>
<Secrecy>0</Secrecy>
<IPAddress>27.14.33.75</IPAddress> //设备实际地址
<Port>8800</Port>
<Password>admin</Password>
<Status>ON</Status>
<Longitude>114.306191347343</Longitude>
<Latitude>30.6136439824189</Latitude>
<Info> //设备的相关信息
<CameraType>1</CameraType>
<PTZType>1</PTZType>
<PositionType>2</PositionType>
<RoomType>1</RoomType>
<UseType>1</UseType>
<SupplyLightType>1</SupplyLightType>
<DirectionType>1</DirectionType>
<Resolution></Resolution>
<DownloadSpeed></DownloadSpeed>
<SVCSpaceSupportMode>0</SVCSpaceSupportMode>
<SVCTimeSupportMode>0</SVCTimeSupportMode>
<StreamNum>1</StreamNum></Info>
</Item>
</DeviceList>
</Response>
4.订阅通知
订阅通知协议
注: 订阅通知相比catalog,主要是在head头里面多了一个event和expires
上下级平台发送
一个简单的gb28181直播预览流程:
- 接收下级域的注册请求
- 发送catalog请求,查看设备信息
- 选择设备进行预览
catalog流程: - 向下级域发送catalog请求
- 下级域回复200
- 下级域发送设备信息,可能一次只发送两个设备的信息
- 上级域回复200
- 循环步骤3和4
向下级域发送catalog请求:
MESSAGE sip:34020000001110000001@3402000000 SIP/2.0
Via: SIP/2.0/UDP 192.168.30.173:5060;rport;branch=SrsGbB14675203
From: <sip:34020000002000000001@192.168.30.173:5060>;tag=SrsGbF87133810
To: <sip:34020000001110000001@3402000000>
Call-ID: 202015285061
CSeq: 100 MESSAGE
Content-Type: Application/MANSCDP+xml
Max-Forwards: 70
User-Agent: SRS/4.0.26(Leo)
Content-Length: 149
<?xml version="1.0" encoding="UTF-8"?>
<Query>
<CmdType>Catalog</CmdType>
<SN>49013560</SN>
<DeviceID>34020000001110000001</DeviceID>
</Query>
下级域回复200:
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.30.173:5060;rport=5060;branch=SrsGbB14675203
From: <sip:34020000002000000001@192.168.30.173:5060>;tag=SrsGbF87133810
To: <sip:34020000001110000001@3402000000>;tag=283848756
Call-ID: 202015285061
CSeq: 100 MESSAGE
User-Agent: Embedded Net DVR/NVR/DVS
Content-Length: 0
下级域发送设备信息:
MESSAGE sip:34020000002000000001@3402000000 SIP/2.0
Via: SIP/2.0/UDP 192.168.30.228:5060;rport;branch=z9hG4bK1779972060
From: <sip:34020000001110000001@3402000000>;tag=1212522955
To: <sip:34020000002000000001@3402000000>
Call-ID: 1323356603
CSeq: 20 MESSAGE
Content-Type: Application/MANSCDP+xml
Max-Forwards: 70
User-Agent: Embedded Net DVR/NVR/DVS
Content-Length: 902
<?xml version="1.0" encoding="gb2312"?>
<Response>
<CmdType>Catalog</CmdType>
<SN>49013560</SN>
<DeviceID>34020000001110000001</DeviceID>
<SumNum>8</SumNum>
<DeviceList Num="2">
<Item>
<DeviceID>34020000001320000001</DeviceID>
<Name>өʯÔÆ</Name>
<Manufacturer>Manufacturer</Manufacturer>
<Model>Camera</Model>
<Owner>Owner</Owner>
<CivilCode>CivilCode</CivilCode>
<Address>192.168.30.200</Address>
<Parental>0</Parental>
<SafetyWay>0</SafetyWay>
<RegisterWay>1</RegisterWay>
<Secrecy>0</Secrecy>
<Status>ON</Status>
</Item>
<Item>
<DeviceID>34020000001320000002</DeviceID>
<Name>Camera 01</Name>
<Manufacturer>Manufacturer</Manufacturer>
<Model>Camera</Model>
<Owner>Owner</Owner>
<CivilCode>CivilCode</CivilCode>
<Address>192.168.30.195</Address>
<Parental>0</Parental>
<SafetyWay>0</SafetyWay>
<RegisterWay>1</RegisterWay>
<Secrecy>0</Secrecy>
<Status>ON</Status>
</Item>
</DeviceList>
</Response>
上级域回复200:
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.30.228:5060;rport=5060;received=192.168.30.228;branch=z9hG4bK1779972060
From: <sip:34020000001110000001@3402000000>;tag=1212522955
To: <sip:34020000002000000001@3402000000>
CSeq: 20 MESSAGE
Call-ID: 1323356603
User-Agent: SRS/4.0.26(Leo)
Content-Length: 0
sip相关的开源项目
pjsip
https://github.com/pjsip/pjproject
jain-siphttps://gitee.com/mirrors/jain-sip
gosiphttps://github.com/jart/gosip
2>,流媒体平台
https://github.com/648540858/wvp-GB28181-pro/tree/wvp-28181-2.0
https://gitee.com/18010473990/wvp-GB28181/tree/wvp-28181-2.0/
3>,流媒体框架
https://github.com/xiongziliang/ZLMediaKit
参考:
https://zhuanlan.zhihu.com/p/393863592
https://www.cnblogs.com/xiaxveliang/p/12434170.html
https://blog.csdn.net/sbddbfm/article/details/99095022
https://www.toutiao.com/article/6829089709172457988/?wid=1648729532579
本文主要是做一个归纳整理,日后有新的资料或许会一直更新。
如果你对生活感觉到了绝望,请不要气馁。因为这样只会让你更加绝望! 所谓的希望往往都是在绝望中萌发的,所以,请不要放弃希望!