TsUnit.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace MatrixIO.IO.MpegTs
  6. {
  7. public class TsUnit
  8. {
  9. private readonly IList<TsPacket> _packets;
  10. public IList<TsPacket> Packets { get { return _packets; } }
  11. private readonly TsUnitPayload _payload;
  12. public IList<byte> Payload { get { return _payload; }}
  13. public TsUnit()
  14. {
  15. _packets = Portability.CreateList<TsPacket>();
  16. _payload = new TsUnitPayload(_packets);
  17. }
  18. public TsUnit(int capacity)
  19. {
  20. _packets = Portability.CreateList<TsPacket>(capacity);
  21. _payload = new TsUnitPayload(_packets);
  22. }
  23. public TsUnit(IEnumerable<TsPacket> packets)
  24. {
  25. _packets = Portability.CreateList(packets);
  26. _payload = new TsUnitPayload(_packets);
  27. }
  28. private class TsUnitPayload : IList<byte>
  29. {
  30. private readonly IList<TsPacket> _packets;
  31. public TsUnitPayload(IList<TsPacket> packets)
  32. {
  33. _packets = packets;
  34. }
  35. public int IndexOf(byte item)
  36. {
  37. throw new NotSupportedException();
  38. }
  39. public void Insert(int index, byte item)
  40. {
  41. throw new NotSupportedException();
  42. }
  43. public void RemoveAt(int index)
  44. {
  45. throw new NotSupportedException();
  46. }
  47. public byte this[int index]
  48. {
  49. get
  50. {
  51. foreach (var packet in _packets)
  52. {
  53. if (index < packet.Payload.Length) return packet.Payload[index];
  54. index -= packet.Payload.Length;
  55. }
  56. throw new IndexOutOfRangeException();
  57. }
  58. set
  59. {
  60. throw new NotSupportedException();
  61. }
  62. }
  63. public void Add(byte item)
  64. {
  65. throw new NotSupportedException();
  66. }
  67. public void Clear()
  68. {
  69. throw new NotSupportedException();
  70. }
  71. public bool Contains(byte item)
  72. {
  73. return _packets.Any(packet => packet.Payload.Contains(item));
  74. }
  75. public void CopyTo(byte[] array, int arrayIndex)
  76. {
  77. var offset = arrayIndex;
  78. foreach (var packet in _packets)
  79. {
  80. if (packet.Payload != null)
  81. {
  82. Buffer.BlockCopy(packet.Payload, 0, array, offset, packet.Payload.Length);
  83. offset += packet.Payload.Length;
  84. }
  85. }
  86. }
  87. public int Count
  88. {
  89. get
  90. {
  91. return _packets.Sum(packet => packet.Payload != null ? packet.Payload.Length : 0);
  92. }
  93. }
  94. public bool IsReadOnly
  95. {
  96. get { return true; }
  97. }
  98. public bool Remove(byte item)
  99. {
  100. throw new NotSupportedException();
  101. }
  102. public IEnumerator<byte> GetEnumerator()
  103. {
  104. return _packets.SelectMany(packet => packet.Payload).GetEnumerator();
  105. }
  106. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  107. {
  108. return GetEnumerator();
  109. }
  110. }
  111. }
  112. }