Android平台GB28181设备接入端语音广播技术探究和填坑指南

简介: GB/T28181-2016官方规范和交互流程,我们不再赘述。

技术背景

GB/T28181-2016官方规范和交互流程,我们不再赘述。

c8637929498c4bbd9fc2e557cbceee97.png


SIP服务器发起广播流程示意图如下:

240f2f15804d4294934a486135027c52.png

需要注意的是:语音广播通知、语音广播应答命令


消息头 Content-type字段为 Content-type:Application/MANSCDP+xml。


语音广播通知、语音广播应答命令采用 MANSCDP协议格式定义。


消息示例如下:


a) 语音广播通知

MESSAGE sip:34020000001310000056@192.168.100.9:6720 SIP/2.0\
Via: SIP/2.0/UDP 192.168.100.10:5060;rport=5060;branch=z9hG4bK1073741856;received=192.168.100.10\
From: <sip:34020000002000000001@192.168.100.10:5060>;tag=912513446\
To: <sip:34020000001310000056@192.168.100.9:6720>\
Call-ID: 536870958\
CSeq: 1 MESSAGE\
Contact: <sip:34020000002000000001@192.168.100.10:5060>\
Content-Type: Application/MANSCDP+xml\
Max-Forwards: 70\
User-Agent: Hikvision\
Content-Length: 172\
\
<?xml version="1.0"?>\
<Notify>\
<CmdType>Broadcast</CmdType>\
<SN>11</SN>\
<SourceID>34020000002000000001</SourceID>\
<TargetID>34020000001310000056</TargetID>\
</Notify>\

b) 语音广播应答

MESSAGE sip:34020000002000000001@192.168.100.10:5060 SIP/2.0\
Call-ID: 6d6bc2a5d380a0f8d6787cf614fb0bd8@192.168.100.9\
CSeq: 18300138 MESSAGE\
From: <sip:34020000001310000056@3402000000>;tag=4a5b3953\
To: <sip:34020000002000000001@192.168.100.10:5060>\
Via: SIP/2.0/UDP 192.168.100.9:6720;rport;branch=z9hG4bK-373435-549b6376963815eb98e2a2f011473b41\
Max-Forwards: 70\
User-Agent: NT GB UserAgent V1.91-20230420[daniusdk.com]\
Content-Type: Application/MANSCDP+xml\
Content-Length: 173\
\
<?xml version="1.0" encoding="GB2312"?>\
<Response>\
<CmdType>Broadcast</CmdType>\
<SN>11</SN>\
<DeviceID>34020000001310000056</DeviceID>\
<Result>OK</Result>\
</Response>\


c) 平台侧回复200 OK:

SIP/2.0 200 OK\
Via: SIP/2.0/UDP 192.168.100.9:6720;rport;branch=z9hG4bK-373435-549b6376963815eb98e2a2f011473b41\
From: <sip:34020000001310000056@3402000000>;tag=4a5b3953\
To: <sip:34020000002000000001@192.168.100.10:5060>\
Call-ID: 6d6bc2a5d380a0f8d6787cf614fb0bd8@192.168.100.9\
CSeq: 18300138 MESSAGE\
User-Agent: Hikvision\
Content-Length: 0\


d) 设备接入侧发起invite请求:

INVITE sip:34020000002000000001@3402000000 SIP/2.0\
...
User-Agent: NT GB UserAgent V1.91-20230420[daniusdk.com]\
Content-Type: APPLICATION/SDP\
Content-Length: 245\
\
v=0\
o=34020000001310000056 3898650599696 3898650599696 IN IP4 192.168.100.9\
s=Play\
c=IN IP4 192.168.100.9\
t=0 0\
m=audio 25000 TCP/RTP/AVP 8\
a=setup:active\
a=connection:new\
a=recvonly\
a=rtpmap:8 PCMA/8000\
y=0200009722\
f=v/a/1/8/1\


e) 国标平台侧回复200 OK:

