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