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