]> git.sesse.net Git - casparcg/blob - protocol/cii/CIICommandsImpl.cpp
[CHANGELOG] Updated for release of 2.1.0 Beta 1
[casparcg] / protocol / cii / CIICommandsImpl.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: Nicklas P Andersson
20 */
21
22  
23 #include "../StdAfx.h"
24
25 #pragma warning (disable: 4244)
26
27 #include "CIIProtocolStrategy.h"
28 #include "CIICommandsImpl.h"
29
30 #include <core/producer/cg_proxy.h>
31
32 #include <sstream>
33 #include <algorithm>
34
35 #include <boost/locale.hpp>
36 #include <boost/algorithm/string/trim.hpp>
37 #include <boost/algorithm/string/split.hpp>
38 #include <boost/lexical_cast.hpp>
39
40 namespace caspar { namespace protocol { namespace cii {
41
42 ////////
43 // MediaCommand
44 void MediaCommand::Setup(const std::vector<std::wstring>& parameters) 
45 {
46         graphicProfile_ = parameters[1].substr(2);
47 }
48
49 void MediaCommand::Execute() 
50 {
51         pCIIStrategy_->SetProfile(graphicProfile_);
52 }
53
54
55 ////////
56 // WriteCommand
57 void WriteCommand::Setup(const std::vector<std::wstring>& parameters)
58 {
59         try 
60         {
61                 if(parameters.size() > 2)
62                 {
63                         targetName_ = parameters[1];
64                         templateName_ = parameters[2];
65
66                         std::wstringstream dataStream;
67
68                         dataStream << L"<templateData>";
69
70                         std::vector<std::wstring>::size_type end = parameters.size();
71                         for(std::vector<std::wstring>::size_type i = 3; i < end; ++i) 
72                                 dataStream << L"<componentData id=\"field" << i-2 << L"\"><data id=\"text\" value=\"" << parameters[i] << L"\" /></componentData>";
73
74                         dataStream << L"</templateData>";
75                         xmlData_ = dataStream.str();
76                 }
77         }
78         catch(std::exception) {
79         }
80 }
81
82 void WriteCommand::Execute() 
83 {
84         pCIIStrategy_->WriteTemplateData(templateName_, targetName_, xmlData_);
85 }
86
87
88 //////////
89 // ImagestoreCommand
90 void ImagestoreCommand::Setup(const std::vector<std::wstring>& parameters) 
91 {
92         if(parameters[1] == L"7" && parameters.size() > 2)
93                 titleName_ = parameters[2].substr(0, 4);        
94 }
95
96 void ImagestoreCommand::Execute()
97 {
98         pCIIStrategy_->DisplayTemplate(titleName_);
99 }
100
101
102 //////////
103 // MiscellaneousCommand
104 void MiscellaneousCommand::Setup(const std::vector<std::wstring>& parameters)
105 {
106         //HAWRYS:       V\5\3\1\1\namn.tga\1
107         //                      Display still
108         if((parameters.size() > 5) && parameters[1] == L"5" && parameters[2] == L"3")
109         {
110                 filename_ = parameters[5];
111                 filename_ = filename_.substr(0, filename_.find_last_of(L'.'));
112                 filename_.append(L".ft");
113                 state_ = 0;
114                 return;
115         }
116         
117         //NEPTUNE:      V\5\13\1\X\Template\0\TabField1\TabField2...
118         //                      Add Template to layer X in the active templatehost
119         if((parameters.size() > 5) && parameters[1] == L"5" && parameters[2] == L"13")
120         {
121                 layer_ = boost::lexical_cast<int>(parameters[4]);
122                 filename_ = parameters[5];
123                 if(filename_.find(L"PK/") == std::wstring::npos && filename_.find(L"PK\\") == std::wstring::npos)
124                         filename_ = L"PK/" + filename_;
125
126                 state_ = 1;
127                 if(parameters.size() > 7) {
128                         std::wstringstream dataStream;
129
130                         dataStream << L"<templateData>";
131                         std::vector<std::wstring>::size_type end = parameters.size();
132                         for(std::vector<std::wstring>::size_type i = 7; i < end; ++i) {
133                                 dataStream << L"<componentData id=\"f" << i-7 << L"\"><data id=\"text\" value=\"" << parameters[i] << L"\" /></componentData>";
134                         }
135                         dataStream << L"</templateData>";
136
137                         xmlData_ = dataStream.str();
138                 }
139         }
140
141         // VIDEO MODE V\5\14\MODE
142         if((parameters.size() > 3) && parameters[1] == L"5" && parameters[2] == L"14")
143         {
144                 std::wstring value = parameters[3];
145                 std::transform(value.begin(), value.end(), value.begin(), toupper);
146
147                 this->pCIIStrategy_->GetChannel()->video_format_desc(core::video_format_desc(value));
148         }
149 }
150
151 void MiscellaneousCommand::Execute() 
152 {
153         if(state_ == 0)
154                 pCIIStrategy_->DisplayMediaFile(filename_);     
155
156         //TODO: Need to be checked for validity
157         else if(state_ == 1)            
158         {
159                 // HACK fix. The data sent is UTF8, however the protocol is implemented for ISO-8859-1. Instead of doing risky changes we simply convert into proper encoding when leaving protocol code.
160                 auto xmlData2 = boost::locale::conv::utf_to_utf<wchar_t, char>(std::string(xmlData_.begin(), xmlData_.end()));
161                 auto proxy = pCIIStrategy_->get_cg_registry()->get_or_create_proxy(
162                                 pCIIStrategy_->GetChannel(),
163                                 pCIIStrategy_->get_dependencies(),
164                                 core::cg_proxy::DEFAULT_LAYER,
165                                 filename_);
166                 proxy->add(layer_, filename_, false, L"", xmlData2);
167         }
168 }
169
170
171 /////////
172 // KeydataCommand
173 void KeydataCommand::Execute() 
174 {
175         auto proxy = pCIIStrategy_->get_cg_registry()->get_proxy(
176                 pCIIStrategy_->GetChannel(), casparLayer_);
177
178         if (state_ == 0)
179                 pCIIStrategy_->DisplayTemplate(titleName_);
180
181         //TODO: Need to be checked for validity
182         else if(state_ == 1)
183                 proxy->stop(layer_, 0);
184         else if(state_ == 2)
185                 pCIIStrategy_->GetChannel()->stage().clear();
186         else if(state_ == 3)
187                 proxy->play(layer_);
188 }
189
190 void KeydataCommand::Setup(const std::vector<std::wstring>& parameters) {
191         //HAWRYS:       Y\<205><247><202><196><192><192><200><248>
192         //parameter[1] looks like this: "=g:XXXXh" where XXXX is the name that we want
193         if(parameters[1].size() > 6) 
194         {
195                 titleName_.resize(4);
196                 for(int i=0;i<4;++i)
197                 {
198                         if(parameters[1][i+3] < 176) {
199                                 titleName_ = L"";
200                                 break;
201                         }
202                         titleName_[i] = parameters[1][i+3]-144;
203                 }
204                 state_ = 0;
205         }
206
207         casparLayer_ = core::cg_proxy::DEFAULT_LAYER;
208         if(parameters.size() > 2)
209         {
210                 //The layer parameter now supports casparlayers.
211                 //the format is [CasparLayer]-[FlashLayer]
212                 std::wstring str = boost::trim_copy(parameters[2]);
213                 std::vector<std::wstring> split;
214                 boost::split(split, str, boost::is_any_of("-"));
215                 
216                 try
217                 {
218                         casparLayer_ = boost::lexical_cast<int>(split[0]);
219
220                         if(split.size() > 1)
221                                 layer_ = boost::lexical_cast<int>(split[1]);
222                 }
223                 catch(...)
224                 { 
225                         casparLayer_ = core::cg_proxy::DEFAULT_LAYER;
226                         layer_ = 0;
227                 }
228         }
229
230
231         if(parameters[1].at(0) == 27)   //NEPTUNE:      Y\<27>\X                        Stop layer X.
232                 state_ = 1;
233         else if(static_cast<unsigned char>(parameters[1].at(1)) == 190) //NEPTUNE:      Y\<254>                 Clear Canvas. 
234                 state_ = 2;
235         else if(static_cast<unsigned char>(parameters[1].at(1)) == 149) //NEPTUNE:      Y\<213><243>\X  Play layer X. 
236                 state_ = 3;                                                                                                     //UPDATE 2011-05-09: These char-codes are aparently not valid after converting to wide-chars
237                                                                                                                                         //the correct sequence is <195><149><195><179> 
238                 
239 }
240
241 }}}