SIP/2.0 200 OK\
...
Content-Type: application/sdp\
User-Agent: Hikvision\
Content-Length: 205\
\
v=0\
o=34020000002000000001 0 0 IN IP4 192.168.100.10\
s=Play\
c=IN IP4 192.168.100.10\
t=0 0\
m=audio 16002 TCP/RTP/AVP 8\
a=rtpmap:8 PCMA/8000\
a=sendonly\
a=setup:passive\
y=0200009727\
f=v/a/1/8/1\


f) 设备接入侧发Ack:

ACK sip:34020000002000000001@192.168.100.10:5060 SIP/2.0\
...
Max-Forwards: 70\
Contact: <sip:34020000001310000056@192.168.100.9:6720>\
User-Agent: NT GB UserAgent V1.91-20230420[daniusdk.com]\
Content-Length: 0\

技术实现

以大牛直播SDK的Android平台GB28181设备接入侧为例:

0f424d5bbff644bb97f0b32ff3650530.jpg

收到语音广播:

@Override
public void ntsOnNotifyBroadcastCommand(String fromUserName, String fromUserNameAtDomain, String sn, String sourceID, String targetID) {
handler_.postDelayed(new Runnable() {
@Override
public void run() {
Log.i(TAG, "daniusdk, ntsOnNotifyBroadcastCommand, fromUserName:"+ from_user_name_ + ", fromUserNameAtDomain:"+ from_user_name_at_domain_
+ ", SN:" + sn_ + ", sourceID:" + source_id_ + ", targetID:" + target_id_);
if (gb28181_agent_ != null ) {
gb28181_agent_.respondBroadcastCommand(from_user_name_, from_user_name_at_domain_,sn_,source_id_, target_id_, true);
btnGB28181AudioBroadcast.setText("收到GB28181语音广播通知");
}
}
private String from_user_name_;
private String from_user_name_at_domain_;
private String sn_;
private String source_id_;
private String target_id_;
public Runnable set(String from_user_name, String from_user_name_at_domain, String sn, String source_id, String target_id) {
this.from_user_name_ = from_user_name;
this.from_user_name_at_domain_ = from_user_name_at_domain;
this.sn_ = sn;
this.source_id_ = source_id;
this.target_id_ = target_id;
return this;
}
}.set(fromUserName, fromUserNameAtDomain, sn, sourceID, targetID),0);
}


ntsOnAudioBroadcast处理:

@Override
public void ntsOnAudioBroadcast(String commandFromUserName, String commandFromUserNameAtDomain, String sourceID, String targetID) {
  handler_.postDelayed(new Runnable() {
    @Override
    public void run() {
      Log.i(TAG, "ntsOnAudioBroadcastPlay, fromFromUserName:" + command_from_user_name_
            + " FromUserNameAtDomain:" + command_from_user_name_at_domain_
            + " sourceID:" + source_id_ + ", targetID:" + target_id_);
      stopAudioPlayer();
      destoryRTPReceiver();
      if (gb28181_agent_ != null ) {
        String local_ip_addr = IPAddrUtils.getIpAddress(context_);
        boolean is_tcp = true; // 考虑到跨网段, 默认用TCP传输rtp包
        rtp_receiver_handle_ = lib_player_.CreateRTPReceiver(0);
        if (rtp_receiver_handle_ != 0 ) {
          lib_player_.SetRTPReceiverTransportProtocol(rtp_receiver_handle_, is_tcp?1:0);
          lib_player_.SetRTPReceiverIPAddressType(rtp_receiver_handle_, 0);
          if (0 == lib_player_.CreateRTPReceiverSession(rtp_receiver_handle_, 0) ) {
            int local_port = lib_player_.GetRTPReceiverLocalPort(rtp_receiver_handle_);
            boolean ret = gb28181_agent_.inviteAudioBroadcast(command_from_user_name_,command_from_user_name_at_domain_,
                                                              source_id_, target_id_, "IP4", local_ip_addr, local_port, is_tcp?"TCP/RTP/AVP":"RTP/AVP");
            if (!ret ) {
              destoryRTPReceiver();
              btnGB28181AudioBroadcast.setText("GB28181语音广播");
            }
            else {
              btnGB28181AudioBroadcast.setText("GB28181语音广播呼叫中");
            }
          } else {
            destoryRTPReceiver();
            btnGB28181AudioBroadcast.setText("GB28181语音广播");
          }
        }
      }
    }
    private String command_from_user_name_;
    private String command_from_user_name_at_domain_;
    private String source_id_;
    private String target_id_;
    public Runnable set(String command_from_user_name, String command_from_user_name_at_domain, String source_id, String target_id) {
      this.command_from_user_name_ = command_from_user_name;
      this.command_from_user_name_at_domain_ = command_from_user_name_at_domain;
      this.source_id_ = source_id;
      this.target_id_ = target_id;
      return this;
    }
  }.set(commandFromUserName, commandFromUserNameAtDomain, sourceID, targetID),0);
}


