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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - hdlr 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 "Ap4HdlrAtom.h"
  33. #include "Ap4AtomFactory.h"
  34. #include "Ap4Utils.h"
  35. /*----------------------------------------------------------------------
  36. |       AP4_HdlrAtom::AP4_HdlrAtom
  37. +---------------------------------------------------------------------*/
  38. AP4_HdlrAtom::AP4_HdlrAtom(AP4_Atom::Type hdlr_type, const char* hdlr_name) :
  39.     AP4_Atom(AP4_ATOM_TYPE_HDLR, true),
  40.     m_HandlerType(hdlr_type),
  41.     m_HandlerName(hdlr_name)
  42. {
  43.     m_Size += 20+m_HandlerName.length()+1;
  44. }
  45. /*----------------------------------------------------------------------
  46. |       AP4_HdlrAtom::AP4_HdlrAtom
  47. +---------------------------------------------------------------------*/
  48. AP4_HdlrAtom::AP4_HdlrAtom(AP4_Size size, AP4_ByteStream& stream) :
  49.     AP4_Atom(AP4_ATOM_TYPE_HDLR, size, true, stream)
  50. {
  51.     unsigned char reserved[12];
  52.     stream.Read(reserved, 4, NULL);
  53.     stream.ReadUI32(m_HandlerType);
  54.     stream.Read(reserved, 12, NULL);
  55.     
  56.     // read the name unless it is empty
  57.     int name_size = size-(AP4_FULL_ATOM_HEADER_SIZE+20);
  58.     if (name_size > 0) {
  59.         char* name = new char[name_size+1];
  60.         stream.Read(name, name_size);
  61.         name[name_size] = ''; // force a null termination
  62.         m_HandlerName = name;
  63.         delete[] name;
  64.     }
  65. }
  66. /*----------------------------------------------------------------------
  67. |       AP4_HdlrAtom::WriteFields
  68. +---------------------------------------------------------------------*/
  69. AP4_Result
  70. AP4_HdlrAtom::WriteFields(AP4_ByteStream& stream)
  71. {
  72.     AP4_Result result;
  73.     // write the data
  74.     unsigned char reserved[12];
  75.     memset(reserved, 0, sizeof(reserved));    
  76.     result = stream.Write(reserved, 4);
  77.     if (AP4_FAILED(result)) return result;
  78.     result = stream.WriteUI32(m_HandlerType);
  79.     if (AP4_FAILED(result)) return result;
  80.     result = stream.Write(reserved, 12);
  81.     if (AP4_FAILED(result)) return result;
  82.     result = stream.Write(m_HandlerName.c_str(), 
  83.                           m_HandlerName.length()+1);
  84.     if (AP4_FAILED(result)) return result;
  85.     // pad with zeros if necessary
  86.     AP4_Size padding = m_Size-(AP4_FULL_ATOM_HEADER_SIZE+20+m_HandlerName.length()+1);
  87.     while (padding--) stream.WriteUI08(0);
  88.     return AP4_SUCCESS;
  89. }
  90. /*----------------------------------------------------------------------
  91. |       AP4_HdlrAtom::InspectFields
  92. +---------------------------------------------------------------------*/
  93. AP4_Result
  94. AP4_HdlrAtom::InspectFields(AP4_AtomInspector& inspector)
  95. {
  96.     char type[5];
  97.     AP4_FormatFourChars(type, m_HandlerType);
  98.     inspector.AddField("handler_type", type);
  99.     inspector.AddField("handler_name", m_HandlerName.c_str());
  100.     return AP4_SUCCESS;
  101. }