建设财经资讯网站的目的优化教程
规范解读
GB28181-2022注册、注销基本要求相对GB28181-2016版本,做了一定的调整,新调整的部分如下:
——更改了注册和注销基本要求(见 9.1.1,2016 年版的 9.1.1)。
1.增加对NAT模式网络传输要求,宜增加TCP传输模式
——增加了注册重定向(见 9.1.2.3)。
这里,我们来看看GB28181-2022针对注册和注销的基本要求:
a)SIP客户端,网关、SIP设备、联网系统等SIP代理(SIP UA)应使用IETF RFC 3261定义的REGISTER方法进行注册和注销。注册和注销时应进行认证,认证方式应支持数字摘要认证方式,高安全级别的宜支持数字证书的认证方式。
b)注册成功后,SIP代理在注册过期时间到来之前,应向注册服务器进行刷新注册﹐刷新注册消息流程应与9.1.2.1的流程描述一致,并遵循IETF RFC 3261对刷新注册的规定。
c)若注册失败,SIP代理应间隔一定时间后继续发起注册过程,与上一次注册时间间隔应可调,一般情况下不应短于60 s。
d)系统、设备注册过期时间应可配置,缺省值为86 400 s(1 天),应在注册过期时间到来之前发送刷新注册消息,为SIP服务器预留适当刷新注册处理时间,注册过期时间不应短于3600 s。
e)SIP代理注册成功则认为SIP服务器为在线状态,注册失败则认为SIP服务器为离线状态SIP服务器在SIP代理注册成功后认为其为在线状态,SIP代理注册过期则认为其为离线状态。
f)对于处于开启网络地址转换(NAT)功能的路由器内侧的SIP代理,宜支持使用TCP发起注册,并在注册成功后保持TCP连接不关闭,SIP代理及服务器在该TCP通道里发送心跳、刷新注册、视音频点播、控制等所有请求及响应SIP消息。若TCP通道断开,则认为SIP代理异常掉线,SIP代理应按前述要求间隔一定时间后重新发起注册。
相对GB28181-2016,注册和注销基本要求,主要增加了NAT模式下的网络传输要求,建议增加TCP传输模式。
下面我们来看看注册重定向,注册重定向在GB28181-2016规范并没有体现,GB28181-2022针对此做了详细的说明,注册重定向流程如下:

