]> git.sesse.net Git - casparcg/blob - protocol/osc/client.cpp
Improved read_fps.
[casparcg] / protocol / osc / client.cpp
1 /*
2 * Copyright 2013 Sveriges Television AB http://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 * Author: Helge Norberg, helge.norberg@svt.se
21 */
22
23 #include "../stdafx.h"
24
25 #include "client.h"
26
27 #include "oscpack/OscOutboundPacketStream.h"
28 #include "oscpack/OscHostEndianness.h"
29
30 #include <common/utility/string.h>
31 #include <common/exception/win32_exception.h>
32 #include <common/memory/endian.h>
33
34 #include <core/monitor/monitor.h>
35
36 #include <functional>
37 #include <vector>
38 #include <unordered_map>
39
40 #include <boost/asio.hpp>
41 #include <boost/foreach.hpp>
42 #include <boost/bind.hpp>
43 #include <boost/thread.hpp>
44
45 #include <tbb/spin_mutex.h>
46 #include <tbb/cache_aligned_allocator.h>
47
48 using namespace boost::asio::ip;
49
50 namespace caspar { namespace protocol { namespace osc {
51
52 template<typename T>
53 struct no_init_proxy
54 {
55     T value;
56
57     no_init_proxy() 
58         {
59                 static_assert(sizeof(no_init_proxy) == sizeof(T), "invalid size");
60         static_assert(__alignof(no_init_proxy) == __alignof(T), "invalid alignment");
61     }
62 };
63
64 typedef std::vector<no_init_proxy<char>, tbb::cache_aligned_allocator<no_init_proxy<char>>> byte_vector;
65
66 template<typename T>
67 struct param_visitor : public boost::static_visitor<void>
68 {
69         T& o;
70
71         param_visitor(T& o)
72                 : o(o)
73         {
74         }               
75                 
76         void operator()(const bool value)                                       {o << value;}
77         void operator()(const int32_t value)                            {o << static_cast<int64_t>(value);}
78         void operator()(const uint32_t value)                           {o << static_cast<int64_t>(value);}
79         void operator()(const int64_t value)                            {o << static_cast<int64_t>(value);}
80         void operator()(const uint64_t value)                           {o << static_cast<int64_t>(value);}
81         void operator()(const float value)                                      {o << value;}
82         void operator()(const double value)                                     {o << static_cast<float>(value);}
83         void operator()(const std::string& value)                       {o << value.c_str();}
84         void operator()(const std::wstring& value)                      {o << narrow(value).c_str();}
85         void operator()(const std::vector<int8_t>& value)       {o << ::osc::Blob(value.data(), static_cast<unsigned long>(value.size()));}
86 };
87
88 void write_osc_event(byte_vector& destination, const core::monitor::message& e)
89 {               
90         destination.resize(4096);
91
92         ::osc::OutboundPacketStream o(reinterpret_cast<char*>(destination.data()), static_cast<unsigned long>(destination.size()));
93         o << ::osc::BeginMessage(e.path().c_str());
94                                 
95         param_visitor<decltype(o)> param_visitor(o);
96         BOOST_FOREACH(const auto& data, e.data())
97                 boost::apply_visitor(param_visitor, data);
98                                 
99         o << ::osc::EndMessage;
100                 
101         destination.resize(o.Size());
102 }
103
104 byte_vector write_osc_bundle_start()
105 {
106         byte_vector destination;
107         destination.resize(16);
108
109         ::osc::OutboundPacketStream o(reinterpret_cast<char*>(destination.data()), static_cast<unsigned long>(destination.size()));
110         o << ::osc::BeginBundle();
111
112         destination.resize(o.Size());
113
114         return destination;
115 }
116
117 void write_osc_bundle_element_start(byte_vector& destination, const byte_vector& message)
118 {               
119         destination.resize(4);
120
121         int32_t* bundle_element_size = reinterpret_cast<int32_t*>(destination.data());
122
123 #ifdef OSC_HOST_LITTLE_ENDIAN
124         *bundle_element_size = swap_byte_order(static_cast<int32_t>(message.size()));
125 #else
126         *bundle_element_size = static_cast<int32_t>(bundle.size());
127 #endif
128 }
129
130 struct client::impl : public std::enable_shared_from_this<client::impl>, core::monitor::sink
131 {
132         udp::socket socket_;
133         tbb::spin_mutex                                                                 endpoints_mutex_;
134         std::map<udp::endpoint, int>                                    reference_counts_by_endpoint_;
135
136         std::unordered_map<std::string, byte_vector>    updates_;
137         boost::mutex                                                                    updates_mutex_;                                                         
138         boost::condition_variable                                               updates_cond_;
139
140         tbb::atomic<bool>                                                               is_running_;
141
142         boost::thread                                                                   thread_;
143         
144 public:
145         impl(boost::asio::io_service& service)
146                 : socket_(service, udp::v4())
147                 , thread_(boost::bind(&impl::run, this))
148         {
149         }
150
151         ~impl()
152         {
153                 is_running_ = false;
154
155                 updates_cond_.notify_one();
156
157                 thread_.join();
158         }
159
160         std::shared_ptr<void> get_subscription_token(
161                         const boost::asio::ip::udp::endpoint& endpoint)
162         {
163                 tbb::spin_mutex::scoped_lock lock(endpoints_mutex_);
164
165                 ++reference_counts_by_endpoint_[endpoint];
166
167                 std::weak_ptr<impl> weak_self = shared_from_this();
168
169                 return std::shared_ptr<void>(nullptr, [weak_self, endpoint] (void*)
170                 {
171                         auto strong = weak_self.lock();
172
173                         if (!strong)
174                                 return;
175
176                         auto& self = *strong;
177
178                         tbb::spin_mutex::scoped_lock lock(self.endpoints_mutex_);
179
180                         int reference_count_after =
181                                 --self.reference_counts_by_endpoint_[endpoint];
182
183                         if (reference_count_after == 0)
184                                 self.reference_counts_by_endpoint_.erase(endpoint);
185                 });
186         }
187 private:
188         void propagate(const core::monitor::message& msg)
189         {
190                 boost::lock_guard<boost::mutex> lock(updates_mutex_);
191
192                 try 
193                 {
194                         write_osc_event(updates_[msg.path()], msg);
195                 }
196                 catch(...)
197                 {
198                         CASPAR_LOG_CURRENT_EXCEPTION();
199                         updates_.erase(msg.path());
200                 }
201
202                 updates_cond_.notify_one();
203         }
204
205         template<typename T>
206         void do_send(
207                         const T& buffers, const std::vector<udp::endpoint>& destinations)
208         {
209                 boost::system::error_code ec;
210
211                 BOOST_FOREACH(const auto& endpoint, destinations)
212                         socket_.send_to(buffers, endpoint, 0, ec);
213         }
214
215         void run()
216         {
217                 // http://stackoverflow.com/questions/14993000/the-most-reliable-and-efficient-udp-packet-size
218                 const int SAFE_DATAGRAM_SIZE = 508;
219
220                 try
221                 {
222                         is_running_ = true;
223
224                         std::unordered_map<std::string, byte_vector> updates;
225                         std::vector<udp::endpoint> destinations;
226                         const byte_vector bundle_header = write_osc_bundle_start();
227                         std::vector<byte_vector> element_headers;
228
229                         while (is_running_)
230                         {               
231                                 updates.clear();
232                                 destinations.clear();
233
234                                 {                       
235                                         boost::unique_lock<boost::mutex> cond_lock(updates_mutex_);
236
237                                         if (updates_.empty())
238                                                 updates_cond_.wait(cond_lock);
239
240                                         std::swap(updates, updates_);
241                                 }
242
243                                 {
244                                         tbb::spin_mutex::scoped_lock lock(endpoints_mutex_);
245
246                                         BOOST_FOREACH(const auto& endpoint, reference_counts_by_endpoint_)
247                                                 destinations.push_back(endpoint.first);
248                                 }
249
250                                 if (destinations.empty())
251                                         continue;
252
253                                 std::vector<boost::asio::const_buffers_1> buffers;
254                                 element_headers.resize(
255                                                 std::max(element_headers.size(), updates.size()));
256
257                                 int i = 0;
258                                 int datagram_size = bundle_header.size();
259                                 buffers.push_back(boost::asio::buffer(bundle_header));
260
261                                 BOOST_FOREACH(const auto& slot, updates)
262                                 {
263                                         write_osc_bundle_element_start(element_headers[i], slot.second);
264                                         const auto& headers = element_headers;
265
266                                         auto size_of_element = headers[i].size() + slot.second.size();
267         
268                                         if (datagram_size + size_of_element >= SAFE_DATAGRAM_SIZE)
269                                         {
270                                                 do_send(buffers, destinations);
271                                                 buffers.clear();
272                                                 buffers.push_back(boost::asio::buffer(bundle_header));
273                                                 datagram_size = bundle_header.size();
274                                         }
275
276                                         buffers.push_back(boost::asio::buffer(headers[i]));
277                                         buffers.push_back(boost::asio::buffer(slot.second));
278
279                                         datagram_size += size_of_element;
280                                         ++i;
281                                 }
282                         
283                                 if (!buffers.empty())
284                                         do_send(buffers, destinations);
285                         }
286                 }
287                 catch (...)
288                 {
289                         CASPAR_LOG_CURRENT_EXCEPTION();
290                 }
291         }
292 };
293
294 client::client(boost::asio::io_service& service) 
295         : impl_(new impl(service))
296 {
297 }
298
299 client::client(client&& other)
300         : impl_(std::move(other.impl_))
301 {
302 }
303
304 client& client::operator=(client&& other)
305 {
306         impl_ = std::move(other.impl_);
307         return *this;
308 }
309
310 client::~client()
311 {
312 }
313
314 std::shared_ptr<void> client::get_subscription_token(
315                         const boost::asio::ip::udp::endpoint& endpoint)
316 {
317         return impl_->get_subscription_token(endpoint);
318 }
319
320 safe_ptr<core::monitor::sink> client::sink()
321 {
322         return impl_;
323 }
324
325 }}}