Broadcast Response处理:

    @Override
    public void ntsOnInviteAudioBroadcastResponse(String sourceID, String targetID, int statusCode, SessionDescription sessionDescription) {
        handler_.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.i(TAG, "ntsOnInviteAudioBroadcastResponse, statusCode:" + status_code_ +" sourceID:" + source_id_ + ", targetID:" + target_id_);
                boolean is_need_destory_rtp = true;
                if (gb28181_agent_ != null ) {
                    boolean is_need_bye = 200==status_code_;
                    if (200 == status_code_ && session_description_ != null && rtp_receiver_handle_ != 0 ) {
                        MediaSessionDescription audio_des = null;
                        List<SDPRtpMapAttribute> audio_attrs = new LinkedList<>();
                        Vector<MediaSessionDescription> audio_des_list = session_description_.getAudioDescriptions();
                        if (audio_des_list != null && !audio_des_list.isEmpty() ) {
                            for (MediaSessionDescription m : audio_des_list) {
                                if (m != null && m.isValidAddressType() && m.isHasAddress() && m.isHasRtpMapAttribute()) {
                                    audio_attrs.clear();
                                    Vector<SDPRtpMapAttribute> rtp_maps = m.getRtpMapAttributes();
                                    for (SDPRtpMapAttribute a : rtp_maps) {
                                        int type = a.getPayloadType();
                                        String name = a.getEncodingName();
                                        if (0 == type || 8 == type)
                                            audio_attrs.add(a);
                                        else if (name != null && !name.isEmpty()) {
                                            if (name.equals("PS") || name.equals("PCMA") || name.equals("PCMU"))
                                                audio_attrs.add(a);
                                        }
                                    }
                                    if (!audio_attrs.isEmpty()) {
                                        audio_des = m;
                                        break;
                                    }
                                }
                            }
                        }
                        if (audio_des != null && !audio_attrs.isEmpty() ) {
                            // 有些场景下 SDP.SSRC 和 RTP.SSRC 不相等, 对于这种情况,不要设置SSRC给SDK, 屏蔽掉下面这行设置SSRC的代码
                            lib_player_.SetRTPReceiverSSRC(rtp_receiver_handle_, audio_des.getSSRC());
                            ....
                            lib_player_.SetRTPReceiverRemoteAddress(rtp_receiver_handle_, audio_des.getAddress(), audio_des.getPort());
                            lib_player_.InitRTPReceiver(rtp_receiver_handle_);
                            if (startAudioPlay()) {
                                is_need_bye = false;
                                is_need_destory_rtp = false;
                                gb_broadcast_source_id_ = source_id_;
                                gb_broadcast_target_id_ = target_id_;
                                btnGB28181AudioBroadcast.setText("终止GB28181语音广播");
                                btnGB28181AudioBroadcast.setEnabled(true);
                            }
                        }
                    } else {
                        btnGB28181AudioBroadcast.setText("GB28181语音广播");
                    }
                    if (is_need_bye)
                        gb28181_agent_.byeAudioBroadcast(source_id_, target_id_);
                }
                if (is_need_destory_rtp)
                    destoryRTPReceiver();
            }
            private String source_id_;
            private String target_id_;
            private int status_code_;
            private SessionDescription session_description_;
            public Runnable set(String source_id, String target_id, int status_code, SessionDescription session_description) {
                this.source_id_ = source_id;
                this.target_id_ = target_id;
                this.status_code_ = status_code;
                this.session_description_ = session_description;
                return this;
            }
        }.set(sourceID, targetID, statusCode, sessionDescription),0);
    }


