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