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>
70 #include <boost/date_time/posix_time/posix_time.hpp>
71 #include <boost/lexical_cast.hpp>
72 #include <boost/algorithm/string.hpp>
73 #include <boost/filesystem.hpp>
74 #include <boost/filesystem/fstream.hpp>
75 #include <boost/regex.hpp>
76 #include <boost/property_tree/xml_parser.hpp>
77 #include <boost/locale.hpp>
78 #include <boost/range/adaptor/transformed.hpp>
79 #include <boost/range/algorithm/copy.hpp>
80 #include <boost/archive/iterators/base64_from_binary.hpp>
81 #include <boost/archive/iterators/insert_linebreaks.hpp>
82 #include <boost/archive/iterators/transform_width.hpp>
84 #include <tbb/concurrent_unordered_map.h>
88 102 [action] Information that [action] has happened
89 101 [action] Information that [action] has happened plus one row of data
91 202 [command] OK [command] has been executed
92 201 [command] OK [command] has been executed, plus one row of data
93 200 [command] OK [command] has been executed, plus multiple lines of data. ends with an empty line
95 400 ERROR the command could not be understood
96 401 [command] ERROR invalid/missing channel
97 402 [command] ERROR parameter missing
98 403 [command] ERROR invalid parameter
99 404 [command] ERROR file not found
101 500 FAILED internal error
102 501 [command] FAILED internal error
103 502 [command] FAILED could not read file
104 503 [command] FAILED access denied
106 600 [command] FAILED [command] not implemented
109 namespace caspar { namespace protocol {
111 using namespace core;
113 std::wstring read_file_base64(const boost::filesystem::wpath& file)
115 using namespace boost::archive::iterators;
117 boost::filesystem::ifstream filestream(file, std::ios::binary);
122 auto length = boost::filesystem::file_size(file);
123 std::vector<char> bytes;
124 bytes.resize(length);
125 filestream.read(bytes.data(), length);
127 std::string result(to_base64(bytes.data(), length));
128 return std::wstring(result.begin(), result.end());
131 std::wstring read_utf8_file(const boost::filesystem::wpath& file)
133 std::wstringstream result;
134 boost::filesystem::wifstream filestream(file);
141 result << filestream.rdbuf();
147 std::wstring read_latin1_file(const boost::filesystem::wpath& file)
149 boost::locale::generator gen;
150 gen.locale_cache_enabled(true);
151 gen.categories(boost::locale::codepage_facet);
153 std::stringstream result_stream;
154 boost::filesystem::ifstream filestream(file);
155 filestream.imbue(gen("en_US.ISO8859-1"));
160 result_stream << filestream.rdbuf();
163 std::string result = result_stream.str();
164 std::wstring widened_result;
166 // The first 255 codepoints in unicode is the same as in latin1
167 auto from_signed_to_signed = std::function<unsigned char(char)>(
168 [] (char c) { return static_cast<unsigned char>(c); }
171 result | boost::adaptors::transformed(from_signed_to_signed),
172 std::back_inserter(widened_result));
174 return widened_result;
177 std::wstring read_file(const boost::filesystem::wpath& file)
179 static const uint8_t BOM[] = {0xef, 0xbb, 0xbf};
181 if (!boost::filesystem::exists(file))
186 if (boost::filesystem::file_size(file) >= 3)
188 boost::filesystem::ifstream bom_stream(file);
191 bom_stream.read(header, 3);
194 if (std::memcmp(BOM, header, 3) == 0)
195 return read_utf8_file(file);
198 return read_latin1_file(file);
201 std::wstring MediaInfo(const boost::filesystem::path& path)
203 if(boost::filesystem::is_regular_file(path))
205 std::wstring clipttype = TEXT("N/A");
206 std::wstring extension = boost::to_upper_copy(path.extension().wstring());
207 if(extension == TEXT(".TGA") || extension == TEXT(".COL") || extension == L".PNG" || extension == L".JPEG" || extension == L".JPG" ||
208 extension == L"GIF" || extension == L"BMP")
209 clipttype = TEXT("STILL");
210 else if(extension == TEXT(".WAV") || extension == TEXT(".MP3"))
211 clipttype = TEXT("AUDIO");
212 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()))
213 clipttype = TEXT("MOVIE");
215 if(clipttype != TEXT("N/A"))
217 auto is_not_digit = [](char c){ return std::isdigit(c) == 0; };
219 auto relativePath = boost::filesystem::path(path.wstring().substr(env::media_folder().size()-1, path.wstring().size()));
221 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(path)));
222 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), is_not_digit), writeTimeStr.end());
223 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
225 auto sizeStr = boost::lexical_cast<std::wstring>(boost::filesystem::file_size(path));
226 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), is_not_digit), sizeStr.end());
227 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
229 auto str = relativePath.replace_extension(TEXT("")).native();
230 while(str.size() > 0 && (str[0] == '\\' || str[0] == '/'))
231 str = std::wstring(str.begin() + 1, str.end());
233 return std::wstring() + TEXT("\"") + str +
234 + TEXT("\" ") + clipttype +
235 + TEXT(" ") + sizeStr +
236 + TEXT(" ") + writeTimeWStr +
243 std::wstring ListMedia()
245 std::wstringstream replyString;
246 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end; ++itr)
247 replyString << MediaInfo(itr->path());
249 return boost::to_upper_copy(replyString.str());
252 std::wstring ListTemplates()
254 std::wstringstream replyString;
256 for (boost::filesystem::recursive_directory_iterator itr(env::template_folder()), end; itr != end; ++itr)
258 if(boost::filesystem::is_regular_file(itr->path()) && (itr->path().extension() == L".ft" || itr->path().extension() == L".ct"))
260 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::template_folder().size()-1, itr->path().wstring().size()));
262 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(itr->path())));
263 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), [](char c){ return std::isdigit(c) == 0;}), writeTimeStr.end());
264 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
266 auto sizeStr = boost::lexical_cast<std::string>(boost::filesystem::file_size(itr->path()));
267 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), [](char c){ return std::isdigit(c) == 0;}), sizeStr.end());
269 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
271 std::wstring dir = relativePath.parent_path().native();
272 std::wstring file = boost::to_upper_copy(relativePath.filename().wstring());
273 relativePath = boost::filesystem::wpath(dir + L"/" + file);
275 auto str = relativePath.replace_extension(TEXT("")).native();
276 boost::trim_if(str, boost::is_any_of("\\/"));
278 replyString << TEXT("\"") << str
279 << TEXT("\" ") << sizeWStr
280 << TEXT(" ") << writeTimeWStr
284 return replyString.str();
289 void AMCPCommand::SendReply()
291 if(replyString_.empty())
294 client_->send(std::move(replyString_));
297 bool DiagnosticsCommand::DoExecute()
301 diagnostics::show_graphs(true);
303 SetReplyString(TEXT("202 DIAG OK\r\n"));
309 CASPAR_LOG_CURRENT_EXCEPTION();
310 SetReplyString(TEXT("502 DIAG FAILED\r\n"));
315 bool ChannelGridCommand::DoExecute()
317 CASPAR_THROW_EXCEPTION(not_implemented());
320 //auto self = channels().back();
322 //std::vector<std::wstring> params;
323 //params.push_back(L"SCREEN");
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 != self)
334 // auto producer = reroute::create_producer(self->frame_factory(), *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)
347 // int index = x+y*n+1;
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 if(!result.timed_wait(boost::posix_time::seconds(2)))
375 CASPAR_THROW_EXCEPTION(timed_out());
377 std::wstringstream replyString;
378 if(result.get().empty())
379 replyString << TEXT("202 CALL OK\r\n");
381 replyString << TEXT("201 CALL OK\r\n") << result.get() << L"\r\n";
383 SetReplyString(replyString.str());
389 CASPAR_LOG_CURRENT_EXCEPTION();
390 SetReplyString(TEXT("502 CALL FAILED\r\n"));
395 tbb::concurrent_unordered_map<int, std::vector<stage::transform_tuple_t>> deferred_transforms;
397 bool MixerCommand::DoExecute()
399 //Perform loading of the clip
402 bool defer = boost::iequals(parameters().back(), L"DEFER");
404 parameters().pop_back();
406 std::vector<stage::transform_tuple_t> transforms;
408 if(boost::iequals(parameters()[0], L"KEYER") || boost::iequals(parameters()[0], L"IS_KEY"))
410 bool value = boost::lexical_cast<int>(parameters().at(1));
411 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
413 transform.image_transform.is_key = value;
417 else if(boost::iequals(parameters()[0], L"OPACITY"))
419 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
420 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
422 double value = boost::lexical_cast<double>(parameters().at(1));
424 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
426 transform.image_transform.opacity = value;
428 }, duration, tween));
430 else if(boost::iequals(parameters()[0], L"FILL") || boost::iequals(parameters()[0], L"FILL_RECT"))
432 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
433 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
434 double x = boost::lexical_cast<double>(parameters().at(1));
435 double y = boost::lexical_cast<double>(parameters().at(2));
436 double x_s = boost::lexical_cast<double>(parameters().at(3));
437 double y_s = boost::lexical_cast<double>(parameters().at(4));
439 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) mutable -> frame_transform
441 transform.image_transform.fill_translation[0] = x;
442 transform.image_transform.fill_translation[1] = y;
443 transform.image_transform.fill_scale[0] = x_s;
444 transform.image_transform.fill_scale[1] = y_s;
446 }, duration, tween));
448 else if(boost::iequals(parameters()[0], L"CLIP") || boost::iequals(parameters()[0], L"CLIP_RECT"))
450 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
451 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
452 double x = boost::lexical_cast<double>(parameters().at(1));
453 double y = boost::lexical_cast<double>(parameters().at(2));
454 double x_s = boost::lexical_cast<double>(parameters().at(3));
455 double y_s = boost::lexical_cast<double>(parameters().at(4));
457 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
459 transform.image_transform.clip_translation[0] = x;
460 transform.image_transform.clip_translation[1] = y;
461 transform.image_transform.clip_scale[0] = x_s;
462 transform.image_transform.clip_scale[1] = y_s;
464 }, duration, tween));
466 else if(boost::iequals(parameters()[0], L"GRID"))
468 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
469 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
470 int n = boost::lexical_cast<int>(parameters().at(1));
471 double delta = 1.0/static_cast<double>(n);
472 for(int x = 0; x < n; ++x)
474 for(int y = 0; y < n; ++y)
477 transforms.push_back(stage::transform_tuple_t(index, [=](frame_transform transform) -> frame_transform
479 transform.image_transform.fill_translation[0] = x*delta;
480 transform.image_transform.fill_translation[1] = y*delta;
481 transform.image_transform.fill_scale[0] = delta;
482 transform.image_transform.fill_scale[1] = delta;
483 transform.image_transform.clip_translation[0] = x*delta;
484 transform.image_transform.clip_translation[1] = y*delta;
485 transform.image_transform.clip_scale[0] = delta;
486 transform.image_transform.clip_scale[1] = delta;
488 }, duration, tween));
492 else if(boost::iequals(parameters()[0], L"BLEND"))
494 auto blend_str = parameters().at(1);
495 int layer = layer_index();
496 channel()->mixer().set_blend_mode(layer_index(), get_blend_mode(blend_str));
498 else if(boost::iequals(parameters()[0], L"MASTERVOLUME"))
500 float master_volume = boost::lexical_cast<float>(parameters().at(1));
501 channel()->mixer().set_master_volume(master_volume);
503 else if(boost::iequals(parameters()[0], L"BRIGHTNESS"))
505 auto value = boost::lexical_cast<double>(parameters().at(1));
506 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
507 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
508 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
510 transform.image_transform.brightness = value;
512 }, duration, tween));
514 else if(boost::iequals(parameters()[0], L"SATURATION"))
516 auto value = boost::lexical_cast<double>(parameters().at(1));
517 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
518 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
519 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
521 transform.image_transform.saturation = value;
523 }, duration, tween));
525 else if(parameters()[0] == L"CONTRAST")
527 auto value = boost::lexical_cast<double>(parameters().at(1));
528 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
529 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
530 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
532 transform.image_transform.contrast = value;
534 }, duration, tween));
536 else if(boost::iequals(parameters()[0], L"LEVELS"))
539 value.min_input = boost::lexical_cast<double>(parameters().at(1));
540 value.max_input = boost::lexical_cast<double>(parameters().at(2));
541 value.gamma = boost::lexical_cast<double>(parameters().at(3));
542 value.min_output = boost::lexical_cast<double>(parameters().at(4));
543 value.max_output = boost::lexical_cast<double>(parameters().at(5));
544 int duration = parameters().size() > 6 ? boost::lexical_cast<int>(parameters()[6]) : 0;
545 std::wstring tween = parameters().size() > 7 ? parameters()[7] : L"linear";
547 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
549 transform.image_transform.levels = value;
551 }, duration, tween));
553 else if(boost::iequals(parameters()[0], L"VOLUME"))
555 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
556 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
557 double value = boost::lexical_cast<double>(parameters()[1]);
559 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
561 transform.audio_transform.volume = value;
563 }, duration, tween));
565 else if(boost::iequals(parameters()[0], L"CLEAR"))
567 int layer = layer_index(std::numeric_limits<int>::max());
569 if (layer == std::numeric_limits<int>::max())
571 channel()->stage().clear_transforms();
572 channel()->mixer().clear_blend_modes();
576 channel()->stage().clear_transforms(layer);
577 channel()->mixer().clear_blend_mode(layer);
580 else if(boost::iequals(parameters()[0], L"COMMIT"))
582 transforms = std::move(deferred_transforms[channel_index()]);
586 SetReplyString(TEXT("404 MIXER ERROR\r\n"));
592 auto& defer_tranforms = deferred_transforms[channel_index()];
593 defer_tranforms.insert(defer_tranforms.end(), transforms.begin(), transforms.end());
596 channel()->stage().apply_transforms(transforms);
598 SetReplyString(TEXT("202 MIXER OK\r\n"));
602 catch(file_not_found&)
604 CASPAR_LOG_CURRENT_EXCEPTION();
605 SetReplyString(TEXT("404 MIXER ERROR\r\n"));
610 CASPAR_LOG_CURRENT_EXCEPTION();
611 SetReplyString(TEXT("502 MIXER FAILED\r\n"));
616 bool SwapCommand::DoExecute()
618 //Perform loading of the clip
621 if(layer_index(-1) != -1)
623 std::vector<std::string> strs;
624 boost::split(strs, parameters()[0], boost::is_any_of("-"));
626 auto ch1 = channel();
627 auto ch2 = channels().at(boost::lexical_cast<int>(strs.at(0))-1);
629 int l1 = layer_index();
630 int l2 = boost::lexical_cast<int>(strs.at(1));
632 ch1->stage().swap_layer(l1, l2, ch2.channel->stage());
636 auto ch1 = channel();
637 auto ch2 = channels().at(boost::lexical_cast<int>(parameters()[0])-1);
638 ch1->stage().swap_layers(ch2.channel->stage());
641 SetReplyString(TEXT("202 SWAP OK\r\n"));
645 catch(file_not_found&)
647 CASPAR_LOG_CURRENT_EXCEPTION();
648 SetReplyString(TEXT("404 SWAP ERROR\r\n"));
653 CASPAR_LOG_CURRENT_EXCEPTION();
654 SetReplyString(TEXT("502 SWAP FAILED\r\n"));
659 bool AddCommand::DoExecute()
661 //Perform loading of the clip
664 //create_consumer still expects all parameters to be uppercase
665 BOOST_FOREACH(std::wstring& str, parameters())
667 boost::to_upper(str);
670 auto consumer = create_consumer(parameters());
671 channel()->output().add(layer_index(consumer->index()), consumer);
673 SetReplyString(TEXT("202 ADD OK\r\n"));
677 catch(file_not_found&)
679 CASPAR_LOG_CURRENT_EXCEPTION();
680 SetReplyString(TEXT("404 ADD ERROR\r\n"));
685 CASPAR_LOG_CURRENT_EXCEPTION();
686 SetReplyString(TEXT("502 ADD FAILED\r\n"));
691 bool RemoveCommand::DoExecute()
693 //Perform loading of the clip
696 auto index = layer_index(std::numeric_limits<int>::min());
697 if(index == std::numeric_limits<int>::min())
699 //create_consumer still expects all parameters to be uppercase
700 BOOST_FOREACH(std::wstring& str, parameters())
702 boost::to_upper(str);
705 index = create_consumer(parameters())->index();
708 channel()->output().remove(index);
710 SetReplyString(TEXT("202 REMOVE OK\r\n"));
714 catch(file_not_found&)
716 CASPAR_LOG_CURRENT_EXCEPTION();
717 SetReplyString(TEXT("404 REMOVE ERROR\r\n"));
722 CASPAR_LOG_CURRENT_EXCEPTION();
723 SetReplyString(TEXT("502 REMOVE FAILED\r\n"));
728 bool LoadCommand::DoExecute()
730 //Perform loading of the clip
733 auto pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
734 channel()->stage().load(layer_index(), pFP, true);
736 SetReplyString(TEXT("202 LOAD OK\r\n"));
740 catch(file_not_found&)
742 CASPAR_LOG_CURRENT_EXCEPTION();
743 SetReplyString(TEXT("404 LOAD ERROR\r\n"));
748 CASPAR_LOG_CURRENT_EXCEPTION();
749 SetReplyString(TEXT("502 LOAD FAILED\r\n"));
756 //std::function<std::wstring()> channel_cg_add_command::parse(const std::wstring& message, const std::vector<renderer::render_device_ptr>& channels)
758 // static boost::wregex expr(L"^CG\\s(?<video_channel>\\d+)-?(?<LAYER>\\d+)?\\sADD\\s(?<FLASH_LAYER>\\d+)\\s(?<TEMPLATE>\\S+)\\s?(?<START_LABEL>\\S\\S+)?\\s?(?<PLAY_ON_LOAD>\\d)?\\s?(?<DATA>.*)?");
760 // boost::wsmatch what;
761 // if(!boost::regex_match(message, what, expr))
764 // auto info = channel_info::parse(what, channels);
766 // int flash_layer_index = boost::lexical_cast<int>(what["FLASH_LAYER"].str());
768 // std::wstring templatename = what["TEMPLATE"].str();
769 // bool play_on_load = what["PLAY_ON_LOAD"].matched ? what["PLAY_ON_LOAD"].str() != L"0" : 0;
770 // std::wstring start_label = what["START_LABEL"].str();
771 // std::wstring data = get_data(what["DATA"].str());
773 // boost::replace_all(templatename, "\"", "");
775 // return [=]() -> std::wstring
777 // std::wstring fullFilename = flash::flash_producer::find_template(server::template_folder() + templatename);
778 // if(fullFilename.empty())
779 // CASPAR_THROW_EXCEPTION(file_not_found());
781 // std::wstring extension = boost::filesystem::wpath(fullFilename).extension();
782 // std::wstring filename = templatename;
783 // filename.append(extension);
785 // flash::flash::create_cg_proxy(info.video_channel, std::max<int>(DEFAULT_CHANNEL_LAYER+1, info.layer_index))
786 // ->add(flash_layer_index, filename, play_on_load, start_label, data);
788 // CASPAR_LOG(info) << L"Executed [amcp_channel_cg_add]";
792 bool LoadbgCommand::DoExecute()
794 transition_info transitionInfo;
800 std::wstring message;
801 for(size_t n = 0; n < parameters().size(); ++n)
802 message += boost::to_upper_copy(parameters()[n]) + L" ";
804 static const boost::wregex expr(L".*(?<TRANSITION>CUT|PUSH|SLIDE|WIPE|MIX)\\s*(?<DURATION>\\d+)\\s*(?<TWEEN>(LINEAR)|(EASE[^\\s]*))?\\s*(?<DIRECTION>FROMLEFT|FROMRIGHT|LEFT|RIGHT)?.*");
806 if(boost::regex_match(message, what, expr))
808 auto transition = what["TRANSITION"].str();
809 transitionInfo.duration = boost::lexical_cast<size_t>(what["DURATION"].str());
810 auto direction = what["DIRECTION"].matched ? what["DIRECTION"].str() : L"";
811 auto tween = what["TWEEN"].matched ? what["TWEEN"].str() : L"";
812 transitionInfo.tweener = tween;
814 if(transition == TEXT("CUT"))
815 transitionInfo.type = transition_type::cut;
816 else if(transition == TEXT("MIX"))
817 transitionInfo.type = transition_type::mix;
818 else if(transition == TEXT("PUSH"))
819 transitionInfo.type = transition_type::push;
820 else if(transition == TEXT("SLIDE"))
821 transitionInfo.type = transition_type::slide;
822 else if(transition == TEXT("WIPE"))
823 transitionInfo.type = transition_type::wipe;
825 if(direction == TEXT("FROMLEFT"))
826 transitionInfo.direction = transition_direction::from_left;
827 else if(direction == TEXT("FROMRIGHT"))
828 transitionInfo.direction = transition_direction::from_right;
829 else if(direction == TEXT("LEFT"))
830 transitionInfo.direction = transition_direction::from_right;
831 else if(direction == TEXT("RIGHT"))
832 transitionInfo.direction = transition_direction::from_left;
835 //Perform loading of the clip
838 std::shared_ptr<core::frame_producer> pFP;
840 static boost::wregex expr(L"\\[(?<CHANNEL>\\d+)\\]", boost::regex::icase);
843 if(boost::regex_match(parameters().at(0), what, expr))
845 auto channel_index = boost::lexical_cast<int>(what["CHANNEL"].str());
846 pFP = reroute::create_producer(*channels().at(channel_index-1).channel);
850 pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
853 if(pFP == frame_producer::empty())
854 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info(parameters().size() > 0 ? parameters()[0] : L""));
856 bool auto_play = contains_param(L"AUTO", parameters());
858 auto pFP2 = create_transition_producer(channel()->video_format_desc().field_mode, spl::make_shared_ptr(pFP), transitionInfo);
860 channel()->stage().load(layer_index(), pFP2, false, transitionInfo.duration); // TODO: LOOP
862 channel()->stage().load(layer_index(), pFP2, false); // TODO: LOOP
865 SetReplyString(TEXT("202 LOADBG OK\r\n"));
869 catch(file_not_found&)
871 CASPAR_LOG(error) << L"File not found. No match found for parameters. Check syntax.";
872 SetReplyString(TEXT("404 LOADBG ERROR\r\n"));
877 CASPAR_LOG_CURRENT_EXCEPTION();
878 SetReplyString(TEXT("502 LOADBG FAILED\r\n"));
883 bool PauseCommand::DoExecute()
887 channel()->stage().pause(layer_index());
888 SetReplyString(TEXT("202 PAUSE OK\r\n"));
893 SetReplyString(TEXT("501 PAUSE FAILED\r\n"));
899 bool PlayCommand::DoExecute()
903 if(!parameters().empty())
905 LoadbgCommand lbg(*this);
908 throw std::exception();
911 channel()->stage().play(layer_index());
913 SetReplyString(TEXT("202 PLAY OK\r\n"));
918 SetReplyString(TEXT("501 PLAY FAILED\r\n"));
924 bool StopCommand::DoExecute()
928 channel()->stage().stop(layer_index());
929 SetReplyString(TEXT("202 STOP OK\r\n"));
934 SetReplyString(TEXT("501 STOP FAILED\r\n"));
940 bool ClearCommand::DoExecute()
942 int index = layer_index(std::numeric_limits<int>::min());
943 if(index != std::numeric_limits<int>::min())
944 channel()->stage().clear(index);
946 channel()->stage().clear();
948 SetReplyString(TEXT("202 CLEAR OK\r\n"));
953 bool PrintCommand::DoExecute()
955 channel()->output().add(create_consumer(boost::assign::list_of(L"IMAGE")));
957 SetReplyString(TEXT("202 PRINT OK\r\n"));
962 bool LogCommand::DoExecute()
964 if(boost::iequals(parameters().at(0), L"LEVEL"))
965 log::set_log_level(parameters().at(1));
967 SetReplyString(TEXT("202 LOG OK\r\n"));
972 bool CGCommand::DoExecute()
976 std::wstring command = boost::to_upper_copy(parameters()[0]);
977 if(command == TEXT("ADD"))
978 return DoExecuteAdd();
979 else if(command == TEXT("PLAY"))
980 return DoExecutePlay();
981 else if(command == TEXT("STOP"))
982 return DoExecuteStop();
983 else if(command == TEXT("NEXT"))
984 return DoExecuteNext();
985 else if(command == TEXT("REMOVE"))
986 return DoExecuteRemove();
987 else if(command == TEXT("CLEAR"))
988 return DoExecuteClear();
989 else if(command == TEXT("UPDATE"))
990 return DoExecuteUpdate();
991 else if(command == TEXT("INVOKE"))
992 return DoExecuteInvoke();
993 else if(command == TEXT("INFO"))
994 return DoExecuteInfo();
998 CASPAR_LOG_CURRENT_EXCEPTION();
1001 SetReplyString(TEXT("403 CG ERROR\r\n"));
1005 bool CGCommand::ValidateLayer(const std::wstring& layerstring) {
1006 int length = layerstring.length();
1007 for(int i = 0; i < length; ++i) {
1008 if(!_istdigit(layerstring[i])) {
1016 bool CGCommand::DoExecuteAdd() {
1017 //CG 1 ADD 0 "template_folder/templatename" [STARTLABEL] 0/1 [DATA]
1019 int layer = 0; //_parameters[1]
1020 // std::wstring templateName; //_parameters[2]
1021 std::wstring label; //_parameters[3]
1022 bool bDoStart = false; //_parameters[3] alt. _parameters[4]
1023 // std::wstring data; //_parameters[4] alt. _parameters[5]
1025 if(parameters().size() < 4)
1027 SetReplyString(TEXT("402 CG ERROR\r\n"));
1030 unsigned int dataIndex = 4;
1032 if(!ValidateLayer(parameters()[1]))
1034 SetReplyString(TEXT("403 CG ERROR\r\n"));
1038 layer = boost::lexical_cast<int>(parameters()[1]);
1040 if(parameters()[3].length() > 1)
1042 label = parameters()[3];
1045 if(parameters().size() > 4 && parameters()[4].length() > 0) //read play-on-load-flag
1046 bDoStart = (parameters()[4][0]==TEXT('1')) ? true : false;
1049 SetReplyString(TEXT("402 CG ERROR\r\n"));
1053 else if(parameters()[3].length() > 0) { //read play-on-load-flag
1054 bDoStart = (parameters()[3][0]==TEXT('1')) ? true : false;
1058 SetReplyString(TEXT("403 CG ERROR\r\n"));
1062 const TCHAR* pDataString = 0;
1063 std::wstringstream data;
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<wchar_t> 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::filesystem3::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 waiting for keypress
1961 SetReplyString(TEXT("202 KILL OK\r\n"));
1966 }} //namespace caspar