]> git.sesse.net Git - casparcg/blob - protocol/clk/CLKProtocolStrategy.cpp
5d25b86bd290a76e0b1cd35c4e08854dd2379329
[casparcg] / protocol / clk / CLKProtocolStrategy.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  \r
21 #include "..\stdafx.h"\r
22 \r
23 #include "CLKProtocolStrategy.h"\r
24 \r
25 #include <modules/flash/producer/cg_producer.h>\r
26 \r
27 #include <string>\r
28 #include <sstream>\r
29 #include <algorithm>\r
30 \r
31 namespace caspar { namespace protocol { namespace CLK {\r
32         \r
33 CLKProtocolStrategy::CLKProtocolStrategy(const std::vector<safe_ptr<core::video_channel>>& channels) \r
34         : currentState_(ExpectingNewCommand), bClockLoaded_(false), pChannel_(channels.at(0))\r
35 {}\r
36 \r
37 void CLKProtocolStrategy::Parse(const TCHAR* pData, int charCount, IO::ClientInfoPtr pClientInfo) \r
38 {\r
39         for(int index = 0; index < charCount; ++index) \r
40         {\r
41                 if(currentState_ == ExpectingNewCommand)\r
42                         currentCommandString_.str(TEXT(""));\r
43 \r
44                 TCHAR currentByte = pData[index];\r
45                 if(currentByte < 32)\r
46                         currentCommandString_ << TEXT("<") << (int)currentByte << TEXT(">");\r
47                 else\r
48                         currentCommandString_ << currentByte;\r
49 \r
50                 if(currentByte != 0)\r
51                 {\r
52                         switch(currentState_)\r
53                         {\r
54                                 case ExpectingNewCommand:\r
55                                         if(currentByte == 1) \r
56                                                 currentState_ = ExpectingCommand;                                       \r
57                                         //just throw anything else away\r
58                                         break;\r
59 \r
60                                 case ExpectingCommand:\r
61                                         if(currentByte == 2) \r
62                                         {\r
63                                                 if(!currentCommand_.SetCommand()) \r
64                                                 {\r
65                                                         CASPAR_LOG(error) << "CLK: Failed to interpret command";\r
66                                                         currentState_ = ExpectingNewCommand;\r
67                                                         currentCommand_.Clear();\r
68                                                 }\r
69                                                 else \r
70                                                         currentState_ = ExpectingClockID;                                               \r
71                                         }\r
72                                         else\r
73                                                 currentCommand_.commandString_ += currentByte;\r
74                                         break;\r
75 \r
76                                 case ExpectingClockID:\r
77                                         if(currentByte == 2)\r
78                                                 currentState_ = currentCommand_.NeedsTime() ? ExpectingTime : ExpectingParameter;\r
79                                         else\r
80                                                 currentCommand_.clockID_ = currentByte - TCHAR('0');\r
81                                         break;\r
82 \r
83                                 case ExpectingTime:\r
84                                         if(currentByte == 2)\r
85                                                 currentState_ = ExpectingParameter;\r
86                                         else\r
87                                                 currentCommand_.time_ += currentByte;\r
88                                         break;\r
89 \r
90                                 case ExpectingParameter:\r
91                                         //allocate new parameter\r
92                                         if(currentCommand_.parameters_.size() == 0 || currentByte == 2)\r
93                                                 currentCommand_.parameters_.push_back(std::wstring());\r
94 \r
95                                         //add the character to end end of the last parameter\r
96                                         if(currentByte == TEXT('<'))\r
97                                                 currentCommand_.parameters_[currentCommand_.parameters_.size()-1] += TEXT("&lt;");\r
98                                         else if(currentByte == TEXT('>'))\r
99                                                 currentCommand_.parameters_[currentCommand_.parameters_.size()-1] += TEXT("&gt;");\r
100                                         else if(currentByte == TEXT('\"'))\r
101                                                 currentCommand_.parameters_[currentCommand_.parameters_.size()-1] += TEXT("&quot;");\r
102                                         else\r
103                                                 currentCommand_.parameters_[currentCommand_.parameters_.size()-1] += currentByte;\r
104 \r
105                                         break;\r
106                         }\r
107                 }\r
108                 else \r
109                 {\r
110                         if(currentState_ == ExpectingCommand)\r
111                         {\r
112                                 if(!currentCommand_.SetCommand())\r
113                                         CASPAR_LOG(error) << "CLK: Failed to interpret command";\r
114                         }\r
115 \r
116                         if(currentCommand_.command_ == CLKCommand::CLKReset) \r
117                         {\r
118                                 pChannel_->stage()->clear(flash::cg_producer::DEFAULT_LAYER);\r
119                                 bClockLoaded_ = false;\r
120                                 \r
121                                 CASPAR_LOG(info) << L"CLK: Recieved and executed reset-command";\r
122                         }\r
123                         else if(currentCommand_.command_ != CLKCommand::CLKInvalidCommand)\r
124                         {\r
125                                 if(!bClockLoaded_) \r
126                                 {\r
127                                         flash::get_default_cg_producer(pChannel_)->add(0, TEXT("hawrysklocka/clock.ft"), true, TEXT(""), currentCommand_.GetData());\r
128                                         bClockLoaded_ = true;\r
129                                 }\r
130                                 else \r
131                                         flash::get_default_cg_producer(pChannel_)->update(0, currentCommand_.GetData());\r
132                                 \r
133                                 CASPAR_LOG(debug) << L"CLK: Clockdata sent: " << currentCommand_.GetData();\r
134                                 CASPAR_LOG(debug) << L"CLK: Executed valid command: " << currentCommandString_.str();\r
135                         }\r
136 \r
137                         currentState_ = ExpectingNewCommand;\r
138                         currentCommand_.Clear();\r
139                 }\r
140         }\r
141 }\r
142 \r
143 }       //namespace CLK\r
144 }}      //namespace caspar