]> git.sesse.net Git - casparcg/blob - core/producer/layer.cpp
7ef42e9fe9875fa6cdf08b4bc1a799e3c84d1bee
[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 namespace caspar { namespace core {\r
29         \r
30 struct layer::implementation\r
31 {                               \r
32         safe_ptr<frame_producer>        foreground_;\r
33         safe_ptr<frame_producer>        background_;\r
34         int64_t                                         frame_number_;\r
35         int                                                     auto_play_delta_;\r
36         bool                                            is_paused_;\r
37 \r
38 public:\r
39         implementation() \r
40                 : foreground_(frame_producer::empty())\r
41                 , background_(frame_producer::empty())\r
42                 , frame_number_(0)\r
43                 , auto_play_delta_(std::numeric_limits<int>::min())\r
44                 , is_paused_(false)\r
45         {\r
46         }\r
47         \r
48         void pause()\r
49         {\r
50                 is_paused_ = true;\r
51         }\r
52 \r
53         void resume()\r
54         {\r
55                 is_paused_ = false;\r
56         }\r
57 \r
58         void load(const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
59         {               \r
60                 background_              = producer;\r
61                 auto_play_delta_ = auto_play_delta;\r
62 \r
63                 if(preview) // Play the first frame and pause.\r
64                 {                       \r
65                         play();\r
66                         receive();\r
67                         pause();\r
68                 }\r
69         }\r
70         \r
71         void play()\r
72         {                       \r
73                 if(background_ != frame_producer::empty())\r
74                 {\r
75                         background_->set_leading_producer(foreground_);\r
76                         \r
77                         foreground_                     = background_;\r
78                         background_                     = frame_producer::empty();\r
79                         frame_number_           = 0;\r
80                         auto_play_delta_        = std::numeric_limits<int>::min();      \r
81                 }\r
82 \r
83                 is_paused_                      = false;\r
84         }\r
85         \r
86         void stop()\r
87         {\r
88                 foreground_                     = frame_producer::empty();\r
89                 background_                     = background_;\r
90                 frame_number_           = 0;\r
91                 auto_play_delta_        = std::numeric_limits<int>::min();\r
92 \r
93                 is_paused_                      = true;\r
94         }\r
95                 \r
96         safe_ptr<basic_frame> receive()\r
97         {               \r
98                 try\r
99                 {\r
100                         if(is_paused_)\r
101                                 return disable_audio(foreground_->last_frame());\r
102                 \r
103                         auto frame = receive_and_follow(foreground_, frame_producer::NO_HINT);\r
104                         if(frame == core::basic_frame::late())\r
105                                 return foreground_->last_frame();\r
106 \r
107                         auto frames_left = foreground_->nb_frames() - (++frame_number_) - auto_play_delta_;\r
108                         if(frames_left < 1)\r
109                         {\r
110                                 play();\r
111                                 return receive();\r
112                         }\r
113                                 \r
114                         return frame;\r
115                 }\r
116                 catch(...)\r
117                 {\r
118                         CASPAR_LOG_CURRENT_EXCEPTION();\r
119                         stop();\r
120                         return core::basic_frame::empty();\r
121                 }\r
122         }\r
123 \r
124         layer_status status() const\r
125         {\r
126                 layer_status status;\r
127                 status.foreground        = foreground_->print();\r
128                 status.background        = background_->print();\r
129                 status.is_paused         = is_paused_;\r
130                 status.total_frames      = foreground_->nb_frames();\r
131                 status.current_frame = frame_number_;\r
132 \r
133                 return status;\r
134         }\r
135 \r
136         bool empty() const\r
137         {\r
138                 return background_ == core::frame_producer::empty() && foreground_ == core::frame_producer::empty();\r
139         }\r
140 };\r
141 \r
142 layer::layer() : impl_(new implementation()){}\r
143 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}\r
144 layer& layer::operator=(layer&& other)\r
145 {\r
146         impl_ = std::move(other.impl_);\r
147         return *this;\r
148 }\r
149 layer::layer(const layer& other) : impl_(new implementation(*other.impl_)){}\r
150 layer& layer::operator=(const layer& other)\r
151 {\r
152         layer temp(other);\r
153         temp.swap(*this);\r
154         return *this;\r
155 }\r
156 void layer::swap(layer& other)\r
157 {       \r
158         impl_.swap(other.impl_);\r
159 }\r
160 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
161 void layer::play(){impl_->play();}\r
162 void layer::pause(){impl_->pause();}\r
163 void layer::stop(){impl_->stop();}\r
164 bool layer::is_paused() const{return impl_->is_paused_;}\r
165 int64_t layer::frame_number() const{return impl_->frame_number_;}\r
166 layer_status layer::status() const {return impl_->status();}\r
167 safe_ptr<basic_frame> layer::receive() {return impl_->receive();}\r
168 safe_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}\r
169 safe_ptr<frame_producer> layer::background() const { return impl_->background_;}\r
170 bool layer::empty() const {return impl_->empty();}\r
171 }}