RDK Documentation (Open Sourced RDK Components)
SubtecPacket.hpp
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 2020 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 #pragma once
21 
22 #include <string>
23 #include <cstdint>
24 #include <memory>
25 #include <vector>
26 #include <array>
27 #include <limits>
28 #include <mutex>
29 #include "SubtecAttribute.hpp"
30 
31 class Packet
32 {
33 public:
34  Packet() : m_buffer(), m_counter(std::numeric_limits<std::uint32_t>::max()) {}
35  Packet(std::uint32_t counter) : m_buffer(), m_counter(counter) {}
36 
37  const uint32_t getType()
38  {
39  uint32_t type = 0;
40 
41  if (getBuffer().size() >= 4)
42  {
43  std::vector<std::uint8_t> buffer = getBuffer();
44  for (int i = 0; i < 4; i++)
45  {
46  type += (buffer[i] << (i*8)) & 0xFF;
47  }
48  }
49  return type;
50  }
51 
52  const std::vector<uint8_t>& getBytes()
53  {
54  return m_buffer;
55  }
56 
57  const std::uint32_t getCounter()
58  {
59  return m_counter;
60  }
61 
62  static std::string getTypeString(uint32_t type)
63  {
64  std::string ret;
65  PacketType pktType = static_cast<PacketType>(type);
66 
67  switch(pktType)
68  {
69  case PacketType::PES_DATA:
70  ret = "PES_DATA";
71  break;
72  case PacketType::TIMESTAMP:
73  ret = "TIMESTAMP";
74  break;
75  case PacketType::RESET_ALL:
76  ret = "RESET_ALL";
77  break;
78  case PacketType::RESET_CHANNEL:
79  ret = "RESET_CHANNEL";
80  break;
81  case PacketType::SUBTITLE_SELECTION:
82  ret = "SUBTITLE_SELECTION";
83  break;
84  case PacketType::TELETEXT_SELECTION:
85  ret = "TELETEXT_SELECTION";
86  break;
87  case PacketType::TTML_SELECTION:
88  ret = "TTML_SELECTION";
89  break;
90  case PacketType::TTML_DATA:
91  ret = "TTML_DATA";
92  break;
93  case PacketType::TTML_TIMESTAMP:
94  ret = "TTML_TIMESTAMP";
95  break;
96  case PacketType::WEBVTT_SELECTION:
97  ret = "WEBVTT_SELECTION";
98  break;
99  case PacketType::WEBVTT_DATA:
100  ret = "WEBVTT_DATA";
101  break;
102  case PacketType::WEBVTT_TIMESTAMP:
103  ret = "WEBVTT_TIMESTAMP";
104  break;
105  case PacketType::CC_DATA :
106  ret = "CC_DATA";
107  break;
108  case PacketType::PAUSE :
109  ret = "PAUSE";
110  break;
111  case PacketType::RESUME :
112  ret = "RESUME";
113  break;
114  case PacketType::MUTE :
115  ret = "MUTE";
116  break;
117  case PacketType::UNMUTE :
118  ret = "UNMUTE";
119  break;
120  case PacketType::CC_SET_ATTRIBUTE:
121  ret = "CC_SET_ATTRIBUTE";
122  break;
123  case PacketType::INVALID:
124  ret = "INVALID";
125  break;
126  default:
127  ret = "UNKNOWN";
128  break;
129  }
130 
131  return ret;
132  }
133 
134 
135 protected:
136  std::vector<uint8_t>& getBuffer() { return m_buffer; }
137 
138  enum class PacketType : std::uint32_t
139  {
140  ZERO,
141  PES_DATA,
142  TIMESTAMP,
143  RESET_ALL,
144  RESET_CHANNEL,
145  SUBTITLE_SELECTION,
146  TELETEXT_SELECTION,
147  TTML_SELECTION,
148  TTML_DATA,
149  TTML_TIMESTAMP,
150  CC_DATA,
151  PAUSE,
152  RESUME,
153  MUTE,
154  UNMUTE,
155  WEBVTT_SELECTION,
156  WEBVTT_DATA,
157  WEBVTT_TIMESTAMP,
158  CC_SET_ATTRIBUTE,
159 
160  INVALID = 0xFFFFFFFF,
161  };
162 
163  std::vector<uint8_t> m_buffer;
164  std::uint32_t m_counter;
165 
166  void append32(std::uint32_t value)
167  {
168  m_buffer.push_back((static_cast<std::uint8_t>((value >> 0)) & 0xFF));
169  m_buffer.push_back((static_cast<std::uint8_t>((value >> 8)) & 0xFF));
170  m_buffer.push_back((static_cast<std::uint8_t>((value >> 16)) & 0xFF));
171  m_buffer.push_back((static_cast<std::uint8_t>((value >> 24)) & 0xFF));
172  }
173 
174  void append64(std::int64_t value)
175  {
176  append32((static_cast<std::int32_t>((value >> 0)) & 0xFFFFFFFF));
177  append32((static_cast<std::int32_t>((value >> 32)) & 0xFFFFFFFF));
178  }
179 
180  void appendType(PacketType type)
181  {
182  append32(static_cast<std::underlying_type<PacketType>::type>(type));
183  }
184 };
185 
186 using PacketPtr = std::unique_ptr<Packet>;
187 
188 class DummyPacket : public Packet
189 {
190 public:
191  DummyPacket() : Packet() {}
192 };
193 
194 /**
195  * Pause packet.
196  */
197 class PausePacket : public Packet
198 {
199 public:
200 
201  /**
202  * Constructor.
203  *
204  * @param counter
205  * Packet counter.
206  */
207  PausePacket(std::uint32_t channelId,
208  std::uint32_t counter) : Packet(counter)
209  {
210  appendType(PacketType::PAUSE);
211  append32(counter);
212  append32(4);
213  append32(channelId);
214  }
215 };
216 
217 /**
218  * Resume packet.
219  */
220 class ResumePacket : public Packet
221 {
222 public:
223 
224  /**
225  * Constructor.
226  *
227  * @param counter
228  * Packet counter.
229  */
230  ResumePacket(std::uint32_t channelId,
231  std::uint32_t counter) : Packet(counter)
232  {
233  appendType(PacketType::RESUME);
234  append32(counter);
235  append32(4);
236  append32(channelId);
237  }
238 };
239 
240 /**
241  * Mute packet.
242  */
243 class MutePacket : public Packet
244 {
245 public:
246 
247  /**
248  * Constructor.
249  *
250  * @param counter
251  * Packet counter.
252  */
253  MutePacket(std::uint32_t channelId,
254  std::uint32_t counter) : Packet(counter)
255  {
256  appendType(PacketType::MUTE);
257  append32(counter);
258  append32(4);
259  append32(channelId);
260  }
261 };
262 
263 /**
264  * Mute packet.
265  */
266 class UnmutePacket : public Packet
267 {
268 public:
269 
270  /**
271  * Constructor.
272  *
273  * @param counter
274  * Packet counter.
275  */
276  UnmutePacket(std::uint32_t channelId,
277  std::uint32_t counter) : Packet(counter)
278  {
279  appendType(PacketType::UNMUTE);
280  append32(counter);
281  append32(4);
282  append32(channelId);
283  }
284 };
285 
286 /**
287  * Reset all data packet.
288  */
289 
290 class ResetAllPacket : public Packet
291 {
292 public:
293 
294  /**
295  * Constructor.
296  *
297  * @param counter
298  * Packet counter.
299  */
301  {
302  appendType(PacketType::RESET_ALL);
303  append32(0);
304  append32(0);
305  }
306 };
307 
308 /**
309  * Reset all data packet.
310  */
311 
313 {
314 public:
315 
316  /**
317  * Constructor.
318  *
319  * @param counter
320  * Packet counter.
321  */
322  ResetChannelPacket(std::uint32_t channelId,
323  std::uint32_t counter) : Packet(counter)
324  {
325  appendType(PacketType::RESET_CHANNEL);
326  append32(counter);
327  append32(4);
328  append32(channelId);
329  }
330 };
331 
332 
333 /*
334 
335 field size value description
336 type 4 18 message type
337 counter 4 0..n
338 size 4 68 specifies size of transferred "data" that includes channelId, ccType, attribType and attributes payload
339 
340 data:
341 
342 channelId 4 Specifies channel on which data for subtitles is transmitted.
343 ccType 4 {0,1} 0 - analog, 1 - digital
344 attribType 4 bitmask specifying which attribs are set
345 
346 attributes payload: -
347 1. FONT_COLOR 4 0..n
348 2. BACKGROUND_COLOR 4 0..n
349 3. FONT_OPACITY 4 0..n
350 4. BACKGROUND_OPACITY 4 0..n
351 5. FONT_STYLE 4 0..n
352 6. FONT_SIZE 4 0..n
353 7. FONT_ITALIC 4 0..n
354 8. FONT_UNDERLINE 4 0..n
355 9. BORDER_TYPE 4 0..n
356 10. BORDER_COLOR 4 0..n
357 11. WIN_COLOR 4 0..n
358 12. WIN_OPACITY 4 0..n
359 13. EDGE_TYPE 4 0..n
360 14. EDGE_COLOR 4 0..n
361 
362 When adding/removing an attribute to the above list, update the definition in the file SubtecAttribute.hpp
363 
364 */
365 
367 {
368 public:
369 
370  /**
371  * Constructor.
372  *
373  * @param counter
374  * Packet counter.
375  */
376  CCSetAttributePacket(std::uint32_t channelId,
377  std::uint32_t counter,
378  std::uint32_t ccType,
379  std::uint32_t attribType,
380  const attributesType &attributesValues) : Packet(counter)
381  {
382  appendType(PacketType::CC_SET_ATTRIBUTE);
383  append32(counter);
384  append32((NUMBER_OF_ATTRIBUTES+3)*4);
385  append32(channelId);
386  append32(ccType);
387  append32(attribType);
388 
389  for(const auto value : attributesValues)
390  append32(value);
391  }
392 };
ResetChannelPacket
Definition: SubtecPacket.hpp:312
SubtecAttribute.hpp
This file has been created to provide common definition related to Attribute.
CCSetAttributePacket
Definition: SubtecPacket.hpp:366
ResumePacket
Definition: SubtecPacket.hpp:220
PausePacket
Definition: SubtecPacket.hpp:197
MutePacket
Definition: SubtecPacket.hpp:243
MutePacket::MutePacket
MutePacket(std::uint32_t channelId, std::uint32_t counter)
Definition: SubtecPacket.hpp:253
ResetChannelPacket::ResetChannelPacket
ResetChannelPacket(std::uint32_t channelId, std::uint32_t counter)
Definition: SubtecPacket.hpp:322
PausePacket::PausePacket
PausePacket(std::uint32_t channelId, std::uint32_t counter)
Definition: SubtecPacket.hpp:207
CCSetAttributePacket::CCSetAttributePacket
CCSetAttributePacket(std::uint32_t channelId, std::uint32_t counter, std::uint32_t ccType, std::uint32_t attribType, const attributesType &attributesValues)
Definition: SubtecPacket.hpp:376
attributesType
std::array< uint32_t, 14 > attributesType
Definition: SubtecAttribute.hpp:30
ResetAllPacket::ResetAllPacket
ResetAllPacket()
Definition: SubtecPacket.hpp:300
UnmutePacket
Definition: SubtecPacket.hpp:266
ResumePacket::ResumePacket
ResumePacket(std::uint32_t channelId, std::uint32_t counter)
Definition: SubtecPacket.hpp:230
UnmutePacket::UnmutePacket
UnmutePacket(std::uint32_t channelId, std::uint32_t counter)
Definition: SubtecPacket.hpp:276
DummyPacket
Definition: SubtecPacket.hpp:188
ResetAllPacket
Definition: SubtecPacket.hpp:290
Packet
Definition: SubtecPacket.hpp:31