]> git.sesse.net Git - casparcg/blob - core/producer/layer.cpp
ยค412
[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                 if(producer != frame_producer::empty() && !producer.unique())\r
39                         CASPAR_LOG(debug) << producer->print() << L" was not destroyed on dedicated destruction thread.";\r
40                 executor_.begin_invoke(std::bind(&frame_producer_remover::do_remove, this, std::move(producer)));\r
41         }\r
42 };\r
43 \r
44 frame_producer_remover g_remover;\r
45 \r
46 struct layer::implementation : public object, boost::noncopyable\r
47 {                               \r
48         mutable tbb::spin_mutex         printer_mutex_;\r
49         printer                                         parent_printer_;\r
50         int                                                     index_;\r
51         \r
52         safe_ptr<frame_producer>        foreground_;\r
53         safe_ptr<frame_producer>        background_;\r
54         safe_ptr<basic_frame>           last_frame_;\r
55         bool                                            is_paused_;\r
56 public:\r
57         implementation(const object* parent, int index) \r
58                 : object(parent)\r
59                 , index_(index)\r
60                 , foreground_(frame_producer::empty())\r
61                 , background_(frame_producer::empty())\r
62                 , last_frame_(basic_frame::empty())\r
63                 , is_paused_(false){}\r
64         \r
65         void load(const safe_ptr<frame_producer>& frame_producer, bool play_on_load, bool preview)\r
66         {               \r
67                 frame_producer->set_parent(this);\r
68                 background_ = frame_producer;\r
69                 is_paused_ = false;\r
70 \r
71                 if(preview)\r
72                 {\r
73                         play();\r
74                         receive();\r
75                         pause();\r
76                 }\r
77 \r
78                 if(play_on_load)\r
79                         play();                         \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" Added.";\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_ = basic_frame::empty();\r
103                 foreground_ = frame_producer::empty();\r
104         }\r
105                 \r
106         safe_ptr<basic_frame> receive()\r
107         {               \r
108                 if(is_paused_)\r
109                 {\r
110                         last_frame_->get_audio_transform().set_gain(0.0);\r
111                         return last_frame_;\r
112                 }\r
113 \r
114                 try\r
115                 {\r
116                         last_frame_ = foreground_->receive(); \r
117                         if(last_frame_ == basic_frame::eof())\r
118                         {\r
119                                 CASPAR_VERIFY(foreground_ != frame_producer::empty());\r
120 \r
121                                 auto following = foreground_->get_following_producer();\r
122                                 following->set_leading_producer(foreground_);\r
123                                 following->set_parent(this);\r
124                                 g_remover.remove(std::move(foreground_));\r
125                                 foreground_ = following;\r
126                                 CASPAR_LOG(info) << foreground_->print() << L" Added.";\r
127 \r
128                                 last_frame_ = receive();\r
129                         }\r
130                 }\r
131                 catch(...)\r
132                 {\r
133                         CASPAR_LOG(error) << print() << L" Unhandled Exception: ";\r
134                         CASPAR_LOG_CURRENT_EXCEPTION();\r
135                         stop();\r
136                 }\r
137 \r
138                 return last_frame_;\r
139         }\r
140                 \r
141         std::wstring print() const\r
142         {\r
143                 tbb::spin_mutex::scoped_lock lock(printer_mutex_); // Child-producers may call print asynchronously to the producer thread.\r
144                 return object::print() + L"layer[" + boost::lexical_cast<std::wstring>(index_) + L"]";\r
145         }\r
146 };\r
147 \r
148 layer::layer(const object* parent, int index) : impl_(new implementation(parent, index)){}\r
149 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}\r
150 layer& layer::operator=(layer&& other)\r
151 {\r
152         impl_ = std::move(other.impl_);\r
153         return *this;\r
154 }\r
155 void layer::swap(layer& other)\r
156 {\r
157         impl_.swap(other.impl_);\r
158         // Printer state is not swapped.\r
159         tbb::spin_mutex::scoped_lock lock(impl_->printer_mutex_);\r
160         std::swap(impl_->parent_printer_, other.impl_->parent_printer_);\r
161         std::swap(impl_->index_, other.impl_->index_);\r
162 }\r
163 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
164 void layer::play(){impl_->play();}\r
165 void layer::pause(){impl_->pause();}\r
166 void layer::stop(){impl_->stop();}\r
167 safe_ptr<basic_frame> layer::receive() {return impl_->receive();}\r
168 safe_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}\r
169 safe_ptr<frame_producer> layer::background() const { return impl_->background_;}\r
170 std::wstring layer::print() const { return impl_->print();}\r
171 }}