]> git.sesse.net Git - casparcg/blob - core/producer/separated/separated_producer.cpp
9ef26c6f0e5a8566028cd7a1feeb1a68634bc1fa
[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 <core/producer/frame/basic_frame.h>\r
27 \r
28 #include <tbb/parallel_invoke.h>\r
29 \r
30 namespace caspar { namespace core {     \r
31 \r
32 struct separated_producer : public frame_producer\r
33 {               \r
34         safe_ptr<frame_producer>        fill_producer_;\r
35         safe_ptr<frame_producer>        key_producer_;\r
36         safe_ptr<basic_frame>           fill_;\r
37         safe_ptr<basic_frame>           key_;\r
38         safe_ptr<basic_frame>           last_frame_;\r
39                 \r
40         explicit separated_producer(const safe_ptr<frame_producer>& fill, const safe_ptr<frame_producer>& key) \r
41                 : fill_producer_(fill)\r
42                 , key_producer_(key)\r
43                 , fill_(core::basic_frame::late())\r
44                 , key_(core::basic_frame::late())\r
45                 , last_frame_(core::basic_frame::empty())\r
46         {\r
47         }\r
48 \r
49         // frame_producer\r
50         \r
51         virtual safe_ptr<basic_frame> receive(int hints) override\r
52         {\r
53                 tbb::parallel_invoke(\r
54                 [&]\r
55                 {\r
56                         if(fill_ == core::basic_frame::late())\r
57                                 fill_ = receive_and_follow(fill_producer_, hints);\r
58                 },\r
59                 [&]\r
60                 {\r
61                         if(key_ == core::basic_frame::late())\r
62                                 key_ = receive_and_follow(key_producer_, hints | ALPHA_HINT);\r
63                 });\r
64 \r
65                 if(fill_ == basic_frame::eof() || key_ == basic_frame::eof())\r
66                         return basic_frame::eof();\r
67 \r
68                 if(fill_ == core::basic_frame::late() || key_ == core::basic_frame::late()) // One of the producers is lagging, keep them in sync.\r
69                         return core::basic_frame::late();\r
70                 \r
71                 auto frame = basic_frame::fill_and_key(fill_, key_);\r
72 \r
73                 fill_ = basic_frame::late();\r
74                 key_  = basic_frame::late();\r
75 \r
76                 return last_frame_ = frame;\r
77         }\r
78 \r
79         virtual safe_ptr<core::basic_frame> last_frame() const override\r
80         {\r
81                 return disable_audio(last_frame_);\r
82         }\r
83 \r
84         virtual safe_ptr<basic_frame> create_thumbnail_frame() override\r
85         {\r
86                 auto fill_frame = fill_producer_->create_thumbnail_frame();\r
87                 auto key_frame = key_producer_->create_thumbnail_frame();\r
88 \r
89                 if (fill_frame == basic_frame::empty() || key_frame == basic_frame::empty())\r
90                         return basic_frame::empty();\r
91 \r
92                 if (fill_frame == basic_frame::eof() || key_frame == basic_frame::eof())\r
93                         return basic_frame::eof();\r
94 \r
95                 if (fill_frame == basic_frame::late() || key_frame == basic_frame::late())\r
96                         return basic_frame::late();\r
97 \r
98                 return basic_frame::fill_and_key(fill_frame, key_frame);\r
99         }\r
100 \r
101         virtual uint32_t nb_frames() const override\r
102         {\r
103                 return std::min(fill_producer_->nb_frames(), key_producer_->nb_frames());\r
104         }\r
105 \r
106         virtual std::wstring print() const override\r
107         {\r
108                 return L"separated[fill:" + fill_producer_->print() + L"|key[" + key_producer_->print() + L"]]";\r
109         }       \r
110 \r
111         boost::property_tree::wptree info() const override\r
112         {\r
113                 boost::property_tree::wptree info;\r
114                 info.add(L"type", L"separated-producer");\r
115                 info.add_child(L"fill.producer",        fill_producer_->info());\r
116                 info.add_child(L"key.producer", key_producer_->info());\r
117                 return info;\r
118         }\r
119 };\r
120 \r
121 safe_ptr<frame_producer> create_separated_producer(const safe_ptr<frame_producer>& fill, const safe_ptr<frame_producer>& key)\r
122 {\r
123         return create_producer_print_proxy(\r
124                         make_safe<separated_producer>(fill, key));\r
125 }\r
126 \r
127 safe_ptr<frame_producer> create_separated_thumbnail_producer(const safe_ptr<frame_producer>& fill, const safe_ptr<frame_producer>& key)\r
128 {\r
129         return make_safe<separated_producer>(fill, key);\r
130 }\r
131 \r
132 }}\r
133 \r