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