]> git.sesse.net Git - casparcg/blob - modules/reroute/producer/reroute_producer.cpp
Merged most recent OSC changes
[casparcg] / modules / reroute / producer / reroute_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: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../stdafx.h"
23
24 #include "reroute_producer.h"
25
26 #include <core/producer/frame_producer.h>
27 #include <core/frame/draw_frame.h>
28 #include <core/frame/frame_factory.h>
29 #include <core/frame/pixel_format.h>
30 #include <core/frame/frame.h>
31 #include <core/video_channel.h>
32 #include <core/producer/stage.h>
33
34 #include <common/except.h>
35 #include <common/diagnostics/graph.h>
36 #include <common/log.h>
37 #include <common/reactive.h>
38
39 #include <asmlib.h>
40
41 #include <tbb/concurrent_queue.h>
42
43 #include <boost/property_tree/ptree.hpp>
44 #include <boost/foreach.hpp>
45 #include <boost/optional.hpp>
46 #include <boost/range/algorithm_ext/push_back.hpp>
47 #include <boost/range/numeric.hpp>
48 #include <boost/range/adaptor/map.hpp>
49
50 #include <queue>
51
52 namespace caspar { namespace reroute {
53                 
54 class reroute_producer : public reactive::observer<std::map<int, core::draw_frame>>
55                                            , public core::frame_producer_base
56 {
57         core::monitor::subject                                                                                  monitor_subject_;
58
59         core::constraints                                                                                               constraints_;
60         const spl::shared_ptr<diagnostics::graph>                                               graph_;
61         
62         tbb::concurrent_bounded_queue<std::map<int, core::draw_frame>>  input_buffer_;
63 public:
64         explicit reroute_producer() 
65         {
66                 graph_->set_color("late-frame", diagnostics::color(0.6f, 0.3f, 0.3f));
67                 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
68                 graph_->set_text(print());
69                 diagnostics::register_graph(graph_);
70
71                 input_buffer_.set_capacity(1);
72         }
73                 
74         // observable
75
76         void on_next(const std::map<int, core::draw_frame>& frames)
77         {
78                 if(!input_buffer_.try_push(frames))
79                         graph_->set_tag("dropped-frame");               
80         }
81
82         // frame_producer
83                         
84         core::draw_frame receive_impl() override
85         {               
86                 std::map<int, core::draw_frame> frames;
87                 if(!input_buffer_.try_pop(frames))
88                 {
89                         graph_->set_tag("late-frame");
90                         return core::draw_frame::late();                
91                 }
92
93                 return boost::accumulate(frames | boost::adaptors::map_values, core::draw_frame::empty(), core::draw_frame::over);
94         }
95
96         core::constraints& pixel_constraints() override
97         {
98                 return constraints_;
99         }
100                 
101         std::wstring print() const override
102         {
103                 return L"reroute[]";
104         }
105
106         std::wstring name() const override
107         {
108                 return L"reroute";
109         }
110
111         boost::property_tree::wptree info() const override
112         {
113                 boost::property_tree::wptree info;
114                 info.add(L"type", L"rerotue-producer");
115                 return info;
116         }
117                 
118         core::monitor::subject& monitor_output()
119         {
120                 return monitor_subject_;
121         }
122 };
123
124 spl::shared_ptr<core::frame_producer> create_producer(core::video_channel& channel)
125 {
126         auto producer = spl::make_shared<reroute_producer>();
127         
128         std::weak_ptr<reactive::observer<std::map<int, core::draw_frame>>> o = producer;
129
130         //channel.stage().monitor_output().link_target.subscribe(o);
131
132         return producer;
133 }
134
135 }}