Rename some stuff Content -> Asset.
[libdcp.git] / src / smpte_subtitle_asset.cc
1 /*
2     Copyright (C) 2012-2015 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 "smpte_subtitle_asset.h"
21 #include "smpte_load_font_node.h"
22 #include "font_node.h"
23 #include "exceptions.h"
24 #include "xml.h"
25 #include "AS_DCP.h"
26 #include "KM_util.h"
27 #include <boost/foreach.hpp>
28
29 using std::string;
30 using std::list;
31 using std::stringstream;
32 using std::cout;
33 using boost::shared_ptr;
34 using namespace dcp;
35
36 SMPTESubtitleAsset::SMPTESubtitleAsset (boost::filesystem::path file, bool mxf)
37         : SubtitleAsset (file)
38 {
39         shared_ptr<cxml::Document> xml (new cxml::Document ("SubtitleReel"));
40         
41         if (mxf) {
42                 ASDCP::TimedText::MXFReader reader;
43                 Kumu::Result_t r = reader.OpenRead (file.string().c_str ());
44                 if (ASDCP_FAILURE (r)) {
45                         boost::throw_exception (MXFFileError ("could not open MXF file for reading", file, r));
46                 }
47         
48                 string s;
49                 reader.ReadTimedTextResource (s, 0, 0);
50                 stringstream t;
51                 t << s;
52                 xml->read_stream (t);
53
54                 ASDCP::WriterInfo info;
55                 reader.FillWriterInfo (info);
56                 
57                 char buffer[64];
58                 Kumu::bin2UUIDhex (info.AssetUUID, ASDCP::UUIDlen, buffer, sizeof (buffer));
59                 _id = buffer;
60         } else {
61                 xml->read_file (file);
62                 _id = xml->string_child("Id").substr (9);
63         }
64         
65         _load_font_nodes = type_children<dcp::SMPTELoadFontNode> (xml, "LoadFont");
66
67         int tcr = xml->number_child<int> ("TimeCodeRate");
68
69         shared_ptr<cxml::Node> subtitle_list = xml->optional_node_child ("SubtitleList");
70
71         list<cxml::NodePtr> f = subtitle_list->node_children ("Font");
72         list<shared_ptr<dcp::FontNode> > font_nodes;
73         BOOST_FOREACH (cxml::NodePtr& i, f) {
74                 font_nodes.push_back (shared_ptr<FontNode> (new FontNode (i, tcr)));
75         }
76
77         parse_common (xml, font_nodes);
78 }
79
80 list<shared_ptr<LoadFontNode> >
81 SMPTESubtitleAsset::load_font_nodes () const
82 {
83         list<shared_ptr<LoadFontNode> > lf;
84         copy (_load_font_nodes.begin(), _load_font_nodes.end(), back_inserter (lf));
85         return lf;
86 }
87
88 bool
89 SMPTESubtitleAsset::valid_mxf (boost::filesystem::path file)
90 {
91         ASDCP::TimedText::MXFReader reader;
92         Kumu::Result_t r = reader.OpenRead (file.string().c_str ());
93         return !ASDCP_FAILURE (r);
94 }