2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Nicklas P Andersson
22 #include "../StdAfx.h"
25 #pragma warning (push, 1) // TODO: Legacy code, just disable warnings
28 #include "AMCPCommandsImpl.h"
29 #include "AMCPProtocolStrategy.h"
31 #include <common/env.h>
33 #include <common/log.h>
34 #include <common/param.h>
35 #include <common/diagnostics/graph.h>
36 #include <common/os/windows/current_version.h>
37 #include <common/os/windows/system_info.h>
38 #include <common/base64.h>
40 #include <core/producer/frame_producer.h>
41 #include <core/video_format.h>
42 #include <core/producer/transition/transition_producer.h>
43 #include <core/frame/frame_transform.h>
44 #include <core/producer/stage.h>
45 #include <core/producer/layer.h>
46 #include <core/mixer/mixer.h>
47 #include <core/consumer/output.h>
48 #include <core/thumbnail_generator.h>
50 #include <modules/reroute/producer/reroute_producer.h>
51 #include <modules/bluefish/bluefish.h>
52 #include <modules/decklink/decklink.h>
53 #include <modules/ffmpeg/ffmpeg.h>
54 #include <modules/flash/flash.h>
55 #include <modules/flash/util/swf.h>
56 #include <modules/flash/producer/flash_producer.h>
57 #include <modules/flash/producer/cg_proxy.h>
58 #include <modules/ffmpeg/producer/util/util.h>
59 #include <modules/image/image.h>
60 #include <modules/screen/screen.h>
61 #include <modules/reroute/producer/reroute_producer.h>
71 #include <boost/date_time/posix_time/posix_time.hpp>
72 #include <boost/lexical_cast.hpp>
73 #include <boost/algorithm/string.hpp>
74 #include <boost/filesystem.hpp>
75 #include <boost/filesystem/fstream.hpp>
76 #include <boost/regex.hpp>
77 #include <boost/property_tree/xml_parser.hpp>
78 #include <boost/locale.hpp>
79 #include <boost/range/adaptor/transformed.hpp>
80 #include <boost/range/algorithm/copy.hpp>
81 #include <boost/archive/iterators/base64_from_binary.hpp>
82 #include <boost/archive/iterators/insert_linebreaks.hpp>
83 #include <boost/archive/iterators/transform_width.hpp>
85 #include <tbb/concurrent_unordered_map.h>
89 102 [action] Information that [action] has happened
90 101 [action] Information that [action] has happened plus one row of data
92 202 [command] OK [command] has been executed
93 201 [command] OK [command] has been executed, plus one row of data
94 200 [command] OK [command] has been executed, plus multiple lines of data. ends with an empty line
96 400 ERROR the command could not be understood
97 401 [command] ERROR invalid/missing channel
98 402 [command] ERROR parameter missing
99 403 [command] ERROR invalid parameter
100 404 [command] ERROR file not found
102 500 FAILED internal error
103 501 [command] FAILED internal error
104 502 [command] FAILED could not read file
105 503 [command] FAILED access denied
107 600 [command] FAILED [command] not implemented
110 namespace caspar { namespace protocol {
112 using namespace core;
114 std::wstring read_file_base64(const boost::filesystem::wpath& file)
116 using namespace boost::archive::iterators;
118 boost::filesystem::ifstream filestream(file, std::ios::binary);
123 auto length = boost::filesystem::file_size(file);
124 std::vector<char> bytes;
125 bytes.resize(length);
126 filestream.read(bytes.data(), length);
128 std::string result(to_base64(bytes.data(), length));
129 return std::wstring(result.begin(), result.end());
132 std::wstring read_utf8_file(const boost::filesystem::wpath& file)
134 std::wstringstream result;
135 boost::filesystem::wifstream filestream(file);
142 result << filestream.rdbuf();
148 std::wstring read_latin1_file(const boost::filesystem::wpath& file)
150 boost::locale::generator gen;
151 gen.locale_cache_enabled(true);
152 gen.categories(boost::locale::codepage_facet);
154 std::stringstream result_stream;
155 boost::filesystem::ifstream filestream(file);
156 filestream.imbue(gen("en_US.ISO8859-1"));
161 result_stream << filestream.rdbuf();
164 std::string result = result_stream.str();
165 std::wstring widened_result;
167 // The first 255 codepoints in unicode is the same as in latin1
168 auto from_signed_to_signed = std::function<unsigned char(char)>(
169 [] (char c) { return static_cast<unsigned char>(c); }
172 result | boost::adaptors::transformed(from_signed_to_signed),
173 std::back_inserter(widened_result));
175 return widened_result;
178 std::wstring read_file(const boost::filesystem::wpath& file)
180 static const uint8_t BOM[] = {0xef, 0xbb, 0xbf};
182 if (!boost::filesystem::exists(file))
187 if (boost::filesystem::file_size(file) >= 3)
189 boost::filesystem::ifstream bom_stream(file);
192 bom_stream.read(header, 3);
195 if (std::memcmp(BOM, header, 3) == 0)
196 return read_utf8_file(file);
199 return read_latin1_file(file);
202 std::wstring MediaInfo(const boost::filesystem::path& path)
204 if(boost::filesystem::is_regular_file(path))
206 std::wstring clipttype = TEXT("N/A");
207 std::wstring extension = boost::to_upper_copy(path.extension().wstring());
208 if(extension == TEXT(".TGA") || extension == TEXT(".COL") || extension == L".PNG" || extension == L".JPEG" || extension == L".JPG" ||
209 extension == L"GIF" || extension == L"BMP")
210 clipttype = TEXT("STILL");
211 else if(extension == TEXT(".WAV") || extension == TEXT(".MP3"))
212 clipttype = TEXT("AUDIO");
213 else if(extension == TEXT("SWF") || extension == TEXT("CT") || extension == TEXT("DV") || extension == TEXT("MOV") || extension == TEXT("MPG") || extension == TEXT("AVI") || caspar::ffmpeg::is_valid_file(path.wstring()))
214 clipttype = TEXT("MOVIE");
216 if(clipttype != TEXT("N/A"))
218 auto is_not_digit = [](char c){ return std::isdigit(c) == 0; };
220 auto relativePath = boost::filesystem::path(path.wstring().substr(env::media_folder().size()-1, path.wstring().size()));
222 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(path)));
223 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), is_not_digit), writeTimeStr.end());
224 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
226 auto sizeStr = boost::lexical_cast<std::wstring>(boost::filesystem::file_size(path));
227 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), is_not_digit), sizeStr.end());
228 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
230 auto str = relativePath.replace_extension(TEXT("")).native();
231 while(str.size() > 0 && (str[0] == '\\' || str[0] == '/'))
232 str = std::wstring(str.begin() + 1, str.end());
234 return std::wstring() + TEXT("\"") + str +
235 + TEXT("\" ") + clipttype +
236 + TEXT(" ") + sizeStr +
237 + TEXT(" ") + writeTimeWStr +
244 std::wstring ListMedia()
246 std::wstringstream replyString;
247 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end; ++itr)
248 replyString << MediaInfo(itr->path());
250 return boost::to_upper_copy(replyString.str());
253 std::wstring ListTemplates()
255 std::wstringstream replyString;
257 for (boost::filesystem::recursive_directory_iterator itr(env::template_folder()), end; itr != end; ++itr)
259 if(boost::filesystem::is_regular_file(itr->path()) && (itr->path().extension() == L".ft" || itr->path().extension() == L".ct"))
261 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::template_folder().size()-1, itr->path().wstring().size()));
263 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(itr->path())));
264 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), [](char c){ return std::isdigit(c) == 0;}), writeTimeStr.end());
265 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
267 auto sizeStr = boost::lexical_cast<std::string>(boost::filesystem::file_size(itr->path()));
268 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), [](char c){ return std::isdigit(c) == 0;}), sizeStr.end());
270 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
272 std::wstring dir = relativePath.parent_path().native();
273 std::wstring file = boost::to_upper_copy(relativePath.filename().wstring());
274 relativePath = boost::filesystem::wpath(dir + L"/" + file);
276 auto str = relativePath.replace_extension(TEXT("")).native();
277 boost::trim_if(str, boost::is_any_of("\\/"));
279 replyString << TEXT("\"") << str
280 << TEXT("\" ") << sizeWStr
281 << TEXT(" ") << writeTimeWStr
285 return replyString.str();
290 void AMCPCommand::SendReply()
292 if(replyString_.empty())
295 client_->send(std::move(replyString_));
298 bool DiagnosticsCommand::DoExecute()
302 diagnostics::show_graphs(true);
304 SetReplyString(TEXT("202 DIAG OK\r\n"));
310 CASPAR_LOG_CURRENT_EXCEPTION();
311 SetReplyString(TEXT("502 DIAG FAILED\r\n"));
316 bool ChannelGridCommand::DoExecute()
319 auto self = channels().back().channel;
321 std::vector<std::wstring> params;
322 params.push_back(L"SCREEN");
323 params.push_back(L"0");
324 params.push_back(L"NAME");
325 params.push_back(L"Channel Grid Window");
326 auto screen = create_consumer(params);
328 self->output().add(screen);
330 BOOST_FOREACH(auto channel, channels())
332 if(channel.channel != self)
334 auto producer = reroute::create_producer(*channel.channel);
335 self->stage().load(index, producer, false);
336 self->stage().play(index);
341 int n = channels().size()-1;
342 double delta = 1.0/static_cast<double>(n);
343 for(int x = 0; x < n; ++x)
345 for(int y = 0; y < n; ++y)
348 auto transform = [=](frame_transform transform) -> frame_transform
350 transform.image_transform.fill_translation[0] = x*delta;
351 transform.image_transform.fill_translation[1] = y*delta;
352 transform.image_transform.fill_scale[0] = delta;
353 transform.image_transform.fill_scale[1] = delta;
354 transform.image_transform.clip_translation[0] = x*delta;
355 transform.image_transform.clip_translation[1] = y*delta;
356 transform.image_transform.clip_scale[0] = delta;
357 transform.image_transform.clip_scale[1] = delta;
360 self->stage().apply_transform(index, transform);
367 bool CallCommand::DoExecute()
369 //Perform loading of the clip
372 auto result = channel()->stage().call(layer_index(), parameters());
374 if (result.wait_for(std::chrono::seconds(2)) != std::future_status::ready)
375 CASPAR_THROW_EXCEPTION(timed_out());
377 std::wstringstream replyString;
378 if(result.get().empty())
379 replyString << TEXT("202 CALL OK\r\n");
381 replyString << TEXT("201 CALL OK\r\n") << result.get() << L"\r\n";
383 SetReplyString(replyString.str());
389 CASPAR_LOG_CURRENT_EXCEPTION();
390 SetReplyString(TEXT("502 CALL FAILED\r\n"));
395 tbb::concurrent_unordered_map<int, std::vector<stage::transform_tuple_t>> deferred_transforms;
397 bool MixerCommand::DoExecute()
399 //Perform loading of the clip
402 bool defer = boost::iequals(parameters().back(), L"DEFER");
404 parameters().pop_back();
406 std::vector<stage::transform_tuple_t> transforms;
408 if(boost::iequals(parameters()[0], L"KEYER") || boost::iequals(parameters()[0], L"IS_KEY"))
410 bool value = boost::lexical_cast<int>(parameters().at(1));
411 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
413 transform.image_transform.is_key = value;
417 else if(boost::iequals(parameters()[0], L"OPACITY"))
419 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
420 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
422 double value = boost::lexical_cast<double>(parameters().at(1));
424 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
426 transform.image_transform.opacity = value;
428 }, duration, tween));
430 else if(boost::iequals(parameters()[0], L"FILL") || boost::iequals(parameters()[0], L"FILL_RECT"))
432 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
433 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
434 double x = boost::lexical_cast<double>(parameters().at(1));
435 double y = boost::lexical_cast<double>(parameters().at(2));
436 double x_s = boost::lexical_cast<double>(parameters().at(3));
437 double y_s = boost::lexical_cast<double>(parameters().at(4));
439 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) mutable -> frame_transform
441 transform.image_transform.fill_translation[0] = x;
442 transform.image_transform.fill_translation[1] = y;
443 transform.image_transform.fill_scale[0] = x_s;
444 transform.image_transform.fill_scale[1] = y_s;
446 }, duration, tween));
448 else if(boost::iequals(parameters()[0], L"CLIP") || boost::iequals(parameters()[0], L"CLIP_RECT"))
450 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
451 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
452 double x = boost::lexical_cast<double>(parameters().at(1));
453 double y = boost::lexical_cast<double>(parameters().at(2));
454 double x_s = boost::lexical_cast<double>(parameters().at(3));
455 double y_s = boost::lexical_cast<double>(parameters().at(4));
457 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
459 transform.image_transform.clip_translation[0] = x;
460 transform.image_transform.clip_translation[1] = y;
461 transform.image_transform.clip_scale[0] = x_s;
462 transform.image_transform.clip_scale[1] = y_s;
464 }, duration, tween));
466 else if(boost::iequals(parameters()[0], L"GRID"))
468 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
469 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
470 int n = boost::lexical_cast<int>(parameters().at(1));
471 double delta = 1.0/static_cast<double>(n);
472 for(int x = 0; x < n; ++x)
474 for(int y = 0; y < n; ++y)
477 transforms.push_back(stage::transform_tuple_t(index, [=](frame_transform transform) -> frame_transform
479 transform.image_transform.fill_translation[0] = x*delta;
480 transform.image_transform.fill_translation[1] = y*delta;
481 transform.image_transform.fill_scale[0] = delta;
482 transform.image_transform.fill_scale[1] = delta;
483 transform.image_transform.clip_translation[0] = x*delta;
484 transform.image_transform.clip_translation[1] = y*delta;
485 transform.image_transform.clip_scale[0] = delta;
486 transform.image_transform.clip_scale[1] = delta;
488 }, duration, tween));
492 else if(boost::iequals(parameters()[0], L"BLEND"))
494 auto blend_str = parameters().at(1);
495 int layer = layer_index();
496 channel()->mixer().set_blend_mode(layer, get_blend_mode(blend_str));
498 else if(boost::iequals(parameters()[0], L"MASTERVOLUME"))
500 float master_volume = boost::lexical_cast<float>(parameters().at(1));
501 channel()->mixer().set_master_volume(master_volume);
503 else if(boost::iequals(parameters()[0], L"BRIGHTNESS"))
505 auto value = boost::lexical_cast<double>(parameters().at(1));
506 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
507 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
508 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
510 transform.image_transform.brightness = value;
512 }, duration, tween));
514 else if(boost::iequals(parameters()[0], L"SATURATION"))
516 auto value = boost::lexical_cast<double>(parameters().at(1));
517 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
518 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
519 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
521 transform.image_transform.saturation = value;
523 }, duration, tween));
525 else if(parameters()[0] == L"CONTRAST")
527 auto value = boost::lexical_cast<double>(parameters().at(1));
528 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
529 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
530 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
532 transform.image_transform.contrast = value;
534 }, duration, tween));
536 else if(boost::iequals(parameters()[0], L"LEVELS"))
539 value.min_input = boost::lexical_cast<double>(parameters().at(1));
540 value.max_input = boost::lexical_cast<double>(parameters().at(2));
541 value.gamma = boost::lexical_cast<double>(parameters().at(3));
542 value.min_output = boost::lexical_cast<double>(parameters().at(4));
543 value.max_output = boost::lexical_cast<double>(parameters().at(5));
544 int duration = parameters().size() > 6 ? boost::lexical_cast<int>(parameters()[6]) : 0;
545 std::wstring tween = parameters().size() > 7 ? parameters()[7] : L"linear";
547 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
549 transform.image_transform.levels = value;
551 }, duration, tween));
553 else if(boost::iequals(parameters()[0], L"VOLUME"))
555 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
556 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
557 double value = boost::lexical_cast<double>(parameters()[1]);
559 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
561 transform.audio_transform.volume = value;
563 }, duration, tween));
565 else if(boost::iequals(parameters()[0], L"CLEAR"))
567 int layer = layer_index(std::numeric_limits<int>::max());
569 if (layer == std::numeric_limits<int>::max())
571 channel()->stage().clear_transforms();
572 channel()->mixer().clear_blend_modes();
576 channel()->stage().clear_transforms(layer);
577 channel()->mixer().clear_blend_mode(layer);
580 else if(boost::iequals(parameters()[0], L"COMMIT"))
582 transforms = std::move(deferred_transforms[channel_index()]);
586 SetReplyString(TEXT("404 MIXER ERROR\r\n"));
592 auto& defer_tranforms = deferred_transforms[channel_index()];
593 defer_tranforms.insert(defer_tranforms.end(), transforms.begin(), transforms.end());
596 channel()->stage().apply_transforms(transforms);
598 SetReplyString(TEXT("202 MIXER OK\r\n"));
602 catch(file_not_found&)
604 CASPAR_LOG_CURRENT_EXCEPTION();
605 SetReplyString(TEXT("404 MIXER ERROR\r\n"));
610 CASPAR_LOG_CURRENT_EXCEPTION();
611 SetReplyString(TEXT("502 MIXER FAILED\r\n"));
616 bool SwapCommand::DoExecute()
618 //Perform loading of the clip
621 if(layer_index(-1) != -1)
623 std::vector<std::string> strs;
624 boost::split(strs, parameters()[0], boost::is_any_of("-"));
626 auto ch1 = channel();
627 auto ch2 = channels().at(boost::lexical_cast<int>(strs.at(0))-1);
629 int l1 = layer_index();
630 int l2 = boost::lexical_cast<int>(strs.at(1));
632 ch1->stage().swap_layer(l1, l2, ch2.channel->stage());
636 auto ch1 = channel();
637 auto ch2 = channels().at(boost::lexical_cast<int>(parameters()[0])-1);
638 ch1->stage().swap_layers(ch2.channel->stage());
641 SetReplyString(TEXT("202 SWAP OK\r\n"));
645 catch(file_not_found&)
647 CASPAR_LOG_CURRENT_EXCEPTION();
648 SetReplyString(TEXT("404 SWAP ERROR\r\n"));
653 CASPAR_LOG_CURRENT_EXCEPTION();
654 SetReplyString(TEXT("502 SWAP FAILED\r\n"));
659 bool AddCommand::DoExecute()
661 //Perform loading of the clip
664 //create_consumer still expects all parameters to be uppercase
665 BOOST_FOREACH(std::wstring& str, parameters())
667 boost::to_upper(str);
670 auto consumer = create_consumer(parameters());
671 channel()->output().add(layer_index(consumer->index()), consumer);
673 SetReplyString(TEXT("202 ADD OK\r\n"));
677 catch(file_not_found&)
679 CASPAR_LOG_CURRENT_EXCEPTION();
680 SetReplyString(TEXT("404 ADD ERROR\r\n"));
685 CASPAR_LOG_CURRENT_EXCEPTION();
686 SetReplyString(TEXT("502 ADD FAILED\r\n"));
691 bool RemoveCommand::DoExecute()
693 //Perform loading of the clip
696 auto index = layer_index(std::numeric_limits<int>::min());
697 if(index == std::numeric_limits<int>::min())
699 //create_consumer still expects all parameters to be uppercase
700 BOOST_FOREACH(std::wstring& str, parameters())
702 boost::to_upper(str);
705 index = create_consumer(parameters())->index();
708 channel()->output().remove(index);
710 SetReplyString(TEXT("202 REMOVE OK\r\n"));
714 catch(file_not_found&)
716 CASPAR_LOG_CURRENT_EXCEPTION();
717 SetReplyString(TEXT("404 REMOVE ERROR\r\n"));
722 CASPAR_LOG_CURRENT_EXCEPTION();
723 SetReplyString(TEXT("502 REMOVE FAILED\r\n"));
728 bool LoadCommand::DoExecute()
730 //Perform loading of the clip
733 auto pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
734 channel()->stage().load(layer_index(), pFP, true);
736 SetReplyString(TEXT("202 LOAD OK\r\n"));
740 catch(file_not_found&)
742 CASPAR_LOG_CURRENT_EXCEPTION();
743 SetReplyString(TEXT("404 LOAD ERROR\r\n"));
748 CASPAR_LOG_CURRENT_EXCEPTION();
749 SetReplyString(TEXT("502 LOAD FAILED\r\n"));
756 //std::function<std::wstring()> channel_cg_add_command::parse(const std::wstring& message, const std::vector<renderer::render_device_ptr>& channels)
758 // static boost::wregex expr(L"^CG\\s(?<video_channel>\\d+)-?(?<LAYER>\\d+)?\\sADD\\s(?<FLASH_LAYER>\\d+)\\s(?<TEMPLATE>\\S+)\\s?(?<START_LABEL>\\S\\S+)?\\s?(?<PLAY_ON_LOAD>\\d)?\\s?(?<DATA>.*)?");
760 // boost::wsmatch what;
761 // if(!boost::regex_match(message, what, expr))
764 // auto info = channel_info::parse(what, channels);
766 // int flash_layer_index = boost::lexical_cast<int>(what["FLASH_LAYER"].str());
768 // std::wstring templatename = what["TEMPLATE"].str();
769 // bool play_on_load = what["PLAY_ON_LOAD"].matched ? what["PLAY_ON_LOAD"].str() != L"0" : 0;
770 // std::wstring start_label = what["START_LABEL"].str();
771 // std::wstring data = get_data(what["DATA"].str());
773 // boost::replace_all(templatename, "\"", "");
775 // return [=]() -> std::wstring
777 // std::wstring fullFilename = flash::flash_producer::find_template(server::template_folder() + templatename);
778 // if(fullFilename.empty())
779 // CASPAR_THROW_EXCEPTION(file_not_found());
781 // std::wstring extension = boost::filesystem::wpath(fullFilename).extension();
782 // std::wstring filename = templatename;
783 // filename.append(extension);
785 // flash::flash::create_cg_proxy(info.video_channel, std::max<int>(DEFAULT_CHANNEL_LAYER+1, info.layer_index))
786 // ->add(flash_layer_index, filename, play_on_load, start_label, data);
788 // CASPAR_LOG(info) << L"Executed [amcp_channel_cg_add]";
792 bool LoadbgCommand::DoExecute()
794 transition_info transitionInfo;
798 std::wstring message;
799 for(size_t n = 0; n < parameters().size(); ++n)
800 message += boost::to_upper_copy(parameters()[n]) + L" ";
802 static const boost::wregex expr(L".*(?<TRANSITION>CUT|PUSH|SLIDE|WIPE|MIX)\\s*(?<DURATION>\\d+)\\s*(?<TWEEN>(LINEAR)|(EASE[^\\s]*))?\\s*(?<DIRECTION>FROMLEFT|FROMRIGHT|LEFT|RIGHT)?.*");
804 if(boost::regex_match(message, what, expr))
806 auto transition = what["TRANSITION"].str();
807 transitionInfo.duration = boost::lexical_cast<size_t>(what["DURATION"].str());
808 auto direction = what["DIRECTION"].matched ? what["DIRECTION"].str() : L"";
809 auto tween = what["TWEEN"].matched ? what["TWEEN"].str() : L"";
810 transitionInfo.tweener = tween;
812 if(transition == TEXT("CUT"))
813 transitionInfo.type = transition_type::cut;
814 else if(transition == TEXT("MIX"))
815 transitionInfo.type = transition_type::mix;
816 else if(transition == TEXT("PUSH"))
817 transitionInfo.type = transition_type::push;
818 else if(transition == TEXT("SLIDE"))
819 transitionInfo.type = transition_type::slide;
820 else if(transition == TEXT("WIPE"))
821 transitionInfo.type = transition_type::wipe;
823 if(direction == TEXT("FROMLEFT"))
824 transitionInfo.direction = transition_direction::from_left;
825 else if(direction == TEXT("FROMRIGHT"))
826 transitionInfo.direction = transition_direction::from_right;
827 else if(direction == TEXT("LEFT"))
828 transitionInfo.direction = transition_direction::from_right;
829 else if(direction == TEXT("RIGHT"))
830 transitionInfo.direction = transition_direction::from_left;
833 //Perform loading of the clip
836 std::shared_ptr<core::frame_producer> pFP;
838 static boost::wregex expr(L"\\[(?<CHANNEL>\\d+)\\]", boost::regex::icase);
841 if(boost::regex_match(parameters().at(0), what, expr))
843 auto channel_index = boost::lexical_cast<int>(what["CHANNEL"].str());
844 pFP = reroute::create_producer(*channels().at(channel_index-1).channel);
848 pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
851 if(pFP == frame_producer::empty())
852 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info(parameters().size() > 0 ? parameters()[0] : L""));
854 bool auto_play = contains_param(L"AUTO", parameters());
856 auto pFP2 = create_transition_producer(channel()->video_format_desc().field_mode, spl::make_shared_ptr(pFP), transitionInfo);
858 channel()->stage().load(layer_index(), pFP2, false, transitionInfo.duration); // TODO: LOOP
860 channel()->stage().load(layer_index(), pFP2, false); // TODO: LOOP
863 SetReplyString(TEXT("202 LOADBG OK\r\n"));
867 catch(file_not_found&)
869 CASPAR_LOG(error) << L"File not found. No match found for parameters. Check syntax.";
870 SetReplyString(TEXT("404 LOADBG ERROR\r\n"));
875 CASPAR_LOG_CURRENT_EXCEPTION();
876 SetReplyString(TEXT("502 LOADBG FAILED\r\n"));
881 bool PauseCommand::DoExecute()
885 channel()->stage().pause(layer_index());
886 SetReplyString(TEXT("202 PAUSE OK\r\n"));
891 SetReplyString(TEXT("501 PAUSE FAILED\r\n"));
897 bool PlayCommand::DoExecute()
901 if(!parameters().empty())
903 LoadbgCommand lbg(*this);
906 throw std::exception();
909 channel()->stage().play(layer_index());
911 SetReplyString(TEXT("202 PLAY OK\r\n"));
916 SetReplyString(TEXT("501 PLAY FAILED\r\n"));
922 bool StopCommand::DoExecute()
926 channel()->stage().stop(layer_index());
927 SetReplyString(TEXT("202 STOP OK\r\n"));
932 SetReplyString(TEXT("501 STOP FAILED\r\n"));
938 bool ClearCommand::DoExecute()
940 int index = layer_index(std::numeric_limits<int>::min());
941 if(index != std::numeric_limits<int>::min())
942 channel()->stage().clear(index);
944 channel()->stage().clear();
946 SetReplyString(TEXT("202 CLEAR OK\r\n"));
951 bool PrintCommand::DoExecute()
953 channel()->output().add(create_consumer(boost::assign::list_of(L"IMAGE")));
955 SetReplyString(TEXT("202 PRINT OK\r\n"));
960 bool LogCommand::DoExecute()
962 if(boost::iequals(parameters().at(0), L"LEVEL"))
963 log::set_log_level(parameters().at(1));
965 SetReplyString(TEXT("202 LOG OK\r\n"));
970 bool CGCommand::DoExecute()
974 std::wstring command = boost::to_upper_copy(parameters()[0]);
975 if(command == TEXT("ADD"))
976 return DoExecuteAdd();
977 else if(command == TEXT("PLAY"))
978 return DoExecutePlay();
979 else if(command == TEXT("STOP"))
980 return DoExecuteStop();
981 else if(command == TEXT("NEXT"))
982 return DoExecuteNext();
983 else if(command == TEXT("REMOVE"))
984 return DoExecuteRemove();
985 else if(command == TEXT("CLEAR"))
986 return DoExecuteClear();
987 else if(command == TEXT("UPDATE"))
988 return DoExecuteUpdate();
989 else if(command == TEXT("INVOKE"))
990 return DoExecuteInvoke();
991 else if(command == TEXT("INFO"))
992 return DoExecuteInfo();
996 CASPAR_LOG_CURRENT_EXCEPTION();
999 SetReplyString(TEXT("403 CG ERROR\r\n"));
1003 bool CGCommand::ValidateLayer(const std::wstring& layerstring) {
1004 int length = layerstring.length();
1005 for(int i = 0; i < length; ++i) {
1006 if(!_istdigit(layerstring[i])) {
1014 bool CGCommand::DoExecuteAdd() {
1015 //CG 1 ADD 0 "template_folder/templatename" [STARTLABEL] 0/1 [DATA]
1017 int layer = 0; //_parameters[1]
1018 // std::wstring templateName; //_parameters[2]
1019 std::wstring label; //_parameters[3]
1020 bool bDoStart = false; //_parameters[3] alt. _parameters[4]
1021 // std::wstring data; //_parameters[4] alt. _parameters[5]
1023 if(parameters().size() < 4)
1025 SetReplyString(TEXT("402 CG ERROR\r\n"));
1028 unsigned int dataIndex = 4;
1030 if(!ValidateLayer(parameters()[1]))
1032 SetReplyString(TEXT("403 CG ERROR\r\n"));
1036 layer = boost::lexical_cast<int>(parameters()[1]);
1038 if(parameters()[3].length() > 1)
1040 label = parameters()[3];
1043 if(parameters().size() > 4 && parameters()[4].length() > 0) //read play-on-load-flag
1044 bDoStart = (parameters()[4][0]==TEXT('1')) ? true : false;
1047 SetReplyString(TEXT("402 CG ERROR\r\n"));
1051 else if(parameters()[3].length() > 0) { //read play-on-load-flag
1052 bDoStart = (parameters()[3][0]==TEXT('1')) ? true : false;
1056 SetReplyString(TEXT("403 CG ERROR\r\n"));
1060 const TCHAR* pDataString = 0;
1061 std::wstring dataFromFile;
1062 if(parameters().size() > dataIndex)
1064 const std::wstring& dataString = parameters()[dataIndex];
1066 if(dataString[0] == TEXT('<')) //the data is an XML-string
1067 pDataString = dataString.c_str();
1070 //The data is not an XML-string, it must be a filename
1071 std::wstring filename = env::data_folder();
1072 filename.append(dataString);
1073 filename.append(TEXT(".ftd"));
1075 dataFromFile = read_file(boost::filesystem::wpath(filename));
1076 pDataString = dataFromFile.c_str();
1080 std::wstring fullFilename = flash::find_template(env::template_folder() + parameters()[2]);
1081 if(!fullFilename.empty())
1083 std::wstring extension = boost::filesystem::path(fullFilename).extension().wstring();
1084 std::wstring filename = parameters()[2];
1085 filename.append(extension);
1087 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(""));
1088 SetReplyString(TEXT("202 CG OK\r\n"));
1092 CASPAR_LOG(warning) << "Could not find template " << parameters()[2];
1093 SetReplyString(TEXT("404 CG ERROR\r\n"));
1098 bool CGCommand::DoExecutePlay()
1100 if(parameters().size() > 1)
1102 if(!ValidateLayer(parameters()[1]))
1104 SetReplyString(TEXT("403 CG ERROR\r\n"));
1107 int layer = boost::lexical_cast<int>(parameters()[1]);
1108 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).play(layer);
1112 SetReplyString(TEXT("402 CG ERROR\r\n"));
1116 SetReplyString(TEXT("202 CG OK\r\n"));
1120 bool CGCommand::DoExecuteStop()
1122 if(parameters().size() > 1)
1124 if(!ValidateLayer(parameters()[1]))
1126 SetReplyString(TEXT("403 CG ERROR\r\n"));
1129 int layer = boost::lexical_cast<int>(parameters()[1]);
1130 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).stop(layer, 0);
1134 SetReplyString(TEXT("402 CG ERROR\r\n"));
1138 SetReplyString(TEXT("202 CG OK\r\n"));
1142 bool CGCommand::DoExecuteNext()
1144 if(parameters().size() > 1)
1146 if(!ValidateLayer(parameters()[1]))
1148 SetReplyString(TEXT("403 CG ERROR\r\n"));
1152 int layer = boost::lexical_cast<int>(parameters()[1]);
1153 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).next(layer);
1157 SetReplyString(TEXT("402 CG ERROR\r\n"));
1161 SetReplyString(TEXT("202 CG OK\r\n"));
1165 bool CGCommand::DoExecuteRemove()
1167 if(parameters().size() > 1)
1169 if(!ValidateLayer(parameters()[1]))
1171 SetReplyString(TEXT("403 CG ERROR\r\n"));
1175 int layer = boost::lexical_cast<int>(parameters()[1]);
1176 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).remove(layer);
1180 SetReplyString(TEXT("402 CG ERROR\r\n"));
1184 SetReplyString(TEXT("202 CG OK\r\n"));
1188 bool CGCommand::DoExecuteClear()
1190 channel()->stage().clear(layer_index(flash::cg_proxy::DEFAULT_LAYER));
1191 SetReplyString(TEXT("202 CG OK\r\n"));
1195 bool CGCommand::DoExecuteUpdate()
1199 if(!ValidateLayer(parameters().at(1)))
1201 SetReplyString(TEXT("403 CG ERROR\r\n"));
1205 std::wstring dataString = parameters().at(2);
1206 if(dataString.at(0) != TEXT('<'))
1208 //The data is not an XML-string, it must be a filename
1209 std::wstring filename = env::data_folder();
1210 filename.append(dataString);
1211 filename.append(TEXT(".ftd"));
1213 dataString = read_file(boost::filesystem::wpath(filename));
1216 int layer = boost::lexical_cast<int>(parameters()[1]);
1217 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).update(layer, dataString);
1221 SetReplyString(TEXT("402 CG ERROR\r\n"));
1225 SetReplyString(TEXT("202 CG OK\r\n"));
1229 bool CGCommand::DoExecuteInvoke()
1231 std::wstringstream replyString;
1232 replyString << TEXT("201 CG OK\r\n");
1234 if(parameters().size() > 2)
1236 if(!ValidateLayer(parameters()[1]))
1238 SetReplyString(TEXT("403 CG ERROR\r\n"));
1241 int layer = boost::lexical_cast<int>(parameters()[1]);
1242 auto result = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).invoke(layer, parameters()[2]);
1243 replyString << result << TEXT("\r\n");
1247 SetReplyString(TEXT("402 CG ERROR\r\n"));
1251 SetReplyString(replyString.str());
1255 bool CGCommand::DoExecuteInfo()
1257 std::wstringstream replyString;
1258 replyString << TEXT("201 CG OK\r\n");
1260 if(parameters().size() > 1)
1262 if(!ValidateLayer(parameters()[1]))
1264 SetReplyString(TEXT("403 CG ERROR\r\n"));
1268 int layer = boost::lexical_cast<int>(parameters()[1]);
1269 auto desc = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).description(layer);
1271 replyString << desc << TEXT("\r\n");
1275 auto info = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).template_host_info();
1276 replyString << info << TEXT("\r\n");
1279 SetReplyString(replyString.str());
1283 bool DataCommand::DoExecute()
1285 std::wstring command = boost::to_upper_copy(parameters()[0]);
1286 if(command == TEXT("STORE"))
1287 return DoExecuteStore();
1288 else if(command == TEXT("RETRIEVE"))
1289 return DoExecuteRetrieve();
1290 else if(command == TEXT("REMOVE"))
1291 return DoExecuteRemove();
1292 else if(command == TEXT("LIST"))
1293 return DoExecuteList();
1295 SetReplyString(TEXT("403 DATA ERROR\r\n"));
1299 bool DataCommand::DoExecuteStore()
1301 if(parameters().size() < 3)
1303 SetReplyString(TEXT("402 DATA STORE ERROR\r\n"));
1307 std::wstring filename = env::data_folder();
1308 filename.append(parameters()[1]);
1309 filename.append(TEXT(".ftd"));
1311 auto data_path = boost::filesystem::wpath(
1312 boost::filesystem::wpath(filename).parent_path());
1314 if(!boost::filesystem::exists(data_path))
1315 boost::filesystem::create_directories(data_path);
1317 std::wofstream datafile(filename.c_str());
1320 SetReplyString(TEXT("501 DATA STORE FAILED\r\n"));
1324 datafile << static_cast<wchar_t>(65279); // UTF-8 BOM character
1325 datafile << parameters()[2] << std::flush;
1328 std::wstring replyString = TEXT("202 DATA STORE OK\r\n");
1329 SetReplyString(replyString);
1333 bool DataCommand::DoExecuteRetrieve()
1335 if(parameters().size() < 2)
1337 SetReplyString(TEXT("402 DATA RETRIEVE ERROR\r\n"));
1341 std::wstring filename = env::data_folder();
1342 filename.append(parameters()[1]);
1343 filename.append(TEXT(".ftd"));
1345 std::wstring file_contents = read_file(boost::filesystem::wpath(filename));
1347 if (file_contents.empty())
1349 SetReplyString(TEXT("404 DATA RETRIEVE ERROR\r\n"));
1353 std::wstringstream reply(TEXT("201 DATA RETRIEVE OK\r\n"));
1355 std::wstringstream file_contents_stream(file_contents);
1358 bool firstLine = true;
1359 while(std::getline(file_contents_stream, line))
1370 SetReplyString(reply.str());
1374 bool DataCommand::DoExecuteRemove()
1376 if (parameters().size() < 2)
1378 SetReplyString(TEXT("402 DATA REMOVE ERROR\r\n"));
1382 std::wstring filename = env::data_folder();
1383 filename.append(parameters()[1]);
1384 filename.append(TEXT(".ftd"));
1386 if (!boost::filesystem::exists(filename))
1388 SetReplyString(TEXT("404 DATA REMOVE ERROR\r\n"));
1392 if (!boost::filesystem::remove(filename))
1394 SetReplyString(TEXT("403 DATA REMOVE ERROR\r\n"));
1398 SetReplyString(TEXT("201 DATA REMOVE OK\r\n"));
1403 bool DataCommand::DoExecuteList()
1405 std::wstringstream replyString;
1406 replyString << TEXT("200 DATA LIST OK\r\n");
1408 for (boost::filesystem::recursive_directory_iterator itr(env::data_folder()), end; itr != end; ++itr)
1410 if(boost::filesystem::is_regular_file(itr->path()))
1412 if(!boost::iequals(itr->path().extension().wstring(), L".ftd"))
1415 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::data_folder().size()-1, itr->path().wstring().size()));
1417 auto str = relativePath.replace_extension(TEXT("")).native();
1418 if(str[0] == '\\' || str[0] == '/')
1419 str = std::wstring(str.begin() + 1, str.end());
1421 replyString << str << TEXT("\r\n");
1425 replyString << TEXT("\r\n");
1427 SetReplyString(boost::to_upper_copy(replyString.str()));
1431 bool CinfCommand::DoExecute()
1433 std::wstringstream replyString;
1438 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end && info.empty(); ++itr)
1440 auto path = itr->path();
1441 auto file = path.replace_extension(L"").filename();
1442 if(boost::iequals(file.wstring(), parameters().at(0)))
1443 info += MediaInfo(itr->path()) + L"\r\n";
1448 SetReplyString(TEXT("404 CINF ERROR\r\n"));
1451 replyString << TEXT("200 CINF OK\r\n");
1452 replyString << info << "\r\n";
1456 CASPAR_LOG_CURRENT_EXCEPTION();
1457 SetReplyString(TEXT("404 CINF ERROR\r\n"));
1461 SetReplyString(replyString.str());
1465 void GenerateChannelInfo(int index, const spl::shared_ptr<core::video_channel>& pChannel, std::wstringstream& replyString)
1467 replyString << index+1 << TEXT(" ") << pChannel->video_format_desc().name << TEXT(" PLAYING") << TEXT("\r\n");
1470 bool InfoCommand::DoExecute()
1472 std::wstringstream replyString;
1474 boost::property_tree::xml_writer_settings<std::wstring> w(' ', 3);
1478 if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"TEMPLATE"))
1480 replyString << L"201 INFO TEMPLATE OK\r\n";
1482 // Needs to be extended for any file, not just flash.
1484 auto filename = flash::find_template(env::template_folder() + parameters().at(1));
1486 std::wstringstream str;
1487 str << u16(flash::read_template_meta_info(filename));
1488 boost::property_tree::wptree info;
1489 boost::property_tree::xml_parser::read_xml(str, info, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);
1491 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1493 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"CONFIG"))
1495 replyString << L"201 INFO CONFIG OK\r\n";
1497 boost::property_tree::write_xml(replyString, caspar::env::properties(), w);
1499 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"PATHS"))
1501 replyString << L"201 INFO PATHS OK\r\n";
1503 boost::property_tree::wptree info;
1504 info.add_child(L"paths", caspar::env::properties().get_child(L"configuration.paths"));
1505 info.add(L"paths.initial-path", boost::filesystem::initial_path().wstring() + L"\\");
1507 boost::property_tree::write_xml(replyString, info, w);
1509 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SYSTEM"))
1511 replyString << L"201 INFO SYSTEM OK\r\n";
1513 boost::property_tree::wptree info;
1515 info.add(L"system.name", caspar::system_product_name());
1516 info.add(L"system.windows.name", caspar::win_product_name());
1517 info.add(L"system.windows.service-pack", caspar::win_sp_version());
1518 info.add(L"system.cpu", caspar::cpu_info());
1520 BOOST_FOREACH(auto device, caspar::decklink::device_list())
1521 info.add(L"system.decklink.device", device);
1523 BOOST_FOREACH(auto device, caspar::bluefish::device_list())
1524 info.add(L"system.bluefish.device", device);
1526 info.add(L"system.flash", caspar::flash::version());
1527 //info.add(L"system.free-image", caspar::image::version());
1528 info.add(L"system.ffmpeg.avcodec", caspar::ffmpeg::avcodec_version());
1529 info.add(L"system.ffmpeg.avformat", caspar::ffmpeg::avformat_version());
1530 info.add(L"system.ffmpeg.avfilter", caspar::ffmpeg::avfilter_version());
1531 info.add(L"system.ffmpeg.avutil", caspar::ffmpeg::avutil_version());
1532 info.add(L"system.ffmpeg.swscale", caspar::ffmpeg::swscale_version());
1534 boost::property_tree::write_xml(replyString, info, w);
1536 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SERVER"))
1538 replyString << L"201 INFO SERVER OK\r\n";
1540 boost::property_tree::wptree info;
1543 BOOST_FOREACH(auto channel, channels())
1544 info.add_child(L"channels.channel", channel.channel->info())
1545 .add(L"index", ++index);
1547 boost::property_tree::write_xml(replyString, info, w);
1551 if(parameters().size() >= 1)
1553 replyString << TEXT("201 INFO OK\r\n");
1554 boost::property_tree::wptree info;
1556 std::vector<std::wstring> split;
1557 boost::split(split, parameters()[0], boost::is_any_of("-"));
1559 int layer = std::numeric_limits<int>::min();
1560 int channel = boost::lexical_cast<int>(split[0]) - 1;
1562 if(split.size() > 1)
1563 layer = boost::lexical_cast<int>(split[1]);
1565 if(layer == std::numeric_limits<int>::min())
1567 info.add_child(L"channel", channels().at(channel).channel->info())
1568 .add(L"index", channel);
1572 if(parameters().size() >= 2)
1574 if(boost::iequals(parameters()[1], L"B"))
1575 info.add_child(L"producer", channels().at(channel).channel->stage().background(layer).get()->info());
1577 info.add_child(L"producer", channels().at(channel).channel->stage().foreground(layer).get()->info());
1581 info.add_child(L"layer", channels().at(channel).channel->stage().info(layer).get())
1582 .add(L"index", layer);
1585 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1589 // This is needed for backwards compatibility with old clients
1590 replyString << TEXT("200 INFO OK\r\n");
1591 for(size_t n = 0; n < channels().size(); ++n)
1592 GenerateChannelInfo(n, channels()[n].channel, replyString);
1599 CASPAR_LOG_CURRENT_EXCEPTION();
1600 SetReplyString(TEXT("403 INFO ERROR\r\n"));
1604 replyString << TEXT("\r\n");
1605 SetReplyString(replyString.str());
1609 bool ClsCommand::DoExecute()
1621 std::wstringstream replyString;
1622 replyString << TEXT("200 CLS OK\r\n");
1623 replyString << ListMedia();
1624 replyString << TEXT("\r\n");
1625 SetReplyString(boost::to_upper_copy(replyString.str()));
1629 CASPAR_LOG_CURRENT_EXCEPTION();
1630 SetReplyString(TEXT("501 CLS FAILED\r\n"));
1637 bool TlsCommand::DoExecute()
1641 std::wstringstream replyString;
1642 replyString << TEXT("200 TLS OK\r\n");
1644 replyString << ListTemplates();
1645 replyString << TEXT("\r\n");
1647 SetReplyString(replyString.str());
1651 CASPAR_LOG_CURRENT_EXCEPTION();
1652 SetReplyString(TEXT("501 TLS FAILED\r\n"));
1658 bool VersionCommand::DoExecute()
1660 std::wstring replyString = TEXT("201 VERSION OK\r\n") + env::version() + TEXT("\r\n");
1662 if(parameters().size() > 0)
1664 if(boost::iequals(parameters()[0], L"FLASH"))
1665 replyString = TEXT("201 VERSION OK\r\n") + flash::version() + TEXT("\r\n");
1666 else if(boost::iequals(parameters()[0], L"TEMPLATEHOST"))
1667 replyString = TEXT("201 VERSION OK\r\n") + flash::cg_version() + TEXT("\r\n");
1668 else if(!boost::iequals(parameters()[0], L"SERVER"))
1669 replyString = TEXT("403 VERSION ERROR\r\n");
1672 SetReplyString(replyString);
1676 bool ByeCommand::DoExecute()
1678 client()->disconnect();
1682 bool SetCommand::DoExecute()
1686 std::wstring name = boost::to_upper_copy(parameters()[0]);
1687 std::wstring value = boost::to_upper_copy(parameters()[1]);
1689 if(name == TEXT("MODE"))
1691 auto format_desc = core::video_format_desc(value);
1692 if(format_desc.format != core::video_format::invalid)
1694 channel()->video_format_desc(format_desc);
1695 SetReplyString(TEXT("202 SET MODE OK\r\n"));
1698 SetReplyString(TEXT("501 SET MODE FAILED\r\n"));
1702 this->SetReplyString(TEXT("403 SET ERROR\r\n"));
1707 CASPAR_LOG_CURRENT_EXCEPTION();
1708 SetReplyString(TEXT("501 SET FAILED\r\n"));
1715 bool LockCommand::DoExecute()
1719 auto it = parameters().begin();
1721 std::shared_ptr<caspar::IO::lock_container> lock;
1724 int channel_index = boost::lexical_cast<int>(*it) - 1;
1725 lock = channels().at(channel_index).lock;
1727 catch(const boost::bad_lexical_cast&) {}
1730 SetReplyString(L"401 LOCK ERROR\r\n");
1737 if(it == parameters().end()) //too few parameters
1739 SetReplyString(L"402 LOCK ERROR\r\n");
1743 std::wstring command = boost::to_upper_copy(*it);
1744 if(command == L"ACQUIRE")
1747 if(it == parameters().end()) //too few parameters
1749 SetReplyString(L"402 LOCK ACQUIRE ERROR\r\n");
1752 std::wstring lock_phrase = (*it);
1754 //TODO: read options
1758 //just lock one channel
1759 if(!lock->try_lock(lock_phrase, client()))
1761 SetReplyString(L"503 LOCK ACQUIRE FAILED\r\n");
1767 //TODO: lock all channels
1768 CASPAR_THROW_EXCEPTION(not_implemented());
1770 SetReplyString(L"202 LOCK ACQUIRE OK\r\n");
1773 else if(command == L"RELEASE")
1777 lock->release_lock(client());
1781 //TODO: release all channels
1782 CASPAR_THROW_EXCEPTION(not_implemented());
1784 SetReplyString(L"202 LOCK RELEASE OK\r\n");
1786 else if(command == L"CLEAR")
1788 std::wstring override_phrase = env::properties().get(L"configuration.lock-clear-phrase", L"");
1789 std::wstring client_override_phrase;
1790 if(!override_phrase.empty())
1793 if(it == parameters().end())
1795 SetReplyString(L"402 LOCK CLEAR ERROR\r\n");
1798 client_override_phrase = (*it);
1803 //just clear one channel
1804 if(client_override_phrase != override_phrase)
1806 SetReplyString(L"503 LOCK CLEAR FAILED\r\n");
1810 lock->clear_locks();
1814 //TODO: clear all channels
1815 CASPAR_THROW_EXCEPTION(not_implemented());
1818 SetReplyString(L"202 LOCK CLEAR OK\r\n");
1822 SetReplyString(L"403 LOCK ERROR\r\n");
1826 catch(not_implemented&)
1828 SetReplyString(L"600 LOCK FAILED\r\n");
1833 CASPAR_LOG_CURRENT_EXCEPTION();
1834 SetReplyString(L"501 LOCK FAILED\r\n");
1841 bool ThumbnailCommand::DoExecute()
1843 std::wstring command = boost::to_upper_copy(parameters()[0]);
1845 if (command == TEXT("RETRIEVE"))
1846 return DoExecuteRetrieve();
1847 else if (command == TEXT("LIST"))
1848 return DoExecuteList();
1849 else if (command == TEXT("GENERATE"))
1850 return DoExecuteGenerate();
1851 else if (command == TEXT("GENERATE_ALL"))
1852 return DoExecuteGenerateAll();
1854 SetReplyString(TEXT("403 THUMBNAIL ERROR\r\n"));
1858 bool ThumbnailCommand::DoExecuteRetrieve()
1860 if(parameters().size() < 2)
1862 SetReplyString(TEXT("402 THUMBNAIL RETRIEVE ERROR\r\n"));
1866 std::wstring filename = env::thumbnails_folder();
1867 filename.append(parameters()[1]);
1868 filename.append(TEXT(".png"));
1870 std::wstring file_contents = read_file_base64(boost::filesystem::wpath(filename));
1872 if (file_contents.empty())
1874 SetReplyString(TEXT("404 THUMBNAIL RETRIEVE ERROR\r\n"));
1878 std::wstringstream reply;
1880 reply << L"201 THUMBNAIL RETRIEVE OK\r\n";
1881 reply << file_contents;
1883 SetReplyString(reply.str());
1887 bool ThumbnailCommand::DoExecuteList()
1889 std::wstringstream replyString;
1890 replyString << TEXT("200 THUMBNAIL LIST OK\r\n");
1892 for (boost::filesystem::recursive_directory_iterator itr(env::thumbnails_folder()), end; itr != end; ++itr)
1894 if(boost::filesystem::is_regular_file(itr->path()))
1896 if(!boost::iequals(itr->path().extension().wstring(), L".png"))
1899 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::thumbnails_folder().size()-1, itr->path().wstring().size()));
1901 auto str = relativePath.replace_extension(L"").native();
1902 if(str[0] == '\\' || str[0] == '/')
1903 str = std::wstring(str.begin() + 1, str.end());
1905 auto mtime = boost::filesystem::last_write_time(itr->path());
1906 auto mtime_readable = boost::posix_time::to_iso_wstring(boost::posix_time::from_time_t(mtime));
1907 auto file_size = boost::filesystem::file_size(itr->path());
1909 replyString << L"\"" << str << L"\" " << mtime_readable << L" " << file_size << L"\r\n";
1913 replyString << TEXT("\r\n");
1915 SetReplyString(boost::to_upper_copy(replyString.str()));
1919 bool ThumbnailCommand::DoExecuteGenerate()
1921 if (parameters().size() < 2)
1923 SetReplyString(L"402 THUMBNAIL GENERATE ERROR\r\n");
1929 thumb_gen_->generate(parameters()[1]);
1930 SetReplyString(L"202 THUMBNAIL GENERATE OK\r\n");
1935 SetReplyString(L"500 THUMBNAIL GENERATE ERROR\r\n");
1940 bool ThumbnailCommand::DoExecuteGenerateAll()
1944 thumb_gen_->generate_all();
1945 SetReplyString(L"202 THUMBNAIL GENERATE_ALL OK\r\n");
1950 SetReplyString(L"500 THUMBNAIL GENERATE_ALL ERROR\r\n");
1955 bool KillCommand::DoExecute()
1957 shutdown_server_now_->set_value(false); //false for not attempting to restart
1958 SetReplyString(TEXT("202 KILL OK\r\n"));
1962 bool RestartCommand::DoExecute()
1964 shutdown_server_now_->set_value(true); //true for attempting to restart
1965 SetReplyString(TEXT("202 RESTART OK\r\n"));
1970 }} //namespace caspar