2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\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
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
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
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "../stdafx.h"
\r
26 #include "frame_producer.h"
\r
28 #include "../video_format.h"
\r
29 #include "../frame/draw_frame.h"
\r
30 #include "../frame/frame_transform.h"
\r
32 #include <boost/optional.hpp>
\r
33 #include <boost/thread/future.hpp>
\r
35 namespace caspar { namespace core {
\r
39 monitor::basic_subject event_subject_;
\r
40 monitor::basic_subject foreground_event_subject_;
\r
41 monitor::basic_subject background_event_subject_;
\r
42 spl::shared_ptr<frame_producer> foreground_;
\r
43 spl::shared_ptr<frame_producer> background_;
\r
44 int64_t frame_number_;
\r
45 boost::optional<int32_t> auto_play_delta_;
\r
50 : event_subject_(monitor::path("layer") % index)
\r
51 , foreground_event_subject_("")
\r
52 , background_event_subject_("background")
\r
53 , foreground_(frame_producer::empty())
\r
54 , background_(frame_producer::empty())
\r
58 foreground_event_subject_.subscribe(event_subject_);
\r
59 background_event_subject_.subscribe(event_subject_);
\r
72 void load(spl::shared_ptr<frame_producer> producer, bool preview, const boost::optional<int32_t>& auto_play_delta)
\r
74 background_->unsubscribe(background_event_subject_);
\r
75 background_ = std::move(producer);
\r
76 background_->subscribe(background_event_subject_);
\r
78 auto_play_delta_ = auto_play_delta;
\r
83 foreground_->receive(0);
\r
87 if(auto_play_delta_ && foreground_ == frame_producer::empty())
\r
93 if(background_ != frame_producer::empty())
\r
95 background_->leading_producer(foreground_);
\r
97 background_->unsubscribe(background_event_subject_);
\r
98 foreground_->unsubscribe(foreground_event_subject_);
\r
100 foreground_ = std::move(background_);
\r
101 background_ = std::move(frame_producer::empty());
\r
103 foreground_->subscribe(foreground_event_subject_);
\r
106 auto_play_delta_.reset();
\r
114 foreground_->unsubscribe(foreground_event_subject_);
\r
116 foreground_ = std::move(frame_producer::empty());
\r
119 auto_play_delta_.reset();
\r
124 draw_frame receive(frame_producer::flags flags, const video_format_desc& format_desc)
\r
129 return foreground_->last_frame();
\r
131 auto frame = foreground_->receive(flags.value());
\r
133 if(frame == core::draw_frame::late())
\r
134 return foreground_->last_frame();
\r
136 if(frame == core::draw_frame::eof())
\r
139 return core::draw_frame::empty();
\r
144 if(auto_play_delta_)
\r
146 auto frames_left = static_cast<int64_t>(foreground_->nb_frames()) - frame_number_ - static_cast<int64_t>(*auto_play_delta_);
\r
147 if(frames_left < 1)
\r
150 return receive(flags, format_desc);
\r
154 event_subject_ << monitor::event("state") % u8(is_paused_ ? L"paused" : (foreground_ == frame_producer::empty() ? L"stopped" : L"playing"))
\r
155 << monitor::event("time") % monitor::duration(frame_number_/format_desc.fps)
\r
156 % monitor::duration(static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)/format_desc.fps)
\r
157 << monitor::event("frame") % static_cast<int64_t>(frame_number_)
\r
158 % static_cast<int64_t>((static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)));
\r
160 foreground_event_subject_ << monitor::event("type") % foreground_->name();
\r
161 background_event_subject_ << monitor::event("type") % background_->name();
\r
167 CASPAR_LOG_CURRENT_EXCEPTION();
\r
169 return core::draw_frame::empty();
\r
173 boost::property_tree::wptree info() const
\r
175 boost::property_tree::wptree info;
\r
176 info.add(L"status", is_paused_ ? L"paused" : (foreground_ == frame_producer::empty() ? L"stopped" : L"playing"));
\r
177 info.add(L"auto_delta", (auto_play_delta_ ? boost::lexical_cast<std::wstring>(*auto_play_delta_) : L"null"));
\r
178 info.add(L"frame-number", frame_number_);
\r
180 auto nb_frames = foreground_->nb_frames();
\r
182 info.add(L"nb_frames", nb_frames == std::numeric_limits<int64_t>::max() ? -1 : nb_frames);
\r
183 info.add(L"frames-left", nb_frames == std::numeric_limits<int64_t>::max() ? -1 : (foreground_->nb_frames() - frame_number_ - (auto_play_delta_ ? *auto_play_delta_ : 0)));
\r
184 info.add_child(L"producer", foreground_->info());
\r
185 info.add_child(L"background.producer", background_->info());
\r
190 layer::layer(int index) : impl_(new impl(index)){}
\r
191 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}
\r
192 layer& layer::operator=(layer&& other)
\r
197 void layer::swap(layer& other)
\r
199 impl_.swap(other.impl_);
\r
201 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);}
\r
202 void layer::play(){impl_->play();}
\r
203 void layer::pause(){impl_->pause();}
\r
204 void layer::stop(){impl_->stop();}
\r
205 draw_frame layer::receive(frame_producer::flags flags, const video_format_desc& format_desc) {return impl_->receive(flags, format_desc);}
\r
206 spl::shared_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}
\r
207 spl::shared_ptr<frame_producer> layer::background() const { return impl_->background_;}
\r
208 boost::property_tree::wptree layer::info() const{return impl_->info();}
\r
209 void layer::subscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.subscribe(o);}
\r
210 void layer::unsubscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.unsubscribe(o);}
\r