]> git.sesse.net Git - casparcg/blob - core/producer/separated/separated_producer.cpp
ddcf2293161149d83511585499ef17b27e6fbe75
[casparcg] / core / producer / separated / separated_producer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../../StdAfx.h"
23
24 #include "separated_producer.h"
25
26 #include <core/producer/frame_producer.h>
27 #include <core/frame/draw_frame.h>
28 #include <core/monitor/monitor.h>
29
30 #include <tbb/parallel_invoke.h>
31
32 #include <future>
33
34 namespace caspar { namespace core {     
35
36 class separated_producer : public frame_producer_base
37 {               
38         spl::shared_ptr<monitor::subject>       monitor_subject_;
39         spl::shared_ptr<monitor::subject>       key_monitor_subject_    = spl::make_shared<monitor::subject>("/keyer");
40
41         spl::shared_ptr<frame_producer> fill_producer_;
42         spl::shared_ptr<frame_producer> key_producer_;
43         draw_frame                                              fill_                                           = core::draw_frame::late();
44         draw_frame                                              key_                                            = core::draw_frame::late();
45                         
46 public:
47         explicit separated_producer(const spl::shared_ptr<frame_producer>& fill, const spl::shared_ptr<frame_producer>& key) 
48                 : fill_producer_(fill)
49                 , key_producer_(key)
50         {
51                 key_monitor_subject_->attach_parent(monitor_subject_);
52
53                 key_producer_->monitor_output().attach_parent(key_monitor_subject_);
54                 fill_producer_->monitor_output().attach_parent(monitor_subject_);
55
56                 CASPAR_LOG(debug) << print() << L" Initialized";
57         }
58
59         // frame_producer
60         
61         draw_frame receive_impl() override
62         {
63                 tbb::parallel_invoke(
64                 [&]
65                 {
66                         if(fill_ == core::draw_frame::late())
67                                 fill_ = fill_producer_->receive();
68                 },
69                 [&]
70                 {
71                         if(key_ == core::draw_frame::late())
72                                 key_ = key_producer_->receive();
73                 });
74                 
75                 if(fill_ == core::draw_frame::late() || key_ == core::draw_frame::late()) // One of the producers is lagging, keep them in sync.
76                         return core::draw_frame::late();
77                 
78                 auto frame = draw_frame::mask(fill_, key_);
79
80                 fill_ = draw_frame::late();
81                 key_  = draw_frame::late();
82                 
83                 return frame;
84         }
85
86         draw_frame last_frame()
87         {
88                 return draw_frame::mask(fill_producer_->last_frame(), key_producer_->last_frame());
89         }
90
91         constraints& pixel_constraints() override
92         {
93                 return fill_producer_->pixel_constraints();
94         }
95                                 
96         uint32_t nb_frames() const override
97         {
98                 return std::min(fill_producer_->nb_frames(), key_producer_->nb_frames());
99         }
100
101         std::wstring print() const override
102         {
103                 return L"separated[fill:" + fill_producer_->print() + L"|key[" + key_producer_->print() + L"]]";
104         }       
105
106         std::future<std::wstring> call(const std::vector<std::wstring>& params) override
107         {
108                 key_producer_->call(params);
109                 return fill_producer_->call(params);
110         }
111
112         std::wstring name() const override
113         {
114                 return L"separated";
115         }
116
117         boost::property_tree::wptree info() const override
118         {
119                 return fill_producer_->info();;
120         }
121
122         monitor::subject& monitor_output() { return *monitor_subject_; }
123 };
124
125 spl::shared_ptr<frame_producer> create_separated_producer(const spl::shared_ptr<frame_producer>& fill, const spl::shared_ptr<frame_producer>& key)
126 {
127         return spl::make_shared<separated_producer>(fill, key);
128 }
129
130 }}
131