]> git.sesse.net Git - casparcg/blobdiff - modules/flash/producer/cg_producer.cpp
#297 Fixed support for flash templates with spaces in the file name.
[casparcg] / modules / flash / producer / cg_producer.cpp
index f5ac48954bec1ce4421facbb9e7c82e2a7f04725..85cd830f23e3c3db93287232c7863267e748325d 100644 (file)
@@ -1,22 +1,24 @@
 /*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+* Copyright 2013 Sveriges Television AB http://casparcg.com/\r
 *\r
-*  This file is part of CasparCG.\r
+* This file is part of CasparCG (www.casparcg.com).\r
 *\r
-*    CasparCG is free software: you can redistribute it and/or modify\r
-*    it under the terms of the GNU General Public License as published by\r
-*    the Free Software Foundation, either version 3 of the License, or\r
-*    (at your option) any later version.\r
+* CasparCG is free software: you can redistribute it and/or modify\r
+* it under the terms of the GNU General Public License as published by\r
+* the Free Software Foundation, either version 3 of the License, or\r
+* (at your option) any later version.\r
 *\r
-*    CasparCG is distributed in the hope that it will be useful,\r
-*    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-*    GNU General Public License for more details.\r
-\r
-*    You should have received a copy of the GNU General Public License\r
-*    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
+* CasparCG is distributed in the hope that it will be useful,\r
+* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+* GNU General Public License for more details.\r
+*\r
+* You should have received a copy of the GNU General Public License\r
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
 *\r
+* Author: Robert Nagy, ronag89@gmail.com\r
 */\r
+\r
 #include "../StdAfx.h"\r
 \r
 #include "cg_producer.h"\r
 \r
 #include <common/env.h>\r
 \r
+#include <core/parameters/parameters.h>\r
+#include <core/mixer/mixer.h>\r
+\r
 #include <boost/filesystem.hpp>\r
 #include <boost/format.hpp>\r
+#include <boost/algorithm/string.hpp>\r
+#include <boost/regex.hpp>\r
+#include <boost/property_tree/ptree.hpp>\r
                \r
