]> 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 <common/concurrency/executor.h>\r
7 #include <common/exception/exceptions.h>\r
8 #include <common/utility/assert.h>\r
9 #include <common/utility/printer.h>\r
10 \r
11 #include "../producer/frame/basic_frame.h"\r
12 #include "../producer/frame/audio_transform.h"\r
13 \r
14 #include <tbb/spin_mutex.h>\r
15 \r
16 namespace caspar { namespace core {\r
17 \r
18 class frame_producer_remover\r
19 {\r
20         executor executor_;\r
21 \r
22         void do_remove(safe_ptr<frame_producer>& producer)\r
23         {\r
24                 auto name = producer->print();\r
25                 producer = frame_producer::empty();\r
26                 CASPAR_LOG(info) << name << L" Removed.";\r
27         }\r
28 \r
29 public:\r
30 \r
31         frame_producer_remover() : executor_(L"frame_producer_remover")\r
32         {\r
33                 executor_.start();\r
34         }\r
35 \r
36         void remove(safe_ptr<frame_producer>&& producer)\r
37         {\r
38                 CASPAR_VERIFY(producer == frame_producer::empty() || producer.unique());\r
39                 executor_.begin_invoke(std::bind(&frame_producer_remover::do_remove, this, std::move(producer)));\r
40         }\r
41 };\r
42 \r
43 frame_producer_remover g_remover;\r
44 \r
45 struct layer::implementation : boost::noncopyable\r
46 {                               \r
47         mutable tbb::spin_mutex         printer_mutex_;\r
48         printer                                         parent_printer_;\r
49         int                                                     index_;\r
50         \r
51         safe_ptr<frame_producer>        foreground_;\r
52         safe_ptr<frame_producer>        background_;\r
53         safe_ptr<basic_frame>           last_frame_;\r
54         bool                                            is_paused_;\r
55 public:\r
56         implementation(int index, const printer& parent_printer) \r
57                 : parent_printer_(parent_printer)\r
58                 , index_(index)\r
59                 , foreground_(frame_producer::empty())\r
60                 , background_(frame_producer::empty())\r
61                 , last_frame_(basic_frame::empty())\r
62                 , is_paused_(false){}\r
63         \r
64         void load(const safe_ptr<frame_producer>& frame_producer, bool play_on_load, bool preview)\r
65         {               \r
66                 background_ = frame_producer;\r
67                 is_paused_ = false;\r
68 \r
69                 if(preview)\r
70                 {\r
71                         play();\r
72                         receive();\r
73                         pause();\r
74                 }\r
75 \r
76                 if(play_on_load)\r
77                         play();                         \r
78         }\r
79 \r
80         void play()\r
81         {                       \r
82                 if(!is_paused_)                 \r
83                 {\r
84                         background_->set_leading_producer(foreground_);\r
85                         foreground_ = background_;\r
86                         CASPAR_LOG(info) << foreground_->print() << L" Added.";\r
87                         background_ = frame_producer::empty();\r
88                 }\r
89                 is_paused_ = false;\r
90         }\r
91 \r
92         void pause()\r
93         {\r
94                 is_paused_ = true;\r
95         }\r
96 \r
97         void stop()\r
98         {\r
99                 pause();\r
100                 last_frame_ = basic_frame::empty();\r
101                 foreground_ = frame_producer::empty();\r
102         }\r
103                 \r
104         safe_ptr<basic_frame> receive()\r
105         {               \r
106                 if(is_paused_)\r
107                 {\r
108                         last_frame_->get_audio_transform().set_gain(0.0);\r
109                         return last_frame_;\r
110                 }\r
111 \r
112                 try\r
113                 {\r
114                         last_frame_ = foreground_->receive(); \r
115                         if(last_frame_ == basic_frame::eof())\r
116                         {\r
117                                 CASPAR_VERIFY(foreground_ != frame_producer::empty());\r
118 \r
119                                 auto following = foreground_->get_following_producer();\r
120                                 following->set_leading_producer(foreground_);\r
121                                 following->set_parent_printer([=]{return print();});\r
122                                 g_remover.remove(std::move(foreground_));\r
123                                 foreground_ = following;\r
124                                 CASPAR_LOG(info) << foreground_->print() << L" Added.";\r
125 \r
126                                 last_frame_ = receive();\r
127                         }\r
128                 }\r
129                 catch(...)\r
130                 {\r
131                         CASPAR_LOG(error) << print() << L" Unhandled Exception: ";\r
132                         CASPAR_LOG_CURRENT_EXCEPTION();\r
133                         stop();\r
134                 }\r
135 \r
136                 return last_frame_;\r
137         }\r
138                 \r
139         std::wstring print() const\r
140         {\r
141                 tbb::spin_mutex::scoped_lock lock(printer_mutex_); // Child-producers may call print asynchronously to the producer thread.\r
142                 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"layer[" + boost::lexical_cast<std::wstring>(index_) + L"]";\r
143         }\r
144 };\r
145 \r
146 layer::layer(int index, const printer& parent_printer) : impl_(new implementation(index, parent_printer)){}\r
147 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}\r
148 layer& layer::operator=(layer&& other)\r
149 {\r
150         impl_ = std::move(other.impl_);\r
151         return *this;\r
152 }\r
153 void layer::swap(layer& other)\r
154 {\r
155         impl_.swap(other.impl_);\r
156         // Printer state is not swapped.\r
157         tbb::spin_mutex::scoped_lock lock(impl_->printer_mutex_);\r
158         std::swap(impl_->parent_printer_, other.impl_->parent_printer_);\r
159         std::swap(impl_->index_, other.impl_->index_);\r
160 }\r
161 void layer::load(const safe_ptr<frame_producer>& frame_producer, bool play_on_load, bool preview){return impl_->load(frame_producer, play_on_load, preview);}   \r
162 void layer::play(){impl_->play();}\r
163 void layer::pause(){impl_->pause();}\r
164 void layer::stop(){impl_->stop();}\r
165 safe_ptr<basic_frame> layer::receive() {return impl_->receive();}\r
166 safe_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}\r
167 safe_ptr<frame_producer> layer::background() const { return impl_->background_;}\r
168 std::wstring layer::print() const { return impl_->print();}\r
169 }}