27ae9636da7ee3a4cff317acc929f1a29518fa51
[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 /** @file  src/dcp.cc
21  *  @brief A class to create a DCP.
22  */
23
24 #include <sstream>
25 #include <fstream>
26 #include <iomanip>
27 #include <cassert>
28 #include <boost/filesystem.hpp>
29 #include "dcp.h"
30 #include "asset.h"
31 #include "sound_asset.h"
32 #include "picture_asset.h"
33 #include "util.h"
34 #include "metadata.h"
35
36 using namespace std;
37 using namespace boost;
38 using namespace libdcp;
39
40 DCP::DCP (string directory, string name, ContentType content_type, int fps, int length)
41         : _directory (directory)
42         , _name (name)
43         , _content_type (content_type)
44         , _fps (fps)
45         , _length (length)
46 {
47         
48 }
49
50 void
51 DCP::add_sound_asset (vector<string> const & files)
52 {
53         filesystem::path p;
54         p /= _directory;
55         p /= "audio.mxf";
56         _assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (files, p.string(), &Progress, _fps, _length)));
57 }
58
59 void
60 DCP::add_sound_asset (sigc::slot<string, Channel> get_path, int channels)
61 {
62         filesystem::path p;
63         p /= _directory;
64         p /= "audio.mxf";
65         _assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (get_path, p.string(), &Progress, _fps, _length, channels)));
66 }
67
68 void
69 DCP::add_picture_asset (vector<string> const & files, int width, int height)
70 {
71         filesystem::path p;
72         p /= _directory;
73         p /= "video.mxf";
74         _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (files, p.string(), &Progress, _fps, _length, width, height)));
75 }
76
77 void
78 DCP::add_picture_asset (sigc::slot<string, int> get_path, int width, int height)
79 {
80         filesystem::path p;
81         p /= _directory;
82         p /= "video.mxf";
83         _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (get_path, p.string(), &Progress, _fps, _length, width, height)));
84 }
85
86 void
87 DCP::write_xml () const
88 {
89         string cpl_uuid = make_uuid ();
90         string cpl_path = write_cpl (cpl_uuid);
91         int cpl_length = filesystem::file_size (cpl_path);
92         string cpl_digest = make_digest (cpl_path, 0);
93
94         string pkl_uuid = make_uuid ();
95         string pkl_path = write_pkl (pkl_uuid, cpl_uuid, cpl_digest, cpl_length);
96         
97         write_volindex ();
98         write_assetmap (cpl_uuid, cpl_length, pkl_uuid, filesystem::file_size (pkl_path));
99 }
100
101 string
102 DCP::write_cpl (string cpl_uuid) const
103 {
104         filesystem::path p;
105         p /= _directory;
106         stringstream s;
107         s << cpl_uuid << "_cpl.xml";
108         p /= s.str();
109         ofstream cpl (p.string().c_str());
110         
111         cpl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
112             << "<CompositionPlaylist xmlns=\"http://www.smpte-ra.org/schemas/429-7/2006/CPL\">\n"
113             << "  <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
114             << "  <AnnotationText>" << _name << "</AnnotationText>\n"
115             << "  <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
116             << "  <Creator>" << Metadata::instance()->creator << "</Creator>\n"
117             << "  <ContentTitleText>" << _name << "</ContentTitleText>\n"
118             << "  <ContentKind>" << content_type_string (_content_type) << "</ContentKind>\n"
119             << "  <ContentVersion>\n"
120             << "    <Id>urn:uri:" << cpl_uuid << "_" << Metadata::instance()->issue_date << "</Id>\n"
121             << "    <LabelText>" << cpl_uuid << "_" << Metadata::instance()->issue_date << "</LabelText>\n"
122             << "  </ContentVersion>\n"
123             << "  <RatingList/>\n"
124             << "  <ReelList>\n";
125
126         cpl << "    <Reel>\n"
127             << "      <Id>urn:uuid:" << make_uuid() << "</Id>\n"
128             << "      <AssetList>\n";
129
130         for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
131                 (*i)->write_to_cpl (cpl);
132         }
133
134         cpl << "      </AssetList>\n"
135             << "    </Reel>\n"
136             << "  </ReelList>\n"
137             << "</CompositionPlaylist>\n";
138
139         return p.string ();
140 }
141
142 std::string
143 DCP::write_pkl (string pkl_uuid, string cpl_uuid, string cpl_digest, int cpl_length) const
144 {
145         filesystem::path p;
146         p /= _directory;
147         stringstream s;
148         s << pkl_uuid << "_pkl.xml";
149         p /= s.str();
150         ofstream pkl (p.string().c_str());
151
152         pkl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
153             << "<PackingList xmlns=\"http://www.smpte-ra.org/schemas/429-8/2007/PKL\">\n"
154             << "  <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
155             << "  <AnnotationText>" << _name << "</AnnotationText>\n"
156             << "  <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
157             << "  <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
158             << "  <Creator>" << Metadata::instance()->creator << "</Creator>\n"
159             << "  <AssetList>\n";
160
161         for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
162                 (*i)->write_to_pkl (pkl);
163         }
164
165         pkl << "    <Asset>\n"
166             << "      <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
167             << "      <Hash>" << cpl_digest << "</Hash>\n"
168             << "      <Size>" << cpl_length << "</Size>\n"
169             << "      <Type>text/xml</Type>\n"
170             << "    </Asset>\n";
171
172         pkl << "  </AssetList>\n"
173             << "</PackingList>\n";
174
175         return p.string ();
176 }
177
178 void
179 DCP::write_volindex () const
180 {
181         filesystem::path p;
182         p /= _directory;
183         p /= "VOLINDEX.xml";
184         ofstream vi (p.string().c_str());
185
186         vi << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
187            << "<VolumeIndex xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
188            << "  <Index>1</Index>\n"
189            << "</VolumeIndex>\n";
190 }
191
192 void
193 DCP::write_assetmap (string cpl_uuid, int cpl_length, string pkl_uuid, int pkl_length) const
194 {
195         filesystem::path p;
196         p /= _directory;
197         p /= "ASSETMAP.xml";
198         ofstream am (p.string().c_str());
199
200         am << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
201            << "<AssetMap xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
202            << "  <Id>urn:uuid:" << make_uuid() << "</Id>\n"
203            << "  <Creator>" << Metadata::instance()->creator << "</Creator>\n"
204            << "  <VolumeCount>1</VolumeCount>\n"
205            << "  <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
206            << "  <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
207            << "  <AssetList>\n";
208
209         am << "    <Asset>\n"
210            << "      <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
211            << "      <PackingList>true</PackingList>\n"
212            << "      <ChunkList>\n"
213            << "        <Chunk>\n"
214            << "          <Path>" << pkl_uuid << "_pkl.xml</Path>\n"
215            << "          <VolumeIndex>1</VolumeIndex>\n"
216            << "          <Offset>0</Offset>\n"
217            << "          <Length>" << pkl_length << "</Length>\n"
218            << "        </Chunk>\n"
219            << "      </ChunkList>\n"
220            << "    </Asset>\n";
221
222         am << "    <Asset>\n"
223            << "      <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
224            << "      <ChunkList>\n"
225            << "        <Chunk>\n"
226            << "          <Path>" << cpl_uuid << "_cpl.xml</Path>\n"
227            << "          <VolumeIndex>1</VolumeIndex>\n"
228            << "          <Offset>0</Offset>\n"
229            << "          <Length>" << cpl_length << "</Length>\n"
230            << "        </Chunk>\n"
231            << "      </ChunkList>\n"
232            << "    </Asset>\n";
233         
234         for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
235                 (*i)->write_to_assetmap (am);
236         }
237
238         am << "  </AssetList>\n"
239            << "</AssetMap>\n";
240 }
241
242 string
243 DCP::content_type_string (ContentType type)
244 {
245         switch (type) {
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