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>
39 #include <core/producer/frame_producer.h>
40 #include <core/video_format.h>
41 #include <core/producer/transition/transition_producer.h>
42 #include <core/frame/frame_transform.h>
43 #include <core/producer/stage.h>
44 #include <core/producer/layer.h>
45 #include <core/mixer/mixer.h>
46 #include <core/consumer/output.h>
47 #include <core/thumbnail_generator.h>
49 #include <modules/reroute/producer/reroute_producer.h>
50 #include <modules/bluefish/bluefish.h>
51 #include <modules/decklink/decklink.h>
52 #include <modules/ffmpeg/ffmpeg.h>
53 #include <modules/flash/flash.h>
54 #include <modules/flash/util/swf.h>
55 #include <modules/flash/producer/flash_producer.h>
56 #include <modules/flash/producer/cg_proxy.h>
57 #include <modules/ffmpeg/producer/util/util.h>
58 #include <modules/image/image.h>
59 #include <modules/screen/screen.h>
60 #include <modules/reroute/producer/reroute_producer.h>
69 #include <boost/date_time/posix_time/posix_time.hpp>
70 #include <boost/lexical_cast.hpp>
71 #include <boost/algorithm/string.hpp>
72 #include <boost/filesystem.hpp>
73 #include <boost/filesystem/fstream.hpp>
74 #include <boost/regex.hpp>
75 #include <boost/property_tree/xml_parser.hpp>
76 #include <boost/locale.hpp>
77 #include <boost/range/adaptor/transformed.hpp>
78 #include <boost/range/algorithm/copy.hpp>
79 #include <boost/archive/iterators/base64_from_binary.hpp>
80 #include <boost/archive/iterators/insert_linebreaks.hpp>
81 #include <boost/archive/iterators/transform_width.hpp>
83 #include <tbb/concurrent_unordered_map.h>
87 102 [action] Information that [action] has happened
88 101 [action] Information that [action] has happened plus one row of data
90 202 [command] OK [command] has been executed
91 201 [command] OK [command] has been executed, plus one row of data
92 200 [command] OK [command] has been executed, plus multiple lines of data. ends with an empty line
94 400 ERROR the command could not be understood
95 401 [command] ERROR invalid/missing channel
96 402 [command] ERROR parameter missing
97 403 [command] ERROR invalid parameter
98 404 [command] ERROR file not found
100 500 FAILED internal error
101 501 [command] FAILED internal error
102 502 [command] FAILED could not read file
103 503 [command] FAILED access denied
105 600 [command] FAILED [command] not implemented
108 namespace caspar { namespace protocol {
110 using namespace core;
112 std::wstring read_file_base64(const boost::filesystem::wpath& file)
114 using namespace boost::archive::iterators;
116 boost::filesystem::ifstream filestream(file, std::ios::binary);
121 // From http://www.webbiscuit.co.uk/2012/04/02/base64-encoder-and-boost/
124 insert_linebreaks< // insert line breaks every 76 characters
125 base64_from_binary< // convert binary values to base64 characters
126 transform_width< // retrieve 6 bit integers from a sequence of 8 bit bytes
127 const unsigned char *,
134 base64_iterator; // compose all the above operations in to a new iterator
135 auto length = boost::filesystem::file_size(file);
136 std::vector<char> bytes;
137 bytes.resize(length);
138 filestream.read(bytes.data(), length);
142 while (bytes.size() % 3 != 0)
145 bytes.push_back(0x00);
148 std::string result(base64_iterator(bytes.data()), base64_iterator(bytes.data() + length - padding));
149 result.insert(result.end(), padding, '=');
151 return std::wstring(result.begin(), result.end());
154 std::wstring read_utf8_file(const boost::filesystem::wpath& file)
156 std::wstringstream result;
157 boost::filesystem::wifstream filestream(file);
164 result << filestream.rdbuf();
170 std::wstring read_latin1_file(const boost::filesystem::wpath& file)
172 boost::locale::generator gen;
173 gen.locale_cache_enabled(true);
174 gen.categories(boost::locale::codepage_facet);
176 std::stringstream result_stream;
177 boost::filesystem::ifstream filestream(file);
178 filestream.imbue(gen("en_US.ISO8859-1"));
183 result_stream << filestream.rdbuf();
186 std::string result = result_stream.str();
187 std::wstring widened_result;
189 // The first 255 codepoints in unicode is the same as in latin1
190 auto from_signed_to_signed = std::function<unsigned char(char)>(
191 [] (char c) { return static_cast<unsigned char>(c); }
194 result | boost::adaptors::transformed(from_signed_to_signed),
195 std::back_inserter(widened_result));
197 return widened_result;
200 std::wstring read_file(const boost::filesystem::wpath& file)
202 static const uint8_t BOM[] = {0xef, 0xbb, 0xbf};
204 if (!boost::filesystem::exists(file))
209 if (boost::filesystem::file_size(file) >= 3)
211 boost::filesystem::ifstream bom_stream(file);
214 bom_stream.read(header, 3);
217 if (std::memcmp(BOM, header, 3) == 0)
218 return read_utf8_file(file);
221 return read_latin1_file(file);
224 std::wstring MediaInfo(const boost::filesystem::path& path)
226 if(boost::filesystem::is_regular_file(path))
228 std::wstring clipttype = TEXT("N/A");
229 std::wstring extension = boost::to_upper_copy(path.extension().wstring());
230 if(extension == TEXT(".TGA") || extension == TEXT(".COL") || extension == L".PNG" || extension == L".JPEG" || extension == L".JPG" ||
231 extension == L"GIF" || extension == L"BMP")
232 clipttype = TEXT("STILL");
233 else if(extension == TEXT(".WAV") || extension == TEXT(".MP3"))
234 clipttype = TEXT("AUDIO");
235 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()))
236 clipttype = TEXT("MOVIE");
238 if(clipttype != TEXT("N/A"))
240 auto is_not_digit = [](char c){ return std::isdigit(c) == 0; };
242 auto relativePath = boost::filesystem::path(path.wstring().substr(env::media_folder().size()-1, path.wstring().size()));
244 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(path)));
245 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), is_not_digit), writeTimeStr.end());
246 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
248 auto sizeStr = boost::lexical_cast<std::wstring>(boost::filesystem::file_size(path));
249 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), is_not_digit), sizeStr.end());
250 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
252 auto str = relativePath.replace_extension(TEXT("")).native();
253 while(str.size() > 0 && (str[0] == '\\' || str[0] == '/'))
254 str = std::wstring(str.begin() + 1, str.end());
256 return std::wstring() + TEXT("\"") + str +
257 + TEXT("\" ") + clipttype +
258 + TEXT(" ") + sizeStr +
259 + TEXT(" ") + writeTimeWStr +
266 std::wstring ListMedia()
268 std::wstringstream replyString;
269 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end; ++itr)
270 replyString << MediaInfo(itr->path());
272 return boost::to_upper_copy(replyString.str());
275 std::wstring ListTemplates()
277 std::wstringstream replyString;
279 for (boost::filesystem::recursive_directory_iterator itr(env::template_folder()), end; itr != end; ++itr)
281 if(boost::filesystem::is_regular_file(itr->path()) && (itr->path().extension() == L".ft" || itr->path().extension() == L".ct"))
283 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::template_folder().size()-1, itr->path().wstring().size()));
285 auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(itr->path())));
286 writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), [](char c){ return std::isdigit(c) == 0;}), writeTimeStr.end());
287 auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());
289 auto sizeStr = boost::lexical_cast<std::string>(boost::filesystem::file_size(itr->path()));
290 sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), [](char c){ return std::isdigit(c) == 0;}), sizeStr.end());
292 auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());
294 std::wstring dir = relativePath.parent_path().native();
295 std::wstring file = boost::to_upper_copy(relativePath.filename().wstring());
296 relativePath = boost::filesystem::wpath(dir + L"/" + file);
298 auto str = relativePath.replace_extension(TEXT("")).native();
299 boost::trim_if(str, boost::is_any_of("\\/"));
301 replyString << TEXT("\"") << str
302 << TEXT("\" ") << sizeWStr
303 << TEXT(" ") << writeTimeWStr
307 return replyString.str();
312 void AMCPCommand::SendReply()
314 if(replyString_.empty())
317 client_->send(std::move(replyString_));
320 bool DiagnosticsCommand::DoExecute()
324 diagnostics::show_graphs(true);
326 SetReplyString(TEXT("202 DIAG OK\r\n"));
332 CASPAR_LOG_CURRENT_EXCEPTION();
333 SetReplyString(TEXT("502 DIAG FAILED\r\n"));
338 bool ChannelGridCommand::DoExecute()
340 CASPAR_THROW_EXCEPTION(not_implemented());
343 //auto self = channels().back();
345 //std::vector<std::wstring> params;
346 //params.push_back(L"SCREEN");
347 //params.push_back(L"NAME");
348 //params.push_back(L"Channel Grid Window");
349 //auto screen = create_consumer(params);
351 //self->output().add(screen);
353 //BOOST_FOREACH(auto channel, channels())
355 // if(channel != self)
357 // auto producer = reroute::create_producer(self->frame_factory(), *channel);
358 // self->stage().load(index, producer, false);
359 // self->stage().play(index);
364 //int n = channels().size()-1;
365 //double delta = 1.0/static_cast<double>(n);
366 //for(int x = 0; x < n; ++x)
368 // for(int y = 0; y < n; ++y)
370 // int index = x+y*n+1;
371 // auto transform = [=](frame_transform transform) -> frame_transform
373 // transform.image_transform.fill_translation[0] = x*delta;
374 // transform.image_transform.fill_translation[1] = y*delta;
375 // transform.image_transform.fill_scale[0] = delta;
376 // transform.image_transform.fill_scale[1] = delta;
377 // transform.image_transform.clip_translation[0] = x*delta;
378 // transform.image_transform.clip_translation[1] = y*delta;
379 // transform.image_transform.clip_scale[0] = delta;
380 // transform.image_transform.clip_scale[1] = delta;
383 // self->stage().apply_transform(index, transform);
390 bool CallCommand::DoExecute()
392 //Perform loading of the clip
395 auto result = channel()->stage().call(layer_index(), parameters());
397 if(!result.timed_wait(boost::posix_time::seconds(2)))
398 CASPAR_THROW_EXCEPTION(timed_out());
400 std::wstringstream replyString;
401 if(result.get().empty())
402 replyString << TEXT("202 CALL OK\r\n");
404 replyString << TEXT("201 CALL OK\r\n") << result.get() << L"\r\n";
406 SetReplyString(replyString.str());
412 CASPAR_LOG_CURRENT_EXCEPTION();
413 SetReplyString(TEXT("502 CALL FAILED\r\n"));
418 tbb::concurrent_unordered_map<int, std::vector<stage::transform_tuple_t>> deferred_transforms;
420 bool MixerCommand::DoExecute()
422 //Perform loading of the clip
425 bool defer = boost::iequals(parameters().back(), L"DEFER");
427 parameters().pop_back();
429 std::vector<stage::transform_tuple_t> transforms;
431 if(boost::iequals(parameters()[0], L"KEYER") || boost::iequals(parameters()[0], L"IS_KEY"))
433 bool value = boost::lexical_cast<int>(parameters().at(1));
434 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
436 transform.image_transform.is_key = value;
440 else if(boost::iequals(parameters()[0], L"OPACITY"))
442 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
443 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
445 double value = boost::lexical_cast<double>(parameters().at(1));
447 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
449 transform.image_transform.opacity = value;
451 }, duration, tween));
453 else if(boost::iequals(parameters()[0], L"FILL") || boost::iequals(parameters()[0], L"FILL_RECT"))
455 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
456 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
457 double x = boost::lexical_cast<double>(parameters().at(1));
458 double y = boost::lexical_cast<double>(parameters().at(2));
459 double x_s = boost::lexical_cast<double>(parameters().at(3));
460 double y_s = boost::lexical_cast<double>(parameters().at(4));
462 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) mutable -> frame_transform
464 transform.image_transform.fill_translation[0] = x;
465 transform.image_transform.fill_translation[1] = y;
466 transform.image_transform.fill_scale[0] = x_s;
467 transform.image_transform.fill_scale[1] = y_s;
469 }, duration, tween));
471 else if(boost::iequals(parameters()[0], L"CLIP") || boost::iequals(parameters()[0], L"CLIP_RECT"))
473 int duration = parameters().size() > 5 ? boost::lexical_cast<int>(parameters()[5]) : 0;
474 std::wstring tween = parameters().size() > 6 ? parameters()[6] : L"linear";
475 double x = boost::lexical_cast<double>(parameters().at(1));
476 double y = boost::lexical_cast<double>(parameters().at(2));
477 double x_s = boost::lexical_cast<double>(parameters().at(3));
478 double y_s = boost::lexical_cast<double>(parameters().at(4));
480 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
482 transform.image_transform.clip_translation[0] = x;
483 transform.image_transform.clip_translation[1] = y;
484 transform.image_transform.clip_scale[0] = x_s;
485 transform.image_transform.clip_scale[1] = y_s;
487 }, duration, tween));
489 else if(boost::iequals(parameters()[0], L"GRID"))
491 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
492 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
493 int n = boost::lexical_cast<int>(parameters().at(1));
494 double delta = 1.0/static_cast<double>(n);
495 for(int x = 0; x < n; ++x)
497 for(int y = 0; y < n; ++y)
500 transforms.push_back(stage::transform_tuple_t(index, [=](frame_transform transform) -> frame_transform
502 transform.image_transform.fill_translation[0] = x*delta;
503 transform.image_transform.fill_translation[1] = y*delta;
504 transform.image_transform.fill_scale[0] = delta;
505 transform.image_transform.fill_scale[1] = delta;
506 transform.image_transform.clip_translation[0] = x*delta;
507 transform.image_transform.clip_translation[1] = y*delta;
508 transform.image_transform.clip_scale[0] = delta;
509 transform.image_transform.clip_scale[1] = delta;
511 }, duration, tween));
515 else if(boost::iequals(parameters()[0], L"BLEND"))
517 auto blend_str = parameters().at(1);
518 int layer = layer_index();
519 channel()->mixer().set_blend_mode(layer_index(), get_blend_mode(blend_str));
521 else if(boost::iequals(parameters()[0], L"MASTERVOLUME"))
523 float master_volume = boost::lexical_cast<float>(parameters().at(1));
524 channel()->mixer().set_master_volume(master_volume);
526 else if(boost::iequals(parameters()[0], L"BRIGHTNESS"))
528 auto value = boost::lexical_cast<double>(parameters().at(1));
529 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
530 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
531 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
533 transform.image_transform.brightness = value;
535 }, duration, tween));
537 else if(boost::iequals(parameters()[0], L"SATURATION"))
539 auto value = boost::lexical_cast<double>(parameters().at(1));
540 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
541 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
542 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
544 transform.image_transform.saturation = value;
546 }, duration, tween));
548 else if(parameters()[0] == L"CONTRAST")
550 auto value = boost::lexical_cast<double>(parameters().at(1));
551 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
552 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
553 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
555 transform.image_transform.contrast = value;
557 }, duration, tween));
559 else if(boost::iequals(parameters()[0], L"LEVELS"))
562 value.min_input = boost::lexical_cast<double>(parameters().at(1));
563 value.max_input = boost::lexical_cast<double>(parameters().at(2));
564 value.gamma = boost::lexical_cast<double>(parameters().at(3));
565 value.min_output = boost::lexical_cast<double>(parameters().at(4));
566 value.max_output = boost::lexical_cast<double>(parameters().at(5));
567 int duration = parameters().size() > 6 ? boost::lexical_cast<int>(parameters()[6]) : 0;
568 std::wstring tween = parameters().size() > 7 ? parameters()[7] : L"linear";
570 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
572 transform.image_transform.levels = value;
574 }, duration, tween));
576 else if(boost::iequals(parameters()[0], L"VOLUME"))
578 int duration = parameters().size() > 2 ? boost::lexical_cast<int>(parameters()[2]) : 0;
579 std::wstring tween = parameters().size() > 3 ? parameters()[3] : L"linear";
580 double value = boost::lexical_cast<double>(parameters()[1]);
582 transforms.push_back(stage::transform_tuple_t(layer_index(), [=](frame_transform transform) -> frame_transform
584 transform.audio_transform.volume = value;
586 }, duration, tween));
588 else if(boost::iequals(parameters()[0], L"CLEAR"))
590 int layer = layer_index(std::numeric_limits<int>::max());
592 if (layer == std::numeric_limits<int>::max())
594 channel()->stage().clear_transforms();
595 channel()->mixer().clear_blend_modes();
599 channel()->stage().clear_transforms(layer);
600 channel()->mixer().clear_blend_mode(layer);
603 else if(boost::iequals(parameters()[0], L"COMMIT"))
605 transforms = std::move(deferred_transforms[channel_index()]);
609 SetReplyString(TEXT("404 MIXER ERROR\r\n"));
615 auto& defer_tranforms = deferred_transforms[channel_index()];
616 defer_tranforms.insert(defer_tranforms.end(), transforms.begin(), transforms.end());
619 channel()->stage().apply_transforms(transforms);
621 SetReplyString(TEXT("202 MIXER OK\r\n"));
625 catch(file_not_found&)
627 CASPAR_LOG_CURRENT_EXCEPTION();
628 SetReplyString(TEXT("404 MIXER ERROR\r\n"));
633 CASPAR_LOG_CURRENT_EXCEPTION();
634 SetReplyString(TEXT("502 MIXER FAILED\r\n"));
639 bool SwapCommand::DoExecute()
641 //Perform loading of the clip
644 if(layer_index(-1) != -1)
646 std::vector<std::string> strs;
647 boost::split(strs, parameters()[0], boost::is_any_of("-"));
649 auto ch1 = channel();
650 auto ch2 = channels().at(boost::lexical_cast<int>(strs.at(0))-1);
652 int l1 = layer_index();
653 int l2 = boost::lexical_cast<int>(strs.at(1));
655 ch1->stage().swap_layer(l1, l2, ch2.channel->stage());
659 auto ch1 = channel();
660 auto ch2 = channels().at(boost::lexical_cast<int>(parameters()[0])-1);
661 ch1->stage().swap_layers(ch2.channel->stage());
664 SetReplyString(TEXT("202 SWAP OK\r\n"));
668 catch(file_not_found&)
670 CASPAR_LOG_CURRENT_EXCEPTION();
671 SetReplyString(TEXT("404 SWAP ERROR\r\n"));
676 CASPAR_LOG_CURRENT_EXCEPTION();
677 SetReplyString(TEXT("502 SWAP FAILED\r\n"));
682 bool AddCommand::DoExecute()
684 //Perform loading of the clip
687 //create_consumer still expects all parameters to be uppercase
688 BOOST_FOREACH(std::wstring& str, parameters())
690 boost::to_upper(str);
693 auto consumer = create_consumer(parameters());
694 channel()->output().add(layer_index(consumer->index()), consumer);
696 SetReplyString(TEXT("202 ADD OK\r\n"));
700 catch(file_not_found&)
702 CASPAR_LOG_CURRENT_EXCEPTION();
703 SetReplyString(TEXT("404 ADD ERROR\r\n"));
708 CASPAR_LOG_CURRENT_EXCEPTION();
709 SetReplyString(TEXT("502 ADD FAILED\r\n"));
714 bool RemoveCommand::DoExecute()
716 //Perform loading of the clip
719 auto index = layer_index(std::numeric_limits<int>::min());
720 if(index == std::numeric_limits<int>::min())
722 //create_consumer still expects all parameters to be uppercase
723 BOOST_FOREACH(std::wstring& str, parameters())
725 boost::to_upper(str);
728 index = create_consumer(parameters())->index();
731 channel()->output().remove(index);
733 SetReplyString(TEXT("202 REMOVE OK\r\n"));
737 catch(file_not_found&)
739 CASPAR_LOG_CURRENT_EXCEPTION();
740 SetReplyString(TEXT("404 REMOVE ERROR\r\n"));
745 CASPAR_LOG_CURRENT_EXCEPTION();
746 SetReplyString(TEXT("502 REMOVE FAILED\r\n"));
751 bool LoadCommand::DoExecute()
753 //Perform loading of the clip
756 auto pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
757 channel()->stage().load(layer_index(), pFP, true);
759 SetReplyString(TEXT("202 LOAD OK\r\n"));
763 catch(file_not_found&)
765 CASPAR_LOG_CURRENT_EXCEPTION();
766 SetReplyString(TEXT("404 LOAD ERROR\r\n"));
771 CASPAR_LOG_CURRENT_EXCEPTION();
772 SetReplyString(TEXT("502 LOAD FAILED\r\n"));
779 //std::function<std::wstring()> channel_cg_add_command::parse(const std::wstring& message, const std::vector<renderer::render_device_ptr>& channels)
781 // 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>.*)?");
783 // boost::wsmatch what;
784 // if(!boost::regex_match(message, what, expr))
787 // auto info = channel_info::parse(what, channels);
789 // int flash_layer_index = boost::lexical_cast<int>(what["FLASH_LAYER"].str());
791 // std::wstring templatename = what["TEMPLATE"].str();
792 // bool play_on_load = what["PLAY_ON_LOAD"].matched ? what["PLAY_ON_LOAD"].str() != L"0" : 0;
793 // std::wstring start_label = what["START_LABEL"].str();
794 // std::wstring data = get_data(what["DATA"].str());
796 // boost::replace_all(templatename, "\"", "");
798 // return [=]() -> std::wstring
800 // std::wstring fullFilename = flash::flash_producer::find_template(server::template_folder() + templatename);
801 // if(fullFilename.empty())
802 // CASPAR_THROW_EXCEPTION(file_not_found());
804 // std::wstring extension = boost::filesystem::wpath(fullFilename).extension();
805 // std::wstring filename = templatename;
806 // filename.append(extension);
808 // flash::flash::create_cg_proxy(info.video_channel, std::max<int>(DEFAULT_CHANNEL_LAYER+1, info.layer_index))
809 // ->add(flash_layer_index, filename, play_on_load, start_label, data);
811 // CASPAR_LOG(info) << L"Executed [amcp_channel_cg_add]";
815 bool LoadbgCommand::DoExecute()
817 transition_info transitionInfo;
823 std::wstring message;
824 for(size_t n = 0; n < parameters().size(); ++n)
825 message += boost::to_upper_copy(parameters()[n]) + L" ";
827 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)?.*");
829 if(boost::regex_match(message, what, expr))
831 auto transition = what["TRANSITION"].str();
832 transitionInfo.duration = boost::lexical_cast<size_t>(what["DURATION"].str());
833 auto direction = what["DIRECTION"].matched ? what["DIRECTION"].str() : L"";
834 auto tween = what["TWEEN"].matched ? what["TWEEN"].str() : L"";
835 transitionInfo.tweener = tween;
837 if(transition == TEXT("CUT"))
838 transitionInfo.type = transition_type::cut;
839 else if(transition == TEXT("MIX"))
840 transitionInfo.type = transition_type::mix;
841 else if(transition == TEXT("PUSH"))
842 transitionInfo.type = transition_type::push;
843 else if(transition == TEXT("SLIDE"))
844 transitionInfo.type = transition_type::slide;
845 else if(transition == TEXT("WIPE"))
846 transitionInfo.type = transition_type::wipe;
848 if(direction == TEXT("FROMLEFT"))
849 transitionInfo.direction = transition_direction::from_left;
850 else if(direction == TEXT("FROMRIGHT"))
851 transitionInfo.direction = transition_direction::from_right;
852 else if(direction == TEXT("LEFT"))
853 transitionInfo.direction = transition_direction::from_right;
854 else if(direction == TEXT("RIGHT"))
855 transitionInfo.direction = transition_direction::from_left;
858 //Perform loading of the clip
861 std::shared_ptr<core::frame_producer> pFP;
863 static boost::wregex expr(L"\\[(?<CHANNEL>\\d+)\\]", boost::regex::icase);
866 if(boost::regex_match(parameters().at(0), what, expr))
868 auto channel_index = boost::lexical_cast<int>(what["CHANNEL"].str());
869 pFP = reroute::create_producer(*channels().at(channel_index-1).channel);
873 pFP = create_producer(channel()->frame_factory(), channel()->video_format_desc(), parameters());
876 if(pFP == frame_producer::empty())
877 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info(parameters().size() > 0 ? parameters()[0] : L""));
879 bool auto_play = contains_param(L"AUTO", parameters());
881 auto pFP2 = create_transition_producer(channel()->video_format_desc().field_mode, spl::make_shared_ptr(pFP), transitionInfo);
883 channel()->stage().load(layer_index(), pFP2, false, transitionInfo.duration); // TODO: LOOP
885 channel()->stage().load(layer_index(), pFP2, false); // TODO: LOOP
888 SetReplyString(TEXT("202 LOADBG OK\r\n"));
892 catch(file_not_found&)
894 CASPAR_LOG(error) << L"File not found. No match found for parameters. Check syntax.";
895 SetReplyString(TEXT("404 LOADBG ERROR\r\n"));
900 CASPAR_LOG_CURRENT_EXCEPTION();
901 SetReplyString(TEXT("502 LOADBG FAILED\r\n"));
906 bool PauseCommand::DoExecute()
910 channel()->stage().pause(layer_index());
911 SetReplyString(TEXT("202 PAUSE OK\r\n"));
916 SetReplyString(TEXT("501 PAUSE FAILED\r\n"));
922 bool PlayCommand::DoExecute()
926 if(!parameters().empty())
928 LoadbgCommand lbg(*this);
931 throw std::exception();
934 channel()->stage().play(layer_index());
936 SetReplyString(TEXT("202 PLAY OK\r\n"));
941 SetReplyString(TEXT("501 PLAY FAILED\r\n"));
947 bool StopCommand::DoExecute()
951 channel()->stage().stop(layer_index());
952 SetReplyString(TEXT("202 STOP OK\r\n"));
957 SetReplyString(TEXT("501 STOP FAILED\r\n"));
963 bool ClearCommand::DoExecute()
965 int index = layer_index(std::numeric_limits<int>::min());
966 if(index != std::numeric_limits<int>::min())
967 channel()->stage().clear(index);
969 channel()->stage().clear();
971 SetReplyString(TEXT("202 CLEAR OK\r\n"));
976 bool PrintCommand::DoExecute()
978 channel()->output().add(create_consumer(boost::assign::list_of(L"IMAGE")));
980 SetReplyString(TEXT("202 PRINT OK\r\n"));
985 bool LogCommand::DoExecute()
987 if(boost::iequals(parameters().at(0), L"LEVEL"))
988 log::set_log_level(parameters().at(1));
990 SetReplyString(TEXT("202 LOG OK\r\n"));
995 bool CGCommand::DoExecute()
999 std::wstring command = boost::to_upper_copy(parameters()[0]);
1000 if(command == TEXT("ADD"))
1001 return DoExecuteAdd();
1002 else if(command == TEXT("PLAY"))
1003 return DoExecutePlay();
1004 else if(command == TEXT("STOP"))
1005 return DoExecuteStop();
1006 else if(command == TEXT("NEXT"))
1007 return DoExecuteNext();
1008 else if(command == TEXT("REMOVE"))
1009 return DoExecuteRemove();
1010 else if(command == TEXT("CLEAR"))
1011 return DoExecuteClear();
1012 else if(command == TEXT("UPDATE"))
1013 return DoExecuteUpdate();
1014 else if(command == TEXT("INVOKE"))
1015 return DoExecuteInvoke();
1016 else if(command == TEXT("INFO"))
1017 return DoExecuteInfo();
1021 CASPAR_LOG_CURRENT_EXCEPTION();
1024 SetReplyString(TEXT("403 CG ERROR\r\n"));
1028 bool CGCommand::ValidateLayer(const std::wstring& layerstring) {
1029 int length = layerstring.length();
1030 for(int i = 0; i < length; ++i) {
1031 if(!_istdigit(layerstring[i])) {
1039 bool CGCommand::DoExecuteAdd() {
1040 //CG 1 ADD 0 "template_folder/templatename" [STARTLABEL] 0/1 [DATA]
1042 int layer = 0; //_parameters[1]
1043 // std::wstring templateName; //_parameters[2]
1044 std::wstring label; //_parameters[3]
1045 bool bDoStart = false; //_parameters[3] alt. _parameters[4]
1046 // std::wstring data; //_parameters[4] alt. _parameters[5]
1048 if(parameters().size() < 4)
1050 SetReplyString(TEXT("402 CG ERROR\r\n"));
1053 unsigned int dataIndex = 4;
1055 if(!ValidateLayer(parameters()[1]))
1057 SetReplyString(TEXT("403 CG ERROR\r\n"));
1061 layer = boost::lexical_cast<int>(parameters()[1]);
1063 if(parameters()[3].length() > 1)
1065 label = parameters()[3];
1068 if(parameters().size() > 4 && parameters()[4].length() > 0) //read play-on-load-flag
1069 bDoStart = (parameters()[4][0]==TEXT('1')) ? true : false;
1072 SetReplyString(TEXT("402 CG ERROR\r\n"));
1076 else if(parameters()[3].length() > 0) { //read play-on-load-flag
1077 bDoStart = (parameters()[3][0]==TEXT('1')) ? true : false;
1081 SetReplyString(TEXT("403 CG ERROR\r\n"));
1085 const TCHAR* pDataString = 0;
1086 std::wstringstream data;
1087 std::wstring dataFromFile;
1088 if(parameters().size() > dataIndex)
1090 const std::wstring& dataString = parameters()[dataIndex];
1092 if(dataString[0] == TEXT('<')) //the data is an XML-string
1093 pDataString = dataString.c_str();
1096 //The data is not an XML-string, it must be a filename
1097 std::wstring filename = env::data_folder();
1098 filename.append(dataString);
1099 filename.append(TEXT(".ftd"));
1101 dataFromFile = read_file(boost::filesystem::wpath(filename));
1102 pDataString = dataFromFile.c_str();
1106 std::wstring fullFilename = flash::find_template(env::template_folder() + parameters()[2]);
1107 if(!fullFilename.empty())
1109 std::wstring extension = boost::filesystem::path(fullFilename).extension().wstring();
1110 std::wstring filename = parameters()[2];
1111 filename.append(extension);
1113 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(""));
1114 SetReplyString(TEXT("202 CG OK\r\n"));
1118 CASPAR_LOG(warning) << "Could not find template " << parameters()[2];
1119 SetReplyString(TEXT("404 CG ERROR\r\n"));
1124 bool CGCommand::DoExecutePlay()
1126 if(parameters().size() > 1)
1128 if(!ValidateLayer(parameters()[1]))
1130 SetReplyString(TEXT("403 CG ERROR\r\n"));
1133 int layer = boost::lexical_cast<int>(parameters()[1]);
1134 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).play(layer);
1138 SetReplyString(TEXT("402 CG ERROR\r\n"));
1142 SetReplyString(TEXT("202 CG OK\r\n"));
1146 bool CGCommand::DoExecuteStop()
1148 if(parameters().size() > 1)
1150 if(!ValidateLayer(parameters()[1]))
1152 SetReplyString(TEXT("403 CG ERROR\r\n"));
1155 int layer = boost::lexical_cast<int>(parameters()[1]);
1156 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).stop(layer, 0);
1160 SetReplyString(TEXT("402 CG ERROR\r\n"));
1164 SetReplyString(TEXT("202 CG OK\r\n"));
1168 bool CGCommand::DoExecuteNext()
1170 if(parameters().size() > 1)
1172 if(!ValidateLayer(parameters()[1]))
1174 SetReplyString(TEXT("403 CG ERROR\r\n"));
1178 int layer = boost::lexical_cast<int>(parameters()[1]);
1179 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).next(layer);
1183 SetReplyString(TEXT("402 CG ERROR\r\n"));
1187 SetReplyString(TEXT("202 CG OK\r\n"));
1191 bool CGCommand::DoExecuteRemove()
1193 if(parameters().size() > 1)
1195 if(!ValidateLayer(parameters()[1]))
1197 SetReplyString(TEXT("403 CG ERROR\r\n"));
1201 int layer = boost::lexical_cast<int>(parameters()[1]);
1202 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).remove(layer);
1206 SetReplyString(TEXT("402 CG ERROR\r\n"));
1210 SetReplyString(TEXT("202 CG OK\r\n"));
1214 bool CGCommand::DoExecuteClear()
1216 channel()->stage().clear(layer_index(flash::cg_proxy::DEFAULT_LAYER));
1217 SetReplyString(TEXT("202 CG OK\r\n"));
1221 bool CGCommand::DoExecuteUpdate()
1225 if(!ValidateLayer(parameters().at(1)))
1227 SetReplyString(TEXT("403 CG ERROR\r\n"));
1231 std::wstring dataString = parameters().at(2);
1232 if(dataString.at(0) != TEXT('<'))
1234 //The data is not an XML-string, it must be a filename
1235 std::wstring filename = env::data_folder();
1236 filename.append(dataString);
1237 filename.append(TEXT(".ftd"));
1239 dataString = read_file(boost::filesystem::wpath(filename));
1242 int layer = boost::lexical_cast<int>(parameters()[1]);
1243 flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).update(layer, dataString);
1247 SetReplyString(TEXT("402 CG ERROR\r\n"));
1251 SetReplyString(TEXT("202 CG OK\r\n"));
1255 bool CGCommand::DoExecuteInvoke()
1257 std::wstringstream replyString;
1258 replyString << TEXT("201 CG OK\r\n");
1260 if(parameters().size() > 2)
1262 if(!ValidateLayer(parameters()[1]))
1264 SetReplyString(TEXT("403 CG ERROR\r\n"));
1267 int layer = boost::lexical_cast<int>(parameters()[1]);
1268 auto result = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).invoke(layer, parameters()[2]);
1269 replyString << result << TEXT("\r\n");
1273 SetReplyString(TEXT("402 CG ERROR\r\n"));
1277 SetReplyString(replyString.str());
1281 bool CGCommand::DoExecuteInfo()
1283 std::wstringstream replyString;
1284 replyString << TEXT("201 CG OK\r\n");
1286 if(parameters().size() > 1)
1288 if(!ValidateLayer(parameters()[1]))
1290 SetReplyString(TEXT("403 CG ERROR\r\n"));
1294 int layer = boost::lexical_cast<int>(parameters()[1]);
1295 auto desc = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).description(layer);
1297 replyString << desc << TEXT("\r\n");
1301 auto info = flash::create_cg_proxy(spl::shared_ptr<core::video_channel>(channel()), layer_index(flash::cg_proxy::DEFAULT_LAYER)).template_host_info();
1302 replyString << info << TEXT("\r\n");
1305 SetReplyString(replyString.str());
1309 bool DataCommand::DoExecute()
1311 std::wstring command = boost::to_upper_copy(parameters()[0]);
1312 if(command == TEXT("STORE"))
1313 return DoExecuteStore();
1314 else if(command == TEXT("RETRIEVE"))
1315 return DoExecuteRetrieve();
1316 else if(command == TEXT("REMOVE"))
1317 return DoExecuteRemove();
1318 else if(command == TEXT("LIST"))
1319 return DoExecuteList();
1321 SetReplyString(TEXT("403 DATA ERROR\r\n"));
1325 bool DataCommand::DoExecuteStore()
1327 if(parameters().size() < 3)
1329 SetReplyString(TEXT("402 DATA STORE ERROR\r\n"));
1333 std::wstring filename = env::data_folder();
1334 filename.append(parameters()[1]);
1335 filename.append(TEXT(".ftd"));
1337 auto data_path = boost::filesystem::wpath(
1338 boost::filesystem::wpath(filename).parent_path());
1340 if(!boost::filesystem::exists(data_path))
1341 boost::filesystem::create_directories(data_path);
1343 std::wofstream datafile(filename.c_str());
1346 SetReplyString(TEXT("501 DATA STORE FAILED\r\n"));
1350 datafile << static_cast<wchar_t>(65279); // UTF-8 BOM character
1351 datafile << parameters()[2] << std::flush;
1354 std::wstring replyString = TEXT("202 DATA STORE OK\r\n");
1355 SetReplyString(replyString);
1359 bool DataCommand::DoExecuteRetrieve()
1361 if(parameters().size() < 2)
1363 SetReplyString(TEXT("402 DATA RETRIEVE ERROR\r\n"));
1367 std::wstring filename = env::data_folder();
1368 filename.append(parameters()[1]);
1369 filename.append(TEXT(".ftd"));
1371 std::wstring file_contents = read_file(boost::filesystem::wpath(filename));
1373 if (file_contents.empty())
1375 SetReplyString(TEXT("404 DATA RETRIEVE ERROR\r\n"));
1379 std::wstringstream reply(TEXT("201 DATA RETRIEVE OK\r\n"));
1381 std::wstringstream file_contents_stream(file_contents);
1384 bool firstLine = true;
1385 while(std::getline(file_contents_stream, line))
1396 SetReplyString(reply.str());
1400 bool DataCommand::DoExecuteRemove()
1402 if (parameters().size() < 2)
1404 SetReplyString(TEXT("402 DATA REMOVE ERROR\r\n"));
1408 std::wstring filename = env::data_folder();
1409 filename.append(parameters()[1]);
1410 filename.append(TEXT(".ftd"));
1412 if (!boost::filesystem::exists(filename))
1414 SetReplyString(TEXT("404 DATA REMOVE ERROR\r\n"));
1418 if (!boost::filesystem::remove(filename))
1420 SetReplyString(TEXT("403 DATA REMOVE ERROR\r\n"));
1424 SetReplyString(TEXT("201 DATA REMOVE OK\r\n"));
1429 bool DataCommand::DoExecuteList()
1431 std::wstringstream replyString;
1432 replyString << TEXT("200 DATA LIST OK\r\n");
1434 for (boost::filesystem::recursive_directory_iterator itr(env::data_folder()), end; itr != end; ++itr)
1436 if(boost::filesystem::is_regular_file(itr->path()))
1438 if(!boost::iequals(itr->path().extension().wstring(), L".ftd"))
1441 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::data_folder().size()-1, itr->path().wstring().size()));
1443 auto str = relativePath.replace_extension(TEXT("")).native();
1444 if(str[0] == '\\' || str[0] == '/')
1445 str = std::wstring(str.begin() + 1, str.end());
1447 replyString << str << TEXT("\r\n");
1451 replyString << TEXT("\r\n");
1453 SetReplyString(boost::to_upper_copy(replyString.str()));
1457 bool CinfCommand::DoExecute()
1459 std::wstringstream replyString;
1464 for (boost::filesystem::recursive_directory_iterator itr(env::media_folder()), end; itr != end && info.empty(); ++itr)
1466 auto path = itr->path();
1467 auto file = path.replace_extension(L"").filename();
1468 if(boost::iequals(file.wstring(), parameters().at(0)))
1469 info += MediaInfo(itr->path()) + L"\r\n";
1474 SetReplyString(TEXT("404 CINF ERROR\r\n"));
1477 replyString << TEXT("200 CINF OK\r\n");
1478 replyString << info << "\r\n";
1482 CASPAR_LOG_CURRENT_EXCEPTION();
1483 SetReplyString(TEXT("404 CINF ERROR\r\n"));
1487 SetReplyString(replyString.str());
1491 void GenerateChannelInfo(int index, const spl::shared_ptr<core::video_channel>& pChannel, std::wstringstream& replyString)
1493 replyString << index+1 << TEXT(" ") << pChannel->video_format_desc().name << TEXT(" PLAYING") << TEXT("\r\n");
1496 bool InfoCommand::DoExecute()
1498 std::wstringstream replyString;
1500 boost::property_tree::xml_writer_settings<wchar_t> w(' ', 3);
1504 if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"TEMPLATE"))
1506 replyString << L"201 INFO TEMPLATE OK\r\n";
1508 // Needs to be extended for any file, not just flash.
1510 auto filename = flash::find_template(env::template_folder() + parameters().at(1));
1512 std::wstringstream str;
1513 str << u16(flash::read_template_meta_info(filename));
1514 boost::property_tree::wptree info;
1515 boost::property_tree::xml_parser::read_xml(str, info, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);
1517 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1519 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"CONFIG"))
1521 replyString << L"201 INFO CONFIG OK\r\n";
1523 boost::property_tree::write_xml(replyString, caspar::env::properties(), w);
1525 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"PATHS"))
1527 replyString << L"201 INFO PATHS OK\r\n";
1529 boost::property_tree::wptree info;
1530 info.add_child(L"paths", caspar::env::properties().get_child(L"configuration.paths"));
1531 info.add(L"paths.initial-path", boost::filesystem3::initial_path().wstring() + L"\\");
1533 boost::property_tree::write_xml(replyString, info, w);
1535 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SYSTEM"))
1537 replyString << L"201 INFO SYSTEM OK\r\n";
1539 boost::property_tree::wptree info;
1541 info.add(L"system.name", caspar::system_product_name());
1542 info.add(L"system.windows.name", caspar::win_product_name());
1543 info.add(L"system.windows.service-pack", caspar::win_sp_version());
1544 info.add(L"system.cpu", caspar::cpu_info());
1546 BOOST_FOREACH(auto device, caspar::decklink::device_list())
1547 info.add(L"system.decklink.device", device);
1549 BOOST_FOREACH(auto device, caspar::bluefish::device_list())
1550 info.add(L"system.bluefish.device", device);
1552 info.add(L"system.flash", caspar::flash::version());
1553 //info.add(L"system.free-image", caspar::image::version());
1554 info.add(L"system.ffmpeg.avcodec", caspar::ffmpeg::avcodec_version());
1555 info.add(L"system.ffmpeg.avformat", caspar::ffmpeg::avformat_version());
1556 info.add(L"system.ffmpeg.avfilter", caspar::ffmpeg::avfilter_version());
1557 info.add(L"system.ffmpeg.avutil", caspar::ffmpeg::avutil_version());
1558 info.add(L"system.ffmpeg.swscale", caspar::ffmpeg::swscale_version());
1560 boost::property_tree::write_xml(replyString, info, w);
1562 else if(parameters().size() >= 1 && boost::iequals(parameters()[0], L"SERVER"))
1564 replyString << L"201 INFO SERVER OK\r\n";
1566 boost::property_tree::wptree info;
1569 BOOST_FOREACH(auto channel, channels())
1570 info.add_child(L"channels.channel", channel.channel->info())
1571 .add(L"index", ++index);
1573 boost::property_tree::write_xml(replyString, info, w);
1577 if(parameters().size() >= 1)
1579 replyString << TEXT("201 INFO OK\r\n");
1580 boost::property_tree::wptree info;
1582 std::vector<std::wstring> split;
1583 boost::split(split, parameters()[0], boost::is_any_of("-"));
1585 int layer = std::numeric_limits<int>::min();
1586 int channel = boost::lexical_cast<int>(split[0]) - 1;
1588 if(split.size() > 1)
1589 layer = boost::lexical_cast<int>(split[1]);
1591 if(layer == std::numeric_limits<int>::min())
1593 info.add_child(L"channel", channels().at(channel).channel->info())
1594 .add(L"index", channel);
1598 if(parameters().size() >= 2)
1600 if(boost::iequals(parameters()[1], L"B"))
1601 info.add_child(L"producer", channels().at(channel).channel->stage().background(layer).get()->info());
1603 info.add_child(L"producer", channels().at(channel).channel->stage().foreground(layer).get()->info());
1607 info.add_child(L"layer", channels().at(channel).channel->stage().info(layer).get())
1608 .add(L"index", layer);
1611 boost::property_tree::xml_parser::write_xml(replyString, info, w);
1615 // This is needed for backwards compatibility with old clients
1616 replyString << TEXT("200 INFO OK\r\n");
1617 for(size_t n = 0; n < channels().size(); ++n)
1618 GenerateChannelInfo(n, channels()[n].channel, replyString);
1625 CASPAR_LOG_CURRENT_EXCEPTION();
1626 SetReplyString(TEXT("403 INFO ERROR\r\n"));
1630 replyString << TEXT("\r\n");
1631 SetReplyString(replyString.str());
1635 bool ClsCommand::DoExecute()
1647 std::wstringstream replyString;
1648 replyString << TEXT("200 CLS OK\r\n");
1649 replyString << ListMedia();
1650 replyString << TEXT("\r\n");
1651 SetReplyString(boost::to_upper_copy(replyString.str()));
1655 CASPAR_LOG_CURRENT_EXCEPTION();
1656 SetReplyString(TEXT("501 CLS FAILED\r\n"));
1663 bool TlsCommand::DoExecute()
1667 std::wstringstream replyString;
1668 replyString << TEXT("200 TLS OK\r\n");
1670 replyString << ListTemplates();
1671 replyString << TEXT("\r\n");
1673 SetReplyString(replyString.str());
1677 CASPAR_LOG_CURRENT_EXCEPTION();
1678 SetReplyString(TEXT("501 TLS FAILED\r\n"));
1684 bool VersionCommand::DoExecute()
1686 std::wstring replyString = TEXT("201 VERSION OK\r\n") + env::version() + TEXT("\r\n");
1688 if(parameters().size() > 0)
1690 if(boost::iequals(parameters()[0], L"FLASH"))
1691 replyString = TEXT("201 VERSION OK\r\n") + flash::version() + TEXT("\r\n");
1692 else if(boost::iequals(parameters()[0], L"TEMPLATEHOST"))
1693 replyString = TEXT("201 VERSION OK\r\n") + flash::cg_version() + TEXT("\r\n");
1694 else if(!boost::iequals(parameters()[0], L"SERVER"))
1695 replyString = TEXT("403 VERSION ERROR\r\n");
1698 SetReplyString(replyString);
1702 bool ByeCommand::DoExecute()
1704 client()->disconnect();
1708 bool SetCommand::DoExecute()
1712 std::wstring name = boost::to_upper_copy(parameters()[0]);
1713 std::wstring value = boost::to_upper_copy(parameters()[1]);
1715 if(name == TEXT("MODE"))
1717 auto format_desc = core::video_format_desc(value);
1718 if(format_desc.format != core::video_format::invalid)
1720 channel()->video_format_desc(format_desc);
1721 SetReplyString(TEXT("202 SET MODE OK\r\n"));
1724 SetReplyString(TEXT("501 SET MODE FAILED\r\n"));
1728 this->SetReplyString(TEXT("403 SET ERROR\r\n"));
1733 CASPAR_LOG_CURRENT_EXCEPTION();
1734 SetReplyString(TEXT("501 SET FAILED\r\n"));
1741 bool LockCommand::DoExecute()
1745 auto it = parameters().begin();
1747 std::shared_ptr<caspar::IO::lock_container> lock;
1750 int channel_index = boost::lexical_cast<int>(*it) - 1;
1751 lock = channels().at(channel_index).lock;
1753 catch(const boost::bad_lexical_cast&) {}
1756 SetReplyString(L"401 LOCK ERROR\r\n");
1763 if(it == parameters().end()) //too few parameters
1765 SetReplyString(L"402 LOCK ERROR\r\n");
1769 std::wstring command = boost::to_upper_copy(*it);
1770 if(command == L"ACQUIRE")
1773 if(it == parameters().end()) //too few parameters
1775 SetReplyString(L"402 LOCK ACQUIRE ERROR\r\n");
1778 std::wstring lock_phrase = (*it);
1780 //TODO: read options
1784 //just lock one channel
1785 if(!lock->try_lock(lock_phrase, client()))
1787 SetReplyString(L"503 LOCK ACQUIRE FAILED\r\n");
1793 //TODO: lock all channels
1794 CASPAR_THROW_EXCEPTION(not_implemented());
1796 SetReplyString(L"202 LOCK ACQUIRE OK\r\n");
1799 else if(command == L"RELEASE")
1803 lock->release_lock(client());
1807 //TODO: release all channels
1808 CASPAR_THROW_EXCEPTION(not_implemented());
1810 SetReplyString(L"202 LOCK RELEASE OK\r\n");
1812 else if(command == L"CLEAR")
1814 std::wstring override_phrase = env::properties().get(L"configuration.lock-clear-phrase", L"");
1815 std::wstring client_override_phrase;
1816 if(!override_phrase.empty())
1819 if(it == parameters().end())
1821 SetReplyString(L"402 LOCK CLEAR ERROR\r\n");
1824 client_override_phrase = (*it);
1829 //just clear one channel
1830 if(client_override_phrase != override_phrase)
1832 SetReplyString(L"503 LOCK CLEAR FAILED\r\n");
1836 lock->clear_locks();
1840 //TODO: clear all channels
1841 CASPAR_THROW_EXCEPTION(not_implemented());
1844 SetReplyString(L"202 LOCK CLEAR OK\r\n");
1848 SetReplyString(L"403 LOCK ERROR\r\n");
1852 catch(not_implemented&)
1854 SetReplyString(L"600 LOCK FAILED\r\n");
1859 CASPAR_LOG_CURRENT_EXCEPTION();
1860 SetReplyString(L"501 LOCK FAILED\r\n");
1867 bool ThumbnailCommand::DoExecute()
1869 std::wstring command = boost::to_upper_copy(parameters()[0]);
1871 if (command == TEXT("RETRIEVE"))
1872 return DoExecuteRetrieve();
1873 else if (command == TEXT("LIST"))
1874 return DoExecuteList();
1875 else if (command == TEXT("GENERATE"))
1876 return DoExecuteGenerate();
1877 else if (command == TEXT("GENERATE_ALL"))
1878 return DoExecuteGenerateAll();
1880 SetReplyString(TEXT("403 THUMBNAIL ERROR\r\n"));
1884 bool ThumbnailCommand::DoExecuteRetrieve()
1886 if(parameters().size() < 2)
1888 SetReplyString(TEXT("402 THUMBNAIL RETRIEVE ERROR\r\n"));
1892 std::wstring filename = env::thumbnails_folder();
1893 filename.append(parameters()[1]);
1894 filename.append(TEXT(".png"));
1896 std::wstring file_contents = read_file_base64(boost::filesystem::wpath(filename));
1898 if (file_contents.empty())
1900 SetReplyString(TEXT("404 THUMBNAIL RETRIEVE ERROR\r\n"));
1904 std::wstringstream reply;
1906 reply << L"201 THUMBNAIL RETRIEVE OK\r\n";
1907 reply << file_contents;
1909 SetReplyString(reply.str());
1913 bool ThumbnailCommand::DoExecuteList()
1915 std::wstringstream replyString;
1916 replyString << TEXT("200 THUMBNAIL LIST OK\r\n");
1918 for (boost::filesystem::recursive_directory_iterator itr(env::thumbnails_folder()), end; itr != end; ++itr)
1920 if(boost::filesystem::is_regular_file(itr->path()))
1922 if(!boost::iequals(itr->path().extension().wstring(), L".png"))
1925 auto relativePath = boost::filesystem::wpath(itr->path().wstring().substr(env::thumbnails_folder().size()-1, itr->path().wstring().size()));
1927 auto str = relativePath.replace_extension(L"").native();
1928 if(str[0] == '\\' || str[0] == '/')
1929 str = std::wstring(str.begin() + 1, str.end());
1931 auto mtime = boost::filesystem::last_write_time(itr->path());
1932 auto mtime_readable = boost::posix_time::to_iso_wstring(boost::posix_time::from_time_t(mtime));
1933 auto file_size = boost::filesystem::file_size(itr->path());
1935 replyString << L"\"" << str << L"\" " << mtime_readable << L" " << file_size << L"\r\n";
1939 replyString << TEXT("\r\n");
1941 SetReplyString(boost::to_upper_copy(replyString.str()));
1945 bool ThumbnailCommand::DoExecuteGenerate()
1947 if (parameters().size() < 2)
1949 SetReplyString(L"402 THUMBNAIL GENERATE ERROR\r\n");
1955 thumb_gen_->generate(parameters()[1]);
1956 SetReplyString(L"202 THUMBNAIL GENERATE OK\r\n");
1961 SetReplyString(L"500 THUMBNAIL GENERATE ERROR\r\n");
1966 bool ThumbnailCommand::DoExecuteGenerateAll()
1970 thumb_gen_->generate_all();
1971 SetReplyString(L"202 THUMBNAIL GENERATE_ALL OK\r\n");
1976 SetReplyString(L"500 THUMBNAIL GENERATE_ALL ERROR\r\n");
1981 bool KillCommand::DoExecute()
1983 shutdown_server_now_->set_value(false); //false for not waiting for keypress
1984 SetReplyString(TEXT("202 KILL OK\r\n"));
1989 }} //namespace caspar