2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Nicklas P Andersson
22 #include "../StdAfx.h"
25 #pragma warning (push, 1) // TODO: Legacy code, just disable warnings
28 #include "AMCPCommandsImpl.h"
29 #include "AMCPProtocolStrategy.h"
31 #include <common/env.h>
33 #include <common/log.h>
34 #include <common/param.h>
35 #include <common/os/system_info.h>
36 #include <common/os/filesystem.h>
37 #include <common/base64.h>
39 #include <core/producer/frame_producer.h>
40 #include <core/video_format.h>
41 #include <core/producer/transition/transition_producer.h>
42 #include <core/frame/frame_transform.h>
43 #include <core/producer/stage.h>
44 #include <core/producer/layer.h>
45 #include <core/mixer/mixer.h>
46 #include <core/consumer/output.h>
47 #include <core/thumbnail_generator.h>
48 #include <core/producer/media_info/media_info.h>
49 #include <core/producer/media_info/media_info_repository.h>
50 #include <core/diagnostics/call_context.h>
51 #include <core/diagnostics/osd_graph.h>
52 #include <core/system_info_provider.h>
54 #include <modules/reroute/producer/reroute_producer.h>
63 #include <boost/date_time/posix_time/posix_time.hpp>
64 #include <boost/lexical_cast.hpp>
65 #include <boost/algorithm/string.hpp>
66 #include <boost/filesystem.hpp>
67 #include <boost/filesystem/fstream.hpp>
68 #include <boost/regex.hpp>
69 #include <boost/property_tree/xml_parser.hpp>
70 #include <boost/locale.hpp>
71 #include <boost/range/adaptor/transformed.hpp>
72 #include <boost/range/algorithm/copy.hpp>
73 #include <boost/archive/iterators/base64_from_binary.hpp>
74 #include <boost/archive/iterators/insert_linebreaks.hpp>
75 #include <boost/archive/iterators/transform_width.hpp>
77 #include <tbb/concurrent_unordered_map.h>
81 102 [action] Information that [action] has happened
82 101 [action] Information that [action] has happened plus one row of data
84 202 [command] OK [command] has been executed
85 201 [command] OK [command] has been executed, plus one row of data
86 200 [command] OK [command] has been executed, plus multiple lines of data. ends with an empty line
88 400 ERROR the command could not be understood
89 401 [command] ERROR invalid/missing channel
90 402 [command] ERROR parameter missing
91 403 [command] ERROR invalid parameter
92 404 [command] ERROR file not found
94 500 FAILED internal error
95 501 [command] FAILED internal error
96 502 [command] FAILED could not read file
97 503 [command] FAILED access denied
99 600 [command] FAILED [command] not implemented
102 namespace caspar { namespace protocol {
104 using namespace core;
106 std::wstring read_file_base64(const boost::filesystem::path& file)
108 using namespace boost::archive::iterators;
110 boost::filesystem::ifstream filestream(file, std::ios::binary);
115 auto length = boost::filesystem::file_size(file);
116 std::vector<char> bytes;
117 bytes.resize(length);
118 filestream.read(bytes.data(), length);
120 std::string result(to_base64(bytes.data(), length));
121 return std::wstring(result.begin(), result.end());
124 std::wstring read_utf8_file(const boost::filesystem::path& file)
126 std::wstringstream result;
127 boost::filesystem::wifstream filestream(file);
134 result << filestream.rdbuf();
140 std::wstring read_latin1_file(const boost::filesystem::path& file)
142 boost::locale::generator gen;
143 gen.locale_cache_enabled(true);
144 gen.categories(boost::locale::codepage_facet);
146 std::stringstream result_stream;
147 boost::filesystem::ifstream filestream(file);
148 filestream.imbue(gen("en_US.ISO8859-1"));
153 result_stream << filestream.rdbuf();
156 std::string result = result_stream.str();
157 std::wstring widened_result;
159 // The first 255 codepoints in unicode is the same as in latin1
161 result | boost::adaptors::transformed(
162 [](char c) { return static_cast<unsigned char>(c); }),
163 std::back_inserter(widened_result));
165 return widened_result;
168 std::wstring read_file(const boost::filesystem::path& file)
170 static const uint8_t BOM[] = {0xef, 0xbb, 0xbf};
172 if (!boost::filesystem::exists(file))
177 if (boost::filesystem::file_size(file) >= 3)
179 boost::filesystem::ifstream bom_stream(file);
182 bom_stream.read(header, 3);
185 if (std::memcmp(BOM, header, 3) == 0)
186 return read_utf8_file(file);
189 return read_latin1_file(file);
192 std::wstring MediaInfo(const boost::filesystem::path& path, const spl::shared_ptr<media_info_repository>& media_info_repo)
194 if (!boost::filesystem::is_regular_file(path))
197 auto media_info = media_info_repo->get(path.wstring());
202 auto is_not_digit = [](char c){ return std::isdigit(c) == 0; };
204 auto relativePath = boost::filesystem::path(path.wstring().substr(env::media_folder().size() - 1, path.wstring().size()));
206 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(path)));
207 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), is_not_digit), writeTimeStr.end());
208 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
210 auto sizeStr = boost::lexical_cast<std::wstring>(boost::filesystem::file_size(path));
211 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), is_not_digit), sizeStr.end());
212 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
214 auto str = relativePath.replace_extension(L"").generic_wstring();
215 if (str[0] == '\\' || str[0] == '/')
216 str = std::wstring(str.begin() + 1, str.end());
218 return std::wstring()
220 + L"\" " + media_info->clip_type +
222 + L" " + writeTimeWStr +
223 + L" " + boost::lexical_cast<std::wstring>(media_info->duration) +
224 + L" " + boost::lexical_cast<std::wstring>(media_info->time_base.numerator()) + L"/" + boost::lexical_cast<std::wstring>(media_info->time_base.denominator())
228 std::wstring ListMedia(const spl::shared_ptr<media_info_repository>& media_info_repo)
230 std::wstringstream replyString;
231 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end; ++itr)
232 replyString << MediaInfo(itr->path(), media_info_repo);
234 return boost::to_upper_copy(replyString.str());
237 std::wstring ListTemplates(const spl::shared_ptr<core::cg_producer_registry>& cg_registry)
239 std::wstringstream replyString;
241 for (boost::filesystem::recursive_directory_iterator itr(env::template_folder()), end; itr != end; ++itr)
243 if(boost::filesystem::is_regular_file(itr->path()) && cg_registry->is_cg_extension(itr->path().extension().wstring()))
245 auto relativePath = boost::filesystem::path(itr->path().wstring().substr(env::template_folder().size()-1, itr->path().wstring().size()));
247 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(itr->path())));
248 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), [](char c){ return std::isdigit(c) == 0;}), writeTimeStr.end());
249 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
251 auto sizeStr = boost::lexical_cast<std::string>(boost::filesystem::file_size(itr->path()));
252 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), [](char c){ return std::isdigit(c) == 0;}), sizeStr.end());
254 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
256 auto dir = relativePath.parent_path();
257 auto file = boost::to_upper_copy(relativePath.filename().wstring());
258 relativePath = dir / file;
260 auto str = relativePath.replace_extension(L"").generic_wstring();
261 boost::trim_if(str, boost::is_any_of("\\/"));
263 replyString << L"\"" << str
264 << L"\" " << sizeWStr
265 << L" " << writeTimeWStr
269 return replyString.str();
274 void AMCPCommand::SendReply()
276 if(replyString_.empty())
279 client_->send(std::move(replyString_));
282 bool DiagnosticsCommand::DoExecute()
286 core::diagnostics::osd::show_graphs(true);
288 SetReplyString(L"202 DIAG OK\r\n");
294 CASPAR_LOG_CURRENT_EXCEPTION();
295 SetReplyString(L"502 DIAG FAILED\r\n");
300 bool ChannelGridCommand::DoExecute()
303 auto self = channels().back().channel;
305 core::diagnostics::scoped_call_context save;
306 core::diagnostics::call_context::for_thread().video_channel = channels().size();
308 std::vector<std::wstring> params;
309 params.push_back(L"SCREEN");
310 params.push_back(L"0");
311 params.push_back(L"NAME");
312 params.push_back(L"Channel Grid Window");
313 auto screen = create_consumer(params, &self->stage());
315 self->output().add(screen);
317 for (auto& channel : channels())
319 if(channel.channel != self)
321 core::diagnostics::call_context::for_thread().layer = index;
322 auto producer = reroute::create_producer(*channel.channel);
323 self->stage().load(index, producer, false);
324 self->stage().play(index);
329 int n = channels().size()-1;
330 double delta = 1.0/static_cast<double>(n);
331 for(int x = 0; x < n; ++x)
333 for(int y = 0; y < n; ++y)
336 auto transform = [=](frame_transform transform) -> frame_transform
338 transform.image_transform.fill_translation[0] = x*delta;
339 transform.image_transform.fill_translation[1] = y*delta;
340 transform.image_transform.fill_scale[0] = delta;
341 transform.image_transform.fill_scale[1] = delta;
342 transform.image_transform.clip_translation[0] = x*delta;
343 transform.image_transform.clip_translation[1] = y*delta;
344 transform.image_transform.clip_scale[0] = delta;
345 transform.image_transform.clip_scale[1] = delta;
348 self->stage().apply_transform(index, transform);
355 bool CallCommand::DoExecute()
357 //Perform loading of the clip
360 auto result = channel()->stage().call(layer_index(), parameters());
362 // TODO: because of std::async deferred timed waiting does not work
364 /*auto wait_res = result.wait_for(std::chrono::seconds(2));
365 if (wait_res == std::future_status::timeout)
366 CASPAR_THROW_EXCEPTION(timed_out());*/
368 std::wstringstream replyString;
369 if(result.get().empty())
370 replyString << L"202 CALL OK\r\n";
372 replyString << L"201 CALL OK\r\n" << result.get() << L"\r\n";
374 SetReplyString(replyString.str());
380 CASPAR_LOG_CURRENT_EXCEPTION();
381 SetReplyString(L"502 CALL FAILED\r\n");
386 tbb::concurrent_unordered_map<int, std::vector<stage::transform_tuple_t>> deferred_transforms;
388 core::frame_transform MixerCommand::get_current_transform()
390 return channel()->stage().get_current_transform(layer_index()).get();
393 bool MixerCommand::DoExecute()
395 //Perform loading of the clip
398 bool defer = boost::iequals(parameters().back(), L"DEFER");
400 parameters().pop_back();
402 std::vector<stage::transform_tuple_t> transforms;
404 if(boost::iequals(parameters()[0], L"KEYER") || boost::iequals(parameters()[0], L"IS_KEY"))
406 if (parameters().size() == 1)
407 return reply_value([](const frame_transform& t) { return t.image_transform.is_key ? 1 : 0; });
409 bool value = boost::lexical_cast<int>(parameters().at(1));
410 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
412 transform.image_transform.is_key = value;
416 else if(boost::iequals(parameters()[0], L"OPACITY"))
418 if (parameters().size() == 1)
419 return reply_value([](const frame_transform& t) { return t.image_transform.opacity; });
421 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
422 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
424 double value = boost::lexical_cast<double>(parameters().at(1));
426 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
428 transform.image_transform.opacity = value;
430 }, duration, tween));
432 else if (boost::iequals(parameters()[0], L"ANCHOR"))
434 if (parameters().size() == 1)
436 auto transform = get_current_transform().image_transform;
437 auto anchor = transform.anchor;
440 + boost::lexical_cast<std::wstring>(anchor[0]) + L" "
441 + boost::lexical_cast<std::wstring>(anchor[1]) + L"\r\n");
445 int duration = parameters().size() > 3 ? boost::lexical_cast<int>(parameters()[3]) : 0;
446 std::wstring tween = parameters().size() > 4 ? parameters()[4] : L"linear";
447 double x = boost::lexical_cast<double>(parameters().at(1));
448 double y = boost::lexical_cast<double>(parameters().at(2));
450 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) mutable -> frame_transform
452 transform.image_transform.anchor[0] = x;
453 transform.image_transform.anchor[1] = y;
455 }, duration, tween));
457 else if (boost::iequals(parameters()[0], L"FILL") || boost::iequals(parameters()[0], L"FILL_RECT"))
459 if (parameters().size() == 1)
461 auto transform = get_current_transform().image_transform;
462 auto translation = transform.fill_translation;
463 auto scale = transform.fill_scale;
466 + boost::lexical_cast<std::wstring>(translation[0]) + L" "
467 + boost::lexical_cast<std::wstring>(translation[1]) + L" "
468 + boost::lexical_cast<std::wstring>(scale[0]) + L" "
469 + boost::lexical_cast<std::wstring>(scale[1]) + L"\r\n");
473 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
474 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
475 double x = boost::lexical_cast<double>(parameters().at(1));
476 double y = boost::lexical_cast<double>(parameters().at(2));
477 double x_s = boost::lexical_cast<double>(parameters().at(3));
478 double y_s = boost::lexical_cast<double>(parameters().at(4));
480 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) mutable -> frame_transform
482 transform.image_transform.fill_translation[0] = x;
483 transform.image_transform.fill_translation[1] = y;
484 transform.image_transform.fill_scale[0] = x_s;
485 transform.image_transform.fill_scale[1] = y_s;
487 }, duration, tween));
489 else if(boost::iequals(parameters()[0], L"CLIP") || boost::iequals(parameters()[0], L"CLIP_RECT"))
491 if (parameters().size() == 1)
493 auto transform = get_current_transform().image_transform;
494 auto translation = transform.clip_translation;
495 auto scale = transform.clip_scale;
498 + boost::lexical_cast<std::wstring>(translation[0]) + L" "
499 + boost::lexical_cast<std::wstring>(translation[1]) + L" "
500 + boost::lexical_cast<std::wstring>(scale[0]) + L" "
501 + boost::lexical_cast<std::wstring>(scale[1]) + L"\r\n");
505 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
506 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
507 double x = boost::lexical_cast<double>(parameters().at(1));
508 double y = boost::lexical_cast<double>(parameters().at(2));
509 double x_s = boost::lexical_cast<double>(parameters().at(3));
510 double y_s = boost::lexical_cast<double>(parameters().at(4));
512 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
514 transform.image_transform.clip_translation[0] = x;
515 transform.image_transform.clip_translation[1] = y;
516 transform.image_transform.clip_scale[0] = x_s;
517 transform.image_transform.clip_scale[1] = y_s;
519 }, duration, tween));
521 else if (boost::iequals(parameters()[0], L"CROP"))
523 if (parameters().size() == 1)
525 auto crop = get_current_transform().image_transform.crop;
528 + boost::lexical_cast<std::wstring>(crop.ul[0]) + L" "
529 + boost::lexical_cast<std::wstring>(crop.ul[1]) + L" "
530 + boost::lexical_cast<std::wstring>(crop.lr[0]) + L" "
531 + boost::lexical_cast<std::wstring>(crop.lr[1]) + L"\r\n");
535 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
536 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
537 double ul_x = boost::lexical_cast<double>(parameters().at(1));
538 double ul_y = boost::lexical_cast<double>(parameters().at(2));
539 double lr_x = boost::lexical_cast<double>(parameters().at(3));
540 double lr_y = boost::lexical_cast<double>(parameters().at(4));
542 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
544 transform.image_transform.crop.ul[0] = ul_x;
545 transform.image_transform.crop.ul[1] = ul_y;
546 transform.image_transform.crop.lr[0] = lr_x;
547 transform.image_transform.crop.lr[1] = lr_y;
549 }, duration, tween));
551 else if (boost::iequals(parameters()[0], L"PERSPECTIVE"))
553 if (parameters().size() == 1)
555 auto perspective = get_current_transform().image_transform.perspective;
558 + boost::lexical_cast<std::wstring>(perspective.ul[0]) + L" "
559 + boost::lexical_cast<std::wstring>(perspective.ul[1]) + L" "
560 + boost::lexical_cast<std::wstring>(perspective.ur[0]) + L" "
561 + boost::lexical_cast<std::wstring>(perspective.ur[1]) + L" "
562 + boost::lexical_cast<std::wstring>(perspective.lr[0]) + L" "
563 + boost::lexical_cast<std::wstring>(perspective.lr[1]) + L" "
564 + boost::lexical_cast<std::wstring>(perspective.ll[0]) + L" "
565 + boost::lexical_cast<std::wstring>(perspective.ll[1]) + L"\r\n");
569 int duration = parameters().size() > 9 ? boost::lexical_cast<int>(parameters()[9]) : 0;
570 std::wstring tween = parameters().size() > 10 ? parameters()[10] : L"linear";
571 double ul_x = boost::lexical_cast<double>(parameters().at(1));
572 double ul_y = boost::lexical_cast<double>(parameters().at(2));
573 double ur_x = boost::lexical_cast<double>(parameters().at(3));
574 double ur_y = boost::lexical_cast<double>(parameters().at(4));
575 double lr_x = boost::lexical_cast<double>(parameters().at(5));
576 double lr_y = boost::lexical_cast<double>(parameters().at(6));
577 double ll_x = boost::lexical_cast<double>(parameters().at(7));
578 double ll_y = boost::lexical_cast<double>(parameters().at(8));
580 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
582 transform.image_transform.perspective.ul[0] = ul_x;
583 transform.image_transform.perspective.ul[1] = ul_y;
584 transform.image_transform.perspective.ur[0] = ur_x;
585 transform.image_transform.perspective.ur[1] = ur_y;
586 transform.image_transform.perspective.lr[0] = lr_x;
587 transform.image_transform.perspective.lr[1] = lr_y;
588 transform.image_transform.perspective.ll[0] = ll_x;
589 transform.image_transform.perspective.ll[1] = ll_y;
591 }, duration, tween));
593 else if (boost::iequals(parameters()[0], L"GRID"))
595 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
596 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
597 int n = boost::lexical_cast<int>(parameters().at(1));
598 double delta = 1.0/static_cast<double>(n);
599 for(int x = 0; x < n; ++x)
601 for(int y = 0; y < n; ++y)
604 transforms.push_back(stage::transform_tuple_t(index, [=](frame_transform transform) -> frame_transform
606 transform.image_transform.fill_translation[0] = x*delta;
607 transform.image_transform.fill_translation[1] = y*delta;
608 transform.image_transform.fill_scale[0] = delta;
609 transform.image_transform.fill_scale[1] = delta;
610 transform.image_transform.clip_translation[0] = x*delta;
611 transform.image_transform.clip_translation[1] = y*delta;
612 transform.image_transform.clip_scale[0] = delta;
613 transform.image_transform.clip_scale[1] = delta;
615 }, duration, tween));
619 else if (boost::iequals(parameters()[0], L"MIPMAP"))
621 if (parameters().size() == 1)
622 return reply_value([](const frame_transform& t) { return t.image_transform.use_mipmap ? 1 : 0; });
624 bool value = boost::lexical_cast<int>(parameters().at(1));
625 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
627 transform.image_transform.use_mipmap = value;
631 else if(boost::iequals(parameters()[0], L"BLEND"))
633 if (parameters().size() == 1)
634 return reply_value([](const frame_transform& t) { return get_blend_mode(t.image_transform.blend_mode); });
636 auto value = get_blend_mode(parameters().at(1));
637 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
639 transform.image_transform.blend_mode = value;
643 else if (boost::iequals(parameters()[0], L"CHROMA"))
645 if (parameters().size() == 1)
647 auto chroma = get_current_transform().image_transform.chroma;
650 + core::get_chroma_mode(chroma.key) + L" "
651 + boost::lexical_cast<std::wstring>(chroma.threshold) + L" "
652 + boost::lexical_cast<std::wstring>(chroma.softness) + L" "
653 + boost::lexical_cast<std::wstring>(chroma.spill) + L"\r\n");
657 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters().at(5)) : 0;
658 std::wstring tween = parameters().size() > 6 ? parameters().at(6) : L"linear";
661 chroma.key = get_chroma_mode(parameters().at(1));
662 chroma.threshold = boost::lexical_cast<double>(parameters().at(2));
663 chroma.softness = boost::lexical_cast<double>(parameters().at(3));
664 chroma.spill = parameters().size() > 4 ? boost::lexical_cast<double>(parameters().at(4)) : 0.0;
665 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
667 transform.image_transform.chroma = chroma;
669 }, duration, tween));
671 else if (boost::iequals(parameters()[0], L"MASTERVOLUME"))
673 if (parameters().size() == 1)
675 auto volume = channel()->mixer().get_master_volume();
676 SetReplyString(L"201 MIXER OK\r\n"
677 + boost::lexical_cast<std::wstring>(volume)+L"\r\n");
681 float master_volume = boost::lexical_cast<float>(parameters().at(1));
682 channel()->mixer().set_master_volume(master_volume);
684 else if(boost::iequals(parameters()[0], L"BRIGHTNESS"))
686 if (parameters().size() == 1)
687 return reply_value([](const frame_transform& t) { return t.image_transform.brightness; });
689 auto value = boost::lexical_cast<double>(parameters().at(1));
690 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
691 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
692 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
694 transform.image_transform.brightness = value;
696 }, duration, tween));
698 else if(boost::iequals(parameters()[0], L"SATURATION"))
700 if (parameters().size() == 1)
701 return reply_value([](const frame_transform& t) { return t.image_transform.saturation; });
703 auto value = boost::lexical_cast<double>(parameters().at(1));
704 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
705 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
706 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
708 transform.image_transform.saturation = value;
710 }, duration, tween));
712 else if (boost::iequals(parameters()[0], L"CONTRAST"))
714 if (parameters().size() == 1)
715 return reply_value([](const frame_transform& t) { return t.image_transform.contrast; });
717 auto value = boost::lexical_cast<double>(parameters().at(1));
718 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
719 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
720 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
722 transform.image_transform.contrast = value;
724 }, duration, tween));
726 else if (boost::iequals(parameters()[0], L"ROTATION"))
728 static const double PI = 3.141592653589793;
730 if (parameters().size() == 1)
731 return reply_value([](const frame_transform& t) { return t.image_transform.angle / PI * 180.0; });
733 auto value = boost::lexical_cast<double>(parameters().at(1)) * PI / 180.0;
734 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
735 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
736 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
738 transform.image_transform.angle = value;
740 }, duration, tween));
742 else if (boost::iequals(parameters()[0], L"LEVELS"))
744 if (parameters().size() == 1)
746 auto levels = get_current_transform().image_transform.levels;
747 SetReplyString(L"201 MIXER OK\r\n"
748 + boost::lexical_cast<std::wstring>(levels.min_input) + L" "
749 + boost::lexical_cast<std::wstring>(levels.max_input) + L" "
750 + boost::lexical_cast<std::wstring>(levels.gamma) + L" "
751 + boost::lexical_cast<std::wstring>(levels.min_output) + L" "
752 + boost::lexical_cast<std::wstring>(levels.max_output) + L"\r\n");
757 value.min_input = boost::lexical_cast<double>(parameters().at(1));
758 value.max_input = boost::lexical_cast<double>(parameters().at(2));
759 value.gamma = boost::lexical_cast<double>(parameters().at(3));
760 value.min_output = boost::lexical_cast<double>(parameters().at(4));
761 value.max_output = boost::lexical_cast<double>(parameters().at(5));
762 int duration = parameters().size() > 6 ? boost::lexical_cast<int>(parameters()[6]) : 0;
763 std::wstring tween = parameters().size() > 7 ? parameters()[7] : L"linear";
765 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
767 transform.image_transform.levels = value;
769 }, duration, tween));
771 else if(boost::iequals(parameters()[0], L"VOLUME"))
773 if (parameters().size() == 1)
774 return reply_value([](const frame_transform& t) { return t.audio_transform.volume; });
776 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
777 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
778 double value = boost::lexical_cast<double>(parameters()[1]);
780 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
782 transform.audio_transform.volume = value;
784 }, duration, tween));
786 else if(boost::iequals(parameters()[0], L"CLEAR"))
788 int layer = layer_index(std::numeric_limits<int>::max());
790 if (layer == std::numeric_limits<int>::max())
792 channel()->stage().clear_transforms();
796 channel()->stage().clear_transforms(layer);
799 else if(boost::iequals(parameters()[0], L"COMMIT"))
801 transforms = std::move(deferred_transforms[channel_index()]);
805 SetReplyString(L"404 MIXER ERROR\r\n");
811 auto& defer_tranforms = deferred_transforms[channel_index()];
812 defer_tranforms.insert(defer_tranforms.end(), transforms.begin(), transforms.end());
815 channel()->stage().apply_transforms(transforms);
817 SetReplyString(L"202 MIXER OK\r\n");
821 catch(file_not_found&)
823 CASPAR_LOG_CURRENT_EXCEPTION();
824 SetReplyString(L"404 MIXER ERROR\r\n");
829 CASPAR_LOG_CURRENT_EXCEPTION();
830 SetReplyString(L"502 MIXER FAILED\r\n");
835 bool SwapCommand::DoExecute()
837 //Perform loading of the clip
840 if(layer_index(-1) != -1)
842 std::vector<std::string> strs;
843 boost::split(strs, parameters()[0], boost::is_any_of("-"));
845 auto ch1 = channel();
846 auto ch2 = channels().at(boost::lexical_cast<int>(strs.at(0))-1);
848 int l1 = layer_index();
849 int l2 = boost::lexical_cast<int>(strs.at(1));
851 ch1->stage().swap_layer(l1, l2, ch2.channel->stage());
855 auto ch1 = channel();
856 auto ch2 = channels().at(boost::lexical_cast<int>(parameters()[0])-1);
857 ch1->stage().swap_layers(ch2.channel->stage());
860 SetReplyString(L"202 SWAP OK\r\n");
864 catch(file_not_found&)
866 CASPAR_LOG_CURRENT_EXCEPTION();
867 SetReplyString(L"404 SWAP ERROR\r\n");
872 CASPAR_LOG_CURRENT_EXCEPTION();
873 SetReplyString(L"502 SWAP FAILED\r\n");
878 bool AddCommand::DoExecute()
880 //Perform loading of the clip
883 //create_consumer still expects all parameters to be uppercase
884 for (auto& str : parameters())
886 boost::to_upper(str);
889 core::diagnostics::scoped_call_context save;
890 core::diagnostics::call_context::for_thread().video_channel = channel_index() + 1;
892 auto consumer = create_consumer(parameters(), &channel()->stage());
893 channel()->output().add(layer_index(consumer->index()), consumer);
895 SetReplyString(L"202 ADD OK\r\n");
899 catch(file_not_found&)
901 CASPAR_LOG_CURRENT_EXCEPTION();
902 SetReplyString(L"404 ADD ERROR\r\n");
907 CASPAR_LOG_CURRENT_EXCEPTION();
908 SetReplyString(L"502 ADD FAILED\r\n");
913 bool RemoveCommand::DoExecute()
915 //Perform loading of the clip
918 auto index = layer_index(std::numeric_limits<int>::min());
919 if(index == std::numeric_limits<int>::min())
921 //create_consumer still expects all parameters to be uppercase
922 for (auto& str : parameters())
924 boost::to_upper(str);
927 index = create_consumer(parameters(), &channel()->stage())->index();
930 channel()->output().remove(index);
932 SetReplyString(L"202 REMOVE OK\r\n");
936 catch(file_not_found&)
938 CASPAR_LOG_CURRENT_EXCEPTION();
939 SetReplyString(L"404 REMOVE ERROR\r\n");
944 CASPAR_LOG_CURRENT_EXCEPTION();
945 SetReplyString(L"502 REMOVE FAILED\r\n");
950 bool LoadCommand::DoExecute()
952 //Perform loading of the clip
955 core::diagnostics::scoped_call_context save;
956 core::diagnostics::call_context::for_thread().video_channel = channel_index() + 1;
957 core::diagnostics::call_context::for_thread().layer = layer_index();
958 auto pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
959 channel()->stage().load(layer_index(), pFP, true);
961 SetReplyString(L"202 LOAD OK\r\n");
965 catch(file_not_found&)
967 CASPAR_LOG_CURRENT_EXCEPTION();
968 SetReplyString(L"404 LOAD ERROR\r\n");
973 CASPAR_LOG_CURRENT_EXCEPTION();
974 SetReplyString(L"502 LOAD FAILED\r\n");
979 bool LoadbgCommand::DoExecute()
981 transition_info transitionInfo;
985 std::wstring message;
986 for(size_t n = 0; n < parameters().size(); ++n)
987 message += boost::to_upper_copy(parameters()[n]) + L" ";
989 static const boost::wregex expr(LR"(.*(?<TRANSITION>CUT|PUSH|SLIDE|WIPE|MIX)\s*(?<DURATION>\d+)\s*(?<TWEEN>(LINEAR)|(EASE[^\s]*))?\s*(?<DIRECTION>FROMLEFT|FROMRIGHT|LEFT|RIGHT)?.*)");
991 if(boost::regex_match(message, what, expr))
993 auto transition = what["TRANSITION"].str();
994 transitionInfo.duration = boost::lexical_cast<size_t>(what["DURATION"].str());
995 auto direction = what["DIRECTION"].matched ? what["DIRECTION"].str() : L"";
996 auto tween = what["TWEEN"].matched ? what["TWEEN"].str() : L"";
997 transitionInfo.tweener = tween;
999 if(transition == L"CUT")
1000 transitionInfo.type = transition_type::cut;
1001 else if(transition == L"MIX")
1002 transitionInfo.type = transition_type::mix;
1003 else if(transition == L"PUSH")
1004 transitionInfo.type = transition_type::push;
1005 else if(transition == L"SLIDE")
1006 transitionInfo.type = transition_type::slide;
1007 else if(transition == L"WIPE")
1008 transitionInfo.type = transition_type::wipe;
1010 if(direction == L"FROMLEFT")
1011 transitionInfo.direction = transition_direction::from_left;
1012 else if(direction == L"FROMRIGHT")
1013 transitionInfo.direction = transition_direction::from_right;
1014 else if(direction == L"LEFT")
1015 transitionInfo.direction = transition_direction::from_right;
1016 else if(direction == L"RIGHT")
1017 transitionInfo.direction = transition_direction::from_left;
1020 //Perform loading of the clip
1023 std::shared_ptr<core::frame_producer> pFP;
1025 static boost::wregex expr(LR"(\[(?<CHANNEL>\d+)\])", boost::regex::icase);
1027 core::diagnostics::scoped_call_context save;
1028 core::diagnostics::call_context::for_thread().video_channel = channel_index() + 1;
1029 core::diagnostics::call_context::for_thread().layer = layer_index();
1031 boost::wsmatch what;
1032 if(boost::regex_match(parameters().at(0), what, expr))
1034 auto channel_index = boost::lexical_cast<int>(what["CHANNEL"].str());
1035 pFP = reroute::create_producer(*channels().at(channel_index-1).channel);
1039 pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
1042 if(pFP == frame_producer::empty())
1043 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info(parameters().size() > 0 ? parameters()[0] : L""));
1045 bool auto_play = contains_param(L"AUTO", parameters());
1047 auto pFP2 = create_transition_producer(channel()->video_format_desc().field_mode, spl::make_shared_ptr(pFP), transitionInfo);
1049 channel()->stage().load(layer_index(), pFP2, false, transitionInfo.duration); // TODO: LOOP
1051 channel()->stage().load(layer_index(), pFP2, false); // TODO: LOOP
1054 SetReplyString(L"202 LOADBG OK\r\n");
1058 catch(file_not_found&)
1060 CASPAR_LOG(error) << L"File not found. No match found for parameters. Check syntax.";
1061 SetReplyString(L"404 LOADBG ERROR\r\n");
1066 CASPAR_LOG_CURRENT_EXCEPTION();
1067 SetReplyString(L"502 LOADBG FAILED\r\n");
1072 bool PauseCommand::DoExecute()
1076 channel()->stage().pause(layer_index());
1077 SetReplyString(L"202 PAUSE OK\r\n");
1082 SetReplyString(L"501 PAUSE FAILED\r\n");
1088 bool PlayCommand::DoExecute()
1092 if(!parameters().empty())
1094 LoadbgCommand lbg(*this);
1097 throw std::exception();
1100 channel()->stage().play(layer_index());
1102 SetReplyString(L"202 PLAY OK\r\n");
1107 SetReplyString(L"501 PLAY FAILED\r\n");
1113 bool StopCommand::DoExecute()
1117 channel()->stage().stop(layer_index());
1118 SetReplyString(L"202 STOP OK\r\n");
1123 SetReplyString(L"501 STOP FAILED\r\n");
1129 bool ClearCommand::DoExecute()
1131 int index = layer_index(std::numeric_limits<int>::min());
1132 if(index != std::numeric_limits<int>::min())
1133 channel()->stage().clear(index);
1135 channel()->stage().clear();
1137 SetReplyString(L"202 CLEAR OK\r\n");
1142 bool PrintCommand::DoExecute()
1144 channel()->output().add(create_consumer({ L"IMAGE" }, &channel()->stage()));
1146 SetReplyString(L"202 PRINT OK\r\n");
1151 bool LogCommand::DoExecute()
1153 if(boost::iequals(parameters().at(0), L"LEVEL"))
1154 log::set_log_level(parameters().at(1));
1156 SetReplyString(L"202 LOG OK\r\n");
1161 bool CGCommand::DoExecute()
1165 std::wstring command = boost::to_upper_copy(parameters()[0]);
1166 if(command == L"ADD")
1167 return DoExecuteAdd();
1168 else if(command == L"PLAY")
1169 return DoExecutePlay();
1170 else if(command == L"STOP")
1171 return DoExecuteStop();
1172 else if(command == L"NEXT")
1173 return DoExecuteNext();
1174 else if(command == L"REMOVE")
1175 return DoExecuteRemove();
1176 else if(command == L"CLEAR")
1177 return DoExecuteClear();
1178 else if(command == L"UPDATE")
1179 return DoExecuteUpdate();
1180 else if(command == L"INVOKE")
1181 return DoExecuteInvoke();
1182 else if(command == L"INFO")
1183 return DoExecuteInfo();
1187 CASPAR_LOG_CURRENT_EXCEPTION();
1190 SetReplyString(L"403 CG ERROR\r\n");
1194 bool CGCommand::ValidateLayer(const std::wstring& layerstring) {
1195 int length = layerstring.length();
1196 for(int i = 0; i < length; ++i) {
1197 if(!std::isdigit(layerstring[i])) {
1205 bool CGCommand::DoExecuteAdd() {
1206 //CG 1 ADD 0 "template_folder/templatename" [STARTLABEL] 0/1 [DATA]
1208 int layer = 0; //_parameters[1]
1209 // std::wstring templateName; //_parameters[2]
1210 std::wstring label; //_parameters[3]
1211 bool bDoStart = false; //_parameters[3] alt. _parameters[4]
1212 // std::wstring data; //_parameters[4] alt. _parameters[5]
1214 if(parameters().size() < 4)
1216 SetReplyString(L"402 CG ERROR\r\n");
1219 unsigned int dataIndex = 4;
1221 if(!ValidateLayer(parameters()[1]))
1223 SetReplyString(L"403 CG ERROR\r\n");
1227 layer = boost::lexical_cast<int>(parameters()[1]);
1229 if(parameters()[3].length() > 1)
1231 label = parameters()[3];
1234 if(parameters().size() > 4 && parameters()[4].length() > 0) //read play-on-load-flag
1235 bDoStart = (parameters()[4][0]==L'1') ? true : false;
1238 SetReplyString(L"402 CG ERROR\r\n");
1242 else if(parameters()[3].length() > 0) { //read play-on-load-flag
1243 bDoStart = (parameters()[3][0]==L'1') ? true : false;
1247 SetReplyString(L"403 CG ERROR\r\n");
1251 const wchar_t* pDataString = 0;
1252 std::wstring dataFromFile;
1253 if(parameters().size() > dataIndex)
1255 const std::wstring& dataString = parameters()[dataIndex];
1257 if (dataString.at(0) == L'<' || dataString.at(0) == L'{') //the data is XML or Json
1258 pDataString = dataString.c_str();
1261 //The data is not an XML-string, it must be a filename
1262 std::wstring filename = env::data_folder();
1263 filename.append(dataString);
1264 filename.append(L".ftd");
1266 auto found_file = find_case_insensitive(filename);
1270 dataFromFile = read_file(boost::filesystem::path(*found_file));
1271 pDataString = dataFromFile.c_str();
1276 auto filename = parameters()[2];
1277 auto proxy = cg_registry_->get_or_create_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER), filename);
1279 if (proxy == core::cg_proxy::empty())
1281 CASPAR_LOG(warning) << "Could not find template " << parameters()[2];
1282 SetReplyString(L"404 CG ERROR\r\n");
1286 proxy->add(layer, filename, bDoStart, label, (pDataString != 0) ? pDataString : L"");
1292 bool CGCommand::DoExecutePlay()
1294 if(parameters().size() > 1)
1296 if(!ValidateLayer(parameters()[1]))
1298 SetReplyString(L"403 CG ERROR\r\n");
1301 int layer = boost::lexical_cast<int>(parameters()[1]);
1302 cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->play(layer);
1306 SetReplyString(L"402 CG ERROR\r\n");
1310 SetReplyString(L"202 CG OK\r\n");
1314 bool CGCommand::DoExecuteStop()
1316 if(parameters().size() > 1)
1318 if(!ValidateLayer(parameters()[1]))
1320 SetReplyString(L"403 CG ERROR\r\n");
1323 int layer = boost::lexical_cast<int>(parameters()[1]);
1324 cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->stop(layer, 0);
1328 SetReplyString(L"402 CG ERROR\r\n");
1332 SetReplyString(L"202 CG OK\r\n");
1336 bool CGCommand::DoExecuteNext()
1338 if(parameters().size() > 1)
1340 if(!ValidateLayer(parameters()[1]))
1342 SetReplyString(L"403 CG ERROR\r\n");
1346 int layer = boost::lexical_cast<int>(parameters()[1]);
1347 cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->next(layer);
1351 SetReplyString(L"402 CG ERROR\r\n");
1355 SetReplyString(L"202 CG OK\r\n");
1359 bool CGCommand::DoExecuteRemove()
1361 if(parameters().size() > 1)
1363 if(!ValidateLayer(parameters()[1]))
1365 SetReplyString(L"403 CG ERROR\r\n");
1369 int layer = boost::lexical_cast<int>(parameters()[1]);
1370 cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->remove(layer);
1374 SetReplyString(L"402 CG ERROR\r\n");
1378 SetReplyString(L"202 CG OK\r\n");
1382 bool CGCommand::DoExecuteClear()
1384 channel()->stage().clear(layer_index(core::cg_proxy::DEFAULT_LAYER));
1385 SetReplyString(L"202 CG OK\r\n");
1389 bool CGCommand::DoExecuteUpdate()
1393 if(!ValidateLayer(parameters().at(1)))
1395 SetReplyString(L"403 CG ERROR\r\n");
1399 std::wstring dataString = parameters().at(2);
1400 if (dataString.at(0) != L'<' && dataString.at(0) != L'{')
1402 //The data is not XML or Json, it must be a filename
1403 std::wstring filename = env::data_folder();
1404 filename.append(dataString);
1405 filename.append(L".ftd");
1407 dataString = read_file(boost::filesystem::path(filename));
1410 int layer = boost::lexical_cast<int>(parameters()[1]);
1411 cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->update(layer, dataString);
1415 SetReplyString(L"402 CG ERROR\r\n");
1419 SetReplyString(L"202 CG OK\r\n");
1423 bool CGCommand::DoExecuteInvoke()
1425 std::wstringstream replyString;
1426 replyString << L"201 CG OK\r\n";
1428 if(parameters().size() > 2)
1430 if(!ValidateLayer(parameters()[1]))
1432 SetReplyString(L"403 CG ERROR\r\n");
1435 int layer = boost::lexical_cast<int>(parameters()[1]);
1436 auto result = cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->invoke(layer, parameters()[2]);
1437 replyString << result << L"\r\n";
1441 SetReplyString(L"402 CG ERROR\r\n");
1445 SetReplyString(replyString.str());
1449 bool CGCommand::DoExecuteInfo()
1451 std::wstringstream replyString;
1452 replyString << L"201 CG OK\r\n";
1454 if(parameters().size() > 1)
1456 if(!ValidateLayer(parameters()[1]))
1458 SetReplyString(L"403 CG ERROR\r\n");
1462 int layer = boost::lexical_cast<int>(parameters()[1]);
1463 auto desc = cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->description(layer);
1465 replyString << desc << L"\r\n";
1469 auto info = cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->template_host_info();
1470 replyString << info << L"\r\n";
1473 SetReplyString(replyString.str());
1477 bool DataCommand::DoExecute()
1479 std::wstring command = boost::to_upper_copy(parameters()[0]);
1480 if(command == L"STORE")
1481 return DoExecuteStore();
1482 else if(command == L"RETRIEVE")
1483 return DoExecuteRetrieve();
1484 else if(command == L"REMOVE")
1485 return DoExecuteRemove();
1486 else if(command == L"LIST")
1487 return DoExecuteList();
1489 SetReplyString(L"403 DATA ERROR\r\n");
1493 bool DataCommand::DoExecuteStore()
1495 if(parameters().size() < 3)
1497 SetReplyString(L"402 DATA STORE ERROR\r\n");
1501 std::wstring filename = env::data_folder();
1502 filename.append(parameters()[1]);
1503 filename.append(L".ftd");
1505 auto data_path = boost::filesystem::path(filename).parent_path().wstring();
1506 auto found_data_path = find_case_insensitive(data_path);
1508 if (found_data_path)
1509 data_path = *found_data_path;
1511 if(!boost::filesystem::exists(data_path))
1512 boost::filesystem::create_directories(data_path);
1514 auto found_filename = find_case_insensitive(filename);
1517 filename = *found_filename; // Overwrite case insensitive.
1519 boost::filesystem::wofstream datafile(filename);
1522 SetReplyString(L"501 DATA STORE FAILED\r\n");
1526 datafile << static_cast<wchar_t>(65279); // UTF-8 BOM character
1527 datafile << parameters()[2] << std::flush;
1530 std::wstring replyString = L"202 DATA STORE OK\r\n";
1531 SetReplyString(replyString);
1535 bool DataCommand::DoExecuteRetrieve()
1537 if(parameters().size() < 2)
1539 SetReplyString(L"402 DATA RETRIEVE ERROR\r\n");
1543 std::wstring filename = env::data_folder();
1544 filename.append(parameters()[1]);
1545 filename.append(L".ftd");
1547 std::wstring file_contents;
1549 auto found_file = find_case_insensitive(filename);
1552 file_contents = read_file(boost::filesystem::path(*found_file));
1554 if (file_contents.empty())
1556 SetReplyString(L"404 DATA RETRIEVE ERROR\r\n");
1560 std::wstringstream reply(L"201 DATA RETRIEVE OK\r\n");
1562 std::wstringstream file_contents_stream(file_contents);
1565 bool firstLine = true;
1566 while(std::getline(file_contents_stream, line))
1577 SetReplyString(reply.str());
1581 bool DataCommand::DoExecuteRemove()
1583 if (parameters().size() < 2)
1585 SetReplyString(L"402 DATA REMOVE ERROR\r\n");
1589 std::wstring filename = env::data_folder();
1590 filename.append(parameters()[1]);
1591 filename.append(L".ftd");
1593 if (!boost::filesystem::exists(filename))
1595 SetReplyString(L"404 DATA REMOVE ERROR\r\n");
1599 if (!boost::filesystem::remove(filename))
1601 SetReplyString(L"403 DATA REMOVE ERROR\r\n");
1605 SetReplyString(L"201 DATA REMOVE OK\r\n");
1610 bool DataCommand::DoExecuteList()
1612 std::wstringstream replyString;
1613 replyString << L"200 DATA LIST OK\r\n";
1615 for (boost::filesystem::recursive_directory_iterator itr(env::data_folder()), end; itr != end; ++itr)
1617 if(boost::filesystem::is_regular_file(itr->path()))
1619 if(!boost::iequals(itr->path().extension().wstring(), L".ftd"))
1622 auto relativePath = boost::filesystem::path(itr->path().wstring().substr(env::data_folder().size()-1, itr->path().wstring().size()));
1624 auto str = relativePath.replace_extension(L"").generic_wstring();
1625 if(str[0] == L'\\' || str[0] == L'/')
1626 str = std::wstring(str.begin() + 1, str.end());
1628 replyString << str << L"\r\n";
1632 replyString << L"\r\n";
1634 SetReplyString(boost::to_upper_copy(replyString.str()));
1638 bool CinfCommand::DoExecute()
1640 std::wstringstream replyString;
1645 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end && info.empty(); ++itr)
1647 auto path = itr->path();
1648 auto file = path.replace_extension(L"").filename().wstring();
1649 if(boost::iequals(file, parameters().at(0)))
1650 info += MediaInfo(itr->path(), system_info_repo_) + L"\r\n";
1655 SetReplyString(L"404 CINF ERROR\r\n");
1658 replyString << L"200 CINF OK\r\n";
1659 replyString << info << "\r\n";
1663 CASPAR_LOG_CURRENT_EXCEPTION();
1664 SetReplyString(L"404 CINF ERROR\r\n");
1668 SetReplyString(replyString.str());
1672 void GenerateChannelInfo(int index, const spl::shared_ptr<core::video_channel>& pChannel, std::wstringstream& replyString)
1674 replyString << index+1 << L" " << pChannel->video_format_desc().name << L" PLAYING\r\n";
1677 bool InfoCommand::DoExecute()
1679 std::wstringstream replyString;
1681 boost::property_tree::xml_writer_settings<std::wstring> w(' ', 3);
1685 if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"TEMPLATE"))
1687 replyString << L"201 INFO TEMPLATE OK\r\n";
1689 auto filename = parameters().at(1);
1691 std::wstringstream str;
1692 str << u16(cg_registry_->read_meta_info(filename));
1693 boost::property_tree::wptree info;
1694 boost::property_tree::xml_parser::read_xml(str, info, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);
1696 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1698 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"CONFIG"))
1700 replyString << L"201 INFO CONFIG OK\r\n";
1702 boost::property_tree::write_xml(replyString, caspar::env::properties(), w);
1704 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"PATHS"))
1706 replyString << L"201 INFO PATHS OK\r\n";
1708 boost::property_tree::wptree info;
1709 info.add_child(L"paths", caspar::env::properties().get_child(L"configuration.paths"));
1710 info.add(L"paths.initial-path", boost::filesystem::initial_path().wstring() + L"/");
1712 boost::property_tree::write_xml(replyString, info, w);
1714 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SYSTEM"))
1716 replyString << L"201 INFO SYSTEM OK\r\n";
1718 boost::property_tree::wptree info;
1720 info.add(L"system.name", caspar::system_product_name());
1721 info.add(L"system.os.description", caspar::os_description());
1722 info.add(L"system.cpu", caspar::cpu_info());
1724 system_info_repo_->fill_information(info);
1726 boost::property_tree::write_xml(replyString, info, w);
1728 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SERVER"))
1730 replyString << L"201 INFO SERVER OK\r\n";
1732 boost::property_tree::wptree info;
1735 for (auto& channel : channels())
1736 info.add_child(L"channels.channel", channel.channel->info())
1737 .add(L"index", ++index);
1739 boost::property_tree::write_xml(replyString, info, w);
1743 if(parameters().size() >= 1)
1745 replyString << L"201 INFO OK\r\n";
1746 boost::property_tree::wptree info;
1748 std::vector<std::wstring> split;
1749 boost::split(split, parameters()[0], boost::is_any_of("-"));
1751 int layer = std::numeric_limits<int>::min();
1752 int channel = boost::lexical_cast<int>(split[0]) - 1;
1754 if(split.size() > 1)
1755 layer = boost::lexical_cast<int>(split[1]);
1757 if(layer == std::numeric_limits<int>::min())
1759 info.add_child(L"channel", channels().at(channel).channel->info())
1760 .add(L"index", channel);
1764 if(parameters().size() >= 2)
1766 if(boost::iequals(parameters()[1], L"B"))
1767 info.add_child(L"producer", channels().at(channel).channel->stage().background(layer).get()->info());
1769 info.add_child(L"producer", channels().at(channel).channel->stage().foreground(layer).get()->info());
1773 info.add_child(L"layer", channels().at(channel).channel->stage().info(layer).get())
1774 .add(L"index", layer);
1777 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1781 // This is needed for backwards compatibility with old clients
1782 replyString << L"200 INFO OK\r\n";
1783 for(size_t n = 0; n < channels().size(); ++n)
1784 GenerateChannelInfo(n, channels()[n].channel, replyString);
1791 CASPAR_LOG_CURRENT_EXCEPTION();
1792 SetReplyString(L"403 INFO ERROR\r\n");
1796 replyString << L"\r\n";
1797 SetReplyString(replyString.str());
1801 bool ClsCommand::DoExecute()
1805 std::wstringstream replyString;
1806 replyString << L"200 CLS OK\r\n";
1807 replyString << ListMedia(system_info_repo_);
1808 replyString << L"\r\n";
1809 SetReplyString(boost::to_upper_copy(replyString.str()));
1813 CASPAR_LOG_CURRENT_EXCEPTION();
1814 SetReplyString(L"501 CLS FAILED\r\n");
1821 bool TlsCommand::DoExecute()
1825 std::wstringstream replyString;
1826 replyString << L"200 TLS OK\r\n";
1828 replyString << ListTemplates(cg_registry_);
1829 replyString << L"\r\n";
1831 SetReplyString(replyString.str());
1835 CASPAR_LOG_CURRENT_EXCEPTION();
1836 SetReplyString(L"501 TLS FAILED\r\n");
1842 bool VersionCommand::DoExecute()
1844 std::wstring replyString = L"201 VERSION OK\r\n" + env::version() + L"\r\n";
1846 if (parameters().size() > 0 && !boost::iequals(parameters()[0], L"SERVER"))
1848 auto version = system_info_repo_->get_version(parameters().at(0));
1850 if (version.empty())
1851 replyString = L"403 VERSION ERROR\r\n";
1853 replyString = L"201 VERSION OK\r\n" + version + L"\r\n";
1856 SetReplyString(replyString);
1860 bool ByeCommand::DoExecute()
1862 client()->disconnect();
1866 bool SetCommand::DoExecute()
1870 std::wstring name = boost::to_upper_copy(parameters()[0]);
1871 std::wstring value = boost::to_upper_copy(parameters()[1]);
1875 auto format_desc = core::video_format_desc(value);
1876 if(format_desc.format != core::video_format::invalid)
1878 channel()->video_format_desc(format_desc);
1879 SetReplyString(L"202 SET MODE OK\r\n");
1882 SetReplyString(L"501 SET MODE FAILED\r\n");
1886 this->SetReplyString(L"403 SET ERROR\r\n");
1891 CASPAR_LOG_CURRENT_EXCEPTION();
1892 SetReplyString(L"501 SET FAILED\r\n");
1899 bool LockCommand::DoExecute()
1903 auto it = parameters().begin();
1905 std::shared_ptr<caspar::IO::lock_container> lock;
1908 int channel_index = boost::lexical_cast<int>(*it) - 1;
1909 lock = channels().at(channel_index).lock;
1911 catch(const boost::bad_lexical_cast&) {}
1914 SetReplyString(L"401 LOCK ERROR\r\n");
1921 if(it == parameters().end()) //too few parameters
1923 SetReplyString(L"402 LOCK ERROR\r\n");
1927 std::wstring command = boost::to_upper_copy(*it);
1928 if(command == L"ACQUIRE")
1931 if(it == parameters().end()) //too few parameters
1933 SetReplyString(L"402 LOCK ACQUIRE ERROR\r\n");
1936 std::wstring lock_phrase = (*it);
1938 //TODO: read options
1942 //just lock one channel
1943 if(!lock->try_lock(lock_phrase, client()))
1945 SetReplyString(L"503 LOCK ACQUIRE FAILED\r\n");
1951 //TODO: lock all channels
1952 CASPAR_THROW_EXCEPTION(not_implemented());
1954 SetReplyString(L"202 LOCK ACQUIRE OK\r\n");
1957 else if(command == L"RELEASE")
1961 lock->release_lock(client());
1965 //TODO: release all channels
1966 CASPAR_THROW_EXCEPTION(not_implemented());
1968 SetReplyString(L"202 LOCK RELEASE OK\r\n");
1970 else if(command == L"CLEAR")
1972 std::wstring override_phrase = env::properties().get(L"configuration.lock-clear-phrase", L"");
1973 std::wstring client_override_phrase;
1974 if(!override_phrase.empty())
1977 if(it == parameters().end())
1979 SetReplyString(L"402 LOCK CLEAR ERROR\r\n");
1982 client_override_phrase = (*it);
1987 //just clear one channel
1988 if(client_override_phrase != override_phrase)
1990 SetReplyString(L"503 LOCK CLEAR FAILED\r\n");
1994 lock->clear_locks();
1998 //TODO: clear all channels
1999 CASPAR_THROW_EXCEPTION(not_implemented());
2002 SetReplyString(L"202 LOCK CLEAR OK\r\n");
2006 SetReplyString(L"403 LOCK ERROR\r\n");
2010 catch(not_implemented&)
2012 SetReplyString(L"600 LOCK FAILED\r\n");
2017 CASPAR_LOG_CURRENT_EXCEPTION();
2018 SetReplyString(L"501 LOCK FAILED\r\n");
2025 bool ThumbnailCommand::DoExecute()
2027 std::wstring command = boost::to_upper_copy(parameters()[0]);
2029 if (command == L"RETRIEVE")
2030 return DoExecuteRetrieve();
2031 else if (command == L"LIST")
2032 return DoExecuteList();
2033 else if (command == L"GENERATE")
2034 return DoExecuteGenerate();
2035 else if (command == L"GENERATE_ALL")
2036 return DoExecuteGenerateAll();
2038 SetReplyString(L"403 THUMBNAIL ERROR\r\n");
2042 bool ThumbnailCommand::DoExecuteRetrieve()
2044 if(parameters().size() < 2)
2046 SetReplyString(L"402 THUMBNAIL RETRIEVE ERROR\r\n");
2050 std::wstring filename = env::thumbnails_folder();
2051 filename.append(parameters()[1]);
2052 filename.append(L".png");
2054 std::wstring file_contents;
2056 auto found_file = find_case_insensitive(filename);
2059 file_contents = read_file_base64(boost::filesystem::path(*found_file));
2061 if (file_contents.empty())
2063 SetReplyString(L"404 THUMBNAIL RETRIEVE ERROR\r\n");
2067 std::wstringstream reply;
2069 reply << L"201 THUMBNAIL RETRIEVE OK\r\n";
2070 reply << file_contents;
2072 SetReplyString(reply.str());
2076 bool ThumbnailCommand::DoExecuteList()
2078 std::wstringstream replyString;
2079 replyString << L"200 THUMBNAIL LIST OK\r\n";
2081 for (boost::filesystem::recursive_directory_iterator itr(env::thumbnails_folder()), end; itr != end; ++itr)
2083 if(boost::filesystem::is_regular_file(itr->path()))
2085 if(!boost::iequals(itr->path().extension().wstring(), L".png"))
2088 auto relativePath = boost::filesystem::path(itr->path().wstring().substr(env::thumbnails_folder().size()-1, itr->path().wstring().size()));
2090 auto str = relativePath.replace_extension(L"").generic_wstring();
2091 if(str[0] == '\\' || str[0] == '/')
2092 str = std::wstring(str.begin() + 1, str.end());
2094 auto mtime = boost::filesystem::last_write_time(itr->path());
2095 auto mtime_readable = boost::posix_time::to_iso_wstring(boost::posix_time::from_time_t(mtime));
2096 auto file_size = boost::filesystem::file_size(itr->path());
2098 replyString << L"\"" << str << L"\" " << mtime_readable << L" " << file_size << L"\r\n";
2102 replyString << L"\r\n";
2104 SetReplyString(boost::to_upper_copy(replyString.str()));
2108 bool ThumbnailCommand::DoExecuteGenerate()
2110 if (parameters().size() < 2)
2112 SetReplyString(L"402 THUMBNAIL GENERATE ERROR\r\n");
2118 thumb_gen_->generate(parameters()[1]);
2119 SetReplyString(L"202 THUMBNAIL GENERATE OK\r\n");
2124 SetReplyString(L"500 THUMBNAIL GENERATE ERROR\r\n");
2129 bool ThumbnailCommand::DoExecuteGenerateAll()
2133 thumb_gen_->generate_all();
2134 SetReplyString(L"202 THUMBNAIL GENERATE_ALL OK\r\n");
2139 SetReplyString(L"500 THUMBNAIL GENERATE_ALL ERROR\r\n");
2144 bool KillCommand::DoExecute()
2146 shutdown_server_now_->set_value(false); //false for not attempting to restart
2147 SetReplyString(L"202 KILL OK\r\n");
2151 bool RestartCommand::DoExecute()
2153 shutdown_server_now_->set_value(true); //true for attempting to restart
2154 SetReplyString(L"202 RESTART OK\r\n");
2159 }} //namespace caspar