Ap4ContainerAtom.cpp
上传用户:xjjlds
上传日期:2015-12-05
资源大小:22823k
文件大小:6k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - Container Atoms
  4. |
  5. |    Copyright 2002 Gilles Boccon-Gibod
  6. |
  7. |
  8. |    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
  9. |
  10. |    Unless you have obtained Bento4 under a difference license,
  11. |    this version of Bento4 is Bento4|GPL.
  12. |    Bento4|GPL is free software; you can redistribute it and/or modify
  13. |    it under the terms of the GNU General Public License as published by
  14. |    the Free Software Foundation; either version 2, or (at your option)
  15. |    any later version.
  16. |
  17. |    Bento4|GPL is distributed in the hope that it will be useful,
  18. |    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. |    GNU General Public License for more details.
  21. |
  22. |    You should have received a copy of the GNU General Public License
  23. |    along with Bento4|GPL; see the file COPYING.  If not, write to the
  24. |    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  25. |    02111-1307, USA.
  26. |
  27.  ****************************************************************/
  28. /*----------------------------------------------------------------------
  29. |       includes
  30. +---------------------------------------------------------------------*/
  31. #include "Ap4.h"
  32. #include "Ap4Atom.h"
  33. #include "Ap4Utils.h"
  34. #include "Ap4ContainerAtom.h"
  35. #include "Ap4AtomFactory.h"
  36. /*----------------------------------------------------------------------
  37. |       AP4_ContainerAtom::AP4_ContainerAtom
  38. +---------------------------------------------------------------------*/
  39. AP4_ContainerAtom::AP4_ContainerAtom(Type type, bool is_full) :
  40.     AP4_Atom(type, is_full)
  41. {
  42. }
  43. /*----------------------------------------------------------------------
  44. |       AP4_ContainerAtom::AP4_ContainerAtom
  45. +---------------------------------------------------------------------*/
  46. AP4_ContainerAtom::AP4_ContainerAtom(Type type, AP4_Size size, bool is_full) :
  47.     AP4_Atom(type, size, is_full)
  48. {
  49. }
  50. /*----------------------------------------------------------------------
  51. |       AP4_ContainerAtom::AP4_ContainerAtom
  52. +---------------------------------------------------------------------*/
  53. AP4_ContainerAtom::AP4_ContainerAtom(Type             type, 
  54.                                      AP4_Size         size,
  55.                                      bool             is_full,
  56.                                      AP4_ByteStream&  stream,
  57.                                      AP4_AtomFactory& atom_factory) :
  58.     AP4_Atom(type, size, is_full, stream)
  59. {
  60.     ReadChildren(atom_factory, stream, size-GetHeaderSize());
  61. }
  62. /*----------------------------------------------------------------------
  63. |       AP4_ContainerAtom::AP4_ContainerAtom
  64. +---------------------------------------------------------------------*/
  65. AP4_ContainerAtom::AP4_ContainerAtom(Type             type, 
  66.                                      AP4_Size         size,
  67.                                      bool             is_full,
  68.                                      AP4_ByteStream&  stream) :
  69.     AP4_Atom(type, size, is_full, stream)
  70. {
  71. }
  72. /*----------------------------------------------------------------------
  73. |       AP4_ContainerAtom::ReadChildren
  74. +---------------------------------------------------------------------*/
  75. void
  76. AP4_ContainerAtom::ReadChildren(AP4_AtomFactory& atom_factory,
  77.                                 AP4_ByteStream&  stream, 
  78.                                 AP4_Size         size)
  79. {
  80.     AP4_Atom* atom;
  81.     AP4_Size  bytes_available = size;
  82.     while (AP4_SUCCEEDED(
  83.         atom_factory.CreateAtomFromStream(stream, bytes_available, atom))) {
  84.         atom->SetParent(this);
  85.         m_Children.Add(atom);
  86.     }
  87. }
  88. /*----------------------------------------------------------------------
  89. |       AP4_ContainerAtom::InspectFields
  90. +---------------------------------------------------------------------*/
  91. AP4_Result
  92. AP4_ContainerAtom::InspectFields(AP4_AtomInspector& inspector)
  93. {
  94.     return InspectChildren(inspector);
  95. }
  96. /*----------------------------------------------------------------------
  97. |       AP4_ContainerAtom::InspectChildren
  98. +---------------------------------------------------------------------*/
  99. AP4_Result
  100. AP4_ContainerAtom::InspectChildren(AP4_AtomInspector& inspector)
  101. {
  102.     // inspect children
  103.     m_Children.Apply(AP4_AtomListInspector(inspector));
  104.     return AP4_SUCCESS;
  105. }
  106. /*----------------------------------------------------------------------
  107. |       AP4_ContainerAtom::WriteFields
  108. +---------------------------------------------------------------------*/
  109. AP4_Result
  110. AP4_ContainerAtom::WriteFields(AP4_ByteStream& stream)
  111. {
  112.     // write all children
  113.     return m_Children.Apply(AP4_AtomListWriter(stream));
  114. }
  115. /*----------------------------------------------------------------------
  116. |       AP4_ContainerAtom::OnChildChanged
  117. +---------------------------------------------------------------------*/
  118. void
  119. AP4_ContainerAtom::OnChildChanged(AP4_Atom*)
  120. {
  121.     // remcompute our size
  122.     m_Size = GetHeaderSize();
  123.     m_Children.Apply(AP4_AtomSizeAdder(m_Size));
  124.     // update our parent
  125.     if (m_Parent) m_Parent->OnChildChanged(this);
  126. }
  127. /*----------------------------------------------------------------------
  128. |       AP4_ContainerAtom::OnChildAdded
  129. +---------------------------------------------------------------------*/
  130. void
  131. AP4_ContainerAtom::OnChildAdded(AP4_Atom* child)
  132. {
  133.     // update our size
  134.     m_Size += child->GetSize();
  135.     // update our parent
  136.     if (m_Parent) m_Parent->OnChildChanged(this);
  137. }
  138. /*----------------------------------------------------------------------
  139. |       AP4_ContainerAtom::OnChildRemoved
  140. +---------------------------------------------------------------------*/
  141. void
  142. AP4_ContainerAtom::OnChildRemoved(AP4_Atom* child)
  143. {
  144.     // update our size
  145.     m_Size -= child->GetSize();
  146.     // update our parent
  147.     if (m_Parent) m_Parent->OnChildChanged(this);
  148. }