]> git.sesse.net Git - casparcg/blob - modules/reroute/producer/layer_producer.cpp
Added support for delaying frames to layer_producer and channel_producer.
[casparcg] / modules / reroute / producer / layer_producer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Cambell Prince, cambell.prince@gmail.com
20 */
21
22 #include "../stdafx.h"
23
24 #include "layer_producer.h"
25
26 #include <core/consumer/write_frame_consumer.h>
27 #include <core/consumer/output.h>
28 #include <core/video_channel.h>
29 #include <core/frame/draw_frame.h>
30 #include <core/frame/frame_factory.h>
31 #include <core/producer/frame_producer.h>
32 #include <core/producer/stage.h>
33
34 #include <common/except.h>
35 #include <common/semaphore.h>
36
37 #include <boost/format.hpp>
38
39 #include <tbb/concurrent_queue.h>
40
41 namespace caspar { namespace reroute {
42
43 class layer_consumer : public core::write_frame_consumer
44 {       
45         tbb::concurrent_bounded_queue<core::draw_frame> frame_buffer_;
46         semaphore                                                                               frames_available_ { 0 };
47         int                                                                                             frames_delay_;
48
49 public:
50         layer_consumer(int frames_delay)
51                 : frames_delay_(frames_delay)
52         {
53                 frame_buffer_.set_capacity(2 + frames_delay);
54         }
55
56         ~layer_consumer()
57         {
58         }
59
60         // write_frame_consumer
61
62         void send(const core::draw_frame& src_frame) override
63         {
64                 bool pushed = frame_buffer_.try_push(src_frame);
65
66                 if (pushed)
67                         frames_available_.release();
68         }
69
70         std::wstring print() const override
71         {
72                 return L"[layer_consumer]";
73         }
74
75         core::draw_frame receive()
76         {
77                 core::draw_frame frame;
78                 if (!frame_buffer_.try_pop(frame))
79                 {
80                         return core::draw_frame::late();
81                 }
82                 return frame;
83         }
84
85         void block_until_first_frame_available()
86         {
87                 if (!frames_available_.try_acquire(1 + frames_delay_, boost::chrono::seconds(2)))
88                         CASPAR_LOG(warning)
89                                         << print() << L" Timed out while waiting for first frame";
90         }
91 };
92
93 class layer_producer : public core::frame_producer_base
94 {
95         core::monitor::subject                                          monitor_subject_;
96
97         const int                                                                       layer_;
98         const spl::shared_ptr<layer_consumer>           consumer_;
99
100         core::draw_frame                                                        last_frame_;
101         uint64_t                                                                        frame_number_;
102
103         const spl::shared_ptr<core::video_channel>      channel_;
104         core::constraints                                                       pixel_constraints_;
105
106 public:
107         explicit layer_producer(const spl::shared_ptr<core::video_channel>& channel, int layer, int frames_delay)
108                 : layer_(layer)
109                 , consumer_(spl::make_shared<layer_consumer>(frames_delay))
110                 , channel_(channel)
111                 , last_frame_(core::draw_frame::empty())
112                 , frame_number_(0)
113         {
114                 pixel_constraints_.width.set(channel->video_format_desc().width);
115                 pixel_constraints_.height.set(channel->video_format_desc().height);
116                 channel_->stage().add_layer_consumer(this, layer_, consumer_);
117                 consumer_->block_until_first_frame_available();
118                 CASPAR_LOG(info) << print() << L" Initialized";
119         }
120
121         ~layer_producer()
122         {
123                 channel_->stage().remove_layer_consumer(this, layer_);
124                 CASPAR_LOG(info) << print() << L" Uninitialized";
125         }
126
127         // frame_producer
128                         
129         core::draw_frame receive_impl() override
130         {
131                 auto consumer_frame = consumer_->receive();
132                 if (consumer_frame == core::draw_frame::late())
133                         return last_frame_;
134
135                 frame_number_++;
136                 return last_frame_ = consumer_frame;
137         }       
138
139         std::wstring print() const override
140         {
141                 return L"layer-producer[" + boost::lexical_cast<std::wstring>(layer_) + L"]";
142         }
143
144         std::wstring name() const override
145         {
146                 return L"layer-producer";
147         }
148
149         boost::property_tree::wptree info() const override
150         {
151                 boost::property_tree::wptree info;
152                 info.add(L"type", L"layer-producer");
153                 return info;
154         }
155
156         core::monitor::subject& monitor_output() override
157         {
158                 return monitor_subject_;
159         }
160
161         core::constraints& pixel_constraints() override
162         {
163                 return pixel_constraints_;
164         }
165 };
166
167 spl::shared_ptr<core::frame_producer> create_layer_producer(const spl::shared_ptr<core::video_channel>& channel, int layer, int frames_delay)
168 {
169         return spl::make_shared<layer_producer>(channel, layer, frames_delay);
170 }
171
172 }}