RtpPacket.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.ObjectModel;
  16. using System.Text;
  17. namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts.SatIp.Rtp
  18. {
  19. public class RtpPacket
  20. {
  21. private static int MinHeaderLength = 12;
  22. public int HeaderSize = MinHeaderLength;
  23. public int Version { get; set; }
  24. public Boolean Padding { get; set; }
  25. public Boolean Extension { get; set; }
  26. public int ContributingSourceCount { get; set; }
  27. public Boolean Marker { get; set; }
  28. public int PayloadType { get; set; }
  29. public int SequenceNumber { get; set; }
  30. public long TimeStamp { get; set; }
  31. public long SynchronizationSource { get; set; }
  32. public Collection<string> ContributingSources { get; private set; }
  33. public int ExtensionHeaderId = 0;
  34. public int ExtensionHeaderLength = 0;
  35. public bool HasPayload { get; set; }
  36. public byte[] Payload { get; set; }
  37. public RtpPacket()
  38. {
  39. }
  40. public static RtpPacket Decode(byte[] buffer)
  41. {
  42. var packet = new RtpPacket();
  43. packet.Version = buffer[0] >> 6;
  44. packet.Padding = (buffer[0] & 0x20) != 0;
  45. packet.Extension = (buffer[0] & 0x10) != 0;
  46. packet.ContributingSourceCount = buffer[0] & 0x0f;
  47. packet.Marker = (buffer[1] & 0x80) != 0;
  48. packet.PayloadType = buffer[1] & 0x7f;
  49. packet.SequenceNumber = Utils.Convert2BytesToInt(buffer, 2);
  50. packet.TimeStamp = Utils.Convert4BytesToLong(buffer, 4);
  51. packet.SynchronizationSource = Utils.Convert4BytesToLong(buffer, 8);
  52. int index = 12;
  53. if (packet.ContributingSourceCount != 0)
  54. {
  55. packet.ContributingSources = new Collection<string>();
  56. while (packet.ContributingSources.Count < packet.ContributingSourceCount)
  57. {
  58. packet.ContributingSources.Add(Utils.ConvertBytesToString(buffer, index, 4));
  59. index += 4;
  60. }
  61. }
  62. var dataoffset = 0;
  63. if (!packet.Extension)
  64. dataoffset = index;
  65. else
  66. {
  67. packet.ExtensionHeaderId = Utils.Convert2BytesToInt(buffer, index);
  68. packet.ExtensionHeaderLength = Utils.Convert2BytesToInt(buffer, index + 2);
  69. dataoffset = index + packet.ExtensionHeaderLength + 4;
  70. }
  71. var dataLength = buffer.Length - dataoffset;
  72. if (dataLength > dataoffset)
  73. {
  74. packet.HasPayload = true;
  75. packet.Payload = new byte[dataLength];
  76. Array.Copy(buffer, dataoffset, packet.Payload, 0, dataLength);
  77. }
  78. else
  79. {
  80. packet.HasPayload = false;
  81. }
  82. return packet;
  83. }
  84. public override string ToString()
  85. {
  86. StringBuilder sb = new StringBuilder();
  87. sb.AppendFormat("RTP Packet");
  88. sb.AppendFormat("Version: {0} \n", Version);
  89. sb.AppendFormat("Padding: {0} \n", Padding);
  90. sb.AppendFormat("Extension: {0} \n", Extension);
  91. sb.AppendFormat("Contributing Source Identifiers Count: {0} \n", ContributingSourceCount);
  92. sb.AppendFormat("Marker: {0} \n", Marker);
  93. sb.AppendFormat("Payload Type: {0} \n", PayloadType);
  94. sb.AppendFormat("Sequence Number: {0} \n", SequenceNumber);
  95. sb.AppendFormat("Timestamp: {0} .\n", TimeStamp);
  96. sb.AppendFormat("Synchronization Source Identifier: {0} \n", SynchronizationSource);
  97. sb.AppendFormat("\n");
  98. return sb.ToString();
  99. }
  100. }
  101. }