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>
61 #include <boost/date_time/posix_time/posix_time.hpp>
62 #include <boost/lexical_cast.hpp>
63 #include <boost/algorithm/string.hpp>
64 #include <boost/filesystem.hpp>
65 #include <boost/filesystem/fstream.hpp>
66 #include <boost/regex.hpp>
67 #include <boost/property_tree/xml_parser.hpp>
68 #include <boost/locale.hpp>
69 #include <boost/range/adaptor/transformed.hpp>
70 #include <boost/range/algorithm/copy.hpp>
71 #include <boost/archive/iterators/base64_from_binary.hpp>
72 #include <boost/archive/iterators/insert_linebreaks.hpp>
73 #include <boost/archive/iterators/transform_width.hpp>
75 #include <tbb/concurrent_unordered_map.h>
79 102 [action] Information that [action] has happened
80 101 [action] Information that [action] has happened plus one row of data
82 202 [command] OK [command] has been executed
83 201 [command] OK [command] has been executed, plus one row of data
84 200 [command] OK [command] has been executed, plus multiple lines of data. ends with an empty line
86 400 ERROR the command could not be understood
87 401 [command] ERROR invalid/missing channel
88 402 [command] ERROR parameter missing
89 403 [command] ERROR invalid parameter
90 404 [command] ERROR file not found
92 500 FAILED internal error
93 501 [command] FAILED internal error
94 502 [command] FAILED could not read file
95 503 [command] FAILED access denied
97 600 [command] FAILED [command] not implemented
100 namespace caspar { namespace protocol {
102 using namespace core;
104 std::wstring read_file_base64(const boost::filesystem::path& file)
106 using namespace boost::archive::iterators;
108 boost::filesystem::ifstream filestream(file, std::ios::binary);
113 auto length = boost::filesystem::file_size(file);
114 std::vector<char> bytes;
115 bytes.resize(length);
116 filestream.read(bytes.data(), length);
118 std::string result(to_base64(bytes.data(), length));
119 return std::wstring(result.begin(), result.end());
122 std::wstring read_utf8_file(const boost::filesystem::path& file)
124 std::wstringstream result;
125 boost::filesystem::wifstream filestream(file);
132 result << filestream.rdbuf();
138 std::wstring read_latin1_file(const boost::filesystem::path& file)
140 boost::locale::generator gen;
141 gen.locale_cache_enabled(true);
142 gen.categories(boost::locale::codepage_facet);
144 std::stringstream result_stream;
145 boost::filesystem::ifstream filestream(file);
146 filestream.imbue(gen("en_US.ISO8859-1"));
151 result_stream << filestream.rdbuf();
154 std::string result = result_stream.str();
155 std::wstring widened_result;
157 // The first 255 codepoints in unicode is the same as in latin1
159 result | boost::adaptors::transformed(
160 [](char c) { return static_cast<unsigned char>(c); }),
161 std::back_inserter(widened_result));
163 return widened_result;
166 std::wstring read_file(const boost::filesystem::path& file)
168 static const uint8_t BOM[] = {0xef, 0xbb, 0xbf};
170 if (!boost::filesystem::exists(file))
175 if (boost::filesystem::file_size(file) >= 3)
177 boost::filesystem::ifstream bom_stream(file);
180 bom_stream.read(header, 3);
183 if (std::memcmp(BOM, header, 3) == 0)
184 return read_utf8_file(file);
187 return read_latin1_file(file);
190 std::wstring MediaInfo(const boost::filesystem::path& path, const spl::shared_ptr<media_info_repository>& media_info_repo)
192 if (!boost::filesystem::is_regular_file(path))
195 auto media_info = media_info_repo->get(path.wstring());
200 auto is_not_digit = [](char c){ return std::isdigit(c) == 0; };
202 auto relativePath = boost::filesystem::path(path.wstring().substr(env::media_folder().size() - 1, path.wstring().size()));
204 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(path)));
205 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), is_not_digit), writeTimeStr.end());
206 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
208 auto sizeStr = boost::lexical_cast<std::wstring>(boost::filesystem::file_size(path));
209 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), is_not_digit), sizeStr.end());
210 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
212 auto str = relativePath.replace_extension(L"").generic_wstring();
213 if (str[0] == '\\' || str[0] == '/')
214 str = std::wstring(str.begin() + 1, str.end());
216 return std::wstring()
218 + L"\" " + media_info->clip_type +
220 + L" " + writeTimeWStr +
221 + L" " + boost::lexical_cast<std::wstring>(media_info->duration) +
222 + L" " + boost::lexical_cast<std::wstring>(media_info->time_base.numerator()) + L"/" + boost::lexical_cast<std::wstring>(media_info->time_base.denominator())
226 std::wstring ListMedia(const spl::shared_ptr<media_info_repository>& media_info_repo)
228 std::wstringstream replyString;
229 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end; ++itr)
230 replyString << MediaInfo(itr->path(), media_info_repo);
232 return boost::to_upper_copy(replyString.str());
235 std::wstring ListTemplates(const spl::shared_ptr<core::cg_producer_registry>& cg_registry)
237 std::wstringstream replyString;
239 for (boost::filesystem::recursive_directory_iterator itr(env::template_folder()), end; itr != end; ++itr)
241 if(boost::filesystem::is_regular_file(itr->path()) && cg_registry->is_cg_extension(itr->path().extension().wstring()))
243 auto relativePath = boost::filesystem::path(itr->path().wstring().substr(env::template_folder().size()-1, itr->path().wstring().size()));
245 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(itr->path())));
246 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), [](char c){ return std::isdigit(c) == 0;}), writeTimeStr.end());
247 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
249 auto sizeStr = boost::lexical_cast<std::string>(boost::filesystem::file_size(itr->path()));
250 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), [](char c){ return std::isdigit(c) == 0;}), sizeStr.end());
252 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
254 auto dir = relativePath.parent_path();
255 auto file = boost::to_upper_copy(relativePath.filename().wstring());
256 relativePath = dir / file;
258 auto str = relativePath.replace_extension(L"").generic_wstring();
259 boost::trim_if(str, boost::is_any_of("\\/"));
261 replyString << L"\"" << str
262 << L"\" " << sizeWStr
263 << L" " << writeTimeWStr
267 return replyString.str();
272 void AMCPCommand::SendReply()
274 if(replyString_.empty())
277 client_->send(std::move(replyString_));
280 bool DiagnosticsCommand::DoExecute()
284 core::diagnostics::osd::show_graphs(true);
286 SetReplyString(L"202 DIAG OK\r\n");
292 CASPAR_LOG_CURRENT_EXCEPTION();
293 SetReplyString(L"502 DIAG FAILED\r\n");
298 bool ChannelGridCommand::DoExecute()
301 auto self = channels().back().channel;
303 core::diagnostics::scoped_call_context save;
304 core::diagnostics::call_context::for_thread().video_channel = channels().size();
306 std::vector<std::wstring> params;
307 params.push_back(L"SCREEN");
308 params.push_back(L"0");
309 params.push_back(L"NAME");
310 params.push_back(L"Channel Grid Window");
311 auto screen = create_consumer(params, &self->stage());
313 self->output().add(screen);
315 for (auto& channel : channels())
317 if(channel.channel != self)
319 core::diagnostics::call_context::for_thread().layer = index;
320 auto producer = create_producer(get_dependencies(channel.channel), L"route://" + boost::lexical_cast<std::wstring>(channel.channel->index()));
321 self->stage().load(index, producer, false);
322 self->stage().play(index);
327 MixerCommand mixer(client(), channels().back(), self->index(), -1);
328 auto num_channels = channels().size() - 1;
329 int square_side_length = std::ceil(std::sqrt(num_channels));
330 mixer.parameters().push_back(L"GRID");
331 mixer.parameters().push_back(boost::lexical_cast<std::wstring>(square_side_length));
337 bool CallCommand::DoExecute()
339 //Perform loading of the clip
342 auto result = channel()->stage().call(layer_index(), parameters());
344 // TODO: because of std::async deferred timed waiting does not work
346 /*auto wait_res = result.wait_for(std::chrono::seconds(2));
347 if (wait_res == std::future_status::timeout)
348 CASPAR_THROW_EXCEPTION(timed_out());*/
350 std::wstringstream replyString;
351 if(result.get().empty())
352 replyString << L"202 CALL OK\r\n";
354 replyString << L"201 CALL OK\r\n" << result.get() << L"\r\n";
356 SetReplyString(replyString.str());
362 CASPAR_LOG_CURRENT_EXCEPTION();
363 SetReplyString(L"502 CALL FAILED\r\n");
368 tbb::concurrent_unordered_map<int, std::vector<stage::transform_tuple_t>> deferred_transforms;
370 core::frame_transform MixerCommand::get_current_transform()
372 return channel()->stage().get_current_transform(layer_index()).get();
375 bool MixerCommand::DoExecute()
377 //Perform loading of the clip
380 bool defer = boost::iequals(parameters().back(), L"DEFER");
382 parameters().pop_back();
384 std::vector<stage::transform_tuple_t> transforms;
386 if(boost::iequals(parameters()[0], L"KEYER") || boost::iequals(parameters()[0], L"IS_KEY"))
388 if (parameters().size() == 1)
389 return reply_value([](const frame_transform& t) { return t.image_transform.is_key ? 1 : 0; });
391 bool value = boost::lexical_cast<int>(parameters().at(1));
392 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
394 transform.image_transform.is_key = value;
398 else if(boost::iequals(parameters()[0], L"OPACITY"))
400 if (parameters().size() == 1)
401 return reply_value([](const frame_transform& t) { return t.image_transform.opacity; });
403 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
404 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
406 double value = boost::lexical_cast<double>(parameters().at(1));
408 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
410 transform.image_transform.opacity = value;
412 }, duration, tween));
414 else if (boost::iequals(parameters()[0], L"ANCHOR"))
416 if (parameters().size() == 1)
418 auto transform = get_current_transform().image_transform;
419 auto anchor = transform.anchor;
422 + boost::lexical_cast<std::wstring>(anchor[0]) + L" "
423 + boost::lexical_cast<std::wstring>(anchor[1]) + L"\r\n");
427 int duration = parameters().size() > 3 ? boost::lexical_cast<int>(parameters()[3]) : 0;
428 std::wstring tween = parameters().size() > 4 ? parameters()[4] : L"linear";
429 double x = boost::lexical_cast<double>(parameters().at(1));
430 double y = boost::lexical_cast<double>(parameters().at(2));
432 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) mutable -> frame_transform
434 transform.image_transform.anchor[0] = x;
435 transform.image_transform.anchor[1] = y;
437 }, duration, tween));
439 else if (boost::iequals(parameters()[0], L"FILL") || boost::iequals(parameters()[0], L"FILL_RECT"))
441 if (parameters().size() == 1)
443 auto transform = get_current_transform().image_transform;
444 auto translation = transform.fill_translation;
445 auto scale = transform.fill_scale;
448 + boost::lexical_cast<std::wstring>(translation[0]) + L" "
449 + boost::lexical_cast<std::wstring>(translation[1]) + L" "
450 + boost::lexical_cast<std::wstring>(scale[0]) + L" "
451 + boost::lexical_cast<std::wstring>(scale[1]) + L"\r\n");
455 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
456 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
457 double x = boost::lexical_cast<double>(parameters().at(1));
458 double y = boost::lexical_cast<double>(parameters().at(2));
459 double x_s = boost::lexical_cast<double>(parameters().at(3));
460 double y_s = boost::lexical_cast<double>(parameters().at(4));
462 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) mutable -> frame_transform
464 transform.image_transform.fill_translation[0] = x;
465 transform.image_transform.fill_translation[1] = y;
466 transform.image_transform.fill_scale[0] = x_s;
467 transform.image_transform.fill_scale[1] = y_s;
469 }, duration, tween));
471 else if(boost::iequals(parameters()[0], L"CLIP") || boost::iequals(parameters()[0], L"CLIP_RECT"))
473 if (parameters().size() == 1)
475 auto transform = get_current_transform().image_transform;
476 auto translation = transform.clip_translation;
477 auto scale = transform.clip_scale;
480 + boost::lexical_cast<std::wstring>(translation[0]) + L" "
481 + boost::lexical_cast<std::wstring>(translation[1]) + L" "
482 + boost::lexical_cast<std::wstring>(scale[0]) + L" "
483 + boost::lexical_cast<std::wstring>(scale[1]) + L"\r\n");
487 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
488 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
489 double x = boost::lexical_cast<double>(parameters().at(1));
490 double y = boost::lexical_cast<double>(parameters().at(2));
491 double x_s = boost::lexical_cast<double>(parameters().at(3));
492 double y_s = boost::lexical_cast<double>(parameters().at(4));
494 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
496 transform.image_transform.clip_translation[0] = x;
497 transform.image_transform.clip_translation[1] = y;
498 transform.image_transform.clip_scale[0] = x_s;
499 transform.image_transform.clip_scale[1] = y_s;
501 }, duration, tween));
503 else if (boost::iequals(parameters()[0], L"CROP"))
505 if (parameters().size() == 1)
507 auto crop = get_current_transform().image_transform.crop;
510 + boost::lexical_cast<std::wstring>(crop.ul[0]) + L" "
511 + boost::lexical_cast<std::wstring>(crop.ul[1]) + L" "
512 + boost::lexical_cast<std::wstring>(crop.lr[0]) + L" "
513 + boost::lexical_cast<std::wstring>(crop.lr[1]) + L"\r\n");
517 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
518 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
519 double ul_x = boost::lexical_cast<double>(parameters().at(1));
520 double ul_y = boost::lexical_cast<double>(parameters().at(2));
521 double lr_x = boost::lexical_cast<double>(parameters().at(3));
522 double lr_y = boost::lexical_cast<double>(parameters().at(4));
524 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
526 transform.image_transform.crop.ul[0] = ul_x;
527 transform.image_transform.crop.ul[1] = ul_y;
528 transform.image_transform.crop.lr[0] = lr_x;
529 transform.image_transform.crop.lr[1] = lr_y;
531 }, duration, tween));
533 else if (boost::iequals(parameters()[0], L"PERSPECTIVE"))
535 if (parameters().size() == 1)
537 auto perspective = get_current_transform().image_transform.perspective;
540 + boost::lexical_cast<std::wstring>(perspective.ul[0]) + L" "
541 + boost::lexical_cast<std::wstring>(perspective.ul[1]) + L" "
542 + boost::lexical_cast<std::wstring>(perspective.ur[0]) + L" "
543 + boost::lexical_cast<std::wstring>(perspective.ur[1]) + L" "
544 + boost::lexical_cast<std::wstring>(perspective.lr[0]) + L" "
545 + boost::lexical_cast<std::wstring>(perspective.lr[1]) + L" "
546 + boost::lexical_cast<std::wstring>(perspective.ll[0]) + L" "
547 + boost::lexical_cast<std::wstring>(perspective.ll[1]) + L"\r\n");
551 int duration = parameters().size() > 9 ? boost::lexical_cast<int>(parameters()[9]) : 0;
552 std::wstring tween = parameters().size() > 10 ? parameters()[10] : L"linear";
553 double ul_x = boost::lexical_cast<double>(parameters().at(1));
554 double ul_y = boost::lexical_cast<double>(parameters().at(2));
555 double ur_x = boost::lexical_cast<double>(parameters().at(3));
556 double ur_y = boost::lexical_cast<double>(parameters().at(4));
557 double lr_x = boost::lexical_cast<double>(parameters().at(5));
558 double lr_y = boost::lexical_cast<double>(parameters().at(6));
559 double ll_x = boost::lexical_cast<double>(parameters().at(7));
560 double ll_y = boost::lexical_cast<double>(parameters().at(8));
562 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
564 transform.image_transform.perspective.ul[0] = ul_x;
565 transform.image_transform.perspective.ul[1] = ul_y;
566 transform.image_transform.perspective.ur[0] = ur_x;
567 transform.image_transform.perspective.ur[1] = ur_y;
568 transform.image_transform.perspective.lr[0] = lr_x;
569 transform.image_transform.perspective.lr[1] = lr_y;
570 transform.image_transform.perspective.ll[0] = ll_x;
571 transform.image_transform.perspective.ll[1] = ll_y;
573 }, duration, tween));
575 else if (boost::iequals(parameters()[0], L"GRID"))
577 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
578 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
579 int n = boost::lexical_cast<int>(parameters().at(1));
580 double delta = 1.0/static_cast<double>(n);
581 for(int x = 0; x < n; ++x)
583 for(int y = 0; y < n; ++y)
586 transforms.push_back(stage::transform_tuple_t(index, [=](frame_transform transform) -> frame_transform
588 transform.image_transform.fill_translation[0] = x*delta;
589 transform.image_transform.fill_translation[1] = y*delta;
590 transform.image_transform.fill_scale[0] = delta;
591 transform.image_transform.fill_scale[1] = delta;
592 transform.image_transform.clip_translation[0] = x*delta;
593 transform.image_transform.clip_translation[1] = y*delta;
594 transform.image_transform.clip_scale[0] = delta;
595 transform.image_transform.clip_scale[1] = delta;
597 }, duration, tween));
601 else if (boost::iequals(parameters()[0], L"MIPMAP"))
603 if (parameters().size() == 1)
604 return reply_value([](const frame_transform& t) { return t.image_transform.use_mipmap ? 1 : 0; });
606 bool value = boost::lexical_cast<int>(parameters().at(1));
607 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
609 transform.image_transform.use_mipmap = value;
613 else if(boost::iequals(parameters()[0], L"BLEND"))
615 if (parameters().size() == 1)
616 return reply_value([](const frame_transform& t) { return get_blend_mode(t.image_transform.blend_mode); });
618 auto value = get_blend_mode(parameters().at(1));
619 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
621 transform.image_transform.blend_mode = value;
625 else if (boost::iequals(parameters()[0], L"CHROMA"))
627 if (parameters().size() == 1)
629 auto chroma = get_current_transform().image_transform.chroma;
632 + core::get_chroma_mode(chroma.key) + L" "
633 + boost::lexical_cast<std::wstring>(chroma.threshold) + L" "
634 + boost::lexical_cast<std::wstring>(chroma.softness) + L" "
635 + boost::lexical_cast<std::wstring>(chroma.spill) + L"\r\n");
639 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters().at(5)) : 0;
640 std::wstring tween = parameters().size() > 6 ? parameters().at(6) : L"linear";
643 chroma.key = get_chroma_mode(parameters().at(1));
644 chroma.threshold = boost::lexical_cast<double>(parameters().at(2));
645 chroma.softness = boost::lexical_cast<double>(parameters().at(3));
646 chroma.spill = parameters().size() > 4 ? boost::lexical_cast<double>(parameters().at(4)) : 0.0;
647 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
649 transform.image_transform.chroma = chroma;
651 }, duration, tween));
653 else if (boost::iequals(parameters()[0], L"MASTERVOLUME"))
655 if (parameters().size() == 1)
657 auto volume = channel()->mixer().get_master_volume();
658 SetReplyString(L"201 MIXER OK\r\n"
659 + boost::lexical_cast<std::wstring>(volume)+L"\r\n");
663 float master_volume = boost::lexical_cast<float>(parameters().at(1));
664 channel()->mixer().set_master_volume(master_volume);
666 else if(boost::iequals(parameters()[0], L"BRIGHTNESS"))
668 if (parameters().size() == 1)
669 return reply_value([](const frame_transform& t) { return t.image_transform.brightness; });
671 auto value = boost::lexical_cast<double>(parameters().at(1));
672 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
673 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
674 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
676 transform.image_transform.brightness = value;
678 }, duration, tween));
680 else if(boost::iequals(parameters()[0], L"SATURATION"))
682 if (parameters().size() == 1)
683 return reply_value([](const frame_transform& t) { return t.image_transform.saturation; });
685 auto value = boost::lexical_cast<double>(parameters().at(1));
686 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
687 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
688 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
690 transform.image_transform.saturation = value;
692 }, duration, tween));
694 else if (boost::iequals(parameters()[0], L"CONTRAST"))
696 if (parameters().size() == 1)
697 return reply_value([](const frame_transform& t) { return t.image_transform.contrast; });
699 auto value = boost::lexical_cast<double>(parameters().at(1));
700 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
701 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
702 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
704 transform.image_transform.contrast = value;
706 }, duration, tween));
708 else if (boost::iequals(parameters()[0], L"ROTATION"))
710 static const double PI = 3.141592653589793;
712 if (parameters().size() == 1)
713 return reply_value([](const frame_transform& t) { return t.image_transform.angle / PI * 180.0; });
715 auto value = boost::lexical_cast<double>(parameters().at(1)) * PI / 180.0;
716 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
717 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
718 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
720 transform.image_transform.angle = value;
722 }, duration, tween));
724 else if (boost::iequals(parameters()[0], L"LEVELS"))
726 if (parameters().size() == 1)
728 auto levels = get_current_transform().image_transform.levels;
729 SetReplyString(L"201 MIXER OK\r\n"
730 + boost::lexical_cast<std::wstring>(levels.min_input) + L" "
731 + boost::lexical_cast<std::wstring>(levels.max_input) + L" "
732 + boost::lexical_cast<std::wstring>(levels.gamma) + L" "
733 + boost::lexical_cast<std::wstring>(levels.min_output) + L" "
734 + boost::lexical_cast<std::wstring>(levels.max_output) + L"\r\n");
739 value.min_input = boost::lexical_cast<double>(parameters().at(1));
740 value.max_input = boost::lexical_cast<double>(parameters().at(2));
741 value.gamma = boost::lexical_cast<double>(parameters().at(3));
742 value.min_output = boost::lexical_cast<double>(parameters().at(4));
743 value.max_output = boost::lexical_cast<double>(parameters().at(5));
744 int duration = parameters().size() > 6 ? boost::lexical_cast<int>(parameters()[6]) : 0;
745 std::wstring tween = parameters().size() > 7 ? parameters()[7] : L"linear";
747 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
749 transform.image_transform.levels = value;
751 }, duration, tween));
753 else if(boost::iequals(parameters()[0], L"VOLUME"))
755 if (parameters().size() == 1)
756 return reply_value([](const frame_transform& t) { return t.audio_transform.volume; });
758 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
759 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
760 double value = boost::lexical_cast<double>(parameters()[1]);
762 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
764 transform.audio_transform.volume = value;
766 }, duration, tween));
768 else if(boost::iequals(parameters()[0], L"CLEAR"))
770 int layer = layer_index(std::numeric_limits<int>::max());
772 if (layer == std::numeric_limits<int>::max())
774 channel()->stage().clear_transforms();
778 channel()->stage().clear_transforms(layer);
781 else if(boost::iequals(parameters()[0], L"COMMIT"))
783 transforms = std::move(deferred_transforms[channel_index()]);
787 SetReplyString(L"404 MIXER ERROR\r\n");
793 auto& defer_tranforms = deferred_transforms[channel_index()];
794 defer_tranforms.insert(defer_tranforms.end(), transforms.begin(), transforms.end());
797 channel()->stage().apply_transforms(transforms);
799 SetReplyString(L"202 MIXER OK\r\n");
803 catch(file_not_found&)
805 CASPAR_LOG_CURRENT_EXCEPTION();
806 SetReplyString(L"404 MIXER ERROR\r\n");
811 CASPAR_LOG_CURRENT_EXCEPTION();
812 SetReplyString(L"502 MIXER FAILED\r\n");
817 bool SwapCommand::DoExecute()
819 //Perform loading of the clip
822 if(layer_index(-1) != -1)
824 std::vector<std::string> strs;
825 boost::split(strs, parameters()[0], boost::is_any_of("-"));
827 auto ch1 = channel();
828 auto ch2 = channels().at(boost::lexical_cast<int>(strs.at(0))-1);
830 int l1 = layer_index();
831 int l2 = boost::lexical_cast<int>(strs.at(1));
833 ch1->stage().swap_layer(l1, l2, ch2.channel->stage());
837 auto ch1 = channel();
838 auto ch2 = channels().at(boost::lexical_cast<int>(parameters()[0])-1);
839 ch1->stage().swap_layers(ch2.channel->stage());
842 SetReplyString(L"202 SWAP OK\r\n");
846 catch(file_not_found&)
848 CASPAR_LOG_CURRENT_EXCEPTION();
849 SetReplyString(L"404 SWAP ERROR\r\n");
854 CASPAR_LOG_CURRENT_EXCEPTION();
855 SetReplyString(L"502 SWAP FAILED\r\n");
860 bool AddCommand::DoExecute()
862 //Perform loading of the clip
865 replace_placeholders(
866 L"<CLIENT_IP_ADDRESS>",
867 this->client()->address(),
870 core::diagnostics::scoped_call_context save;
871 core::diagnostics::call_context::for_thread().video_channel = channel_index() + 1;
873 auto consumer = create_consumer(parameters(), &channel()->stage());
874 channel()->output().add(layer_index(consumer->index()), consumer);
876 SetReplyString(L"202 ADD OK\r\n");
880 catch(file_not_found&)
882 CASPAR_LOG_CURRENT_EXCEPTION();
883 SetReplyString(L"404 ADD ERROR\r\n");
888 CASPAR_LOG_CURRENT_EXCEPTION();
889 SetReplyString(L"502 ADD FAILED\r\n");
894 bool RemoveCommand::DoExecute()
896 //Perform loading of the clip
899 auto index = layer_index(std::numeric_limits<int>::min());
900 if(index == std::numeric_limits<int>::min())
902 replace_placeholders(
903 L"<CLIENT_IP_ADDRESS>",
904 this->client()->address(),
907 index = create_consumer(parameters(), &channel()->stage())->index();
910 channel()->output().remove(index);
912 SetReplyString(L"202 REMOVE OK\r\n");
916 catch(file_not_found&)
918 CASPAR_LOG_CURRENT_EXCEPTION();
919 SetReplyString(L"404 REMOVE ERROR\r\n");
924 CASPAR_LOG_CURRENT_EXCEPTION();
925 SetReplyString(L"502 REMOVE FAILED\r\n");
930 bool LoadCommand::DoExecute()
932 //Perform loading of the clip
935 core::diagnostics::scoped_call_context save;
936 core::diagnostics::call_context::for_thread().video_channel = channel_index() + 1;
937 core::diagnostics::call_context::for_thread().layer = layer_index();
938 auto pFP = create_producer(get_dependencies(channel()), parameters());
939 channel()->stage().load(layer_index(), pFP, true);
941 SetReplyString(L"202 LOAD OK\r\n");
945 catch(file_not_found&)
947 CASPAR_LOG_CURRENT_EXCEPTION();
948 SetReplyString(L"404 LOAD ERROR\r\n");
953 CASPAR_LOG_CURRENT_EXCEPTION();
954 SetReplyString(L"502 LOAD FAILED\r\n");
959 bool LoadbgCommand::DoExecute()
961 transition_info transitionInfo;
965 std::wstring message;
966 for(size_t n = 0; n < parameters().size(); ++n)
967 message += boost::to_upper_copy(parameters()[n]) + L" ";
969 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)?.*)");
971 if(boost::regex_match(message, what, expr))
973 auto transition = what["TRANSITION"].str();
974 transitionInfo.duration = boost::lexical_cast<size_t>(what["DURATION"].str());
975 auto direction = what["DIRECTION"].matched ? what["DIRECTION"].str() : L"";
976 auto tween = what["TWEEN"].matched ? what["TWEEN"].str() : L"";
977 transitionInfo.tweener = tween;
979 if(transition == L"CUT")
980 transitionInfo.type = transition_type::cut;
981 else if(transition == L"MIX")
982 transitionInfo.type = transition_type::mix;
983 else if(transition == L"PUSH")
984 transitionInfo.type = transition_type::push;
985 else if(transition == L"SLIDE")
986 transitionInfo.type = transition_type::slide;
987 else if(transition == L"WIPE")
988 transitionInfo.type = transition_type::wipe;
990 if(direction == L"FROMLEFT")
991 transitionInfo.direction = transition_direction::from_left;
992 else if(direction == L"FROMRIGHT")
993 transitionInfo.direction = transition_direction::from_right;
994 else if(direction == L"LEFT")
995 transitionInfo.direction = transition_direction::from_right;
996 else if(direction == L"RIGHT")
997 transitionInfo.direction = transition_direction::from_left;
1000 //Perform loading of the clip
1003 core::diagnostics::scoped_call_context save;
1004 core::diagnostics::call_context::for_thread().video_channel = channel_index() + 1;
1005 core::diagnostics::call_context::for_thread().layer = layer_index();
1007 auto pFP = create_producer(get_dependencies(channel()), parameters());
1009 if(pFP == frame_producer::empty())
1010 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info(parameters().size() > 0 ? parameters()[0] : L""));
1012 bool auto_play = contains_param(L"AUTO", parameters());
1014 auto pFP2 = create_transition_producer(channel()->video_format_desc().field_mode, pFP, transitionInfo);
1016 channel()->stage().load(layer_index(), pFP2, false, transitionInfo.duration); // TODO: LOOP
1018 channel()->stage().load(layer_index(), pFP2, false); // TODO: LOOP
1021 SetReplyString(L"202 LOADBG OK\r\n");
1025 catch(file_not_found&)
1027 CASPAR_LOG(error) << L"File not found. No match found for parameters. Check syntax.";
1028 SetReplyString(L"404 LOADBG ERROR\r\n");
1033 CASPAR_LOG_CURRENT_EXCEPTION();
1034 SetReplyString(L"502 LOADBG FAILED\r\n");
1039 bool PauseCommand::DoExecute()
1043 channel()->stage().pause(layer_index());
1044 SetReplyString(L"202 PAUSE OK\r\n");
1049 SetReplyString(L"501 PAUSE FAILED\r\n");
1055 bool PlayCommand::DoExecute()
1059 if(!parameters().empty())
1061 LoadbgCommand lbg(*this);
1064 throw std::exception();
1067 channel()->stage().play(layer_index());
1069 SetReplyString(L"202 PLAY OK\r\n");
1074 SetReplyString(L"501 PLAY FAILED\r\n");
1080 bool StopCommand::DoExecute()
1084 channel()->stage().stop(layer_index());
1085 SetReplyString(L"202 STOP OK\r\n");
1090 SetReplyString(L"501 STOP FAILED\r\n");
1096 bool ClearCommand::DoExecute()
1098 int index = layer_index(std::numeric_limits<int>::min());
1099 if(index != std::numeric_limits<int>::min())
1100 channel()->stage().clear(index);
1102 channel()->stage().clear();
1104 SetReplyString(L"202 CLEAR OK\r\n");
1109 bool PrintCommand::DoExecute()
1111 channel()->output().add(create_consumer({ L"IMAGE" }, &channel()->stage()));
1113 SetReplyString(L"202 PRINT OK\r\n");
1118 bool LogCommand::DoExecute()
1120 if(boost::iequals(parameters().at(0), L"LEVEL"))
1121 log::set_log_level(parameters().at(1));
1123 SetReplyString(L"202 LOG OK\r\n");
1128 bool CGCommand::DoExecute()
1132 std::wstring command = boost::to_upper_copy(parameters()[0]);
1133 if(command == L"ADD")
1134 return DoExecuteAdd();
1135 else if(command == L"PLAY")
1136 return DoExecutePlay();
1137 else if(command == L"STOP")
1138 return DoExecuteStop();
1139 else if(command == L"NEXT")
1140 return DoExecuteNext();
1141 else if(command == L"REMOVE")
1142 return DoExecuteRemove();
1143 else if(command == L"CLEAR")
1144 return DoExecuteClear();
1145 else if(command == L"UPDATE")
1146 return DoExecuteUpdate();
1147 else if(command == L"INVOKE")
1148 return DoExecuteInvoke();
1149 else if(command == L"INFO")
1150 return DoExecuteInfo();
1154 CASPAR_LOG_CURRENT_EXCEPTION();
1157 SetReplyString(L"403 CG ERROR\r\n");
1161 bool CGCommand::ValidateLayer(const std::wstring& layerstring) {
1162 int length = layerstring.length();
1163 for(int i = 0; i < length; ++i) {
1164 if(!std::isdigit(layerstring[i])) {
1172 bool CGCommand::DoExecuteAdd() {
1173 //CG 1 ADD 0 "template_folder/templatename" [STARTLABEL] 0/1 [DATA]
1175 int layer = 0; //_parameters[1]
1176 // std::wstring templateName; //_parameters[2]
1177 std::wstring label; //_parameters[3]
1178 bool bDoStart = false; //_parameters[3] alt. _parameters[4]
1179 // std::wstring data; //_parameters[4] alt. _parameters[5]
1181 if(parameters().size() < 4)
1183 SetReplyString(L"402 CG ERROR\r\n");
1186 unsigned int dataIndex = 4;
1188 if(!ValidateLayer(parameters()[1]))
1190 SetReplyString(L"403 CG ERROR\r\n");
1194 layer = boost::lexical_cast<int>(parameters()[1]);
1196 if(parameters()[3].length() > 1)
1198 label = parameters()[3];
1201 if(parameters().size() > 4 && parameters()[4].length() > 0) //read play-on-load-flag
1202 bDoStart = (parameters()[4][0]==L'1') ? true : false;
1205 SetReplyString(L"402 CG ERROR\r\n");
1209 else if(parameters()[3].length() > 0) { //read play-on-load-flag
1210 bDoStart = (parameters()[3][0]==L'1') ? true : false;
1214 SetReplyString(L"403 CG ERROR\r\n");
1218 const wchar_t* pDataString = 0;
1219 std::wstring dataFromFile;
1220 if(parameters().size() > dataIndex)
1222 const std::wstring& dataString = parameters()[dataIndex];
1224 if (dataString.at(0) == L'<' || dataString.at(0) == L'{') //the data is XML or Json
1225 pDataString = dataString.c_str();
1228 //The data is not an XML-string, it must be a filename
1229 std::wstring filename = env::data_folder();
1230 filename.append(dataString);
1231 filename.append(L".ftd");
1233 auto found_file = find_case_insensitive(filename);
1237 dataFromFile = read_file(boost::filesystem::path(*found_file));
1238 pDataString = dataFromFile.c_str();
1243 auto filename = parameters()[2];
1244 auto proxy = cg_registry_->get_or_create_proxy(channel(), get_dependencies(channel()), layer_index(core::cg_proxy::DEFAULT_LAYER), filename);
1246 if (proxy == core::cg_proxy::empty())
1248 CASPAR_LOG(warning) << "Could not find template " << parameters()[2];
1249 SetReplyString(L"404 CG ERROR\r\n");
1253 proxy->add(layer, filename, bDoStart, label, (pDataString != 0) ? pDataString : L"");
1259 bool CGCommand::DoExecutePlay()
1261 if(parameters().size() > 1)
1263 if(!ValidateLayer(parameters()[1]))
1265 SetReplyString(L"403 CG ERROR\r\n");
1268 int layer = boost::lexical_cast<int>(parameters()[1]);
1269 cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->play(layer);
1273 SetReplyString(L"402 CG ERROR\r\n");
1277 SetReplyString(L"202 CG OK\r\n");
1281 bool CGCommand::DoExecuteStop()
1283 if(parameters().size() > 1)
1285 if(!ValidateLayer(parameters()[1]))
1287 SetReplyString(L"403 CG ERROR\r\n");
1290 int layer = boost::lexical_cast<int>(parameters()[1]);
1291 cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->stop(layer, 0);
1295 SetReplyString(L"402 CG ERROR\r\n");
1299 SetReplyString(L"202 CG OK\r\n");
1303 bool CGCommand::DoExecuteNext()
1305 if(parameters().size() > 1)
1307 if(!ValidateLayer(parameters()[1]))
1309 SetReplyString(L"403 CG ERROR\r\n");
1313 int layer = boost::lexical_cast<int>(parameters()[1]);
1314 cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->next(layer);
1318 SetReplyString(L"402 CG ERROR\r\n");
1322 SetReplyString(L"202 CG OK\r\n");
1326 bool CGCommand::DoExecuteRemove()
1328 if(parameters().size() > 1)
1330 if(!ValidateLayer(parameters()[1]))
1332 SetReplyString(L"403 CG ERROR\r\n");
1336 int layer = boost::lexical_cast<int>(parameters()[1]);
1337 cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->remove(layer);
1341 SetReplyString(L"402 CG ERROR\r\n");
1345 SetReplyString(L"202 CG OK\r\n");
1349 bool CGCommand::DoExecuteClear()
1351 channel()->stage().clear(layer_index(core::cg_proxy::DEFAULT_LAYER));
1352 SetReplyString(L"202 CG OK\r\n");
1356 bool CGCommand::DoExecuteUpdate()
1360 if(!ValidateLayer(parameters().at(1)))
1362 SetReplyString(L"403 CG ERROR\r\n");
1366 std::wstring dataString = parameters().at(2);
1367 if (dataString.at(0) != L'<' && dataString.at(0) != L'{')
1369 //The data is not XML or Json, it must be a filename
1370 std::wstring filename = env::data_folder();
1371 filename.append(dataString);
1372 filename.append(L".ftd");
1374 dataString = read_file(boost::filesystem::path(filename));
1377 int layer = boost::lexical_cast<int>(parameters()[1]);
1378 cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->update(layer, dataString);
1382 SetReplyString(L"402 CG ERROR\r\n");
1386 SetReplyString(L"202 CG OK\r\n");
1390 bool CGCommand::DoExecuteInvoke()
1392 std::wstringstream replyString;
1393 replyString << L"201 CG OK\r\n";
1395 if(parameters().size() > 2)
1397 if(!ValidateLayer(parameters()[1]))
1399 SetReplyString(L"403 CG ERROR\r\n");
1402 int layer = boost::lexical_cast<int>(parameters()[1]);
1403 auto result = cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->invoke(layer, parameters()[2]);
1404 replyString << result << L"\r\n";
1408 SetReplyString(L"402 CG ERROR\r\n");
1412 SetReplyString(replyString.str());
1416 bool CGCommand::DoExecuteInfo()
1418 std::wstringstream replyString;
1419 replyString << L"201 CG OK\r\n";
1421 if(parameters().size() > 1)
1423 if(!ValidateLayer(parameters()[1]))
1425 SetReplyString(L"403 CG ERROR\r\n");
1429 int layer = boost::lexical_cast<int>(parameters()[1]);
1430 auto desc = cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->description(layer);
1432 replyString << desc << L"\r\n";
1436 auto info = cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->template_host_info();
1437 replyString << info << L"\r\n";
1440 SetReplyString(replyString.str());
1444 bool DataCommand::DoExecute()
1446 std::wstring command = boost::to_upper_copy(parameters()[0]);
1447 if(command == L"STORE")
1448 return DoExecuteStore();
1449 else if(command == L"RETRIEVE")
1450 return DoExecuteRetrieve();
1451 else if(command == L"REMOVE")
1452 return DoExecuteRemove();
1453 else if(command == L"LIST")
1454 return DoExecuteList();
1456 SetReplyString(L"403 DATA ERROR\r\n");
1460 bool DataCommand::DoExecuteStore()
1462 if(parameters().size() < 3)
1464 SetReplyString(L"402 DATA STORE ERROR\r\n");
1468 std::wstring filename = env::data_folder();
1469 filename.append(parameters()[1]);
1470 filename.append(L".ftd");
1472 auto data_path = boost::filesystem::path(filename).parent_path().wstring();
1473 auto found_data_path = find_case_insensitive(data_path);
1475 if (found_data_path)
1476 data_path = *found_data_path;
1478 if(!boost::filesystem::exists(data_path))
1479 boost::filesystem::create_directories(data_path);
1481 auto found_filename = find_case_insensitive(filename);
1484 filename = *found_filename; // Overwrite case insensitive.
1486 boost::filesystem::wofstream datafile(filename);
1489 SetReplyString(L"501 DATA STORE FAILED\r\n");
1493 datafile << static_cast<wchar_t>(65279); // UTF-8 BOM character
1494 datafile << parameters()[2] << std::flush;
1497 std::wstring replyString = L"202 DATA STORE OK\r\n";
1498 SetReplyString(replyString);
1502 bool DataCommand::DoExecuteRetrieve()
1504 if(parameters().size() < 2)
1506 SetReplyString(L"402 DATA RETRIEVE ERROR\r\n");
1510 std::wstring filename = env::data_folder();
1511 filename.append(parameters()[1]);
1512 filename.append(L".ftd");
1514 std::wstring file_contents;
1516 auto found_file = find_case_insensitive(filename);
1519 file_contents = read_file(boost::filesystem::path(*found_file));
1521 if (file_contents.empty())
1523 SetReplyString(L"404 DATA RETRIEVE ERROR\r\n");
1527 std::wstringstream reply(L"201 DATA RETRIEVE OK\r\n");
1529 std::wstringstream file_contents_stream(file_contents);
1532 bool firstLine = true;
1533 while(std::getline(file_contents_stream, line))
1544 SetReplyString(reply.str());
1548 bool DataCommand::DoExecuteRemove()
1550 if (parameters().size() < 2)
1552 SetReplyString(L"402 DATA REMOVE ERROR\r\n");
1556 std::wstring filename = env::data_folder();
1557 filename.append(parameters()[1]);
1558 filename.append(L".ftd");
1560 if (!boost::filesystem::exists(filename))
1562 SetReplyString(L"404 DATA REMOVE ERROR\r\n");
1566 if (!boost::filesystem::remove(filename))
1568 SetReplyString(L"403 DATA REMOVE ERROR\r\n");
1572 SetReplyString(L"201 DATA REMOVE OK\r\n");
1577 bool DataCommand::DoExecuteList()
1579 std::wstringstream replyString;
1580 replyString << L"200 DATA LIST OK\r\n";
1582 for (boost::filesystem::recursive_directory_iterator itr(env::data_folder()), end; itr != end; ++itr)
1584 if(boost::filesystem::is_regular_file(itr->path()))
1586 if(!boost::iequals(itr->path().extension().wstring(), L".ftd"))
1589 auto relativePath = boost::filesystem::path(itr->path().wstring().substr(env::data_folder().size()-1, itr->path().wstring().size()));
1591 auto str = relativePath.replace_extension(L"").generic_wstring();
1592 if(str[0] == L'\\' || str[0] == L'/')
1593 str = std::wstring(str.begin() + 1, str.end());
1595 replyString << str << L"\r\n";
1599 replyString << L"\r\n";
1601 SetReplyString(boost::to_upper_copy(replyString.str()));
1605 bool CinfCommand::DoExecute()
1607 std::wstringstream replyString;
1612 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end && info.empty(); ++itr)
1614 auto path = itr->path();
1615 auto file = path.replace_extension(L"").filename().wstring();
1616 if(boost::iequals(file, parameters().at(0)))
1617 info += MediaInfo(itr->path(), system_info_repo_) + L"\r\n";
1622 SetReplyString(L"404 CINF ERROR\r\n");
1625 replyString << L"200 CINF OK\r\n";
1626 replyString << info << "\r\n";
1630 CASPAR_LOG_CURRENT_EXCEPTION();
1631 SetReplyString(L"404 CINF ERROR\r\n");
1635 SetReplyString(replyString.str());
1639 void GenerateChannelInfo(int index, const spl::shared_ptr<core::video_channel>& pChannel, std::wstringstream& replyString)
1641 replyString << index+1 << L" " << pChannel->video_format_desc().name << L" PLAYING\r\n";
1644 bool InfoCommand::DoExecute()
1646 std::wstringstream replyString;
1648 boost::property_tree::xml_writer_settings<std::wstring> w(' ', 3);
1652 if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"TEMPLATE"))
1654 replyString << L"201 INFO TEMPLATE OK\r\n";
1656 auto filename = parameters().at(1);
1658 std::wstringstream str;
1659 str << u16(cg_registry_->read_meta_info(filename));
1660 boost::property_tree::wptree info;
1661 boost::property_tree::xml_parser::read_xml(str, info, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);
1663 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1665 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"CONFIG"))
1667 replyString << L"201 INFO CONFIG OK\r\n";
1669 boost::property_tree::write_xml(replyString, caspar::env::properties(), w);
1671 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"PATHS"))
1673 replyString << L"201 INFO PATHS OK\r\n";
1675 boost::property_tree::wptree info;
1676 info.add_child(L"paths", caspar::env::properties().get_child(L"configuration.paths"));
1677 info.add(L"paths.initial-path", boost::filesystem::initial_path().wstring() + L"/");
1679 boost::property_tree::write_xml(replyString, info, w);
1681 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SYSTEM"))
1683 replyString << L"201 INFO SYSTEM OK\r\n";
1685 boost::property_tree::wptree info;
1687 info.add(L"system.name", caspar::system_product_name());
1688 info.add(L"system.os.description", caspar::os_description());
1689 info.add(L"system.cpu", caspar::cpu_info());
1691 system_info_repo_->fill_information(info);
1693 boost::property_tree::write_xml(replyString, info, w);
1695 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SERVER"))
1697 replyString << L"201 INFO SERVER OK\r\n";
1699 boost::property_tree::wptree info;
1702 for (auto& channel : channels())
1703 info.add_child(L"channels.channel", channel.channel->info())
1704 .add(L"index", ++index);
1706 boost::property_tree::write_xml(replyString, info, w);
1710 if(parameters().size() >= 1)
1712 replyString << L"201 INFO OK\r\n";
1713 boost::property_tree::wptree info;
1715 std::vector<std::wstring> split;
1716 boost::split(split, parameters()[0], boost::is_any_of("-"));
1718 int layer = std::numeric_limits<int>::min();
1719 int channel = boost::lexical_cast<int>(split[0]) - 1;
1721 if(split.size() > 1)
1722 layer = boost::lexical_cast<int>(split[1]);
1724 if(layer == std::numeric_limits<int>::min())
1726 info.add_child(L"channel", channels().at(channel).channel->info())
1727 .add(L"index", channel);
1731 if(parameters().size() >= 2)
1733 if(boost::iequals(parameters()[1], L"B"))
1734 info.add_child(L"producer", channels().at(channel).channel->stage().background(layer).get()->info());
1736 info.add_child(L"producer", channels().at(channel).channel->stage().foreground(layer).get()->info());
1740 info.add_child(L"layer", channels().at(channel).channel->stage().info(layer).get())
1741 .add(L"index", layer);
1744 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1748 // This is needed for backwards compatibility with old clients
1749 replyString << L"200 INFO OK\r\n";
1750 for(size_t n = 0; n < channels().size(); ++n)
1751 GenerateChannelInfo(n, channels()[n].channel, replyString);
1758 CASPAR_LOG_CURRENT_EXCEPTION();
1759 SetReplyString(L"403 INFO ERROR\r\n");
1763 replyString << L"\r\n";
1764 SetReplyString(replyString.str());
1768 bool ClsCommand::DoExecute()
1772 std::wstringstream replyString;
1773 replyString << L"200 CLS OK\r\n";
1774 replyString << ListMedia(system_info_repo_);
1775 replyString << L"\r\n";
1776 SetReplyString(boost::to_upper_copy(replyString.str()));
1780 CASPAR_LOG_CURRENT_EXCEPTION();
1781 SetReplyString(L"501 CLS FAILED\r\n");
1788 bool TlsCommand::DoExecute()
1792 std::wstringstream replyString;
1793 replyString << L"200 TLS OK\r\n";
1795 replyString << ListTemplates(cg_registry_);
1796 replyString << L"\r\n";
1798 SetReplyString(replyString.str());
1802 CASPAR_LOG_CURRENT_EXCEPTION();
1803 SetReplyString(L"501 TLS FAILED\r\n");
1809 bool VersionCommand::DoExecute()
1811 std::wstring replyString = L"201 VERSION OK\r\n" + env::version() + L"\r\n";
1813 if (parameters().size() > 0 && !boost::iequals(parameters()[0], L"SERVER"))
1815 auto version = system_info_repo_->get_version(parameters().at(0));
1817 if (version.empty())
1818 replyString = L"403 VERSION ERROR\r\n";
1820 replyString = L"201 VERSION OK\r\n" + version + L"\r\n";
1823 SetReplyString(replyString);
1827 bool ByeCommand::DoExecute()
1829 client()->disconnect();
1833 bool SetCommand::DoExecute()
1837 std::wstring name = boost::to_upper_copy(parameters()[0]);
1838 std::wstring value = boost::to_upper_copy(parameters()[1]);
1842 auto format_desc = core::video_format_desc(value);
1843 if(format_desc.format != core::video_format::invalid)
1845 channel()->video_format_desc(format_desc);
1846 SetReplyString(L"202 SET MODE OK\r\n");
1849 SetReplyString(L"501 SET MODE FAILED\r\n");
1853 this->SetReplyString(L"403 SET ERROR\r\n");
1858 CASPAR_LOG_CURRENT_EXCEPTION();
1859 SetReplyString(L"501 SET FAILED\r\n");
1866 bool LockCommand::DoExecute()
1870 auto it = parameters().begin();
1872 std::shared_ptr<caspar::IO::lock_container> lock;
1875 int channel_index = boost::lexical_cast<int>(*it) - 1;
1876 lock = channels().at(channel_index).lock;
1878 catch(const boost::bad_lexical_cast&) {}
1881 SetReplyString(L"401 LOCK ERROR\r\n");
1888 if(it == parameters().end()) //too few parameters
1890 SetReplyString(L"402 LOCK ERROR\r\n");
1894 std::wstring command = boost::to_upper_copy(*it);
1895 if(command == L"ACQUIRE")
1898 if(it == parameters().end()) //too few parameters
1900 SetReplyString(L"402 LOCK ACQUIRE ERROR\r\n");
1903 std::wstring lock_phrase = (*it);
1905 //TODO: read options
1909 //just lock one channel
1910 if(!lock->try_lock(lock_phrase, client()))
1912 SetReplyString(L"503 LOCK ACQUIRE FAILED\r\n");
1918 //TODO: lock all channels
1919 CASPAR_THROW_EXCEPTION(not_implemented());
1921 SetReplyString(L"202 LOCK ACQUIRE OK\r\n");
1924 else if(command == L"RELEASE")
1928 lock->release_lock(client());
1932 //TODO: release all channels
1933 CASPAR_THROW_EXCEPTION(not_implemented());
1935 SetReplyString(L"202 LOCK RELEASE OK\r\n");
1937 else if(command == L"CLEAR")
1939 std::wstring override_phrase = env::properties().get(L"configuration.lock-clear-phrase", L"");
1940 std::wstring client_override_phrase;
1941 if(!override_phrase.empty())
1944 if(it == parameters().end())
1946 SetReplyString(L"402 LOCK CLEAR ERROR\r\n");
1949 client_override_phrase = (*it);
1954 //just clear one channel
1955 if(client_override_phrase != override_phrase)
1957 SetReplyString(L"503 LOCK CLEAR FAILED\r\n");
1961 lock->clear_locks();
1965 //TODO: clear all channels
1966 CASPAR_THROW_EXCEPTION(not_implemented());
1969 SetReplyString(L"202 LOCK CLEAR OK\r\n");
1973 SetReplyString(L"403 LOCK ERROR\r\n");
1977 catch(not_implemented&)
1979 SetReplyString(L"600 LOCK FAILED\r\n");
1984 CASPAR_LOG_CURRENT_EXCEPTION();
1985 SetReplyString(L"501 LOCK FAILED\r\n");
1992 bool ThumbnailCommand::DoExecute()
1994 std::wstring command = boost::to_upper_copy(parameters()[0]);
1996 if (command == L"RETRIEVE")
1997 return DoExecuteRetrieve();
1998 else if (command == L"LIST")
1999 return DoExecuteList();
2000 else if (command == L"GENERATE")
2001 return DoExecuteGenerate();
2002 else if (command == L"GENERATE_ALL")
2003 return DoExecuteGenerateAll();
2005 SetReplyString(L"403 THUMBNAIL ERROR\r\n");
2009 bool ThumbnailCommand::DoExecuteRetrieve()
2011 if(parameters().size() < 2)
2013 SetReplyString(L"402 THUMBNAIL RETRIEVE ERROR\r\n");
2017 std::wstring filename = env::thumbnails_folder();
2018 filename.append(parameters()[1]);
2019 filename.append(L".png");
2021 std::wstring file_contents;
2023 auto found_file = find_case_insensitive(filename);
2026 file_contents = read_file_base64(boost::filesystem::path(*found_file));
2028 if (file_contents.empty())
2030 SetReplyString(L"404 THUMBNAIL RETRIEVE ERROR\r\n");
2034 std::wstringstream reply;
2036 reply << L"201 THUMBNAIL RETRIEVE OK\r\n";
2037 reply << file_contents;
2039 SetReplyString(reply.str());
2043 bool ThumbnailCommand::DoExecuteList()
2045 std::wstringstream replyString;
2046 replyString << L"200 THUMBNAIL LIST OK\r\n";
2048 for (boost::filesystem::recursive_directory_iterator itr(env::thumbnails_folder()), end; itr != end; ++itr)
2050 if(boost::filesystem::is_regular_file(itr->path()))
2052 if(!boost::iequals(itr->path().extension().wstring(), L".png"))
2055 auto relativePath = boost::filesystem::path(itr->path().wstring().substr(env::thumbnails_folder().size()-1, itr->path().wstring().size()));
2057 auto str = relativePath.replace_extension(L"").generic_wstring();
2058 if(str[0] == '\\' || str[0] == '/')
2059 str = std::wstring(str.begin() + 1, str.end());
2061 auto mtime = boost::filesystem::last_write_time(itr->path());
2062 auto mtime_readable = boost::posix_time::to_iso_wstring(boost::posix_time::from_time_t(mtime));
2063 auto file_size = boost::filesystem::file_size(itr->path());
2065 replyString << L"\"" << str << L"\" " << mtime_readable << L" " << file_size << L"\r\n";
2069 replyString << L"\r\n";
2071 SetReplyString(boost::to_upper_copy(replyString.str()));
2075 bool ThumbnailCommand::DoExecuteGenerate()
2077 if (parameters().size() < 2)
2079 SetReplyString(L"402 THUMBNAIL GENERATE ERROR\r\n");
2085 thumb_gen_->generate(parameters()[1]);
2086 SetReplyString(L"202 THUMBNAIL GENERATE OK\r\n");
2091 SetReplyString(L"500 THUMBNAIL GENERATE ERROR\r\n");
2096 bool ThumbnailCommand::DoExecuteGenerateAll()
2100 thumb_gen_->generate_all();
2101 SetReplyString(L"202 THUMBNAIL GENERATE_ALL OK\r\n");
2106 SetReplyString(L"500 THUMBNAIL GENERATE_ALL ERROR\r\n");
2111 bool KillCommand::DoExecute()
2113 shutdown_server_now_->set_value(false); //false for not attempting to restart
2114 SetReplyString(L"202 KILL OK\r\n");
2118 bool RestartCommand::DoExecute()
2120 shutdown_server_now_->set_value(true); //true for attempting to restart
2121 SetReplyString(L"202 RESTART OK\r\n");
2126 }} //namespace caspar