/* Copyright (C) <2007-2016> SatIp.RtspSample is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. SatIp.RtspSample is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with SatIp.RtspSample. If not, see . */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts.SatIp.Rtsp { /// /// A simple class that can be used to deserialise RTSP responses. /// public class RtspResponse { private static readonly Regex RegexStatusLine = new Regex(@"RTSP/(\d+)\.(\d+)\s+(\d+)\s+([^.]+?)\r\n(.*)", RegexOptions.Singleline); private int _majorVersion = 1; private int _minorVersion; private RtspStatusCode _statusCode; private string _reasonPhrase; private IDictionary _headers; private string _body; /// /// Initialise a new instance of the class. /// private RtspResponse() { } /// /// Get the response major version number. /// public int MajorVersion { get { return _majorVersion; } } /// /// Get the response minor version number. /// public int MinorVersion { get { return _minorVersion; } } /// /// Get the response status code. /// public RtspStatusCode StatusCode { get { return _statusCode; } } /// /// Get the response reason phrase. /// public string ReasonPhrase { get { return _reasonPhrase; } } /// /// Get the response headers. /// public IDictionary Headers { get { return _headers; } } /// /// Get the response body. /// public string Body { get { return _body; } set { _body = value; } } /// /// Deserialise/parse an RTSP response. /// /// The raw response bytes. /// The number of valid bytes in the response. /// a response object public static RtspResponse Deserialise(byte[] responseBytes, int responseByteCount) { var response = new RtspResponse(); var responseString = Encoding.UTF8.GetString(responseBytes, 0, responseByteCount); var m = RegexStatusLine.Match(responseString); if (m.Success) { response._majorVersion = int.Parse(m.Groups[1].Captures[0].Value); response._minorVersion = int.Parse(m.Groups[2].Captures[0].Value); response._statusCode = (RtspStatusCode)int.Parse(m.Groups[3].Captures[0].Value); response._reasonPhrase = m.Groups[4].Captures[0].Value; responseString = m.Groups[5].Captures[0].Value; } var sections = responseString.Split(new[] { "\r\n\r\n" }, StringSplitOptions.None); response._body = sections[1]; var headers = sections[0].Split(new[] { "\r\n" }, StringSplitOptions.None); response._headers = new Dictionary(); foreach (var headerInfo in headers.Select(header => header.Split(':'))) { response._headers.Add(headerInfo[0], headerInfo[1].Trim()); } return response; } } }