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/>.
21 /** @file src/lib/content_factory.cc
22 * @brief Methods to create content objects.
25 #include "ffmpeg_content.h"
26 #include "audio_content.h"
27 #include "image_content.h"
28 #include "atmos_mxf_content.h"
29 #include "text_subtitle_content.h"
30 #include "dcp_content.h"
31 #include "dcp_subtitle_content.h"
33 #include "ffmpeg_audio_stream.h"
34 #include "video_mxf_content.h"
36 #include "log_entry.h"
38 #include "compose.hpp"
39 #include <libcxml/cxml.h>
40 #include <dcp/smpte_subtitle_asset.h>
41 #include <boost/algorithm/string.hpp>
45 using boost::shared_ptr;
46 using boost::optional;
48 #define LOG_GENERAL(...) film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
50 /** Create a Content object from an XML node.
51 * @param film Film that the content will be in.
52 * @param node XML description.
53 * @param version XML state version.
54 * @param notes A list to which is added descriptions of any non-critial warnings / messages.
55 * @return Content object, or 0 if no content was recognised in the XML.
58 content_factory (shared_ptr<const Film> film, cxml::NodePtr node, int version, list<string>& notes)
60 string const type = node->string_child ("Type");
62 boost::shared_ptr<Content> content;
64 if (type == "FFmpeg") {
65 /* SndfileContent is now handled by the FFmpeg code rather than by
66 separate libsndfile-based code.
68 content.reset (new FFmpegContent (film, node, version, notes));
69 } else if (type == "Image") {
70 content.reset (new ImageContent (film, node, version));
71 } else if (type == "Sndfile") {
72 /* SndfileContent is now handled by the FFmpeg code rather than by
73 separate libsndfile-based code.
75 content.reset (new FFmpegContent (film, node, version, notes));
77 content->audio->set_stream (
79 new FFmpegAudioStream (
81 node->number_child<int> ("AudioFrameRate"),
82 node->number_child<Frame> ("AudioLength"),
83 AudioMapping (node->node_child ("AudioMapping"), version)
88 } else if (type == "SubRip" || type == "TextSubtitle") {
89 content.reset (new TextSubtitleContent (film, node, version));
90 } else if (type == "DCP") {
91 content.reset (new DCPContent (film, node, version));
92 } else if (type == "DCPSubtitle") {
93 content.reset (new DCPSubtitleContent (film, node, version));
94 } else if (type == "VideoMXF") {
95 content.reset (new VideoMXFContent (film, node, version));
96 } else if (type == "AtmosMXF") {
97 content.reset (new AtmosMXFContent (film, node, version));
103 /** Create a Content object from a file or directory.
104 * @param film Film that the content will be in.
105 * @param path File or directory.
106 * @return Content object.
109 content_factory (shared_ptr<const Film> film, boost::filesystem::path path)
111 shared_ptr<Content> content;
113 if (boost::filesystem::is_directory (path)) {
115 LOG_GENERAL ("Look in directory %1", path);
117 if (boost::filesystem::is_empty (path)) {
118 return shared_ptr<Content> ();
121 /* Guess if this is a DCP or a set of images: read the first ten filenames and if they
122 are all valid image files we assume it is a set of images.
127 for (boost::filesystem::directory_iterator i(path); i != boost::filesystem::directory_iterator() && read < 10; ++i) {
129 LOG_GENERAL ("Checking file %1", i->path());
131 if (boost::starts_with (i->path().leaf().string(), ".")) {
132 /* We ignore hidden files */
133 LOG_GENERAL ("Ignored %1 (starts with .)", i->path());
137 if (!boost::filesystem::is_regular_file(i->path())) {
138 /* Ignore things which aren't files (probably directories) */
139 LOG_GENERAL ("Ignored %1 (not a regular file)", i->path());
143 if (!valid_image_file (i->path())) {
144 /* We have a normal file which isn't an image; assume we are looking
147 LOG_GENERAL ("It's a DCP because of %1", i->path());
155 content.reset (new DCPContent (film, path));
157 content.reset (new ImageContent (film, path));
162 string ext = path.extension().string ();
163 transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
165 if (valid_image_file (path)) {
166 content.reset (new ImageContent (film, path));
167 } else if (ext == ".srt" || ext == ".ssa" || ext == ".ass") {
168 content.reset (new TextSubtitleContent (film, path));
169 } else if (ext == ".xml") {
170 content.reset (new DCPSubtitleContent (film, path));
171 } else if (ext == ".mxf" && dcp::SMPTESubtitleAsset::valid_mxf (path)) {
172 content.reset (new DCPSubtitleContent (film, path));
173 } else if (ext == ".mxf" && VideoMXFContent::valid_mxf (path)) {
174 content.reset (new VideoMXFContent (film, path));
175 } else if (ext == ".mxf" && AtmosMXFContent::valid_mxf (path)) {
176 content.reset (new AtmosMXFContent (film, path));
180 content.reset (new FFmpegContent (film, path));