Based on the GB28181 protocol, realize WVP platform voice broadcasting

        In the previous article, we talked about building a streaming media platform from scratch. In this article, we will talk about how to realize the voice broadcasting function based on the national standard protocol.
        The national standard GB/T 28181-2016 document, section 9.12, has a detailed description of voice broadcasting. We will not repeat the specific content, and developers can refer to it by themselves. Let's take a look at the command flowchart for voice broadcast in the document:

        If the developer develops based on WVP, the above process can only look at the command process of the voice stream receiver and the SIP server, and others are not needed for the time being, and the entire voice broadcast does not need to interact with the streaming media here. Let's take Dahua equipment as an example to demonstrate the entire voice broadcasting process. It should be noted here that if the Dahua device does not report the voice channel, it will fill in the default voice code as 34020000001370000001.

## 平台向设备发送语音广播通知
MESSAGE sip:[email protected]:5061 SIP/2.0
Call-ID: [email protected]
CSeq: 1 MESSAGE
From: <sip:[email protected]:6060>;tag=FromBcst1689689910253
To: <sip:52000010102330000004@5200001010>
Via: SIP/2.0/UDP 10.10.10.188:6060;branch=z9hG4bK-ViaBcst-1689689910253;rport
Max-Forwards: 70
Content-Type: Application/MANSCDP+xml
Content-Length: 178

<?xml version="1.0" ?>
<Notify>
<! -- 命令类型:广播通知-->
<CmdType>Broadcast</CmdType>
<! -- 命令序列号-->
<SN>914042</SN>
<! -- 语音输入设备的设备编码-->
<SourceID>52000010102330000001</SourceID>
<! -- 语音输出设备的设备编码-->
<TargetID>34020000001370000001</TargetID>
</Notify>

## 设备向平台回复200 OK
SIP/2.0 200 OK
Call-ID: [email protected]
Content-Length: 0
CSeq: 1 MESSAGE
From: <sip:[email protected]:6060>;tag=FromBcst1689689910253
To: <sip:52000010102330000004@5200001010>;tag=019d182856233baf702250b60fbf48f3
User-Agent: SIP UAS V3.0.0.954282
Via: SIP/2.0/UDP 10.10.10.188:6060;rport=6060;branch=z9hG4bK-ViaBcst-1689689910253
## 设备向平台发送语音广播应答
MESSAGE sip:[email protected]:6060 SIP/2.0
Call-ID: 8a75dde22d56ff96180fce46d8c242e5
Content-Length: 181
Content-Type: Application/MANSCDP+xml
CSeq: 1482 MESSAGE
From: <sip:[email protected]:5061>;tag=c76ca68a1f706a5276df0f3d2f2a42ae
Max-Forwards: 70
To: <sip:[email protected]:6060>
User-Agent: SIP UAS V3.0.0.954282
Via: SIP/2.0/UDP 10.10.10.17:5061;rport;branch=z9hG4bK3c908afb2f8160dff91b52e613c674ac

<?xml version="1.0" encoding="GB2312" standalone="yes" ?>
<Response>
<CmdType>Broadcast</CmdType>
<SN>914042</SN>
<DeviceID>34020000001370000001</DeviceID>
<Result>OK</Result>
</Response>
## 平台向设备回复200 OK
SIP/2.0 200 OK
CSeq: 1482 MESSAGE
Call-ID: 8a75dde22d56ff96180fce46d8c242e5
From: <sip:[email protected]:5061>;tag=c76ca68a1f706a5276df0f3d2f2a42ae
To: <sip:[email protected]:6060>
Via: SIP/2.0/UDP 10.10.10.17:5061;rport=5061;branch=z9hG4bK3c908afb2f8160dff91b52e613c674ac;received=10.10.10.17
Content-Length: 0
## 设备回复INVITE消息,告知平台设备接收音频的端口号
INVITE sip:[email protected]:6060 SIP/2.0
Call-ID: 8f3f84a660fb8bfa13d8ebc7729a7d51
Contact: <sip:[email protected]:5061>
Content-Length: 237
Content-Type: application/sdp
CSeq: 1 INVITE
Expires: 120
From: <sip:[email protected]:5061>;tag=4ecd395802160bdaedd6151885fcd27b
Max-Forwards: 70
Subject: 52000010102330000001:03d7a8ef3276a8ef327,34020000001370000001:0552354c54f3354c54f
To: <sip:[email protected]:6060>
User-Agent: SIP UAS V3.0.0.954282
Via: SIP/2.0/UDP 10.10.10.17:5061;rport;branch=z9hG4bKdf2458160f3f6a696bc219528e74f505

