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>
54 #include <modules/flash/flash.h>
55 #include <modules/flash/util/swf.h>
56 #include <modules/flash/producer/flash_producer.h>
57 #include <modules/flash/producer/cg_proxy.h>
66 #include <boost/date_time/posix_time/posix_time.hpp>
67 #include <boost/lexical_cast.hpp>
68 #include <boost/algorithm/string.hpp>
69 #include <boost/filesystem.hpp>
70 #include <boost/filesystem/fstream.hpp>
71 #include <boost/regex.hpp>
72 #include <boost/property_tree/xml_parser.hpp>
73 #include <boost/locale.hpp>
74 #include <boost/range/adaptor/transformed.hpp>
75 #include <boost/range/algorithm/copy.hpp>
76 #include <boost/archive/iterators/base64_from_binary.hpp>
77 #include <boost/archive/iterators/insert_linebreaks.hpp>
78 #include <boost/archive/iterators/transform_width.hpp>
80 #include <tbb/concurrent_unordered_map.h>
84 102 [action] Information that [action] has happened
85 101 [action] Information that [action] has happened plus one row of data
87 202 [command] OK [command] has been executed
88 201 [command] OK [command] has been executed, plus one row of data
89 200 [command] OK [command] has been executed, plus multiple lines of data. ends with an empty line
91 400 ERROR the command could not be understood
92 401 [command] ERROR invalid/missing channel
93 402 [command] ERROR parameter missing
94 403 [command] ERROR invalid parameter
95 404 [command] ERROR file not found
97 500 FAILED internal error
98 501 [command] FAILED internal error
99 502 [command] FAILED could not read file
100 503 [command] FAILED access denied
102 600 [command] FAILED [command] not implemented
105 namespace caspar { namespace protocol {
107 using namespace core;
109 std::wstring read_file_base64(const boost::filesystem::wpath& file)
111 using namespace boost::archive::iterators;
113 boost::filesystem::ifstream filestream(file, std::ios::binary);
118 auto length = boost::filesystem::file_size(file);
119 std::vector<char> bytes;
120 bytes.resize(length);
121 filestream.read(bytes.data(), length);
123 std::string result(to_base64(bytes.data(), length));
124 return std::wstring(result.begin(), result.end());
127 std::wstring read_utf8_file(const boost::filesystem::wpath& file)
129 std::wstringstream result;
130 boost::filesystem::wifstream filestream(file);
137 result << filestream.rdbuf();
143 std::wstring read_latin1_file(const boost::filesystem::wpath& file)
145 boost::locale::generator gen;
146 gen.locale_cache_enabled(true);
147 gen.categories(boost::locale::codepage_facet);
149 std::stringstream result_stream;
150 boost::filesystem::ifstream filestream(file);
151 filestream.imbue(gen("en_US.ISO8859-1"));
156 result_stream << filestream.rdbuf();
159 std::string result = result_stream.str();
160 std::wstring widened_result;
162 // The first 255 codepoints in unicode is the same as in latin1
164 result | boost::adaptors::transformed(
165 [](char c) { return static_cast<unsigned char>(c); }),
166 std::back_inserter(widened_result));
168 return widened_result;
171 std::wstring read_file(const boost::filesystem::wpath& file)
173 static const uint8_t BOM[] = {0xef, 0xbb, 0xbf};
175 if (!boost::filesystem::exists(file))
180 if (boost::filesystem::file_size(file) >= 3)
182 boost::filesystem::ifstream bom_stream(file);
185 bom_stream.read(header, 3);
188 if (std::memcmp(BOM, header, 3) == 0)
189 return read_utf8_file(file);
192 return read_latin1_file(file);
195 std::wstring MediaInfo(const boost::filesystem::path& path, const spl::shared_ptr<media_info_repository>& media_info_repo)
197 if (!boost::filesystem::is_regular_file(path))
200 auto media_info = media_info_repo->get(path.wstring());
205 auto is_not_digit = [](char c){ return std::isdigit(c) == 0; };
207 auto relativePath = boost::filesystem::wpath(path.wstring().substr(env::media_folder().size() - 1, path.wstring().size()));
209 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(path)));
210 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), is_not_digit), writeTimeStr.end());
211 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
213 auto sizeStr = boost::lexical_cast<std::wstring>(boost::filesystem::file_size(path));
214 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), is_not_digit), sizeStr.end());
215 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
217 auto str = relativePath.replace_extension(L"").generic_wstring();
218 if (str[0] == '\\' || str[0] == '/')
219 str = std::wstring(str.begin() + 1, str.end());
221 return std::wstring()
223 + L"\" " + media_info->clip_type +
225 + L" " + writeTimeWStr +
226 + L" " + boost::lexical_cast<std::wstring>(media_info->duration) +
227 + L" " + boost::lexical_cast<std::wstring>(media_info->time_base.numerator()) + L"/" + boost::lexical_cast<std::wstring>(media_info->time_base.denominator())
231 std::wstring ListMedia(const spl::shared_ptr<media_info_repository>& media_info_repo)
233 std::wstringstream replyString;
234 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end; ++itr)
235 replyString << MediaInfo(itr->path(), media_info_repo);
237 return boost::to_upper_copy(replyString.str());
240 std::wstring ListTemplates()
242 std::wstringstream replyString;
244 for (boost::filesystem::recursive_directory_iterator itr(env::template_folder()), end; itr != end; ++itr)
246 if(boost::filesystem::is_regular_file(itr->path()) && (itr->path().extension() == L".ft" || itr->path().extension() == L".ct"))
248 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::template_folder().size()-1, itr->path().wstring().size()));
250 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(itr->path())));
251 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), [](char c){ return std::isdigit(c) == 0;}), writeTimeStr.end());
252 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
254 auto sizeStr = boost::lexical_cast<std::string>(boost::filesystem::file_size(itr->path()));
255 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), [](char c){ return std::isdigit(c) == 0;}), sizeStr.end());
257 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
259 std::wstring dir = relativePath.parent_path().generic_wstring();
260 std::wstring file = boost::to_upper_copy(relativePath.filename().wstring());
261 relativePath = boost::filesystem::wpath(dir + L"/" + file);
263 auto str = relativePath.replace_extension(L"").generic_wstring();
264 boost::trim_if(str, boost::is_any_of("\\/"));
266 replyString << L"\"" << str
267 << L"\" " << sizeWStr
268 << L" " << writeTimeWStr
272 return replyString.str();
277 void AMCPCommand::SendReply()
279 if(replyString_.empty())
282 client_->send(std::move(replyString_));
285 bool DiagnosticsCommand::DoExecute()
289 core::diagnostics::osd::show_graphs(true);
291 SetReplyString(L"202 DIAG OK\r\n");
297 CASPAR_LOG_CURRENT_EXCEPTION();
298 SetReplyString(L"502 DIAG FAILED\r\n");
303 bool ChannelGridCommand::DoExecute()
306 auto self = channels().back().channel;
308 core::diagnostics::scoped_call_context save;
309 core::diagnostics::call_context::for_thread().video_channel = channels().size();
311 std::vector<std::wstring> params;
312 params.push_back(L"SCREEN");
313 params.push_back(L"0");
314 params.push_back(L"NAME");
315 params.push_back(L"Channel Grid Window");
316 auto screen = create_consumer(params);
318 self->output().add(screen);
320 for (auto& channel : channels())
322 if(channel.channel != self)
324 core::diagnostics::call_context::for_thread().layer = index;
325 auto producer = reroute::create_producer(*channel.channel);
326 self->stage().load(index, producer, false);
327 self->stage().play(index);
332 int n = channels().size()-1;
333 double delta = 1.0/static_cast<double>(n);
334 for(int x = 0; x < n; ++x)
336 for(int y = 0; y < n; ++y)
339 auto transform = [=](frame_transform transform) -> frame_transform
341 transform.image_transform.fill_translation[0] = x*delta;
342 transform.image_transform.fill_translation[1] = y*delta;
343 transform.image_transform.fill_scale[0] = delta;
344 transform.image_transform.fill_scale[1] = delta;
345 transform.image_transform.clip_translation[0] = x*delta;
346 transform.image_transform.clip_translation[1] = y*delta;
347 transform.image_transform.clip_scale[0] = delta;
348 transform.image_transform.clip_scale[1] = delta;
351 self->stage().apply_transform(index, transform);
358 bool CallCommand::DoExecute()
360 //Perform loading of the clip
363 auto result = channel()->stage().call(layer_index(), parameters());
365 // TODO: because of std::async deferred timed waiting does not work
367 /*auto wait_res = result.wait_for(std::chrono::seconds(2));
368 if (wait_res == std::future_status::timeout)
369 CASPAR_THROW_EXCEPTION(timed_out());*/
371 std::wstringstream replyString;
372 if(result.get().empty())
373 replyString << L"202 CALL OK\r\n";
375 replyString << L"201 CALL OK\r\n" << result.get() << L"\r\n";
377 SetReplyString(replyString.str());
383 CASPAR_LOG_CURRENT_EXCEPTION();
384 SetReplyString(L"502 CALL FAILED\r\n");
389 tbb::concurrent_unordered_map<int, std::vector<stage::transform_tuple_t>> deferred_transforms;
391 bool MixerCommand::DoExecute()
393 //Perform loading of the clip
396 bool defer = boost::iequals(parameters().back(), L"DEFER");
398 parameters().pop_back();
400 std::vector<stage::transform_tuple_t> transforms;
402 if(boost::iequals(parameters()[0], L"KEYER") || boost::iequals(parameters()[0], L"IS_KEY"))
404 bool value = boost::lexical_cast<int>(parameters().at(1));
405 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
407 transform.image_transform.is_key = value;
411 else if(boost::iequals(parameters()[0], L"OPACITY"))
413 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
414 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
416 double value = boost::lexical_cast<double>(parameters().at(1));
418 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
420 transform.image_transform.opacity = value;
422 }, duration, tween));
424 else if(boost::iequals(parameters()[0], L"FILL") || boost::iequals(parameters()[0], L"FILL_RECT"))
426 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
427 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
428 double x = boost::lexical_cast<double>(parameters().at(1));
429 double y = boost::lexical_cast<double>(parameters().at(2));
430 double x_s = boost::lexical_cast<double>(parameters().at(3));
431 double y_s = boost::lexical_cast<double>(parameters().at(4));
433 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) mutable -> frame_transform
435 transform.image_transform.fill_translation[0] = x;
436 transform.image_transform.fill_translation[1] = y;
437 transform.image_transform.fill_scale[0] = x_s;
438 transform.image_transform.fill_scale[1] = y_s;
440 }, duration, tween));
442 else if(boost::iequals(parameters()[0], L"CLIP") || boost::iequals(parameters()[0], L"CLIP_RECT"))
444 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
445 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
446 double x = boost::lexical_cast<double>(parameters().at(1));
447 double y = boost::lexical_cast<double>(parameters().at(2));
448 double x_s = boost::lexical_cast<double>(parameters().at(3));
449 double y_s = boost::lexical_cast<double>(parameters().at(4));
451 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
453 transform.image_transform.clip_translation[0] = x;
454 transform.image_transform.clip_translation[1] = y;
455 transform.image_transform.clip_scale[0] = x_s;
456 transform.image_transform.clip_scale[1] = y_s;
458 }, duration, tween));
460 else if(boost::iequals(parameters()[0], L"GRID"))
462 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
463 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
464 int n = boost::lexical_cast<int>(parameters().at(1));
465 double delta = 1.0/static_cast<double>(n);
466 for(int x = 0; x < n; ++x)
468 for(int y = 0; y < n; ++y)
471 transforms.push_back(stage::transform_tuple_t(index, [=](frame_transform transform) -> frame_transform
473 transform.image_transform.fill_translation[0] = x*delta;
474 transform.image_transform.fill_translation[1] = y*delta;
475 transform.image_transform.fill_scale[0] = delta;
476 transform.image_transform.fill_scale[1] = delta;
477 transform.image_transform.clip_translation[0] = x*delta;
478 transform.image_transform.clip_translation[1] = y*delta;
479 transform.image_transform.clip_scale[0] = delta;
480 transform.image_transform.clip_scale[1] = delta;
482 }, duration, tween));
486 else if(boost::iequals(parameters()[0], L"BLEND"))
488 auto blend_str = parameters().at(1);
489 int layer = layer_index();
490 channel()->mixer().set_blend_mode(layer, get_blend_mode(blend_str));
492 else if(boost::iequals(parameters()[0], L"MASTERVOLUME"))
494 float master_volume = boost::lexical_cast<float>(parameters().at(1));
495 channel()->mixer().set_master_volume(master_volume);
497 else if(boost::iequals(parameters()[0], L"BRIGHTNESS"))
499 auto value = boost::lexical_cast<double>(parameters().at(1));
500 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
501 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
502 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
504 transform.image_transform.brightness = value;
506 }, duration, tween));
508 else if(boost::iequals(parameters()[0], L"SATURATION"))
510 auto value = boost::lexical_cast<double>(parameters().at(1));
511 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
512 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
513 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
515 transform.image_transform.saturation = value;
517 }, duration, tween));
519 else if(parameters()[0] == L"CONTRAST")
521 auto value = boost::lexical_cast<double>(parameters().at(1));
522 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
523 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
524 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
526 transform.image_transform.contrast = value;
528 }, duration, tween));
530 else if(boost::iequals(parameters()[0], L"LEVELS"))
533 value.min_input = boost::lexical_cast<double>(parameters().at(1));
534 value.max_input = boost::lexical_cast<double>(parameters().at(2));
535 value.gamma = boost::lexical_cast<double>(parameters().at(3));
536 value.min_output = boost::lexical_cast<double>(parameters().at(4));
537 value.max_output = boost::lexical_cast<double>(parameters().at(5));
538 int duration = parameters().size() > 6 ? boost::lexical_cast<int>(parameters()[6]) : 0;
539 std::wstring tween = parameters().size() > 7 ? parameters()[7] : L"linear";
541 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
543 transform.image_transform.levels = value;
545 }, duration, tween));
547 else if(boost::iequals(parameters()[0], L"VOLUME"))
549 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
550 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
551 double value = boost::lexical_cast<double>(parameters()[1]);
553 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
555 transform.audio_transform.volume = value;
557 }, duration, tween));
559 else if(boost::iequals(parameters()[0], L"CLEAR"))
561 int layer = layer_index(std::numeric_limits<int>::max());
563 if (layer == std::numeric_limits<int>::max())
565 channel()->stage().clear_transforms();
566 channel()->mixer().clear_blend_modes();
570 channel()->stage().clear_transforms(layer);
571 channel()->mixer().clear_blend_mode(layer);
574 else if(boost::iequals(parameters()[0], L"COMMIT"))
576 transforms = std::move(deferred_transforms[channel_index()]);
580 SetReplyString(L"404 MIXER ERROR\r\n");
586 auto& defer_tranforms = deferred_transforms[channel_index()];
587 defer_tranforms.insert(defer_tranforms.end(), transforms.begin(), transforms.end());
590 channel()->stage().apply_transforms(transforms);
592 SetReplyString(L"202 MIXER OK\r\n");
596 catch(file_not_found&)
598 CASPAR_LOG_CURRENT_EXCEPTION();
599 SetReplyString(L"404 MIXER ERROR\r\n");
604 CASPAR_LOG_CURRENT_EXCEPTION();
605 SetReplyString(L"502 MIXER FAILED\r\n");
610 bool SwapCommand::DoExecute()
612 //Perform loading of the clip
615 if(layer_index(-1) != -1)
617 std::vector<std::string> strs;
618 boost::split(strs, parameters()[0], boost::is_any_of("-"));
620 auto ch1 = channel();
621 auto ch2 = channels().at(boost::lexical_cast<int>(strs.at(0))-1);
623 int l1 = layer_index();
624 int l2 = boost::lexical_cast<int>(strs.at(1));
626 ch1->stage().swap_layer(l1, l2, ch2.channel->stage());
630 auto ch1 = channel();
631 auto ch2 = channels().at(boost::lexical_cast<int>(parameters()[0])-1);
632 ch1->stage().swap_layers(ch2.channel->stage());
635 SetReplyString(L"202 SWAP OK\r\n");
639 catch(file_not_found&)
641 CASPAR_LOG_CURRENT_EXCEPTION();
642 SetReplyString(L"404 SWAP ERROR\r\n");
647 CASPAR_LOG_CURRENT_EXCEPTION();
648 SetReplyString(L"502 SWAP FAILED\r\n");
653 bool AddCommand::DoExecute()
655 //Perform loading of the clip
658 //create_consumer still expects all parameters to be uppercase
659 for (auto& str : parameters())
661 boost::to_upper(str);
664 core::diagnostics::scoped_call_context save;
665 core::diagnostics::call_context::for_thread().video_channel = channel_index() + 1;
667 auto consumer = create_consumer(parameters());
668 channel()->output().add(layer_index(consumer->index()), consumer);
670 SetReplyString(L"202 ADD OK\r\n");
674 catch(file_not_found&)
676 CASPAR_LOG_CURRENT_EXCEPTION();
677 SetReplyString(L"404 ADD ERROR\r\n");
682 CASPAR_LOG_CURRENT_EXCEPTION();
683 SetReplyString(L"502 ADD FAILED\r\n");
688 bool RemoveCommand::DoExecute()
690 //Perform loading of the clip
693 auto index = layer_index(std::numeric_limits<int>::min());
694 if(index == std::numeric_limits<int>::min())
696 //create_consumer still expects all parameters to be uppercase
697 for (auto& str : parameters())
699 boost::to_upper(str);
702 index = create_consumer(parameters())->index();
705 channel()->output().remove(index);
707 SetReplyString(L"202 REMOVE OK\r\n");
711 catch(file_not_found&)
713 CASPAR_LOG_CURRENT_EXCEPTION();
714 SetReplyString(L"404 REMOVE ERROR\r\n");
719 CASPAR_LOG_CURRENT_EXCEPTION();
720 SetReplyString(L"502 REMOVE FAILED\r\n");
725 bool LoadCommand::DoExecute()
727 //Perform loading of the clip
730 core::diagnostics::scoped_call_context save;
731 core::diagnostics::call_context::for_thread().video_channel = channel_index() + 1;
732 core::diagnostics::call_context::for_thread().layer = layer_index();
733 auto pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
734 channel()->stage().load(layer_index(), pFP, true);
736 SetReplyString(L"202 LOAD OK\r\n");
740 catch(file_not_found&)
742 CASPAR_LOG_CURRENT_EXCEPTION();
743 SetReplyString(L"404 LOAD ERROR\r\n");
748 CASPAR_LOG_CURRENT_EXCEPTION();
749 SetReplyString(L"502 LOAD FAILED\r\n");
756 //std::function<std::wstring()> channel_cg_add_command::parse(const std::wstring& message, const std::vector<renderer::render_device_ptr>& channels)
758 // static boost::wregex expr(L"^CG\\s(?<video_channel>\\d+)-?(?<LAYER>\\d+)?\\sADD\\s(?<FLASH_LAYER>\\d+)\\s(?<TEMPLATE>\\S+)\\s?(?<START_LABEL>\\S\\S+)?\\s?(?<PLAY_ON_LOAD>\\d)?\\s?(?<DATA>.*)?");
760 // boost::wsmatch what;
761 // if(!boost::regex_match(message, what, expr))
764 // auto info = channel_info::parse(what, channels);
766 // int flash_layer_index = boost::lexical_cast<int>(what["FLASH_LAYER"].str());
768 // std::wstring templatename = what["TEMPLATE"].str();
769 // bool play_on_load = what["PLAY_ON_LOAD"].matched ? what["PLAY_ON_LOAD"].str() != L"0" : 0;
770 // std::wstring start_label = what["START_LABEL"].str();
771 // std::wstring data = get_data(what["DATA"].str());
773 // boost::replace_all(templatename, "\"", "");
775 // return [=]() -> std::wstring
777 // std::wstring fullFilename = flash::flash_producer::find_template(server::template_folder() + templatename);
778 // if(fullFilename.empty())
779 // CASPAR_THROW_EXCEPTION(file_not_found());
781 // std::wstring extension = boost::filesystem::wpath(fullFilename).extension();
782 // std::wstring filename = templatename;
783 // filename.append(extension);
785 // flash::flash::create_cg_proxy(info.video_channel, std::max<int>(DEFAULT_CHANNEL_LAYER+1, info.layer_index))
786 // ->add(flash_layer_index, filename, play_on_load, start_label, data);
788 // CASPAR_LOG(info) << L"Executed [amcp_channel_cg_add]";
792 bool LoadbgCommand::DoExecute()
794 transition_info transitionInfo;
798 std::wstring message;
799 for(size_t n = 0; n < parameters().size(); ++n)
800 message += boost::to_upper_copy(parameters()[n]) + L" ";
802 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)?.*)");
804 if(boost::regex_match(message, what, expr))
806 auto transition = what["TRANSITION"].str();
807 transitionInfo.duration = boost::lexical_cast<size_t>(what["DURATION"].str());
808 auto direction = what["DIRECTION"].matched ? what["DIRECTION"].str() : L"";
809 auto tween = what["TWEEN"].matched ? what["TWEEN"].str() : L"";
810 transitionInfo.tweener = tween;
812 if(transition == L"CUT")
813 transitionInfo.type = transition_type::cut;
814 else if(transition == L"MIX")
815 transitionInfo.type = transition_type::mix;
816 else if(transition == L"PUSH")
817 transitionInfo.type = transition_type::push;
818 else if(transition == L"SLIDE")
819 transitionInfo.type = transition_type::slide;
820 else if(transition == L"WIPE")
821 transitionInfo.type = transition_type::wipe;
823 if(direction == L"FROMLEFT")
824 transitionInfo.direction = transition_direction::from_left;
825 else if(direction == L"FROMRIGHT")
826 transitionInfo.direction = transition_direction::from_right;
827 else if(direction == L"LEFT")
828 transitionInfo.direction = transition_direction::from_right;
829 else if(direction == L"RIGHT")
830 transitionInfo.direction = transition_direction::from_left;
833 //Perform loading of the clip
836 std::shared_ptr<core::frame_producer> pFP;
838 static boost::wregex expr(LR"(\[(?<CHANNEL>\d+)\])", boost::regex::icase);
840 core::diagnostics::scoped_call_context save;
841 core::diagnostics::call_context::for_thread().video_channel = channel_index() + 1;
842 core::diagnostics::call_context::for_thread().layer = layer_index();
845 if(boost::regex_match(parameters().at(0), what, expr))
847 auto channel_index = boost::lexical_cast<int>(what["CHANNEL"].str());
848 pFP = reroute::create_producer(*channels().at(channel_index-1).channel);
852 pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
855 if(pFP == frame_producer::empty())
856 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info(parameters().size() > 0 ? parameters()[0] : L""));
858 bool auto_play = contains_param(L"AUTO", parameters());
860 auto pFP2 = create_transition_producer(channel()->video_format_desc().field_mode, spl::make_shared_ptr(pFP), transitionInfo);
862 channel()->stage().load(layer_index(), pFP2, false, transitionInfo.duration); // TODO: LOOP
864 channel()->stage().load(layer_index(), pFP2, false); // TODO: LOOP
867 SetReplyString(L"202 LOADBG OK\r\n");
871 catch(file_not_found&)
873 CASPAR_LOG(error) << L"File not found. No match found for parameters. Check syntax.";
874 SetReplyString(L"404 LOADBG ERROR\r\n");
879 CASPAR_LOG_CURRENT_EXCEPTION();
880 SetReplyString(L"502 LOADBG FAILED\r\n");
885 bool PauseCommand::DoExecute()
889 channel()->stage().pause(layer_index());
890 SetReplyString(L"202 PAUSE OK\r\n");
895 SetReplyString(L"501 PAUSE FAILED\r\n");
901 bool PlayCommand::DoExecute()
905 if(!parameters().empty())
907 LoadbgCommand lbg(*this);
910 throw std::exception();
913 channel()->stage().play(layer_index());
915 SetReplyString(L"202 PLAY OK\r\n");
920 SetReplyString(L"501 PLAY FAILED\r\n");
926 bool StopCommand::DoExecute()
930 channel()->stage().stop(layer_index());
931 SetReplyString(L"202 STOP OK\r\n");
936 SetReplyString(L"501 STOP FAILED\r\n");
942 bool ClearCommand::DoExecute()
944 int index = layer_index(std::numeric_limits<int>::min());
945 if(index != std::numeric_limits<int>::min())
946 channel()->stage().clear(index);
948 channel()->stage().clear();
950 SetReplyString(L"202 CLEAR OK\r\n");
955 bool PrintCommand::DoExecute()
957 channel()->output().add(create_consumer({ L"IMAGE" }));
959 SetReplyString(L"202 PRINT OK\r\n");
964 bool LogCommand::DoExecute()
966 if(boost::iequals(parameters().at(0), L"LEVEL"))
967 log::set_log_level(parameters().at(1));
969 SetReplyString(L"202 LOG OK\r\n");
974 bool CGCommand::DoExecute()
978 std::wstring command = boost::to_upper_copy(parameters()[0]);
979 if(command == L"ADD")
980 return DoExecuteAdd();
981 else if(command == L"PLAY")
982 return DoExecutePlay();
983 else if(command == L"STOP")
984 return DoExecuteStop();
985 else if(command == L"NEXT")
986 return DoExecuteNext();
987 else if(command == L"REMOVE")
988 return DoExecuteRemove();
989 else if(command == L"CLEAR")
990 return DoExecuteClear();
991 else if(command == L"UPDATE")
992 return DoExecuteUpdate();
993 else if(command == L"INVOKE")
994 return DoExecuteInvoke();
995 else if(command == L"INFO")
996 return DoExecuteInfo();
1000 CASPAR_LOG_CURRENT_EXCEPTION();
1003 SetReplyString(L"403 CG ERROR\r\n");
1007 bool CGCommand::ValidateLayer(const std::wstring& layerstring) {
1008 int length = layerstring.length();
1009 for(int i = 0; i < length; ++i) {
1010 if(!std::isdigit(layerstring[i])) {
1018 bool CGCommand::DoExecuteAdd() {
1019 //CG 1 ADD 0 "template_folder/templatename" [STARTLABEL] 0/1 [DATA]
1021 int layer = 0; //_parameters[1]
1022 // std::wstring templateName; //_parameters[2]
1023 std::wstring label; //_parameters[3]
1024 bool bDoStart = false; //_parameters[3] alt. _parameters[4]
1025 // std::wstring data; //_parameters[4] alt. _parameters[5]
1027 if(parameters().size() < 4)
1029 SetReplyString(L"402 CG ERROR\r\n");
1032 unsigned int dataIndex = 4;
1034 if(!ValidateLayer(parameters()[1]))
1036 SetReplyString(L"403 CG ERROR\r\n");
1040 layer = boost::lexical_cast<int>(parameters()[1]);
1042 if(parameters()[3].length() > 1)
1044 label = parameters()[3];
1047 if(parameters().size() > 4 && parameters()[4].length() > 0) //read play-on-load-flag
1048 bDoStart = (parameters()[4][0]==L'1') ? true : false;
1051 SetReplyString(L"402 CG ERROR\r\n");
1055 else if(parameters()[3].length() > 0) { //read play-on-load-flag
1056 bDoStart = (parameters()[3][0]==L'1') ? true : false;
1060 SetReplyString(L"403 CG ERROR\r\n");
1064 const wchar_t* pDataString = 0;
1065 std::wstring dataFromFile;
1066 if(parameters().size() > dataIndex)
1068 const std::wstring& dataString = parameters()[dataIndex];
1070 if(dataString[0] == L'<') //the data is an XML-string
1071 pDataString = dataString.c_str();
1074 //The data is not an XML-string, it must be a filename
1075 std::wstring filename = env::data_folder();
1076 filename.append(dataString);
1077 filename.append(L".ftd");
1079 dataFromFile = read_file(boost::filesystem::wpath(filename));
1080 pDataString = dataFromFile.c_str();
1084 std::wstring fullFilename = flash::find_template(env::template_folder() + parameters()[2]);
1085 if(!fullFilename.empty())
1087 std::wstring extension = boost::filesystem::path(fullFilename).extension().wstring();
1088 std::wstring filename = parameters()[2];
1089 filename.append(extension);
1091 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).add(layer, filename, bDoStart, label, (pDataString!=0) ? pDataString : L"");
1092 SetReplyString(L"202 CG OK\r\n");
1096 CASPAR_LOG(warning) << "Could not find template " << parameters()[2];
1097 SetReplyString(L"404 CG ERROR\r\n");
1102 bool CGCommand::DoExecutePlay()
1104 if(parameters().size() > 1)
1106 if(!ValidateLayer(parameters()[1]))
1108 SetReplyString(L"403 CG ERROR\r\n");
1111 int layer = boost::lexical_cast<int>(parameters()[1]);
1112 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).play(layer);
1116 SetReplyString(L"402 CG ERROR\r\n");
1120 SetReplyString(L"202 CG OK\r\n");
1124 bool CGCommand::DoExecuteStop()
1126 if(parameters().size() > 1)
1128 if(!ValidateLayer(parameters()[1]))
1130 SetReplyString(L"403 CG ERROR\r\n");
1133 int layer = boost::lexical_cast<int>(parameters()[1]);
1134 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).stop(layer, 0);
1138 SetReplyString(L"402 CG ERROR\r\n");
1142 SetReplyString(L"202 CG OK\r\n");
1146 bool CGCommand::DoExecuteNext()
1148 if(parameters().size() > 1)
1150 if(!ValidateLayer(parameters()[1]))
1152 SetReplyString(L"403 CG ERROR\r\n");
1156 int layer = boost::lexical_cast<int>(parameters()[1]);
1157 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).next(layer);
1161 SetReplyString(L"402 CG ERROR\r\n");
1165 SetReplyString(L"202 CG OK\r\n");
1169 bool CGCommand::DoExecuteRemove()
1171 if(parameters().size() > 1)
1173 if(!ValidateLayer(parameters()[1]))
1175 SetReplyString(L"403 CG ERROR\r\n");
1179 int layer = boost::lexical_cast<int>(parameters()[1]);
1180 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).remove(layer);
1184 SetReplyString(L"402 CG ERROR\r\n");
1188 SetReplyString(L"202 CG OK\r\n");
1192 bool CGCommand::DoExecuteClear()
1194 channel()->stage().clear(layer_index(flash::cg_proxy::DEFAULT_LAYER));
1195 SetReplyString(L"202 CG OK\r\n");
1199 bool CGCommand::DoExecuteUpdate()
1203 if(!ValidateLayer(parameters().at(1)))
1205 SetReplyString(L"403 CG ERROR\r\n");
1209 std::wstring dataString = parameters().at(2);
1210 if(dataString.at(0) != L'<')
1212 //The data is not an XML-string, it must be a filename
1213 std::wstring filename = env::data_folder();
1214 filename.append(dataString);
1215 filename.append(L".ftd");
1217 dataString = read_file(boost::filesystem::wpath(filename));
1220 int layer = boost::lexical_cast<int>(parameters()[1]);
1221 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).update(layer, dataString);
1225 SetReplyString(L"402 CG ERROR\r\n");
1229 SetReplyString(L"202 CG OK\r\n");
1233 bool CGCommand::DoExecuteInvoke()
1235 std::wstringstream replyString;
1236 replyString << L"201 CG OK\r\n";
1238 if(parameters().size() > 2)
1240 if(!ValidateLayer(parameters()[1]))
1242 SetReplyString(L"403 CG ERROR\r\n");
1245 int layer = boost::lexical_cast<int>(parameters()[1]);
1246 auto result = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).invoke(layer, parameters()[2]);
1247 replyString << result << L"\r\n";
1251 SetReplyString(L"402 CG ERROR\r\n");
1255 SetReplyString(replyString.str());
1259 bool CGCommand::DoExecuteInfo()
1261 std::wstringstream replyString;
1262 replyString << L"201 CG OK\r\n";
1264 if(parameters().size() > 1)
1266 if(!ValidateLayer(parameters()[1]))
1268 SetReplyString(L"403 CG ERROR\r\n");
1272 int layer = boost::lexical_cast<int>(parameters()[1]);
1273 auto desc = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).description(layer);
1275 replyString << desc << L"\r\n";
1279 auto info = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).template_host_info();
1280 replyString << info << L"\r\n";
1283 SetReplyString(replyString.str());
1287 bool DataCommand::DoExecute()
1289 std::wstring command = boost::to_upper_copy(parameters()[0]);
1290 if(command == L"STORE")
1291 return DoExecuteStore();
1292 else if(command == L"RETRIEVE")
1293 return DoExecuteRetrieve();
1294 else if(command == L"REMOVE")
1295 return DoExecuteRemove();
1296 else if(command == L"LIST")
1297 return DoExecuteList();
1299 SetReplyString(L"403 DATA ERROR\r\n");
1303 bool DataCommand::DoExecuteStore()
1305 if(parameters().size() < 3)
1307 SetReplyString(L"402 DATA STORE ERROR\r\n");
1311 std::wstring filename = env::data_folder();
1312 filename.append(parameters()[1]);
1313 filename.append(L".ftd");
1315 auto data_path = boost::filesystem::wpath(
1316 boost::filesystem::wpath(filename).parent_path());
1318 if(!boost::filesystem::exists(data_path))
1319 boost::filesystem::create_directories(data_path);
1321 boost::filesystem::wofstream datafile(filename);
1324 SetReplyString(L"501 DATA STORE FAILED\r\n");
1328 datafile << static_cast<wchar_t>(65279); // UTF-8 BOM character
1329 datafile << parameters()[2] << std::flush;
1332 std::wstring replyString = L"202 DATA STORE OK\r\n";
1333 SetReplyString(replyString);
1337 bool DataCommand::DoExecuteRetrieve()
1339 if(parameters().size() < 2)
1341 SetReplyString(L"402 DATA RETRIEVE ERROR\r\n");
1345 std::wstring filename = env::data_folder();
1346 filename.append(parameters()[1]);
1347 filename.append(L".ftd");
1349 std::wstring file_contents = read_file(boost::filesystem::wpath(filename));
1351 if (file_contents.empty())
1353 SetReplyString(L"404 DATA RETRIEVE ERROR\r\n");
1357 std::wstringstream reply(L"201 DATA RETRIEVE OK\r\n");
1359 std::wstringstream file_contents_stream(file_contents);
1362 bool firstLine = true;
1363 while(std::getline(file_contents_stream, line))
1374 SetReplyString(reply.str());
1378 bool DataCommand::DoExecuteRemove()
1380 if (parameters().size() < 2)
1382 SetReplyString(L"402 DATA REMOVE ERROR\r\n");
1386 std::wstring filename = env::data_folder();
1387 filename.append(parameters()[1]);
1388 filename.append(L".ftd");
1390 if (!boost::filesystem::exists(filename))
1392 SetReplyString(L"404 DATA REMOVE ERROR\r\n");
1396 if (!boost::filesystem::remove(filename))
1398 SetReplyString(L"403 DATA REMOVE ERROR\r\n");
1402 SetReplyString(L"201 DATA REMOVE OK\r\n");
1407 bool DataCommand::DoExecuteList()
1409 std::wstringstream replyString;
1410 replyString << L"200 DATA LIST OK\r\n";
1412 for (boost::filesystem::recursive_directory_iterator itr(env::data_folder()), end; itr != end; ++itr)
1414 if(boost::filesystem::is_regular_file(itr->path()))
1416 if(!boost::iequals(itr->path().extension().wstring(), L".ftd"))
1419 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::data_folder().size()-1, itr->path().wstring().size()));
1421 auto str = relativePath.replace_extension(L"").generic_wstring();
1422 if(str[0] == L'\\' || str[0] == L'/')
1423 str = std::wstring(str.begin() + 1, str.end());
1425 replyString << str << L"\r\n";
1429 replyString << L"\r\n";
1431 SetReplyString(boost::to_upper_copy(replyString.str()));
1435 bool CinfCommand::DoExecute()
1437 std::wstringstream replyString;
1442 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end && info.empty(); ++itr)
1444 auto path = itr->path();
1445 auto file = path.replace_extension(L"").filename();
1446 if(boost::iequals(file.wstring(), parameters().at(0)))
1447 info += MediaInfo(itr->path(), repo_) + L"\r\n";
1452 SetReplyString(L"404 CINF ERROR\r\n");
1455 replyString << L"200 CINF OK\r\n";
1456 replyString << info << "\r\n";
1460 CASPAR_LOG_CURRENT_EXCEPTION();
1461 SetReplyString(L"404 CINF ERROR\r\n");
1465 SetReplyString(replyString.str());
1469 void GenerateChannelInfo(int index, const spl::shared_ptr<core::video_channel>& pChannel, std::wstringstream& replyString)
1471 replyString << index+1 << L" " << pChannel->video_format_desc().name << L" PLAYING\r\n";
1474 bool InfoCommand::DoExecute()
1476 std::wstringstream replyString;
1478 boost::property_tree::xml_writer_settings<std::wstring> w(' ', 3);
1482 if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"TEMPLATE"))
1484 replyString << L"201 INFO TEMPLATE OK\r\n";
1486 // Needs to be extended for any file, not just flash.
1488 auto filename = flash::find_template(env::template_folder() + parameters().at(1));
1490 std::wstringstream str;
1491 str << u16(flash::read_template_meta_info(filename));
1492 boost::property_tree::wptree info;
1493 boost::property_tree::xml_parser::read_xml(str, info, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);
1495 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1497 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"CONFIG"))
1499 replyString << L"201 INFO CONFIG OK\r\n";
1501 boost::property_tree::write_xml(replyString, caspar::env::properties(), w);
1503 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"PATHS"))
1505 replyString << L"201 INFO PATHS OK\r\n";
1507 boost::property_tree::wptree info;
1508 info.add_child(L"paths", caspar::env::properties().get_child(L"configuration.paths"));
1509 info.add(L"paths.initial-path", boost::filesystem::initial_path().wstring() + L"\\");
1511 boost::property_tree::write_xml(replyString, info, w);
1513 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SYSTEM"))
1515 replyString << L"201 INFO SYSTEM OK\r\n";
1517 boost::property_tree::wptree info;
1519 info.add(L"system.name", caspar::system_product_name());
1520 info.add(L"system.os.description", caspar::os_description());
1521 info.add(L"system.cpu", caspar::cpu_info());
1523 repo_->fill_information(info);
1525 boost::property_tree::write_xml(replyString, info, w);
1527 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SERVER"))
1529 replyString << L"201 INFO SERVER OK\r\n";
1531 boost::property_tree::wptree info;
1534 for (auto& channel : channels())
1535 info.add_child(L"channels.channel", channel.channel->info())
1536 .add(L"index", ++index);
1538 boost::property_tree::write_xml(replyString, info, w);
1542 if(parameters().size() >= 1)
1544 replyString << L"201 INFO OK\r\n";
1545 boost::property_tree::wptree info;
1547 std::vector<std::wstring> split;
1548 boost::split(split, parameters()[0], boost::is_any_of("-"));
1550 int layer = std::numeric_limits<int>::min();
1551 int channel = boost::lexical_cast<int>(split[0]) - 1;
1553 if(split.size() > 1)
1554 layer = boost::lexical_cast<int>(split[1]);
1556 if(layer == std::numeric_limits<int>::min())
1558 info.add_child(L"channel", channels().at(channel).channel->info())
1559 .add(L"index", channel);
1563 if(parameters().size() >= 2)
1565 if(boost::iequals(parameters()[1], L"B"))
1566 info.add_child(L"producer", channels().at(channel).channel->stage().background(layer).get()->info());
1568 info.add_child(L"producer", channels().at(channel).channel->stage().foreground(layer).get()->info());
1572 info.add_child(L"layer", channels().at(channel).channel->stage().info(layer).get())
1573 .add(L"index", layer);
1576 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1580 // This is needed for backwards compatibility with old clients
1581 replyString << L"200 INFO OK\r\n";
1582 for(size_t n = 0; n < channels().size(); ++n)
1583 GenerateChannelInfo(n, channels()[n].channel, replyString);
1590 CASPAR_LOG_CURRENT_EXCEPTION();
1591 SetReplyString(L"403 INFO ERROR\r\n");
1595 replyString << L"\r\n";
1596 SetReplyString(replyString.str());
1600 bool ClsCommand::DoExecute()
1612 std::wstringstream replyString;
1613 replyString << L"200 CLS OK\r\n";
1614 replyString << ListMedia(repo_);
1615 replyString << L"\r\n";
1616 SetReplyString(boost::to_upper_copy(replyString.str()));
1620 CASPAR_LOG_CURRENT_EXCEPTION();
1621 SetReplyString(L"501 CLS FAILED\r\n");
1628 bool TlsCommand::DoExecute()
1632 std::wstringstream replyString;
1633 replyString << L"200 TLS OK\r\n";
1635 replyString << ListTemplates();
1636 replyString << L"\r\n";
1638 SetReplyString(replyString.str());
1642 CASPAR_LOG_CURRENT_EXCEPTION();
1643 SetReplyString(L"501 TLS FAILED\r\n");
1649 bool VersionCommand::DoExecute()
1651 std::wstring replyString = L"201 VERSION OK\r\n" + env::version() + L"\r\n";
1653 if (parameters().size() > 0 && !boost::iequals(parameters()[0], L"SERVER"))
1655 auto version = repo_->get_version(parameters().at(0));
1657 if (version.empty())
1658 replyString = L"403 VERSION ERROR\r\n";
1660 replyString = L"201 VERSION OK\r\n" + version + L"\r\n";
1663 SetReplyString(replyString);
1667 bool ByeCommand::DoExecute()
1669 client()->disconnect();
1673 bool SetCommand::DoExecute()
1677 std::wstring name = boost::to_upper_copy(parameters()[0]);
1678 std::wstring value = boost::to_upper_copy(parameters()[1]);
1682 auto format_desc = core::video_format_desc(value);
1683 if(format_desc.format != core::video_format::invalid)
1685 channel()->video_format_desc(format_desc);
1686 SetReplyString(L"202 SET MODE OK\r\n");
1689 SetReplyString(L"501 SET MODE FAILED\r\n");
1693 this->SetReplyString(L"403 SET ERROR\r\n");
1698 CASPAR_LOG_CURRENT_EXCEPTION();
1699 SetReplyString(L"501 SET FAILED\r\n");
1706 bool LockCommand::DoExecute()
1710 auto it = parameters().begin();
1712 std::shared_ptr<caspar::IO::lock_container> lock;
1715 int channel_index = boost::lexical_cast<int>(*it) - 1;
1716 lock = channels().at(channel_index).lock;
1718 catch(const boost::bad_lexical_cast&) {}
1721 SetReplyString(L"401 LOCK ERROR\r\n");
1728 if(it == parameters().end()) //too few parameters
1730 SetReplyString(L"402 LOCK ERROR\r\n");
1734 std::wstring command = boost::to_upper_copy(*it);
1735 if(command == L"ACQUIRE")
1738 if(it == parameters().end()) //too few parameters
1740 SetReplyString(L"402 LOCK ACQUIRE ERROR\r\n");
1743 std::wstring lock_phrase = (*it);
1745 //TODO: read options
1749 //just lock one channel
1750 if(!lock->try_lock(lock_phrase, client()))
1752 SetReplyString(L"503 LOCK ACQUIRE FAILED\r\n");
1758 //TODO: lock all channels
1759 CASPAR_THROW_EXCEPTION(not_implemented());
1761 SetReplyString(L"202 LOCK ACQUIRE OK\r\n");
1764 else if(command == L"RELEASE")
1768 lock->release_lock(client());
1772 //TODO: release all channels
1773 CASPAR_THROW_EXCEPTION(not_implemented());
1775 SetReplyString(L"202 LOCK RELEASE OK\r\n");
1777 else if(command == L"CLEAR")
1779 std::wstring override_phrase = env::properties().get(L"configuration.lock-clear-phrase", L"");
1780 std::wstring client_override_phrase;
1781 if(!override_phrase.empty())
1784 if(it == parameters().end())
1786 SetReplyString(L"402 LOCK CLEAR ERROR\r\n");
1789 client_override_phrase = (*it);
1794 //just clear one channel
1795 if(client_override_phrase != override_phrase)
1797 SetReplyString(L"503 LOCK CLEAR FAILED\r\n");
1801 lock->clear_locks();
1805 //TODO: clear all channels
1806 CASPAR_THROW_EXCEPTION(not_implemented());
1809 SetReplyString(L"202 LOCK CLEAR OK\r\n");
1813 SetReplyString(L"403 LOCK ERROR\r\n");
1817 catch(not_implemented&)
1819 SetReplyString(L"600 LOCK FAILED\r\n");
1824 CASPAR_LOG_CURRENT_EXCEPTION();
1825 SetReplyString(L"501 LOCK FAILED\r\n");
1832 bool ThumbnailCommand::DoExecute()
1834 std::wstring command = boost::to_upper_copy(parameters()[0]);
1836 if (command == L"RETRIEVE")
1837 return DoExecuteRetrieve();
1838 else if (command == L"LIST")
1839 return DoExecuteList();
1840 else if (command == L"GENERATE")
1841 return DoExecuteGenerate();
1842 else if (command == L"GENERATE_ALL")
1843 return DoExecuteGenerateAll();
1845 SetReplyString(L"403 THUMBNAIL ERROR\r\n");
1849 bool ThumbnailCommand::DoExecuteRetrieve()
1851 if(parameters().size() < 2)
1853 SetReplyString(L"402 THUMBNAIL RETRIEVE ERROR\r\n");
1857 std::wstring filename = env::thumbnails_folder();
1858 filename.append(parameters()[1]);
1859 filename.append(L".png");
1861 std::wstring file_contents = read_file_base64(boost::filesystem::wpath(filename));
1863 if (file_contents.empty())
1865 SetReplyString(L"404 THUMBNAIL RETRIEVE ERROR\r\n");
1869 std::wstringstream reply;
1871 reply << L"201 THUMBNAIL RETRIEVE OK\r\n";
1872 reply << file_contents;
1874 SetReplyString(reply.str());
1878 bool ThumbnailCommand::DoExecuteList()
1880 std::wstringstream replyString;
1881 replyString << L"200 THUMBNAIL LIST OK\r\n";
1883 for (boost::filesystem::recursive_directory_iterator itr(env::thumbnails_folder()), end; itr != end; ++itr)
1885 if(boost::filesystem::is_regular_file(itr->path()))
1887 if(!boost::iequals(itr->path().extension().wstring(), L".png"))
1890 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::thumbnails_folder().size()-1, itr->path().wstring().size()));
1892 auto str = relativePath.replace_extension(L"").generic_wstring();
1893 if(str[0] == '\\' || str[0] == '/')
1894 str = std::wstring(str.begin() + 1, str.end());
1896 auto mtime = boost::filesystem::last_write_time(itr->path());
1897 auto mtime_readable = boost::posix_time::to_iso_wstring(boost::posix_time::from_time_t(mtime));
1898 auto file_size = boost::filesystem::file_size(itr->path());
1900 replyString << L"\"" << str << L"\" " << mtime_readable << L" " << file_size << L"\r\n";
1904 replyString << L"\r\n";
1906 SetReplyString(boost::to_upper_copy(replyString.str()));
1910 bool ThumbnailCommand::DoExecuteGenerate()
1912 if (parameters().size() < 2)
1914 SetReplyString(L"402 THUMBNAIL GENERATE ERROR\r\n");
1920 thumb_gen_->generate(parameters()[1]);
1921 SetReplyString(L"202 THUMBNAIL GENERATE OK\r\n");
1926 SetReplyString(L"500 THUMBNAIL GENERATE ERROR\r\n");
1931 bool ThumbnailCommand::DoExecuteGenerateAll()
1935 thumb_gen_->generate_all();
1936 SetReplyString(L"202 THUMBNAIL GENERATE_ALL OK\r\n");
1941 SetReplyString(L"500 THUMBNAIL GENERATE_ALL ERROR\r\n");
1946 bool KillCommand::DoExecute()
1948 shutdown_server_now_->set_value(false); //false for not attempting to restart
1949 SetReplyString(L"202 KILL OK\r\n");
1953 bool RestartCommand::DoExecute()
1955 shutdown_server_now_->set_value(true); //true for attempting to restart
1956 SetReplyString(L"202 RESTART OK\r\n");
1961 }} //namespace caspar