public RtspStatusCode TearDown() { if ((_rtspSocket == null)) { Connect(); } RtspResponse response; var request = new RtspRequest(RtspMethod.Teardown, string.Format("rtsp://{0}:{1}/stream={2}", _address, 554, _rtspStreamId), 1, 0); request.Headers.Add("Session", _rtspSessionId); SendRequest(request); ReceiveResponse(out response); if (_rtpListener != null) { _rtpListener.Dispose(); _rtpListener.PacketReceived -= new RtpListener.PacketReceivedHandler(RtpPacketReceived); _rtpListener = null; } if (_rtcpListener != null) { _rtcpListener.Dispose(); _rtcpListener.PacketReceived -= new RtcpListener.PacketReceivedHandler(RtcpPacketReceived); _rtcpListener = null; } StopKeepAliveThread(); //Disconnect(); _rtspSessionId = string.Empty; return(response.StatusCode); }
public RtspStatusCode Setup(string query, TransmissionMode transmissionmode) { RtspRequest request = null; RtspResponse response; if ((_rtspSocket == null)) { Connect(); } if (string.IsNullOrEmpty(_rtspSessionId)) { request = new RtspRequest(RtspMethod.Setup, string.Format("rtsp://{0}:{1}/?{2}", _address, 554, query), 1, 0); switch (transmissionmode) { case TransmissionMode.Multicast: request.Headers.Add("Transport", string.Format("RTP/AVP;{0}", transmissionmode.ToString().ToLower())); break; case TransmissionMode.Unicast: var activeTcpConnections = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections(); var usedPorts = new HashSet <int>(); foreach (var connection in activeTcpConnections) { usedPorts.Add(connection.LocalEndPoint.Port); } for (var port = 40000; port <= 65534; port += 2) { if (!usedPorts.Contains(port) && !usedPorts.Contains(port + 1)) { _rtpPort = port; _rtcpPort = port + 1; break; } } request.Headers.Add("Transport", string.Format("RTP/AVP;{0};client_port={1}-{2}", transmissionmode.ToString().ToLower(), _rtpPort, _rtcpPort)); break; } } else { request = new RtspRequest(RtspMethod.Setup, string.Format("rtsp://{0}:{1}/?{2}", _address, 554, query), 1, 0); switch (transmissionmode) { case TransmissionMode.Multicast: request.Headers.Add("Session", _rtspSessionId); request.Headers.Add("Transport", string.Format("RTP/AVP;{0}", transmissionmode.ToString().ToLower())); break; case TransmissionMode.Unicast: request.Headers.Add("Session", _rtspSessionId); request.Headers.Add("Transport", string.Format("RTP/AVP;{0};client_port={1}-{2}", transmissionmode.ToString().ToLower(), _rtpPort, _rtcpPort)); break; } } SendRequest(request); ReceiveResponse(out response); if (response.StatusCode == RtspStatusCode.Ok) { if (!response.Headers.TryGetValue("com.ses.streamID", out _rtspStreamId)) { Logger.Error(string.Format("Failed to tune, not able to locate Stream ID header in RTSP SETUP response")); } string sessionHeader; if (!response.Headers.TryGetValue("Session", out sessionHeader)) { Logger.Error(string.Format("Failed to tune, not able to locate Session header in RTSP SETUP response")); } ProcessSessionHeader(sessionHeader, "Setup"); string transportHeader; if (!response.Headers.TryGetValue("Transport", out transportHeader)) { Logger.Error(string.Format("Failed to tune, not able to locate Transport header in RTSP SETUP response")); } ProcessTransportHeader(transportHeader); StartKeepAliveThread(); //if (_rtpListener == null) //{ // _rtpListener = new RtpListener(_destination, _rtpPort, transmissionmode); // _rtpListener.PacketReceived += new RtpListener.PacketReceivedHandler(RtpPacketReceived); // _rtpListener.StartRtpListenerThread(); //} if (_rtcpListener == null) { _rtcpListener = new RtcpListener(Destination, _rtcpPort, transmissionmode); _rtcpListener.PacketReceived += new RtcpListener.PacketReceivedHandler(RtcpPacketReceived); _rtcpListener.StartRtcpListenerThread(); } } return(response.StatusCode); }