Ap4HdlrAtom.cpp

00001 /*****************************************************************
00002 |
00003 |    AP4 - hdlr Atoms 
00004 |
00005 |    Copyright 2002 Gilles Boccon-Gibod
00006 |
00007 |
00008 |    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
00009 |
00010 |    Unless you have obtained Bento4 under a difference license,
00011 |    this version of Bento4 is Bento4|GPL.
00012 |    Bento4|GPL is free software; you can redistribute it and/or modify
00013 |    it under the terms of the GNU General Public License as published by
00014 |    the Free Software Foundation; either version 2, or (at your option)
00015 |    any later version.
00016 |
00017 |    Bento4|GPL is distributed in the hope that it will be useful,
00018 |    but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 |    GNU General Public License for more details.
00021 |
00022 |    You should have received a copy of the GNU General Public License
00023 |    along with Bento4|GPL; see the file COPYING.  If not, write to the
00024 |    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
00025 |    02111-1307, USA.
00026 |
00027  ****************************************************************/
00028 
00029 /*----------------------------------------------------------------------
00030 |       includes
00031 +---------------------------------------------------------------------*/
00032 #include "Ap4.h"
00033 #include "Ap4HdlrAtom.h"
00034 #include "Ap4AtomFactory.h"
00035 #include "Ap4Utils.h"
00036 
00037 /*----------------------------------------------------------------------
00038 |       AP4_HdlrAtom::AP4_HdlrAtom
00039 +---------------------------------------------------------------------*/
00040 AP4_HdlrAtom::AP4_HdlrAtom(AP4_Atom::Type hdlr_type, const char* hdlr_name) :
00041     AP4_Atom(AP4_ATOM_TYPE_HDLR, true),
00042     m_HandlerType(hdlr_type),
00043     m_HandlerName(hdlr_name)
00044 {
00045     m_Size += 20+m_HandlerName.length()+1;
00046 }
00047 
00048 /*----------------------------------------------------------------------
00049 |       AP4_HdlrAtom::AP4_HdlrAtom
00050 +---------------------------------------------------------------------*/
00051 AP4_HdlrAtom::AP4_HdlrAtom(AP4_Size size, AP4_ByteStream& stream) :
00052     AP4_Atom(AP4_ATOM_TYPE_HDLR, size, true, stream)
00053 {
00054     unsigned char reserved[12];
00055     stream.Read(reserved, 4, NULL);
00056     stream.ReadUI32(m_HandlerType);
00057     stream.Read(reserved, 12, NULL);
00058     
00059     // read the name unless it is empty
00060     int name_size = size-(AP4_FULL_ATOM_HEADER_SIZE+20);
00061     if (name_size > 0) {
00062         char* name = new char[name_size+1];
00063         stream.Read(name, name_size);
00064         name[name_size] = '\0'; // force a null termination
00065         m_HandlerName = name;
00066         delete[] name;
00067     }
00068 }
00069 
00070 /*----------------------------------------------------------------------
00071 |       AP4_HdlrAtom::WriteFields
00072 +---------------------------------------------------------------------*/
00073 AP4_Result
00074 AP4_HdlrAtom::WriteFields(AP4_ByteStream& stream)
00075 {
00076     AP4_Result result;
00077 
00078     // write the data
00079     unsigned char reserved[12];
00080     memset(reserved, 0, sizeof(reserved));    
00081     result = stream.Write(reserved, 4);
00082     if (AP4_FAILED(result)) return result;
00083     result = stream.WriteUI32(m_HandlerType);
00084     if (AP4_FAILED(result)) return result;
00085     result = stream.Write(reserved, 12);
00086     if (AP4_FAILED(result)) return result;
00087     result = stream.Write(m_HandlerName.c_str(), 
00088                           m_HandlerName.length()+1);
00089     if (AP4_FAILED(result)) return result;
00090 
00091     // pad with zeros if necessary
00092     AP4_Size padding = m_Size-(AP4_FULL_ATOM_HEADER_SIZE+20+m_HandlerName.length()+1);
00093     while (padding--) stream.WriteUI08(0);
00094 
00095     return AP4_SUCCESS;
00096 }
00097 
00098 /*----------------------------------------------------------------------
00099 |       AP4_HdlrAtom::InspectFields
00100 +---------------------------------------------------------------------*/
00101 AP4_Result
00102 AP4_HdlrAtom::InspectFields(AP4_AtomInspector& inspector)
00103 {
00104     char type[5];
00105     AP4_FormatFourChars(type, m_HandlerType);
00106     inspector.AddField("handler_type", type);
00107     inspector.AddField("handler_name", m_HandlerName.c_str());
00108 
00109     return AP4_SUCCESS;
00110 }

Generated on Tue Dec 13 14:47:19 2005 for guliverkli by  doxygen 1.4.5