2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Nicklas P Andersson
22 #include "../StdAfx.h"
25 #pragma warning (push, 1) // TODO: Legacy code, just disable warnings
28 #include "AMCPCommandsImpl.h"
29 #include "AMCPProtocolStrategy.h"
31 #include <common/env.h>
33 #include <common/log.h>
34 #include <common/param.h>
35 #include <common/diagnostics/graph.h>
36 #include <common/os/windows/current_version.h>
37 #include <common/os/windows/system_info.h>
38 #include <common/base64.h>
40 #include <core/producer/frame_producer.h>
41 #include <core/video_format.h>
42 #include <core/producer/transition/transition_producer.h>
43 #include <core/frame/frame_transform.h>
44 #include <core/producer/stage.h>
45 #include <core/producer/layer.h>
46 #include <core/mixer/mixer.h>
47 #include <core/consumer/output.h>
48 #include <core/thumbnail_generator.h>
50 #include <modules/reroute/producer/reroute_producer.h>
51 #include <modules/bluefish/bluefish.h>
52 #include <modules/decklink/decklink.h>
53 #include <modules/ffmpeg/ffmpeg.h>
54 #include <modules/flash/flash.h>
55 #include <modules/flash/util/swf.h>
56 #include <modules/flash/producer/flash_producer.h>
57 #include <modules/flash/producer/cg_proxy.h>
58 #include <modules/ffmpeg/producer/util/util.h>
59 #include <modules/image/image.h>
60 #include <modules/screen/screen.h>
61 #include <modules/reroute/producer/reroute_producer.h>
71 #include <boost/date_time/posix_time/posix_time.hpp>
72 #include <boost/lexical_cast.hpp>
73 #include <boost/algorithm/string.hpp>
74 #include <boost/filesystem.hpp>
75 #include <boost/filesystem/fstream.hpp>
76 #include <boost/regex.hpp>
77 #include <boost/property_tree/xml_parser.hpp>
78 #include <boost/locale.hpp>
79 #include <boost/range/adaptor/transformed.hpp>
80 #include <boost/range/algorithm/copy.hpp>
81 #include <boost/archive/iterators/base64_from_binary.hpp>
82 #include <boost/archive/iterators/insert_linebreaks.hpp>
83 #include <boost/archive/iterators/transform_width.hpp>
85 #include <tbb/concurrent_unordered_map.h>
89 102 [action] Information that [action] has happened
90 101 [action] Information that [action] has happened plus one row of data
92 202 [command] OK [command] has been executed
93 201 [command] OK [command] has been executed, plus one row of data
94 200 [command] OK [command] has been executed, plus multiple lines of data. ends with an empty line
96 400 ERROR the command could not be understood
97 401 [command] ERROR invalid/missing channel
98 402 [command] ERROR parameter missing
99 403 [command] ERROR invalid parameter
100 404 [command] ERROR file not found
102 500 FAILED internal error
103 501 [command] FAILED internal error
104 502 [command] FAILED could not read file
105 503 [command] FAILED access denied
107 600 [command] FAILED [command] not implemented
110 namespace caspar { namespace protocol {
112 using namespace core;
114 std::wstring read_file_base64(const boost::filesystem::wpath& file)
116 using namespace boost::archive::iterators;
118 boost::filesystem::ifstream filestream(file, std::ios::binary);
123 auto length = boost::filesystem::file_size(file);
124 std::vector<char> bytes;
125 bytes.resize(length);
126 filestream.read(bytes.data(), length);
128 std::string result(to_base64(bytes.data(), length));
129 return std::wstring(result.begin(), result.end());
132 std::wstring read_utf8_file(const boost::filesystem::wpath& file)
134 std::wstringstream result;
135 boost::filesystem::wifstream filestream(file);
142 result << filestream.rdbuf();
148 std::wstring read_latin1_file(const boost::filesystem::wpath& file)
150 boost::locale::generator gen;
151 gen.locale_cache_enabled(true);
152 gen.categories(boost::locale::codepage_facet);
154 std::stringstream result_stream;
155 boost::filesystem::ifstream filestream(file);
156 filestream.imbue(gen("en_US.ISO8859-1"));
161 result_stream << filestream.rdbuf();
164 std::string result = result_stream.str();
165 std::wstring widened_result;
167 // The first 255 codepoints in unicode is the same as in latin1
169 result | boost::adaptors::transformed(
170 [](char c) { return static_cast<unsigned char>(c); }),
171 std::back_inserter(widened_result));
173 return widened_result;
176 std::wstring read_file(const boost::filesystem::wpath& file)
178 static const uint8_t BOM[] = {0xef, 0xbb, 0xbf};
180 if (!boost::filesystem::exists(file))
185 if (boost::filesystem::file_size(file) >= 3)
187 boost::filesystem::ifstream bom_stream(file);
190 bom_stream.read(header, 3);
193 if (std::memcmp(BOM, header, 3) == 0)
194 return read_utf8_file(file);
197 return read_latin1_file(file);
200 std::wstring MediaInfo(const boost::filesystem::path& path)
202 if(boost::filesystem::is_regular_file(path))
204 std::wstring clipttype = TEXT("N/A");
205 std::wstring extension = boost::to_upper_copy(path.extension().wstring());
206 if(extension == TEXT(".TGA") || extension == TEXT(".COL") || extension == L".PNG" || extension == L".JPEG" || extension == L".JPG" ||
207 extension == L"GIF" || extension == L"BMP")
208 clipttype = TEXT("STILL");
209 else if(extension == TEXT(".WAV") || extension == TEXT(".MP3"))
210 clipttype = TEXT("AUDIO");
211 else if(extension == TEXT("SWF") || extension == TEXT("CT") || extension == TEXT("DV") || extension == TEXT("MOV") || extension == TEXT("MPG") || extension == TEXT("AVI") || caspar::ffmpeg::is_valid_file(path.wstring()))
212 clipttype = TEXT("MOVIE");
214 if(clipttype != TEXT("N/A"))
216 auto is_not_digit = [](char c){ return std::isdigit(c) == 0; };
218 auto relativePath = boost::filesystem::path(path.wstring().substr(env::media_folder().size()-1, path.wstring().size()));
220 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(path)));
221 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), is_not_digit), writeTimeStr.end());
222 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
224 auto sizeStr = boost::lexical_cast<std::wstring>(boost::filesystem::file_size(path));
225 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), is_not_digit), sizeStr.end());
226 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
228 auto str = relativePath.replace_extension(TEXT("")).native();
229 while(str.size() > 0 && (str[0] == '\\' || str[0] == '/'))
230 str = std::wstring(str.begin() + 1, str.end());
232 return std::wstring() + TEXT("\"") + str +
233 + TEXT("\" ") + clipttype +
234 + TEXT(" ") + sizeStr +
235 + TEXT(" ") + writeTimeWStr +
242 std::wstring ListMedia()
244 std::wstringstream replyString;
245 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end; ++itr)
246 replyString << MediaInfo(itr->path());
248 return boost::to_upper_copy(replyString.str());
251 std::wstring ListTemplates()
253 std::wstringstream replyString;
255 for (boost::filesystem::recursive_directory_iterator itr(env::template_folder()), end; itr != end; ++itr)
257 if(boost::filesystem::is_regular_file(itr->path()) && (itr->path().extension() == L".ft" || itr->path().extension() == L".ct"))
259 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::template_folder().size()-1, itr->path().wstring().size()));
261 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(itr->path())));
262 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), [](char c){ return std::isdigit(c) == 0;}), writeTimeStr.end());
263 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
265 auto sizeStr = boost::lexical_cast<std::string>(boost::filesystem::file_size(itr->path()));
266 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), [](char c){ return std::isdigit(c) == 0;}), sizeStr.end());
268 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
270 std::wstring dir = relativePath.parent_path().native();
271 std::wstring file = boost::to_upper_copy(relativePath.filename().wstring());
272 relativePath = boost::filesystem::wpath(dir + L"/" + file);
274 auto str = relativePath.replace_extension(TEXT("")).native();
275 boost::trim_if(str, boost::is_any_of("\\/"));
277 replyString << TEXT("\"") << str
278 << TEXT("\" ") << sizeWStr
279 << TEXT(" ") << writeTimeWStr
283 return replyString.str();
288 void AMCPCommand::SendReply()
290 if(replyString_.empty())
293 client_->send(std::move(replyString_));
296 bool DiagnosticsCommand::DoExecute()
300 diagnostics::show_graphs(true);
302 SetReplyString(TEXT("202 DIAG OK\r\n"));
308 CASPAR_LOG_CURRENT_EXCEPTION();
309 SetReplyString(TEXT("502 DIAG FAILED\r\n"));
314 bool ChannelGridCommand::DoExecute()
317 auto self = channels().back().channel;
319 std::vector<std::wstring> params;
320 params.push_back(L"SCREEN");
321 params.push_back(L"0");
322 params.push_back(L"NAME");
323 params.push_back(L"Channel Grid Window");
324 auto screen = create_consumer(params);
326 self->output().add(screen);
328 for (auto& channel : channels())
330 if(channel.channel != self)
332 auto producer = reroute::create_producer(*channel.channel);
333 self->stage().load(index, producer, false);
334 self->stage().play(index);
339 int n = channels().size()-1;
340 double delta = 1.0/static_cast<double>(n);
341 for(int x = 0; x < n; ++x)
343 for(int y = 0; y < n; ++y)
346 auto transform = [=](frame_transform transform) -> frame_transform
348 transform.image_transform.fill_translation[0] = x*delta;
349 transform.image_transform.fill_translation[1] = y*delta;
350 transform.image_transform.fill_scale[0] = delta;
351 transform.image_transform.fill_scale[1] = delta;
352 transform.image_transform.clip_translation[0] = x*delta;
353 transform.image_transform.clip_translation[1] = y*delta;
354 transform.image_transform.clip_scale[0] = delta;
355 transform.image_transform.clip_scale[1] = delta;
358 self->stage().apply_transform(index, transform);
365 bool CallCommand::DoExecute()
367 //Perform loading of the clip
370 auto result = channel()->stage().call(layer_index(), parameters());
372 // TODO: because of std::async deferred timed waiting does not work
374 /*auto wait_res = result.wait_for(std::chrono::seconds(2));
375 if (wait_res == std::future_status::timeout)
376 CASPAR_THROW_EXCEPTION(timed_out());*/
378 std::wstringstream replyString;
379 if(result.get().empty())
380 replyString << TEXT("202 CALL OK\r\n");
382 replyString << TEXT("201 CALL OK\r\n") << result.get() << L"\r\n";
384 SetReplyString(replyString.str());
390 CASPAR_LOG_CURRENT_EXCEPTION();
391 SetReplyString(TEXT("502 CALL FAILED\r\n"));
396 tbb::concurrent_unordered_map<int, std::vector<stage::transform_tuple_t>> deferred_transforms;
398 bool MixerCommand::DoExecute()
400 //Perform loading of the clip
403 bool defer = boost::iequals(parameters().back(), L"DEFER");
405 parameters().pop_back();
407 std::vector<stage::transform_tuple_t> transforms;
409 if(boost::iequals(parameters()[0], L"KEYER") || boost::iequals(parameters()[0], L"IS_KEY"))
411 bool value = boost::lexical_cast<int>(parameters().at(1));
412 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
414 transform.image_transform.is_key = value;
418 else if(boost::iequals(parameters()[0], L"OPACITY"))
420 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
421 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
423 double value = boost::lexical_cast<double>(parameters().at(1));
425 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
427 transform.image_transform.opacity = value;
429 }, duration, tween));
431 else if(boost::iequals(parameters()[0], L"FILL") || boost::iequals(parameters()[0], L"FILL_RECT"))
433 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
434 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
435 double x = boost::lexical_cast<double>(parameters().at(1));
436 double y = boost::lexical_cast<double>(parameters().at(2));
437 double x_s = boost::lexical_cast<double>(parameters().at(3));
438 double y_s = boost::lexical_cast<double>(parameters().at(4));
440 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) mutable -> frame_transform
442 transform.image_transform.fill_translation[0] = x;
443 transform.image_transform.fill_translation[1] = y;
444 transform.image_transform.fill_scale[0] = x_s;
445 transform.image_transform.fill_scale[1] = y_s;
447 }, duration, tween));
449 else if(boost::iequals(parameters()[0], L"CLIP") || boost::iequals(parameters()[0], L"CLIP_RECT"))
451 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
452 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
453 double x = boost::lexical_cast<double>(parameters().at(1));
454 double y = boost::lexical_cast<double>(parameters().at(2));
455 double x_s = boost::lexical_cast<double>(parameters().at(3));
456 double y_s = boost::lexical_cast<double>(parameters().at(4));
458 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
460 transform.image_transform.clip_translation[0] = x;
461 transform.image_transform.clip_translation[1] = y;
462 transform.image_transform.clip_scale[0] = x_s;
463 transform.image_transform.clip_scale[1] = y_s;
465 }, duration, tween));
467 else if(boost::iequals(parameters()[0], L"GRID"))
469 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
470 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
471 int n = boost::lexical_cast<int>(parameters().at(1));
472 double delta = 1.0/static_cast<double>(n);
473 for(int x = 0; x < n; ++x)
475 for(int y = 0; y < n; ++y)
478 transforms.push_back(stage::transform_tuple_t(index, [=](frame_transform transform) -> frame_transform
480 transform.image_transform.fill_translation[0] = x*delta;
481 transform.image_transform.fill_translation[1] = y*delta;
482 transform.image_transform.fill_scale[0] = delta;
483 transform.image_transform.fill_scale[1] = delta;
484 transform.image_transform.clip_translation[0] = x*delta;
485 transform.image_transform.clip_translation[1] = y*delta;
486 transform.image_transform.clip_scale[0] = delta;
487 transform.image_transform.clip_scale[1] = delta;
489 }, duration, tween));
493 else if(boost::iequals(parameters()[0], L"BLEND"))
495 auto blend_str = parameters().at(1);
496 int layer = layer_index();
497 channel()->mixer().set_blend_mode(layer, get_blend_mode(blend_str));
499 else if(boost::iequals(parameters()[0], L"MASTERVOLUME"))
501 float master_volume = boost::lexical_cast<float>(parameters().at(1));
502 channel()->mixer().set_master_volume(master_volume);
504 else if(boost::iequals(parameters()[0], L"BRIGHTNESS"))
506 auto value = boost::lexical_cast<double>(parameters().at(1));
507 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
508 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
509 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
511 transform.image_transform.brightness = value;
513 }, duration, tween));
515 else if(boost::iequals(parameters()[0], L"SATURATION"))
517 auto value = boost::lexical_cast<double>(parameters().at(1));
518 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
519 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
520 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
522 transform.image_transform.saturation = value;
524 }, duration, tween));
526 else if(parameters()[0] == L"CONTRAST")
528 auto value = boost::lexical_cast<double>(parameters().at(1));
529 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
530 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
531 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
533 transform.image_transform.contrast = value;
535 }, duration, tween));
537 else if(boost::iequals(parameters()[0], L"LEVELS"))
540 value.min_input = boost::lexical_cast<double>(parameters().at(1));
541 value.max_input = boost::lexical_cast<double>(parameters().at(2));
542 value.gamma = boost::lexical_cast<double>(parameters().at(3));
543 value.min_output = boost::lexical_cast<double>(parameters().at(4));
544 value.max_output = boost::lexical_cast<double>(parameters().at(5));
545 int duration = parameters().size() > 6 ? boost::lexical_cast<int>(parameters()[6]) : 0;
546 std::wstring tween = parameters().size() > 7 ? parameters()[7] : L"linear";
548 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
550 transform.image_transform.levels = value;
552 }, duration, tween));
554 else if(boost::iequals(parameters()[0], L"VOLUME"))
556 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
557 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
558 double value = boost::lexical_cast<double>(parameters()[1]);
560 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
562 transform.audio_transform.volume = value;
564 }, duration, tween));
566 else if(boost::iequals(parameters()[0], L"CLEAR"))
568 int layer = layer_index(std::numeric_limits<int>::max());
570 if (layer == std::numeric_limits<int>::max())
572 channel()->stage().clear_transforms();
573 channel()->mixer().clear_blend_modes();
577 channel()->stage().clear_transforms(layer);
578 channel()->mixer().clear_blend_mode(layer);
581 else if(boost::iequals(parameters()[0], L"COMMIT"))
583 transforms = std::move(deferred_transforms[channel_index()]);
587 SetReplyString(TEXT("404 MIXER ERROR\r\n"));
593 auto& defer_tranforms = deferred_transforms[channel_index()];
594 defer_tranforms.insert(defer_tranforms.end(), transforms.begin(), transforms.end());
597 channel()->stage().apply_transforms(transforms);
599 SetReplyString(TEXT("202 MIXER OK\r\n"));
603 catch(file_not_found&)
605 CASPAR_LOG_CURRENT_EXCEPTION();
606 SetReplyString(TEXT("404 MIXER ERROR\r\n"));
611 CASPAR_LOG_CURRENT_EXCEPTION();
612 SetReplyString(TEXT("502 MIXER FAILED\r\n"));
617 bool SwapCommand::DoExecute()
619 //Perform loading of the clip
622 if(layer_index(-1) != -1)
624 std::vector<std::string> strs;
625 boost::split(strs, parameters()[0], boost::is_any_of("-"));
627 auto ch1 = channel();
628 auto ch2 = channels().at(boost::lexical_cast<int>(strs.at(0))-1);
630 int l1 = layer_index();
631 int l2 = boost::lexical_cast<int>(strs.at(1));
633 ch1->stage().swap_layer(l1, l2, ch2.channel->stage());
637 auto ch1 = channel();
638 auto ch2 = channels().at(boost::lexical_cast<int>(parameters()[0])-1);
639 ch1->stage().swap_layers(ch2.channel->stage());
642 SetReplyString(TEXT("202 SWAP OK\r\n"));
646 catch(file_not_found&)
648 CASPAR_LOG_CURRENT_EXCEPTION();
649 SetReplyString(TEXT("404 SWAP ERROR\r\n"));
654 CASPAR_LOG_CURRENT_EXCEPTION();
655 SetReplyString(TEXT("502 SWAP FAILED\r\n"));
660 bool AddCommand::DoExecute()
662 //Perform loading of the clip
665 //create_consumer still expects all parameters to be uppercase
666 for (auto& str : parameters())
668 boost::to_upper(str);
671 auto consumer = create_consumer(parameters());
672 channel()->output().add(layer_index(consumer->index()), consumer);
674 SetReplyString(TEXT("202 ADD OK\r\n"));
678 catch(file_not_found&)
680 CASPAR_LOG_CURRENT_EXCEPTION();
681 SetReplyString(TEXT("404 ADD ERROR\r\n"));
686 CASPAR_LOG_CURRENT_EXCEPTION();
687 SetReplyString(TEXT("502 ADD FAILED\r\n"));
692 bool RemoveCommand::DoExecute()
694 //Perform loading of the clip
697 auto index = layer_index(std::numeric_limits<int>::min());
698 if(index == std::numeric_limits<int>::min())
700 //create_consumer still expects all parameters to be uppercase
701 for (auto& str : parameters())
703 boost::to_upper(str);
706 index = create_consumer(parameters())->index();
709 channel()->output().remove(index);
711 SetReplyString(TEXT("202 REMOVE OK\r\n"));
715 catch(file_not_found&)
717 CASPAR_LOG_CURRENT_EXCEPTION();
718 SetReplyString(TEXT("404 REMOVE ERROR\r\n"));
723 CASPAR_LOG_CURRENT_EXCEPTION();
724 SetReplyString(TEXT("502 REMOVE FAILED\r\n"));
729 bool LoadCommand::DoExecute()
731 //Perform loading of the clip
734 auto pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
735 channel()->stage().load(layer_index(), pFP, true);
737 SetReplyString(TEXT("202 LOAD OK\r\n"));
741 catch(file_not_found&)
743 CASPAR_LOG_CURRENT_EXCEPTION();
744 SetReplyString(TEXT("404 LOAD ERROR\r\n"));
749 CASPAR_LOG_CURRENT_EXCEPTION();
750 SetReplyString(TEXT("502 LOAD FAILED\r\n"));
757 //std::function<std::wstring()> channel_cg_add_command::parse(const std::wstring& message, const std::vector<renderer::render_device_ptr>& channels)
759 // 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>.*)?");
761 // boost::wsmatch what;
762 // if(!boost::regex_match(message, what, expr))
765 // auto info = channel_info::parse(what, channels);
767 // int flash_layer_index = boost::lexical_cast<int>(what["FLASH_LAYER"].str());
769 // std::wstring templatename = what["TEMPLATE"].str();
770 // bool play_on_load = what["PLAY_ON_LOAD"].matched ? what["PLAY_ON_LOAD"].str() != L"0" : 0;
771 // std::wstring start_label = what["START_LABEL"].str();
772 // std::wstring data = get_data(what["DATA"].str());
774 // boost::replace_all(templatename, "\"", "");
776 // return [=]() -> std::wstring
778 // std::wstring fullFilename = flash::flash_producer::find_template(server::template_folder() + templatename);
779 // if(fullFilename.empty())
780 // CASPAR_THROW_EXCEPTION(file_not_found());
782 // std::wstring extension = boost::filesystem::wpath(fullFilename).extension();
783 // std::wstring filename = templatename;
784 // filename.append(extension);
786 // flash::flash::create_cg_proxy(info.video_channel, std::max<int>(DEFAULT_CHANNEL_LAYER+1, info.layer_index))
787 // ->add(flash_layer_index, filename, play_on_load, start_label, data);
789 // CASPAR_LOG(info) << L"Executed [amcp_channel_cg_add]";
793 bool LoadbgCommand::DoExecute()
795 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(LR"(.*(?<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(LR"(\[(?<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({ 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::wstring dataFromFile;
1063 if(parameters().size() > dataIndex)
1065 const std::wstring& dataString = parameters()[dataIndex];
1067 if(dataString[0] == TEXT('<')) //the data is an XML-string
1068 pDataString = dataString.c_str();
1071 //The data is not an XML-string, it must be a filename
1072 std::wstring filename = env::data_folder();
1073 filename.append(dataString);
1074 filename.append(TEXT(".ftd"));
1076 dataFromFile = read_file(boost::filesystem::wpath(filename));
1077 pDataString = dataFromFile.c_str();
1081 std::wstring fullFilename = flash::find_template(env::template_folder() + parameters()[2]);
1082 if(!fullFilename.empty())
1084 std::wstring extension = boost::filesystem::path(fullFilename).extension().wstring();
1085 std::wstring filename = parameters()[2];
1086 filename.append(extension);
1088 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(""));
1089 SetReplyString(TEXT("202 CG OK\r\n"));
1093 CASPAR_LOG(warning) << "Could not find template " << parameters()[2];
1094 SetReplyString(TEXT("404 CG ERROR\r\n"));
1099 bool CGCommand::DoExecutePlay()
1101 if(parameters().size() > 1)
1103 if(!ValidateLayer(parameters()[1]))
1105 SetReplyString(TEXT("403 CG ERROR\r\n"));
1108 int layer = boost::lexical_cast<int>(parameters()[1]);
1109 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).play(layer);
1113 SetReplyString(TEXT("402 CG ERROR\r\n"));
1117 SetReplyString(TEXT("202 CG OK\r\n"));
1121 bool CGCommand::DoExecuteStop()
1123 if(parameters().size() > 1)
1125 if(!ValidateLayer(parameters()[1]))
1127 SetReplyString(TEXT("403 CG ERROR\r\n"));
1130 int layer = boost::lexical_cast<int>(parameters()[1]);
1131 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).stop(layer, 0);
1135 SetReplyString(TEXT("402 CG ERROR\r\n"));
1139 SetReplyString(TEXT("202 CG OK\r\n"));
1143 bool CGCommand::DoExecuteNext()
1145 if(parameters().size() > 1)
1147 if(!ValidateLayer(parameters()[1]))
1149 SetReplyString(TEXT("403 CG ERROR\r\n"));
1153 int layer = boost::lexical_cast<int>(parameters()[1]);
1154 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).next(layer);
1158 SetReplyString(TEXT("402 CG ERROR\r\n"));
1162 SetReplyString(TEXT("202 CG OK\r\n"));
1166 bool CGCommand::DoExecuteRemove()
1168 if(parameters().size() > 1)
1170 if(!ValidateLayer(parameters()[1]))
1172 SetReplyString(TEXT("403 CG ERROR\r\n"));
1176 int layer = boost::lexical_cast<int>(parameters()[1]);
1177 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).remove(layer);
1181 SetReplyString(TEXT("402 CG ERROR\r\n"));
1185 SetReplyString(TEXT("202 CG OK\r\n"));
1189 bool CGCommand::DoExecuteClear()
1191 channel()->stage().clear(layer_index(flash::cg_proxy::DEFAULT_LAYER));
1192 SetReplyString(TEXT("202 CG OK\r\n"));
1196 bool CGCommand::DoExecuteUpdate()
1200 if(!ValidateLayer(parameters().at(1)))
1202 SetReplyString(TEXT("403 CG ERROR\r\n"));
1206 std::wstring dataString = parameters().at(2);
1207 if(dataString.at(0) != TEXT('<'))
1209 //The data is not an XML-string, it must be a filename
1210 std::wstring filename = env::data_folder();
1211 filename.append(dataString);
1212 filename.append(TEXT(".ftd"));
1214 dataString = read_file(boost::filesystem::wpath(filename));
1217 int layer = boost::lexical_cast<int>(parameters()[1]);
1218 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).update(layer, dataString);
1222 SetReplyString(TEXT("402 CG ERROR\r\n"));
1226 SetReplyString(TEXT("202 CG OK\r\n"));
1230 bool CGCommand::DoExecuteInvoke()
1232 std::wstringstream replyString;
1233 replyString << TEXT("201 CG OK\r\n");
1235 if(parameters().size() > 2)
1237 if(!ValidateLayer(parameters()[1]))
1239 SetReplyString(TEXT("403 CG ERROR\r\n"));
1242 int layer = boost::lexical_cast<int>(parameters()[1]);
1243 auto result = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).invoke(layer, parameters()[2]);
1244 replyString << result << TEXT("\r\n");
1248 SetReplyString(TEXT("402 CG ERROR\r\n"));
1252 SetReplyString(replyString.str());
1256 bool CGCommand::DoExecuteInfo()
1258 std::wstringstream replyString;
1259 replyString << TEXT("201 CG OK\r\n");
1261 if(parameters().size() > 1)
1263 if(!ValidateLayer(parameters()[1]))
1265 SetReplyString(TEXT("403 CG ERROR\r\n"));
1269 int layer = boost::lexical_cast<int>(parameters()[1]);
1270 auto desc = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).description(layer);
1272 replyString << desc << TEXT("\r\n");
1276 auto info = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).template_host_info();
1277 replyString << info << TEXT("\r\n");
1280 SetReplyString(replyString.str());
1284 bool DataCommand::DoExecute()
1286 std::wstring command = boost::to_upper_copy(parameters()[0]);
1287 if(command == TEXT("STORE"))
1288 return DoExecuteStore();
1289 else if(command == TEXT("RETRIEVE"))
1290 return DoExecuteRetrieve();
1291 else if(command == TEXT("REMOVE"))
1292 return DoExecuteRemove();
1293 else if(command == TEXT("LIST"))
1294 return DoExecuteList();
1296 SetReplyString(TEXT("403 DATA ERROR\r\n"));
1300 bool DataCommand::DoExecuteStore()
1302 if(parameters().size() < 3)
1304 SetReplyString(TEXT("402 DATA STORE ERROR\r\n"));
1308 std::wstring filename = env::data_folder();
1309 filename.append(parameters()[1]);
1310 filename.append(TEXT(".ftd"));
1312 auto data_path = boost::filesystem::wpath(
1313 boost::filesystem::wpath(filename).parent_path());
1315 if(!boost::filesystem::exists(data_path))
1316 boost::filesystem::create_directories(data_path);
1318 std::wofstream datafile(filename.c_str());
1321 SetReplyString(TEXT("501 DATA STORE FAILED\r\n"));
1325 datafile << static_cast<wchar_t>(65279); // UTF-8 BOM character
1326 datafile << parameters()[2] << std::flush;
1329 std::wstring replyString = TEXT("202 DATA STORE OK\r\n");
1330 SetReplyString(replyString);
1334 bool DataCommand::DoExecuteRetrieve()
1336 if(parameters().size() < 2)
1338 SetReplyString(TEXT("402 DATA RETRIEVE ERROR\r\n"));
1342 std::wstring filename = env::data_folder();
1343 filename.append(parameters()[1]);
1344 filename.append(TEXT(".ftd"));
1346 std::wstring file_contents = read_file(boost::filesystem::wpath(filename));
1348 if (file_contents.empty())
1350 SetReplyString(TEXT("404 DATA RETRIEVE ERROR\r\n"));
1354 std::wstringstream reply(TEXT("201 DATA RETRIEVE OK\r\n"));
1356 std::wstringstream file_contents_stream(file_contents);
1359 bool firstLine = true;
1360 while(std::getline(file_contents_stream, line))
1371 SetReplyString(reply.str());
1375 bool DataCommand::DoExecuteRemove()
1377 if (parameters().size() < 2)
1379 SetReplyString(TEXT("402 DATA REMOVE ERROR\r\n"));
1383 std::wstring filename = env::data_folder();
1384 filename.append(parameters()[1]);
1385 filename.append(TEXT(".ftd"));
1387 if (!boost::filesystem::exists(filename))
1389 SetReplyString(TEXT("404 DATA REMOVE ERROR\r\n"));
1393 if (!boost::filesystem::remove(filename))
1395 SetReplyString(TEXT("403 DATA REMOVE ERROR\r\n"));
1399 SetReplyString(TEXT("201 DATA REMOVE OK\r\n"));
1404 bool DataCommand::DoExecuteList()
1406 std::wstringstream replyString;
1407 replyString << TEXT("200 DATA LIST OK\r\n");
1409 for (boost::filesystem::recursive_directory_iterator itr(env::data_folder()), end; itr != end; ++itr)
1411 if(boost::filesystem::is_regular_file(itr->path()))
1413 if(!boost::iequals(itr->path().extension().wstring(), L".ftd"))
1416 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::data_folder().size()-1, itr->path().wstring().size()));
1418 auto str = relativePath.replace_extension(TEXT("")).native();
1419 if(str[0] == '\\' || str[0] == '/')
1420 str = std::wstring(str.begin() + 1, str.end());
1422 replyString << str << TEXT("\r\n");
1426 replyString << TEXT("\r\n");
1428 SetReplyString(boost::to_upper_copy(replyString.str()));
1432 bool CinfCommand::DoExecute()
1434 std::wstringstream replyString;
1439 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end && info.empty(); ++itr)
1441 auto path = itr->path();
1442 auto file = path.replace_extension(L"").filename();
1443 if(boost::iequals(file.wstring(), parameters().at(0)))
1444 info += MediaInfo(itr->path()) + L"\r\n";
1449 SetReplyString(TEXT("404 CINF ERROR\r\n"));
1452 replyString << TEXT("200 CINF OK\r\n");
1453 replyString << info << "\r\n";
1457 CASPAR_LOG_CURRENT_EXCEPTION();
1458 SetReplyString(TEXT("404 CINF ERROR\r\n"));
1462 SetReplyString(replyString.str());
1466 void GenerateChannelInfo(int index, const spl::shared_ptr<core::video_channel>& pChannel, std::wstringstream& replyString)
1468 replyString << index+1 << TEXT(" ") << pChannel->video_format_desc().name << TEXT(" PLAYING") << TEXT("\r\n");
1471 bool InfoCommand::DoExecute()
1473 std::wstringstream replyString;
1475 boost::property_tree::xml_writer_settings<std::wstring> w(' ', 3);
1479 if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"TEMPLATE"))
1481 replyString << L"201 INFO TEMPLATE OK\r\n";
1483 // Needs to be extended for any file, not just flash.
1485 auto filename = flash::find_template(env::template_folder() + parameters().at(1));
1487 std::wstringstream str;
1488 str << u16(flash::read_template_meta_info(filename));
1489 boost::property_tree::wptree info;
1490 boost::property_tree::xml_parser::read_xml(str, info, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);
1492 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1494 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"CONFIG"))
1496 replyString << L"201 INFO CONFIG OK\r\n";
1498 boost::property_tree::write_xml(replyString, caspar::env::properties(), w);
1500 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"PATHS"))
1502 replyString << L"201 INFO PATHS OK\r\n";
1504 boost::property_tree::wptree info;
1505 info.add_child(L"paths", caspar::env::properties().get_child(L"configuration.paths"));
1506 info.add(L"paths.initial-path", boost::filesystem::initial_path().wstring() + L"\\");
1508 boost::property_tree::write_xml(replyString, info, w);
1510 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SYSTEM"))
1512 replyString << L"201 INFO SYSTEM OK\r\n";
1514 boost::property_tree::wptree info;
1516 info.add(L"system.name", caspar::system_product_name());
1517 info.add(L"system.windows.name", caspar::win_product_name());
1518 info.add(L"system.windows.service-pack", caspar::win_sp_version());
1519 info.add(L"system.cpu", caspar::cpu_info());
1521 for (auto& device : caspar::decklink::device_list())
1522 info.add(L"system.decklink.device", device);
1524 for (auto& device : caspar::bluefish::device_list())
1525 info.add(L"system.bluefish.device", device);
1527 info.add(L"system.flash", caspar::flash::version());
1528 //info.add(L"system.free-image", caspar::image::version());
1529 info.add(L"system.ffmpeg.avcodec", caspar::ffmpeg::avcodec_version());
1530 info.add(L"system.ffmpeg.avformat", caspar::ffmpeg::avformat_version());
1531 info.add(L"system.ffmpeg.avfilter", caspar::ffmpeg::avfilter_version());
1532 info.add(L"system.ffmpeg.avutil", caspar::ffmpeg::avutil_version());
1533 info.add(L"system.ffmpeg.swscale", caspar::ffmpeg::swscale_version());
1535 boost::property_tree::write_xml(replyString, info, w);
1537 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SERVER"))
1539 replyString << L"201 INFO SERVER OK\r\n";
1541 boost::property_tree::wptree info;
1544 for (auto& channel : channels())
1545 info.add_child(L"channels.channel", channel.channel->info())
1546 .add(L"index", ++index);
1548 boost::property_tree::write_xml(replyString, info, w);
1552 if(parameters().size() >= 1)
1554 replyString << TEXT("201 INFO OK\r\n");
1555 boost::property_tree::wptree info;
1557 std::vector<std::wstring> split;
1558 boost::split(split, parameters()[0], boost::is_any_of("-"));
1560 int layer = std::numeric_limits<int>::min();
1561 int channel = boost::lexical_cast<int>(split[0]) - 1;
1563 if(split.size() > 1)
1564 layer = boost::lexical_cast<int>(split[1]);
1566 if(layer == std::numeric_limits<int>::min())
1568 info.add_child(L"channel", channels().at(channel).channel->info())
1569 .add(L"index", channel);
1573 if(parameters().size() >= 2)
1575 if(boost::iequals(parameters()[1], L"B"))
1576 info.add_child(L"producer", channels().at(channel).channel->stage().background(layer).get()->info());
1578 info.add_child(L"producer", channels().at(channel).channel->stage().foreground(layer).get()->info());
1582 info.add_child(L"layer", channels().at(channel).channel->stage().info(layer).get())
1583 .add(L"index", layer);
1586 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1590 // This is needed for backwards compatibility with old clients
1591 replyString << TEXT("200 INFO OK\r\n");
1592 for(size_t n = 0; n < channels().size(); ++n)
1593 GenerateChannelInfo(n, channels()[n].channel, replyString);
1600 CASPAR_LOG_CURRENT_EXCEPTION();
1601 SetReplyString(TEXT("403 INFO ERROR\r\n"));
1605 replyString << TEXT("\r\n");
1606 SetReplyString(replyString.str());
1610 bool ClsCommand::DoExecute()
1622 std::wstringstream replyString;
1623 replyString << TEXT("200 CLS OK\r\n");
1624 replyString << ListMedia();
1625 replyString << TEXT("\r\n");
1626 SetReplyString(boost::to_upper_copy(replyString.str()));
1630 CASPAR_LOG_CURRENT_EXCEPTION();
1631 SetReplyString(TEXT("501 CLS FAILED\r\n"));
1638 bool TlsCommand::DoExecute()
1642 std::wstringstream replyString;
1643 replyString << TEXT("200 TLS OK\r\n");
1645 replyString << ListTemplates();
1646 replyString << TEXT("\r\n");
1648 SetReplyString(replyString.str());
1652 CASPAR_LOG_CURRENT_EXCEPTION();
1653 SetReplyString(TEXT("501 TLS FAILED\r\n"));
1659 bool VersionCommand::DoExecute()
1661 std::wstring replyString = TEXT("201 VERSION OK\r\n") + env::version() + TEXT("\r\n");
1663 if(parameters().size() > 0)
1665 if(boost::iequals(parameters()[0], L"FLASH"))
1666 replyString = TEXT("201 VERSION OK\r\n") + flash::version() + TEXT("\r\n");
1667 else if(boost::iequals(parameters()[0], L"TEMPLATEHOST"))
1668 replyString = TEXT("201 VERSION OK\r\n") + flash::cg_version() + TEXT("\r\n");
1669 else if(!boost::iequals(parameters()[0], L"SERVER"))
1670 replyString = TEXT("403 VERSION ERROR\r\n");
1673 SetReplyString(replyString);
1677 bool ByeCommand::DoExecute()
1679 client()->disconnect();
1683 bool SetCommand::DoExecute()
1687 std::wstring name = boost::to_upper_copy(parameters()[0]);
1688 std::wstring value = boost::to_upper_copy(parameters()[1]);
1690 if(name == TEXT("MODE"))
1692 auto format_desc = core::video_format_desc(value);
1693 if(format_desc.format != core::video_format::invalid)
1695 channel()->video_format_desc(format_desc);
1696 SetReplyString(TEXT("202 SET MODE OK\r\n"));
1699 SetReplyString(TEXT("501 SET MODE FAILED\r\n"));
1703 this->SetReplyString(TEXT("403 SET ERROR\r\n"));
1708 CASPAR_LOG_CURRENT_EXCEPTION();
1709 SetReplyString(TEXT("501 SET FAILED\r\n"));
1716 bool LockCommand::DoExecute()
1720 auto it = parameters().begin();
1722 std::shared_ptr<caspar::IO::lock_container> lock;
1725 int channel_index = boost::lexical_cast<int>(*it) - 1;
1726 lock = channels().at(channel_index).lock;
1728 catch(const boost::bad_lexical_cast&) {}
1731 SetReplyString(L"401 LOCK ERROR\r\n");
1738 if(it == parameters().end()) //too few parameters
1740 SetReplyString(L"402 LOCK ERROR\r\n");
1744 std::wstring command = boost::to_upper_copy(*it);
1745 if(command == L"ACQUIRE")
1748 if(it == parameters().end()) //too few parameters
1750 SetReplyString(L"402 LOCK ACQUIRE ERROR\r\n");
1753 std::wstring lock_phrase = (*it);
1755 //TODO: read options
1759 //just lock one channel
1760 if(!lock->try_lock(lock_phrase, client()))
1762 SetReplyString(L"503 LOCK ACQUIRE FAILED\r\n");
1768 //TODO: lock all channels
1769 CASPAR_THROW_EXCEPTION(not_implemented());
1771 SetReplyString(L"202 LOCK ACQUIRE OK\r\n");
1774 else if(command == L"RELEASE")
1778 lock->release_lock(client());
1782 //TODO: release all channels
1783 CASPAR_THROW_EXCEPTION(not_implemented());
1785 SetReplyString(L"202 LOCK RELEASE OK\r\n");
1787 else if(command == L"CLEAR")
1789 std::wstring override_phrase = env::properties().get(L"configuration.lock-clear-phrase", L"");
1790 std::wstring client_override_phrase;
1791 if(!override_phrase.empty())
1794 if(it == parameters().end())
1796 SetReplyString(L"402 LOCK CLEAR ERROR\r\n");
1799 client_override_phrase = (*it);
1804 //just clear one channel
1805 if(client_override_phrase != override_phrase)
1807 SetReplyString(L"503 LOCK CLEAR FAILED\r\n");
1811 lock->clear_locks();
1815 //TODO: clear all channels
1816 CASPAR_THROW_EXCEPTION(not_implemented());
1819 SetReplyString(L"202 LOCK CLEAR OK\r\n");
1823 SetReplyString(L"403 LOCK ERROR\r\n");
1827 catch(not_implemented&)
1829 SetReplyString(L"600 LOCK FAILED\r\n");
1834 CASPAR_LOG_CURRENT_EXCEPTION();
1835 SetReplyString(L"501 LOCK FAILED\r\n");
1842 bool ThumbnailCommand::DoExecute()
1844 std::wstring command = boost::to_upper_copy(parameters()[0]);
1846 if (command == TEXT("RETRIEVE"))
1847 return DoExecuteRetrieve();
1848 else if (command == TEXT("LIST"))
1849 return DoExecuteList();
1850 else if (command == TEXT("GENERATE"))
1851 return DoExecuteGenerate();
1852 else if (command == TEXT("GENERATE_ALL"))
1853 return DoExecuteGenerateAll();
1855 SetReplyString(TEXT("403 THUMBNAIL ERROR\r\n"));
1859 bool ThumbnailCommand::DoExecuteRetrieve()
1861 if(parameters().size() < 2)
1863 SetReplyString(TEXT("402 THUMBNAIL RETRIEVE ERROR\r\n"));
1867 std::wstring filename = env::thumbnails_folder();
1868 filename.append(parameters()[1]);
1869 filename.append(TEXT(".png"));
1871 std::wstring file_contents = read_file_base64(boost::filesystem::wpath(filename));
1873 if (file_contents.empty())
1875 SetReplyString(TEXT("404 THUMBNAIL RETRIEVE ERROR\r\n"));
1879 std::wstringstream reply;
1881 reply << L"201 THUMBNAIL RETRIEVE OK\r\n";
1882 reply << file_contents;
1884 SetReplyString(reply.str());
1888 bool ThumbnailCommand::DoExecuteList()
1890 std::wstringstream replyString;
1891 replyString << TEXT("200 THUMBNAIL LIST OK\r\n");
1893 for (boost::filesystem::recursive_directory_iterator itr(env::thumbnails_folder()), end; itr != end; ++itr)
1895 if(boost::filesystem::is_regular_file(itr->path()))
1897 if(!boost::iequals(itr->path().extension().wstring(), L".png"))
1900 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::thumbnails_folder().size()-1, itr->path().wstring().size()));
1902 auto str = relativePath.replace_extension(L"").native();
1903 if(str[0] == '\\' || str[0] == '/')
1904 str = std::wstring(str.begin() + 1, str.end());
1906 auto mtime = boost::filesystem::last_write_time(itr->path());
1907 auto mtime_readable = boost::posix_time::to_iso_wstring(boost::posix_time::from_time_t(mtime));
1908 auto file_size = boost::filesystem::file_size(itr->path());
1910 replyString << L"\"" << str << L"\" " << mtime_readable << L" " << file_size << L"\r\n";
1914 replyString << TEXT("\r\n");
1916 SetReplyString(boost::to_upper_copy(replyString.str()));
1920 bool ThumbnailCommand::DoExecuteGenerate()
1922 if (parameters().size() < 2)
1924 SetReplyString(L"402 THUMBNAIL GENERATE ERROR\r\n");
1930 thumb_gen_->generate(parameters()[1]);
1931 SetReplyString(L"202 THUMBNAIL GENERATE OK\r\n");
1936 SetReplyString(L"500 THUMBNAIL GENERATE ERROR\r\n");
1941 bool ThumbnailCommand::DoExecuteGenerateAll()
1945 thumb_gen_->generate_all();
1946 SetReplyString(L"202 THUMBNAIL GENERATE_ALL OK\r\n");
1951 SetReplyString(L"500 THUMBNAIL GENERATE_ALL ERROR\r\n");
1956 bool KillCommand::DoExecute()
1958 shutdown_server_now_->set_value(false); //false for not attempting to restart
1959 SetReplyString(TEXT("202 KILL OK\r\n"));
1963 bool RestartCommand::DoExecute()
1965 shutdown_server_now_->set_value(true); //true for attempting to restart
1966 SetReplyString(TEXT("202 RESTART OK\r\n"));
1971 }} //namespace caspar