/// <summary> /// Invite请求消息 /// </summary> /// <param name="localEP"></param> /// <param name="remoteEP"></param> /// <param name="request"></param> private void InviteHandle(SIPEndPoint localEP, SIPEndPoint remoteEP, SIPRequest request) { _byeRemoteEP = remoteEP; int[] port = SetMediaPort(); var trying = GetResponse(localEP, remoteEP, SIPResponseStatusCodesEnum.Trying, "", request); Transport.SendResponse(trying); //Thread.Sleep(200); SIPResponse audioOK = GetResponse(localEP, remoteEP, SIPResponseStatusCodesEnum.Ok, "", request); audioOK.Header.ContentType = "application/sdp"; SIPURI localUri = new SIPURI(LocalSIPId, LocalEP.ToHost(), ""); SIPContactHeader contact = new SIPContactHeader(null, localUri); audioOK.Header.Contact = new List <SIPContactHeader>(); audioOK.Header.Contact.Add(contact); //SDP audioOK.Body = SetMediaAudio(localEP.Address.ToString(), port[0], request.URI.User); _audioResponse = audioOK; Transport.SendResponse(audioOK); int recvPort = GetReceivePort(request.Body, SDPMediaTypesEnum.audio); string ip = GetReceiveIP(request.Body); _audioRemoteEP = new IPEndPoint(IPAddress.Parse(ip), recvPort); if (_audioChannel == null) { _audioChannel = new UDPChannel(TcpConnectMode.active, IPAddress.Any, port, ProtocolType.Udp, false, recvPort); } _audioChannel.Start(); }
/// <summary> /// 注册消息处理 /// </summary> /// <param name="localEP">本地终结点</param> /// <param name="remoteEP">远程终结点</param> /// <param name="request">sip请求</param> private void RegisterHandle(SIPEndPoint localEP, SIPEndPoint remoteEP, SIPRequest request) { OnSIPServiceChange(remoteEP.ToHost(), ServiceStatus.Complete); lock (_remoteTransEPs) { if (!_remoteTransEPs.ContainsKey(remoteEP.ToHost())) { _remoteTransEPs.Add(remoteEP.ToHost(), request.Header.From.FromURI.User); } } _registrarCore.AddRegisterRequest(localEP, remoteEP, request); }
/// <summary> /// 目录查询响应消息处理 /// </summary> /// <param name="localEP">本地终结点</param> /// <param name="remoteEP">远程终结点</param> /// <param name="request">sip请求</param> /// <param name="catalog">目录结构体</param> private void CatalogHandle(SIPEndPoint localEP, SIPEndPoint remoteEP, SIPRequest request, Catalog catalog) { try { catalog.DeviceList.Items.FindAll(item => item != null).ForEach(catalogItem => { catalogItem.RemoteEP = remoteEP.ToHost(); var devCata = DevType.GetCataType(catalogItem.DeviceID); //logger.Debug("CatalogHandle: DevCataType=" + devCata); if (devCata == DevCataType.Device) { if (!_nodeMonitorService.ContainsKey(catalogItem.DeviceID)) { //remoteEP.Port = _LocalSipAccount.RemotePort; _nodeMonitorService.TryAdd(catalogItem.DeviceID, new SIPMonitorCore(this, _transport, _sipAccountStorage) { RemoteEndPoint = remoteEP, DeviceId = catalogItem.DeviceID }); logger.Debug("CatalogHandle: nodeMonitorService.Count=" + _nodeMonitorService.Count); logger.Debug("CatalogHandle: nodeMonitorService.TryAdd DeviceId=" + catalogItem.DeviceID); } } // CommandType cmdType = i == 0 ? CommandType.Play : CommandType.Playback; }); OnCatalogReceived?.Invoke(catalog); } catch (Exception ex) { logger.Warn("CatalogHandle Exception: " + ex.Message); } }
/// <summary> /// 目录订阅请求 /// </summary> /// <returns></returns> private SIPRequest SubscribeCatalog(SIPEndPoint remoteEndPoint, string remoteSIPId) { string fromTag = CallProperties.CreateNewTag(); int cSeq = CallProperties.CreateNewCSeq(); string callId = CallProperties.CreateNewCallId(); SIPURI remoteUri = new SIPURI(remoteSIPId, remoteEndPoint.ToHost(), ""); SIPURI localUri = new SIPURI(LocalSIPId, LocalEP.ToHost(), ""); SIPFromHeader from = new SIPFromHeader(null, localUri, fromTag); SIPToHeader to = new SIPToHeader(null, remoteUri, null); SIPRequest catalogReq = _transport.GetRequest(SIPMethodsEnum.SUBSCRIBE, remoteUri); catalogReq.Header.From = from; catalogReq.Header.Contact = new List <SIPContactHeader> { new SIPContactHeader(null, localUri) }; catalogReq.Header.Allow = null; catalogReq.Header.To = to; catalogReq.Header.UserAgent = SIPConstants.SIP_USERAGENT_STRING; catalogReq.Header.Event = "Catalog";//"presence";//"Catalog;id=1894"; catalogReq.Header.Expires = 60000; catalogReq.Header.CSeq = cSeq; catalogReq.Header.CallId = callId; catalogReq.Header.ContentType = "application/MANSCDP+xml";//"Application/MANSCDP+xml" return(catalogReq); }
/// <summary> /// 前端设备信息请求 /// </summary> /// <param name="cameraId"></param> /// <returns></returns> private SIPRequest DeviceReq(string cameraId) { SIPURI remoteUri = new SIPURI(cameraId, _remoteEndPoint.ToHost(), ""); SIPURI localUri = new SIPURI(_sipAccount.LocalSipId, _localEndPoint.ToHost(), ""); SIPFromHeader from = new SIPFromHeader(null, localUri, CallProperties.CreateNewTag()); SIPToHeader to = new SIPToHeader(null, remoteUri, null); SIPRequest queryReq = _m_sipTransport.GetRequest(SIPMethodsEnum.DO, remoteUri); queryReq.Header.Contact = null; queryReq.Header.From = from; queryReq.Header.Allow = null; queryReq.Header.To = to; queryReq.Header.CSeq = CallProperties.CreateNewCSeq(); queryReq.Header.CallId = CallProperties.CreateNewCallId(); queryReq.Header.ContentType = "Application/DDCP"; return(queryReq); }
private SIPRequest ByeRequest() { SIPURI uri = new SIPURI(_cameraId, _remoteEndPoint.ToHost(), ""); SIPRequest byeRequest = _m_sipTransport.GetRequest(SIPMethodsEnum.BYE, uri); SIPFromHeader from = new SIPFromHeader(null, _sipRequest.URI, _realReqSession.Header.From.FromTag); SIPHeader header = new SIPHeader(from, byeRequest.Header.To, _realReqSession.Header.CSeq, _realReqSession.Header.CallId); header.ContentType = "application/DDCP"; header.Expires = byeRequest.Header.Expires; header.CSeqMethod = byeRequest.Header.CSeqMethod; header.Vias = byeRequest.Header.Vias; header.MaxForwards = byeRequest.Header.MaxForwards; header.UserAgent = _userAgent; byeRequest.Header.From = from; byeRequest.Header = header; return(byeRequest); }
/// <summary> /// 发送sip请求消息 /// </summary> /// <param name="remoteEP">远程终结点</param> /// <param name="request">sip请求</param> public void SendRequest(SIPEndPoint remoteEP, SIPRequest request) { if (_serviceState == ServiceStatus.Wait) { OnSIPServiceChange(remoteEP.ToHost(), ServiceStatus.Wait); return; } _transport.SendRequest(remoteEP, request); }
/// <summary> /// 心跳消息处理 /// </summary> /// <param name="localEP">本地终结点</param> /// <param name="remoteEP">远程终结点</param> /// <param name="request">sip请求</param> /// <param name="keepAlive">心跳结构体</param> private void KeepaliveHandle(SIPEndPoint localEP, SIPEndPoint remoteEP, SIPRequest request, KeepAlive keepAlive) { //SendResponse(localEP, remoteEP, request); OnSIPServiceChange(remoteEP.ToHost(), ServiceStatus.Complete); lock (_remoteTransEPs) { if (!_remoteTransEPs.ContainsKey(remoteEP.ToHost())) { _remoteTransEPs.Add(remoteEP.ToHost(), request.Header.From.FromURI.User); } } //if (!_subscribe) //{ // DeviceCatalogSubscribe(remoteEP, request.Header.From.FromURI.User); //} OnKeepaliveReceived?.Invoke(remoteEP, keepAlive, request.Header.From.FromURI.User); // _subscribe = true; logger.Debug("KeepAlive:" + remoteEP.ToHost() + "=====DevID:" + keepAlive.DeviceID + "=====Status:" + keepAlive.Status + "=====SN:" + keepAlive.SN); }
/// <summary> /// 发送sip响应消息 /// </summary> /// <param name="localEP">本地终结点</param> /// <param name="remoteEP">远程终结点</param> /// <param name="request">sip请求</param> private void SendResponse(SIPEndPoint localEP, SIPEndPoint remoteEP, SIPRequest request) { if (_serviceState == ServiceStatus.Wait) { OnSIPServiceChange(remoteEP.ToHost(), ServiceStatus.Wait); return; } SIPResponse res = GetResponse(localEP, remoteEP, SIPResponseStatusCodesEnum.Ok, "", request); _transport.SendResponse(res); }
/// <summary> /// 发送可靠的sip请求消息 /// </summary> /// <param name="remoteEP">远程终结点</param> /// <param name="request">sip请求消息</param> public void SendReliableRequest(SIPEndPoint remoteEP, SIPRequest request) { if (_serviceState == ServiceStatus.Wait) { OnSIPServiceChange(remoteEP.ToHost(), ServiceStatus.Wait); return; } var transaction = _transport.CreateUASTransaction(request, remoteEP, LocalEP, null); transaction.TransactionStateChanged += Trans_TransactionStateChanged; transaction.SendReliableRequest(); }
/// <summary> /// 确认接收视频请求 /// </summary> /// <param name="response">响应消息</param> /// <returns></returns> public void AckRequest(SIPResponse response) { _rtpChannel = new RTPChannel(_remoteEndPoint.GetIPEndPoint(), _mediaPort[0], _mediaPort[1], FrameTypesEnum.H264); _rtpChannel.OnFrameReady += _rtpChannel_OnFrameReady; _rtpChannel.Start(); SIPURI localUri = new SIPURI(_msgCore.LocalSIPId, _msgCore.LocalEndPoint.ToHost(), ""); SIPURI remoteUri = new SIPURI(_deviceId, _remoteEndPoint.ToHost(), ""); SIPRequest ackReq = _msgCore.Transport.GetRequest(SIPMethodsEnum.ACK, remoteUri); SIPFromHeader from = new SIPFromHeader(null, response.Header.From.FromURI, response.Header.From.FromTag); SIPToHeader to = new SIPToHeader(null, remoteUri, response.Header.To.ToTag); SIPContactHeader contactHeader = new SIPContactHeader(null, localUri); SIPHeader header = new SIPHeader(from, to, response.Header.CSeq, response.Header.CallId); header.CSeqMethod = SIPMethodsEnum.ACK; header.Contact = response.Header.Contact; header.Contact.Clear(); header.Contact.Add(contactHeader); header.Vias = response.Header.Vias; header.MaxForwards = response.Header.MaxForwards; header.ContentLength = response.Header.ContentLength; header.UserAgent = _msgCore.UserAgent; header.Allow = null; ackReq.Header = header; _okTag = response.Header.To.ToTag; _contact = header.Contact.FirstOrDefault(); _via = header.Vias; _msgCore.Transport.SendRequest(_remoteEndPoint, ackReq); }
/// <summary> /// 初始化远程sip /// </summary> /// <param name="localEndPoint">本地终结点</param> /// <param name="remoteEndPoint">远程终结点</param> /// <param name="request">sip请求</param> private void SIPTransportInit(SIPEndPoint localEndPoint, SIPEndPoint remoteEndPoint, SIPRequest request) { lock (RemoteTrans) { if (!RemoteTrans.ContainsKey(remoteEndPoint.ToString())) { Console.ForegroundColor = ConsoleColor.Green; RemoteTrans.Add(remoteEndPoint.ToString(), request.Header.From.FromURI.User); logger.Debug("RemoteTrans Init:Remote:" + remoteEndPoint.ToHost() + "-----User:" + request.Header.From.FromURI.User); Console.ForegroundColor = ConsoleColor.White; } } }
/// <summary> /// 查询设备目录请求 /// </summary> /// <returns></returns> private SIPRequest QueryItems(string fromTag, string toTag, int cSeq, string callId) { SIPURI remoteUri = new SIPURI(RemoteSIPId, RemoteEndPoint.ToHost(), ""); SIPURI localUri = new SIPURI(LocalSIPId, LocalEndPoint.ToHost(), ""); SIPFromHeader from = new SIPFromHeader(null, localUri, fromTag); SIPToHeader to = new SIPToHeader(null, remoteUri, toTag); SIPRequest catalogReq = Transport.GetRequest(SIPMethodsEnum.MESSAGE, remoteUri); catalogReq.Header.From = from; catalogReq.Header.Contact = null; catalogReq.Header.Allow = null; catalogReq.Header.To = to; catalogReq.Header.UserAgent = UserAgent; catalogReq.Header.CSeq = cSeq; catalogReq.Header.CallId = callId; catalogReq.Header.ContentType = "application/MANSCDP+xml"; return(catalogReq); }
public override void AddMessageResponse(SIPEndPoint localSIPEndPoint, SIPEndPoint remoteEndPoint, SIPResponse response) { if (response.Status == SIPResponseStatusCodesEnum.Unauthorised) { logger.Info("需要提供身份认证字段" + remoteEndPoint.ToHost()); //安全身份认证 SIPAuthorisationDigest digest = new SIPAuthorisationDigest(SIPAuthorisationHeadersEnum.Authorize); digest.Username = response.Header.AuthenticationHeader.SIPDigest.Username; digest.Password = "******"; digest.Realm = response.Header.AuthenticationHeader.SIPDigest.Realm; digest.Nonce = response.Header.AuthenticationHeader.SIPDigest.Nonce; digest.Response = digest.Digest; string md5Pass = digest.ToString(); SIPAuthenticationHeader auth = new SIPAuthenticationHeader(digest); _auth = auth; RegisterToPlatform(); } }
/// <summary> /// 目录查询响应消息处理 /// </summary> /// <param name="localEP">本地终结点</param> /// <param name="remoteEP">远程终结点</param> /// <param name="request">sip请求</param> /// <param name="catalog">目录结构体</param> private void CatalogHandle(SIPEndPoint localEP, SIPEndPoint remoteEP, SIPRequest request, Catalog catalog) { foreach (var cata in catalog.DeviceList.Items) { if (cata == null) { continue; } cata.RemoteEP = remoteEP.ToHost(); DevCataType devCata = DevType.GetCataType(cata.DeviceID); if (devCata != DevCataType.Device) { continue; } for (int i = 0; i < 2; i++) { CommandType cmdType = i == 0 ? CommandType.Play : CommandType.Playback; MonitorKey key = new MonitorKey() { DeviceID = cata.DeviceID, CmdType = cmdType }; lock (MonitorService) { if (MonitorService.ContainsKey(key)) { continue; } remoteEP.Port = _account.KeepaliveInterval; ISIPMonitorService monitor = new SIPMonitorCore(this, cata.DeviceID, remoteEP, _account); MonitorService.Add(key, monitor); } } } if (OnCatalogReceived != null) { OnCatalogReceived(catalog); } }
/// <summary> /// 目录查询响应消息处理 /// </summary> /// <param name="localEP">本地终结点</param> /// <param name="remoteEP">远程终结点</param> /// <param name="request">sip请求</param> /// <param name="catalog">目录结构体</param> private void CatalogHandle(SIPEndPoint localEP, SIPEndPoint remoteEP, SIPRequest request, Catalog catalog) { catalog.DeviceList.Items.FindAll(item => item != null).ForEach(catalogItem => { catalogItem.RemoteEP = remoteEP.ToHost(); var devCata = DevType.GetCataType(catalogItem.DeviceID); if (devCata != DevCataType.Device) { if (!_nodeMonitorService.ContainsKey(catalogItem.DeviceID)) { remoteEP.Port = _LocalSipAccount.KeepaliveInterval; _nodeMonitorService.TryAdd(catalogItem.DeviceID, new SIPMonitorCoreService(this, _transport, _sipAccountStorage) { RemoteEndPoint = remoteEP, DeviceId = catalogItem.DeviceID }); } } // CommandType cmdType = i == 0 ? CommandType.Play : CommandType.Playback; }); OnCatalogReceived?.Invoke(catalog); }
/// <summary> /// 查询设备目录请求 /// </summary> /// <returns></returns> private SIPRequest QueryItems(SIPEndPoint remoteEndPoint, string remoteSIPId) { string fromTag = CallProperties.CreateNewTag(); string toTag = CallProperties.CreateNewTag(); int cSeq = CallProperties.CreateNewCSeq(); string callId = CallProperties.CreateNewCallId(); SIPURI remoteUri = new SIPURI(remoteSIPId, remoteEndPoint.ToHost(), ""); SIPURI localUri = new SIPURI(LocalSIPId, LocalEP.ToHost(), ""); SIPFromHeader from = new SIPFromHeader(null, localUri, fromTag); SIPToHeader to = new SIPToHeader(null, remoteUri, null); SIPRequest catalogReq = _transport.GetRequest(SIPMethodsEnum.MESSAGE, remoteUri); catalogReq.Header.From = from; catalogReq.Header.Contact = null; catalogReq.Header.Allow = null; catalogReq.Header.To = to; catalogReq.Header.UserAgent = SIPConstants.SIP_USERAGENT_STRING; catalogReq.Header.CSeq = cSeq; catalogReq.Header.CallId = callId; catalogReq.Header.ContentType = "application/MANSCDP+xml"; return(catalogReq); }
/// <summary> /// sip请求消息 /// </summary> /// <param name="localEndPoint">本地终结点</param> /// <param name="remoteEndPoint"b>远程终结点</param> /// <param name="request">sip请求</param> public virtual void AddMessageRequest(SIPEndPoint localEndPoint, SIPEndPoint remoteEndPoint, SIPRequest request) { MessageCore[remoteEndPoint.ToHost()].AddMessageRequest(localEndPoint, remoteEndPoint, request); }
/// <summary> /// sip响应消息 /// </summary> /// <param name="localSIPEndPoint">本地终结点</param> /// <param name="remoteEndPoint">远程终结点</param> /// <param name="response">sip响应</param> public virtual void AddMessageResponse(SIPEndPoint localSIPEndPoint, SIPEndPoint remoteEndPoint, SIPResponse response) { MessageCore[remoteEndPoint.ToHost()].AddMessageResponse(localSIPEndPoint, remoteEndPoint, response); }
public void ByeVideoReq() { if (_audioChannel == null) { return; } _audioChannel.Stop(); _audioChannel = null; SIPURI localUri = new SIPURI(_LocalSipAccount.SIPPassword, LocalEP.ToHost(), ""); SIPURI remoteUri = new SIPURI(_ackRequest.Header.From.FromURI.User, _byeRemoteEP.ToHost(), ""); SIPFromHeader from = new SIPFromHeader(null, localUri, _ackRequest.Header.To.ToTag); SIPToHeader to = new SIPToHeader(null, remoteUri, _ackRequest.Header.From.FromTag); SIPRequest byeReq = new SIPRequest(SIPMethodsEnum.BYE, localUri); SIPHeader header = new SIPHeader(from, to, _ackRequest.Header.CSeq + 1, _ackRequest.Header.CallId) { CSeqMethod = SIPMethodsEnum.BYE }; SIPViaHeader viaHeader = new SIPViaHeader(LocalEP, CallProperties.CreateBranchId()) { Branch = CallProperties.CreateBranchId(), Transport = SIPProtocolsEnum.udp }; SIPViaSet viaSet = new SIPViaSet { Via = new List <SIPViaHeader>() }; viaSet.Via.Add(viaHeader); header.Vias = viaSet; header.UserAgent = SIPConstants.SIP_USERAGENT_STRING; header.Contact = _ackRequest.Header.Contact; byeReq.Header = header; SendRequest(_byeRemoteEP, byeReq); }
/// <summary> /// sip请求消息 /// </summary> /// <param name="localEndPoint">本地终结点</param> /// <param name="remoteEndPoint"b>远程终结点</param> /// <param name="request">sip请求</param> public void AddMessageRequest(SIPEndPoint localEndPoint, SIPEndPoint remoteEndPoint, SIPRequest request) { //注册请求 if (request.Method == SIPMethodsEnum.REGISTER) { m_registrarCore.AddRegisterRequest(localEndPoint, remoteEndPoint, request); SIPTransportInit(localEndPoint, remoteEndPoint, request); } //消息请求 else if (request.Method == SIPMethodsEnum.MESSAGE) { SIPTransportInit(localEndPoint, remoteEndPoint, request); KeepAlive keepAlive = KeepAlive.Instance.Read(request.Body); if (keepAlive != null && keepAlive.CmdType == CommandType.Keepalive) //心跳 { //if (!_initSIP) //{ //LocalEndPoint = request.Header.To.ToURI.ToSIPEndPoint(); //RemoteEndPoint = request.Header.From.FromURI.ToSIPEndPoint(); //LocalSIPId = request.Header.To.ToURI.User; //RemoteSIPId = request.Header.From.FromURI.User; //} //_initSIP = true; logger.Debug("KeepAlive:" + remoteEndPoint.ToHost() + "=====DevID:" + keepAlive.DeviceID + "=====Status:" + keepAlive.Status + "=====SN:" + keepAlive.SN); OnSIPServiceChange(remoteEndPoint.ToHost(), SipServiceStatus.Complete); } else { Catalog catalog = Catalog.Instance.Read(request.Body); if (catalog != null && catalog.CmdType == CommandType.Catalog) //设备目录 { foreach (var cata in catalog.DeviceList.Items) { //cata.RemoteEP = request.Header.From.FromURI.Host; cata.RemoteEP = remoteEndPoint.ToHost(); for (int i = 0; i < 2; i++) { CommandType cmdType = CommandType.Unknown; if (i == 0) { cmdType = CommandType.Play; } else { cmdType = CommandType.Playback; } string key = cata.DeviceID + cmdType; lock (MonitorService) { if (MonitorService.ContainsKey(key)) { continue; } ISIPMonitorService monitor = new SIPMonitorCore(this, cata.DeviceID, cata.Name, remoteEndPoint); monitor.OnSIPServiceChanged += monitor_OnSIPServiceChanged; MonitorService.Add(key, monitor); } } } if (OnCatalogReceived != null) { OnCatalogReceived(catalog); } } RecordInfo record = RecordInfo.Instance.Read(request.Body); if (record != null && record.CmdType == CommandType.RecordInfo) //录像检索 { lock (MonitorService) { MonitorService[record.DeviceID + CommandType.Playback].RecordQueryTotal(record.SumNum); } if (OnRecordInfoReceived != null && record.RecordItems != null) { OnRecordInfoReceived(record); } } } SIPResponse msgRes = GetResponse(localEndPoint, remoteEndPoint, SIPResponseStatusCodesEnum.Ok, "", request); Transport.SendResponse(msgRes); } //停止播放请求 else if (request.Method == SIPMethodsEnum.BYE) { SIPResponse byeRes = GetResponse(localEndPoint, remoteEndPoint, SIPResponseStatusCodesEnum.Ok, "", request); Transport.SendResponse(byeRes); } }