]> git.sesse.net Git - casparcg/blob - modules/flash/producer/cg_producer.cpp
Merge pull request #262 from gfto/html_producer_fix
[casparcg] / modules / flash / producer / cg_producer.cpp
1 /*\r
2 * Copyright 2013 Sveriges Television AB http://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: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../StdAfx.h"\r
23 \r
24 #include "cg_producer.h"\r
25 \r
26 #include "flash_producer.h"\r
27 \r
28 #include <common/env.h>\r
29 \r
30 #include <core/parameters/parameters.h>\r
31 #include <core/mixer/mixer.h>\r
32 \r
33 #include <boost/filesystem.hpp>\r
34 #include <boost/format.hpp>\r
35 #include <boost/algorithm/string.hpp>\r
36 #include <boost/regex.hpp>\r
37 #include <boost/property_tree/ptree.hpp>\r
38                 \r
39 namespace caspar { namespace flash {\r
40         \r
41 struct cg_producer::implementation : boost::noncopyable\r
42 {\r
43         safe_ptr<core::frame_producer> flash_producer_;\r
44 public:\r
45         implementation(const safe_ptr<core::frame_producer>& frame_producer) \r
46                 : flash_producer_(frame_producer)\r
47         {}\r
48         \r
49         boost::unique_future<std::wstring> add(int layer, std::wstring filename,  bool play_on_load, const std::wstring& label, const std::wstring& data)\r
50         {\r
51                 if(filename.size() > 0 && filename[0] == L'/')\r
52                         filename = filename.substr(1, filename.size()-1);\r
53 \r
54                 if(boost::filesystem::wpath(filename).extension() == L"")\r
55                         filename += L".ft";\r
56                 \r
57                 auto str = (boost::wformat(L"<invoke name=\"Add\" returntype=\"xml\"><arguments><number>%1%</number><string>%2%</string>%3%<string>%4%</string><string><![CDATA[%5%]]></string></arguments></invoke>") % layer % filename % (play_on_load?TEXT("<true/>"):TEXT("<false/>")) % label % data).str();\r
58 \r
59                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking add-command: " << str;\r
60                 return flash_producer_->call(str);\r
61         }\r
62 \r
63         boost::unique_future<std::wstring> remove(int layer)\r
64         {\r
65                 auto str = (boost::wformat(L"<invoke name=\"Delete\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();\r
66                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking remove-command: " << str;\r
67                 return flash_producer_->call(str);\r
68         }\r
69 \r
70         boost::unique_future<std::wstring> play(int layer)\r
71         {\r
72                 auto str = (boost::wformat(L"<invoke name=\"Play\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();\r
73                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking play-command: " << str;\r
74                 return flash_producer_->call(str);\r
75         }\r
76 \r
77         boost::unique_future<std::wstring> stop(int layer, unsigned int)\r
78         {\r
79                 auto str = (boost::wformat(L"<invoke name=\"Stop\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array><number>0</number></arguments></invoke>") % layer).str();\r
80                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking stop-command: " << str;\r
81                 return flash_producer_->call(str);\r
82         }\r
83 \r
84         boost::unique_future<std::wstring> next(int layer)\r
85         {\r
86                 auto str = (boost::wformat(L"<invoke name=\"Next\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();\r
87                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking next-command: " << str;\r
88                 return flash_producer_->call(str);\r
89         }\r
90 \r
91         boost::unique_future<std::wstring> update(int layer, const std::wstring& data)\r
92         {\r
93                 auto str = (boost::wformat(L"<invoke name=\"SetData\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array><string><![CDATA[%2%]]></string></arguments></invoke>") % layer % data).str();\r
94                 CASPAR_LOG(info) << flash_producer_->print() <<" Invoking update-command: " << str;\r
95                 return flash_producer_->call(str);\r
96         }\r
97 \r
98         boost::unique_future<std::wstring> invoke(int layer, const std::wstring& label)\r
99         {\r
100                 auto str = (boost::wformat(L"<invoke name=\"Invoke\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array><string>%2%</string></arguments></invoke>") % layer % label).str();\r
101                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking invoke-command: " << str;\r
102                 return flash_producer_->call(str);\r
103         }\r
104 \r
105         boost::unique_future<std::wstring> description(int layer)\r
106         {\r
107                 auto str = (boost::wformat(L"<invoke name=\"GetDescription\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();\r
108                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking description-command: " << str;\r
109                 return flash_producer_->call(str);\r
110         }\r
111 \r
112         boost::unique_future<std::wstring> template_host_info()\r
113         {\r
114                 auto str = (boost::wformat(L"<invoke name=\"GetInfo\" returntype=\"xml\"><arguments></arguments></invoke>")).str();\r
115                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking info-command: " << str;\r
116                 return flash_producer_->call(str);\r
117         }\r
118 \r
119         boost::unique_future<std::wstring> call(const std::wstring& str)\r
120         {               \r
121                 static const boost::wregex add_exp                      (L"ADD (?<LAYER>\\d+) (?<FILENAME>[^\\s]+) (?<PLAY_ON_LOAD>\\d)( (?<DATA>.*))?", boost::regex::perl|boost::regex::icase);\r
122                 static const boost::wregex remove_exp           (L"REMOVE (?<LAYER>\\d+)", boost::regex::perl|boost::regex::icase);\r
123                 static const boost::wregex play_exp                     (L"PLAY (?<LAYER>\\d+)", boost::regex::perl|boost::regex::icase);\r
124                 static const boost::wregex stop_exp                     (L"STOP (?<LAYER>\\d+)", boost::regex::perl|boost::regex::icase);\r
125                 static const boost::wregex next_exp                     (L"NEXT (?<LAYER>\\d+)", boost::regex::perl|boost::regex::icase);\r
126                 static const boost::wregex update_exp           (L"UPDATE (?<LAYER>\\d+) (?<DATA>.+)", boost::regex::perl|boost::regex::icase);\r
127                 static const boost::wregex invoke_exp           (L"INVOKE (?<LAYER>\\d+) (?<LABEL>.+)", boost::regex::perl|boost::regex::icase);\r
128                 static const boost::wregex description_exp      (L"INFO (?<LAYER>\\d+)", boost::regex::perl|boost::regex::icase);\r
129                 static const boost::wregex info_exp                     (L"INFO", boost::regex::perl|boost::regex::icase);\r
130                 \r
131                 boost::wsmatch what;\r
132                 if(boost::regex_match(str, what, add_exp))\r
133                         return add(\r
134                                         boost::lexical_cast<int>(what["LAYER"].str()),\r
135                                         what["FILENAME"].str(),\r
136                                         boost::lexical_cast<bool>(what["PLAY_ON_LOAD"].str()),\r
137                                         L"",\r
138                                         what["DATA"].str());\r
139                 else if(boost::regex_match(str, what, remove_exp))\r
140                         return remove(boost::lexical_cast<int>(what["LAYER"].str()));\r
141                 else if(boost::regex_match(str, what, play_exp))\r
142                         return play(boost::lexical_cast<int>(what["LAYER"].str()));\r
143                 else if(boost::regex_match(str, what, stop_exp))\r
144                         return stop(boost::lexical_cast<int>(what["LAYER"].str()), 0);\r
145                 else if(boost::regex_match(str, what, next_exp))\r
146                         return next(boost::lexical_cast<int>(what["LAYER"].str()));\r
147                 else if(boost::regex_match(str, what, update_exp))\r
148                         return update(boost::lexical_cast<int>(what["LAYER"].str()), what["DATA"].str());\r
149                 else if(boost::regex_match(str, what, invoke_exp))\r
150                         return invoke(boost::lexical_cast<int>(what["LAYER"].str()), what["LABEL"].str());\r
151                 else if(boost::regex_match(str, what, description_exp))\r
152                         return description(boost::lexical_cast<int>(what["LAYER"].str()));\r
153                 else if(boost::regex_match(str, what, info_exp))\r
154                         return template_host_info(); \r
155 \r
156                 return flash_producer_->call(str);\r
157         }\r
158 \r
159         safe_ptr<core::basic_frame> receive(int hints)\r
160         {\r
161                 return flash_producer_->receive(hints);\r
162         }\r
163 \r
164         safe_ptr<core::basic_frame> last_frame() const\r
165         {\r
166                 return flash_producer_->last_frame();\r
167         }               \r
168                         \r
169         std::wstring print() const\r
170         {\r
171                 return flash_producer_->print();\r
172         }\r
173 \r
174         boost::property_tree::wptree info() const\r
175         {\r
176                 boost::property_tree::wptree info;\r
177                 info.add(L"type", L"cg-producer");\r
178                 return info;\r
179         }\r
180 \r
181         std::wstring timed_invoke(int layer, const std::wstring& label)\r
182         {\r
183                 auto result = invoke(layer, label);\r
184                 if(result.timed_wait(boost::posix_time::seconds(2)))\r
185                         return result.get();\r
186                 return L"";\r
187         }\r
188         std::wstring timed_description(int layer)\r
189         {\r
190                 auto result = description(layer);\r
191                 if(result.timed_wait(boost::posix_time::seconds(2)))\r
192                         return result.get();\r
193                 return L"";\r
194         }\r
195         std::wstring timed_template_host_info()\r
196         {\r
197                 auto result = template_host_info();\r
198                 if(result.timed_wait(boost::posix_time::seconds(2)))\r
199                         return result.get();\r
200                 return L"";\r
201         }\r
202 \r
203         core::monitor::subject& monitor_output()\r
204         {\r
205                 return flash_producer_->monitor_output();\r
206         }\r
207 };\r
208 \r
209 void with_default_cg_producer(\r
210                 std::function<void (safe_ptr<cg_producer>)> command,\r
211                 const safe_ptr<core::video_channel>& video_channel,\r
212                 bool expect_existing,\r
213                 int layer_index)\r
214 {\r
215         auto flash_producer = video_channel->stage()->foreground(layer_index).get();\r
216         bool was_created = false;\r
217 \r
218         try\r
219         {\r
220                 if(flash_producer->print().find(L"flash[") == std::string::npos) // UGLY hack\r
221                 {\r
222                         if (expect_existing)\r
223                                 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(\r
224                                                 "No flash producer on layer "\r
225                                                 + boost::lexical_cast<std::string>(layer_index)));\r
226 \r
227                         flash_producer = flash::create_producer(video_channel->mixer(), boost::assign::list_of<std::wstring>());\r
228                 }\r
229 \r
230                 if (expect_existing && flash_producer->call(L"?").get() == L"0")\r
231                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(\r
232                                         "No flash player on layer "\r
233                                         + boost::lexical_cast<std::string>(layer_index)));\r
234 \r
235                 was_created = true;\r
236         }\r
237         catch(...)\r
238         {\r
239                 CASPAR_LOG_CURRENT_EXCEPTION();\r
240                 throw;\r
241         }\r
242 \r
243         command(make_safe<cg_producer>(flash_producer));\r
244 \r
245         if (was_created)\r
246         {\r
247                 video_channel->stage()->load(layer_index, flash_producer); \r
248                 video_channel->stage()->play(layer_index);\r
249         }\r
250 }\r
251         \r
252 safe_ptr<cg_producer> get_default_cg_producer(\r
253                 const safe_ptr<core::video_channel>& video_channel,\r
254                 bool expect_existing,\r
255                 int render_layer)\r
256 {       \r
257         std::shared_ptr<cg_producer> producer;\r
258 \r
259         with_default_cg_producer(\r
260                         [&producer](safe_ptr<cg_producer> p)\r
261                         {\r
262                                 producer = p;\r
263                         }, video_channel, expect_existing, render_layer);\r
264 \r
265         return make_safe_ptr(producer);\r
266 }\r
267 \r
268 safe_ptr<core::frame_producer> create_cg_producer_and_autoplay_file(\r
269                 const safe_ptr<core::frame_factory>& frame_factory, \r
270                 const core::parameters& params,\r
271                 const std::wstring& filename) \r
272 {\r
273         if(!boost::filesystem::exists(filename))\r
274                 return core::frame_producer::empty();\r
275                 \r
276         boost::filesystem2::wpath path(filename);\r
277         path = boost::filesystem2::complete(path);\r
278         auto filename2 = path.file_string();\r
279 \r
280         auto flash_producer = flash::create_producer(frame_factory, boost::assign::list_of<std::wstring>());    \r
281         auto producer = make_safe<cg_producer>(flash_producer);\r
282         producer->add(0, filename2, 1);\r
283 \r
284         return producer;\r
285 }\r
286 \r
287 safe_ptr<core::frame_producer> create_ct_producer(\r
288                 const safe_ptr<core::frame_factory>& frame_factory,\r
289                 const core::parameters& params) \r
290 {\r
291         return create_cg_producer_and_autoplay_file(frame_factory, params, env::media_folder() + L"\\" + params.at_original(0) + L".ct");\r
292 }\r
293 \r
294 safe_ptr<core::frame_producer> create_cg_producer(\r
295                 const safe_ptr<core::frame_factory>& frame_factory,\r
296                 const core::parameters& params) \r
297 {\r
298         if(params.empty() || params.at(0) != L"[CG]")\r
299                 return core::frame_producer::empty();\r
300 \r
301         return make_safe<cg_producer>(flash::create_producer(frame_factory, boost::assign::list_of<std::wstring>()));   \r
302 }\r
303 \r
304 cg_producer::cg_producer(const safe_ptr<core::frame_producer>& frame_producer) : impl_(new implementation(frame_producer)){}\r
305 cg_producer::cg_producer(cg_producer&& other) : impl_(std::move(other.impl_)){}\r
306 safe_ptr<core::basic_frame> cg_producer::receive(int hints){return impl_->receive(hints);}\r
307 safe_ptr<core::basic_frame> cg_producer::last_frame() const{return impl_->last_frame();}\r
308 void cg_producer::add(int layer, const std::wstring& template_name,  bool play_on_load, const std::wstring& startFromLabel, const std::wstring& data){impl_->add(layer, template_name, play_on_load, startFromLabel, data).wait();}\r
309 void cg_producer::remove(int layer){impl_->remove(layer);}\r
310 void cg_producer::play(int layer){impl_->play(layer);}\r
311 void cg_producer::stop(int layer, unsigned int mix_out_duration){impl_->stop(layer, mix_out_duration);}\r
312 void cg_producer::next(int layer){impl_->next(layer);}\r
313 void cg_producer::update(int layer, const std::wstring& data){impl_->update(layer, data);}\r
314 std::wstring cg_producer::print() const{return impl_->print();}\r
315 boost::unique_future<std::wstring> cg_producer::call(const std::wstring& str){return impl_->call(str);}\r
316 std::wstring cg_producer::invoke(int layer, const std::wstring& label){return impl_->timed_invoke(layer, label);}\r
317 std::wstring cg_producer::description(int layer){return impl_->timed_description(layer);}\r
318 std::wstring cg_producer::template_host_info(){return impl_->timed_template_host_info();}\r
319 boost::property_tree::wptree cg_producer::info() const{return impl_->info();}\r
320 core::monitor::subject& cg_producer::monitor_output(){return impl_->monitor_output();}\r
321 }}