注册重定向流程描述如下:
a)1 :设备或系统端的SIP代理向SIP重定向服务器发起注册请求;
b)(可选)2:SIP重定向服务器向SIP代理发送响应401,并在响应的消息头 WWW-Authentica
字段中给出适合SIP代理的认证体制和参数;
c)(可选)3;SIP代理重新向SIP重定向服务器发送REGISTER请求,在请求的Authorization
字段给出信任书,包含认证信息;
d)4:SIP重定向服务器对请求进行验证,如果检查出SIP代理身份合法,SIP重定向服务器根据内部策略选择设备需注册的SIP服务器A,并回复注册请求响应302,响应消息携带Contact头域和Expires,Contact格式为(sip;SIP服务器A编码@目的IP地址端口>,在此头域中携带SIP服务A的地址;
e)5:SIP代理收到302重定向响应后,向SIP服务器A发起注册;
f)6~8:完成注册重定向流程后,后续所有请求和响应动作由SIP服务器A和设备直接完成;
g)重定向后,如果SIP代理按照9.1.1的注册要求或9.6.1的心跳要求,判定SIP服务器A离线,或注册被SIP服务器A拒绝(包括403或500等错误),则SIP代理应重新执行注册重定向流程,向SIP重定向服务器发起注册请求。
技术实现
本文以大牛直播SDK的Android平台GB28181设备接入端为例,通过setTransportProtocol()接口,信令可选TCP或UDP模式:
/** Author: daniusdk.com*/
private boolean initGB28181Agent() {if ( gb28181_agent_ != null )return true;getLocation(context_);String local_ip_addr = IPAddrUtils.getIpAddress(context_);Log.i(TAG, "initGB28181Agent local ip addr: " + local_ip_addr);if ( local_ip_addr == null || local_ip_addr.isEmpty() ) {Log.e(TAG, "initGB28181Agent local ip is empty");return false;}gb28181_agent_ = GBSIPAgentFactory.getInstance().create();if ( gb28181_agent_ == null ) {Log.e(TAG, "initGB28181Agent create agent failed");return false;}gb28181_agent_.addListener(this);gb28181_agent_.addPlayListener(this);gb28181_agent_.addTalkListener(this);gb28181_agent_.addAudioBroadcastListener(this);gb28181_agent_.addDeviceControlListener(this);gb28181_agent_.addQueryCommandListener(this);// 必填信息gb28181_agent_.setLocalAddress(local_ip_addr);gb28181_agent_.setServerParameter(gb28181_sip_server_addr_, gb28181_sip_server_port_, gb28181_sip_server_id_, gb28181_sip_domain_);gb28181_agent_.setUserInfo(gb28181_sip_username_, gb28181_sip_password_);// 可选参数gb28181_agent_.setUserAgent(gb28181_sip_user_agent_filed_);gb28181_agent_.setTransportProtocol(gb28181_sip_trans_protocol_==0?"UDP":"TCP");// GB28181配置gb28181_agent_.config(gb28181_reg_expired_, gb28181_heartbeat_interval_, gb28181_heartbeat_count_);com.gb.ntsignalling.Device gb_device = new com.gb.ntsignalling.Device("34020000001380000001", "安卓测试设备", Build.MANUFACTURER, Build.MODEL,"宇宙","火星1","火星", true);if (mLongitude != null && mLatitude != null) {com.gb.ntsignalling.DevicePosition device_pos = new com.gb.ntsignalling.DevicePosition();device_pos.setTime(mLocationTime);device_pos.setLongitude(mLongitude);device_pos.setLatitude(mLatitude);gb_device.setPosition(device_pos);gb_device.setSupportMobilePosition(true); // 设置支持移动位置上报}gb28181_agent_.addDevice(gb_device);/*com.gb.ntsignalling.Device gb_device1 = new com.gb.ntsignalling.Device("34020000001380000002", "安卓测试设备2", Build.MANUFACTURER, Build.MODEL,"宇宙","火星1","火星", true);if (mLongitude != null && mLatitude != null) {com.gb.ntsignalling.DevicePosition device_pos = new com.gb.ntsignalling.DevicePosition();device_pos.setTime(mLocationTime);device_pos.setLongitude(mLongitude);device_pos.setLatitude(mLatitude);gb_device1.setPosition(device_pos);gb_device1.setSupportMobilePosition(true);}gb28181_agent_.addDevice(gb_device1);*/if (!gb28181_agent_.createSipStack()) {gb28181_agent_ = null;Log.e(TAG, "initGB28181Agent gb28181_agent_.createSipStack failed.");return false;}boolean is_bind_local_port_ok = false;// 最多尝试5000个端口int try_end_port = gb28181_sip_local_port_base_ + 5000;try_end_port = try_end_port > 65536 ?65536: try_end_port;for (int i = gb28181_sip_local_port_base_; i < try_end_port; ++i) {if (gb28181_agent_.bindLocalPort(i)) {is_bind_local_port_ok = true;break;}}if (!is_bind_local_port_ok) {gb28181_agent_.releaseSipStack();gb28181_agent_ = null;Log.e(TAG, "initGB28181Agent gb28181_agent_.bindLocalPort failed.");return false;}if (!gb28181_agent_.initialize()) {gb28181_agent_.unBindLocalPort();gb28181_agent_.releaseSipStack();gb28181_agent_ = null;Log.e(TAG, "initGB28181Agent gb28181_agent_.initialize failed.");return false;}return true;
}@Override
public void ntsRegisterOK(String dateString) {Log.i(TAG, "ntsRegisterOK Date: " + (dateString!= null? dateString : ""));
}@Override
public void ntsRegisterTimeout() {Log.e(TAG, "ntsRegisterTimeout");
}@Override
public void ntsRegisterTransportError(String errorInfo) {Log.e(TAG, "ntsRegisterTransportError error:" + (errorInfo != null?errorInfo :""));
}
总结
以上是GB28181-2022注册注销基本要求和注册重定向相关的调整,感兴趣的开发者,可以直接阅读相关规范,总的来说GB28181-2022更全面,2016版忽略的细节,好多都已很完善,剩下的就是需要开发者们调整自己的业务逻辑,快速兼容GB28181-2022版。