]> git.sesse.net Git - casparcg/blob - core/producer/separated/separated_producer.cpp
66cb7e8281f47706fae0b722d63ae0cdbe160a10
[casparcg] / core / producer / separated / separated_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 #include "../../stdafx.h"\r
21 \r
22 #include "separated_producer.h"\r
23 \r
24 #include <core/producer/frame/basic_frame.h>\r
25 \r
26 #include <tbb/parallel_invoke.h>\r
27 \r
28 namespace caspar { namespace core {     \r
29 \r
30 struct separated_producer : public frame_producer\r
31 {               \r
32         safe_ptr<frame_producer>        fill_producer_;\r
33         safe_ptr<frame_producer>        key_producer_;\r
34         safe_ptr<basic_frame>           fill_;\r
35         safe_ptr<basic_frame>           key_;\r
36         safe_ptr<basic_frame>           last_frame_;\r
37                 \r
38         explicit separated_producer(const safe_ptr<frame_producer>& fill, const safe_ptr<frame_producer>& key) \r
39                 : fill_producer_(fill)\r
40                 , key_producer_(key)\r
41                 , fill_(core::basic_frame::late())\r
42                 , key_(core::basic_frame::late())\r
43                 , last_frame_(core::basic_frame::empty())\r
44         {\r
45         }\r
46         // frame_producer\r
47         \r
48         virtual safe_ptr<basic_frame> receive(int hints)\r
49         {\r
50                 tbb::parallel_invoke(\r
51                 [&]\r
52                 {\r
53                         if(fill_ == core::basic_frame::late())\r
54                                 fill_ = receive_and_follow(fill_producer_, hints);\r
55                 },\r
56                 [&]\r
57                 {\r
58                         if(key_ == core::basic_frame::late())\r
59                                 key_ = receive_and_follow(key_producer_, hints | ALPHA_HINT);\r
60                 });\r
61 \r
62                 if(fill_ == basic_frame::eof() || key_ == basic_frame::eof())\r
63                         return basic_frame::eof();\r
64 \r
65                 if(fill_ == core::basic_frame::late() || key_ == core::basic_frame::late()) // One of the producers is lagging, keep them in sync.\r
66                         return core::basic_frame::late();\r
67                 \r
68                 auto frame = basic_frame::fill_and_key(fill_, key_);\r
69 \r
70                 fill_ = basic_frame::late();\r
71                 key_  = basic_frame::late();\r
72 \r
73                 return last_frame_ = frame;\r
74         }\r
75 \r
76         virtual safe_ptr<core::basic_frame> last_frame() const\r
77         {\r
78                 return last_frame_;\r
79         }\r
80 \r
81         virtual std::wstring print() const\r
82         {\r
83                 return L"separated[fill:" + fill_producer_->print() + L"|key:" + key_producer_->print() + L"]";\r
84         }       \r
85 \r
86         virtual int64_t nb_frames() const \r
87         {\r
88                 return std::min(fill_producer_->nb_frames(), key_producer_->nb_frames());\r
89         }\r
90 };\r
91 \r
92 safe_ptr<frame_producer> create_separated_producer(const safe_ptr<frame_producer>& fill, const safe_ptr<frame_producer>& key)\r
93 {\r
94         return make_safe<separated_producer>(fill, key);\r
95 }\r
96 \r
97 }}\r
98 \r