/* Copyright (C) 2012 Carl Hetherington This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include #include #include #include #include #include "dcp.h" #include "asset.h" #include "sound_asset.h" #include "picture_asset.h" #include "util.h" #include "tags.h" using namespace std; using namespace boost; using namespace libdcp; /** Construct a DCP. * @param d Directory to write files to. */ DCP::DCP (string d, string n, ContentType c, int fps, int length) : _directory (d) , _name (n) , _content_type (c) , _fps (fps) , _length (length) { char buffer[64]; time_t now; time (&now); struct tm* tm = localtime (&now); strftime (buffer, 64, "%Y-%m-%dT%I:%M:%S+00:00", tm); _date = string (buffer); } void DCP::add_sound_asset (list const & files) { filesystem::path p; p /= _directory; p /= "audio.mxf"; _assets.push_back (shared_ptr (new SoundAsset (files, p.string(), _fps, _length))); } void DCP::add_picture_asset (list const & files, int w, int h) { filesystem::path p; p /= _directory; p /= "video.mxf"; _assets.push_back (shared_ptr (new PictureAsset (files, p.string(), _fps, _length, w, h))); } /** Write the required XML files to the directory that was * passed into the constructor. */ void DCP::write_xml () const { string cpl_uuid = make_uuid (); string cpl_path = write_cpl (cpl_uuid); int cpl_length = filesystem::file_size (cpl_path); string cpl_digest = make_digest (cpl_path); string pkl_uuid = make_uuid (); string pkl_path = write_pkl (pkl_uuid, cpl_uuid, cpl_digest, cpl_length); write_volindex (); write_assetmap (cpl_uuid, cpl_length, pkl_uuid, filesystem::file_size (pkl_path)); } /** Write the CPL file. * @param cpl_uuid UUID to use. * @return CPL pathname. */ string DCP::write_cpl (string cpl_uuid) const { filesystem::path p; p /= _directory; stringstream s; s << cpl_uuid << "_cpl.xml"; p /= s.str(); ofstream cpl (p.string().c_str()); cpl << "\n" << "\n" << " urn:uuid:" << cpl_uuid << "\n" << " " << _name << "\n" << " " << _date << "\n" << " libdcp " << Tags::instance()->creator << "\n" << " " << _name << "\n" << " " << _content_type << "\n" << " \n" << " urn:uri:" << cpl_uuid << "_" << _date << "\n" << " " << cpl_uuid << "_" << _date << "\n" << " \n" << " \n" << " \n"; cpl << " \n" << " urn:uuid:" << make_uuid() << "\n" << " \n"; for (list >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) { (*i)->write_to_cpl (cpl); } cpl << " \n" << " \n" << " \n" << "\n"; return p.string (); } /** Write the PKL file. * @param pkl_uuid UUID to use. * @param cpl_uuid UUID of the CPL file. * @param cpl_digest SHA digest of the CPL file. * @param cpl_length Length of the CPL file in bytes. */ std::string DCP::write_pkl (string pkl_uuid, string cpl_uuid, string cpl_digest, int cpl_length) const { filesystem::path p; p /= _directory; stringstream s; s << pkl_uuid << "_pkl.xml"; p /= s.str(); ofstream pkl (p.string().c_str()); pkl << "\n" << "\n" << " urn:uuid:" << pkl_uuid << "\n" << " " << _name << "\n" << " " << _date << "\n" << " " << Tags::instance()->issuer << "\n" << " " << Tags::instance()->creator << "\n" << " \n"; for (list >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) { (*i)->write_to_pkl (pkl); } pkl << " \n" << " urn:uuid" << cpl_uuid << "\n" << " " << cpl_digest << "\n" << " " << cpl_length << "\n" << " text/xml\n" << " \n"; pkl << " \n" << "\n"; return p.string (); } void DCP::write_volindex () const { filesystem::path p; p /= _directory; p /= "VOLINDEX.xml"; ofstream vi (p.string().c_str()); vi << "\n" << "\n" << " 1\n" << "\n"; } void DCP::write_assetmap (string cpl_uuid, int cpl_length, string pkl_uuid, int pkl_length) const { filesystem::path p; p /= _directory; p /= "ASSETMAP.xml"; ofstream am (p.string().c_str()); am << "\n" << "\n" << " urn:uuid:" << make_uuid() << "\n" << " " << Tags::instance()->creator << "\n" << " 1\n" << " " << _date << "\n" << " " << Tags::instance()->issuer << "\n" << " \n"; am << " \n" << " urn:uuid:" << pkl_uuid << "\n" << " true\n" << " \n" << " \n" << " " << pkl_uuid << "_pkl.xml\n" << " 1\n" << " 0\n" << " " << pkl_length << "\n" << " \n" << " \n" << " \n"; am << " \n" << " urn:uuid:" << cpl_uuid << "\n" << " \n" << " \n" << " " << cpl_uuid << "_cpl.xml\n" << " 1\n" << " 0\n" << " " << cpl_length << "\n" << " \n" << " \n" << " \n"; for (list >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) { (*i)->write_to_assetmap (am); } am << " \n" << "\n"; } string DCP::content_type_string (ContentType t) { switch (t) { case FEATURE: return "feature"; case SHORT: return "short"; case TRAILER: return "trailer"; case TEST: return "test"; case TRANSITIONAL: return "transitional"; case RATING: return "rating"; case TEASER: return "teaser"; case POLICY: return "policy"; case PUBLIC_SERVICE_ANNOUNCEMENT: return "psa"; case ADVERTISEMENT: return "advertisement"; } assert (false); }