]> git.sesse.net Git - casparcg/blob - core/thumbnail_generator.cpp
* Merged layer_producer and channel_producer from 2.0 to the reroute module (replacin...
[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
39 #include "producer/frame_producer.h"
40 #include "consumer/frame_consumer.h"
41 #include "mixer/mixer.h"
42 #include "mixer/image/image_mixer.h"
43 #include "video_format.h"
44 #include "frame/frame.h"
45 #include "frame/draw_frame.h"
46 #include "frame/frame_transform.h"
47 #include "producer/media_info/media_info.h"
48 #include "producer/media_info/media_info_repository.h"
49
50 namespace caspar { namespace core {
51
52 std::wstring get_relative_without_extension(
53                 const boost::filesystem::path& file,
54                 const boost::filesystem::path& relative_to)
55 {
56         auto result = file.stem();
57
58         boost::filesystem::path current_path = file;
59
60         while (true)
61         {
62                 current_path = current_path.parent_path();
63
64                 if (boost::filesystem::equivalent(current_path, relative_to))
65                         break;
66
67                 if (current_path.empty())
68                         throw std::runtime_error("File not relative to folder");
69
70                 result = current_path.filename() / result;
71         }
72
73         return result.wstring();
74 }
75
76 struct thumbnail_output
77 {
78         tbb::atomic<int> sleep_millis;
79         std::function<void (const_frame)> on_send;
80
81         thumbnail_output(int sleep_millis)
82         {
83                 this->sleep_millis = sleep_millis;
84         }
85
86         void send(const_frame frame, std::shared_ptr<void> frame_and_ticket)
87         {
88                 int current_sleep = sleep_millis;
89
90                 if (current_sleep > 0)
91                         boost::this_thread::sleep_for(boost::chrono::milliseconds(current_sleep));
92
93                 on_send(std::move(frame));
94                 on_send = nullptr;
95         }
96 };
97
98 struct thumbnail_generator::impl
99 {
100 private:
101         boost::filesystem::path                                 media_path_;
102         boost::filesystem::path                                 thumbnails_path_;
103         int                                                                             width_;
104         int                                                                             height_;
105         spl::shared_ptr<image_mixer>                    image_mixer_;
106         spl::shared_ptr<diagnostics::graph>             graph_;
107         video_format_desc                                               format_desc_;
108         spl::unique_ptr<thumbnail_output>               output_;
109         mixer                                                                   mixer_;
110         thumbnail_creator                                               thumbnail_creator_;
111         spl::shared_ptr<media_info_repository>  media_info_repo_;
112         bool                                                                    mipmap_;
113         filesystem_monitor::ptr                                 monitor_;
114 public:
115         impl(
116                         filesystem_monitor_factory& monitor_factory,
117                         const boost::filesystem::path& media_path,
118                         const boost::filesystem::path& thumbnails_path,
119                         int width,
120                         int height,
121                         const video_format_desc& render_video_mode,
122                         std::unique_ptr<image_mixer> image_mixer,
123                         int generate_delay_millis,
124                         const thumbnail_creator& thumbnail_creator,
125                         spl::shared_ptr<media_info_repository> media_info_repo,
126                         bool mipmap)
127                 : media_path_(media_path)
128                 , thumbnails_path_(thumbnails_path)
129                 , width_(width)
130                 , height_(height)
131                 , image_mixer_(std::move(image_mixer))
132                 , format_desc_(render_video_mode)
133                 , output_(spl::make_unique<thumbnail_output>(generate_delay_millis))
134                 , mixer_(graph_, image_mixer_)
135                 , thumbnail_creator_(thumbnail_creator)
136                 , media_info_repo_(std::move(media_info_repo))
137                 , monitor_(monitor_factory.create(
138                                 media_path,
139                                 filesystem_event::ALL,
140                                 true,
141                                 [this] (filesystem_event event, const boost::filesystem::path& file)
142                                 {
143                                         this->on_file_event(event, file);
144                                 },
145                                 [this] (const std::set<boost::filesystem::path>& initial_files)
146                                 {
147                                         this->on_initial_files(initial_files);
148                                 }))
149         {
150                 graph_->set_text(L"thumbnail-channel");
151                 graph_->auto_reset();
152                 diagnostics::register_graph(graph_);
153                 //monitor_->initial_scan_completion().get();
154                 //output_->sleep_millis = 2000;
155         }
156
157         void on_initial_files(const std::set<boost::filesystem::path>& initial_files)
158         {
159                 using namespace boost::filesystem;
160
161                 std::set<std::wstring> relative_without_extensions;
162                 boost::transform(
163                                 initial_files,
164                                 std::insert_iterator<std::set<std::wstring>>(
165                                                 relative_without_extensions,
166                                                 relative_without_extensions.end()),
167                                 boost::bind(&get_relative_without_extension, _1, media_path_));
168
169                 for (boost::filesystem::wrecursive_directory_iterator iter(thumbnails_path_); iter != boost::filesystem::wrecursive_directory_iterator(); ++iter)
170                 {
171                         auto& path = iter->path();
172
173                         if (!is_regular_file(path))
174                                 continue;
175
176                         auto relative_without_extension = get_relative_without_extension(path, thumbnails_path_);
177                         bool no_corresponding_media_file = relative_without_extensions.find(relative_without_extension) 
178                                         == relative_without_extensions.end();
179
180                         if (no_corresponding_media_file)
181                                 remove(thumbnails_path_ / (relative_without_extension + L".png"));
182                 }
183         }
184
185         void generate(const std::wstring& media_file)
186         {
187                 using namespace boost::filesystem;
188                 auto base_file = media_path_ / media_file;
189                 auto folder = base_file.parent_path();
190
191                 for (boost::filesystem::directory_iterator iter(folder); iter != boost::filesystem::directory_iterator(); ++iter)
192                 {
193                         auto stem = iter->path().stem();
194
195                         if (boost::iequals(stem.wstring(), base_file.filename().wstring()))
196                                 monitor_->reemmit(iter->path());
197                 }
198         }
199
200         void generate_all()
201         {
202                 monitor_->reemmit_all();
203         }
204
205         void on_file_event(filesystem_event event, const boost::filesystem::path& file)
206         {
207                 switch (event)
208                 {
209                 case filesystem_event::CREATED:
210                         if (needs_to_be_generated(file))
211                                 generate_thumbnail(file);
212
213                         break;
214                 case filesystem_event::MODIFIED:
215                         generate_thumbnail(file);
216
217                         break;
218                 case filesystem_event::REMOVED:
219                         auto relative_without_extension = get_relative_without_extension(file, media_path_);
220                         boost::filesystem::remove(thumbnails_path_ / (relative_without_extension + L".png"));
221                         media_info_repo_->remove(file.wstring());
222
223                         break;
224                 }
225         }
226
227         bool needs_to_be_generated(const boost::filesystem::path& file)
228         {
229                 using namespace boost::filesystem;
230
231                 auto png_file = thumbnails_path_ / (get_relative_without_extension(file, media_path_) + L".png");
232
233                 if (!exists(png_file))
234                         return true;
235
236                 std::time_t media_file_mtime;
237
238                 try
239                 {
240                         media_file_mtime = last_write_time(file);
241                 }
242                 catch (...)
243                 {
244                         // Probably removed.
245                         return false;
246                 }
247
248                 try
249                 {
250                         return media_file_mtime != last_write_time(png_file);
251                 }
252                 catch (...)
253                 {
254                         // thumbnail probably removed.
255                         return true;
256                 }
257         }
258
259         void generate_thumbnail(const boost::filesystem::path& file)
260         {
261                 auto media_file = get_relative_without_extension(file, media_path_);
262                 auto png_file = thumbnails_path_ / (media_file + L".png");
263                 std::promise<void> thumbnail_ready;
264
265                 {
266                         auto producer = frame_producer::empty();
267
268                         try
269                         {
270                                 producer = create_thumbnail_producer(
271                                                 frame_producer_dependencies(image_mixer_, { }, format_desc_),
272                                                 media_file);
273                         }
274                         catch (const boost::thread_interrupted&)
275                         {
276                                 throw;
277                         }
278                         catch (...)
279                         {
280                                 CASPAR_LOG(debug) << L"Thumbnail producer failed to initialize for " << media_file;
281                                 return;
282                         }
283
284                         if (producer == frame_producer::empty())
285                         {
286                                 CASPAR_LOG(trace) << L"No appropriate thumbnail producer found for " << media_file;
287                                 return;
288                         }
289
290                         boost::filesystem::create_directories(png_file.parent_path());
291                         output_->on_send = [this, &png_file] (const_frame frame)
292                         {
293                                 thumbnail_creator_(frame, format_desc_, png_file, width_, height_);
294                         };
295
296                         std::map<int, draw_frame> frames;
297                         auto raw_frame = draw_frame::empty();
298
299                         try
300                         {
301                                 raw_frame = producer->create_thumbnail_frame();
302                                 media_info_repo_->remove(file.wstring());
303                                 media_info_repo_->get(file.wstring());
304                         }
305                         catch (const boost::thread_interrupted&)
306                         {
307                                 throw;
308                         }
309                         catch (...)
310                         {
311                                 CASPAR_LOG(debug) << L"Thumbnail producer failed to create thumbnail for " << media_file;
312                                 return;
313                         }
314
315                         if (raw_frame == draw_frame::empty()
316                                         || raw_frame == draw_frame::late())
317                         {
318                                 CASPAR_LOG(debug) << L"No thumbnail generated for " << media_file;
319                                 return;
320                         }
321
322                         auto transformed_frame = draw_frame(raw_frame);
323                         transformed_frame.transform().image_transform.fill_scale[0] = static_cast<double>(width_) / format_desc_.width;
324                         transformed_frame.transform().image_transform.fill_scale[1] = static_cast<double>(height_) / format_desc_.height;
325                         transformed_frame.transform().image_transform.use_mipmap = mipmap_;
326                         frames.insert(std::make_pair(0, transformed_frame));
327
328                         std::shared_ptr<void> ticket(nullptr, [&thumbnail_ready](void*) { thumbnail_ready.set_value(); });
329
330                         auto mixed_frame = mixer_(std::move(frames), format_desc_);
331
332                         output_->send(std::move(mixed_frame), ticket);
333                         ticket.reset();
334                 }
335                 thumbnail_ready.get_future().get();
336
337                 if (boost::filesystem::exists(png_file))
338                 {
339                         // Adjust timestamp to match source file.
340                         try
341                         {
342                                 boost::filesystem::last_write_time(png_file, boost::filesystem::last_write_time(file));
343                                 CASPAR_LOG(debug) << L"Generated thumbnail for " << media_file;
344                         }
345                         catch (...)
346                         {
347                                 // One of the files was removed before the call to last_write_time.
348                         }
349                 }
350                 else
351                         CASPAR_LOG(debug) << L"No thumbnail generated for " << media_file;
352         }
353 };
354
355 thumbnail_generator::thumbnail_generator(
356                 filesystem_monitor_factory& monitor_factory,
357                 const boost::filesystem::path& media_path,
358                 const boost::filesystem::path& thumbnails_path,
359                 int width,
360                 int height,
361                 const video_format_desc& render_video_mode,
362                 std::unique_ptr<image_mixer> image_mixer,
363                 int generate_delay_millis,
364                 const thumbnail_creator& thumbnail_creator,
365                 spl::shared_ptr<media_info_repository> media_info_repo,
366                 bool mipmap)
367                 : impl_(new impl(
368                                 monitor_factory,
369                                 media_path,
370                                 thumbnails_path,
371                                 width, height,
372                                 render_video_mode,
373                                 std::move(image_mixer),
374                                 generate_delay_millis,
375                                 thumbnail_creator,
376                                 media_info_repo,
377                                 mipmap))
378 {
379 }
380
381 thumbnail_generator::~thumbnail_generator()
382 {
383 }
384
385 void thumbnail_generator::generate(const std::wstring& media_file)
386 {
387         impl_->generate(media_file);
388 }
389
390 void thumbnail_generator::generate_all()
391 {
392         impl_->generate_all();
393 }
394
395 }}