]> git.sesse.net Git - casparcg/blob - core/producer/layer.cpp
2.0.2: Is now locked, only bug fixes should be added. New features go into 2.1.0.
[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 \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         int                                                     auto_play_delta_;\r
39         bool                                            is_paused_;\r
40 \r
41 public:\r
42         implementation() \r
43                 : foreground_(frame_producer::empty())\r
44                 , background_(frame_producer::empty())\r
45                 , frame_number_(0)\r
46                 , auto_play_delta_(-1)\r
47                 , is_paused_(false)\r
48         {\r
49         }\r
50         \r
51         void pause()\r
52         {\r
53                 is_paused_ = true;\r
54         }\r
55 \r
56         void resume()\r
57         {\r
58                 is_paused_ = false;\r
59         }\r
60 \r
61         void load(const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
62         {               \r
63                 background_              = producer;\r
64                 auto_play_delta_ = auto_play_delta;\r
65 \r
66                 if(preview) // Play the first frame and pause.\r
67                 {                       \r
68                         play();\r
69                         receive(frame_producer::NO_HINT);\r
70                         pause();\r
71                 }\r
72         }\r
73         \r
74         void play()\r
75         {                       \r
76                 if(background_ != frame_producer::empty())\r
77                 {\r
78                         background_->set_leading_producer(foreground_);\r
79                         \r
80                         foreground_                     = background_;\r
81                         background_                     = frame_producer::empty();\r
82                         frame_number_           = 0;\r
83                         auto_play_delta_        = -1;   \r
84                 }\r
85 \r
86                 is_paused_                      = false;\r
87         }\r
88         \r
89         void stop()\r
90         {\r
91                 foreground_                     = frame_producer::empty();\r
92                 background_                     = background_;\r
93                 frame_number_           = 0;\r
94                 auto_play_delta_        = -1;\r
95 \r
96                 is_paused_                      = true;\r
97         }\r
98                 \r
99         safe_ptr<basic_frame> receive(int hints)\r
100         {               \r
101                 try\r
102                 {\r
103                         if(is_paused_)\r
104                                 return disable_audio(foreground_->last_frame());\r
105                 \r
106                         auto frame = receive_and_follow(foreground_, hints);\r
107                         if(frame == core::basic_frame::late())\r
108                                 return foreground_->last_frame();\r
109 \r
110                         auto frames_left = foreground_->nb_frames() - (++frame_number_) - auto_play_delta_;\r
111                         if(auto_play_delta_ > -1 && frames_left < 1)\r
112                         {\r
113                                 play();\r
114                                 return receive(hints);\r
115                         }\r
116                                 \r
117                         return frame;\r
118                 }\r
119                 catch(...)\r
120                 {\r
121                         CASPAR_LOG_CURRENT_EXCEPTION();\r
122                         stop();\r
123                         return core::basic_frame::empty();\r
124                 }\r
125         }\r
126 \r
127         boost::unique_future<std::wstring> call(bool foreground, const std::wstring& param)\r
128         {\r
129                 return (foreground ? foreground_ : background_)->call(param);\r
130         }\r
131 \r
132         bool empty() const\r
133         {\r
134                 return background_ == core::frame_producer::empty() && foreground_ == core::frame_producer::empty();\r
135         }\r
136 \r
137         boost::property_tree::wptree info() const\r
138         {\r
139                 boost::property_tree::wptree info;\r
140                 info.add(L"status",             is_paused_ ? L"paused" : (foreground_ == frame_producer::empty() ? L"stopped" : L"playing"));\r
141                 info.add(L"auto_delta", auto_play_delta_);\r
142                 info.add(L"frame-number", frame_number_);\r
143 \r
144                 auto nb_frames = foreground_->nb_frames();\r
145 \r
146                 info.add(L"nb_frames",   nb_frames == std::numeric_limits<int64_t>::max() ? -1 : nb_frames);\r
147                 info.add(L"frames-left", nb_frames == std::numeric_limits<int64_t>::max() ? -1 : (foreground_->nb_frames() - frame_number_ - auto_play_delta_));\r
148                 info.add_child(L"foreground", foreground_->info());\r
149                 info.add_child(L"background", background_->info());\r
150                 return info;\r
151         }\r
152 };\r
153 \r
154 layer::layer() : impl_(new implementation()){}\r
155 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}\r
156 layer& layer::operator=(layer&& other)\r
157 {\r
158         impl_ = std::move(other.impl_);\r
159         return *this;\r
160 }\r
161 layer::layer(const layer& other) : impl_(new implementation(*other.impl_)){}\r
162 layer& layer::operator=(const layer& other)\r
163 {\r
164         layer temp(other);\r
165         temp.swap(*this);\r
166         return *this;\r
167 }\r
168 void layer::swap(layer& other)\r
169 {       \r
170         impl_.swap(other.impl_);\r
171 }\r
172 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
173 void layer::play(){impl_->play();}\r
174 void layer::pause(){impl_->pause();}\r
175 void layer::stop(){impl_->stop();}\r
176 bool layer::is_paused() const{return impl_->is_paused_;}\r
177 int64_t layer::frame_number() const{return impl_->frame_number_;}\r
178 safe_ptr<basic_frame> layer::receive(int hints) {return impl_->receive(hints);}\r
179 safe_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}\r
180 safe_ptr<frame_producer> layer::background() const { return impl_->background_;}\r
181 bool layer::empty() const {return impl_->empty();}\r
182 boost::unique_future<std::wstring> layer::call(bool foreground, const std::wstring& param){return impl_->call(foreground, param);}\r
183 boost::property_tree::wptree layer::info() const{return impl_->info();}\r
184 }}