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 spl::shared_ptr<monitor::subject> event_subject_;
\r
40 spl::shared_ptr<monitor::subject> foreground_event_subject_;
\r
41 spl::shared_ptr<monitor::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_(new monitor::subject(monitor::path("layer") % index))
\r
51 , foreground_event_subject_(new monitor::subject("foreground"))
\r
52 , background_event_subject_(new monitor::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, 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
80 if(auto_play_delta_ && foreground_ == frame_producer::empty())
\r
86 if(background_ != frame_producer::empty())
\r
88 background_->set_leading_producer(foreground_);
\r
90 background_->unsubscribe(background_event_subject_);
\r
91 foreground_->unsubscribe(foreground_event_subject_);
\r
93 foreground_ = std::move(background_);
\r
94 background_ = std::move(frame_producer::empty());
\r
96 foreground_->subscribe(foreground_event_subject_);
\r
99 auto_play_delta_.reset();
\r
107 foreground_->unsubscribe(foreground_event_subject_);
\r
109 foreground_ = std::move(frame_producer::empty());
\r
112 auto_play_delta_.reset();
\r
117 spl::shared_ptr<draw_frame> receive(frame_producer::flags flags, const video_format_desc& format_desc)
\r
122 return draw_frame::mute(foreground_->last_frame());
\r
124 auto frame = foreground_->receive(flags.value());
\r
125 if(frame == core::draw_frame::late())
\r
126 return foreground_->last_frame();
\r
130 if(auto_play_delta_)
\r
132 auto frames_left = static_cast<int64_t>(foreground_->nb_frames()) - frame_number_ - static_cast<int64_t>(*auto_play_delta_);
\r
133 if(frames_left < 1)
\r
136 return receive(flags, format_desc);
\r
140 *event_subject_ << monitor::event("state") % u8(is_paused_ ? L"paused" : (foreground_ == frame_producer::empty() ? L"stopped" : L"playing"))
\r
141 << monitor::event("time") % monitor::duration(frame_number_/format_desc.fps)
\r
142 % monitor::duration(static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)/format_desc.fps)
\r
143 << monitor::event("frame") % static_cast<int64_t>(frame_number_)
\r
144 % static_cast<int64_t>((static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)));
\r
146 *foreground_event_subject_ << monitor::event("type") % u8(foreground_->name());
\r
147 *background_event_subject_ << monitor::event("type") % u8(foreground_->name());
\r
153 CASPAR_LOG_CURRENT_EXCEPTION();
\r
155 return core::draw_frame::empty();
\r
159 boost::property_tree::wptree info() const
\r
161 boost::property_tree::wptree info;
\r
162 info.add(L"status", is_paused_ ? L"paused" : (foreground_ == frame_producer::empty() ? L"stopped" : L"playing"));
\r
163 info.add(L"auto_delta", (auto_play_delta_ ? boost::lexical_cast<std::wstring>(*auto_play_delta_) : L"null"));
\r
164 info.add(L"frame-number", frame_number_);
\r
166 auto nb_frames = foreground_->nb_frames();
\r
168 info.add(L"nb_frames", nb_frames == std::numeric_limits<int64_t>::max() ? -1 : nb_frames);
\r
169 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
170 info.add_child(L"foreground.producer", foreground_->info());
\r
171 info.add_child(L"background.producer", background_->info());
\r
176 layer::layer(int index) : impl_(new impl(index)){}
\r
177 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}
\r
178 layer& layer::operator=(layer&& other)
\r
183 void layer::swap(layer& other)
\r
185 impl_.swap(other.impl_);
\r
187 void layer::load(spl::shared_ptr<frame_producer> frame_producer, const boost::optional<int32_t>& auto_play_delta){return impl_->load(std::move(frame_producer), auto_play_delta);}
\r
188 void layer::play(){impl_->play();}
\r
189 void layer::pause(){impl_->pause();}
\r
190 void layer::stop(){impl_->stop();}
\r
191 spl::shared_ptr<draw_frame> layer::receive(frame_producer::flags flags, const video_format_desc& format_desc) {return impl_->receive(flags, format_desc);}
\r
192 spl::shared_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}
\r
193 spl::shared_ptr<frame_producer> layer::background() const { return impl_->background_;}
\r
194 boost::property_tree::wptree layer::info() const{return impl_->info();}
\r
195 void layer::subscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_->subscribe(o);}
\r
196 void layer::unsubscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_->unsubscribe(o);}
\r