java自研udp负载均衡服务实现gb28181的sip信令代理

文章目录

  • 参考
  • 一、国标gb28181信令交互流程
    • 1、注册
    • 2、心跳
    • 3、查询设备目录 queryCatalog
    • 3、服务端点播
    • 4、云台控制
    • 5、目录订阅
    • 6、移动位置订阅 MobilePosition
    • 7、布防和报警订阅
    • 8、级联配置
    • 9、级联下级向上级推送通道
  • 二、udp协议
    • 1、udp协议理论
      • UDP的客户端通信建立步骤
      • UDP的服务端通信建立步骤
    • 2、Nat穿透
  • 三、自定义gb28181Proxy负载均衡代理
    • 原理图
    • 1、创建SipProxy 服务
    • 2、初始化需要负载均衡loadBlance代理的sipServers
    • 1、SipProxy启动监听
    • 2、解析每个receive的数据包
    • 3、formward转发消息
      • 客户端->服务端
      • 服务端->客户端
  • 四、需要考虑的问题
    • SipProxy需要缓存客户端的ip和port,避免SipProxy挂掉重启之后,来自服务端的消息因找不到client而无法转发
    • SipProxy目前时阻塞式receive监听,可以加入多线程处理接收到的消息,需要注意顺序消费问题
  • 五、rtp推流
    • 1、参考

参考

Java实现UDP功能
UDP内网穿透讲解及代码实现

一、国标gb28181信令交互流程

通过happytime客户端模拟器实现效果

1、注册

  • 客户端发送REGISTER信令到服务器
  • 服务器需要开启认证,则会返回401 Unauthorized,并在WWW-Authenticate中携带认证相关信息,如果服务器没有开启认证,则直接回复SIP/2.0 200 OK
  • 客户端收到401 Unauthorized之后,则组装REGISTER并携带计算好的认证信息Authorization
  • 服务端收到REGISTER消息,则返回SIP/2.0 200 OK说明认证注册成功,

注册密码验证原理参考:GB28181 设备注册密码验证算法

交互日志如下:

[2023-05-23 11:02:52] : [INFO] init_local_net, sony

[2023-05-23 11:02:52] : [INFO] init_local_net, hsip.local_ipstr = 192.168.0.102

[2023-05-23 11:02:52] : [INFO] net_buf_init, num = 32

[2023-05-23 11:02:52] : [INFO] hdrv_buf_init, num = 256

[2023-05-23 11:02:52] : [INFO] sip_start, create net rx thread sucessful

[2023-05-23 11:02:52] : [INFO] sip_start, create sip main thread sucessful

[2023-05-23 11:02:52] : [INFO] sip:34020000001110000099@4401007844

[2023-05-23 11:02:52] : [INFO] sip_user_socket_init, sip local port 5066

[2023-05-23 11:02:52] : [INFO] cm_start_reg, sip_user_socket_init finish

[2023-05-23 11:02:52] : [INFO] cm_start_reg, sip_build_register_normal_msg finish

[2023-05-23 11:02:52] : [DEBUG] sip_send_msg, client-->server : 

REGISTER sip:44010078442009000089@4401007844 SIP/2.0

Via: SIP/2.0/UDP 192.168.0.102:5066;rport;branch=z9hG4bK504c6

From: <sip:34020000001110000099@4401007844>;tag=00001560

To: <sip:34020000001110000099@4401007844>

Contact: <sip:[email protected]:5066>

Call-ID: [email protected]

CSeq: 1 REGISTER

Max-Forwards: 70

Expires: 3600

User-Agent: Happytime GB28181 Device V6.2

Content-Length: 0





[2023-05-23 11:02:52] : [INFO] cm_start_reg, user_tx_free_msg finish

[2023-05-23 11:02:54] : [DEBUG] sip_msg_rx, server-->client : 

SIP/2.0 401 Unauthorized

Via: SIP/2.0/UDP 192.168.0.102:5066;rport=15692;received=192.168.0.44;branch=z9hG4bK504c6

From: <sip:34020000001110000099@4401007844>;tag=00001560

To: <sip:34020000001110000099@4401007844>;tag=833794167

Call-ID: [email protected]

CSeq: 1 REGISTER

Contact: <sip:[email protected]:15692>

User-Agent: EasyGBS v200511

Content-Length: 0

WWW-Authenticate: Digest realm="4401007844",nonce="301745769f4fc5498732b9b40d8febc5"



[2023-05-23 11:02:54] : [DEBUG] sip_send_msg, client-->server : 

