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