Import.
[libdcp.git] / src / dcp.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <sstream>
21 #include <fstream>
22 #include <iomanip>
23 #include <cassert>
24 #include <boost/filesystem.hpp>
25 #include "dcp.h"
26 #include "asset.h"
27 #include "sound_asset.h"
28 #include "picture_asset.h"
29 #include "util.h"
30 #include "tags.h"
31
32 using namespace std;
33 using namespace boost;
34 using namespace libdcp;
35
36 /** Construct a DCP.
37  *  @param d Directory to write files to.
38  */
39 DCP::DCP (string d, string n, ContentType c, int fps, int length)
40         : _directory (d)
41         , _name (n)
42         , _content_type (c)
43         , _fps (fps)
44         , _length (length)
45 {
46         char buffer[64];
47         time_t now;
48         time (&now);
49         struct tm* tm = localtime (&now);
50         strftime (buffer, 64, "%Y-%m-%dT%I:%M:%S+00:00", tm);
51         _date = string (buffer);
52 }
53
54 void
55 DCP::add_sound_asset (list<string> const & files)
56 {
57         filesystem::path p;
58         p /= _directory;
59         p /= "audio.mxf";
60         _assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (files, p.string(), _fps, _length)));
61 }
62
63 void
64 DCP::add_picture_asset (list<string> const & files, int w, int h)
65 {
66         filesystem::path p;
67         p /= _directory;
68         p /= "video.mxf";
69         _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (files, p.string(), _fps, _length, w, h)));
70 }
71
72 /** Write the required XML files to the directory that was
73  *  passed into the constructor.
74  */
75 void
76 DCP::write_xml () const
77 {
78         string cpl_uuid = make_uuid ();
79         string cpl_path = write_cpl (cpl_uuid);
80         int cpl_length = filesystem::file_size (cpl_path);
81         string cpl_digest = make_digest (cpl_path);
82
83         string pkl_uuid = make_uuid ();
84         string pkl_path = write_pkl (pkl_uuid, cpl_uuid, cpl_digest, cpl_length);
85         
86         write_volindex ();
87         write_assetmap (cpl_uuid, cpl_length, pkl_uuid, filesystem::file_size (pkl_path));
88 }
89
90 /** Write the CPL file.
91  *  @param cpl_uuid UUID to use.
92  *  @return CPL pathname.
93  */
94 string
95 DCP::write_cpl (string cpl_uuid) const
96 {
97         filesystem::path p;
98         p /= _directory;
99         stringstream s;
100         s << cpl_uuid << "_cpl.xml";
101         p /= s.str();
102         ofstream cpl (p.string().c_str());
103         
104         cpl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
105             << "<CompositionPlaylist xmlns=\"http://www.smpte-ra.org/schemas/429-7/2006/CPL\">\n"
106             << "  <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
107             << "  <AnnotationText>" << _name << "</AnnotationText>\n"
108             << "  <IssueDate>" << _date << "</IssueDate>\n"
109             << "  <Creator>libdcp " << Tags::instance()->creator << "</Creator>\n"
110             << "  <ContentTitleText>" << _name << "</ContentTitleText>\n"
111             << "  <ContentKind>" << _content_type << "</ContentKind>\n"
112             << "  <ContentVersion>\n"
113             << "    <Id>urn:uri:" << cpl_uuid << "_" << _date << "</Id>\n"
114             << "    <LabelText>" << cpl_uuid << "_" << _date << "</LabelText>\n"
115             << "  </ContentVersion>\n"
116             << "  <RatingList/>\n"
117             << "  <ReelList>\n";
118
119         cpl << "    <Reel>\n"
120             << "      <Id>urn:uuid:" << make_uuid() << "</Id>\n"
121             << "      <AssetList>\n";
122
123         for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
124                 (*i)->write_to_cpl (cpl);
125         }
126
127         cpl << "      </AssetList>\n"
128             << "    </Reel>\n"
129             << "  </ReelList>\n"
130             << "</CompositionPlaylist>\n";
131
132         return p.string ();
133 }
134
135 /** Write the PKL file.
136  *  @param pkl_uuid UUID to use.
137  *  @param cpl_uuid UUID of the CPL file.
138  *  @param cpl_digest SHA digest of the CPL file.
139  *  @param cpl_length Length of the CPL file in bytes.
140  */
141 std::string
142 DCP::write_pkl (string pkl_uuid, string cpl_uuid, string cpl_digest, int cpl_length) const
143 {
144         filesystem::path p;
145         p /= _directory;
146         stringstream s;
147         s << pkl_uuid << "_pkl.xml";
148         p /= s.str();
149         ofstream pkl (p.string().c_str());
150
151         pkl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
152             << "<PackingList xmlns=\"http://www.smpte-ra.org/schemas/429-8/2007/PKL\">\n"
153             << "  <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
154             << "  <AnnotationText>" << _name << "</AnnotationText>\n"
155             << "  <IssueDate>" << _date << "</IssueDate>\n"
156             << "  <Issuer>" << Tags::instance()->issuer << "</Issuer>\n"
157             << "  <Creator>" << Tags::instance()->creator << "</Creator>\n"
158             << "  <AssetList>\n";
159
160         for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
161                 (*i)->write_to_pkl (pkl);
162         }
163
164         pkl << "    <Asset>\n"
165             << "      <Id>urn:uuid" << cpl_uuid << "</Id>\n"
166             << "      <Hash>" << cpl_digest << "</Hash>\n"
167             << "      <Size>" << cpl_length << "</Size>\n"
168             << "      <Type>text/xml</Type>\n"
169             << "    </Asset>\n";
170
171         pkl << "  </AssetList>\n"
172             << "</PackingList>\n";
173
174         return p.string ();
175 }
176
177 void
178 DCP::write_volindex () const
179 {
180         filesystem::path p;
181         p /= _directory;
182         p /= "VOLINDEX.xml";
183         ofstream vi (p.string().c_str());
184
185         vi << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
186            << "<VolumeIndex xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
187            << "  <Index>1</Index>\n"
188            << "</VolumeIndex>\n";
189 }
190
191 void
192 DCP::write_assetmap (string cpl_uuid, int cpl_length, string pkl_uuid, int pkl_length) const
193 {
194         filesystem::path p;
195         p /= _directory;
196         p /= "ASSETMAP.xml";
197         ofstream am (p.string().c_str());
198
199         am << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
200            << "<AssetMap xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
201            << "  <Id>urn:uuid:" << make_uuid() << "</Id>\n"
202            << "  <Creator>" << Tags::instance()->creator << "</Creator>\n"
203            << "  <VolumeCount>1</VolumeCount>\n"
204            << "  <IssueDate>" << _date << "</IssueDate>\n"
205            << "  <Issuer>" << Tags::instance()->issuer << "</Issuer>\n"
206            << "  <AssetList>\n";
207
208         am << "    <Asset>\n"
209            << "      <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
210            << "      <PackingList>true</PackingList>\n"
211            << "      <ChunkList>\n"
212            << "        <Chunk>\n"
213            << "          <Path>" << pkl_uuid << "_pkl.xml</Path>\n"
214            << "          <VolumeIndex>1</VolumeIndex>\n"
215            << "          <Offset>0</Offset>\n"
216            << "          <Length>" << pkl_length << "</Length>\n"
217            << "        </Chunk>\n"
218            << "      </ChunkList>\n"
219            << "    </Asset>\n";
220
221         am << "    <Asset>\n"
222            << "      <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
223            << "      <ChunkList>\n"
224            << "        <Chunk>\n"
225            << "          <Path>" << cpl_uuid << "_cpl.xml</Path>\n"
226            << "          <VolumeIndex>1</VolumeIndex>\n"
227            << "          <Offset>0</Offset>\n"
228            << "          <Length>" << cpl_length << "</Length>\n"
229            << "        </Chunk>\n"
230            << "      </ChunkList>\n"
231            << "    </Asset>\n";
232         
233         for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
234                 (*i)->write_to_assetmap (am);
235         }
236
237         am << "  </AssetList>\n"
238            << "</AssetMap>\n";
239 }
240
241
242 string
243 DCP::content_type_string (ContentType t)
244 {
245         switch (t) {
246         case FEATURE:
247                 return "feature";
248         case SHORT:
249                 return "short";
250         case TRAILER:
251                 return "trailer";
252         case TEST:
253                 return "test";
254         case TRANSITIONAL:
255                 return "transitional";
256         case RATING:
257                 return "rating";
258         case TEASER:
259                 return "teaser";
260         case POLICY:
261                 return "policy";
262         case PUBLIC_SERVICE_ANNOUNCEMENT:
263                 return "psa";
264         case ADVERTISEMENT:
265                 return "advertisement";
266         }
267
268         assert (false);
269 }
270