ProgramMapTable.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MatrixIO.IO.MpegTs.Descriptors;
  6. namespace MatrixIO.IO.MpegTs.Tables
  7. {
  8. public class ProgramMapTable : TsTable<ProgramMap>
  9. {
  10. public ushort ProgramNumber { get { return _identifier; } set { _identifier = value; } }
  11. private ushort _programClockReferencePid;
  12. public ushort ProgramClockReferencePid
  13. {
  14. get { return (ushort)(_programClockReferencePid & 0x1FFF); }
  15. set { _programClockReferencePid = (ushort)((_programClockReferencePid & 0xE000) | (value & 0x1FFF)); }
  16. }
  17. private ushort _programInfoLength;
  18. public ushort ProgramInfoLength
  19. {
  20. get { return (ushort)(_programInfoLength & 0x0FFF); }
  21. set { _programInfoLength = (ushort)((_programInfoLength & 0xF000) | (value & 0x0FFF)); }
  22. }
  23. public IList<TsDescriptor> ProgramInfo { get; private set; }
  24. public ProgramMapTable() { }
  25. public ProgramMapTable(byte[] buffer, int offset, int length)
  26. : base(buffer, offset, length)
  27. {
  28. _programClockReferencePid = (ushort)((buffer[_position++] << 8) | buffer[_position++]);
  29. _programInfoLength = (ushort)((buffer[_position++] << 8) | buffer[_position++]);
  30. ProgramInfo = Portability.CreateList<TsDescriptor>();
  31. var descriptorEndPosition = _position + ProgramInfoLength;
  32. while (_position < descriptorEndPosition)
  33. {
  34. var descriptor = new UnknownDescriptor(buffer, _position);
  35. ProgramInfo.Add(descriptor);
  36. _position += descriptor.Length;
  37. }
  38. while (_position < _sectionLengthStartOffset + SectionLength - 4 && _position < offset + length - 4)
  39. {
  40. var row = new ProgramMap(buffer, _position);
  41. _position += row.Length;
  42. Rows.Add(row);
  43. }
  44. }
  45. }
  46. public class ProgramMap
  47. {
  48. public int Length { get { return 1 + 2 + 2 + StreamInfo.Sum(d => d.Length); } }
  49. public StreamType StreamType { get; set; }
  50. public ushort PacketIdentifier { get; set; }
  51. public IList<TsDescriptor> StreamInfo { get; set; }
  52. public ProgramMap() { }
  53. public ProgramMap(byte[] buffer, int offset)
  54. {
  55. var position = offset;
  56. StreamType = (StreamType) buffer[position++];
  57. PacketIdentifier = (ushort) (((buffer[position++] << 8) | buffer[position++]) & 0x1FFF);
  58. var streamInfoLength = (ushort)(((buffer[position++] << 8) | buffer[position++]) & 0x0FFF);
  59. StreamInfo = Portability.CreateList<TsDescriptor>();
  60. var descriptorEndPosition = position + streamInfoLength;
  61. while (position < descriptorEndPosition)
  62. {
  63. var descriptor = new UnknownDescriptor(buffer, position);
  64. StreamInfo.Add(descriptor);
  65. position += descriptor.Length;
  66. }
  67. }
  68. }
  69. }