2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Nicklas P Andersson
22 #include "../StdAfx.h"
25 #pragma warning (push, 1) // TODO: Legacy code, just disable warnings
28 #include "AMCPCommandsImpl.h"
29 #include "AMCPProtocolStrategy.h"
31 #include <common/env.h>
33 #include <common/log.h>
34 #include <common/param.h>
35 #include <common/os/system_info.h>
36 #include <common/base64.h>
38 #include <core/producer/frame_producer.h>
39 #include <core/video_format.h>
40 #include <core/producer/transition/transition_producer.h>
41 #include <core/frame/frame_transform.h>
42 #include <core/producer/stage.h>
43 #include <core/producer/layer.h>
44 #include <core/mixer/mixer.h>
45 #include <core/consumer/output.h>
46 #include <core/thumbnail_generator.h>
47 #include <core/diagnostics/call_context.h>
48 #include <core/diagnostics/osd_graph.h>
50 #include <modules/reroute/producer/reroute_producer.h>
51 #include <modules/bluefish/bluefish.h>
52 #include <modules/decklink/decklink.h>
53 #include <modules/ffmpeg/ffmpeg.h>
54 #include <modules/flash/flash.h>
55 #include <modules/flash/util/swf.h>
56 #include <modules/flash/producer/flash_producer.h>
57 #include <modules/flash/producer/cg_proxy.h>
58 #include <modules/ffmpeg/producer/util/util.h>
59 #include <modules/screen/screen.h>
60 #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
168 result | boost::adaptors::transformed(
169 [](char c) { return static_cast<unsigned char>(c); }),
170 std::back_inserter(widened_result));
172 return widened_result;
175 std::wstring read_file(const boost::filesystem::wpath& file)
177 static const uint8_t BOM[] = {0xef, 0xbb, 0xbf};
179 if (!boost::filesystem::exists(file))
184 if (boost::filesystem::file_size(file) >= 3)
186 boost::filesystem::ifstream bom_stream(file);
189 bom_stream.read(header, 3);
192 if (std::memcmp(BOM, header, 3) == 0)
193 return read_utf8_file(file);
196 return read_latin1_file(file);
199 std::wstring MediaInfo(const boost::filesystem::path& path)
201 if(boost::filesystem::is_regular_file(path))
203 std::wstring clipttype = L"N/A";
204 std::wstring extension = boost::to_upper_copy(path.extension().wstring());
205 if(extension == L".TGA" || extension == L".COL" || extension == L".PNG" || extension == L".JPEG" || extension == L".JPG" ||
206 extension == L"GIF" || extension == L"BMP")
207 clipttype = L"STILL";
208 else if(extension == L".WAV" || extension == L".MP3")
209 clipttype = L"AUDIO";
210 else if(extension == L"SWF" || extension == L"CT" || extension == L"DV" || extension == L"MOV" || extension == L"MPG" || extension == L"AVI" || caspar::ffmpeg::is_valid_file(path.wstring()))
211 clipttype = L"MOVIE";
213 if(clipttype != L"N/A")
215 auto is_not_digit = [](char c){ return std::isdigit(c) == 0; };
217 auto relativePath = boost::filesystem::path(path.wstring().substr(env::media_folder().size()-1, path.wstring().size()));
219 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(path)));
220 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), is_not_digit), writeTimeStr.end());
221 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
223 auto sizeStr = boost::lexical_cast<std::wstring>(boost::filesystem::file_size(path));
224 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), is_not_digit), sizeStr.end());
225 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
227 auto str = relativePath.replace_extension(L"").generic_wstring();
228 while(str.size() > 0 && (str[0] == L'\\' || str[0] == L'/'))
229 str = std::wstring(str.begin() + 1, str.end());
231 return std::wstring() + L"\"" + str +
232 + L"\" " + clipttype +
234 + L" " + writeTimeWStr +
241 std::wstring ListMedia()
243 std::wstringstream replyString;
244 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end; ++itr)
245 replyString << MediaInfo(itr->path());
247 return boost::to_upper_copy(replyString.str());
250 std::wstring ListTemplates()
252 std::wstringstream replyString;
254 for (boost::filesystem::recursive_directory_iterator itr(env::template_folder()), end; itr != end; ++itr)
256 if(boost::filesystem::is_regular_file(itr->path()) && (itr->path().extension() == L".ft" || itr->path().extension() == L".ct"))
258 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::template_folder().size()-1, itr->path().wstring().size()));
260 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(itr->path())));
261 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), [](char c){ return std::isdigit(c) == 0;}), writeTimeStr.end());
262 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
264 auto sizeStr = boost::lexical_cast<std::string>(boost::filesystem::file_size(itr->path()));
265 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), [](char c){ return std::isdigit(c) == 0;}), sizeStr.end());
267 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
269 std::wstring dir = relativePath.parent_path().generic_wstring();
270 std::wstring file = boost::to_upper_copy(relativePath.filename().wstring());
271 relativePath = boost::filesystem::wpath(dir + L"/" + file);
273 auto str = relativePath.replace_extension(L"").generic_wstring();
274 boost::trim_if(str, boost::is_any_of("\\/"));
276 replyString << L"\"" << str
277 << L"\" " << sizeWStr
278 << L" " << writeTimeWStr
282 return replyString.str();
287 void AMCPCommand::SendReply()
289 if(replyString_.empty())
292 client_->send(std::move(replyString_));
295 bool DiagnosticsCommand::DoExecute()
299 core::diagnostics::osd::show_graphs(true);
301 SetReplyString(L"202 DIAG OK\r\n");
307 CASPAR_LOG_CURRENT_EXCEPTION();
308 SetReplyString(L"502 DIAG FAILED\r\n");
313 bool ChannelGridCommand::DoExecute()
316 auto self = channels().back().channel;
318 core::diagnostics::scoped_call_context save;
319 core::diagnostics::call_context::for_thread().video_channel = channels().size();
321 std::vector<std::wstring> params;
322 params.push_back(L"SCREEN");
323 params.push_back(L"0");
324 params.push_back(L"NAME");
325 params.push_back(L"Channel Grid Window");
326 auto screen = create_consumer(params);
328 self->output().add(screen);
330 for (auto& channel : channels())
332 if(channel.channel != self)
334 core::diagnostics::call_context::for_thread().layer = index;
335 auto producer = reroute::create_producer(*channel.channel);
336 self->stage().load(index, producer, false);
337 self->stage().play(index);
342 int n = channels().size()-1;
343 double delta = 1.0/static_cast<double>(n);
344 for(int x = 0; x < n; ++x)
346 for(int y = 0; y < n; ++y)
349 auto transform = [=](frame_transform transform) -> frame_transform
351 transform.image_transform.fill_translation[0] = x*delta;
352 transform.image_transform.fill_translation[1] = y*delta;
353 transform.image_transform.fill_scale[0] = delta;
354 transform.image_transform.fill_scale[1] = delta;
355 transform.image_transform.clip_translation[0] = x*delta;
356 transform.image_transform.clip_translation[1] = y*delta;
357 transform.image_transform.clip_scale[0] = delta;
358 transform.image_transform.clip_scale[1] = delta;
361 self->stage().apply_transform(index, transform);
368 bool CallCommand::DoExecute()
370 //Perform loading of the clip
373 auto result = channel()->stage().call(layer_index(), parameters());
375 // TODO: because of std::async deferred timed waiting does not work
377 /*auto wait_res = result.wait_for(std::chrono::seconds(2));
378 if (wait_res == std::future_status::timeout)
379 CASPAR_THROW_EXCEPTION(timed_out());*/
381 std::wstringstream replyString;
382 if(result.get().empty())
383 replyString << L"202 CALL OK\r\n";
385 replyString << L"201 CALL OK\r\n" << result.get() << L"\r\n";
387 SetReplyString(replyString.str());
393 CASPAR_LOG_CURRENT_EXCEPTION();
394 SetReplyString(L"502 CALL FAILED\r\n");
399 tbb::concurrent_unordered_map<int, std::vector<stage::transform_tuple_t>> deferred_transforms;
401 bool MixerCommand::DoExecute()
403 //Perform loading of the clip
406 bool defer = boost::iequals(parameters().back(), L"DEFER");
408 parameters().pop_back();
410 std::vector<stage::transform_tuple_t> transforms;
412 if(boost::iequals(parameters()[0], L"KEYER") || boost::iequals(parameters()[0], L"IS_KEY"))
414 bool value = boost::lexical_cast<int>(parameters().at(1));
415 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
417 transform.image_transform.is_key = value;
421 else if(boost::iequals(parameters()[0], L"OPACITY"))
423 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
424 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
426 double value = boost::lexical_cast<double>(parameters().at(1));
428 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
430 transform.image_transform.opacity = value;
432 }, duration, tween));
434 else if(boost::iequals(parameters()[0], L"FILL") || boost::iequals(parameters()[0], L"FILL_RECT"))
436 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
437 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
438 double x = boost::lexical_cast<double>(parameters().at(1));
439 double y = boost::lexical_cast<double>(parameters().at(2));
440 double x_s = boost::lexical_cast<double>(parameters().at(3));
441 double y_s = boost::lexical_cast<double>(parameters().at(4));
443 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) mutable -> frame_transform
445 transform.image_transform.fill_translation[0] = x;
446 transform.image_transform.fill_translation[1] = y;
447 transform.image_transform.fill_scale[0] = x_s;
448 transform.image_transform.fill_scale[1] = y_s;
450 }, duration, tween));
452 else if(boost::iequals(parameters()[0], L"CLIP") || boost::iequals(parameters()[0], L"CLIP_RECT"))
454 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
455 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
456 double x = boost::lexical_cast<double>(parameters().at(1));
457 double y = boost::lexical_cast<double>(parameters().at(2));
458 double x_s = boost::lexical_cast<double>(parameters().at(3));
459 double y_s = boost::lexical_cast<double>(parameters().at(4));
461 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
463 transform.image_transform.clip_translation[0] = x;
464 transform.image_transform.clip_translation[1] = y;
465 transform.image_transform.clip_scale[0] = x_s;
466 transform.image_transform.clip_scale[1] = y_s;
468 }, duration, tween));
470 else if(boost::iequals(parameters()[0], L"GRID"))
472 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
473 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
474 int n = boost::lexical_cast<int>(parameters().at(1));
475 double delta = 1.0/static_cast<double>(n);
476 for(int x = 0; x < n; ++x)
478 for(int y = 0; y < n; ++y)
481 transforms.push_back(stage::transform_tuple_t(index, [=](frame_transform transform) -> frame_transform
483 transform.image_transform.fill_translation[0] = x*delta;
484 transform.image_transform.fill_translation[1] = y*delta;
485 transform.image_transform.fill_scale[0] = delta;
486 transform.image_transform.fill_scale[1] = delta;
487 transform.image_transform.clip_translation[0] = x*delta;
488 transform.image_transform.clip_translation[1] = y*delta;
489 transform.image_transform.clip_scale[0] = delta;
490 transform.image_transform.clip_scale[1] = delta;
492 }, duration, tween));
496 else if(boost::iequals(parameters()[0], L"BLEND"))
498 auto blend_str = parameters().at(1);
499 int layer = layer_index();
500 channel()->mixer().set_blend_mode(layer, get_blend_mode(blend_str));
502 else if(boost::iequals(parameters()[0], L"MASTERVOLUME"))
504 float master_volume = boost::lexical_cast<float>(parameters().at(1));
505 channel()->mixer().set_master_volume(master_volume);
507 else if(boost::iequals(parameters()[0], L"BRIGHTNESS"))
509 auto value = boost::lexical_cast<double>(parameters().at(1));
510 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
511 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
512 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
514 transform.image_transform.brightness = value;
516 }, duration, tween));
518 else if(boost::iequals(parameters()[0], L"SATURATION"))
520 auto value = boost::lexical_cast<double>(parameters().at(1));
521 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
522 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
523 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
525 transform.image_transform.saturation = value;
527 }, duration, tween));
529 else if(parameters()[0] == L"CONTRAST")
531 auto value = boost::lexical_cast<double>(parameters().at(1));
532 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
533 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
534 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
536 transform.image_transform.contrast = value;
538 }, duration, tween));
540 else if(boost::iequals(parameters()[0], L"LEVELS"))
543 value.min_input = boost::lexical_cast<double>(parameters().at(1));
544 value.max_input = boost::lexical_cast<double>(parameters().at(2));
545 value.gamma = boost::lexical_cast<double>(parameters().at(3));
546 value.min_output = boost::lexical_cast<double>(parameters().at(4));
547 value.max_output = boost::lexical_cast<double>(parameters().at(5));
548 int duration = parameters().size() > 6 ? boost::lexical_cast<int>(parameters()[6]) : 0;
549 std::wstring tween = parameters().size() > 7 ? parameters()[7] : L"linear";
551 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
553 transform.image_transform.levels = value;
555 }, duration, tween));
557 else if(boost::iequals(parameters()[0], L"VOLUME"))
559 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
560 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
561 double value = boost::lexical_cast<double>(parameters()[1]);
563 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
565 transform.audio_transform.volume = value;
567 }, duration, tween));
569 else if(boost::iequals(parameters()[0], L"CLEAR"))
571 int layer = layer_index(std::numeric_limits<int>::max());
573 if (layer == std::numeric_limits<int>::max())
575 channel()->stage().clear_transforms();
576 channel()->mixer().clear_blend_modes();
580 channel()->stage().clear_transforms(layer);
581 channel()->mixer().clear_blend_mode(layer);
584 else if(boost::iequals(parameters()[0], L"COMMIT"))
586 transforms = std::move(deferred_transforms[channel_index()]);
590 SetReplyString(L"404 MIXER ERROR\r\n");
596 auto& defer_tranforms = deferred_transforms[channel_index()];
597 defer_tranforms.insert(defer_tranforms.end(), transforms.begin(), transforms.end());
600 channel()->stage().apply_transforms(transforms);
602 SetReplyString(L"202 MIXER OK\r\n");
606 catch(file_not_found&)
608 CASPAR_LOG_CURRENT_EXCEPTION();
609 SetReplyString(L"404 MIXER ERROR\r\n");
614 CASPAR_LOG_CURRENT_EXCEPTION();
615 SetReplyString(L"502 MIXER FAILED\r\n");
620 bool SwapCommand::DoExecute()
622 //Perform loading of the clip
625 if(layer_index(-1) != -1)
627 std::vector<std::string> strs;
628 boost::split(strs, parameters()[0], boost::is_any_of("-"));
630 auto ch1 = channel();
631 auto ch2 = channels().at(boost::lexical_cast<int>(strs.at(0))-1);
633 int l1 = layer_index();
634 int l2 = boost::lexical_cast<int>(strs.at(1));
636 ch1->stage().swap_layer(l1, l2, ch2.channel->stage());
640 auto ch1 = channel();
641 auto ch2 = channels().at(boost::lexical_cast<int>(parameters()[0])-1);
642 ch1->stage().swap_layers(ch2.channel->stage());
645 SetReplyString(L"202 SWAP OK\r\n");
649 catch(file_not_found&)
651 CASPAR_LOG_CURRENT_EXCEPTION();
652 SetReplyString(L"404 SWAP ERROR\r\n");
657 CASPAR_LOG_CURRENT_EXCEPTION();
658 SetReplyString(L"502 SWAP FAILED\r\n");
663 bool AddCommand::DoExecute()
665 //Perform loading of the clip
668 //create_consumer still expects all parameters to be uppercase
669 for (auto& str : parameters())
671 boost::to_upper(str);
674 core::diagnostics::scoped_call_context save;
675 core::diagnostics::call_context::for_thread().video_channel = channel_index() + 1;
677 auto consumer = create_consumer(parameters());
678 channel()->output().add(layer_index(consumer->index()), consumer);
680 SetReplyString(L"202 ADD OK\r\n");
684 catch(file_not_found&)
686 CASPAR_LOG_CURRENT_EXCEPTION();
687 SetReplyString(L"404 ADD ERROR\r\n");
692 CASPAR_LOG_CURRENT_EXCEPTION();
693 SetReplyString(L"502 ADD FAILED\r\n");
698 bool RemoveCommand::DoExecute()
700 //Perform loading of the clip
703 auto index = layer_index(std::numeric_limits<int>::min());
704 if(index == std::numeric_limits<int>::min())
706 //create_consumer still expects all parameters to be uppercase
707 for (auto& str : parameters())
709 boost::to_upper(str);
712 index = create_consumer(parameters())->index();
715 channel()->output().remove(index);
717 SetReplyString(L"202 REMOVE OK\r\n");
721 catch(file_not_found&)
723 CASPAR_LOG_CURRENT_EXCEPTION();
724 SetReplyString(L"404 REMOVE ERROR\r\n");
729 CASPAR_LOG_CURRENT_EXCEPTION();
730 SetReplyString(L"502 REMOVE FAILED\r\n");
735 bool LoadCommand::DoExecute()
737 //Perform loading of the clip
740 core::diagnostics::scoped_call_context save;
741 core::diagnostics::call_context::for_thread().video_channel = channel_index() + 1;
742 core::diagnostics::call_context::for_thread().layer = layer_index();
743 auto pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
744 channel()->stage().load(layer_index(), pFP, true);
746 SetReplyString(L"202 LOAD OK\r\n");
750 catch(file_not_found&)
752 CASPAR_LOG_CURRENT_EXCEPTION();
753 SetReplyString(L"404 LOAD ERROR\r\n");
758 CASPAR_LOG_CURRENT_EXCEPTION();
759 SetReplyString(L"502 LOAD FAILED\r\n");
766 //std::function<std::wstring()> channel_cg_add_command::parse(const std::wstring& message, const std::vector<renderer::render_device_ptr>& channels)
768 // 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>.*)?");
770 // boost::wsmatch what;
771 // if(!boost::regex_match(message, what, expr))
774 // auto info = channel_info::parse(what, channels);
776 // int flash_layer_index = boost::lexical_cast<int>(what["FLASH_LAYER"].str());
778 // std::wstring templatename = what["TEMPLATE"].str();
779 // bool play_on_load = what["PLAY_ON_LOAD"].matched ? what["PLAY_ON_LOAD"].str() != L"0" : 0;
780 // std::wstring start_label = what["START_LABEL"].str();
781 // std::wstring data = get_data(what["DATA"].str());
783 // boost::replace_all(templatename, "\"", "");
785 // return [=]() -> std::wstring
787 // std::wstring fullFilename = flash::flash_producer::find_template(server::template_folder() + templatename);
788 // if(fullFilename.empty())
789 // CASPAR_THROW_EXCEPTION(file_not_found());
791 // std::wstring extension = boost::filesystem::wpath(fullFilename).extension();
792 // std::wstring filename = templatename;
793 // filename.append(extension);
795 // flash::flash::create_cg_proxy(info.video_channel, std::max<int>(DEFAULT_CHANNEL_LAYER+1, info.layer_index))
796 // ->add(flash_layer_index, filename, play_on_load, start_label, data);
798 // CASPAR_LOG(info) << L"Executed [amcp_channel_cg_add]";
802 bool LoadbgCommand::DoExecute()
804 transition_info transitionInfo;
808 std::wstring message;
809 for(size_t n = 0; n < parameters().size(); ++n)
810 message += boost::to_upper_copy(parameters()[n]) + L" ";
812 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)?.*)");
814 if(boost::regex_match(message, what, expr))
816 auto transition = what["TRANSITION"].str();
817 transitionInfo.duration = boost::lexical_cast<size_t>(what["DURATION"].str());
818 auto direction = what["DIRECTION"].matched ? what["DIRECTION"].str() : L"";
819 auto tween = what["TWEEN"].matched ? what["TWEEN"].str() : L"";
820 transitionInfo.tweener = tween;
822 if(transition == L"CUT")
823 transitionInfo.type = transition_type::cut;
824 else if(transition == L"MIX")
825 transitionInfo.type = transition_type::mix;
826 else if(transition == L"PUSH")
827 transitionInfo.type = transition_type::push;
828 else if(transition == L"SLIDE")
829 transitionInfo.type = transition_type::slide;
830 else if(transition == L"WIPE")
831 transitionInfo.type = transition_type::wipe;
833 if(direction == L"FROMLEFT")
834 transitionInfo.direction = transition_direction::from_left;
835 else if(direction == L"FROMRIGHT")
836 transitionInfo.direction = transition_direction::from_right;
837 else if(direction == L"LEFT")
838 transitionInfo.direction = transition_direction::from_right;
839 else if(direction == L"RIGHT")
840 transitionInfo.direction = transition_direction::from_left;
843 //Perform loading of the clip
846 std::shared_ptr<core::frame_producer> pFP;
848 static boost::wregex expr(LR"(\[(?<CHANNEL>\d+)\])", boost::regex::icase);
850 core::diagnostics::scoped_call_context save;
851 core::diagnostics::call_context::for_thread().video_channel = channel_index() + 1;
852 core::diagnostics::call_context::for_thread().layer = layer_index();
855 if(boost::regex_match(parameters().at(0), what, expr))
857 auto channel_index = boost::lexical_cast<int>(what["CHANNEL"].str());
858 pFP = reroute::create_producer(*channels().at(channel_index-1).channel);
862 pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
865 if(pFP == frame_producer::empty())
866 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info(parameters().size() > 0 ? parameters()[0] : L""));
868 bool auto_play = contains_param(L"AUTO", parameters());
870 auto pFP2 = create_transition_producer(channel()->video_format_desc().field_mode, spl::make_shared_ptr(pFP), transitionInfo);
872 channel()->stage().load(layer_index(), pFP2, false, transitionInfo.duration); // TODO: LOOP
874 channel()->stage().load(layer_index(), pFP2, false); // TODO: LOOP
877 SetReplyString(L"202 LOADBG OK\r\n");
881 catch(file_not_found&)
883 CASPAR_LOG(error) << L"File not found. No match found for parameters. Check syntax.";
884 SetReplyString(L"404 LOADBG ERROR\r\n");
889 CASPAR_LOG_CURRENT_EXCEPTION();
890 SetReplyString(L"502 LOADBG FAILED\r\n");
895 bool PauseCommand::DoExecute()
899 channel()->stage().pause(layer_index());
900 SetReplyString(L"202 PAUSE OK\r\n");
905 SetReplyString(L"501 PAUSE FAILED\r\n");
911 bool PlayCommand::DoExecute()
915 if(!parameters().empty())
917 LoadbgCommand lbg(*this);
920 throw std::exception();
923 channel()->stage().play(layer_index());
925 SetReplyString(L"202 PLAY OK\r\n");
930 SetReplyString(L"501 PLAY FAILED\r\n");
936 bool StopCommand::DoExecute()
940 channel()->stage().stop(layer_index());
941 SetReplyString(L"202 STOP OK\r\n");
946 SetReplyString(L"501 STOP FAILED\r\n");
952 bool ClearCommand::DoExecute()
954 int index = layer_index(std::numeric_limits<int>::min());
955 if(index != std::numeric_limits<int>::min())
956 channel()->stage().clear(index);
958 channel()->stage().clear();
960 SetReplyString(L"202 CLEAR OK\r\n");
965 bool PrintCommand::DoExecute()
967 channel()->output().add(create_consumer({ L"IMAGE" }));
969 SetReplyString(L"202 PRINT OK\r\n");
974 bool LogCommand::DoExecute()
976 if(boost::iequals(parameters().at(0), L"LEVEL"))
977 log::set_log_level(parameters().at(1));
979 SetReplyString(L"202 LOG OK\r\n");
984 bool CGCommand::DoExecute()
988 std::wstring command = boost::to_upper_copy(parameters()[0]);
989 if(command == L"ADD")
990 return DoExecuteAdd();
991 else if(command == L"PLAY")
992 return DoExecutePlay();
993 else if(command == L"STOP")
994 return DoExecuteStop();
995 else if(command == L"NEXT")
996 return DoExecuteNext();
997 else if(command == L"REMOVE")
998 return DoExecuteRemove();
999 else if(command == L"CLEAR")
1000 return DoExecuteClear();
1001 else if(command == L"UPDATE")
1002 return DoExecuteUpdate();
1003 else if(command == L"INVOKE")
1004 return DoExecuteInvoke();
1005 else if(command == L"INFO")
1006 return DoExecuteInfo();
1010 CASPAR_LOG_CURRENT_EXCEPTION();
1013 SetReplyString(L"403 CG ERROR\r\n");
1017 bool CGCommand::ValidateLayer(const std::wstring& layerstring) {
1018 int length = layerstring.length();
1019 for(int i = 0; i < length; ++i) {
1020 if(!std::isdigit(layerstring[i])) {
1028 bool CGCommand::DoExecuteAdd() {
1029 //CG 1 ADD 0 "template_folder/templatename" [STARTLABEL] 0/1 [DATA]
1031 int layer = 0; //_parameters[1]
1032 // std::wstring templateName; //_parameters[2]
1033 std::wstring label; //_parameters[3]
1034 bool bDoStart = false; //_parameters[3] alt. _parameters[4]
1035 // std::wstring data; //_parameters[4] alt. _parameters[5]
1037 if(parameters().size() < 4)
1039 SetReplyString(L"402 CG ERROR\r\n");
1042 unsigned int dataIndex = 4;
1044 if(!ValidateLayer(parameters()[1]))
1046 SetReplyString(L"403 CG ERROR\r\n");
1050 layer = boost::lexical_cast<int>(parameters()[1]);
1052 if(parameters()[3].length() > 1)
1054 label = parameters()[3];
1057 if(parameters().size() > 4 && parameters()[4].length() > 0) //read play-on-load-flag
1058 bDoStart = (parameters()[4][0]==L'1') ? true : false;
1061 SetReplyString(L"402 CG ERROR\r\n");
1065 else if(parameters()[3].length() > 0) { //read play-on-load-flag
1066 bDoStart = (parameters()[3][0]==L'1') ? true : false;
1070 SetReplyString(L"403 CG ERROR\r\n");
1074 const wchar_t* pDataString = 0;
1075 std::wstring dataFromFile;
1076 if(parameters().size() > dataIndex)
1078 const std::wstring& dataString = parameters()[dataIndex];
1080 if(dataString[0] == L'<') //the data is an XML-string
1081 pDataString = dataString.c_str();
1084 //The data is not an XML-string, it must be a filename
1085 std::wstring filename = env::data_folder();
1086 filename.append(dataString);
1087 filename.append(L".ftd");
1089 dataFromFile = read_file(boost::filesystem::wpath(filename));
1090 pDataString = dataFromFile.c_str();
1094 std::wstring fullFilename = flash::find_template(env::template_folder() + parameters()[2]);
1095 if(!fullFilename.empty())
1097 std::wstring extension = boost::filesystem::path(fullFilename).extension().wstring();
1098 std::wstring filename = parameters()[2];
1099 filename.append(extension);
1101 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 : L"");
1102 SetReplyString(L"202 CG OK\r\n");
1106 CASPAR_LOG(warning) << "Could not find template " << parameters()[2];
1107 SetReplyString(L"404 CG ERROR\r\n");
1112 bool CGCommand::DoExecutePlay()
1114 if(parameters().size() > 1)
1116 if(!ValidateLayer(parameters()[1]))
1118 SetReplyString(L"403 CG ERROR\r\n");
1121 int layer = boost::lexical_cast<int>(parameters()[1]);
1122 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).play(layer);
1126 SetReplyString(L"402 CG ERROR\r\n");
1130 SetReplyString(L"202 CG OK\r\n");
1134 bool CGCommand::DoExecuteStop()
1136 if(parameters().size() > 1)
1138 if(!ValidateLayer(parameters()[1]))
1140 SetReplyString(L"403 CG ERROR\r\n");
1143 int layer = boost::lexical_cast<int>(parameters()[1]);
1144 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).stop(layer, 0);
1148 SetReplyString(L"402 CG ERROR\r\n");
1152 SetReplyString(L"202 CG OK\r\n");
1156 bool CGCommand::DoExecuteNext()
1158 if(parameters().size() > 1)
1160 if(!ValidateLayer(parameters()[1]))
1162 SetReplyString(L"403 CG ERROR\r\n");
1166 int layer = boost::lexical_cast<int>(parameters()[1]);
1167 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).next(layer);
1171 SetReplyString(L"402 CG ERROR\r\n");
1175 SetReplyString(L"202 CG OK\r\n");
1179 bool CGCommand::DoExecuteRemove()
1181 if(parameters().size() > 1)
1183 if(!ValidateLayer(parameters()[1]))
1185 SetReplyString(L"403 CG ERROR\r\n");
1189 int layer = boost::lexical_cast<int>(parameters()[1]);
1190 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).remove(layer);
1194 SetReplyString(L"402 CG ERROR\r\n");
1198 SetReplyString(L"202 CG OK\r\n");
1202 bool CGCommand::DoExecuteClear()
1204 channel()->stage().clear(layer_index(flash::cg_proxy::DEFAULT_LAYER));
1205 SetReplyString(L"202 CG OK\r\n");
1209 bool CGCommand::DoExecuteUpdate()
1213 if(!ValidateLayer(parameters().at(1)))
1215 SetReplyString(L"403 CG ERROR\r\n");
1219 std::wstring dataString = parameters().at(2);
1220 if(dataString.at(0) != L'<')
1222 //The data is not an XML-string, it must be a filename
1223 std::wstring filename = env::data_folder();
1224 filename.append(dataString);
1225 filename.append(L".ftd");
1227 dataString = read_file(boost::filesystem::wpath(filename));
1230 int layer = boost::lexical_cast<int>(parameters()[1]);
1231 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).update(layer, dataString);
1235 SetReplyString(L"402 CG ERROR\r\n");
1239 SetReplyString(L"202 CG OK\r\n");
1243 bool CGCommand::DoExecuteInvoke()
1245 std::wstringstream replyString;
1246 replyString << L"201 CG OK\r\n";
1248 if(parameters().size() > 2)
1250 if(!ValidateLayer(parameters()[1]))
1252 SetReplyString(L"403 CG ERROR\r\n");
1255 int layer = boost::lexical_cast<int>(parameters()[1]);
1256 auto result = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).invoke(layer, parameters()[2]);
1257 replyString << result << L"\r\n";
1261 SetReplyString(L"402 CG ERROR\r\n");
1265 SetReplyString(replyString.str());
1269 bool CGCommand::DoExecuteInfo()
1271 std::wstringstream replyString;
1272 replyString << L"201 CG OK\r\n";
1274 if(parameters().size() > 1)
1276 if(!ValidateLayer(parameters()[1]))
1278 SetReplyString(L"403 CG ERROR\r\n");
1282 int layer = boost::lexical_cast<int>(parameters()[1]);
1283 auto desc = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).description(layer);
1285 replyString << desc << L"\r\n";
1289 auto info = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).template_host_info();
1290 replyString << info << L"\r\n";
1293 SetReplyString(replyString.str());
1297 bool DataCommand::DoExecute()
1299 std::wstring command = boost::to_upper_copy(parameters()[0]);
1300 if(command == L"STORE")
1301 return DoExecuteStore();
1302 else if(command == L"RETRIEVE")
1303 return DoExecuteRetrieve();
1304 else if(command == L"REMOVE")
1305 return DoExecuteRemove();
1306 else if(command == L"LIST")
1307 return DoExecuteList();
1309 SetReplyString(L"403 DATA ERROR\r\n");
1313 bool DataCommand::DoExecuteStore()
1315 if(parameters().size() < 3)
1317 SetReplyString(L"402 DATA STORE ERROR\r\n");
1321 std::wstring filename = env::data_folder();
1322 filename.append(parameters()[1]);
1323 filename.append(L".ftd");
1325 auto data_path = boost::filesystem::wpath(
1326 boost::filesystem::wpath(filename).parent_path());
1328 if(!boost::filesystem::exists(data_path))
1329 boost::filesystem::create_directories(data_path);
1331 boost::filesystem::wofstream datafile(filename);
1334 SetReplyString(L"501 DATA STORE FAILED\r\n");
1338 datafile << static_cast<wchar_t>(65279); // UTF-8 BOM character
1339 datafile << parameters()[2] << std::flush;
1342 std::wstring replyString = L"202 DATA STORE OK\r\n";
1343 SetReplyString(replyString);
1347 bool DataCommand::DoExecuteRetrieve()
1349 if(parameters().size() < 2)
1351 SetReplyString(L"402 DATA RETRIEVE ERROR\r\n");
1355 std::wstring filename = env::data_folder();
1356 filename.append(parameters()[1]);
1357 filename.append(L".ftd");
1359 std::wstring file_contents = read_file(boost::filesystem::wpath(filename));
1361 if (file_contents.empty())
1363 SetReplyString(L"404 DATA RETRIEVE ERROR\r\n");
1367 std::wstringstream reply(L"201 DATA RETRIEVE OK\r\n");
1369 std::wstringstream file_contents_stream(file_contents);
1372 bool firstLine = true;
1373 while(std::getline(file_contents_stream, line))
1384 SetReplyString(reply.str());
1388 bool DataCommand::DoExecuteRemove()
1390 if (parameters().size() < 2)
1392 SetReplyString(L"402 DATA REMOVE ERROR\r\n");
1396 std::wstring filename = env::data_folder();
1397 filename.append(parameters()[1]);
1398 filename.append(L".ftd");
1400 if (!boost::filesystem::exists(filename))
1402 SetReplyString(L"404 DATA REMOVE ERROR\r\n");
1406 if (!boost::filesystem::remove(filename))
1408 SetReplyString(L"403 DATA REMOVE ERROR\r\n");
1412 SetReplyString(L"201 DATA REMOVE OK\r\n");
1417 bool DataCommand::DoExecuteList()
1419 std::wstringstream replyString;
1420 replyString << L"200 DATA LIST OK\r\n";
1422 for (boost::filesystem::recursive_directory_iterator itr(env::data_folder()), end; itr != end; ++itr)
1424 if(boost::filesystem::is_regular_file(itr->path()))
1426 if(!boost::iequals(itr->path().extension().wstring(), L".ftd"))
1429 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::data_folder().size()-1, itr->path().wstring().size()));
1431 auto str = relativePath.replace_extension(L"").generic_wstring();
1432 if(str[0] == L'\\' || str[0] == L'/')
1433 str = std::wstring(str.begin() + 1, str.end());
1435 replyString << str << L"\r\n";
1439 replyString << L"\r\n";
1441 SetReplyString(boost::to_upper_copy(replyString.str()));
1445 bool CinfCommand::DoExecute()
1447 std::wstringstream replyString;
1452 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end && info.empty(); ++itr)
1454 auto path = itr->path();
1455 auto file = path.replace_extension(L"").filename();
1456 if(boost::iequals(file.wstring(), parameters().at(0)))
1457 info += MediaInfo(itr->path()) + L"\r\n";
1462 SetReplyString(L"404 CINF ERROR\r\n");
1465 replyString << L"200 CINF OK\r\n";
1466 replyString << info << "\r\n";
1470 CASPAR_LOG_CURRENT_EXCEPTION();
1471 SetReplyString(L"404 CINF ERROR\r\n");
1475 SetReplyString(replyString.str());
1479 void GenerateChannelInfo(int index, const spl::shared_ptr<core::video_channel>& pChannel, std::wstringstream& replyString)
1481 replyString << index+1 << L" " << pChannel->video_format_desc().name << L" PLAYING\r\n";
1484 bool InfoCommand::DoExecute()
1486 std::wstringstream replyString;
1488 boost::property_tree::xml_writer_settings<std::wstring> w(' ', 3);
1492 if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"TEMPLATE"))
1494 replyString << L"201 INFO TEMPLATE OK\r\n";
1496 // Needs to be extended for any file, not just flash.
1498 auto filename = flash::find_template(env::template_folder() + parameters().at(1));
1500 std::wstringstream str;
1501 str << u16(flash::read_template_meta_info(filename));
1502 boost::property_tree::wptree info;
1503 boost::property_tree::xml_parser::read_xml(str, info, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);
1505 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1507 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"CONFIG"))
1509 replyString << L"201 INFO CONFIG OK\r\n";
1511 boost::property_tree::write_xml(replyString, caspar::env::properties(), w);
1513 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"PATHS"))
1515 replyString << L"201 INFO PATHS OK\r\n";
1517 boost::property_tree::wptree info;
1518 info.add_child(L"paths", caspar::env::properties().get_child(L"configuration.paths"));
1519 info.add(L"paths.initial-path", boost::filesystem::initial_path().wstring() + L"\\");
1521 boost::property_tree::write_xml(replyString, info, w);
1523 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SYSTEM"))
1525 replyString << L"201 INFO SYSTEM OK\r\n";
1527 boost::property_tree::wptree info;
1529 info.add(L"system.name", caspar::system_product_name());
1530 info.add(L"system.os.description", caspar::os_description());
1531 info.add(L"system.cpu", caspar::cpu_info());
1533 for (auto& device : caspar::decklink::device_list())
1534 info.add(L"system.decklink.device", device);
1536 for (auto& device : caspar::bluefish::device_list())
1537 info.add(L"system.bluefish.device", device);
1539 info.add(L"system.flash", caspar::flash::version());
1540 info.add(L"system.ffmpeg.avcodec", caspar::ffmpeg::avcodec_version());
1541 info.add(L"system.ffmpeg.avformat", caspar::ffmpeg::avformat_version());
1542 info.add(L"system.ffmpeg.avfilter", caspar::ffmpeg::avfilter_version());
1543 info.add(L"system.ffmpeg.avutil", caspar::ffmpeg::avutil_version());
1544 info.add(L"system.ffmpeg.swscale", caspar::ffmpeg::swscale_version());
1546 boost::property_tree::write_xml(replyString, info, w);
1548 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SERVER"))
1550 replyString << L"201 INFO SERVER OK\r\n";
1552 boost::property_tree::wptree info;
1555 for (auto& channel : channels())
1556 info.add_child(L"channels.channel", channel.channel->info())
1557 .add(L"index", ++index);
1559 boost::property_tree::write_xml(replyString, info, w);
1563 if(parameters().size() >= 1)
1565 replyString << L"201 INFO OK\r\n";
1566 boost::property_tree::wptree info;
1568 std::vector<std::wstring> split;
1569 boost::split(split, parameters()[0], boost::is_any_of("-"));
1571 int layer = std::numeric_limits<int>::min();
1572 int channel = boost::lexical_cast<int>(split[0]) - 1;
1574 if(split.size() > 1)
1575 layer = boost::lexical_cast<int>(split[1]);
1577 if(layer == std::numeric_limits<int>::min())
1579 info.add_child(L"channel", channels().at(channel).channel->info())
1580 .add(L"index", channel);
1584 if(parameters().size() >= 2)
1586 if(boost::iequals(parameters()[1], L"B"))
1587 info.add_child(L"producer", channels().at(channel).channel->stage().background(layer).get()->info());
1589 info.add_child(L"producer", channels().at(channel).channel->stage().foreground(layer).get()->info());
1593 info.add_child(L"layer", channels().at(channel).channel->stage().info(layer).get())
1594 .add(L"index", layer);
1597 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1601 // This is needed for backwards compatibility with old clients
1602 replyString << L"200 INFO OK\r\n";
1603 for(size_t n = 0; n < channels().size(); ++n)
1604 GenerateChannelInfo(n, channels()[n].channel, replyString);
1611 CASPAR_LOG_CURRENT_EXCEPTION();
1612 SetReplyString(L"403 INFO ERROR\r\n");
1616 replyString << L"\r\n";
1617 SetReplyString(replyString.str());
1621 bool ClsCommand::DoExecute()
1633 std::wstringstream replyString;
1634 replyString << L"200 CLS OK\r\n";
1635 replyString << ListMedia();
1636 replyString << L"\r\n";
1637 SetReplyString(boost::to_upper_copy(replyString.str()));
1641 CASPAR_LOG_CURRENT_EXCEPTION();
1642 SetReplyString(L"501 CLS FAILED\r\n");
1649 bool TlsCommand::DoExecute()
1653 std::wstringstream replyString;
1654 replyString << L"200 TLS OK\r\n";
1656 replyString << ListTemplates();
1657 replyString << L"\r\n";
1659 SetReplyString(replyString.str());
1663 CASPAR_LOG_CURRENT_EXCEPTION();
1664 SetReplyString(L"501 TLS FAILED\r\n");
1670 bool VersionCommand::DoExecute()
1672 std::wstring replyString = L"201 VERSION OK\r\n" + env::version() + L"\r\n";
1674 if(parameters().size() > 0)
1676 if(boost::iequals(parameters()[0], L"FLASH"))
1677 replyString = L"201 VERSION OK\r\n" + flash::version() + L"\r\n";
1678 else if(boost::iequals(parameters()[0], L"TEMPLATEHOST"))
1679 replyString = L"201 VERSION OK\r\n" + flash::cg_version() + L"\r\n";
1680 else if(!boost::iequals(parameters()[0], L"SERVER"))
1681 replyString = L"403 VERSION ERROR\r\n";
1684 SetReplyString(replyString);
1688 bool ByeCommand::DoExecute()
1690 client()->disconnect();
1694 bool SetCommand::DoExecute()
1698 std::wstring name = boost::to_upper_copy(parameters()[0]);
1699 std::wstring value = boost::to_upper_copy(parameters()[1]);
1703 auto format_desc = core::video_format_desc(value);
1704 if(format_desc.format != core::video_format::invalid)
1706 channel()->video_format_desc(format_desc);
1707 SetReplyString(L"202 SET MODE OK\r\n");
1710 SetReplyString(L"501 SET MODE FAILED\r\n");
1714 this->SetReplyString(L"403 SET ERROR\r\n");
1719 CASPAR_LOG_CURRENT_EXCEPTION();
1720 SetReplyString(L"501 SET FAILED\r\n");
1727 bool LockCommand::DoExecute()
1731 auto it = parameters().begin();
1733 std::shared_ptr<caspar::IO::lock_container> lock;
1736 int channel_index = boost::lexical_cast<int>(*it) - 1;
1737 lock = channels().at(channel_index).lock;
1739 catch(const boost::bad_lexical_cast&) {}
1742 SetReplyString(L"401 LOCK ERROR\r\n");
1749 if(it == parameters().end()) //too few parameters
1751 SetReplyString(L"402 LOCK ERROR\r\n");
1755 std::wstring command = boost::to_upper_copy(*it);
1756 if(command == L"ACQUIRE")
1759 if(it == parameters().end()) //too few parameters
1761 SetReplyString(L"402 LOCK ACQUIRE ERROR\r\n");
1764 std::wstring lock_phrase = (*it);
1766 //TODO: read options
1770 //just lock one channel
1771 if(!lock->try_lock(lock_phrase, client()))
1773 SetReplyString(L"503 LOCK ACQUIRE FAILED\r\n");
1779 //TODO: lock all channels
1780 CASPAR_THROW_EXCEPTION(not_implemented());
1782 SetReplyString(L"202 LOCK ACQUIRE OK\r\n");
1785 else if(command == L"RELEASE")
1789 lock->release_lock(client());
1793 //TODO: release all channels
1794 CASPAR_THROW_EXCEPTION(not_implemented());
1796 SetReplyString(L"202 LOCK RELEASE OK\r\n");
1798 else if(command == L"CLEAR")
1800 std::wstring override_phrase = env::properties().get(L"configuration.lock-clear-phrase", L"");
1801 std::wstring client_override_phrase;
1802 if(!override_phrase.empty())
1805 if(it == parameters().end())
1807 SetReplyString(L"402 LOCK CLEAR ERROR\r\n");
1810 client_override_phrase = (*it);
1815 //just clear one channel
1816 if(client_override_phrase != override_phrase)
1818 SetReplyString(L"503 LOCK CLEAR FAILED\r\n");
1822 lock->clear_locks();
1826 //TODO: clear all channels
1827 CASPAR_THROW_EXCEPTION(not_implemented());
1830 SetReplyString(L"202 LOCK CLEAR OK\r\n");
1834 SetReplyString(L"403 LOCK ERROR\r\n");
1838 catch(not_implemented&)
1840 SetReplyString(L"600 LOCK FAILED\r\n");
1845 CASPAR_LOG_CURRENT_EXCEPTION();
1846 SetReplyString(L"501 LOCK FAILED\r\n");
1853 bool ThumbnailCommand::DoExecute()
1855 std::wstring command = boost::to_upper_copy(parameters()[0]);
1857 if (command == L"RETRIEVE")
1858 return DoExecuteRetrieve();
1859 else if (command == L"LIST")
1860 return DoExecuteList();
1861 else if (command == L"GENERATE")
1862 return DoExecuteGenerate();
1863 else if (command == L"GENERATE_ALL")
1864 return DoExecuteGenerateAll();
1866 SetReplyString(L"403 THUMBNAIL ERROR\r\n");
1870 bool ThumbnailCommand::DoExecuteRetrieve()
1872 if(parameters().size() < 2)
1874 SetReplyString(L"402 THUMBNAIL RETRIEVE ERROR\r\n");
1878 std::wstring filename = env::thumbnails_folder();
1879 filename.append(parameters()[1]);
1880 filename.append(L".png");
1882 std::wstring file_contents = read_file_base64(boost::filesystem::wpath(filename));
1884 if (file_contents.empty())
1886 SetReplyString(L"404 THUMBNAIL RETRIEVE ERROR\r\n");
1890 std::wstringstream reply;
1892 reply << L"201 THUMBNAIL RETRIEVE OK\r\n";
1893 reply << file_contents;
1895 SetReplyString(reply.str());
1899 bool ThumbnailCommand::DoExecuteList()
1901 std::wstringstream replyString;
1902 replyString << L"200 THUMBNAIL LIST OK\r\n";
1904 for (boost::filesystem::recursive_directory_iterator itr(env::thumbnails_folder()), end; itr != end; ++itr)
1906 if(boost::filesystem::is_regular_file(itr->path()))
1908 if(!boost::iequals(itr->path().extension().wstring(), L".png"))
1911 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::thumbnails_folder().size()-1, itr->path().wstring().size()));
1913 auto str = relativePath.replace_extension(L"").generic_wstring();
1914 if(str[0] == '\\' || str[0] == '/')
1915 str = std::wstring(str.begin() + 1, str.end());
1917 auto mtime = boost::filesystem::last_write_time(itr->path());
1918 auto mtime_readable = boost::posix_time::to_iso_wstring(boost::posix_time::from_time_t(mtime));
1919 auto file_size = boost::filesystem::file_size(itr->path());
1921 replyString << L"\"" << str << L"\" " << mtime_readable << L" " << file_size << L"\r\n";
1925 replyString << L"\r\n";
1927 SetReplyString(boost::to_upper_copy(replyString.str()));
1931 bool ThumbnailCommand::DoExecuteGenerate()
1933 if (parameters().size() < 2)
1935 SetReplyString(L"402 THUMBNAIL GENERATE ERROR\r\n");
1941 thumb_gen_->generate(parameters()[1]);
1942 SetReplyString(L"202 THUMBNAIL GENERATE OK\r\n");
1947 SetReplyString(L"500 THUMBNAIL GENERATE ERROR\r\n");
1952 bool ThumbnailCommand::DoExecuteGenerateAll()
1956 thumb_gen_->generate_all();
1957 SetReplyString(L"202 THUMBNAIL GENERATE_ALL OK\r\n");
1962 SetReplyString(L"500 THUMBNAIL GENERATE_ALL ERROR\r\n");
1967 bool KillCommand::DoExecute()
1969 shutdown_server_now_->set_value(false); //false for not attempting to restart
1970 SetReplyString(L"202 KILL OK\r\n");
1974 bool RestartCommand::DoExecute()
1976 shutdown_server_now_->set_value(true); //true for attempting to restart
1977 SetReplyString(L"202 RESTART OK\r\n");
1982 }} //namespace caspar