forked from sony/nmos-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_implementation.cpp
258 lines (207 loc) · 13.3 KB
/
node_implementation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "node_implementation.h"
#include "detail/for_each_reversed.h"
#include "nmos/activation_mode.h"
#include "nmos/connection_api.h"
#include "nmos/group_hint.h"
#include "nmos/model.h"
#include "nmos/node_resource.h"
#include "nmos/node_resources.h"
#include "nmos/sdp_utils.h"
#include "nmos/slog.h"
#include "nmos/thread_utils.h"
#include "nmos/transport.h"
#include "sdp/sdp.h"
// as part of activation, the sender /transportfile should be updated based on the active transport parameters
void set_connection_sender_transportfile(nmos::resource& connection_sender, const nmos::sdp_parameters& sdp_params);
// This is an example of how to integrate the nmos-cpp library with a device-specific underlying implementation.
// It constructs and inserts a node resource and some sub-resources into the model, based on the model settings,
// and then waits for sender/receiver activations or shutdown.
void node_implementation_thread(nmos::node_model& model, slog::base_gate& gate)
{
using web::json::value;
using web::json::value_of;
const auto seed_id = nmos::with_read_lock(model.mutex, [&] { return nmos::experimental::fields::seed_id(model.settings); });
auto node_id = nmos::make_repeatable_id(seed_id, U("/x-nmos/node/self"));
auto device_id = nmos::make_repeatable_id(seed_id, U("/x-nmos/node/device/0"));
auto source_id = nmos::make_repeatable_id(seed_id, U("/x-nmos/node/source/0"));
auto flow_id = nmos::make_repeatable_id(seed_id, U("/x-nmos/node/flow/0"));
auto sender_id = nmos::make_repeatable_id(seed_id, U("/x-nmos/node/sender/0"));
auto receiver_id = nmos::make_repeatable_id(seed_id, U("/x-nmos/node/receiver/0"));
auto lock = model.write_lock(); // in order to update the resources
// any delay between updates to the model resources is unnecessary
// this just serves as a slightly more realistic example!
const unsigned int delay_millis{ 10 };
const auto insert_resource_after = [&model, &lock](unsigned int milliseconds, nmos::resources& resources, nmos::resource&& resource, slog::base_gate& gate)
{
if (!nmos::details::wait_for(model.shutdown_condition, lock, std::chrono::milliseconds(milliseconds), [&] { return model.shutdown; }))
{
const std::pair<nmos::id, nmos::type> id_type{ resource.id, resource.type };
const bool success = insert_resource(resources, std::move(resource)).second;
if (success)
slog::log<slog::severities::info>(gate, SLOG_FLF) << "Updated model with " << id_type;
else
slog::log<slog::severities::severe>(gate, SLOG_FLF) << "Model update error: " << id_type;
slog::log<slog::severities::too_much_info>(gate, SLOG_FLF) << "Notifying node behaviour thread"; // and anyone else who cares...
model.notify();
}
};
const auto resolve_auto = [](const nmos::type& type, value& endpoint_active)
{
auto& transport_params = endpoint_active[nmos::fields::transport_params];
// "In some cases the behaviour is more complex, and may be determined by the vendor."
// See https://github.com/AMWA-TV/nmos-device-connection-management/blob/v1.0/docs/2.2.%20APIs%20-%20Server%20Side%20Implementation.md#use-of-auto
if (nmos::types::sender == type)
{
nmos::details::resolve_auto(transport_params[0], nmos::fields::source_ip, [] { return value::string(U("192.168.255.0")); });
nmos::details::resolve_auto(transport_params[1], nmos::fields::source_ip, [] { return value::string(U("192.168.255.1")); });
nmos::details::resolve_auto(transport_params[0], nmos::fields::destination_ip, [] { return value::string(U("239.255.255.0")); });
nmos::details::resolve_auto(transport_params[1], nmos::fields::destination_ip, [] { return value::string(U("239.255.255.1")); });
}
else // if (nmos::types::receiver == type)
{
nmos::details::resolve_auto(transport_params[0], nmos::fields::interface_ip, [] { return value::string(U("192.168.255.2")); });
nmos::details::resolve_auto(transport_params[1], nmos::fields::interface_ip, [] { return value::string(U("192.168.255.3")); });
}
nmos::resolve_auto(type, transport_params);
};
// example node
{
auto node = nmos::make_node(node_id, model.settings);
// add one example network interface
node.data[U("interfaces")] = value_of({ value_of({ { U("chassis_id"), value::null() }, { U("port_id"), U("ff-ff-ff-ff-ff-ff") }, { U("name"), U("example") } }) });
insert_resource_after(delay_millis, model.node_resources, std::move(node), gate);
}
// example device
insert_resource_after(delay_millis, model.node_resources, nmos::make_device(device_id, node_id, { sender_id }, { receiver_id }, model.settings), gate);
// example source, flow and sender
nmos::sdp_parameters sdp_params;
{
auto source = nmos::make_video_source(source_id, device_id, { 25, 1 }, model.settings);
auto flow = nmos::make_raw_video_flow(flow_id, source_id, device_id, model.settings);
// add example network interface binding for both primary and secondary
auto sender = nmos::make_sender(sender_id, flow_id, device_id, { U("example"), U("example") }, model.settings);
// add example "natural grouping" hint
web::json::push_back(sender.data[U("tags")][nmos::fields::group_hint], nmos::make_group_hint({ U("example"), U("sender 0") }));
sdp_params = nmos::make_sdp_parameters(source.data, flow.data, sender.data, { U("PRIMARY"), U("SECONDARY") });
auto connection_sender = nmos::make_connection_sender(sender_id, true);
resolve_auto(connection_sender.type, connection_sender.data[nmos::fields::endpoint_active]);
set_connection_sender_transportfile(connection_sender, sdp_params);
insert_resource_after(delay_millis, model.node_resources, std::move(source), gate);
insert_resource_after(delay_millis, model.node_resources, std::move(flow), gate);
insert_resource_after(delay_millis, model.node_resources, std::move(sender), gate);
insert_resource_after(delay_millis, model.connection_resources, std::move(connection_sender), gate);
}
// example receiver
{
// add example network interface binding for both primary and secondary
auto receiver = nmos::make_video_receiver(receiver_id, device_id, nmos::transports::rtp_mcast, { U("example"), U("example") }, model.settings);
// add example "natural grouping" hint
web::json::push_back(receiver.data[U("tags")][nmos::fields::group_hint], nmos::make_group_hint({ U("example"), U("receiver 0") }));
auto connection_receiver = nmos::make_connection_receiver(receiver_id, true);
resolve_auto(connection_receiver.type, connection_receiver.data[nmos::fields::endpoint_active]);
insert_resource_after(delay_millis, model.node_resources, std::move(receiver), gate);
insert_resource_after(delay_millis, model.connection_resources, std::move(connection_receiver), gate);
}
auto most_recent_update = nmos::tai_min();
auto earliest_scheduled_activation = (nmos::tai_clock::time_point::max)();
for (;;)
{
// wait for the thread to be interrupted because there may be new scheduled activations, or immediate activations to process
// or because the server is being shut down
// or because it's time for the next scheduled activation
model.wait_until(lock, earliest_scheduled_activation, [&] { return model.shutdown || most_recent_update < nmos::most_recent_update(model.connection_resources); });
if (model.shutdown) break;
auto& by_updated = model.connection_resources.get<nmos::tags::updated>();
// go through all connection resources
// process any immediate activations
// process any scheduled activations whose requested_time has passed
// identify the next scheduled activation
const auto now = nmos::tai_clock::now();
earliest_scheduled_activation = (nmos::tai_clock::time_point::max)();
bool notify = false;
// since modify reorders the resource in this index, use for_each_reversed
detail::for_each_reversed(by_updated.begin(), by_updated.end(), [&](const nmos::resource& resource)
{
if (!resource.has_data()) return;
const std::pair<nmos::id, nmos::type> id_type{ resource.id, resource.type };
auto& staged = nmos::fields::endpoint_staged(resource.data);
auto& staged_activation = nmos::fields::activation(staged);
auto& staged_mode_or_null = nmos::fields::mode(staged_activation);
if (staged_mode_or_null.is_null()) return;
const nmos::activation_mode staged_mode{ staged_mode_or_null.as_string() };
if (nmos::activation_modes::activate_scheduled_absolute == staged_mode ||
nmos::activation_modes::activate_scheduled_relative == staged_mode)
{
auto& staged_activation_time = nmos::fields::activation_time(staged_activation);
const auto scheduled_activation = nmos::time_point_from_tai(nmos::parse_version(staged_activation_time.as_string()));
if (scheduled_activation < now)
{
slog::log<slog::severities::info>(gate, SLOG_FLF) << "Processing scheduled activation for " << id_type;
}
else
{
if (scheduled_activation < earliest_scheduled_activation)
{
earliest_scheduled_activation = scheduled_activation;
}
return;
}
}
else if (nmos::activation_modes::activate_immediate == staged_mode)
{
// check for cancelled in-flight immediate activation
if (nmos::fields::requested_time(staged_activation).is_null()) return;
// check for processed in-flight immediate activation
if (!nmos::fields::activation_time(staged_activation).is_null()) return;
slog::log<slog::severities::info>(gate, SLOG_FLF) << "Processing immediate activation for " << id_type;
}
else
{
slog::log<slog::severities::severe>(gate, SLOG_FLF) << "Unexpected activation mode for " << id_type;
return;
}
const auto activation_time = nmos::tai_now();
bool active = false;
nmos::id connected_id;
// Update the IS-05 connection resource
nmos::modify_resource(model.connection_resources, resource.id, [&resolve_auto, &sdp_params, &activation_time, &active, &connected_id](nmos::resource& connection_resource)
{
const auto& type = connection_resource.type;
nmos::set_connection_resource_active(connection_resource, [&](web::json::value& endpoint_active)
{
resolve_auto(type, endpoint_active);
active = nmos::fields::master_enable(endpoint_active);
// Senders indicate the connected receiver_id, receivers indicate the connected sender_id
auto& connected_id_or_null = nmos::types::sender == type ? nmos::fields::receiver_id(endpoint_active) : nmos::fields::sender_id(endpoint_active);
if (!connected_id_or_null.is_null()) connected_id = connected_id_or_null.as_string();
}, activation_time);
if (nmos::types::sender == type) set_connection_sender_transportfile(connection_resource, sdp_params);
});
// Update the IS-04 resource
nmos::modify_resource(model.node_resources, resource.id, [&activation_time, &active, &connected_id](nmos::resource& resource)
{
nmos::set_resource_subscription(resource, active, connected_id, activation_time);
});
notify = true;
});
if ((nmos::tai_clock::time_point::max)() != earliest_scheduled_activation)
{
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Next scheduled activation is at " << nmos::make_version(nmos::tai_from_time_point(earliest_scheduled_activation))
<< " in about " << std::fixed << std::setprecision(3) << std::chrono::duration_cast<std::chrono::duration<double>>(earliest_scheduled_activation - now).count() << " seconds time";
}
if (notify)
{
slog::log<slog::severities::too_much_info>(gate, SLOG_FLF) << "Notifying node behaviour thread"; // and anyone else who cares...
model.notify();
}
most_recent_update = nmos::most_recent_update(model.connection_resources);
}
}
// as part of activation, the sender /transportfile should be updated based on the active transport parameters
void set_connection_sender_transportfile(nmos::resource& connection_sender, const nmos::sdp_parameters& sdp_params)
{
auto& transport_params = connection_sender.data[nmos::fields::endpoint_active][nmos::fields::transport_params];
auto session_description = nmos::make_session_description(sdp_params, transport_params);
auto sdp = utility::s2us(sdp::make_session_description(session_description));
connection_sender.data[nmos::fields::endpoint_transportfile] = nmos::make_connection_sender_transportfile(sdp);
}