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