No-op: whitespace.
[libdcp.git] / src / reel.cc
1 /*
2     Copyright (C) 2014-2015 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 "reel.h"
21 #include "util.h"
22 #include "picture_asset.h"
23 #include "mono_picture_asset.h"
24 #include "stereo_picture_asset.h"
25 #include "sound_asset.h"
26 #include "subtitle_asset.h"
27 #include "reel_mono_picture_asset.h"
28 #include "reel_stereo_picture_asset.h"
29 #include "reel_sound_asset.h"
30 #include "reel_subtitle_asset.h"
31 #include "decrypted_kdm_key.h"
32 #include "decrypted_kdm.h"
33 #include "interop_subtitle_asset.h"
34 #include <libxml++/nodes/element.h>
35
36 using std::string;
37 using std::list;
38 using std::cout;
39 using boost::shared_ptr;
40 using boost::dynamic_pointer_cast;
41 using namespace dcp;
42
43 Reel::Reel (boost::shared_ptr<const cxml::Node> node)
44         : Object (node->string_child ("Id"))
45 {
46         shared_ptr<cxml::Node> asset_list = node->node_child ("AssetList");
47
48         shared_ptr<cxml::Node> main_picture = asset_list->optional_node_child ("MainPicture");
49         if (main_picture) {
50                 _main_picture.reset (new ReelMonoPictureAsset (main_picture));
51         }
52
53         shared_ptr<cxml::Node> main_stereoscopic_picture = asset_list->optional_node_child ("MainStereoscopicPicture");
54         if (main_stereoscopic_picture) {
55                 _main_picture.reset (new ReelStereoPictureAsset (main_stereoscopic_picture));
56         }
57
58         shared_ptr<cxml::Node> main_sound = asset_list->optional_node_child ("MainSound");
59         if (main_sound) {
60                 _main_sound.reset (new ReelSoundAsset (main_sound));
61         }
62
63         shared_ptr<cxml::Node> main_subtitle = asset_list->optional_node_child ("MainSubtitle");
64         if (main_subtitle) {
65                 _main_subtitle.reset (new ReelSubtitleAsset (main_subtitle));
66         }
67
68         node->ignore_child ("AnnotationText");
69         node->done ();
70 }
71
72 void
73 Reel::write_to_cpl (xmlpp::Element* node, Standard standard) const
74 {
75         xmlpp::Element* reel = node->add_child ("Reel");
76         reel->add_child("Id")->add_child_text ("urn:uuid:" + make_uuid());
77         xmlpp::Element* asset_list = reel->add_child ("AssetList");
78
79         if (_main_picture && dynamic_pointer_cast<ReelMonoPictureAsset> (_main_picture)) {
80                 /* Mono pictures come before other stuff... */
81                 _main_picture->write_to_cpl (asset_list, standard);
82         }
83
84         if (_main_sound) {
85                 _main_sound->write_to_cpl (asset_list, standard);
86         }
87
88         if (_main_subtitle) {
89                 _main_subtitle->write_to_cpl (asset_list, standard);
90         }
91
92         if (_main_picture && dynamic_pointer_cast<ReelStereoPictureAsset> (_main_picture)) {
93                 /* ... but stereo pictures must come after */
94                 _main_picture->write_to_cpl (asset_list, standard);
95         }
96 }
97
98 bool
99 Reel::equals (boost::shared_ptr<const Reel> other, EqualityOptions opt, NoteHandler note) const
100 {
101         if ((_main_picture && !other->_main_picture) || (!_main_picture && other->_main_picture)) {
102                 note (DCP_ERROR, "Reel: assets differ");
103                 return false;
104         }
105
106         if (_main_picture && !_main_picture->equals (other->_main_picture, opt, note)) {
107                 return false;
108         }
109
110         if ((_main_sound && !other->_main_sound) || (!_main_sound && other->_main_sound)) {
111                 note (DCP_ERROR, "Reel: assets differ");
112                 return false;
113         }
114
115         if (_main_sound && !_main_sound->equals (other->_main_sound, opt, note)) {
116                 return false;
117         }
118
119         if ((_main_subtitle && !other->_main_subtitle) || (!_main_subtitle && other->_main_subtitle)) {
120                 note (DCP_ERROR, "Reel: assets differ");
121                 return false;
122         }
123
124         if (_main_subtitle && !_main_subtitle->equals (other->_main_subtitle, opt, note)) {
125                 return false;
126         }
127
128         return true;
129 }
130
131 bool
132 Reel::encrypted () const
133 {
134         return ((_main_picture && _main_picture->encrypted ()) || (_main_sound && _main_sound->encrypted ()));
135 }
136
137 void
138 Reel::add (DecryptedKDM const & kdm)
139 {
140         list<DecryptedKDMKey> keys = kdm.keys ();
141
142         for (list<DecryptedKDMKey>::iterator i = keys.begin(); i != keys.end(); ++i) {
143                 if (i->id() == _main_picture->key_id()) {
144                         _main_picture->asset()->set_key (i->key ());
145                 }
146                 if (i->id() == _main_sound->key_id()) {
147                         _main_sound->asset()->set_key (i->key ());
148                 }
149         }
150 }
151
152 void
153 Reel::add (shared_ptr<ReelAsset> asset)
154 {
155         shared_ptr<ReelPictureAsset> p = dynamic_pointer_cast<ReelPictureAsset> (asset);
156         shared_ptr<ReelSoundAsset> so = dynamic_pointer_cast<ReelSoundAsset> (asset);
157         shared_ptr<ReelSubtitleAsset> su = dynamic_pointer_cast<ReelSubtitleAsset> (asset);
158         if (p) {
159                 _main_picture = p;
160         } else if (so) {
161                 _main_sound = so;
162         } else if (su) {
163                 _main_subtitle = su;
164         }
165 }
166
167 void
168 Reel::resolve_refs (list<shared_ptr<Object> > objects)
169 {
170         if (_main_picture) {
171                 _main_picture->asset_ref().resolve (objects);
172         }
173
174         if (_main_sound) {
175                 _main_sound->asset_ref().resolve (objects);
176         }
177
178         if (_main_subtitle) {
179                 _main_subtitle->asset_ref().resolve (objects);
180
181                 /* Interop subtitle handling is all special cases */
182                 shared_ptr<InteropSubtitleAsset> iop = dynamic_pointer_cast<InteropSubtitleAsset> (_main_subtitle->asset_ref().object ());
183                 if (iop) {
184                         iop->resolve_fonts (objects);
185                 }
186         }
187 }