这里写自定义目录标题
业务需求
业务需求是与一款安全帽设备进行SIP连接,搭建语音通道。
SIPSorcery SDK
可以在GitHub上找到,里面有案例和文档
流程和代码
流程如下:
UserClientAgent发起会话,等待目标设备Answer。收到OK后,通道建立。按服务端要求,定期回复心跳协议,发送心跳消息。
发起绘画需要构造request消息,要点如下:
1、构造URI
sip:目标设备ID@SIP服务器地址:SIP服务器端口;其他信息
session-expires用来保活sip连接,服务器会默认session-expires这个参数为120s,我们web端采用的是3600s
SIPURI callUri = SIPURI.ParseSIPURI("sip:" + phoneNum + "@" +sipServer + ":" + sipPort + ";transport=tcp;session-expires=3600");
2、构造SIPTransport
var sipTransport = new SIPTransport();
sipTransport.PreferIPv6NameResolution = true;
sipTransport.EnableTraceLogs();
3、构造MediaSession用来处理音视频流数据
var audioSession = new WindowsAudioEndPoint(new AudioEncoder());
audioSession.RestrictFormats(x => x.Codec == AudioCodecsEnum.PCMA || x.Codec == AudioCodecsEnum.PCMU);
//audioSession.RestrictFormats(x => x.Codec == AudioCodecsEnum.G722);
rtpSession = new VoIPMediaSession(audioSession.ToMediaEndPoints());
var offerSDP = rtpSession.CreateOffer(IPAddress.Any);
4、构造请求描述
// Start the thread that places the call.
SIPCallDescriptor callDescriptor = new SIPCallDescriptor(
sipAccount,
sipPwd,
callUri.ToString(),
"sip:" + sipAccount + "@" + sipServer + ";transport=tcp",
callUri.ToString(),
sipServer, null,
sipAccount,
SIPCallDirection.Out,
SDP.SDP_MIME_CONTENTTYPE,
offerSDP.ToString(),
null
)
{
AccountCode = HelmetMgr.Instance().sipAccount };
5、构造UserClientAgent并发起Call
//ManualResetEvent exitMre = new ManualResetEvent(false);
bool isCallHungup = false;
bool hasCallFailed