Ap4MoovAtom.cpp
上传用户:yuexiaohui
上传日期:2014-10-25
资源大小:3138k
文件大小:4k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

MultiPlatform

  1. /*****************************************************************
  2. |
  3. |    AP4 - moov Atoms 
  4. |
  5. |    Copyright 2002-2006 Gilles Boccon-Gibod & Julien Boeuf
  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 "Ap4MoovAtom.h"
  32. #include "Ap4TrakAtom.h"
  33. #include "Ap4AtomFactory.h"
  34. /*----------------------------------------------------------------------
  35. |   AP4_TrakAtomCollector
  36. +---------------------------------------------------------------------*/
  37. class AP4_TrakAtomCollector : public AP4_List<AP4_Atom>::Item::Operator
  38. {
  39. public:
  40.     AP4_TrakAtomCollector(AP4_List<AP4_TrakAtom>* track_atoms) :
  41.         m_TrakAtoms(track_atoms) {}
  42.     AP4_Result Action(AP4_Atom* atom) const {
  43.         if (atom->GetType() == AP4_ATOM_TYPE_TRAK) {
  44.             AP4_TrakAtom* trak = dynamic_cast<AP4_TrakAtom*>(atom);
  45.             if (trak) {
  46.                 m_TrakAtoms->Add(trak);
  47.             }
  48.         }
  49.         return AP4_SUCCESS;
  50.     }
  51. private:
  52.     AP4_List<AP4_TrakAtom>* m_TrakAtoms;
  53. };
  54. /*----------------------------------------------------------------------
  55. |   AP4_MoovAtom::AP4_MoovAtom
  56. +---------------------------------------------------------------------*/
  57. AP4_MoovAtom::AP4_MoovAtom() :
  58.     AP4_ContainerAtom(AP4_ATOM_TYPE_MOOV),
  59.     m_TimeScale(0)
  60. {
  61. }
  62. /*----------------------------------------------------------------------
  63. |   AP4_MoovAtom::AP4_MoovAtom
  64. +---------------------------------------------------------------------*/
  65. AP4_MoovAtom::AP4_MoovAtom(AP4_UI32         size,
  66.                            AP4_ByteStream&  stream,
  67.                            AP4_AtomFactory& atom_factory) :
  68.     AP4_ContainerAtom(AP4_ATOM_TYPE_MOOV, size, stream, atom_factory),
  69.     m_TimeScale(0)
  70. {
  71.     // collect all trak atoms
  72.     m_Children.Apply(AP4_TrakAtomCollector(&m_TrakAtoms));    
  73. }
  74. /*----------------------------------------------------------------------
  75. |   AP4_MoovAtom::AdjustChunkOffsets
  76. +---------------------------------------------------------------------*/
  77. AP4_Result 
  78. AP4_MoovAtom::AdjustChunkOffsets(AP4_SI64 offset)
  79. {
  80.     for (AP4_List<AP4_TrakAtom>::Item* item = m_TrakAtoms.FirstItem();
  81.          item;
  82.          item = item->GetNext()) {
  83.         AP4_TrakAtom* trak = item->GetData();
  84.         trak->AdjustChunkOffsets(offset);
  85.     }
  86.     return AP4_SUCCESS;
  87. }
  88. /*----------------------------------------------------------------------
  89. |   AP4_MoovAtom::OnChildAdded
  90. +---------------------------------------------------------------------*/
  91. void
  92. AP4_MoovAtom::OnChildAdded(AP4_Atom* atom)
  93. {
  94.     // keep the atom in the list of trak atoms
  95.     if (atom->GetType() == AP4_ATOM_TYPE_TRAK) {
  96.         AP4_TrakAtom* trak = dynamic_cast<AP4_TrakAtom*>(atom);
  97.         if (trak) {
  98.             m_TrakAtoms.Add(trak);
  99.         }
  100.     }
  101.     // call the base class implementation
  102.     AP4_ContainerAtom::OnChildAdded(atom);
  103. }
  104. /*----------------------------------------------------------------------
  105. |   AP4_MoovAtom::OnChildRemoved
  106. +---------------------------------------------------------------------*/
  107. void
  108. AP4_MoovAtom::OnChildRemoved(AP4_Atom* atom)
  109. {
  110.     // remove the atom from the list of trak atoms
  111.     if (atom->GetType() == AP4_ATOM_TYPE_TRAK) {
  112.         AP4_TrakAtom* trak = dynamic_cast<AP4_TrakAtom*>(atom);
  113.         if (trak) {
  114.             m_TrakAtoms.Remove(trak);
  115.         }
  116.     }
  117.     // call the base class implementation
  118.     AP4_ContainerAtom::OnChildRemoved(atom);
  119. }