Fix up progress reporting, some better exceptions.
[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  *  @param n Name.
39  *  @param c Content type.
40  *  @param fps Frames per second.
41  *  @param length Length in frames.
42  */
43 DCP::DCP (string d, string n, ContentType c, int fps, int length)
44         : _directory (d)
45         , _name (n)
46         , _content_type (c)
47         , _fps (fps)
48         , _length (length)
49 {
50 }
51
52 /** Add a sound asset.
53  *  @param files Pathnames of WAV files to use in the order Left, Right, Centre, Lfe (sub), Left surround, Right surround.
54  */
55 void
56 DCP::add_sound_asset (list<string> const & files)
57 {
58         filesystem::path p;
59         p /= _directory;
60         p /= "audio.mxf";
61         _assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (files, p.string(), &Progress, _fps, _length)));
62 }
63
64 /** Add a picture asset.
65  *  @param files Pathnames of JPEG2000 files, in frame order.
66  */
67 void
68 DCP::add_picture_asset (list<string> const & files, int w, int h)
69 {
70         filesystem::path p;
71         p /= _directory;
72         p /= "video.mxf";
73         _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (files, p.string(), &Progress, _fps, _length, w, h)));
74 }
75
76 /** Write the required XML files to the directory that was
77  *  passed into the constructor.
78  */
79 void
80 DCP::write_xml () const
81 {
82         string cpl_uuid = make_uuid ();
83         string cpl_path = write_cpl (cpl_uuid);
84         int cpl_length = filesystem::file_size (cpl_path);
85         string cpl_digest = make_digest (cpl_path, 0);
86
87         string pkl_uuid = make_uuid ();
88         string pkl_path = write_pkl (pkl_uuid, cpl_uuid, cpl_digest, cpl_length);
89         
90         write_volindex ();
91         write_assetmap (cpl_uuid, cpl_length, pkl_uuid, filesystem::file_size (pkl_path));
92 }
93
94 /** Write the CPL file.
95  *  @param cpl_uuid UUID to use.
96  *  @return CPL pathname.
97  */
98 string
99 DCP::write_cpl (string cpl_uuid) const
100 {
101         filesystem::path p;
102         p /= _directory;
103         stringstream s;
104         s << cpl_uuid << "_cpl.xml";
105         p /= s.str();
106         ofstream cpl (p.string().c_str());
107         
108         cpl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
109             << "<CompositionPlaylist xmlns=\"http://www.smpte-ra.org/schemas/429-7/2006/CPL\">\n"
110             << "  <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
111             << "  <AnnotationText>" << _name << "</AnnotationText>\n"
112             << "  <IssueDate>" << Tags::instance()->issue_date << "</IssueDate>\n"
113             << "  <Creator>" << Tags::instance()->creator << "</Creator>\n"
114             << "  <ContentTitleText>" << _name << "</ContentTitleText>\n"
115             << "  <ContentKind>" << content_type_string (_content_type) << "</ContentKind>\n"
116             << "  <ContentVersion>\n"
117             << "    <Id>urn:uri:" << cpl_uuid << "_" << Tags::instance()->issue_date << "</Id>\n"
118             << "    <LabelText>" << cpl_uuid << "_" << Tags::instance()->issue_date << "</LabelText>\n"
119             << "  </ContentVersion>\n"
120             << "  <RatingList/>\n"
121             << "  <ReelList>\n";
122
123         cpl << "    <Reel>\n"
124             << "      <Id>urn:uuid:" << make_uuid() << "</Id>\n"
125             << "      <AssetList>\n";
126
127         for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
128                 (*i)->write_to_cpl (cpl);
129         }
130
131         cpl << "      </AssetList>\n"
132             << "    </Reel>\n"
133             << "  </ReelList>\n"
134             << "</CompositionPlaylist>\n";
135
136         return p.string ();
137 }
138
139 /** Write the PKL file.
140  *  @param pkl_uuid UUID to use.
141  *  @param cpl_uuid UUID of the CPL file.
142  *  @param cpl_digest SHA digest of the CPL file.
143  *  @param cpl_length Length of the CPL file in bytes.
144  */
145 std::string
146 DCP::write_pkl (string pkl_uuid, string cpl_uuid, string cpl_digest, int cpl_length) const
147 {
148         filesystem::path p;
149         p /= _directory;
150         stringstream s;
151         s << pkl_uuid << "_pkl.xml";
152         p /= s.str();
153         ofstream pkl (p.string().c_str());
154
155         pkl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
156             << "<PackingList xmlns=\"http://www.smpte-ra.org/schemas/429-8/2007/PKL\">\n"
157             << "  <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
158             << "  <AnnotationText>" << _name << "</AnnotationText>\n"
159             << "  <IssueDate>" << Tags::instance()->issue_date << "</IssueDate>\n"
160             << "  <Issuer>" << Tags::instance()->issuer << "</Issuer>\n"
161             << "  <Creator>" << Tags::instance()->creator << "</Creator>\n"
162             << "  <AssetList>\n";
163
164         for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
165                 (*i)->write_to_pkl (pkl);
166         }
167
168         pkl << "    <Asset>\n"
169             << "      <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
170             << "      <Hash>" << cpl_digest << "</Hash>\n"
171             << "      <Size>" << cpl_length << "</Size>\n"
172             << "      <Type>text/xml</Type>\n"
173             << "    </Asset>\n";
174
175         pkl << "  </AssetList>\n"
176             << "</PackingList>\n";
177
178         return p.string ();
179 }
180
181 /** Write the VOLINDEX file */
182 void
183 DCP::write_volindex () const
184 {
185         filesystem::path p;
186         p /= _directory;
187         p /= "VOLINDEX.xml";
188         ofstream vi (p.string().c_str());
189
190         vi << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
191            << "<VolumeIndex xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
192            << "  <Index>1</Index>\n"
193            << "</VolumeIndex>\n";
194 }
195
196 /** Write the ASSETMAP file.
197  *  @param cpl_uuid UUID of our CPL.
198  *  @param cpl_length Length of our CPL in bytes.
199  *  @param pkl_uuid UUID of our PKL.
200  *  @param pkl_length Length of our PKL in bytes.
201  */
202 void
203 DCP::write_assetmap (string cpl_uuid, int cpl_length, string pkl_uuid, int pkl_length) const
204 {
205         filesystem::path p;
206         p /= _directory;
207         p /= "ASSETMAP.xml";
208         ofstream am (p.string().c_str());
209
210         am << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
211            << "<AssetMap xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
212            << "  <Id>urn:uuid:" << make_uuid() << "</Id>\n"
213            << "  <Creator>" << Tags::instance()->creator << "</Creator>\n"
214            << "  <VolumeCount>1</VolumeCount>\n"
215            << "  <IssueDate>" << Tags::instance()->issue_date << "</IssueDate>\n"
216            << "  <Issuer>" << Tags::instance()->issuer << "</Issuer>\n"
217            << "  <AssetList>\n";
218
219         am << "    <Asset>\n"
220            << "      <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
221            << "      <PackingList>true</PackingList>\n"
222            << "      <ChunkList>\n"
223            << "        <Chunk>\n"
224            << "          <Path>" << pkl_uuid << "_pkl.xml</Path>\n"
225            << "          <VolumeIndex>1</VolumeIndex>\n"
226            << "          <Offset>0</Offset>\n"
227            << "          <Length>" << pkl_length << "</Length>\n"
228            << "        </Chunk>\n"
229            << "      </ChunkList>\n"
230            << "    </Asset>\n";
231
232         am << "    <Asset>\n"
233            << "      <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
234            << "      <ChunkList>\n"
235            << "        <Chunk>\n"
236            << "          <Path>" << cpl_uuid << "_cpl.xml</Path>\n"
237            << "          <VolumeIndex>1</VolumeIndex>\n"
238            << "          <Offset>0</Offset>\n"
239            << "          <Length>" << cpl_length << "</Length>\n"
240            << "        </Chunk>\n"
241            << "      </ChunkList>\n"
242            << "    </Asset>\n";
243         
244         for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
245                 (*i)->write_to_assetmap (am);
246         }
247
248         am << "  </AssetList>\n"
249            << "</AssetMap>\n";
250 }
251
252 /** @param t A content type.
253  *  @return A string representation suitable for use in a CPL.
254  */
255 string
256 DCP::content_type_string (ContentType t)
257 {
258         switch (t) {
259         case FEATURE:
260                 return "feature";
261         case SHORT:
262                 return "short";
263         case TRAILER:
264                 return "trailer";
265         case TEST:
266                 return "test";
267         case TRANSITIONAL:
268                 return "transitional";
269         case RATING:
270                 return "rating";
271         case TEASER:
272                 return "teaser";
273         case POLICY:
274                 return "policy";
275         case PUBLIC_SERVICE_ANNOUNCEMENT:
276                 return "psa";
277         case ADVERTISEMENT:
278                 return "advertisement";
279         }
280
281         assert (false);
282 }
283