]> git.sesse.net Git - casparcg/blob - modules/flash/producer/cg_proxy.cpp
Merged sfw playing through template host instead of via ffmpeg
[casparcg] / modules / flash / producer / cg_proxy.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_proxy.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_proxy::impl : boost::noncopyable\r
41 {\r
42         spl::shared_ptr<core::frame_producer> flash_producer_;\r
43 public:\r
44         impl(const spl::shared_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, const 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?L"<true/>":L"<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         std::wstring timed_invoke(int layer, const std::wstring& label)\r
119         {\r
120                 auto result = invoke(layer, label);\r
121                 if(result.timed_wait(boost::posix_time::seconds(2)))\r
122                         return result.get();\r
123                 return L"";\r
124         }\r
125         std::wstring timed_description(int layer)\r
126         {\r
127                 auto result = description(layer);\r
128                 if(result.timed_wait(boost::posix_time::seconds(2)))\r
129                         return result.get();\r
130                 return L"";\r
131         }\r
132         std::wstring timed_template_host_info()\r
133         {\r
134                 auto result = template_host_info();\r
135                 if(result.timed_wait(boost::posix_time::seconds(2)))\r
136                         return result.get();\r
137                 return L"";\r
138         }\r
139 };\r
140         \r
141 cg_proxy create_cg_proxy(const spl::shared_ptr<core::video_channel>& video_channel, int render_layer)\r
142 {       \r
143         auto flash_producer = video_channel->stage().foreground(render_layer).get();\r
144 \r
145         try\r
146         {\r
147                 if(flash_producer->name() != L"flash")\r
148                 {\r
149                         flash_producer = flash::create_producer(video_channel->frame_factory(), video_channel->video_format_desc(), boost::assign::list_of<std::wstring>());    \r
150                         video_channel->stage().load(render_layer, flash_producer); \r
151                         video_channel->stage().play(render_layer);\r
152                 }\r
153         }\r
154         catch(...)\r
155         {\r
156                 CASPAR_LOG_CURRENT_EXCEPTION();\r
157                 throw;\r
158         }\r
159 \r
160         return cg_proxy(std::move(flash_producer));\r
161 }\r
162 \r
163 spl::shared_ptr<core::frame_producer> create_cg_producer_and_autoplay_file(\r
164                 const spl::shared_ptr<core::frame_factory> frame_factory, \r
165                 const core::video_format_desc& format_desc, \r
166                 const std::vector<std::wstring>& params,\r
167                 const std::wstring& filename) \r
168 {\r
169         if(!boost::filesystem::exists(filename))\r
170                 return core::frame_producer::empty();\r
171                 \r
172         boost::filesystem::path path(filename);\r
173         path = boost::filesystem3::complete(path);\r
174         auto filename2 = path.wstring();\r
175 \r
176         auto flash_producer = flash::create_producer(frame_factory, format_desc, boost::assign::list_of<std::wstring>());       \r
177         auto producer = flash_producer;\r
178         cg_proxy(producer).add(0, filename2, 1);\r
179 \r
180         return producer;\r
181 }\r
182 \r
183 spl::shared_ptr<core::frame_producer> create_ct_producer(const spl::shared_ptr<core::frame_factory> frame_factory, const core::video_format_desc& format_desc, const std::vector<std::wstring>& params) \r
184 {\r
185         return create_cg_producer_and_autoplay_file(\r
186                 frame_factory, format_desc, params, env::media_folder() + L"\\" + params[0] + L".ct");\r
187 }\r
188 \r
189 spl::shared_ptr<core::frame_producer> create_swf_producer(const spl::shared_ptr<core::frame_factory> frame_factory, const core::video_format_desc& format_desc, const std::vector<std::wstring>& params) \r
190 {\r
191         return create_cg_producer_and_autoplay_file(\r
192                 frame_factory, format_desc, params, env::media_folder() + L"\\" + params[0] + L".swf");\r
193 }\r
194 \r
195 cg_proxy::cg_proxy(const spl::shared_ptr<core::frame_producer>& frame_producer) : impl_(new impl(frame_producer)){}\r
196 cg_proxy::cg_proxy(cg_proxy&& other) : impl_(std::move(other.impl_)){}\r
197 void cg_proxy::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
198 void cg_proxy::remove(int layer){impl_->remove(layer);}\r
199 void cg_proxy::play(int layer){impl_->play(layer);}\r
200 void cg_proxy::stop(int layer, unsigned int mix_out_duration){impl_->stop(layer, mix_out_duration);}\r
201 void cg_proxy::next(int layer){impl_->next(layer);}\r
202 void cg_proxy::update(int layer, const std::wstring& data){impl_->update(layer, data);}\r
203 std::wstring cg_proxy::invoke(int layer, const std::wstring& label){return impl_->timed_invoke(layer, label);}\r
204 std::wstring cg_proxy::description(int layer){return impl_->timed_description(layer);}\r
205 std::wstring cg_proxy::template_host_info(){return impl_->timed_template_host_info();}\r
206 \r
207 }}