c4e389c7cd794c95b7ac23dd09470e01750027e1
[dcpomatic.git] / src / lib / content_factory.cc
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 /** @file  src/lib/content_factory.cc
22  *  @brief Methods to create content objects.
23  */
24
25 #include "ffmpeg_content.h"
26 #include "audio_content.h"
27 #include "image_content.h"
28 #include "atmos_mxf_content.h"
29 #include "string_text_file_content.h"
30 #include "dcp_content.h"
31 #include "dcp_subtitle_content.h"
32 #include "util.h"
33 #include "ffmpeg_audio_stream.h"
34 #include "video_mxf_content.h"
35 #include "film.h"
36 #include "log_entry.h"
37 #include "dcpomatic_log.h"
38 #include "log.h"
39 #include "compose.hpp"
40 #include <libcxml/cxml.h>
41 #include <dcp/smpte_subtitle_asset.h>
42 #include <boost/algorithm/string.hpp>
43
44 #include "i18n.h"
45
46 using std::string;
47 using std::list;
48 using std::shared_ptr;
49 using boost::optional;
50
51 /** Create a Content object from an XML node.
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.
56  */
57 shared_ptr<Content>
58 content_factory (cxml::ConstNodePtr node, int version, list<string>& notes)
59 {
60         string const type = node->string_child ("Type");
61
62         std::shared_ptr<Content> content;
63
64         if (type == "FFmpeg") {
65                 /* SndfileContent is now handled by the FFmpeg code rather than by
66                    separate libsndfile-based code.
67                 */
68                 content.reset (new FFmpegContent (node, version, notes));
69         } else if (type == "Image") {
70                 content.reset (new ImageContent (node, version));
71         } else if (type == "Sndfile") {
72                 /* SndfileContent is now handled by the FFmpeg code rather than by
73                    separate libsndfile-based code.
74                 */
75                 content.reset (new FFmpegContent (node, version, notes));
76
77                 content->audio->set_stream (
78                         AudioStreamPtr (
79                                 new FFmpegAudioStream (
80                                         "Stream", 0,
81                                         node->number_child<int> ("AudioFrameRate"),
82                                         node->number_child<Frame> ("AudioLength"),
83                                         AudioMapping (node->node_child ("AudioMapping"), version)
84                                         )
85                                 )
86                         );
87
88         } else if (type == "SubRip" || type == "TextSubtitle") {
89                 content.reset (new StringTextFileContent (node, version));
90         } else if (type == "DCP") {
91                 content.reset (new DCPContent (node, version));
92         } else if (type == "DCPSubtitle") {
93                 content.reset (new DCPSubtitleContent (node, version));
94         } else if (type == "VideoMXF") {
95                 content.reset (new VideoMXFContent (node, version));
96         } else if (type == "AtmosMXF") {
97                 content.reset (new AtmosMXFContent (node, version));
98         }
99
100         return content;
101 }
102
103 /** Create some Content objects from a file or directory.
104  *  @param path File or directory.
105  *  @return Content objects.
106  */
107 list<shared_ptr<Content> >
108 content_factory (boost::filesystem::path path)
109 {
110         list<shared_ptr<Content> > content;
111
112         if (boost::filesystem::is_directory (path)) {
113
114                 LOG_GENERAL ("Look in directory %1", path);
115
116                 if (boost::filesystem::is_empty (path)) {
117                         return content;
118                 }
119
120                 /* See if this is a set of images or a set of sound files */
121
122                 int image_files = 0;
123                 int sound_files = 0;
124                 int read = 0;
125                 for (boost::filesystem::directory_iterator i(path); i != boost::filesystem::directory_iterator() && read < 10; ++i) {
126
127                         LOG_GENERAL ("Checking file %1", i->path());
128
129                         if (boost::starts_with (i->path().leaf().string(), ".")) {
130                                 /* We ignore hidden files */
131                                 LOG_GENERAL ("Ignored %1 (starts with .)", i->path());
132                                 continue;
133                         }
134
135                         if (!boost::filesystem::is_regular_file(i->path())) {
136                                 /* Ignore things which aren't files (probably directories) */
137                                 LOG_GENERAL ("Ignored %1 (not a regular file)", i->path());
138                                 continue;
139                         }
140
141                         if (valid_image_file (i->path ())) {
142                                 ++image_files;
143                         }
144
145                         if (valid_sound_file (i->path ())) {
146                                 ++sound_files;
147                         }
148
149                         ++read;
150                 }
151
152                 if (image_files > 0 && sound_files == 0)  {
153                         content.push_back (shared_ptr<Content> (new ImageContent(path)));
154                 } else if (image_files == 0 && sound_files > 0) {
155                         for (boost::filesystem::directory_iterator i(path); i != boost::filesystem::directory_iterator(); ++i) {
156                                 content.push_back (shared_ptr<FFmpegContent> (new FFmpegContent(i->path())));
157                         }
158                 }
159
160         } else {
161
162                 shared_ptr<Content> single;
163
164                 string ext = path.extension().string ();
165                 transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
166
167                 if (valid_image_file (path)) {
168                         single.reset (new ImageContent(path));
169                 } else if (ext == ".srt" || ext == ".ssa" || ext == ".ass" || ext == ".stl") {
170                         single.reset (new StringTextFileContent(path));
171                 } else if (ext == ".xml") {
172                         cxml::Document doc;
173                         doc.read_file (path);
174                         if (doc.root_name() == "DCinemaSecurityMessage") {
175                                 throw KDMAsContentError ();
176                         }
177                         single.reset (new DCPSubtitleContent(path));
178                 } else if (ext == ".mxf" && dcp::SMPTESubtitleAsset::valid_mxf (path)) {
179                         single.reset (new DCPSubtitleContent(path));
180                 } else if (ext == ".mxf" && VideoMXFContent::valid_mxf (path)) {
181                         single.reset (new VideoMXFContent(path));
182                 } else if (ext == ".mxf" && AtmosMXFContent::valid_mxf (path)) {
183                         single.reset (new AtmosMXFContent(path));
184                 }
185
186                 if (!single) {
187                         single.reset (new FFmpegContent(path));
188                 }
189
190                 content.push_back (single);
191         }
192
193         return content;
194 }