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"
26 #include "frame_producer.h"
28 #include "../video_format.h"
29 #include "../frame/draw_frame.h"
30 #include "../frame/frame_transform.h"
32 #include <boost/optional.hpp>
33 #include <boost/thread/future.hpp>
35 namespace caspar { namespace core {
39 spl::shared_ptr<monitor::subject> monitor_subject_;
40 spl::shared_ptr<frame_producer> foreground_ = frame_producer::empty();
41 spl::shared_ptr<frame_producer> background_ = frame_producer::empty();;
42 boost::optional<int32_t> auto_play_delta_;
43 bool is_paused_ = false;
44 int64_t current_frame_age_ = 0;
48 : monitor_subject_(spl::make_shared<monitor::subject>(
49 "/layer/" + boost::lexical_cast<std::string>(index)))
50 // , foreground_event_subject_("")
51 // , background_event_subject_("background")
53 // foreground_event_subject_.subscribe(event_subject_);
54 // background_event_subject_.subscribe(event_subject_);
57 void set_foreground(spl::shared_ptr<frame_producer> producer)
59 foreground_->monitor_output().detach_parent();
60 foreground_ = std::move(producer);
61 foreground_->monitor_output().attach_parent(monitor_subject_);
66 foreground_->paused(true);
72 foreground_->paused(false);
76 void load(spl::shared_ptr<frame_producer> producer, bool preview, const boost::optional<int32_t>& auto_play_delta)
78 // background_->unsubscribe(background_event_subject_);
79 background_ = std::move(producer);
80 // background_->subscribe(background_event_subject_);
82 auto_play_delta_ = auto_play_delta;
87 receive(video_format::invalid);
88 foreground_->paused(true);
92 if(auto_play_delta_ && foreground_ == frame_producer::empty())
98 if(background_ != frame_producer::empty())
100 background_->leading_producer(foreground_);
102 set_foreground(background_);
103 background_ = std::move(frame_producer::empty());
105 auto_play_delta_.reset();
108 foreground_->paused(false);
114 set_foreground(frame_producer::empty());
116 auto_play_delta_.reset();
119 draw_frame receive(const video_format_desc& format_desc)
123 *monitor_subject_ << monitor::message("/paused") % is_paused_;
125 caspar::timer produce_timer;
126 auto frame = foreground_->receive();
127 auto produce_time = produce_timer.elapsed();
129 *monitor_subject_ << monitor::message("/profiler/time") % produce_time % (1.0 / format_desc.fps);
131 if(frame == core::draw_frame::late())
132 return foreground_->last_frame();
136 auto frames_left = static_cast<int64_t>(foreground_->nb_frames()) - foreground_->frame_number() - static_cast<int64_t>(*auto_play_delta_);
140 return receive(format_desc);
144 //event_subject_ << monitor::event("time") % monitor::duration(foreground_->frame_number()/format_desc.fps)
145 // % monitor::duration(static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)/format_desc.fps)
146 // << monitor::event("frame") % static_cast<int64_t>(foreground_->frame_number())
147 // % static_cast<int64_t>((static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)));
149 //foreground_event_subject_ << monitor::event("type") % foreground_->name();
150 //background_event_subject_ << monitor::event("type") % background_->name();
152 current_frame_age_ = frame.get_and_record_age_millis();
158 CASPAR_LOG_CURRENT_EXCEPTION();
160 return core::draw_frame::empty();
164 boost::property_tree::wptree info() const
166 boost::property_tree::wptree info;
167 info.add(L"auto_delta", (auto_play_delta_ ? boost::lexical_cast<std::wstring>(*auto_play_delta_) : L"null"));
168 info.add(L"frame-number", foreground_->frame_number());
170 auto nb_frames = foreground_->nb_frames();
172 info.add(L"nb_frames", nb_frames == std::numeric_limits<int64_t>::max() ? -1 : nb_frames);
173 info.add(L"frames-left", nb_frames == std::numeric_limits<int64_t>::max() ? -1 : (foreground_->nb_frames() - foreground_->frame_number() - (auto_play_delta_ ? *auto_play_delta_ : 0)));
174 info.add(L"frame-age", current_frame_age_);
175 info.add_child(L"producer", foreground_->info());
176 info.add_child(L"background.producer", background_->info());
180 boost::property_tree::wptree delay_info() const
182 boost::property_tree::wptree info;
183 info.add(L"producer", foreground_->print());
184 info.add(L"frame-age", current_frame_age_);
188 void on_interaction(const interaction_event::ptr& event)
190 foreground_->on_interaction(event);
193 bool collides(double x, double y) const
195 return foreground_->collides(x, y);
199 layer::layer(int index) : impl_(new impl(index)){}
200 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}
201 layer& layer::operator=(layer&& other)
206 void layer::swap(layer& other)
208 impl_.swap(other.impl_);
210 void layer::load(spl::shared_ptr<frame_producer> frame_producer, bool preview, const boost::optional<int32_t>& auto_play_delta){return impl_->load(std::move(frame_producer), preview, auto_play_delta);}
211 void layer::play(){impl_->play();}
212 void layer::pause(){impl_->pause();}
213 void layer::resume(){impl_->resume();}
214 void layer::stop(){impl_->stop();}
215 draw_frame layer::receive(const video_format_desc& format_desc) {return impl_->receive(format_desc);}
216 spl::shared_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}
217 spl::shared_ptr<frame_producer> layer::background() const { return impl_->background_;}
218 boost::property_tree::wptree layer::info() const{return impl_->info();}
219 boost::property_tree::wptree layer::delay_info() const{return impl_->delay_info();}
220 monitor::subject& layer::monitor_output() {return *impl_->monitor_subject_;}
221 void layer::on_interaction(const interaction_event::ptr& event) { impl_->on_interaction(event); }
222 bool layer::collides(double x, double y) const { return impl_->collides(x, y); }