]> git.sesse.net Git - casparcg/blob - modules/image/consumer/image_consumer.cpp
Merged asynchronous invocation of consumers from 2.0
[casparcg] / modules / image / consumer / image_consumer.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: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "image_consumer.h"
23
24 #include <common/except.h>
25 #include <common/env.h>
26 #include <common/log.h>
27 #include <common/utf.h>
28 #include <common/array.h>
29 #include <common/future.h>
30
31 #include <core/consumer/frame_consumer.h>
32 #include <core/video_format.h>
33 #include <core/frame/frame.h>
34
35 #include <boost/date_time/posix_time/posix_time.hpp>
36 #include <boost/thread.hpp>
37
38 #include <tbb/concurrent_queue.h>
39
40 #include <asmlib.h>
41
42 #include <FreeImage.h>
43
44 #include <vector>
45
46 namespace caspar { namespace image {
47         
48 struct image_consumer : public core::frame_consumer
49 {
50 public:
51
52         // frame_consumer
53
54         void initialize(const core::video_format_desc&, int) override
55         {
56         }
57         
58         boost::unique_future<bool> send(core::const_frame frame) override
59         {                               
60                 boost::thread async([frame]
61                 {
62                         try
63                         {
64                                 auto filename = u8(env::data_folder()) +  boost::posix_time::to_iso_string(boost::posix_time::second_clock::local_time()) + ".png";
65
66                                 auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_Allocate(static_cast<int>(frame.width()), static_cast<int>(frame.height()), 32), FreeImage_Unload);
67                                 A_memcpy(FreeImage_GetBits(bitmap.get()), frame.image_data().begin(), frame.image_data().size());
68                                 FreeImage_FlipVertical(bitmap.get());
69                                 FreeImage_Save(FIF_PNG, bitmap.get(), filename.c_str(), 0);
70                         }
71                         catch(...)
72                         {
73                                 CASPAR_LOG_CURRENT_EXCEPTION();
74                         }
75                 });
76                 async.detach();
77
78                 return wrap_as_future(false);
79         }
80
81         std::wstring print() const override
82         {
83                 return L"image[]";
84         }
85         
86         std::wstring name() const override
87         {
88                 return L"image";
89         }
90
91         boost::property_tree::wptree info() const override
92         {
93                 boost::property_tree::wptree info;
94                 info.add(L"type", L"image");
95                 return info;
96         }
97
98         int buffer_depth() const override
99         {
100                 return 0;
101         }
102
103         int index() const override
104         {
105                 return 100;
106         }
107
108         void subscribe(const monitor::observable::observer_ptr& o) override
109         {
110         }
111
112         void unsubscribe(const monitor::observable::observer_ptr& o) override
113         {
114         }       
115 };
116
117 spl::shared_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)
118 {
119         if(params.size() < 1 || params[0] != L"IMAGE")
120                 return core::frame_consumer::empty();
121
122         return spl::make_shared<image_consumer>();
123 }
124
125 }}