Bv2.1 7.2.3: Check that subtitle <StartTime> exists and is 0.
[libdcp.git] / src / combine.cc
1 /*
2     Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 #include "asset.h"
36 #include "combine.h"
37 #include "cpl.h"
38 #include "dcp.h"
39 #include "dcp_assert.h"
40 #include "exceptions.h"
41 #include "font_asset.h"
42 #include "interop_subtitle_asset.h"
43 #include "raw_convert.h"
44 #include <boost/filesystem.hpp>
45 #include <boost/foreach.hpp>
46 #include <set>
47 #include <string>
48 #include <vector>
49
50
51 using std::list;
52 using std::map;
53 using std::set;
54 using std::string;
55 using std::vector;
56 using std::dynamic_pointer_cast;
57 using boost::optional;
58 using std::shared_ptr;
59
60
61 boost::filesystem::path
62 make_unique (boost::filesystem::path path)
63 {
64         if (!boost::filesystem::exists(path)) {
65                 return path;
66         }
67
68         for (int i = 0; i < 10000; ++i) {
69                 boost::filesystem::path p = path.parent_path() / (path.stem().string() + dcp::raw_convert<string>(i) + path.extension().string());
70                 if (!boost::filesystem::exists(p)) {
71                         return p;
72                 }
73         }
74
75         DCP_ASSERT (false);
76         return path;
77 }
78
79
80 static
81 void
82 create_hard_link_or_copy (boost::filesystem::path from, boost::filesystem::path to)
83 {
84         try {
85                 create_hard_link (from, to);
86         } catch (boost::filesystem::filesystem_error& e) {
87                 if (e.code() == boost::system::errc::cross_device_link) {
88                         copy_file (from, to);
89                 } else {
90                         throw;
91                 }
92         }
93 }
94
95
96 void
97 dcp::combine (
98         vector<boost::filesystem::path> inputs,
99         boost::filesystem::path output,
100         string issuer,
101         string creator,
102         string issue_date,
103         string annotation_text,
104         shared_ptr<const CertificateChain> signer
105         )
106 {
107         using namespace boost::filesystem;
108
109         DCP_ASSERT (!inputs.empty());
110
111         DCP output_dcp (output);
112         optional<dcp::Standard> standard;
113
114         BOOST_FOREACH (path i, inputs) {
115                 DCP dcp (i);
116                 dcp.read ();
117                 if (!standard) {
118                         standard = *dcp.standard();
119                 } else if (standard != dcp.standard()) {
120                         throw CombineError ("Cannot combine Interop and SMPTE DCPs.");
121                 }
122         }
123
124         list<path> paths;
125         list<shared_ptr<dcp::Asset> > assets;
126
127         BOOST_FOREACH (path i, inputs) {
128                 DCP dcp (i);
129                 dcp.read ();
130
131                 BOOST_FOREACH (shared_ptr<dcp::CPL> j, dcp.cpls()) {
132                         output_dcp.add (j);
133                 }
134
135                 BOOST_FOREACH (shared_ptr<dcp::Asset> j, dcp.assets(true)) {
136                         if (dynamic_pointer_cast<dcp::CPL>(j)) {
137                                 continue;
138                         }
139
140                         optional<path> file = j->file();
141                         DCP_ASSERT (file);
142                         path new_path = make_unique(output / file->filename());
143
144                         shared_ptr<dcp::InteropSubtitleAsset> sub = dynamic_pointer_cast<dcp::InteropSubtitleAsset>(j);
145                         if (sub) {
146                                 /* Interop fonts are really fiddly.  The font files are assets (in the ASSETMAP)
147                                  * and also linked from the font XML by filename.  We have to fix both these things,
148                                  * and re-write the font XML file since the font URI might have changed if it's a duplicate
149                                  * with another DCP.
150                                  */
151                                 map<string, path> fonts = sub->font_filenames ();
152                                 for (map<string, path>::const_iterator k = fonts.begin(); k != fonts.end(); ++k) {
153                                         sub->set_font_file (k->first, make_unique(output / k->second.filename()));
154                                 }
155                                 sub->write (new_path);
156                         } else if (!dynamic_pointer_cast<dcp::FontAsset>(j)) {
157                                 /* Take care of everything else that's not a Interop subtitle asset, Interop font file
158                                  * or CPL.
159                                  */
160                                 optional<path> file = j->file();
161                                 DCP_ASSERT (file);
162                                 path new_path = make_unique(output / file->filename());
163                                 create_hard_link_or_copy (*file, new_path);
164                                 j->set_file (new_path);
165                         }
166
167                         assets.push_back (j);
168                 }
169         }
170
171         output_dcp.resolve_refs (assets);
172         output_dcp.write_xml (*standard, issuer, creator, issue_date, annotation_text, signer);
173 }