BaseMedia.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Diagnostics;
  7. namespace MatrixIO.IO.Bmff
  8. {
  9. /// <summary>
  10. /// BaseMedia encapsulates a tree of Boxes in a random-access stream.
  11. /// </summary>
  12. public class BaseMedia : ISuperBox
  13. {
  14. protected Stream _SourceStream;
  15. private const string DefaultName = "Unknown File";
  16. public BaseMedia() { }
  17. public BaseMedia(Stream stream)
  18. {
  19. if (!stream.CanSeek) new ArgumentException("BaseMedia requires a random-access stream.");
  20. _SourceStream = stream;
  21. }
  22. private bool _childrenLoaded = false;
  23. private readonly IList<Box> _children = Portability.CreateList<Box>();
  24. public IList<Box> Children
  25. {
  26. get
  27. {
  28. if (!_childrenLoaded)
  29. {
  30. _childrenLoaded = true;
  31. if (_SourceStream != null)
  32. {
  33. //Debug.WriteLine("Loading Children: ");
  34. Box box = null;
  35. do
  36. {
  37. box = Box.FromStream(_SourceStream);
  38. if (box != null)
  39. {
  40. _children.Add(box);
  41. //Debug.WriteLine("\t" + box);
  42. }
  43. } while (box != null);
  44. }
  45. }
  46. return _children;
  47. }
  48. }
  49. public void SaveTo(Stream stream)
  50. {
  51. foreach (Box box in _children)
  52. {
  53. box.ToStream(stream);
  54. }
  55. }
  56. protected ulong GetBoxWriteSize(Box box)
  57. {
  58. return box.CalculateSize();
  59. }
  60. public IEnumerator<Box> GetEnumerator()
  61. {
  62. return Children.GetEnumerator();
  63. }
  64. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  65. {
  66. return Children.GetEnumerator();
  67. }
  68. }
  69. }