]> git.sesse.net Git - casparcg/blob - core/producer/layer.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / core / producer / layer.cpp
1 #include "../stdafx.h"\r
2 \r
3 #include "layer.h"\r
4 #include "frame_producer.h"\r
5 \r
6 #include "../video_format.h"\r
7 \r
8 #include <common/concurrency/executor.h>\r
9 #include <common/utility/assert.h>\r
10 #include <common/utility/printable.h>\r
11 \r
12 #include <mixer/frame/draw_frame.h>\r
13 #include <mixer/image/image_mixer.h>\r
14 #include <mixer/audio/audio_mixer.h>\r
15 #include <mixer/audio/audio_transform.h>\r
16 \r
17 #include <tbb/spin_mutex.h>\r
18 \r
19 namespace caspar { namespace core {\r
20 \r
21 class frame_producer_remover\r
22 {\r
23         executor executor_;\r
24         tbb::atomic<int> count_;\r
25 \r
26         void do_remove(safe_ptr<frame_producer>& producer)\r
27         {\r
28                 auto name = producer->print();\r
29                 producer = frame_producer::empty();\r
30                 CASPAR_LOG(info) << name << L" Removed.";\r
31         }\r
32 public:\r
33 \r
34         frame_producer_remover()\r
35         {\r
36                 executor_.start();\r
37                 count_ = 0;\r
38         }\r
39 \r
40         void remove(safe_ptr<frame_producer>&& producer)\r
41         {\r
42                 CASPAR_ASSERT(producer.unique());\r
43                 executor_.begin_invoke(std::bind(&frame_producer_remover::do_remove, this, std::move(producer)));\r
44         }\r
45 };\r
46 \r
47 frame_producer_remover g_remover;\r
48 \r
49 \r
50 struct layer::implementation : boost::noncopyable\r
51 {                               \r
52         printer                                         parent_printer_;\r
53         int                                                     index_;\r
54         \r
55         safe_ptr<frame_producer>        foreground_;\r
56         safe_ptr<frame_producer>        background_;\r
57         safe_ptr<draw_frame>            last_frame_;\r
58         bool                                            is_paused_;\r
59 public:\r
60         implementation(int index, const printer& parent_printer) \r
61                 : parent_printer_(parent_printer)\r
62                 , index_(index)\r
63                 , foreground_(frame_producer::empty())\r
64                 , background_(frame_producer::empty())\r
65                 , last_frame_(draw_frame::empty())\r
66                 , is_paused_(false){}\r
67         \r
68         void load(const safe_ptr<frame_producer>& frame_producer, bool play_on_load)\r
69         {               \r
70                 background_ = frame_producer;\r
71                 is_paused_ = false;\r
72                 if(play_on_load)\r
73                         play();         \r
74         }\r
75 \r
76         void preview(const safe_ptr<frame_producer>& frame_producer)\r
77         {\r
78                 load(frame_producer, true);\r
79                 receive();\r
80                 pause();\r
81         }\r
82         \r
83         void play()\r
84         {                       \r
85                 if(!is_paused_)                 \r
86                 {\r
87                         background_->set_leading_producer(foreground_);\r
88                         foreground_ = background_;\r
89                         CASPAR_LOG(info) << foreground_->print() << L" Added.";\r
90                         background_ = frame_producer::empty();\r
91                 }\r
92                 is_paused_ = false;\r
93         }\r
94 \r
95         void pause()\r
96         {\r
97                 is_paused_ = true;\r
98         }\r
99 \r
100         void stop()\r
101         {\r
102                 pause();\r
103                 last_frame_ = draw_frame::empty();\r
104                 foreground_ = frame_producer::empty();\r
105         }\r
106 \r
107         void clear()\r
108         {               \r
109                 foreground_ = frame_producer::empty();\r
110                 background_ = frame_producer::empty();\r
111                 last_frame_ = draw_frame::empty();\r
112                 is_paused_ = false;\r
113         }\r
114         \r
115         safe_ptr<draw_frame> receive()\r
116         {               \r
117                 if(is_paused_)\r
118                 {\r
119                         last_frame_->get_audio_transform().set_gain(0.0);\r
120                         return last_frame_;\r
121                 }\r
122 \r
123                 try\r
124                 {\r
125                         last_frame_ = foreground_->receive(); \r
126                         if(last_frame_ == draw_frame::eof())\r
127                         {\r
128                                 CASPAR_ASSERT(foreground_ != frame_producer::empty());\r
129 \r
130                                 auto following = foreground_->get_following_producer();\r
131                                 following->set_leading_producer(foreground_);\r
132                                 following->set_parent_printer(boost::bind(&implementation::print, this));\r
133                                 g_remover.remove(std::move(foreground_));\r
134                                 foreground_ = following;\r
135                                 CASPAR_LOG(info) << foreground_->print() << L" Added.";\r
136 \r
137                                 last_frame_ = receive();\r
138                         }\r
139                 }\r
140                 catch(...)\r
141                 {\r
142                         CASPAR_LOG(error) << print() << L" Unhandled Exception: ";\r
143                         CASPAR_LOG_CURRENT_EXCEPTION();\r
144                         stop();\r
145                 }\r
146 \r
147                 return last_frame_;\r
148         }\r
149                 \r
150         std::wstring print() const\r
151         {\r
152                 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"layer[" + boost::lexical_cast<std::wstring>(index_) + L"]";\r
153         }\r
154 \r
155         void swap(implementation& other)\r
156         {\r
157                 std::swap(foreground_, other.foreground_);\r
158                 std::swap(background_, other.background_);\r
159                 std::swap(last_frame_, other.last_frame_);\r
160                 std::swap(is_paused_, other.is_paused_);\r
161         }\r
162 };\r
163 \r
164 layer::layer(int index, const printer& parent_printer) : impl_(new implementation(index, parent_printer)){}\r
165 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}\r
166 layer& layer::operator=(layer&& other)\r
167 {\r
168         impl_ = std::move(other.impl_);\r
169         return *this;\r
170 }\r
171 void layer::swap(layer& other)\r
172 {\r
173         impl_->swap(*other.impl_);\r
174 }\r
175 void layer::load(const safe_ptr<frame_producer>& frame_producer, bool play_on_load){return impl_->load(frame_producer, play_on_load);}  \r
176 void layer::preview(const safe_ptr<frame_producer>& frame_producer){return impl_->preview(frame_producer);}     \r
177 void layer::play(){impl_->play();}\r
178 void layer::pause(){impl_->pause();}\r
179 void layer::stop(){impl_->stop();}\r
180 void layer::clear(){impl_->clear();}\r
181 safe_ptr<draw_frame> layer::receive() {return impl_->receive();}\r
182 safe_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}\r
183 safe_ptr<frame_producer> layer::background() const { return impl_->background_;}\r
184 std::wstring layer::print() const { return impl_->print();}\r
185 }}