2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
27 #include "producer/flash_producer.h"
29 #include <common/env.h>
30 #include <common/os/windows/windows.h>
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>
41 #include <boost/property_tree/ptree.hpp>
42 #include <boost/noncopyable.hpp>
43 #include <boost/filesystem.hpp>
48 namespace caspar { namespace flash {
50 std::wstring version();
51 std::wstring cg_version();
53 std::wstring get_absolute(const std::wstring& base_folder, const std::wstring& filename)
55 return (boost::filesystem::path(base_folder) / filename).wstring();
58 class flash_cg_proxy : public core::cg_proxy, boost::noncopyable
60 spl::shared_ptr<core::frame_producer> flash_producer_;
61 std::wstring base_folder_;
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)
71 void add(int layer, const std::wstring& template_name, bool play_on_load, const std::wstring& label, const std::wstring& data) override
73 auto filename = template_name;
75 if (filename.size() > 0 && filename[0] == L'/')
76 filename = filename.substr(1, filename.size() - 1);
78 filename = (boost::filesystem::path(base_folder_) / filename).wstring();
79 filename = find_template(filename);
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(info) << 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));
88 void remove(int layer) override
90 auto str = (boost::wformat(L"<invoke name=\"Delete\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();
91 CASPAR_LOG(info) << flash_producer_->print() << " Invoking remove-command: " << str;
92 std::vector<std::wstring> params;
93 params.push_back(std::move(str));
94 flash_producer_->call(std::move(params));
97 void play(int layer) override
99 auto str = (boost::wformat(L"<invoke name=\"Play\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();
100 CASPAR_LOG(info) << flash_producer_->print() << " Invoking play-command: " << str;
101 std::vector<std::wstring> params;
102 params.push_back(std::move(str));
103 flash_producer_->call(std::move(params));
106 void stop(int layer, unsigned int) override
108 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();
109 CASPAR_LOG(info) << flash_producer_->print() << " Invoking stop-command: " << str;
110 std::vector<std::wstring> params;
111 params.push_back(std::move(str));
112 flash_producer_->call(std::move(params));
115 void next(int layer) override
117 auto str = (boost::wformat(L"<invoke name=\"Next\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();
118 CASPAR_LOG(info) << flash_producer_->print() << " Invoking next-command: " << str;
119 std::vector<std::wstring> params;
120 params.push_back(std::move(str));
121 flash_producer_->call(std::move(params));
124 void update(int layer, const std::wstring& data) override
126 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();
127 CASPAR_LOG(info) << flash_producer_->print() << " Invoking update-command: " << str;
128 std::vector<std::wstring> params;
129 params.push_back(std::move(str));
130 flash_producer_->call(std::move(params));
133 std::wstring invoke(int layer, const std::wstring& label) override
135 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();
136 CASPAR_LOG(info) << flash_producer_->print() << " Invoking invoke-command: " << str;
137 std::vector<std::wstring> params;
138 params.push_back(std::move(str));
139 // TODO: because of std::async deferred timed waiting does not work so for now we have to block
140 return flash_producer_->call(std::move(params)).get();
143 std::wstring description(int layer) override
145 auto str = (boost::wformat(L"<invoke name=\"GetDescription\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();
146 CASPAR_LOG(info) << flash_producer_->print() << " Invoking description-command: " << str;
147 std::vector<std::wstring> params;
148 params.push_back(std::move(str));
149 // TODO: because of std::async deferred timed waiting does not work so for now we have to block
150 return flash_producer_->call(std::move(params)).get();
153 std::wstring template_host_info() override
155 auto str = (boost::wformat(L"<invoke name=\"GetInfo\" returntype=\"xml\"><arguments></arguments></invoke>")).str();
156 CASPAR_LOG(info) << flash_producer_->print() << " Invoking info-command: " << str;
157 std::vector<std::wstring> params;
158 params.push_back(std::move(str));
159 // TODO: because of std::async deferred timed waiting does not work so for now we have to block
160 return flash_producer_->call(std::move(params)).get();
164 void describe_ct_producer(core::help_sink& sink, const core::help_repository& repo)
166 sink.short_description(L"Plays compressed flash templates (.ct files).");
167 sink.syntax(L"[ct_file:string]");
168 sink.para()->text(L"Plays compressed flash templates (.ct files). The file should reside under the media folder.");
169 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.");
170 sink.para()->text(L"Examples:");
171 sink.example(L">> PLAY 1-10 folder/ct_file");
174 spl::shared_ptr<core::frame_producer> create_ct_producer(
175 const core::frame_producer_dependencies& dependencies,
176 const std::vector<std::wstring>& params)
178 if (params.empty() || !boost::filesystem::exists(get_absolute(env::media_folder(), params.at(0)) + L".ct"))
179 return core::frame_producer::empty();
181 auto flash_producer = flash::create_producer(dependencies, {});
182 auto producer = flash_producer;
183 flash_cg_proxy(producer, env::media_folder()).add(0, params.at(0), true, L"", L"");
188 void init(core::module_dependencies dependencies)
190 dependencies.producer_registry->register_producer_factory(L"Flash Producer (.ct)", create_ct_producer, describe_ct_producer);
191 dependencies.producer_registry->register_producer_factory(L"Flash Producer (.swf)", create_swf_producer, describe_swf_producer);
192 dependencies.media_info_repo->register_extractor([](const std::wstring& file, const std::wstring& extension, core::media_info& info)
194 if (extension != L".CT" && extension != L".SWF")
197 info.clip_type = L"MOVIE";
201 dependencies.system_info_provider_repo->register_system_info_provider([](boost::property_tree::wptree& info)
203 info.add(L"system.flash", version());
205 dependencies.system_info_provider_repo->register_version_provider(L"FLASH", &version);
206 dependencies.system_info_provider_repo->register_version_provider(L"TEMPLATEHOST", &cg_version);
207 dependencies.cg_registry->register_cg_producer(
210 [](const std::wstring& filename)
212 return read_template_meta_info(filename);
214 [](const spl::shared_ptr<core::frame_producer>& producer)
216 return spl::make_shared<flash_cg_proxy>(producer);
218 [](const core::frame_producer_dependencies& dependencies, const std::wstring&)
220 return flash::create_producer(dependencies, { });
226 std::wstring cg_version()
231 std::wstring version()
233 std::wstring version = L"Not found";
237 DWORD dwType, dwSize;
238 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Macromedia\\FlashPlayerActiveX"), 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS)
240 wchar_t ver_str[1024];
243 dwSize = sizeof(ver_str);
244 RegQueryValueEx(hkey, TEXT("Version"), NULL, &dwType, (PBYTE)&ver_str, &dwSize);