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 boost::optional<int32_t> auto_play_delta_;
\r
48 : event_subject_(monitor::path("layer") % index)
\r
49 , foreground_event_subject_("")
\r
50 , background_event_subject_("background")
\r
51 , foreground_(frame_producer::empty())
\r
52 , background_(frame_producer::empty())
\r
54 foreground_event_subject_.subscribe(event_subject_);
\r
55 background_event_subject_.subscribe(event_subject_);
\r
60 foreground_->paused(true);
\r
63 void load(spl::shared_ptr<frame_producer> producer, bool preview, const boost::optional<int32_t>& auto_play_delta)
\r
65 background_->unsubscribe(background_event_subject_);
\r
66 background_ = std::move(producer);
\r
67 background_->subscribe(background_event_subject_);
\r
69 auto_play_delta_ = auto_play_delta;
\r
74 foreground_->receive();
\r
75 foreground_->paused(true);
\r
78 if(auto_play_delta_ && foreground_ == frame_producer::empty())
\r
84 if(background_ != frame_producer::empty())
\r
86 background_->leading_producer(foreground_);
\r
88 background_->unsubscribe(background_event_subject_);
\r
89 foreground_->unsubscribe(foreground_event_subject_);
\r
91 foreground_ = std::move(background_);
\r
92 background_ = std::move(frame_producer::empty());
\r
94 foreground_->subscribe(foreground_event_subject_);
\r
96 auto_play_delta_.reset();
\r
99 foreground_->paused(false);
\r
104 foreground_->unsubscribe(foreground_event_subject_);
\r
106 foreground_ = std::move(frame_producer::empty());
\r
108 auto_play_delta_.reset();
\r
111 draw_frame receive(const video_format_desc& format_desc)
\r
115 auto frame = foreground_->receive();
\r
117 if(frame == core::draw_frame::late())
\r
118 return foreground_->last_frame();
\r
120 if(auto_play_delta_)
\r
122 auto frames_left = static_cast<int64_t>(foreground_->nb_frames()) - foreground_->frame_number() - static_cast<int64_t>(*auto_play_delta_);
\r
123 if(frames_left < 1)
\r
126 return receive(format_desc);
\r
130 event_subject_ << monitor::event("time") % monitor::duration(foreground_->frame_number()/format_desc.fps)
\r
131 % monitor::duration(static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)/format_desc.fps)
\r
132 << monitor::event("frame") % static_cast<int64_t>(foreground_->frame_number())
\r
133 % static_cast<int64_t>((static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)));
\r
135 foreground_event_subject_ << monitor::event("type") % foreground_->name();
\r
136 background_event_subject_ << monitor::event("type") % background_->name();
\r
142 CASPAR_LOG_CURRENT_EXCEPTION();
\r
144 return core::draw_frame::empty();
\r
148 boost::property_tree::wptree info() const
\r
150 boost::property_tree::wptree info;
\r
151 info.add(L"auto_delta", (auto_play_delta_ ? boost::lexical_cast<std::wstring>(*auto_play_delta_) : L"null"));
\r
152 info.add(L"frame-number", foreground_->frame_number());
\r
154 auto nb_frames = foreground_->nb_frames();
\r
156 info.add(L"nb_frames", nb_frames == std::numeric_limits<int64_t>::max() ? -1 : nb_frames);
\r
157 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)));
\r
158 info.add_child(L"producer", foreground_->info());
\r
159 info.add_child(L"background.producer", background_->info());
\r
164 layer::layer(int index) : impl_(new impl(index)){}
\r
165 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}
\r
166 layer& layer::operator=(layer&& other)
\r
171 void layer::swap(layer& other)
\r
173 impl_.swap(other.impl_);
\r
175 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
176 void layer::play(){impl_->play();}
\r
177 void layer::pause(){impl_->pause();}
\r
178 void layer::stop(){impl_->stop();}
\r
179 draw_frame layer::receive(const video_format_desc& format_desc) {return impl_->receive(format_desc);}
\r
180 spl::shared_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}
\r
181 spl::shared_ptr<frame_producer> layer::background() const { return impl_->background_;}
\r
182 boost::property_tree::wptree layer::info() const{return impl_->info();}
\r
183 void layer::subscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.subscribe(o);}
\r
184 void layer::unsubscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.unsubscribe(o);}
\r