/// <summary> /// 设备目录查询 /// </summary> /// <param name="deviceId">目的设备编码</param> public void DeviceCatalogQuery(string deviceId) { if (LocalEndPoint == null) { OnSIPServiceChange(deviceId, SipServiceStatus.Wait); return; } string fromTag = CallProperties.CreateNewTag(); string toTag = CallProperties.CreateNewTag(); int cSeq = CallProperties.CreateNewCSeq(); string callId = CallProperties.CreateNewCallId(); SIPRequest catalogReq = QueryItems(fromTag, toTag, cSeq, callId); CatalogQuery catalog = new CatalogQuery() { CommandType = CommandType.Catalog, DeviceID = deviceId, SN = new Random().Next(9999) }; string xmlBody = CatalogQuery.Instance.Save <CatalogQuery>(catalog); catalogReq.Body = xmlBody; Transport.SendRequest(RemoteEndPoint, catalogReq); _catalogTask = new TaskTiming(catalogReq, Transport); this.SendRequestTimeout += _catalogTask.MessageSendRequestTimeout; _catalogTask.Start(); }
/// <summary> /// 暂停录像播放 /// </summary> public void BackVideoPauseControlReq() { if (_msgCore.LocalEndPoint == null) { OnSIPServiceChange(_deviceName + "-" + _deviceId, SipServiceStatus.Wait); return; } if (_mediaPort == null) { _mediaPort = _msgCore.SetMediaPort(); } string localIp = _msgCore.LocalEndPoint.Address.ToString(); string fromTag = CallProperties.CreateNewTag(); int cSeq = CallProperties.CreateNewCSeq(); string callId = CallProperties.CreateNewCallId(); //this.Stop(); SIPRequest realReq = BackVideoPauseControlReq(localIp, _mediaPort, fromTag, cSeq, callId); _msgCore.Transport.SendRequest(_msgCore.RemoteEndPoint, realReq); _realTask = new TaskTiming(realReq, _msgCore.Transport); _msgCore.SendRequestTimeout += _realTask.MessageSendRequestTimeout; _realTask.OnCloseRTPChannel += Task_OnCloseRTPChannel; _realTask.Start(); }
/// <summary> /// PTZ云台控制 /// </summary> /// <param name="deviceId">设备ID</param> /// <param name="ucommand">控制命令</param> /// <param name="dwStop">开始或结束</param> /// <param name="dwSpeed">速度</param> public void PtzContrl(string deviceId, int ucommand, int dwStop, int dwSpeed) { if (LocalEndPoint == null) { OnSIPServiceChange(deviceId, SipServiceStatus.Wait); return; } string fromTag = CallProperties.CreateNewTag(); string toTag = CallProperties.CreateNewTag(); int cSeq = CallProperties.CreateNewCSeq(); string callId = CallProperties.CreateNewCallId(); SIPRequest catalogReq = QueryItems(fromTag, toTag, cSeq, callId); string cmdStr = GetPtzCmd(ucommand, dwStop, dwSpeed); PTZControl ptz = new PTZControl() { CommandType = CommandType.DeviceControl, DeviceID = deviceId, SN = new Random().Next(9999), PTZCmd = cmdStr }; string xmlBody = PTZControl.Instance.Save <PTZControl>(ptz); catalogReq.Body = xmlBody; Transport.SendRequest(RemoteEndPoint, catalogReq); _catalogTask = new TaskTiming(catalogReq, Transport); this.SendRequestTimeout += _catalogTask.MessageSendRequestTimeout; _catalogTask.Start(); }
/// <summary> /// 录像点播请求 /// </summary> /// <param name="localIp">本地ip</param> /// <param name="mediaPort">端口号</param> /// <param name="startTime">开始时间</param> /// <param name="stopTime">结束时间</param> /// <param name="fromTag">from tag</param> /// <param name="cSeq">序号</param> /// <param name="callId">呼叫编号</param> /// <returns></returns> private SIPRequest BackVideoReq(int[] mediaPort, uint startTime, uint stopTime) { string localIp = _msgCore.LocalEndPoint.Address.ToString(); string fromTag = CallProperties.CreateNewTag(); int cSeq = CallProperties.CreateNewCSeq(); string callId = CallProperties.CreateNewCallId(); SIPURI remoteUri = new SIPURI(_deviceId, _remoteEndPoint.ToHost(), ""); SIPURI localUri = new SIPURI(_msgCore.LocalSIPId, _msgCore.LocalEndPoint.ToHost(), ""); SIPFromHeader from = new SIPFromHeader(null, localUri, fromTag); SIPToHeader to = new SIPToHeader(null, remoteUri, null); SIPRequest backReq = _msgCore.Transport.GetRequest(SIPMethodsEnum.INVITE, remoteUri); SIPContactHeader contactHeader = new SIPContactHeader(null, localUri); backReq.Header.Contact.Clear(); backReq.Header.Contact.Add(contactHeader); backReq.Header.Allow = null; backReq.Header.From = from; backReq.Header.To = to; backReq.Header.UserAgent = _msgCore.UserAgent; backReq.Header.CSeq = cSeq; backReq.Header.CallId = callId; backReq.Header.Subject = SetSubject(); backReq.Header.ContentType = "application/sdp"; backReq.Body = SetMediaReq(localIp, mediaPort, startTime, stopTime); _realReqSession = backReq; return(backReq); }
private SIPRequest GetAuthenticatedRequest(SIPRequest originalRequest, SIPResponse sipResponse) { try { SIPAuthorisationDigest digest = sipResponse.Header.AuthenticationHeader.SIPDigest; m_lastServerNonce = digest.Nonce; string username = (m_callDescriptor.AuthUsername != null) ? m_callDescriptor.AuthUsername : m_callDescriptor.Username; digest.SetCredentials(username, m_callDescriptor.Password, originalRequest.URI.ToString(), originalRequest.Method.ToString()); SIPRequest authRequest = originalRequest.Copy(); authRequest.SetSendFromHints(originalRequest.LocalSIPEndPoint); authRequest.Header.Vias.TopViaHeader.Branch = CallProperties.CreateBranchId(); authRequest.Header.From.FromTag = CallProperties.CreateNewTag(); authRequest.Header.To.ToTag = null; authRequest.Header.CallId = CallProperties.CreateNewCallId(); authRequest.Header.CSeq = originalRequest.Header.CSeq + 1; authRequest.Header.AuthenticationHeader = new SIPAuthenticationHeader(digest); authRequest.Header.AuthenticationHeader.SIPDigest.Response = digest.Digest; return(authRequest); } catch (Exception excp) { logger.LogError("Exception SIPNonInviteClientUserAgent GetAuthenticatedRequest. " + excp.Message); throw; } }
/// <summary> /// PTZ云台控制 /// </summary> /// <param name="ucommand">控制命令</param> /// <param name="dwStop">开始或结束</param> /// <param name="dwSpeed">速度</param> public void PtzContrl(int ucommand, int dwStop, int dwSpeed) { lock (_msgCore.RemoteTrans) { if (!_msgCore.RemoteTrans.ContainsKey(_remoteEndPoint.ToString())) { OnSIPServiceChange(_deviceName + "-" + _deviceId + _remoteEndPoint.ToString(), SipServiceStatus.Wait); return; } } string fromTag = CallProperties.CreateNewTag(); string toTag = CallProperties.CreateNewTag(); int cSeq = CallProperties.CreateNewCSeq(); string callId = CallProperties.CreateNewCallId(); SIPRequest catalogReq = PTZRequest(fromTag, toTag, cSeq, callId); string cmdStr = GetPtzCmd(ucommand, dwStop, dwSpeed); PTZControl ptz = new PTZControl() { CommandType = CommandType.DeviceControl, DeviceID = _deviceId, SN = new Random().Next(9999), PTZCmd = cmdStr }; string xmlBody = PTZControl.Instance.Save <PTZControl>(ptz); catalogReq.Body = xmlBody; _msgCore.Transport.SendRequest(_remoteEndPoint, catalogReq); }
public SIPRegistrationUserAgent( SIPTransport sipTransport, SIPEndPoint outboundProxy, SIPURI sipAccountAOR, string authUsername, string password, string realm, string registrarHost, SIPURI contactURI, int expiry, SIPMonitorLogDelegate logDelegate) { m_sipTransport = sipTransport; m_outboundProxy = outboundProxy; m_sipAccountAOR = sipAccountAOR; m_authUsername = authUsername; m_password = password; m_realm = realm; m_registrarHost = registrarHost; m_contactURI = contactURI; m_expiry = (expiry >= REGISTER_MINIMUM_EXPIRY && expiry <= MAX_EXPIRY) ? expiry : DEFAULT_REGISTER_EXPIRY; m_callID = CallProperties.CreateNewCallId(); if (logDelegate != null) { Log_External = logDelegate; } else { Log_External = (ev) => logger.LogDebug(ev?.Message); } }
public SIPRegistrationUserAgent( SIPTransport sipTransport, SIPEndPoint outboundProxy, SIPURI sipAccountAOR, string authUsername, string password, string realm, string registrarHost, SIPURI contactURI, int expiry, string[] customHeaders, int maxRegistrationAttemptTimeout = DEFAULT_MAX_REGISTRATION_ATTEMPT_TIMEOUT, int registerFailureRetryInterval = DEFAULT_REGISTER_FAILURE_RETRY_INTERVAL, int maxRegisterAttempts = DEFAULT_MAX_REGISTER_ATTEMPTS) { m_sipTransport = sipTransport; m_outboundProxy = outboundProxy; m_sipAccountAOR = sipAccountAOR; m_authUsername = authUsername; m_password = password; m_realm = realm; m_registrarHost = registrarHost; m_contactURI = contactURI; m_expiry = (expiry >= REGISTER_MINIMUM_EXPIRY && expiry <= MAX_EXPIRY) ? expiry : DEFAULT_REGISTER_EXPIRY; m_originalExpiry = m_expiry; m_customHeaders = customHeaders; m_callID = CallProperties.CreateNewCallId(); m_maxRegistrationAttemptTimeout = maxRegistrationAttemptTimeout; m_registerFailureRetryInterval = registerFailureRetryInterval; m_maxRegisterAttempts = maxRegisterAttempts; }
public SIPNotifierClient( SIPMonitorLogDelegate logDelegate, SIPTransport sipTransport, SIPEndPoint outboundProxy, SIPEventPackage sipEventPackage, SIPURI resourceURI, string authUsername, string authDomain, string authPassword, int expiry, string filter) { Log_External = logDelegate; m_sipTransport = sipTransport; m_outboundProxy = outboundProxy; m_sipEventPackage = sipEventPackage; m_authUsername = authUsername; m_authDomain = authDomain; m_authPassword = authPassword; m_expiry = (expiry > 0) ? expiry : DEFAULT_SUBSCRIBE_EXPIRY; m_filter = filter; m_resourceURI = resourceURI.CopyOf(); m_subscribeCallID = CallProperties.CreateNewCallId(); m_subscriptionFromTag = CallProperties.CreateNewTag(); }
/// <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> /// <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, LocalEndPoint.ToHost(), ""); SIPFromHeader from = new SIPFromHeader(null, localUri, fromTag); //SIPToHeader to = new SIPToHeader(null, remoteUri, toTag); 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 = UserAgent; catalogReq.Header.CSeq = cSeq; catalogReq.Header.CallId = callId; catalogReq.Header.ContentType = "application/MANSCDP+xml"; return(catalogReq); }
private SIPRequest GetRequest(SIPMethodsEnum method) { try { SIPURI uri = SIPURI.ParseSIPURIRelaxed(m_callDescriptor.Uri); SIPRequest request = new SIPRequest(method, uri); SIPFromHeader fromHeader = m_callDescriptor.GetFromHeader(); fromHeader.FromTag = CallProperties.CreateNewTag(); SIPToHeader toHeader = new SIPToHeader(null, uri, null); int cseq = Crypto.GetRandomInt(10000, 20000); SIPHeader header = new SIPHeader(fromHeader, toHeader, cseq, CallProperties.CreateNewCallId()); header.CSeqMethod = method; header.UserAgent = m_userAgent; request.Header = header; header.Vias.PushViaHeader(SIPViaHeader.GetDefaultSIPViaHeader()); try { if (m_callDescriptor.CustomHeaders != null && m_callDescriptor.CustomHeaders.Count > 0) { foreach (string customHeader in m_callDescriptor.CustomHeaders) { if (customHeader.IsNullOrBlank()) { continue; } else if (customHeader.Trim().StartsWith(SIPHeaders.SIP_HEADER_USERAGENT)) { request.Header.UserAgent = customHeader.Substring(customHeader.IndexOf(":") + 1).Trim(); } else { request.Header.UnknownHeaders.Add(customHeader); } } } } catch (Exception excp) { logger.LogError("Exception Parsing CustomHeader for SIPNonInviteClientUserAgent GetRequest. " + excp.Message + m_callDescriptor.CustomHeaders); } if (!m_callDescriptor.Content.IsNullOrBlank()) { request.Body = m_callDescriptor.Content; request.Header.ContentType = m_callDescriptor.ContentType; request.Header.ContentLength = request.Body.Length; } return(request); } catch (Exception excp) { logger.LogError("Exception SIPNonInviteClientUserAgent GetRequest. " + excp.Message); throw; } }
private SIPRequest GetInviteRequest(string callURI, SIPCallDescriptor sipCallDescriptor) { SIPFromHeader fromHeader = sipCallDescriptor.GetFromHeader(); SIPRequest inviteRequest = new SIPRequest(SIPMethodsEnum.INVITE, SIPURI.ParseSIPURI(callURI)) { LocalSIPEndPoint = m_blackhole }; SIPHeader inviteHeader = new SIPHeader(fromHeader, new SIPToHeader(null, inviteRequest.URI, null), 1, CallProperties.CreateNewCallId()); inviteHeader.From.FromTag = CallProperties.CreateNewTag(); // For incoming calls forwarded via the dial plan the username needs to go into the Contact header. inviteHeader.Contact = new List <SIPContactHeader>() { new SIPContactHeader(null, new SIPURI(inviteRequest.URI.Scheme, m_blackhole)) }; inviteHeader.CSeqMethod = SIPMethodsEnum.INVITE; inviteRequest.Header = inviteHeader; SIPViaHeader viaHeader = new SIPViaHeader(m_blackhole, CallProperties.CreateBranchId()); inviteRequest.Header.Vias.PushViaHeader(viaHeader); try { if (sipCallDescriptor.CustomHeaders != null && sipCallDescriptor.CustomHeaders.Count > 0) { foreach (string customHeader in sipCallDescriptor.CustomHeaders) { if (customHeader.IsNullOrBlank()) { continue; } else if (customHeader.Trim().StartsWith(SIPHeaders.SIP_HEADER_USERAGENT)) { inviteRequest.Header.UserAgent = customHeader.Substring(customHeader.IndexOf(":") + 1).Trim(); } else { inviteRequest.Header.UnknownHeaders.Add(customHeader); } } } } catch (Exception excp) { logger.Error("Exception Parsing CustomHeader for GetInviteRequest. " + excp.Message + sipCallDescriptor.CustomHeaders); } return(inviteRequest); }
public SIPRequest GetInviteRequest(string inviteURIStr, string fromURIStr, string body, int rtpPort) { SIPURI inviteURI = (inviteURIStr.StartsWith("sip:")) ? SIPURI.ParseSIPURI(inviteURIStr) : SIPURI.ParseSIPURI("sip:" + inviteURIStr); SIPFromHeader fromHeader = SIPFromHeader.ParseFromHeader(fromURIStr); // (fromURIStr.StartsWith("sip:")) ? SIPFromHeader.ParseFromHeader(fromURIStr) : SIPFromHeader.ParseFromHeader("sip:" + fromURIStr); SIPToHeader toHeader = new SIPToHeader(null, inviteURI, null); SIPRequest inviteRequest = new SIPRequest(SIPMethodsEnum.INVITE, inviteURI); IPEndPoint localSIPEndPoint = m_sipTransport.GetIPEndPointsList()[0]; SIPHeader inviteHeader = new SIPHeader(fromHeader, toHeader, 1, CallProperties.CreateNewCallId()); inviteHeader.From.FromTag = CallProperties.CreateNewTag(); inviteHeader.Contact = SIPContactHeader.ParseContactHeader("sip:" + localSIPEndPoint.ToString()); inviteHeader.CSeqMethod = SIPMethodsEnum.INVITE; //inviteHeader.UnknownHeaders.Add("BlueFace-Test: 12324"); inviteRequest.Header = inviteHeader; SIPViaHeader viaHeader = new SIPViaHeader(localSIPEndPoint.Address.ToString(), localSIPEndPoint.Port, CallProperties.CreateBranchId()); inviteRequest.Header.Via.PushViaHeader(viaHeader); rtpPort = (rtpPort != 0) ? rtpPort : Crypto.GetRandomInt(10000, 20000); string sessionId = Crypto.GetRandomInt(1000, 5000).ToString(); if (body != null && body.Trim().Length > 0) { inviteRequest.Body = body; } else { inviteRequest.Body = "v=0" + CRLF + "o=- " + sessionId + " " + sessionId + " IN IP4 " + localSIPEndPoint.Address.ToString() + CRLF + "s=session" + CRLF + "c=IN IP4 " + localSIPEndPoint.Address.ToString() + CRLF + "t=0 0" + CRLF + "m=audio " + rtpPort + " RTP/AVP 0 101" + CRLF + "a=rtpmap:0 PCMU/8000" + CRLF + "a=rtpmap:101 telephone-event/8000" + CRLF + "a=fmtp:101 0-16" + CRLF + "a=sendrecv"; } inviteRequest.Header.ContentLength = inviteRequest.Body.Length; inviteRequest.Header.ContentType = "application/sdp"; return(inviteRequest); }
/// <summary> /// 查询设备目录请求 /// </summary> /// <returns></returns> private SIPRequest QueryItems() { SIPURI remoteUri = new SIPURI(_sipAccount.RemoteSipId, _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.From = from; queryReq.Header.Contact = null; 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 CreateDummyRequest(SIPDialogue dialogueToReplace, string callDestination) { SIPRequest dummyInvite = new SIPRequest(SIPMethodsEnum.INVITE, SIPURI.ParseSIPURIRelaxed(callDestination + "@sipsorcery.com")); SIPHeader dummyHeader = new SIPHeader("<sip:[email protected]>", "<sip:[email protected]>", 1, CallProperties.CreateNewCallId()) { CSeqMethod = SIPMethodsEnum.INVITE }; dummyHeader.Vias.PushViaHeader(new SIPViaHeader(new IPEndPoint(SIPTransport.BlackholeAddress, 0), CallProperties.CreateBranchId())); dummyInvite.Header = dummyHeader; dummyInvite.Header.ContentType = "application/sdp"; dummyInvite.Body = dialogueToReplace.SDP; return(dummyInvite); }
/// <summary> /// 向上级平台注册 /// </summary> private void RegisterToPlatform() { string fromTag = CallProperties.CreateNewTag(); string callId = CallProperties.CreateNewCallId(); int cSeq = CallProperties.CreateNewCSeq(); string branch = CallProperties.CreateBranchId(); SIPURI localUri = new SIPURI(LocalSIPId, LocalEndPoint.ToHost(), ""); SIPURI remoteUri = new SIPURI(RemoteSIPId, RemoteEndPoint.ToHost(), ""); SIPRequest registerReq = new SIPRequest(SIPMethodsEnum.REGISTER, remoteUri); SIPViaHeader via = new SIPViaHeader(LocalEndPoint, branch); SIPViaSet viaSet = new SIPViaSet(); viaSet.PushViaHeader(via); SIPFromHeader from = new SIPFromHeader(null, localUri, fromTag); SIPToHeader to = new SIPToHeader(null, localUri, null); SIPHeader header = new SIPHeader(from, to, cSeq, callId); registerReq.Header = header; SIPContactHeader contact = new SIPContactHeader(null, localUri); header.Contact = new List <SIPContactHeader>(); header.Contact.Add(contact); header.Vias = viaSet; header.AuthenticationHeader = _auth; header.Expires = 3600; header.CSeqMethod = SIPMethodsEnum.REGISTER; header.MaxForwards = 70; header.ContentLength = 0; header.UserAgent = SIPConstants.SIP_SERVER_STRING; Transport.SendRequest(RemoteEndPoint, registerReq); }
private SIPRequest GetOptionsRequest(SIPURI serverURI, int cseq, IPEndPoint contact) { SIPRequest optionsRequest = new SIPRequest(SIPMethodsEnum.OPTIONS, serverURI); SIPFromHeader fromHeader = new SIPFromHeader(null, SIPURI.ParseSIPURI("sip:" + contact.ToString()), null); SIPToHeader toHeader = new SIPToHeader(null, serverURI, null); string callId = CallProperties.CreateNewCallId(); string branchId = CallProperties.CreateBranchId(); SIPHeader header = new SIPHeader(fromHeader, toHeader, cseq, callId); header.CSeqMethod = SIPMethodsEnum.OPTIONS; header.MaxForwards = 0; SIPViaHeader viaHeader = new SIPViaHeader(contact.Address.ToString(), contact.Port, branchId); header.Vias.PushViaHeader(viaHeader); optionsRequest.Header = header; return(optionsRequest); }
private SIPRequest GetDummyINVITERequest(SIPURI dummyURI) { string dummyFrom = "<sip:[email protected]>"; string dummyContact = "sip:127.0.0.1:1234"; SIPRequest inviteRequest = new SIPRequest(SIPMethodsEnum.INVITE, dummyURI); SIPHeader inviteHeader = new SIPHeader(SIPFromHeader.ParseFromHeader(dummyFrom), new SIPToHeader(null, dummyURI, null), 1, CallProperties.CreateNewCallId()); inviteHeader.From.FromTag = CallProperties.CreateNewTag(); inviteHeader.Contact = SIPContactHeader.ParseContactHeader(dummyContact); inviteHeader.CSeqMethod = SIPMethodsEnum.INVITE; inviteHeader.UserAgent = "unittest"; inviteRequest.Header = inviteHeader; SIPViaHeader viaHeader = new SIPViaHeader("127.0.0.1", 1234, CallProperties.CreateBranchId(), SIPProtocolsEnum.udp); inviteRequest.Header.Vias.PushViaHeader(viaHeader); inviteRequest.Body = "dummy"; inviteRequest.Header.ContentLength = inviteRequest.Body.Length; inviteRequest.Header.ContentType = "application/sdp"; return(inviteRequest); }
/// <summary> /// 监控视频请求 /// </summary> /// <returns></returns> private SIPRequest InviteRequest() { SIPURI uri = new SIPURI(_cameraId, _remoteEndPoint.ToHost(), ""); SIPRequest inviteRequest = _m_sipTransport.GetRequest(SIPMethodsEnum.INVITE, uri); SIPFromHeader from = new SIPFromHeader(null, _sipRequest.URI, CallProperties.CreateNewTag()); SIPHeader header = new SIPHeader(from, inviteRequest.Header.To, CallProperties.CreateNewCSeq(), CallProperties.CreateNewCallId()); header.ContentType = "application/DDCP"; header.Expires = inviteRequest.Header.Expires; header.CSeqMethod = inviteRequest.Header.CSeqMethod; header.Vias = inviteRequest.Header.Vias; header.MaxForwards = inviteRequest.Header.MaxForwards; header.UserAgent = _userAgent; inviteRequest.Header.From = from; inviteRequest.Header = header; _realReqSession = inviteRequest; return(inviteRequest); }
/// <summary> /// Initiates the call to the remote user agent server. /// </summary> /// <param name="sipCallDescriptor">The descriptor for the call that describes how to reach the user agent server and other properties.</param> /// <param name="serverEndPoint">Optional. If the server end point for the call is known or has been resolved in advance. If /// not set the SIP transport layer will attempt to resolve the destination at sending time.</param> public SIPRequest Call(SIPCallDescriptor sipCallDescriptor, SIPEndPoint serverEndPoint) { try { m_sipCallDescriptor = sipCallDescriptor; SIPURI callURI = SIPURI.ParseSIPURI(sipCallDescriptor.Uri); SIPRouteSet routeSet = null; logger.LogDebug($"UAC commencing call to {SIPURI.ParseSIPURI(m_sipCallDescriptor.Uri).CanonicalAddress}."); // A custom route set may have been specified for the call. if (m_sipCallDescriptor.RouteSet != null && m_sipCallDescriptor.RouteSet.IndexOf(OUTBOUNDPROXY_AS_ROUTESET_CHAR) != -1) { try { routeSet = new SIPRouteSet(); routeSet.PushRoute(new SIPRoute(m_sipCallDescriptor.RouteSet, true)); } catch { logger.LogDebug("Error an outbound proxy value was not recognised in SIPClientUserAgent Call. " + m_sipCallDescriptor.RouteSet + "."); } } string content = sipCallDescriptor.Content; if (content.IsNullOrBlank()) { logger.LogDebug("Body on UAC call was empty."); } if (this.m_sipCallDescriptor.BranchId.IsNullOrBlank()) { this.m_sipCallDescriptor.BranchId = CallProperties.CreateBranchId(); } if (this.m_sipCallDescriptor.CallId.IsNullOrBlank()) { this.m_sipCallDescriptor.CallId = CallProperties.CreateNewCallId(); } SIPRequest inviteRequest = GetInviteRequest(m_sipCallDescriptor, m_sipCallDescriptor.BranchId, m_sipCallDescriptor.CallId, routeSet, content, sipCallDescriptor.ContentType); // Now that we have a destination socket create a new UAC transaction for forwarded leg of the call. m_serverTransaction = new UACInviteTransaction(m_sipTransport, inviteRequest, m_outboundProxy); m_serverTransaction.UACInviteTransactionInformationResponseReceived += ServerInformationResponseReceived; m_serverTransaction.UACInviteTransactionFinalResponseReceived += ServerFinalResponseReceived; m_serverTransaction.UACInviteTransactionFailed += ServerTransactionFailed; m_serverTransaction.SendInviteRequest(); return(inviteRequest); } catch (ApplicationException appExcp) { m_serverTransaction?.CancelCall(appExcp.Message); CallFailed?.Invoke(this, appExcp.Message, null); return(null); } catch (Exception excp) { logger.LogError("Exception UserAgentClient Call. " + excp); m_serverTransaction?.CancelCall("Unknown exception"); CallFailed?.Invoke(this, excp.Message, null); return(null); } }
public void ProcessNotifyRequest(SIPRequest sipRequest) { try { // Hack to work around MWI request from callcentric not having a trailing CRLF and breaking some softphones like the Bria. if (sipRequest.Header.Event == MWI_EVENT_TYPE && sipRequest.Body.NotNullOrBlank() && sipRequest.Body.Substring(sipRequest.Body.Length - 2) != m_crlf) { sipRequest.Body += m_crlf; } string fromURI = (sipRequest.Header.From != null && sipRequest.Header.From.FromURI != null) ? sipRequest.Header.From.FromURI.ToString() : "unknown"; string domain = GetCanonicalDomain_External(sipRequest.URI.Host, true); if (domain != null) { SIPAccountAsset sipAccount = GetSIPAccount_External(s => s.SIPUsername == sipRequest.URI.User && s.SIPDomain == domain); if (sipAccount != null) { List <SIPRegistrarBinding> bindings = GetSIPAccountBindings_External(b => b.SIPAccountId == sipAccount.Id, null, 0, MAX_FORWARD_BINDINGS); if (bindings != null) { foreach (SIPRegistrarBinding binding in bindings) { SIPURI dstURI = binding.MangledContactSIPURI; SIPEndPoint localSIPEndPoint = (m_outboundProxy != null) ? m_sipTransport.GetDefaultSIPEndPoint(m_outboundProxy.Protocol) : m_sipTransport.GetDefaultSIPEndPoint(dstURI.Protocol); SIPEndPoint dstSIPEndPoint = null; // If the outbound proxy is a loopback address, as it will normally be for local deployments, then it cannot be overriden. if (m_outboundProxy != null && IPAddress.IsLoopback(m_outboundProxy.Address)) { dstSIPEndPoint = m_outboundProxy; } else if (binding.ProxySIPEndPoint != null) { // If the binding has a specific proxy end point sent then the request needs to be forwarded to the proxy's default end point for it to take care of. dstSIPEndPoint = new SIPEndPoint(SIPProtocolsEnum.udp, new IPEndPoint(binding.ProxySIPEndPoint.Address, m_defaultSIPPort)); } else if (m_outboundProxy != null) { dstSIPEndPoint = m_outboundProxy; } else { SIPDNSLookupResult lookupResult = m_sipTransport.GetURIEndPoint(dstURI, false); if (lookupResult.LookupError != null) { Log_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Notifier, SIPMonitorEventTypesEnum.MWI, "A NOTIFY request from " + fromURI + " was not forwarded due to DNS failure for " + dstURI.Host + ", " + lookupResult.LookupError + ".", sipAccount.Owner)); } else { dstSIPEndPoint = lookupResult.GetSIPEndPoint(); } } if (dstSIPEndPoint != null) { // Rather than create a brand new request copy the received one and modify the headers that need to be unique. SIPRequest notifyRequest = sipRequest.Copy(); notifyRequest.URI = dstURI; notifyRequest.Header.Contact = SIPContactHeader.CreateSIPContactList(new SIPURI(dstURI.Scheme, localSIPEndPoint)); notifyRequest.Header.To = new SIPToHeader(null, dstURI, null); notifyRequest.Header.CallId = CallProperties.CreateNewCallId(); SIPViaHeader viaHeader = new SIPViaHeader(localSIPEndPoint, CallProperties.CreateBranchId()); notifyRequest.Header.Vias = new SIPViaSet(); notifyRequest.Header.Vias.PushViaHeader(viaHeader); // If the binding has a proxy socket defined set the header to ask the upstream proxy to use it. if (binding.ProxySIPEndPoint != null) { notifyRequest.Header.ProxySendFrom = binding.ProxySIPEndPoint.ToString(); // If the binding has a specific proxy end point sent then the request needs to be forwarded to the proxy's default end point for it to take care of. dstSIPEndPoint = new SIPEndPoint(SIPProtocolsEnum.udp, new IPEndPoint(binding.ProxySIPEndPoint.Address, m_defaultSIPPort)); } Log_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Notifier, SIPMonitorEventTypesEnum.MWI, "Forwarding NOTIFY request from " + fromURI + " to registered binding at " + dstURI.ToString() + ", proxy " + dstSIPEndPoint.ToString() + ".", sipAccount.Owner)); SIPNonInviteTransaction notifyTransaction = m_sipTransport.CreateNonInviteTransaction(notifyRequest, dstSIPEndPoint, localSIPEndPoint, dstSIPEndPoint); notifyTransaction.SendReliableRequest(); } else { Log_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Notifier, SIPMonitorEventTypesEnum.MWI, "A NOTIFY request from " + fromURI + " was not forwarded as no destination end point was resolved.", sipAccount.Owner)); } } // Send OK response to server. SIPResponse okResponse = SIPTransport.GetResponse(sipRequest, SIPResponseStatusCodesEnum.Ok, null); m_sipTransport.SendResponse(okResponse); } else { // Send unavailable response to server. Log_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Notifier, SIPMonitorEventTypesEnum.MWI, "NOTIFY request from " + fromURI + " for " + sipAccount.SIPUsername + "@" + sipAccount.SIPDomain + " but no bindings available, responding with temporarily unavailable.", sipAccount.Owner)); SIPResponse notAvailableResponse = SIPTransport.GetResponse(sipRequest, SIPResponseStatusCodesEnum.TemporarilyUnavailable, null); m_sipTransport.SendResponse(notAvailableResponse); } } else { // Send Not found response to server. Log_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Notifier, SIPMonitorEventTypesEnum.MWI, "NOTIFY request from " + fromURI + " for " + sipRequest.URI.ToString() + " but no matching SIP account, responding with not found.", null)); SIPResponse notFoundResponse = SIPTransport.GetResponse(sipRequest, SIPResponseStatusCodesEnum.NotFound, null); m_sipTransport.SendResponse(notFoundResponse); } } else { // Send Not Serviced response to server. Log_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Notifier, SIPMonitorEventTypesEnum.MWI, "NOTIFY request from " + fromURI + " for a non-serviced domain responding with not found.", null)); SIPResponse notServicedResponse = SIPTransport.GetResponse(sipRequest, SIPResponseStatusCodesEnum.NotFound, "Domain not serviced"); m_sipTransport.SendResponse(notServicedResponse); } } catch (Exception excp) { logger.Error("Exception SIPNotifyManager ProcessNotifyRequest. " + excp.Message); } }
private SIPRequest GetCallbackInviteRequest(IPEndPoint localSIPEndPoint, string sdp) { string callBackURI = "sip:[email protected]"; string callBackUserField = "<" + callBackURI + ">"; SIPRequest inviteRequest = new SIPRequest(SIPMethodsEnum.INVITE, callBackURI); SIPHeader inviteHeader = new SIPHeader(callBackUserField, callBackUserField, 1, CallProperties.CreateNewCallId()); inviteHeader.CSeqMethod = SIPMethodsEnum.INVITE; inviteHeader.ContentType = "application/sdp"; SIPViaHeader viaHeader = new SIPViaHeader(localSIPEndPoint, CallProperties.CreateBranchId()); inviteHeader.Vias.PushViaHeader(viaHeader); inviteRequest.Header = inviteHeader; if (sdp == null) { sdp = "v=0" + CRLF + "o=- " + Crypto.GetRandomInt(1000, 5000).ToString() + " 2 IN IP4 " + localSIPEndPoint.Address.ToString() + CRLF + "s=session" + CRLF + "c=IN IP4 " + localSIPEndPoint.Address.ToString() + CRLF + "t=0 0" + CRLF + "m=audio " + Crypto.GetRandomInt(10000, 20000).ToString() + " RTP/AVP 0 18 101" + CRLF + "a=rtpmap:0 PCMU/8000" + CRLF + "a=rtpmap:18 G729/8000" + CRLF + "a=rtpmap:101 telephone-event/8000" + CRLF + "a=fmtp:101 0-16" + CRLF + "a=recvonly"; } inviteHeader.ContentLength = sdp.Length; inviteRequest.Body = sdp; return(inviteRequest); }
/// <summary> /// 录像文件查询 /// </summary> /// <param name="startTime">开始时间</param> /// <param name="endTime">结束时间</param> public int RecordFileQuery(DateTime startTime, DateTime endTime) { lock (_msgCore.RemoteTrans) { if (!_msgCore.RemoteTrans.ContainsKey(_remoteEndPoint.ToString())) { OnSIPServiceChange(_deviceName + "-" + _deviceId + _remoteEndPoint.ToString(), SipServiceStatus.Wait); return(0); } } this.Stop(); SIPURI remoteUri = new SIPURI(_deviceId, _remoteEndPoint.ToHost(), ""); SIPURI localUri = new SIPURI(_msgCore.LocalSIPId, _msgCore.LocalEndPoint.ToHost(), ""); SIPFromHeader from = new SIPFromHeader(null, localUri, CallProperties.CreateNewTag()); SIPToHeader to = new SIPToHeader(null, remoteUri, CallProperties.CreateNewTag()); SIPRequest recordFileReq = _msgCore.Transport.GetRequest(SIPMethodsEnum.MESSAGE, remoteUri); SIPContactHeader contactHeader = new SIPContactHeader(null, localUri); recordFileReq.Header.Contact.Clear(); recordFileReq.Header.Contact.Add(contactHeader); recordFileReq.Header.Allow = null; recordFileReq.Header.From = from; recordFileReq.Header.To = to; recordFileReq.Header.UserAgent = _msgCore.UserAgent; recordFileReq.Header.CSeq = CallProperties.CreateNewCSeq(); recordFileReq.Header.CallId = CallProperties.CreateNewCallId(); recordFileReq.Header.ContentType = "application/MANSCDP+xml"; string bTime = startTime.ToString("yyyy-MM-ddTHH:mm:ss"); string eTime = endTime.ToString("yyyy-MM-ddTHH:mm:ss"); RecordQuery record = new RecordQuery() { DeviceID = _deviceId, SN = new Random().Next(1, 3000), CmdType = CommandType.RecordInfo, Secrecy = 0, StartTime = bTime, EndTime = eTime, Type = "time" }; _recordTotal = -1; string xmlBody = RecordQuery.Instance.Save <RecordQuery>(record); recordFileReq.Body = xmlBody; _msgCore.Transport.SendRequest(_remoteEndPoint, recordFileReq); DateTime recordQueryTime = DateTime.Now; while (_recordTotal < 0) { Thread.Sleep(50); if (DateTime.Now.Subtract(recordQueryTime).TotalSeconds > 2) { logger.Debug(_deviceName + "[" + _deviceId + "] 等待录像查询超时"); _recordTotal = 0; break; } } return(_recordTotal); }
public SIPRequest GetSIPRequest(SIPMethodsEnum sipMethod, string requestURIStr, string fromURIStr) { return(GetSIPRequest(sipMethod, requestURIStr, fromURIStr, 1, CallProperties.CreateNewCallId(), null, null)); }
private SIPRequest GetInviteRequest(IPEndPoint localContact, string inviteBody, IPEndPoint dstEndPoint) { SIPRequest inviteRequest = new SIPRequest(SIPMethodsEnum.INVITE, SIPURI.ParseSIPURI("sip:" + dstEndPoint)); SIPHeader inviteHeader = new SIPHeader(SIPFromHeader.ParseFromHeader("<sip:" + localContact + ">"), SIPToHeader.ParseToHeader("<sip:" + dstEndPoint + ">"), 1, CallProperties.CreateNewCallId()); inviteHeader.From.FromTag = CallProperties.CreateNewTag(); inviteHeader.Contact = SIPContactHeader.ParseContactHeader("sip:" + localContact); inviteHeader.CSeqMethod = SIPMethodsEnum.INVITE; inviteHeader.UserAgent = "unit test"; inviteRequest.Header = inviteHeader; SIPViaHeader viaHeader = new SIPViaHeader(localContact.Address.ToString(), localContact.Port, CallProperties.CreateBranchId(), SIPProtocolsEnum.udp); inviteRequest.Header.Vias.PushViaHeader(viaHeader); //inviteRequest.Body = inviteBody; //inviteRequest.Header.ContentLength = inviteBody.Length; inviteRequest.Header.ContentType = "application/sdp"; return(inviteRequest); }
[Ignore] // Broken after NEtStandard migration. public void SingleProviderLegUnitTest() { Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); SIPRequest inviteRequest = new SIPRequest(SIPMethodsEnum.INVITE, SIPURI.ParseSIPURI("sip:1234@localhost")); SIPHeader inviteHeader = new SIPHeader(SIPFromHeader.ParseFromHeader("<sip:joe@localhost>"), SIPToHeader.ParseToHeader("<sip:jane@localhost>"), 23, CallProperties.CreateNewCallId()); SIPViaHeader viaHeader = new SIPViaHeader("127.0.0.1", 5060, CallProperties.CreateBranchId()); inviteHeader.Vias.PushViaHeader(viaHeader); inviteRequest.Header = inviteHeader; List <SIPProvider> providers = new List <SIPProvider>(); SIPProvider provider = new SIPProvider(ProviderTypes.SIP, "test", "blueface", "test", "password", SIPURI.ParseSIPURIRelaxed("sip.blueface.ie"), null, null, null, null, 3600, null, null, null, false, false, null, null, null); providers.Add(provider); DialStringParser dialStringParser = new DialStringParser(null, "test", null, providers, delegate { return(null); }, delegate { return(null); }, (host, wildcard) => { return(null); }, null, "test"); Queue <List <SIPCallDescriptor> > callQueue = dialStringParser.ParseDialString(DialPlanContextsEnum.Script, inviteRequest, "blueface", null, null, null, null, null, null, null, null, CustomerServiceLevels.Free); Assert.IsNotNull(callQueue, "The call list should have contained a call."); Assert.IsTrue(callQueue.Count == 1, "The call queue list should have contained one leg."); List <SIPCallDescriptor> firstLeg = callQueue.Dequeue(); Assert.IsNotNull(firstLeg, "The first call leg should exist."); Assert.IsTrue(firstLeg.Count == 1, "The first call leg should have had one switch call."); Assert.IsTrue(firstLeg[0].Username == "test", "The username for the first call leg was not correct."); Assert.IsTrue(firstLeg[0].Uri.ToString() == "sip:[email protected]", "The destination URI for the first call leg was not correct."); Console.WriteLine("---------------------------------"); }
public SIPRequest GetRegisterRequest(string server, string toURIStr, string contactStr) { try { IPEndPoint localSIPEndPoint = m_sipTransport.GetDefaultTransportContact(SIPProtocolsEnum.UDP); SIPRequest registerRequest = new SIPRequest(SIPMethodsEnum.REGISTER, "sip:" + server); SIPHeader registerHeader = new SIPHeader(SIPFromHeader.ParseFromHeader(toURIStr), SIPToHeader.ParseToHeader(toURIStr), 1, CallProperties.CreateNewCallId()); registerHeader.From.FromTag = CallProperties.CreateNewTag(); registerHeader.Contact = SIPContactHeader.ParseContactHeader(contactStr); SIPViaHeader viaHeader = new SIPViaHeader(localSIPEndPoint.Address.ToString(), localSIPEndPoint.Port, CallProperties.CreateBranchId()); registerHeader.Via.PushViaHeader(viaHeader); registerHeader.CSeqMethod = SIPMethodsEnum.REGISTER; registerHeader.Expires = SIPConstants.DEFAULT_REGISTEREXPIRY_SECONDS; registerRequest.Header = registerHeader; return(registerRequest); } catch (Exception excp) { logger.Error("Exception GetRegisterRequest. " + excp.Message); throw new ApplicationException("GetRegisterRequest " + excp.GetType().ToString() + ". " + excp.Message); } }
private SIPRequest GetDummyINVITERequest(SIPURI dummyURI) { string dummyFrom = "<sip:[email protected]>"; SIPRequest inviteRequest = new SIPRequest(SIPMethodsEnum.INVITE, dummyURI); SIPHeader inviteHeader = new SIPHeader(SIPFromHeader.ParseFromHeader(dummyFrom), new SIPToHeader(null, dummyURI, null), 1, CallProperties.CreateNewCallId()); inviteHeader.From.FromTag = CallProperties.CreateNewTag(); inviteHeader.Contact = new List <SIPContactHeader> { SIPContactHeader.GetDefaultSIPContactHeader(inviteRequest.URI.Scheme) }; inviteHeader.CSeqMethod = SIPMethodsEnum.INVITE; inviteHeader.UserAgent = "unittest"; inviteRequest.Header = inviteHeader; SIPViaHeader viaHeader = SIPViaHeader.GetDefaultSIPViaHeader(); inviteRequest.Header.Vias.PushViaHeader(viaHeader); inviteRequest.Body = "dummy"; inviteRequest.Header.ContentLength = inviteRequest.Body.Length; inviteRequest.Header.ContentType = "application/sdp"; return(inviteRequest); }