]> git.sesse.net Git - casparcg/blob - core/producer/separated/separated_producer.cpp
b18e8a7065705cfda5cfc109efae809ef98118ce
[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_producer.h>\r
27 #include <core/frame/draw_frame.h>\r
28 #include <core/monitor/monitor.h>\r
29 \r
30 #include <tbb/parallel_invoke.h>\r
31 \r
32 namespace caspar { namespace core {     \r
33 \r
34 class separated_producer : public frame_producer_base\r
35 {               \r
36         monitor::basic_subject                  event_subject_;\r
37         monitor::basic_subject                  key_event_subject_;\r
38 \r
39         spl::shared_ptr<frame_producer> fill_producer_;\r
40         spl::shared_ptr<frame_producer> key_producer_;\r
41         draw_frame                                              fill_;\r
42         draw_frame                                              key_;\r
43                         \r
44 public:\r
45         explicit separated_producer(const spl::shared_ptr<frame_producer>& fill, const spl::shared_ptr<frame_producer>& key) \r
46                 : key_event_subject_("keyer")           \r
47                 , fill_producer_(fill)\r
48                 , key_producer_(key)\r
49                 , fill_(core::draw_frame::late())\r
50                 , key_(core::draw_frame::late())\r
51         {\r
52                 CASPAR_LOG(info) << print() << L" Initialized";\r
53 \r
54                 key_event_subject_.subscribe(event_subject_);\r
55 \r
56                 key_producer_->subscribe(key_event_subject_);\r
57                 fill_producer_->subscribe(event_subject_);\r
58         }\r
59 \r
60         // frame_producer\r
61         \r
62         draw_frame receive_impl() override\r
63         {\r
64                 tbb::parallel_invoke(\r
65                 [&]\r
66                 {\r
67                         if(fill_ == core::draw_frame::late())\r
68                                 fill_ = fill_producer_->receive();\r
69                 },\r
70                 [&]\r
71                 {\r
72                         if(key_ == core::draw_frame::late())\r
73                                 key_ = key_producer_->receive();\r
74                 });\r
75                 \r
76                 if(fill_ == core::draw_frame::late() || key_ == core::draw_frame::late()) // One of the producers is lagging, keep them in sync.\r
77                         return core::draw_frame::late();\r
78                 \r
79                 auto frame = draw_frame::mask(fill_, key_);\r
80 \r
81                 fill_ = draw_frame::late();\r
82                 key_  = draw_frame::late();\r
83                 \r
84                 return frame;\r
85         }\r
86 \r
87         draw_frame last_frame()\r
88         {\r
89                 return draw_frame::mask(fill_producer_->last_frame(), key_producer_->last_frame());\r
90         }\r
91                                 \r
92         uint32_t nb_frames() const override\r
93         {\r
94                 return std::min(fill_producer_->nb_frames(), key_producer_->nb_frames());\r
95         }\r
96 \r
97         std::wstring print() const override\r
98         {\r
99                 return L"separated[fill:" + fill_producer_->print() + L"|key[" + key_producer_->print() + L"]]";\r
100         }       \r
101 \r
102         boost::unique_future<std::wstring> call(const std::wstring& str) override\r
103         {\r
104                 key_producer_->call(str);\r
105                 return fill_producer_->call(str);\r
106         }\r
107 \r
108         std::wstring name() const override\r
109         {\r
110                 return L"separated";\r
111         }\r
112 \r
113         boost::property_tree::wptree info() const override\r
114         {\r
115                 return fill_producer_->info();;\r
116         }\r
117 \r
118         void subscribe(const monitor::observable::observer_ptr& o) override                                                                                                                     \r
119         {\r
120                 return event_subject_.subscribe(o);\r
121         }\r
122 \r
123         void unsubscribe(const monitor::observable::observer_ptr& o) override           \r
124         {\r
125                 return event_subject_.unsubscribe(o);\r
126         }\r
127 };\r
128 \r
129 spl::shared_ptr<frame_producer> create_separated_producer(const spl::shared_ptr<frame_producer>& fill, const spl::shared_ptr<frame_producer>& key)\r
130 {\r
131         return spl::make_shared<separated_producer>(fill, key);\r
132 }\r
133 \r
134 }}\r
135 \r