]> git.sesse.net Git - casparcg/blob - common/diagnostics/graph.cpp
ffmpeg_producer: Multiple audio streams are now merged (flattened) before the audio...
[casparcg] / common / diagnostics / graph.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 "graph.h"
25
26 #include "../linq.h"
27
28 #include <boost/thread.hpp>
29
30 #include <tbb/spin_mutex.h>
31
32 #include <vector>
33
34 namespace caspar { namespace diagnostics {
35                 
36 int color(float r, float g, float b, float a)
37 {
38         int code = 0;
39         code |= static_cast<int>(r*255.0f+0.5f) << 24;
40         code |= static_cast<int>(g*255.0f+0.5f) << 16;
41         code |= static_cast<int>(b*255.0f+0.5f) <<  8;
42         code |= static_cast<int>(a*255.0f+0.5f) <<  0;
43         return code;
44 }
45
46 std::tuple<float, float, float, float> color(int code)
47 {
48         float r = static_cast<float>((code >> 24) & 255)/255.0f;
49         float g = static_cast<float>((code >> 16) & 255)/255.0f;
50         float b = static_cast<float>((code >>  8) & 255)/255.0f;
51         float a = static_cast<float>((code >>  0) & 255)/255.0f;
52         return std::make_tuple(r, g, b, a);
53 }
54
55 typedef std::vector<spi::sink_factory_t> sink_factories_t;
56 static boost::mutex g_sink_factories_mutex;
57 static sink_factories_t g_sink_factories;
58
59 std::vector<spl::shared_ptr<spi::graph_sink>> create_sinks()
60 {
61         boost::lock_guard<boost::mutex> lock(g_sink_factories_mutex);
62
63         return cpplinq::from(g_sink_factories)
64                 .select([](const spi::sink_factory_t& s){ return s(); })
65                 .to_vector();
66 }
67
68 struct graph::impl
69 {
70         std::vector<spl::shared_ptr<spi::graph_sink>> sinks_ = create_sinks();
71 public:
72         impl()
73         {
74         }
75
76         void activate()
77         {
78                 for (auto& sink : sinks_)
79                         sink->activate();
80         }
81
82         void set_text(const std::wstring& value)
83         {
84                 for (auto& sink : sinks_)
85                         sink->set_text(value);
86         }
87
88         void set_value(const std::string& name, double value)
89         {
90                 for (auto& sink : sinks_)
91                         sink->set_value(name, value);
92         }
93
94         void set_tag(tag_severity severity, const std::string& name)
95         {
96                 for (auto& sink : sinks_)
97                         sink->set_tag(severity, name);
98         }
99
100         void set_color(const std::string& name, int color)
101         {
102                 for (auto& sink : sinks_)
103                         sink->set_color(name, color);
104         }
105
106         void auto_reset()
107         {
108                 for (auto& sink : sinks_)
109                         sink->auto_reset();
110         }
111                 
112 private:
113         impl(impl&);
114         impl& operator=(impl&);
115 };
116         
117 graph::graph() : impl_(new impl)
118 {
119 }
120
121 void graph::set_text(const std::wstring& value) { impl_->set_text(value); }
122 void graph::set_value(const std::string& name, double value) { impl_->set_value(name, value); }
123 void graph::set_color(const std::string& name, int color) { impl_->set_color(name, color); }
124 void graph::set_tag(tag_severity severity, const std::string& name) { impl_->set_tag(severity, name); }
125 void graph::auto_reset() { impl_->auto_reset(); }
126
127 void register_graph(const spl::shared_ptr<graph>& graph)
128 {
129         graph->impl_->activate();
130 }
131
132 namespace spi {
133
134 void register_sink_factory(sink_factory_t factory)
135 {
136         boost::lock_guard<boost::mutex> lock(g_sink_factories_mutex);
137
138         g_sink_factories.push_back(std::move(factory));
139 }
140
141 }
142
143 }}