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