]> git.sesse.net Git - casparcg/blob - core/producer/layer.cpp
Updated CHANGES.txt
[casparcg] / core / producer / layer.cpp
1 /*\r
2 * Copyright 2013 Sveriges Television AB http://casparcg.com/\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\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
10 *\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
15 *\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
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../stdafx.h"\r
23 \r
24 #include "layer.h"\r
25 \r
26 #include "frame_producer.h"\r
27 #include "frame/basic_frame.h"\r
28 \r
29 #include <boost/property_tree/ptree.hpp>\r
30 \r
31 namespace caspar { namespace core {\r
32         \r
33 struct layer::implementation\r
34 {                               \r
35         safe_ptr<frame_producer>        foreground_;\r
36         safe_ptr<frame_producer>        background_;\r
37         int64_t                                         frame_number_;\r
38         int32_t                                         auto_play_delta_;\r
39         bool                                            is_paused_;\r
40         int64_t                                         current_frame_age_;\r
41         safe_ptr<monitor::subject>      monitor_subject_;\r
42 \r
43 public:\r
44         implementation(int index) \r
45                 : foreground_(frame_producer::empty())\r
46                 , background_(frame_producer::empty())\r
47                 , frame_number_(0)\r
48                 , auto_play_delta_(-1)\r
49                 , is_paused_(false)\r
50                 , monitor_subject_(make_safe<monitor::subject>("/layer/" + boost::lexical_cast<std::string>(index)))\r
51         {\r
52         }\r
53         \r
54         void pause()\r
55         {\r
56                 is_paused_ = true;\r
57         }\r
58 \r
59         void resume()\r
60         {\r
61                 is_paused_ = false;\r
62         }\r
63 \r
64         void load(const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
65         {               \r
66                 background_              = producer;\r
67                 auto_play_delta_ = auto_play_delta;\r
68 \r
69                 if(auto_play_delta_ > -1 && foreground_ == frame_producer::empty())\r
70                         play();\r
71 \r
72                 if(preview) // Play the first frame and pause.\r
73                 {                       \r
74                         play();\r
75                         receive(frame_producer::NO_HINT);\r
76                         pause();\r
77                 }\r
78         }\r
79         \r
80         void play()\r
81         {                       \r
82                 if(background_ != frame_producer::empty())\r
83                 {\r
84                         background_->set_leading_producer(foreground_);\r
85                         \r
86                         set_foreground(background_);\r
87 \r
88                         background_                     = frame_producer::empty();\r
89                         frame_number_           = 0;\r
90                         auto_play_delta_        = -1;   \r
91                 }\r
92 \r
93                 is_paused_                      = false;\r
94         }\r
95         \r
96         void stop()\r
97         {\r
98                 set_foreground(frame_producer::empty());\r
99 \r
100                 background_                     = background_;\r
101                 frame_number_           = 0;\r
102                 auto_play_delta_        = -1;\r
103 \r
104                 is_paused_                      = false;\r
105         }\r
106                 \r
107         safe_ptr<basic_frame> receive(int hints)\r
108         {               \r
109                 try\r
110                 {\r
111                         *monitor_subject_ << monitor::message("/paused") % is_paused_;\r
112 \r
113                         if(is_paused_)\r
114                         {\r
115                                 if(foreground_->last_frame() == basic_frame::empty())\r
116                                         foreground_->receive(frame_producer::NO_HINT);\r
117 \r
118                                 return disable_audio(foreground_->last_frame());\r
119                         }\r
120 \r
121                         auto foreground = foreground_;\r
122 \r
123                         auto frame = receive_and_follow(foreground, hints);\r
124 \r
125                         if(foreground != foreground_)\r
126                                 set_foreground(foreground);\r
127 \r
128                         if(frame == core::basic_frame::late())\r
129                                 return foreground_->last_frame();\r
130 \r
131                         auto frames_left = static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(++frame_number_) - static_cast<int64_t>(auto_play_delta_);\r
132                         if(auto_play_delta_ > -1 && frames_left < 1)\r
133                         {\r
134                                 play();\r
135                                 return receive(hints);\r
136                         }\r
137 \r
138                         current_frame_age_ = frame->get_and_record_age_millis();\r
139 \r
140                         return frame;\r
141                 }\r
142                 catch(...)\r
143                 {\r
144                         CASPAR_LOG_CURRENT_EXCEPTION();\r
145                         stop();\r
146                         return core::basic_frame::empty();\r
147                 }\r
148         }\r
149 \r
150         boost::unique_future<std::wstring> call(bool foreground, const std::wstring& param)\r
151         {\r
152                 return (foreground ? foreground_ : background_)->call(param);\r
153         }\r
154 \r
155         bool empty() const\r
156         {\r
157                 return background_ == core::frame_producer::empty() && foreground_ == core::frame_producer::empty();\r
158         }\r
159 \r
160         boost::property_tree::wptree info() const\r
161         {\r
162                 boost::property_tree::wptree info;\r
163                 info.add(L"status",             is_paused_ ? L"paused" : (foreground_ == frame_producer::empty() ? L"stopped" : L"playing"));\r
164                 info.add(L"auto_delta", auto_play_delta_);\r
165                 info.add(L"frame-number", frame_number_);\r
166 \r
167                 auto nb_frames = foreground_->nb_frames();\r
168 \r
169                 info.add(L"nb_frames",   nb_frames == std::numeric_limits<int64_t>::max() ? -1 : nb_frames);\r
170                 info.add(L"frames-left", nb_frames == std::numeric_limits<int64_t>::max() ? -1 : (foreground_->nb_frames() - frame_number_ - auto_play_delta_));\r
171                 info.add(L"frame-age", current_frame_age_);\r
172                 info.add_child(L"foreground.producer", foreground_->info());\r
173                 info.add_child(L"background.producer", background_->info());\r
174                 return info;\r
175         }\r
176 \r
177         boost::property_tree::wptree delay_info() const\r
178         {\r
179                 boost::property_tree::wptree info;\r
180                 info.add(L"producer", foreground_->print());\r
181                 info.add(L"frame-age", current_frame_age_);\r
182                 return info;\r
183         }\r
184 \r
185         void set_foreground(safe_ptr<core::frame_producer> producer)\r
186         {\r
187                 foreground_->monitor_output().detach_parent();\r
188                 foreground_                     = producer;\r
189                 foreground_->monitor_output().attach_parent(monitor_subject_);\r
190         }\r
191 };\r
192 \r
193 layer::layer(int index) : impl_(new implementation(index)){}\r
194 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}\r
195 layer& layer::operator=(layer&& other)\r
196 {\r
197         impl_ = std::move(other.impl_);\r
198         return *this;\r
199 }\r
200 void layer::swap(layer& other)\r
201 {       \r
202         impl_.swap(other.impl_);\r
203 }\r
204 void layer::load(const safe_ptr<frame_producer>& frame_producer, bool preview, int auto_play_delta){return impl_->load(frame_producer, preview, auto_play_delta);}      \r
205 void layer::play(){impl_->play();}\r
206 void layer::pause(){impl_->pause();}\r
207 void layer::resume(){impl_->resume();}\r
208 void layer::stop(){impl_->stop();}\r
209 bool layer::is_paused() const{return impl_->is_paused_;}\r
210 int64_t layer::frame_number() const{return impl_->frame_number_;}\r
211 safe_ptr<basic_frame> layer::receive(int hints) {return impl_->receive(hints);}\r
212 safe_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}\r
213 safe_ptr<frame_producer> layer::background() const { return impl_->background_;}\r
214 bool layer::empty() const {return impl_->empty();}\r
215 boost::unique_future<std::wstring> layer::call(bool foreground, const std::wstring& param){return impl_->call(foreground, param);}\r
216 boost::property_tree::wptree layer::info() const{return impl_->info();}\r
217 boost::property_tree::wptree layer::delay_info() const{return impl_->delay_info();}\r
218 monitor::subject& layer::monitor_output(){return *impl_->monitor_subject_;}\r
219 }}