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

多媒体编程

开发平台:

Visual C++

  1. /*****************************************************************
  2. |
  3. |    AP4 - schm 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 "Ap4SchmAtom.h"
  33. #include "Ap4AtomFactory.h"
  34. #include "Ap4Utils.h"
  35. /*----------------------------------------------------------------------
  36. |       AP4_SchmAtom::AP4_SchmAtom
  37. +---------------------------------------------------------------------*/
  38. AP4_SchmAtom::AP4_SchmAtom(AP4_UI32    scheme_type,
  39.                            AP4_UI32    scheme_version,
  40.                            const char* scheme_uri) :
  41.     AP4_Atom(AP4_ATOM_TYPE_SCHM, AP4_FULL_ATOM_HEADER_SIZE+8, true),
  42.     m_SchemeType(scheme_type),
  43.     m_SchemeVersion(scheme_version)
  44. {
  45.     if (scheme_uri) {
  46.         m_SchemeUri = scheme_uri;
  47.         m_Flags = 1;
  48.         m_Size += m_SchemeUri.length()+1;
  49.     }
  50. }
  51. /*----------------------------------------------------------------------
  52. |       AP4_SchmAtom::AP4_SchmAtom
  53. +---------------------------------------------------------------------*/
  54. AP4_SchmAtom::AP4_SchmAtom(AP4_Size size, AP4_ByteStream& stream) :
  55.     AP4_Atom(AP4_ATOM_TYPE_SCHM, size, true, stream)
  56. {
  57.     stream.ReadUI32(m_SchemeType);
  58.     stream.ReadUI32(m_SchemeVersion);
  59.     if (m_Flags & 1) {
  60.         int str_size = size-(AP4_FULL_ATOM_HEADER_SIZE+8);
  61.         if (str_size > 0) {
  62.             char* str = new char[str_size];
  63.             stream.Read(str, str_size);
  64.             str[str_size-1] = ''; // force null-termination
  65.             m_SchemeUri = str;
  66.             delete[] str;
  67.         }
  68.     }
  69. }
  70. /*----------------------------------------------------------------------
  71. |       AP4_SchmAtom::WriteFields
  72. +---------------------------------------------------------------------*/
  73. AP4_Result
  74. AP4_SchmAtom::WriteFields(AP4_ByteStream& stream)
  75. {
  76.     AP4_Result result;
  77.     // scheme type
  78.     result = stream.WriteUI32(m_SchemeType);
  79.     if (AP4_FAILED(result)) return result;
  80.     // scheme version
  81.     result = stream.WriteUI32(m_SchemeVersion);
  82.     if (AP4_FAILED(result)) return result;
  83.     // uri if needed
  84.     if (m_Flags & 1) {
  85.         result = stream.Write(m_SchemeUri.c_str(), m_SchemeUri.length()+1);
  86.         if (AP4_FAILED(result)) return result;
  87.         // pad with zeros if necessary
  88.         AP4_Size padding = m_Size-(AP4_FULL_ATOM_HEADER_SIZE+8+m_SchemeUri.length()+1);
  89.         while (padding--) {
  90.             stream.WriteUI08(0);
  91.         }
  92.     }
  93.     return result;
  94. }
  95. /*----------------------------------------------------------------------
  96. |       AP4_SchmAtom::InspectFields
  97. +---------------------------------------------------------------------*/
  98. AP4_Result
  99. AP4_SchmAtom::InspectFields(AP4_AtomInspector& inspector)
  100. {
  101.     char st[5];
  102.     AP4_FormatFourChars(st, m_SchemeType);
  103.     inspector.AddField("scheme_type", st);
  104.     inspector.AddField("scheme_version", m_SchemeVersion);
  105.     if (m_Flags & 1) {
  106.         inspector.AddField("scheme_uri", m_SchemeUri.c_str());    
  107.     }
  108.     return AP4_SUCCESS;
  109. }