需要注意的是,以上述GB28181平台厂商为例,尽管SDP协商的是PCMA,实际上,平台侧下发的是PS的audio数据,如果不设置PS下去,会有以下日志:

2023-07-18 14:30:05.193 9248-9574/com.smartpublisher.camera2demo I/NTLogAndroid: NTRTP readSource: received rtp packet, is_udp:0, payload_type=96, len=232, t:0, sn=0, ssrc=200009727, src_address:0.0.0.0:0\
2023-07-18 14:30:05.193 9248-9574/com.smartpublisher.camera2demo E/NTLogAndroid: NTRTP readSource could not find configuration for payload type:96\
2023-07-18 14:30:05.193 9248-9574/com.smartpublisher.camera2demo E/NTLogAndroid: NTRTP readSource could not find configuration for payload type:96\
2023-07-18 14:30:05.193 9248-9574/com.smartpublisher.camera2demo E/NTLogAndroid: NTRTP readSource could not find configuration for payload type:96\
2023-07-18 14:30:05.193 9248-9574/com.smartpublisher.camera2demo E/NTLogAndroid: NTRTP readSource could not find configuration for payload type:96\
2023-07-18 14:30:05.193 9248-9574/com.smartpublisher.camera2demo E/NTLogAndroid: NTRTP readSource could not find configuration for payload type:96\
2023-07-18 14:30:05.193 9248-9574/com.smartpublisher.camera2demo E/NTLogAndroid: NTRTP readSource could not find configuration for payload type:96\
2023-07-18 14:30:05.250 9248-9574/com.smartpublisher.camera2demo E/NTLogAndroid: NTRTP readSource could not find configuration for payload type:96\
2023-07-18 14:30:05.277 9248-9574/com.smartpublisher.camera2demo E/NTLogAndroid: NTRTP readSource could not find configuration for payload type:96\
2023-07-18 14:30:05.277 9248-9574/com.smartpublisher.camera2demo E/NTLogAndroid: NTRTP readSource could not find configuration for payload type:96\


所以通用的做法是判断SDP里面有没有PS,如果没有,设置RTP Receiver Payload Type下去:

60e5d9c4ed3443b4a6e0a483cf9760f0.jpg

总结

Android平台GB28181设备接入侧为什么没有公司愿意做?真的是坑太多,GB28181厂商太多,好多厂商包括大厂商并没有严格按照规范来,简单来说,50%的精力写代码,50%的精力查问题和各种兼容处理。

