]> git.sesse.net Git - casparcg/blob - core/diagnostics/subject_diagnostics.cpp
[streaming_consumer] Added default cropping/scaling/pixel format selection/bitrate...
[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 #include <common/timer.h>
30
31 #include <boost/lexical_cast.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         caspar::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(caspar::diagnostics::tag_severity severity, const std::string& name) override
96         {
97                 std::string severity_path;
98
99                 switch (severity)
100                 {
101                 case caspar::diagnostics::tag_severity::INFO:
102                         severity_path = "info/";
103                         break;
104                 case caspar::diagnostics::tag_severity::WARNING:
105                         severity_path = "warning/";
106                         break;
107                 }
108
109                 *subject_ << monitor::message("/tag/" + severity_path + name);
110
111                 send_full_state_if_long_ago();
112         }
113         
114         void auto_reset() override
115         {
116         }
117
118         monitor::subject& subject()
119         {
120                 return *subject_;
121         }
122 private:
123         void send_full_state_if_long_ago()
124         {
125                 std::lock_guard<std::mutex> lock(mutex_);
126
127                 if (time_since_full_state_send_.elapsed() > SECONDS_BETWEEN_FULL_STATE)
128                 {
129                         send_full_state();
130                 }
131         }
132
133         void send_full_state()
134         {
135                 *subject_ << monitor::message("/text") % text_;
136
137                 if (context_.video_channel != -1)
138                         *subject_ << monitor::message("/context/channel") % context_.video_channel;
139
140                 if (context_.layer != -1)
141                         *subject_ << monitor::message("/context/layer") % context_.layer;
142
143                 for (const auto& color : colors_)
144                         *subject_ << monitor::message("/color/" + color.first) % color.second;
145
146                 time_since_full_state_send_.restart();
147         }
148 };
149
150 spl::shared_ptr<monitor::subject> get_or_create_subject()
151 {
152         static auto diag_subject = []()
153         {
154                 auto subject = spl::make_shared<monitor::subject>("/diag");
155
156                 caspar::diagnostics::spi::register_sink_factory([=]()
157                 {
158                         return spl::make_shared<subject_graph>();
159                 });
160
161                 return subject;
162         }();
163
164         return diag_subject;
165 }
166
167 }}}