Ap4Protection.h

Go to the documentation of this file.
00001 /*****************************************************************
00002 |
00003 |    AP4 - Protected Streams support
00004 |
00005 |    Copyright 2002-2006 Gilles Boccon-Gibod & Julien Boeuf & Julien Boeuf
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 #ifndef _AP4_PROTECTION_H_
00030 #define _AP4_PROTECTION_H_
00031 
00032 /*----------------------------------------------------------------------
00033 |   includes
00034 +---------------------------------------------------------------------*/
00035 #include "Ap4Types.h"
00036 #include "Ap4SampleEntry.h"
00037 #include "Ap4Atom.h"
00038 #include "Ap4AtomFactory.h"
00039 #include "Ap4SampleDescription.h"
00040 #include "Ap4Processor.h"
00041 
00042 /*----------------------------------------------------------------------
00043 |   constants
00044 +---------------------------------------------------------------------*/
00045 // this is fixed for now
00046 const unsigned int AP4_PROTECTION_KEY_LENGTH = 16;
00047 
00048 const AP4_UI32 AP4_PROTECTION_SCHEME_TYPE_ITUNES = AP4_ATOM_TYPE('i','t','u','n');
00049 
00050 /*----------------------------------------------------------------------
00051 |   AP4_EncaSampleEntry
00052 +---------------------------------------------------------------------*/
00053 class AP4_EncaSampleEntry : public AP4_AudioSampleEntry
00054 {
00055 public:
00056     // methods
00057     AP4_EncaSampleEntry(AP4_Size         size,
00058                         AP4_ByteStream&  stream,
00059                         AP4_AtomFactory& atom_factory);
00060     AP4_EncaSampleEntry(AP4_UI32         type,
00061                         AP4_Size         size,
00062                         AP4_ByteStream&  stream,
00063                         AP4_AtomFactory& atom_factory);
00064 
00065     // methods
00066     AP4_SampleDescription* ToSampleDescription();
00067 };
00068 
00069 /*----------------------------------------------------------------------
00070 |   AP4_EncvSampleEntry
00071 +---------------------------------------------------------------------*/
00072 class AP4_EncvSampleEntry : public AP4_VisualSampleEntry
00073 {
00074 public:
00075     // constructors
00076     AP4_EncvSampleEntry(AP4_Size         size,
00077                         AP4_ByteStream&  stream,
00078                         AP4_AtomFactory& atom_factory);
00079     AP4_EncvSampleEntry(AP4_UI32         type,
00080                         AP4_Size         size,
00081                         AP4_ByteStream&  stream,
00082                         AP4_AtomFactory& atom_factory);
00083 
00084     // methods
00085     AP4_SampleDescription* ToSampleDescription();
00086 };
00087 
00088 /*----------------------------------------------------------------------
00089 |   AP4_DrmsSampleEntry
00090 +---------------------------------------------------------------------*/
00091 class AP4_DrmsSampleEntry : public AP4_EncaSampleEntry
00092 {
00093 public:
00094     // methods
00095     AP4_DrmsSampleEntry(AP4_Size         size,
00096                         AP4_ByteStream&  stream,
00097                         AP4_AtomFactory& atom_factory);
00098 };
00099 
00100 /*----------------------------------------------------------------------
00101 |   AP4_DrmiSampleEntry
00102 +---------------------------------------------------------------------*/
00103 class AP4_DrmiSampleEntry : public AP4_EncvSampleEntry
00104 {
00105 public:
00106     // methods
00107     AP4_DrmiSampleEntry(AP4_Size         size,
00108                         AP4_ByteStream&  stream,
00109                         AP4_AtomFactory& atom_factory);
00110 };
00111 
00112 /*----------------------------------------------------------------------
00113 |   AP4_ProtectionKeyMap
00114 +---------------------------------------------------------------------*/
00115 class AP4_ProtectionKeyMap
00116 {
00117 public:
00118     // constructors and destructor
00119     AP4_ProtectionKeyMap();
00120     ~AP4_ProtectionKeyMap();
00121 
00122     // methods
00123     AP4_Result      SetKey(AP4_UI32 track_id, const AP4_UI08* key, const AP4_UI08* iv = NULL);
00124     AP4_Result      SetKeys(const AP4_ProtectionKeyMap& key_map);
00125     AP4_Result      GetKeyAndIv(AP4_UI32 track_id, const AP4_UI08*& key, const AP4_UI08*& iv);
00126     const AP4_UI08* GetKey(AP4_UI32 track_id) const;
00127 
00128 private:
00129     // types
00130     class KeyEntry {
00131     public:
00132         KeyEntry(AP4_UI32 track_id, const AP4_UI08* key, const AP4_UI08* iv = NULL);
00133         void SetKey(const AP4_UI08* key, const AP4_UI08* iv);
00134         AP4_Ordinal m_TrackId;
00135         AP4_UI08    m_Key[AP4_PROTECTION_KEY_LENGTH];
00136         AP4_UI08    m_IV[AP4_PROTECTION_KEY_LENGTH];
00137     };
00138 
00139     // methods
00140     KeyEntry* GetEntry(AP4_UI32 track_id) const;
00141 
00142     // members
00143     AP4_List<KeyEntry> m_KeyEntries;
00144 };
00145 
00146 /*----------------------------------------------------------------------
00147 |   AP4_ProtectionSchemeInfo
00148 +---------------------------------------------------------------------*/
00149 class AP4_ProtectionSchemeInfo
00150 {
00151 public:
00152     // constructors and destructor
00153     AP4_ProtectionSchemeInfo(AP4_ContainerAtom* schi);
00154     virtual ~AP4_ProtectionSchemeInfo();
00155 
00156     // accessors
00157     AP4_ContainerAtom* GetSchiAtom() { return m_SchiAtom; }
00158 
00159 protected:
00160     AP4_ContainerAtom* m_SchiAtom;
00161 };
00162 
00163 /*----------------------------------------------------------------------
00164 |   AP4_ProtectedSampleDescription
00165 +---------------------------------------------------------------------*/
00166 class AP4_ProtectedSampleDescription : public AP4_SampleDescription
00167 {
00168 public:
00169     // constructor and destructor
00170     AP4_ProtectedSampleDescription(AP4_UI32               format,
00171                                    AP4_SampleDescription* original_sample_description,
00172                                    AP4_UI32               original_format,
00173                                    AP4_UI32               scheme_type,
00174                                    AP4_UI32               scheme_version,
00175                                    const char*            scheme_uri,
00176                                    AP4_ContainerAtom*     schi_atom);
00177     ~AP4_ProtectedSampleDescription();
00178     
00179     // accessors
00180     AP4_SampleDescription* GetOriginalSampleDescription() {
00181         return m_OriginalSampleDescription;
00182     }
00183     AP4_UI32          GetOriginalFormat() const { return m_OriginalFormat; }
00184     AP4_UI32          GetSchemeType()     const { return m_SchemeType;     }
00185     AP4_UI32          GetSchemeVersion()  const { return m_SchemeVersion;  }
00186     const AP4_String& GetSchemeUri()      const { return m_SchemeUri;      }
00187     AP4_ProtectionSchemeInfo* GetSchemeInfo() const { 
00188         return m_SchemeInfo; 
00189     }
00190 
00191     // implementation of abstract base class methods
00192     virtual AP4_Atom* ToAtom() const;
00193 
00194 private:
00195     // members
00196     AP4_SampleDescription*    m_OriginalSampleDescription;
00197     AP4_UI32                  m_OriginalFormat;
00198     AP4_UI32                  m_SchemeType;
00199     AP4_UI32                  m_SchemeVersion;
00200     AP4_String                m_SchemeUri;
00201     AP4_ProtectionSchemeInfo* m_SchemeInfo;
00202 };
00203 
00204 /*----------------------------------------------------------------------
00205 |   AP4_BlockCipher
00206 +---------------------------------------------------------------------*/
00207 class AP4_BlockCipher
00208 {
00209 public:
00210     // types
00211     typedef enum {
00212         ENCRYPT,
00213         DECRYPT
00214     } CipherDirection;
00215 
00216     typedef enum {
00217         AES_128
00218     } CipherType;
00219 
00220     // constructor and destructor
00221     virtual ~AP4_BlockCipher() {}
00222     
00223     // methods
00224     virtual AP4_Result ProcessBlock(const AP4_UI08* block_in, AP4_UI08* block_out) = 0;
00225 };
00226 
00227 /*----------------------------------------------------------------------
00228 |   AP4_BlockCipherFactory
00229 +---------------------------------------------------------------------*/
00230 class AP4_BlockCipherFactory
00231 {
00232 public:
00233     // methods
00234     virtual ~AP4_BlockCipherFactory() {}
00235     virtual AP4_Result Create(AP4_BlockCipher::CipherType      type,
00236                               AP4_BlockCipher::CipherDirection direction,
00237                               const AP4_UI08*                  key,
00238                               AP4_Size                         key_size,
00239                               AP4_BlockCipher**                cipher) = 0;
00240 };
00241 
00242 /*----------------------------------------------------------------------
00243 |   AP4_DefaultBlockCipherFactory
00244 +---------------------------------------------------------------------*/
00245 class AP4_DefaultBlockCipherFactory : public AP4_BlockCipherFactory
00246 {
00247 public:
00248     // class variables
00249     static AP4_DefaultBlockCipherFactory Instance;
00250 
00251     // methods
00252     virtual AP4_Result Create(AP4_BlockCipher::CipherType      type,
00253                               AP4_BlockCipher::CipherDirection direction,
00254                               const AP4_UI08*                  key,
00255                               AP4_Size                         key_size,
00256                               AP4_BlockCipher**                cipher);
00257 };
00258 
00259 /*----------------------------------------------------------------------
00260 |   AP4_SampleDecrypter
00261 +---------------------------------------------------------------------*/
00262 class AP4_SampleDecrypter
00263 {
00264 public:
00265     // factory
00266     static AP4_SampleDecrypter* Create(AP4_ProtectedSampleDescription* sample_description,
00267                                        const AP4_UI08*                 key,
00268                                        AP4_Size                        key_size,
00269                                        AP4_BlockCipherFactory*         block_cipher_factory = NULL);
00270 
00271     // destructor
00272     virtual ~AP4_SampleDecrypter() {}
00273 
00274     // methods
00275     virtual AP4_Result DecryptSampleData(AP4_DataBuffer& data_in,
00276                                          AP4_DataBuffer& data_out) = 0;
00277 };
00278 
00279 /*----------------------------------------------------------------------
00280 |   AP4_StandardDecryptingProcessor
00281 +---------------------------------------------------------------------*/
00282 class AP4_StandardDecryptingProcessor : public AP4_Processor
00283 {
00284 public:
00285     // constructor
00286     AP4_StandardDecryptingProcessor(AP4_BlockCipherFactory* block_cipher_factory = NULL);
00287 
00288     // accessors
00289     AP4_ProtectionKeyMap& GetKeyMap() { return m_KeyMap; }
00290 
00291     // methods
00292     virtual AP4_Processor::TrackHandler* CreateTrackHandler(AP4_TrakAtom* trak);
00293     virtual AP4_Result Initialize(AP4_AtomParent&   top_level,
00294                                   ProgressListener* listener);
00295 
00296 private:
00297     // members
00298     AP4_BlockCipherFactory* m_BlockCipherFactory;
00299     AP4_ProtectionKeyMap    m_KeyMap;
00300 };
00301 
00302 #endif // _AP4_PROTECTION_H_

Generated on Thu Mar 15 16:06:11 2007 for Bento4 MP4 SDK by  doxygen 1.5.1-p1