]> git.sesse.net Git - casparcg/blob - core/thumbnail_generator.cpp
Merge pull request #506 from dimitry-ishenko-casparcg/fixes-flags
[casparcg] / core / thumbnail_generator.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG 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 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG 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 CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #include "StdAfx.h"
23
24 #include "thumbnail_generator.h"
25
26 #include <iostream>
27 #include <iterator>
28 #include <set>
29 #include <future>
30
31 #include <boost/thread.hpp>
32 #include <boost/algorithm/string/predicate.hpp>
33 #include <boost/filesystem.hpp>
34
35 #include <tbb/atomic.h>
36
37 #include <common/diagnostics/graph.h>
38 #include <common/filesystem.h>
39
40 #include "producer/frame_producer.h"
41 #include "producer/cg_proxy.h"
42 #include "consumer/frame_consumer.h"
43 #include "mixer/mixer.h"
44 #include "mixer/image/image_mixer.h"
45 #include "video_format.h"
46 #include "frame/frame.h"
47 #include "frame/draw_frame.h"
48 #include "frame/frame_transform.h"
49 #include "frame/audio_channel_layout.h"
50 #include "producer/media_info/media_info.h"
51 #include "producer/media_info/media_info_repository.h"
52
53 namespace caspar { namespace core {
54
55 struct thumbnail_output
56 {
57         tbb::atomic<int> sleep_millis;
58         std::function<void (const_frame)> on_send;
59
60         thumbnail_output(int sleep_millis)
61         {
62                 this->sleep_millis = sleep_millis;
63         }
64
65         void send(const_frame frame, std::shared_ptr<void> frame_and_ticket)
66         {
67                 int current_sleep = sleep_millis;
68
69                 if (current_sleep > 0)
70                         boost::this_thread::sleep_for(boost::chrono::milliseconds(current_sleep));
71
72                 on_send(std::move(frame));
73                 on_send = nullptr;
74         }
75 };
76
77 struct thumbnail_generator::impl
78 {
79 private:
80         boost::filesystem::path                                                 media_path_;
81         boost::filesystem::path                                                 thumbnails_path_;
82         int                                                                                             width_;
83         int                                                                                             height_;
84         spl::shared_ptr<image_mixer>                                    image_mixer_;
85         spl::shared_ptr<diagnostics::graph>                             graph_;
86         video_format_desc                                                               format_desc_;
87         spl::unique_ptr<thumbnail_output>                               output_;
88         mixer                                                                                   mixer_;
89         thumbnail_creator                                                               thumbnail_creator_;
90         spl::shared_ptr<media_info_repository>                  media_info_repo_;
91         spl::shared_ptr<const frame_producer_registry>  producer_registry_;
92         spl::shared_ptr<const cg_producer_registry>             cg_registry_;
93         bool                                                                                    mipmap_;
94         filesystem_monitor::ptr                                                 monitor_;
95 public:
96         impl(
97                         filesystem_monitor_factory& monitor_factory,
98                         const boost::filesystem::path& media_path,
99                         const boost::filesystem::path& thumbnails_path,
100                         int width,
101                         int height,
102                         const video_format_desc& render_video_mode,
103                         std::unique_ptr<image_mixer> image_mixer,
104                         int generate_delay_millis,
105                         const thumbnail_creator& thumbnail_creator,
106                         spl::shared_ptr<media_info_repository> media_info_repo,
107                         spl::shared_ptr<const frame_producer_registry> producer_registry,
108                         spl::shared_ptr<const cg_producer_registry> cg_registry,
109                         bool mipmap)
110                 : media_path_(media_path)
111                 , thumbnails_path_(thumbnails_path)
112                 , width_(width)
113                 , height_(height)
114                 , image_mixer_(std::move(image_mixer))
115                 , format_desc_(render_video_mode)
116                 , output_(spl::make_unique<thumbnail_output>(generate_delay_millis))
117                 , mixer_(0, graph_, image_mixer_)
118                 , thumbnail_creator_(thumbnail_creator)
119                 , media_info_repo_(std::move(media_info_repo))
120                 , producer_registry_(std::move(producer_registry))
121                 , cg_registry_(std::move(cg_registry))
122                 , mipmap_(mipmap)
123                 , monitor_(monitor_factory.create(
124                                 media_path,
125                                 filesystem_event::ALL,
126                                 true,
127                                 [this] (filesystem_event event, const boost::filesystem::path& file)
128                                 {
129                                         this->on_file_event(event, file);
130                                 },
131                                 [this] (const std::set<boost::filesystem::path>& initial_files)
132                                 {
133                                         this->on_initial_files(initial_files);
134                                 }))
135         {
136                 graph_->set_text(L"thumbnail-channel");
137                 graph_->auto_reset();
138                 diagnostics::register_graph(graph_);
139                 //monitor_->initial_scan_completion().get();
140                 //output_->sleep_millis = 2000;
141         }
142
143         void on_initial_files(const std::set<boost::filesystem::path>& initial_files)
144         {
145                 using namespace boost::filesystem;
146
147                 std::set<std::wstring> relative_without_extensions;
148                 boost::transform(
149                                 initial_files,
150                                 std::insert_iterator<std::set<std::wstring>>(
151                                                 relative_without_extensions,
152                                                 relative_without_extensions.end()),
153                                 [&](const path& p) { return get_relative_without_extension(p, media_path_).wstring(); });
154
155                 for (boost::filesystem::wrecursive_directory_iterator iter(thumbnails_path_); iter != boost::filesystem::wrecursive_directory_iterator(); ++iter)
156                 {
157                         auto& path = iter->path();
158
159                         if (!is_regular_file(path))
160                                 continue;
161
162                         auto relative_without_extension = get_relative_without_extension(path, thumbnails_path_);
163                         bool no_corresponding_media_file = relative_without_extensions.find(relative_without_extension.wstring())
164                                         == relative_without_extensions.end();
165
166                         if (no_corresponding_media_file)
167                                 remove(thumbnails_path_ / (relative_without_extension.wstring() + L".png"));
168                 }
169         }
170
171         void generate(const std::wstring& media_file)
172         {
173                 using namespace boost::filesystem;
174                 auto base_file = media_path_ / media_file;
175                 auto folder = base_file.parent_path();
176
177                 for (boost::filesystem::directory_iterator iter(folder); iter != boost::filesystem::directory_iterator(); ++iter)
178                 {
179                         auto stem = iter->path().stem();
180
181                         if (boost::iequals(stem.wstring(), base_file.filename().wstring()))
182                                 monitor_->reemmit(iter->path());
183                 }
184         }
185
186         void generate_all()
187         {
188                 monitor_->reemmit_all();
189         }
190
191         void on_file_event(filesystem_event event, const boost::filesystem::path& file)
192         {
193                 switch (event)
194                 {
195                 case filesystem_event::CREATED:
196                         if (needs_to_be_generated(file))
197                                 generate_thumbnail(file);
198
199                         break;
200                 case filesystem_event::MODIFIED:
201                         generate_thumbnail(file);
202
203                         break;
204                 case filesystem_event::REMOVED:
205                         auto relative_without_extension = get_relative_without_extension(file, media_path_);
206                         boost::filesystem::remove(thumbnails_path_ / (relative_without_extension.wstring() + L".png"));
207                         media_info_repo_->remove(file.wstring());
208
209                         break;
210                 }
211         }
212
213         bool needs_to_be_generated(const boost::filesystem::path& file)
214         {
215                 using namespace boost::filesystem;
216
217                 auto png_file = thumbnails_path_ / (get_relative_without_extension(file, media_path_).wstring() + L".png");
218
219                 if (!exists(png_file))
220                         return true;
221
222                 std::time_t media_file_mtime;
223
224                 try
225                 {
226                         media_file_mtime = last_write_time(file);
227                 }
228                 catch (...)
229                 {
230                         // Probably removed.
231                         return false;
232                 }
233
234                 try
235                 {
236                         return media_file_mtime != last_write_time(png_file);
237                 }
238                 catch (...)
239                 {
240                         // thumbnail probably removed.
241                         return true;
242                 }
243         }
244
245         void generate_thumbnail(const boost::filesystem::path& file)
246         {
247                 auto media_file_with_extension = get_relative(file, media_path_);
248                 auto media_file = get_relative_without_extension(file, media_path_);
249                 auto png_file = thumbnails_path_ / (media_file.wstring() + L".png");
250                 std::promise<void> thumbnail_ready;
251
252                 {
253                         boost::filesystem::create_directories(png_file.parent_path());
254                         output_->on_send = [this, &png_file] (const_frame frame)
255                         {
256                                 thumbnail_creator_(frame, format_desc_, png_file, width_, height_);
257                         };
258
259                         std::map<int, draw_frame> frames;
260                         auto raw_frame = draw_frame::empty();
261
262                         try
263                         {
264                                 raw_frame = producer_registry_->create_thumbnail(frame_producer_dependencies(image_mixer_, {}, format_desc_, producer_registry_, cg_registry_), media_file.wstring());
265                                 media_info_repo_->remove(file.wstring());
266                                 media_info_repo_->get(file.wstring());
267                         }
268                         catch (const boost::thread_interrupted&)
269                         {
270                                 throw;
271                         }
272                         catch (...)
273                         {
274                                 CASPAR_LOG_CURRENT_EXCEPTION_AT_LEVEL(trace);
275                                 CASPAR_LOG(info) << L"Thumbnail producer failed to create thumbnail for " << media_file_with_extension << L". Turn on log level trace to see more information.";
276                                 return;
277                         }
278
279                         if (raw_frame == draw_frame::empty()
280                                         || raw_frame == draw_frame::late())
281                         {
282                                 CASPAR_LOG(debug) << L"No thumbnail producer for " << media_file_with_extension;
283                                 return;
284                         }
285
286                         auto transformed_frame = draw_frame(raw_frame);
287                         transformed_frame.transform().image_transform.fill_scale[0] = static_cast<double>(width_) / format_desc_.width;
288                         transformed_frame.transform().image_transform.fill_scale[1] = static_cast<double>(height_) / format_desc_.height;
289                         transformed_frame.transform().image_transform.use_mipmap = mipmap_;
290                         frames.insert(std::make_pair(0, transformed_frame));
291
292                         std::shared_ptr<void> ticket(nullptr, [&thumbnail_ready](void*) { thumbnail_ready.set_value(); });
293
294                         auto mixed_frame = mixer_(std::move(frames), format_desc_, audio_channel_layout(2, L"stereo", L""));
295
296                         output_->send(std::move(mixed_frame), ticket);
297                         ticket.reset();
298                 }
299                 thumbnail_ready.get_future().get();
300
301                 if (boost::filesystem::exists(png_file))
302                 {
303                         // Adjust timestamp to match source file.
304                         try
305                         {
306                                 boost::filesystem::last_write_time(png_file, boost::filesystem::last_write_time(file));
307                                 CASPAR_LOG(info) << L"Generated thumbnail for " << media_file_with_extension;
308                         }
309                         catch (...)
310                         {
311                                 // One of the files was removed before the call to last_write_time.
312                         }
313                 }
314                 else
315                         CASPAR_LOG(debug) << L"No thumbnail generated for " << media_file_with_extension;
316         }
317 };
318
319 thumbnail_generator::thumbnail_generator(
320                 filesystem_monitor_factory& monitor_factory,
321                 const boost::filesystem::path& media_path,
322                 const boost::filesystem::path& thumbnails_path,
323                 int width,
324                 int height,
325                 const video_format_desc& render_video_mode,
326                 std::unique_ptr<image_mixer> image_mixer,
327                 int generate_delay_millis,
328                 const thumbnail_creator& thumbnail_creator,
329                 spl::shared_ptr<media_info_repository> media_info_repo,
330                 spl::shared_ptr<const frame_producer_registry> producer_registry,
331                 spl::shared_ptr<const cg_producer_registry> cg_registry,
332                 bool mipmap)
333                 : impl_(new impl(
334                                 monitor_factory,
335                                 media_path,
336                                 thumbnails_path,
337                                 width, height,
338                                 render_video_mode,
339                                 std::move(image_mixer),
340                                 generate_delay_millis,
341                                 thumbnail_creator,
342                                 media_info_repo,
343                                 producer_registry,
344                                 cg_registry,
345                                 mipmap))
346 {
347 }
348
349 thumbnail_generator::~thumbnail_generator()
350 {
351 }
352
353 void thumbnail_generator::generate(const std::wstring& media_file)
354 {
355         impl_->generate(media_file);
356 }
357
358 void thumbnail_generator::generate_all()
359 {
360         impl_->generate_all();
361 }
362
363 }}