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
22 #include "../StdAfx.h"
26 #include "flash_producer.h"
28 #include <common/env.h>
30 #include <core/mixer/mixer.h>
31 #include <core/diagnostics/call_context.h>
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>
41 namespace caspar { namespace flash {
43 struct cg_proxy::impl : boost::noncopyable
45 spl::shared_ptr<core::frame_producer> flash_producer_;
47 impl(const spl::shared_ptr<core::frame_producer>& frame_producer)
48 : flash_producer_(frame_producer)
51 std::future<std::wstring> add(int layer, std::wstring filename, bool play_on_load, const std::wstring& label, const std::wstring& data)
53 if(filename.size() > 0 && filename[0] == L'/')
54 filename = filename.substr(1, filename.size()-1);
56 if(boost::filesystem::wpath(filename).extension() == L"")
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));
66 std::future<std::wstring> remove(int layer)
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));
75 std::future<std::wstring> play(int layer)
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));
84 std::future<std::wstring> stop(int layer, unsigned int)
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));
93 std::future<std::wstring> next(int layer)
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));
102 std::future<std::wstring> update(int layer, const std::wstring& data)
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));
111 std::future<std::wstring> invoke(int layer, const std::wstring& label)
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));
120 std::future<std::wstring> description(int layer)
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));
129 std::future<std::wstring> template_host_info()
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));
138 std::wstring timed_invoke(int layer, const std::wstring& label)
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)
146 std::wstring timed_description(int layer)
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)
154 std::wstring timed_template_host_info()
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)
163 core::monitor::subject& monitor_output()
165 return flash_producer_->monitor_output();
169 cg_proxy create_cg_proxy(const spl::shared_ptr<core::video_channel>& video_channel, int render_layer)
171 auto flash_producer = spl::make_shared_ptr(video_channel->stage().foreground(render_layer).get());
175 if(flash_producer->name() != L"flash")
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;
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);
188 CASPAR_LOG_CURRENT_EXCEPTION();
192 return cg_proxy(std::move(flash_producer));
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)
201 if(!boost::filesystem::exists(filename))
202 return core::frame_producer::empty();
204 boost::filesystem::path path(filename);
205 path = boost::filesystem::complete(path);
206 auto filename2 = path.wstring();
208 auto flash_producer = flash::create_producer(frame_factory, format_desc, {});
209 auto producer = flash_producer;
210 cg_proxy(producer).add(0, filename2, 1);
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)
217 return create_cg_producer_and_autoplay_file(
218 frame_factory, format_desc, params, env::media_folder() + L"\\" + params[0] + L".ct");
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();}