]> git.sesse.net Git - casparcg/blob - core/producer/layer.cpp
2.1.0: Make auto-play-delta a boost::optional instead of magic number int.
[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 #include "frame/basic_frame.h"\r
28 #include "frame/frame_transform.h"\r
29 \r
30 #include <boost/optional.hpp>\r
31 #include <boost/property_tree/ptree.hpp>\r
32 \r
33 namespace caspar { namespace core {\r
34 \r
35 struct layer::impl\r
36 {                               \r
37         safe_ptr<frame_producer>        foreground_;\r
38         safe_ptr<frame_producer>        background_;\r
39         int64_t                                         frame_number_;\r
40         boost::optional<int32_t>        auto_play_delta_;\r
41         bool                                            is_paused_;\r
42 \r
43 public:\r
44         impl() \r
45                 : foreground_(frame_producer::empty())\r
46                 , background_(frame_producer::empty())\r
47                 , frame_number_(0)\r
48                 , is_paused_(false)\r
49         {\r
50         }\r
51         \r
52         void pause()\r
53         {\r
54                 is_paused_ = true;\r
55         }\r
56 \r
57         void resume()\r
58         {\r
59                 is_paused_ = false;\r
60         }\r
61 \r
62         void load(const safe_ptr<frame_producer>& producer, bool preview, const boost::optional<int32_t>& auto_play_delta)\r
63         {               \r
64                 background_              = producer;\r
65                 auto_play_delta_ = auto_play_delta;\r
66 \r
67                 if(auto_play_delta_ && foreground_ == frame_producer::empty())\r
68                         play();\r
69 \r
70                 if(preview) // Play the first frame and pause.\r
71                 {                       \r
72                         play();\r
73                         receive(frame_producer::NO_FLAG);\r
74                         pause();\r
75                 }\r
76         }\r
77         \r
78         void play()\r
79         {                       \r
80                 if(background_ != frame_producer::empty())\r
81                 {\r
82                         background_->set_leading_producer(foreground_);\r
83                         \r
84                         foreground_                     = background_;\r
85                         background_                     = frame_producer::empty();\r
86                         frame_number_           = 0;\r
87                         auto_play_delta_        = nullptr;      \r
88                 }\r
89 \r
90                 is_paused_                      = false;\r
91         }\r
92         \r
93         void stop()\r
94         {\r
95                 foreground_                     = frame_producer::empty();\r
96                 background_                     = background_;\r
97                 frame_number_           = 0;\r
98                 auto_play_delta_        = nullptr;      \r
99 \r
100                 is_paused_                      = true;\r
101         }\r
102                 \r
103         safe_ptr<basic_frame> receive(int flags)\r
104         {               \r
105                 try\r
106                 {\r
107                         if(is_paused_)\r
108                                 return disable_audio(foreground_->last_frame());\r
109                 \r
110                         auto frame = receive_and_follow(foreground_, flags);\r
111                         if(frame == core::basic_frame::late())\r
112                                 return disable_audio(foreground_->last_frame());\r
113 \r
114                         if(auto_play_delta_)\r
115                         {\r
116                                 auto frames_left = static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(++frame_number_) - static_cast<int64_t>(*auto_play_delta_);\r
117                                 if(frames_left < 1)\r
118                                 {\r
119                                         play();\r
120                                         return receive(flags);\r
121                                 }\r
122                         }\r
123                                 \r
124                         return frame;\r
125                 }\r
126                 catch(...)\r
127                 {\r
128                         CASPAR_LOG_CURRENT_EXCEPTION();\r
129                         stop();\r
130                         return core::basic_frame::empty();\r
131                 }\r
132         }\r
133 \r
134         boost::unique_future<std::wstring> call(bool foreground, const std::wstring& param)\r
135         {\r
136                 return (foreground ? foreground_ : background_)->call(param);\r
137         }\r
138 \r
139         bool empty() const\r
140         {\r
141                 return background_ == core::frame_producer::empty() && foreground_ == core::frame_producer::empty();\r
142         }\r
143 \r
144         boost::property_tree::wptree info() const\r
145         {\r
146                 boost::property_tree::wptree info;\r
147                 info.add(L"status",             is_paused_ ? L"paused" : (foreground_ == frame_producer::empty() ? L"stopped" : L"playing"));\r
148                 info.add(L"auto_delta", auto_play_delta_);\r
149                 info.add(L"frame-number", frame_number_);\r
150 \r
151                 auto nb_frames = foreground_->nb_frames();\r
152 \r
153                 info.add(L"nb_frames",   nb_frames == std::numeric_limits<int64_t>::max() ? -1 : nb_frames);\r
154                 info.add(L"frames-left", nb_frames == std::numeric_limits<int64_t>::max() ? -1 : (foreground_->nb_frames() - frame_number_ - auto_play_delta_));\r
155                 info.add_child(L"foreground.producer", foreground_->info());\r
156                 info.add_child(L"background.producer", background_->info());\r
157                 return info;\r
158         }\r
159 };\r
160 \r
161 layer::layer() : impl_(new impl()){}\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 impl(*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, const boost::optional<int32_t>& 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 safe_ptr<basic_frame> layer::receive(int flags) {return impl_->receive(flags);}\r
186 safe_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}\r
187 safe_ptr<frame_producer> layer::background() const { return impl_->background_;}\r
188 bool layer::empty() const {return impl_->empty();}\r
189 boost::unique_future<std::wstring> layer::call(bool foreground, const std::wstring& param){return impl_->call(foreground, param);}\r
190 boost::property_tree::wptree layer::info() const{return impl_->info();}\r
191 }}