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/base64.h>
38 #include <core/producer/frame_producer.h>
39 #include <core/video_format.h>
40 #include <core/producer/transition/transition_producer.h>
41 #include <core/frame/frame_transform.h>
42 #include <core/producer/stage.h>
43 #include <core/producer/layer.h>
44 #include <core/mixer/mixer.h>
45 #include <core/consumer/output.h>
46 #include <core/thumbnail_generator.h>
47 #include <core/producer/media_info/media_info.h>
48 #include <core/producer/media_info/media_info_repository.h>
49 #include <core/diagnostics/call_context.h>
50 #include <core/diagnostics/osd_graph.h>
51 #include <core/system_info_provider.h>
53 #include <modules/reroute/producer/reroute_producer.h>
62 #include <boost/date_time/posix_time/posix_time.hpp>
63 #include <boost/lexical_cast.hpp>
64 #include <boost/algorithm/string.hpp>
65 #include <boost/filesystem.hpp>
66 #include <boost/filesystem/fstream.hpp>
67 #include <boost/regex.hpp>
68 #include <boost/property_tree/xml_parser.hpp>
69 #include <boost/locale.hpp>
70 #include <boost/range/adaptor/transformed.hpp>
71 #include <boost/range/algorithm/copy.hpp>
72 #include <boost/archive/iterators/base64_from_binary.hpp>
73 #include <boost/archive/iterators/insert_linebreaks.hpp>
74 #include <boost/archive/iterators/transform_width.hpp>
76 #include <tbb/concurrent_unordered_map.h>
80 102 [action] Information that [action] has happened
81 101 [action] Information that [action] has happened plus one row of data
83 202 [command] OK [command] has been executed
84 201 [command] OK [command] has been executed, plus one row of data
85 200 [command] OK [command] has been executed, plus multiple lines of data. ends with an empty line
87 400 ERROR the command could not be understood
88 401 [command] ERROR invalid/missing channel
89 402 [command] ERROR parameter missing
90 403 [command] ERROR invalid parameter
91 404 [command] ERROR file not found
93 500 FAILED internal error
94 501 [command] FAILED internal error
95 502 [command] FAILED could not read file
96 503 [command] FAILED access denied
98 600 [command] FAILED [command] not implemented
101 namespace caspar { namespace protocol {
103 using namespace core;
105 std::wstring read_file_base64(const boost::filesystem::wpath& file)
107 using namespace boost::archive::iterators;
109 boost::filesystem::ifstream filestream(file, std::ios::binary);
114 auto length = boost::filesystem::file_size(file);
115 std::vector<char> bytes;
116 bytes.resize(length);
117 filestream.read(bytes.data(), length);
119 std::string result(to_base64(bytes.data(), length));
120 return std::wstring(result.begin(), result.end());
123 std::wstring read_utf8_file(const boost::filesystem::wpath& file)
125 std::wstringstream result;
126 boost::filesystem::wifstream filestream(file);
133 result << filestream.rdbuf();
139 std::wstring read_latin1_file(const boost::filesystem::wpath& file)
141 boost::locale::generator gen;
142 gen.locale_cache_enabled(true);
143 gen.categories(boost::locale::codepage_facet);
145 std::stringstream result_stream;
146 boost::filesystem::ifstream filestream(file);
147 filestream.imbue(gen("en_US.ISO8859-1"));
152 result_stream << filestream.rdbuf();
155 std::string result = result_stream.str();
156 std::wstring widened_result;
158 // The first 255 codepoints in unicode is the same as in latin1
160 result | boost::adaptors::transformed(
161 [](char c) { return static_cast<unsigned char>(c); }),
162 std::back_inserter(widened_result));
164 return widened_result;
167 std::wstring read_file(const boost::filesystem::wpath& file)
169 static const uint8_t BOM[] = {0xef, 0xbb, 0xbf};
171 if (!boost::filesystem::exists(file))
176 if (boost::filesystem::file_size(file) >= 3)
178 boost::filesystem::ifstream bom_stream(file);
181 bom_stream.read(header, 3);
184 if (std::memcmp(BOM, header, 3) == 0)
185 return read_utf8_file(file);
188 return read_latin1_file(file);
191 std::wstring MediaInfo(const boost::filesystem::path& path, const spl::shared_ptr<media_info_repository>& media_info_repo)
193 if (!boost::filesystem::is_regular_file(path))
196 auto media_info = media_info_repo->get(path.wstring());
201 auto is_not_digit = [](char c){ return std::isdigit(c) == 0; };
203 auto relativePath = boost::filesystem::wpath(path.wstring().substr(env::media_folder().size() - 1, path.wstring().size()));
205 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(path)));
206 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), is_not_digit), writeTimeStr.end());
207 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
209 auto sizeStr = boost::lexical_cast<std::wstring>(boost::filesystem::file_size(path));
210 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), is_not_digit), sizeStr.end());
211 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
213 auto str = relativePath.replace_extension(L"").generic_wstring();
214 if (str[0] == '\\' || str[0] == '/')
215 str = std::wstring(str.begin() + 1, str.end());
217 return std::wstring()
219 + L"\" " + media_info->clip_type +
221 + L" " + writeTimeWStr +
222 + L" " + boost::lexical_cast<std::wstring>(media_info->duration) +
223 + L" " + boost::lexical_cast<std::wstring>(media_info->time_base.numerator()) + L"/" + boost::lexical_cast<std::wstring>(media_info->time_base.denominator())
227 std::wstring ListMedia(const spl::shared_ptr<media_info_repository>& media_info_repo)
229 std::wstringstream replyString;
230 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end; ++itr)
231 replyString << MediaInfo(itr->path(), media_info_repo);
233 return boost::to_upper_copy(replyString.str());
236 std::wstring ListTemplates(const spl::shared_ptr<core::cg_producer_registry>& cg_registry)
238 std::wstringstream replyString;
240 for (boost::filesystem::recursive_directory_iterator itr(env::template_folder()), end; itr != end; ++itr)
242 if(boost::filesystem::is_regular_file(itr->path()) && cg_registry->is_cg_extension(itr->path().extension().wstring()))
244 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::template_folder().size()-1, itr->path().wstring().size()));
246 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(itr->path())));
247 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), [](char c){ return std::isdigit(c) == 0;}), writeTimeStr.end());
248 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
250 auto sizeStr = boost::lexical_cast<std::string>(boost::filesystem::file_size(itr->path()));
251 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), [](char c){ return std::isdigit(c) == 0;}), sizeStr.end());
253 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
255 auto dir = relativePath.parent_path();
256 auto file = boost::to_upper_copy(relativePath.filename().wstring());
257 relativePath = dir / file;
259 auto str = relativePath.replace_extension(L"").generic_wstring();
260 boost::trim_if(str, boost::is_any_of("\\/"));
262 replyString << L"\"" << str
263 << L"\" " << sizeWStr
264 << L" " << writeTimeWStr
268 return replyString.str();
273 void AMCPCommand::SendReply()
275 if(replyString_.empty())
278 client_->send(std::move(replyString_));
281 bool DiagnosticsCommand::DoExecute()
285 core::diagnostics::osd::show_graphs(true);
287 SetReplyString(L"202 DIAG OK\r\n");
293 CASPAR_LOG_CURRENT_EXCEPTION();
294 SetReplyString(L"502 DIAG FAILED\r\n");
299 bool ChannelGridCommand::DoExecute()
302 auto self = channels().back().channel;
304 core::diagnostics::scoped_call_context save;
305 core::diagnostics::call_context::for_thread().video_channel = channels().size();
307 std::vector<std::wstring> params;
308 params.push_back(L"SCREEN");
309 params.push_back(L"0");
310 params.push_back(L"NAME");
311 params.push_back(L"Channel Grid Window");
312 auto screen = create_consumer(params);
314 self->output().add(screen);
316 for (auto& channel : channels())
318 if(channel.channel != self)
320 core::diagnostics::call_context::for_thread().layer = index;
321 auto producer = reroute::create_producer(*channel.channel);
322 self->stage().load(index, producer, false);
323 self->stage().play(index);
328 int n = channels().size()-1;
329 double delta = 1.0/static_cast<double>(n);
330 for(int x = 0; x < n; ++x)
332 for(int y = 0; y < n; ++y)
335 auto transform = [=](frame_transform transform) -> frame_transform
337 transform.image_transform.fill_translation[0] = x*delta;
338 transform.image_transform.fill_translation[1] = y*delta;
339 transform.image_transform.fill_scale[0] = delta;
340 transform.image_transform.fill_scale[1] = delta;
341 transform.image_transform.clip_translation[0] = x*delta;
342 transform.image_transform.clip_translation[1] = y*delta;
343 transform.image_transform.clip_scale[0] = delta;
344 transform.image_transform.clip_scale[1] = delta;
347 self->stage().apply_transform(index, transform);
354 bool CallCommand::DoExecute()
356 //Perform loading of the clip
359 auto result = channel()->stage().call(layer_index(), parameters());
361 // TODO: because of std::async deferred timed waiting does not work
363 /*auto wait_res = result.wait_for(std::chrono::seconds(2));
364 if (wait_res == std::future_status::timeout)
365 CASPAR_THROW_EXCEPTION(timed_out());*/
367 std::wstringstream replyString;
368 if(result.get().empty())
369 replyString << L"202 CALL OK\r\n";
371 replyString << L"201 CALL OK\r\n" << result.get() << L"\r\n";
373 SetReplyString(replyString.str());
379 CASPAR_LOG_CURRENT_EXCEPTION();
380 SetReplyString(L"502 CALL FAILED\r\n");
385 tbb::concurrent_unordered_map<int, std::vector<stage::transform_tuple_t>> deferred_transforms;
387 bool MixerCommand::DoExecute()
389 //Perform loading of the clip
392 bool defer = boost::iequals(parameters().back(), L"DEFER");
394 parameters().pop_back();
396 std::vector<stage::transform_tuple_t> transforms;
398 if(boost::iequals(parameters()[0], L"KEYER") || boost::iequals(parameters()[0], L"IS_KEY"))
400 bool value = boost::lexical_cast<int>(parameters().at(1));
401 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
403 transform.image_transform.is_key = value;
407 else if(boost::iequals(parameters()[0], L"OPACITY"))
409 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
410 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
412 double value = boost::lexical_cast<double>(parameters().at(1));
414 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
416 transform.image_transform.opacity = value;
418 }, duration, tween));
420 else if(boost::iequals(parameters()[0], L"FILL") || boost::iequals(parameters()[0], L"FILL_RECT"))
422 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
423 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
424 double x = boost::lexical_cast<double>(parameters().at(1));
425 double y = boost::lexical_cast<double>(parameters().at(2));
426 double x_s = boost::lexical_cast<double>(parameters().at(3));
427 double y_s = boost::lexical_cast<double>(parameters().at(4));
429 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) mutable -> frame_transform
431 transform.image_transform.fill_translation[0] = x;
432 transform.image_transform.fill_translation[1] = y;
433 transform.image_transform.fill_scale[0] = x_s;
434 transform.image_transform.fill_scale[1] = y_s;
436 }, duration, tween));
438 else if(boost::iequals(parameters()[0], L"CLIP") || boost::iequals(parameters()[0], L"CLIP_RECT"))
440 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
441 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
442 double x = boost::lexical_cast<double>(parameters().at(1));
443 double y = boost::lexical_cast<double>(parameters().at(2));
444 double x_s = boost::lexical_cast<double>(parameters().at(3));
445 double y_s = boost::lexical_cast<double>(parameters().at(4));
447 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
449 transform.image_transform.clip_translation[0] = x;
450 transform.image_transform.clip_translation[1] = y;
451 transform.image_transform.clip_scale[0] = x_s;
452 transform.image_transform.clip_scale[1] = y_s;
454 }, duration, tween));
456 else if(boost::iequals(parameters()[0], L"GRID"))
458 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
459 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
460 int n = boost::lexical_cast<int>(parameters().at(1));
461 double delta = 1.0/static_cast<double>(n);
462 for(int x = 0; x < n; ++x)
464 for(int y = 0; y < n; ++y)
467 transforms.push_back(stage::transform_tuple_t(index, [=](frame_transform transform) -> frame_transform
469 transform.image_transform.fill_translation[0] = x*delta;
470 transform.image_transform.fill_translation[1] = y*delta;
471 transform.image_transform.fill_scale[0] = delta;
472 transform.image_transform.fill_scale[1] = delta;
473 transform.image_transform.clip_translation[0] = x*delta;
474 transform.image_transform.clip_translation[1] = y*delta;
475 transform.image_transform.clip_scale[0] = delta;
476 transform.image_transform.clip_scale[1] = delta;
478 }, duration, tween));
482 else if(boost::iequals(parameters()[0], L"BLEND"))
484 auto blend_str = parameters().at(1);
485 int layer = layer_index();
486 channel()->mixer().set_blend_mode(layer, get_blend_mode(blend_str));
488 else if(boost::iequals(parameters()[0], L"MASTERVOLUME"))
490 float master_volume = boost::lexical_cast<float>(parameters().at(1));
491 channel()->mixer().set_master_volume(master_volume);
493 else if(boost::iequals(parameters()[0], L"BRIGHTNESS"))
495 auto value = boost::lexical_cast<double>(parameters().at(1));
496 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
497 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
498 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
500 transform.image_transform.brightness = value;
502 }, duration, tween));
504 else if(boost::iequals(parameters()[0], L"SATURATION"))
506 auto value = boost::lexical_cast<double>(parameters().at(1));
507 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
508 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
509 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
511 transform.image_transform.saturation = value;
513 }, duration, tween));
515 else if(parameters()[0] == L"CONTRAST")
517 auto value = boost::lexical_cast<double>(parameters().at(1));
518 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
519 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
520 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
522 transform.image_transform.contrast = value;
524 }, duration, tween));
526 else if(boost::iequals(parameters()[0], L"LEVELS"))
529 value.min_input = boost::lexical_cast<double>(parameters().at(1));
530 value.max_input = boost::lexical_cast<double>(parameters().at(2));
531 value.gamma = boost::lexical_cast<double>(parameters().at(3));
532 value.min_output = boost::lexical_cast<double>(parameters().at(4));
533 value.max_output = boost::lexical_cast<double>(parameters().at(5));
534 int duration = parameters().size() > 6 ? boost::lexical_cast<int>(parameters()[6]) : 0;
535 std::wstring tween = parameters().size() > 7 ? parameters()[7] : L"linear";
537 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
539 transform.image_transform.levels = value;
541 }, duration, tween));
543 else if(boost::iequals(parameters()[0], L"VOLUME"))
545 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
546 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
547 double value = boost::lexical_cast<double>(parameters()[1]);
549 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
551 transform.audio_transform.volume = value;
553 }, duration, tween));
555 else if(boost::iequals(parameters()[0], L"CLEAR"))
557 int layer = layer_index(std::numeric_limits<int>::max());
559 if (layer == std::numeric_limits<int>::max())
561 channel()->stage().clear_transforms();
562 channel()->mixer().clear_blend_modes();
566 channel()->stage().clear_transforms(layer);
567 channel()->mixer().clear_blend_mode(layer);
570 else if(boost::iequals(parameters()[0], L"COMMIT"))
572 transforms = std::move(deferred_transforms[channel_index()]);
576 SetReplyString(L"404 MIXER ERROR\r\n");
582 auto& defer_tranforms = deferred_transforms[channel_index()];
583 defer_tranforms.insert(defer_tranforms.end(), transforms.begin(), transforms.end());
586 channel()->stage().apply_transforms(transforms);
588 SetReplyString(L"202 MIXER OK\r\n");
592 catch(file_not_found&)
594 CASPAR_LOG_CURRENT_EXCEPTION();
595 SetReplyString(L"404 MIXER ERROR\r\n");
600 CASPAR_LOG_CURRENT_EXCEPTION();
601 SetReplyString(L"502 MIXER FAILED\r\n");
606 bool SwapCommand::DoExecute()
608 //Perform loading of the clip
611 if(layer_index(-1) != -1)
613 std::vector<std::string> strs;
614 boost::split(strs, parameters()[0], boost::is_any_of("-"));
616 auto ch1 = channel();
617 auto ch2 = channels().at(boost::lexical_cast<int>(strs.at(0))-1);
619 int l1 = layer_index();
620 int l2 = boost::lexical_cast<int>(strs.at(1));
622 ch1->stage().swap_layer(l1, l2, ch2.channel->stage());
626 auto ch1 = channel();
627 auto ch2 = channels().at(boost::lexical_cast<int>(parameters()[0])-1);
628 ch1->stage().swap_layers(ch2.channel->stage());
631 SetReplyString(L"202 SWAP OK\r\n");
635 catch(file_not_found&)
637 CASPAR_LOG_CURRENT_EXCEPTION();
638 SetReplyString(L"404 SWAP ERROR\r\n");
643 CASPAR_LOG_CURRENT_EXCEPTION();
644 SetReplyString(L"502 SWAP FAILED\r\n");
649 bool AddCommand::DoExecute()
651 //Perform loading of the clip
654 //create_consumer still expects all parameters to be uppercase
655 for (auto& str : parameters())
657 boost::to_upper(str);
660 core::diagnostics::scoped_call_context save;
661 core::diagnostics::call_context::for_thread().video_channel = channel_index() + 1;
663 auto consumer = create_consumer(parameters());
664 channel()->output().add(layer_index(consumer->index()), consumer);
666 SetReplyString(L"202 ADD OK\r\n");
670 catch(file_not_found&)
672 CASPAR_LOG_CURRENT_EXCEPTION();
673 SetReplyString(L"404 ADD ERROR\r\n");
678 CASPAR_LOG_CURRENT_EXCEPTION();
679 SetReplyString(L"502 ADD FAILED\r\n");
684 bool RemoveCommand::DoExecute()
686 //Perform loading of the clip
689 auto index = layer_index(std::numeric_limits<int>::min());
690 if(index == std::numeric_limits<int>::min())
692 //create_consumer still expects all parameters to be uppercase
693 for (auto& str : parameters())
695 boost::to_upper(str);
698 index = create_consumer(parameters())->index();
701 channel()->output().remove(index);
703 SetReplyString(L"202 REMOVE OK\r\n");
707 catch(file_not_found&)
709 CASPAR_LOG_CURRENT_EXCEPTION();
710 SetReplyString(L"404 REMOVE ERROR\r\n");
715 CASPAR_LOG_CURRENT_EXCEPTION();
716 SetReplyString(L"502 REMOVE FAILED\r\n");
721 bool LoadCommand::DoExecute()
723 //Perform loading of the clip
726 core::diagnostics::scoped_call_context save;
727 core::diagnostics::call_context::for_thread().video_channel = channel_index() + 1;
728 core::diagnostics::call_context::for_thread().layer = layer_index();
729 auto pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
730 channel()->stage().load(layer_index(), pFP, true);
732 SetReplyString(L"202 LOAD OK\r\n");
736 catch(file_not_found&)
738 CASPAR_LOG_CURRENT_EXCEPTION();
739 SetReplyString(L"404 LOAD ERROR\r\n");
744 CASPAR_LOG_CURRENT_EXCEPTION();
745 SetReplyString(L"502 LOAD FAILED\r\n");
750 bool LoadbgCommand::DoExecute()
752 transition_info transitionInfo;
756 std::wstring message;
757 for(size_t n = 0; n < parameters().size(); ++n)
758 message += boost::to_upper_copy(parameters()[n]) + L" ";
760 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)?.*)");
762 if(boost::regex_match(message, what, expr))
764 auto transition = what["TRANSITION"].str();
765 transitionInfo.duration = boost::lexical_cast<size_t>(what["DURATION"].str());
766 auto direction = what["DIRECTION"].matched ? what["DIRECTION"].str() : L"";
767 auto tween = what["TWEEN"].matched ? what["TWEEN"].str() : L"";
768 transitionInfo.tweener = tween;
770 if(transition == L"CUT")
771 transitionInfo.type = transition_type::cut;
772 else if(transition == L"MIX")
773 transitionInfo.type = transition_type::mix;
774 else if(transition == L"PUSH")
775 transitionInfo.type = transition_type::push;
776 else if(transition == L"SLIDE")
777 transitionInfo.type = transition_type::slide;
778 else if(transition == L"WIPE")
779 transitionInfo.type = transition_type::wipe;
781 if(direction == L"FROMLEFT")
782 transitionInfo.direction = transition_direction::from_left;
783 else if(direction == L"FROMRIGHT")
784 transitionInfo.direction = transition_direction::from_right;
785 else if(direction == L"LEFT")
786 transitionInfo.direction = transition_direction::from_right;
787 else if(direction == L"RIGHT")
788 transitionInfo.direction = transition_direction::from_left;
791 //Perform loading of the clip
794 std::shared_ptr<core::frame_producer> pFP;
796 static boost::wregex expr(LR"(\[(?<CHANNEL>\d+)\])", boost::regex::icase);
798 core::diagnostics::scoped_call_context save;
799 core::diagnostics::call_context::for_thread().video_channel = channel_index() + 1;
800 core::diagnostics::call_context::for_thread().layer = layer_index();
803 if(boost::regex_match(parameters().at(0), what, expr))
805 auto channel_index = boost::lexical_cast<int>(what["CHANNEL"].str());
806 pFP = reroute::create_producer(*channels().at(channel_index-1).channel);
810 pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
813 if(pFP == frame_producer::empty())
814 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info(parameters().size() > 0 ? parameters()[0] : L""));
816 bool auto_play = contains_param(L"AUTO", parameters());
818 auto pFP2 = create_transition_producer(channel()->video_format_desc().field_mode, spl::make_shared_ptr(pFP), transitionInfo);
820 channel()->stage().load(layer_index(), pFP2, false, transitionInfo.duration); // TODO: LOOP
822 channel()->stage().load(layer_index(), pFP2, false); // TODO: LOOP
825 SetReplyString(L"202 LOADBG OK\r\n");
829 catch(file_not_found&)
831 CASPAR_LOG(error) << L"File not found. No match found for parameters. Check syntax.";
832 SetReplyString(L"404 LOADBG ERROR\r\n");
837 CASPAR_LOG_CURRENT_EXCEPTION();
838 SetReplyString(L"502 LOADBG FAILED\r\n");
843 bool PauseCommand::DoExecute()
847 channel()->stage().pause(layer_index());
848 SetReplyString(L"202 PAUSE OK\r\n");
853 SetReplyString(L"501 PAUSE FAILED\r\n");
859 bool PlayCommand::DoExecute()
863 if(!parameters().empty())
865 LoadbgCommand lbg(*this);
868 throw std::exception();
871 channel()->stage().play(layer_index());
873 SetReplyString(L"202 PLAY OK\r\n");
878 SetReplyString(L"501 PLAY FAILED\r\n");
884 bool StopCommand::DoExecute()
888 channel()->stage().stop(layer_index());
889 SetReplyString(L"202 STOP OK\r\n");
894 SetReplyString(L"501 STOP FAILED\r\n");
900 bool ClearCommand::DoExecute()
902 int index = layer_index(std::numeric_limits<int>::min());
903 if(index != std::numeric_limits<int>::min())
904 channel()->stage().clear(index);
906 channel()->stage().clear();
908 SetReplyString(L"202 CLEAR OK\r\n");
913 bool PrintCommand::DoExecute()
915 channel()->output().add(create_consumer({ L"IMAGE" }));
917 SetReplyString(L"202 PRINT OK\r\n");
922 bool LogCommand::DoExecute()
924 if(boost::iequals(parameters().at(0), L"LEVEL"))
925 log::set_log_level(parameters().at(1));
927 SetReplyString(L"202 LOG OK\r\n");
932 bool CGCommand::DoExecute()
936 std::wstring command = boost::to_upper_copy(parameters()[0]);
937 if(command == L"ADD")
938 return DoExecuteAdd();
939 else if(command == L"PLAY")
940 return DoExecutePlay();
941 else if(command == L"STOP")
942 return DoExecuteStop();
943 else if(command == L"NEXT")
944 return DoExecuteNext();
945 else if(command == L"REMOVE")
946 return DoExecuteRemove();
947 else if(command == L"CLEAR")
948 return DoExecuteClear();
949 else if(command == L"UPDATE")
950 return DoExecuteUpdate();
951 else if(command == L"INVOKE")
952 return DoExecuteInvoke();
953 else if(command == L"INFO")
954 return DoExecuteInfo();
958 CASPAR_LOG_CURRENT_EXCEPTION();
961 SetReplyString(L"403 CG ERROR\r\n");
965 bool CGCommand::ValidateLayer(const std::wstring& layerstring) {
966 int length = layerstring.length();
967 for(int i = 0; i < length; ++i) {
968 if(!std::isdigit(layerstring[i])) {
976 bool CGCommand::DoExecuteAdd() {
977 //CG 1 ADD 0 "template_folder/templatename" [STARTLABEL] 0/1 [DATA]
979 int layer = 0; //_parameters[1]
980 // std::wstring templateName; //_parameters[2]
981 std::wstring label; //_parameters[3]
982 bool bDoStart = false; //_parameters[3] alt. _parameters[4]
983 // std::wstring data; //_parameters[4] alt. _parameters[5]
985 if(parameters().size() < 4)
987 SetReplyString(L"402 CG ERROR\r\n");
990 unsigned int dataIndex = 4;
992 if(!ValidateLayer(parameters()[1]))
994 SetReplyString(L"403 CG ERROR\r\n");
998 layer = boost::lexical_cast<int>(parameters()[1]);
1000 if(parameters()[3].length() > 1)
1002 label = parameters()[3];
1005 if(parameters().size() > 4 && parameters()[4].length() > 0) //read play-on-load-flag
1006 bDoStart = (parameters()[4][0]==L'1') ? true : false;
1009 SetReplyString(L"402 CG ERROR\r\n");
1013 else if(parameters()[3].length() > 0) { //read play-on-load-flag
1014 bDoStart = (parameters()[3][0]==L'1') ? true : false;
1018 SetReplyString(L"403 CG ERROR\r\n");
1022 const wchar_t* pDataString = 0;
1023 std::wstring dataFromFile;
1024 if(parameters().size() > dataIndex)
1026 const std::wstring& dataString = parameters()[dataIndex];
1028 if(dataString[0] == L'<') //the data is an XML-string
1029 pDataString = dataString.c_str();
1032 //The data is not an XML-string, it must be a filename
1033 std::wstring filename = env::data_folder();
1034 filename.append(dataString);
1035 filename.append(L".ftd");
1037 dataFromFile = read_file(boost::filesystem::wpath(filename));
1038 pDataString = dataFromFile.c_str();
1042 auto filename = parameters()[2];
1043 auto proxy = cg_registry_->get_or_create_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER), filename);
1045 if (proxy == core::cg_proxy::empty())
1047 CASPAR_LOG(warning) << "Could not find template " << parameters()[2];
1048 SetReplyString(L"404 CG ERROR\r\n");
1052 proxy->add(layer, filename, bDoStart, label, (pDataString != 0) ? pDataString : L"");
1058 bool CGCommand::DoExecutePlay()
1060 if(parameters().size() > 1)
1062 if(!ValidateLayer(parameters()[1]))
1064 SetReplyString(L"403 CG ERROR\r\n");
1067 int layer = boost::lexical_cast<int>(parameters()[1]);
1068 cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->play(layer);
1072 SetReplyString(L"402 CG ERROR\r\n");
1076 SetReplyString(L"202 CG OK\r\n");
1080 bool CGCommand::DoExecuteStop()
1082 if(parameters().size() > 1)
1084 if(!ValidateLayer(parameters()[1]))
1086 SetReplyString(L"403 CG ERROR\r\n");
1089 int layer = boost::lexical_cast<int>(parameters()[1]);
1090 cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->stop(layer, 0);
1094 SetReplyString(L"402 CG ERROR\r\n");
1098 SetReplyString(L"202 CG OK\r\n");
1102 bool CGCommand::DoExecuteNext()
1104 if(parameters().size() > 1)
1106 if(!ValidateLayer(parameters()[1]))
1108 SetReplyString(L"403 CG ERROR\r\n");
1112 int layer = boost::lexical_cast<int>(parameters()[1]);
1113 cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->next(layer);
1117 SetReplyString(L"402 CG ERROR\r\n");
1121 SetReplyString(L"202 CG OK\r\n");
1125 bool CGCommand::DoExecuteRemove()
1127 if(parameters().size() > 1)
1129 if(!ValidateLayer(parameters()[1]))
1131 SetReplyString(L"403 CG ERROR\r\n");
1135 int layer = boost::lexical_cast<int>(parameters()[1]);
1136 cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->remove(layer);
1140 SetReplyString(L"402 CG ERROR\r\n");
1144 SetReplyString(L"202 CG OK\r\n");
1148 bool CGCommand::DoExecuteClear()
1150 channel()->stage().clear(layer_index(core::cg_proxy::DEFAULT_LAYER));
1151 SetReplyString(L"202 CG OK\r\n");
1155 bool CGCommand::DoExecuteUpdate()
1159 if(!ValidateLayer(parameters().at(1)))
1161 SetReplyString(L"403 CG ERROR\r\n");
1165 std::wstring dataString = parameters().at(2);
1166 if(dataString.at(0) != L'<')
1168 //The data is not an XML-string, it must be a filename
1169 std::wstring filename = env::data_folder();
1170 filename.append(dataString);
1171 filename.append(L".ftd");
1173 dataString = read_file(boost::filesystem::wpath(filename));
1176 int layer = boost::lexical_cast<int>(parameters()[1]);
1177 cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->update(layer, dataString);
1181 SetReplyString(L"402 CG ERROR\r\n");
1185 SetReplyString(L"202 CG OK\r\n");
1189 bool CGCommand::DoExecuteInvoke()
1191 std::wstringstream replyString;
1192 replyString << L"201 CG OK\r\n";
1194 if(parameters().size() > 2)
1196 if(!ValidateLayer(parameters()[1]))
1198 SetReplyString(L"403 CG ERROR\r\n");
1201 int layer = boost::lexical_cast<int>(parameters()[1]);
1202 auto result = cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->invoke(layer, parameters()[2]);
1203 replyString << result << L"\r\n";
1207 SetReplyString(L"402 CG ERROR\r\n");
1211 SetReplyString(replyString.str());
1215 bool CGCommand::DoExecuteInfo()
1217 std::wstringstream replyString;
1218 replyString << L"201 CG OK\r\n";
1220 if(parameters().size() > 1)
1222 if(!ValidateLayer(parameters()[1]))
1224 SetReplyString(L"403 CG ERROR\r\n");
1228 int layer = boost::lexical_cast<int>(parameters()[1]);
1229 auto desc = cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->description(layer);
1231 replyString << desc << L"\r\n";
1235 auto info = cg_registry_->get_proxy(channel(), layer_index(core::cg_proxy::DEFAULT_LAYER))->template_host_info();
1236 replyString << info << L"\r\n";
1239 SetReplyString(replyString.str());
1243 bool DataCommand::DoExecute()
1245 std::wstring command = boost::to_upper_copy(parameters()[0]);
1246 if(command == L"STORE")
1247 return DoExecuteStore();
1248 else if(command == L"RETRIEVE")
1249 return DoExecuteRetrieve();
1250 else if(command == L"REMOVE")
1251 return DoExecuteRemove();
1252 else if(command == L"LIST")
1253 return DoExecuteList();
1255 SetReplyString(L"403 DATA ERROR\r\n");
1259 bool DataCommand::DoExecuteStore()
1261 if(parameters().size() < 3)
1263 SetReplyString(L"402 DATA STORE ERROR\r\n");
1267 std::wstring filename = env::data_folder();
1268 filename.append(parameters()[1]);
1269 filename.append(L".ftd");
1271 auto data_path = boost::filesystem::wpath(
1272 boost::filesystem::wpath(filename).parent_path());
1274 if(!boost::filesystem::exists(data_path))
1275 boost::filesystem::create_directories(data_path);
1277 boost::filesystem::wofstream datafile(filename);
1280 SetReplyString(L"501 DATA STORE FAILED\r\n");
1284 datafile << static_cast<wchar_t>(65279); // UTF-8 BOM character
1285 datafile << parameters()[2] << std::flush;
1288 std::wstring replyString = L"202 DATA STORE OK\r\n";
1289 SetReplyString(replyString);
1293 bool DataCommand::DoExecuteRetrieve()
1295 if(parameters().size() < 2)
1297 SetReplyString(L"402 DATA RETRIEVE ERROR\r\n");
1301 std::wstring filename = env::data_folder();
1302 filename.append(parameters()[1]);
1303 filename.append(L".ftd");
1305 std::wstring file_contents = read_file(boost::filesystem::wpath(filename));
1307 if (file_contents.empty())
1309 SetReplyString(L"404 DATA RETRIEVE ERROR\r\n");
1313 std::wstringstream reply(L"201 DATA RETRIEVE OK\r\n");
1315 std::wstringstream file_contents_stream(file_contents);
1318 bool firstLine = true;
1319 while(std::getline(file_contents_stream, line))
1330 SetReplyString(reply.str());
1334 bool DataCommand::DoExecuteRemove()
1336 if (parameters().size() < 2)
1338 SetReplyString(L"402 DATA REMOVE ERROR\r\n");
1342 std::wstring filename = env::data_folder();
1343 filename.append(parameters()[1]);
1344 filename.append(L".ftd");
1346 if (!boost::filesystem::exists(filename))
1348 SetReplyString(L"404 DATA REMOVE ERROR\r\n");
1352 if (!boost::filesystem::remove(filename))
1354 SetReplyString(L"403 DATA REMOVE ERROR\r\n");
1358 SetReplyString(L"201 DATA REMOVE OK\r\n");
1363 bool DataCommand::DoExecuteList()
1365 std::wstringstream replyString;
1366 replyString << L"200 DATA LIST OK\r\n";
1368 for (boost::filesystem::recursive_directory_iterator itr(env::data_folder()), end; itr != end; ++itr)
1370 if(boost::filesystem::is_regular_file(itr->path()))
1372 if(!boost::iequals(itr->path().extension().wstring(), L".ftd"))
1375 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::data_folder().size()-1, itr->path().wstring().size()));
1377 auto str = relativePath.replace_extension(L"").generic_wstring();
1378 if(str[0] == L'\\' || str[0] == L'/')
1379 str = std::wstring(str.begin() + 1, str.end());
1381 replyString << str << L"\r\n";
1385 replyString << L"\r\n";
1387 SetReplyString(boost::to_upper_copy(replyString.str()));
1391 bool CinfCommand::DoExecute()
1393 std::wstringstream replyString;
1398 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end && info.empty(); ++itr)
1400 auto path = itr->path();
1401 auto file = path.replace_extension(L"").filename();
1402 if(boost::iequals(file.wstring(), parameters().at(0)))
1403 info += MediaInfo(itr->path(), system_info_repo_) + L"\r\n";
1408 SetReplyString(L"404 CINF ERROR\r\n");
1411 replyString << L"200 CINF OK\r\n";
1412 replyString << info << "\r\n";
1416 CASPAR_LOG_CURRENT_EXCEPTION();
1417 SetReplyString(L"404 CINF ERROR\r\n");
1421 SetReplyString(replyString.str());
1425 void GenerateChannelInfo(int index, const spl::shared_ptr<core::video_channel>& pChannel, std::wstringstream& replyString)
1427 replyString << index+1 << L" " << pChannel->video_format_desc().name << L" PLAYING\r\n";
1430 bool InfoCommand::DoExecute()
1432 std::wstringstream replyString;
1434 boost::property_tree::xml_writer_settings<std::wstring> w(' ', 3);
1438 if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"TEMPLATE"))
1440 replyString << L"201 INFO TEMPLATE OK\r\n";
1442 auto filename = parameters().at(1);
1444 std::wstringstream str;
1445 str << u16(cg_registry_->read_meta_info(filename));
1446 boost::property_tree::wptree info;
1447 boost::property_tree::xml_parser::read_xml(str, info, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);
1449 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1451 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"CONFIG"))
1453 replyString << L"201 INFO CONFIG OK\r\n";
1455 boost::property_tree::write_xml(replyString, caspar::env::properties(), w);
1457 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"PATHS"))
1459 replyString << L"201 INFO PATHS OK\r\n";
1461 boost::property_tree::wptree info;
1462 info.add_child(L"paths", caspar::env::properties().get_child(L"configuration.paths"));
1463 info.add(L"paths.initial-path", boost::filesystem::initial_path().wstring() + L"\\");
1465 boost::property_tree::write_xml(replyString, info, w);
1467 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SYSTEM"))
1469 replyString << L"201 INFO SYSTEM OK\r\n";
1471 boost::property_tree::wptree info;
1473 info.add(L"system.name", caspar::system_product_name());
1474 info.add(L"system.os.description", caspar::os_description());
1475 info.add(L"system.cpu", caspar::cpu_info());
1477 system_info_repo_->fill_information(info);
1479 boost::property_tree::write_xml(replyString, info, w);
1481 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SERVER"))
1483 replyString << L"201 INFO SERVER OK\r\n";
1485 boost::property_tree::wptree info;
1488 for (auto& channel : channels())
1489 info.add_child(L"channels.channel", channel.channel->info())
1490 .add(L"index", ++index);
1492 boost::property_tree::write_xml(replyString, info, w);
1496 if(parameters().size() >= 1)
1498 replyString << L"201 INFO OK\r\n";
1499 boost::property_tree::wptree info;
1501 std::vector<std::wstring> split;
1502 boost::split(split, parameters()[0], boost::is_any_of("-"));
1504 int layer = std::numeric_limits<int>::min();
1505 int channel = boost::lexical_cast<int>(split[0]) - 1;
1507 if(split.size() > 1)
1508 layer = boost::lexical_cast<int>(split[1]);
1510 if(layer == std::numeric_limits<int>::min())
1512 info.add_child(L"channel", channels().at(channel).channel->info())
1513 .add(L"index", channel);
1517 if(parameters().size() >= 2)
1519 if(boost::iequals(parameters()[1], L"B"))
1520 info.add_child(L"producer", channels().at(channel).channel->stage().background(layer).get()->info());
1522 info.add_child(L"producer", channels().at(channel).channel->stage().foreground(layer).get()->info());
1526 info.add_child(L"layer", channels().at(channel).channel->stage().info(layer).get())
1527 .add(L"index", layer);
1530 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1534 // This is needed for backwards compatibility with old clients
1535 replyString << L"200 INFO OK\r\n";
1536 for(size_t n = 0; n < channels().size(); ++n)
1537 GenerateChannelInfo(n, channels()[n].channel, replyString);
1544 CASPAR_LOG_CURRENT_EXCEPTION();
1545 SetReplyString(L"403 INFO ERROR\r\n");
1549 replyString << L"\r\n";
1550 SetReplyString(replyString.str());
1554 bool ClsCommand::DoExecute()
1558 std::wstringstream replyString;
1559 replyString << L"200 CLS OK\r\n";
1560 replyString << ListMedia(system_info_repo_);
1561 replyString << L"\r\n";
1562 SetReplyString(boost::to_upper_copy(replyString.str()));
1566 CASPAR_LOG_CURRENT_EXCEPTION();
1567 SetReplyString(L"501 CLS FAILED\r\n");
1574 bool TlsCommand::DoExecute()
1578 std::wstringstream replyString;
1579 replyString << L"200 TLS OK\r\n";
1581 replyString << ListTemplates(cg_registry_);
1582 replyString << L"\r\n";
1584 SetReplyString(replyString.str());
1588 CASPAR_LOG_CURRENT_EXCEPTION();
1589 SetReplyString(L"501 TLS FAILED\r\n");
1595 bool VersionCommand::DoExecute()
1597 std::wstring replyString = L"201 VERSION OK\r\n" + env::version() + L"\r\n";
1599 if (parameters().size() > 0 && !boost::iequals(parameters()[0], L"SERVER"))
1601 auto version = system_info_repo_->get_version(parameters().at(0));
1603 if (version.empty())
1604 replyString = L"403 VERSION ERROR\r\n";
1606 replyString = L"201 VERSION OK\r\n" + version + L"\r\n";
1609 SetReplyString(replyString);
1613 bool ByeCommand::DoExecute()
1615 client()->disconnect();
1619 bool SetCommand::DoExecute()
1623 std::wstring name = boost::to_upper_copy(parameters()[0]);
1624 std::wstring value = boost::to_upper_copy(parameters()[1]);
1628 auto format_desc = core::video_format_desc(value);
1629 if(format_desc.format != core::video_format::invalid)
1631 channel()->video_format_desc(format_desc);
1632 SetReplyString(L"202 SET MODE OK\r\n");
1635 SetReplyString(L"501 SET MODE FAILED\r\n");
1639 this->SetReplyString(L"403 SET ERROR\r\n");
1644 CASPAR_LOG_CURRENT_EXCEPTION();
1645 SetReplyString(L"501 SET FAILED\r\n");
1652 bool LockCommand::DoExecute()
1656 auto it = parameters().begin();
1658 std::shared_ptr<caspar::IO::lock_container> lock;
1661 int channel_index = boost::lexical_cast<int>(*it) - 1;
1662 lock = channels().at(channel_index).lock;
1664 catch(const boost::bad_lexical_cast&) {}
1667 SetReplyString(L"401 LOCK ERROR\r\n");
1674 if(it == parameters().end()) //too few parameters
1676 SetReplyString(L"402 LOCK ERROR\r\n");
1680 std::wstring command = boost::to_upper_copy(*it);
1681 if(command == L"ACQUIRE")
1684 if(it == parameters().end()) //too few parameters
1686 SetReplyString(L"402 LOCK ACQUIRE ERROR\r\n");
1689 std::wstring lock_phrase = (*it);
1691 //TODO: read options
1695 //just lock one channel
1696 if(!lock->try_lock(lock_phrase, client()))
1698 SetReplyString(L"503 LOCK ACQUIRE FAILED\r\n");
1704 //TODO: lock all channels
1705 CASPAR_THROW_EXCEPTION(not_implemented());
1707 SetReplyString(L"202 LOCK ACQUIRE OK\r\n");
1710 else if(command == L"RELEASE")
1714 lock->release_lock(client());
1718 //TODO: release all channels
1719 CASPAR_THROW_EXCEPTION(not_implemented());
1721 SetReplyString(L"202 LOCK RELEASE OK\r\n");
1723 else if(command == L"CLEAR")
1725 std::wstring override_phrase = env::properties().get(L"configuration.lock-clear-phrase", L"");
1726 std::wstring client_override_phrase;
1727 if(!override_phrase.empty())
1730 if(it == parameters().end())
1732 SetReplyString(L"402 LOCK CLEAR ERROR\r\n");
1735 client_override_phrase = (*it);
1740 //just clear one channel
1741 if(client_override_phrase != override_phrase)
1743 SetReplyString(L"503 LOCK CLEAR FAILED\r\n");
1747 lock->clear_locks();
1751 //TODO: clear all channels
1752 CASPAR_THROW_EXCEPTION(not_implemented());
1755 SetReplyString(L"202 LOCK CLEAR OK\r\n");
1759 SetReplyString(L"403 LOCK ERROR\r\n");
1763 catch(not_implemented&)
1765 SetReplyString(L"600 LOCK FAILED\r\n");
1770 CASPAR_LOG_CURRENT_EXCEPTION();
1771 SetReplyString(L"501 LOCK FAILED\r\n");
1778 bool ThumbnailCommand::DoExecute()
1780 std::wstring command = boost::to_upper_copy(parameters()[0]);
1782 if (command == L"RETRIEVE")
1783 return DoExecuteRetrieve();
1784 else if (command == L"LIST")
1785 return DoExecuteList();
1786 else if (command == L"GENERATE")
1787 return DoExecuteGenerate();
1788 else if (command == L"GENERATE_ALL")
1789 return DoExecuteGenerateAll();
1791 SetReplyString(L"403 THUMBNAIL ERROR\r\n");
1795 bool ThumbnailCommand::DoExecuteRetrieve()
1797 if(parameters().size() < 2)
1799 SetReplyString(L"402 THUMBNAIL RETRIEVE ERROR\r\n");
1803 std::wstring filename = env::thumbnails_folder();
1804 filename.append(parameters()[1]);
1805 filename.append(L".png");
1807 std::wstring file_contents = read_file_base64(boost::filesystem::wpath(filename));
1809 if (file_contents.empty())
1811 SetReplyString(L"404 THUMBNAIL RETRIEVE ERROR\r\n");
1815 std::wstringstream reply;
1817 reply << L"201 THUMBNAIL RETRIEVE OK\r\n";
1818 reply << file_contents;
1820 SetReplyString(reply.str());
1824 bool ThumbnailCommand::DoExecuteList()
1826 std::wstringstream replyString;
1827 replyString << L"200 THUMBNAIL LIST OK\r\n";
1829 for (boost::filesystem::recursive_directory_iterator itr(env::thumbnails_folder()), end; itr != end; ++itr)
1831 if(boost::filesystem::is_regular_file(itr->path()))
1833 if(!boost::iequals(itr->path().extension().wstring(), L".png"))
1836 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::thumbnails_folder().size()-1, itr->path().wstring().size()));
1838 auto str = relativePath.replace_extension(L"").generic_wstring();
1839 if(str[0] == '\\' || str[0] == '/')
1840 str = std::wstring(str.begin() + 1, str.end());
1842 auto mtime = boost::filesystem::last_write_time(itr->path());
1843 auto mtime_readable = boost::posix_time::to_iso_wstring(boost::posix_time::from_time_t(mtime));
1844 auto file_size = boost::filesystem::file_size(itr->path());
1846 replyString << L"\"" << str << L"\" " << mtime_readable << L" " << file_size << L"\r\n";
1850 replyString << L"\r\n";
1852 SetReplyString(boost::to_upper_copy(replyString.str()));
1856 bool ThumbnailCommand::DoExecuteGenerate()
1858 if (parameters().size() < 2)
1860 SetReplyString(L"402 THUMBNAIL GENERATE ERROR\r\n");
1866 thumb_gen_->generate(parameters()[1]);
1867 SetReplyString(L"202 THUMBNAIL GENERATE OK\r\n");
1872 SetReplyString(L"500 THUMBNAIL GENERATE ERROR\r\n");
1877 bool ThumbnailCommand::DoExecuteGenerateAll()
1881 thumb_gen_->generate_all();
1882 SetReplyString(L"202 THUMBNAIL GENERATE_ALL OK\r\n");
1887 SetReplyString(L"500 THUMBNAIL GENERATE_ALL ERROR\r\n");
1892 bool KillCommand::DoExecute()
1894 shutdown_server_now_->set_value(false); //false for not attempting to restart
1895 SetReplyString(L"202 KILL OK\r\n");
1899 bool RestartCommand::DoExecute()
1901 shutdown_server_now_->set_value(true); //true for attempting to restart
1902 SetReplyString(L"202 RESTART OK\r\n");
1907 }} //namespace caspar