]> git.sesse.net Git - casparcg/blob - modules/flash/producer/cg_proxy.cpp
- Fixed diag to work with new SFML version.
[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 #include <core/diagnostics/call_context.h>
32
33 #include <boost/filesystem.hpp>
34 #include <boost/format.hpp>
35 #include <boost/algorithm/string.hpp>
36 #include <boost/regex.hpp>
37 #include <boost/property_tree/ptree.hpp>
38
39 #include <future>
40
41 namespace caspar { namespace flash {
42         
43 struct cg_proxy::impl : boost::noncopyable
44 {
45         spl::shared_ptr<core::frame_producer> flash_producer_;
46 public:
47         impl(const spl::shared_ptr<core::frame_producer>& frame_producer) 
48                 : flash_producer_(frame_producer)
49         {}
50         
51         std::future<std::wstring> add(int layer, std::wstring filename,  bool play_on_load, const std::wstring& label, const std::wstring& data)
52         {
53                 if(filename.size() > 0 && filename[0] == L'/')
54                         filename = filename.substr(1, filename.size()-1);
55
56                 if(boost::filesystem::wpath(filename).extension() == L"")
57                         filename += L".ft";
58                 
59                 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();
60                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking add-command: " << str;
61                 std::vector<std::wstring> params;
62                 params.push_back(std::move(str));
63                 return flash_producer_->call(std::move(params));
64         }
65
66         std::future<std::wstring> remove(int layer)
67         {
68                 auto str = (boost::wformat(L"<invoke name=\"Delete\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();
69                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking remove-command: " << str;
70                 std::vector<std::wstring> params;
71                 params.push_back(std::move(str));
72                 return flash_producer_->call(std::move(params));
73         }
74
75         std::future<std::wstring> play(int layer)
76         {
77                 auto str = (boost::wformat(L"<invoke name=\"Play\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();
78                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking play-command: " << str;
79                 std::vector<std::wstring> params;
80                 params.push_back(std::move(str));
81                 return flash_producer_->call(std::move(params));
82         }
83
84         std::future<std::wstring> stop(int layer, unsigned int)
85         {
86                 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();
87                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking stop-command: " << str;
88                 std::vector<std::wstring> params;
89                 params.push_back(std::move(str));
90                 return flash_producer_->call(std::move(params));
91         }
92
93         std::future<std::wstring> next(int layer)
94         {
95                 auto str = (boost::wformat(L"<invoke name=\"Next\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();
96                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking next-command: " << str;
97                 std::vector<std::wstring> params;
98                 params.push_back(std::move(str));
99                 return flash_producer_->call(std::move(params));
100         }
101
102         std::future<std::wstring> update(int layer, const std::wstring& data)
103         {
104                 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();
105                 CASPAR_LOG(info) << flash_producer_->print() <<" Invoking update-command: " << str;
106                 std::vector<std::wstring> params;
107                 params.push_back(std::move(str));
108                 return flash_producer_->call(std::move(params));
109         }
110
111         std::future<std::wstring> invoke(int layer, const std::wstring& label)
112         {
113                 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();
114                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking invoke-command: " << str;
115                 std::vector<std::wstring> params;
116                 params.push_back(std::move(str));
117                 return flash_producer_->call(std::move(params));
118         }
119
120         std::future<std::wstring> description(int layer)
121         {
122                 auto str = (boost::wformat(L"<invoke name=\"GetDescription\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str();
123                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking description-command: " << str;
124                 std::vector<std::wstring> params;
125                 params.push_back(std::move(str));
126                 return flash_producer_->call(std::move(params));
127         }
128
129         std::future<std::wstring> template_host_info()
130         {
131                 auto str = (boost::wformat(L"<invoke name=\"GetInfo\" returntype=\"xml\"><arguments></arguments></invoke>")).str();
132                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking info-command: " << str;
133                 std::vector<std::wstring> params;
134                 params.push_back(std::move(str));
135                 return flash_producer_->call(std::move(params));
136         }
137                 
138         std::wstring timed_invoke(int layer, const std::wstring& label)
139         {
140                 auto result = invoke(layer, label);
141                 // TODO: because of std::async deferred timed waiting does not work
142                 //if (result.wait_for(std::chrono::seconds(2)) != std::future_status::timeout)
143                         return result.get();
144                 //return L"";
145         }
146         std::wstring timed_description(int layer)
147         {
148                 auto result = description(layer);
149                 // TODO: because of std::async deferred timed waiting does not work
150                 //if (result.wait_for(std::chrono::seconds(2)) != std::future_status::timeout)
151                         return result.get();
152                 //return L"";
153         }
154         std::wstring timed_template_host_info()
155         {
156                 auto result = template_host_info();
157                 // TODO: because of std::async deferred timed waiting does not work
158                 //if (result.wait_for(std::chrono::seconds(2)) != std::future_status::timeout)
159                         return result.get();
160                 //return L"";
161         }
162
163         core::monitor::subject& monitor_output()
164         {
165                 return flash_producer_->monitor_output();
166         }
167 };
168         
169 cg_proxy create_cg_proxy(const spl::shared_ptr<core::video_channel>& video_channel, int render_layer)
170 {       
171         auto flash_producer = spl::make_shared_ptr(video_channel->stage().foreground(render_layer).get());
172
173         try
174         {
175                 if(flash_producer->name() != L"flash")
176                 {
177                         core::diagnostics::scoped_call_context save;
178                         core::diagnostics::call_context::for_thread().video_channel = video_channel->index();
179                         core::diagnostics::call_context::for_thread().layer = render_layer;
180
181                         flash_producer = flash::create_producer(video_channel->frame_factory(), video_channel->video_format_desc(), {});
182                         video_channel->stage().load(render_layer, flash_producer); 
183                         video_channel->stage().play(render_layer);
184                 }
185         }
186         catch(...)
187         {
188                 CASPAR_LOG_CURRENT_EXCEPTION();
189                 throw;
190         }
191
192         return cg_proxy(std::move(flash_producer));
193 }
194
195 spl::shared_ptr<core::frame_producer> create_cg_producer_and_autoplay_file(
196                 const spl::shared_ptr<core::frame_factory> frame_factory, 
197                 const core::video_format_desc& format_desc, 
198                 const std::vector<std::wstring>& params,
199                 const std::wstring& filename) 
200 {
201         if(!boost::filesystem::exists(filename))
202                 return core::frame_producer::empty();
203                 
204         boost::filesystem::path path(filename);
205         path = boost::filesystem::complete(path);
206         auto filename2 = path.wstring();
207
208         auto flash_producer = flash::create_producer(frame_factory, format_desc, {});
209         auto producer = flash_producer;
210         cg_proxy(producer).add(0, filename2, 1);
211
212         return producer;
213 }
214
215 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) 
216 {
217         return create_cg_producer_and_autoplay_file(
218                 frame_factory, format_desc, params, env::media_folder() + L"\\" + params[0] + L".ct");
219 }
220
221 cg_proxy::cg_proxy(const spl::shared_ptr<core::frame_producer>& frame_producer) : impl_(new impl(frame_producer)){}
222 cg_proxy::cg_proxy(cg_proxy&& other) : impl_(std::move(other.impl_)){}
223 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);}
224 void cg_proxy::remove(int layer){impl_->remove(layer);}
225 void cg_proxy::play(int layer){impl_->play(layer);}
226 void cg_proxy::stop(int layer, unsigned int mix_out_duration){impl_->stop(layer, mix_out_duration);}
227 void cg_proxy::next(int layer){impl_->next(layer);}
228 void cg_proxy::update(int layer, const std::wstring& data){impl_->update(layer, data);}
229 std::wstring cg_proxy::invoke(int layer, const std::wstring& label){return impl_->timed_invoke(layer, label);}
230 std::wstring cg_proxy::description(int layer){return impl_->timed_description(layer);}
231 std::wstring cg_proxy::template_host_info(){return impl_->timed_template_host_info();}
232 core::monitor::subject& cg_proxy::monitor_output(){return impl_->monitor_output();}
233
234 }}