]> git.sesse.net Git - casparcg/blob - core/producer/separated/separated_producer.cpp
2.1.0: refactored print and destroy proxies.
[casparcg] / core / producer / separated / separated_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 "separated_producer.h"\r
25 \r
26 #include "../frame_producer.h"\r
27 #include "../../frame/draw_frame.h"\r
28 \r
29 #include <tbb/parallel_invoke.h>\r
30 \r
31 namespace caspar { namespace core {     \r
32 \r
33 struct separated_producer : public frame_producer\r
34 {               \r
35         safe_ptr<frame_producer>        fill_producer_;\r
36         safe_ptr<frame_producer>        key_producer_;\r
37         safe_ptr<draw_frame>            fill_;\r
38         safe_ptr<draw_frame>            key_;\r
39         safe_ptr<draw_frame>            last_frame_;\r
40                 \r
41         explicit separated_producer(const safe_ptr<frame_producer>& fill, const safe_ptr<frame_producer>& key) \r
42                 : fill_producer_(fill)\r
43                 , key_producer_(key)\r
44                 , fill_(core::draw_frame::late())\r
45                 , key_(core::draw_frame::late())\r
46                 , last_frame_(core::draw_frame::empty())\r
47         {\r
48         }\r
49 \r
50         // frame_producer\r
51         \r
52         virtual safe_ptr<draw_frame> receive(int flags) override\r
53         {\r
54                 tbb::parallel_invoke(\r
55                 [&]\r
56                 {\r
57                         if(fill_ == core::draw_frame::late())\r
58                                 fill_ = receive_and_follow(fill_producer_, flags);\r
59                 },\r
60                 [&]\r
61                 {\r
62                         if(key_ == core::draw_frame::late())\r
63                                 key_ = receive_and_follow(key_producer_, flags | frame_producer::flags::alpha_only);\r
64                 });\r
65 \r
66                 if(fill_ == draw_frame::eof() || key_ == draw_frame::eof())\r
67                         return draw_frame::eof();\r
68 \r
69                 if(fill_ == core::draw_frame::late() || key_ == core::draw_frame::late()) // One of the producers is lagging, keep them in sync.\r
70                         return core::draw_frame::late();\r
71                 \r
72                 auto frame = draw_frame::mask(fill_, key_);\r
73 \r
74                 fill_ = draw_frame::late();\r
75                 key_  = draw_frame::late();\r
76 \r
77                 return last_frame_ = frame;\r
78         }\r
79 \r
80         virtual safe_ptr<core::draw_frame> last_frame() const override\r
81         {\r
82                 return last_frame_;\r
83         }\r
84 \r
85         virtual uint32_t nb_frames() const override\r
86         {\r
87                 return std::min(fill_producer_->nb_frames(), key_producer_->nb_frames());\r
88         }\r
89 \r
90         virtual std::wstring print() const override\r
91         {\r
92                 return L"separated[fill:" + fill_producer_->print() + L"|key[" + key_producer_->print() + L"]]";\r
93         }       \r
94 \r
95         boost::property_tree::wptree info() const override\r
96         {\r
97                 boost::property_tree::wptree info;\r
98                 info.add(L"type", L"separated-producer");\r
99                 info.add_child(L"fill.producer",        fill_producer_->info());\r
100                 info.add_child(L"key.producer", key_producer_->info());\r
101                 return info;\r
102         }\r
103 };\r
104 \r
105 safe_ptr<frame_producer> create_separated_producer(const safe_ptr<frame_producer>& fill, const safe_ptr<frame_producer>& key)\r
106 {\r
107         return make_safe<separated_producer>(fill, key);\r
108 }\r
109 \r
110 }}\r
111 \r