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/diagnostics/graph.h>
36 #include <common/os/windows/current_version.h>
37 #include <common/os/windows/system_info.h>
38 #include <common/base64.h>
40 #include <core/producer/frame_producer.h>
41 #include <core/video_format.h>
42 #include <core/producer/transition/transition_producer.h>
43 #include <core/frame/frame_transform.h>
44 #include <core/producer/stage.h>
45 #include <core/producer/layer.h>
46 #include <core/mixer/mixer.h>
47 #include <core/consumer/output.h>
48 #include <core/thumbnail_generator.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
168 auto from_signed_to_signed = std::function<unsigned char(char)>(
169 [] (char c) { return static_cast<unsigned char>(c); }
172 result | boost::adaptors::transformed(from_signed_to_signed),
173 std::back_inserter(widened_result));
175 return widened_result;
178 std::wstring read_file(const boost::filesystem::wpath& file)
180 static const uint8_t BOM[] = {0xef, 0xbb, 0xbf};
182 if (!boost::filesystem::exists(file))
187 if (boost::filesystem::file_size(file) >= 3)
189 boost::filesystem::ifstream bom_stream(file);
192 bom_stream.read(header, 3);
195 if (std::memcmp(BOM, header, 3) == 0)
196 return read_utf8_file(file);
199 return read_latin1_file(file);
202 std::wstring MediaInfo(const boost::filesystem::path& path)
204 if(boost::filesystem::is_regular_file(path))
206 std::wstring clipttype = TEXT("N/A");
207 std::wstring extension = boost::to_upper_copy(path.extension().wstring());
208 if(extension == TEXT(".TGA") || extension == TEXT(".COL") || extension == L".PNG" || extension == L".JPEG" || extension == L".JPG" ||
209 extension == L"GIF" || extension == L"BMP")
210 clipttype = TEXT("STILL");
211 else if(extension == TEXT(".WAV") || extension == TEXT(".MP3"))
212 clipttype = TEXT("AUDIO");
213 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()))
214 clipttype = TEXT("MOVIE");
216 if(clipttype != TEXT("N/A"))
218 auto is_not_digit = [](char c){ return std::isdigit(c) == 0; };
220 auto relativePath = boost::filesystem::path(path.wstring().substr(env::media_folder().size()-1, path.wstring().size()));
222 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(path)));
223 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), is_not_digit), writeTimeStr.end());
224 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
226 auto sizeStr = boost::lexical_cast<std::wstring>(boost::filesystem::file_size(path));
227 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), is_not_digit), sizeStr.end());
228 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
230 auto str = relativePath.replace_extension(TEXT("")).native();
231 while(str.size() > 0 && (str[0] == '\\' || str[0] == '/'))
232 str = std::wstring(str.begin() + 1, str.end());
234 return std::wstring() + TEXT("\"") + str +
235 + TEXT("\" ") + clipttype +
236 + TEXT(" ") + sizeStr +
237 + TEXT(" ") + writeTimeWStr +
244 std::wstring ListMedia()
246 std::wstringstream replyString;
247 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end; ++itr)
248 replyString << MediaInfo(itr->path());
250 return boost::to_upper_copy(replyString.str());
253 std::wstring ListTemplates()
255 std::wstringstream replyString;
257 for (boost::filesystem::recursive_directory_iterator itr(env::template_folder()), end; itr != end; ++itr)
259 if(boost::filesystem::is_regular_file(itr->path()) && (itr->path().extension() == L".ft" || itr->path().extension() == L".ct"))
261 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::template_folder().size()-1, itr->path().wstring().size()));
263 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(itr->path())));
264 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), [](char c){ return std::isdigit(c) == 0;}), writeTimeStr.end());
265 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
267 auto sizeStr = boost::lexical_cast<std::string>(boost::filesystem::file_size(itr->path()));
268 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), [](char c){ return std::isdigit(c) == 0;}), sizeStr.end());
270 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
272 std::wstring dir = relativePath.parent_path().native();
273 std::wstring file = boost::to_upper_copy(relativePath.filename().wstring());
274 relativePath = boost::filesystem::wpath(dir + L"/" + file);
276 auto str = relativePath.replace_extension(TEXT("")).native();
277 boost::trim_if(str, boost::is_any_of("\\/"));
279 replyString << TEXT("\"") << str
280 << TEXT("\" ") << sizeWStr
281 << TEXT(" ") << writeTimeWStr
285 return replyString.str();
290 void AMCPCommand::SendReply()
292 if(replyString_.empty())
295 client_->send(std::move(replyString_));
298 bool DiagnosticsCommand::DoExecute()
302 diagnostics::show_graphs(true);
304 SetReplyString(TEXT("202 DIAG OK\r\n"));
310 CASPAR_LOG_CURRENT_EXCEPTION();
311 SetReplyString(TEXT("502 DIAG FAILED\r\n"));
316 bool ChannelGridCommand::DoExecute()
319 auto self = channels().back().channel;
321 std::vector<std::wstring> params;
322 params.push_back(L"SCREEN");
323 params.push_back(L"0");
324 params.push_back(L"NAME");
325 params.push_back(L"Channel Grid Window");
326 auto screen = create_consumer(params);
328 self->output().add(screen);
330 BOOST_FOREACH(auto channel, channels())
332 if(channel.channel != self)
334 auto producer = reroute::create_producer(*channel.channel);
335 self->stage().load(index, producer, false);
336 self->stage().play(index);
341 int n = channels().size()-1;
342 double delta = 1.0/static_cast<double>(n);
343 for(int x = 0; x < n; ++x)
345 for(int y = 0; y < n; ++y)
348 auto transform = [=](frame_transform transform) -> frame_transform
350 transform.image_transform.fill_translation[0] = x*delta;
351 transform.image_transform.fill_translation[1] = y*delta;
352 transform.image_transform.fill_scale[0] = delta;
353 transform.image_transform.fill_scale[1] = delta;
354 transform.image_transform.clip_translation[0] = x*delta;
355 transform.image_transform.clip_translation[1] = y*delta;
356 transform.image_transform.clip_scale[0] = delta;
357 transform.image_transform.clip_scale[1] = delta;
360 self->stage().apply_transform(index, transform);
367 bool CallCommand::DoExecute()
369 //Perform loading of the clip
372 auto result = channel()->stage().call(layer_index(), parameters());
374 // TODO: because of std::async deferred timed waiting does not work
376 /*auto wait_res = result.wait_for(std::chrono::seconds(2));
377 if (wait_res == std::future_status::timeout)
378 CASPAR_THROW_EXCEPTION(timed_out());*/
380 std::wstringstream replyString;
381 if(result.get().empty())
382 replyString << TEXT("202 CALL OK\r\n");
384 replyString << TEXT("201 CALL OK\r\n") << result.get() << L"\r\n";
386 SetReplyString(replyString.str());
392 CASPAR_LOG_CURRENT_EXCEPTION();
393 SetReplyString(TEXT("502 CALL FAILED\r\n"));
398 tbb::concurrent_unordered_map<int, std::vector<stage::transform_tuple_t>> deferred_transforms;
400 bool MixerCommand::DoExecute()
402 //Perform loading of the clip
405 bool defer = boost::iequals(parameters().back(), L"DEFER");
407 parameters().pop_back();
409 std::vector<stage::transform_tuple_t> transforms;
411 if(boost::iequals(parameters()[0], L"KEYER") || boost::iequals(parameters()[0], L"IS_KEY"))
413 bool value = boost::lexical_cast<int>(parameters().at(1));
414 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
416 transform.image_transform.is_key = value;
420 else if(boost::iequals(parameters()[0], L"OPACITY"))
422 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
423 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
425 double value = boost::lexical_cast<double>(parameters().at(1));
427 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
429 transform.image_transform.opacity = value;
431 }, duration, tween));
433 else if(boost::iequals(parameters()[0], L"FILL") || boost::iequals(parameters()[0], L"FILL_RECT"))
435 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
436 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
437 double x = boost::lexical_cast<double>(parameters().at(1));
438 double y = boost::lexical_cast<double>(parameters().at(2));
439 double x_s = boost::lexical_cast<double>(parameters().at(3));
440 double y_s = boost::lexical_cast<double>(parameters().at(4));
442 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) mutable -> frame_transform
444 transform.image_transform.fill_translation[0] = x;
445 transform.image_transform.fill_translation[1] = y;
446 transform.image_transform.fill_scale[0] = x_s;
447 transform.image_transform.fill_scale[1] = y_s;
449 }, duration, tween));
451 else if(boost::iequals(parameters()[0], L"CLIP") || boost::iequals(parameters()[0], L"CLIP_RECT"))
453 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
454 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
455 double x = boost::lexical_cast<double>(parameters().at(1));
456 double y = boost::lexical_cast<double>(parameters().at(2));
457 double x_s = boost::lexical_cast<double>(parameters().at(3));
458 double y_s = boost::lexical_cast<double>(parameters().at(4));
460 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
462 transform.image_transform.clip_translation[0] = x;
463 transform.image_transform.clip_translation[1] = y;
464 transform.image_transform.clip_scale[0] = x_s;
465 transform.image_transform.clip_scale[1] = y_s;
467 }, duration, tween));
469 else if(boost::iequals(parameters()[0], L"GRID"))
471 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
472 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
473 int n = boost::lexical_cast<int>(parameters().at(1));
474 double delta = 1.0/static_cast<double>(n);
475 for(int x = 0; x < n; ++x)
477 for(int y = 0; y < n; ++y)
480 transforms.push_back(stage::transform_tuple_t(index, [=](frame_transform transform) -> frame_transform
482 transform.image_transform.fill_translation[0] = x*delta;
483 transform.image_transform.fill_translation[1] = y*delta;
484 transform.image_transform.fill_scale[0] = delta;
485 transform.image_transform.fill_scale[1] = delta;
486 transform.image_transform.clip_translation[0] = x*delta;
487 transform.image_transform.clip_translation[1] = y*delta;
488 transform.image_transform.clip_scale[0] = delta;
489 transform.image_transform.clip_scale[1] = delta;
491 }, duration, tween));
495 else if(boost::iequals(parameters()[0], L"BLEND"))
497 auto blend_str = parameters().at(1);
498 int layer = layer_index();
499 channel()->mixer().set_blend_mode(layer, get_blend_mode(blend_str));
501 else if(boost::iequals(parameters()[0], L"MASTERVOLUME"))
503 float master_volume = boost::lexical_cast<float>(parameters().at(1));
504 channel()->mixer().set_master_volume(master_volume);
506 else if(boost::iequals(parameters()[0], L"BRIGHTNESS"))
508 auto value = boost::lexical_cast<double>(parameters().at(1));
509 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
510 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
511 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
513 transform.image_transform.brightness = value;
515 }, duration, tween));
517 else if(boost::iequals(parameters()[0], L"SATURATION"))
519 auto value = boost::lexical_cast<double>(parameters().at(1));
520 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
521 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
522 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
524 transform.image_transform.saturation = value;
526 }, duration, tween));
528 else if(parameters()[0] == L"CONTRAST")
530 auto value = boost::lexical_cast<double>(parameters().at(1));
531 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
532 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
533 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
535 transform.image_transform.contrast = value;
537 }, duration, tween));
539 else if(boost::iequals(parameters()[0], L"LEVELS"))
542 value.min_input = boost::lexical_cast<double>(parameters().at(1));
543 value.max_input = boost::lexical_cast<double>(parameters().at(2));
544 value.gamma = boost::lexical_cast<double>(parameters().at(3));
545 value.min_output = boost::lexical_cast<double>(parameters().at(4));
546 value.max_output = boost::lexical_cast<double>(parameters().at(5));
547 int duration = parameters().size() > 6 ? boost::lexical_cast<int>(parameters()[6]) : 0;
548 std::wstring tween = parameters().size() > 7 ? parameters()[7] : L"linear";
550 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
552 transform.image_transform.levels = value;
554 }, duration, tween));
556 else if(boost::iequals(parameters()[0], L"VOLUME"))
558 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
559 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
560 double value = boost::lexical_cast<double>(parameters()[1]);
562 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
564 transform.audio_transform.volume = value;
566 }, duration, tween));
568 else if(boost::iequals(parameters()[0], L"CLEAR"))
570 int layer = layer_index(std::numeric_limits<int>::max());
572 if (layer == std::numeric_limits<int>::max())
574 channel()->stage().clear_transforms();
575 channel()->mixer().clear_blend_modes();
579 channel()->stage().clear_transforms(layer);
580 channel()->mixer().clear_blend_mode(layer);
583 else if(boost::iequals(parameters()[0], L"COMMIT"))
585 transforms = std::move(deferred_transforms[channel_index()]);
589 SetReplyString(TEXT("404 MIXER ERROR\r\n"));
595 auto& defer_tranforms = deferred_transforms[channel_index()];
596 defer_tranforms.insert(defer_tranforms.end(), transforms.begin(), transforms.end());
599 channel()->stage().apply_transforms(transforms);
601 SetReplyString(TEXT("202 MIXER OK\r\n"));
605 catch(file_not_found&)
607 CASPAR_LOG_CURRENT_EXCEPTION();
608 SetReplyString(TEXT("404 MIXER ERROR\r\n"));
613 CASPAR_LOG_CURRENT_EXCEPTION();
614 SetReplyString(TEXT("502 MIXER FAILED\r\n"));
619 bool SwapCommand::DoExecute()
621 //Perform loading of the clip
624 if(layer_index(-1) != -1)
626 std::vector<std::string> strs;
627 boost::split(strs, parameters()[0], boost::is_any_of("-"));
629 auto ch1 = channel();
630 auto ch2 = channels().at(boost::lexical_cast<int>(strs.at(0))-1);
632 int l1 = layer_index();
633 int l2 = boost::lexical_cast<int>(strs.at(1));
635 ch1->stage().swap_layer(l1, l2, ch2.channel->stage());
639 auto ch1 = channel();
640 auto ch2 = channels().at(boost::lexical_cast<int>(parameters()[0])-1);
641 ch1->stage().swap_layers(ch2.channel->stage());
644 SetReplyString(TEXT("202 SWAP OK\r\n"));
648 catch(file_not_found&)
650 CASPAR_LOG_CURRENT_EXCEPTION();
651 SetReplyString(TEXT("404 SWAP ERROR\r\n"));
656 CASPAR_LOG_CURRENT_EXCEPTION();
657 SetReplyString(TEXT("502 SWAP FAILED\r\n"));
662 bool AddCommand::DoExecute()
664 //Perform loading of the clip
667 //create_consumer still expects all parameters to be uppercase
668 BOOST_FOREACH(std::wstring& str, parameters())
670 boost::to_upper(str);
673 auto consumer = create_consumer(parameters());
674 channel()->output().add(layer_index(consumer->index()), consumer);
676 SetReplyString(TEXT("202 ADD OK\r\n"));
680 catch(file_not_found&)
682 CASPAR_LOG_CURRENT_EXCEPTION();
683 SetReplyString(TEXT("404 ADD ERROR\r\n"));
688 CASPAR_LOG_CURRENT_EXCEPTION();
689 SetReplyString(TEXT("502 ADD FAILED\r\n"));
694 bool RemoveCommand::DoExecute()
696 //Perform loading of the clip
699 auto index = layer_index(std::numeric_limits<int>::min());
700 if(index == std::numeric_limits<int>::min())
702 //create_consumer still expects all parameters to be uppercase
703 BOOST_FOREACH(std::wstring& str, parameters())
705 boost::to_upper(str);
708 index = create_consumer(parameters())->index();
711 channel()->output().remove(index);
713 SetReplyString(TEXT("202 REMOVE OK\r\n"));
717 catch(file_not_found&)
719 CASPAR_LOG_CURRENT_EXCEPTION();
720 SetReplyString(TEXT("404 REMOVE ERROR\r\n"));
725 CASPAR_LOG_CURRENT_EXCEPTION();
726 SetReplyString(TEXT("502 REMOVE FAILED\r\n"));
731 bool LoadCommand::DoExecute()
733 //Perform loading of the clip
736 auto pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
737 channel()->stage().load(layer_index(), pFP, true);
739 SetReplyString(TEXT("202 LOAD OK\r\n"));
743 catch(file_not_found&)
745 CASPAR_LOG_CURRENT_EXCEPTION();
746 SetReplyString(TEXT("404 LOAD ERROR\r\n"));
751 CASPAR_LOG_CURRENT_EXCEPTION();
752 SetReplyString(TEXT("502 LOAD FAILED\r\n"));
759 //std::function<std::wstring()> channel_cg_add_command::parse(const std::wstring& message, const std::vector<renderer::render_device_ptr>& channels)
761 // 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>.*)?");
763 // boost::wsmatch what;
764 // if(!boost::regex_match(message, what, expr))
767 // auto info = channel_info::parse(what, channels);
769 // int flash_layer_index = boost::lexical_cast<int>(what["FLASH_LAYER"].str());
771 // std::wstring templatename = what["TEMPLATE"].str();
772 // bool play_on_load = what["PLAY_ON_LOAD"].matched ? what["PLAY_ON_LOAD"].str() != L"0" : 0;
773 // std::wstring start_label = what["START_LABEL"].str();
774 // std::wstring data = get_data(what["DATA"].str());
776 // boost::replace_all(templatename, "\"", "");
778 // return [=]() -> std::wstring
780 // std::wstring fullFilename = flash::flash_producer::find_template(server::template_folder() + templatename);
781 // if(fullFilename.empty())
782 // CASPAR_THROW_EXCEPTION(file_not_found());
784 // std::wstring extension = boost::filesystem::wpath(fullFilename).extension();
785 // std::wstring filename = templatename;
786 // filename.append(extension);
788 // flash::flash::create_cg_proxy(info.video_channel, std::max<int>(DEFAULT_CHANNEL_LAYER+1, info.layer_index))
789 // ->add(flash_layer_index, filename, play_on_load, start_label, data);
791 // CASPAR_LOG(info) << L"Executed [amcp_channel_cg_add]";
795 bool LoadbgCommand::DoExecute()
797 transition_info transitionInfo;
801 std::wstring message;
802 for(size_t n = 0; n < parameters().size(); ++n)
803 message += boost::to_upper_copy(parameters()[n]) + L" ";
805 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)?.*)");
807 if(boost::regex_match(message, what, expr))
809 auto transition = what["TRANSITION"].str();
810 transitionInfo.duration = boost::lexical_cast<size_t>(what["DURATION"].str());
811 auto direction = what["DIRECTION"].matched ? what["DIRECTION"].str() : L"";
812 auto tween = what["TWEEN"].matched ? what["TWEEN"].str() : L"";
813 transitionInfo.tweener = tween;
815 if(transition == TEXT("CUT"))
816 transitionInfo.type = transition_type::cut;
817 else if(transition == TEXT("MIX"))
818 transitionInfo.type = transition_type::mix;
819 else if(transition == TEXT("PUSH"))
820 transitionInfo.type = transition_type::push;
821 else if(transition == TEXT("SLIDE"))
822 transitionInfo.type = transition_type::slide;
823 else if(transition == TEXT("WIPE"))
824 transitionInfo.type = transition_type::wipe;
826 if(direction == TEXT("FROMLEFT"))
827 transitionInfo.direction = transition_direction::from_left;
828 else if(direction == TEXT("FROMRIGHT"))
829 transitionInfo.direction = transition_direction::from_right;
830 else if(direction == TEXT("LEFT"))
831 transitionInfo.direction = transition_direction::from_right;
832 else if(direction == TEXT("RIGHT"))
833 transitionInfo.direction = transition_direction::from_left;
836 //Perform loading of the clip
839 std::shared_ptr<core::frame_producer> pFP;
841 static boost::wregex expr(LR"(\[(?<CHANNEL>\d+)\])", boost::regex::icase);
844 if(boost::regex_match(parameters().at(0), what, expr))
846 auto channel_index = boost::lexical_cast<int>(what["CHANNEL"].str());
847 pFP = reroute::create_producer(*channels().at(channel_index-1).channel);
851 pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
854 if(pFP == frame_producer::empty())
855 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info(parameters().size() > 0 ? parameters()[0] : L""));
857 bool auto_play = contains_param(L"AUTO", parameters());
859 auto pFP2 = create_transition_producer(channel()->video_format_desc().field_mode, spl::make_shared_ptr(pFP), transitionInfo);
861 channel()->stage().load(layer_index(), pFP2, false, transitionInfo.duration); // TODO: LOOP
863 channel()->stage().load(layer_index(), pFP2, false); // TODO: LOOP
866 SetReplyString(TEXT("202 LOADBG OK\r\n"));
870 catch(file_not_found&)
872 CASPAR_LOG(error) << L"File not found. No match found for parameters. Check syntax.";
873 SetReplyString(TEXT("404 LOADBG ERROR\r\n"));
878 CASPAR_LOG_CURRENT_EXCEPTION();
879 SetReplyString(TEXT("502 LOADBG FAILED\r\n"));
884 bool PauseCommand::DoExecute()
888 channel()->stage().pause(layer_index());
889 SetReplyString(TEXT("202 PAUSE OK\r\n"));
894 SetReplyString(TEXT("501 PAUSE FAILED\r\n"));
900 bool PlayCommand::DoExecute()
904 if(!parameters().empty())
906 LoadbgCommand lbg(*this);
909 throw std::exception();
912 channel()->stage().play(layer_index());
914 SetReplyString(TEXT("202 PLAY OK\r\n"));
919 SetReplyString(TEXT("501 PLAY FAILED\r\n"));
925 bool StopCommand::DoExecute()
929 channel()->stage().stop(layer_index());
930 SetReplyString(TEXT("202 STOP OK\r\n"));
935 SetReplyString(TEXT("501 STOP FAILED\r\n"));
941 bool ClearCommand::DoExecute()
943 int index = layer_index(std::numeric_limits<int>::min());
944 if(index != std::numeric_limits<int>::min())
945 channel()->stage().clear(index);
947 channel()->stage().clear();
949 SetReplyString(TEXT("202 CLEAR OK\r\n"));
954 bool PrintCommand::DoExecute()
956 channel()->output().add(create_consumer({ L"IMAGE" }));
958 SetReplyString(TEXT("202 PRINT OK\r\n"));
963 bool LogCommand::DoExecute()
965 if(boost::iequals(parameters().at(0), L"LEVEL"))
966 log::set_log_level(parameters().at(1));
968 SetReplyString(TEXT("202 LOG OK\r\n"));
973 bool CGCommand::DoExecute()
977 std::wstring command = boost::to_upper_copy(parameters()[0]);
978 if(command == TEXT("ADD"))
979 return DoExecuteAdd();
980 else if(command == TEXT("PLAY"))
981 return DoExecutePlay();
982 else if(command == TEXT("STOP"))
983 return DoExecuteStop();
984 else if(command == TEXT("NEXT"))
985 return DoExecuteNext();
986 else if(command == TEXT("REMOVE"))
987 return DoExecuteRemove();
988 else if(command == TEXT("CLEAR"))
989 return DoExecuteClear();
990 else if(command == TEXT("UPDATE"))
991 return DoExecuteUpdate();
992 else if(command == TEXT("INVOKE"))
993 return DoExecuteInvoke();
994 else if(command == TEXT("INFO"))
995 return DoExecuteInfo();
999 CASPAR_LOG_CURRENT_EXCEPTION();
1002 SetReplyString(TEXT("403 CG ERROR\r\n"));
1006 bool CGCommand::ValidateLayer(const std::wstring& layerstring) {
1007 int length = layerstring.length();
1008 for(int i = 0; i < length; ++i) {
1009 if(!_istdigit(layerstring[i])) {
1017 bool CGCommand::DoExecuteAdd() {
1018 //CG 1 ADD 0 "template_folder/templatename" [STARTLABEL] 0/1 [DATA]
1020 int layer = 0; //_parameters[1]
1021 // std::wstring templateName; //_parameters[2]
1022 std::wstring label; //_parameters[3]
1023 bool bDoStart = false; //_parameters[3] alt. _parameters[4]
1024 // std::wstring data; //_parameters[4] alt. _parameters[5]
1026 if(parameters().size() < 4)
1028 SetReplyString(TEXT("402 CG ERROR\r\n"));
1031 unsigned int dataIndex = 4;
1033 if(!ValidateLayer(parameters()[1]))
1035 SetReplyString(TEXT("403 CG ERROR\r\n"));
1039 layer = boost::lexical_cast<int>(parameters()[1]);
1041 if(parameters()[3].length() > 1)
1043 label = parameters()[3];
1046 if(parameters().size() > 4 && parameters()[4].length() > 0) //read play-on-load-flag
1047 bDoStart = (parameters()[4][0]==TEXT('1')) ? true : false;
1050 SetReplyString(TEXT("402 CG ERROR\r\n"));
1054 else if(parameters()[3].length() > 0) { //read play-on-load-flag
1055 bDoStart = (parameters()[3][0]==TEXT('1')) ? true : false;
1059 SetReplyString(TEXT("403 CG ERROR\r\n"));
1063 const TCHAR* pDataString = 0;
1064 std::wstring dataFromFile;
1065 if(parameters().size() > dataIndex)
1067 const std::wstring& dataString = parameters()[dataIndex];
1069 if(dataString[0] == TEXT('<')) //the data is an XML-string
1070 pDataString = dataString.c_str();
1073 //The data is not an XML-string, it must be a filename
1074 std::wstring filename = env::data_folder();
1075 filename.append(dataString);
1076 filename.append(TEXT(".ftd"));
1078 dataFromFile = read_file(boost::filesystem::wpath(filename));
1079 pDataString = dataFromFile.c_str();
1083 std::wstring fullFilename = flash::find_template(env::template_folder() + parameters()[2]);
1084 if(!fullFilename.empty())
1086 std::wstring extension = boost::filesystem::path(fullFilename).extension().wstring();
1087 std::wstring filename = parameters()[2];
1088 filename.append(extension);
1090 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(""));
1091 SetReplyString(TEXT("202 CG OK\r\n"));
1095 CASPAR_LOG(warning) << "Could not find template " << parameters()[2];
1096 SetReplyString(TEXT("404 CG ERROR\r\n"));
1101 bool CGCommand::DoExecutePlay()
1103 if(parameters().size() > 1)
1105 if(!ValidateLayer(parameters()[1]))
1107 SetReplyString(TEXT("403 CG ERROR\r\n"));
1110 int layer = boost::lexical_cast<int>(parameters()[1]);
1111 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).play(layer);
1115 SetReplyString(TEXT("402 CG ERROR\r\n"));
1119 SetReplyString(TEXT("202 CG OK\r\n"));
1123 bool CGCommand::DoExecuteStop()
1125 if(parameters().size() > 1)
1127 if(!ValidateLayer(parameters()[1]))
1129 SetReplyString(TEXT("403 CG ERROR\r\n"));
1132 int layer = boost::lexical_cast<int>(parameters()[1]);
1133 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).stop(layer, 0);
1137 SetReplyString(TEXT("402 CG ERROR\r\n"));
1141 SetReplyString(TEXT("202 CG OK\r\n"));
1145 bool CGCommand::DoExecuteNext()
1147 if(parameters().size() > 1)
1149 if(!ValidateLayer(parameters()[1]))
1151 SetReplyString(TEXT("403 CG ERROR\r\n"));
1155 int layer = boost::lexical_cast<int>(parameters()[1]);
1156 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).next(layer);
1160 SetReplyString(TEXT("402 CG ERROR\r\n"));
1164 SetReplyString(TEXT("202 CG OK\r\n"));
1168 bool CGCommand::DoExecuteRemove()
1170 if(parameters().size() > 1)
1172 if(!ValidateLayer(parameters()[1]))
1174 SetReplyString(TEXT("403 CG ERROR\r\n"));
1178 int layer = boost::lexical_cast<int>(parameters()[1]);
1179 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).remove(layer);
1183 SetReplyString(TEXT("402 CG ERROR\r\n"));
1187 SetReplyString(TEXT("202 CG OK\r\n"));
1191 bool CGCommand::DoExecuteClear()
1193 channel()->stage().clear(layer_index(flash::cg_proxy::DEFAULT_LAYER));
1194 SetReplyString(TEXT("202 CG OK\r\n"));
1198 bool CGCommand::DoExecuteUpdate()
1202 if(!ValidateLayer(parameters().at(1)))
1204 SetReplyString(TEXT("403 CG ERROR\r\n"));
1208 std::wstring dataString = parameters().at(2);
1209 if(dataString.at(0) != TEXT('<'))
1211 //The data is not an XML-string, it must be a filename
1212 std::wstring filename = env::data_folder();
1213 filename.append(dataString);
1214 filename.append(TEXT(".ftd"));
1216 dataString = read_file(boost::filesystem::wpath(filename));
1219 int layer = boost::lexical_cast<int>(parameters()[1]);
1220 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).update(layer, dataString);
1224 SetReplyString(TEXT("402 CG ERROR\r\n"));
1228 SetReplyString(TEXT("202 CG OK\r\n"));
1232 bool CGCommand::DoExecuteInvoke()
1234 std::wstringstream replyString;
1235 replyString << TEXT("201 CG OK\r\n");
1237 if(parameters().size() > 2)
1239 if(!ValidateLayer(parameters()[1]))
1241 SetReplyString(TEXT("403 CG ERROR\r\n"));
1244 int layer = boost::lexical_cast<int>(parameters()[1]);
1245 auto result = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).invoke(layer, parameters()[2]);
1246 replyString << result << TEXT("\r\n");
1250 SetReplyString(TEXT("402 CG ERROR\r\n"));
1254 SetReplyString(replyString.str());
1258 bool CGCommand::DoExecuteInfo()
1260 std::wstringstream replyString;
1261 replyString << TEXT("201 CG OK\r\n");
1263 if(parameters().size() > 1)
1265 if(!ValidateLayer(parameters()[1]))
1267 SetReplyString(TEXT("403 CG ERROR\r\n"));
1271 int layer = boost::lexical_cast<int>(parameters()[1]);
1272 auto desc = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).description(layer);
1274 replyString << desc << TEXT("\r\n");
1278 auto info = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).template_host_info();
1279 replyString << info << TEXT("\r\n");
1282 SetReplyString(replyString.str());
1286 bool DataCommand::DoExecute()
1288 std::wstring command = boost::to_upper_copy(parameters()[0]);
1289 if(command == TEXT("STORE"))
1290 return DoExecuteStore();
1291 else if(command == TEXT("RETRIEVE"))
1292 return DoExecuteRetrieve();
1293 else if(command == TEXT("REMOVE"))
1294 return DoExecuteRemove();
1295 else if(command == TEXT("LIST"))
1296 return DoExecuteList();
1298 SetReplyString(TEXT("403 DATA ERROR\r\n"));
1302 bool DataCommand::DoExecuteStore()
1304 if(parameters().size() < 3)
1306 SetReplyString(TEXT("402 DATA STORE ERROR\r\n"));
1310 std::wstring filename = env::data_folder();
1311 filename.append(parameters()[1]);
1312 filename.append(TEXT(".ftd"));
1314 auto data_path = boost::filesystem::wpath(
1315 boost::filesystem::wpath(filename).parent_path());
1317 if(!boost::filesystem::exists(data_path))
1318 boost::filesystem::create_directories(data_path);
1320 std::wofstream datafile(filename.c_str());
1323 SetReplyString(TEXT("501 DATA STORE FAILED\r\n"));
1327 datafile << static_cast<wchar_t>(65279); // UTF-8 BOM character
1328 datafile << parameters()[2] << std::flush;
1331 std::wstring replyString = TEXT("202 DATA STORE OK\r\n");
1332 SetReplyString(replyString);
1336 bool DataCommand::DoExecuteRetrieve()
1338 if(parameters().size() < 2)
1340 SetReplyString(TEXT("402 DATA RETRIEVE ERROR\r\n"));
1344 std::wstring filename = env::data_folder();
1345 filename.append(parameters()[1]);
1346 filename.append(TEXT(".ftd"));
1348 std::wstring file_contents = read_file(boost::filesystem::wpath(filename));
1350 if (file_contents.empty())
1352 SetReplyString(TEXT("404 DATA RETRIEVE ERROR\r\n"));
1356 std::wstringstream reply(TEXT("201 DATA RETRIEVE OK\r\n"));
1358 std::wstringstream file_contents_stream(file_contents);
1361 bool firstLine = true;
1362 while(std::getline(file_contents_stream, line))
1373 SetReplyString(reply.str());
1377 bool DataCommand::DoExecuteRemove()
1379 if (parameters().size() < 2)
1381 SetReplyString(TEXT("402 DATA REMOVE ERROR\r\n"));
1385 std::wstring filename = env::data_folder();
1386 filename.append(parameters()[1]);
1387 filename.append(TEXT(".ftd"));
1389 if (!boost::filesystem::exists(filename))
1391 SetReplyString(TEXT("404 DATA REMOVE ERROR\r\n"));
1395 if (!boost::filesystem::remove(filename))
1397 SetReplyString(TEXT("403 DATA REMOVE ERROR\r\n"));
1401 SetReplyString(TEXT("201 DATA REMOVE OK\r\n"));
1406 bool DataCommand::DoExecuteList()
1408 std::wstringstream replyString;
1409 replyString << TEXT("200 DATA LIST OK\r\n");
1411 for (boost::filesystem::recursive_directory_iterator itr(env::data_folder()), end; itr != end; ++itr)
1413 if(boost::filesystem::is_regular_file(itr->path()))
1415 if(!boost::iequals(itr->path().extension().wstring(), L".ftd"))
1418 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::data_folder().size()-1, itr->path().wstring().size()));
1420 auto str = relativePath.replace_extension(TEXT("")).native();
1421 if(str[0] == '\\' || str[0] == '/')
1422 str = std::wstring(str.begin() + 1, str.end());
1424 replyString << str << TEXT("\r\n");
1428 replyString << TEXT("\r\n");
1430 SetReplyString(boost::to_upper_copy(replyString.str()));
1434 bool CinfCommand::DoExecute()
1436 std::wstringstream replyString;
1441 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end && info.empty(); ++itr)
1443 auto path = itr->path();
1444 auto file = path.replace_extension(L"").filename();
1445 if(boost::iequals(file.wstring(), parameters().at(0)))
1446 info += MediaInfo(itr->path()) + L"\r\n";
1451 SetReplyString(TEXT("404 CINF ERROR\r\n"));
1454 replyString << TEXT("200 CINF OK\r\n");
1455 replyString << info << "\r\n";
1459 CASPAR_LOG_CURRENT_EXCEPTION();
1460 SetReplyString(TEXT("404 CINF ERROR\r\n"));
1464 SetReplyString(replyString.str());
1468 void GenerateChannelInfo(int index, const spl::shared_ptr<core::video_channel>& pChannel, std::wstringstream& replyString)
1470 replyString << index+1 << TEXT(" ") << pChannel->video_format_desc().name << TEXT(" PLAYING") << TEXT("\r\n");
1473 bool InfoCommand::DoExecute()
1475 std::wstringstream replyString;
1477 boost::property_tree::xml_writer_settings<std::wstring> w(' ', 3);
1481 if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"TEMPLATE"))
1483 replyString << L"201 INFO TEMPLATE OK\r\n";
1485 // Needs to be extended for any file, not just flash.
1487 auto filename = flash::find_template(env::template_folder() + parameters().at(1));
1489 std::wstringstream str;
1490 str << u16(flash::read_template_meta_info(filename));
1491 boost::property_tree::wptree info;
1492 boost::property_tree::xml_parser::read_xml(str, info, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);
1494 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1496 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"CONFIG"))
1498 replyString << L"201 INFO CONFIG OK\r\n";
1500 boost::property_tree::write_xml(replyString, caspar::env::properties(), w);
1502 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"PATHS"))
1504 replyString << L"201 INFO PATHS OK\r\n";
1506 boost::property_tree::wptree info;
1507 info.add_child(L"paths", caspar::env::properties().get_child(L"configuration.paths"));
1508 info.add(L"paths.initial-path", boost::filesystem::initial_path().wstring() + L"\\");
1510 boost::property_tree::write_xml(replyString, info, w);
1512 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SYSTEM"))
1514 replyString << L"201 INFO SYSTEM OK\r\n";
1516 boost::property_tree::wptree info;
1518 info.add(L"system.name", caspar::system_product_name());
1519 info.add(L"system.windows.name", caspar::win_product_name());
1520 info.add(L"system.windows.service-pack", caspar::win_sp_version());
1521 info.add(L"system.cpu", caspar::cpu_info());
1523 BOOST_FOREACH(auto device, caspar::decklink::device_list())
1524 info.add(L"system.decklink.device", device);
1526 BOOST_FOREACH(auto device, caspar::bluefish::device_list())
1527 info.add(L"system.bluefish.device", device);
1529 info.add(L"system.flash", caspar::flash::version());
1530 //info.add(L"system.free-image", caspar::image::version());
1531 info.add(L"system.ffmpeg.avcodec", caspar::ffmpeg::avcodec_version());
1532 info.add(L"system.ffmpeg.avformat", caspar::ffmpeg::avformat_version());
1533 info.add(L"system.ffmpeg.avfilter", caspar::ffmpeg::avfilter_version());
1534 info.add(L"system.ffmpeg.avutil", caspar::ffmpeg::avutil_version());
1535 info.add(L"system.ffmpeg.swscale", caspar::ffmpeg::swscale_version());
1537 boost::property_tree::write_xml(replyString, info, w);
1539 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SERVER"))
1541 replyString << L"201 INFO SERVER OK\r\n";
1543 boost::property_tree::wptree info;
1546 BOOST_FOREACH(auto channel, channels())
1547 info.add_child(L"channels.channel", channel.channel->info())
1548 .add(L"index", ++index);
1550 boost::property_tree::write_xml(replyString, info, w);
1554 if(parameters().size() >= 1)
1556 replyString << TEXT("201 INFO OK\r\n");
1557 boost::property_tree::wptree info;
1559 std::vector<std::wstring> split;
1560 boost::split(split, parameters()[0], boost::is_any_of("-"));
1562 int layer = std::numeric_limits<int>::min();
1563 int channel = boost::lexical_cast<int>(split[0]) - 1;
1565 if(split.size() > 1)
1566 layer = boost::lexical_cast<int>(split[1]);
1568 if(layer == std::numeric_limits<int>::min())
1570 info.add_child(L"channel", channels().at(channel).channel->info())
1571 .add(L"index", channel);
1575 if(parameters().size() >= 2)
1577 if(boost::iequals(parameters()[1], L"B"))
1578 info.add_child(L"producer", channels().at(channel).channel->stage().background(layer).get()->info());
1580 info.add_child(L"producer", channels().at(channel).channel->stage().foreground(layer).get()->info());
1584 info.add_child(L"layer", channels().at(channel).channel->stage().info(layer).get())
1585 .add(L"index", layer);
1588 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1592 // This is needed for backwards compatibility with old clients
1593 replyString << TEXT("200 INFO OK\r\n");
1594 for(size_t n = 0; n < channels().size(); ++n)
1595 GenerateChannelInfo(n, channels()[n].channel, replyString);
1602 CASPAR_LOG_CURRENT_EXCEPTION();
1603 SetReplyString(TEXT("403 INFO ERROR\r\n"));
1607 replyString << TEXT("\r\n");
1608 SetReplyString(replyString.str());
1612 bool ClsCommand::DoExecute()
1624 std::wstringstream replyString;
1625 replyString << TEXT("200 CLS OK\r\n");
1626 replyString << ListMedia();
1627 replyString << TEXT("\r\n");
1628 SetReplyString(boost::to_upper_copy(replyString.str()));
1632 CASPAR_LOG_CURRENT_EXCEPTION();
1633 SetReplyString(TEXT("501 CLS FAILED\r\n"));
1640 bool TlsCommand::DoExecute()
1644 std::wstringstream replyString;
1645 replyString << TEXT("200 TLS OK\r\n");
1647 replyString << ListTemplates();
1648 replyString << TEXT("\r\n");
1650 SetReplyString(replyString.str());
1654 CASPAR_LOG_CURRENT_EXCEPTION();
1655 SetReplyString(TEXT("501 TLS FAILED\r\n"));
1661 bool VersionCommand::DoExecute()
1663 std::wstring replyString = TEXT("201 VERSION OK\r\n") + env::version() + TEXT("\r\n");
1665 if(parameters().size() > 0)
1667 if(boost::iequals(parameters()[0], L"FLASH"))
1668 replyString = TEXT("201 VERSION OK\r\n") + flash::version() + TEXT("\r\n");
1669 else if(boost::iequals(parameters()[0], L"TEMPLATEHOST"))
1670 replyString = TEXT("201 VERSION OK\r\n") + flash::cg_version() + TEXT("\r\n");
1671 else if(!boost::iequals(parameters()[0], L"SERVER"))
1672 replyString = TEXT("403 VERSION ERROR\r\n");
1675 SetReplyString(replyString);
1679 bool ByeCommand::DoExecute()
1681 client()->disconnect();
1685 bool SetCommand::DoExecute()
1689 std::wstring name = boost::to_upper_copy(parameters()[0]);
1690 std::wstring value = boost::to_upper_copy(parameters()[1]);
1692 if(name == TEXT("MODE"))
1694 auto format_desc = core::video_format_desc(value);
1695 if(format_desc.format != core::video_format::invalid)
1697 channel()->video_format_desc(format_desc);
1698 SetReplyString(TEXT("202 SET MODE OK\r\n"));
1701 SetReplyString(TEXT("501 SET MODE FAILED\r\n"));
1705 this->SetReplyString(TEXT("403 SET ERROR\r\n"));
1710 CASPAR_LOG_CURRENT_EXCEPTION();
1711 SetReplyString(TEXT("501 SET FAILED\r\n"));
1718 bool LockCommand::DoExecute()
1722 auto it = parameters().begin();
1724 std::shared_ptr<caspar::IO::lock_container> lock;
1727 int channel_index = boost::lexical_cast<int>(*it) - 1;
1728 lock = channels().at(channel_index).lock;
1730 catch(const boost::bad_lexical_cast&) {}
1733 SetReplyString(L"401 LOCK ERROR\r\n");
1740 if(it == parameters().end()) //too few parameters
1742 SetReplyString(L"402 LOCK ERROR\r\n");
1746 std::wstring command = boost::to_upper_copy(*it);
1747 if(command == L"ACQUIRE")
1750 if(it == parameters().end()) //too few parameters
1752 SetReplyString(L"402 LOCK ACQUIRE ERROR\r\n");
1755 std::wstring lock_phrase = (*it);
1757 //TODO: read options
1761 //just lock one channel
1762 if(!lock->try_lock(lock_phrase, client()))
1764 SetReplyString(L"503 LOCK ACQUIRE FAILED\r\n");
1770 //TODO: lock all channels
1771 CASPAR_THROW_EXCEPTION(not_implemented());
1773 SetReplyString(L"202 LOCK ACQUIRE OK\r\n");
1776 else if(command == L"RELEASE")
1780 lock->release_lock(client());
1784 //TODO: release all channels
1785 CASPAR_THROW_EXCEPTION(not_implemented());
1787 SetReplyString(L"202 LOCK RELEASE OK\r\n");
1789 else if(command == L"CLEAR")
1791 std::wstring override_phrase = env::properties().get(L"configuration.lock-clear-phrase", L"");
1792 std::wstring client_override_phrase;
1793 if(!override_phrase.empty())
1796 if(it == parameters().end())
1798 SetReplyString(L"402 LOCK CLEAR ERROR\r\n");
1801 client_override_phrase = (*it);
1806 //just clear one channel
1807 if(client_override_phrase != override_phrase)
1809 SetReplyString(L"503 LOCK CLEAR FAILED\r\n");
1813 lock->clear_locks();
1817 //TODO: clear all channels
1818 CASPAR_THROW_EXCEPTION(not_implemented());
1821 SetReplyString(L"202 LOCK CLEAR OK\r\n");
1825 SetReplyString(L"403 LOCK ERROR\r\n");
1829 catch(not_implemented&)
1831 SetReplyString(L"600 LOCK FAILED\r\n");
1836 CASPAR_LOG_CURRENT_EXCEPTION();
1837 SetReplyString(L"501 LOCK FAILED\r\n");
1844 bool ThumbnailCommand::DoExecute()
1846 std::wstring command = boost::to_upper_copy(parameters()[0]);
1848 if (command == TEXT("RETRIEVE"))
1849 return DoExecuteRetrieve();
1850 else if (command == TEXT("LIST"))
1851 return DoExecuteList();
1852 else if (command == TEXT("GENERATE"))
1853 return DoExecuteGenerate();
1854 else if (command == TEXT("GENERATE_ALL"))
1855 return DoExecuteGenerateAll();
1857 SetReplyString(TEXT("403 THUMBNAIL ERROR\r\n"));
1861 bool ThumbnailCommand::DoExecuteRetrieve()
1863 if(parameters().size() < 2)
1865 SetReplyString(TEXT("402 THUMBNAIL RETRIEVE ERROR\r\n"));
1869 std::wstring filename = env::thumbnails_folder();
1870 filename.append(parameters()[1]);
1871 filename.append(TEXT(".png"));
1873 std::wstring file_contents = read_file_base64(boost::filesystem::wpath(filename));
1875 if (file_contents.empty())
1877 SetReplyString(TEXT("404 THUMBNAIL RETRIEVE ERROR\r\n"));
1881 std::wstringstream reply;
1883 reply << L"201 THUMBNAIL RETRIEVE OK\r\n";
1884 reply << file_contents;
1886 SetReplyString(reply.str());
1890 bool ThumbnailCommand::DoExecuteList()
1892 std::wstringstream replyString;
1893 replyString << TEXT("200 THUMBNAIL LIST OK\r\n");
1895 for (boost::filesystem::recursive_directory_iterator itr(env::thumbnails_folder()), end; itr != end; ++itr)
1897 if(boost::filesystem::is_regular_file(itr->path()))
1899 if(!boost::iequals(itr->path().extension().wstring(), L".png"))
1902 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::thumbnails_folder().size()-1, itr->path().wstring().size()));
1904 auto str = relativePath.replace_extension(L"").native();
1905 if(str[0] == '\\' || str[0] == '/')
1906 str = std::wstring(str.begin() + 1, str.end());
1908 auto mtime = boost::filesystem::last_write_time(itr->path());
1909 auto mtime_readable = boost::posix_time::to_iso_wstring(boost::posix_time::from_time_t(mtime));
1910 auto file_size = boost::filesystem::file_size(itr->path());
1912 replyString << L"\"" << str << L"\" " << mtime_readable << L" " << file_size << L"\r\n";
1916 replyString << TEXT("\r\n");
1918 SetReplyString(boost::to_upper_copy(replyString.str()));
1922 bool ThumbnailCommand::DoExecuteGenerate()
1924 if (parameters().size() < 2)
1926 SetReplyString(L"402 THUMBNAIL GENERATE ERROR\r\n");
1932 thumb_gen_->generate(parameters()[1]);
1933 SetReplyString(L"202 THUMBNAIL GENERATE OK\r\n");
1938 SetReplyString(L"500 THUMBNAIL GENERATE ERROR\r\n");
1943 bool ThumbnailCommand::DoExecuteGenerateAll()
1947 thumb_gen_->generate_all();
1948 SetReplyString(L"202 THUMBNAIL GENERATE_ALL OK\r\n");
1953 SetReplyString(L"500 THUMBNAIL GENERATE_ALL ERROR\r\n");
1958 bool KillCommand::DoExecute()
1960 shutdown_server_now_->set_value(false); //false for not attempting to restart
1961 SetReplyString(TEXT("202 KILL OK\r\n"));
1965 bool RestartCommand::DoExecute()
1967 shutdown_server_now_->set_value(true); //true for attempting to restart
1968 SetReplyString(TEXT("202 RESTART OK\r\n"));
1973 }} //namespace caspar