]> git.sesse.net Git - casparcg/blob - core/producer/layer.cpp
Merge branch '2.1.0' of https://github.com/CasparCG/Server into 2.1.0
[casparcg] / core / producer / layer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../stdafx.h"
23
24 #include "layer.h"
25
26 #include "frame_producer.h"
27
28 #include "../video_format.h"
29 #include "../frame/draw_frame.h"
30 #include "../frame/frame_transform.h"
31
32 #include <boost/optional.hpp>
33 #include <boost/thread/future.hpp>
34
35 namespace caspar { namespace core {
36
37 struct layer::impl
38 {                               
39         monitor::subject                                        monitor_subject_;
40         spl::shared_ptr<frame_producer>         foreground_;
41         spl::shared_ptr<frame_producer>         background_;
42         boost::optional<int32_t>                        auto_play_delta_;
43
44 public:
45         impl(int index) 
46                 : monitor_subject_("/layer/" + boost::lexical_cast<std::string>(index))
47 //              , foreground_event_subject_("")
48 //              , background_event_subject_("background")
49                 , foreground_(frame_producer::empty())
50                 , background_(frame_producer::empty())
51         {
52 //              foreground_event_subject_.subscribe(event_subject_);
53 //              background_event_subject_.subscribe(event_subject_);
54         }
55
56         void set_foreground(spl::shared_ptr<frame_producer> producer)
57         {
58                 foreground_->monitor_output().unlink_target(&monitor_subject_);
59                 foreground_ = std::move(producer);
60                 foreground_->monitor_output().link_target(&monitor_subject_);
61         }
62
63         void pause()
64         {
65                 foreground_->paused(true);
66         }
67         
68         void load(spl::shared_ptr<frame_producer> producer, bool preview, const boost::optional<int32_t>& auto_play_delta)
69         {               
70 //              background_->unsubscribe(background_event_subject_);
71                 background_ = std::move(producer);
72 //              background_->subscribe(background_event_subject_);
73
74                 auto_play_delta_ = auto_play_delta;
75
76                 if(preview)
77                 {
78                         play();
79                         foreground_->paused(true);
80                 }
81
82                 if(auto_play_delta_ && foreground_ == frame_producer::empty())
83                         play();
84         }
85         
86         void play()
87         {                       
88                 if(background_ != frame_producer::empty())
89                 {
90                         background_->leading_producer(foreground_);
91
92                         set_foreground(background_);
93                         background_ = std::move(frame_producer::empty());
94
95                         auto_play_delta_.reset();
96                 }
97
98                 foreground_->paused(false);
99         }
100         
101         void stop()
102         {
103                 set_foreground(frame_producer::empty());
104
105                 auto_play_delta_.reset();
106         }
107                 
108         draw_frame receive(const video_format_desc& format_desc)
109         {               
110                 try
111                 {               
112                         auto frame = foreground_->receive();
113                         
114                         if(frame == core::draw_frame::late())
115                                 return foreground_->last_frame();
116                                                 
117                         if(auto_play_delta_)
118                         {
119                                 auto frames_left = static_cast<int64_t>(foreground_->nb_frames()) - foreground_->frame_number() - static_cast<int64_t>(*auto_play_delta_);
120                                 if(frames_left < 1)
121                                 {
122                                         play();
123                                         return receive(format_desc);
124                                 }
125                         }
126
127                         //event_subject_        << monitor::event("time")       % monitor::duration(foreground_->frame_number()/format_desc.fps)
128                         //                                                                                      % monitor::duration(static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)/format_desc.fps)
129                         //                              << monitor::event("frame")      % static_cast<int64_t>(foreground_->frame_number())
130                         //                                                                                      % static_cast<int64_t>((static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)));
131
132                         //foreground_event_subject_ << monitor::event("type") % foreground_->name();
133                         //background_event_subject_ << monitor::event("type") % background_->name();
134                                 
135                         return frame;
136                 }
137                 catch(...)
138                 {
139                         CASPAR_LOG_CURRENT_EXCEPTION();
140                         stop();
141                         return core::draw_frame::empty();
142                 }
143         }
144         
145         boost::property_tree::wptree info() const
146         {
147                 boost::property_tree::wptree info;
148                 info.add(L"auto_delta", (auto_play_delta_ ? boost::lexical_cast<std::wstring>(*auto_play_delta_) : L"null"));
149                 info.add(L"frame-number", foreground_->frame_number());
150
151                 auto nb_frames = foreground_->nb_frames();
152
153                 info.add(L"nb_frames",   nb_frames == std::numeric_limits<int64_t>::max() ? -1 : nb_frames);
154                 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)));
155                 info.add_child(L"producer", foreground_->info());
156                 info.add_child(L"background.producer", background_->info());
157                 return info;
158         }
159
160         void on_interaction(const interaction_event::ptr& event)
161         {
162                 foreground_->on_interaction(event);
163         }
164         
165         bool collides(double x, double y) const
166         {
167                 return foreground_->collides(x, y);
168         }
169 };
170
171 layer::layer(int index) : impl_(new impl(index)){}
172 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}
173 layer& layer::operator=(layer&& other)
174 {
175         other.swap(*this);
176         return *this;
177 }
178 void layer::swap(layer& other)
179 {       
180         impl_.swap(other.impl_);
181 }
182 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);}       
183 void layer::play(){impl_->play();}
184 void layer::pause(){impl_->pause();}
185 void layer::stop(){impl_->stop();}
186 draw_frame layer::receive(const video_format_desc& format_desc) {return impl_->receive(format_desc);}
187 spl::shared_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}
188 spl::shared_ptr<frame_producer> layer::background() const { return impl_->background_;}
189 boost::property_tree::wptree layer::info() const{return impl_->info();}
190 monitor::source& layer::monitor_output() {return impl_->monitor_subject_;}
191 void layer::on_interaction(const interaction_event::ptr& event) { impl_->on_interaction(event); }
192 bool layer::collides(double x, double y) const { return impl_->collides(x, y); }
193 }}