-namespace caspar {\r
+namespace caspar { namespace flash {\r
        \r
 struct cg_producer::implementation : boost::noncopyable\r
 {\r
-       safe_ptr<core::frame_factory> frame_factory_;\r
        safe_ptr<core::frame_producer> flash_producer_;\r
 public:\r
-       implementation(const safe_ptr<core::frame_factory>& frame_factory) \r
-               : frame_factory_(frame_factory)\r
-               , flash_producer_(create_flash_producer(frame_factory_, boost::assign::list_of(env::template_host())))\r
+       implementation(const safe_ptr<core::frame_producer>& frame_producer) \r
+               : flash_producer_(frame_producer)\r
        {}\r
        \r
-       void add(int layer, const std::wstring& filename,  bool play_on_load, const std::wstring& label, const std::wstring& data)\r
+       boost::unique_future<std::wstring> add(int layer, std::wstring filename,  bool play_on_load, const std::wstring& label, const std::wstring& data)\r
        {\r
-               CASPAR_LOG(info) << flash_producer_->print() << " Invoking add-command";\r
-               flash_producer_->param((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?TEXT("<true/>"):TEXT("<false/>")) % label % data).str());\r
+               if(filename.size() > 0 && filename[0] == L'/')\r
+                       filename = filename.substr(1, filename.size()-1);\r
+\r
+               if(boost::filesystem::wpath(filename).extension() == L"")\r
+                       filename += L".ft";\r
+               \r
+               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?TEXT("<true/>"):TEXT("<false/>")) % label % data).str();\r
+\r
+               CASPAR_LOG(info) << flash_producer_->print() << " Invoking add-command: " << str;\r
+               return flash_producer_->call(str);\r
        }\r
 \r
-       void remove(int layer)\r
+       boost::unique_future<std::wstring> remove(int layer)\r
        {\r
-               CASPAR_LOG(info) << flash_producer_->print() << " Invoking remove-command";\r
-               flash_producer_->param((boost::wformat(L"<invoke name=\"Delete\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str());\r
+               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
+               CASPAR_LOG(info) << flash_producer_->print() << " Invoking remove-command: " << str;\r
+               return flash_producer_->call(str);\r
        }\r
 \r
-       void play(int layer)\r
+       boost::unique_future<std::wstring> play(int layer)\r
        {\r
-               CASPAR_LOG(info) << flash_producer_->print() << " Invoking play-command";\r
-               flash_producer_->param((boost::wformat(L"<invoke name=\"Play\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str());\r
+               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
+               CASPAR_LOG(info) << flash_producer_->print() << " Invoking play-command: " << str;\r
+               return flash_producer_->call(str);\r
        }\r
 \r
-       void stop(int layer, unsigned int)\r
+       boost::unique_future<std::wstring> stop(int layer, unsigned int)\r
        {\r
-               CASPAR_LOG(info) << flash_producer_->print() << " Invoking stop-command";\r
-               flash_producer_->param((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
+               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
+               CASPAR_LOG(info) << flash_producer_->print() << " Invoking stop-command: " << str;\r
+               return flash_producer_->call(str);\r
        }\r
 \r
-       void next(int layer)\r
+       boost::unique_future<std::wstring> next(int layer)\r
        {\r
-               CASPAR_LOG(info) << flash_producer_->print() << " Invoking next-command";\r
-               flash_producer_->param((boost::wformat(L"<invoke name=\"Next\" returntype=\"xml\"><arguments><array><property id=\"0\"><number>%1%</number></property></array></arguments></invoke>") % layer).str());\r
+               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
+               CASPAR_LOG(info) << flash_producer_->print() << " Invoking next-command: " << str;\r
+               return flash_producer_->call(str);\r
        }\r
 \r
-       void update(int layer, const std::wstring& data)\r
+       boost::unique_future<std::wstring> update(int layer, const std::wstring& data)\r
        {\r
-               CASPAR_LOG(info) << flash_producer_->print() <<" Invoking update-command";\r
-               flash_producer_->param((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
+               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
+               CASPAR_LOG(info) << flash_producer_->print() <<" Invoking update-command: " << str;\r
+               return flash_producer_->call(str);\r
        }\r
 \r
-       void invoke(int layer, const std::wstring& label)\r
+       boost::unique_future<std::wstring> invoke(int layer, const std::wstring& label)\r
        {\r
-               CASPAR_LOG(info) << flash_producer_->print() << " Invoking invoke-command";\r
-               flash_producer_->param((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
+               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
+               CASPAR_LOG(info) << flash_producer_->print() << " Invoking invoke-command: " << str;\r
+               return flash_producer_->call(str);\r
+       }\r
+\r
+       boost::unique_future<std::wstring> description(int layer)\r
+       {\r
+               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
+               CASPAR_LOG(info) << flash_producer_->print() << " Invoking description-command: " << str;\r
+               return flash_producer_->call(str);\r
+       }\r
+\r
+       boost::unique_future<std::wstring> template_host_info()\r
+       {\r
+               auto str = (boost::wformat(L"<invoke name=\"GetInfo\" returntype=\"xml\"><arguments></arguments></invoke>")).str();\r
+               CASPAR_LOG(info) << flash_producer_->print() << " Invoking info-command: " << str;\r
+               return flash_producer_->call(str);\r
+       }\r
+\r
+       boost::unique_future<std::wstring> call(const std::wstring& str)\r
+       {               \r
+               static const boost::wregex add_exp                      (L"ADD (?<LAYER>\\d+) \"(?<FILENAME>[^\"]*)\" (?<PLAY_ON_LOAD>\\d)( (?<DATA>.*))?", boost::regex::perl|boost::regex::icase);\r
+               static const boost::wregex remove_exp           (L"REMOVE (?<LAYER>\\d+)", boost::regex::perl|boost::regex::icase);\r
+               static const boost::wregex play_exp                     (L"PLAY (?<LAYER>\\d+)", boost::regex::perl|boost::regex::icase);\r
+               static const boost::wregex stop_exp                     (L"STOP (?<LAYER>\\d+)", boost::regex::perl|boost::regex::icase);\r
+               static const boost::wregex next_exp                     (L"NEXT (?<LAYER>\\d+)", boost::regex::perl|boost::regex::icase);\r
+               static const boost::wregex update_exp           (L"UPDATE (?<LAYER>\\d+) (?<DATA>.+)", boost::regex::perl|boost::regex::icase);\r
+               static const boost::wregex invoke_exp           (L"INVOKE (?<LAYER>\\d+) (?<LABEL>.+)", boost::regex::perl|boost::regex::icase);\r
+               static const boost::wregex description_exp      (L"INFO (?<LAYER>\\d+)", boost::regex::perl|boost::regex::icase);\r
+               static const boost::wregex info_exp                     (L"INFO", boost::regex::perl|boost::regex::icase);\r
+               \r
+               boost::wsmatch what;\r
+               if(boost::regex_match(str, what, add_exp))\r
+                       return add(\r
+                                       boost::lexical_cast<int>(what["LAYER"].str()),\r
+                                       what["FILENAME"].str(),\r
+                                       boost::lexical_cast<bool>(what["PLAY_ON_LOAD"].str()),\r
+                                       L"",\r
+                                       what["DATA"].str());\r
+               else if(boost::regex_match(str, what, remove_exp))\r
+                       return remove(boost::lexical_cast<int>(what["LAYER"].str()));\r
+               else if(boost::regex_match(str, what, play_exp))\r
+                       return play(boost::lexical_cast<int>(what["LAYER"].str()));\r
+               else if(boost::regex_match(str, what, stop_exp))\r
+                       return stop(boost::lexical_cast<int>(what["LAYER"].str()), 0);\r
+               else if(boost::regex_match(str, what, next_exp))\r
+                       return next(boost::lexical_cast<int>(what["LAYER"].str()));\r
+               else if(boost::regex_match(str, what, update_exp))\r
+                       return update(boost::lexical_cast<int>(what["LAYER"].str()), what["DATA"].str());\r
+               else if(boost::regex_match(str, what, invoke_exp))\r
+                       return invoke(boost::lexical_cast<int>(what["LAYER"].str()), what["LABEL"].str());\r
+               else if(boost::regex_match(str, what, description_exp))\r
+                       return description(boost::lexical_cast<int>(what["LAYER"].str()));\r
+               else if(boost::regex_match(str, what, info_exp))\r
+                       return template_host_info(); \r
+\r
+               return flash_producer_->call(str);\r
        }\r
 \r
-       safe_ptr<core::basic_frame> receive()\r
+       safe_ptr<core::basic_frame> receive(int hints)\r
        {\r
-               return flash_producer_->receive();\r
+               return flash_producer_->receive(hints);\r
        }\r
+\r
+       safe_ptr<core::basic_frame> last_frame() const\r
+       {\r
+               return flash_producer_->last_frame();\r
+       }               \r
                        \r
        std::wstring print() const\r
        {\r
                return flash_producer_->print();\r
        }\r
+\r
+       boost::property_tree::wptree info() const\r
+       {\r
+               boost::property_tree::wptree info;\r
+               info.add(L"type", L"cg-producer");\r
+               return info;\r
+       }\r
+\r
+       std::wstring timed_invoke(int layer, const std::wstring& label)\r
+       {\r
+               auto result = invoke(layer, label);\r
+               if(result.timed_wait(boost::posix_time::seconds(2)))\r
+                       return result.get();\r
+               return L"";\r
+       }\r
+       std::wstring timed_description(int layer)\r
+       {\r
+               auto result = description(layer);\r
+               if(result.timed_wait(boost::posix_time::seconds(2)))\r
+                       return result.get();\r
+               return L"";\r
+       }\r
+       std::wstring timed_template_host_info()\r
+       {\r
+               auto result = template_host_info();\r
+               if(result.timed_wait(boost::posix_time::seconds(2)))\r
+                       return result.get();\r
+               return L"";\r
+       }\r
+\r
+       core::monitor::subject& monitor_output()\r
+       {\r
+               return flash_producer_->monitor_output();\r
+       }\r
 };\r
-       \r
-safe_ptr<cg_producer> get_default_cg_producer(const safe_ptr<core::channel>& channel, int render_layer)\r
-{      \r
+\r
+void with_default_cg_producer(\r
+               std::function<void (safe_ptr<cg_producer>)> command,\r
+               const safe_ptr<core::video_channel>& video_channel,\r
+               bool expect_existing,\r
+               int layer_index)\r
+{\r
+       auto flash_producer = video_channel->stage()->foreground(layer_index).get();\r
+       bool was_created = false;\r
+\r
        try\r
        {\r
-               return dynamic_pointer_cast<cg_producer>(channel->producer()->foreground(render_layer).get());\r
+               if(flash_producer->print().find(L"flash[") == std::string::npos) // UGLY hack\r
+               {\r
+                       if (expect_existing)\r
+                               BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(\r
+                                               "No flash producer on layer "\r
+                                               + boost::lexical_cast<std::string>(layer_index)));\r
+\r
+                       flash_producer = flash::create_producer(video_channel->mixer(), boost::assign::list_of<std::wstring>());\r
+               }\r
+\r
+               if (expect_existing && flash_producer->call(L"?").get() == L"0")\r
+                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(\r
+                                       "No flash player on layer "\r
+                                       + boost::lexical_cast<std::string>(layer_index)));\r
+\r
+               was_created = true;\r
        }\r
-       catch(std::bad_cast&)\r
+       catch(...)\r
        {\r
-               safe_ptr<core::frame_factory> factory = channel->mixer();\r
-               auto producer = make_safe<cg_producer>(factory);                \r
-               channel->producer()->load(render_layer, producer, true); \r
-               channel->producer()->play(render_layer);\r
-               return producer;\r
+               CASPAR_LOG_CURRENT_EXCEPTION();\r
+               throw;\r
+       }\r
+\r
+       command(make_safe<cg_producer>(flash_producer));\r
+\r
+       if (was_created)\r
+       {\r
+               video_channel->stage()->load(layer_index, flash_producer); \r
+               video_channel->stage()->play(layer_index);\r
        }\r
 }\r
+       \r
+safe_ptr<cg_producer> get_default_cg_producer(\r
+               const safe_ptr<core::video_channel>& video_channel,\r
+               bool expect_existing,\r
+               int render_layer)\r
+{      \r
+       std::shared_ptr<cg_producer> producer;\r
+\r
+       with_default_cg_producer(\r
+                       [&producer](safe_ptr<cg_producer> p)\r
+                       {\r
+                               producer = p;\r
+                       }, video_channel, expect_existing, render_layer);\r
+\r
+       return make_safe_ptr(producer);\r
+}\r
 \r
-safe_ptr<core::frame_producer> create_ct_producer(const safe_ptr<core::frame_factory> frame_factory, const std::vector<std::wstring>& params) \r
+safe_ptr<core::frame_producer> create_cg_producer_and_autoplay_file(\r
+               const safe_ptr<core::frame_factory>& frame_factory, \r
+               const core::parameters& params,\r
+               const std::wstring& filename) \r
 {\r
-       std::wstring filename = env::media_folder() + L"\\" + params[0] + L".ct";\r
        if(!boost::filesystem::exists(filename))\r
                return core::frame_producer::empty();\r
+               \r
+       boost::filesystem2::wpath path(filename);\r
+       path = boost::filesystem2::complete(path);\r
+       auto filename2 = path.file_string();\r
 \r
-       auto producer = make_safe<cg_producer>(frame_factory);\r
-       producer->add(0, filename, 1);\r
+       auto flash_producer = flash::create_producer(frame_factory, boost::assign::list_of<std::wstring>());    \r
+       auto producer = make_safe<cg_producer>(flash_producer);\r
+       producer->add(0, filename2, 1);\r
 \r
        return producer;\r
 }\r
 \r
-cg_producer::cg_producer(const safe_ptr<core::frame_factory>& frame_factory) : impl_(new implementation(frame_factory)){}\r
+safe_ptr<core::frame_producer> create_ct_producer(\r
+               const safe_ptr<core::frame_factory>& frame_factory,\r
+               const core::parameters& params) \r
+{\r
+       return create_cg_producer_and_autoplay_file(frame_factory, params, env::media_folder() + L"\\" + params.at_original(0) + L".ct");\r
+}\r
+\r
+safe_ptr<core::frame_producer> create_cg_producer(\r
+               const safe_ptr<core::frame_factory>& frame_factory,\r
+               const core::parameters& params) \r
+{\r
+       if(params.empty() || params.at(0) != L"[CG]")\r
+               return core::frame_producer::empty();\r
+\r
+       return make_safe<cg_producer>(flash::create_producer(frame_factory, boost::assign::list_of<std::wstring>()));   \r
+}\r
+\r
+cg_producer::cg_producer(const safe_ptr<core::frame_producer>& frame_producer) : impl_(new implementation(frame_producer)){}\r
 cg_producer::cg_producer(cg_producer&& other) : impl_(std::move(other.impl_)){}\r
-safe_ptr<core::basic_frame> cg_producer::receive(){return impl_->receive();}\r
-void cg_producer::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
+safe_ptr<core::basic_frame> cg_producer::receive(int hints){return impl_->receive(hints);}\r
+safe_ptr<core::basic_frame> cg_producer::last_frame() const{return impl_->last_frame();}\r
+void cg_producer::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).wait();}\r
 void cg_producer::remove(int layer){impl_->remove(layer);}\r
 void cg_producer::play(int layer){impl_->play(layer);}\r
 void cg_producer::stop(int layer, unsigned int mix_out_duration){impl_->stop(layer, mix_out_duration);}\r
 void cg_producer::next(int layer){impl_->next(layer);}\r
 void cg_producer::update(int layer, const std::wstring& data){impl_->update(layer, data);}\r
-void cg_producer::invoke(int layer, const std::wstring& label){impl_->invoke(layer, label);}\r
 std::wstring cg_producer::print() const{return impl_->print();}\r
-\r
-}
\ No newline at end of file
+boost::unique_future<std::wstring> cg_producer::call(const std::wstring& str){return impl_->call(str);}\r
+std::wstring cg_producer::invoke(int layer, const std::wstring& label){return impl_->timed_invoke(layer, label);}\r
+std::wstring cg_producer::description(int layer){return impl_->timed_description(layer);}\r
+std::wstring cg_producer::template_host_info(){return impl_->timed_template_host_info();}\r
+boost::property_tree::wptree cg_producer::info() const{return impl_->info();}\r
+core::monitor::subject& cg_producer::monitor_output(){return impl_->monitor_output();}\r
+}}
\ No newline at end of file