]> git.sesse.net Git - casparcg/blob - modules/flash/producer/cg_producer.cpp
2.0.0.2: Added copyright notice to all files.
[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 <common/env.h>\r
23 \r
24 #include "cg_producer.h"\r
25 \r
26 #include "flash_producer.h"\r
27 \r
28 #include <boost/filesystem.hpp>\r
29 #include <boost/format.hpp>\r
30 \r
31 #include <boost/assign.hpp>\r
32 \r
33 using boost::format;\r
34 using boost::io::group;\r
35                 \r
36 namespace caspar {\r
37         \r
38 struct cg_producer::implementation : boost::noncopyable\r
39 {\r
40         safe_ptr<core::frame_factory> frame_factory_;\r
41         safe_ptr<core::frame_producer> flash_producer_;\r
42 public:\r
43         implementation(const safe_ptr<core::frame_factory>& frame_factory) \r
44                 : frame_factory_(frame_factory)\r
45                 , flash_producer_(create_flash_producer(frame_factory_, boost::assign::list_of(env::template_host())))\r
46         {}\r
47         \r
48         void add(int layer, const std::wstring& filename,  bool play_on_load, const std::wstring& label, const std::wstring& data)\r
49         {\r
50                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking add-command";\r
51                 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
52         }\r
53 \r
54         void remove(int layer)\r
55         {\r
56                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking remove-command";\r
57                 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
58         }\r
59 \r
60         void play(int layer)\r
61         {\r
62                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking play-command";\r
63                 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
64         }\r
65 \r
66         void stop(int layer, unsigned int)\r
67         {\r
68                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking stop-command";\r
69                 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
70         }\r
71 \r
72         void next(int layer)\r
73         {\r
74                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking next-command";\r
75                 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
76         }\r
77 \r
78         void update(int layer, const std::wstring& data)\r
79         {\r
80                 CASPAR_LOG(info) << flash_producer_->print() <<" Invoking update-command";\r
81                 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
82         }\r
83 \r
84         void invoke(int layer, const std::wstring& label)\r
85         {\r
86                 CASPAR_LOG(info) << flash_producer_->print() << " Invoking invoke-command";\r
87                 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
88         }\r
89 \r
90         safe_ptr<core::basic_frame> receive()\r
91         {\r
92                 return flash_producer_->receive();\r
93         }\r
94                         \r
95         std::wstring print() const\r
96         {\r
97                 return flash_producer_->print();\r
98         }\r
99 };\r
100         \r
101 safe_ptr<cg_producer> get_default_cg_producer(const safe_ptr<core::channel>& channel, int render_layer)\r
102 {       \r
103         try\r
104         {\r
105                 return dynamic_pointer_cast<cg_producer>(channel->producer()->foreground(render_layer).get());\r
106         }\r
107         catch(std::bad_cast&)\r
108         {\r
109                 safe_ptr<core::frame_factory> factory = channel->mixer();\r
110                 auto producer = make_safe<cg_producer>(factory);                \r
111                 channel->producer()->load(render_layer, producer, true); \r
112                 channel->producer()->play(render_layer);\r
113                 return producer;\r
114         }\r
115 }\r
116 \r
117 safe_ptr<core::frame_producer> create_ct_producer(const safe_ptr<core::frame_factory> frame_factory, const std::vector<std::wstring>& params) \r
118 {\r
119         std::wstring filename = env::media_folder() + L"\\" + params[0] + L".ct";\r
120         if(!boost::filesystem::exists(filename))\r
121                 return core::frame_producer::empty();\r
122 \r
123         auto producer = make_safe<cg_producer>(frame_factory);\r
124         producer->add(0, filename, 1);\r
125 \r
126         return producer;\r
127 }\r
128 \r
129 cg_producer::cg_producer(const safe_ptr<core::frame_factory>& frame_factory) : impl_(new implementation(frame_factory)){}\r
130 cg_producer::cg_producer(cg_producer&& other) : impl_(std::move(other.impl_)){}\r
131 safe_ptr<core::basic_frame> cg_producer::receive(){return impl_->receive();}\r
132 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
133 void cg_producer::remove(int layer){impl_->remove(layer);}\r
134 void cg_producer::play(int layer){impl_->play(layer);}\r
135 void cg_producer::stop(int layer, unsigned int mix_out_duration){impl_->stop(layer, mix_out_duration);}\r
136 void cg_producer::next(int layer){impl_->next(layer);}\r
137 void cg_producer::update(int layer, const std::wstring& data){impl_->update(layer, data);}\r
138 void cg_producer::invoke(int layer, const std::wstring& label){impl_->invoke(layer, label);}\r
139 std::wstring cg_producer::print() const{return impl_->print();}\r
140 \r
141 }