]> git.sesse.net Git - casparcg/blob - modules/flash/flash.cpp
* Merged streaming_consumer from 2.0
[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
39 #include <boost/property_tree/ptree.hpp>
40 #include <boost/noncopyable.hpp>
41 #include <boost/filesystem.hpp>
42
43 #include <string>
44 #include <future>
45
46 namespace caspar { namespace flash {
47
48 std::wstring version();
49 std::wstring cg_version();
50
51 std::wstring get_absolute(const std::wstring& base_folder, const std::wstring& filename)
52 {
53         return (boost::filesystem::path(base_folder) / filename).wstring();
54 }
55
56 class flash_cg_proxy : public core::cg_proxy, boost::noncopyable
57 {
58         spl::shared_ptr<core::frame_producer>   flash_producer_;
59         std::wstring                                                    base_folder_;
60 public:
61         explicit flash_cg_proxy(const spl::shared_ptr<core::frame_producer>& producer, std::wstring base_folder = env::template_folder())
62                 : flash_producer_(producer)
63                 , base_folder_(base_folder)
64         {
65         }
66
67         //cg_proxy
68
69         void add(int layer, const std::wstring& template_name, bool play_on_load, const std::wstring& label, const std::wstring& data) override
70         {
71                 auto filename = template_name;
72
73                 if (filename.size() > 0 && filename[0] == L'/')
74                         filename = filename.substr(1, filename.size() - 1);
75
76                 filename = (boost::filesystem::path(base_folder_) / filename).wstring();
77                 filename = find_template(filename);
78
79                 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();
80                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking add-command: " << str;
81                 std::vector<std::wstring> params;
82                 params.push_back(std::move(str));
83                 flash_producer_->call(std::move(params));
84         }
85
86         void remove(int layer) override
87         {
88                 auto str = (boost::wformat(L"<invoke name=\"Delete\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();
89                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking remove-command: " << str;
90                 std::vector<std::wstring> params;
91                 params.push_back(std::move(str));
92                 flash_producer_->call(std::move(params));
93         }
94
95         void play(int layer) override
96         {
97                 auto str = (boost::wformat(L"<invoke name=\"Play\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();
98                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking play-command: " << str;
99                 std::vector<std::wstring> params;
100                 params.push_back(std::move(str));
101                 flash_producer_->call(std::move(params));
102         }
103
104         void stop(int layer, unsigned int) override
105         {
106                 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();
107                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking stop-command: " << str;
108                 std::vector<std::wstring> params;
109                 params.push_back(std::move(str));
110                 flash_producer_->call(std::move(params));
111         }
112
113         void next(int layer) override
114         {
115                 auto str = (boost::wformat(L"<invoke name=\"Next\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();
116                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking next-command: " << str;
117                 std::vector<std::wstring> params;
118                 params.push_back(std::move(str));
119                 flash_producer_->call(std::move(params));
120         }
121
122         void update(int layer, const std::wstring& data) override
123         {
124                 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();
125                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking update-command: " << str;
126                 std::vector<std::wstring> params;
127                 params.push_back(std::move(str));
128                 flash_producer_->call(std::move(params));
129         }
130
131         std::wstring invoke(int layer, const std::wstring& label) override
132         {
133                 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();
134                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking invoke-command: " << str;
135                 std::vector<std::wstring> params;
136                 params.push_back(std::move(str));
137                 // TODO: because of std::async deferred timed waiting does not work so for now we have to block
138                 return flash_producer_->call(std::move(params)).get();
139         }
140
141         std::wstring description(int layer) override
142         {
143                 auto str = (boost::wformat(L"<invoke name=\"GetDescription\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();
144                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking description-command: " << str;
145                 std::vector<std::wstring> params;
146                 params.push_back(std::move(str));
147                 // TODO: because of std::async deferred timed waiting does not work so for now we have to block
148                 return flash_producer_->call(std::move(params)).get();
149         }
150
151         std::wstring template_host_info() override
152         {
153                 auto str = (boost::wformat(L"<invoke name=\"GetInfo\" returntype=\"xml\"><arguments></arguments></invoke>")).str();
154                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking info-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
162 spl::shared_ptr<core::frame_producer> create_ct_producer(
163                 const spl::shared_ptr<core::frame_factory> frame_factory,
164                 const core::video_format_desc& format_desc,
165                 const std::vector<std::wstring>& params)
166 {
167         if (params.empty() || !boost::filesystem::exists(get_absolute(env::media_folder(), params.at(0)) + L".ct"))
168                 return core::frame_producer::empty();
169
170         auto flash_producer = flash::create_producer(frame_factory, format_desc, {});
171         auto producer = flash_producer;
172         flash_cg_proxy(producer, env::media_folder()).add(0, params.at(0), true, L"", L"");
173
174         return producer;
175 }
176
177 void init(core::module_dependencies dependencies)
178 {
179         core::register_producer_factory(create_ct_producer);
180         core::register_producer_factory(create_swf_producer);
181         dependencies.media_info_repo->register_extractor([](const std::wstring& file, const std::wstring& extension, core::media_info& info)
182         {
183                 if (extension != L".CT" && extension != L".SWF")
184                         return false;
185
186                 info.clip_type = L"MOVIE";
187
188                 return true;
189         });
190         dependencies.system_info_provider_repo->register_system_info_provider([](boost::property_tree::wptree& info)
191         {
192                 info.add(L"system.flash", version());
193         });
194         dependencies.system_info_provider_repo->register_version_provider(L"FLASH", &version);
195         dependencies.system_info_provider_repo->register_version_provider(L"TEMPLATEHOST", &cg_version);
196         dependencies.cg_registry->register_cg_producer(
197                         L"flash",
198                         { L".ft", L".ct" },
199                         [](const std::wstring& filename)
200                         {
201                                 return read_template_meta_info(get_absolute(env::template_folder(), filename) + L".ft");
202                         },
203                         [](const spl::shared_ptr<core::frame_producer>& producer)
204                         {
205                                 return spl::make_shared<flash_cg_proxy>(producer);
206                         },
207                         [](const spl::shared_ptr<core::frame_factory>& ff, const core::video_format_desc& f, const std::wstring&)
208                         {
209                                 return flash::create_producer(ff, f, { });
210                         },
211                         true
212                 );
213 }
214
215 std::wstring cg_version()
216 {
217         return L"Unknown";
218 }
219
220 std::wstring version()
221 {               
222         std::wstring version = L"Not found";
223 #ifdef WIN32 
224         HKEY   hkey;
225  
226         DWORD dwType, dwSize;
227         if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Macromedia\\FlashPlayerActiveX"), 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS)
228         {
229                 wchar_t ver_str[1024];
230
231                 dwType = REG_SZ;
232                 dwSize = sizeof(ver_str);
233                 RegQueryValueEx(hkey, TEXT("Version"), NULL, &dwType, (PBYTE)&ver_str, &dwSize);
234  
235                 version = ver_str;
236
237                 RegCloseKey(hkey);
238         }
239 #endif
240         return version;
241 }
242
243 }}