forked from CasparCG/server
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
478 lines (382 loc) · 16.4 KB
/
main.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
*
* This file is part of CasparCG (www.casparcg.com).
*
* CasparCG is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CasparCG is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Robert Nagy, ronag89@gmail.com
*/
#if defined _DEBUG && defined _MSC_VER
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#include <stdlib.h>
#endif
#include "included_modules.h"
#include "platform_specific.h"
#include "server.h"
#include <common/env.h>
#include <common/except.h>
#include <common/log.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/filesystem.hpp>
#include <boost/locale.hpp>
#include <boost/property_tree/detail/file_parser_error.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/stacktrace.hpp>
#include <core/diagnostics/call_context.h>
#include <core/diagnostics/osd_graph.h>
#include <atomic>
#include <future>
#include <thread>
#include <clocale>
#include <csignal>
#include "napi.h"
#include "./channel_state.h"
#include "./conv.h"
#include "./log_receiver.h"
#include "./operations/base.h"
#include "./operations/cg.h"
#include "./operations/channel.h"
#include "./operations/consumer.h"
#include "./operations/mixer.h"
#include "./operations/producer.h"
#include "./operations/stage.h"
#include "./operations/util.h"
#include <iostream>
namespace caspar {
void setup_global_locale()
{
boost::locale::generator gen;
#if BOOST_VERSION >= 108100
gen.categories(boost::locale::category_t::codepage);
#else
gen.categories(boost::locale::codepage_facet);
#endif
std::locale::global(gen(""));
// sscanf is used in for example FFmpeg where we want decimals to be parsed as .
std::setlocale(LC_ALL, "C");
}
void print_info()
{
CASPAR_LOG(info) << L"############################################################################";
CASPAR_LOG(info) << L"CasparCG Server is distributed by the Swedish Broadcasting Corporation (SVT)";
CASPAR_LOG(info) << L"under the GNU General Public License GPLv3 or higher.";
CASPAR_LOG(info) << L"Please see LICENSE.TXT for details.";
CASPAR_LOG(info) << L"http://www.casparcg.com/";
CASPAR_LOG(info) << L"############################################################################";
CASPAR_LOG(info) << L"Starting CasparCG Video and Graphics Playout Server " << env::version();
}
// void signal_handler(int signum)
// {
// ::signal(signum, SIG_DFL);
// boost::stacktrace::safe_dump_to("./backtrace.dump");
// ::raise(SIGABRT);
// }
// void terminate_handler()
// {
// try {
// std::rethrow_exception(std::current_exception());
// } catch (...) {
// CASPAR_LOG_CURRENT_EXCEPTION();
// }
// std::abort();
// }
} // namespace caspar
Napi::Value InitServer(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
CasparCgInstanceData* instance_data = env.GetInstanceData<CasparCgInstanceData>();
if (!instance_data) {
Napi::Error::New(env, "Module is not initialised correctly. This is likely a bug!")
.ThrowAsJavaScriptException();
return env.Null();
}
if (instance_data->caspar_server) {
Napi::Error::New(env, "Server is already running, it must be stopped before you can call init again")
.ThrowAsJavaScriptException();
return env.Null();
}
// TODO - ensure not in a worker, or that multiple servers arent run in the same process
if (info.Length() < 1 || !info[0].IsObject()) {
Napi::Error::New(env, "Expected configuration object").ThrowAsJavaScriptException();
return env.Null();
}
if (info.Length() < 2 || !info[1].IsFunction()) {
Napi::Error::New(env, "Expected log receiver").ThrowAsJavaScriptException();
return env.Null();
}
if (info.Length() >= 3) {
if (!info[2].IsFunction()) {
Napi::Error::New(env, "Expected callback for state updates").ThrowAsJavaScriptException();
return env.Null();
}
auto callback = info[2].As<Napi::Function>();
// TODO - cleanup if something throws
instance_data->osc_sender = create_channel_state_emitter(env, callback);
CASPAR_LOG(info) << "has sender " << !!instance_data->osc_sender;
}
auto configuration = info[0].As<Napi::Object>();
auto logReceiver = info[1].As<Napi::Function>();
using namespace caspar;
// setup_global_locale();
setup_prerequisites();
// Increase process priority.
increase_process_priority();
try {
boost::property_tree::wptree config_tree;
if (!NapiObjectToBoostPropertyTree(env, configuration, config_tree)) {
return env.Null();
}
auto paths_config = configuration.Get("paths");
if (!paths_config.IsObject()) {
Napi::Error::New(env, "Expected paths to be an object").ThrowAsJavaScriptException();
return env.Null();
}
auto paths_object = paths_config.As<Napi::Object>();
std::wstring initial_path = u16(paths_object.Get("initial").As<Napi::String>().Utf8Value());
std::wstring media_path = u16(paths_object.Get("media").As<Napi::String>().Utf8Value());
std::wstring template_path = u16(paths_object.Get("template").As<Napi::String>().Utf8Value());
log::remove_all_sinks(); // Ensure there isn't an existing logger
log::add_nodejs_sink(create_log_receiver_emitter(env, logReceiver));
env::configure(initial_path, media_path, template_path, config_tree);
{
std::wstring target_level = env::properties().get(L"configuration.log-level", L"info");
if (!log::set_log_level(target_level)) {
log::set_log_level(L"info");
std::wcout << L"Failed to set log level [" << target_level << L"]" << std::endl;
}
}
print_info();
// Create server object which initializes channels, protocols and controllers.
instance_data->caspar_server.reset(new server());
// For example CEF resets the global locale, so this is to reset it back to "our" preference.
setup_global_locale();
instance_data->caspar_server->start();
} catch (boost::property_tree::file_parser_error& e) {
Napi::Error::New(env, "Please check the configuration for errors").ThrowAsJavaScriptException();
} catch (user_error&) {
CASPAR_LOG_CURRENT_EXCEPTION();
Napi::Error::New(env, "Please check the configuration for errors").ThrowAsJavaScriptException();
} catch (...) {
CASPAR_LOG_CURRENT_EXCEPTION();
Napi::Error::New(env, "Unhandled exception").ThrowAsJavaScriptException();
}
return env.Null();
}
Napi::Value StopServer(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
CasparCgInstanceData* instance_data = env.GetInstanceData<CasparCgInstanceData>();
if (!instance_data) {
Napi::Error::New(env, "Module is not initialised correctly. This is likely a bug!")
.ThrowAsJavaScriptException();
return env.Null();
}
if (!instance_data->caspar_server) {
// Nothing to do
return env.Null();
}
instance_data->caspar_server = nullptr;
instance_data->osc_sender = nullptr;
// TODO: some log lines can be lost at exit for some reason
// Ensure references to log callback are disposed
caspar::log::remove_all_sinks();
return env.Null();
}
Napi::Value AddChannelConsumer(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
CasparCgInstanceData* instance_data = env.GetInstanceData<CasparCgInstanceData>();
if (!instance_data) {
Napi::Error::New(env, "Module is not initialised correctly. This is likely a bug!")
.ThrowAsJavaScriptException();
return env.Null();
}
if (!instance_data->caspar_server) {
Napi::Error::New(env, "Server is not running").ThrowAsJavaScriptException();
return env.Null();
}
if (info.Length() < 2 || !info[0].IsNumber() || !info[1].IsObject()) {
Napi::Error::New(env, "Expected channel and config").ThrowAsJavaScriptException();
return env.Null();
}
auto channel_index = info[0].As<Napi::Number>();
auto config = info[1].As<Napi::Object>();
boost::property_tree::wptree boost_config;
if (!NapiObjectToBoostPropertyTree(env, config, boost_config)) {
return env.Null();
}
int port = instance_data->caspar_server->add_consumer_from_xml(channel_index.Int32Value() - 1, boost_config);
if (port == -1) {
Napi::Error::New(env, "Invalid consumer config").ThrowAsJavaScriptException();
return env.Null();
}
return Napi::Number::New(env, port);
}
Napi::Value AddChannel(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
CasparCgInstanceData* instance_data = env.GetInstanceData<CasparCgInstanceData>();
if (!instance_data) {
Napi::Error::New(env, "Module is not initialised correctly. This is likely a bug!")
.ThrowAsJavaScriptException();
return env.Null();
}
if (!instance_data->caspar_server) {
Napi::Error::New(env, "Server is not running").ThrowAsJavaScriptException();
return env.Null();
}
if (info.Length() < 1 || !info[0].IsString()) {
Napi::Error::New(env, "Expected video mode").ThrowAsJavaScriptException();
return env.Null();
}
auto video_mode = info[0].As<Napi::String>();
// TODO - calling this once amcp has begun is not safe
int channel_index =
instance_data->caspar_server->add_channel(caspar::u16(video_mode.Utf8Value()), instance_data->osc_sender);
if (channel_index == -1) {
Napi::Error::New(env, "Failed to add channel").ThrowAsJavaScriptException();
return env.Null();
}
return Napi::Number::New(env, channel_index);
}
Napi::Value OpenDiag(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
CasparCgInstanceData* instance_data = env.GetInstanceData<CasparCgInstanceData>();
if (!instance_data) {
Napi::Error::New(env, "Module is not initialised correctly. This is likely a bug!")
.ThrowAsJavaScriptException();
return env.Null();
}
if (!instance_data->caspar_server) {
Napi::Error::New(env, "Server is not running").ThrowAsJavaScriptException();
return env.Null();
}
caspar::core::diagnostics::osd::show_graphs(true);
return env.Null();
}
Napi::Value CallStageMethod(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
CasparCgInstanceData* instance_data = env.GetInstanceData<CasparCgInstanceData>();
if (!instance_data) {
Napi::Error::New(env, "Module is not initialised correctly. This is likely a bug!")
.ThrowAsJavaScriptException();
return env.Null();
}
if (!instance_data->caspar_server) {
Napi::Error::New(env, "Server is not running").ThrowAsJavaScriptException();
return env.Null();
}
if (info.Length() < 1 || !info[0].IsString()) {
Napi::Error::New(env, "Expected command").ThrowAsJavaScriptException();
return env.Null();
}
auto command = info[0].As<Napi::String>().Utf8Value();
if (command == "play") {
return StagePlay(info, instance_data);
} else if (command == "preview") {
return StagePreview(info, instance_data);
} else if (command == "load") {
return StageLoad(info, instance_data);
} else if (command == "pause") {
return StagePause(info, instance_data);
} else if (command == "resume") {
return StageResume(info, instance_data);
} else if (command == "stop") {
return StageStop(info, instance_data);
} else if (command == "clear") {
return StageClear(info, instance_data);
} else if (command == "call") {
return StageCall(info, instance_data);
} else if (command == "callbg") {
return StageCallBg(info, instance_data);
} else if (command == "swapChannel") {
return StageSwapChannel(info, instance_data);
} else if (command == "swapLayer") {
return StageSwapLayer(info, instance_data);
} else {
Napi::Error::New(env, "Unknown command").ThrowAsJavaScriptException();
return env.Null();
}
}
Napi::Value CallCgMethod(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
CasparCgInstanceData* instance_data = env.GetInstanceData<CasparCgInstanceData>();
if (!instance_data) {
Napi::Error::New(env, "Module is not initialised correctly. This is likely a bug!")
.ThrowAsJavaScriptException();
return env.Null();
}
if (!instance_data->caspar_server) {
Napi::Error::New(env, "Server is not running").ThrowAsJavaScriptException();
return env.Null();
}
if (info.Length() < 1 || !info[0].IsString()) {
Napi::Error::New(env, "Expected command").ThrowAsJavaScriptException();
return env.Null();
}
auto command = info[0].As<Napi::String>().Utf8Value();
if (command == "add") {
return CgAdd(info, instance_data);
} else if (command == "play") {
return CgPlay(info, instance_data);
} else if (command == "stop") {
return CgStop(info, instance_data);
} else if (command == "next") {
return CgNext(info, instance_data);
} else if (command == "remove") {
return CgRemove(info, instance_data);
} else if (command == "invoke") {
return CgInvoke(info, instance_data);
} else if (command == "update") {
return CgUpdate(info, instance_data);
} else {
Napi::Error::New(env, "Unknown command").ThrowAsJavaScriptException();
return env.Null();
}
}
Napi::Object Init(Napi::Env env, Napi::Object exports)
{
CasparCgInstanceData* instance_data = new CasparCgInstanceData;
env.SetInstanceData(instance_data); // TODO - cleanup
instance_data->unused_producer = NodeUnusedProducer::Init(env, exports);
exports.Set(Napi::String::New(env, "version"), Napi::String::New(env, caspar::u8(caspar::env::version())));
exports.Set(Napi::String::New(env, "init"), Napi::Function::New(env, InitServer));
exports.Set(Napi::String::New(env, "stop"), Napi::Function::New(env, StopServer));
exports.Set(Napi::String::New(env, "ConfigAddCustomVideoFormat"),
Napi::Function::New(env, ConfigAddCustomVideoFormat));
exports.Set(Napi::String::New(env, "CreateProducer"), Napi::Function::New(env, CreateProducer));
exports.Set(Napi::String::New(env, "CreateStingTransition"), Napi::Function::New(env, CreateStingTransition));
exports.Set(Napi::String::New(env, "CreateBasicTransition"), Napi::Function::New(env, CreateBasicTransition));
exports.Set(Napi::String::New(env, "AddConsumer"), Napi::Function::New(env, AddConsumer));
exports.Set(Napi::String::New(env, "RemoveConsumerByPort"), Napi::Function::New(env, RemoveConsumerByPort));
exports.Set(Napi::String::New(env, "RemoveConsumerByParams"), Napi::Function::New(env, RemoveConsumerByParams));
exports.Set(Napi::String::New(env, "AddChannelConsumer"), Napi::Function::New(env, AddChannelConsumer));
exports.Set(Napi::String::New(env, "AddChannel"), Napi::Function::New(env, AddChannel));
exports.Set(Napi::String::New(env, "CallStageMethod"), Napi::Function::New(env, CallStageMethod));
exports.Set(Napi::String::New(env, "CallCgMethod"), Napi::Function::New(env, CallCgMethod));
exports.Set(Napi::String::New(env, "SetChannelFormat"), Napi::Function::New(env, SetChannelFormat));
exports.Set(Napi::String::New(env, "GetLayerMixerProperties"), Napi::Function::New(env, GetLayerMixerProperties));
exports.Set(Napi::String::New(env, "ApplyTransforms"), Napi::Function::New(env, ApplyTransforms));
exports.Set(Napi::String::New(env, "OpenDiag"), Napi::Function::New(env, OpenDiag));
exports.Set(Napi::String::New(env, "FindCaseInsensitive"), Napi::Function::New(env, FindCaseInsensitive));
return exports;
}
NODE_API_MODULE(casparcg, Init)