]> git.sesse.net Git - casparcg/blob - core/producer/layer.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / core / producer / layer.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@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 \r
28 #include "../frame/draw_frame.h"\r
29 #include "../frame/frame_transform.h"\r
30 \r
31 #include <boost/optional.hpp>\r
32 #include <boost/thread/future.hpp>\r
33 \r
34 namespace caspar { namespace core {\r
35 \r
36 struct layer::impl\r
37 {                               \r
38         safe_ptr<frame_producer>        foreground_;\r
39         safe_ptr<frame_producer>        background_;\r
40         int64_t                                         frame_number_;\r
41         boost::optional<int32_t>        auto_play_delta_;\r
42         bool                                            is_paused_;\r
43 \r
44 public:\r
45         impl() \r
46                 : foreground_(frame_producer::empty())\r
47                 , background_(frame_producer::empty())\r
48                 , frame_number_(0)\r
49                 , is_paused_(false)\r
50         {\r
51         }\r
52         \r
53         void pause()\r
54         {\r
55                 is_paused_ = true;\r
56         }\r
57 \r
58         void resume()\r
59         {\r
60                 is_paused_ = false;\r
61         }\r
62 \r
63         void load(const safe_ptr<frame_producer>& producer, const boost::optional<int32_t>& auto_play_delta)\r
64         {               \r
65                 background_              = producer;\r
66                 auto_play_delta_ = auto_play_delta;\r
67 \r
68                 if(auto_play_delta_ && foreground_ == frame_producer::empty())\r
69                         play();\r
70         }\r
71         \r
72         void play()\r
73         {                       \r
74                 if(background_ != frame_producer::empty())\r
75                 {\r
76                         background_->set_leading_producer(foreground_);\r
77                         \r
78                         foreground_                     = background_;\r
79                         background_                     = frame_producer::empty();\r
80                         frame_number_           = 0;\r
81                         auto_play_delta_.reset();\r
82                 }\r
83 \r
84                 is_paused_                      = false;\r
85         }\r
86         \r
87         void stop()\r
88         {\r
89                 foreground_                     = frame_producer::empty();\r
90                 background_                     = background_;\r
91                 frame_number_           = 0;\r
92                 auto_play_delta_.reset();\r
93 \r
94                 is_paused_                      = true;\r
95         }\r
96                 \r
97         safe_ptr<draw_frame> receive(frame_producer::flags flags)\r
98         {               \r
99                 try\r
100                 {\r
101                         if(is_paused_)\r
102                                 return draw_frame::silence(foreground_->last_frame());\r
103                 \r
104                         auto frame = receive_and_follow(foreground_, flags.value());\r
105                         if(frame == core::draw_frame::late())\r
106                                 return draw_frame::silence(foreground_->last_frame());\r
107 \r
108                         if(auto_play_delta_)\r
109                         {\r
110                                 auto frames_left = static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(++frame_number_) - static_cast<int64_t>(*auto_play_delta_);\r
111                                 if(frames_left < 1)\r
112                                 {\r
113                                         play();\r
114                                         return receive(flags);\r
115                                 }\r
116                         }\r
117                                 \r
118                         return frame;\r
119                 }\r
120                 catch(...)\r
121                 {\r
122                         CASPAR_LOG_CURRENT_EXCEPTION();\r
123                         stop();\r
124                         return core::draw_frame::empty();\r
125                 }\r
126         }\r
127         \r
128         bool empty() const\r
129         {\r
130                 return background_ == core::frame_producer::empty() && foreground_ == core::frame_producer::empty();\r
131         }\r
132 \r
133         boost::property_tree::wptree info() const\r
134         {\r
135                 boost::property_tree::wptree info;\r
136                 info.add(L"status",             is_paused_ ? L"paused" : (foreground_ == frame_producer::empty() ? L"stopped" : L"playing"));\r
137                 info.add(L"auto_delta", (auto_play_delta_ ? boost::lexical_cast<std::wstring>(*auto_play_delta_) : L"null"));\r
138                 info.add(L"frame-number", frame_number_);\r
139 \r
140                 auto nb_frames = foreground_->nb_frames();\r
141 \r
142                 info.add(L"nb_frames",   nb_frames == std::numeric_limits<int64_t>::max() ? -1 : nb_frames);\r
143                 info.add(L"frames-left", nb_frames == std::numeric_limits<int64_t>::max() ? -1 : (foreground_->nb_frames() - frame_number_ - (auto_play_delta_ ? *auto_play_delta_ : 0)));\r
144                 info.add_child(L"foreground.producer", foreground_->info());\r
145                 info.add_child(L"background.producer", background_->info());\r
146                 return info;\r
147         }\r
148 };\r
149 \r
150 layer::layer() : impl_(new impl()){}\r
151 layer::layer(const layer& other) : impl_(new impl(*other.impl_)){}\r
152 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}\r
153 layer& layer::operator=(layer other)\r
154 {\r
155         other.swap(*this);\r
156         return *this;\r
157 }\r
158 void layer::swap(layer& other)\r
159 {       \r
160         impl_.swap(other.impl_);\r
161 }\r
162 void layer::load(const safe_ptr<frame_producer>& frame_producer, const boost::optional<int32_t>& auto_play_delta){return impl_->load(frame_producer, auto_play_delta);} \r
163 void layer::play(){impl_->play();}\r
164 void layer::pause(){impl_->pause();}\r
165 void layer::stop(){impl_->stop();}\r
166 bool layer::is_paused() const{return impl_->is_paused_;}\r
167 int64_t layer::frame_number() const{return impl_->frame_number_;}\r
168 safe_ptr<draw_frame> layer::receive(frame_producer::flags flags) {return impl_->receive(flags);}\r
169 safe_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}\r
170 safe_ptr<frame_producer> layer::background() const { return impl_->background_;}\r
171 bool layer::empty() const {return impl_->empty();}\r
172 boost::property_tree::wptree layer::info() const{return impl_->info();}\r
173 }}