Remove stringstream from Time.
[libdcp.git] / src / reel.cc
1 /*
2     Copyright (C) 2014-2015 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 #include "reel.h"
35 #include "util.h"
36 #include "picture_asset.h"
37 #include "mono_picture_asset.h"
38 #include "stereo_picture_asset.h"
39 #include "sound_asset.h"
40 #include "subtitle_asset.h"
41 #include "reel_mono_picture_asset.h"
42 #include "reel_stereo_picture_asset.h"
43 #include "reel_sound_asset.h"
44 #include "reel_subtitle_asset.h"
45 #include "decrypted_kdm_key.h"
46 #include "decrypted_kdm.h"
47 #include "interop_subtitle_asset.h"
48 #include "reel_atmos_asset.h"
49 #include <libxml++/nodes/element.h>
50
51 using std::string;
52 using std::list;
53 using std::cout;
54 using std::max;
55 using boost::shared_ptr;
56 using boost::dynamic_pointer_cast;
57 using namespace dcp;
58
59 Reel::Reel (boost::shared_ptr<const cxml::Node> node)
60         : Object (remove_urn_uuid (node->string_child ("Id")))
61 {
62         shared_ptr<cxml::Node> asset_list = node->node_child ("AssetList");
63
64         shared_ptr<cxml::Node> main_picture = asset_list->optional_node_child ("MainPicture");
65         if (main_picture) {
66                 _main_picture.reset (new ReelMonoPictureAsset (main_picture));
67         }
68
69         shared_ptr<cxml::Node> main_stereoscopic_picture = asset_list->optional_node_child ("MainStereoscopicPicture");
70         if (main_stereoscopic_picture) {
71                 _main_picture.reset (new ReelStereoPictureAsset (main_stereoscopic_picture));
72         }
73
74         shared_ptr<cxml::Node> main_sound = asset_list->optional_node_child ("MainSound");
75         if (main_sound) {
76                 _main_sound.reset (new ReelSoundAsset (main_sound));
77         }
78
79         shared_ptr<cxml::Node> main_subtitle = asset_list->optional_node_child ("MainSubtitle");
80         if (main_subtitle) {
81                 _main_subtitle.reset (new ReelSubtitleAsset (main_subtitle));
82         }
83
84         shared_ptr<cxml::Node> atmos = asset_list->optional_node_child ("axd:AuxData");
85         if (atmos) {
86                 _atmos.reset (new ReelAtmosAsset (atmos));
87         }
88
89         node->ignore_child ("AnnotationText");
90         node->done ();
91 }
92
93 void
94 Reel::write_to_cpl (xmlpp::Element* node, Standard standard) const
95 {
96         xmlpp::Element* reel = node->add_child ("Reel");
97         reel->add_child("Id")->add_child_text ("urn:uuid:" + make_uuid());
98         xmlpp::Element* asset_list = reel->add_child ("AssetList");
99
100         if (_main_picture && dynamic_pointer_cast<ReelMonoPictureAsset> (_main_picture)) {
101                 /* Mono pictures come before other stuff... */
102                 _main_picture->write_to_cpl (asset_list, standard);
103         }
104
105         if (_main_sound) {
106                 _main_sound->write_to_cpl (asset_list, standard);
107         }
108
109         if (_main_subtitle) {
110                 _main_subtitle->write_to_cpl (asset_list, standard);
111         }
112
113         if (_main_picture && dynamic_pointer_cast<ReelStereoPictureAsset> (_main_picture)) {
114                 /* ... but stereo pictures must come after */
115                 _main_picture->write_to_cpl (asset_list, standard);
116         }
117
118         if (_atmos) {
119                 _atmos->write_to_cpl (asset_list, standard);
120         }
121 }
122
123 bool
124 Reel::equals (boost::shared_ptr<const Reel> other, EqualityOptions opt, NoteHandler note) const
125 {
126         if ((_main_picture && !other->_main_picture) || (!_main_picture && other->_main_picture)) {
127                 note (DCP_ERROR, "Reel: assets differ");
128                 return false;
129         }
130
131         if (_main_picture && !_main_picture->equals (other->_main_picture, opt, note)) {
132                 return false;
133         }
134
135         if ((_main_sound && !other->_main_sound) || (!_main_sound && other->_main_sound)) {
136                 note (DCP_ERROR, "Reel: assets differ");
137                 return false;
138         }
139
140         if (_main_sound && !_main_sound->equals (other->_main_sound, opt, note)) {
141                 return false;
142         }
143
144         if ((_main_subtitle && !other->_main_subtitle) || (!_main_subtitle && other->_main_subtitle)) {
145                 note (DCP_ERROR, "Reel: assets differ");
146                 return false;
147         }
148
149         if (_main_subtitle && !_main_subtitle->equals (other->_main_subtitle, opt, note)) {
150                 return false;
151         }
152
153         if ((_atmos && !other->_atmos) || (!_atmos && other->_atmos)) {
154                 note (DCP_ERROR, "Reel: assets differ");
155                 return false;
156         }
157
158         if (_atmos && !_atmos->equals (other->_atmos, opt, note)) {
159                 return false;
160         }
161
162         return true;
163 }
164
165 bool
166 Reel::encrypted () const
167 {
168         return (
169                 (_main_picture && _main_picture->encrypted ()) ||
170                 (_main_sound && _main_sound->encrypted ()) ||
171                 (_atmos && _atmos->encrypted ())
172                 );
173 }
174
175 void
176 Reel::add (DecryptedKDM const & kdm)
177 {
178         list<DecryptedKDMKey> keys = kdm.keys ();
179
180         for (list<DecryptedKDMKey>::iterator i = keys.begin(); i != keys.end(); ++i) {
181                 if (_main_picture && i->id() == _main_picture->key_id()) {
182                         _main_picture->asset()->set_key (i->key ());
183                 }
184                 if (_main_sound && i->id() == _main_sound->key_id()) {
185                         _main_sound->asset()->set_key (i->key ());
186                 }
187                 if (_atmos && i->id() == _atmos->key_id()) {
188                         _atmos->asset()->set_key (i->key ());
189                 }
190         }
191 }
192
193 void
194 Reel::add (shared_ptr<ReelAsset> asset)
195 {
196         shared_ptr<ReelPictureAsset> p = dynamic_pointer_cast<ReelPictureAsset> (asset);
197         shared_ptr<ReelSoundAsset> so = dynamic_pointer_cast<ReelSoundAsset> (asset);
198         shared_ptr<ReelSubtitleAsset> su = dynamic_pointer_cast<ReelSubtitleAsset> (asset);
199         shared_ptr<ReelAtmosAsset> a = dynamic_pointer_cast<ReelAtmosAsset> (asset);
200         if (p) {
201                 _main_picture = p;
202         } else if (so) {
203                 _main_sound = so;
204         } else if (su) {
205                 _main_subtitle = su;
206         } else if (a) {
207                 _atmos = a;
208         }
209 }
210
211 void
212 Reel::resolve_refs (list<shared_ptr<Asset> > assets)
213 {
214         if (_main_picture) {
215                 _main_picture->asset_ref().resolve (assets);
216         }
217
218         if (_main_sound) {
219                 _main_sound->asset_ref().resolve (assets);
220         }
221
222         if (_main_subtitle) {
223                 _main_subtitle->asset_ref().resolve (assets);
224
225                 /* Interop subtitle handling is all special cases */
226                 shared_ptr<InteropSubtitleAsset> iop = dynamic_pointer_cast<InteropSubtitleAsset> (_main_subtitle->asset_ref().asset ());
227                 if (iop) {
228                         iop->resolve_fonts (assets);
229                 }
230         }
231
232         if (_atmos) {
233                 _atmos->asset_ref().resolve (assets);
234         }
235 }
236
237 int64_t
238 Reel::duration () const
239 {
240         int64_t d = 0;
241
242         if (_main_picture) {
243                 d = max (d, _main_picture->duration ());
244         }
245         if (_main_sound) {
246                 d = max (d, _main_sound->duration ());
247         }
248         if (_main_subtitle) {
249                 d = max (d, _main_subtitle->duration ());
250         }
251         if (_atmos) {
252                 d = max (d, _atmos->duration ());
253         }
254
255         return d;
256 }