]> git.sesse.net Git - casparcg/blob - core/producer/frame_producer.cpp
da08185ac5ef7e66d22c0db298098c6f153a2410
[casparcg] / core / producer / frame_producer.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */\r
20 \r
21 #include "../StdAfx.h"\r
22 \r
23 #include "frame_producer.h"\r
24 #include "frame/basic_frame.h"\r
25 #include "frame/frame_transform.h"\r
26 \r
27 #include "color/color_producer.h"\r
28 #include "separated/separated_producer.h"\r
29 \r
30 #include <common/memory/safe_ptr.h>\r
31 #include <common/exception/exceptions.h>\r
32 \r
33 namespace caspar { namespace core {\r
34         \r
35 std::vector<const producer_factory_t> g_factories;\r
36 \r
37 const safe_ptr<frame_producer>& frame_producer::empty() // nothrow\r
38 {\r
39         struct empty_frame_producer : public frame_producer\r
40         {\r
41                 virtual safe_ptr<basic_frame> receive(int){return basic_frame::empty();}\r
42                 virtual safe_ptr<basic_frame> last_frame() const{return basic_frame::empty();}\r
43                 virtual void set_frame_factory(const safe_ptr<frame_factory>&){}\r
44                 virtual int64_t nb_frames() const {return 0;}\r
45                 virtual std::wstring print() const { return L"empty";}\r
46         };\r
47         static safe_ptr<frame_producer> producer = make_safe<empty_frame_producer>();\r
48         return producer;\r
49 }       \r
50 \r
51 safe_ptr<basic_frame> receive_and_follow(safe_ptr<frame_producer>& producer, int hints)\r
52 {       \r
53         auto frame = producer->receive(hints);\r
54         if(frame == basic_frame::eof())\r
55         {\r
56                 CASPAR_LOG(info) << producer->print() << " End Of File.";\r
57                 auto following = producer->get_following_producer();\r
58                 following->set_leading_producer(producer);\r
59                 producer = std::move(following);                \r
60                                 \r
61                 if(producer == frame_producer::empty())\r
62                         return basic_frame::eof();\r
63 \r
64                 return receive_and_follow(producer, hints);\r
65         }\r
66         return frame;\r
67 }\r
68 \r
69 void register_producer_factory(const producer_factory_t& factory)\r
70 {\r
71         g_factories.push_back(factory);\r
72 }\r
73 \r
74 safe_ptr<core::frame_producer> do_create_producer(const safe_ptr<frame_factory>& my_frame_factory, const std::vector<std::wstring>& params)\r
75 {\r
76         if(params.empty())\r
77                 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));\r
78         \r
79         auto producer = frame_producer::empty();\r
80         std::any_of(g_factories.begin(), g_factories.end(), [&](const producer_factory_t& factory) -> bool\r
81                 {\r
82                         try\r
83                         {\r
84                                 producer = factory(my_frame_factory, params);\r
85                         }\r
86                         catch(...)\r
87                         {\r
88                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
89                         }\r
90                         return producer != frame_producer::empty();\r
91                 });\r
92 \r
93         if(producer == frame_producer::empty())\r
94                 producer = create_color_producer(my_frame_factory, params);\r
95         \r
96         return producer;\r
97 }\r
98 \r
99 \r
100 safe_ptr<core::frame_producer> create_producer(const safe_ptr<frame_factory>& my_frame_factory, const std::vector<std::wstring>& params)\r
101 {       \r
102         auto producer = do_create_producer(my_frame_factory, params);\r
103         auto key_producer = frame_producer::empty();\r
104         \r
105         try // to find a key file.\r
106         {\r
107                 auto params_copy = params;\r
108                 if(params_copy.size() > 0)\r
109                 {\r
110                         params_copy[0] += L"_A";\r
111                         key_producer = do_create_producer(my_frame_factory, params_copy);                       \r
112                         if(key_producer == frame_producer::empty())\r
113                         {\r
114                                 params_copy[0] += L"LPHA";\r
115                                 key_producer = do_create_producer(my_frame_factory, params_copy);       \r
116                         }\r
117                 }\r
118         }\r
119         catch(...){}\r
120 \r
121         if(producer != frame_producer::empty() && key_producer != frame_producer::empty())\r
122                 producer = create_separated_producer(producer, key_producer);\r
123         \r
124         if(producer == frame_producer::empty())\r
125         {\r
126                 std::wstring str;\r
127                 BOOST_FOREACH(auto& param, params)\r
128                         str += param + L" ";\r
129                 BOOST_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax.") << arg_value_info(narrow(str)));\r
130         }\r
131 \r
132         return producer;\r
133 }\r
134 \r
135 }}