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