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