⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mp4drm.cpp

📁 完整的RTP RTSP代码库
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/**	\file mp4drm.cpp    \ingroup mpeg4ipencoder	Example of use of DRM plugins in encoding case.    The Initial Developer of the Original Code is Adnecto d.o.o.<br>    Portions created by Adnecto d.o.o. are<br>    Copyright (C) Adnecto d.o.o. 2005.  All Rights Reserved.<p>	\author Danijel Kopcinovic (danijel.kopcinovic@adnecto.net)*//*****************************************************************************//*  Added by Danijel Kopcinovic as support for DRM encryption.           *//*****************************************************************************/#include "mp4creator.h"#include "mpeg4ip_getopt.h"#include "mpeg.h"#include "mp4common.h"#include "ContentInfoManagerFactory.h"#include "ISMADRMEncManagerFactory.h"#include "OMADRMEncManagerFactory.h"#include "OpenIPMPDRMEncManagerFactory.h"#include "IDRMEncManager.h"#include "MPEG4DRMPluginException.h"#include "OpenIPMP.h"#include "ICipher.h"#include "XMLFactory.h"#include "PublicCryptoFactory.h"#include "ThreadSyncFactory.h"#include <string>#include <vector>using namespace DRMPlugin;/*! \brief Encrypts MP4 data with Sinf signalling.    It is used to encrypt track samples, and add neccessary information    for decryption.*/class IMPEG4SinfDRMEncryptor {public:  virtual ~IMPEG4SinfDRMEncryptor() {};  /*! \brief  Encrypt sample sampleBuffer.        Returns encrypted sample in encSampleData.      \param    sampleBuffer      input, sample to be encrypted.      \param    sampleSize        input, size of input sample.      \param    encSampleData     output, encrypted sample.      \param    encSampleLen      output, size of encrypted sample.      \param    logger            input-output, message logger.      \return Boolean indicating success or failure. In case of failure,              logger's log will contain error message.  */  virtual bool EncryptSample(ByteT* sampleBuffer, UInt32T sampleSize,    ByteT** encSampleData, UInt32T* encSampleLen, DRMLogger& logger) = 0;  /*! \brief  Add drm information into sinf atom.        Caller must take care that given sinf atom really coresponds to      sample description atom which refers to encrypted samples.      \param    originalFormat  input, 4CC code of the original data format.      \param    sinf            input-output, sinf atom where to add drm information.      \param    logger          input-output, message logger.      \return Boolean indicating success or failure. In case of failure,              logger's log will contain error message.  */  virtual bool Finish(UInt32T originalFormat, MP4Atom* sinf, DRMLogger& logger) = 0;};/*! \brief  Encrypts MP4 data using AES128ICM encryptor according to ISMA            specifications with Sinf signalling.    Encrypts samples and adds information for decryption to MP4 stream.*/class AES128ICMISMAMPEG4SinfDRMEncryptor: public IMPEG4SinfDRMEncryptor {public:  /*! \brief  Constructor.      Mandatory tags in the XML file are:       - ROOT.RightsHostInfo      Here follows an example of a correctly formatted XML document.      \verbatim      <ROOT>       <RightsHostInfo>localhost:8080</RightsHostInfo>      </ROOT>      \endverbatim      \param      drm             input, DRM manager.      \param      conID           input-output, content identifier.      \param      xmlDoc          input, XML document.      \param      logger          input-output, message logger.      If constructor fails, it throws MPEG4DRMPluginException.  */  AES128ICMISMAMPEG4SinfDRMEncryptor(IDRMEncManager* drm, std::string& conID,      DRMPlugin::IXMLElement* xmlDoc, DRMLogger& logger): encryptor(0), contentID(),      rightsHost() {    if (xmlDoc == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::ctor: zero XML file.");      throw MPEG4DRMPluginException();    }    encryptor = drm->CreateAES128ICMEnc(conID, xmlDoc, logger);    if (encryptor == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::ctor: zero encryptor.");      throw MPEG4DRMPluginException();    }    contentID = conID;    try {      rightsHost = xmlDoc->GetChildStrValue("RightsHostInfo");    }    catch (XMLException) {      delete encryptor;      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::ctor: missing data in XML file.");      throw MPEG4DRMPluginException();    }  }  virtual ~AES128ICMISMAMPEG4SinfDRMEncryptor() {    if (encryptor != 0) delete encryptor;  }  /*! \brief  Encrypt sample sampleBuffer.        Returns encrypted sample in encSampleData.      \param    sampleBuffer      input, sample to be encrypted.      \param    sampleSize        input, size of input sample.      \param    encSampleData     output, encrypted sample.      \param    encSampleLen      output, size of encrypted sample.      \param    logger            input-output, message logger.      \return Boolean indicating success or failure. In case of failure,              logger's log will contain error message.  */  virtual bool EncryptSample(ByteT* sampleBuffer, UInt32T sampleSize,      ByteT** encSampleData, UInt32T* encSampleLen, DRMLogger& logger) {    int headerLength;    ByteT* iv;    UInt32T ivLen;    if (encryptor->NextIV(&iv, &ivLen, logger) == false) {      return false;    }    //! Sanity check for iv length.    if (ivLen != 16) {      if (iv != NULL) free(iv);      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::EncryptSample: invalid initialization vector size.");      return false;    }    ByteT* tmp;    UInt32T tmpLen;    if (encryptor->Encrypt(sampleBuffer, sampleSize, &tmp, &tmpLen, logger) == false) {      if (iv != NULL) free(iv);      return false;    }    headerLength = 0 + ivLen;    *encSampleData = (ByteT *)malloc(headerLength + tmpLen);    if (*encSampleData == NULL) {      if (tmp != NULL) free(tmp);       if (iv != NULL) free(iv);      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::EncryptSample: memory allocation error.");      return false;     }    memcpy((*encSampleData) + headerLength, tmp, tmpLen);    memset(*encSampleData, 0, 0);    memcpy((*encSampleData) + 0, iv, ivLen);    *encSampleLen = headerLength + tmpLen;    if (tmp != NULL) free(tmp);     if (iv != NULL) free(iv);      return true;  }  /*! \brief  Add drm information into sinf atom.        Caller must take care that given sinf atom really coresponds to sample      description atom which refers to encrypted samples.      \param    originalFormat  input, 4CC code of the original data format.      \param    sinf            input-output, sinf atom where to add drm information.      \param    logger          input-output, message logger.      \return Boolean indicating success or failure. In case of failure,              logger's log will contain error message.  */  virtual bool Finish(UInt32T originalFormat, MP4Atom* sinf, DRMLogger& logger) {	  MP4Atom* frma = sinf->FindChildAtom("frma");    if (frma == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::Finish: no original format atom.");      return false;    }	  MP4Property* data_format = 0;	  frma->FindProperty("frma.data-format", &data_format, 0); 		  if (data_format == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::Finish: frma->SetDataFormat() error.");      return false;	  }    ((MP4Integer32Property*)data_format)->SetValue(originalFormat);    MP4Atom* schm = sinf->CreateAtom("schm");    if (schm == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::Finish: could not create scheme type atom.");      return false;    }    sinf->AddChildAtom(schm);    schm->Generate();	  MP4Property* scheme_type = 0;	  schm->FindProperty("schm.scheme_type", &scheme_type, 0); 		  if (scheme_type == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::Finish: schm->SetSchemeCode() error.");      return false;	  }    ((MP4Integer32Property*)scheme_type)->SetValue(('i' << 24) | ('A' << 16) | ('E' << 8) | 'C');	  MP4Property* scheme_version = 0;	  schm->FindProperty("schm.scheme_version", &scheme_version, 0); 		  if (scheme_version == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::Finish: schm->SetSchemeVersion() error.");      return false;	  }    ((MP4Integer32Property*)scheme_version)->SetValue(1);    MP4Atom* schi = sinf->CreateAtom("schi");    if (schi == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::Finish: could not create scheme information atom.");      return false;    }    sinf->AddChildAtom(schi);    schi->Generate();    MP4Atom* iKMS = schi->CreateAtom("iKMS");    if (iKMS == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::Finish: could not create ISMA drm atoms.");      return false;    }    schi->AddChildAtom(iKMS);    iKMS->Generate();    MP4Atom* iSFM = schi->CreateAtom("iSFM");    if (iSFM == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::Finish: could not create ISMA drm atoms.");      return false;    }    schi->AddChildAtom(iSFM);    iSFM->Generate();	  MP4Property* kms_URI = 0;	  iKMS->FindProperty("iKMS.kms_URI", &kms_URI, 0); 		  if (kms_URI == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::Finish: iKMS->SetKeyManagementSystemURI() error.");      return false;	  }    ((MP4StringProperty*)kms_URI)->SetValue(rightsHost.data());	  MP4Property* selective_encryption = 0;	  iSFM->FindProperty("iSFM.selective-encryption", &selective_encryption, 0); 		  if (selective_encryption == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::Finish: iSFM->SetSelectiveEncryption() error.");      return false;	  }    ((MP4BitfieldProperty*)selective_encryption)->SetValue(0);	  MP4Property* key_indicator_length = 0;	  iSFM->FindProperty("iSFM.key-indicator-length", &key_indicator_length, 0); 		  if (key_indicator_length == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::Finish: iSFM->SetKeyIndicatorLength() error.");      return false;	  }    ((MP4Integer8Property*)key_indicator_length)->SetValue(0);	  MP4Property* IV_length = 0;	  iSFM->FindProperty("iSFM.IV-length", &IV_length, 0); 		  if (IV_length == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::Finish: iSFM->SetIVLength() error.");      return false;	  }    ((MP4Integer8Property*)IV_length)->SetValue(16);    MP4Atom* imif = sinf->CreateAtom("imif");    if (imif == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::Finish: could not create IPMP info atom.");      return false;    }    sinf->AddChildAtom(imif);    imif->Generate();	  MP4Property* ipmp_desc = 0;	  imif->FindProperty("imif.ipmp_desc", &ipmp_desc, 0); 		  if (ipmp_desc == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::Finish: missing IPMP descriptor property.");      return false;	  }    MP4Descriptor* ipmpDesc = ((MP4DescriptorProperty*)ipmp_desc)->AddDescriptor(MP4IPMPDescrTag);    if (ipmpDesc == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::Finish: could not create IPMP descriptor.");      return false;    }	  MP4Property* IPMPDescriptorId = 0;	  ipmpDesc->FindProperty("IPMPDescriptorId", &IPMPDescriptorId, 0); 		  if (IPMPDescriptorId == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::Finish: ipmpDesc->SetIPMPDescriptorIdentifier() error.");      return false;	  }    ((MP4Integer8Property*)IPMPDescriptorId)->SetValue(1);	  MP4Property* IPMPSType = 0;	  ipmpDesc->FindProperty("IPMPSType", &IPMPSType, 0); 		  if (IPMPSType == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::Finish: ipmpDesc->SetIPMPDescriptorType() error.");      return false;	  }    ((MP4Integer16Property*)IPMPSType)->SetValue(0x4953);	  MP4Property* IPMPData = 0;	  ipmpDesc->FindProperty("IPMPData", &IPMPData, 0); 		  if (IPMPData == 0) {      logger.UpdateLog("AES128ICMISMAMPEG4SinfDRMEncryptor::Finish: ipmpDesc->SetIPMPData() error.");      return false;	  }    ((MP4BytesProperty*)IPMPData)->SetValue((u_int8_t*)(contentID.data()), contentID.size() + 1);    return true;  }private:  AES128ICMEncryptor* encryptor;  std::string contentID;  std::string rightsHost;};/*! \brief  Encrypts MP4 data using AES128CBC encryptor according to OMA            specifications with Sinf signalling.    Encrypts samples and adds information for decryption to MP4 stream.*/class AES128CBCOMAMPEG4SinfDRMEncryptor: public IMPEG4SinfDRMEncryptor {public:  /*! \brief  Constructor.      Mandatory tags in the XML file are:       - ROOT.RightsHostInfo      Here follows an example of a correctly formatted XML document.      \verbatim      <ROOT>       <RightsHostInfo>localhost:8080</RightsHostInfo>       <SilentHeader>Silent:on-demand;http://www.silent.com/</SilentHeader>       <PreviewHeader>Preview:instant;http://www.preview.com/</PreviewHeader>       <ContentURLHeader>ContentURL:http://www.content.com/</ContentURLHeader>       <ContentVersionHeader>ContentVersion:original-content-identifier:1.1</ContentVersionHeader>       <ContentLocationHeader>Content-Location:http://www.content.com/</ContentLocationHeader>      </ROOT>      \endverbatim

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -