]> git.sesse.net Git - casparcg/blob - modules/flash/producer/cg_proxy.cpp
set svn:eol-style native on .h and .cpp files
[casparcg] / modules / flash / producer / cg_proxy.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 "cg_proxy.h"
25
26 #include "flash_producer.h"
27
28 #include <common/env.h>
29
30 #include <core/mixer/mixer.h>
31
32 #include <boost/filesystem.hpp>
33 #include <boost/format.hpp>
34 #include <boost/algorithm/string.hpp>
35 #include <boost/regex.hpp>
36 #include <boost/property_tree/ptree.hpp>
37                 
38 namespace caspar { namespace flash {
39         
40 struct cg_proxy::impl : boost::noncopyable
41 {
42         spl::shared_ptr<core::frame_producer> flash_producer_;
43 public:
44         impl(const spl::shared_ptr<core::frame_producer>& frame_producer) 
45                 : flash_producer_(frame_producer)
46         {}
47         
48         boost::unique_future<std::wstring> add(int layer, std::wstring filename,  bool play_on_load, const std::wstring& label, const std::wstring& data)
49         {
50                 if(filename.size() > 0 && filename[0] == L'/')
51                         filename = filename.substr(1, filename.size()-1);
52
53                 if(boost::filesystem::wpath(filename).extension() == L"")
54                         filename += L".ft";
55                 
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();
57
58                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking add-command: " << str;
59                 return flash_producer_->call(str);
60         }
61
62         boost::unique_future<std::wstring> remove(int layer)
63         {
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();
65                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking remove-command: " << str;
66                 return flash_producer_->call(str);
67         }
68
69         boost::unique_future<std::wstring> play(int layer)
70         {
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();
72                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking play-command: " << str;
73                 return flash_producer_->call(str);
74         }
75
76         boost::unique_future<std::wstring> stop(int layer, unsigned int)
77         {
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();
79                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking stop-command: " << str;
80                 return flash_producer_->call(str);
81         }
82
83         boost::unique_future<std::wstring> next(int layer)
84         {
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();
86                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking next-command: " << str;
87                 return flash_producer_->call(str);
88         }
89
90         boost::unique_future<std::wstring> update(int layer, const std::wstring& data)
91         {
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();
93                 CASPAR_LOG(info) << flash_producer_->print() <<" Invoking update-command: " << str;
94                 return flash_producer_->call(str);
95         }
96
97         boost::unique_future<std::wstring> invoke(int layer, const std::wstring& label)
98         {
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();
100                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking invoke-command: " << str;
101                 return flash_producer_->call(str);
102         }
103
104         boost::unique_future<std::wstring> description(int layer)
105         {
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();
107                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking description-command: " << str;
108                 return flash_producer_->call(str);
109         }
110
111         boost::unique_future<std::wstring> template_host_info()
112         {
113                 auto str = (boost::wformat(L"<invoke name=\"GetInfo\" returntype=\"xml\"><arguments></arguments></invoke>")).str();
114                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking info-command: " << str;
115                 return flash_producer_->call(str);
116         }
117                 
118         std::wstring timed_invoke(int layer, const std::wstring& label)
119         {
120                 auto result = invoke(layer, label);
121                 if(result.timed_wait(boost::posix_time::seconds(2)))
122                         return result.get();
123                 return L"";
124         }
125         std::wstring timed_description(int layer)
126         {
127                 auto result = description(layer);
128                 if(result.timed_wait(boost::posix_time::seconds(2)))
129                         return result.get();
130                 return L"";
131         }
132         std::wstring timed_template_host_info()
133         {
134                 auto result = template_host_info();
135                 if(result.timed_wait(boost::posix_time::seconds(2)))
136                         return result.get();
137                 return L"";
138         }
139 };
140         
141 cg_proxy create_cg_proxy(const spl::shared_ptr<core::video_channel>& video_channel, int render_layer)
142 {       
143         auto flash_producer = video_channel->stage().foreground(render_layer).get();
144
145         try
146         {
147                 if(flash_producer->name() != L"flash")
148                 {
149                         flash_producer = flash::create_producer(video_channel->frame_factory(), video_channel->video_format_desc(), boost::assign::list_of<std::wstring>());    
150                         video_channel->stage().load(render_layer, flash_producer); 
151                         video_channel->stage().play(render_layer);
152                 }
153         }
154         catch(...)
155         {
156                 CASPAR_LOG_CURRENT_EXCEPTION();
157                 throw;
158         }
159
160         return cg_proxy(std::move(flash_producer));
161 }
162
163 spl::shared_ptr<core::frame_producer> create_cg_producer_and_autoplay_file(
164                 const spl::shared_ptr<core::frame_factory> frame_factory, 
165                 const core::video_format_desc& format_desc, 
166                 const std::vector<std::wstring>& params,
167                 const std::wstring& filename) 
168 {
169         if(!boost::filesystem::exists(filename))
170                 return core::frame_producer::empty();
171                 
172         boost::filesystem::path path(filename);
173         path = boost::filesystem3::complete(path);
174         auto filename2 = path.wstring();
175
176         auto flash_producer = flash::create_producer(frame_factory, format_desc, boost::assign::list_of<std::wstring>());       
177         auto producer = flash_producer;
178         cg_proxy(producer).add(0, filename2, 1);
179
180         return producer;
181 }
182
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) 
184 {
185         return create_cg_producer_and_autoplay_file(
186                 frame_factory, format_desc, params, env::media_folder() + L"\\" + params[0] + L".ct");
187 }
188
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) 
190 {
191         return create_cg_producer_and_autoplay_file(
192                 frame_factory, format_desc, params, env::media_folder() + L"\\" + params[0] + L".swf");
193 }
194
195 cg_proxy::cg_proxy(const spl::shared_ptr<core::frame_producer>& frame_producer) : impl_(new impl(frame_producer)){}
196 cg_proxy::cg_proxy(cg_proxy&& other) : impl_(std::move(other.impl_)){}
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);}
198 void cg_proxy::remove(int layer){impl_->remove(layer);}
199 void cg_proxy::play(int layer){impl_->play(layer);}
200 void cg_proxy::stop(int layer, unsigned int mix_out_duration){impl_->stop(layer, mix_out_duration);}
201 void cg_proxy::next(int layer){impl_->next(layer);}
202 void cg_proxy::update(int layer, const std::wstring& data){impl_->update(layer, data);}
203 std::wstring cg_proxy::invoke(int layer, const std::wstring& label){return impl_->timed_invoke(layer, label);}
204 std::wstring cg_proxy::description(int layer){return impl_->timed_description(layer);}
205 std::wstring cg_proxy::template_host_info(){return impl_->timed_template_host_info();}
206
207 }}