]> git.sesse.net Git - casparcg/blob - core/producer/layer.cpp
2.0.0.2: Renamed draw_frame to basic_frame.
[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/printer.h>\r
11 \r
12 #include <mixer/frame/basic_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_VERIFY(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         mutable tbb::spin_mutex         printer_mutex_;\r
53         printer                                         parent_printer_;\r
54         int                                                     index_;\r
55         \r
56         safe_ptr<frame_producer>        foreground_;\r
57         safe_ptr<frame_producer>        background_;\r
58         safe_ptr<basic_frame>           last_frame_;\r
59         bool                                            is_paused_;\r
60 public:\r
61         implementation(int index, const printer& parent_printer) \r
62                 : parent_printer_(parent_printer)\r
63                 , index_(index)\r
64                 , foreground_(frame_producer::empty())\r
65                 , background_(frame_producer::empty())\r
66                 , last_frame_(basic_frame::empty())\r
67                 , is_paused_(false){}\r
68         \r
69         void load(const safe_ptr<frame_producer>& frame_producer, bool play_on_load, bool preview)\r
70         {               \r
71                 background_ = frame_producer;\r
72                 is_paused_ = false;\r
73 \r
74                 if(preview)\r
75                 {\r
76                         play();\r
77                         receive();\r
78                         pause();\r
79                 }\r
80 \r
81                 if(play_on_load)\r
82                         play();                         \r
83         }\r
84 \r
85         void play()\r
86         {                       \r
87                 if(!is_paused_)                 \r
88                 {\r
89                         background_->set_leading_producer(foreground_);\r
90                         foreground_ = background_;\r
91                         CASPAR_LOG(info) << foreground_->print() << L" Added.";\r
92                         background_ = frame_producer::empty();\r
93                 }\r
94                 is_paused_ = false;\r
95         }\r
96 \r
97         void pause()\r
98         {\r
99                 is_paused_ = true;\r
100         }\r
101 \r
102         void stop()\r
103         {\r
104                 pause();\r
105                 last_frame_ = basic_frame::empty();\r
106                 foreground_ = frame_producer::empty();\r
107         }\r
108                 \r
109         safe_ptr<basic_frame> receive()\r
110         {               \r
111                 if(is_paused_)\r
112                 {\r
113                         last_frame_->get_audio_transform().set_gain(0.0);\r
114                         return last_frame_;\r
115                 }\r
116 \r
117                 try\r
118                 {\r
119                         last_frame_ = foreground_->receive(); \r
120                         if(last_frame_ == basic_frame::eof())\r
121                         {\r
122                                 CASPAR_VERIFY(foreground_ != frame_producer::empty());\r
123 \r
124                                 auto following = foreground_->get_following_producer();\r
125                                 following->set_leading_producer(foreground_);\r
126                                 following->set_parent_printer(boost::bind(&implementation::print, this));\r
127                                 g_remover.remove(std::move(foreground_));\r
128                                 foreground_ = following;\r
129                                 CASPAR_LOG(info) << foreground_->print() << L" Added.";\r
130 \r
131                                 last_frame_ = receive();\r
132                         }\r
133                 }\r
134                 catch(...)\r
135                 {\r
136                         CASPAR_LOG(error) << print() << L" Unhandled Exception: ";\r
137                         CASPAR_LOG_CURRENT_EXCEPTION();\r
138                         stop();\r
139                 }\r
140 \r
141                 return last_frame_;\r
142         }\r
143                 \r
144         std::wstring print() const\r
145         {\r
146                 tbb::spin_mutex::scoped_lock lock(printer_mutex_); // Child-producers may call print asynchronously to the producer thread.\r
147                 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"layer[" + boost::lexical_cast<std::wstring>(index_) + L"]";\r
148         }\r
149 };\r
150 \r
151 layer::layer(int index, const printer& parent_printer) : impl_(new implementation(index, parent_printer)){}\r
152 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}\r
153 layer& layer::operator=(layer&& other)\r
154 {\r
155         impl_ = std::move(other.impl_);\r
156         return *this;\r
157 }\r
158 void layer::swap(layer& other)\r
159 {\r
160         impl_.swap(other.impl_);\r
161         // Printer state is not swapped.\r
162         tbb::spin_mutex::scoped_lock lock(impl_->printer_mutex_);\r
163         std::swap(impl_->parent_printer_, other.impl_->parent_printer_);\r
164         std::swap(impl_->index_, other.impl_->index_);\r
165 }\r
166 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
167 void layer::play(){impl_->play();}\r
168 void layer::pause(){impl_->pause();}\r
169 void layer::stop(){impl_->stop();}\r
170 safe_ptr<basic_frame> layer::receive() {return impl_->receive();}\r
171 safe_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}\r
172 safe_ptr<frame_producer> layer::background() const { return impl_->background_;}\r
173 std::wstring layer::print() const { return impl_->print();}\r
174 }}