]> git.sesse.net Git - casparcg/blob - core/producer/layer.cpp
Merged INFO DELAY from 2.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         spl::shared_ptr<monitor::subject>       monitor_subject_;
40         spl::shared_ptr<frame_producer>         foreground_                     = frame_producer::empty();
41         spl::shared_ptr<frame_producer>         background_                     = frame_producer::empty();;
42         boost::optional<int32_t>                        auto_play_delta_;
43         bool                                                            is_paused_                      = false;
44         int64_t                                                         current_frame_age_      = 0;
45
46 public:
47         impl(int index) 
48                 : monitor_subject_(spl::make_shared<monitor::subject>(
49                                 "/layer/" + boost::lexical_cast<std::string>(index)))
50 //              , foreground_event_subject_("")
51 //              , background_event_subject_("background")
52         {
53 //              foreground_event_subject_.subscribe(event_subject_);
54 //              background_event_subject_.subscribe(event_subject_);
55         }
56
57         void set_foreground(spl::shared_ptr<frame_producer> producer)
58         {
59                 foreground_->monitor_output().detach_parent();
60                 foreground_ = std::move(producer);
61                 foreground_->monitor_output().attach_parent(monitor_subject_);
62         }
63
64         void pause()
65         {
66                 foreground_->paused(true);
67                 is_paused_ = true;
68         }
69
70         void resume()
71         {
72                 foreground_->paused(false);
73                 is_paused_ = false;
74         }
75
76         void load(spl::shared_ptr<frame_producer> producer, bool preview, const boost::optional<int32_t>& auto_play_delta)
77         {               
78 //              background_->unsubscribe(background_event_subject_);
79                 background_ = std::move(producer);
80 //              background_->subscribe(background_event_subject_);
81
82                 auto_play_delta_ = auto_play_delta;
83
84                 if(preview)
85                 {
86                         play();
87                         foreground_->paused(true);
88                         is_paused_ = true;
89                 }
90
91                 if(auto_play_delta_ && foreground_ == frame_producer::empty())
92                         play();
93         }
94         
95         void play()
96         {                       
97                 if(background_ != frame_producer::empty())
98                 {
99                         background_->leading_producer(foreground_);
100
101                         set_foreground(background_);
102                         background_ = std::move(frame_producer::empty());
103
104                         auto_play_delta_.reset();
105                 }
106
107                 foreground_->paused(false);
108                 is_paused_ = false;
109         }
110         
111         void stop()
112         {
113                 set_foreground(frame_producer::empty());
114
115                 auto_play_delta_.reset();
116         }
117                 
118         draw_frame receive(const video_format_desc& format_desc)
119         {               
120                 try
121                 {               
122                         *monitor_subject_ << monitor::message("/paused") % is_paused_;
123
124                         auto frame = foreground_->receive();
125                         
126                         if(frame == core::draw_frame::late())
127                                 return foreground_->last_frame();
128                                                 
129                         if(auto_play_delta_)
130                         {
131                                 auto frames_left = static_cast<int64_t>(foreground_->nb_frames()) - foreground_->frame_number() - static_cast<int64_t>(*auto_play_delta_);
132                                 if(frames_left < 1)
133                                 {
134                                         play();
135                                         return receive(format_desc);
136                                 }
137                         }
138
139                         //event_subject_        << monitor::event("time")       % monitor::duration(foreground_->frame_number()/format_desc.fps)
140                         //                                                                                      % monitor::duration(static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)/format_desc.fps)
141                         //                              << monitor::event("frame")      % static_cast<int64_t>(foreground_->frame_number())
142                         //                                                                                      % static_cast<int64_t>((static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)));
143
144                         //foreground_event_subject_ << monitor::event("type") % foreground_->name();
145                         //background_event_subject_ << monitor::event("type") % background_->name();
146
147                         current_frame_age_ = frame.get_and_record_age_millis();
148                                 
149                         return frame;
150                 }
151                 catch(...)
152                 {
153                         CASPAR_LOG_CURRENT_EXCEPTION();
154                         stop();
155                         return core::draw_frame::empty();
156                 }
157         }
158         
159         boost::property_tree::wptree info() const
160         {
161                 boost::property_tree::wptree info;
162                 info.add(L"auto_delta", (auto_play_delta_ ? boost::lexical_cast<std::wstring>(*auto_play_delta_) : L"null"));
163                 info.add(L"frame-number", foreground_->frame_number());
164
165                 auto nb_frames = foreground_->nb_frames();
166
167                 info.add(L"nb_frames",   nb_frames == std::numeric_limits<int64_t>::max() ? -1 : nb_frames);
168                 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)));
169                 info.add(L"frame-age", current_frame_age_);
170                 info.add_child(L"producer", foreground_->info());
171                 info.add_child(L"background.producer", background_->info());
172                 return info;
173         }
174
175         boost::property_tree::wptree delay_info() const
176         {
177                 boost::property_tree::wptree info;
178                 info.add(L"producer", foreground_->print());
179                 info.add(L"frame-age", current_frame_age_);
180                 return info;
181         }
182
183         void on_interaction(const interaction_event::ptr& event)
184         {
185                 foreground_->on_interaction(event);
186         }
187         
188         bool collides(double x, double y) const
189         {
190                 return foreground_->collides(x, y);
191         }
192 };
193
194 layer::layer(int index) : impl_(new impl(index)){}
195 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}
196 layer& layer::operator=(layer&& other)
197 {
198         other.swap(*this);
199         return *this;
200 }
201 void layer::swap(layer& other)
202 {       
203         impl_.swap(other.impl_);
204 }
205 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);}       
206 void layer::play(){impl_->play();}
207 void layer::pause(){impl_->pause();}
208 void layer::resume(){impl_->resume();}
209 void layer::stop(){impl_->stop();}
210 draw_frame layer::receive(const video_format_desc& format_desc) {return impl_->receive(format_desc);}
211 spl::shared_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}
212 spl::shared_ptr<frame_producer> layer::background() const { return impl_->background_;}
213 boost::property_tree::wptree layer::info() const{return impl_->info();}
214 boost::property_tree::wptree layer::delay_info() const{return impl_->delay_info();}
215 monitor::subject& layer::monitor_output() {return *impl_->monitor_subject_;}
216 void layer::on_interaction(const interaction_event::ptr& event) { impl_->on_interaction(event); }
217 bool layer::collides(double x, double y) const { return impl_->collides(x, y); }
218 }}