]> git.sesse.net Git - casparcg/blob - core/producer/frame_producer.cpp
2.1.0: -frame_consumer: Asynchronous destruction.
[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 boost::unique_future<std::wstring> frame_producer::call(const std::wstring&) \r
43 {\r
44         BOOST_THROW_EXCEPTION(not_supported());\r
45 }\r
46 \r
47 struct empty_frame_producer : public frame_producer\r
48 {\r
49         virtual spl::shared_ptr<draw_frame> receive(int){return draw_frame::empty();}\r
50         virtual spl::shared_ptr<draw_frame> last_frame() const{return draw_frame::empty();}\r
51         virtual void set_frame_factory(const spl::shared_ptr<frame_factory>&){}\r
52         virtual uint32_t nb_frames() const {return 0;}\r
53         virtual std::wstring print() const { return L"empty";}\r
54         \r
55         virtual boost::property_tree::wptree info() const override\r
56         {\r
57                 boost::property_tree::wptree info;\r
58                 info.add(L"type", L"empty-producer");\r
59                 return info;\r
60         }\r
61 };\r
62 \r
63 const spl::shared_ptr<frame_producer>& frame_producer::empty() // nothrow\r
64 {\r
65         static spl::shared_ptr<frame_producer> producer = spl::make_shared<empty_frame_producer>();\r
66         return producer;\r
67 }       \r
68 \r
69 spl::shared_ptr<draw_frame> receive_and_follow(spl::shared_ptr<frame_producer>& producer, int hints)\r
70 {       \r
71         auto frame = producer->receive(hints);\r
72         if(frame == draw_frame::eof())\r
73         {\r
74                 CASPAR_LOG(info) << producer->print() << " End Of File.";\r
75                 auto following = producer->get_following_producer();\r
76                 if(following != frame_producer::empty())\r
77                 {\r
78                         following->set_leading_producer(producer);\r
79                         producer = std::move(following);\r
80                 }\r
81 \r
82                 return receive_and_follow(producer, hints);\r
83         }\r
84         return frame;\r
85 }\r
86 \r
87 void register_producer_factory(const producer_factory_t& factory)\r
88 {\r
89         g_factories.push_back(factory);\r
90 }\r
91 \r
92 class destroy_producer_proxy : public frame_producer\r
93 {       \r
94         std::unique_ptr<std::shared_ptr<frame_producer>> producer_;\r
95 public:\r
96         destroy_producer_proxy(spl::shared_ptr<frame_producer>&& producer) \r
97                 : producer_(new std::shared_ptr<frame_producer>(std::move(producer)))\r
98         {\r
99         }\r
100 \r
101         ~destroy_producer_proxy()\r
102         {               \r
103                 static tbb::atomic<int> counter = tbb::atomic<int>();\r
104                 \r
105                 ++counter;\r
106                 CASPAR_VERIFY(counter < 32);\r
107                 \r
108                 auto producer = producer_.release();\r
109                 async([=]\r
110                 {\r
111                         std::unique_ptr<std::shared_ptr<frame_producer>> pointer_guard(producer);\r
112 \r
113                         auto str = (*producer)->print();\r
114                         try\r
115                         {\r
116                                 if(!producer->unique())\r
117                                         CASPAR_LOG(trace) << str << L" Not destroyed on asynchronous destruction thread: " << producer->use_count();\r
118                                 else\r
119                                         CASPAR_LOG(trace) << str << L" Destroying on asynchronous destruction thread.";\r
120                         }\r
121                         catch(...){}\r
122 \r
123                         pointer_guard.reset();\r
124 \r
125                         --counter;\r
126                 }); \r
127         }\r
128 \r
129         virtual spl::shared_ptr<draw_frame>                                                             receive(int hints) override                                                                                             {return (*producer_)->receive(hints);}\r
130         virtual spl::shared_ptr<draw_frame>                                                             last_frame() const override                                                                                             {return (*producer_)->last_frame();}\r
131         virtual std::wstring                                                                            print() const override                                                                                                  {return (*producer_)->print();}\r
132         virtual boost::property_tree::wptree                                            info() const override                                                                                                   {return (*producer_)->info();}\r
133         virtual boost::unique_future<std::wstring>                                      call(const std::wstring& str) override                                                                  {return (*producer_)->call(str);}\r
134         virtual spl::shared_ptr<frame_producer>                                                 get_following_producer() const override                                                                 {return (*producer_)->get_following_producer();}\r
135         virtual void                                                                                            set_leading_producer(const spl::shared_ptr<frame_producer>& producer) override  {(*producer_)->set_leading_producer(producer);}\r
136         virtual uint32_t                                                                                        nb_frames() const override                                                                                              {return (*producer_)->nb_frames();}\r
137 };\r
138 \r
139 class print_producer_proxy : public frame_producer\r
140 {       \r
141         std::shared_ptr<frame_producer> producer_;\r
142 public:\r
143         print_producer_proxy(spl::shared_ptr<frame_producer>&& producer) \r
144                 : producer_(std::move(producer))\r
145         {\r
146                 CASPAR_LOG(info) << producer_->print() << L" Initialized.";\r
147         }\r
148 \r
149         ~print_producer_proxy()\r
150         {               \r
151                 auto str = producer_->print();\r
152                 CASPAR_LOG(trace) << str << L" Uninitializing.";\r
153                 producer_.reset();\r
154                 CASPAR_LOG(info) << str << L" Uninitialized.";\r
155         }\r
156 \r
157         virtual spl::shared_ptr<draw_frame>                             receive(int hints) override                                                                                             {return (producer_)->receive(hints);}\r
158         virtual spl::shared_ptr<draw_frame>                             last_frame() const override                                                                                             {return (producer_)->last_frame();}\r
159         virtual std::wstring                                            print() const override                                                                                                  {return (producer_)->print();}\r
160         virtual boost::property_tree::wptree            info() const override                                                                                                   {return (producer_)->info();}\r
161         virtual boost::unique_future<std::wstring>      call(const std::wstring& str) override                                                                  {return (producer_)->call(str);}\r
162         virtual spl::shared_ptr<frame_producer>                 get_following_producer() const override                                                                 {return (producer_)->get_following_producer();}\r
163         virtual void                                                            set_leading_producer(const spl::shared_ptr<frame_producer>& producer) override  {(producer_)->set_leading_producer(producer);}\r
164         virtual uint32_t                                                        nb_frames() const override                                                                                              {return (producer_)->nb_frames();}\r
165 };\r
166 \r
167 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
168 {\r
169         if(params.empty())\r
170                 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));\r
171         \r
172         auto producer = frame_producer::empty();\r
173         std::any_of(g_factories.begin(), g_factories.end(), [&](const producer_factory_t& factory) -> bool\r
174                 {\r
175                         try\r
176                         {\r
177                                 producer = factory(my_frame_factory, params);\r
178                         }\r
179                         catch(...)\r
180                         {\r
181                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
182                         }\r
183                         return producer != frame_producer::empty();\r
184                 });\r
185 \r
186         if(producer == frame_producer::empty())\r
187                 producer = create_color_producer(my_frame_factory, params);\r
188 \r
189         if(producer == frame_producer::empty())\r
190                 return producer;\r
191                 \r
192         return spl::make_shared<destroy_producer_proxy>(\r
193                         spl::make_shared<print_producer_proxy>(\r
194                          std::move(producer)));\r
195 }\r
196 \r
197 spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<frame_factory>& my_frame_factory, const std::vector<std::wstring>& params)\r
198 {       \r
199         auto producer = do_create_producer(my_frame_factory, params);\r
200         auto key_producer = frame_producer::empty();\r
201         \r
202         try // to find a key file.\r
203         {\r
204                 auto params_copy = params;\r
205                 if(params_copy.size() > 0)\r
206                 {\r
207                         params_copy[0] += L"_A";\r
208                         key_producer = do_create_producer(my_frame_factory, params_copy);                       \r
209                         if(key_producer == frame_producer::empty())\r
210                         {\r
211                                 params_copy[0] += L"LPHA";\r
212                                 key_producer = do_create_producer(my_frame_factory, params_copy);       \r
213                         }\r
214                 }\r
215         }\r
216         catch(...){}\r
217 \r
218         if(producer != frame_producer::empty() && key_producer != frame_producer::empty())\r
219                 return create_separated_producer(producer, key_producer);\r
220         \r
221         if(producer == frame_producer::empty())\r
222         {\r
223                 std::wstring str;\r
224                 BOOST_FOREACH(auto& param, params)\r
225                         str += param + L" ";\r
226                 BOOST_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax.") << arg_value_info(u8(str)));\r
227         }\r
228 \r
229         return producer;\r
230 }\r
231 \r
232 \r
233 spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<frame_factory>& factory, const std::wstring& params)\r
234 {\r
235         std::wstringstream iss(params);\r
236         std::vector<std::wstring> tokens;\r
237         typedef std::istream_iterator<std::wstring, wchar_t, std::char_traits<wchar_t> > iterator;\r
238         std::copy(iterator(iss),  iterator(), std::back_inserter(tokens));\r
239         return create_producer(factory, tokens);\r
240 }\r
241 \r
242 }}