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