Assert that entry_point and intrinsic_duration are not completely bogus.
[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 "dcp_assert.h"
43 #include <libcxml/cxml.h>
44 #include <libxml++/libxml++.h>
45
46 using std::pair;
47 using std::string;
48 using std::make_pair;
49 using std::shared_ptr;
50 using boost::optional;
51 using namespace dcp;
52
53 /** Construct a ReelAsset.
54  *  @param id ID of this ReelAsset (which is that of the MXF, if there is one)
55  *  @param edit_rate Edit rate for the asset.
56  *  @param intrinsic_duration Intrinsic duration of this asset.
57  *  @param entry_point Entry point to use in that asset.
58  */
59 ReelAsset::ReelAsset (string id, Fraction edit_rate, int64_t intrinsic_duration, int64_t entry_point)
60         : Object (id)
61         , _intrinsic_duration (intrinsic_duration)
62         , _duration (intrinsic_duration - entry_point)
63         , _edit_rate (edit_rate)
64         , _entry_point (entry_point)
65 {
66         DCP_ASSERT (_entry_point <= _intrinsic_duration);
67 }
68
69 ReelAsset::ReelAsset (shared_ptr<const cxml::Node> node)
70         : Object (remove_urn_uuid (node->string_child ("Id")))
71         , _intrinsic_duration (node->number_child<int64_t> ("IntrinsicDuration"))
72         , _duration (node->optional_number_child<int64_t>("Duration"))
73         , _annotation_text (node->optional_string_child ("AnnotationText").get_value_or (""))
74         , _edit_rate (Fraction (node->string_child ("EditRate")))
75         , _entry_point (node->optional_number_child<int64_t>("EntryPoint"))
76 {
77
78 }
79
80 xmlpp::Node*
81 ReelAsset::write_to_cpl_asset (xmlpp::Node* node, Standard standard, optional<string> hash) const
82 {
83         xmlpp::Element* a = node->add_child (cpl_node_name (standard));
84         pair<string, string> const attr = cpl_node_attribute (standard);
85         if (!attr.first.empty ()) {
86                 a->set_attribute (attr.first, attr.second);
87         }
88         pair<string, string> const ns = cpl_node_namespace (standard);
89         if (!ns.first.empty ()) {
90                 a->set_namespace_declaration (ns.first, ns.second);
91         }
92         a->add_child("Id")->add_child_text ("urn:uuid:" + _id);
93         a->add_child("AnnotationText")->add_child_text (_annotation_text);
94         a->add_child("EditRate")->add_child_text (_edit_rate.as_string());
95         a->add_child("IntrinsicDuration")->add_child_text (raw_convert<string> (_intrinsic_duration));
96         if (_entry_point) {
97                 a->add_child("EntryPoint")->add_child_text(raw_convert<string>(*_entry_point));
98         }
99         if (_duration) {
100                 a->add_child("Duration")->add_child_text(raw_convert<string>(*_duration));
101         }
102         if (hash) {
103                 a->add_child("Hash")->add_child_text (hash.get());
104         }
105         return a;
106 }
107
108 pair<string, string>
109 ReelAsset::cpl_node_attribute (Standard) const
110 {
111         return make_pair ("", "");
112 }
113
114 pair<string, string>
115 ReelAsset::cpl_node_namespace (Standard) const
116 {
117         return make_pair ("", "");
118 }
119
120 bool
121 ReelAsset::asset_equals (shared_ptr<const ReelAsset> other, EqualityOptions opt, NoteHandler note) const
122 {
123         if (_annotation_text != other->_annotation_text) {
124                 string const s = "Reel: annotation texts differ (" + _annotation_text + " vs " + other->_annotation_text + ")\n";
125                 if (!opt.reel_annotation_texts_can_differ) {
126                         note (DCP_ERROR, s);
127                         return false;
128                 } else {
129                         note (DCP_NOTE, s);
130                 }
131         }
132
133         if (_edit_rate != other->_edit_rate) {
134                 note (DCP_ERROR, "Reel: edit rates differ");
135                 return false;
136         }
137
138         if (_intrinsic_duration != other->_intrinsic_duration) {
139                 note (DCP_ERROR, String::compose ("Reel: intrinsic durations differ (%1 vs %2)", _intrinsic_duration, other->_intrinsic_duration));
140                 return false;
141         }
142
143         if (_entry_point != other->_entry_point) {
144                 note (DCP_ERROR, "Reel: entry points differ");
145                 return false;
146         }
147
148         if (_duration != other->_duration) {
149                 note (DCP_ERROR, "Reel: durations differ");
150                 return false;
151         }
152
153         return true;
154 }
155
156 /** @return <Duration>, or <IntrinsicDuration> - <EntryPoint> if <Duration> is not present */
157 int64_t
158 ReelAsset::actual_duration () const
159 {
160         if (_duration) {
161                 return *_duration;
162         }
163
164         return _intrinsic_duration - _entry_point.get_value_or(0);
165 }