]> git.sesse.net Git - casparcg/blob - core/producer/layer.cpp
2.0.0.2:
[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 #include "frame_producer.h"\r
25 \r
26 #include "../producer/frame/basic_frame.h"\r
27 #include "../producer/frame/audio_transform.h"\r
28 \r
29 namespace caspar { namespace core {\r
30         \r
31 struct layer::implementation\r
32 {                               \r
33         safe_ptr<frame_producer>        foreground_;\r
34         safe_ptr<frame_producer>        background_;\r
35         safe_ptr<basic_frame>           last_frame_;\r
36         bool                                            is_paused_;\r
37 public:\r
38         implementation() \r
39                 : foreground_(frame_producer::empty())\r
40                 , background_(frame_producer::empty())\r
41                 , last_frame_(basic_frame::empty())\r
42                 , is_paused_(false){}\r
43         \r
44         void pause(){is_paused_ = true;}\r
45         void resume(){is_paused_ = false;}\r
46 \r
47         void load(const safe_ptr<frame_producer>& producer, bool preview)\r
48         {               \r
49                 background_ = producer;\r
50 \r
51                 if(preview) \r
52                 {\r
53                         // Play the first frame and pause.\r
54                         play();\r
55                         receive();\r
56                         pause();\r
57                 }\r
58         }\r
59         \r
60         void play()\r
61         {                       \r
62                 if(background_ != frame_producer::empty())\r
63                 {\r
64                         background_->set_leading_producer(foreground_);\r
65                         foreground_ = background_;\r
66                         background_ = frame_producer::empty();\r
67                 }\r
68                 resume();\r
69         }\r
70         \r
71         void stop()\r
72         {\r
73                 pause();\r
74                 last_frame_ = basic_frame::empty();\r
75                 foreground_ = frame_producer::empty();\r
76         }\r
77                 \r
78         safe_ptr<basic_frame> receive()\r
79         {               \r
80                 if(is_paused_)\r
81                         return last_frame_;\r
82 \r
83                 auto next_frame = receive_and_follow(foreground_);\r
84                 if(next_frame == core::basic_frame::late())\r
85                         return last_frame_;\r
86                                 \r
87                 last_frame_ = basic_frame(next_frame);\r
88                 last_frame_->get_audio_transform().set_has_audio(false);\r
89 \r
90                 return next_frame;\r
91         }\r
92 };\r
93 \r
94 layer::layer() : impl_(new implementation()){}\r
95 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}\r
96 layer& layer::operator=(layer&& other)\r
97 {\r
98         impl_ = std::move(other.impl_);\r
99         return *this;\r
100 }\r
101 layer::layer(const layer& other) : impl_(new implementation(*other.impl_)){}\r
102 layer& layer::operator=(const layer& other)\r
103 {\r
104         layer temp(other);\r
105         temp.swap(*this);\r
106         return *this;\r
107 }\r
108 void layer::swap(layer& other)\r
109 {       \r
110         impl_.swap(other.impl_);\r
111 }\r
112 void layer::load(const safe_ptr<frame_producer>& frame_producer, bool preview){return impl_->load(frame_producer, preview);}    \r
113 void layer::play(){impl_->play();}\r
114 void layer::pause(){impl_->pause();}\r
115 void layer::stop(){impl_->stop();}\r
116 safe_ptr<basic_frame> layer::receive() {return impl_->receive();}\r
117 safe_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}\r
118 safe_ptr<frame_producer> layer::background() const { return impl_->background_;}\r
119 }}