background

The full name of 28181 protocol is GB/T28181 “Technical Requirements for Information Transmission, Exchange and Control of Security Video Surveillance Network System”, which is proposed by the Bureau of Science and Technology Information of the Ministry of Public Security and under the supervision of National Technical Committee on Security Alarm System of Standardization Administration (SAC/TC100). A national standard jointly drafted by the Ministry of Public Security and other units (hereinafter referred to as 28181).

28181 protocol is widely used in the monitoring of safe cities, traffic and roads in China. If you want to build a unified monitoring platform, it is essential to support 28181 protocol access. Now many customers want to expand on the previous 28181 platform.

instructions

LiveGBS GB28181 streaming media server is responsible for transferring PS pushed by GB28181 device/platform into ES stream and then distributing it. At the same time, LiveGBS provides HTTP API, through which it can learn the running status information of streaming media forwarding service, forwarding session information, server configuration and version information, etc.

The LiveGBS GB28181 streaming media server provides the following functions: 1. Accept and process the push flow request of GB28181 access server (call the authentication server interface if there is push flow permission verification); 2. Accept and process the push current of GB28181 equipment; 3. Real-time streaming media processing, PS (TS) to ES; 4. Push ES stream to EasyDSS streaming media server; 5. Accept and process the flow disconnection request of GB28181 access server; 6. Provide HTTP API interface for server to obtain status, information and control;

Download: www.liveqing.com/docs/downlo…

Detailed process of LiveGBS live streaming

1 The access server sends an Invite request. The access server sends an Invite request to the streaming media server. The streaming media service returns an SDP message body, which describes the IP address, port number, and media format of media streams received by the media server. The Invite request code is as follows:

const options = { serialServer: serialServer, serialDevice: code, method: common.SIP_INVITE, contentType: common.CONTENT_NONE, content: sdp, host: hostip, port: hostport, rtpovertcp: (parseInt(rtpovertcp)===0? 'UDP':'TCP') }; console.log('inviteMediaServer...... sendRequest' + JSON.stringify(options)); uas.sendRequest(options);Copy the code

2 Streaming media service accepts Invite request processing and ACK reply The streaming media service accepts Invite request and processes the request in the callback function. The JS code is as follows:

uas.on('invite', async ctx => { const request = ctx.request; const content = JSON.parse(request.content); const status = 200; const serial = sip.parseUri(request.uri).user; const host = config.server.serverHost; Let ssid = serial. The substring (16, 20). // PrefixInteger(sessionid,4); Let sirialid = serial. The substring (3, 8). const ssrc = "0"+sirialid+ssid; console.log("ssrc = "+ssrc); let sdp = ''; // let bHas = this.session_. Has (serial); console.log(bHas); if (bHas) { console.log('this.session_ has exist serial: '+serial); sdp = ''; } else{ let port = config.server.udpPort; // let transport = 'RTP/AVP'; let a = "a=recvonly\r\n"; if(content.rtpovertcp === 'TCP' ) { port = config.server.tcpPort; Transport = 'TCP/RTP/AVP'; a = "a=recvonly\r\na=setup:passive\r\n"; } sdp = "v=0\r\n" + `o=${serial} 0 0 IN IP4 ${host}\r\n` + "s=Play\r\n" + `c=IN IP4 ${host}\r\n` + "t=0 0\r\n" + `m=video ${port} ${transport} 96 98 97\r\n` + "a=rtpmap:96 PS/90000\r\n" + "a=rtpmap:98 H264/90000\r\n" + "a=rtpmap:97 MPEG4/90000\r\n" + `${a}`+ //`a=connection:new\r\n` + `y=${ssrc}\r\n`; // A new channel is coming, delete the old rtpserver.deleteChannels(parseInt(ssrc)); // Create a new stram,and add to redis this.registerStream(parseInt(ssrc),uuidv4(),true); } let response = sip.makeResponse(request, status, common.messages[status]); uas.sendAckEx(response, sdp); });Copy the code

As shown in the code above, we provide two stream transport modes in the SDP message body, namely TCP and UDP, which are controlled by the “RtpoverTCP” parameter of Invite requests. TCP is widely used in GB28181’s scheme of pushing streams to public network servers because of its no-packet transfer. At present, most devices that support national standards on the market do not support TCP mode. Udp is still the mainstream mode. However, udp mode is not effective in public network applications after testing, so it needs to be further optimized or improved.

3 The access server receives an ACK response and invites the request device to start pushing the flow. The JS code for ACK response processing in the callback function is as follows:

uas.once('ack', async ctx => { const request = ctx.request; const callId = request.headers['call-id']; if (request.content.length > 0 ) { const serial = serialDevice; //sip.parseUri(request.headers.from.uri).user; let response ; if(! this.session_.has(callId)) { response = await this.inviteDevice(serial, code, callId, request.content); //Invite Device is complete if(response ! = undefined) { if(response.content) { const transform = require('sdp'); const res = transform.parse(response.content); console.log(res.media[0].protocol); if((res.media[0].protocol === 'RTP/AVP'&&parseInt(rtpovertcp)===0) || (res.media[0].protocol === 'TCP/RTP/AVP'&&parseInt(rtpovertcp)===1) ){ if (response.status === 200 ) { //send ack to stream server this.ackMediaServer(response.status,request,request.content); this.session_.set(callId, response); } } else{ response.status = 700; } } console.log('inviteMediaServer ack is coming....... response='+JSON.stringify(response)); } resolve(response); } else{console.log('inviteMediaServer this.session_. Has: '+callId); }}});Copy the code

As shown in the code above, after the InviteDevice request completes, we do a special processing in the return Response processing, namely: If the device returns the UDP mode of ‘RTP/AVP’ when the TCP pulls a stream, the device does not support the TCP mode, and therefore returns an unsupported streaming media transmission mode to the upper layer.

4 The Invite device normally returns a 200 reply and sends it to the streaming media server.

5 The streaming media service responds successfully to the pull request

        uas.on('ack', async ctx => {
            const request = ctx.request;
            if (request.content.length === 0) {
                return;
            }
            const serial = sip.parseUri(request.headers.from.uri).user;
            this.session_.set(serial, request);
            const ssrc = serialTossrc(serial);
            // resole a new stram,and refresh to redis  
            const info = JSON.parse(await redis.get(`stream:${parseInt(ssrc)}`)); 
            this.registerStream(parseInt(ssrc),info.uuId,false);     
        });
Copy the code

At this point, the whole pull-flow process has been completed.

WEB :www.liveqing.com