Fix thinko causing hang in ::equals().
[libdcp.git] / src / reel_asset.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 /** @file  src/reel_asset.cc
35  *  @brief ReelAsset class.
36  */
37
38 #include "raw_convert.h"
39 #include "reel_asset.h"
40 #include "asset.h"
41 #include "compose.hpp"
42 #include <libcxml/cxml.h>
43 #include <libxml++/libxml++.h>
44
45 using std::pair;
46 using std::string;
47 using std::make_pair;
48 using boost::shared_ptr;
49 using namespace dcp;
50
51 ReelAsset::ReelAsset ()
52         : _asset_ref (_id)
53         , _edit_rate (Fraction (24, 1))
54         , _intrinsic_duration (0)
55         , _entry_point (0)
56         , _duration (0)
57 {
58
59 }
60
61 /** Construct a ReelAsset.
62  *  @param asset Asset that this ReelAsset refers to.
63  *  @param edit_rate Edit rate for the asset.
64  *  @param intrinsic_duration Intrinsic duration of this asset.
65  *  @param entry_point Entry point to use in that asset.
66  */
67 ReelAsset::ReelAsset (shared_ptr<Asset> asset, Fraction edit_rate, int64_t intrinsic_duration, int64_t entry_point)
68         : Object (asset->id ())
69         , _asset_ref (asset)
70         , _edit_rate (edit_rate)
71         , _intrinsic_duration (intrinsic_duration)
72         , _entry_point (entry_point)
73         , _duration (intrinsic_duration - entry_point)
74         , _hash (asset->hash ())
75 {
76         /* default _annotation_text to the leaf name of our file */
77         if (asset->file ()) {
78                 _annotation_text = asset->file()->leaf().string ();
79         }
80 }
81
82 ReelAsset::ReelAsset (shared_ptr<const cxml::Node> node)
83         : Object (remove_urn_uuid (node->string_child ("Id")))
84         , _asset_ref (_id)
85         , _annotation_text (node->optional_string_child ("AnnotationText").get_value_or (""))
86         , _edit_rate (Fraction (node->string_child ("EditRate")))
87         , _intrinsic_duration (node->number_child<int64_t> ("IntrinsicDuration"))
88         , _entry_point (node->number_child<int64_t> ("EntryPoint"))
89         , _duration (node->number_child<int64_t> ("Duration"))
90         , _hash (node->optional_string_child ("Hash"))
91 {
92
93 }
94
95 void
96 ReelAsset::write_to_cpl (xmlpp::Node* node, Standard standard) const
97 {
98         xmlpp::Element* a = node->add_child (cpl_node_name (standard));
99         pair<string, string> const attr = cpl_node_attribute (standard);
100         if (!attr.first.empty ()) {
101                 a->set_attribute (attr.first, attr.second);
102         }
103         pair<string, string> const ns = cpl_node_namespace (standard);
104         if (!ns.first.empty ()) {
105                 a->set_namespace_declaration (ns.first, ns.second);
106         }
107         a->add_child("Id")->add_child_text ("urn:uuid:" + _id);
108         a->add_child("AnnotationText")->add_child_text (_annotation_text);
109         a->add_child("EditRate")->add_child_text (String::compose ("%1 %2", _edit_rate.numerator, _edit_rate.denominator));
110         a->add_child("IntrinsicDuration")->add_child_text (raw_convert<string> (_intrinsic_duration));
111         a->add_child("EntryPoint")->add_child_text (raw_convert<string> (_entry_point));
112         a->add_child("Duration")->add_child_text (raw_convert<string> (_duration));
113         if (_hash) {
114                 a->add_child("Hash")->add_child_text (_hash.get());
115         }
116 }
117
118 pair<string, string>
119 ReelAsset::cpl_node_attribute (Standard) const
120 {
121         return make_pair ("", "");
122 }
123
124 pair<string, string>
125 ReelAsset::cpl_node_namespace (Standard) const
126 {
127         return make_pair ("", "");
128 }
129
130 bool
131 ReelAsset::equals (shared_ptr<const ReelAsset> other, EqualityOptions opt, NoteHandler note) const
132 {
133         if (_annotation_text != other->_annotation_text) {
134                 string const s = "Reel: annotation texts differ (" + _annotation_text + " vs " + other->_annotation_text + ")\n";
135                 if (!opt.reel_annotation_texts_can_differ) {
136                         note (DCP_ERROR, s);
137                         return false;
138                 } else {
139                         note (DCP_NOTE, s);
140                 }
141         }
142
143         if (_edit_rate != other->_edit_rate) {
144                 note (DCP_ERROR, "Reel: edit rates differ");
145                 return false;
146         }
147
148         if (_intrinsic_duration != other->_intrinsic_duration) {
149                 note (DCP_ERROR, String::compose ("Reel: intrinsic durations differ (%1 vs %2)", _intrinsic_duration, other->_intrinsic_duration));
150                 return false;
151         }
152
153         if (_entry_point != other->_entry_point) {
154                 note (DCP_ERROR, "Reel: entry points differ");
155                 return false;
156         }
157
158         if (_duration != other->_duration) {
159                 note (DCP_ERROR, "Reel: durations differ");
160                 return false;
161         }
162
163         if (_hash != other->_hash) {
164                 if (!opt.reel_hashes_can_differ) {
165                         note (DCP_ERROR, "Reel: hashes differ");
166                         return false;
167                 } else {
168                         note (DCP_NOTE, "Reel: hashes differ");
169                 }
170         }
171
172         if (_asset_ref.resolved () && other->_asset_ref.resolved ()) {
173                 return _asset_ref->equals (other->_asset_ref.asset(), opt, note);
174         }
175
176         return true;
177 }