]> git.sesse.net Git - casparcg/blob - modules/flash/flash.cpp
[flash] Moved template host copying to flash module startup instead from env setup...
[casparcg] / modules / flash / flash.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "StdAfx.h"
23
24 #include "flash.h"
25 #include "util/swf.h"
26
27 #include "producer/flash_producer.h"
28
29 #include <common/env.h>
30 #include <common/os/windows/windows.h>
31
32 #include <core/producer/media_info/media_info.h>
33 #include <core/producer/media_info/media_info_repository.h>
34 #include <core/producer/cg_proxy.h>
35 #include <core/system_info_provider.h>
36 #include <core/frame/frame_factory.h>
37 #include <core/video_format.h>
38 #include <core/help/help_sink.h>
39 #include <core/help/help_repository.h>
40
41 #include <boost/property_tree/ptree.hpp>
42 #include <boost/noncopyable.hpp>
43 #include <boost/filesystem.hpp>
44
45 #include <string>
46 #include <future>
47
48 namespace caspar { namespace flash {
49
50 std::wstring version();
51 std::wstring cg_version();
52
53 std::wstring get_absolute(const std::wstring& base_folder, const std::wstring& filename)
54 {
55         return (boost::filesystem::path(base_folder) / filename).wstring();
56 }
57
58 class flash_cg_proxy : public core::cg_proxy, boost::noncopyable
59 {
60         spl::shared_ptr<core::frame_producer>   flash_producer_;
61         std::wstring                                                    base_folder_;
62 public:
63         explicit flash_cg_proxy(const spl::shared_ptr<core::frame_producer>& producer, std::wstring base_folder = env::template_folder())
64                 : flash_producer_(producer)
65                 , base_folder_(base_folder)
66         {
67         }
68
69         //cg_proxy
70
71         void add(int layer, const std::wstring& template_name, bool play_on_load, const std::wstring& label, const std::wstring& data) override
72         {
73                 auto filename = template_name;
74
75                 if (filename.size() > 0 && filename[0] == L'/')
76                         filename = filename.substr(1, filename.size() - 1);
77
78                 filename = (boost::filesystem::path(base_folder_) / filename).wstring();
79                 filename = find_template(filename);
80
81                 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 ? L"<true/>" : L"<false/>") % label % data).str();
82                 CASPAR_LOG(debug) << flash_producer_->print() << " Invoking add-command: " << str;
83                 std::vector<std::wstring> params;
84                 params.push_back(std::move(str));
85                 flash_producer_->call(std::move(params)).get();
86         }
87
88         void verify_flash_player()
89         {
90                 if (flash_producer_->call({ L"?" }).get() == L"0")
91                         CASPAR_THROW_EXCEPTION(expected_user_error() << msg_info("No flash player running on video layer."));
92         }
93
94         void remove(int layer) override
95         {
96                 verify_flash_player();
97
98                 auto str = (boost::wformat(L"<invoke name=\"Delete\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();
99                 CASPAR_LOG(debug) << flash_producer_->print() << " Invoking remove-command: " << str;
100                 std::vector<std::wstring> params;
101                 params.push_back(std::move(str));
102                 flash_producer_->call(std::move(params));
103         }
104
105         void play(int layer) override
106         {
107                 verify_flash_player();
108
109                 auto str = (boost::wformat(L"<invoke name=\"Play\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();
110                 CASPAR_LOG(debug) << flash_producer_->print() << " Invoking play-command: " << str;
111                 std::vector<std::wstring> params;
112                 params.push_back(std::move(str));
113                 flash_producer_->call(std::move(params));
114         }
115
116         void stop(int layer, unsigned int) override
117         {
118                 verify_flash_player();
119
120                 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();
121                 CASPAR_LOG(debug) << flash_producer_->print() << " Invoking stop-command: " << str;
122                 std::vector<std::wstring> params;
123                 params.push_back(std::move(str));
124                 flash_producer_->call(std::move(params));
125         }
126
127         void next(int layer) override
128         {
129                 verify_flash_player();
130
131                 auto str = (boost::wformat(L"<invoke name=\"Next\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();
132                 CASPAR_LOG(debug) << flash_producer_->print() << " Invoking next-command: " << str;
133                 std::vector<std::wstring> params;
134                 params.push_back(std::move(str));
135                 flash_producer_->call(std::move(params));
136         }
137
138         void update(int layer, const std::wstring& data) override
139         {
140                 verify_flash_player();
141
142                 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();
143                 CASPAR_LOG(debug) << flash_producer_->print() << " Invoking update-command: " << str;
144                 std::vector<std::wstring> params;
145                 params.push_back(std::move(str));
146                 flash_producer_->call(std::move(params));
147         }
148
149         std::wstring invoke(int layer, const std::wstring& label) override
150         {
151                 verify_flash_player();
152
153                 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();
154                 CASPAR_LOG(debug) << flash_producer_->print() << " Invoking invoke-command: " << str;
155                 std::vector<std::wstring> params;
156                 params.push_back(std::move(str));
157                 // TODO: because of std::async deferred timed waiting does not work so for now we have to block
158                 return flash_producer_->call(std::move(params)).get();
159         }
160
161         std::wstring description(int layer) override
162         {
163                 verify_flash_player();
164
165                 auto str = (boost::wformat(L"<invoke name=\"GetDescription\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();
166                 CASPAR_LOG(debug) << flash_producer_->print() << " Invoking description-command: " << str;
167                 std::vector<std::wstring> params;
168                 params.push_back(std::move(str));
169                 // TODO: because of std::async deferred timed waiting does not work so for now we have to block
170                 return flash_producer_->call(std::move(params)).get();
171         }
172
173         std::wstring template_host_info() override
174         {
175                 auto str = (boost::wformat(L"<invoke name=\"GetInfo\" returntype=\"xml\"><arguments></arguments></invoke>")).str();
176                 CASPAR_LOG(debug) << flash_producer_->print() << " Invoking info-command: " << str;
177                 std::vector<std::wstring> params;
178                 params.push_back(std::move(str));
179                 // TODO: because of std::async deferred timed waiting does not work so for now we have to block
180                 return flash_producer_->call(std::move(params)).get();
181         }
182 };
183
184 void describe_ct_producer(core::help_sink& sink, const core::help_repository& repo)
185 {
186         sink.short_description(L"Plays compressed flash templates (.ct files).");
187         sink.syntax(L"[ct_file:string]");
188         sink.para()->text(L"Plays compressed flash templates (.ct files). The file should reside under the media folder.");
189         sink.para()->text(L"A ct file is a zip file containing a flash template (.ft), an XML file with template data and media files.");
190         sink.para()->text(L"Examples:");
191         sink.example(L">> PLAY 1-10 folder/ct_file");
192 }
193
194 spl::shared_ptr<core::frame_producer> create_ct_producer(
195                 const core::frame_producer_dependencies& dependencies,
196                 const std::vector<std::wstring>& params)
197 {
198         if (params.empty() || !boost::filesystem::exists(get_absolute(env::media_folder(), params.at(0)) + L".ct"))
199                 return core::frame_producer::empty();
200
201         auto flash_producer = flash::create_producer(dependencies, {});
202         auto producer = flash_producer;
203         flash_cg_proxy(producer, env::media_folder()).add(0, params.at(0), true, L"", L"");
204
205         return producer;
206 }
207
208 void copy_template_hosts()
209 {
210         try
211         {
212                 for (auto it = boost::filesystem::directory_iterator(env::initial_folder()); it != boost::filesystem::directory_iterator(); ++it)
213                 {
214                         if (it->path().wstring().find(L".fth") != std::wstring::npos)
215                         {
216                                 auto from_path = *it;
217                                 auto to_path = boost::filesystem::path(env::template_folder() + L"/" + it->path().filename().wstring());
218
219                                 if (boost::filesystem::exists(to_path))
220                                         boost::filesystem::remove(to_path);
221
222                                 boost::filesystem::copy_file(from_path, to_path);
223                         }
224                 }
225         }
226         catch (...)
227         {
228                 CASPAR_LOG_CURRENT_EXCEPTION();
229                 CASPAR_LOG(error) << L"Failed to copy template-hosts from initial-path to template-path.";
230         }
231 }
232
233 void init(core::module_dependencies dependencies)
234 {
235         copy_template_hosts();
236
237         dependencies.producer_registry->register_producer_factory(L"Flash Producer (.ct)", create_ct_producer, describe_ct_producer);
238         dependencies.producer_registry->register_producer_factory(L"Flash Producer (.swf)", create_swf_producer, describe_swf_producer);
239         dependencies.media_info_repo->register_extractor([](const std::wstring& file, const std::wstring& extension, core::media_info& info)
240         {
241                 if (extension != L".CT" && extension != L".SWF")
242                         return false;
243
244                 info.clip_type = L"MOVIE";
245
246                 return true;
247         });
248         dependencies.system_info_provider_repo->register_system_info_provider([](boost::property_tree::wptree& info)
249         {
250                 info.add(L"system.flash", version());
251         });
252         dependencies.system_info_provider_repo->register_version_provider(L"FLASH", &version);
253         dependencies.system_info_provider_repo->register_version_provider(L"TEMPLATEHOST", &cg_version);
254         dependencies.cg_registry->register_cg_producer(
255                         L"flash",
256                         { L".ft", L".ct" },
257                         [](const std::wstring& filename)
258                         {
259                                 return read_template_meta_info(filename);
260                         },
261                         [](const spl::shared_ptr<core::frame_producer>& producer)
262                         {
263                                 return spl::make_shared<flash_cg_proxy>(producer);
264                         },
265                         [](const core::frame_producer_dependencies& dependencies, const std::wstring&)
266                         {
267                                 return flash::create_producer(dependencies, { });
268                         },
269                         true
270                 );
271 }
272
273 std::wstring cg_version()
274 {
275         return L"Unknown";
276 }
277
278 std::wstring version()
279 {
280         std::wstring version = L"Not found";
281 #ifdef WIN32
282         HKEY   hkey;
283
284         DWORD dwType, dwSize;
285         if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Macromedia\\FlashPlayerActiveX"), 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS)
286         {
287                 wchar_t ver_str[1024];
288
289                 dwType = REG_SZ;
290                 dwSize = sizeof(ver_str);
291                 RegQueryValueEx(hkey, TEXT("Version"), NULL, &dwType, (PBYTE)&ver_str, &dwSize);
292
293                 version = ver_str;
294
295                 RegCloseKey(hkey);
296         }
297 #endif
298         return version;
299 }
300
301 }}