]> git.sesse.net Git - casparcg/blob - modules/flash/producer/cg_producer.cpp
da983e7515a7ff1c1b37189f4bba63f1f2380632
[casparcg] / modules / flash / producer / cg_producer.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */\r
20 #include "../StdAfx.h"\r
21 \r
22 #include "cg_producer.h"\r
23 \r
24 #include "flash_producer.h"\r
25 \r
26 #include <common/env.h>\r
27 \r
28 #include <core/mixer/mixer.h>\r
29 \r
30 #include <boost/filesystem.hpp>\r
31 #include <boost/format.hpp>\r
32 #include <boost/algorithm/string.hpp>\r
33                 \r
34 namespace caspar { namespace flash {\r
35         \r
36 struct cg_producer::implementation : boost::noncopyable\r
37 {\r
38         safe_ptr<core::frame_producer> flash_producer_;\r
39 public:\r
40         implementation(const safe_ptr<core::frame_producer>& frame_producer) \r
41                 : flash_producer_(frame_producer)\r
42         {}\r
43         \r
44         void add(int layer, std::wstring filename,  bool play_on_load, const std::wstring& label, const std::wstring& data)\r
45         {\r
46                 if(filename.size() > 0 && filename[0] == L'/')\r
47                         filename = filename.substr(1, filename.size()-1);\r
48 \r
49                 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
50 \r
51                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking add-command:" << str;\r
52                 flash_producer_->param(str);\r
53         }\r
54 \r
55         void remove(int layer)\r
56         {\r
57                 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
58                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking remove-command:" << str;\r
59                 flash_producer_->param(str);\r
60         }\r
61 \r
62         void play(int layer)\r
63         {\r
64                 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
65                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking play-command: " << str;\r
66                 flash_producer_->param(str);\r
67         }\r
68 \r
69         void stop(int layer, unsigned int)\r
70         {\r
71                 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
72                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking stop-command:" << str;\r
73                 flash_producer_->param(str);\r
74         }\r
75 \r
76         void next(int layer)\r
77         {\r
78                 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
79                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking next-command:" << str;\r
80                 flash_producer_->param(str);\r
81         }\r
82 \r
83         void update(int layer, const std::wstring& data)\r
84         {\r
85                 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
86                 CASPAR_LOG(info) << flash_producer_->print() <<" Invoking update-command:" << str;\r
87                 flash_producer_->param(str);\r
88         }\r
89 \r
90         void invoke(int layer, const std::wstring& label)\r
91         {\r
92                 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
93                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking invoke-command: " << str;\r
94                 flash_producer_->param(str);\r
95         }\r
96 \r
97         virtual safe_ptr<core::basic_frame> receive(int hints)\r
98         {\r
99                 return flash_producer_->receive(hints);\r
100         }\r
101 \r
102         virtual safe_ptr<core::basic_frame> last_frame() const\r
103         {\r
104                 return flash_producer_->last_frame();\r
105         }                       \r
106                         \r
107         std::wstring print() const\r
108         {\r
109                 return flash_producer_->print();\r
110         }\r
111 };\r
112         \r
113 safe_ptr<cg_producer> get_default_cg_producer(const safe_ptr<core::video_channel>& video_channel, int render_layer)\r
114 {       \r
115         auto flash_producer = video_channel->stage()->foreground(render_layer);\r
116 \r
117         if(flash_producer->print().find(L"flash[") == std::string::npos) // UGLY hack\r
118         {\r
119                 flash_producer = flash::create_producer(video_channel->mixer(), boost::assign::list_of<std::wstring>());        \r
120                 video_channel->stage()->load(render_layer, flash_producer); \r
121                 video_channel->stage()->play(render_layer);\r
122         }\r
123 \r
124         return make_safe<cg_producer>(flash_producer);\r
125 }\r
126 \r
127 safe_ptr<core::frame_producer> create_ct_producer(const safe_ptr<core::frame_factory> frame_factory, const std::vector<std::wstring>& params) \r
128 {\r
129         std::wstring filename = env::media_folder() + L"\\" + params[0] + L".ct";\r
130         if(!boost::filesystem::exists(filename))\r
131                 return core::frame_producer::empty();\r
132         \r
133         boost::algorithm::replace_all(filename, L"\\", L"/");\r
134         boost::algorithm::replace_all(filename, L"//", L"/");\r
135         boost::algorithm::replace_all(filename, L"///", L"/");\r
136         \r
137         auto flash_producer = flash::create_producer(frame_factory, boost::assign::list_of<std::wstring>());    \r
138         auto producer = make_safe<cg_producer>(flash_producer);\r
139         producer->add(0, filename, 1);\r
140 \r
141         return producer;\r
142 }\r
143 \r
144 cg_producer::cg_producer(const safe_ptr<core::frame_producer>& frame_producer) : impl_(new implementation(frame_producer)){}\r
145 cg_producer::cg_producer(cg_producer&& other) : impl_(std::move(other.impl_)){}\r
146 safe_ptr<core::basic_frame> cg_producer::receive(int hints){return impl_->receive(hints);}\r
147 safe_ptr<core::basic_frame> cg_producer::last_frame() const{return impl_->last_frame();}\r
148 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
149 void cg_producer::remove(int layer){impl_->remove(layer);}\r
150 void cg_producer::play(int layer){impl_->play(layer);}\r
151 void cg_producer::stop(int layer, unsigned int mix_out_duration){impl_->stop(layer, mix_out_duration);}\r
152 void cg_producer::next(int layer){impl_->next(layer);}\r
153 void cg_producer::update(int layer, const std::wstring& data){impl_->update(layer, data);}\r
154 void cg_producer::invoke(int layer, const std::wstring& label){impl_->invoke(layer, label);}\r
155 std::wstring cg_producer::print() const{return impl_->print();}\r
156 \r
157 }}