安防国标学习笔记8 NodeJS实现GB28181信令服务端

一、实现步骤

通过前面的几次学习,一个能接收摄像机注册的GB28181服务端信令服务器功能基本上可以使用了。本文只是对前面的内容进行总结汇总。

1. 整体架构

  • NodeJS实现信令服务器
  • ZLMediaKit 用作媒体服务器

2. 部署过程

I 克隆项目GB28181_NODE_HTTP到本地

  1. 通过npm i安装依赖
  2. 修改配置文件,将 sip_config.js.template 修改为 sip_config.js

配置文件说明:

/**
 * 配置文件
 */
var config={
    // 设备信息,没有写在这里的将注册不了,不允许匿名注册
    registry : {
        '34020000001110000001'  : { password : '12345678', online:false},   // 测试,这个是设备的account,需要密码
        '34020000001320000001'  : {}                                        // 测试,这个是通道的account,不需要密码
    },
    http:{
        // http restful接口端口
        port        : 8777
    },
    sip:{
       server:{
           // sip服务端口号
           port     : 5060,
           // sip 信令服务器域
           realm    : '3401030000',
           // sip信令服务器ip地址,一般是本机ip
           ip       : '148.70.110.25',
           // sip服务端 account
           account  : '34010300002000002250'
       } 
    },
    logger:{
        // 日志地址
        path:'./sip.log'
    },
    // 生成Tag使用的函数
    rstring:function() { return Math.floor(Math.random()*1e6).toString(); }
};

module.exports=config;

启动项目,使用node sip_app.js
也可以使用pm2启动,使用pm2 start sip_app.js

II 部署ZLMediaKit

这里使用CentOS7,使用docker部署

a. 安装docker

b. 下载并启动镜像

# 创建docker
docker run -id -p 10000:10000/udp -p 1935:1935 -p 8080:80 -p 554:554 -p 332:332 -p 9000:9000 gemfield/zlmediakit:20.04-runtime-ubuntu18.04

c. 设置webhook

MediaServer可以把内部的一些事件通过http post 第三方http服务器的方式通知出去,以下是相关的默认配置:

[hook]
enable=1
admin_params=secret=135c73f7-bb6b-4889-a715-d9eb2d1925dd
timeoutSec=10

on_flow_report=https://127.0.0.1/index/hook/on_flow_report
on_http_access=https://127.0.0.1/index/hook/on_http_access
on_play=https://127.0.0.1/index/hook/on_play
on_publish=https://127.0.0.1/index/hook/on_publish
on_record_mp4=https://127.0.0.1/index/hook/on_record_mp4
on_rtsp_auth=https://127.0.0.1/index/hook/on_rtsp_auth
on_rtsp_realm=https://127.0.0.1/index/hook/on_rtsp_realm
on_shell_login=https://127.0.0.1/index/hook/on_shell_login
on_stream_changed=https://127.0.0.1/index/hook/on_stream_changed
on_stream_none_reader=https://127.0.0.1/index/hook/on_stream_none_reader
on_stream_not_found=https://127.0.0.1/index/hook/on_stream_not_found
on_server_started=https://127.0.0.1/index/hook/on_server_started

如果是鉴权事件,且访问IP是127.0.0.1或者鉴权url参数与admin_params一致,那么会直接鉴权成功(不会触发鉴权web hook)。

设置on_stream_changed:

on_stream_changed=https://sip信令服务器地址:8777/index/hook/on_stream_changed

设置以后,推送成功的消息就会传到我们的NodeJS程序里。

在Express里接收此消息,并解析出播放的url;

on_stream_changed解释:

rtsp/rtmp流注册或注销时触发此事件;此事件对回复不敏感。

触发请求:

POST /index/hook/on_stream_changed HTTP/1.1
Accept: */*
Accept-Language: zh-CN,zh;q=0.8
Connection: keep-alive
Content-Length: 118
Content-Type: application/json
Host: 127.0.0.1
Tools: ZLMediaKit
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
{
   "app" : "live",
   "regist" : true,
   "schema" : "rtsp",
   "stream" : "obs",
   "vhost" : "__defaultVhost__"
}

请求参数详解:

参数名 参数类型 参数解释
app string 流应用名
regist bool 流注册或注销
schema string rtsp或rtmp
stream string 流ID
vhost string 流虚拟主机

默认回复:

HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 40
Content-Type: application/json; charset=utf-8
Date: Fri, Sep 20 2019 08:27:35 GMT
Keep-Alive: timeout=10, max=100
Server: ZLMediaKit-4.0

{
   "code" : 0,
   "msg" : "success"
}

二、使用过程

1. 设置IPC

找一台大华或海康摄像机,设置:

  • sip服务器: 设置为sip信令服务器地址
  • sip服务端口号: 5060
  • sip服务账号: 与上面sip_config.js里的sip.server.account设置保持一致
  • sip客户端账号: 与上面sip_config.js registry里值保持一致
  • sip通道账号: 与上面sip_config.js registry不带密码的值保持一致

2. 访问列表api

http://信令服务器ip:8777/api/v1/device/list

3. 访问播放api

http://信令服务器ip:8777/api/v1/device/invite?account=34020000001320000001

4. 获得播放地址

http://信令服务器ip:8777/api/v1/device/query?account=34020000001320000001
从中获取streamid值。

5. 使用rtsp播放

rtsp://媒体服务器地址/rtp/streamid

猜你喜欢

转载自blog.csdn.net/xundh/article/details/106597781
今日推荐