2 Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic 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.
11 DCP-o-matic 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.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 /** @file src/lib/content_factory.cc
23 * @brief Methods to create content objects.
27 #include "ffmpeg_content.h"
28 #include "audio_content.h"
29 #include "image_content.h"
30 #include "atmos_mxf_content.h"
31 #include "string_text_file_content.h"
32 #include "dcp_content.h"
33 #include "dcp_subtitle_content.h"
35 #include "ffmpeg_audio_stream.h"
36 #include "video_mxf_content.h"
38 #include "log_entry.h"
39 #include "dcpomatic_log.h"
41 #include "compose.hpp"
42 #include <libcxml/cxml.h>
43 #include <dcp/filesystem.h>
44 #include <dcp/smpte_subtitle_asset.h>
45 #include <boost/algorithm/string.hpp>
51 using std::make_shared;
52 using std::shared_ptr;
57 /** Create a Content object from an XML node.
58 * @param node XML description.
59 * @param version XML state version.
60 * @param notes A list to which is added descriptions of any non-critial warnings / messages.
61 * @return Content object, or 0 if no content was recognised in the XML.
64 content_factory (cxml::ConstNodePtr node, int version, list<string>& notes)
66 auto const type = node->string_child ("Type");
68 std::shared_ptr<Content> content;
70 if (type == "FFmpeg") {
71 content = make_shared<FFmpegContent>(node, version, notes);
72 } else if (type == "Image") {
73 content = make_shared<ImageContent>(node, version);
74 } else if (type == "Sndfile") {
75 /* SndfileContent is now handled by the FFmpeg code rather than by
76 separate libsndfile-based code.
78 content = make_shared<FFmpegContent>(node, version, notes);
80 content->audio->set_stream (
81 make_shared<FFmpegAudioStream>(
83 node->number_child<int> ("AudioFrameRate"),
84 node->number_child<Frame> ("AudioLength"),
85 AudioMapping(node->node_child("AudioMapping"), version),
90 } else if (type == "SubRip" || type == "TextSubtitle") {
91 content = make_shared<StringTextFileContent>(node, version, notes);
92 } else if (type == "DCP") {
93 content = make_shared<DCPContent>(node, version);
94 } else if (type == "DCPSubtitle") {
95 content = make_shared<DCPSubtitleContent>(node, version);
96 } else if (type == "VideoMXF") {
97 content = make_shared<VideoMXFContent>(node, version);
98 } else if (type == "AtmosMXF") {
99 content = make_shared<AtmosMXFContent>(node, version);
106 /** Create some Content objects from a file or directory.
107 * @param path File or directory.
108 * @return Content objects.
110 vector<shared_ptr<Content>>
111 content_factory (boost::filesystem::path path)
113 vector<shared_ptr<Content>> content;
115 if (dcp::filesystem::is_directory(path)) {
117 LOG_GENERAL ("Look in directory %1", path);
119 if (dcp::filesystem::is_empty(path)) {
123 /* See if this is a set of images or a set of sound files */
128 for (dcp::filesystem::directory_iterator i(path); i != dcp::filesystem::directory_iterator() && read < 10; ++i) {
130 LOG_GENERAL ("Checking file %1", i->path());
132 if (boost::starts_with(i->path().filename().string(), ".")) {
133 /* We ignore hidden files */
134 LOG_GENERAL ("Ignored %1 (starts with .)", i->path());
138 if (!dcp::filesystem::is_regular_file(i->path())) {
139 /* Ignore things which aren't files (probably directories) */
140 LOG_GENERAL ("Ignored %1 (not a regular file)", i->path());
144 if (valid_image_file(i->path())) {
148 if (valid_sound_file(i->path())) {
155 if (image_files > 0 && sound_files == 0) {
156 content.push_back (make_shared<ImageContent>(path));
157 } else if (image_files == 0 && sound_files > 0) {
158 for (auto i: dcp::filesystem::directory_iterator(path)) {
159 content.push_back (make_shared<FFmpegContent>(i.path()));
165 shared_ptr<Content> single;
167 auto ext = path.extension().string();
168 transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
170 if (valid_image_file (path)) {
171 single = make_shared<ImageContent>(path);
172 } else if (ext == ".srt" || ext == ".ssa" || ext == ".ass" || ext == ".stl" || ext == ".vtt") {
173 single = make_shared<StringTextFileContent>(path);
174 } else if (ext == ".xml") {
176 doc.read_file(dcp::filesystem::fix_long_path(path));
177 if (doc.root_name() == "DCinemaSecurityMessage") {
178 throw KDMAsContentError ();
180 single = make_shared<DCPSubtitleContent>(path);
181 } else if (ext == ".mxf" && dcp::SMPTESubtitleAsset::valid_mxf(path)) {
182 single = make_shared<DCPSubtitleContent>(path);
183 } else if (ext == ".mxf" && VideoMXFContent::valid_mxf(path)) {
184 single = make_shared<VideoMXFContent>(path);
185 } else if (ext == ".mxf" && AtmosMXFContent::valid_mxf(path)) {
186 single = make_shared<AtmosMXFContent>(path);
190 single = make_shared<FFmpegContent>(path);
193 content.push_back (single);