REGISTER sip:44010078442009000089@4401007844 SIP/2.0

Via: SIP/2.0/UDP 192.168.0.102:5066;rport;branch=z9hG4bK504c9

From: <sip:34020000001110000099@4401007844>;tag=00001560

To: <sip:34020000001110000099@4401007844>

Contact: <sip:[email protected]:5066>

Call-ID: [email protected]

CSeq: 2 REGISTER

Max-Forwards: 70

Expires: 3600

User-Agent: Happytime GB28181 Device V6.2

Authorization: Digest username="34020000001110000099",realm="4401007844",nonce="301745769f4fc5498732b9b40d8febc5",response="af4a6f526d4b6fd1781ee32f98b118dd",uri="sip:44010078442009000089@4401007844"

Content-Length: 0





[2023-05-23 11:02:54] : [DEBUG] sip_msg_rx, server-->client : 

SIP/2.0 200 OK

Via: SIP/2.0/UDP 192.168.0.102:5066;rport=15692;received=192.168.0.44;branch=z9hG4bK504c9

From: <sip:34020000001110000099@4401007844>;tag=00001560

To: <sip:34020000001110000099@4401007844>;tag=717794764

Call-ID: [email protected]

CSeq: 2 REGISTER

Contact: <sip:[email protected]:15692>

Expires: 3600

User-Agent: Easy SIP Server

Content-Length: 0



[2023-05-23 11:02:54] : [INFO] reg pass!

2、心跳

  • 客户端发起MESSAGE信令,并且Content-Type: Application/MANSCDP+xml,发起keeplive心跳请求
  • 服务端收到之后回复 SIP/2.0 200 OK

交互日志如下:

[2023-05-23 11:03:25] : [DEBUG] sip_send_msg, client-->server : 

MESSAGE sip:44010078442009000089@4401007844 SIP/2.0

Via: SIP/2.0/UDP 192.168.0.102:5066

From: <sip:34020000001110000099@4401007844>;tag=00004823

To: <sip:44010078442009000089@4401007844>

Call-ID: [email protected]

CSeq: 2 MESSAGE

Max-Forwards: 70

User-Agent: Happytime GB28181 Device V6.2

Content-Type: Application/MANSCDP+xml

Content-Length: 170



<?xml version="1.0" encoding="GB2312"?>

<Notify>

<CmdType>Keepalive</CmdType>

<SN>1</SN>

<DeviceID>34020000001110000099</DeviceID>

<Status>OK</Status>

</Notify>





[2023-05-23 11:03:25] : [DEBUG] sip_msg_rx, server-->client : 

SIP/2.0 200 OK

Via: SIP/2.0/UDP 192.168.0.102:5066

From: <sip:34020000001110000099@4401007844>;tag=00004823

To: <sip:44010078442009000089@4401007844>;tag=407825634

Call-ID: [email protected]

CSeq: 2 MESSAGE

User-Agent: Easy SIP Server

Content-Length: 0

3、查询设备目录 queryCatalog

  • 服务端主动下发MESSAGE信令并且Content-Type: Application/MANSCDP+xml,发起Catalog查询请求
  • 客户端收到之后先回复 SIP/2.0 200 OK
  • 客户端接着发送MESSAGE信令并且Content-Type: Application/MANSCDP+xml,发起Response响应,罗列设备和通道信息
  • 服务端收到之后回复 SIP/2.0 200 OK

交互日志如下:

[2023-05-23 11:02:57] : [DEBUG] sip_msg_rx, server-->client : 

MESSAGE sip:[email protected]:15692;transport=udp SIP/2.0

Via: SIP/2.0/UDP 182.xx.xx.73:15692;rport=15692;branch=z9hG4bK269797766

From: <sip:44010078442009000089@4401007844>;tag=430797766

To: <sip:34020000001110000099@4401007844>

Call-ID: 275797766

CSeq: 89325 MESSAGE

Max-Forwards: 70

User-Agent: Easy SIP Server

Content-Type: Application/MANSCDP+xml

Content-Length: 156



<?xml version="1.0" encoding="UTF-8"?>
<Query>
    <CmdType>Catalog</CmdType>
    <SN>683797766</SN>
    <DeviceID>34020000001110000099</DeviceID>
</Query>






[2023-05-23 11:02:57] : [WARN] sip_msg_parse, strlen[160] != ctx len[156]!!!

[2023-05-23 11:02:57] : [INFO] sip_request_rx, from user [44010078442009000089]@[4401007844]

你可能感兴趣的:(音视频,java,java,udp,负载均衡)