]> git.sesse.net Git - casparcg/blob - modules/flash/producer/cg_producer.cpp
Fixed Neptune support.
[casparcg] / modules / flash / producer / cg_producer.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: 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/mixer/mixer.h>\r
31 \r
32 #include <boost/filesystem.hpp>\r
33 #include <boost/format.hpp>\r
34 #include <boost/algorithm/string.hpp>\r
35 #include <boost/regex.hpp>\r
36 #include <boost/property_tree/ptree.hpp>\r
37                 \r
38 namespace caspar { namespace flash {\r
39         \r
40 struct cg_producer::implementation : boost::noncopyable\r
41 {\r
42         safe_ptr<core::frame_producer> flash_producer_;\r
43 public:\r
44         implementation(const safe_ptr<core::frame_producer>& frame_producer) \r
45                 : flash_producer_(frame_producer)\r
46         {}\r
47         \r
48         boost::unique_future<std::wstring> add(int layer, std::wstring filename,  bool play_on_load, const std::wstring& label, std::wstring data)\r
49         {\r
50                 if(filename.size() > 0 && filename[0] == L'/')\r
51                         filename = filename.substr(1, filename.size()-1);\r
52 \r
53                 if(boost::filesystem::wpath(filename).extension() == L"")\r
54                         filename += L".ft";\r
55                 \r
56                 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
57 \r
58                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking add-command: " << str;\r
59                 return flash_producer_->call(str);\r
60         }\r
61 \r
62         boost::unique_future<std::wstring> remove(int layer)\r
63         {\r
64                 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
65                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking remove-command: " << str;\r
66                 return flash_producer_->call(str);\r
67         }\r
68 \r
69         boost::unique_future<std::wstring> play(int layer)\r
70         {\r
71                 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
72                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking play-command: " << str;\r
73                 return flash_producer_->call(str);\r
74         }\r
75 \r
76         boost::unique_future<std::wstring> stop(int layer, unsigned int)\r
77         {\r
78                 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
79                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking stop-command: " << str;\r
80                 return flash_producer_->call(str);\r
81         }\r
82 \r
83         boost::unique_future<std::wstring> next(int layer)\r
84         {\r
85                 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
86                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking next-command: " << str;\r
87                 return flash_producer_->call(str);\r
88         }\r
89 \r
90         boost::unique_future<std::wstring> update(int layer, const std::wstring& data)\r
91         {\r
92                 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
93                 CASPAR_LOG(info) << flash_producer_->print() <<" Invoking update-command: " << str;\r
94                 return flash_producer_->call(str);\r
95         }\r
96 \r
97         boost::unique_future<std::wstring> invoke(int layer, const std::wstring& label)\r
98         {\r
99                 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
100                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking invoke-command: " << str;\r
101                 return flash_producer_->call(str);\r
102         }\r
103 \r
104         boost::unique_future<std::wstring> description(int layer)\r
105         {\r
106                 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
107                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking description-command: " << str;\r
108                 return flash_producer_->call(str);\r
109         }\r
110 \r
111         boost::unique_future<std::wstring> template_host_info()\r
112         {\r
113                 auto str = (boost::wformat(L"<invoke name=\"GetInfo\" returntype=\"xml\"><arguments></arguments></invoke>")).str();\r
114                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking info-command: " << str;\r
115                 return flash_producer_->call(str);\r
116         }\r
117 \r
118         boost::unique_future<std::wstring> call(const std::wstring& str)\r
119         {               \r
120                 static const boost::wregex add_exp                      (L"ADD (?<LAYER>\\d+) (?<FILENAME>[^\\s]+) (?<PLAY_ON_LOAD>\\d)( (?<DATA>.*))?");\r
121                 static const boost::wregex remove_exp           (L"REMOVE (?<LAYER>\\d+)");\r
122                 static const boost::wregex play_exp                     (L"PLAY (?<LAYER>\\d+)");\r
123                 static const boost::wregex stop_exp                     (L"STOP (?<LAYER>\\d+)");\r
124                 static const boost::wregex next_exp                     (L"NEXT (?<LAYER>\\d+)");\r
125                 static const boost::wregex update_exp           (L"UPDATE (?<LAYER>\\d+) (?<DATA>.+)");\r
126                 static const boost::wregex invoke_exp           (L"INVOKE (?<LAYER>\\d+) (?<LABEL>.+)");\r
127                 static const boost::wregex description_exp      (L"INFO (?<LAYER>\\d+)");\r
128                 static const boost::wregex info_exp                     (L"INFO");\r
129                 \r
130                 boost::wsmatch what;\r
131                 if(boost::regex_match(str, what, add_exp))\r
132                         return add(boost::lexical_cast<int>(what["LAYER"].str()), flash::find_template(env::template_folder() + what["FILENAME"].str()), boost::lexical_cast<bool>(what["PLAY_ON_LOAD"].str()), L"", what["DATA"].str()); \r
133                 else if(boost::regex_match(str, what, remove_exp))\r
134                         return remove(boost::lexical_cast<int>(what["LAYER"].str())); \r
135                 else if(boost::regex_match(str, what, stop_exp))\r
136                         return stop(boost::lexical_cast<int>(what["LAYER"].str()), 0); \r
137                 else if(boost::regex_match(str, what, next_exp))\r
138                         return next(boost::lexical_cast<int>(what["LAYER"].str())); \r
139                 else if(boost::regex_match(str, what, update_exp))\r
140                         return update(boost::lexical_cast<int>(what["LAYER"].str()), what["DATA"].str()); \r
141                 else if(boost::regex_match(str, what, next_exp))\r
142                         return invoke(boost::lexical_cast<int>(what["LAYER"].str()), what["LABEL"].str()); \r
143                 else if(boost::regex_match(str, what, description_exp))\r
144                         return description(boost::lexical_cast<int>(what["LAYER"].str())); \r
145                 else if(boost::regex_match(str, what, invoke_exp))\r
146                         return template_host_info(); \r
147 \r
148                 return flash_producer_->call(str);\r
149         }\r
150 \r
151         safe_ptr<core::basic_frame> receive(int hints)\r
152         {\r
153                 return flash_producer_->receive(hints);\r
154         }\r
155 \r
156         safe_ptr<core::basic_frame> last_frame() const\r
157         {\r
158                 return flash_producer_->last_frame();\r
159         }               \r
160                         \r
161         std::wstring print() const\r
162         {\r
163                 return flash_producer_->print();\r
164         }\r
165 \r
166         boost::property_tree::wptree info() const\r
167         {\r
168                 boost::property_tree::wptree info;\r
169                 info.add(L"type", L"cg-producer");\r
170                 return info;\r
171         }\r
172 \r
173         std::wstring timed_invoke(int layer, const std::wstring& label)\r
174         {\r
175                 auto result = invoke(layer, label);\r
176                 if(result.timed_wait(boost::posix_time::seconds(2)))\r
177                         return result.get();\r
178                 return L"";\r
179         }\r
180         std::wstring timed_description(int layer)\r
181         {\r
182                 auto result = description(layer);\r
183                 if(result.timed_wait(boost::posix_time::seconds(2)))\r
184                         return result.get();\r
185                 return L"";\r
186         }\r
187         std::wstring timed_template_host_info()\r
188         {\r
189                 auto result = template_host_info();\r
190                 if(result.timed_wait(boost::posix_time::seconds(2)))\r
191                         return result.get();\r
192                 return L"";\r
193         }\r
194 };\r
195         \r
196 safe_ptr<cg_producer> get_default_cg_producer(const safe_ptr<core::video_channel>& video_channel, int render_layer)\r
197 {       \r
198         auto flash_producer = video_channel->stage()->foreground(render_layer).get();\r
199 \r
200         try\r
201         {\r
202                 if(flash_producer->print().find(L"flash[") == std::string::npos) // UGLY hack\r
203                 {\r
204                         flash_producer = make_safe<cg_producer>(flash::create_producer(video_channel->mixer(), boost::assign::list_of<std::wstring>()));        \r
205                         video_channel->stage()->load(render_layer, flash_producer); \r
206                         video_channel->stage()->play(render_layer);\r
207                 }\r
208         }\r
209         catch(...)\r
210         {\r
211                 CASPAR_LOG_CURRENT_EXCEPTION();\r
212                 throw;\r
213         }\r
214 \r
215         return static_pointer_cast<cg_producer>(flash_producer);\r
216 }\r
217 \r
218 safe_ptr<core::frame_producer> create_ct_producer(const safe_ptr<core::frame_factory> frame_factory, const std::vector<std::wstring>& params) \r
219 {\r
220         std::wstring filename = env::media_folder() + L"\\" + params[0] + L".ct";\r
221         if(!boost::filesystem::exists(filename))\r
222                 return core::frame_producer::empty();\r
223                 \r
224         boost::filesystem2::wpath path(filename);\r
225         path = boost::filesystem2::complete(path);\r
226         filename = path.file_string();\r
227 \r
228         auto flash_producer = flash::create_producer(frame_factory, boost::assign::list_of<std::wstring>());    \r
229         auto producer = make_safe<cg_producer>(flash_producer);\r
230         producer->add(0, filename, 1);\r
231 \r
232         return producer;\r
233 }\r
234 \r
235 safe_ptr<core::frame_producer> create_cg_producer(const safe_ptr<core::frame_factory> frame_factory, const std::vector<std::wstring>& params) \r
236 {\r
237         if(params.empty() || params.at(0) != L"[CG]")\r
238                 return core::frame_producer::empty();\r
239 \r
240         return make_safe<cg_producer>(flash::create_producer(frame_factory, boost::assign::list_of<std::wstring>()));   \r
241 }\r
242 \r
243 cg_producer::cg_producer(const safe_ptr<core::frame_producer>& frame_producer) : impl_(new implementation(frame_producer)){}\r
244 cg_producer::cg_producer(cg_producer&& other) : impl_(std::move(other.impl_)){}\r
245 safe_ptr<core::basic_frame> cg_producer::receive(int hints){return impl_->receive(hints);}\r
246 safe_ptr<core::basic_frame> cg_producer::last_frame() const{return impl_->last_frame();}\r
247 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);}\r
248 void cg_producer::remove(int layer){impl_->remove(layer);}\r
249 void cg_producer::play(int layer){impl_->play(layer);}\r
250 void cg_producer::stop(int layer, unsigned int mix_out_duration){impl_->stop(layer, mix_out_duration);}\r
251 void cg_producer::next(int layer){impl_->next(layer);}\r
252 void cg_producer::update(int layer, const std::wstring& data){impl_->update(layer, data);}\r
253 std::wstring cg_producer::print() const{return impl_->print();}\r
254 boost::unique_future<std::wstring> cg_producer::call(const std::wstring& str){return impl_->call(str);}\r
255 std::wstring cg_producer::invoke(int layer, const std::wstring& label){return impl_->timed_invoke(layer, label);}\r
256 std::wstring cg_producer::description(int layer){return impl_->timed_description(layer);}\r
257 std::wstring cg_producer::template_host_info(){return impl_->timed_template_host_info();}\r
258 boost::property_tree::wptree cg_producer::info() const{return impl_->info();}\r
259 \r
260 }}