Various test fixes.
[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::make_pair;
29 using boost::shared_ptr;
30 using namespace dcp;
31
32 ReelAsset::ReelAsset ()
33         : Object (make_uuid ())
34         , _content (_id)
35         , _edit_rate (Fraction (24, 1))
36         , _intrinsic_duration (0)
37         , _entry_point (0)
38         , _duration (0)
39 {
40
41 }
42
43 /** Construct a ReelAsset.
44  *  @param content Content that this asset refers to.
45  *  @param edit_rate Edit rate for the content.
46  *  @param intrinsic_duration Intrinsic duration of this content.
47  *  @param entry_point Entry point to use in that content.
48  */
49 ReelAsset::ReelAsset (boost::shared_ptr<Content> content, Fraction edit_rate, int64_t intrinsic_duration, int64_t entry_point)
50         : Object (content->id ())
51         , _content (content)
52         , _edit_rate (edit_rate)
53         , _intrinsic_duration (intrinsic_duration)
54         , _entry_point (entry_point)
55         , _duration (intrinsic_duration - entry_point)
56         , _hash (make_digest (content->file (), 0))
57 {
58         /* default _annotation_text to the leaf name of our file */
59         _annotation_text = content->file().leaf().string ();
60 }
61
62 ReelAsset::ReelAsset (boost::shared_ptr<const cxml::Node> node)
63         : Object (node->string_child ("Id"))
64         , _content (_id)
65         , _annotation_text (node->optional_string_child ("AnnotationText").get_value_or (""))
66         , _edit_rate (Fraction (node->string_child ("EditRate")))
67         , _intrinsic_duration (node->number_child<int64_t> ("IntrinsicDuration"))
68         , _entry_point (node->number_child<int64_t> ("EntryPoint"))
69         , _duration (node->number_child<int64_t> ("Duration"))
70         , _hash (node->optional_string_child ("Hash").get_value_or (""))
71         , _key_id (node->optional_string_child ("KeyId").get_value_or (""))
72 {
73         if (_id.length() > 9) {
74                 _id = _id.substr (9);
75                 _content.set_id (_id);
76         }
77         
78         if (_key_id.length() > 9) {
79                 _key_id = _key_id.substr (9);
80         }
81 }
82
83 void
84 ReelAsset::write_to_cpl (xmlpp::Node* node, Standard) const
85 {
86         pair<string, string> const attr = cpl_node_attribute ();
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         if (!_key_id.empty ()) {
98                 a->add_child("KeyId")->add_child_text ("urn:uuid:" + _key_id);
99         }
100         a->add_child("Hash")->add_child_text (_content.object()->hash ());
101 }
102
103 pair<string, string>
104 ReelAsset::cpl_node_attribute () const
105 {
106         return make_pair ("", "");
107 }