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 monitor::basic_subject event_subject_;
40 monitor::basic_subject foreground_event_subject_;
41 monitor::basic_subject background_event_subject_;
42 spl::shared_ptr<frame_producer> foreground_;
43 spl::shared_ptr<frame_producer> background_;
44 boost::optional<int32_t> auto_play_delta_;
48 : event_subject_(monitor::path("layer") % index)
49 , foreground_event_subject_("")
50 , background_event_subject_("background")
51 , foreground_(frame_producer::empty())
52 , background_(frame_producer::empty())
54 foreground_event_subject_.subscribe(event_subject_);
55 background_event_subject_.subscribe(event_subject_);
60 foreground_->paused(true);
63 void load(spl::shared_ptr<frame_producer> producer, bool preview, const boost::optional<int32_t>& auto_play_delta)
65 background_->unsubscribe(background_event_subject_);
66 background_ = std::move(producer);
67 background_->subscribe(background_event_subject_);
69 auto_play_delta_ = auto_play_delta;
74 foreground_->paused(true);
77 if(auto_play_delta_ && foreground_ == frame_producer::empty())
83 if(background_ != frame_producer::empty())
85 background_->leading_producer(foreground_);
87 background_->unsubscribe(background_event_subject_);
88 foreground_->unsubscribe(foreground_event_subject_);
90 foreground_ = std::move(background_);
91 background_ = std::move(frame_producer::empty());
93 foreground_->subscribe(foreground_event_subject_);
95 auto_play_delta_.reset();
98 foreground_->paused(false);
103 foreground_->unsubscribe(foreground_event_subject_);
105 foreground_ = std::move(frame_producer::empty());
107 auto_play_delta_.reset();
110 draw_frame receive(const video_format_desc& format_desc)
114 auto frame = foreground_->receive();
116 if(frame == core::draw_frame::late())
117 return foreground_->last_frame();
121 auto frames_left = static_cast<int64_t>(foreground_->nb_frames()) - foreground_->frame_number() - static_cast<int64_t>(*auto_play_delta_);
125 return receive(format_desc);
129 event_subject_ << monitor::event("time") % monitor::duration(foreground_->frame_number()/format_desc.fps)
130 % monitor::duration(static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)/format_desc.fps)
131 << monitor::event("frame") % static_cast<int64_t>(foreground_->frame_number())
132 % static_cast<int64_t>((static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)));
134 foreground_event_subject_ << monitor::event("type") % foreground_->name();
135 background_event_subject_ << monitor::event("type") % background_->name();
141 CASPAR_LOG_CURRENT_EXCEPTION();
143 return core::draw_frame::empty();
147 boost::property_tree::wptree info() const
149 boost::property_tree::wptree info;
150 info.add(L"auto_delta", (auto_play_delta_ ? boost::lexical_cast<std::wstring>(*auto_play_delta_) : L"null"));
151 info.add(L"frame-number", foreground_->frame_number());
153 auto nb_frames = foreground_->nb_frames();
155 info.add(L"nb_frames", nb_frames == std::numeric_limits<int64_t>::max() ? -1 : nb_frames);
156 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)));
157 info.add_child(L"producer", foreground_->info());
158 info.add_child(L"background.producer", background_->info());
162 void on_interaction(const interaction_event::ptr& event)
164 foreground_->on_interaction(event);
167 bool collides(double x, double y) const
169 return foreground_->collides(x, y);
173 layer::layer(int index) : impl_(new impl(index)){}
174 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}
175 layer& layer::operator=(layer&& other)
180 void layer::swap(layer& other)
182 impl_.swap(other.impl_);
184 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);}
185 void layer::play(){impl_->play();}
186 void layer::pause(){impl_->pause();}
187 void layer::stop(){impl_->stop();}
188 draw_frame layer::receive(const video_format_desc& format_desc) {return impl_->receive(format_desc);}
189 spl::shared_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}
190 spl::shared_ptr<frame_producer> layer::background() const { return impl_->background_;}
191 boost::property_tree::wptree layer::info() const{return impl_->info();}
192 void layer::subscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.subscribe(o);}
193 void layer::unsubscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.unsubscribe(o);}
194 void layer::on_interaction(const interaction_event::ptr& event) { impl_->on_interaction(event); }
195 bool layer::collides(double x, double y) const { return impl_->collides(x, y); }