RDK Documentation (Open Sourced RDK Components)
CCDataController.cpp
Go to the documentation of this file.
1 /*
2  * If not stated otherwise in this file or this component's license file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2018 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 /**
21  * @file CCDataController.cpp
22  *
23  * @brief Impl of subtec communication layer
24  *
25  */
26 
27 #include <cstring>
28 #include <unordered_map>
29 
31 
32 #include "priv_aamp.h"
33 
34 
35 namespace subtecConnector
36 {
37 
38 namespace
39 {
40  gsw_CcAttributes createDefaultAttributes()
41  {
42  gsw_CcAttributes attribs;
43  memset(&attribs, 0, sizeof(struct gsw_CcAttributes));
44  attribs.charBgColor.rgb = GSW_CC_EMBEDDED_COLOR;
45  attribs.charFgColor.rgb = GSW_CC_EMBEDDED_COLOR;
46  attribs.winColor.rgb = GSW_CC_EMBEDDED_COLOR;
47  attribs.charBgOpacity = GSW_CC_OPACITY_EMBEDDED;
48  attribs.charFgOpacity = GSW_CC_OPACITY_EMBEDDED;
49  attribs.winOpacity = GSW_CC_OPACITY_EMBEDDED;
50  attribs.fontSize = GSW_CC_FONT_SIZE_EMBEDDED;
51 
52  std::memcpy(attribs.fontStyle , GSW_CC_FONT_STYLE_EMBEDDED, sizeof(GSW_CC_FONT_STYLE_EMBEDDED));
53 
54  attribs.fontItalic = GSW_CC_TEXT_STYLE_EMBEDDED_TEXT;
55  attribs.fontUnderline = GSW_CC_TEXT_STYLE_EMBEDDED_TEXT;
56  attribs.borderType = GSW_CC_BORDER_TYPE_EMBEDDED;
57  attribs.borderColor.rgb = GSW_CC_EMBEDDED_COLOR;
58  attribs.edgeType = GSW_CC_EDGE_TYPE_EMBEDDED;
59  attribs.edgeColor.rgb = GSW_CC_EMBEDDED_COLOR;
60  return attribs;
61  }
62 }
63 
64 
65 CCDataController* CCDataController::Instance()
66 {
67  static CCDataController instance;
68  return &instance;
69 }
70 
71 void CCDataController::closedCaptionDataCb (int decoderIndex, VL_CC_DATA_TYPE eType, unsigned char* ccData,
72  unsigned dataLength, int sequenceNumber, long long localPts)
73 {
74  channel.SendDataPacketWithPTS(localPts, ccData, dataLength);
75 }
76 
77 void CCDataController::closedCaptionDecodeCb(int decoderIndex, int event)
78 {
79  logprintf("closedCaptionDecodeCb decoderIndex = %d, event = %d", decoderIndex, event);
80 }
81 
82 void CCDataController::sendMute()
83 {
84  channel.SendMutePacket();
85 }
86 
87 void CCDataController::sendUnmute()
88 {
89  channel.SendUnmutePacket();
90 }
91 
92 void CCDataController::sendPause()
93 {
94  channel.SendPausePacket();
95 }
96 
97 void CCDataController::sendResume()
98 {
99  channel.SendResumePacket();
100 }
101 
102 void CCDataController::sendResetChannelPacket()
103 {
104  channel.SendResetChannelPacket();
105 }
106 
107 CCDataController::CCDataController()
108  : channel{}
109  , currentAttributes{createDefaultAttributes()}
110 {
111 }
112 
113 void CCDataController::ccSetDigitalChannel(unsigned int channelId)
114 {
115  channel.SendActiveTypePacket(ClosedCaptionsActiveTypePacket::CEA::type_708, channelId);
116 }
117 
118 void CCDataController::ccSetAnalogChannel(unsigned int channelId)
119 {
120  channel.SendActiveTypePacket(ClosedCaptionsActiveTypePacket::CEA::type_608, channelId);
121 }
122 
123 void CCDataController::ccGetAttributes(gsw_CcAttributes * attrib, gsw_CcType /*ccType*/)
124 {
125  std::memcpy(attrib, &currentAttributes, sizeof(gsw_CcAttributes));
126 }
127 
128 namespace
129 {
130  uint32_t getValue(const gsw_CcColor& attrib)
131  {
132  return static_cast<uint32_t>(attrib.rgb);
133  }
134  uint32_t getValue(const gsw_CcOpacity& attrib)
135  {
136  return static_cast<uint32_t>(attrib);
137  }
138  uint32_t getValue(const gsw_CcFontSize& attrib)
139  {
140  return static_cast<uint32_t>(attrib);
141  }
142  uint32_t getValue(const gsw_CcFontStyle& attrib)
143  {
144  // values based on cea-708
145  static const std::unordered_map<std::string, uint32_t> valuesMap{
146  {GSW_CC_FONT_STYLE_DEFAULT, 0},
147  {GSW_CC_FONT_STYLE_MONOSPACED_SERIF, 1},
148  {GSW_CC_FONT_STYLE_PROPORTIONAL_SERIF, 2},
149  {GSW_CC_FONT_STYLE_MONOSPACED_SANSSERIF, 3},
150  {GSW_CC_FONT_STYLE_PROPORTIONAL_SANSSERIF, 4},
151  {GSW_CC_FONT_STYLE_CASUAL, 5},
152  {GSW_CC_FONT_STYLE_CURSIVE, 6},
153  {GSW_CC_FONT_STYLE_SMALL_CAPITALS, 7},
154  };
155 
156  const auto it = valuesMap.find(attrib);
157  if(it != valuesMap.end())
158  return it->second;
159  else
160  {
161  logprintf("Cannot match %s to attribute value", attrib);
162  return 0; // value for default
163  }
164 
165  }
166  uint32_t getValue(const gsw_CcTextStyle& attrib)
167  {
168  return static_cast<uint32_t>(attrib);
169  }
170  uint32_t getValue(const gsw_CcBorderType& attrib)
171  {
172  return static_cast<uint32_t>(attrib);
173  }
174  uint32_t getValue(const gsw_CcEdgeType& attrib)
175  {
176  return static_cast<uint32_t>(attrib);
177  }
178  uint32_t getValue(const gsw_CcType& type)
179  {
180  return static_cast<uint32_t>(type);
181  }
182 
183  uint32_t getValue(gsw_CcAttributes * attrib, short type)
184  {
185  switch(type)
186  {
187  case GSW_CC_ATTRIB_BACKGROUND_COLOR:
188  return getValue(attrib->charBgColor);
189  case GSW_CC_ATTRIB_FONT_COLOR:
190  return getValue(attrib->charFgColor);
191  case GSW_CC_ATTRIB_WIN_COLOR:
192  return getValue(attrib->winColor);
193  case GSW_CC_ATTRIB_BACKGROUND_OPACITY:
194  return getValue(attrib->charBgOpacity);
195  case GSW_CC_ATTRIB_FONT_OPACITY:
196  return getValue(attrib->charFgOpacity);
197  case GSW_CC_ATTRIB_WIN_OPACITY:
198  return getValue(attrib->winOpacity);
199  case GSW_CC_ATTRIB_FONT_SIZE:
200  return getValue(attrib->fontSize);
201  case GSW_CC_ATTRIB_FONT_STYLE:
202  return getValue(attrib->fontStyle);
203  case GSW_CC_ATTRIB_FONT_ITALIC:
204  return getValue(attrib->fontItalic);
205  case GSW_CC_ATTRIB_FONT_UNDERLINE:
206  return getValue(attrib->fontUnderline);
207  case GSW_CC_ATTRIB_BORDER_TYPE:
208  return getValue(attrib->borderType);
209  case GSW_CC_ATTRIB_BORDER_COLOR:
210  return getValue(attrib->borderColor);
211  case GSW_CC_ATTRIB_EDGE_TYPE:
212  return getValue(attrib->edgeType);
213  case GSW_CC_ATTRIB_EDGE_COLOR:
214  return getValue(attrib->edgeColor);
215  default:
216  AAMPLOG_WARN("wrong attribute type used 0x%x",type);
217  return -1;
218 
219  }
220  }
221 
222  void setValue(gsw_CcAttributes* currentAttributes, gsw_CcAttributes * attrib, short type)
223  {
224  switch(type)
225  {
226  case GSW_CC_ATTRIB_BACKGROUND_COLOR:
227  currentAttributes->charBgColor = attrib->charBgColor;
228  return;
229  case GSW_CC_ATTRIB_FONT_COLOR:
230  currentAttributes->charFgColor = attrib->charFgColor;
231  return;
232  case GSW_CC_ATTRIB_WIN_COLOR:
233  currentAttributes->winColor = attrib->winColor;
234  return;
235  case GSW_CC_ATTRIB_BACKGROUND_OPACITY:
236  currentAttributes->charBgOpacity = attrib->charBgOpacity;
237  return;
238  case GSW_CC_ATTRIB_FONT_OPACITY:
239  currentAttributes->charFgOpacity = attrib->charFgOpacity;
240  return;
241  case GSW_CC_ATTRIB_WIN_OPACITY:
242  currentAttributes->winOpacity = attrib->winOpacity;
243  return;
244  case GSW_CC_ATTRIB_FONT_SIZE:
245  currentAttributes->fontSize = attrib->fontSize;
246  return;
247  case GSW_CC_ATTRIB_FONT_STYLE:
248  std::memcpy(currentAttributes->fontStyle, attrib->fontStyle, GSW_CC_MAX_FONT_NAME_LENGTH);
249  // currentAttributes->fontStyle = attrib->fontStyle;
250  return;
251  case GSW_CC_ATTRIB_FONT_ITALIC:
252  currentAttributes->fontItalic = attrib->fontItalic;
253  return;
254  case GSW_CC_ATTRIB_FONT_UNDERLINE:
255  currentAttributes->fontUnderline = attrib->fontUnderline;
256  return;
257  case GSW_CC_ATTRIB_BORDER_TYPE:
258  currentAttributes->borderType = attrib->borderType;
259  return;
260  case GSW_CC_ATTRIB_BORDER_COLOR:
261  currentAttributes->borderColor = attrib->borderColor;
262  return;
263  case GSW_CC_ATTRIB_EDGE_TYPE:
264  currentAttributes->edgeType = attrib->edgeType;
265  return;
266  case GSW_CC_ATTRIB_EDGE_COLOR:
267  currentAttributes->edgeColor = attrib->edgeColor;
268  return;
269  default:
270  logprintf("%s: wrong attribute type used 0x%x",__func__, type);
271  return;
272 
273  }
274  }
275 }
276 
277 void CCDataController::sendCCSetAttribute(gsw_CcAttributes * attrib, short type, gsw_CcType ccType)
278 {
279  using AttributesArray = std::array<uint32_t, 14> ;
280 
281  static constexpr AttributesArray masks = {
282  GSW_CC_ATTRIB_FONT_COLOR,
283  GSW_CC_ATTRIB_BACKGROUND_COLOR,
284  GSW_CC_ATTRIB_FONT_OPACITY,
285  GSW_CC_ATTRIB_BACKGROUND_OPACITY,
286  GSW_CC_ATTRIB_FONT_STYLE,
287  GSW_CC_ATTRIB_FONT_SIZE,
288  GSW_CC_ATTRIB_FONT_ITALIC,
289  GSW_CC_ATTRIB_FONT_UNDERLINE,
290  GSW_CC_ATTRIB_BORDER_TYPE,
291  GSW_CC_ATTRIB_BORDER_COLOR,
292  GSW_CC_ATTRIB_WIN_COLOR,
293  GSW_CC_ATTRIB_WIN_OPACITY,
294  GSW_CC_ATTRIB_EDGE_TYPE,
295  GSW_CC_ATTRIB_EDGE_COLOR,
296  };
297 
298  AttributesArray attributes{};
299 
300  for(int i = 0; i<attributes.size(); ++i)
301  {
302  if(type & (masks[i]))
303  {
304  setValue(&currentAttributes, attrib, masks[i]);
305  attributes[i] = getValue(attrib, masks[i]);
306  }
307  else
308  {
309  attributes[i] = 0;
310  }
311  }
312 
313  const auto ccTypeValue = getValue(ccType);
314 
315  channel.SendCCSetAttributePacket(ccTypeValue, uint32_t{type}, attributes);
316 }
317 
318 void closedCaptionDecodeCb(void *context, int decoderIndex, int event)
319 {
320  CCDataController* p = static_cast<CCDataController*>(context);
321  p->closedCaptionDecodeCb(decoderIndex, event);
322 }
323 
324 void closedCaptionDataCb (void *context, int decoderIndex, VL_CC_DATA_TYPE eType, unsigned char* ccData,
325  unsigned dataLength, int sequenceNumber, long long localPts)
326 {
327  CCDataController* p = static_cast<CCDataController*>(context);
328  p->closedCaptionDataCb(decoderIndex, eType, ccData, dataLength, sequenceNumber, localPts);
329 }
330 
331 
332 } // subtecConnector
gsw_CcAttributes::charFgColor
gsw_CcColor charFgColor
Definition: SubtecConnector.h:178
gsw_CcAttributes::charBgColor
gsw_CcColor charBgColor
Definition: SubtecConnector.h:177
gsw_CcTextStyle
gsw_CcTextStyle
Closed captioning text styles.
Definition: SubtecConnector.h:134
logprintf
void logprintf(const char *format,...)
Print logs to console / log fil.
Definition: aamplogging.cpp:432
gsw_CcAttributes::fontSize
gsw_CcFontSize fontSize
Definition: SubtecConnector.h:183
gsw_CcEdgeType
gsw_CcEdgeType
Font Edge type.
Definition: SubtecConnector.h:161
gsw_CcAttributes::winOpacity
gsw_CcOpacity winOpacity
Definition: SubtecConnector.h:182
gsw_CcAttributes
Definition: SubtecConnector.h:176
gsw_CcAttributes::charBgOpacity
gsw_CcOpacity charBgOpacity
Definition: SubtecConnector.h:180
gsw_CcFontSize
gsw_CcFontSize
Closed caption Fontsize.
Definition: SubtecConnector.h:79
gsw_CcAttributes::fontUnderline
gsw_CcTextStyle fontUnderline
Definition: SubtecConnector.h:186
gsw_CcAttributes::fontItalic
gsw_CcTextStyle fontItalic
Definition: SubtecConnector.h:185
gsw_CcAttributes::winColor
gsw_CcColor winColor
Definition: SubtecConnector.h:179
CCDataController.h
Impl of subtec communication layer.
gsw_CcOpacity
gsw_CcOpacity
Closed Captioning Opacity.
Definition: SubtecConnector.h:66
gsw_CcAttributes::edgeType
gsw_CcEdgeType edgeType
Definition: SubtecConnector.h:189
gsw_CcAttributes::edgeColor
gsw_CcColor edgeColor
Definition: SubtecConnector.h:190
gsw_CcAttributes::borderType
gsw_CcBorderType borderType
Definition: SubtecConnector.h:187
gsw_CcBorderType
gsw_CcBorderType
Window Border type.
Definition: SubtecConnector.h:146
priv_aamp.h
Private functions and types used internally by AAMP.
gsw_CcAttributes::charFgOpacity
gsw_CcOpacity charFgOpacity
Definition: SubtecConnector.h:181
gsw_CcType
gsw_CcType
Closed Captioning type.
Definition: SubtecConnector.h:55
gsw_CcAttributes::borderColor
gsw_CcColor borderColor
Definition: SubtecConnector.h:188
gsw_CcAttributes::fontStyle
gsw_CcFontStyle fontStyle
Definition: SubtecConnector.h:184
gsw_CcColor
Structure to hold color information for CC.
Definition: SubtecConnector.h:43