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