2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../../stdafx.h"
24 #include "separated_producer.h"
26 #include <core/producer/frame_producer.h>
27 #include <core/frame/draw_frame.h>
28 #include <core/monitor/monitor.h>
30 #include <tbb/parallel_invoke.h>
32 namespace caspar { namespace core {
34 class separated_producer : public frame_producer_base
36 monitor::basic_subject event_subject_;
37 monitor::basic_subject key_event_subject_;
39 spl::shared_ptr<frame_producer> fill_producer_;
40 spl::shared_ptr<frame_producer> key_producer_;
45 explicit separated_producer(const spl::shared_ptr<frame_producer>& fill, const spl::shared_ptr<frame_producer>& key)
46 : key_event_subject_("keyer")
47 , fill_producer_(fill)
49 , fill_(core::draw_frame::late())
50 , key_(core::draw_frame::late())
52 CASPAR_LOG(info) << print() << L" Initialized";
54 key_event_subject_.subscribe(event_subject_);
56 key_producer_->subscribe(key_event_subject_);
57 fill_producer_->subscribe(event_subject_);
62 draw_frame receive_impl() override
67 if(fill_ == core::draw_frame::late())
68 fill_ = fill_producer_->receive();
72 if(key_ == core::draw_frame::late())
73 key_ = key_producer_->receive();
76 if(fill_ == core::draw_frame::late() || key_ == core::draw_frame::late()) // One of the producers is lagging, keep them in sync.
77 return core::draw_frame::late();
79 auto frame = draw_frame::mask(fill_, key_);
81 fill_ = draw_frame::late();
82 key_ = draw_frame::late();
87 draw_frame last_frame()
89 return draw_frame::mask(fill_producer_->last_frame(), key_producer_->last_frame());
92 uint32_t nb_frames() const override
94 return std::min(fill_producer_->nb_frames(), key_producer_->nb_frames());
97 std::wstring print() const override
99 return L"separated[fill:" + fill_producer_->print() + L"|key[" + key_producer_->print() + L"]]";
102 boost::unique_future<std::wstring> call(const std::wstring& str) override
104 key_producer_->call(str);
105 return fill_producer_->call(str);
108 std::wstring name() const override
113 boost::property_tree::wptree info() const override
115 return fill_producer_->info();;
118 void subscribe(const monitor::observable::observer_ptr& o) override
120 return event_subject_.subscribe(o);
123 void unsubscribe(const monitor::observable::observer_ptr& o) override
125 return event_subject_.unsubscribe(o);
129 spl::shared_ptr<frame_producer> create_separated_producer(const spl::shared_ptr<frame_producer>& fill, const spl::shared_ptr<frame_producer>& key)
131 return spl::make_shared<separated_producer>(fill, key);