]> git.sesse.net Git - casparcg/blob - core/producer/channel/channel_producer.cpp
Subtree merge of old SVN "docs" folder into the "master" git branch. You can see...
[casparcg] / core / producer / channel / channel_producer.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../../stdafx.h"\r
23 \r
24 #include "channel_producer.h"\r
25 \r
26 #include "../../consumer/frame_consumer.h"\r
27 #include "../../consumer/output.h"\r
28 #include "../../video_channel.h"\r
29 \r
30 #include "../frame/basic_frame.h"\r
31 #include "../frame/frame_factory.h"\r
32 #include "../../mixer/write_frame.h"\r
33 #include "../../mixer/read_frame.h"\r
34 \r
35 #include <common/exception/exceptions.h>\r
36 #include <common/memory/memcpy.h>\r
37 #include <common/concurrency/future_util.h>\r\r
38 #include <tbb/concurrent_queue.h>\r
39 \r
40 namespace caspar { namespace core {\r
41 \r
42 class channel_consumer : public frame_consumer\r
43 {       \r
44         tbb::concurrent_bounded_queue<std::shared_ptr<read_frame>>      frame_buffer_;\r
45         core::video_format_desc                                                                         format_desc_;\r
46         int                                                                                                                     channel_index_;\r
47         tbb::atomic<bool>                                                                                       is_running_;\r
48 \r
49 public:\r
50         channel_consumer() \r
51         {\r
52                 is_running_ = true;\r
53                 frame_buffer_.set_capacity(3);\r
54         }\r
55 \r
56         ~channel_consumer()\r
57         {\r
58                 stop();\r
59         }\r
60 \r
61         // frame_consumer\r
62 \r
63         virtual boost::unique_future<bool> send(const safe_ptr<read_frame>& frame) override\r
64         {\r
65                 frame_buffer_.try_push(frame);\r
66                 return caspar::wrap_as_future(is_running_.load());\r
67         }\r
68 \r
69         virtual void initialize(const core::video_format_desc& format_desc, int channel_index) override\r
70         {\r
71                 format_desc_    = format_desc;\r
72                 channel_index_  = channel_index;\r
73         }\r
74 \r
75         virtual std::wstring print() const override\r
76         {\r
77                 return L"[channel-consumer|" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";\r
78         }\r
79 \r
80         virtual boost::property_tree::wptree info() const override\r
81         {\r
82                 boost::property_tree::wptree info;\r
83                 info.add(L"type", L"channel-consumer");\r
84                 info.add(L"channel-index", channel_index_);\r
85                 return info;\r
86         }\r
87         \r
88         virtual bool has_synchronization_clock() const override\r
89         {\r
90                 return false;\r
91         }\r
92 \r
93         virtual size_t buffer_depth() const override\r
94         {\r
95                 return 1;\r
96         }\r
97 \r
98         virtual int index() const override\r
99         {\r
100                 return 78500 + channel_index_;\r
101         }\r
102 \r
103         // channel_consumer\r
104 \r
105         void stop()\r
106         {\r
107                 is_running_ = false;\r
108                 frame_buffer_.try_push(make_safe<read_frame>());\r
109         }\r
110         \r
111         const core::video_format_desc& get_video_format_desc()\r
112         {\r
113                 return format_desc_;\r
114         }\r
115 \r
116         std::shared_ptr<read_frame> receive()\r
117         {\r
118                 if(!is_running_)\r
119                         return make_safe<read_frame>();\r
120                 std::shared_ptr<read_frame> frame;\r
121                 frame_buffer_.try_pop(frame);\r
122                 return frame;\r
123         }\r
124 };\r
125         \r
126 class channel_producer : public frame_producer\r
127 {\r
128         const safe_ptr<frame_factory>           frame_factory_;\r
129         const safe_ptr<channel_consumer>        consumer_;\r
130 \r
131         std::queue<safe_ptr<basic_frame>>       frame_buffer_;\r
132         safe_ptr<basic_frame>                           last_frame_;\r
133         uint64_t                                                        frame_number_;\r
134 \r
135 public:\r
136         explicit channel_producer(const safe_ptr<frame_factory>& frame_factory, const safe_ptr<video_channel>& channel) \r
137                 : frame_factory_(frame_factory)\r
138                 , consumer_(make_safe<channel_consumer>())\r
139                 , last_frame_(basic_frame::empty())\r
140                 , frame_number_(0)\r
141         {\r
142                 channel->output()->add(consumer_);\r
143                 CASPAR_LOG(info) << print() << L" Initialized";\r
144         }\r
145 \r
146         ~channel_producer()\r
147         {\r
148                 consumer_->stop();\r
149                 CASPAR_LOG(info) << print() << L" Uninitialized";\r
150         }\r
151 \r
152         // frame_producer\r
153                         \r
154         virtual safe_ptr<basic_frame> receive(int) override\r
155         {\r
156                 auto format_desc = consumer_->get_video_format_desc();\r
157 \r
158                 if(frame_buffer_.size() > 1)\r
159                 {\r
160                         auto frame = frame_buffer_.front();\r
161                         frame_buffer_.pop();\r
162                         return last_frame_ = frame;\r
163                 }\r
164                 \r
165                 auto read_frame = consumer_->receive();\r
166                 if(!read_frame || read_frame->image_data().empty())\r
167                         return basic_frame::late();             \r
168 \r
169                 frame_number_++;\r
170                 \r
171                 core::pixel_format_desc desc;\r
172                 bool double_speed       = std::abs(frame_factory_->get_video_format_desc().fps / 2.0 - format_desc.fps) < 0.01;         \r
173                 bool half_speed         = std::abs(format_desc.fps / 2.0 - frame_factory_->get_video_format_desc().fps) < 0.01;\r
174 \r
175                 if(half_speed && frame_number_ % 2 == 0) // Skip frame\r
176                         return receive(0);\r
177 \r
178                 desc.pix_fmt = core::pixel_format::bgra;\r
179                 desc.planes.push_back(core::pixel_format_desc::plane(format_desc.width, format_desc.height, 4));\r
180                 auto frame = frame_factory_->create_frame(this, desc);\r
181 \r
182                 fast_memcpy(frame->image_data().begin(), read_frame->image_data().begin(), read_frame->image_data().size());\r
183                 frame->commit();\r
184 \r
185                 frame_buffer_.push(frame);      \r
186                 \r
187                 if(double_speed)        \r
188                         frame_buffer_.push(frame);\r
189 \r
190                 return receive(0);\r
191         }       \r
192 \r
193         virtual safe_ptr<basic_frame> last_frame() const override\r
194         {\r
195                 return last_frame_; \r
196         }       \r
197 \r
198         virtual std::wstring print() const override\r
199         {\r
200                 return L"channel[]";\r
201         }\r
202 \r
203         virtual boost::property_tree::wptree info() const override\r
204         {\r
205                 boost::property_tree::wptree info;\r
206                 info.add(L"type", L"channel-producer");\r
207                 return info;\r
208         }\r
209 };\r
210 \r
211 safe_ptr<frame_producer> create_channel_producer(const safe_ptr<core::frame_factory>& frame_factory, const safe_ptr<video_channel>& channel)\r
212 {\r
213         return create_producer_print_proxy(\r
214                         make_safe<channel_producer>(frame_factory, channel));\r
215 }\r
216 \r
217 }}