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