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()
318 auto self = channels().back().channel;
320 std::vector<std::wstring> params;
321 params.push_back(L"SCREEN");
322 params.push_back(L"0");
323 params.push_back(L"NAME");
324 params.push_back(L"Channel Grid Window");
325 auto screen = create_consumer(params);
327 self->output().add(screen);
329 BOOST_FOREACH(auto channel, channels())
331 if(channel.channel != self)
333 auto producer = reroute::create_producer(*channel.channel);
334 self->stage().load(index, producer, false);
335 self->stage().play(index);
340 int n = channels().size()-1;
341 double delta = 1.0/static_cast<double>(n);
342 for(int x = 0; x < n; ++x)
344 for(int y = 0; y < n; ++y)
347 auto transform = [=](frame_transform transform) -> frame_transform
349 transform.image_transform.fill_translation[0] = x*delta;
350 transform.image_transform.fill_translation[1] = y*delta;
351 transform.image_transform.fill_scale[0] = delta;
352 transform.image_transform.fill_scale[1] = delta;
353 transform.image_transform.clip_translation[0] = x*delta;
354 transform.image_transform.clip_translation[1] = y*delta;
355 transform.image_transform.clip_scale[0] = delta;
356 transform.image_transform.clip_scale[1] = delta;
359 self->stage().apply_transform(index, transform);
366 bool CallCommand::DoExecute()
368 //Perform loading of the clip
371 auto result = channel()->stage().call(layer_index(), parameters());
373 if(!result.timed_wait(boost::posix_time::seconds(2)))
374 CASPAR_THROW_EXCEPTION(timed_out());
376 std::wstringstream replyString;
377 if(result.get().empty())
378 replyString << TEXT("202 CALL OK\r\n");
380 replyString << TEXT("201 CALL OK\r\n") << result.get() << L"\r\n";
382 SetReplyString(replyString.str());
388 CASPAR_LOG_CURRENT_EXCEPTION();
389 SetReplyString(TEXT("502 CALL FAILED\r\n"));
394 tbb::concurrent_unordered_map<int, std::vector<stage::transform_tuple_t>> deferred_transforms;
396 bool MixerCommand::DoExecute()
398 //Perform loading of the clip
401 bool defer = boost::iequals(parameters().back(), L"DEFER");
403 parameters().pop_back();
405 std::vector<stage::transform_tuple_t> transforms;
407 if(boost::iequals(parameters()[0], L"KEYER") || boost::iequals(parameters()[0], L"IS_KEY"))
409 bool value = boost::lexical_cast<int>(parameters().at(1));
410 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
412 transform.image_transform.is_key = value;
416 else if(boost::iequals(parameters()[0], L"OPACITY"))
418 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
419 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
421 double value = boost::lexical_cast<double>(parameters().at(1));
423 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
425 transform.image_transform.opacity = value;
427 }, duration, tween));
429 else if(boost::iequals(parameters()[0], L"FILL") || boost::iequals(parameters()[0], L"FILL_RECT"))
431 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
432 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
433 double x = boost::lexical_cast<double>(parameters().at(1));
434 double y = boost::lexical_cast<double>(parameters().at(2));
435 double x_s = boost::lexical_cast<double>(parameters().at(3));
436 double y_s = boost::lexical_cast<double>(parameters().at(4));
438 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) mutable -> frame_transform
440 transform.image_transform.fill_translation[0] = x;
441 transform.image_transform.fill_translation[1] = y;
442 transform.image_transform.fill_scale[0] = x_s;
443 transform.image_transform.fill_scale[1] = y_s;
445 }, duration, tween));
447 else if(boost::iequals(parameters()[0], L"CLIP") || boost::iequals(parameters()[0], L"CLIP_RECT"))
449 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
450 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
451 double x = boost::lexical_cast<double>(parameters().at(1));
452 double y = boost::lexical_cast<double>(parameters().at(2));
453 double x_s = boost::lexical_cast<double>(parameters().at(3));
454 double y_s = boost::lexical_cast<double>(parameters().at(4));
456 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
458 transform.image_transform.clip_translation[0] = x;
459 transform.image_transform.clip_translation[1] = y;
460 transform.image_transform.clip_scale[0] = x_s;
461 transform.image_transform.clip_scale[1] = y_s;
463 }, duration, tween));
465 else if(boost::iequals(parameters()[0], L"GRID"))
467 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
468 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
469 int n = boost::lexical_cast<int>(parameters().at(1));
470 double delta = 1.0/static_cast<double>(n);
471 for(int x = 0; x < n; ++x)
473 for(int y = 0; y < n; ++y)
476 transforms.push_back(stage::transform_tuple_t(index, [=](frame_transform transform) -> frame_transform
478 transform.image_transform.fill_translation[0] = x*delta;
479 transform.image_transform.fill_translation[1] = y*delta;
480 transform.image_transform.fill_scale[0] = delta;
481 transform.image_transform.fill_scale[1] = delta;
482 transform.image_transform.clip_translation[0] = x*delta;
483 transform.image_transform.clip_translation[1] = y*delta;
484 transform.image_transform.clip_scale[0] = delta;
485 transform.image_transform.clip_scale[1] = delta;
487 }, duration, tween));
491 else if(boost::iequals(parameters()[0], L"BLEND"))
493 auto blend_str = parameters().at(1);
494 int layer = layer_index();
495 channel()->mixer().set_blend_mode(layer_index(), get_blend_mode(blend_str));
497 else if(boost::iequals(parameters()[0], L"MASTERVOLUME"))
499 float master_volume = boost::lexical_cast<float>(parameters().at(1));
500 channel()->mixer().set_master_volume(master_volume);
502 else if(boost::iequals(parameters()[0], L"BRIGHTNESS"))
504 auto value = boost::lexical_cast<double>(parameters().at(1));
505 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
506 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
507 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
509 transform.image_transform.brightness = value;
511 }, duration, tween));
513 else if(boost::iequals(parameters()[0], L"SATURATION"))
515 auto value = boost::lexical_cast<double>(parameters().at(1));
516 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
517 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
518 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
520 transform.image_transform.saturation = value;
522 }, duration, tween));
524 else if(parameters()[0] == L"CONTRAST")
526 auto value = boost::lexical_cast<double>(parameters().at(1));
527 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
528 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
529 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
531 transform.image_transform.contrast = value;
533 }, duration, tween));
535 else if(boost::iequals(parameters()[0], L"LEVELS"))
538 value.min_input = boost::lexical_cast<double>(parameters().at(1));
539 value.max_input = boost::lexical_cast<double>(parameters().at(2));
540 value.gamma = boost::lexical_cast<double>(parameters().at(3));
541 value.min_output = boost::lexical_cast<double>(parameters().at(4));
542 value.max_output = boost::lexical_cast<double>(parameters().at(5));
543 int duration = parameters().size() > 6 ? boost::lexical_cast<int>(parameters()[6]) : 0;
544 std::wstring tween = parameters().size() > 7 ? parameters()[7] : L"linear";
546 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
548 transform.image_transform.levels = value;
550 }, duration, tween));
552 else if(boost::iequals(parameters()[0], L"VOLUME"))
554 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
555 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
556 double value = boost::lexical_cast<double>(parameters()[1]);
558 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
560 transform.audio_transform.volume = value;
562 }, duration, tween));
564 else if(boost::iequals(parameters()[0], L"CLEAR"))
566 int layer = layer_index(std::numeric_limits<int>::max());
568 if (layer == std::numeric_limits<int>::max())
570 channel()->stage().clear_transforms();
571 channel()->mixer().clear_blend_modes();
575 channel()->stage().clear_transforms(layer);
576 channel()->mixer().clear_blend_mode(layer);
579 else if(boost::iequals(parameters()[0], L"COMMIT"))
581 transforms = std::move(deferred_transforms[channel_index()]);
585 SetReplyString(TEXT("404 MIXER ERROR\r\n"));
591 auto& defer_tranforms = deferred_transforms[channel_index()];
592 defer_tranforms.insert(defer_tranforms.end(), transforms.begin(), transforms.end());
595 channel()->stage().apply_transforms(transforms);
597 SetReplyString(TEXT("202 MIXER OK\r\n"));
601 catch(file_not_found&)
603 CASPAR_LOG_CURRENT_EXCEPTION();
604 SetReplyString(TEXT("404 MIXER ERROR\r\n"));
609 CASPAR_LOG_CURRENT_EXCEPTION();
610 SetReplyString(TEXT("502 MIXER FAILED\r\n"));
615 bool SwapCommand::DoExecute()
617 //Perform loading of the clip
620 if(layer_index(-1) != -1)
622 std::vector<std::string> strs;
623 boost::split(strs, parameters()[0], boost::is_any_of("-"));
625 auto ch1 = channel();
626 auto ch2 = channels().at(boost::lexical_cast<int>(strs.at(0))-1);
628 int l1 = layer_index();
629 int l2 = boost::lexical_cast<int>(strs.at(1));
631 ch1->stage().swap_layer(l1, l2, ch2.channel->stage());
635 auto ch1 = channel();
636 auto ch2 = channels().at(boost::lexical_cast<int>(parameters()[0])-1);
637 ch1->stage().swap_layers(ch2.channel->stage());
640 SetReplyString(TEXT("202 SWAP OK\r\n"));
644 catch(file_not_found&)
646 CASPAR_LOG_CURRENT_EXCEPTION();
647 SetReplyString(TEXT("404 SWAP ERROR\r\n"));
652 CASPAR_LOG_CURRENT_EXCEPTION();
653 SetReplyString(TEXT("502 SWAP FAILED\r\n"));
658 bool AddCommand::DoExecute()
660 //Perform loading of the clip
663 //create_consumer still expects all parameters to be uppercase
664 BOOST_FOREACH(std::wstring& str, parameters())
666 boost::to_upper(str);
669 auto consumer = create_consumer(parameters());
670 channel()->output().add(layer_index(consumer->index()), consumer);
672 SetReplyString(TEXT("202 ADD OK\r\n"));
676 catch(file_not_found&)
678 CASPAR_LOG_CURRENT_EXCEPTION();
679 SetReplyString(TEXT("404 ADD ERROR\r\n"));
684 CASPAR_LOG_CURRENT_EXCEPTION();
685 SetReplyString(TEXT("502 ADD FAILED\r\n"));
690 bool RemoveCommand::DoExecute()
692 //Perform loading of the clip
695 auto index = layer_index(std::numeric_limits<int>::min());
696 if(index == std::numeric_limits<int>::min())
698 //create_consumer still expects all parameters to be uppercase
699 BOOST_FOREACH(std::wstring& str, parameters())
701 boost::to_upper(str);
704 index = create_consumer(parameters())->index();
707 channel()->output().remove(index);
709 SetReplyString(TEXT("202 REMOVE OK\r\n"));
713 catch(file_not_found&)
715 CASPAR_LOG_CURRENT_EXCEPTION();
716 SetReplyString(TEXT("404 REMOVE ERROR\r\n"));
721 CASPAR_LOG_CURRENT_EXCEPTION();
722 SetReplyString(TEXT("502 REMOVE FAILED\r\n"));
727 bool LoadCommand::DoExecute()
729 //Perform loading of the clip
732 auto pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
733 channel()->stage().load(layer_index(), pFP, true);
735 SetReplyString(TEXT("202 LOAD OK\r\n"));
739 catch(file_not_found&)
741 CASPAR_LOG_CURRENT_EXCEPTION();
742 SetReplyString(TEXT("404 LOAD ERROR\r\n"));
747 CASPAR_LOG_CURRENT_EXCEPTION();
748 SetReplyString(TEXT("502 LOAD FAILED\r\n"));
755 //std::function<std::wstring()> channel_cg_add_command::parse(const std::wstring& message, const std::vector<renderer::render_device_ptr>& channels)
757 // 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>.*)?");
759 // boost::wsmatch what;
760 // if(!boost::regex_match(message, what, expr))
763 // auto info = channel_info::parse(what, channels);
765 // int flash_layer_index = boost::lexical_cast<int>(what["FLASH_LAYER"].str());
767 // std::wstring templatename = what["TEMPLATE"].str();
768 // bool play_on_load = what["PLAY_ON_LOAD"].matched ? what["PLAY_ON_LOAD"].str() != L"0" : 0;
769 // std::wstring start_label = what["START_LABEL"].str();
770 // std::wstring data = get_data(what["DATA"].str());
772 // boost::replace_all(templatename, "\"", "");
774 // return [=]() -> std::wstring
776 // std::wstring fullFilename = flash::flash_producer::find_template(server::template_folder() + templatename);
777 // if(fullFilename.empty())
778 // CASPAR_THROW_EXCEPTION(file_not_found());
780 // std::wstring extension = boost::filesystem::wpath(fullFilename).extension();
781 // std::wstring filename = templatename;
782 // filename.append(extension);
784 // flash::flash::create_cg_proxy(info.video_channel, std::max<int>(DEFAULT_CHANNEL_LAYER+1, info.layer_index))
785 // ->add(flash_layer_index, filename, play_on_load, start_label, data);
787 // CASPAR_LOG(info) << L"Executed [amcp_channel_cg_add]";
791 bool LoadbgCommand::DoExecute()
793 transition_info transitionInfo;
799 std::wstring message;
800 for(size_t n = 0; n < parameters().size(); ++n)
801 message += boost::to_upper_copy(parameters()[n]) + L" ";
803 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)?.*");
805 if(boost::regex_match(message, what, expr))
807 auto transition = what["TRANSITION"].str();
808 transitionInfo.duration = boost::lexical_cast<size_t>(what["DURATION"].str());
809 auto direction = what["DIRECTION"].matched ? what["DIRECTION"].str() : L"";
810 auto tween = what["TWEEN"].matched ? what["TWEEN"].str() : L"";
811 transitionInfo.tweener = tween;
813 if(transition == TEXT("CUT"))
814 transitionInfo.type = transition_type::cut;
815 else if(transition == TEXT("MIX"))
816 transitionInfo.type = transition_type::mix;
817 else if(transition == TEXT("PUSH"))
818 transitionInfo.type = transition_type::push;
819 else if(transition == TEXT("SLIDE"))
820 transitionInfo.type = transition_type::slide;
821 else if(transition == TEXT("WIPE"))
822 transitionInfo.type = transition_type::wipe;
824 if(direction == TEXT("FROMLEFT"))
825 transitionInfo.direction = transition_direction::from_left;
826 else if(direction == TEXT("FROMRIGHT"))
827 transitionInfo.direction = transition_direction::from_right;
828 else if(direction == TEXT("LEFT"))
829 transitionInfo.direction = transition_direction::from_right;
830 else if(direction == TEXT("RIGHT"))
831 transitionInfo.direction = transition_direction::from_left;
834 //Perform loading of the clip
837 std::shared_ptr<core::frame_producer> pFP;
839 static boost::wregex expr(L"\\[(?<CHANNEL>\\d+)\\]", boost::regex::icase);
842 if(boost::regex_match(parameters().at(0), what, expr))
844 auto channel_index = boost::lexical_cast<int>(what["CHANNEL"].str());
845 pFP = reroute::create_producer(*channels().at(channel_index-1).channel);
849 pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
852 if(pFP == frame_producer::empty())
853 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info(parameters().size() > 0 ? parameters()[0] : L""));
855 bool auto_play = contains_param(L"AUTO", parameters());
857 auto pFP2 = create_transition_producer(channel()->video_format_desc().field_mode, spl::make_shared_ptr(pFP), transitionInfo);
859 channel()->stage().load(layer_index(), pFP2, false, transitionInfo.duration); // TODO: LOOP
861 channel()->stage().load(layer_index(), pFP2, false); // TODO: LOOP
864 SetReplyString(TEXT("202 LOADBG OK\r\n"));
868 catch(file_not_found&)
870 CASPAR_LOG(error) << L"File not found. No match found for parameters. Check syntax.";
871 SetReplyString(TEXT("404 LOADBG ERROR\r\n"));
876 CASPAR_LOG_CURRENT_EXCEPTION();
877 SetReplyString(TEXT("502 LOADBG FAILED\r\n"));
882 bool PauseCommand::DoExecute()
886 channel()->stage().pause(layer_index());
887 SetReplyString(TEXT("202 PAUSE OK\r\n"));
892 SetReplyString(TEXT("501 PAUSE FAILED\r\n"));
898 bool PlayCommand::DoExecute()
902 if(!parameters().empty())
904 LoadbgCommand lbg(*this);
907 throw std::exception();
910 channel()->stage().play(layer_index());
912 SetReplyString(TEXT("202 PLAY OK\r\n"));
917 SetReplyString(TEXT("501 PLAY FAILED\r\n"));
923 bool StopCommand::DoExecute()
927 channel()->stage().stop(layer_index());
928 SetReplyString(TEXT("202 STOP OK\r\n"));
933 SetReplyString(TEXT("501 STOP FAILED\r\n"));
939 bool ClearCommand::DoExecute()
941 int index = layer_index(std::numeric_limits<int>::min());
942 if(index != std::numeric_limits<int>::min())
943 channel()->stage().clear(index);
945 channel()->stage().clear();
947 SetReplyString(TEXT("202 CLEAR OK\r\n"));
952 bool PrintCommand::DoExecute()
954 channel()->output().add(create_consumer(boost::assign::list_of(L"IMAGE")));
956 SetReplyString(TEXT("202 PRINT OK\r\n"));
961 bool LogCommand::DoExecute()
963 if(boost::iequals(parameters().at(0), L"LEVEL"))
964 log::set_log_level(parameters().at(1));
966 SetReplyString(TEXT("202 LOG OK\r\n"));
971 bool CGCommand::DoExecute()
975 std::wstring command = boost::to_upper_copy(parameters()[0]);
976 if(command == TEXT("ADD"))
977 return DoExecuteAdd();
978 else if(command == TEXT("PLAY"))
979 return DoExecutePlay();
980 else if(command == TEXT("STOP"))
981 return DoExecuteStop();
982 else if(command == TEXT("NEXT"))
983 return DoExecuteNext();
984 else if(command == TEXT("REMOVE"))
985 return DoExecuteRemove();
986 else if(command == TEXT("CLEAR"))
987 return DoExecuteClear();
988 else if(command == TEXT("UPDATE"))
989 return DoExecuteUpdate();
990 else if(command == TEXT("INVOKE"))
991 return DoExecuteInvoke();
992 else if(command == TEXT("INFO"))
993 return DoExecuteInfo();
997 CASPAR_LOG_CURRENT_EXCEPTION();
1000 SetReplyString(TEXT("403 CG ERROR\r\n"));
1004 bool CGCommand::ValidateLayer(const std::wstring& layerstring) {
1005 int length = layerstring.length();
1006 for(int i = 0; i < length; ++i) {
1007 if(!_istdigit(layerstring[i])) {
1015 bool CGCommand::DoExecuteAdd() {
1016 //CG 1 ADD 0 "template_folder/templatename" [STARTLABEL] 0/1 [DATA]
1018 int layer = 0; //_parameters[1]
1019 // std::wstring templateName; //_parameters[2]
1020 std::wstring label; //_parameters[3]
1021 bool bDoStart = false; //_parameters[3] alt. _parameters[4]
1022 // std::wstring data; //_parameters[4] alt. _parameters[5]
1024 if(parameters().size() < 4)
1026 SetReplyString(TEXT("402 CG ERROR\r\n"));
1029 unsigned int dataIndex = 4;
1031 if(!ValidateLayer(parameters()[1]))
1033 SetReplyString(TEXT("403 CG ERROR\r\n"));
1037 layer = boost::lexical_cast<int>(parameters()[1]);
1039 if(parameters()[3].length() > 1)
1041 label = parameters()[3];
1044 if(parameters().size() > 4 && parameters()[4].length() > 0) //read play-on-load-flag
1045 bDoStart = (parameters()[4][0]==TEXT('1')) ? true : false;
1048 SetReplyString(TEXT("402 CG ERROR\r\n"));
1052 else if(parameters()[3].length() > 0) { //read play-on-load-flag
1053 bDoStart = (parameters()[3][0]==TEXT('1')) ? true : false;
1057 SetReplyString(TEXT("403 CG ERROR\r\n"));
1061 const TCHAR* pDataString = 0;
1062 std::wstringstream data;
1063 std::wstring dataFromFile;
1064 if(parameters().size() > dataIndex)
1066 const std::wstring& dataString = parameters()[dataIndex];
1068 if(dataString[0] == TEXT('<')) //the data is an XML-string
1069 pDataString = dataString.c_str();
1072 //The data is not an XML-string, it must be a filename
1073 std::wstring filename = env::data_folder();
1074 filename.append(dataString);
1075 filename.append(TEXT(".ftd"));
1077 dataFromFile = read_file(boost::filesystem::wpath(filename));
1078 pDataString = dataFromFile.c_str();
1082 std::wstring fullFilename = flash::find_template(env::template_folder() + parameters()[2]);
1083 if(!fullFilename.empty())
1085 std::wstring extension = boost::filesystem::path(fullFilename).extension().wstring();
1086 std::wstring filename = parameters()[2];
1087 filename.append(extension);
1089 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(""));
1090 SetReplyString(TEXT("202 CG OK\r\n"));
1094 CASPAR_LOG(warning) << "Could not find template " << parameters()[2];
1095 SetReplyString(TEXT("404 CG ERROR\r\n"));
1100 bool CGCommand::DoExecutePlay()
1102 if(parameters().size() > 1)
1104 if(!ValidateLayer(parameters()[1]))
1106 SetReplyString(TEXT("403 CG ERROR\r\n"));
1109 int layer = boost::lexical_cast<int>(parameters()[1]);
1110 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).play(layer);
1114 SetReplyString(TEXT("402 CG ERROR\r\n"));
1118 SetReplyString(TEXT("202 CG OK\r\n"));
1122 bool CGCommand::DoExecuteStop()
1124 if(parameters().size() > 1)
1126 if(!ValidateLayer(parameters()[1]))
1128 SetReplyString(TEXT("403 CG ERROR\r\n"));
1131 int layer = boost::lexical_cast<int>(parameters()[1]);
1132 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).stop(layer, 0);
1136 SetReplyString(TEXT("402 CG ERROR\r\n"));
1140 SetReplyString(TEXT("202 CG OK\r\n"));
1144 bool CGCommand::DoExecuteNext()
1146 if(parameters().size() > 1)
1148 if(!ValidateLayer(parameters()[1]))
1150 SetReplyString(TEXT("403 CG ERROR\r\n"));
1154 int layer = boost::lexical_cast<int>(parameters()[1]);
1155 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).next(layer);
1159 SetReplyString(TEXT("402 CG ERROR\r\n"));
1163 SetReplyString(TEXT("202 CG OK\r\n"));
1167 bool CGCommand::DoExecuteRemove()
1169 if(parameters().size() > 1)
1171 if(!ValidateLayer(parameters()[1]))
1173 SetReplyString(TEXT("403 CG ERROR\r\n"));
1177 int layer = boost::lexical_cast<int>(parameters()[1]);
1178 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).remove(layer);
1182 SetReplyString(TEXT("402 CG ERROR\r\n"));
1186 SetReplyString(TEXT("202 CG OK\r\n"));
1190 bool CGCommand::DoExecuteClear()
1192 channel()->stage().clear(layer_index(flash::cg_proxy::DEFAULT_LAYER));
1193 SetReplyString(TEXT("202 CG OK\r\n"));
1197 bool CGCommand::DoExecuteUpdate()
1201 if(!ValidateLayer(parameters().at(1)))
1203 SetReplyString(TEXT("403 CG ERROR\r\n"));
1207 std::wstring dataString = parameters().at(2);
1208 if(dataString.at(0) != TEXT('<'))
1210 //The data is not an XML-string, it must be a filename
1211 std::wstring filename = env::data_folder();
1212 filename.append(dataString);
1213 filename.append(TEXT(".ftd"));
1215 dataString = read_file(boost::filesystem::wpath(filename));
1218 int layer = boost::lexical_cast<int>(parameters()[1]);
1219 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).update(layer, dataString);
1223 SetReplyString(TEXT("402 CG ERROR\r\n"));
1227 SetReplyString(TEXT("202 CG OK\r\n"));
1231 bool CGCommand::DoExecuteInvoke()
1233 std::wstringstream replyString;
1234 replyString << TEXT("201 CG OK\r\n");
1236 if(parameters().size() > 2)
1238 if(!ValidateLayer(parameters()[1]))
1240 SetReplyString(TEXT("403 CG ERROR\r\n"));
1243 int layer = boost::lexical_cast<int>(parameters()[1]);
1244 auto result = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).invoke(layer, parameters()[2]);
1245 replyString << result << TEXT("\r\n");
1249 SetReplyString(TEXT("402 CG ERROR\r\n"));
1253 SetReplyString(replyString.str());
1257 bool CGCommand::DoExecuteInfo()
1259 std::wstringstream replyString;
1260 replyString << TEXT("201 CG OK\r\n");
1262 if(parameters().size() > 1)
1264 if(!ValidateLayer(parameters()[1]))
1266 SetReplyString(TEXT("403 CG ERROR\r\n"));
1270 int layer = boost::lexical_cast<int>(parameters()[1]);
1271 auto desc = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).description(layer);
1273 replyString << desc << TEXT("\r\n");
1277 auto info = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).template_host_info();
1278 replyString << info << TEXT("\r\n");
1281 SetReplyString(replyString.str());
1285 bool DataCommand::DoExecute()
1287 std::wstring command = boost::to_upper_copy(parameters()[0]);
1288 if(command == TEXT("STORE"))
1289 return DoExecuteStore();
1290 else if(command == TEXT("RETRIEVE"))
1291 return DoExecuteRetrieve();
1292 else if(command == TEXT("REMOVE"))
1293 return DoExecuteRemove();
1294 else if(command == TEXT("LIST"))
1295 return DoExecuteList();
1297 SetReplyString(TEXT("403 DATA ERROR\r\n"));
1301 bool DataCommand::DoExecuteStore()
1303 if(parameters().size() < 3)
1305 SetReplyString(TEXT("402 DATA STORE ERROR\r\n"));
1309 std::wstring filename = env::data_folder();
1310 filename.append(parameters()[1]);
1311 filename.append(TEXT(".ftd"));
1313 auto data_path = boost::filesystem::wpath(
1314 boost::filesystem::wpath(filename).parent_path());
1316 if(!boost::filesystem::exists(data_path))
1317 boost::filesystem::create_directories(data_path);
1319 std::wofstream datafile(filename.c_str());
1322 SetReplyString(TEXT("501 DATA STORE FAILED\r\n"));
1326 datafile << static_cast<wchar_t>(65279); // UTF-8 BOM character
1327 datafile << parameters()[2] << std::flush;
1330 std::wstring replyString = TEXT("202 DATA STORE OK\r\n");
1331 SetReplyString(replyString);
1335 bool DataCommand::DoExecuteRetrieve()
1337 if(parameters().size() < 2)
1339 SetReplyString(TEXT("402 DATA RETRIEVE ERROR\r\n"));
1343 std::wstring filename = env::data_folder();
1344 filename.append(parameters()[1]);
1345 filename.append(TEXT(".ftd"));
1347 std::wstring file_contents = read_file(boost::filesystem::wpath(filename));
1349 if (file_contents.empty())
1351 SetReplyString(TEXT("404 DATA RETRIEVE ERROR\r\n"));
1355 std::wstringstream reply(TEXT("201 DATA RETRIEVE OK\r\n"));
1357 std::wstringstream file_contents_stream(file_contents);
1360 bool firstLine = true;
1361 while(std::getline(file_contents_stream, line))
1372 SetReplyString(reply.str());
1376 bool DataCommand::DoExecuteRemove()
1378 if (parameters().size() < 2)
1380 SetReplyString(TEXT("402 DATA REMOVE ERROR\r\n"));
1384 std::wstring filename = env::data_folder();
1385 filename.append(parameters()[1]);
1386 filename.append(TEXT(".ftd"));
1388 if (!boost::filesystem::exists(filename))
1390 SetReplyString(TEXT("404 DATA REMOVE ERROR\r\n"));
1394 if (!boost::filesystem::remove(filename))
1396 SetReplyString(TEXT("403 DATA REMOVE ERROR\r\n"));
1400 SetReplyString(TEXT("201 DATA REMOVE OK\r\n"));
1405 bool DataCommand::DoExecuteList()
1407 std::wstringstream replyString;
1408 replyString << TEXT("200 DATA LIST OK\r\n");
1410 for (boost::filesystem::recursive_directory_iterator itr(env::data_folder()), end; itr != end; ++itr)
1412 if(boost::filesystem::is_regular_file(itr->path()))
1414 if(!boost::iequals(itr->path().extension().wstring(), L".ftd"))
1417 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::data_folder().size()-1, itr->path().wstring().size()));
1419 auto str = relativePath.replace_extension(TEXT("")).native();
1420 if(str[0] == '\\' || str[0] == '/')
1421 str = std::wstring(str.begin() + 1, str.end());
1423 replyString << str << TEXT("\r\n");
1427 replyString << TEXT("\r\n");
1429 SetReplyString(boost::to_upper_copy(replyString.str()));
1433 bool CinfCommand::DoExecute()
1435 std::wstringstream replyString;
1440 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end && info.empty(); ++itr)
1442 auto path = itr->path();
1443 auto file = path.replace_extension(L"").filename();
1444 if(boost::iequals(file.wstring(), parameters().at(0)))
1445 info += MediaInfo(itr->path()) + L"\r\n";
1450 SetReplyString(TEXT("404 CINF ERROR\r\n"));
1453 replyString << TEXT("200 CINF OK\r\n");
1454 replyString << info << "\r\n";
1458 CASPAR_LOG_CURRENT_EXCEPTION();
1459 SetReplyString(TEXT("404 CINF ERROR\r\n"));
1463 SetReplyString(replyString.str());
1467 void GenerateChannelInfo(int index, const spl::shared_ptr<core::video_channel>& pChannel, std::wstringstream& replyString)
1469 replyString << index+1 << TEXT(" ") << pChannel->video_format_desc().name << TEXT(" PLAYING") << TEXT("\r\n");
1472 bool InfoCommand::DoExecute()
1474 std::wstringstream replyString;
1476 boost::property_tree::xml_writer_settings<wchar_t> w(' ', 3);
1480 if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"TEMPLATE"))
1482 replyString << L"201 INFO TEMPLATE OK\r\n";
1484 // Needs to be extended for any file, not just flash.
1486 auto filename = flash::find_template(env::template_folder() + parameters().at(1));
1488 std::wstringstream str;
1489 str << u16(flash::read_template_meta_info(filename));
1490 boost::property_tree::wptree info;
1491 boost::property_tree::xml_parser::read_xml(str, info, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);
1493 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1495 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"CONFIG"))
1497 replyString << L"201 INFO CONFIG OK\r\n";
1499 boost::property_tree::write_xml(replyString, caspar::env::properties(), w);
1501 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"PATHS"))
1503 replyString << L"201 INFO PATHS OK\r\n";
1505 boost::property_tree::wptree info;
1506 info.add_child(L"paths", caspar::env::properties().get_child(L"configuration.paths"));
1507 info.add(L"paths.initial-path", boost::filesystem3::initial_path().wstring() + L"\\");
1509 boost::property_tree::write_xml(replyString, info, w);
1511 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SYSTEM"))
1513 replyString << L"201 INFO SYSTEM OK\r\n";
1515 boost::property_tree::wptree info;
1517 info.add(L"system.name", caspar::system_product_name());
1518 info.add(L"system.windows.name", caspar::win_product_name());
1519 info.add(L"system.windows.service-pack", caspar::win_sp_version());
1520 info.add(L"system.cpu", caspar::cpu_info());
1522 BOOST_FOREACH(auto device, caspar::decklink::device_list())
1523 info.add(L"system.decklink.device", device);
1525 BOOST_FOREACH(auto device, caspar::bluefish::device_list())
1526 info.add(L"system.bluefish.device", device);
1528 info.add(L"system.flash", caspar::flash::version());
1529 //info.add(L"system.free-image", caspar::image::version());
1530 info.add(L"system.ffmpeg.avcodec", caspar::ffmpeg::avcodec_version());
1531 info.add(L"system.ffmpeg.avformat", caspar::ffmpeg::avformat_version());
1532 info.add(L"system.ffmpeg.avfilter", caspar::ffmpeg::avfilter_version());
1533 info.add(L"system.ffmpeg.avutil", caspar::ffmpeg::avutil_version());
1534 info.add(L"system.ffmpeg.swscale", caspar::ffmpeg::swscale_version());
1536 boost::property_tree::write_xml(replyString, info, w);
1538 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SERVER"))
1540 replyString << L"201 INFO SERVER OK\r\n";
1542 boost::property_tree::wptree info;
1545 BOOST_FOREACH(auto channel, channels())
1546 info.add_child(L"channels.channel", channel.channel->info())
1547 .add(L"index", ++index);
1549 boost::property_tree::write_xml(replyString, info, w);
1553 if(parameters().size() >= 1)
1555 replyString << TEXT("201 INFO OK\r\n");
1556 boost::property_tree::wptree info;
1558 std::vector<std::wstring> split;
1559 boost::split(split, parameters()[0], boost::is_any_of("-"));
1561 int layer = std::numeric_limits<int>::min();
1562 int channel = boost::lexical_cast<int>(split[0]) - 1;
1564 if(split.size() > 1)
1565 layer = boost::lexical_cast<int>(split[1]);
1567 if(layer == std::numeric_limits<int>::min())
1569 info.add_child(L"channel", channels().at(channel).channel->info())
1570 .add(L"index", channel);
1574 if(parameters().size() >= 2)
1576 if(boost::iequals(parameters()[1], L"B"))
1577 info.add_child(L"producer", channels().at(channel).channel->stage().background(layer).get()->info());
1579 info.add_child(L"producer", channels().at(channel).channel->stage().foreground(layer).get()->info());
1583 info.add_child(L"layer", channels().at(channel).channel->stage().info(layer).get())
1584 .add(L"index", layer);
1587 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1591 // This is needed for backwards compatibility with old clients
1592 replyString << TEXT("200 INFO OK\r\n");
1593 for(size_t n = 0; n < channels().size(); ++n)
1594 GenerateChannelInfo(n, channels()[n].channel, replyString);
1601 CASPAR_LOG_CURRENT_EXCEPTION();
1602 SetReplyString(TEXT("403 INFO ERROR\r\n"));
1606 replyString << TEXT("\r\n");
1607 SetReplyString(replyString.str());
1611 bool ClsCommand::DoExecute()
1623 std::wstringstream replyString;
1624 replyString << TEXT("200 CLS OK\r\n");
1625 replyString << ListMedia();
1626 replyString << TEXT("\r\n");
1627 SetReplyString(boost::to_upper_copy(replyString.str()));
1631 CASPAR_LOG_CURRENT_EXCEPTION();
1632 SetReplyString(TEXT("501 CLS FAILED\r\n"));
1639 bool TlsCommand::DoExecute()
1643 std::wstringstream replyString;
1644 replyString << TEXT("200 TLS OK\r\n");
1646 replyString << ListTemplates();
1647 replyString << TEXT("\r\n");
1649 SetReplyString(replyString.str());
1653 CASPAR_LOG_CURRENT_EXCEPTION();
1654 SetReplyString(TEXT("501 TLS FAILED\r\n"));
1660 bool VersionCommand::DoExecute()
1662 std::wstring replyString = TEXT("201 VERSION OK\r\n") + env::version() + TEXT("\r\n");
1664 if(parameters().size() > 0)
1666 if(boost::iequals(parameters()[0], L"FLASH"))
1667 replyString = TEXT("201 VERSION OK\r\n") + flash::version() + TEXT("\r\n");
1668 else if(boost::iequals(parameters()[0], L"TEMPLATEHOST"))
1669 replyString = TEXT("201 VERSION OK\r\n") + flash::cg_version() + TEXT("\r\n");
1670 else if(!boost::iequals(parameters()[0], L"SERVER"))
1671 replyString = TEXT("403 VERSION ERROR\r\n");
1674 SetReplyString(replyString);
1678 bool ByeCommand::DoExecute()
1680 client()->disconnect();
1684 bool SetCommand::DoExecute()
1688 std::wstring name = boost::to_upper_copy(parameters()[0]);
1689 std::wstring value = boost::to_upper_copy(parameters()[1]);
1691 if(name == TEXT("MODE"))
1693 auto format_desc = core::video_format_desc(value);
1694 if(format_desc.format != core::video_format::invalid)
1696 channel()->video_format_desc(format_desc);
1697 SetReplyString(TEXT("202 SET MODE OK\r\n"));
1700 SetReplyString(TEXT("501 SET MODE FAILED\r\n"));
1704 this->SetReplyString(TEXT("403 SET ERROR\r\n"));
1709 CASPAR_LOG_CURRENT_EXCEPTION();
1710 SetReplyString(TEXT("501 SET FAILED\r\n"));
1717 bool LockCommand::DoExecute()
1721 auto it = parameters().begin();
1723 std::shared_ptr<caspar::IO::lock_container> lock;
1726 int channel_index = boost::lexical_cast<int>(*it) - 1;
1727 lock = channels().at(channel_index).lock;
1729 catch(const boost::bad_lexical_cast&) {}
1732 SetReplyString(L"401 LOCK ERROR\r\n");
1739 if(it == parameters().end()) //too few parameters
1741 SetReplyString(L"402 LOCK ERROR\r\n");
1745 std::wstring command = boost::to_upper_copy(*it);
1746 if(command == L"ACQUIRE")
1749 if(it == parameters().end()) //too few parameters
1751 SetReplyString(L"402 LOCK ACQUIRE ERROR\r\n");
1754 std::wstring lock_phrase = (*it);
1756 //TODO: read options
1760 //just lock one channel
1761 if(!lock->try_lock(lock_phrase, client()))
1763 SetReplyString(L"503 LOCK ACQUIRE FAILED\r\n");
1769 //TODO: lock all channels
1770 CASPAR_THROW_EXCEPTION(not_implemented());
1772 SetReplyString(L"202 LOCK ACQUIRE OK\r\n");
1775 else if(command == L"RELEASE")
1779 lock->release_lock(client());
1783 //TODO: release all channels
1784 CASPAR_THROW_EXCEPTION(not_implemented());
1786 SetReplyString(L"202 LOCK RELEASE OK\r\n");
1788 else if(command == L"CLEAR")
1790 std::wstring override_phrase = env::properties().get(L"configuration.lock-clear-phrase", L"");
1791 std::wstring client_override_phrase;
1792 if(!override_phrase.empty())
1795 if(it == parameters().end())
1797 SetReplyString(L"402 LOCK CLEAR ERROR\r\n");
1800 client_override_phrase = (*it);
1805 //just clear one channel
1806 if(client_override_phrase != override_phrase)
1808 SetReplyString(L"503 LOCK CLEAR FAILED\r\n");
1812 lock->clear_locks();
1816 //TODO: clear all channels
1817 CASPAR_THROW_EXCEPTION(not_implemented());
1820 SetReplyString(L"202 LOCK CLEAR OK\r\n");
1824 SetReplyString(L"403 LOCK ERROR\r\n");
1828 catch(not_implemented&)
1830 SetReplyString(L"600 LOCK FAILED\r\n");
1835 CASPAR_LOG_CURRENT_EXCEPTION();
1836 SetReplyString(L"501 LOCK FAILED\r\n");
1843 bool ThumbnailCommand::DoExecute()
1845 std::wstring command = boost::to_upper_copy(parameters()[0]);
1847 if (command == TEXT("RETRIEVE"))
1848 return DoExecuteRetrieve();
1849 else if (command == TEXT("LIST"))
1850 return DoExecuteList();
1851 else if (command == TEXT("GENERATE"))
1852 return DoExecuteGenerate();
1853 else if (command == TEXT("GENERATE_ALL"))
1854 return DoExecuteGenerateAll();
1856 SetReplyString(TEXT("403 THUMBNAIL ERROR\r\n"));
1860 bool ThumbnailCommand::DoExecuteRetrieve()
1862 if(parameters().size() < 2)
1864 SetReplyString(TEXT("402 THUMBNAIL RETRIEVE ERROR\r\n"));
1868 std::wstring filename = env::thumbnails_folder();
1869 filename.append(parameters()[1]);
1870 filename.append(TEXT(".png"));
1872 std::wstring file_contents = read_file_base64(boost::filesystem::wpath(filename));
1874 if (file_contents.empty())
1876 SetReplyString(TEXT("404 THUMBNAIL RETRIEVE ERROR\r\n"));
1880 std::wstringstream reply;
1882 reply << L"201 THUMBNAIL RETRIEVE OK\r\n";
1883 reply << file_contents;
1885 SetReplyString(reply.str());
1889 bool ThumbnailCommand::DoExecuteList()
1891 std::wstringstream replyString;
1892 replyString << TEXT("200 THUMBNAIL LIST OK\r\n");
1894 for (boost::filesystem::recursive_directory_iterator itr(env::thumbnails_folder()), end; itr != end; ++itr)
1896 if(boost::filesystem::is_regular_file(itr->path()))
1898 if(!boost::iequals(itr->path().extension().wstring(), L".png"))
1901 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::thumbnails_folder().size()-1, itr->path().wstring().size()));
1903 auto str = relativePath.replace_extension(L"").native();
1904 if(str[0] == '\\' || str[0] == '/')
1905 str = std::wstring(str.begin() + 1, str.end());
1907 auto mtime = boost::filesystem::last_write_time(itr->path());
1908 auto mtime_readable = boost::posix_time::to_iso_wstring(boost::posix_time::from_time_t(mtime));
1909 auto file_size = boost::filesystem::file_size(itr->path());
1911 replyString << L"\"" << str << L"\" " << mtime_readable << L" " << file_size << L"\r\n";
1915 replyString << TEXT("\r\n");
1917 SetReplyString(boost::to_upper_copy(replyString.str()));
1921 bool ThumbnailCommand::DoExecuteGenerate()
1923 if (parameters().size() < 2)
1925 SetReplyString(L"402 THUMBNAIL GENERATE ERROR\r\n");
1931 thumb_gen_->generate(parameters()[1]);
1932 SetReplyString(L"202 THUMBNAIL GENERATE OK\r\n");
1937 SetReplyString(L"500 THUMBNAIL GENERATE ERROR\r\n");
1942 bool ThumbnailCommand::DoExecuteGenerateAll()
1946 thumb_gen_->generate_all();
1947 SetReplyString(L"202 THUMBNAIL GENERATE_ALL OK\r\n");
1952 SetReplyString(L"500 THUMBNAIL GENERATE_ALL ERROR\r\n");
1957 bool KillCommand::DoExecute()
1959 shutdown_server_now_->set_value(false); //false for not waiting for keypress
1960 SetReplyString(TEXT("202 KILL OK\r\n"));
1965 }} //namespace caspar