]> git.sesse.net Git - casparcg/blob - core/producer/layer.cpp
2.0.2: Improved INFO.
[casparcg] / core / producer / layer.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */\r
20 \r
21 #include "../stdafx.h"\r
22 \r
23 #include "layer.h"\r
24 \r
25 #include "frame_producer.h"\r
26 #include "frame/basic_frame.h"\r
27 \r
28 #include <boost/property_tree/ptree.hpp>\r
29 \r
30 namespace caspar { namespace core {\r
31         \r
32 struct layer::implementation\r
33 {                               \r
34         safe_ptr<frame_producer>        foreground_;\r
35         safe_ptr<frame_producer>        background_;\r
36         int64_t                                         frame_number_;\r
37         int                                                     auto_play_delta_;\r
38         bool                                            is_paused_;\r
39 \r
40 public:\r
41         implementation() \r
42                 : foreground_(frame_producer::empty())\r
43                 , background_(frame_producer::empty())\r
44                 , frame_number_(0)\r
45                 , auto_play_delta_(-1)\r
46                 , is_paused_(false)\r
47         {\r
48         }\r
49         \r
50         void pause()\r
51         {\r
52                 is_paused_ = true;\r
53         }\r
54 \r
55         void resume()\r
56         {\r
57                 is_paused_ = false;\r
58         }\r
59 \r
60         void load(const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
61         {               \r
62                 background_              = producer;\r
63                 auto_play_delta_ = auto_play_delta;\r
64 \r
65                 if(preview) // Play the first frame and pause.\r
66                 {                       \r
67                         play();\r
68                         receive();\r
69                         pause();\r
70                 }\r
71         }\r
72         \r
73         void play()\r
74         {                       \r
75                 if(background_ != frame_producer::empty())\r
76                 {\r
77                         background_->set_leading_producer(foreground_);\r
78                         \r
79                         foreground_                     = background_;\r
80                         background_                     = frame_producer::empty();\r
81                         frame_number_           = 0;\r
82                         auto_play_delta_        = -1;   \r
83                 }\r
84 \r
85                 is_paused_                      = false;\r
86         }\r
87         \r
88         void stop()\r
89         {\r
90                 foreground_                     = frame_producer::empty();\r
91                 background_                     = background_;\r
92                 frame_number_           = 0;\r
93                 auto_play_delta_        = -1;\r
94 \r
95                 is_paused_                      = true;\r
96         }\r
97                 \r
98         safe_ptr<basic_frame> receive()\r
99         {               \r
100                 try\r
101                 {\r
102                         if(is_paused_)\r
103                                 return disable_audio(foreground_->last_frame());\r
104                 \r
105                         auto frame = receive_and_follow(foreground_, frame_producer::NO_HINT);\r
106                         if(frame == core::basic_frame::late())\r
107                                 return foreground_->last_frame();\r
108 \r
109                         auto frames_left = foreground_->nb_frames() - (++frame_number_) - auto_play_delta_;\r
110                         if(auto_play_delta_ > -1 && frames_left < 1)\r
111                         {\r
112                                 play();\r
113                                 return receive();\r
114                         }\r
115                                 \r
116                         return frame;\r
117                 }\r
118                 catch(...)\r
119                 {\r
120                         CASPAR_LOG_CURRENT_EXCEPTION();\r
121                         stop();\r
122                         return core::basic_frame::empty();\r
123                 }\r
124         }\r
125 \r
126         layer_status status() const\r
127         {\r
128                 layer_status status;\r
129                 status.foreground                       = foreground_->print();\r
130                 status.background                       = background_->print();\r
131                 status.is_paused                        = is_paused_;\r
132                 status.nb_frames                        = foreground_->nb_frames();\r
133                 status.frame_number                     = std::max(frame_number_, foreground_->frame_number());\r
134                 status.file_nb_frames           = foreground_->file_nb_frames();\r
135                 status.file_frame_number        = foreground_->file_frame_number();\r
136 \r
137                 return status;\r
138         }\r
139 \r
140         boost::unique_future<std::wstring> call(bool foreground, const std::wstring& param)\r
141         {\r
142                 return (foreground ? foreground_ : background_)->call(param);\r
143         }\r
144 \r
145         bool empty() const\r
146         {\r
147                 return background_ == core::frame_producer::empty() && foreground_ == core::frame_producer::empty();\r
148         }\r
149 \r
150         boost::property_tree::wptree info() const\r
151         {\r
152                 boost::property_tree::wptree info;\r
153                 info.add_child(L"layer.foreground", foreground_->info());\r
154                 info.add_child(L"layer.background", foreground_->info());\r
155                 info.add(L"layer.status",     is_paused_ ? L"paused" : (foreground_ == frame_producer::empty() ? L"stopped" : L"playing"));\r
156                 info.add(L"layer.auto_delta", auto_play_delta_);\r
157                 return info;\r
158         }\r
159 };\r
160 \r
161 layer::layer() : impl_(new implementation()){}\r
162 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}\r
163 layer& layer::operator=(layer&& other)\r
164 {\r
165         impl_ = std::move(other.impl_);\r
166         return *this;\r
167 }\r
168 layer::layer(const layer& other) : impl_(new implementation(*other.impl_)){}\r
169 layer& layer::operator=(const layer& other)\r
170 {\r
171         layer temp(other);\r
172         temp.swap(*this);\r
173         return *this;\r
174 }\r
175 void layer::swap(layer& other)\r
176 {       \r
177         impl_.swap(other.impl_);\r
178 }\r
179 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
180 void layer::play(){impl_->play();}\r
181 void layer::pause(){impl_->pause();}\r
182 void layer::stop(){impl_->stop();}\r
183 bool layer::is_paused() const{return impl_->is_paused_;}\r
184 int64_t layer::frame_number() const{return impl_->frame_number_;}\r
185 layer_status layer::status() const {return impl_->status();}\r
186 safe_ptr<basic_frame> layer::receive() {return impl_->receive();}\r
187 safe_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}\r
188 safe_ptr<frame_producer> layer::background() const { return impl_->background_;}\r
189 bool layer::empty() const {return impl_->empty();}\r
190 boost::unique_future<std::wstring> layer::call(bool foreground, const std::wstring& param){return impl_->call(foreground, param);}\r
191 boost::property_tree::wptree layer::info() const{return impl_->info();}\r
192 }}