MistServer  2.5.3-Pro-19-gf5e75b1 ( Generic_64)
ts_packet.h
Go to the documentation of this file.
1 
4 #pragma once
5 #include <string>
6 #include <map>
7 #include <cmath>
8 #include <stdint.h>//for uint64_t
9 #include <iostream>
10 #include <cstdio>
11 #include <cstdlib>
12 #include <algorithm>
13 #include "dtsc.h"
14 #include "checksum.h"
15 
17 namespace TS {
18 
21  class Packet {
22  public:
23  //Constructors and fillers
24  Packet();
25  Packet(const Packet & rhs);
26  ~Packet();
27  bool FromPointer(const char * data);
28  bool FromFile(FILE * data);
29 
30  //Base properties
31  void setPID(int NewPID);
32  unsigned int getPID() const;
33  void setContinuityCounter(int NewContinuity);
34  int getContinuityCounter() const;
35  void setPCR(int64_t NewVal);
36  int64_t getPCR() const;
37  int64_t getOPCR() const;
38  void setAdaptationField(int NewVal);
39  int getAdaptationField() const;
40  int getAdaptationFieldLen() const;
41  unsigned int getTransportScramblingControl() const;
42 
43  //Flags
44  void setUnitStart(bool newVal);
45  bool getUnitStart() const;
46  void setRandomAccess(bool newVal);
47  bool getRandomAccess() const;
48 
49  void setDiscontinuity(bool newVal);
50  bool hasDiscontinuity() const;
51  bool hasPCR() const;
52  bool hasOPCR() const;
53  bool hasSplicingPoint() const;
54  bool hasTransportError() const;
55  bool hasPriority() const;
56  bool hasESpriority() const;
57 
58  //Helper functions
59  operator bool() const;
60  bool isPMT() const;
61  void clear();
62  void setDefaultPAT();
63  unsigned int getDataSize() const;
64 
65  unsigned int getBytesFree() const;
66  int fillFree(const char * PackageData, int maxLen);
67  void addStuffing();
68  void updPos(unsigned int newPos);
69 
70  //PES helpers
71  static std::string & getPESVideoLeadIn(unsigned int len, unsigned long long PTS, unsigned long long offset, bool isAligned);
72  static std::string & getPESAudioLeadIn(unsigned int len, unsigned long long PTS);
73 
74  //Printers and writers
75  std::string toPrettyString(size_t indent = 0, int detailLevel = 3) const;
76  const char * getPayload() const;
77  int getPayloadLength() const;
78  const char * checkAndGetBuffer() const;
79 
80  protected:
81  char strBuf[189];
82  unsigned int pos;
83  };
84 
86  public:
87  ProgramAssociationTable & operator = (const Packet & rhs);
88  char getOffset() const;
89  char getTableId() const;
90  short getSectionLength() const;
91  short getTransportStreamId() const;
92  char getVersionNumber() const;
93  bool getCurrentNextIndicator() const;
94  char getSectionNumber() const;
95  char getLastSectionNumber() const;
96  short getProgramCount() const;
97  short getProgramNumber(short index) const;
98  short getProgramPID(short index) const;
99  int getCRC() const;
100  std::string toPrettyString(size_t indent) const;
101  };
102 
104  public:
105  ProgramMappingEntry(char * begin, char * end);
106 
107  operator bool() const;
108 
109  int getStreamType() const;
110  void setStreamType(int newType);
111  std::string getCodec() const;
112  std::string getStreamTypeString() const;
113  int getElementaryPid() const;
114  void setElementaryPid(int newElementaryPid);
115  int getESInfoLength() const;
116  const char * getESInfo() const;
117  void setESInfo(const std::string & newInfo);
118  void advance();
119  private:
120  char* data;
121  char* boundary;
122  };
123 
124  class ProgramMappingTable : public Packet {
125  public:
127  ProgramMappingTable & operator = (const Packet & rhs);
128  char getOffset() const;
129  void setOffset(char newVal);
130  char getTableId() const;
131  void setTableId(char newVal);
132  short getSectionLength() const;
133  void setSectionLength(short newVal);
134  short getProgramNumber() const;
135  void setProgramNumber(short newVal);
136  char getVersionNumber() const;
137  void setVersionNumber(char newVal);
138  bool getCurrentNextIndicator() const;
139  void setCurrentNextIndicator(bool newVal);
140  char getSectionNumber() const;
141  void setSectionNumber(char newVal);
142  char getLastSectionNumber() const;
143  void setLastSectionNumber(char newVal);
144  short getPCRPID() const;
145  void setPCRPID(short newVal);
146  short getProgramInfoLength() const;
147  void setProgramInfoLength(short newVal);
148  ProgramMappingEntry getEntry(int index) const;
149  int getCRC() const;
150  void calcCRC();
151  std::string toPrettyString(size_t indent) const;
152  };
153 
159  static inline std::string getAudioHeader(int FrameLen, std::string initData) {
160  char StandardHeader[7] = {0xFF, 0xF1, 0x00, 0x00, 0x00, 0x1F, 0xFC};
161  FrameLen += 7;
162  StandardHeader[2] = ((((initData[0] >> 3) - 1) << 6) & 0xC0); //AAC Profile - 1 ( First two bits )
163  StandardHeader[2] |= ((((initData[0] & 0x07) << 1) | ((initData[1] >> 7) & 0x01)) << 2); //AAC Frequency Index
164  StandardHeader[2] |= ((initData[1] & 0x20) >> 5); //AAC Channel Config
165  StandardHeader[3] = ((initData[1] & 0x18) << 3); //AAC Channel Config (cont.)
166  StandardHeader[3] |= ((FrameLen & 0x00001800) >> 11);
167  StandardHeader[4] = ((FrameLen & 0x000007F8) >> 3);
168  StandardHeader[5] |= ((FrameLen & 0x00000007) << 5);
169  return std::string(StandardHeader, 7);
170  }
171 
172 
175  //0x47 = sync byte
176  //0x4000 = transport error(1) = 0, payload unit start(1) = 1, priority(1) = 0, PID(13) = 0
177  //0x10 = transportscrambling(2) = 0, adaptation(2) = 1, continuity(4) = 0
178  //0x00 = pointer = 0
179  //0x00 = table ID = 0 = PAT
180  //0xB00D = section syntax(1) = 1, 0(1)=0, reserved(2) = 3, section_len(12) = 13
181  //0x0001 = transport stream id = 1
182  //0xC1 = reserved(2) = 3, version(5)=0, curr_next_indi(1) = 1
183  //0x00 = section_number = 0
184  //0x00 = last_section_no = 0
185  //0x0001 = ProgNo = 1
186  //0xF000 = reserved(3) = 7, network pid = 4096
187  //0x2AB104B2 = CRC32
188  static char PAT[188] = {0x47, 0x40, 0x00, 0x10, 0x00, 0x00, 0xB0, 0x0D, 0x00, 0x01, 0xC1, 0x00, 0x00, 0x00, 0x01, 0xF0, 0x00, 0x2A, 0xB1, 0x04,
189  0xB2, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
190  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
191  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
192  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
193  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
194  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
195  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
196  };
197 
198  const char * createPMT(std::set<unsigned long>& selectedTracks, DTSC::Meta& myMeta, int contCounter=0);
199 
200 } //TS namespace
201 
Definition: ts_packet.h:124
Class for storage of meta data.
Definition: dtsc.h:337
int getAdaptationFieldLen() const
Gets the current length of the adaptationfield.
Definition: ts_packet.cpp:215
unsigned int pos
Definition: ts_packet.h:82
Definition: ts_packet.h:103
bool hasSplicingPoint() const
Gets the value of the splicing point flag.
Definition: ts_packet.cpp:379
void setPID(int NewPID)
Sets the PID of a single Packet.
Definition: ts_packet.cpp:74
Definition: ts_packet.h:85
void setUnitStart(bool newVal)
Sets the start of a new unit in this Packet.
Definition: ts_packet.cpp:316
char strBuf[189]
Definition: ts_packet.h:81
void setDiscontinuity(bool newVal)
Definition: ts_packet.cpp:334
static std::string getAudioHeader(int FrameLen, std::string initData)
Constructs an audio header to be used on each audio frame.
Definition: ts_packet.h:159
void setAdaptationField(int NewVal)
Sets the selection value for an adaptationfield of a Packet.
Definition: ts_packet.cpp:124
static std::string & getPESAudioLeadIn(unsigned int len, unsigned long long PTS)
Generates a PES Lead-in for an audio frame.
Definition: ts_packet.cpp:475
char * boundary
Definition: ts_packet.h:121
bool hasPriority() const
Gets the transport priority of a Packet.
Definition: ts_packet.cpp:203
bool FromPointer(const char *data)
This funtion fills a Packet from a char array.
Definition: ts_packet.cpp:55
unsigned int getDataSize() const
Definition: ts_packet.cpp:304
const char * getPayload() const
returns the character buffer with a std::string wrapper
Definition: ts_packet.cpp:561
void addStuffing()
Adds stuffing to the Packet depending on how much content you want to send.
Definition: ts_packet.cpp:504
void setContinuityCounter(int NewContinuity)
Sets the Continuity Counter of a single Packet.
Definition: ts_packet.cpp:88
unsigned int getBytesFree() const
Gets the amount of bytes that are not written yet in a Packet.
Definition: ts_packet.cpp:101
bool hasESpriority() const
Gets the elementary stream priority indicator of a Packet.
Definition: ts_packet.cpp:326
Holds all TS processing related code.
Definition: ts_packet.cpp:20
void setDefaultPAT()
Transforms the Packet into a standard Program Association Table.
Definition: ts_packet.cpp:408
const char * checkAndGetBuffer() const
Checks the size of the internal packet buffer (prints error if size !=188), then returns a pointer to...
Definition: ts_packet.cpp:418
bool FromFile(FILE *data)
This function fills a Packet from a file.
Definition: ts_packet.cpp:37
void setPCR(int64_t NewVal)
Sets the PCR (Program Clock Reference) of a Packet.
Definition: ts_packet.cpp:143
Class for reading and writing TS Streams.
Definition: ts_packet.h:21
void clear()
Sets the packet pos to 4, and resets the first 4 fields to defaults (including sync byte on pos 0) ...
Definition: ts_packet.cpp:110
void setRandomAccess(bool newVal)
Gets the value of the transport private data point flag.
Definition: ts_packet.cpp:385
void updPos(unsigned int newPos)
update position in character array (pos),
Definition: ts_packet.cpp:66
bool hasPCR() const
Gets the value of the PCR flag.
Definition: ts_packet.cpp:367
bool hasTransportError() const
Gets the transport error inficator of a Packet.
Definition: ts_packet.cpp:191
static char PAT[188]
A standard Program Association Table, as generated by FFMPEG.
Definition: ts_packet.h:188
~Packet()
The deconstructor deletes all space that may be occupied by a Packet.
Definition: ts_packet.cpp:62
static std::string & getPESVideoLeadIn(unsigned int len, unsigned long long PTS, unsigned long long offset, bool isAligned)
Generates a PES Lead-in for a video frame.
Definition: ts_packet.cpp:449
char * data
Definition: ts_packet.h:120
int getContinuityCounter() const
Gets the Continuity Counter of a single Packet.
Definition: ts_packet.cpp:95
std::string toPrettyString(size_t indent=0, int detailLevel=3) const
Prints a packet to stdout, for analyser purposes.
Definition: ts_packet.cpp:227
bool getUnitStart() const
Gets the payload unit start inficator of a Packet.
Definition: ts_packet.cpp:197
bool hasOPCR() const
Gets the value of the OPCR flag.
Definition: ts_packet.cpp:373
bool getRandomAccess() const
Gets whether this Packet can be accessed at random (indicates keyframe).
Definition: ts_packet.cpp:358
int64_t getOPCR() const
Gets the OPCR (Original Program Clock Reference) of a Packet.
Definition: ts_packet.cpp:175
int getAdaptationField() const
Gets whether a Packet contains an adaptationfield.
Definition: ts_packet.cpp:137
unsigned int getTransportScramblingControl() const
Gets the transport scrambling control of a Packet.
Definition: ts_packet.cpp:209
bool hasDiscontinuity() const
Definition: ts_packet.cpp:330
const char * createPMT(std::set< unsigned long > &selectedTracks, DTSC::Meta &myMeta, int contCounter)
Construct a PMT (special 188B ts packet) from a set of selected tracks and metadata.
Definition: ts_packet.cpp:967
bool isPMT() const
Returns true if this PID contains a PMT.
Definition: ts_packet.cpp:310
int fillFree(const char *PackageData, int maxLen)
Fills the free bytes of the Packet.
Definition: ts_packet.cpp:494
unsigned int getPID() const
Gets the PID of a single Packet.
Definition: ts_packet.cpp:82
int getPayloadLength() const
Gets the length of the payload for this apcket.
Definition: ts_packet.cpp:567
Packet()
This constructor creates an empty Packet, ready for use for either reading or writing.
Definition: ts_packet.cpp:23
int64_t getPCR() const
Gets the PCR (Program Clock Reference) of a Packet.
Definition: ts_packet.cpp:160