/// <summary> /// Processes a RtspRequest based on the contents /// </summary> /// <param name="request">The rtsp Request</param> /// <param name="session">The client information</param> internal void ProcessRtspRequest(RtspMessage request, ClientSession session, bool sendResponse = true) { try { //Log the reqeust now if (Logger != null) Logger.LogRequest(request, session); //Ensure we have a session and request and that the server is still running and that the session is still contained in this server if (request == null || request.IsDisposed || session == null || session.IsDisposed) { return; }//Ensure the session is added to the connected clients if it has not been already. else if (session.m_Contained == null) TryAddSession(session); //Check for session moved from another process. //else if (session.m_Contained != null && session.m_Contained != this) return; //Turtle power! if (request.MethodString == "REGISTER") { //Send back turtle food. ProcessInvalidRtspRequest(session, Rtsp.RtspStatusCode.BadRequest, session.RtspSocket.ProtocolType == ProtocolType.Tcp ? "FU_WILL_NEVER_SUPPORT_THIS$00\a" : string.Empty, sendResponse); return; } //All requests need the CSeq if (false == request.ContainsHeader(RtspHeaders.CSeq)) { //Send back a BadRequest. ProcessInvalidRtspRequest(session, Rtsp.RtspStatusCode.BadRequest, null, sendResponse); return; } /* 12.37 Session This request and response header field identifies an RTSP session started by the media server in a SETUP response and concluded by TEARDOWN on the presentation URL. The session identifier is chosen by the media server (see Section 3.4). Once a client receives a Session identifier, it MUST return it for any request related to that session. A server does not have to set up a session identifier if it has other means of identifying a session, such as dynamically generated URLs. */ //Determine if the request is specific to a session if (request.ContainsHeader(RtspHeaders.Session)) { //Determine what session is being acted on. string requestedSessionId = request.GetHeader(RtspHeaders.Session); //If there is a null or empty session id this request is invalid. if (string.IsNullOrWhiteSpace(requestedSessionId)) { //Send back a BadRequest. ProcessInvalidRtspRequest(session, Rtsp.RtspStatusCode.BadRequest, null, sendResponse); return; } else requestedSessionId = requestedSessionId.Trim(); //If the given session does not have a sessionId or does not match the sessionId requested. if (session.SessionId != requestedSessionId) { //Find any session which has the given id. IEnumerable<ClientSession> matches = GetSessions(requestedSessionId); //Atttempt to get the correct session ClientSession correctSession = matches.FirstOrDefault();//(c => false == c.IsDisconnected); //If no session could be found by the given sessionId if (correctSession == null) { //Indicate the session requested could not be found. ProcessInvalidRtspRequest(session, RtspStatusCode.SessionNotFound, null, sendResponse); return; } else //There was a session found by the given Id { //Indicate the last request of this session was as given session.LastRequest = request; //The LastResponse is updated to be the value of whatever the correctSessions LastResponse is // session.LastResponse = correctSession.LastResponse; //Process the request from the correctSession but do not send a response. ProcessRtspRequest(request, correctSession, false); session.LastResponse = correctSession.LastResponse; //Take the created response and sent it to the new session using it as the last response. ProcessSendRtspMessage(session.LastResponse, session, sendResponse); return; } } } //Check for out of order or duplicate requests. if (session.LastRequest != null) { //Out of order if (request.CSeq < session.LastRequest.CSeq) { //Send back a BadRequest. ProcessInvalidRtspRequest(session, Rtsp.RtspStatusCode.BadRequest, null, sendResponse); return; } } //Dispose any last request. if (session.LastRequest != null && false == session.LastRequest.IsDisposed) session.LastRequest.Dispose(); //Synchronize the server and client since this is not a duplicate session.LastRequest = request; //Determine if we support what the client requests in `Require` Header if (request.ContainsHeader(RtspHeaders.Require)) { //Certain features are requried... tcp etc. //Todo ProcessRequired( } //If there is a body and no content-length if (false == string.IsNullOrWhiteSpace(request.Body) && false == request.ContainsHeader(RtspHeaders.ContentLength)) { //Send back a BadRequest. ProcessInvalidRtspRequest(session, Rtsp.RtspStatusCode.BadRequest, null, sendResponse); return; } //Optional Checks //UserAgent if (RequireUserAgent && false == request.ContainsHeader(RtspHeaders.UserAgent)) { //Send back a BadRequest. ProcessInvalidRtspRequest(session, Rtsp.RtspStatusCode.BadRequest, null, sendResponse); return; } //Minor version reflects changes made to the protocol but not the 'general message parsing' `algorithm` //Thus, RTSP/2.4 is a lower version than RTSP/2.13, which in turn is lower than RTSP/12.3. //Leading zeros SHALL NOT be sent and MUST be ignored by recipients. //Version - Should check request.Version != Version and that earlier versions are supprted. if (request.Version > Version) { //ConvertToMessage ProcessInvalidRtspRequest(session, RtspStatusCode.RtspVersionNotSupported, null, sendResponse); return; } //4.2. RTSP IRI and URI, Draft requires 501 response for rtspu iri but not for udp sockets using a rtsp location.... //Should check request.Location.Scheme to not be rtspu but it was allowed previously... //If any custom handlers were registered. if (m_RequestHandlers.Count > 0) { //Determine if there is a custom handler for the mthod RtspRequestHandler custom; //If there is if (m_RequestHandlers.TryGetValue(request.Method, out custom)) { //Then create the response RtspMessage response; //By invoking the handler, if true is returned if (custom(request, out response)) { //Use the response created by the custom handler ProcessSendRtspMessage(response, session, sendResponse); //Return because the custom handler has handled the request. return; } } } //From this point if Authrorization is required and the stream exists //The server will responsd with AuthorizationRequired when it should NOT have probably respoded with that at this point. //The problem is that RequiredCredentails uses a Uri format by ID. //We could get a stream and then respond accordingly but that is how it currently works and it allows probing of streams which is what not desirable in some cases //Thus we have to use the location of the request and see if RequiredCredentials has anything which matches root. //This would force everything to have some type of authentication which would also be applicable to all lower level streams in the uri in the credential cache. //I could also change up the semantic and make everything Uri based rather then locations //Then it would also be easier to make /audio only passwords etc. //When stopping only handle teardown and keep alives if (m_StopRequested && (request.Method != RtspMethod.TEARDOWN && request.Method != RtspMethod.GET_PARAMETER && request.Method != RtspMethod.OPTIONS)) { ProcessInvalidRtspRequest(session, RtspStatusCode.BadRequest, null, sendResponse); return; } //Determine the handler for the request and process it switch (request.Method) { case RtspMethod.ANNOUNCE: { ProcessInvalidRtspRequest(session, RtspStatusCode.MethodNotAllowed, null, sendResponse); break; } case RtspMethod.OPTIONS: { ProcessRtspOptions(request, session, sendResponse); break; } case RtspMethod.DESCRIBE: { ProcessRtspDescribe(request, session, sendResponse); break; } case RtspMethod.SETUP: { ProcessRtspSetup(request, session, sendResponse); break; } case RtspMethod.PLAY: { ProcessRtspPlay(request, session, sendResponse); break; } case RtspMethod.RECORD: { ProcessRtspRecord(request, session, sendResponse); break; } case RtspMethod.PAUSE: { ProcessRtspPause(request, session, sendResponse); break; } case RtspMethod.TEARDOWN: { ProcessRtspTeardown(request, session, sendResponse); break; } case RtspMethod.GET_PARAMETER: { ProcessGetParameter(request, session, sendResponse); break; } case RtspMethod.SET_PARAMETER: { ProcessSetParameter(request, session, sendResponse); break; } case RtspMethod.REDIRECT: //Client can't redirect a server { ProcessInvalidRtspRequest(session, RtspStatusCode.BadRequest, null, sendResponse); break; } case RtspMethod.UNKNOWN: default: { //Per 2.0 Draft ProcessInvalidRtspRequest(session, RtspStatusCode.NotImplemented, null, sendResponse); break; } } } catch (Exception ex) { //Log it if (Logger != null) Logger.LogException(ex); } }
/// <summary> /// Sets the given session up, TODO Make functions which help with creation of TransportContext and Initialization /// </summary> /// <param name="request"></param> /// <param name="session"></param> internal void ProcessRtspSetup(RtspMessage request, ClientSession session, bool sendResponse = true) { RtpSource found = FindStreamByLocation(request.Location) as RtpSource; if (found == null) { //This allows probing for streams even if not authenticated.... ProcessLocationNotFoundRtspRequest(session, sendResponse); return; } else if (false == AuthenticateRequest(request, found)) { ProcessAuthorizationRequired(found, session, sendResponse); return; } else if (false == found.Ready) { ProcessInvalidRtspRequest(session, RtspStatusCode.PreconditionFailed, null, sendResponse); return; } //The source is ready //Determine if we have the track string track = request.Location.Segments.Last().Replace("/", string.Empty); Sdp.MediaDescription mediaDescription = found.SessionDescription.MediaDescriptions.FirstOrDefault(md => string.Compare(track, md.MediaType.ToString(), true, System.Globalization.CultureInfo.InvariantCulture) == 0); ////Find the MediaDescription for the request based on the track variable //foreach (Sdp.MediaDescription md in found.SessionDescription.MediaDescriptions) //{ // Sdp.SessionDescriptionLine attributeLine = md.Lines.Where(l => l.Type == 'a' && l.Parts.Any(p => p.Contains("control"))).FirstOrDefault(); // if (attributeLine != null) // { // string actualTrack = attributeLine.Parts.Where(p => p.Contains("control")).First().Replace("control:", string.Empty); // if(actualTrack == track || actualTrack.Contains(track)) // { // mediaDescription = md; // break; // } // } //} //Cannot setup media if (mediaDescription == null) { ProcessLocationNotFoundRtspRequest(session, sendResponse); return; } //Add the state information for the source RtpClient.TransportContext sourceContext = found.RtpClient.GetContextForMediaDescription(mediaDescription); //If the source has no TransportContext for that format or the source has not recieved a packet yet if (sourceContext == null || sourceContext.SynchronizationSourceIdentifier == 0) { //Stream is not yet ready ProcessInvalidRtspRequest(session, RtspStatusCode.PreconditionFailed, null, sendResponse); return; } //Create the response and initialize the sockets if required RtspMessage resp = session.ProcessSetup(request, found, sourceContext); //Send the response ProcessSendRtspMessage(resp, session, sendResponse); resp = null; }
/// <summary> /// /// </summary> /// <param name="request"></param> /// <param name="session"></param> internal void ProcessRtspPause(RtspMessage request, ClientSession session, bool sendResponse = true) { RtpSource found = FindStreamByLocation(request.Location) as RtpSource; if (found == null) { ProcessLocationNotFoundRtspRequest(session, sendResponse); return; } if (false == AuthenticateRequest(request, found)) { ProcessAuthorizationRequired(found, session, sendResponse); return; } //session.m_RtpClient.m_WorkerThread.Priority = ThreadPriority.BelowNormal; var resp = session.ProcessPause(request, found); //Might need to add some headers ProcessSendRtspMessage(resp, session, sendResponse); resp = null; }
/// <summary> /// /// </summary> /// <param name="request"></param> /// <param name="session"></param> internal void ProcessRtspPlay(RtspMessage request, ClientSession session, bool sendResponse = true) { RtpSource found = FindStreamByLocation(request.Location) as RtpSource; if (found == null) { ProcessLocationNotFoundRtspRequest(session, sendResponse); return; } if (false == AuthenticateRequest(request, found)) { ProcessAuthorizationRequired(found, session, sendResponse); return; } else if (false == found.Ready) { //Stream is not yet ready ProcessInvalidRtspRequest(session, RtspStatusCode.PreconditionFailed, null, sendResponse); return; } //New method... TryCreateResponse: try { RtspMessage resp = session.ProcessPlay(request, found); //Send the response to the client ProcessSendRtspMessage(resp, session, sendResponse); if (resp.StatusCode == RtspStatusCode.OK) { //Take the range into account given. session.ProcessPacketBuffer(found); } resp = null; } catch(Exception ex) { if(Logger != null) Logger.LogException(ex); if (ex is InvalidOperationException) goto TryCreateResponse; throw; } }
/// <summary> /// Disposes and tries to remove the session from the Clients Collection. /// </summary> /// <param name="session">The session to remove</param> /// <returns>True if the session was disposed</returns> internal bool TryDisposeAndRemoveSession(ClientSession session) { if (session == null) return false; Exception any = null; try { //If the session was not disposed if (false == session.IsDisposed) { //indicate the session is disposing Common.ILoggingExtensions.Log(Logger, "Disposing Client: " + session.Id + " @ " + DateTime.UtcNow); //Dispose the session session.Dispose(); } //If the client was already removed indicate this in the logs if (false == Common.Collections.DictionaryExtensions.TryRemove(m_Clients, session.Id, out any)) Common.ILoggingExtensions.Log(Logger, "Client Already Removed(" + session.Id + ")"); //Indicate success return true; } catch (Exception ex) { if (Logger != null) Logger.LogException(ex); //Indicate if a failure occured return session == null || session.IsDisposed; } finally { if (Logger != null && any != null) Logger.LogException(any); } }
internal void HandleClientSocketException(SocketException se, ClientSession cs) { if(se == null || cs == null || cs.IsDisposed) return; switch (se.SocketErrorCode) { case SocketError.TimedOut: { //Clients interleaving shouldn't time out ever if (cs.m_RtspSocket.ProtocolType == ProtocolType.Tcp) { Common.ILoggingExtensions.Log(Logger, "Client:" + cs.Id + " Timeouts when interleaving"); if(cs.Playing.Count > 0) return; } //Increase send and receive timeout cs.m_RtspSocket.SendTimeout = cs.m_RtspSocket.ReceiveTimeout += (int)(RtspClient.DefaultConnectionTime.TotalMilliseconds); Common.ILoggingExtensions.Log(Logger, "Increased Client:" + cs.Id + " Timeouts to: " + cs.m_RtspSocket.SendTimeout); //Try to receive again cs.StartReceive(); return; } case SocketError.ConnectionAborted: case SocketError.ConnectionReset: case SocketError.Disconnecting: case SocketError.Shutdown: case SocketError.NotConnected: { Common.ILoggingExtensions.Log(Logger, "Marking Client:" + cs.Id + " Disconnected"); //Mark the client as disconnected. cs.IsDisconnected = true; return; } default: { if (Logger != null) Logger.LogException(se); return; } } }
/// <summary> /// Sends a Rtsp LocationNotFound Response /// </summary> /// <param name="ci">The session to send the response on</param> internal void ProcessLocationNotFoundRtspRequest(ClientSession ci, bool sendResponse = true) { ProcessInvalidRtspRequest(ci, RtspStatusCode.NotFound, null, sendResponse); }
/// <summary> /// Handles the SET_PARAMETER RtspRequest /// </summary> /// <param name="request">The GET_PARAMETER RtspRequest to handle</param> /// <param name="ci">The RtspSession from which the request was receieved</param> internal void ProcessSetParameter(RtspMessage request, ClientSession session, bool sendResponse = true) { //Could be used for PTZ or other stuff //Should have a way to determine to forward send parameters... public bool ForwardSetParameter { get; set; } //Should have a way to call SendSetParamter on the source if required. //Should allow sever parameters to be set? using (var resp = session.CreateRtspResponse(request)) { //Content type ProcessSendRtspMessage(resp, session, sendResponse); } }
/// <summary> /// Sends a Rtsp Response on the given client session /// </summary> /// <param name="ci">The client session to send the response on</param> /// <param name="code">The status code of the response if other than BadRequest</param> //Should allow a header to be put into the response or a KeyValuePair<string,string> headers internal void ProcessInvalidRtspRequest(ClientSession session, RtspStatusCode code = RtspStatusCode.BadRequest, string body = null, bool sendResponse = true) { //Create and Send the response ProcessInvalidRtspRequest(session != null ? session.CreateRtspResponse(null, code, body) : new RtspMessage(RtspMessageType.Response) { StatusCode = code, Body = body, CSeq = Utility.Random.Next() }, session); }
internal void ProcessInvalidRtspRequest(RtspMessage response, ClientSession session, bool sendResponse = true) { ProcessSendRtspMessage(response, session, sendResponse); }
/// <summary> /// Handles the GET_PARAMETER RtspRequest /// </summary> /// <param name="request">The GET_PARAMETER RtspRequest to handle</param> /// <param name="ci">The RtspSession from which the request was receieved</param> internal void ProcessGetParameter(RtspMessage request, ClientSession session, bool sendResponse = true) { //TODO Determine API //packets_sent //jitter //rtcp_interval var resp = session.CreateRtspResponse(request); resp.SetHeader(RtspHeaders.Connection, "Keep-Alive"); ProcessSendRtspMessage(resp, session, sendResponse); resp = null; }
internal void ProcessClientBuffer(ClientSession session, int received) { if (session == null || session.IsDisposed || received <= 0) return; try { //Use a segment around the data received which is already in the buffer. using (Common.MemorySegment data = new Common.MemorySegment(session.m_Buffer.Array, session.m_Buffer.Offset, received)) { if (data[0] == RtpClient.BigEndianFrameControl) { if (session.m_RtpClient == null) return; received -= session.m_RtpClient.ProcessFrameData(session.m_Buffer.Array, session.m_Buffer.Offset, received, session.m_RtspSocket); } if (received <= 0) return; //Ensure the message is really Rtsp RtspMessage request = new RtspMessage(data); //Check for validity if (request.MessageType != RtspMessageType.Invalid) { //Process the request when complete if (request.IsComplete) { ProcessRtspRequest(request, session); return; } } //Otherwise if LastRequest is not disposed then attempt completion with the invalid data else if (session.LastRequest != null && false == session.LastRequest.IsDisposed) { //Indicate a CompleteFrom is occuring Media.Common.ILoggingExtensions.Log(Logger, "Session:" + session.Id + " Attempting to complete previous mesage with buffer of " + data.Count + " bytes."); //Attempt to complete it received = session.LastRequest.CompleteFrom(null, data); //If nothing was recieved then do nothing. if (received == 0) { Media.Common.ILoggingExtensions.Log(Logger, "Session:" + session.Id + "Did not use buffer of " + data.Count + " bytes."); //return; } Media.Common.ILoggingExtensions.Log(Logger, "Session:" + session.Id + " used " + received + " of buffer bytes"); //Account for the received data session.m_Receieved += received; //Determine how to process the messge switch (session.LastRequest.MessageType) { case RtspMessageType.Response: case RtspMessageType.Invalid: { //Ensure the session is still connected. session.SendRtspData(Media.Common.MemorySegment.EmptyBytes); return; } case RtspMessageType.Request: { //If the request is complete now then process it if (session.LastRequest.IsComplete) { //Process the request (it may not be complete yet) ProcessRtspRequest(session.LastRequest, session); return; } goto case RtspMessageType.Invalid; } } } //Log the invalid request Media.Common.ILoggingExtensions.Log(Logger, "Received Invalid Message:" + request + " \r\nFor Session:" + session.Id); if (session.LastRequest != null) session.LastRequest.Dispose(); //Store it for now to allow completion. session.LastRequest = request; //Ensure the session is still connected. session.SendRtspData(Media.Common.MemorySegment.EmptyBytes); } } catch(Exception ex) { if (Logger != null) Logger.LogException(ex); } }
internal virtual void ProcessAuthorizationRequired(IMedia source, ClientSession session, bool sendResponse = true) { RtspMessage response = new RtspMessage(RtspMessageType.Response); response.CSeq = session.LastRequest.CSeq; string authHeader = session.LastRequest.GetHeader(RtspHeaders.Authorization); RtspStatusCode statusCode; bool noAuthHeader = string.IsNullOrWhiteSpace(authHeader); if (noAuthHeader && session.LastRequest.ContainsHeader(RtspHeaders.AcceptCredentials)) statusCode = RtspStatusCode.ConnectionAuthorizationRequired; //If the last request did not have an authorization header else if (noAuthHeader) { /* -- http://tools.ietf.org/html/rfc2617 qop Indicates what "quality of protection" the client has applied to the message. If present, its value MUST be one of the alternatives the server indicated it supports in the WWW-Authenticate header. These values affect the computation of the request-digest. Note that this is a single token, not a quoted list of alternatives as in WWW- Authenticate. This directive is optional in order to preserve backward compatibility with a minimal implementation of RFC 2617 [6], but SHOULD be used if the server indicated that qop is supported by providing a qop directive in the WWW-Authenticate header field. cnonce This MUST be specified if a qop directive is sent (see above), and MUST NOT be specified if the server did not send a qop directive in the WWW-Authenticate header field. The cnonce-value is an opaque quoted string value provided by the client and used by both client and server to avoid chosen plaintext attacks, to provide mutual authentication, and to provide some message integrity protection. See the descriptions below of the calculation of the response- digest and request-digest values. */ //Could retrieve values from last Request if needed.. //string realm = "//", nOnceCount = "00000001"; //Should handle multiple types of auth //Should store the nonce and cnonce values on the session statusCode = RtspStatusCode.Unauthorized; NetworkCredential requiredCredential = null; string authenticateHeader = null; Uri relativeLocation = source.ServerLocation; //Check for Digest first - Todo Finish implementation if ((requiredCredential = RequiredCredentials.GetCredential(relativeLocation, "Digest")) != null) { //Might need to store values qop nc, cnonce and nonce in session storage for later retrival //Should use auth-int and qop authenticateHeader = string.Format(System.Globalization.CultureInfo.InvariantCulture, "Digest username={0},realm={1},nonce={2},cnonce={3}", requiredCredential.UserName, (string.IsNullOrWhiteSpace(requiredCredential.Domain) ? ServerName : requiredCredential.Domain), ((long)(Utility.Random.Next(int.MaxValue) << 32 | (Utility.Random.Next(int.MaxValue)))).ToString("X"), Utility.Random.Next(int.MaxValue).ToString("X")); } else if ((requiredCredential = RequiredCredentials.GetCredential(relativeLocation, "Basic")) != null) { authenticateHeader = "Basic realm=\"" + (string.IsNullOrWhiteSpace(requiredCredential.Domain) ? ServerName : requiredCredential.Domain + '"'); } if (!string.IsNullOrWhiteSpace(authenticateHeader)) { response.SetHeader(RtspHeaders.WWWAuthenticate, authenticateHeader); } } else //Authorization header was present but data was incorrect { //Parse type from authHeader string[] parts = authHeader.Split((char)Common.ASCII.Space); string authType = null; if (parts.Length > 0) { authType = parts[0]; authHeader = parts[1]; } //should check to ensure wrong type was not used e.g. basic in place of digest... if (string.Compare(authType, "digest", true) == 0) { //if (session.Storage["nOnce"] != null) //{ // //Increment NonceCount //} } //Increment session attempts? statusCode = RtspStatusCode.Forbidden; } //Set the status code response.StatusCode = statusCode; //Send the response ProcessInvalidRtspRequest(response, session, sendResponse); }
/// <summary> /// Ends the client session /// </summary> /// <param name="request">The Teardown request</param> /// <param name="session">The session which recieved the request</param> internal void ProcessRtspTeardown(RtspMessage request, ClientSession session, bool sendResponse = true) { //If there was a location which was not a wildcard attempt to honor it. if (request.Location != RtspMessage.Wildcard) { RtpSource found = FindStreamByLocation(request.Location) as RtpSource; if (found == null) { ProcessLocationNotFoundRtspRequest(session, sendResponse); return; } if (false == AuthenticateRequest(request, found)) { ProcessAuthorizationRequired(found, session, sendResponse); return; } //Send the response var resp = session.ProcessTeardown(request, found); ////Keep track if LeaveOpen should be set (based on if the session still shared the socket) //if (false == (session.LeaveOpen = session.SharesSocket)) //{ // //if it doesn't then inform that close may occur? // resp.AppendOrSetHeader(RtspHeaders.Connection, "close"); //} ProcessSendRtspMessage(resp, session, sendResponse); session.ReleaseUnusedResources(); resp = null; //Attempt to remove the sessionId when nothing is playing... why? //10.4 SETUP says we would have to bundle pipelined requests. //That is contradictory. //if (session.Playing.Count == 0) session.SessionId = null; string connectionHeader = request[RtspHeaders.Connection]; if (false == string.IsNullOrWhiteSpace(connectionHeader)) { if (string.Compare(connectionHeader.Trim(), "close", true) == 0) { TryDisposeAndRemoveSession(session); } } } else { //Create the response var resp = session.CreateRtspResponse(request); //Todo //Make a RtpInfo header for each stream ending... //Stop transport level activity if required if (session.m_RtpClient != null && session.m_RtpClient.IsActive) session.m_RtpClient.SendGoodbyes(); //Remove all attachments and clear playing session.RemoveAllAttachmentsAndClearPlaying(); //Send the response ProcessSendRtspMessage(resp, session, sendResponse); //Release any unused resources at this point session.ReleaseUnusedResources(); //Remove the sessionId session.SessionId = null; resp = null; string connectionHeader = request[RtspHeaders.Connection]; if (false == string.IsNullOrWhiteSpace(connectionHeader)) { if (string.Compare(connectionHeader.Trim(), "close", true) == 0) { TryDisposeAndRemoveSession(session); } } } }
internal void ProcessRedirect(RtspMessage request, ClientSession session, bool sendResponse = true) { var found = FindStreamByLocation(request.Location); if (found == null) { ProcessLocationNotFoundRtspRequest(session, sendResponse); return; } if (false == AuthenticateRequest(request, found)) { ProcessAuthorizationRequired(found, session, sendResponse); return; } if (false == found.Ready) { ProcessInvalidRtspRequest(session, RtspStatusCode.PreconditionFailed, null, sendResponse); return; } using (var resp = session.CreateRtspResponse(request)) { resp.Method = RtspMethod.REDIRECT; resp.AppendOrSetHeader(RtspHeaders.Location, "rtsp://" + session.LocalEndPoint.Address.ToString() + "/live/" + found.Id); ProcessSendRtspMessage(resp, session, sendResponse); } }
/// <summary> /// Sends a Rtsp Response on the given client session /// </summary> /// <param name="message">The RtspResponse to send</param> If this was byte[] then it could handle http /// <param name="ci">The session to send the response on</param> internal void ProcessSendRtspMessage(RtspMessage message, ClientSession session, bool sendResponse = true) { //Todo have a SupportedFeatures and RequiredFeatures HashSet. //Check Require Header // And /* Add Unsupported Header if needed Require: play.basic, con.persistent (basic play, TCP is supported) setup.playing means that setup and teardown can be used in the play state. */ //If we have a session if (session == null || session.IsDisposed) return; try { //if there is a message to send if (message != null && false == message.IsDisposed) { //AddServerHeaders()-> if (false == message.ContainsHeader(RtspHeaders.Server)) message.SetHeader(RtspHeaders.Server, ServerName); if (false == message.ContainsHeader(RtspHeaders.Date)) message.SetHeader(RtspHeaders.Date, DateTime.UtcNow.ToString("r")); #region RFC2326 12.38 Timestamp / Delay /* 12.38 Timestamp The timestamp general header describes when the client sent the request to the server. The value of the timestamp is of significance only to the client and may use any timescale. The server MUST echo the exact same value and MAY, if it has accurate information about this, add a floating point number indicating the number of seconds that has elapsed since it has received the request. The timestamp is used by the client to compute the round-trip time to the server so that it can adjust the timeout value for retransmissions. Timestamp = "Timestamp" ":" *(DIGIT) [ "." *(DIGIT) ] [ delay ] delay = *(DIGIT) [ "." *(DIGIT) ] */ if (false == message.ContainsHeader(RtspHeaders.Timestamp) && session.LastRequest != null && session.LastRequest.ContainsHeader(RtspHeaders.Timestamp)) { ////Apparently not joined with ; ////message.SetHeader(RtspHeaders.Timestamp, session.LastRequest[RtspHeaders.Timestamp] + "delay=" + (DateTime.UtcNow - session.LastRequest.Created).TotalSeconds); //Set the value of the Timestamp header as given message.AppendOrSetHeader(RtspHeaders.Timestamp, session.LastRequest[RtspHeaders.Timestamp]); //Add a delay datum message.AppendOrSetHeader(RtspHeaders.Timestamp, "delay=" + (DateTime.UtcNow - session.LastRequest.Created).TotalSeconds); } #endregion string sess = message.GetHeader(RtspHeaders.Session); //Check for a session header if (false == string.IsNullOrWhiteSpace(sess)) { //Add the timeout header if there was a session header. if (RtspClientInactivityTimeout > TimeSpan.Zero && false == sess.Contains("timeout")) message.AppendOrSetHeader(RtspHeaders.Session, "timeout=" + (int)(RtspClientInactivityTimeout.TotalSeconds / 2)); } //Oops //if (session.m_RtspSocket.ProtocolType == ProtocolType.Tcp && session.Attached.Count > 0) response.SetHeader("Ignore", "$0\09\r\n$\0:\0"); //Dispose the last response if (session.LastResponse != null) session.LastResponse.Dispose(); //Todo //Content-Encoding should be the same as the request's if possible.. //If sending a response if (sendResponse) { //Log response if (Logger != null) Logger.LogResponse(message, session); session.SendRtspData((session.LastResponse = message).ToBytes()); }//Otherwise just update the property else session.LastResponse = message; //Indicate the session is not disconnected session.IsDisconnected = false; //Indicate the last response was sent now. session.LastResponse.Transferred = DateTime.UtcNow; } else { //Test the connectivity and start another receive session.SendRtspData(Media.Common.MemorySegment.EmptyBytes); } } catch (Exception ex) { if (Logger != null) Logger.LogException(ex); //if a socket exception occured then handle it. if (session != null && ex is SocketException) HandleClientSocketException((SocketException)ex, session); } }
/// <summary> /// Decribes the requested stream /// </summary> /// <param name="request"></param> /// <param name="session"></param> internal void ProcessRtspDescribe(RtspMessage request, ClientSession session, bool sendResponse = true) { if (request.Location == RtspMessage.Wildcard) { var resp = session.CreateRtspResponse(request); ProcessSendRtspMessage(resp, session, sendResponse); resp = null; } else { string acceptHeader = request[RtspHeaders.Accept]; //If an Accept header was given it must reflect a content-type of SDP. if (false == string.IsNullOrWhiteSpace(acceptHeader) && string.Compare(acceptHeader, Sdp.SessionDescription.MimeType, true, System.Globalization.CultureInfo.InvariantCulture) > 0) { ProcessInvalidRtspRequest(session); return; } RtpSource found = FindStreamByLocation(request.Location) as RtpSource; if (found == null) { ProcessLocationNotFoundRtspRequest(session, sendResponse); return; } if (false == AuthenticateRequest(request, found)) { ProcessAuthorizationRequired(found, session, sendResponse); return; } if (false == found.Ready) { ProcessInvalidRtspRequest(session, RtspStatusCode.PreconditionFailed, null, sendResponse); return; } var resp = session.ProcessDescribe(request, found); ProcessSendRtspMessage(resp, session, sendResponse); resp = null; } }
internal bool TryAddSession(ClientSession session) { Exception any = null; try { Common.ILoggingExtensions.Log(Logger, "Adding Client: " + session.Id); if (Common.Collections.DictionaryExtensions.TryAdd(m_Clients, session.Id, session, out any)) { session.m_Contained = this; } return session.m_Contained == this; } catch(Exception ex) { if (Logger != null) Logger.LogException(ex); return false; } finally { if (any != null && Logger != null) Logger.LogException(any); } }
/// <summary> /// Provides the Options this server supports /// </summary> /// <param name="request"></param> /// <param name="session"></param> internal void ProcessRtspOptions(RtspMessage request, ClientSession session, bool sendResponse = true) { //See if the location requires a certain stream if (request.Location != RtspMessage.Wildcard && false == string.IsNullOrWhiteSpace(request.Location.LocalPath) && request.Location.LocalPath.Length > 1) { IMedia found = FindStreamByLocation(request.Location); //No stream with name if (found == null) { ProcessLocationNotFoundRtspRequest(session, sendResponse); return; } //See if RECORD is supported? } //Check for additional options of the stream... e.g. allow recording or not RtspMessage resp = session.CreateRtspResponse(request); resp.SetHeader(RtspHeaders.Public, "OPTIONS, DESCRIBE, SETUP, PLAY, PAUSE, TEARDOWN, GET_PARAMETER"); //Allow for Authorized resp.SetHeader(RtspHeaders.Allow, "ANNOUNCE, RECORD, SET_PARAMETER"); //Add from handlers? //if(m_RequestHandlers.Count > 0) string.Join(" ", m_RequestHandlers.Keys.ToArray()) //Should allow server to have certain options removed from this result //ClientSession.ProcessOptions ProcessSendRtspMessage(resp, session, sendResponse); resp = null; }
private void ProcessRtspRecord(RtspMessage request, ClientSession session, bool sendResponse = true) { var found = FindStreamByLocation(request.Location); if (found == null) { ProcessLocationNotFoundRtspRequest(session, sendResponse); return; } if (false == AuthenticateRequest(request, found)) { ProcessAuthorizationRequired(found, session, sendResponse); return; } if (false == found.Ready || Archiver == null) { ProcessInvalidRtspRequest(session, RtspStatusCode.PreconditionFailed, null, sendResponse); return; } using (var resp = session.ProcessRecord(request, found)) { ProcessSendRtspMessage(resp, session, sendResponse); } }
internal bool ContainsSession(ClientSession session) { return m_Clients.ContainsKey(session.Id); }