]> git.sesse.net Git - casparcg/blob - core/producer/frame_producer.cpp
2.1.0: Refactored "last_frame".
[casparcg] / core / producer / frame_producer.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../StdAfx.h"\r
23 \r
24 #include "frame_producer.h"\r
25 \r
26 #include "../frame/draw_frame.h"\r
27 #include "../frame/frame_transform.h"\r
28 \r
29 #include "color/color_producer.h"\r
30 #include "separated/separated_producer.h"\r
31 \r
32 #include <common/assert.h>\r
33 #include <common/except.h>\r
34 #include <common/concurrency/executor.h>\r
35 #include <common/concurrency/async.h>\r
36 #include <common/spl/memory.h>\r
37 \r
38 namespace caspar { namespace core {\r
39         \r
40 std::vector<const producer_factory_t> g_factories;\r
41 \r
42 void register_producer_factory(const producer_factory_t& factory)\r
43 {\r
44         g_factories.push_back(factory);\r
45 }\r
46 \r
47 spl::shared_ptr<class draw_frame> frame_producer::last_frame() const\r
48 {\r
49         BOOST_THROW_EXCEPTION(not_implemented());\r
50 }\r
51 \r
52 boost::unique_future<std::wstring> frame_producer::call(const std::wstring&) \r
53 {\r
54         BOOST_THROW_EXCEPTION(not_supported());\r
55 }\r
56 \r
57 const spl::shared_ptr<frame_producer>& frame_producer::empty() // nothrow\r
58 {\r
59 \r
60 struct empty_frame_producer : public frame_producer\r
61 {\r
62         virtual spl::shared_ptr<draw_frame> receive(int){return draw_frame::empty();}\r
63         virtual spl::shared_ptr<draw_frame> last_frame() const{return draw_frame::empty();}\r
64         virtual void set_frame_factory(const spl::shared_ptr<frame_factory>&){}\r
65         virtual uint32_t nb_frames() const {return 0;}\r
66         virtual std::wstring print() const { return L"empty";}\r
67         virtual void subscribe(const monitor::observable::observer_ptr& o){}\r
68         virtual void unsubscribe(const monitor::observable::observer_ptr& o){}  \r
69         virtual std::wstring name() const {return L"empty";}\r
70         \r
71         virtual boost::property_tree::wptree info() const override\r
72         {\r
73                 boost::property_tree::wptree info;\r
74                 info.add(L"type", L"empty-producer");\r
75                 return info;\r
76         }\r
77 };\r
78 \r
79         static spl::shared_ptr<frame_producer> producer = spl::make_shared<empty_frame_producer>();\r
80         return producer;\r
81 }       \r
82 \r
83 class producer_proxy : public frame_producer\r
84 {       \r
85         std::shared_ptr<frame_producer> producer_;\r
86         spl::shared_ptr<draw_frame>             last_frame_;\r
87 public:\r
88         producer_proxy(spl::shared_ptr<frame_producer>&& producer) \r
89                 : producer_(std::move(producer))\r
90         {\r
91                 CASPAR_LOG(info) << producer_->print() << L" Initialized.";\r
92         }\r
93 \r
94         virtual ~producer_proxy()\r
95         {               \r
96                 static tbb::atomic<int> counter = tbb::atomic<int>();\r
97                 \r
98                 ++counter;\r
99                 CASPAR_VERIFY(counter < 32);\r
100                 \r
101                 auto producer = new spl::shared_ptr<frame_producer>(std::move(producer_));\r
102                 async([=]\r
103                 {\r
104                         std::unique_ptr<spl::shared_ptr<frame_producer>> pointer_guard(producer);\r
105                         auto str = (*producer)->print();\r
106                         try\r
107                         {\r
108                                 if(!producer->unique())\r
109                                         CASPAR_LOG(trace) << str << L" Not destroyed on asynchronous destruction thread: " << producer->use_count();\r
110                                 else\r
111                                         CASPAR_LOG(trace) << str << L" Destroying on asynchronous destruction thread.";\r
112                         }\r
113                         catch(...){}\r
114                         \r
115                         CASPAR_LOG(trace) << str << L" Uninitializing.";\r
116                         pointer_guard.reset();\r
117                         CASPAR_LOG(info) << str << L" Uninitialized.";\r
118 \r
119                         --counter;\r
120                 }); \r
121         }\r
122         \r
123         virtual spl::shared_ptr<draw_frame>     receive(int hints) override                                                                                                             \r
124         {\r
125                 auto frame = producer_->receive(hints);\r
126                 \r
127                 if(frame != draw_frame::late())\r
128                         last_frame_ = frame;\r
129 \r
130                 return std::move(frame);\r
131         }\r
132         virtual spl::shared_ptr<draw_frame>     last_frame() const override                                                                                                             \r
133         {\r
134                 return draw_frame::still(last_frame_);\r
135         }\r
136 \r
137         virtual std::wstring                                                                            print() const override                                                                                                                  {return producer_->print();}\r
138         virtual std::wstring                                                                            name() const override                                                                                                                   {return producer_->name();}\r
139         virtual boost::property_tree::wptree                                            info() const override                                                                                                                   {return producer_->info();}\r
140         virtual boost::unique_future<std::wstring>                                      call(const std::wstring& str) override                                                                                  {return producer_->call(str);}\r
141         virtual void                                                                                            leading_producer(const spl::shared_ptr<frame_producer>& producer) override              {return producer_->leading_producer(producer);}\r
142         virtual uint32_t                                                                                        nb_frames() const override                                                                                                              {return producer_->nb_frames();}\r
143         virtual void subscribe(const monitor::observable::observer_ptr& o)                                                                                                                                                      {return producer_->subscribe(o);}\r
144         virtual void unsubscribe(const monitor::observable::observer_ptr& o)                                                                                                                                            {return producer_->unsubscribe(o);}\r
145 };\r
146 \r
147 spl::shared_ptr<core::frame_producer> wrap_producer(spl::shared_ptr<core::frame_producer> producer)\r
148 {\r
149         return spl::make_shared<producer_proxy>(std::move(producer));\r
150 }\r
151 \r
152 spl::shared_ptr<core::frame_producer> do_create_producer(const spl::shared_ptr<frame_factory>& my_frame_factory, const std::vector<std::wstring>& params)\r
153 {\r
154         if(params.empty())\r
155                 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));\r
156         \r
157         auto producer = frame_producer::empty();\r
158         std::any_of(g_factories.begin(), g_factories.end(), [&](const producer_factory_t& factory) -> bool\r
159                 {\r
160                         try\r
161                         {\r
162                                 producer = factory(my_frame_factory, params);\r
163                         }\r
164                         catch(...)\r
165                         {\r
166                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
167                         }\r
168                         return producer != frame_producer::empty();\r
169                 });\r
170 \r
171         if(producer == frame_producer::empty())\r
172                 producer = create_color_producer(my_frame_factory, params);\r
173 \r
174         if(producer == frame_producer::empty())\r
175                 return producer;\r
176                 \r
177         return producer;\r
178 }\r
179 \r
180 spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<frame_factory>& my_frame_factory, const std::vector<std::wstring>& params)\r
181 {       \r
182         auto producer = do_create_producer(my_frame_factory, params);\r
183         auto key_producer = frame_producer::empty();\r
184         \r
185         try // to find a key file.\r
186         {\r
187                 auto params_copy = params;\r
188                 if(params_copy.size() > 0)\r
189                 {\r
190                         params_copy[0] += L"_A";\r
191                         key_producer = do_create_producer(my_frame_factory, params_copy);                       \r
192                         if(key_producer == frame_producer::empty())\r
193                         {\r
194                                 params_copy[0] += L"LPHA";\r
195                                 key_producer = do_create_producer(my_frame_factory, params_copy);       \r
196                         }\r
197                 }\r
198         }\r
199         catch(...){}\r
200 \r
201         if(producer != frame_producer::empty() && key_producer != frame_producer::empty())\r
202                 return create_separated_producer(producer, key_producer);\r
203         \r
204         if(producer == frame_producer::empty())\r
205         {\r
206                 std::wstring str;\r
207                 BOOST_FOREACH(auto& param, params)\r
208                         str += param + L" ";\r
209                 BOOST_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax.") << arg_value_info(u8(str)));\r
210         }\r
211 \r
212         return producer;\r
213 }\r
214 \r
215 \r
216 spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<frame_factory>& factory, const std::wstring& params)\r
217 {\r
218         std::wstringstream iss(params);\r
219         std::vector<std::wstring> tokens;\r
220         typedef std::istream_iterator<std::wstring, wchar_t, std::char_traits<wchar_t> > iterator;\r
221         std::copy(iterator(iss),  iterator(), std::back_inserter(tokens));\r
222         return create_producer(factory, tokens);\r
223 }\r
224 \r
225 }}