相关文章
|
3天前
|
开发工具 Android开发 数据安全/隐私保护
探索iOS与安卓应用开发的异同:技术、工具和市场趋势
在移动操作系统的广阔舞台上,iOS和安卓两大主角各自演绎着怎样的精彩?本文将深入剖析这两大平台在应用开发过程中的技术差异、开发工具的选择以及面对的市场环境。通过数据支撑和案例分析,我们将一窥这两个系统如何影响开发者的决策,并探讨它们未来的发展方向。
|
5天前
|
存储 缓存 Java
Android性能优化:内存管理与LeakCanary技术详解
【7月更文挑战第21天】内存管理是Android性能优化的关键部分,而LeakCanary则是进行内存泄漏检测和修复的强大工具。
|
23天前
|
机器学习/深度学习 人工智能 文字识别
文本,文字扫描01,OCR文本识别技术展示,一个安卓App,一个简单的设计,文字识别可以应用于人工智能,机器学习,车牌识别,身份证识别,银行卡识别,PaddleOCR+SpringBoot+Andr
文本,文字扫描01,OCR文本识别技术展示,一个安卓App,一个简单的设计,文字识别可以应用于人工智能,机器学习,车牌识别,身份证识别,银行卡识别,PaddleOCR+SpringBoot+Andr
|
1天前
|
Android开发 数据安全/隐私保护 iOS开发
探索未来:安卓与iOS在智能穿戴设备领域的较量
随着科技的飞速发展,智能穿戴设备已逐渐成为我们日常生活的一部分。从健康监测到通讯交流,它们正以惊人的速度改变着我们的生活方式。本文将深入探讨安卓和iOS这两大操作系统在智能穿戴领域的现状、竞争以及未来发展趋势,揭示它们如何通过创新技术满足用户需求,并预测未来可能的发展方向。
3 0
|
18天前
|
安全 搜索推荐 Android开发
安卓与iOS的较量:技术深度与用户体验的博弈
在数字时代的浪潮中,安卓与iOS两大操作系统如同巨人般主宰着移动设备的生态。本文将深入探讨这两大系统在技术架构、安全性、应用生态和用户体验等方面的差异,并结合最新的市场数据和技术发展动态,为读者呈现一个全面而深入的分析视角。我们将看到,尽管安卓以其开放性和高度可定制性赢得了广泛的市场份额,iOS则以其卓越的用户体验和强大的品牌忠诚度构建了坚固的护城河。两者的竞争不仅推动了技术的快速进步,也不断塑造着我们对智能手机的期待和想象。
|
16天前
|
开发工具 Android开发 iOS开发
探索Android与iOS开发的差异与挑战
【7月更文挑战第11天】在移动应用开发的广阔天地中,Android和iOS两大平台如同双子星座般耀眼,各自拥有独特的开发生态和用户群体。本文将深入分析这两个平台的显著差异,从技术架构到开发工具,再到市场定位,揭示它们之间的异同。通过比较,我们不仅能够更好地理解各自的优势和局限,还能洞察未来移动应用开发的趋势。
|
12天前
|
Android开发 Kotlin
kotlin开发安卓app,如何让布局自适应系统传统导航和全面屏导航
使用`navigationBarsPadding()`修饰符实现界面自适应,自动处理底部导航栏的内边距,再加上`.padding(bottom = 10.dp)`设定内容与屏幕底部的距离,以完成全面的布局适配。示例代码采用Kotlin。
57 15
|
3天前
|
前端开发 Android开发 iOS开发
探索安卓与iOS开发的差异性与互补性
在移动应用开发的广阔舞台上,安卓和iOS这两大操作系统各据一方,引领着市场潮流。它们在技术架构、开发环境及用户群体等方面展现出独特的差异性,同时也存在着潜在的互补性。本文将深入剖析这两种平台的开发细节,从不同角度揭示其各自优势及相互之间的协同潜力,为开发者提供全面而深刻的视角。
10 2
|
10天前
|
Java Android开发 iOS开发
探索安卓与iOS开发的差异性与互操作性
【7月更文挑战第17天】在移动应用开发的广阔天地中,安卓和iOS这两大操作系统如同双子星座般璀璨夺目。它们各自拥有独特的开发环境、编程语言和用户群体,为开发者提供了不同的挑战和机遇。本文将从多个维度深入剖析安卓与iOS开发的差异性,并探讨它们之间的互操作性如何实现,以期为开发者们提供一份实用的指南。
24 7
|
8天前
|
Java Android开发 Swift
探索iOS与安卓开发的差异与挑战
本文深入探讨了iOS和安卓两大移动操作系统在应用开发领域的不同点及其所面临的挑战。通过对开发环境、编程语言、用户界面设计、性能优化及市场策略的比较分析,揭示了各自平台的独特性以及开发者需要克服的技术与市场障碍。 【7月更文挑战第19天】