]> git.sesse.net Git - casparcg/blob - core/diagnostics/subject_diagnostics.cpp
- Fixed diag to work with new SFML version.
[casparcg] / core / diagnostics / subject_diagnostics.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: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #include "../StdAfx.h"
23
24 #include "subject_diagnostics.h"
25
26 #include "call_context.h"
27
28 #include <common/diagnostics/graph.h>
29
30 #include <boost/lexical_cast.hpp>
31 #include <boost/timer.hpp>
32
33 #include <atomic>
34 #include <mutex>
35 #include <unordered_map>
36
37 namespace {
38
39 int64_t create_id()
40 {
41         static std::atomic<int64_t> counter = 0;
42
43         return ++counter;
44 }
45
46 }
47
48 namespace caspar { namespace core { namespace diagnostics {
49
50 static const double SECONDS_BETWEEN_FULL_STATE = 0.4;
51
52 class subject_graph : public caspar::diagnostics::spi::graph_sink
53 {
54         spl::shared_ptr<monitor::subject>               subject_                                                = spl::make_shared<monitor::subject>("/" + boost::lexical_cast<std::string>(create_id()));
55         call_context                                                    context_                                                = call_context::for_thread();
56         std::mutex                                                              mutex_;
57         std::wstring                                                    text_;
58         std::unordered_map<std::string, int>    colors_;
59         boost::timer                                                    time_since_full_state_send_;
60 public:
61         subject_graph()
62         {
63         }
64
65         void activate() override
66         {
67                 subject_->attach_parent(get_or_create_subject());
68         }
69
70         void set_text(const std::wstring& value) override
71         {
72                 std::lock_guard<std::mutex> lock(mutex_);
73
74                 text_ = value;
75
76                 *subject_ << monitor::message("/text") % text_;
77         }
78
79         void set_value(const std::string& name, double value) override
80         {
81                 *subject_ << monitor::message("/value/" + name) % value;
82
83                 send_full_state_if_long_ago();
84         }
85         
86         void set_color(const std::string& name, int color) override
87         {
88                 std::lock_guard<std::mutex> lock(mutex_);
89
90                 colors_[name] = color;
91
92                 *subject_ << monitor::message("/color/" + name) % color;
93         }
94         
95         void set_tag(const std::string& name) override
96         {
97                 *subject_ << monitor::message("/tag/" + name);
98
99                 send_full_state_if_long_ago();
100         }
101         
102         void auto_reset() override
103         {
104         }
105
106         monitor::subject& subject()
107         {
108                 return *subject_;
109         }
110 private:
111         void send_full_state_if_long_ago()
112         {
113                 std::lock_guard<std::mutex> lock(mutex_);
114
115                 if (time_since_full_state_send_.elapsed() > SECONDS_BETWEEN_FULL_STATE)
116                 {
117                         send_full_state();
118                 }
119         }
120
121         void send_full_state()
122         {
123                 *subject_ << monitor::message("/text") % text_;
124
125                 if (context_.video_channel != -1)
126                         *subject_ << monitor::message("/context/channel") % context_.video_channel;
127
128                 if (context_.layer != -1)
129                         *subject_ << monitor::message("/context/layer") % context_.layer;
130
131                 for (const auto& color : colors_)
132                         *subject_ << monitor::message("/color/" + color.first) % color.second;
133
134                 time_since_full_state_send_.restart();
135         }
136 };
137
138 spl::shared_ptr<monitor::subject> get_or_create_subject()
139 {
140         static auto diag_subject = []()
141         {
142                 auto subject = spl::make_shared<monitor::subject>("/diag");
143
144                 caspar::diagnostics::spi::register_sink_factory([=]()
145                 {
146                         return spl::make_shared<subject_graph>();
147                 });
148
149                 return subject;
150         }();
151
152         return diag_subject;
153 }
154
155 }}}