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/diagnostics/call_context.h>
48 #include <core/diagnostics/osd_graph.h>
50 #include <modules/reroute/producer/reroute_producer.h>
51 #include <modules/bluefish/bluefish.h>
52 #include <modules/decklink/decklink.h>
53 #include <modules/ffmpeg/ffmpeg.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>
58 #include <modules/ffmpeg/producer/util/util.h>
59 #include <modules/image/image.h>
60 #include <modules/screen/screen.h>
61 #include <modules/reroute/producer/reroute_producer.h>
71 #include <boost/date_time/posix_time/posix_time.hpp>
72 #include <boost/lexical_cast.hpp>
73 #include <boost/algorithm/string.hpp>
74 #include <boost/filesystem.hpp>
75 #include <boost/filesystem/fstream.hpp>
76 #include <boost/regex.hpp>
77 #include <boost/property_tree/xml_parser.hpp>
78 #include <boost/locale.hpp>
79 #include <boost/range/adaptor/transformed.hpp>
80 #include <boost/range/algorithm/copy.hpp>
81 #include <boost/archive/iterators/base64_from_binary.hpp>
82 #include <boost/archive/iterators/insert_linebreaks.hpp>
83 #include <boost/archive/iterators/transform_width.hpp>
85 #include <tbb/concurrent_unordered_map.h>
89 102 [action] Information that [action] has happened
90 101 [action] Information that [action] has happened plus one row of data
92 202 [command] OK [command] has been executed
93 201 [command] OK [command] has been executed, plus one row of data
94 200 [command] OK [command] has been executed, plus multiple lines of data. ends with an empty line
96 400 ERROR the command could not be understood
97 401 [command] ERROR invalid/missing channel
98 402 [command] ERROR parameter missing
99 403 [command] ERROR invalid parameter
100 404 [command] ERROR file not found
102 500 FAILED internal error
103 501 [command] FAILED internal error
104 502 [command] FAILED could not read file
105 503 [command] FAILED access denied
107 600 [command] FAILED [command] not implemented
110 namespace caspar { namespace protocol {
112 using namespace core;
114 std::wstring read_file_base64(const boost::filesystem::wpath& file)
116 using namespace boost::archive::iterators;
118 boost::filesystem::ifstream filestream(file, std::ios::binary);
123 auto length = boost::filesystem::file_size(file);
124 std::vector<char> bytes;
125 bytes.resize(length);
126 filestream.read(bytes.data(), length);
128 std::string result(to_base64(bytes.data(), length));
129 return std::wstring(result.begin(), result.end());
132 std::wstring read_utf8_file(const boost::filesystem::wpath& file)
134 std::wstringstream result;
135 boost::filesystem::wifstream filestream(file);
142 result << filestream.rdbuf();
148 std::wstring read_latin1_file(const boost::filesystem::wpath& file)
150 boost::locale::generator gen;
151 gen.locale_cache_enabled(true);
152 gen.categories(boost::locale::codepage_facet);
154 std::stringstream result_stream;
155 boost::filesystem::ifstream filestream(file);
156 filestream.imbue(gen("en_US.ISO8859-1"));
161 result_stream << filestream.rdbuf();
164 std::string result = result_stream.str();
165 std::wstring widened_result;
167 // The first 255 codepoints in unicode is the same as in latin1
169 result | boost::adaptors::transformed(
170 [](char c) { return static_cast<unsigned char>(c); }),
171 std::back_inserter(widened_result));
173 return widened_result;
176 std::wstring read_file(const boost::filesystem::wpath& file)
178 static const uint8_t BOM[] = {0xef, 0xbb, 0xbf};
180 if (!boost::filesystem::exists(file))
185 if (boost::filesystem::file_size(file) >= 3)
187 boost::filesystem::ifstream bom_stream(file);
190 bom_stream.read(header, 3);
193 if (std::memcmp(BOM, header, 3) == 0)
194 return read_utf8_file(file);
197 return read_latin1_file(file);
200 std::wstring MediaInfo(const boost::filesystem::path& path)
202 if(boost::filesystem::is_regular_file(path))
204 std::wstring clipttype = TEXT("N/A");
205 std::wstring extension = boost::to_upper_copy(path.extension().wstring());
206 if(extension == TEXT(".TGA") || extension == TEXT(".COL") || extension == L".PNG" || extension == L".JPEG" || extension == L".JPG" ||
207 extension == L"GIF" || extension == L"BMP")
208 clipttype = TEXT("STILL");
209 else if(extension == TEXT(".WAV") || extension == TEXT(".MP3"))
210 clipttype = TEXT("AUDIO");
211 else if(extension == TEXT("SWF") || extension == TEXT("CT") || extension == TEXT("DV") || extension == TEXT("MOV") || extension == TEXT("MPG") || extension == TEXT("AVI") || caspar::ffmpeg::is_valid_file(path.wstring()))
212 clipttype = TEXT("MOVIE");
214 if(clipttype != TEXT("N/A"))
216 auto is_not_digit = [](char c){ return std::isdigit(c) == 0; };
218 auto relativePath = boost::filesystem::path(path.wstring().substr(env::media_folder().size()-1, path.wstring().size()));
220 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(path)));
221 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), is_not_digit), writeTimeStr.end());
222 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
224 auto sizeStr = boost::lexical_cast<std::wstring>(boost::filesystem::file_size(path));
225 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), is_not_digit), sizeStr.end());
226 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
228 auto str = relativePath.replace_extension(TEXT("")).native();
229 while(str.size() > 0 && (str[0] == '\\' || str[0] == '/'))
230 str = std::wstring(str.begin() + 1, str.end());
232 return std::wstring() + TEXT("\"") + str +
233 + TEXT("\" ") + clipttype +
234 + TEXT(" ") + sizeStr +
235 + TEXT(" ") + writeTimeWStr +
242 std::wstring ListMedia()
244 std::wstringstream replyString;
245 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end; ++itr)
246 replyString << MediaInfo(itr->path());
248 return boost::to_upper_copy(replyString.str());
251 std::wstring ListTemplates()
253 std::wstringstream replyString;
255 for (boost::filesystem::recursive_directory_iterator itr(env::template_folder()), end; itr != end; ++itr)
257 if(boost::filesystem::is_regular_file(itr->path()) && (itr->path().extension() == L".ft" || itr->path().extension() == L".ct"))
259 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::template_folder().size()-1, itr->path().wstring().size()));
261 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(itr->path())));
262 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), [](char c){ return std::isdigit(c) == 0;}), writeTimeStr.end());
263 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
265 auto sizeStr = boost::lexical_cast<std::string>(boost::filesystem::file_size(itr->path()));
266 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), [](char c){ return std::isdigit(c) == 0;}), sizeStr.end());
268 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
270 std::wstring dir = relativePath.parent_path().native();
271 std::wstring file = boost::to_upper_copy(relativePath.filename().wstring());
272 relativePath = boost::filesystem::wpath(dir + L"/" + file);
274 auto str = relativePath.replace_extension(TEXT("")).native();
275 boost::trim_if(str, boost::is_any_of("\\/"));
277 replyString << TEXT("\"") << str
278 << TEXT("\" ") << sizeWStr
279 << TEXT(" ") << writeTimeWStr
283 return replyString.str();
288 void AMCPCommand::SendReply()
290 if(replyString_.empty())
293 client_->send(std::move(replyString_));
296 bool DiagnosticsCommand::DoExecute()
300 core::diagnostics::osd::show_graphs(true);
302 SetReplyString(TEXT("202 DIAG OK\r\n"));
308 CASPAR_LOG_CURRENT_EXCEPTION();
309 SetReplyString(TEXT("502 DIAG FAILED\r\n"));
314 bool ChannelGridCommand::DoExecute()
317 auto self = channels().back().channel;
319 core::diagnostics::scoped_call_context save;
320 core::diagnostics::call_context::for_thread().video_channel = channels().size();
322 std::vector<std::wstring> params;
323 params.push_back(L"SCREEN");
324 params.push_back(L"0");
325 params.push_back(L"NAME");
326 params.push_back(L"Channel Grid Window");
327 auto screen = create_consumer(params);
329 self->output().add(screen);
331 for (auto& channel : channels())
333 if(channel.channel != self)
335 core::diagnostics::call_context::for_thread().layer = index;
336 auto producer = reroute::create_producer(*channel.channel);
337 self->stage().load(index, producer, false);
338 self->stage().play(index);
343 int n = channels().size()-1;
344 double delta = 1.0/static_cast<double>(n);
345 for(int x = 0; x < n; ++x)
347 for(int y = 0; y < n; ++y)
350 auto transform = [=](frame_transform transform) -> frame_transform
352 transform.image_transform.fill_translation[0] = x*delta;
353 transform.image_transform.fill_translation[1] = y*delta;
354 transform.image_transform.fill_scale[0] = delta;
355 transform.image_transform.fill_scale[1] = delta;
356 transform.image_transform.clip_translation[0] = x*delta;
357 transform.image_transform.clip_translation[1] = y*delta;
358 transform.image_transform.clip_scale[0] = delta;
359 transform.image_transform.clip_scale[1] = delta;
362 self->stage().apply_transform(index, transform);
369 bool CallCommand::DoExecute()
371 //Perform loading of the clip
374 auto result = channel()->stage().call(layer_index(), parameters());
376 // TODO: because of std::async deferred timed waiting does not work
378 /*auto wait_res = result.wait_for(std::chrono::seconds(2));
379 if (wait_res == std::future_status::timeout)
380 CASPAR_THROW_EXCEPTION(timed_out());*/
382 std::wstringstream replyString;
383 if(result.get().empty())
384 replyString << TEXT("202 CALL OK\r\n");
386 replyString << TEXT("201 CALL OK\r\n") << result.get() << L"\r\n";
388 SetReplyString(replyString.str());
394 CASPAR_LOG_CURRENT_EXCEPTION();
395 SetReplyString(TEXT("502 CALL FAILED\r\n"));
400 tbb::concurrent_unordered_map<int, std::vector<stage::transform_tuple_t>> deferred_transforms;
402 bool MixerCommand::DoExecute()
404 //Perform loading of the clip
407 bool defer = boost::iequals(parameters().back(), L"DEFER");
409 parameters().pop_back();
411 std::vector<stage::transform_tuple_t> transforms;
413 if(boost::iequals(parameters()[0], L"KEYER") || boost::iequals(parameters()[0], L"IS_KEY"))
415 bool value = boost::lexical_cast<int>(parameters().at(1));
416 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
418 transform.image_transform.is_key = value;
422 else if(boost::iequals(parameters()[0], L"OPACITY"))
424 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
425 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
427 double value = boost::lexical_cast<double>(parameters().at(1));
429 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
431 transform.image_transform.opacity = value;
433 }, duration, tween));
435 else if(boost::iequals(parameters()[0], L"FILL") || boost::iequals(parameters()[0], L"FILL_RECT"))
437 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
438 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
439 double x = boost::lexical_cast<double>(parameters().at(1));
440 double y = boost::lexical_cast<double>(parameters().at(2));
441 double x_s = boost::lexical_cast<double>(parameters().at(3));
442 double y_s = boost::lexical_cast<double>(parameters().at(4));
444 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) mutable -> frame_transform
446 transform.image_transform.fill_translation[0] = x;
447 transform.image_transform.fill_translation[1] = y;
448 transform.image_transform.fill_scale[0] = x_s;
449 transform.image_transform.fill_scale[1] = y_s;
451 }, duration, tween));
453 else if(boost::iequals(parameters()[0], L"CLIP") || boost::iequals(parameters()[0], L"CLIP_RECT"))
455 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
456 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
457 double x = boost::lexical_cast<double>(parameters().at(1));
458 double y = boost::lexical_cast<double>(parameters().at(2));
459 double x_s = boost::lexical_cast<double>(parameters().at(3));
460 double y_s = boost::lexical_cast<double>(parameters().at(4));
462 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
464 transform.image_transform.clip_translation[0] = x;
465 transform.image_transform.clip_translation[1] = y;
466 transform.image_transform.clip_scale[0] = x_s;
467 transform.image_transform.clip_scale[1] = y_s;
469 }, duration, tween));
471 else if(boost::iequals(parameters()[0], L"GRID"))
473 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
474 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
475 int n = boost::lexical_cast<int>(parameters().at(1));
476 double delta = 1.0/static_cast<double>(n);
477 for(int x = 0; x < n; ++x)
479 for(int y = 0; y < n; ++y)
482 transforms.push_back(stage::transform_tuple_t(index, [=](frame_transform transform) -> frame_transform
484 transform.image_transform.fill_translation[0] = x*delta;
485 transform.image_transform.fill_translation[1] = y*delta;
486 transform.image_transform.fill_scale[0] = delta;
487 transform.image_transform.fill_scale[1] = delta;
488 transform.image_transform.clip_translation[0] = x*delta;
489 transform.image_transform.clip_translation[1] = y*delta;
490 transform.image_transform.clip_scale[0] = delta;
491 transform.image_transform.clip_scale[1] = delta;
493 }, duration, tween));
497 else if(boost::iequals(parameters()[0], L"BLEND"))
499 auto blend_str = parameters().at(1);
500 int layer = layer_index();
501 channel()->mixer().set_blend_mode(layer, get_blend_mode(blend_str));
503 else if(boost::iequals(parameters()[0], L"MASTERVOLUME"))
505 float master_volume = boost::lexical_cast<float>(parameters().at(1));
506 channel()->mixer().set_master_volume(master_volume);
508 else if(boost::iequals(parameters()[0], L"BRIGHTNESS"))
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.brightness = value;
517 }, duration, tween));
519 else if(boost::iequals(parameters()[0], L"SATURATION"))
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.saturation = value;
528 }, duration, tween));
530 else if(parameters()[0] == L"CONTRAST")
532 auto value = boost::lexical_cast<double>(parameters().at(1));
533 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
534 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
535 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
537 transform.image_transform.contrast = value;
539 }, duration, tween));
541 else if(boost::iequals(parameters()[0], L"LEVELS"))
544 value.min_input = boost::lexical_cast<double>(parameters().at(1));
545 value.max_input = boost::lexical_cast<double>(parameters().at(2));
546 value.gamma = boost::lexical_cast<double>(parameters().at(3));
547 value.min_output = boost::lexical_cast<double>(parameters().at(4));
548 value.max_output = boost::lexical_cast<double>(parameters().at(5));
549 int duration = parameters().size() > 6 ? boost::lexical_cast<int>(parameters()[6]) : 0;
550 std::wstring tween = parameters().size() > 7 ? parameters()[7] : L"linear";
552 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
554 transform.image_transform.levels = value;
556 }, duration, tween));
558 else if(boost::iequals(parameters()[0], L"VOLUME"))
560 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
561 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
562 double value = boost::lexical_cast<double>(parameters()[1]);
564 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
566 transform.audio_transform.volume = value;
568 }, duration, tween));
570 else if(boost::iequals(parameters()[0], L"CLEAR"))
572 int layer = layer_index(std::numeric_limits<int>::max());
574 if (layer == std::numeric_limits<int>::max())
576 channel()->stage().clear_transforms();
577 channel()->mixer().clear_blend_modes();
581 channel()->stage().clear_transforms(layer);
582 channel()->mixer().clear_blend_mode(layer);
585 else if(boost::iequals(parameters()[0], L"COMMIT"))
587 transforms = std::move(deferred_transforms[channel_index()]);
591 SetReplyString(TEXT("404 MIXER ERROR\r\n"));
597 auto& defer_tranforms = deferred_transforms[channel_index()];
598 defer_tranforms.insert(defer_tranforms.end(), transforms.begin(), transforms.end());
601 channel()->stage().apply_transforms(transforms);
603 SetReplyString(TEXT("202 MIXER OK\r\n"));
607 catch(file_not_found&)
609 CASPAR_LOG_CURRENT_EXCEPTION();
610 SetReplyString(TEXT("404 MIXER ERROR\r\n"));
615 CASPAR_LOG_CURRENT_EXCEPTION();
616 SetReplyString(TEXT("502 MIXER FAILED\r\n"));
621 bool SwapCommand::DoExecute()
623 //Perform loading of the clip
626 if(layer_index(-1) != -1)
628 std::vector<std::string> strs;
629 boost::split(strs, parameters()[0], boost::is_any_of("-"));
631 auto ch1 = channel();
632 auto ch2 = channels().at(boost::lexical_cast<int>(strs.at(0))-1);
634 int l1 = layer_index();
635 int l2 = boost::lexical_cast<int>(strs.at(1));
637 ch1->stage().swap_layer(l1, l2, ch2.channel->stage());
641 auto ch1 = channel();
642 auto ch2 = channels().at(boost::lexical_cast<int>(parameters()[0])-1);
643 ch1->stage().swap_layers(ch2.channel->stage());
646 SetReplyString(TEXT("202 SWAP OK\r\n"));
650 catch(file_not_found&)
652 CASPAR_LOG_CURRENT_EXCEPTION();
653 SetReplyString(TEXT("404 SWAP ERROR\r\n"));
658 CASPAR_LOG_CURRENT_EXCEPTION();
659 SetReplyString(TEXT("502 SWAP FAILED\r\n"));
664 bool AddCommand::DoExecute()
666 //Perform loading of the clip
669 //create_consumer still expects all parameters to be uppercase
670 for (auto& str : parameters())
672 boost::to_upper(str);
675 core::diagnostics::scoped_call_context save;
676 core::diagnostics::call_context::for_thread().video_channel = channel_index() + 1;
678 auto consumer = create_consumer(parameters());
679 channel()->output().add(layer_index(consumer->index()), consumer);
681 SetReplyString(TEXT("202 ADD OK\r\n"));
685 catch(file_not_found&)
687 CASPAR_LOG_CURRENT_EXCEPTION();
688 SetReplyString(TEXT("404 ADD ERROR\r\n"));
693 CASPAR_LOG_CURRENT_EXCEPTION();
694 SetReplyString(TEXT("502 ADD FAILED\r\n"));
699 bool RemoveCommand::DoExecute()
701 //Perform loading of the clip
704 auto index = layer_index(std::numeric_limits<int>::min());
705 if(index == std::numeric_limits<int>::min())
707 //create_consumer still expects all parameters to be uppercase
708 for (auto& str : parameters())
710 boost::to_upper(str);
713 index = create_consumer(parameters())->index();
716 channel()->output().remove(index);
718 SetReplyString(TEXT("202 REMOVE OK\r\n"));
722 catch(file_not_found&)
724 CASPAR_LOG_CURRENT_EXCEPTION();
725 SetReplyString(TEXT("404 REMOVE ERROR\r\n"));
730 CASPAR_LOG_CURRENT_EXCEPTION();
731 SetReplyString(TEXT("502 REMOVE FAILED\r\n"));
736 bool LoadCommand::DoExecute()
738 //Perform loading of the clip
741 core::diagnostics::scoped_call_context save;
742 core::diagnostics::call_context::for_thread().video_channel = channel_index() + 1;
743 core::diagnostics::call_context::for_thread().layer = layer_index();
744 auto pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
745 channel()->stage().load(layer_index(), pFP, true);
747 SetReplyString(TEXT("202 LOAD OK\r\n"));
751 catch(file_not_found&)
753 CASPAR_LOG_CURRENT_EXCEPTION();
754 SetReplyString(TEXT("404 LOAD ERROR\r\n"));
759 CASPAR_LOG_CURRENT_EXCEPTION();
760 SetReplyString(TEXT("502 LOAD FAILED\r\n"));
767 //std::function<std::wstring()> channel_cg_add_command::parse(const std::wstring& message, const std::vector<renderer::render_device_ptr>& channels)
769 // 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>.*)?");
771 // boost::wsmatch what;
772 // if(!boost::regex_match(message, what, expr))
775 // auto info = channel_info::parse(what, channels);
777 // int flash_layer_index = boost::lexical_cast<int>(what["FLASH_LAYER"].str());
779 // std::wstring templatename = what["TEMPLATE"].str();
780 // bool play_on_load = what["PLAY_ON_LOAD"].matched ? what["PLAY_ON_LOAD"].str() != L"0" : 0;
781 // std::wstring start_label = what["START_LABEL"].str();
782 // std::wstring data = get_data(what["DATA"].str());
784 // boost::replace_all(templatename, "\"", "");
786 // return [=]() -> std::wstring
788 // std::wstring fullFilename = flash::flash_producer::find_template(server::template_folder() + templatename);
789 // if(fullFilename.empty())
790 // CASPAR_THROW_EXCEPTION(file_not_found());
792 // std::wstring extension = boost::filesystem::wpath(fullFilename).extension();
793 // std::wstring filename = templatename;
794 // filename.append(extension);
796 // flash::flash::create_cg_proxy(info.video_channel, std::max<int>(DEFAULT_CHANNEL_LAYER+1, info.layer_index))
797 // ->add(flash_layer_index, filename, play_on_load, start_label, data);
799 // CASPAR_LOG(info) << L"Executed [amcp_channel_cg_add]";
803 bool LoadbgCommand::DoExecute()
805 transition_info transitionInfo;
809 std::wstring message;
810 for(size_t n = 0; n < parameters().size(); ++n)
811 message += boost::to_upper_copy(parameters()[n]) + L" ";
813 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)?.*)");
815 if(boost::regex_match(message, what, expr))
817 auto transition = what["TRANSITION"].str();
818 transitionInfo.duration = boost::lexical_cast<size_t>(what["DURATION"].str());
819 auto direction = what["DIRECTION"].matched ? what["DIRECTION"].str() : L"";
820 auto tween = what["TWEEN"].matched ? what["TWEEN"].str() : L"";
821 transitionInfo.tweener = tween;
823 if(transition == TEXT("CUT"))
824 transitionInfo.type = transition_type::cut;
825 else if(transition == TEXT("MIX"))
826 transitionInfo.type = transition_type::mix;
827 else if(transition == TEXT("PUSH"))
828 transitionInfo.type = transition_type::push;
829 else if(transition == TEXT("SLIDE"))
830 transitionInfo.type = transition_type::slide;
831 else if(transition == TEXT("WIPE"))
832 transitionInfo.type = transition_type::wipe;
834 if(direction == TEXT("FROMLEFT"))
835 transitionInfo.direction = transition_direction::from_left;
836 else if(direction == TEXT("FROMRIGHT"))
837 transitionInfo.direction = transition_direction::from_right;
838 else if(direction == TEXT("LEFT"))
839 transitionInfo.direction = transition_direction::from_right;
840 else if(direction == TEXT("RIGHT"))
841 transitionInfo.direction = transition_direction::from_left;
844 //Perform loading of the clip
847 std::shared_ptr<core::frame_producer> pFP;
849 static boost::wregex expr(LR"(\[(?<CHANNEL>\d+)\])", boost::regex::icase);
851 core::diagnostics::scoped_call_context save;
852 core::diagnostics::call_context::for_thread().video_channel = channel_index() + 1;
853 core::diagnostics::call_context::for_thread().layer = layer_index();
856 if(boost::regex_match(parameters().at(0), what, expr))
858 auto channel_index = boost::lexical_cast<int>(what["CHANNEL"].str());
859 pFP = reroute::create_producer(*channels().at(channel_index-1).channel);
863 pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
866 if(pFP == frame_producer::empty())
867 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info(parameters().size() > 0 ? parameters()[0] : L""));
869 bool auto_play = contains_param(L"AUTO", parameters());
871 auto pFP2 = create_transition_producer(channel()->video_format_desc().field_mode, spl::make_shared_ptr(pFP), transitionInfo);
873 channel()->stage().load(layer_index(), pFP2, false, transitionInfo.duration); // TODO: LOOP
875 channel()->stage().load(layer_index(), pFP2, false); // TODO: LOOP
878 SetReplyString(TEXT("202 LOADBG OK\r\n"));
882 catch(file_not_found&)
884 CASPAR_LOG(error) << L"File not found. No match found for parameters. Check syntax.";
885 SetReplyString(TEXT("404 LOADBG ERROR\r\n"));
890 CASPAR_LOG_CURRENT_EXCEPTION();
891 SetReplyString(TEXT("502 LOADBG FAILED\r\n"));
896 bool PauseCommand::DoExecute()
900 channel()->stage().pause(layer_index());
901 SetReplyString(TEXT("202 PAUSE OK\r\n"));
906 SetReplyString(TEXT("501 PAUSE FAILED\r\n"));
912 bool PlayCommand::DoExecute()
916 if(!parameters().empty())
918 LoadbgCommand lbg(*this);
921 throw std::exception();
924 channel()->stage().play(layer_index());
926 SetReplyString(TEXT("202 PLAY OK\r\n"));
931 SetReplyString(TEXT("501 PLAY FAILED\r\n"));
937 bool StopCommand::DoExecute()
941 channel()->stage().stop(layer_index());
942 SetReplyString(TEXT("202 STOP OK\r\n"));
947 SetReplyString(TEXT("501 STOP FAILED\r\n"));
953 bool ClearCommand::DoExecute()
955 int index = layer_index(std::numeric_limits<int>::min());
956 if(index != std::numeric_limits<int>::min())
957 channel()->stage().clear(index);
959 channel()->stage().clear();
961 SetReplyString(TEXT("202 CLEAR OK\r\n"));
966 bool PrintCommand::DoExecute()
968 channel()->output().add(create_consumer({ L"IMAGE" }));
970 SetReplyString(TEXT("202 PRINT OK\r\n"));
975 bool LogCommand::DoExecute()
977 if(boost::iequals(parameters().at(0), L"LEVEL"))
978 log::set_log_level(parameters().at(1));
980 SetReplyString(TEXT("202 LOG OK\r\n"));
985 bool CGCommand::DoExecute()
989 std::wstring command = boost::to_upper_copy(parameters()[0]);
990 if(command == TEXT("ADD"))
991 return DoExecuteAdd();
992 else if(command == TEXT("PLAY"))
993 return DoExecutePlay();
994 else if(command == TEXT("STOP"))
995 return DoExecuteStop();
996 else if(command == TEXT("NEXT"))
997 return DoExecuteNext();
998 else if(command == TEXT("REMOVE"))
999 return DoExecuteRemove();
1000 else if(command == TEXT("CLEAR"))
1001 return DoExecuteClear();
1002 else if(command == TEXT("UPDATE"))
1003 return DoExecuteUpdate();
1004 else if(command == TEXT("INVOKE"))
1005 return DoExecuteInvoke();
1006 else if(command == TEXT("INFO"))
1007 return DoExecuteInfo();
1011 CASPAR_LOG_CURRENT_EXCEPTION();
1014 SetReplyString(TEXT("403 CG ERROR\r\n"));
1018 bool CGCommand::ValidateLayer(const std::wstring& layerstring) {
1019 int length = layerstring.length();
1020 for(int i = 0; i < length; ++i) {
1021 if(!_istdigit(layerstring[i])) {
1029 bool CGCommand::DoExecuteAdd() {
1030 //CG 1 ADD 0 "template_folder/templatename" [STARTLABEL] 0/1 [DATA]
1032 int layer = 0; //_parameters[1]
1033 // std::wstring templateName; //_parameters[2]
1034 std::wstring label; //_parameters[3]
1035 bool bDoStart = false; //_parameters[3] alt. _parameters[4]
1036 // std::wstring data; //_parameters[4] alt. _parameters[5]
1038 if(parameters().size() < 4)
1040 SetReplyString(TEXT("402 CG ERROR\r\n"));
1043 unsigned int dataIndex = 4;
1045 if(!ValidateLayer(parameters()[1]))
1047 SetReplyString(TEXT("403 CG ERROR\r\n"));
1051 layer = boost::lexical_cast<int>(parameters()[1]);
1053 if(parameters()[3].length() > 1)
1055 label = parameters()[3];
1058 if(parameters().size() > 4 && parameters()[4].length() > 0) //read play-on-load-flag
1059 bDoStart = (parameters()[4][0]==TEXT('1')) ? true : false;
1062 SetReplyString(TEXT("402 CG ERROR\r\n"));
1066 else if(parameters()[3].length() > 0) { //read play-on-load-flag
1067 bDoStart = (parameters()[3][0]==TEXT('1')) ? true : false;
1071 SetReplyString(TEXT("403 CG ERROR\r\n"));
1075 const TCHAR* pDataString = 0;
1076 std::wstring dataFromFile;
1077 if(parameters().size() > dataIndex)
1079 const std::wstring& dataString = parameters()[dataIndex];
1081 if(dataString[0] == TEXT('<')) //the data is an XML-string
1082 pDataString = dataString.c_str();
1085 //The data is not an XML-string, it must be a filename
1086 std::wstring filename = env::data_folder();
1087 filename.append(dataString);
1088 filename.append(TEXT(".ftd"));
1090 dataFromFile = read_file(boost::filesystem::wpath(filename));
1091 pDataString = dataFromFile.c_str();
1095 std::wstring fullFilename = flash::find_template(env::template_folder() + parameters()[2]);
1096 if(!fullFilename.empty())
1098 std::wstring extension = boost::filesystem::path(fullFilename).extension().wstring();
1099 std::wstring filename = parameters()[2];
1100 filename.append(extension);
1102 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 : TEXT(""));
1103 SetReplyString(TEXT("202 CG OK\r\n"));
1107 CASPAR_LOG(warning) << "Could not find template " << parameters()[2];
1108 SetReplyString(TEXT("404 CG ERROR\r\n"));
1113 bool CGCommand::DoExecutePlay()
1115 if(parameters().size() > 1)
1117 if(!ValidateLayer(parameters()[1]))
1119 SetReplyString(TEXT("403 CG ERROR\r\n"));
1122 int layer = boost::lexical_cast<int>(parameters()[1]);
1123 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).play(layer);
1127 SetReplyString(TEXT("402 CG ERROR\r\n"));
1131 SetReplyString(TEXT("202 CG OK\r\n"));
1135 bool CGCommand::DoExecuteStop()
1137 if(parameters().size() > 1)
1139 if(!ValidateLayer(parameters()[1]))
1141 SetReplyString(TEXT("403 CG ERROR\r\n"));
1144 int layer = boost::lexical_cast<int>(parameters()[1]);
1145 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).stop(layer, 0);
1149 SetReplyString(TEXT("402 CG ERROR\r\n"));
1153 SetReplyString(TEXT("202 CG OK\r\n"));
1157 bool CGCommand::DoExecuteNext()
1159 if(parameters().size() > 1)
1161 if(!ValidateLayer(parameters()[1]))
1163 SetReplyString(TEXT("403 CG ERROR\r\n"));
1167 int layer = boost::lexical_cast<int>(parameters()[1]);
1168 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).next(layer);
1172 SetReplyString(TEXT("402 CG ERROR\r\n"));
1176 SetReplyString(TEXT("202 CG OK\r\n"));
1180 bool CGCommand::DoExecuteRemove()
1182 if(parameters().size() > 1)
1184 if(!ValidateLayer(parameters()[1]))
1186 SetReplyString(TEXT("403 CG ERROR\r\n"));
1190 int layer = boost::lexical_cast<int>(parameters()[1]);
1191 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).remove(layer);
1195 SetReplyString(TEXT("402 CG ERROR\r\n"));
1199 SetReplyString(TEXT("202 CG OK\r\n"));
1203 bool CGCommand::DoExecuteClear()
1205 channel()->stage().clear(layer_index(flash::cg_proxy::DEFAULT_LAYER));
1206 SetReplyString(TEXT("202 CG OK\r\n"));
1210 bool CGCommand::DoExecuteUpdate()
1214 if(!ValidateLayer(parameters().at(1)))
1216 SetReplyString(TEXT("403 CG ERROR\r\n"));
1220 std::wstring dataString = parameters().at(2);
1221 if(dataString.at(0) != TEXT('<'))
1223 //The data is not an XML-string, it must be a filename
1224 std::wstring filename = env::data_folder();
1225 filename.append(dataString);
1226 filename.append(TEXT(".ftd"));
1228 dataString = read_file(boost::filesystem::wpath(filename));
1231 int layer = boost::lexical_cast<int>(parameters()[1]);
1232 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).update(layer, dataString);
1236 SetReplyString(TEXT("402 CG ERROR\r\n"));
1240 SetReplyString(TEXT("202 CG OK\r\n"));
1244 bool CGCommand::DoExecuteInvoke()
1246 std::wstringstream replyString;
1247 replyString << TEXT("201 CG OK\r\n");
1249 if(parameters().size() > 2)
1251 if(!ValidateLayer(parameters()[1]))
1253 SetReplyString(TEXT("403 CG ERROR\r\n"));
1256 int layer = boost::lexical_cast<int>(parameters()[1]);
1257 auto result = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).invoke(layer, parameters()[2]);
1258 replyString << result << TEXT("\r\n");
1262 SetReplyString(TEXT("402 CG ERROR\r\n"));
1266 SetReplyString(replyString.str());
1270 bool CGCommand::DoExecuteInfo()
1272 std::wstringstream replyString;
1273 replyString << TEXT("201 CG OK\r\n");
1275 if(parameters().size() > 1)
1277 if(!ValidateLayer(parameters()[1]))
1279 SetReplyString(TEXT("403 CG ERROR\r\n"));
1283 int layer = boost::lexical_cast<int>(parameters()[1]);
1284 auto desc = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).description(layer);
1286 replyString << desc << TEXT("\r\n");
1290 auto info = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).template_host_info();
1291 replyString << info << TEXT("\r\n");
1294 SetReplyString(replyString.str());
1298 bool DataCommand::DoExecute()
1300 std::wstring command = boost::to_upper_copy(parameters()[0]);
1301 if(command == TEXT("STORE"))
1302 return DoExecuteStore();
1303 else if(command == TEXT("RETRIEVE"))
1304 return DoExecuteRetrieve();
1305 else if(command == TEXT("REMOVE"))
1306 return DoExecuteRemove();
1307 else if(command == TEXT("LIST"))
1308 return DoExecuteList();
1310 SetReplyString(TEXT("403 DATA ERROR\r\n"));
1314 bool DataCommand::DoExecuteStore()
1316 if(parameters().size() < 3)
1318 SetReplyString(TEXT("402 DATA STORE ERROR\r\n"));
1322 std::wstring filename = env::data_folder();
1323 filename.append(parameters()[1]);
1324 filename.append(TEXT(".ftd"));
1326 auto data_path = boost::filesystem::wpath(
1327 boost::filesystem::wpath(filename).parent_path());
1329 if(!boost::filesystem::exists(data_path))
1330 boost::filesystem::create_directories(data_path);
1332 std::wofstream datafile(filename.c_str());
1335 SetReplyString(TEXT("501 DATA STORE FAILED\r\n"));
1339 datafile << static_cast<wchar_t>(65279); // UTF-8 BOM character
1340 datafile << parameters()[2] << std::flush;
1343 std::wstring replyString = TEXT("202 DATA STORE OK\r\n");
1344 SetReplyString(replyString);
1348 bool DataCommand::DoExecuteRetrieve()
1350 if(parameters().size() < 2)
1352 SetReplyString(TEXT("402 DATA RETRIEVE ERROR\r\n"));
1356 std::wstring filename = env::data_folder();
1357 filename.append(parameters()[1]);
1358 filename.append(TEXT(".ftd"));
1360 std::wstring file_contents = read_file(boost::filesystem::wpath(filename));
1362 if (file_contents.empty())
1364 SetReplyString(TEXT("404 DATA RETRIEVE ERROR\r\n"));
1368 std::wstringstream reply(TEXT("201 DATA RETRIEVE OK\r\n"));
1370 std::wstringstream file_contents_stream(file_contents);
1373 bool firstLine = true;
1374 while(std::getline(file_contents_stream, line))
1385 SetReplyString(reply.str());
1389 bool DataCommand::DoExecuteRemove()
1391 if (parameters().size() < 2)
1393 SetReplyString(TEXT("402 DATA REMOVE ERROR\r\n"));
1397 std::wstring filename = env::data_folder();
1398 filename.append(parameters()[1]);
1399 filename.append(TEXT(".ftd"));
1401 if (!boost::filesystem::exists(filename))
1403 SetReplyString(TEXT("404 DATA REMOVE ERROR\r\n"));
1407 if (!boost::filesystem::remove(filename))
1409 SetReplyString(TEXT("403 DATA REMOVE ERROR\r\n"));
1413 SetReplyString(TEXT("201 DATA REMOVE OK\r\n"));
1418 bool DataCommand::DoExecuteList()
1420 std::wstringstream replyString;
1421 replyString << TEXT("200 DATA LIST OK\r\n");
1423 for (boost::filesystem::recursive_directory_iterator itr(env::data_folder()), end; itr != end; ++itr)
1425 if(boost::filesystem::is_regular_file(itr->path()))
1427 if(!boost::iequals(itr->path().extension().wstring(), L".ftd"))
1430 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::data_folder().size()-1, itr->path().wstring().size()));
1432 auto str = relativePath.replace_extension(TEXT("")).native();
1433 if(str[0] == '\\' || str[0] == '/')
1434 str = std::wstring(str.begin() + 1, str.end());
1436 replyString << str << TEXT("\r\n");
1440 replyString << TEXT("\r\n");
1442 SetReplyString(boost::to_upper_copy(replyString.str()));
1446 bool CinfCommand::DoExecute()
1448 std::wstringstream replyString;
1453 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end && info.empty(); ++itr)
1455 auto path = itr->path();
1456 auto file = path.replace_extension(L"").filename();
1457 if(boost::iequals(file.wstring(), parameters().at(0)))
1458 info += MediaInfo(itr->path()) + L"\r\n";
1463 SetReplyString(TEXT("404 CINF ERROR\r\n"));
1466 replyString << TEXT("200 CINF OK\r\n");
1467 replyString << info << "\r\n";
1471 CASPAR_LOG_CURRENT_EXCEPTION();
1472 SetReplyString(TEXT("404 CINF ERROR\r\n"));
1476 SetReplyString(replyString.str());
1480 void GenerateChannelInfo(int index, const spl::shared_ptr<core::video_channel>& pChannel, std::wstringstream& replyString)
1482 replyString << index+1 << TEXT(" ") << pChannel->video_format_desc().name << TEXT(" PLAYING") << TEXT("\r\n");
1485 bool InfoCommand::DoExecute()
1487 std::wstringstream replyString;
1489 boost::property_tree::xml_writer_settings<std::wstring> w(' ', 3);
1493 if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"TEMPLATE"))
1495 replyString << L"201 INFO TEMPLATE OK\r\n";
1497 // Needs to be extended for any file, not just flash.
1499 auto filename = flash::find_template(env::template_folder() + parameters().at(1));
1501 std::wstringstream str;
1502 str << u16(flash::read_template_meta_info(filename));
1503 boost::property_tree::wptree info;
1504 boost::property_tree::xml_parser::read_xml(str, info, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);
1506 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1508 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"CONFIG"))
1510 replyString << L"201 INFO CONFIG OK\r\n";
1512 boost::property_tree::write_xml(replyString, caspar::env::properties(), w);
1514 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"PATHS"))
1516 replyString << L"201 INFO PATHS OK\r\n";
1518 boost::property_tree::wptree info;
1519 info.add_child(L"paths", caspar::env::properties().get_child(L"configuration.paths"));
1520 info.add(L"paths.initial-path", boost::filesystem::initial_path().wstring() + L"\\");
1522 boost::property_tree::write_xml(replyString, info, w);
1524 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SYSTEM"))
1526 replyString << L"201 INFO SYSTEM OK\r\n";
1528 boost::property_tree::wptree info;
1530 info.add(L"system.name", caspar::system_product_name());
1531 info.add(L"system.os.description", caspar::os_description());
1532 info.add(L"system.cpu", caspar::cpu_info());
1534 for (auto& device : caspar::decklink::device_list())
1535 info.add(L"system.decklink.device", device);
1537 for (auto& device : caspar::bluefish::device_list())
1538 info.add(L"system.bluefish.device", device);
1540 info.add(L"system.flash", caspar::flash::version());
1541 //info.add(L"system.free-image", caspar::image::version());
1542 info.add(L"system.ffmpeg.avcodec", caspar::ffmpeg::avcodec_version());
1543 info.add(L"system.ffmpeg.avformat", caspar::ffmpeg::avformat_version());
1544 info.add(L"system.ffmpeg.avfilter", caspar::ffmpeg::avfilter_version());
1545 info.add(L"system.ffmpeg.avutil", caspar::ffmpeg::avutil_version());
1546 info.add(L"system.ffmpeg.swscale", caspar::ffmpeg::swscale_version());
1548 boost::property_tree::write_xml(replyString, info, w);
1550 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SERVER"))
1552 replyString << L"201 INFO SERVER OK\r\n";
1554 boost::property_tree::wptree info;
1557 for (auto& channel : channels())
1558 info.add_child(L"channels.channel", channel.channel->info())
1559 .add(L"index", ++index);
1561 boost::property_tree::write_xml(replyString, info, w);
1565 if(parameters().size() >= 1)
1567 replyString << TEXT("201 INFO OK\r\n");
1568 boost::property_tree::wptree info;
1570 std::vector<std::wstring> split;
1571 boost::split(split, parameters()[0], boost::is_any_of("-"));
1573 int layer = std::numeric_limits<int>::min();
1574 int channel = boost::lexical_cast<int>(split[0]) - 1;
1576 if(split.size() > 1)
1577 layer = boost::lexical_cast<int>(split[1]);
1579 if(layer == std::numeric_limits<int>::min())
1581 info.add_child(L"channel", channels().at(channel).channel->info())
1582 .add(L"index", channel);
1586 if(parameters().size() >= 2)
1588 if(boost::iequals(parameters()[1], L"B"))
1589 info.add_child(L"producer", channels().at(channel).channel->stage().background(layer).get()->info());
1591 info.add_child(L"producer", channels().at(channel).channel->stage().foreground(layer).get()->info());
1595 info.add_child(L"layer", channels().at(channel).channel->stage().info(layer).get())
1596 .add(L"index", layer);
1599 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1603 // This is needed for backwards compatibility with old clients
1604 replyString << TEXT("200 INFO OK\r\n");
1605 for(size_t n = 0; n < channels().size(); ++n)
1606 GenerateChannelInfo(n, channels()[n].channel, replyString);
1613 CASPAR_LOG_CURRENT_EXCEPTION();
1614 SetReplyString(TEXT("403 INFO ERROR\r\n"));
1618 replyString << TEXT("\r\n");
1619 SetReplyString(replyString.str());
1623 bool ClsCommand::DoExecute()
1635 std::wstringstream replyString;
1636 replyString << TEXT("200 CLS OK\r\n");
1637 replyString << ListMedia();
1638 replyString << TEXT("\r\n");
1639 SetReplyString(boost::to_upper_copy(replyString.str()));
1643 CASPAR_LOG_CURRENT_EXCEPTION();
1644 SetReplyString(TEXT("501 CLS FAILED\r\n"));
1651 bool TlsCommand::DoExecute()
1655 std::wstringstream replyString;
1656 replyString << TEXT("200 TLS OK\r\n");
1658 replyString << ListTemplates();
1659 replyString << TEXT("\r\n");
1661 SetReplyString(replyString.str());
1665 CASPAR_LOG_CURRENT_EXCEPTION();
1666 SetReplyString(TEXT("501 TLS FAILED\r\n"));
1672 bool VersionCommand::DoExecute()
1674 std::wstring replyString = TEXT("201 VERSION OK\r\n") + env::version() + TEXT("\r\n");
1676 if(parameters().size() > 0)
1678 if(boost::iequals(parameters()[0], L"FLASH"))
1679 replyString = TEXT("201 VERSION OK\r\n") + flash::version() + TEXT("\r\n");
1680 else if(boost::iequals(parameters()[0], L"TEMPLATEHOST"))
1681 replyString = TEXT("201 VERSION OK\r\n") + flash::cg_version() + TEXT("\r\n");
1682 else if(!boost::iequals(parameters()[0], L"SERVER"))
1683 replyString = TEXT("403 VERSION ERROR\r\n");
1686 SetReplyString(replyString);
1690 bool ByeCommand::DoExecute()
1692 client()->disconnect();
1696 bool SetCommand::DoExecute()
1700 std::wstring name = boost::to_upper_copy(parameters()[0]);
1701 std::wstring value = boost::to_upper_copy(parameters()[1]);
1703 if(name == TEXT("MODE"))
1705 auto format_desc = core::video_format_desc(value);
1706 if(format_desc.format != core::video_format::invalid)
1708 channel()->video_format_desc(format_desc);
1709 SetReplyString(TEXT("202 SET MODE OK\r\n"));
1712 SetReplyString(TEXT("501 SET MODE FAILED\r\n"));
1716 this->SetReplyString(TEXT("403 SET ERROR\r\n"));
1721 CASPAR_LOG_CURRENT_EXCEPTION();
1722 SetReplyString(TEXT("501 SET FAILED\r\n"));
1729 bool LockCommand::DoExecute()
1733 auto it = parameters().begin();
1735 std::shared_ptr<caspar::IO::lock_container> lock;
1738 int channel_index = boost::lexical_cast<int>(*it) - 1;
1739 lock = channels().at(channel_index).lock;
1741 catch(const boost::bad_lexical_cast&) {}
1744 SetReplyString(L"401 LOCK ERROR\r\n");
1751 if(it == parameters().end()) //too few parameters
1753 SetReplyString(L"402 LOCK ERROR\r\n");
1757 std::wstring command = boost::to_upper_copy(*it);
1758 if(command == L"ACQUIRE")
1761 if(it == parameters().end()) //too few parameters
1763 SetReplyString(L"402 LOCK ACQUIRE ERROR\r\n");
1766 std::wstring lock_phrase = (*it);
1768 //TODO: read options
1772 //just lock one channel
1773 if(!lock->try_lock(lock_phrase, client()))
1775 SetReplyString(L"503 LOCK ACQUIRE FAILED\r\n");
1781 //TODO: lock all channels
1782 CASPAR_THROW_EXCEPTION(not_implemented());
1784 SetReplyString(L"202 LOCK ACQUIRE OK\r\n");
1787 else if(command == L"RELEASE")
1791 lock->release_lock(client());
1795 //TODO: release all channels
1796 CASPAR_THROW_EXCEPTION(not_implemented());
1798 SetReplyString(L"202 LOCK RELEASE OK\r\n");
1800 else if(command == L"CLEAR")
1802 std::wstring override_phrase = env::properties().get(L"configuration.lock-clear-phrase", L"");
1803 std::wstring client_override_phrase;
1804 if(!override_phrase.empty())
1807 if(it == parameters().end())
1809 SetReplyString(L"402 LOCK CLEAR ERROR\r\n");
1812 client_override_phrase = (*it);
1817 //just clear one channel
1818 if(client_override_phrase != override_phrase)
1820 SetReplyString(L"503 LOCK CLEAR FAILED\r\n");
1824 lock->clear_locks();
1828 //TODO: clear all channels
1829 CASPAR_THROW_EXCEPTION(not_implemented());
1832 SetReplyString(L"202 LOCK CLEAR OK\r\n");
1836 SetReplyString(L"403 LOCK ERROR\r\n");
1840 catch(not_implemented&)
1842 SetReplyString(L"600 LOCK FAILED\r\n");
1847 CASPAR_LOG_CURRENT_EXCEPTION();
1848 SetReplyString(L"501 LOCK FAILED\r\n");
1855 bool ThumbnailCommand::DoExecute()
1857 std::wstring command = boost::to_upper_copy(parameters()[0]);
1859 if (command == TEXT("RETRIEVE"))
1860 return DoExecuteRetrieve();
1861 else if (command == TEXT("LIST"))
1862 return DoExecuteList();
1863 else if (command == TEXT("GENERATE"))
1864 return DoExecuteGenerate();
1865 else if (command == TEXT("GENERATE_ALL"))
1866 return DoExecuteGenerateAll();
1868 SetReplyString(TEXT("403 THUMBNAIL ERROR\r\n"));
1872 bool ThumbnailCommand::DoExecuteRetrieve()
1874 if(parameters().size() < 2)
1876 SetReplyString(TEXT("402 THUMBNAIL RETRIEVE ERROR\r\n"));
1880 std::wstring filename = env::thumbnails_folder();
1881 filename.append(parameters()[1]);
1882 filename.append(TEXT(".png"));
1884 std::wstring file_contents = read_file_base64(boost::filesystem::wpath(filename));
1886 if (file_contents.empty())
1888 SetReplyString(TEXT("404 THUMBNAIL RETRIEVE ERROR\r\n"));
1892 std::wstringstream reply;
1894 reply << L"201 THUMBNAIL RETRIEVE OK\r\n";
1895 reply << file_contents;
1897 SetReplyString(reply.str());
1901 bool ThumbnailCommand::DoExecuteList()
1903 std::wstringstream replyString;
1904 replyString << TEXT("200 THUMBNAIL LIST OK\r\n");
1906 for (boost::filesystem::recursive_directory_iterator itr(env::thumbnails_folder()), end; itr != end; ++itr)
1908 if(boost::filesystem::is_regular_file(itr->path()))
1910 if(!boost::iequals(itr->path().extension().wstring(), L".png"))
1913 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::thumbnails_folder().size()-1, itr->path().wstring().size()));
1915 auto str = relativePath.replace_extension(L"").native();
1916 if(str[0] == '\\' || str[0] == '/')
1917 str = std::wstring(str.begin() + 1, str.end());
1919 auto mtime = boost::filesystem::last_write_time(itr->path());
1920 auto mtime_readable = boost::posix_time::to_iso_wstring(boost::posix_time::from_time_t(mtime));
1921 auto file_size = boost::filesystem::file_size(itr->path());
1923 replyString << L"\"" << str << L"\" " << mtime_readable << L" " << file_size << L"\r\n";
1927 replyString << TEXT("\r\n");
1929 SetReplyString(boost::to_upper_copy(replyString.str()));
1933 bool ThumbnailCommand::DoExecuteGenerate()
1935 if (parameters().size() < 2)
1937 SetReplyString(L"402 THUMBNAIL GENERATE ERROR\r\n");
1943 thumb_gen_->generate(parameters()[1]);
1944 SetReplyString(L"202 THUMBNAIL GENERATE OK\r\n");
1949 SetReplyString(L"500 THUMBNAIL GENERATE ERROR\r\n");
1954 bool ThumbnailCommand::DoExecuteGenerateAll()
1958 thumb_gen_->generate_all();
1959 SetReplyString(L"202 THUMBNAIL GENERATE_ALL OK\r\n");
1964 SetReplyString(L"500 THUMBNAIL GENERATE_ALL ERROR\r\n");
1969 bool KillCommand::DoExecute()
1971 shutdown_server_now_->set_value(false); //false for not attempting to restart
1972 SetReplyString(TEXT("202 KILL OK\r\n"));
1976 bool RestartCommand::DoExecute()
1978 shutdown_server_now_->set_value(true); //true for attempting to restart
1979 SetReplyString(TEXT("202 RESTART OK\r\n"));
1984 }} //namespace caspar