RtspResponse.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Copyright (C) <2007-2016> <Kay Diefenthal>
  3. SatIp.RtspSample is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. SatIp.RtspSample is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with SatIp.RtspSample. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Text.RegularExpressions;
  19. namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts.SatIp.SatIp
  20. {
  21. /// <summary>
  22. /// A simple class that can be used to deserialise RTSP responses.
  23. /// </summary>
  24. public class RtspResponse
  25. {
  26. private static readonly Regex RegexStatusLine = new Regex(@"RTSP/(\d+)\.(\d+)\s+(\d+)\s+([^.]+?)\r\n(.*)", RegexOptions.Singleline);
  27. private int _majorVersion = 1;
  28. private int _minorVersion;
  29. private RtspStatusCode _statusCode;
  30. private string _reasonPhrase;
  31. private IDictionary<string, string> _headers;
  32. private string _body;
  33. /// <summary>
  34. /// Initialise a new instance of the <see cref="RtspResponse"/> class.
  35. /// </summary>
  36. private RtspResponse()
  37. {
  38. }
  39. /// <summary>
  40. /// Get the response major version number.
  41. /// </summary>
  42. public int MajorVersion
  43. {
  44. get
  45. {
  46. return _majorVersion;
  47. }
  48. }
  49. /// <summary>
  50. /// Get the response minor version number.
  51. /// </summary>
  52. public int MinorVersion
  53. {
  54. get
  55. {
  56. return _minorVersion;
  57. }
  58. }
  59. /// <summary>
  60. /// Get the response status code.
  61. /// </summary>
  62. public RtspStatusCode StatusCode
  63. {
  64. get
  65. {
  66. return _statusCode;
  67. }
  68. }
  69. /// <summary>
  70. /// Get the response reason phrase.
  71. /// </summary>
  72. public string ReasonPhrase
  73. {
  74. get
  75. {
  76. return _reasonPhrase;
  77. }
  78. }
  79. /// <summary>
  80. /// Get the response headers.
  81. /// </summary>
  82. public IDictionary<string, string> Headers
  83. {
  84. get
  85. {
  86. return _headers;
  87. }
  88. }
  89. /// <summary>
  90. /// Get the response body.
  91. /// </summary>
  92. public string Body
  93. {
  94. get
  95. {
  96. return _body;
  97. }
  98. set
  99. {
  100. _body = value;
  101. }
  102. }
  103. /// <summary>
  104. /// Deserialise/parse an RTSP response.
  105. /// </summary>
  106. /// <param name="responseBytes">The raw response bytes.</param>
  107. /// <param name="responseByteCount">The number of valid bytes in the response.</param>
  108. /// <returns>a response object</returns>
  109. public static RtspResponse Deserialise(byte[] responseBytes, int responseByteCount)
  110. {
  111. var response = new RtspResponse();
  112. var responseString = Encoding.UTF8.GetString(responseBytes, 0, responseByteCount);
  113. var m = RegexStatusLine.Match(responseString);
  114. if (m.Success)
  115. {
  116. response._majorVersion = int.Parse(m.Groups[1].Captures[0].Value);
  117. response._minorVersion = int.Parse(m.Groups[2].Captures[0].Value);
  118. response._statusCode = (RtspStatusCode)int.Parse(m.Groups[3].Captures[0].Value);
  119. response._reasonPhrase = m.Groups[4].Captures[0].Value;
  120. responseString = m.Groups[5].Captures[0].Value;
  121. }
  122. var sections = responseString.Split(new[] { "\r\n\r\n" }, StringSplitOptions.None);
  123. response._body = sections[1];
  124. var headers = sections[0].Split(new[] { "\r\n" }, StringSplitOptions.None);
  125. response._headers = new Dictionary<string, string>();
  126. foreach (var headerInfo in headers.Select(header => header.Split(':')))
  127. {
  128. response._headers.Add(headerInfo[0], headerInfo[1].Trim());
  129. }
  130. return response;
  131. }
  132. }
  133. }