]> git.sesse.net Git - casparcg/blob - protocol/amcp/AMCPCommandsImpl.cpp
2026b852a7d4748352aff3b98a46fd550afb6416
[casparcg] / protocol / amcp / AMCPCommandsImpl.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Nicklas P Andersson\r
20 */\r
21 \r
22 #include "../StdAfx.h"\r
23 \r
24 #if defined(_MSC_VER)\r
25 #pragma warning (push, 1) // TODO: Legacy code, just disable warnings\r
26 #endif\r
27 \r
28 #include "AMCPCommandsImpl.h"\r
29 #include "AMCPProtocolStrategy.h"\r
30 \r
31 #include <common/env.h>\r
32 \r
33 #include <common/log/log.h>\r
34 #include <common/diagnostics/graph.h>\r
35 #include <common/os/windows/current_version.h>\r
36 #include <common/os/windows/system_info.h>\r
37 \r
38 #include <core/producer/frame_producer.h>\r
39 #include <core/video_format.h>\r
40 #include <core/producer/transition/transition_producer.h>\r
41 #include <core/producer/frame/frame_transform.h>\r
42 #include <core/producer/stage.h>\r
43 #include <core/producer/layer.h>\r
44 #include <core/mixer/mixer.h>\r
45 #include <core/consumer/output.h>\r
46 \r
47 #include <modules/bluefish/bluefish.h>\r
48 #include <modules/decklink/decklink.h>\r
49 #include <modules/ffmpeg/ffmpeg.h>\r
50 #include <modules/flash/flash.h>\r
51 #include <modules/flash/util/swf.h>\r
52 #include <modules/flash/producer/flash_producer.h>\r
53 #include <modules/flash/producer/cg_producer.h>\r
54 #include <modules/ffmpeg/producer/util/util.h>\r
55 #include <modules/image/image.h>\r
56 \r
57 #include <algorithm>\r
58 #include <locale>\r
59 #include <fstream>\r
60 #include <cctype>\r
61 #include <io.h>\r
62 \r
63 #include <boost/date_time/posix_time/posix_time.hpp>\r
64 #include <boost/lexical_cast.hpp>\r
65 #include <boost/algorithm/string.hpp>\r
66 #include <boost/filesystem.hpp>\r
67 #include <boost/regex.hpp>\r
68 #include <boost/property_tree/xml_parser.hpp>\r
69 \r
70 /* Return codes\r
71 \r
72 100 [action]                    Information om att något har hänt  \r
73 101 [action]                    Information om att något har hänt, en rad data skickas  \r
74 \r
75 202 [kommando] OK               Kommandot har utförts  \r
76 201 [kommando] OK               Kommandot har utförts, och en rad data skickas tillbaka  \r
77 200 [kommando] OK               Kommandot har utförts, och flera rader data skickas tillbaka. Avslutas med tomrad  \r
78 \r
79 400 ERROR                               Kommandot kunde inte förstås  \r
80 401 [kommando] ERROR    Ogiltig kanal  \r
81 402 [kommando] ERROR    Parameter saknas  \r
82 403 [kommando] ERROR    Ogiltig parameter  \r
83 404 [kommando] ERROR    Mediafilen hittades inte  \r
84 \r
85 500 FAILED                              Internt configurationfel  \r
86 501 [kommando] FAILED   Internt configurationfel  \r
87 502 [kommando] FAILED   Oläslig mediafil  \r
88 \r
89 600 [kommando] FAILED   funktion ej implementerad\r
90 */\r
91 \r
92 namespace caspar { namespace protocol {\r
93 \r
94 using namespace core;\r
95 \r
96 std::wstring MediaInfo(const boost::filesystem::wpath& path)\r
97 {\r
98         if(boost::filesystem::is_regular_file(path))\r
99         {\r
100                 std::wstring clipttype = TEXT(" N/A ");\r
101                 std::wstring extension = boost::to_upper_copy(path.extension());\r
102                 if(extension == TEXT(".TGA") || extension == TEXT(".COL") || extension == L".PNG" || extension == L".JPEG" || extension == L".JPG" ||\r
103                         extension == L"GIF" || extension == L"BMP")\r
104                         clipttype = TEXT(" STILL ");\r
105                 else if(extension == TEXT(".WAV") || extension == TEXT(".MP3"))\r
106                         clipttype = TEXT(" STILL ");\r
107                 else if(caspar::ffmpeg::is_valid_file(path.file_string()) || extension == L".CT")\r
108                         clipttype = TEXT(" MOVIE ");\r
109 \r
110                 if(clipttype != TEXT(" N/A "))\r
111                 {               \r
112                         auto is_not_digit = [](char c){ return std::isdigit(c) == 0; };\r
113 \r
114                         auto relativePath = boost::filesystem::wpath(path.file_string().substr(env::media_folder().size()-1, path.file_string().size()));\r
115 \r
116                         auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(path)));\r
117                         writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), is_not_digit), writeTimeStr.end());\r
118                         auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());\r
119 \r
120                         auto sizeStr = boost::lexical_cast<std::wstring>(boost::filesystem::file_size(path));\r
121                         sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), is_not_digit), sizeStr.end());\r
122                         auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());\r
123                                 \r
124                         auto str = relativePath.replace_extension(TEXT("")).external_file_string();\r
125                         if(str[0] == '\\' || str[0] == '/')\r
126                                 str = std::wstring(str.begin() + 1, str.end());\r
127 \r
128                         return std::wstring() + TEXT("\"") + str +\r
129                                         + TEXT("\" ") + clipttype +\r
130                                         + TEXT(" ") + sizeStr +\r
131                                         + TEXT(" ") + writeTimeWStr +\r
132                                         + TEXT("\r\n");         \r
133                 }       \r
134         }\r
135         return L"";\r
136 }\r
137 \r
138 std::wstring ListMedia()\r
139 {       \r
140         std::wstringstream replyString;\r
141         for (boost::filesystem::wrecursive_directory_iterator itr(env::media_folder()), end; itr != end; ++itr) \r
142                 replyString << MediaInfo(itr->path());\r
143         \r
144         return boost::to_upper_copy(replyString.str());\r
145 }\r
146 \r
147 std::wstring ListTemplates() \r
148 {\r
149         std::wstringstream replyString;\r
150 \r
151         for (boost::filesystem::wrecursive_directory_iterator itr(env::template_folder()), end; itr != end; ++itr)\r
152         {               \r
153                 if(boost::filesystem::is_regular_file(itr->path()) && (itr->path().extension() == L".ft" || itr->path().extension() == L".ct"))\r
154                 {\r
155                         auto relativePath = boost::filesystem::wpath(itr->path().file_string().substr(env::template_folder().size()-1, itr->path().file_string().size()));\r
156 \r
157                         auto writeTimeStr = boost::posix_time::to_iso_string(boost::posix_time::from_time_t(boost::filesystem::last_write_time(itr->path())));\r
158                         writeTimeStr.erase(std::remove_if(writeTimeStr.begin(), writeTimeStr.end(), [](char c){ return std::isdigit(c) == 0;}), writeTimeStr.end());\r
159                         auto writeTimeWStr = std::wstring(writeTimeStr.begin(), writeTimeStr.end());\r
160 \r
161                         auto sizeStr = boost::lexical_cast<std::string>(boost::filesystem::file_size(itr->path()));\r
162                         sizeStr.erase(std::remove_if(sizeStr.begin(), sizeStr.end(), [](char c){ return std::isdigit(c) == 0;}), sizeStr.end());\r
163 \r
164                         auto sizeWStr = std::wstring(sizeStr.begin(), sizeStr.end());\r
165 \r
166                         std::wstring dir = relativePath.parent_path().external_directory_string();\r
167                         std::wstring file = boost::to_upper_copy(relativePath.filename());\r
168                         relativePath = boost::filesystem::wpath(dir + L"/" + file);\r
169                                                 \r
170                         auto str = relativePath.replace_extension(TEXT("")).external_file_string();\r
171                         if(str[0] == '\\' || str[0] == '/')\r
172                                 str = std::wstring(str.begin() + 1, str.end());\r
173 \r
174                         replyString << TEXT("\"") << str\r
175                                                 << TEXT("\" ") << sizeWStr\r
176                                                 << TEXT(" ") << writeTimeWStr\r
177                                                 << TEXT("\r\n");                \r
178                 }\r
179         }\r
180         return replyString.str();\r
181 }\r
182 \r
183 namespace amcp {\r
184         \r
185 AMCPCommand::AMCPCommand() : channelIndex_(0), scheduling_(Default), layerIndex_(-1)\r
186 {}\r
187 \r
188 void AMCPCommand::SendReply()\r
189 {\r
190         if(!pClientInfo_) \r
191                 return;\r
192 \r
193         if(replyString_.empty())\r
194                 return;\r
195         pClientInfo_->Send(replyString_);\r
196 }\r
197 \r
198 void AMCPCommand::Clear() \r
199 {\r
200         pChannel_->stage()->clear();\r
201         pClientInfo_.reset();\r
202         channelIndex_ = 0;\r
203         _parameters.clear();\r
204 }\r
205 \r
206 bool DiagnosticsCommand::DoExecute()\r
207 {       \r
208         try\r
209         {\r
210                 diagnostics::show_graphs(true);\r
211 \r
212                 SetReplyString(TEXT("202 DIAG OK\r\n"));\r
213 \r
214                 return true;\r
215         }\r
216         catch(...)\r
217         {\r
218                 CASPAR_LOG_CURRENT_EXCEPTION();\r
219                 SetReplyString(TEXT("502 DIAG FAILED\r\n"));\r
220                 return false;\r
221         }\r
222 }\r
223 \r
224 bool CallCommand::DoExecute()\r
225 {       \r
226         //Perform loading of the clip\r
227         try\r
228         {\r
229                 auto what = _parameters.at(0);\r
230 \r
231                 std::wstring param = _parameters2.at(1);\r
232                 for(auto it = std::begin(_parameters2)+2; it != std::end(_parameters2); ++it)\r
233                         param += L" " + *it;\r
234                 \r
235                 boost::unique_future<std::wstring> result;\r
236                 if(what == L"B")\r
237                         result = GetChannel()->stage()->call(GetLayerIndex(), false, param);\r
238                 else if(what == L"F")\r
239                         result = GetChannel()->stage()->call(GetLayerIndex(), true, param);\r
240                 else\r
241                         result = GetChannel()->stage()->call(GetLayerIndex(), true, _parameters.at(0) + L" " + param);\r
242         \r
243                 if(!result.timed_wait(boost::posix_time::seconds(2)))\r
244                         BOOST_THROW_EXCEPTION(timed_out());\r
245 \r
246                 CASPAR_LOG(info) << "Executed call: " <<  _parameters[0] << TEXT(" successfully");\r
247                 \r
248                 std::wstringstream replyString;\r
249                 if(result.get().empty())\r
250                         replyString << TEXT("202 CALL OK\r\n");\r
251                 else\r
252                         replyString << TEXT("201 CALL OK\r\n") << result.get() << L"\r\n";\r
253                 \r
254                 SetReplyString(replyString.str());\r
255 \r
256                 return true;\r
257         }\r
258         catch(...)\r
259         {\r
260                 CASPAR_LOG_CURRENT_EXCEPTION();\r
261                 SetReplyString(TEXT("502 CALL FAILED\r\n"));\r
262                 return false;\r
263         }\r
264 }\r
265 \r
266 bool MixerCommand::DoExecute()\r
267 {       \r
268         //Perform loading of the clip\r
269         try\r
270         {       \r
271                 if(_parameters[0] == L"KEYER" || _parameters[0] == L"IS_KEY")\r
272                 {\r
273                         bool value = lexical_cast_or_default(_parameters.at(1), false);\r
274                         auto transform = [=](frame_transform transform) -> frame_transform\r
275                         {\r
276                                 transform.is_key = value;\r
277                                 return transform;                                       \r
278                         };\r
279 \r
280                         int layer = GetLayerIndex();\r
281                         GetChannel()->stage()->apply_frame_transform(GetLayerIndex(), transform);\r
282                 }\r
283                 else if(_parameters[0] == L"OPACITY")\r
284                 {\r
285                         int duration = _parameters.size() > 2 ? lexical_cast_or_default(_parameters[2], 0) : 0;\r
286                         std::wstring tween = _parameters.size() > 3 ? _parameters[3] : L"linear";\r
287 \r
288                         double value = boost::lexical_cast<double>(_parameters.at(1));\r
289                         \r
290                         auto transform = [=](frame_transform transform) -> frame_transform\r
291                         {\r
292                                 transform.opacity = value;\r
293                                 return transform;                                       \r
294                         };\r
295 \r
296                         int layer = GetLayerIndex();\r
297                         GetChannel()->stage()->apply_frame_transform(GetLayerIndex(), transform, duration, tween);\r
298                 }\r
299                 else if(_parameters[0] == L"FILL" || _parameters[0] == L"FILL_RECT")\r
300                 {\r
301                         int duration = _parameters.size() > 5 ? lexical_cast_or_default(_parameters[5], 0) : 0;\r
302                         std::wstring tween = _parameters.size() > 6 ? _parameters[6] : L"linear";\r
303                         double x        = boost::lexical_cast<double>(_parameters.at(1));\r
304                         double y        = boost::lexical_cast<double>(_parameters.at(2));\r
305                         double x_s      = boost::lexical_cast<double>(_parameters.at(3));\r
306                         double y_s      = boost::lexical_cast<double>(_parameters.at(4));\r
307 \r
308                         auto transform = [=](frame_transform transform) mutable -> frame_transform\r
309                         {\r
310                                 transform.fill_translation[0]   = x;\r
311                                 transform.fill_translation[1]   = y;\r
312                                 transform.fill_scale[0]                 = x_s;\r
313                                 transform.fill_scale[1]                 = y_s;\r
314                                 transform.clip_translation[0]   = x;\r
315                                 transform.clip_translation[1]   = y;\r
316                                 transform.clip_scale[0]                 = x_s;\r
317                                 transform.clip_scale[1]                 = y_s;\r
318                                 return transform;\r
319                         };\r
320                                 \r
321                         int layer = GetLayerIndex();\r
322                         GetChannel()->stage()->apply_frame_transform(GetLayerIndex(), transform, duration, tween);\r
323                 }\r
324                 else if(_parameters[0] == L"CLIP" || _parameters[0] == L"CLIP_RECT")\r
325                 {\r
326                         int duration = _parameters.size() > 5 ? lexical_cast_or_default(_parameters[5], 0) : 0;\r
327                         std::wstring tween = _parameters.size() > 6 ? _parameters[6] : L"linear";\r
328                         double x        = boost::lexical_cast<double>(_parameters.at(1));\r
329                         double y        = boost::lexical_cast<double>(_parameters.at(2));\r
330                         double x_s      = boost::lexical_cast<double>(_parameters.at(3));\r
331                         double y_s      = boost::lexical_cast<double>(_parameters.at(4));\r
332 \r
333                         auto transform = [=](frame_transform transform) -> frame_transform\r
334                         {\r
335                                 transform.clip_translation[0]   = x;\r
336                                 transform.clip_translation[1]   = y;\r
337                                 transform.clip_scale[0]                 = x_s;\r
338                                 transform.clip_scale[1]                 = y_s;\r
339                                 return transform;\r
340                         };\r
341                                 \r
342                         int layer = GetLayerIndex();\r
343                         GetChannel()->stage()->apply_frame_transform(GetLayerIndex(), transform, duration, tween);\r
344                 }\r
345                 else if(_parameters[0] == L"GRID")\r
346                 {\r
347                         int duration = _parameters.size() > 2 ? lexical_cast_or_default(_parameters[2], 0) : 0;\r
348                         std::wstring tween = _parameters.size() > 3 ? _parameters[3] : L"linear";\r
349                         int n = boost::lexical_cast<int>(_parameters.at(1));\r
350                         double delta = 1.0/static_cast<double>(n);\r
351                         for(int x = 0; x < n; ++x)\r
352                         {\r
353                                 for(int y = 0; y < n; ++y)\r
354                                 {\r
355                                         int index = x+y*n+1;\r
356                                         auto transform = [=](frame_transform transform) -> frame_transform\r
357                                         {               \r
358                                                 transform.fill_translation[0]   = x*delta;\r
359                                                 transform.fill_translation[1]   = y*delta;\r
360                                                 transform.fill_scale[0]                 = delta;\r
361                                                 transform.fill_scale[1]                 = delta;\r
362                                                 transform.clip_translation[0]   = x*delta;\r
363                                                 transform.clip_translation[1]   = y*delta;\r
364                                                 transform.clip_scale[0]                 = delta;\r
365                                                 transform.clip_scale[1]                 = delta;                        \r
366                                                 return transform;\r
367                                         };\r
368                                         GetChannel()->stage()->apply_frame_transform(index, transform, duration, tween);\r
369                                 }\r
370                         }\r
371                 }\r
372                 else if(_parameters[0] == L"BLEND")\r
373                 {\r
374                         auto blend_str = _parameters.at(1);                                                             \r
375                         int layer = GetLayerIndex();\r
376                         GetChannel()->mixer()->set_blend_mode(GetLayerIndex(), get_blend_mode(blend_str));      \r
377                 }\r
378                 else if(_parameters[0] == L"BRIGHTNESS")\r
379                 {\r
380                         auto value = boost::lexical_cast<double>(_parameters.at(1));\r
381                         int duration = _parameters.size() > 2 ? lexical_cast_or_default(_parameters[2], 0) : 0;\r
382                         std::wstring tween = _parameters.size() > 3 ? _parameters[3] : L"linear";\r
383                         auto transform = [=](frame_transform transform) -> frame_transform\r
384                         {\r
385                                 transform.brightness = value;\r
386                                 return transform;\r
387                         };\r
388                                 \r
389                         int layer = GetLayerIndex();\r
390                         GetChannel()->stage()->apply_frame_transform(GetLayerIndex(), transform, duration, tween);      \r
391                 }\r
392                 else if(_parameters[0] == L"SATURATION")\r
393                 {\r
394                         auto value = boost::lexical_cast<double>(_parameters.at(1));\r
395                         int duration = _parameters.size() > 2 ? lexical_cast_or_default(_parameters[2], 0) : 0;\r
396                         std::wstring tween = _parameters.size() > 3 ? _parameters[3] : L"linear";\r
397                         auto transform = [=](frame_transform transform) -> frame_transform\r
398                         {\r
399                                 transform.saturation = value;\r
400                                 return transform;\r
401                         };\r
402                                 \r
403                         int layer = GetLayerIndex();\r
404                         GetChannel()->stage()->apply_frame_transform(GetLayerIndex(), transform, duration, tween);      \r
405                 }\r
406                 else if(_parameters[0] == L"CONTRAST")\r
407                 {\r
408                         auto value = boost::lexical_cast<double>(_parameters.at(1));\r
409                         int duration = _parameters.size() > 2 ? lexical_cast_or_default(_parameters[2], 0) : 0;\r
410                         std::wstring tween = _parameters.size() > 3 ? _parameters[3] : L"linear";\r
411                         auto transform = [=](frame_transform transform) -> frame_transform\r
412                         {\r
413                                 transform.contrast = value;\r
414                                 return transform;\r
415                         };\r
416                                 \r
417                         int layer = GetLayerIndex();\r
418                         GetChannel()->stage()->apply_frame_transform(GetLayerIndex(), transform, duration, tween);      \r
419                 }\r
420                 else if(_parameters[0] == L"LEVELS")\r
421                 {\r
422                         levels value;\r
423                         value.min_input  = boost::lexical_cast<double>(_parameters.at(1));\r
424                         value.max_input  = boost::lexical_cast<double>(_parameters.at(2));\r
425                         value.gamma              = boost::lexical_cast<double>(_parameters.at(3));\r
426                         value.min_output = boost::lexical_cast<double>(_parameters.at(4));\r
427                         value.max_output = boost::lexical_cast<double>(_parameters.at(5));\r
428                         int duration = _parameters.size() > 6 ? lexical_cast_or_default(_parameters[6], 0) : 0;\r
429                         std::wstring tween = _parameters.size() > 7 ? _parameters[7] : L"linear";\r
430 \r
431                         auto transform = [=](frame_transform transform) -> frame_transform\r
432                         {\r
433                                 transform.levels = value;\r
434                                 return transform;\r
435                         };\r
436                                 \r
437                         int layer = GetLayerIndex();\r
438                         GetChannel()->stage()->apply_frame_transform(GetLayerIndex(), transform, duration, tween);      \r
439                 }\r
440                 else if(_parameters[0] == L"VOLUME")\r
441                 {\r
442                         int duration = _parameters.size() > 2 ? lexical_cast_or_default(_parameters[2], 0) : 0;\r
443                         std::wstring tween = _parameters.size() > 3 ? _parameters[3] : L"linear";\r
444                         double value = boost::lexical_cast<double>(_parameters[1]);\r
445 \r
446                         auto transform = [=](frame_transform transform) -> frame_transform\r
447                         {\r
448                                 transform.volume = value;\r
449                                 return transform;\r
450                         };\r
451                                 \r
452                         int layer = GetLayerIndex();\r
453                         GetChannel()->stage()->apply_frame_transform(GetLayerIndex(), transform, duration, tween);\r
454                 }\r
455                 else if(_parameters[0] == L"CLEAR")\r
456                 {\r
457                         int layer = GetLayerIndex(std::numeric_limits<int>::max());\r
458                         if(layer ==     std::numeric_limits<int>::max())\r
459                                 GetChannel()->stage()->clear_transforms();\r
460                         else\r
461                                 GetChannel()->stage()->clear_transforms(layer);\r
462                 }\r
463                 else\r
464                 {\r
465                         SetReplyString(TEXT("404 MIXER ERROR\r\n"));\r
466                         return false;\r
467                 }\r
468         \r
469                 SetReplyString(TEXT("202 MIXER OK\r\n"));\r
470 \r
471                 return true;\r
472         }\r
473         catch(file_not_found&)\r
474         {\r
475                 CASPAR_LOG_CURRENT_EXCEPTION();\r
476                 SetReplyString(TEXT("404 MIXER ERROR\r\n"));\r
477                 return false;\r
478         }\r
479         catch(...)\r
480         {\r
481                 CASPAR_LOG_CURRENT_EXCEPTION();\r
482                 SetReplyString(TEXT("502 MIXER FAILED\r\n"));\r
483                 return false;\r
484         }\r
485 }\r
486 \r
487 bool SwapCommand::DoExecute()\r
488 {       \r
489         //Perform loading of the clip\r
490         try\r
491         {\r
492                 if(GetLayerIndex(-1) != -1)\r
493                 {\r
494                         std::vector<std::string> strs;\r
495                         boost::split(strs, _parameters[0], boost::is_any_of("-"));\r
496                         \r
497                         auto ch1 = GetChannel();\r
498                         auto ch2 = GetChannels().at(boost::lexical_cast<int>(strs.at(0))-1);\r
499 \r
500                         int l1 = GetLayerIndex();\r
501                         int l2 = boost::lexical_cast<int>(strs.at(1));\r
502 \r
503                         ch1->stage()->swap_layer(l1, l2, ch2->stage());\r
504                 }\r
505                 else\r
506                 {\r
507                         auto ch1 = GetChannel();\r
508                         auto ch2 = GetChannels().at(boost::lexical_cast<int>(_parameters[0])-1);\r
509                         ch1->stage()->swap_layers(ch2->stage());\r
510                 }\r
511 \r
512                 CASPAR_LOG(info) << "Swapped successfully";\r
513 \r
514                 SetReplyString(TEXT("202 SWAP OK\r\n"));\r
515 \r
516                 return true;\r
517         }\r
518         catch(file_not_found&)\r
519         {\r
520                 CASPAR_LOG_CURRENT_EXCEPTION();\r
521                 SetReplyString(TEXT("404 SWAP ERROR\r\n"));\r
522                 return false;\r
523         }\r
524         catch(...)\r
525         {\r
526                 CASPAR_LOG_CURRENT_EXCEPTION();\r
527                 SetReplyString(TEXT("502 SWAP FAILED\r\n"));\r
528                 return false;\r
529         }\r
530 }\r
531 \r
532 bool AddCommand::DoExecute()\r
533 {       \r
534         //Perform loading of the clip\r
535         try\r
536         {\r
537                 auto consumer = create_consumer(_parameters);\r
538                 GetChannel()->output()->add(GetLayerIndex(consumer->index()), consumer);\r
539         \r
540                 CASPAR_LOG(info) << "Added " <<  _parameters[0] << TEXT(" successfully");\r
541 \r
542                 SetReplyString(TEXT("202 ADD OK\r\n"));\r
543 \r
544                 return true;\r
545         }\r
546         catch(file_not_found&)\r
547         {\r
548                 CASPAR_LOG_CURRENT_EXCEPTION();\r
549                 SetReplyString(TEXT("404 ADD ERROR\r\n"));\r
550                 return false;\r
551         }\r
552         catch(...)\r
553         {\r
554                 CASPAR_LOG_CURRENT_EXCEPTION();\r
555                 SetReplyString(TEXT("502 ADD FAILED\r\n"));\r
556                 return false;\r
557         }\r
558 }\r
559 \r
560 bool RemoveCommand::DoExecute()\r
561 {       \r
562         //Perform loading of the clip\r
563         try\r
564         {\r
565                 auto index = GetLayerIndex(std::numeric_limits<int>::min());\r
566                 if(index == std::numeric_limits<int>::min())\r
567                         index = create_consumer(_parameters)->index();\r
568 \r
569                 GetChannel()->output()->remove(index);\r
570 \r
571                 SetReplyString(TEXT("202 REMOVE OK\r\n"));\r
572 \r
573                 return true;\r
574         }\r
575         catch(file_not_found&)\r
576         {\r
577                 CASPAR_LOG_CURRENT_EXCEPTION();\r
578                 SetReplyString(TEXT("404 REMOVE ERROR\r\n"));\r
579                 return false;\r
580         }\r
581         catch(...)\r
582         {\r
583                 CASPAR_LOG_CURRENT_EXCEPTION();\r
584                 SetReplyString(TEXT("502 REMOVE FAILED\r\n"));\r
585                 return false;\r
586         }\r
587 }\r
588 \r
589 bool LoadCommand::DoExecute()\r
590 {       \r
591         //Perform loading of the clip\r
592         try\r
593         {\r
594                 _parameters[0] = _parameters[0];\r
595                 auto pFP = create_producer(GetChannel()->mixer(), _parameters);         \r
596                 GetChannel()->stage()->load(GetLayerIndex(), pFP, true);\r
597         \r
598                 CASPAR_LOG(info) << "Loaded " <<  _parameters[0] << TEXT(" successfully");\r
599 \r
600                 SetReplyString(TEXT("202 LOAD OK\r\n"));\r
601 \r
602                 return true;\r
603         }\r
604         catch(file_not_found&)\r
605         {\r
606                 CASPAR_LOG_CURRENT_EXCEPTION();\r
607                 SetReplyString(TEXT("404 LOAD ERROR\r\n"));\r
608                 return false;\r
609         }\r
610         catch(...)\r
611         {\r
612                 CASPAR_LOG_CURRENT_EXCEPTION();\r
613                 SetReplyString(TEXT("502 LOAD FAILED\r\n"));\r
614                 return false;\r
615         }\r
616 }\r
617 \r
618 \r
619 \r
620 //std::function<std::wstring()> channel_cg_add_command::parse(const std::wstring& message, const std::vector<renderer::render_device_ptr>& channels)\r
621 //{\r
622 //      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>.*)?");\r
623 //\r
624 //      boost::wsmatch what;\r
625 //      if(!boost::regex_match(message, what, expr))\r
626 //              return nullptr;\r
627 //\r
628 //      auto info = channel_info::parse(what, channels);\r
629 //\r
630 //      int flash_layer_index = boost::lexical_cast<int>(what["FLASH_LAYER"].str());\r
631 //\r
632 //      std::wstring templatename = what["TEMPLATE"].str();\r
633 //      bool play_on_load = what["PLAY_ON_LOAD"].matched ? what["PLAY_ON_LOAD"].str() != L"0" : 0;\r
634 //      std::wstring start_label = what["START_LABEL"].str();   \r
635 //      std::wstring data = get_data(what["DATA"].str());\r
636 //      \r
637 //      boost::replace_all(templatename, "\"", "");\r
638 //\r
639 //      return [=]() -> std::wstring\r
640 //      {       \r
641 //              std::wstring fullFilename = flash::flash_producer::find_template(server::template_folder() + templatename);\r
642 //              if(fullFilename.empty())\r
643 //                      BOOST_THROW_EXCEPTION(file_not_found());\r
644 //      \r
645 //              std::wstring extension = boost::filesystem::wpath(fullFilename).extension();\r
646 //              std::wstring filename = templatename;\r
647 //              filename.append(extension);\r
648 //\r
649 //              flash::flash::get_default_cg_producer(info.video_channel, std::max<int>(DEFAULT_CHANNEL_LAYER+1, info.layer_index))\r
650 //                      ->add(flash_layer_index, filename, play_on_load, start_label, data);\r
651 //\r
652 //              CASPAR_LOG(info) << L"Executed [amcp_channel_cg_add]";\r
653 //              return L"";\r
654 //      };\r
655 \r
656 bool LoadbgCommand::DoExecute()\r
657 {\r
658         transition_info transitionInfo;\r
659         \r
660         bool bLoop = false;\r
661 \r
662         // TRANSITION\r
663 \r
664         std::wstring message;\r
665         for(size_t n = 0; n < _parameters.size(); ++n)\r
666                 message += _parameters[n] + L" ";\r
667                 \r
668         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)?.*");\r
669         boost::wsmatch what;\r
670         if(boost::regex_match(message, what, expr))\r
671         {\r
672                 auto transition = what["TRANSITION"].str();\r
673                 transitionInfo.duration = lexical_cast_or_default<size_t>(what["DURATION"].str());\r
674                 auto direction = what["DIRECTION"].matched ? what["DIRECTION"].str() : L"";\r
675                 auto tween = what["TWEEN"].matched ? what["TWEEN"].str() : L"";\r
676                 transitionInfo.tweener = get_tweener(tween);            \r
677 \r
678                 if(transition == TEXT("CUT"))\r
679                         transitionInfo.type = transition::cut;\r
680                 else if(transition == TEXT("MIX"))\r
681                         transitionInfo.type = transition::mix;\r
682                 else if(transition == TEXT("PUSH"))\r
683                         transitionInfo.type = transition::push;\r
684                 else if(transition == TEXT("SLIDE"))\r
685                         transitionInfo.type = transition::slide;\r
686                 else if(transition == TEXT("WIPE"))\r
687                         transitionInfo.type = transition::wipe;\r
688                 \r
689                 if(direction == TEXT("FROMLEFT"))\r
690                         transitionInfo.direction = transition_direction::from_left;\r
691                 else if(direction == TEXT("FROMRIGHT"))\r
692                         transitionInfo.direction = transition_direction::from_right;\r
693                 else if(direction == TEXT("LEFT"))\r
694                         transitionInfo.direction = transition_direction::from_right;\r
695                 else if(direction == TEXT("RIGHT"))\r
696                         transitionInfo.direction = transition_direction::from_left;\r
697         }\r
698         \r
699         //Perform loading of the clip\r
700         try\r
701         {\r
702                 _parameters[0] = _parameters[0];\r
703                 auto pFP = create_producer(GetChannel()->mixer(), _parameters);\r
704                 if(pFP == frame_producer::empty())\r
705                         BOOST_THROW_EXCEPTION(file_not_found() << msg_info(_parameters.size() > 0 ? u8(_parameters[0]) : ""));\r
706 \r
707                 bool auto_play = std::find(_parameters.begin(), _parameters.end(), L"AUTO") != _parameters.end();\r
708 \r
709                 auto pFP2 = create_transition_producer(GetChannel()->get_video_format_desc().field_mode, pFP, transitionInfo);\r
710                 GetChannel()->stage()->load(GetLayerIndex(), pFP2, false, auto_play ? transitionInfo.duration : -1); // TODO: LOOP\r
711         \r
712                 CASPAR_LOG(info) << "Loaded " << _parameters[0] << TEXT(" successfully to background");\r
713                 SetReplyString(TEXT("202 LOADBG OK\r\n"));\r
714 \r
715                 return true;\r
716         }\r
717         catch(file_not_found&)\r
718         {\r
719                 CASPAR_LOG_CURRENT_EXCEPTION();\r
720                 SetReplyString(TEXT("404 LOADBG ERROR\r\n"));\r
721                 return false;\r
722         }\r
723         catch(...)\r
724         {\r
725                 CASPAR_LOG_CURRENT_EXCEPTION();\r
726                 SetReplyString(TEXT("502 LOADBG FAILED\r\n"));\r
727                 return false;\r
728         }\r
729 }\r
730 \r
731 bool PauseCommand::DoExecute()\r
732 {\r
733         try\r
734         {\r
735                 GetChannel()->stage()->pause(GetLayerIndex());\r
736                 SetReplyString(TEXT("202 PAUSE OK\r\n"));\r
737                 return true;\r
738         }\r
739         catch(...)\r
740         {\r
741                 SetReplyString(TEXT("501 PAUSE FAILED\r\n"));\r
742         }\r
743 \r
744         return false;\r
745 }\r
746 \r
747 bool PlayCommand::DoExecute()\r
748 {\r
749         try\r
750         {\r
751                 if(!_parameters.empty())\r
752                 {\r
753                         LoadbgCommand lbg;\r
754                         lbg.SetChannel(GetChannel());\r
755                         lbg.SetChannelIndex(GetChannelIndex());\r
756                         lbg.SetLayerIntex(GetLayerIndex());\r
757                         lbg.SetClientInfo(GetClientInfo());\r
758                         for(auto it = _parameters.begin(); it != _parameters.end(); ++it)\r
759                                 lbg.AddParameter(*it);\r
760                         if(!lbg.Execute())\r
761                                 CASPAR_LOG(warning) << " Failed to play.";\r
762 \r
763                         CASPAR_LOG(info) << "Playing " << _parameters[0];\r
764                 }\r
765 \r
766                 GetChannel()->stage()->play(GetLayerIndex());\r
767                 \r
768                 SetReplyString(TEXT("202 PLAY OK\r\n"));\r
769                 return true;\r
770         }\r
771         catch(...)\r
772         {\r
773                 SetReplyString(TEXT("501 PLAY FAILED\r\n"));\r
774         }\r
775 \r
776         return false;\r
777 }\r
778 \r
779 bool StopCommand::DoExecute()\r
780 {\r
781         try\r
782         {\r
783                 GetChannel()->stage()->stop(GetLayerIndex());\r
784                 SetReplyString(TEXT("202 STOP OK\r\n"));\r
785                 return true;\r
786         }\r
787         catch(...)\r
788         {\r
789                 SetReplyString(TEXT("501 STOP FAILED\r\n"));\r
790         }\r
791 \r
792         return false;\r
793 }\r
794 \r
795 bool ClearCommand::DoExecute()\r
796 {\r
797         int index = GetLayerIndex(std::numeric_limits<int>::min());\r
798         if(index != std::numeric_limits<int>::min())\r
799                 GetChannel()->stage()->clear(index);\r
800         else\r
801                 GetChannel()->stage()->clear();\r
802                 \r
803         SetReplyString(TEXT("202 CLEAR OK\r\n"));\r
804 \r
805         return true;\r
806 }\r
807 \r
808 bool PrintCommand::DoExecute()\r
809 {\r
810         GetChannel()->output()->add(create_consumer(boost::assign::list_of(L"IMAGE")));\r
811                 \r
812         SetReplyString(TEXT("202 PRINT OK\r\n"));\r
813 \r
814         return true;\r
815 }\r
816 \r
817 bool LogCommand::DoExecute()\r
818 {\r
819         if(_parameters.at(0) == L"LEVEL")\r
820                 log::set_log_level(_parameters.at(1));\r
821 \r
822         SetReplyString(TEXT("202 LOG OK\r\n"));\r
823 \r
824         return true;\r
825 }\r
826 \r
827 bool CGCommand::DoExecute()\r
828 {\r
829         std::wstring command = _parameters[0];\r
830         if(command == TEXT("ADD"))\r
831                 return DoExecuteAdd();\r
832         else if(command == TEXT("PLAY"))\r
833                 return DoExecutePlay();\r
834         else if(command == TEXT("STOP"))\r
835                 return DoExecuteStop();\r
836         else if(command == TEXT("NEXT"))\r
837                 return DoExecuteNext();\r
838         else if(command == TEXT("REMOVE"))\r
839                 return DoExecuteRemove();\r
840         else if(command == TEXT("CLEAR"))\r
841                 return DoExecuteClear();\r
842         else if(command == TEXT("UPDATE"))\r
843                 return DoExecuteUpdate();\r
844         else if(command == TEXT("INVOKE"))\r
845                 return DoExecuteInvoke();\r
846         else if(command == TEXT("INFO"))\r
847                 return DoExecuteInfo();\r
848 \r
849         SetReplyString(TEXT("403 CG ERROR\r\n"));\r
850         return false;\r
851 }\r
852 \r
853 bool CGCommand::ValidateLayer(const std::wstring& layerstring) {\r
854         int length = layerstring.length();\r
855         for(int i = 0; i < length; ++i) {\r
856                 if(!_istdigit(layerstring[i])) {\r
857                         return false;\r
858                 }\r
859         }\r
860 \r
861         return true;\r
862 }\r
863 \r
864 bool CGCommand::DoExecuteAdd() {\r
865         //CG 1 ADD 0 "template_folder/templatename" [STARTLABEL] 0/1 [DATA]\r
866 \r
867         int layer = 0;                          //_parameters[1]\r
868 //      std::wstring templateName;      //_parameters[2]\r
869         std::wstring label;             //_parameters[3]\r
870         bool bDoStart = false;          //_parameters[3] alt. _parameters[4]\r
871 //      std::wstring data;                      //_parameters[4] alt. _parameters[5]\r
872 \r
873         if(_parameters.size() < 4) \r
874         {\r
875                 SetReplyString(TEXT("402 CG ERROR\r\n"));\r
876                 return false;\r
877         }\r
878         unsigned int dataIndex = 4;\r
879 \r
880         if(!ValidateLayer(_parameters[1])) \r
881         {\r
882                 SetReplyString(TEXT("403 CG ERROR\r\n"));\r
883                 return false;\r
884         }\r
885 \r
886         layer = _ttoi(_parameters[1].c_str());\r
887 \r
888         if(_parameters[3].length() > 1) \r
889         {       //read label\r
890                 label = _parameters2[3];\r
891                 ++dataIndex;\r
892 \r
893                 if(_parameters.size() > 4 && _parameters[4].length() > 0)       //read play-on-load-flag\r
894                         bDoStart = (_parameters[4][0]==TEXT('1')) ? true : false;\r
895                 else \r
896                 {\r
897                         SetReplyString(TEXT("402 CG ERROR\r\n"));\r
898                         return false;\r
899                 }\r
900         }\r
901         else if(_parameters[3].length() > 0) {  //read play-on-load-flag\r
902                 bDoStart = (_parameters[3][0]==TEXT('1')) ? true : false;\r
903         }\r
904         else \r
905         {\r
906                 SetReplyString(TEXT("403 CG ERROR\r\n"));\r
907                 return false;\r
908         }\r
909 \r
910         const TCHAR* pDataString = 0;\r
911         std::wstringstream data;\r
912         std::wstring dataFromFile;\r
913         if(_parameters.size() > dataIndex) \r
914         {       //read data\r
915                 const std::wstring& dataString = _parameters2[dataIndex];\r
916 \r
917                 if(dataString[0] == TEXT('<')) //the data is an XML-string\r
918                         pDataString = dataString.c_str();\r
919                 else \r
920                 {\r
921                         //The data is not an XML-string, it must be a filename\r
922                         std::wstring filename = env::data_folder();\r
923                         filename.append(dataString);\r
924                         filename.append(TEXT(".ftd"));\r
925 \r
926                         //open file\r
927                         std::wifstream datafile(filename.c_str());\r
928                         if(datafile) \r
929                         {\r
930                                 //read all data\r
931                                 data << datafile.rdbuf();\r
932                                 datafile.close();\r
933 \r
934                                 //extract data to _parameters\r
935                                 dataFromFile = data.str();\r
936                                 pDataString = dataFromFile.c_str();\r
937                         }\r
938                 }\r
939         }\r
940 \r
941         std::wstring fullFilename = flash::find_template(env::template_folder() + _parameters[2]);\r
942         if(!fullFilename.empty())\r
943         {\r
944                 std::wstring extension = boost::filesystem::wpath(fullFilename).extension();\r
945                 std::wstring filename = _parameters[2];\r
946                 filename.append(extension);\r
947 \r
948                 flash::get_default_cg_producer(safe_ptr<core::video_channel>(GetChannel()), GetLayerIndex(flash::cg_producer::DEFAULT_LAYER))->add(layer, filename, bDoStart, label, (pDataString!=0) ? pDataString : TEXT(""));\r
949                 SetReplyString(TEXT("202 CG OK\r\n"));\r
950         }\r
951         else\r
952         {\r
953                 CASPAR_LOG(warning) << "Could not find template " << _parameters[2];\r
954                 SetReplyString(TEXT("404 CG ERROR\r\n"));\r
955         }\r
956         return true;\r
957 }\r
958 \r
959 bool CGCommand::DoExecutePlay()\r
960 {\r
961         if(_parameters.size() > 1)\r
962         {\r
963                 if(!ValidateLayer(_parameters[1])) \r
964                 {\r
965                         SetReplyString(TEXT("403 CG ERROR\r\n"));\r
966                         return false;\r
967                 }\r
968                 int layer = _ttoi(_parameters[1].c_str());\r
969                 flash::get_default_cg_producer(safe_ptr<core::video_channel>(GetChannel()), GetLayerIndex(flash::cg_producer::DEFAULT_LAYER))->play(layer);\r
970         }\r
971         else\r
972         {\r
973                 SetReplyString(TEXT("402 CG ERROR\r\n"));\r
974                 return true;\r
975         }\r
976 \r
977         SetReplyString(TEXT("202 CG OK\r\n"));\r
978         return true;\r
979 }\r
980 \r
981 bool CGCommand::DoExecuteStop() \r
982 {\r
983         if(_parameters.size() > 1)\r
984         {\r
985                 if(!ValidateLayer(_parameters[1])) \r
986                 {\r
987                         SetReplyString(TEXT("403 CG ERROR\r\n"));\r
988                         return false;\r
989                 }\r
990                 int layer = _ttoi(_parameters[1].c_str());\r
991                 flash::get_default_cg_producer(safe_ptr<core::video_channel>(GetChannel()), GetLayerIndex(flash::cg_producer::DEFAULT_LAYER))->stop(layer, 0);\r
992         }\r
993         else \r
994         {\r
995                 SetReplyString(TEXT("402 CG ERROR\r\n"));\r
996                 return true;\r
997         }\r
998 \r
999         SetReplyString(TEXT("202 CG OK\r\n"));\r
1000         return true;\r
1001 }\r
1002 \r
1003 bool CGCommand::DoExecuteNext()\r
1004 {\r
1005         if(_parameters.size() > 1) \r
1006         {\r
1007                 if(!ValidateLayer(_parameters[1])) \r
1008                 {\r
1009                         SetReplyString(TEXT("403 CG ERROR\r\n"));\r
1010                         return false;\r
1011                 }\r
1012 \r
1013                 int layer = _ttoi(_parameters[1].c_str());\r
1014                 flash::get_default_cg_producer(safe_ptr<core::video_channel>(GetChannel()), GetLayerIndex(flash::cg_producer::DEFAULT_LAYER))->next(layer);\r
1015         }\r
1016         else \r
1017         {\r
1018                 SetReplyString(TEXT("402 CG ERROR\r\n"));\r
1019                 return true;\r
1020         }\r
1021 \r
1022         SetReplyString(TEXT("202 CG OK\r\n"));\r
1023         return true;\r
1024 }\r
1025 \r
1026 bool CGCommand::DoExecuteRemove() \r
1027 {\r
1028         if(_parameters.size() > 1) \r
1029         {\r
1030                 if(!ValidateLayer(_parameters[1])) \r
1031                 {\r
1032                         SetReplyString(TEXT("403 CG ERROR\r\n"));\r
1033                         return false;\r
1034                 }\r
1035 \r
1036                 int layer = _ttoi(_parameters[1].c_str());\r
1037                 flash::get_default_cg_producer(safe_ptr<core::video_channel>(GetChannel()), GetLayerIndex(flash::cg_producer::DEFAULT_LAYER))->remove(layer);\r
1038         }\r
1039         else \r
1040         {\r
1041                 SetReplyString(TEXT("402 CG ERROR\r\n"));\r
1042                 return true;\r
1043         }\r
1044 \r
1045         SetReplyString(TEXT("202 CG OK\r\n"));\r
1046         return true;\r
1047 }\r
1048 \r
1049 bool CGCommand::DoExecuteClear() \r
1050 {\r
1051         GetChannel()->stage()->clear(GetLayerIndex(flash::cg_producer::DEFAULT_LAYER));\r
1052         SetReplyString(TEXT("202 CG OK\r\n"));\r
1053         return true;\r
1054 }\r
1055 \r
1056 bool CGCommand::DoExecuteUpdate() \r
1057 {\r
1058         try\r
1059         {\r
1060                 if(!ValidateLayer(_parameters.at(1)))\r
1061                 {\r
1062                         SetReplyString(TEXT("403 CG ERROR\r\n"));\r
1063                         return false;\r
1064                 }\r
1065                                                 \r
1066                 std::wstring dataString = _parameters2.at(2);                           \r
1067                 if(dataString.at(0) != TEXT('<'))\r
1068                 {\r
1069                         //The data is not an XML-string, it must be a filename\r
1070                         std::wstring filename = env::data_folder();\r
1071                         filename.append(dataString);\r
1072                         filename.append(TEXT(".ftd"));\r
1073 \r
1074                         //open file\r
1075                         std::wifstream datafile(filename.c_str());\r
1076                         if(datafile) \r
1077                         {\r
1078                                 std::wstringstream data;\r
1079                                 //read all data\r
1080                                 data << datafile.rdbuf();\r
1081                                 datafile.close();\r
1082 \r
1083                                 //extract data to _parameters\r
1084                                 dataString = data.str();\r
1085                         }\r
1086                 }               \r
1087 \r
1088                 int layer = _ttoi(_parameters.at(1).c_str());\r
1089                 flash::get_default_cg_producer(safe_ptr<core::video_channel>(GetChannel()), GetLayerIndex(flash::cg_producer::DEFAULT_LAYER))->update(layer, dataString);\r
1090         }\r
1091         catch(...)\r
1092         {\r
1093                 SetReplyString(TEXT("402 CG ERROR\r\n"));\r
1094                 return true;\r
1095         }\r
1096 \r
1097         SetReplyString(TEXT("202 CG OK\r\n"));\r
1098         return true;\r
1099 }\r
1100 \r
1101 bool CGCommand::DoExecuteInvoke() \r
1102 {\r
1103         std::wstringstream replyString;\r
1104         replyString << TEXT("201 CG OK\r\n");\r
1105 \r
1106         if(_parameters.size() > 2)\r
1107         {\r
1108                 if(!ValidateLayer(_parameters[1]))\r
1109                 {\r
1110                         SetReplyString(TEXT("403 CG ERROR\r\n"));\r
1111                         return false;\r
1112                 }\r
1113                 int layer = _ttoi(_parameters[1].c_str());\r
1114                 auto result = flash::get_default_cg_producer(safe_ptr<core::video_channel>(GetChannel()), GetLayerIndex(flash::cg_producer::DEFAULT_LAYER))->invoke(layer, _parameters2[2]);\r
1115                 replyString << result << TEXT("\r\n"); \r
1116         }\r
1117         else \r
1118         {\r
1119                 SetReplyString(TEXT("402 CG ERROR\r\n"));\r
1120                 return true;\r
1121         }\r
1122         \r
1123         SetReplyString(replyString.str());\r
1124         return true;\r
1125 }\r
1126 \r
1127 bool CGCommand::DoExecuteInfo() \r
1128 {\r
1129         std::wstringstream replyString;\r
1130         replyString << TEXT("201 CG OK\r\n");\r
1131 \r
1132         if(_parameters.size() > 1)\r
1133         {\r
1134                 if(!ValidateLayer(_parameters[1]))\r
1135                 {\r
1136                         SetReplyString(TEXT("403 CG ERROR\r\n"));\r
1137                         return false;\r
1138                 }\r
1139 \r
1140                 int layer = _ttoi(_parameters[1].c_str());\r
1141                 auto desc = flash::get_default_cg_producer(safe_ptr<core::video_channel>(GetChannel()), GetLayerIndex(flash::cg_producer::DEFAULT_LAYER))->description(layer);\r
1142                 \r
1143                 replyString << desc << TEXT("\r\n"); \r
1144         }\r
1145         else \r
1146         {\r
1147                 auto info = flash::get_default_cg_producer(safe_ptr<core::video_channel>(GetChannel()), GetLayerIndex(flash::cg_producer::DEFAULT_LAYER))->template_host_info();\r
1148                 replyString << info << TEXT("\r\n"); \r
1149         }       \r
1150 \r
1151         SetReplyString(replyString.str());\r
1152         return true;\r
1153 }\r
1154 \r
1155 bool DataCommand::DoExecute()\r
1156 {\r
1157         std::wstring command = _parameters[0];\r
1158         if(command == TEXT("STORE"))\r
1159                 return DoExecuteStore();\r
1160         else if(command == TEXT("RETRIEVE"))\r
1161                 return DoExecuteRetrieve();\r
1162         else if(command == TEXT("LIST"))\r
1163                 return DoExecuteList();\r
1164 \r
1165         SetReplyString(TEXT("403 DATA ERROR\r\n"));\r
1166         return false;\r
1167 }\r
1168 \r
1169 bool DataCommand::DoExecuteStore() \r
1170 {\r
1171         if(_parameters.size() < 3) \r
1172         {\r
1173                 SetReplyString(TEXT("402 DATA STORE ERROR\r\n"));\r
1174                 return false;\r
1175         }\r
1176 \r
1177         std::wstring filename = env::data_folder();\r
1178         filename.append(_parameters[1]);\r
1179         filename.append(TEXT(".ftd"));\r
1180 \r
1181         std::wofstream datafile(filename.c_str());\r
1182         if(!datafile) \r
1183         {\r
1184                 SetReplyString(TEXT("501 DATA STORE FAILED\r\n"));\r
1185                 return false;\r
1186         }\r
1187 \r
1188         datafile << _parameters2[2];\r
1189         datafile.close();\r
1190 \r
1191         std::wstring replyString = TEXT("202 DATA STORE OK\r\n");\r
1192         SetReplyString(replyString);\r
1193         return true;\r
1194 }\r
1195 \r
1196 bool DataCommand::DoExecuteRetrieve() \r
1197 {\r
1198         if(_parameters.size() < 2) \r
1199         {\r
1200                 SetReplyString(TEXT("402 DATA RETRIEVE ERROR\r\n"));\r
1201                 return false;\r
1202         }\r
1203 \r
1204         std::wstring filename = env::data_folder();\r
1205         filename.append(_parameters[1]);\r
1206         filename.append(TEXT(".ftd"));\r
1207 \r
1208         std::wifstream datafile(filename.c_str());\r
1209         if(!datafile) \r
1210         {\r
1211                 SetReplyString(TEXT("404 DATA RETRIEVE ERROR\r\n"));\r
1212                 return false;\r
1213         }\r
1214 \r
1215         std::wstringstream reply(TEXT("201 DATA RETRIEVE OK\r\n"));\r
1216         std::wstring line;\r
1217         bool bFirstLine = true;\r
1218         while(std::getline(datafile, line))\r
1219         {\r
1220                 if(!bFirstLine)\r
1221                         reply << "\\n";\r
1222                 else\r
1223                         bFirstLine = false;\r
1224 \r
1225                 reply << line;\r
1226         }\r
1227         datafile.close();\r
1228 \r
1229         reply << "\r\n";\r
1230         SetReplyString(reply.str());\r
1231         return true;\r
1232 }\r
1233 \r
1234 bool DataCommand::DoExecuteList() \r
1235 {\r
1236         std::wstringstream replyString;\r
1237         replyString << TEXT("200 DATA LIST OK\r\n");\r
1238 \r
1239         for (boost::filesystem::wrecursive_directory_iterator itr(env::data_folder()), end; itr != end; ++itr)\r
1240         {                       \r
1241                 if(boost::filesystem::is_regular_file(itr->path()))\r
1242                 {\r
1243                         if(!boost::iequals(itr->path().extension(), L".ftd"))\r
1244                                 continue;\r
1245                         \r
1246                         auto relativePath = boost::filesystem::wpath(itr->path().file_string().substr(env::data_folder().size()-1, itr->path().file_string().size()));\r
1247                         \r
1248                         auto str = relativePath.replace_extension(TEXT("")).external_file_string();\r
1249                         if(str[0] == '\\' || str[0] == '/')\r
1250                                 str = std::wstring(str.begin() + 1, str.end());\r
1251 \r
1252                         replyString << str << TEXT("\r\n");     \r
1253                 }\r
1254         }\r
1255         \r
1256         replyString << TEXT("\r\n");\r
1257 \r
1258         SetReplyString(boost::to_upper_copy(replyString.str()));\r
1259         return true;\r
1260 }\r
1261 \r
1262 bool CinfCommand::DoExecute()\r
1263 {\r
1264         std::wstringstream replyString;\r
1265         \r
1266         try\r
1267         {\r
1268                 std::wstring info;\r
1269                 for (boost::filesystem::wrecursive_directory_iterator itr(env::media_folder()), end; itr != end; ++itr)\r
1270                 {\r
1271                         auto path = itr->path();\r
1272                         auto file = path.replace_extension(L"").filename();\r
1273                         if(boost::iequals(file, _parameters.at(0)))\r
1274                                 info += MediaInfo(itr->path()) + L"\r\n";\r
1275                 }\r
1276 \r
1277                 if(info.empty())\r
1278                 {\r
1279                         SetReplyString(TEXT("404 CINF ERROR\r\n"));\r
1280                         return false;\r
1281                 }\r
1282                 replyString << TEXT("200 CINF OK\r\n");\r
1283                 replyString << info << "\r\n";\r
1284         }\r
1285         catch(...)\r
1286         {\r
1287                 SetReplyString(TEXT("404 CINF ERROR\r\n"));\r
1288                 return false;\r
1289         }\r
1290         \r
1291         SetReplyString(replyString.str());\r
1292         return true;\r
1293 }\r
1294 \r
1295 void GenerateChannelInfo(int index, const safe_ptr<core::video_channel>& pChannel, std::wstringstream& replyString)\r
1296 {\r
1297         replyString << index+1 << TEXT(" ") << pChannel->get_video_format_desc().name << TEXT(" PLAYING") << TEXT("\r\n");\r
1298 }\r
1299 \r
1300 bool InfoCommand::DoExecute()\r
1301 {\r
1302         std::wstringstream replyString;\r
1303         \r
1304         boost::property_tree::xml_writer_settings<wchar_t> w(' ', 3);\r
1305 \r
1306         try\r
1307         {\r
1308                 if(_parameters.size() >= 1 && _parameters[0] == L"TEMPLATE")\r
1309                 {               \r
1310                         replyString << L"201 INFO TEMPLATE OK\r\n";\r
1311 \r
1312                         // Needs to be extended for any file, not just flash.\r
1313 \r
1314                         auto filename = flash::find_template(env::template_folder() + _parameters.at(1));\r
1315                                                 \r
1316                         std::wstringstream str;\r
1317                         str << u16(flash::read_template_meta_info(filename));\r
1318                         boost::property_tree::wptree info;\r
1319                         boost::property_tree::xml_parser::read_xml(str, info, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments);\r
1320 \r
1321                         boost::property_tree::xml_parser::write_xml(replyString, info, w);\r
1322                 }\r
1323                 else if(_parameters.size() >= 1 && _parameters[0] == L"CONFIG")\r
1324                 {               \r
1325                         replyString << L"201 INFO CONFIG OK\r\n";\r
1326 \r
1327                         boost::property_tree::write_xml(replyString, caspar::env::properties(), w);\r
1328                 }\r
1329                 else if(_parameters.size() >= 1 && _parameters[0] == L"PATHS")\r
1330                 {\r
1331                         replyString << L"201 INFO PATHS OK\r\n";\r
1332 \r
1333                         boost::property_tree::wptree info;\r
1334                         info.add_child(L"paths", caspar::env::properties().get_child(L"configuration.paths"));\r
1335                         info.add(L"paths.initial-path", boost::filesystem2::initial_path<boost::filesystem2::wpath>().directory_string() + L"\\");\r
1336 \r
1337                         boost::property_tree::write_xml(replyString, info, w);\r
1338                 }\r
1339                 else if(_parameters.size() >= 1 && _parameters[0] == L"SYSTEM")\r
1340                 {\r
1341                         replyString << L"201 INFO SYSTEM OK\r\n";\r
1342                         \r
1343                         boost::property_tree::wptree info;\r
1344                         \r
1345                         info.add(L"system.name",                                        caspar::get_system_product_name());\r
1346                         info.add(L"system.windows.name",                        caspar::get_win_product_name());\r
1347                         info.add(L"system.windows.service-pack",        caspar::get_win_sp_version());\r
1348                         info.add(L"system.cpu",                                         caspar::get_cpu_info());\r
1349         \r
1350                         BOOST_FOREACH(auto device, caspar::decklink::get_device_list())\r
1351                                 info.add(L"system.decklink.device", device);\r
1352 \r
1353                         BOOST_FOREACH(auto device, caspar::bluefish::get_device_list())\r
1354                                 info.add(L"system.bluefish.device", device);\r
1355                                 \r
1356                         info.add(L"system.flash",                                       caspar::flash::get_version());\r
1357                         info.add(L"system.free-image",                          caspar::image::get_version());\r
1358                         info.add(L"system.ffmpeg.avcodec",                      caspar::ffmpeg::get_avcodec_version());\r
1359                         info.add(L"system.ffmpeg.avformat",                     caspar::ffmpeg::get_avformat_version());\r
1360                         info.add(L"system.ffmpeg.avfilter",                     caspar::ffmpeg::get_avfilter_version());\r
1361                         info.add(L"system.ffmpeg.avutil",                       caspar::ffmpeg::get_avutil_version());\r
1362                         info.add(L"system.ffmpeg.swscale",                      caspar::ffmpeg::get_swscale_version());\r
1363                                                 \r
1364                         boost::property_tree::write_xml(replyString, info, w);\r
1365                 }\r
1366                 else if(_parameters.size() >= 1 && _parameters[0] == L"SERVER")\r
1367                 {\r
1368                         replyString << L"201 INFO SYSTEM OK\r\n";\r
1369                         \r
1370                         boost::property_tree::wptree info;\r
1371 \r
1372                         int index = 0;\r
1373                         BOOST_FOREACH(auto channel, channels_)\r
1374                                 info.add_child(L"channels.channel", channel->info())\r
1375                                         .add(L"index", ++index);\r
1376                         \r
1377                         boost::property_tree::write_xml(replyString, info, w);\r
1378                 }\r
1379                 else // channel\r
1380                 {                       \r
1381                         if(_parameters.size() >= 1)\r
1382                         {\r
1383                                 replyString << TEXT("201 INFO OK\r\n");\r
1384                                 boost::property_tree::wptree info;\r
1385 \r
1386                                 std::vector<std::wstring> split;\r
1387                                 boost::split(split, _parameters[0], boost::is_any_of("-"));\r
1388                                         \r
1389                                 int layer = std::numeric_limits<int>::min();\r
1390                                 int channel = boost::lexical_cast<int>(split[0]) - 1;\r
1391 \r
1392                                 if(split.size() > 1)\r
1393                                         layer = boost::lexical_cast<int>(split[1]);\r
1394                                 \r
1395                                 if(layer == std::numeric_limits<int>::min())\r
1396                                 {       \r
1397                                         info.add_child(L"channel", channels_.at(channel)->info())\r
1398                                                         .add(L"index", channel);\r
1399                                 }\r
1400                                 else\r
1401                                 {\r
1402                                         if(_parameters.size() >= 2)\r
1403                                         {\r
1404                                                 if(_parameters[1] == L"B")\r
1405                                                         info.add_child(L"producer", channels_.at(channel)->stage()->background(layer).get()->info());\r
1406                                                 else\r
1407                                                         info.add_child(L"producer", channels_.at(channel)->stage()->foreground(layer).get()->info());\r
1408                                         }\r
1409                                         else\r
1410                                         {\r
1411                                                 info.add_child(L"layer", channels_.at(channel)->stage()->info(layer).get())\r
1412                                                         .add(L"index", layer);\r
1413                                         }\r
1414                                 }\r
1415                                 boost::property_tree::xml_parser::write_xml(replyString, info, w);\r
1416                         }\r
1417                         else\r
1418                         {\r
1419                                 // This is needed for backwards compatibility with old clients\r
1420                                 replyString << TEXT("200 INFO OK\r\n");\r
1421                                 for(size_t n = 0; n < channels_.size(); ++n)\r
1422                                         GenerateChannelInfo(n, channels_[n], replyString);\r
1423                         }\r
1424 \r
1425                 }\r
1426         }\r
1427         catch(...)\r
1428         {\r
1429                 SetReplyString(TEXT("403 INFO ERROR\r\n"));\r
1430                 return false;\r
1431         }\r
1432 \r
1433         replyString << TEXT("\r\n");\r
1434         SetReplyString(replyString.str());\r
1435         return true;\r
1436 }\r
1437 \r
1438 bool ClsCommand::DoExecute()\r
1439 {\r
1440 /*\r
1441                 wav = audio\r
1442                 mp3 = audio\r
1443                 swf     = movie\r
1444                 dv  = movie\r
1445                 tga = still\r
1446                 col = still\r
1447         */\r
1448         std::wstringstream replyString;\r
1449         replyString << TEXT("200 CLS OK\r\n");\r
1450         replyString << ListMedia();\r
1451         replyString << TEXT("\r\n");\r
1452         SetReplyString(boost::to_upper_copy(replyString.str()));\r
1453         return true;\r
1454 }\r
1455 \r
1456 bool TlsCommand::DoExecute()\r
1457 {\r
1458         std::wstringstream replyString;\r
1459         replyString << TEXT("200 TLS OK\r\n");\r
1460 \r
1461         replyString << ListTemplates();\r
1462         replyString << TEXT("\r\n");\r
1463 \r
1464         SetReplyString(replyString.str());\r
1465         return true;\r
1466 }\r
1467 \r
1468 bool VersionCommand::DoExecute()\r
1469 {\r
1470         std::wstring replyString = TEXT("201 VERSION OK\r\n") + env::version() + TEXT("\r\n");\r
1471 \r
1472         if(_parameters.size() > 0)\r
1473         {\r
1474                 if(_parameters[0] == L"FLASH")\r
1475                         replyString = TEXT("201 VERSION OK\r\n") + flash::get_version() + TEXT("\r\n");\r
1476                 else if(_parameters[0] == L"TEMPLATEHOST")\r
1477                         replyString = TEXT("201 VERSION OK\r\n") + flash::get_cg_version() + TEXT("\r\n");\r
1478                 else if(_parameters[0] != L"SERVER")\r
1479                         replyString = TEXT("403 VERSION ERROR\r\n");\r
1480         }\r
1481 \r
1482         SetReplyString(replyString);\r
1483         return true;\r
1484 }\r
1485 \r
1486 bool ByeCommand::DoExecute()\r
1487 {\r
1488         GetClientInfo()->Disconnect();\r
1489         return true;\r
1490 }\r
1491 \r
1492 bool SetCommand::DoExecute()\r
1493 {\r
1494         std::wstring name = _parameters[0];\r
1495         std::transform(name.begin(), name.end(), name.begin(), toupper);\r
1496 \r
1497         std::wstring value = _parameters[1];\r
1498         std::transform(value.begin(), value.end(), value.begin(), toupper);\r
1499 \r
1500         if(name == TEXT("MODE"))\r
1501         {\r
1502                 auto format_desc = core::video_format_desc::get(value);\r
1503                 if(format_desc.format != core::video_format::invalid)\r
1504                 {\r
1505                         GetChannel()->set_video_format_desc(format_desc);\r
1506                         SetReplyString(TEXT("202 SET MODE OK\r\n"));\r
1507                 }\r
1508                 else\r
1509                         SetReplyString(TEXT("501 SET MODE FAILED\r\n"));\r
1510         }\r
1511         else\r
1512         {\r
1513                 this->SetReplyString(TEXT("403 SET ERROR\r\n"));\r
1514         }\r
1515 \r
1516         return true;\r
1517 }\r
1518 \r
1519 \r
1520 }       //namespace amcp\r
1521 }}      //namespace caspar