v=0
o=52000010102330000004 0 0 IN IP4 10.10.10.17
s=Play
i=VCam BroadCast Session
c=IN IP4 10.10.10.17
t=0 0
m=audio 10520 RTP/AVP 8 96 ##设备接收语音广播的端口号为10520,需要开发者自行储存,方便下面调用
a=recvonly
a=rtpmap:8 PCMA/8000/1
a=rtpmap:96 PS/90000
y=0065457400
f=v/0/0/0/0/0a/1/8/1
## 平台回应200 OK 响应设备的INVITE消息
SIP/2.0 200 OK
CSeq: 1 INVITE
Call-ID: 8f3f84a660fb8bfa13d8ebc7729a7d51
From: <sip:[email protected]:5061>;tag=4ecd395802160bdaedd6151885fcd27b
To: <sip:[email protected]:6060>;tag=z9hG4bK-ViaBcst-1689689910282
Via: SIP/2.0/UDP 10.10.10.17:5061;rport=5061;branch=z9hG4bKdf2458160f3f6a696bc219528e74f505;received=10.10.10.17
Content-Type: APPLICATION/SDP
Contact: <sip:[email protected]:6060>
User-Agent: DC-ColorFul
Content-Length: 180

v=0
o=52000010102330000004 0 0 IN IP4 10.10.10.188
s=Play
c=IN IP4 10.10.10.188
t=0 0
m=audio 8157 RTP/AVP 8 ## 准备一个本地端口,用于语音广播创建UDP连接,这里为8157
a=rtpmap:8 PCMA/8000
<! -- 只发不收-->
a=sendonly
y=0065457400
<! --字段:f= v/编码格式/分辨率/帧率/码率类型/码率大小a/编码格式/码率大小/采样率 -->
f=v/a/1/8/1

        Here we need to prepare a local port for creating a UDP connection for voice broadcast. It should be noted that this local port cannot be occupied by other programs, nor can it be occupied by other devices. If there are many devices used for voice broadcasting, developers need to allocate these local ports by themselves.

## 设备向平台发送ACK消息
ACK sip:[email protected]:6060 SIP/2.0
Call-ID: 8f3f84a660fb8bfa13d8ebc7729a7d51
Contact: <sip:[email protected]:5061>
Content-Length: 0
CSeq: 1 ACK
From: <sip:[email protected]:5061>;tag=4ecd395802160bdaedd6151885fcd27b
Max-Forwards: 70
To: <sip:[email protected]:6060>;tag=z9hG4bK-ViaBcst-1689689910282
User-Agent: SIP UAS V3.0.0.954282
Via: SIP/2.0/UDP 10.10.10.17:5061;rport;branch=z9hG4bKb874c74b8b87e4b96e45608bd9c5d801

        After receiving the ACK message from the device, no reply is required.

        Get the port number (10520) for receiving the audio stream of the previously stored device, the device IP (10.10.10.17), and create a local UDP port number (8157). Start to send RTP packaged audio data to the camera, because most devices only support audio in g711a format, so here we pack audio data in g711a format and send it to the device.

        The g711a audio data can be generated by the FFMPEG command. The command is as follows:

ffmpeg -i D:\文档\GB28181\fangsheng.mp3 -map_metadata -1 -fflags +bitexact -acodec pcm_alaw -ac 1 -ar 8000 D:\文档\GB28181\fangsheng.wav -y

        This constructs a mono audio encoded in PCMA format with a sample rate of 8000.

## 语音广播结束之后,关闭语音广播,给设备发送BYE指令
BYE sip:[email protected]:5061 SIP/2.0
Call-ID: 1fcbf8f1e8efc171daaf1677b585f4ec
CSeq: 2 BYE
From: <sip:52000010102330000001@5200001010>;tag=z9hG4bK-ViaBcst-1689694372435
To: <sip:34020000001370000001@5200001010>;tag=4f7a72e1f01183dae1b67f77b120c811
Via: SIP/2.0/UDP 10.10.10.17:5061;branch=z9hG4bK-313335-f9f2f7c7dafcb225442b96a2460fb586
Max-Forwards: 70
Content-Length: 0
## 设备回复200 OK
SIP/2.0 200 OK
Via: SIP/2.0/UDP 10.10.10.17:5061;branch=z9hG4bK-313335-f9f2f7c7dafcb225442b96a2460fb586
From: <sip:34020000001370000001@5200001010>;tag=4f7a72e1f01183dae1b67f77b120c811
To: <sip:52000010102330000001@5200001010>;tag=z9hG4bK-ViaBcst-1689694372435
Call-ID: 1fcbf8f1e8efc171daaf1677b585f4ec
CSeq: 2 BYE
Content-Length: 0

        After the broadcast, we remember to close the locally created UDP port. Finally, post a screenshot of the voice broadcast function implemented on my platform. The voice files broadcast by the voice broadcast can be implemented in the code according to specific business scenarios.

        This is the process and implementation logic of the entire voice broadcast, and the same principle applies to other devices, such as Uniview and Hikvision. RTP packaged audio data is sent to the camera. We will not introduce it in detail here. If you need in-depth understanding and related secondary development, you can leave a message for exchange.

        ps: (Preview) In the next article, I am going to talk about the implementation logic of voice intercom based on the national standard protocol~

Guess you like

Origin blog.csdn.net/qq_33104169/article/details/131799834