BaseMediaReader.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. namespace MatrixIO.IO.Bmff
  7. {
  8. /// <summary>
  9. /// BaseMediaReader allows forward-only reading with the ability to skip over boxes we aren't interested in.
  10. /// </summary>
  11. public class BaseMediaReader
  12. {
  13. public Stream BaseStream { get; private set; }
  14. public BaseMediaOptions Options { get; private set; }
  15. public BaseMediaReader(Stream stream, BaseMediaOptions options = BaseMediaOptions.None)
  16. {
  17. if (stream.CanSeek && stream.Position!=0) stream.Seek(0, SeekOrigin.Begin);
  18. BaseStream = stream;
  19. Options = options;
  20. }
  21. private readonly Stack<Box> _boxStack = new Stack<Box>();
  22. public string CurrentPath
  23. {
  24. get
  25. {
  26. var path = new StringBuilder();
  27. foreach(var box in _boxStack)
  28. {
  29. if(path.Length > 0) path.Append("|");
  30. path.Append(box.ToString());
  31. }
  32. return path.ToString();
  33. }
  34. }
  35. public int Depth
  36. {
  37. get
  38. {
  39. return _boxStack.Count;
  40. }
  41. }
  42. public Box CurrentBox
  43. {
  44. get
  45. {
  46. if(_boxStack.Count == 0) _boxStack.Push(Box.FromStream(BaseStream, BaseMediaOptions.None));
  47. return _boxStack.Peek();
  48. }
  49. }
  50. public bool HasChildren
  51. {
  52. get {
  53. return CurrentBox is ISuperBox;
  54. }
  55. }
  56. public void NextSibling()
  57. {
  58. CurrentBox.Sync(BaseStream, false);
  59. if (Depth > 1)
  60. {
  61. _boxStack.Pop();
  62. }
  63. }
  64. public void Next()
  65. {
  66. if (CurrentBox is ISuperBox)
  67. {
  68. // TODO: Add Next() functionality
  69. //var currentSuperBox = (ISuperBox)_CurrentBox;
  70. //if (currentSuperBox.Children != null & currentSuperBox.Children.Count > 0) ;
  71. throw new NotImplementedException();
  72. }
  73. else NextSibling();
  74. }
  75. }
  76. }